Simplify lock handling in enqueue_timer_rotate_job()
[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 "http://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 const char * const mi_lttng_element_command_rotate = "rotate";
78
79 /* Strings related to version command */
80 const char * const mi_lttng_element_version = "version";
81 const char * const mi_lttng_element_version_commit = "commit";
82 const char * const mi_lttng_element_version_description = "description";
83 const char * const mi_lttng_element_version_license = "license";
84 const char * const mi_lttng_element_version_major = "major";
85 const char * const mi_lttng_element_version_minor = "minor";
86 const char * const mi_lttng_element_version_patch_level = "patchLevel";
87 const char * const mi_lttng_element_version_str = "string";
88 const char * const mi_lttng_element_version_web = "url";
89
90 /* String related to a lttng_event_field */
91 const char * const mi_lttng_element_event_field = "event_field";
92 const char * const mi_lttng_element_event_fields = "event_fields";
93
94 /* String related to lttng_event_perf_counter_ctx */
95 const char * const mi_lttng_element_perf_counter_context = "perf";
96
97 /* Strings related to pid */
98 const char * const mi_lttng_element_pid_id = "id";
99
100 /* Strings related to save command */
101 const char * const mi_lttng_element_save = "save";
102
103 /* Strings related to load command */
104 const char * const mi_lttng_element_load = "load";
105 LTTNG_HIDDEN const char * const mi_lttng_element_load_overrides = "overrides";
106 LTTNG_HIDDEN const char * const mi_lttng_element_load_override_url = "url";
107
108 /* General elements of mi_lttng */
109 const char * const mi_lttng_element_empty = "";
110 const char * const mi_lttng_element_id = "id";
111 const char * const mi_lttng_element_nowrite = "nowrite";
112 const char * const mi_lttng_element_success = "success";
113 const char * const mi_lttng_element_type_enum = "ENUM";
114 const char * const mi_lttng_element_type_float = "FLOAT";
115 const char * const mi_lttng_element_type_integer = "INTEGER";
116 const char * const mi_lttng_element_type_other = "OTHER";
117 const char * const mi_lttng_element_type_string = "STRING";
118
119 /* String related to loglevel */
120 const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
121 const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
122 const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
123 const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
124 const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
125 const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
126 const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
127 const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
128 const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
129 const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
130 const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
131 const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
132 const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
133 const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
134 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
135 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
136
137 /* String related to loglevel JUL */
138 const char * const mi_lttng_loglevel_str_jul_all = "JUL_ALL";
139 const char * const mi_lttng_loglevel_str_jul_config = "JUL_CONFIG";
140 const char * const mi_lttng_loglevel_str_jul_fine = "JUL_FINE";
141 const char * const mi_lttng_loglevel_str_jul_finer = "JUL_FINER";
142 const char * const mi_lttng_loglevel_str_jul_finest = "JUL_FINEST";
143 const char * const mi_lttng_loglevel_str_jul_info = "JUL_INFO";
144 const char * const mi_lttng_loglevel_str_jul_off = "JUL_OFF";
145 const char * const mi_lttng_loglevel_str_jul_severe = "JUL_SEVERE";
146 const char * const mi_lttng_loglevel_str_jul_warning = "JUL_WARNING";
147
148 /* String related to loglevel LOG4J */
149 const char * const mi_lttng_loglevel_str_log4j_off = "LOG4J_OFF";
150 const char * const mi_lttng_loglevel_str_log4j_fatal = "LOG4J_FATAL";
151 const char * const mi_lttng_loglevel_str_log4j_error = "LOG4J_ERROR";
152 const char * const mi_lttng_loglevel_str_log4j_warn = "LOG4J_WARN";
153 const char * const mi_lttng_loglevel_str_log4j_info = "LOG4J_INFO";
154 const char * const mi_lttng_loglevel_str_log4j_debug = "LOG4J_DEBUG";
155 const char * const mi_lttng_loglevel_str_log4j_trace = "LOG4J_TRACE";
156 const char * const mi_lttng_loglevel_str_log4j_all = "LOG4J_ALL";
157
158 /* String related to loglevel Python */
159 const char * const mi_lttng_loglevel_str_python_critical = "PYTHON_CRITICAL";
160 const char * const mi_lttng_loglevel_str_python_error = "PYTHON_ERROR";
161 const char * const mi_lttng_loglevel_str_python_warning = "PYTHON_WARNING";
162 const char * const mi_lttng_loglevel_str_python_info = "PYTHON_INFO";
163 const char * const mi_lttng_loglevel_str_python_debug = "PYTHON_DEBUG";
164 const char * const mi_lttng_loglevel_str_python_notset = "PYTHON_NOTSET";
165
166 /* String related to loglevel type */
167 const char * const mi_lttng_loglevel_type_all = "ALL";
168 const char * const mi_lttng_loglevel_type_range = "RANGE";
169 const char * const mi_lttng_loglevel_type_single = "SINGLE";
170 const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
171
172 /* String related to a lttng_snapshot_output */
173 const char * const mi_lttng_element_snapshot_ctrl_url = "ctrl_url";
174 const char * const mi_lttng_element_snapshot_data_url = "data_url";
175 const char * const mi_lttng_element_snapshot_max_size = "max_size";
176 const char * const mi_lttng_element_snapshot_n_ptr = "n_ptr";
177 const char * const mi_lttng_element_snapshot_session_name = "session_name";
178 const char * const mi_lttng_element_snapshots = "snapshots";
179
180 /* String related to track/untrack command */
181 const char * const mi_lttng_element_track_untrack_all_wildcard = "*";
182
183 LTTNG_HIDDEN const char * const mi_lttng_element_session_name = "session_name";
184
185 /* String related to rotate command */
186 LTTNG_HIDDEN const char * const mi_lttng_element_rotation = "rotation";
187 LTTNG_HIDDEN const char * const mi_lttng_element_rotations = "rotations";
188 LTTNG_HIDDEN const char * const mi_lttng_element_rotate_status = "status";
189
190 /* Deprecated symbols preserved for ABI compatibility. */
191 const char * const mi_lttng_context_type_perf_counter;
192 const char * const mi_lttng_context_type_perf_cpu_counter;
193 const char * const mi_lttng_context_type_perf_thread_counter;
194 const char * const mi_lttng_element_track_untrack_pid_target;
195 const char * const mi_lttng_element_track_untrack_targets;
196 const char * const mi_lttng_element_calibrate;
197 const char * const mi_lttng_element_calibrate_function;
198 const char * const mi_lttng_element_command_calibrate;
199
200 /* This is a merge of jul loglevel and regular loglevel
201 * Those should never overlap by definition
202 * (see struct lttng_event loglevel)
203 */
204 LTTNG_HIDDEN
205 const char *mi_lttng_loglevel_string(int value, enum lttng_domain_type domain)
206 {
207 switch (domain) {
208 case LTTNG_DOMAIN_KERNEL:
209 case LTTNG_DOMAIN_UST:
210 switch (value) {
211 case -1:
212 return mi_lttng_element_empty;
213 case LTTNG_LOGLEVEL_EMERG:
214 return mi_lttng_loglevel_str_emerg;
215 case LTTNG_LOGLEVEL_ALERT:
216 return mi_lttng_loglevel_str_alert;
217 case LTTNG_LOGLEVEL_CRIT:
218 return mi_lttng_loglevel_str_crit;
219 case LTTNG_LOGLEVEL_ERR:
220 return mi_lttng_loglevel_str_err;
221 case LTTNG_LOGLEVEL_WARNING:
222 return mi_lttng_loglevel_str_warning;
223 case LTTNG_LOGLEVEL_NOTICE:
224 return mi_lttng_loglevel_str_notice;
225 case LTTNG_LOGLEVEL_INFO:
226 return mi_lttng_loglevel_str_info;
227 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
228 return mi_lttng_loglevel_str_debug_system;
229 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
230 return mi_lttng_loglevel_str_debug_program;
231 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
232 return mi_lttng_loglevel_str_debug_process;
233 case LTTNG_LOGLEVEL_DEBUG_MODULE:
234 return mi_lttng_loglevel_str_debug_module;
235 case LTTNG_LOGLEVEL_DEBUG_UNIT:
236 return mi_lttng_loglevel_str_debug_unit;
237 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
238 return mi_lttng_loglevel_str_debug_function;
239 case LTTNG_LOGLEVEL_DEBUG_LINE:
240 return mi_lttng_loglevel_str_debug_line;
241 case LTTNG_LOGLEVEL_DEBUG:
242 return mi_lttng_loglevel_str_debug;
243 default:
244 return mi_lttng_loglevel_str_unknown;
245 }
246 break;
247 case LTTNG_DOMAIN_LOG4J:
248 switch (value) {
249 case -1:
250 return mi_lttng_element_empty;
251 case LTTNG_LOGLEVEL_LOG4J_OFF:
252 return mi_lttng_loglevel_str_log4j_off;
253 case LTTNG_LOGLEVEL_LOG4J_FATAL:
254 return mi_lttng_loglevel_str_log4j_fatal;
255 case LTTNG_LOGLEVEL_LOG4J_ERROR:
256 return mi_lttng_loglevel_str_log4j_error;
257 case LTTNG_LOGLEVEL_LOG4J_WARN:
258 return mi_lttng_loglevel_str_log4j_warn;
259 case LTTNG_LOGLEVEL_LOG4J_INFO:
260 return mi_lttng_loglevel_str_log4j_info;
261 case LTTNG_LOGLEVEL_LOG4J_DEBUG:
262 return mi_lttng_loglevel_str_log4j_debug;
263 case LTTNG_LOGLEVEL_LOG4J_TRACE:
264 return mi_lttng_loglevel_str_log4j_trace;
265 case LTTNG_LOGLEVEL_LOG4J_ALL:
266 return mi_lttng_loglevel_str_log4j_all;
267 default:
268 return mi_lttng_loglevel_str_unknown;
269 }
270 break;
271 case LTTNG_DOMAIN_JUL:
272 switch (value) {
273 case -1:
274 return mi_lttng_element_empty;
275 case LTTNG_LOGLEVEL_JUL_OFF:
276 return mi_lttng_loglevel_str_jul_off;
277 case LTTNG_LOGLEVEL_JUL_SEVERE:
278 return mi_lttng_loglevel_str_jul_severe;
279 case LTTNG_LOGLEVEL_JUL_WARNING:
280 return mi_lttng_loglevel_str_jul_warning;
281 case LTTNG_LOGLEVEL_JUL_INFO:
282 return mi_lttng_loglevel_str_jul_info;
283 case LTTNG_LOGLEVEL_JUL_CONFIG:
284 return mi_lttng_loglevel_str_jul_config;
285 case LTTNG_LOGLEVEL_JUL_FINE:
286 return mi_lttng_loglevel_str_jul_fine;
287 case LTTNG_LOGLEVEL_JUL_FINER:
288 return mi_lttng_loglevel_str_jul_finer;
289 case LTTNG_LOGLEVEL_JUL_FINEST:
290 return mi_lttng_loglevel_str_jul_finest;
291 case LTTNG_LOGLEVEL_JUL_ALL:
292 return mi_lttng_loglevel_str_jul_all;
293 default:
294 return mi_lttng_loglevel_str_unknown;
295 }
296 break;
297 case LTTNG_DOMAIN_PYTHON:
298 switch (value) {
299 case LTTNG_LOGLEVEL_PYTHON_CRITICAL:
300 return mi_lttng_loglevel_str_python_critical;
301 case LTTNG_LOGLEVEL_PYTHON_ERROR:
302 return mi_lttng_loglevel_str_python_error;
303 case LTTNG_LOGLEVEL_PYTHON_WARNING:
304 return mi_lttng_loglevel_str_python_warning;
305 case LTTNG_LOGLEVEL_PYTHON_INFO:
306 return mi_lttng_loglevel_str_python_info;
307 case LTTNG_LOGLEVEL_PYTHON_DEBUG:
308 return mi_lttng_loglevel_str_python_debug;
309 case LTTNG_LOGLEVEL_PYTHON_NOTSET:
310 return mi_lttng_loglevel_str_python_notset;
311 default:
312 return mi_lttng_loglevel_str_unknown;
313 }
314 break;
315 default:
316 return mi_lttng_loglevel_str_unknown;
317 }
318 }
319
320 LTTNG_HIDDEN
321 const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
322 {
323 switch (value) {
324 case LTTNG_EVENT_LOGLEVEL_ALL:
325 return mi_lttng_loglevel_type_all;
326 case LTTNG_EVENT_LOGLEVEL_RANGE:
327 return mi_lttng_loglevel_type_range;
328 case LTTNG_EVENT_LOGLEVEL_SINGLE:
329 return mi_lttng_loglevel_type_single;
330 default:
331 return mi_lttng_loglevel_type_unknown;
332 }
333 }
334
335 LTTNG_HIDDEN
336 const char *mi_lttng_eventtype_string(enum lttng_event_type value)
337 {
338 switch (value) {
339 case LTTNG_EVENT_ALL:
340 return config_event_type_all;
341 case LTTNG_EVENT_TRACEPOINT:
342 return config_event_type_tracepoint;
343 case LTTNG_EVENT_PROBE:
344 return config_event_type_probe;
345 case LTTNG_EVENT_FUNCTION:
346 return config_event_type_function;
347 case LTTNG_EVENT_FUNCTION_ENTRY:
348 return config_event_type_function_entry;
349 case LTTNG_EVENT_SYSCALL:
350 return config_event_type_syscall;
351 case LTTNG_EVENT_NOOP:
352 return config_event_type_noop;
353 default:
354 return mi_lttng_element_empty;
355 }
356 }
357
358 static
359 const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
360 {
361 switch (val) {
362 case LTTNG_EVENT_CONTEXT_PID:
363 return config_event_context_pid;
364 case LTTNG_EVENT_CONTEXT_PROCNAME:
365 return config_event_context_procname;
366 case LTTNG_EVENT_CONTEXT_PRIO:
367 return config_event_context_prio;
368 case LTTNG_EVENT_CONTEXT_NICE:
369 return config_event_context_nice;
370 case LTTNG_EVENT_CONTEXT_VPID:
371 return config_event_context_vpid;
372 case LTTNG_EVENT_CONTEXT_TID:
373 return config_event_context_tid;
374 case LTTNG_EVENT_CONTEXT_VTID:
375 return config_event_context_vtid;
376 case LTTNG_EVENT_CONTEXT_PPID:
377 return config_event_context_ppid;
378 case LTTNG_EVENT_CONTEXT_VPPID:
379 return config_event_context_vppid;
380 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
381 return config_event_context_pthread_id;
382 case LTTNG_EVENT_CONTEXT_HOSTNAME:
383 return config_event_context_hostname;
384 case LTTNG_EVENT_CONTEXT_IP:
385 return config_event_context_ip;
386 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
387 return config_event_context_interruptible;
388 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
389 return config_event_context_preemptible;
390 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
391 return config_event_context_need_reschedule;
392 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
393 return config_event_context_migratable;
394 default:
395 return NULL;
396 }
397 }
398
399 LTTNG_HIDDEN
400 const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
401 {
402 switch (val) {
403 case(LTTNG_EVENT_FIELD_INTEGER):
404 return mi_lttng_element_type_integer;
405 case(LTTNG_EVENT_FIELD_ENUM):
406 return mi_lttng_element_type_enum;
407 case(LTTNG_EVENT_FIELD_FLOAT):
408 return mi_lttng_element_type_float;
409 case(LTTNG_EVENT_FIELD_STRING):
410 return mi_lttng_element_type_string;
411 default:
412 return mi_lttng_element_type_other;
413 }
414 }
415
416 LTTNG_HIDDEN
417 const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
418 {
419 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
420 switch (value) {
421 case LTTNG_DOMAIN_KERNEL:
422 return config_domain_type_kernel;
423 case LTTNG_DOMAIN_UST:
424 return config_domain_type_ust;
425 case LTTNG_DOMAIN_JUL:
426 return config_domain_type_jul;
427 case LTTNG_DOMAIN_LOG4J:
428 return config_domain_type_log4j;
429 case LTTNG_DOMAIN_PYTHON:
430 return config_domain_type_python;
431 default:
432 /* Should not have an unknown domain */
433 assert(0);
434 return NULL;
435 }
436 }
437
438 LTTNG_HIDDEN
439 const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
440 {
441 switch (value) {
442 case LTTNG_BUFFER_PER_PID:
443 return config_buffer_type_per_pid;
444 case LTTNG_BUFFER_PER_UID:
445 return config_buffer_type_per_uid;
446 case LTTNG_BUFFER_GLOBAL:
447 return config_buffer_type_global;
448 default:
449 /* Should not have an unknow buffer type */
450 assert(0);
451 return NULL;
452 }
453 }
454
455 LTTNG_HIDDEN
456 struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
457 {
458 struct mi_writer *mi_writer;
459
460 mi_writer = zmalloc(sizeof(struct mi_writer));
461 if (!mi_writer) {
462 PERROR("zmalloc mi_writer_create");
463 goto end;
464 }
465 if (mi_output_type == LTTNG_MI_XML) {
466 mi_writer->writer = config_writer_create(fd_output, 0);
467 if (!mi_writer->writer) {
468 goto err_destroy;
469 }
470 mi_writer->type = LTTNG_MI_XML;
471 } else {
472 goto err_destroy;
473 }
474
475 end:
476 return mi_writer;
477
478 err_destroy:
479 free(mi_writer);
480 return NULL;
481 }
482
483 LTTNG_HIDDEN
484 int mi_lttng_writer_destroy(struct mi_writer *writer)
485 {
486 int ret;
487
488 if (!writer) {
489 ret = -EINVAL;
490 goto end;
491 }
492
493 ret = config_writer_destroy(writer->writer);
494 if (ret < 0) {
495 goto end;
496 }
497
498 free(writer);
499 end:
500 return ret;
501 }
502
503 LTTNG_HIDDEN
504 int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
505 {
506 int ret;
507
508 /*
509 * A command is always the MI's root node, it must declare the current
510 * namespace and schema URIs and the schema's version.
511 */
512 ret = config_writer_open_element(writer->writer,
513 mi_lttng_element_command);
514 if (ret) {
515 goto end;
516 }
517
518 ret = config_writer_write_attribute(writer->writer,
519 mi_lttng_xmlns, DEFAULT_LTTNG_MI_NAMESPACE);
520 if (ret) {
521 goto end;
522 }
523
524 ret = config_writer_write_attribute(writer->writer,
525 mi_lttng_xmlns_xsi, mi_lttng_w3_schema_uri);
526 if (ret) {
527 goto end;
528 }
529
530 ret = config_writer_write_attribute(writer->writer,
531 mi_lttng_schema_location,
532 mi_lttng_schema_location_uri);
533 if (ret) {
534 goto end;
535 }
536
537 ret = config_writer_write_attribute(writer->writer,
538 mi_lttng_schema_version,
539 mi_lttng_schema_version_value);
540 if (ret) {
541 goto end;
542 }
543
544 ret = mi_lttng_writer_write_element_string(writer,
545 mi_lttng_element_command_name, command);
546 end:
547 return ret;
548 }
549
550 LTTNG_HIDDEN
551 int mi_lttng_writer_command_close(struct mi_writer *writer)
552 {
553 return mi_lttng_writer_close_element(writer);
554 }
555
556 LTTNG_HIDDEN
557 int mi_lttng_writer_open_element(struct mi_writer *writer,
558 const char *element_name)
559 {
560 return config_writer_open_element(writer->writer, element_name);
561 }
562
563 LTTNG_HIDDEN
564 int mi_lttng_writer_close_element(struct mi_writer *writer)
565 {
566 return config_writer_close_element(writer->writer);
567 }
568
569 LTTNG_HIDDEN
570 int mi_lttng_close_multi_element(struct mi_writer *writer,
571 unsigned int nb_element)
572 {
573 int ret, i;
574
575 if (nb_element < 1) {
576 ret = 0;
577 goto end;
578 }
579 for (i = 0; i < nb_element; i++) {
580 ret = mi_lttng_writer_close_element(writer);
581 if (ret) {
582 goto end;
583 }
584 }
585 end:
586 return ret;
587 }
588
589 LTTNG_HIDDEN
590 int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
591 const char *element_name, uint64_t value)
592 {
593 return config_writer_write_element_unsigned_int(writer->writer,
594 element_name, value);
595 }
596
597 LTTNG_HIDDEN
598 int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
599 const char *element_name, int64_t value)
600 {
601 return config_writer_write_element_signed_int(writer->writer,
602 element_name, value);
603 }
604
605 LTTNG_HIDDEN
606 int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
607 const char *element_name, int value)
608 {
609 return config_writer_write_element_bool(writer->writer,
610 element_name, value);
611 }
612
613 LTTNG_HIDDEN
614 int mi_lttng_writer_write_element_string(struct mi_writer *writer,
615 const char *element_name, const char *value)
616 {
617 return config_writer_write_element_string(writer->writer,
618 element_name, value);
619 }
620
621 LTTNG_HIDDEN
622 int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
623 const char *lttng_description, const char *lttng_license)
624 {
625 int ret;
626
627 /* Open version */
628 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
629 if (ret) {
630 goto end;
631 }
632
633 /* Version string (contain info like rc etc.) */
634 ret = mi_lttng_writer_write_element_string(writer,
635 mi_lttng_element_version_str, version->version);
636 if (ret) {
637 goto end;
638 }
639
640 /* Major version number */
641 ret = mi_lttng_writer_write_element_unsigned_int(writer,
642 mi_lttng_element_version_major, version->version_major);
643 if (ret) {
644 goto end;
645 }
646
647 /* Minor version number */
648 ret = mi_lttng_writer_write_element_unsigned_int(writer,
649 mi_lttng_element_version_minor, version->version_minor);
650 if (ret) {
651 goto end;
652 }
653
654 /* Commit version number */
655 ret = mi_lttng_writer_write_element_string(writer,
656 mi_lttng_element_version_commit, version->version_commit);
657 if (ret) {
658 goto end;
659 }
660
661 /* Patch number */
662 ret = mi_lttng_writer_write_element_unsigned_int(writer,
663 mi_lttng_element_version_patch_level, version->version_patchlevel);
664 if (ret) {
665 goto end;
666 }
667
668 /* Name of the version */
669 ret = mi_lttng_writer_write_element_string(writer,
670 config_element_name, version->version_name);
671 if (ret) {
672 goto end;
673 }
674
675 /* Description mostly related to beer... */
676 ret = mi_lttng_writer_write_element_string(writer,
677 mi_lttng_element_version_description, lttng_description);
678 if (ret) {
679 goto end;
680 }
681
682 /* url */
683 ret = mi_lttng_writer_write_element_string(writer,
684 mi_lttng_element_version_web, version->package_url);
685 if (ret) {
686 goto end;
687 }
688
689 /* License: free as in free beer...no...*speech* */
690 ret = mi_lttng_writer_write_element_string(writer,
691 mi_lttng_element_version_license, lttng_license);
692 if (ret) {
693 goto end;
694 }
695
696 /* Close version element */
697 ret = mi_lttng_writer_close_element(writer);
698
699 end:
700 return ret;
701 }
702
703 LTTNG_HIDDEN
704 int mi_lttng_sessions_open(struct mi_writer *writer)
705 {
706 return mi_lttng_writer_open_element(writer, config_element_sessions);
707 }
708
709 LTTNG_HIDDEN
710 int mi_lttng_session(struct mi_writer *writer,
711 struct lttng_session *session, int is_open)
712 {
713 int ret;
714
715 assert(session);
716
717 /* Open sessions element */
718 ret = mi_lttng_writer_open_element(writer,
719 config_element_session);
720 if (ret) {
721 goto end;
722 }
723
724 /* Name of the session */
725 ret = mi_lttng_writer_write_element_string(writer,
726 config_element_name, session->name);
727 if (ret) {
728 goto end;
729 }
730
731 /* Path */
732 ret = mi_lttng_writer_write_element_string(writer,
733 config_element_path, session->path);
734 if (ret) {
735 goto end;
736 }
737
738 /* Enabled ? */
739 ret = mi_lttng_writer_write_element_bool(writer,
740 config_element_enabled, session->enabled);
741 if (ret) {
742 goto end;
743 }
744
745 /* Snapshot mode */
746 ret = mi_lttng_writer_write_element_unsigned_int(writer,
747 config_element_snapshot_mode, session->snapshot_mode);
748 if (ret) {
749 goto end;
750 }
751
752 /* Live timer interval in usec */
753 ret = mi_lttng_writer_write_element_unsigned_int(writer,
754 config_element_live_timer_interval,
755 session->live_timer_interval);
756 if (ret) {
757 goto end;
758 }
759
760 if (!is_open) {
761 /* Closing session element */
762 ret = mi_lttng_writer_close_element(writer);
763 }
764 end:
765 return ret;
766
767 }
768
769 LTTNG_HIDDEN
770 int mi_lttng_domains_open(struct mi_writer *writer)
771 {
772 return mi_lttng_writer_open_element(writer, config_element_domains);
773 }
774
775 LTTNG_HIDDEN
776 int mi_lttng_domain(struct mi_writer *writer,
777 struct lttng_domain *domain, int is_open)
778 {
779 int ret = 0;
780 const char *str_domain;
781 const char *str_buffer;
782
783 assert(domain);
784
785 /* Open domain element */
786 ret = mi_lttng_writer_open_element(writer, config_element_domain);
787 if (ret) {
788 goto end;
789 }
790
791 /* Domain Type */
792 str_domain = mi_lttng_domaintype_string(domain->type);
793 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
794 str_domain);
795 if (ret) {
796 goto end;
797 }
798
799 /* Buffer Type */
800 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
801 ret = mi_lttng_writer_write_element_string(writer,
802 config_element_buffer_type, str_buffer);
803 if (ret) {
804 goto end;
805 }
806
807 /* TODO: union attr
808 * This union is not currently used and was added for
809 * future ust domain support.
810 * Date: 25-06-2014
811 * */
812
813 if (!is_open) {
814 /* Closing domain element */
815 ret = mi_lttng_writer_close_element(writer);
816 }
817
818 end:
819 return ret;
820
821 }
822
823 LTTNG_HIDDEN
824 int mi_lttng_channels_open(struct mi_writer *writer)
825 {
826 return mi_lttng_writer_open_element(writer, config_element_channels);
827 }
828
829 LTTNG_HIDDEN
830 int mi_lttng_channel(struct mi_writer *writer,
831 struct lttng_channel *channel, int is_open)
832 {
833 int ret = 0;
834
835 assert(channel);
836
837 /* Opening channel element */
838 ret = mi_lttng_writer_open_element(writer, config_element_channel);
839 if (ret) {
840 goto end;
841 }
842
843 /* Name */
844 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
845 channel->name);
846 if (ret) {
847 goto end;
848 }
849
850 /* Enabled ? */
851 ret = mi_lttng_writer_write_element_bool(writer,
852 config_element_enabled, channel->enabled);
853 if (ret) {
854 goto end;
855 }
856
857 /* Attribute */
858 ret = mi_lttng_channel_attr(writer, &channel->attr);
859 if (ret) {
860 goto end;
861 }
862
863 if (!is_open) {
864 /* Closing channel element */
865 ret = mi_lttng_writer_close_element(writer);
866 if (ret) {
867 goto end;
868 }
869 }
870 end:
871 return ret;
872 }
873
874 LTTNG_HIDDEN
875 int mi_lttng_channel_attr(struct mi_writer *writer,
876 struct lttng_channel_attr *attr)
877 {
878 int ret = 0;
879 struct lttng_channel *chan = caa_container_of(attr,
880 struct lttng_channel, attr);
881 uint64_t discarded_events, lost_packets, monitor_timer_interval;
882 int64_t blocking_timeout;
883
884 assert(attr);
885
886 ret = lttng_channel_get_discarded_event_count(chan, &discarded_events);
887 if (ret) {
888 goto end;
889 }
890
891 ret = lttng_channel_get_lost_packet_count(chan, &lost_packets);
892 if (ret) {
893 goto end;
894 }
895
896 ret = lttng_channel_get_monitor_timer_interval(chan,
897 &monitor_timer_interval);
898 if (ret) {
899 goto end;
900 }
901
902 ret = lttng_channel_get_blocking_timeout(chan,
903 &blocking_timeout);
904 if (ret) {
905 goto end;
906 }
907
908 /* Opening Attributes */
909 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
910 if (ret) {
911 goto end;
912 }
913
914 /* Overwrite */
915 ret = mi_lttng_writer_write_element_string(writer,
916 config_element_overwrite_mode,
917 attr->overwrite ? config_overwrite_mode_overwrite :
918 config_overwrite_mode_discard);
919 if (ret) {
920 goto end;
921 }
922
923 /* Sub buffer size in byte */
924 ret = mi_lttng_writer_write_element_unsigned_int(writer,
925 config_element_subbuf_size, attr->subbuf_size);
926 if (ret) {
927 goto end;
928 }
929
930 /* Number of subbuffer (power of two) */
931 ret = mi_lttng_writer_write_element_unsigned_int(writer,
932 config_element_num_subbuf,
933 attr->num_subbuf);
934 if (ret) {
935 goto end;
936 }
937
938 /* Switch timer interval in usec */
939 ret = mi_lttng_writer_write_element_unsigned_int(writer,
940 config_element_switch_timer_interval,
941 attr->switch_timer_interval);
942 if (ret) {
943 goto end;
944 }
945
946 /* Read timer interval in usec */
947 ret = mi_lttng_writer_write_element_unsigned_int(writer,
948 config_element_read_timer_interval,
949 attr->read_timer_interval);
950 if (ret) {
951 goto end;
952 }
953
954 /* Monitor timer interval in usec */
955 ret = mi_lttng_writer_write_element_unsigned_int(writer,
956 config_element_monitor_timer_interval,
957 monitor_timer_interval);
958 if (ret) {
959 goto end;
960 }
961
962 /* Retry timeout in usec */
963 ret = mi_lttng_writer_write_element_signed_int(writer,
964 config_element_blocking_timeout,
965 blocking_timeout);
966 if (ret) {
967 goto end;
968 }
969
970 /* Event output */
971 ret = mi_lttng_writer_write_element_string(writer,
972 config_element_output_type,
973 attr->output == LTTNG_EVENT_SPLICE ?
974 config_output_type_splice : config_output_type_mmap);
975 if (ret) {
976 goto end;
977 }
978
979 /* Tracefile size in bytes */
980 ret = mi_lttng_writer_write_element_unsigned_int(writer,
981 config_element_tracefile_size, attr->tracefile_size);
982 if (ret) {
983 goto end;
984 }
985
986 /* Count of tracefiles */
987 ret = mi_lttng_writer_write_element_unsigned_int(writer,
988 config_element_tracefile_count,
989 attr->tracefile_count);
990 if (ret) {
991 goto end;
992 }
993
994 /* Live timer interval in usec*/
995 ret = mi_lttng_writer_write_element_unsigned_int(writer,
996 config_element_live_timer_interval,
997 attr->live_timer_interval);
998 if (ret) {
999 goto end;
1000 }
1001
1002 /* Discarded events */
1003 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1004 config_element_discarded_events,
1005 discarded_events);
1006 if (ret) {
1007 goto end;
1008 }
1009
1010 /* Lost packets */
1011 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1012 config_element_lost_packets,
1013 lost_packets);
1014 if (ret) {
1015 goto end;
1016 }
1017
1018 /* Closing attributes */
1019 ret = mi_lttng_writer_close_element(writer);
1020 if (ret) {
1021 goto end;
1022 }
1023 end:
1024 return ret;
1025
1026 }
1027
1028 LTTNG_HIDDEN
1029 int mi_lttng_event_common_attributes(struct mi_writer *writer,
1030 struct lttng_event *event)
1031 {
1032 int ret;
1033 const char *filter_expression;
1034
1035 /* Open event element */
1036 ret = mi_lttng_writer_open_element(writer, config_element_event);
1037 if (ret) {
1038 goto end;
1039 }
1040
1041 /* Event name */
1042 ret = mi_lttng_writer_write_element_string(writer,
1043 config_element_name, event->name);
1044 if (ret) {
1045 goto end;
1046 }
1047
1048 /* Event type */
1049 ret = mi_lttng_writer_write_element_string(writer,
1050 config_element_type, mi_lttng_eventtype_string(event->type));
1051 if (ret) {
1052 goto end;
1053 }
1054
1055 /* Is event enabled */
1056 ret = mi_lttng_writer_write_element_bool(writer,
1057 config_element_enabled, event->enabled);
1058 if (ret) {
1059 goto end;
1060 }
1061
1062 /* Event filter expression */
1063 ret = lttng_event_get_filter_expression(event, &filter_expression);
1064 if (ret) {
1065 goto end;
1066 }
1067
1068 if (filter_expression) {
1069 ret = mi_lttng_writer_write_element_string(writer,
1070 config_element_filter_expression,
1071 filter_expression);
1072 if (ret) {
1073 goto end;
1074 }
1075 }
1076
1077 end:
1078 return ret;
1079 }
1080
1081 static int write_event_exclusions(struct mi_writer *writer,
1082 struct lttng_event *event)
1083 {
1084 int i;
1085 int ret;
1086 int exclusion_count;
1087
1088 /* Open event exclusions */
1089 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
1090 if (ret) {
1091 goto end;
1092 }
1093
1094 exclusion_count = lttng_event_get_exclusion_name_count(event);
1095 if (exclusion_count < 0) {
1096 ret = exclusion_count;
1097 goto end;
1098 }
1099
1100 for (i = 0; i < exclusion_count; i++) {
1101 const char *name;
1102
1103 ret = lttng_event_get_exclusion_name(event, i, &name);
1104 if (ret) {
1105 /* Close exclusions */
1106 mi_lttng_writer_close_element(writer);
1107 goto end;
1108 }
1109
1110 ret = mi_lttng_writer_write_element_string(writer,
1111 config_element_exclusion, name);
1112 if (ret) {
1113 /* Close exclusions */
1114 mi_lttng_writer_close_element(writer);
1115 goto end;
1116 }
1117 }
1118
1119 /* Close exclusions */
1120 ret = mi_lttng_writer_close_element(writer);
1121
1122 end:
1123 return ret;
1124 }
1125
1126 LTTNG_HIDDEN
1127 int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
1128 struct lttng_event *event, enum lttng_domain_type domain)
1129 {
1130 int ret;
1131
1132 /* Event loglevel */
1133 ret = mi_lttng_writer_write_element_string(writer,
1134 config_element_loglevel,
1135 mi_lttng_loglevel_string(event->loglevel, domain));
1136 if (ret) {
1137 goto end;
1138 }
1139
1140 /* Log level type */
1141 ret = mi_lttng_writer_write_element_string(writer,
1142 config_element_loglevel_type,
1143 mi_lttng_logleveltype_string(event->loglevel_type));
1144 if (ret) {
1145 goto end;
1146 }
1147
1148 /* Event exclusions */
1149 ret = write_event_exclusions(writer, event);
1150
1151 end:
1152 return ret;
1153 }
1154
1155 LTTNG_HIDDEN
1156 int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
1157 struct lttng_event *event)
1158 {
1159 /* event exclusion filter */
1160 return write_event_exclusions(writer, event);
1161 }
1162
1163 LTTNG_HIDDEN
1164 int mi_lttng_event_function_probe(struct mi_writer *writer,
1165 struct lttng_event *event)
1166 {
1167 int ret;
1168
1169 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1170 if (ret) {
1171 goto end;
1172 }
1173
1174 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1175 if (ret) {
1176 goto end;
1177 }
1178
1179 if (event->attr.probe.addr != 0) {
1180 /* event probe address */
1181 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1182 config_element_address, event->attr.probe.addr);
1183 if (ret) {
1184 goto end;
1185 }
1186 } else {
1187 /* event probe offset */
1188 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1189 config_element_offset, event->attr.probe.offset);
1190 if (ret) {
1191 goto end;
1192 }
1193
1194 /* event probe symbol_name */
1195 ret = mi_lttng_writer_write_element_string(writer,
1196 config_element_symbol_name, event->attr.probe.symbol_name);
1197 if (ret) {
1198 goto end;
1199 }
1200 }
1201
1202 /* Close probe_attributes and attributes */
1203 ret = mi_lttng_close_multi_element(writer, 2);
1204 end:
1205 return ret;
1206 }
1207
1208 LTTNG_HIDDEN
1209 int mi_lttng_event_function_entry(struct mi_writer *writer,
1210 struct lttng_event *event)
1211 {
1212 int ret;
1213
1214 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1215 if (ret) {
1216 goto end;
1217 }
1218
1219 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1220 if (ret) {
1221 goto end;
1222 }
1223
1224 /* event probe symbol_name */
1225 ret = mi_lttng_writer_write_element_string(writer,
1226 config_element_symbol_name, event->attr.ftrace.symbol_name);
1227 if (ret) {
1228 goto end;
1229 }
1230
1231 /* Close function_attributes and attributes */
1232 ret = mi_lttng_close_multi_element(writer, 2);
1233 end:
1234 return ret;
1235 }
1236
1237 LTTNG_HIDDEN
1238 int mi_lttng_events_open(struct mi_writer *writer)
1239 {
1240 return mi_lttng_writer_open_element(writer, config_element_events);
1241 }
1242
1243 LTTNG_HIDDEN
1244 int mi_lttng_event(struct mi_writer *writer,
1245 struct lttng_event *event, int is_open, enum lttng_domain_type domain)
1246 {
1247 int ret;
1248
1249 ret = mi_lttng_event_common_attributes(writer, event);
1250 if (ret) {
1251 goto end;
1252 }
1253
1254 switch (event->type) {
1255 case LTTNG_EVENT_TRACEPOINT:
1256 {
1257 if (event->loglevel != -1) {
1258 ret = mi_lttng_event_tracepoint_loglevel(writer, event, domain);
1259 } else {
1260 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
1261 }
1262 break;
1263 }
1264 case LTTNG_EVENT_FUNCTION:
1265 /* Fallthrough */
1266 case LTTNG_EVENT_PROBE:
1267 ret = mi_lttng_event_function_probe(writer, event);
1268 break;
1269 case LTTNG_EVENT_FUNCTION_ENTRY:
1270 ret = mi_lttng_event_function_entry(writer, event);
1271 break;
1272 case LTTNG_EVENT_ALL:
1273 /* Fallthrough */
1274 default:
1275 break;
1276 }
1277
1278 if (!is_open) {
1279 ret = mi_lttng_writer_close_element(writer);
1280 }
1281
1282 end:
1283 return ret;
1284 }
1285
1286 LTTNG_HIDDEN
1287 int mi_lttng_trackers_open(struct mi_writer *writer)
1288 {
1289 return mi_lttng_writer_open_element(writer, config_element_trackers);
1290 }
1291
1292 LTTNG_HIDDEN
1293 int mi_lttng_pid_tracker_open(struct mi_writer *writer)
1294 {
1295 int ret;
1296
1297 /* Open element pid_tracker */
1298 ret = mi_lttng_writer_open_element(writer, config_element_pid_tracker);
1299 if (ret) {
1300 goto end;
1301 }
1302
1303 /* Open targets element */
1304 ret = mi_lttng_targets_open(writer);
1305 end:
1306 return ret;
1307 }
1308
1309 LTTNG_HIDDEN
1310 int mi_lttng_pids_open(struct mi_writer *writer)
1311 {
1312 return mi_lttng_writer_open_element(writer, config_element_pids);
1313 }
1314
1315 /*
1316 * TODO: move the listing of pid for user agent to process semantic on
1317 * mi api bump. The use of process element break the mi api.
1318 */
1319 LTTNG_HIDDEN
1320 int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *name,
1321 int is_open)
1322 {
1323 int ret;
1324
1325 /* Open pid process */
1326 ret = mi_lttng_writer_open_element(writer, config_element_pid);
1327 if (ret) {
1328 goto end;
1329 }
1330
1331 /* Writing pid number */
1332 ret = mi_lttng_writer_write_element_signed_int(writer,
1333 mi_lttng_element_pid_id, (int)pid);
1334 if (ret) {
1335 goto end;
1336 }
1337
1338 /* Writing name of the process */
1339 if (name) {
1340 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1341 name);
1342 if (ret) {
1343 goto end;
1344 }
1345 }
1346
1347 if (!is_open) {
1348 /* Closing Pid */
1349 ret = mi_lttng_writer_close_element(writer);
1350 }
1351
1352 end:
1353 return ret;
1354 }
1355
1356 LTTNG_HIDDEN
1357 int mi_lttng_targets_open(struct mi_writer *writer)
1358 {
1359 return mi_lttng_writer_open_element(writer,
1360 config_element_targets);
1361 }
1362
1363 LTTNG_HIDDEN
1364 int mi_lttng_pid_target(struct mi_writer *writer, pid_t pid, int is_open)
1365 {
1366 int ret;
1367
1368 ret = mi_lttng_writer_open_element(writer,
1369 config_element_target_pid);
1370 if (ret) {
1371 goto end;
1372 }
1373
1374 /* Writing pid number
1375 * Special case for element all on track untrack command
1376 * All pid is represented as wildcard *
1377 */
1378 if ((int) pid == -1) {
1379 ret = mi_lttng_writer_write_element_string(writer,
1380 config_element_pid,
1381 mi_lttng_element_track_untrack_all_wildcard);
1382 } else {
1383 ret = mi_lttng_writer_write_element_signed_int(writer,
1384 config_element_pid, (int) pid);
1385 }
1386 if (ret) {
1387 goto end;
1388 }
1389
1390 if (!is_open) {
1391 ret = mi_lttng_writer_close_element(writer);
1392 if (ret) {
1393 goto end;
1394 }
1395 }
1396
1397 end:
1398 return ret;
1399 }
1400
1401 LTTNG_HIDDEN
1402 int mi_lttng_event_fields_open(struct mi_writer *writer)
1403 {
1404 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1405 }
1406
1407 LTTNG_HIDDEN
1408 int mi_lttng_event_field(struct mi_writer *writer,
1409 struct lttng_event_field *field)
1410 {
1411 int ret;
1412
1413 if (!field->field_name[0]) {
1414 ret = 0;
1415 goto end;
1416 }
1417
1418 /* Open field */
1419 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1420 if (ret) {
1421 goto end;
1422 }
1423
1424 if (!field->field_name[0]) {
1425 goto close;
1426 }
1427
1428 /* Name */
1429 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1430 field->field_name);
1431 if (ret) {
1432 goto end;
1433 }
1434
1435 /* Type */
1436 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1437 mi_lttng_eventfieldtype_string(field->type));
1438 if (ret) {
1439 goto end;
1440 }
1441
1442 /* nowrite */
1443 ret = mi_lttng_writer_write_element_signed_int(writer,
1444 mi_lttng_element_nowrite, field->nowrite);
1445 if (ret) {
1446 goto end;
1447 }
1448
1449 close:
1450 /* Close field element */
1451 ret = mi_lttng_writer_close_element(writer);
1452
1453 end:
1454 return ret;
1455 }
1456
1457 LTTNG_HIDDEN
1458 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1459 struct lttng_event_perf_counter_ctx *perf_context)
1460 {
1461 int ret;
1462
1463 /* Open perf_counter_context */
1464 ret = mi_lttng_writer_open_element(writer,
1465 mi_lttng_element_perf_counter_context);
1466 if (ret) {
1467 goto end;
1468 }
1469
1470 /* Type */
1471 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1472 config_element_type, perf_context->type);
1473 if (ret) {
1474 goto end;
1475 }
1476
1477 /* Config */
1478 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1479 config_element_config, perf_context->config);
1480 if (ret) {
1481 goto end;
1482 }
1483
1484 /* Name of the perf counter */
1485 ret = mi_lttng_writer_write_element_string(writer,
1486 config_element_name, perf_context->name);
1487 if (ret) {
1488 goto end;
1489 }
1490
1491 /* Close perf_counter_context */
1492 ret = mi_lttng_writer_close_element(writer);
1493 end:
1494 return ret;
1495 }
1496
1497 static
1498 int mi_lttng_app_context(struct mi_writer *writer,
1499 const char *provider_name, const char *ctx_name)
1500 {
1501 int ret;
1502
1503 /* Open app */
1504 ret = mi_lttng_writer_open_element(writer,
1505 config_element_context_app);
1506 if (ret) {
1507 goto end;
1508 }
1509
1510 /* provider_name */
1511 ret = mi_lttng_writer_write_element_string(writer,
1512 config_element_context_app_provider_name,
1513 provider_name);
1514 if (ret) {
1515 goto end;
1516 }
1517
1518 /* ctx_name */
1519 ret = mi_lttng_writer_write_element_string(writer,
1520 config_element_context_app_ctx_name, ctx_name);
1521 if (ret) {
1522 goto end;
1523 }
1524
1525 /* Close app */
1526 ret = mi_lttng_writer_close_element(writer);
1527 end:
1528 return ret;
1529 }
1530
1531 LTTNG_HIDDEN
1532 int mi_lttng_context(struct mi_writer *writer,
1533 struct lttng_event_context *context, int is_open)
1534 {
1535 int ret;
1536
1537 /* Open context */
1538 ret = mi_lttng_writer_open_element(writer , config_element_context);
1539 if (ret) {
1540 goto end;
1541 }
1542
1543 /* Special case for PERF_*_COUNTER
1544 * print the lttng_event_perf_counter_ctx*/
1545 switch (context->ctx) {
1546 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1547 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1548 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1549 {
1550 struct lttng_event_perf_counter_ctx *perf_context =
1551 &context->u.perf_counter;
1552 ret = mi_lttng_perf_counter_context(writer, perf_context);
1553 if (ret) {
1554 goto end;
1555 }
1556 break;
1557 }
1558 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1559 {
1560 ret = mi_lttng_app_context(writer,
1561 context->u.app_ctx.provider_name,
1562 context->u.app_ctx.ctx_name);
1563 if (ret) {
1564 goto end;
1565 }
1566 break;
1567 }
1568 default:
1569 {
1570 const char *type_string = mi_lttng_event_contexttype_string(
1571 context->ctx);
1572 if (!type_string) {
1573 ret = -LTTNG_ERR_INVALID;
1574 goto end;
1575 }
1576
1577 /* Print context type */
1578 ret = mi_lttng_writer_write_element_string(writer,
1579 config_element_type, type_string);
1580 break;
1581 }
1582 }
1583
1584 /* Close context */
1585 if (!is_open) {
1586 ret = mi_lttng_writer_close_element(writer);
1587 }
1588
1589 end:
1590 return ret;
1591 }
1592
1593 LTTNG_HIDDEN
1594 int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
1595 const char *session_name)
1596 {
1597 int ret;
1598
1599 /* Open session element */
1600 ret = mi_lttng_writer_open_element(writer, config_element_session);
1601 if (ret) {
1602 goto end;
1603 }
1604
1605 /* Snapshot output list for current session name */
1606 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1607 session_name);
1608 if (ret) {
1609 goto end;
1610 }
1611
1612 /* Open element snapshots (sequence one snapshot) */
1613 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
1614 if (ret) {
1615 goto end;
1616 }
1617
1618 end:
1619 return ret;
1620 }
1621
1622 LTTNG_HIDDEN
1623 int mi_lttng_snapshot_list_output(struct mi_writer *writer,
1624 struct lttng_snapshot_output *output)
1625 {
1626 int ret;
1627
1628 /* Open element snapshot output */
1629 ret = mi_lttng_writer_open_element(writer,
1630 mi_lttng_element_command_snapshot);
1631 if (ret) {
1632 goto end;
1633 }
1634
1635 /* ID of the snapshot output */
1636 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1637 mi_lttng_element_id, output->id);
1638 if (ret) {
1639 goto end;
1640 }
1641
1642 /* Name of the output */
1643 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1644 output->name);
1645 if (ret) {
1646 goto end;
1647 }
1648
1649 /* Destination of the output (ctrl_url)*/
1650 ret = mi_lttng_writer_write_element_string(writer,
1651 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1652 if (ret) {
1653 goto end;
1654 }
1655
1656 /* Destination of the output (data_url) */
1657 ret = mi_lttng_writer_write_element_string(writer,
1658 mi_lttng_element_snapshot_data_url, output->data_url);
1659 if (ret) {
1660 goto end;
1661 }
1662
1663 /* total size of all stream combined */
1664 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1665 mi_lttng_element_snapshot_max_size, output->max_size);
1666 if (ret) {
1667 goto end;
1668 }
1669
1670 /* Close snapshot output element */
1671 ret = mi_lttng_writer_close_element(writer);
1672
1673 end:
1674 return ret;
1675 }
1676
1677 LTTNG_HIDDEN
1678 int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
1679 const char *name, const char *current_session_name)
1680 {
1681 int ret;
1682
1683 /* Open element del_snapshot */
1684 ret = mi_lttng_writer_open_element(writer,
1685 mi_lttng_element_command_snapshot);
1686 if (ret) {
1687 goto end;
1688 }
1689
1690
1691 if (id != UINT32_MAX) {
1692 /* "Snapshot output "id" successfully deleted
1693 * for "current_session_name"
1694 * ID of the snapshot output
1695 */
1696 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1697 mi_lttng_element_id, id);
1698 if (ret) {
1699 goto end;
1700 }
1701 } else {
1702 /* "Snapshot output "name" successfully deleted
1703 * for session "current_session_name"
1704 * Name of the output
1705 */
1706 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1707 name);
1708 if (ret) {
1709 goto end;
1710 }
1711 }
1712
1713 /* Snapshot was deleted for session "current_session_name"*/
1714 ret = mi_lttng_writer_write_element_string(writer,
1715 mi_lttng_element_snapshot_session_name,
1716 current_session_name);
1717 if (ret) {
1718 goto end;
1719 }
1720
1721 /* Close snapshot element */
1722 ret = mi_lttng_writer_close_element(writer);
1723
1724 end:
1725 return ret;
1726 }
1727
1728 LTTNG_HIDDEN
1729 int mi_lttng_snapshot_add_output(struct mi_writer *writer,
1730 const char *current_session_name, const char *n_ptr,
1731 struct lttng_snapshot_output *output)
1732 {
1733 int ret;
1734
1735 /* Open element snapshot */
1736 ret = mi_lttng_writer_open_element(writer,
1737 mi_lttng_element_command_snapshot);
1738 if (ret) {
1739 goto end;
1740 }
1741
1742 /* Snapshot output id */
1743 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1744 mi_lttng_element_id, output->id);
1745 if (ret) {
1746 goto end;
1747 }
1748
1749 /* Snapshot output names */
1750 ret = mi_lttng_writer_write_element_string(writer,
1751 config_element_name, n_ptr);
1752 if (ret) {
1753 goto end;
1754 }
1755
1756 /* Destination of the output (ctrl_url)*/
1757 ret = mi_lttng_writer_write_element_string(writer,
1758 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1759 if (ret) {
1760 goto end;
1761 }
1762
1763 /* Snapshot added for session "current_session_name"*/
1764 ret = mi_lttng_writer_write_element_string(writer,
1765 mi_lttng_element_snapshot_session_name, current_session_name);
1766 if (ret) {
1767 goto end;
1768 }
1769
1770 /* total size of all stream combined */
1771 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1772 mi_lttng_element_snapshot_max_size, output->max_size);
1773 if (ret) {
1774 goto end;
1775 }
1776
1777 /* Close snapshot element */
1778 ret = mi_lttng_writer_close_element(writer);
1779
1780 end:
1781 return ret;
1782 }
1783
1784 LTTNG_HIDDEN
1785 int mi_lttng_snapshot_record(struct mi_writer *writer,
1786 const char *current_session_name, const char *url,
1787 const char *cmdline_ctrl_url, const char *cmdline_data_url)
1788 {
1789 int ret;
1790
1791 /* Open element snapshot */
1792 ret = mi_lttng_writer_open_element(writer,
1793 mi_lttng_element_command_snapshot);
1794 if (ret) {
1795 goto end;
1796 }
1797
1798 /*
1799 * If a valid an URL was given, serialize it,
1800 * else take the command line data and ctrl urls*/
1801 if (url) {
1802 /* Destination of the output (ctrl_url)*/
1803 ret = mi_lttng_writer_write_element_string(writer,
1804 mi_lttng_element_snapshot_ctrl_url, url);
1805 if (ret) {
1806 goto end;
1807 }
1808 } else if (cmdline_ctrl_url) {
1809 /* Destination of the output (ctrl_url)*/
1810 ret = mi_lttng_writer_write_element_string(writer,
1811 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
1812 if (ret) {
1813 goto end;
1814 }
1815
1816 /* Destination of the output (data_url) */
1817 ret = mi_lttng_writer_write_element_string(writer,
1818 mi_lttng_element_snapshot_data_url, cmdline_data_url);
1819 if (ret) {
1820 goto end;
1821 }
1822 }
1823
1824 /* Close record_snapshot element */
1825 ret = mi_lttng_writer_close_element(writer);
1826
1827 end:
1828 return ret;
1829 }
This page took 0.068596 seconds and 6 git commands to generate.