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