lttng: Implement lttng clear session command
[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 LTTNG_HIDDEN
1544 int mi_lttng_pid_tracker_open(struct mi_writer *writer)
1545 {
1546 int ret;
1547
1548 /* Open element pid_tracker */
1549 ret = mi_lttng_writer_open_element(writer, config_element_pid_tracker);
1550 if (ret) {
1551 goto end;
1552 }
1553
1554 /* Open targets element */
1555 ret = mi_lttng_targets_open(writer);
1556 end:
1557 return ret;
1558 }
1559
1560 LTTNG_HIDDEN
1561 int mi_lttng_pids_open(struct mi_writer *writer)
1562 {
1563 return mi_lttng_writer_open_element(writer, config_element_pids);
1564 }
1565
1566 /*
1567 * TODO: move the listing of pid for user agent to process semantic on
1568 * mi api bump. The use of process element break the mi api.
1569 */
1570 LTTNG_HIDDEN
1571 int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *name,
1572 int is_open)
1573 {
1574 int ret;
1575
1576 /* Open pid process */
1577 ret = mi_lttng_writer_open_element(writer, config_element_pid);
1578 if (ret) {
1579 goto end;
1580 }
1581
1582 /* Writing pid number */
1583 ret = mi_lttng_writer_write_element_signed_int(writer,
1584 mi_lttng_element_pid_id, (int)pid);
1585 if (ret) {
1586 goto end;
1587 }
1588
1589 /* Writing name of the process */
1590 if (name) {
1591 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1592 name);
1593 if (ret) {
1594 goto end;
1595 }
1596 }
1597
1598 if (!is_open) {
1599 /* Closing Pid */
1600 ret = mi_lttng_writer_close_element(writer);
1601 }
1602
1603 end:
1604 return ret;
1605 }
1606
1607 LTTNG_HIDDEN
1608 int mi_lttng_targets_open(struct mi_writer *writer)
1609 {
1610 return mi_lttng_writer_open_element(writer,
1611 config_element_targets);
1612 }
1613
1614 LTTNG_HIDDEN
1615 int mi_lttng_pid_target(struct mi_writer *writer, pid_t pid, int is_open)
1616 {
1617 int ret;
1618
1619 ret = mi_lttng_writer_open_element(writer,
1620 config_element_target_pid);
1621 if (ret) {
1622 goto end;
1623 }
1624
1625 /* Writing pid number
1626 * Special case for element all on track untrack command
1627 * All pid is represented as wildcard *
1628 */
1629 if ((int) pid == -1) {
1630 ret = mi_lttng_writer_write_element_string(writer,
1631 config_element_pid,
1632 mi_lttng_element_track_untrack_all_wildcard);
1633 } else {
1634 ret = mi_lttng_writer_write_element_signed_int(writer,
1635 config_element_pid, (int) pid);
1636 }
1637 if (ret) {
1638 goto end;
1639 }
1640
1641 if (!is_open) {
1642 ret = mi_lttng_writer_close_element(writer);
1643 if (ret) {
1644 goto end;
1645 }
1646 }
1647
1648 end:
1649 return ret;
1650 }
1651
1652 LTTNG_HIDDEN
1653 int mi_lttng_event_fields_open(struct mi_writer *writer)
1654 {
1655 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1656 }
1657
1658 LTTNG_HIDDEN
1659 int mi_lttng_event_field(struct mi_writer *writer,
1660 struct lttng_event_field *field)
1661 {
1662 int ret;
1663
1664 if (!field->field_name[0]) {
1665 ret = 0;
1666 goto end;
1667 }
1668
1669 /* Open field */
1670 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1671 if (ret) {
1672 goto end;
1673 }
1674
1675 if (!field->field_name[0]) {
1676 goto close;
1677 }
1678
1679 /* Name */
1680 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1681 field->field_name);
1682 if (ret) {
1683 goto end;
1684 }
1685
1686 /* Type */
1687 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1688 mi_lttng_eventfieldtype_string(field->type));
1689 if (ret) {
1690 goto end;
1691 }
1692
1693 /* nowrite */
1694 ret = mi_lttng_writer_write_element_signed_int(writer,
1695 mi_lttng_element_nowrite, field->nowrite);
1696 if (ret) {
1697 goto end;
1698 }
1699
1700 close:
1701 /* Close field element */
1702 ret = mi_lttng_writer_close_element(writer);
1703
1704 end:
1705 return ret;
1706 }
1707
1708 LTTNG_HIDDEN
1709 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1710 struct lttng_event_perf_counter_ctx *perf_context)
1711 {
1712 int ret;
1713
1714 /* Open perf_counter_context */
1715 ret = mi_lttng_writer_open_element(writer,
1716 mi_lttng_element_perf_counter_context);
1717 if (ret) {
1718 goto end;
1719 }
1720
1721 /* Type */
1722 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1723 config_element_type, perf_context->type);
1724 if (ret) {
1725 goto end;
1726 }
1727
1728 /* Config */
1729 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1730 config_element_config, perf_context->config);
1731 if (ret) {
1732 goto end;
1733 }
1734
1735 /* Name of the perf counter */
1736 ret = mi_lttng_writer_write_element_string(writer,
1737 config_element_name, perf_context->name);
1738 if (ret) {
1739 goto end;
1740 }
1741
1742 /* Close perf_counter_context */
1743 ret = mi_lttng_writer_close_element(writer);
1744 end:
1745 return ret;
1746 }
1747
1748 static
1749 int mi_lttng_app_context(struct mi_writer *writer,
1750 const char *provider_name, const char *ctx_name)
1751 {
1752 int ret;
1753
1754 /* Open app */
1755 ret = mi_lttng_writer_open_element(writer,
1756 config_element_context_app);
1757 if (ret) {
1758 goto end;
1759 }
1760
1761 /* provider_name */
1762 ret = mi_lttng_writer_write_element_string(writer,
1763 config_element_context_app_provider_name,
1764 provider_name);
1765 if (ret) {
1766 goto end;
1767 }
1768
1769 /* ctx_name */
1770 ret = mi_lttng_writer_write_element_string(writer,
1771 config_element_context_app_ctx_name, ctx_name);
1772 if (ret) {
1773 goto end;
1774 }
1775
1776 /* Close app */
1777 ret = mi_lttng_writer_close_element(writer);
1778 end:
1779 return ret;
1780 }
1781
1782 LTTNG_HIDDEN
1783 int mi_lttng_context(struct mi_writer *writer,
1784 struct lttng_event_context *context, int is_open)
1785 {
1786 int ret;
1787
1788 /* Open context */
1789 ret = mi_lttng_writer_open_element(writer , config_element_context);
1790 if (ret) {
1791 goto end;
1792 }
1793
1794 /* Special case for PERF_*_COUNTER
1795 * print the lttng_event_perf_counter_ctx*/
1796 switch (context->ctx) {
1797 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1798 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1799 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1800 {
1801 struct lttng_event_perf_counter_ctx *perf_context =
1802 &context->u.perf_counter;
1803 ret = mi_lttng_perf_counter_context(writer, perf_context);
1804 if (ret) {
1805 goto end;
1806 }
1807 break;
1808 }
1809 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1810 {
1811 ret = mi_lttng_app_context(writer,
1812 context->u.app_ctx.provider_name,
1813 context->u.app_ctx.ctx_name);
1814 if (ret) {
1815 goto end;
1816 }
1817 break;
1818 }
1819 default:
1820 {
1821 const char *type_string = mi_lttng_event_contexttype_string(
1822 context->ctx);
1823 if (!type_string) {
1824 ret = -LTTNG_ERR_INVALID;
1825 goto end;
1826 }
1827
1828 /* Print context type */
1829 ret = mi_lttng_writer_write_element_string(writer,
1830 config_element_type, type_string);
1831 break;
1832 }
1833 }
1834
1835 /* Close context */
1836 if (!is_open) {
1837 ret = mi_lttng_writer_close_element(writer);
1838 }
1839
1840 end:
1841 return ret;
1842 }
1843
1844 LTTNG_HIDDEN
1845 int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
1846 const char *session_name)
1847 {
1848 int ret;
1849
1850 /* Open session element */
1851 ret = mi_lttng_writer_open_element(writer, config_element_session);
1852 if (ret) {
1853 goto end;
1854 }
1855
1856 /* Snapshot output list for current session name */
1857 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1858 session_name);
1859 if (ret) {
1860 goto end;
1861 }
1862
1863 /* Open element snapshots (sequence one snapshot) */
1864 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
1865 if (ret) {
1866 goto end;
1867 }
1868
1869 end:
1870 return ret;
1871 }
1872
1873 LTTNG_HIDDEN
1874 int mi_lttng_snapshot_list_output(struct mi_writer *writer,
1875 struct lttng_snapshot_output *output)
1876 {
1877 int ret;
1878
1879 /* Open element snapshot output */
1880 ret = mi_lttng_writer_open_element(writer,
1881 mi_lttng_element_command_snapshot);
1882 if (ret) {
1883 goto end;
1884 }
1885
1886 /* ID of the snapshot output */
1887 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1888 mi_lttng_element_id, output->id);
1889 if (ret) {
1890 goto end;
1891 }
1892
1893 /* Name of the output */
1894 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1895 output->name);
1896 if (ret) {
1897 goto end;
1898 }
1899
1900 /* Destination of the output (ctrl_url)*/
1901 ret = mi_lttng_writer_write_element_string(writer,
1902 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1903 if (ret) {
1904 goto end;
1905 }
1906
1907 /* Destination of the output (data_url) */
1908 ret = mi_lttng_writer_write_element_string(writer,
1909 mi_lttng_element_snapshot_data_url, output->data_url);
1910 if (ret) {
1911 goto end;
1912 }
1913
1914 /* total size of all stream combined */
1915 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1916 mi_lttng_element_snapshot_max_size, output->max_size);
1917 if (ret) {
1918 goto end;
1919 }
1920
1921 /* Close snapshot output element */
1922 ret = mi_lttng_writer_close_element(writer);
1923
1924 end:
1925 return ret;
1926 }
1927
1928 LTTNG_HIDDEN
1929 int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
1930 const char *name, const char *current_session_name)
1931 {
1932 int ret;
1933
1934 /* Open element del_snapshot */
1935 ret = mi_lttng_writer_open_element(writer,
1936 mi_lttng_element_command_snapshot);
1937 if (ret) {
1938 goto end;
1939 }
1940
1941
1942 if (id != UINT32_MAX) {
1943 /* "Snapshot output "id" successfully deleted
1944 * for "current_session_name"
1945 * ID of the snapshot output
1946 */
1947 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1948 mi_lttng_element_id, id);
1949 if (ret) {
1950 goto end;
1951 }
1952 } else {
1953 /* "Snapshot output "name" successfully deleted
1954 * for session "current_session_name"
1955 * Name of the output
1956 */
1957 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1958 name);
1959 if (ret) {
1960 goto end;
1961 }
1962 }
1963
1964 /* Snapshot was deleted for session "current_session_name"*/
1965 ret = mi_lttng_writer_write_element_string(writer,
1966 mi_lttng_element_snapshot_session_name,
1967 current_session_name);
1968 if (ret) {
1969 goto end;
1970 }
1971
1972 /* Close snapshot element */
1973 ret = mi_lttng_writer_close_element(writer);
1974
1975 end:
1976 return ret;
1977 }
1978
1979 LTTNG_HIDDEN
1980 int mi_lttng_snapshot_add_output(struct mi_writer *writer,
1981 const char *current_session_name, const char *n_ptr,
1982 struct lttng_snapshot_output *output)
1983 {
1984 int ret;
1985
1986 /* Open element snapshot */
1987 ret = mi_lttng_writer_open_element(writer,
1988 mi_lttng_element_command_snapshot);
1989 if (ret) {
1990 goto end;
1991 }
1992
1993 /* Snapshot output id */
1994 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1995 mi_lttng_element_id, output->id);
1996 if (ret) {
1997 goto end;
1998 }
1999
2000 /* Snapshot output names */
2001 ret = mi_lttng_writer_write_element_string(writer,
2002 config_element_name, n_ptr);
2003 if (ret) {
2004 goto end;
2005 }
2006
2007 /* Destination of the output (ctrl_url)*/
2008 ret = mi_lttng_writer_write_element_string(writer,
2009 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
2010 if (ret) {
2011 goto end;
2012 }
2013
2014 /* Snapshot added for session "current_session_name"*/
2015 ret = mi_lttng_writer_write_element_string(writer,
2016 mi_lttng_element_snapshot_session_name, current_session_name);
2017 if (ret) {
2018 goto end;
2019 }
2020
2021 /* total size of all stream combined */
2022 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2023 mi_lttng_element_snapshot_max_size, output->max_size);
2024 if (ret) {
2025 goto end;
2026 }
2027
2028 /* Close snapshot element */
2029 ret = mi_lttng_writer_close_element(writer);
2030
2031 end:
2032 return ret;
2033 }
2034
2035 LTTNG_HIDDEN
2036 int mi_lttng_snapshot_record(struct mi_writer *writer,
2037 const char *current_session_name, const char *url,
2038 const char *cmdline_ctrl_url, const char *cmdline_data_url)
2039 {
2040 int ret;
2041
2042 /* Open element snapshot */
2043 ret = mi_lttng_writer_open_element(writer,
2044 mi_lttng_element_command_snapshot);
2045 if (ret) {
2046 goto end;
2047 }
2048
2049 /*
2050 * If a valid an URL was given, serialize it,
2051 * else take the command line data and ctrl urls*/
2052 if (url) {
2053 /* Destination of the output (ctrl_url)*/
2054 ret = mi_lttng_writer_write_element_string(writer,
2055 mi_lttng_element_snapshot_ctrl_url, url);
2056 if (ret) {
2057 goto end;
2058 }
2059 } else if (cmdline_ctrl_url) {
2060 /* Destination of the output (ctrl_url)*/
2061 ret = mi_lttng_writer_write_element_string(writer,
2062 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
2063 if (ret) {
2064 goto end;
2065 }
2066
2067 /* Destination of the output (data_url) */
2068 ret = mi_lttng_writer_write_element_string(writer,
2069 mi_lttng_element_snapshot_data_url, cmdline_data_url);
2070 if (ret) {
2071 goto end;
2072 }
2073 }
2074
2075 /* Close record_snapshot element */
2076 ret = mi_lttng_writer_close_element(writer);
2077
2078 end:
2079 return ret;
2080 }
2081
2082 LTTNG_HIDDEN
2083 int mi_lttng_rotation_schedule(struct mi_writer *writer,
2084 const struct lttng_rotation_schedule *schedule)
2085 {
2086 int ret = 0;
2087 enum lttng_rotation_status status;
2088 uint64_t value;
2089 const char *element_name;
2090 const char *value_name;
2091 bool empty_schedule = false;
2092
2093 switch (lttng_rotation_schedule_get_type(schedule)) {
2094 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
2095 status = lttng_rotation_schedule_periodic_get_period(schedule,
2096 &value);
2097 element_name = mi_lttng_element_rotation_schedule_periodic;
2098 value_name = mi_lttng_element_rotation_schedule_periodic_time_us;
2099 break;
2100 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
2101 status = lttng_rotation_schedule_size_threshold_get_threshold(
2102 schedule, &value);
2103 element_name = mi_lttng_element_rotation_schedule_size_threshold;
2104 value_name = mi_lttng_element_rotation_schedule_size_threshold_bytes;
2105 break;
2106 default:
2107 ret = -1;
2108 goto end;
2109 }
2110
2111 if (status != LTTNG_ROTATION_STATUS_OK) {
2112 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
2113 empty_schedule = true;
2114 } else {
2115 ret = -1;
2116 goto end;
2117 }
2118 }
2119
2120 ret = mi_lttng_writer_open_element(writer, element_name);
2121 if (ret) {
2122 goto end;
2123 }
2124
2125 if (!empty_schedule) {
2126 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2127 value_name, value);
2128 if (ret) {
2129 goto end;
2130 }
2131 }
2132
2133 /* Close schedule descriptor element. */
2134 ret = mi_lttng_writer_close_element(writer);
2135 if (ret) {
2136 goto end;
2137 }
2138 end:
2139 return ret;
2140 }
2141
2142 LTTNG_HIDDEN
2143 int mi_lttng_rotation_schedule_result(struct mi_writer *writer,
2144 const struct lttng_rotation_schedule *schedule,
2145 bool success)
2146 {
2147 int ret = 0;
2148
2149 ret = mi_lttng_writer_open_element(writer,
2150 mi_lttng_element_rotation_schedule_result);
2151 if (ret) {
2152 goto end;
2153 }
2154
2155 ret = mi_lttng_writer_open_element(writer,
2156 mi_lttng_element_rotation_schedule);
2157 if (ret) {
2158 goto end;
2159 }
2160
2161 ret = mi_lttng_rotation_schedule(writer, schedule);
2162 if (ret) {
2163 goto end;
2164 }
2165
2166 /* Close rotation_schedule element */
2167 ret = mi_lttng_writer_close_element(writer);
2168 if (ret) {
2169 goto end;
2170 }
2171
2172 ret = mi_lttng_writer_write_element_bool(writer,
2173 mi_lttng_element_command_success, success);
2174 if (ret) {
2175 goto end;
2176 }
2177
2178 /* Close rotation_schedule_result element */
2179 ret = mi_lttng_writer_close_element(writer);
2180 if (ret) {
2181 goto end;
2182 }
2183 end:
2184 return ret;
2185 }
2186
2187 static
2188 int mi_lttng_location(struct mi_writer *writer,
2189 const struct lttng_trace_archive_location *location)
2190 {
2191 int ret = 0;
2192 enum lttng_trace_archive_location_type location_type;
2193 enum lttng_trace_archive_location_status status;
2194
2195 location_type = lttng_trace_archive_location_get_type(location);
2196
2197 switch (location_type) {
2198 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
2199 {
2200 const char *absolute_path;
2201
2202 status = lttng_trace_archive_location_local_get_absolute_path(
2203 location, &absolute_path);
2204 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2205 ret = -1;
2206 goto end;
2207 }
2208
2209 ret = mi_lttng_writer_open_element(writer,
2210 mi_lttng_element_rotation_location_local);
2211 if (ret) {
2212 goto end;
2213 }
2214
2215
2216 ret = mi_lttng_writer_write_element_string(writer,
2217 mi_lttng_element_rotation_location_local_absolute_path,
2218 absolute_path);
2219 if (ret) {
2220 goto end;
2221 }
2222
2223 /* Close local element */
2224 ret = mi_lttng_writer_close_element(writer);
2225 if (ret) {
2226 goto end;
2227 }
2228 break;
2229 }
2230 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
2231 {
2232 uint16_t control_port, data_port;
2233 const char *host, *relative_path;
2234 enum lttng_trace_archive_location_relay_protocol_type protocol;
2235
2236 /* Fetch all relay location parameters. */
2237 status = lttng_trace_archive_location_relay_get_protocol_type(
2238 location, &protocol);
2239 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2240 ret = -1;
2241 goto end;
2242 }
2243
2244 status = lttng_trace_archive_location_relay_get_host(
2245 location, &host);
2246 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2247 ret = -1;
2248 goto end;
2249 }
2250
2251 status = lttng_trace_archive_location_relay_get_control_port(
2252 location, &control_port);
2253 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2254 ret = -1;
2255 goto end;
2256 }
2257
2258 status = lttng_trace_archive_location_relay_get_data_port(
2259 location, &data_port);
2260 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2261 ret = -1;
2262 goto end;
2263 }
2264
2265 status = lttng_trace_archive_location_relay_get_relative_path(
2266 location, &relative_path);
2267 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
2268 ret = -1;
2269 goto end;
2270 }
2271
2272 ret = mi_lttng_writer_open_element(writer,
2273 mi_lttng_element_rotation_location_relay);
2274 if (ret) {
2275 goto end;
2276 }
2277
2278 ret = mi_lttng_writer_write_element_string(writer,
2279 mi_lttng_element_rotation_location_relay_host,
2280 host);
2281 if (ret) {
2282 goto end;
2283 }
2284
2285 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2286 mi_lttng_element_rotation_location_relay_control_port,
2287 control_port);
2288 if (ret) {
2289 goto end;
2290 }
2291
2292 ret = mi_lttng_writer_write_element_unsigned_int(writer,
2293 mi_lttng_element_rotation_location_relay_data_port,
2294 data_port);
2295 if (ret) {
2296 goto end;
2297 }
2298
2299 ret = mi_lttng_writer_write_element_string(writer,
2300 mi_lttng_element_rotation_location_relay_protocol,
2301 mi_lttng_trace_archive_location_relay_protocol_type_string(protocol));
2302 if (ret) {
2303 goto end;
2304 }
2305
2306 ret = mi_lttng_writer_write_element_string(writer,
2307 mi_lttng_element_rotation_location_relay_relative_path,
2308 relative_path);
2309 if (ret) {
2310 goto end;
2311 }
2312
2313 /* Close relay element */
2314 ret = mi_lttng_writer_close_element(writer);
2315 if (ret) {
2316 goto end;
2317 }
2318 break;
2319 }
2320 default:
2321 abort();
2322 }
2323 end:
2324 return ret;
2325 }
2326
2327 LTTNG_HIDDEN
2328 int mi_lttng_rotate(struct mi_writer *writer,
2329 const char *session_name,
2330 enum lttng_rotation_state rotation_state,
2331 const struct lttng_trace_archive_location *location)
2332 {
2333 int ret;
2334
2335 ret = mi_lttng_writer_open_element(writer,
2336 mi_lttng_element_rotation);
2337 if (ret) {
2338 goto end;
2339 }
2340
2341 ret = mi_lttng_writer_write_element_string(writer,
2342 mi_lttng_element_session_name,
2343 session_name);
2344 if (ret) {
2345 goto end;
2346 }
2347
2348 ret = mi_lttng_writer_write_element_string(writer,
2349 mi_lttng_element_rotation_state,
2350 mi_lttng_rotation_state_string(rotation_state));
2351 if (ret) {
2352 goto end;
2353 }
2354
2355 if (!location) {
2356 /* Not a serialization error. */
2357 goto close_rotation;
2358 }
2359
2360 ret = mi_lttng_writer_open_element(writer,
2361 mi_lttng_element_rotation_location);
2362 if (ret) {
2363 goto end;
2364 }
2365
2366 ret = mi_lttng_location(writer, location);
2367 if (ret) {
2368 goto close_location;
2369 }
2370
2371 close_location:
2372 /* Close location element */
2373 ret = mi_lttng_writer_close_element(writer);
2374 if (ret) {
2375 goto end;
2376 }
2377
2378 close_rotation:
2379 /* Close rotation element */
2380 ret = mi_lttng_writer_close_element(writer);
2381 if (ret) {
2382 goto end;
2383 }
2384 end:
2385 return ret;
2386 }
This page took 0.143337 seconds and 5 git commands to generate.