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