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