event-notifier: implement `lttng_trigger_needs_tracer_notifier()` function
[lttng-tools.git] / src / common / mi-lttng.c
1 /*
2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
3 * Copyright (C) 2014 Olivier Cotte <olivier.cotte@polymtl.ca>
4 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #include "lttng/tracker.h"
11 #define _LGPL_SOURCE
12 #include "mi-lttng.h"
13 #include <common/config/session-config.h>
14 #include <common/defaults.h>
15 #include <common/tracker.h>
16 #include <lttng/channel.h>
17 #include <lttng/snapshot-internal.h>
18
19 #include <assert.h>
20
21 #define MI_SCHEMA_MAJOR_VERSION 4
22 #define MI_SCHEMA_MINOR_VERSION 0
23
24 /* Machine interface namespace URI */
25 LTTNG_HIDDEN const char * const mi_lttng_xmlns = "xmlns";
26 LTTNG_HIDDEN const char * const mi_lttng_xmlns_xsi = "xmlns:xsi";
27 LTTNG_HIDDEN const char * const mi_lttng_w3_schema_uri = "http://www.w3.org/2001/XMLSchema-instance";
28 LTTNG_HIDDEN const char * const mi_lttng_schema_location = "xsi:schemaLocation";
29 LTTNG_HIDDEN const char * const mi_lttng_schema_location_uri =
30 DEFAULT_LTTNG_MI_NAMESPACE " "
31 "https://lttng.org/xml/schemas/lttng-mi/" XSTR(MI_SCHEMA_MAJOR_VERSION)
32 "/lttng-mi-" XSTR(MI_SCHEMA_MAJOR_VERSION) "."
33 XSTR(MI_SCHEMA_MINOR_VERSION) ".xsd";
34 LTTNG_HIDDEN const char * const mi_lttng_schema_version = "schemaVersion";
35 LTTNG_HIDDEN const char * const mi_lttng_schema_version_value = XSTR(MI_SCHEMA_MAJOR_VERSION)
36 "." XSTR(MI_SCHEMA_MINOR_VERSION);
37
38 /* Strings related to command */
39 const char * const mi_lttng_element_command = "command";
40 const char * const mi_lttng_element_command_action = "snapshot_action";
41 const char * const mi_lttng_element_command_add_context = "add-context";
42 const char * const mi_lttng_element_command_create = "create";
43 const char * const mi_lttng_element_command_destroy = "destroy";
44 const char * const mi_lttng_element_command_disable_channel = "disable-channel";
45 const char * const mi_lttng_element_command_disable_event = "disable-event";
46 const char * const mi_lttng_element_command_enable_channels = "enable-channel";
47 const char * const mi_lttng_element_command_enable_event = "enable-event";
48 const char * const mi_lttng_element_command_list = "list";
49 const char * const mi_lttng_element_command_load = "load";
50 LTTNG_HIDDEN const char * const mi_lttng_element_command_metadata = "metadata";
51 LTTNG_HIDDEN const char * const mi_lttng_element_command_metadata_action = "metadata_action";
52 LTTNG_HIDDEN const char * const mi_lttng_element_command_regenerate = "regenerate";
53 LTTNG_HIDDEN const char * const mi_lttng_element_command_regenerate_action = "regenerate_action";
54 const char * const mi_lttng_element_command_name = "name";
55 const char * const mi_lttng_element_command_output = "output";
56 const char * const mi_lttng_element_command_save = "save";
57 const char * const mi_lttng_element_command_set_session = "set-session";
58 const char * const mi_lttng_element_command_snapshot = "snapshot";
59 const char * const mi_lttng_element_command_snapshot_add = "add_snapshot";
60 const char * const mi_lttng_element_command_snapshot_del = "del_snapshot";
61 const char * const mi_lttng_element_command_snapshot_list = "list_snapshot";
62 const char * const mi_lttng_element_command_snapshot_record = "record_snapshot";
63 const char * const mi_lttng_element_command_start = "start";
64 const char * const mi_lttng_element_command_stop = "stop";
65 const char * const mi_lttng_element_command_success = "success";
66 const char * const mi_lttng_element_command_track = "track";
67 const char * const mi_lttng_element_command_untrack = "untrack";
68 const char * const mi_lttng_element_command_version = "version";
69 LTTNG_HIDDEN const char * const mi_lttng_element_command_rotate = "rotate";
70 LTTNG_HIDDEN const char * const mi_lttng_element_command_enable_rotation = "enable-rotation";
71 LTTNG_HIDDEN const char * const mi_lttng_element_command_disable_rotation = "disable-rotation";
72 LTTNG_HIDDEN const char * const mi_lttng_element_command_clear = "clear";
73
74 /* Strings related to version command */
75 const char * const mi_lttng_element_version = "version";
76 const char * const mi_lttng_element_version_commit = "commit";
77 const char * const mi_lttng_element_version_description = "description";
78 const char * const mi_lttng_element_version_license = "license";
79 const char * const mi_lttng_element_version_major = "major";
80 const char * const mi_lttng_element_version_minor = "minor";
81 const char * const mi_lttng_element_version_patch_level = "patchLevel";
82 const char * const mi_lttng_element_version_str = "string";
83 const char * const mi_lttng_element_version_web = "url";
84
85 /* String related to a lttng_event_field */
86 const char * const mi_lttng_element_event_field = "event_field";
87 const char * const mi_lttng_element_event_fields = "event_fields";
88
89 /* String related to lttng_event_perf_counter_ctx */
90 const char * const mi_lttng_element_perf_counter_context = "perf";
91
92 /* Strings related to pid */
93 const char * const mi_lttng_element_pid_id = "id";
94
95 /* Strings related to save command */
96 const char * const mi_lttng_element_save = "save";
97
98 /* Strings related to load command */
99 const char * const mi_lttng_element_load = "load";
100 LTTNG_HIDDEN const char * const mi_lttng_element_load_overrides = "overrides";
101 LTTNG_HIDDEN const char * const mi_lttng_element_load_override_url = "url";
102
103 /* General elements of mi_lttng */
104 const char * const mi_lttng_element_empty = "";
105 const char * const mi_lttng_element_id = "id";
106 const char * const mi_lttng_element_nowrite = "nowrite";
107 const char * const mi_lttng_element_success = "success";
108 const char * const mi_lttng_element_type_enum = "ENUM";
109 const char * const mi_lttng_element_type_float = "FLOAT";
110 const char * const mi_lttng_element_type_integer = "INTEGER";
111 const char * const mi_lttng_element_type_other = "OTHER";
112 const char * const mi_lttng_element_type_string = "STRING";
113
114 /* String related to loglevel */
115 const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
116 const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
117 const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
118 const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
119 const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
120 const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
121 const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
122 const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
123 const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
124 const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
125 const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
126 const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
127 const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
128 const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
129 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
130 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
131
132 /* String related to loglevel JUL */
133 const char * const mi_lttng_loglevel_str_jul_all = "JUL_ALL";
134 const char * const mi_lttng_loglevel_str_jul_config = "JUL_CONFIG";
135 const char * const mi_lttng_loglevel_str_jul_fine = "JUL_FINE";
136 const char * const mi_lttng_loglevel_str_jul_finer = "JUL_FINER";
137 const char * const mi_lttng_loglevel_str_jul_finest = "JUL_FINEST";
138 const char * const mi_lttng_loglevel_str_jul_info = "JUL_INFO";
139 const char * const mi_lttng_loglevel_str_jul_off = "JUL_OFF";
140 const char * const mi_lttng_loglevel_str_jul_severe = "JUL_SEVERE";
141 const char * const mi_lttng_loglevel_str_jul_warning = "JUL_WARNING";
142
143 /* String related to loglevel LOG4J */
144 const char * const mi_lttng_loglevel_str_log4j_off = "LOG4J_OFF";
145 const char * const mi_lttng_loglevel_str_log4j_fatal = "LOG4J_FATAL";
146 const char * const mi_lttng_loglevel_str_log4j_error = "LOG4J_ERROR";
147 const char * const mi_lttng_loglevel_str_log4j_warn = "LOG4J_WARN";
148 const char * const mi_lttng_loglevel_str_log4j_info = "LOG4J_INFO";
149 const char * const mi_lttng_loglevel_str_log4j_debug = "LOG4J_DEBUG";
150 const char * const mi_lttng_loglevel_str_log4j_trace = "LOG4J_TRACE";
151 const char * const mi_lttng_loglevel_str_log4j_all = "LOG4J_ALL";
152
153 /* String related to loglevel Python */
154 const char * const mi_lttng_loglevel_str_python_critical = "PYTHON_CRITICAL";
155 const char * const mi_lttng_loglevel_str_python_error = "PYTHON_ERROR";
156 const char * const mi_lttng_loglevel_str_python_warning = "PYTHON_WARNING";
157 const char * const mi_lttng_loglevel_str_python_info = "PYTHON_INFO";
158 const char * const mi_lttng_loglevel_str_python_debug = "PYTHON_DEBUG";
159 const char * const mi_lttng_loglevel_str_python_notset = "PYTHON_NOTSET";
160
161 /* String related to loglevel type */
162 const char * const mi_lttng_loglevel_type_all = "ALL";
163 const char * const mi_lttng_loglevel_type_range = "RANGE";
164 const char * const mi_lttng_loglevel_type_single = "SINGLE";
165 const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
166
167 /* String related to a lttng_snapshot_output */
168 const char * const mi_lttng_element_snapshot_ctrl_url = "ctrl_url";
169 const char * const mi_lttng_element_snapshot_data_url = "data_url";
170 const char * const mi_lttng_element_snapshot_max_size = "max_size";
171 const char * const mi_lttng_element_snapshot_n_ptr = "n_ptr";
172 const char * const mi_lttng_element_snapshot_session_name = "session_name";
173 const char * const mi_lttng_element_snapshots = "snapshots";
174
175 /* String related to track/untrack command */
176 const char * const mi_lttng_element_track_untrack_all_wildcard = "*";
177
178 LTTNG_HIDDEN const char * const mi_lttng_element_session_name = "session_name";
179
180 /* String related to rotate command */
181 LTTNG_HIDDEN const char * const mi_lttng_element_rotation = "rotation";
182 LTTNG_HIDDEN const char * const mi_lttng_element_rotate_status = "status";
183 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule = "rotation_schedule";
184 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedules = "rotation_schedules";
185 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_result = "rotation_schedule_result";
186 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_results = "rotation_schedule_results";
187 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_periodic = "periodic";
188 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_periodic_time_us = "time_us";
189 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_size_threshold = "size_threshold";
190 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_schedule_size_threshold_bytes = "bytes";
191 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_state = "state";
192 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location = "location";
193 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_local = "local";
194 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_local_absolute_path = "absolute_path";
195 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay = "relay";
196 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay_host = "host";
197 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay_control_port = "control_port";
198 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay_data_port = "data_port";
199 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay_protocol = "protocol";
200 LTTNG_HIDDEN const char * const mi_lttng_element_rotation_location_relay_relative_path = "relative_path";
201
202 /* String related to enum lttng_rotation_state */
203 LTTNG_HIDDEN const char * const mi_lttng_rotation_state_str_ongoing = "ONGOING";
204 LTTNG_HIDDEN const char * const mi_lttng_rotation_state_str_completed = "COMPLETED";
205 LTTNG_HIDDEN const char * const mi_lttng_rotation_state_str_expired = "EXPIRED";
206 LTTNG_HIDDEN const char * const mi_lttng_rotation_state_str_error = "ERROR";
207
208 /* String related to enum lttng_trace_archive_location_relay_protocol_type */
209 LTTNG_HIDDEN const char * const mi_lttng_rotation_location_relay_protocol_str_tcp = "TCP";
210
211 /* String related to add-context command */
212 LTTNG_HIDDEN const char * const mi_lttng_element_context_symbol = "symbol";
213
214 /* Deprecated symbols preserved for ABI compatibility. */
215 const char * const mi_lttng_context_type_perf_counter;
216 const char * const mi_lttng_context_type_perf_cpu_counter;
217 const char * const mi_lttng_context_type_perf_thread_counter;
218 const char * const mi_lttng_element_track_untrack_pid_target;
219 const char * const mi_lttng_element_track_untrack_targets;
220 const char * const mi_lttng_element_calibrate;
221 const char * const mi_lttng_element_calibrate_function;
222 const char * const mi_lttng_element_command_calibrate;
223
224 /* This is a merge of jul loglevel and regular loglevel
225 * Those should never overlap by definition
226 * (see struct lttng_event loglevel)
227 */
228 LTTNG_HIDDEN
229 const char *mi_lttng_loglevel_string(int value, enum lttng_domain_type domain)
230 {
231 switch (domain) {
232 case LTTNG_DOMAIN_KERNEL:
233 case LTTNG_DOMAIN_UST:
234 switch (value) {
235 case -1:
236 return mi_lttng_element_empty;
237 case LTTNG_LOGLEVEL_EMERG:
238 return mi_lttng_loglevel_str_emerg;
239 case LTTNG_LOGLEVEL_ALERT:
240 return mi_lttng_loglevel_str_alert;
241 case LTTNG_LOGLEVEL_CRIT:
242 return mi_lttng_loglevel_str_crit;
243 case LTTNG_LOGLEVEL_ERR:
244 return mi_lttng_loglevel_str_err;
245 case LTTNG_LOGLEVEL_WARNING:
246 return mi_lttng_loglevel_str_warning;
247 case LTTNG_LOGLEVEL_NOTICE:
248 return mi_lttng_loglevel_str_notice;
249 case LTTNG_LOGLEVEL_INFO:
250 return mi_lttng_loglevel_str_info;
251 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
252 return mi_lttng_loglevel_str_debug_system;
253 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
254 return mi_lttng_loglevel_str_debug_program;
255 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
256 return mi_lttng_loglevel_str_debug_process;
257 case LTTNG_LOGLEVEL_DEBUG_MODULE:
258 return mi_lttng_loglevel_str_debug_module;
259 case LTTNG_LOGLEVEL_DEBUG_UNIT:
260 return mi_lttng_loglevel_str_debug_unit;
261 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
262 return mi_lttng_loglevel_str_debug_function;
263 case LTTNG_LOGLEVEL_DEBUG_LINE:
264 return mi_lttng_loglevel_str_debug_line;
265 case LTTNG_LOGLEVEL_DEBUG:
266 return mi_lttng_loglevel_str_debug;
267 default:
268 return mi_lttng_loglevel_str_unknown;
269 }
270 break;
271 case LTTNG_DOMAIN_LOG4J:
272 switch (value) {
273 case -1:
274 return mi_lttng_element_empty;
275 case LTTNG_LOGLEVEL_LOG4J_OFF:
276 return mi_lttng_loglevel_str_log4j_off;
277 case LTTNG_LOGLEVEL_LOG4J_FATAL:
278 return mi_lttng_loglevel_str_log4j_fatal;
279 case LTTNG_LOGLEVEL_LOG4J_ERROR:
280 return mi_lttng_loglevel_str_log4j_error;
281 case LTTNG_LOGLEVEL_LOG4J_WARN:
282 return mi_lttng_loglevel_str_log4j_warn;
283 case LTTNG_LOGLEVEL_LOG4J_INFO:
284 return mi_lttng_loglevel_str_log4j_info;
285 case LTTNG_LOGLEVEL_LOG4J_DEBUG:
286 return mi_lttng_loglevel_str_log4j_debug;
287 case LTTNG_LOGLEVEL_LOG4J_TRACE:
288 return mi_lttng_loglevel_str_log4j_trace;
289 case LTTNG_LOGLEVEL_LOG4J_ALL:
290 return mi_lttng_loglevel_str_log4j_all;
291 default:
292 return mi_lttng_loglevel_str_unknown;
293 }
294 break;
295 case LTTNG_DOMAIN_JUL:
296 switch (value) {
297 case -1:
298 return mi_lttng_element_empty;
299 case LTTNG_LOGLEVEL_JUL_OFF:
300 return mi_lttng_loglevel_str_jul_off;
301 case LTTNG_LOGLEVEL_JUL_SEVERE:
302 return mi_lttng_loglevel_str_jul_severe;
303 case LTTNG_LOGLEVEL_JUL_WARNING:
304 return mi_lttng_loglevel_str_jul_warning;
305 case LTTNG_LOGLEVEL_JUL_INFO:
306 return mi_lttng_loglevel_str_jul_info;
307 case LTTNG_LOGLEVEL_JUL_CONFIG:
308 return mi_lttng_loglevel_str_jul_config;
309 case LTTNG_LOGLEVEL_JUL_FINE:
310 return mi_lttng_loglevel_str_jul_fine;
311 case LTTNG_LOGLEVEL_JUL_FINER:
312 return mi_lttng_loglevel_str_jul_finer;
313 case LTTNG_LOGLEVEL_JUL_FINEST:
314 return mi_lttng_loglevel_str_jul_finest;
315 case LTTNG_LOGLEVEL_JUL_ALL:
316 return mi_lttng_loglevel_str_jul_all;
317 default:
318 return mi_lttng_loglevel_str_unknown;
319 }
320 break;
321 case LTTNG_DOMAIN_PYTHON:
322 switch (value) {
323 case LTTNG_LOGLEVEL_PYTHON_CRITICAL:
324 return mi_lttng_loglevel_str_python_critical;
325 case LTTNG_LOGLEVEL_PYTHON_ERROR:
326 return mi_lttng_loglevel_str_python_error;
327 case LTTNG_LOGLEVEL_PYTHON_WARNING:
328 return mi_lttng_loglevel_str_python_warning;
329 case LTTNG_LOGLEVEL_PYTHON_INFO:
330 return mi_lttng_loglevel_str_python_info;
331 case LTTNG_LOGLEVEL_PYTHON_DEBUG:
332 return mi_lttng_loglevel_str_python_debug;
333 case LTTNG_LOGLEVEL_PYTHON_NOTSET:
334 return mi_lttng_loglevel_str_python_notset;
335 default:
336 return mi_lttng_loglevel_str_unknown;
337 }
338 break;
339 default:
340 return mi_lttng_loglevel_str_unknown;
341 }
342 }
343
344 LTTNG_HIDDEN
345 const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
346 {
347 switch (value) {
348 case LTTNG_EVENT_LOGLEVEL_ALL:
349 return mi_lttng_loglevel_type_all;
350 case LTTNG_EVENT_LOGLEVEL_RANGE:
351 return mi_lttng_loglevel_type_range;
352 case LTTNG_EVENT_LOGLEVEL_SINGLE:
353 return mi_lttng_loglevel_type_single;
354 default:
355 return mi_lttng_loglevel_type_unknown;
356 }
357 }
358
359 static
360 const char *mi_lttng_eventtype_string(enum lttng_event_type value)
361 {
362 switch (value) {
363 case LTTNG_EVENT_ALL:
364 return config_event_type_all;
365 case LTTNG_EVENT_TRACEPOINT:
366 return config_event_type_tracepoint;
367 case LTTNG_EVENT_PROBE:
368 return config_event_type_probe;
369 case LTTNG_EVENT_USERSPACE_PROBE:
370 return config_event_type_userspace_probe;
371 case LTTNG_EVENT_FUNCTION:
372 return config_event_type_function;
373 case LTTNG_EVENT_FUNCTION_ENTRY:
374 return config_event_type_function_entry;
375 case LTTNG_EVENT_SYSCALL:
376 return config_event_type_syscall;
377 case LTTNG_EVENT_NOOP:
378 return config_event_type_noop;
379 default:
380 return mi_lttng_element_empty;
381 }
382 }
383
384 static
385 const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
386 {
387 switch (val) {
388 case LTTNG_EVENT_CONTEXT_PID:
389 return config_event_context_pid;
390 case LTTNG_EVENT_CONTEXT_PROCNAME:
391 return config_event_context_procname;
392 case LTTNG_EVENT_CONTEXT_PRIO:
393 return config_event_context_prio;
394 case LTTNG_EVENT_CONTEXT_NICE:
395 return config_event_context_nice;
396 case LTTNG_EVENT_CONTEXT_VPID:
397 return config_event_context_vpid;
398 case LTTNG_EVENT_CONTEXT_TID:
399 return config_event_context_tid;
400 case LTTNG_EVENT_CONTEXT_VTID:
401 return config_event_context_vtid;
402 case LTTNG_EVENT_CONTEXT_PPID:
403 return config_event_context_ppid;
404 case LTTNG_EVENT_CONTEXT_VPPID:
405 return config_event_context_vppid;
406 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
407 return config_event_context_pthread_id;
408 case LTTNG_EVENT_CONTEXT_HOSTNAME:
409 return config_event_context_hostname;
410 case LTTNG_EVENT_CONTEXT_IP:
411 return config_event_context_ip;
412 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
413 return config_event_context_interruptible;
414 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
415 return config_event_context_preemptible;
416 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
417 return config_event_context_need_reschedule;
418 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
419 return config_event_context_migratable;
420 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER:
421 return config_event_context_callstack_user;
422 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL:
423 return config_event_context_callstack_kernel;
424 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
425 return config_event_context_cgroup_ns;
426 case LTTNG_EVENT_CONTEXT_IPC_NS:
427 return config_event_context_ipc_ns;
428 case LTTNG_EVENT_CONTEXT_MNT_NS:
429 return config_event_context_mnt_ns;
430 case LTTNG_EVENT_CONTEXT_NET_NS:
431 return config_event_context_net_ns;
432 case LTTNG_EVENT_CONTEXT_PID_NS:
433 return config_event_context_pid_ns;
434 case LTTNG_EVENT_CONTEXT_TIME_NS:
435 return config_event_context_time_ns;
436 case LTTNG_EVENT_CONTEXT_USER_NS:
437 return config_event_context_user_ns;
438 case LTTNG_EVENT_CONTEXT_UTS_NS:
439 return config_event_context_uts_ns;
440 case LTTNG_EVENT_CONTEXT_UID:
441 return config_event_context_uid;
442 case LTTNG_EVENT_CONTEXT_EUID:
443 return config_event_context_euid;
444 case LTTNG_EVENT_CONTEXT_SUID:
445 return config_event_context_suid;
446 case LTTNG_EVENT_CONTEXT_GID:
447 return config_event_context_gid;
448 case LTTNG_EVENT_CONTEXT_EGID:
449 return config_event_context_egid;
450 case LTTNG_EVENT_CONTEXT_SGID:
451 return config_event_context_sgid;
452 case LTTNG_EVENT_CONTEXT_VUID:
453 return config_event_context_vuid;
454 case LTTNG_EVENT_CONTEXT_VEUID:
455 return config_event_context_veuid;
456 case LTTNG_EVENT_CONTEXT_VSUID:
457 return config_event_context_vsuid;
458 case LTTNG_EVENT_CONTEXT_VGID:
459 return config_event_context_vgid;
460 case LTTNG_EVENT_CONTEXT_VEGID:
461 return config_event_context_vegid;
462 case LTTNG_EVENT_CONTEXT_VSGID:
463 return config_event_context_vsgid;
464 default:
465 return NULL;
466 }
467 }
468
469 LTTNG_HIDDEN
470 const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
471 {
472 switch (val) {
473 case(LTTNG_EVENT_FIELD_INTEGER):
474 return mi_lttng_element_type_integer;
475 case(LTTNG_EVENT_FIELD_ENUM):
476 return mi_lttng_element_type_enum;
477 case(LTTNG_EVENT_FIELD_FLOAT):
478 return mi_lttng_element_type_float;
479 case(LTTNG_EVENT_FIELD_STRING):
480 return mi_lttng_element_type_string;
481 default:
482 return mi_lttng_element_type_other;
483 }
484 }
485
486 LTTNG_HIDDEN
487 const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
488 {
489 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
490 switch (value) {
491 case LTTNG_DOMAIN_KERNEL:
492 return config_domain_type_kernel;
493 case LTTNG_DOMAIN_UST:
494 return config_domain_type_ust;
495 case LTTNG_DOMAIN_JUL:
496 return config_domain_type_jul;
497 case LTTNG_DOMAIN_LOG4J:
498 return config_domain_type_log4j;
499 case LTTNG_DOMAIN_PYTHON:
500 return config_domain_type_python;
501 default:
502 /* Should not have an unknown domain */
503 assert(0);
504 return NULL;
505 }
506 }
507
508 LTTNG_HIDDEN
509 const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
510 {
511 switch (value) {
512 case LTTNG_BUFFER_PER_PID:
513 return config_buffer_type_per_pid;
514 case LTTNG_BUFFER_PER_UID:
515 return config_buffer_type_per_uid;
516 case LTTNG_BUFFER_GLOBAL:
517 return config_buffer_type_global;
518 default:
519 /* Should not have an unknow buffer type */
520 assert(0);
521 return NULL;
522 }
523 }
524
525 LTTNG_HIDDEN
526 const char *mi_lttng_rotation_state_string(enum lttng_rotation_state value)
527 {
528 switch (value) {
529 case LTTNG_ROTATION_STATE_ONGOING:
530 return mi_lttng_rotation_state_str_ongoing;
531 case LTTNG_ROTATION_STATE_COMPLETED:
532 return mi_lttng_rotation_state_str_completed;
533 case LTTNG_ROTATION_STATE_EXPIRED:
534 return mi_lttng_rotation_state_str_expired;
535 case LTTNG_ROTATION_STATE_ERROR:
536 return mi_lttng_rotation_state_str_error;
537 default:
538 /* Should not have an unknow rotation state. */
539 assert(0);
540 return NULL;
541 }
542 }
543
544 LTTNG_HIDDEN
545 const char *mi_lttng_trace_archive_location_relay_protocol_type_string(
546 enum lttng_trace_archive_location_relay_protocol_type value)
547 {
548 switch (value) {
549 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
550 return mi_lttng_rotation_location_relay_protocol_str_tcp;
551 default:
552 /* Should not have an unknow relay protocol. */
553 assert(0);
554 return NULL;
555 }
556 }
557
558 LTTNG_HIDDEN
559 struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
560 {
561 struct mi_writer *mi_writer;
562
563 mi_writer = zmalloc(sizeof(struct mi_writer));
564 if (!mi_writer) {
565 PERROR("zmalloc mi_writer_create");
566 goto end;
567 }
568 if (mi_output_type == LTTNG_MI_XML) {
569 mi_writer->writer = config_writer_create(fd_output, 0);
570 if (!mi_writer->writer) {
571 goto err_destroy;
572 }
573 mi_writer->type = LTTNG_MI_XML;
574 } else {
575 goto err_destroy;
576 }
577
578 end:
579 return mi_writer;
580
581 err_destroy:
582 free(mi_writer);
583 return NULL;
584 }
585
586 LTTNG_HIDDEN
587 int mi_lttng_writer_destroy(struct mi_writer *writer)
588 {
589 int ret;
590
591 if (!writer) {
592 ret = -EINVAL;
593 goto end;
594 }
595
596 ret = config_writer_destroy(writer->writer);
597 if (ret < 0) {
598 goto end;
599 }
600
601 free(writer);
602 end:
603 return ret;
604 }
605
606 LTTNG_HIDDEN
607 int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
608 {
609 int ret;
610
611 /*
612 * A command is always the MI's root node, it must declare the current
613 * namespace and schema URIs and the schema's version.
614 */
615 ret = config_writer_open_element(writer->writer,
616 mi_lttng_element_command);
617 if (ret) {
618 goto end;
619 }
620
621 ret = config_writer_write_attribute(writer->writer,
622 mi_lttng_xmlns, DEFAULT_LTTNG_MI_NAMESPACE);
623 if (ret) {
624 goto end;
625 }
626
627 ret = config_writer_write_attribute(writer->writer,
628 mi_lttng_xmlns_xsi, mi_lttng_w3_schema_uri);
629 if (ret) {
630 goto end;
631 }
632
633 ret = config_writer_write_attribute(writer->writer,
634 mi_lttng_schema_location,
635 mi_lttng_schema_location_uri);
636 if (ret) {
637 goto end;
638 }
639
640 ret = config_writer_write_attribute(writer->writer,
641 mi_lttng_schema_version,
642 mi_lttng_schema_version_value);
643 if (ret) {
644 goto end;
645 }
646
647 ret = mi_lttng_writer_write_element_string(writer,
648 mi_lttng_element_command_name, command);
649 end:
650 return ret;
651 }
652
653 LTTNG_HIDDEN
654 int mi_lttng_writer_command_close(struct mi_writer *writer)
655 {
656 return mi_lttng_writer_close_element(writer);
657 }
658
659 LTTNG_HIDDEN
660 int mi_lttng_writer_open_element(struct mi_writer *writer,
661 const char *element_name)
662 {
663 return config_writer_open_element(writer->writer, element_name);
664 }
665
666 LTTNG_HIDDEN
667 int mi_lttng_writer_close_element(struct mi_writer *writer)
668 {
669 return config_writer_close_element(writer->writer);
670 }
671
672 LTTNG_HIDDEN
673 int mi_lttng_close_multi_element(struct mi_writer *writer,
674 unsigned int nb_element)
675 {
676 int ret, i;
677
678 if (nb_element < 1) {
679 ret = 0;
680 goto end;
681 }
682 for (i = 0; i < nb_element; i++) {
683 ret = mi_lttng_writer_close_element(writer);
684 if (ret) {
685 goto end;
686 }
687 }
688 end:
689 return ret;
690 }
691
692 LTTNG_HIDDEN
693 int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
694 const char *element_name, uint64_t value)
695 {
696 return config_writer_write_element_unsigned_int(writer->writer,
697 element_name, value);
698 }
699
700 LTTNG_HIDDEN
701 int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
702 const char *element_name, int64_t value)
703 {
704 return config_writer_write_element_signed_int(writer->writer,
705 element_name, value);
706 }
707
708 LTTNG_HIDDEN
709 int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
710 const char *element_name, int value)
711 {
712 return config_writer_write_element_bool(writer->writer,
713 element_name, value);
714 }
715
716 LTTNG_HIDDEN
717 int mi_lttng_writer_write_element_string(struct mi_writer *writer,
718 const char *element_name, const char *value)
719 {
720 return config_writer_write_element_string(writer->writer,
721 element_name, value);
722 }
723
724 LTTNG_HIDDEN
725 int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
726 const char *lttng_description, const char *lttng_license)
727 {
728 int ret;
729
730 /* Open version */
731 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
732 if (ret) {
733 goto end;
734 }
735
736 /* Version string (contain info like rc etc.) */
737 ret = mi_lttng_writer_write_element_string(writer,
738 mi_lttng_element_version_str, version->version);
739 if (ret) {
740 goto end;
741 }
742
743 /* Major version number */
744 ret = mi_lttng_writer_write_element_unsigned_int(writer,
745 mi_lttng_element_version_major, version->version_major);
746 if (ret) {
747 goto end;
748 }
749
750 /* Minor version number */
751 ret = mi_lttng_writer_write_element_unsigned_int(writer,
752 mi_lttng_element_version_minor, version->version_minor);
753 if (ret) {
754 goto end;
755 }
756
757 /* Commit version number */
758 ret = mi_lttng_writer_write_element_string(writer,
759 mi_lttng_element_version_commit, version->version_commit);
760 if (ret) {
761 goto end;
762 }
763
764 /* Patch number */
765 ret = mi_lttng_writer_write_element_unsigned_int(writer,
766 mi_lttng_element_version_patch_level, version->version_patchlevel);
767 if (ret) {
768 goto end;
769 }
770
771 /* Name of the version */
772 ret = mi_lttng_writer_write_element_string(writer,
773 config_element_name, version->version_name);
774 if (ret) {
775 goto end;
776 }
777
778 /* Description mostly related to beer... */
779 ret = mi_lttng_writer_write_element_string(writer,
780 mi_lttng_element_version_description, lttng_description);
781 if (ret) {
782 goto end;
783 }
784
785 /* url */
786 ret = mi_lttng_writer_write_element_string(writer,
787 mi_lttng_element_version_web, version->package_url);
788 if (ret) {
789 goto end;
790 }
791
792 /* License: free as in free beer...no...*speech* */
793 ret = mi_lttng_writer_write_element_string(writer,
794 mi_lttng_element_version_license, lttng_license);
795 if (ret) {
796 goto end;
797 }
798
799 /* Close version element */
800 ret = mi_lttng_writer_close_element(writer);
801
802 end:
803 return ret;
804 }
805
806 LTTNG_HIDDEN
807 int mi_lttng_sessions_open(struct mi_writer *writer)
808 {
809 return mi_lttng_writer_open_element(writer, config_element_sessions);
810 }
811
812 LTTNG_HIDDEN
813 int mi_lttng_session(struct mi_writer *writer,
814 struct lttng_session *session, int is_open)
815 {
816 int ret;
817
818 assert(session);
819
820 /* Open sessions element */
821 ret = mi_lttng_writer_open_element(writer,
822 config_element_session);
823 if (ret) {
824 goto end;
825 }
826
827 /* Name of the session */
828 ret = mi_lttng_writer_write_element_string(writer,
829 config_element_name, session->name);
830 if (ret) {
831 goto end;
832 }
833
834 /* Path */
835 ret = mi_lttng_writer_write_element_string(writer,
836 config_element_path, session->path);
837 if (ret) {
838 goto end;
839 }
840
841 /* Enabled ? */
842 ret = mi_lttng_writer_write_element_bool(writer,
843 config_element_enabled, session->enabled);
844 if (ret) {
845 goto end;
846 }
847
848 /* Snapshot mode */
849 ret = mi_lttng_writer_write_element_unsigned_int(writer,
850 config_element_snapshot_mode, session->snapshot_mode);
851 if (ret) {
852 goto end;
853 }
854
855 /* Live timer interval in usec */
856 ret = mi_lttng_writer_write_element_unsigned_int(writer,
857 config_element_live_timer_interval,
858 session->live_timer_interval);
859 if (ret) {
860 goto end;
861 }
862
863 if (!is_open) {
864 /* Closing session element */
865 ret = mi_lttng_writer_close_element(writer);
866 }
867 end:
868 return ret;
869
870 }
871
872 LTTNG_HIDDEN
873 int mi_lttng_domains_open(struct mi_writer *writer)
874 {
875 return mi_lttng_writer_open_element(writer, config_element_domains);
876 }
877
878 LTTNG_HIDDEN
879 int mi_lttng_domain(struct mi_writer *writer,
880 struct lttng_domain *domain, int is_open)
881 {
882 int ret = 0;
883 const char *str_domain;
884 const char *str_buffer;
885
886 assert(domain);
887
888 /* Open domain element */
889 ret = mi_lttng_writer_open_element(writer, config_element_domain);
890 if (ret) {
891 goto end;
892 }
893
894 /* Domain Type */
895 str_domain = mi_lttng_domaintype_string(domain->type);
896 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
897 str_domain);
898 if (ret) {
899 goto end;
900 }
901
902 /* Buffer Type */
903 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
904 ret = mi_lttng_writer_write_element_string(writer,
905 config_element_buffer_type, str_buffer);
906 if (ret) {
907 goto end;
908 }
909
910 /* TODO: union attr
911 * This union is not currently used and was added for
912 * future ust domain support.
913 * Date: 25-06-2014
914 * */
915
916 if (!is_open) {
917 /* Closing domain element */
918 ret = mi_lttng_writer_close_element(writer);
919 }
920
921 end:
922 return ret;
923
924 }
925
926 LTTNG_HIDDEN
927 int mi_lttng_channels_open(struct mi_writer *writer)
928 {
929 return mi_lttng_writer_open_element(writer, config_element_channels);
930 }
931
932 LTTNG_HIDDEN
933 int mi_lttng_channel(struct mi_writer *writer,
934 struct lttng_channel *channel, int is_open)
935 {
936 int ret = 0;
937
938 assert(channel);
939
940 /* Opening channel element */
941 ret = mi_lttng_writer_open_element(writer, config_element_channel);
942 if (ret) {
943 goto end;
944 }
945
946 /* Name */
947 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
948 channel->name);
949 if (ret) {
950 goto end;
951 }
952
953 /* Enabled ? */
954 ret = mi_lttng_writer_write_element_bool(writer,
955 config_element_enabled, channel->enabled);
956 if (ret) {
957 goto end;
958 }
959
960 /* Attribute */
961 ret = mi_lttng_channel_attr(writer, &channel->attr);
962 if (ret) {
963 goto end;
964 }
965
966 if (!is_open) {
967 /* Closing channel element */
968 ret = mi_lttng_writer_close_element(writer);
969 if (ret) {
970 goto end;
971 }
972 }
973 end:
974 return ret;
975 }
976
977 LTTNG_HIDDEN
978 int mi_lttng_channel_attr(struct mi_writer *writer,
979 struct lttng_channel_attr *attr)
980 {
981 int ret = 0;
982 struct lttng_channel *chan = caa_container_of(attr,
983 struct lttng_channel, attr);
984 uint64_t discarded_events, lost_packets, monitor_timer_interval;
985 int64_t blocking_timeout;
986
987 assert(attr);
988
989 ret = lttng_channel_get_discarded_event_count(chan, &discarded_events);
990 if (ret) {
991 goto end;
992 }
993
994 ret = lttng_channel_get_lost_packet_count(chan, &lost_packets);
995 if (ret) {
996 goto end;
997 }
998
999 ret = lttng_channel_get_monitor_timer_interval(chan,
1000 &monitor_timer_interval);
1001 if (ret) {
1002 goto end;
1003 }
1004
1005 ret = lttng_channel_get_blocking_timeout(chan,
1006 &blocking_timeout);
1007 if (ret) {
1008 goto end;
1009 }
1010
1011 /* Opening Attributes */
1012 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1013 if (ret) {
1014 goto end;
1015 }
1016
1017 /* Overwrite */
1018 ret = mi_lttng_writer_write_element_string(writer,
1019 config_element_overwrite_mode,
1020 attr->overwrite ? config_overwrite_mode_overwrite :
1021 config_overwrite_mode_discard);
1022 if (ret) {
1023 goto end;
1024 }
1025
1026 /* Sub buffer size in byte */
1027 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1028 config_element_subbuf_size, attr->subbuf_size);
1029 if (ret) {
1030 goto end;
1031 }
1032
1033 /* Number of subbuffer (power of two) */
1034 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1035 config_element_num_subbuf,
1036 attr->num_subbuf);
1037 if (ret) {
1038 goto end;
1039 }
1040
1041 /* Switch timer interval in usec */
1042 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1043 config_element_switch_timer_interval,
1044 attr->switch_timer_interval);
1045 if (ret) {
1046 goto end;
1047 }
1048
1049 /* Read timer interval in usec */
1050 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1051 config_element_read_timer_interval,
1052 attr->read_timer_interval);
1053 if (ret) {
1054 goto end;
1055 }
1056
1057 /* Monitor timer interval in usec */
1058 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1059 config_element_monitor_timer_interval,
1060 monitor_timer_interval);
1061 if (ret) {
1062 goto end;
1063 }
1064
1065 /* Retry timeout in usec */
1066 ret = mi_lttng_writer_write_element_signed_int(writer,
1067 config_element_blocking_timeout,
1068 blocking_timeout);
1069 if (ret) {
1070 goto end;
1071 }
1072
1073 /* Event output */
1074 ret = mi_lttng_writer_write_element_string(writer,
1075 config_element_output_type,
1076 attr->output == LTTNG_EVENT_SPLICE ?
1077 config_output_type_splice : config_output_type_mmap);
1078 if (ret) {
1079 goto end;
1080 }
1081
1082 /* Tracefile size in bytes */
1083 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1084 config_element_tracefile_size, attr->tracefile_size);
1085 if (ret) {
1086 goto end;
1087 }
1088
1089 /* Count of tracefiles */
1090 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1091 config_element_tracefile_count,
1092 attr->tracefile_count);
1093 if (ret) {
1094 goto end;
1095 }
1096
1097 /* Live timer interval in usec*/
1098 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1099 config_element_live_timer_interval,
1100 attr->live_timer_interval);
1101 if (ret) {
1102 goto end;
1103 }
1104
1105 /* Discarded events */
1106 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1107 config_element_discarded_events,
1108 discarded_events);
1109 if (ret) {
1110 goto end;
1111 }
1112
1113 /* Lost packets */
1114 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1115 config_element_lost_packets,
1116 lost_packets);
1117 if (ret) {
1118 goto end;
1119 }
1120
1121 /* Closing attributes */
1122 ret = mi_lttng_writer_close_element(writer);
1123 if (ret) {
1124 goto end;
1125 }
1126 end:
1127 return ret;
1128
1129 }
1130
1131 LTTNG_HIDDEN
1132 int mi_lttng_event_common_attributes(struct mi_writer *writer,
1133 struct lttng_event *event)
1134 {
1135 int ret;
1136 const char *filter_expression;
1137
1138 /* Open event element */
1139 ret = mi_lttng_writer_open_element(writer, config_element_event);
1140 if (ret) {
1141 goto end;
1142 }
1143
1144 /* Event name */
1145 ret = mi_lttng_writer_write_element_string(writer,
1146 config_element_name, event->name);
1147 if (ret) {
1148 goto end;
1149 }
1150
1151 /* Event type */
1152 ret = mi_lttng_writer_write_element_string(writer,
1153 config_element_type, mi_lttng_eventtype_string(event->type));
1154 if (ret) {
1155 goto end;
1156 }
1157
1158 /* Is event enabled */
1159 ret = mi_lttng_writer_write_element_bool(writer,
1160 config_element_enabled, event->enabled);
1161 if (ret) {
1162 goto end;
1163 }
1164
1165 /* Event filter expression */
1166 ret = lttng_event_get_filter_expression(event, &filter_expression);
1167 if (ret) {
1168 goto end;
1169 }
1170
1171 if (filter_expression) {
1172 ret = mi_lttng_writer_write_element_string(writer,
1173 config_element_filter_expression,
1174 filter_expression);
1175 if (ret) {
1176 goto end;
1177 }
1178 }
1179
1180 end:
1181 return ret;
1182 }
1183
1184 static int write_event_exclusions(struct mi_writer *writer,
1185 struct lttng_event *event)
1186 {
1187 int i;
1188 int ret;
1189 int exclusion_count;
1190
1191 /* Open event exclusions */
1192 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
1193 if (ret) {
1194 goto end;
1195 }
1196
1197 exclusion_count = lttng_event_get_exclusion_name_count(event);
1198 if (exclusion_count < 0) {
1199 ret = exclusion_count;
1200 goto end;
1201 }
1202
1203 for (i = 0; i < exclusion_count; i++) {
1204 const char *name;
1205
1206 ret = lttng_event_get_exclusion_name(event, i, &name);
1207 if (ret) {
1208 /* Close exclusions */
1209 mi_lttng_writer_close_element(writer);
1210 goto end;
1211 }
1212
1213 ret = mi_lttng_writer_write_element_string(writer,
1214 config_element_exclusion, name);
1215 if (ret) {
1216 /* Close exclusions */
1217 mi_lttng_writer_close_element(writer);
1218 goto end;
1219 }
1220 }
1221
1222 /* Close exclusions */
1223 ret = mi_lttng_writer_close_element(writer);
1224
1225 end:
1226 return ret;
1227 }
1228
1229 LTTNG_HIDDEN
1230 int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
1231 struct lttng_event *event, enum lttng_domain_type domain)
1232 {
1233 int ret;
1234
1235 /* Event loglevel */
1236 ret = mi_lttng_writer_write_element_string(writer,
1237 config_element_loglevel,
1238 mi_lttng_loglevel_string(event->loglevel, domain));
1239 if (ret) {
1240 goto end;
1241 }
1242
1243 /* Log level type */
1244 ret = mi_lttng_writer_write_element_string(writer,
1245 config_element_loglevel_type,
1246 mi_lttng_logleveltype_string(event->loglevel_type));
1247 if (ret) {
1248 goto end;
1249 }
1250
1251 /* Event exclusions */
1252 ret = write_event_exclusions(writer, event);
1253
1254 end:
1255 return ret;
1256 }
1257
1258 LTTNG_HIDDEN
1259 int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
1260 struct lttng_event *event)
1261 {
1262 /* event exclusion filter */
1263 return write_event_exclusions(writer, event);
1264 }
1265
1266 LTTNG_HIDDEN
1267 int mi_lttng_event_function_probe(struct mi_writer *writer,
1268 struct lttng_event *event)
1269 {
1270 int ret;
1271
1272 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1273 if (ret) {
1274 goto end;
1275 }
1276
1277 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1278 if (ret) {
1279 goto end;
1280 }
1281
1282 if (event->attr.probe.addr != 0) {
1283 /* event probe address */
1284 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1285 config_element_address, event->attr.probe.addr);
1286 if (ret) {
1287 goto end;
1288 }
1289 } else {
1290 /* event probe offset */
1291 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1292 config_element_offset, event->attr.probe.offset);
1293 if (ret) {
1294 goto end;
1295 }
1296
1297 /* event probe symbol_name */
1298 ret = mi_lttng_writer_write_element_string(writer,
1299 config_element_symbol_name, event->attr.probe.symbol_name);
1300 if (ret) {
1301 goto end;
1302 }
1303 }
1304
1305 /* Close probe_attributes and attributes */
1306 ret = mi_lttng_close_multi_element(writer, 2);
1307 end:
1308 return ret;
1309 }
1310
1311 static
1312 int mi_lttng_event_userspace_probe(struct mi_writer *writer,
1313 struct lttng_event *event)
1314 {
1315 int ret;
1316 const struct lttng_userspace_probe_location *location;
1317 const struct lttng_userspace_probe_location_lookup_method *lookup_method;
1318 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
1319
1320 location = lttng_event_get_userspace_probe_location(event);
1321 if (!location) {
1322 ret = -LTTNG_ERR_INVALID;
1323 goto end;
1324 }
1325
1326 lookup_method = lttng_userspace_probe_location_get_lookup_method(location);
1327 if (!lookup_method) {
1328 ret = -LTTNG_ERR_INVALID;
1329 goto end;
1330 }
1331
1332 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
1333
1334 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1335 if (ret) {
1336 goto end;
1337 }
1338
1339 switch (lttng_userspace_probe_location_get_type(location)) {
1340 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1341 {
1342 const char *function_name;
1343 const char *binary_path;
1344
1345 ret = mi_lttng_writer_open_element(writer,
1346 config_element_userspace_probe_function_attributes);
1347 if (ret) {
1348 goto end;
1349 }
1350
1351 switch (lookup_type) {
1352 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
1353 ret = mi_lttng_writer_write_element_string(writer,
1354 config_element_userspace_probe_lookup,
1355 config_element_userspace_probe_lookup_function_elf);
1356 if (ret) {
1357 goto end;
1358 }
1359 break;
1360 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
1361 ret = mi_lttng_writer_write_element_string(writer,
1362 config_element_userspace_probe_lookup,
1363 config_element_userspace_probe_lookup_function_default);
1364 if (ret) {
1365 goto end;
1366 }
1367 break;
1368 default:
1369 goto end;
1370 }
1371
1372 binary_path = lttng_userspace_probe_location_function_get_binary_path(location);
1373 ret = mi_lttng_writer_write_element_string(writer,
1374 config_element_userspace_probe_location_binary_path, binary_path);
1375 if (ret) {
1376 goto end;
1377 }
1378
1379 function_name = lttng_userspace_probe_location_function_get_function_name(location);
1380 ret = mi_lttng_writer_write_element_string(writer,
1381 config_element_userspace_probe_function_location_function_name,
1382 function_name);
1383 if (ret) {
1384 goto end;
1385 }
1386
1387 break;
1388 }
1389 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1390 {
1391 const char *probe_name, *provider_name;
1392 const char *binary_path;
1393
1394 ret = mi_lttng_writer_open_element(writer,
1395 config_element_userspace_probe_function_attributes);
1396 if (ret) {
1397 goto end;
1398 }
1399
1400 switch (lookup_type) {
1401 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
1402 ret = mi_lttng_writer_write_element_string(writer,
1403 config_element_userspace_probe_lookup,
1404 config_element_userspace_probe_lookup_tracepoint_sdt);
1405 if (ret) {
1406 goto end;
1407 }
1408 break;
1409 default:
1410 goto end;
1411 }
1412
1413 binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(location);
1414 ret = mi_lttng_writer_write_element_string(writer,
1415 config_element_userspace_probe_location_binary_path,
1416 binary_path);
1417 if (ret) {
1418 goto end;
1419 }
1420
1421 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
1422 ret = mi_lttng_writer_write_element_string(writer,
1423 config_element_userspace_probe_tracepoint_location_provider_name,
1424 provider_name);
1425 if (ret) {
1426 goto end;
1427 }
1428
1429 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
1430 ret = mi_lttng_writer_write_element_string(writer,
1431 config_element_userspace_probe_tracepoint_location_probe_name, probe_name);
1432 if (ret) {
1433 goto end;
1434 }
1435 break;
1436 }
1437 default:
1438 ERR("Invalid probe type encountered");
1439 }
1440 /* Close probe_attributes and attributes */
1441 ret = mi_lttng_close_multi_element(writer, 2);
1442 end:
1443 return ret;
1444 }
1445
1446 LTTNG_HIDDEN
1447 int mi_lttng_event_function_entry(struct mi_writer *writer,
1448 struct lttng_event *event)
1449 {
1450 int ret;
1451
1452 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1453 if (ret) {
1454 goto end;
1455 }
1456
1457 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1458 if (ret) {
1459 goto end;
1460 }
1461
1462 /* event probe symbol_name */
1463 ret = mi_lttng_writer_write_element_string(writer,
1464 config_element_symbol_name, event->attr.ftrace.symbol_name);
1465 if (ret) {
1466 goto end;
1467 }
1468
1469 /* Close function_attributes and attributes */
1470 ret = mi_lttng_close_multi_element(writer, 2);
1471 end:
1472 return ret;
1473 }
1474
1475 LTTNG_HIDDEN
1476 int mi_lttng_events_open(struct mi_writer *writer)
1477 {
1478 return mi_lttng_writer_open_element(writer, config_element_events);
1479 }
1480
1481 LTTNG_HIDDEN
1482 int mi_lttng_event(struct mi_writer *writer,
1483 struct lttng_event *event, int is_open, enum lttng_domain_type domain)
1484 {
1485 int ret;
1486
1487 ret = mi_lttng_event_common_attributes(writer, event);
1488 if (ret) {
1489 goto end;
1490 }
1491
1492 switch (event->type) {
1493 case LTTNG_EVENT_TRACEPOINT:
1494 {
1495 if (event->loglevel != -1) {
1496 ret = mi_lttng_event_tracepoint_loglevel(writer, event, domain);
1497 } else {
1498 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
1499 }
1500 break;
1501 }
1502 case LTTNG_EVENT_FUNCTION:
1503 /* Fallthrough */
1504 case LTTNG_EVENT_PROBE:
1505 ret = mi_lttng_event_function_probe(writer, event);
1506 break;
1507 case LTTNG_EVENT_FUNCTION_ENTRY:
1508 ret = mi_lttng_event_function_entry(writer, event);
1509 break;
1510 case LTTNG_EVENT_USERSPACE_PROBE:
1511 ret = mi_lttng_event_userspace_probe(writer, event);
1512 break;
1513 case LTTNG_EVENT_ALL:
1514 /* Fallthrough */
1515 default:
1516 break;
1517 }
1518
1519 if (ret) {
1520 goto end;
1521 }
1522
1523 if (!is_open) {
1524 ret = mi_lttng_writer_close_element(writer);
1525 }
1526
1527 end:
1528 return ret;
1529 }
1530
1531 LTTNG_HIDDEN
1532 int mi_lttng_trackers_open(struct mi_writer *writer)
1533 {
1534 return mi_lttng_writer_open_element(
1535 writer, config_element_process_attr_trackers);
1536 }
1537
1538 static int get_tracker_elements(enum lttng_process_attr process_attr,
1539 const char **element_process_attr_tracker,
1540 const char **element_process_attr_value)
1541 {
1542 int ret = 0;
1543
1544 switch (process_attr) {
1545 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1546 *element_process_attr_tracker =
1547 config_element_process_attr_tracker_pid;
1548 *element_process_attr_value =
1549 config_element_process_attr_pid_value;
1550 break;
1551 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1552 *element_process_attr_tracker =
1553 config_element_process_attr_tracker_vpid;
1554 *element_process_attr_value =
1555 config_element_process_attr_vpid_value;
1556 break;
1557 case LTTNG_PROCESS_ATTR_USER_ID:
1558 *element_process_attr_tracker =
1559 config_element_process_attr_tracker_uid;
1560 *element_process_attr_value =
1561 config_element_process_attr_uid_value;
1562 break;
1563 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1564 *element_process_attr_tracker =
1565 config_element_process_attr_tracker_vuid;
1566 *element_process_attr_value =
1567 config_element_process_attr_vuid_value;
1568 break;
1569 case LTTNG_PROCESS_ATTR_GROUP_ID:
1570 *element_process_attr_tracker =
1571 config_element_process_attr_tracker_gid;
1572 *element_process_attr_value =
1573 config_element_process_attr_gid_value;
1574 break;
1575 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1576 *element_process_attr_tracker =
1577 config_element_process_attr_tracker_vgid;
1578 *element_process_attr_value =
1579 config_element_process_attr_vgid_value;
1580 break;
1581 default:
1582 ret = LTTNG_ERR_SAVE_IO_FAIL;
1583 }
1584 return ret;
1585 }
1586
1587 LTTNG_HIDDEN
1588 int mi_lttng_process_attribute_tracker_open(
1589 struct mi_writer *writer, enum lttng_process_attr process_attr)
1590 {
1591 int ret;
1592 const char *element_tracker, *element_value;
1593
1594 ret = get_tracker_elements(
1595 process_attr, &element_tracker, &element_value);
1596 if (ret) {
1597 return ret;
1598 }
1599
1600 /* Open process attribute tracker element */
1601 ret = mi_lttng_writer_open_element(writer, element_tracker);
1602 if (ret) {
1603 goto end;
1604 }
1605
1606 /* Open values element */
1607 ret = mi_lttng_process_attr_values_open(writer);
1608 end:
1609 return ret;
1610 }
1611
1612 LTTNG_HIDDEN
1613 int mi_lttng_pids_open(struct mi_writer *writer)
1614 {
1615 return mi_lttng_writer_open_element(writer, config_element_pids);
1616 }
1617
1618 /*
1619 * TODO: move the listing of pid for user agent to process semantic on
1620 * mi api bump. The use of process element break the mi api.
1621 */
1622 LTTNG_HIDDEN
1623 int mi_lttng_pid(struct mi_writer *writer,
1624 pid_t pid,
1625 const char *name,
1626 int is_open)
1627 {
1628 int ret;
1629
1630 /* Open pid process */
1631 ret = mi_lttng_writer_open_element(writer, config_element_pid);
1632 if (ret) {
1633 goto end;
1634 }
1635
1636 /* Writing pid number */
1637 ret = mi_lttng_writer_write_element_signed_int(writer,
1638 mi_lttng_element_pid_id, (int)pid);
1639 if (ret) {
1640 goto end;
1641 }
1642
1643 /* Writing name of the process */
1644 if (name) {
1645 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1646 name);
1647 if (ret) {
1648 goto end;
1649 }
1650 }
1651
1652 if (!is_open) {
1653 /* Closing Pid */
1654 ret = mi_lttng_writer_close_element(writer);
1655 }
1656
1657 end:
1658 return ret;
1659 }
1660
1661 LTTNG_HIDDEN
1662 int mi_lttng_process_attr_values_open(struct mi_writer *writer)
1663 {
1664 return mi_lttng_writer_open_element(
1665 writer, config_element_process_attr_values);
1666 }
1667
1668 LTTNG_HIDDEN
1669 int mi_lttng_all_process_attribute_value(struct mi_writer *writer,
1670 enum lttng_process_attr process_attr,
1671 bool is_open)
1672 {
1673 int ret;
1674 const char *element_id_tracker, *element_target_id;
1675
1676 ret = get_tracker_elements(
1677 process_attr, &element_id_tracker, &element_target_id);
1678 if (ret) {
1679 return ret;
1680 }
1681
1682 ret = mi_lttng_writer_open_element(writer, element_target_id);
1683 if (ret) {
1684 goto end;
1685 }
1686
1687 ret = mi_lttng_writer_open_element(writer, config_element_type);
1688 if (ret) {
1689 goto end;
1690 }
1691
1692 ret = mi_lttng_writer_write_element_bool(writer, config_element_all, 1);
1693 if (ret) {
1694 goto end;
1695 }
1696
1697 ret = mi_lttng_writer_close_element(writer);
1698 if (ret) {
1699 goto end;
1700 }
1701
1702 if (!is_open) {
1703 ret = mi_lttng_writer_close_element(writer);
1704 if (ret) {
1705 goto end;
1706 }
1707 }
1708 end:
1709 return ret;
1710 }
1711
1712 LTTNG_HIDDEN
1713 int mi_lttng_integral_process_attribute_value(struct mi_writer *writer,
1714 enum lttng_process_attr process_attr,
1715 int64_t value,
1716 bool is_open)
1717 {
1718 int ret;
1719 const char *element_id_tracker, *element_target_id;
1720
1721 ret = get_tracker_elements(
1722 process_attr, &element_id_tracker, &element_target_id);
1723 if (ret) {
1724 return ret;
1725 }
1726
1727 ret = mi_lttng_writer_open_element(writer, element_target_id);
1728 if (ret) {
1729 goto end;
1730 }
1731
1732 ret = mi_lttng_writer_open_element(writer, config_element_type);
1733 if (ret) {
1734 goto end;
1735 }
1736
1737 ret = mi_lttng_writer_write_element_signed_int(
1738 writer, config_element_process_attr_id, value);
1739 if (ret) {
1740 goto end;
1741 }
1742
1743 ret = mi_lttng_writer_close_element(writer);
1744 if (ret) {
1745 goto end;
1746 }
1747
1748 if (!is_open) {
1749 ret = mi_lttng_writer_close_element(writer);
1750 if (ret) {
1751 goto end;
1752 }
1753 }
1754
1755 end:
1756 return ret;
1757 }
1758
1759 LTTNG_HIDDEN
1760 int mi_lttng_string_process_attribute_value(struct mi_writer *writer,
1761 enum lttng_process_attr process_attr,
1762 const char *value,
1763 bool is_open)
1764
1765 {
1766 int ret;
1767 const char *element_id_tracker, *element_target_id;
1768
1769 ret = get_tracker_elements(
1770 process_attr, &element_id_tracker, &element_target_id);
1771 if (ret) {
1772 return ret;
1773 }
1774
1775 ret = mi_lttng_writer_open_element(writer, element_target_id);
1776 if (ret) {
1777 goto end;
1778 }
1779
1780 ret = mi_lttng_writer_open_element(writer, config_element_type);
1781 if (ret) {
1782 goto end;
1783 }
1784
1785 ret = mi_lttng_writer_write_element_string(
1786 writer, config_element_name, value);
1787 if (ret) {
1788 goto end;
1789 }
1790
1791 ret = mi_lttng_writer_close_element(writer);
1792 if (ret) {
1793 goto end;
1794 }
1795
1796 if (!is_open) {
1797 ret = mi_lttng_writer_close_element(writer);
1798 if (ret) {
1799 goto end;
1800 }
1801 }
1802
1803 end:
1804 return ret;
1805 }
1806
1807 LTTNG_HIDDEN
1808 int mi_lttng_event_fields_open(struct mi_writer *writer)
1809 {
1810 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1811 }
1812
1813 LTTNG_HIDDEN
1814 int mi_lttng_event_field(struct mi_writer *writer,
1815 struct lttng_event_field *field)
1816 {
1817 int ret;
1818
1819 if (!field->field_name[0]) {
1820 ret = 0;
1821 goto end;
1822 }
1823
1824 /* Open field */
1825 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1826 if (ret) {
1827 goto end;
1828 }
1829
1830 if (!field->field_name[0]) {
1831 goto close;
1832 }
1833
1834 /* Name */
1835 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1836 field->field_name);
1837 if (ret) {
1838 goto end;
1839 }
1840
1841 /* Type */
1842 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1843 mi_lttng_eventfieldtype_string(field->type));
1844 if (ret) {
1845 goto end;
1846 }
1847
1848 /* nowrite */
1849 ret = mi_lttng_writer_write_element_signed_int(writer,
1850 mi_lttng_element_nowrite, field->nowrite);
1851 if (ret) {
1852 goto end;
1853 }
1854
1855 close:
1856 /* Close field element */
1857 ret = mi_lttng_writer_close_element(writer);
1858
1859 end:
1860 return ret;
1861 }
1862
1863 LTTNG_HIDDEN
1864 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1865 struct lttng_event_perf_counter_ctx *perf_context)
1866 {
1867 int ret;
1868
1869 /* Open perf_counter_context */
1870 ret = mi_lttng_writer_open_element(writer,
1871 mi_lttng_element_perf_counter_context);
1872 if (ret) {
1873 goto end;
1874 }
1875
1876 /* Type */
1877 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1878 config_element_type, perf_context->type);
1879 if (ret) {
1880 goto end;
1881 }
1882
1883 /* Config */
1884 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1885 config_element_config, perf_context->config);
1886 if (ret) {
1887 goto end;
1888 }
1889
1890 /* Name of the perf counter */
1891 ret = mi_lttng_writer_write_element_string(writer,
1892 config_element_name, perf_context->name);
1893 if (ret) {
1894 goto end;
1895 }
1896
1897 /* Close perf_counter_context */
1898 ret = mi_lttng_writer_close_element(writer);
1899 end:
1900 return ret;
1901 }
1902
1903 static
1904 int mi_lttng_app_context(struct mi_writer *writer,
1905 const char *provider_name, const char *ctx_name)
1906 {
1907 int ret;
1908
1909 /* Open app */
1910 ret = mi_lttng_writer_open_element(writer,
1911 config_element_context_app);
1912 if (ret) {
1913 goto end;
1914 }
1915
1916 /* provider_name */
1917 ret = mi_lttng_writer_write_element_string(writer,
1918 config_element_context_app_provider_name,
1919 provider_name);
1920 if (ret) {
1921 goto end;
1922 }
1923
1924 /* ctx_name */
1925 ret = mi_lttng_writer_write_element_string(writer,
1926 config_element_context_app_ctx_name, ctx_name);
1927 if (ret) {
1928 goto end;
1929 }
1930
1931 /* Close app */
1932 ret = mi_lttng_writer_close_element(writer);
1933 end:
1934 return ret;
1935 }
1936
1937 LTTNG_HIDDEN
1938 int mi_lttng_context(struct mi_writer *writer,
1939 struct lttng_event_context *context, int is_open)
1940 {
1941 int ret;
1942
1943 /* Open context */
1944 ret = mi_lttng_writer_open_element(writer , config_element_context);
1945 if (ret) {
1946 goto end;
1947 }
1948
1949 /* Special case for PERF_*_COUNTER
1950 * print the lttng_event_perf_counter_ctx*/
1951 switch (context->ctx) {
1952 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1953 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1954 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1955 {
1956 struct lttng_event_perf_counter_ctx *perf_context =
1957 &context->u.perf_counter;
1958 ret = mi_lttng_perf_counter_context(writer, perf_context);
1959 if (ret) {
1960 goto end;
1961 }
1962 break;
1963 }
1964 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1965 {
1966 ret = mi_lttng_app_context(writer,
1967 context->u.app_ctx.provider_name,
1968 context->u.app_ctx.ctx_name);
1969 if (ret) {
1970 goto end;
1971 }
1972 break;
1973 }
1974 default:
1975 {
1976 const char *type_string = mi_lttng_event_contexttype_string(
1977 context->ctx);
1978 if (!type_string) {
1979 ret = -LTTNG_ERR_INVALID;
1980 goto end;
1981 }
1982
1983 /* Print context type */
1984 ret = mi_lttng_writer_write_element_string(writer,
1985 config_element_type, type_string);
1986 break;
1987 }
1988 }
1989
1990 /* Close context */
1991 if (!is_open) {
1992 ret = mi_lttng_writer_close_element(writer);
1993 }
1994
1995 end:
1996 return ret;
1997 }
1998
1999 LTTNG_HIDDEN
2000 int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
2001 const char *session_name)
2002 {
2003 int ret;
2004
2005 /* Open session element */
2006 ret = mi_lttng_writer_open_element(writer, config_element_session);
2007 if (ret) {
2008 goto end;
2009 }
2010
2011 /* Snapshot output list for current session name */
2012 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
2013 session_name);
2014 if (ret) {
2015 goto end;
2016 }
2017
2018 /* Open element snapshots (sequence one snapshot) */
2019 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
2020 if (ret) {
2021 goto end;
2022 }
2023
2024 end:
2025 return ret;
2026 }
2027
2028 LTTNG_HIDDEN
2029 int mi_lttng_snapshot_list_output(struct mi_writer *writer,
2030 struct lttng_snapshot_output *output)
2031 {
2032 int ret;
2033
2034 /* Open element snapshot output */
2035 ret = mi_lttng_writer_open_element(writer,
2036 mi_lttng_element_command_snapshot);
2037 if (ret) {
2038 goto end;
2039 }
2040
2041 /* ID of the snapshot output */
2042 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2043 mi_lttng_element_id, output->id);
2044 if (ret) {
2045 goto end;
2046 }
2047
2048 /* Name of the output */
2049 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
2050 output->name);
2051 if (ret) {
2052 goto end;
2053 }
2054
2055 /* Destination of the output (ctrl_url)*/
2056 ret = mi_lttng_writer_write_element_string(writer,
2057 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
2058 if (ret) {
2059 goto end;
2060 }
2061
2062 /* Destination of the output (data_url) */
2063 ret = mi_lttng_writer_write_element_string(writer,
2064 mi_lttng_element_snapshot_data_url, output->data_url);
2065 if (ret) {
2066 goto end;
2067 }
2068
2069 /* total size of all stream combined */
2070 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2071 mi_lttng_element_snapshot_max_size, output->max_size);
2072 if (ret) {
2073 goto end;
2074 }
2075
2076 /* Close snapshot output element */
2077 ret = mi_lttng_writer_close_element(writer);
2078
2079 end:
2080 return ret;
2081 }
2082
2083 LTTNG_HIDDEN
2084 int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
2085 const char *name, const char *current_session_name)
2086 {
2087 int ret;
2088
2089 /* Open element del_snapshot */
2090 ret = mi_lttng_writer_open_element(writer,
2091 mi_lttng_element_command_snapshot);
2092 if (ret) {
2093 goto end;
2094 }
2095
2096
2097 if (id != UINT32_MAX) {
2098 /* "Snapshot output "id" successfully deleted
2099 * for "current_session_name"
2100 * ID of the snapshot output
2101 */
2102 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2103 mi_lttng_element_id, id);
2104 if (ret) {
2105 goto end;
2106 }
2107 } else {
2108 /* "Snapshot output "name" successfully deleted
2109 * for session "current_session_name"
2110 * Name of the output
2111 */
2112 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
2113 name);
2114 if (ret) {
2115 goto end;
2116 }
2117 }
2118
2119 /* Snapshot was deleted for session "current_session_name"*/
2120 ret = mi_lttng_writer_write_element_string(writer,
2121 mi_lttng_element_snapshot_session_name,
2122 current_session_name);
2123 if (ret) {
2124 goto end;
2125 }
2126
2127 /* Close snapshot element */
2128 ret = mi_lttng_writer_close_element(writer);
2129
2130 end:
2131 return ret;
2132 }
2133
2134 LTTNG_HIDDEN
2135 int mi_lttng_snapshot_add_output(struct mi_writer *writer,
2136 const char *current_session_name, const char *n_ptr,
2137 struct lttng_snapshot_output *output)
2138 {
2139 int ret;
2140
2141 /* Open element snapshot */
2142 ret = mi_lttng_writer_open_element(writer,
2143 mi_lttng_element_command_snapshot);
2144 if (ret) {
2145 goto end;
2146 }
2147
2148 /* Snapshot output id */
2149 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2150 mi_lttng_element_id, output->id);
2151 if (ret) {
2152 goto end;
2153 }
2154
2155 /* Snapshot output names */
2156 ret = mi_lttng_writer_write_element_string(writer,
2157 config_element_name, n_ptr);
2158 if (ret) {
2159 goto end;
2160 }
2161
2162 /* Destination of the output (ctrl_url)*/
2163 ret = mi_lttng_writer_write_element_string(writer,
2164 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
2165 if (ret) {
2166 goto end;
2167 }
2168
2169 /* Snapshot added for session "current_session_name"*/
2170 ret = mi_lttng_writer_write_element_string(writer,
2171 mi_lttng_element_snapshot_session_name, current_session_name);
2172 if (ret) {
2173 goto end;
2174 }
2175
2176 /* total size of all stream combined */
2177 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2178 mi_lttng_element_snapshot_max_size, output->max_size);
2179 if (ret) {
2180 goto end;
2181 }
2182
2183 /* Close snapshot element */
2184 ret = mi_lttng_writer_close_element(writer);
2185
2186 end:
2187 return ret;
2188 }
2189
2190 LTTNG_HIDDEN
2191 int mi_lttng_snapshot_record(struct mi_writer *writer,
2192 const char *current_session_name, const char *url,
2193 const char *cmdline_ctrl_url, const char *cmdline_data_url)
2194 {
2195 int ret;
2196
2197 /* Open element snapshot */
2198 ret = mi_lttng_writer_open_element(writer,
2199 mi_lttng_element_command_snapshot);
2200 if (ret) {
2201 goto end;
2202 }
2203
2204 /*
2205 * If a valid an URL was given, serialize it,
2206 * else take the command line data and ctrl urls*/
2207 if (url) {
2208 /* Destination of the output (ctrl_url)*/
2209 ret = mi_lttng_writer_write_element_string(writer,
2210 mi_lttng_element_snapshot_ctrl_url, url);
2211 if (ret) {
2212 goto end;
2213 }
2214 } else if (cmdline_ctrl_url) {
2215 /* Destination of the output (ctrl_url)*/
2216 ret = mi_lttng_writer_write_element_string(writer,
2217 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
2218 if (ret) {
2219 goto end;
2220 }
2221
2222 /* Destination of the output (data_url) */
2223 ret = mi_lttng_writer_write_element_string(writer,
2224 mi_lttng_element_snapshot_data_url, cmdline_data_url);
2225 if (ret) {
2226 goto end;
2227 }
2228 }
2229
2230 /* Close record_snapshot element */
2231 ret = mi_lttng_writer_close_element(writer);
2232
2233 end:
2234 return ret;
2235 }
2236
2237 LTTNG_HIDDEN
2238 int mi_lttng_rotation_schedule(struct mi_writer *writer,
2239 const struct lttng_rotation_schedule *schedule)
2240 {
2241 int ret = 0;
2242 enum lttng_rotation_status status;
2243 uint64_t value;
2244 const char *element_name;
2245 const char *value_name;
2246 bool empty_schedule = false;
2247
2248 switch (lttng_rotation_schedule_get_type(schedule)) {
2249 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
2250 status = lttng_rotation_schedule_periodic_get_period(schedule,
2251 &value);
2252 element_name = mi_lttng_element_rotation_schedule_periodic;
2253 value_name = mi_lttng_element_rotation_schedule_periodic_time_us;
2254 break;
2255 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
2256 status = lttng_rotation_schedule_size_threshold_get_threshold(
2257 schedule, &value);
2258 element_name = mi_lttng_element_rotation_schedule_size_threshold;
2259 value_name = mi_lttng_element_rotation_schedule_size_threshold_bytes;
2260 break;
2261 default:
2262 ret = -1;
2263 goto end;
2264 }
2265
2266 if (status != LTTNG_ROTATION_STATUS_OK) {
2267 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
2268 empty_schedule = true;
2269 } else {
2270 ret = -1;
2271 goto end;
2272 }
2273 }
2274
2275 ret = mi_lttng_writer_open_element(writer, element_name);
2276 if (ret) {
2277 goto end;
2278 }
2279
2280 if (!empty_schedule) {
2281 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2282 value_name, value);
2283 if (ret) {
2284 goto end;
2285 }
2286 }
2287
2288 /* Close schedule descriptor element. */
2289 ret = mi_lttng_writer_close_element(writer);
2290 if (ret) {
2291 goto end;
2292 }
2293 end:
2294 return ret;
2295 }
2296
2297 LTTNG_HIDDEN
2298 int mi_lttng_rotation_schedule_result(struct mi_writer *writer,
2299 const struct lttng_rotation_schedule *schedule,
2300 bool success)
2301 {
2302 int ret = 0;
2303
2304 ret = mi_lttng_writer_open_element(writer,
2305 mi_lttng_element_rotation_schedule_result);
2306 if (ret) {
2307 goto end;
2308 }
2309
2310 ret = mi_lttng_writer_open_element(writer,
2311 mi_lttng_element_rotation_schedule);
2312 if (ret) {
2313 goto end;
2314 }
2315
2316 ret = mi_lttng_rotation_schedule(writer, schedule);
2317 if (ret) {
2318 goto end;
2319 }
2320
2321 /* Close rotation_schedule element */
2322 ret = mi_lttng_writer_close_element(writer);
2323 if (ret) {
2324 goto end;
2325 }
2326
2327 ret = mi_lttng_writer_write_element_bool(writer,
2328 mi_lttng_element_command_success, success);
2329 if (ret) {
2330 goto end;
2331 }
2332
2333 /* Close rotation_schedule_result element */
2334 ret = mi_lttng_writer_close_element(writer);
2335 if (ret) {
2336 goto end;
2337 }
2338 end:
2339 return ret;
2340 }
2341
2342 static
2343 int mi_lttng_location(struct mi_writer *writer,
2344 const struct lttng_trace_archive_location *location)
2345 {
2346 int ret = 0;
2347 enum lttng_trace_archive_location_type location_type;
2348 enum lttng_trace_archive_location_status status;
2349
2350 location_type = lttng_trace_archive_location_get_type(location);
2351
2352 switch (location_type) {
2353 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
2354 {
2355 const char *absolute_path;
2356
2357 status = lttng_trace_archive_location_local_get_absolute_path(
2358 location, &absolute_path);
2359 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2360 ret = -1;
2361 goto end;
2362 }
2363
2364 ret = mi_lttng_writer_open_element(writer,
2365 mi_lttng_element_rotation_location_local);
2366 if (ret) {
2367 goto end;
2368 }
2369
2370
2371 ret = mi_lttng_writer_write_element_string(writer,
2372 mi_lttng_element_rotation_location_local_absolute_path,
2373 absolute_path);
2374 if (ret) {
2375 goto end;
2376 }
2377
2378 /* Close local element */
2379 ret = mi_lttng_writer_close_element(writer);
2380 if (ret) {
2381 goto end;
2382 }
2383 break;
2384 }
2385 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
2386 {
2387 uint16_t control_port, data_port;
2388 const char *host, *relative_path;
2389 enum lttng_trace_archive_location_relay_protocol_type protocol;
2390
2391 /* Fetch all relay location parameters. */
2392 status = lttng_trace_archive_location_relay_get_protocol_type(
2393 location, &protocol);
2394 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2395 ret = -1;
2396 goto end;
2397 }
2398
2399 status = lttng_trace_archive_location_relay_get_host(
2400 location, &host);
2401 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2402 ret = -1;
2403 goto end;
2404 }
2405
2406 status = lttng_trace_archive_location_relay_get_control_port(
2407 location, &control_port);
2408 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2409 ret = -1;
2410 goto end;
2411 }
2412
2413 status = lttng_trace_archive_location_relay_get_data_port(
2414 location, &data_port);
2415 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2416 ret = -1;
2417 goto end;
2418 }
2419
2420 status = lttng_trace_archive_location_relay_get_relative_path(
2421 location, &relative_path);
2422 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2423 ret = -1;
2424 goto end;
2425 }
2426
2427 ret = mi_lttng_writer_open_element(writer,
2428 mi_lttng_element_rotation_location_relay);
2429 if (ret) {
2430 goto end;
2431 }
2432
2433 ret = mi_lttng_writer_write_element_string(writer,
2434 mi_lttng_element_rotation_location_relay_host,
2435 host);
2436 if (ret) {
2437 goto end;
2438 }
2439
2440 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2441 mi_lttng_element_rotation_location_relay_control_port,
2442 control_port);
2443 if (ret) {
2444 goto end;
2445 }
2446
2447 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2448 mi_lttng_element_rotation_location_relay_data_port,
2449 data_port);
2450 if (ret) {
2451 goto end;
2452 }
2453
2454 ret = mi_lttng_writer_write_element_string(writer,
2455 mi_lttng_element_rotation_location_relay_protocol,
2456 mi_lttng_trace_archive_location_relay_protocol_type_string(protocol));
2457 if (ret) {
2458 goto end;
2459 }
2460
2461 ret = mi_lttng_writer_write_element_string(writer,
2462 mi_lttng_element_rotation_location_relay_relative_path,
2463 relative_path);
2464 if (ret) {
2465 goto end;
2466 }
2467
2468 /* Close relay element */
2469 ret = mi_lttng_writer_close_element(writer);
2470 if (ret) {
2471 goto end;
2472 }
2473 break;
2474 }
2475 default:
2476 abort();
2477 }
2478 end:
2479 return ret;
2480 }
2481
2482 LTTNG_HIDDEN
2483 int mi_lttng_rotate(struct mi_writer *writer,
2484 const char *session_name,
2485 enum lttng_rotation_state rotation_state,
2486 const struct lttng_trace_archive_location *location)
2487 {
2488 int ret;
2489
2490 ret = mi_lttng_writer_open_element(writer,
2491 mi_lttng_element_rotation);
2492 if (ret) {
2493 goto end;
2494 }
2495
2496 ret = mi_lttng_writer_write_element_string(writer,
2497 mi_lttng_element_session_name,
2498 session_name);
2499 if (ret) {
2500 goto end;
2501 }
2502
2503 ret = mi_lttng_writer_write_element_string(writer,
2504 mi_lttng_element_rotation_state,
2505 mi_lttng_rotation_state_string(rotation_state));
2506 if (ret) {
2507 goto end;
2508 }
2509
2510 if (!location) {
2511 /* Not a serialization error. */
2512 goto close_rotation;
2513 }
2514
2515 ret = mi_lttng_writer_open_element(writer,
2516 mi_lttng_element_rotation_location);
2517 if (ret) {
2518 goto end;
2519 }
2520
2521 ret = mi_lttng_location(writer, location);
2522 if (ret) {
2523 goto close_location;
2524 }
2525
2526 close_location:
2527 /* Close location element */
2528 ret = mi_lttng_writer_close_element(writer);
2529 if (ret) {
2530 goto end;
2531 }
2532
2533 close_rotation:
2534 /* Close rotation element */
2535 ret = mi_lttng_writer_close_element(writer);
2536 if (ret) {
2537 goto end;
2538 }
2539 end:
2540 return ret;
2541 }
This page took 0.137095 seconds and 4 git commands to generate.