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