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