Fix: parse_prob_opts return the actual success of the function
[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 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19
20 #include <include/config.h>
21 #include <common/config/config.h>
22 #include <lttng/snapshot-internal.h>
23 #include "mi-lttng.h"
24
25 #include <assert.h>
26
27 /* Strings related to command */
28 const char * const mi_lttng_element_command = "command";
29 const char * const mi_lttng_element_command_action = "snapshot_action";
30 const char * const mi_lttng_element_command_add_context = "add-context";
31 const char * const mi_lttng_element_command_calibrate = "calibrate";
32 const char * const mi_lttng_element_command_create = "create";
33 const char * const mi_lttng_element_command_destroy = "destroy";
34 const char * const mi_lttng_element_command_disable_channel = "disable-channel";
35 const char * const mi_lttng_element_command_disable_event = "disable-event";
36 const char * const mi_lttng_element_command_enable_channels = "enable-channel";
37 const char * const mi_lttng_element_command_enable_event = "enable-event";
38 const char * const mi_lttng_element_command_list = "list";
39 const char * const mi_lttng_element_command_load = "load";
40 const char * const mi_lttng_element_command_name = "name";
41 const char * const mi_lttng_element_command_output = "output";
42 const char * const mi_lttng_element_command_save = "save";
43 const char * const mi_lttng_element_command_set_session = "set-session";
44 const char * const mi_lttng_element_command_snapshot = "snapshot";
45 const char * const mi_lttng_element_command_snapshot_add = "add_snapshot";
46 const char * const mi_lttng_element_command_snapshot_del = "del_snapshot";
47 const char * const mi_lttng_element_command_snapshot_list = "list_snapshot";
48 const char * const mi_lttng_element_command_snapshot_record = "record_snapshot";
49 const char * const mi_lttng_element_command_start = "start";
50 const char * const mi_lttng_element_command_stop = "stop";
51 const char * const mi_lttng_element_command_success = "success";
52 const char * const mi_lttng_element_command_version = "version";
53
54 /* Strings related to version command */
55 const char * const mi_lttng_element_version = "version";
56 const char * const mi_lttng_element_version_commit = "commit";
57 const char * const mi_lttng_element_version_description = "description";
58 const char * const mi_lttng_element_version_license = "license";
59 const char * const mi_lttng_element_version_major = "major";
60 const char * const mi_lttng_element_version_minor = "minor";
61 const char * const mi_lttng_element_version_patch_level = "patchLevel";
62 const char * const mi_lttng_element_version_str = "string";
63 const char * const mi_lttng_element_version_web = "url";
64
65 /* String related to a lttng_event_field */
66 const char * const mi_lttng_element_event_field = "event_field";
67 const char * const mi_lttng_element_event_fields = "event_fields";
68
69 /* String related to lttng_event_context */
70 const char * const mi_lttng_context_type_perf_counter = "PERF_COUNTER";
71 const char * const mi_lttng_context_type_perf_cpu_counter = "PERF_CPU_COUNTER";
72 const char * const mi_lttng_context_type_perf_thread_counter = "PERF_THREAD_COUNTER";
73
74 /* String related to lttng_event_perf_counter_ctx */
75 const char * const mi_lttng_element_perf_counter_context = "perf_counter_context";
76
77 /* Strings related to pid */
78 const char * const mi_lttng_element_pids = "pids";
79 const char * const mi_lttng_element_pid = "pid";
80 const char * const mi_lttng_element_pid_id = "id";
81
82 /* Strings related to save command */
83 const char * const mi_lttng_element_save = "save";
84
85 /* Strings related to load command */
86 const char * const mi_lttng_element_load = "load";
87
88 /* General elements of mi_lttng */
89 const char * const mi_lttng_element_empty = "";
90 const char * const mi_lttng_element_id = "id";
91 const char * const mi_lttng_element_nowrite = "nowrite";
92 const char * const mi_lttng_element_success = "success";
93 const char * const mi_lttng_element_type_enum = "ENUM";
94 const char * const mi_lttng_element_type_float = "FLOAT";
95 const char * const mi_lttng_element_type_integer = "INTEGER";
96 const char * const mi_lttng_element_type_other = "OTHER";
97 const char * const mi_lttng_element_type_string = "STRING";
98
99 /* String related to loglevel */
100 const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
101 const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
102 const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
103 const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
104 const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
105 const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
106 const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
107 const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
108 const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
109 const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
110 const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
111 const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
112 const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
113 const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
114 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
115 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
116
117 /* String related to loglevel JUL */
118 const char * const mi_lttng_loglevel_str_jul_all = "JUL_ALL";
119 const char * const mi_lttng_loglevel_str_jul_config = "JUL_CONFIG";
120 const char * const mi_lttng_loglevel_str_jul_fine = "JUL_FINE";
121 const char * const mi_lttng_loglevel_str_jul_finer = "JUL_FINER";
122 const char * const mi_lttng_loglevel_str_jul_finest = "JUL_FINEST";
123 const char * const mi_lttng_loglevel_str_jul_info = "JUL_INFO";
124 const char * const mi_lttng_loglevel_str_jul_off = "JUL_OFF";
125 const char * const mi_lttng_loglevel_str_jul_severe = "JUL_SEVERE";
126 const char * const mi_lttng_loglevel_str_jul_warning = "JUL_WARNING";
127
128 /* String related to loglevel type */
129 const char * const mi_lttng_loglevel_type_all = "ALL";
130 const char * const mi_lttng_loglevel_type_range = "RANGE";
131 const char * const mi_lttng_loglevel_type_single = "SINGLE";
132 const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
133
134 /* String related to lttng_calibrate */
135 const char * const mi_lttng_element_calibrate = "calibrate";
136 const char * const mi_lttng_element_calibrate_function = "FUNCTION";
137
138 /* String related to a lttng_snapshot_output */
139 const char * const mi_lttng_element_snapshot_ctrl_url = "ctrl_url";
140 const char * const mi_lttng_element_snapshot_data_url = "data_url";
141 const char * const mi_lttng_element_snapshot_max_size = "max_size";
142 const char * const mi_lttng_element_snapshot_n_ptr = "n_ptr";
143 const char * const mi_lttng_element_snapshot_session_name = "session_name";
144 const char * const mi_lttng_element_snapshots = "snapshots";
145
146 /* This is a merge of jul loglevel and regular loglevel
147 * Those should never overlap by definition
148 * (see struct lttng_event loglevel)
149 */
150 const char *mi_lttng_loglevel_string(int value)
151 {
152 switch (value) {
153 case -1:
154 return mi_lttng_element_empty;
155 case LTTNG_LOGLEVEL_EMERG:
156 return mi_lttng_loglevel_str_emerg;
157 case LTTNG_LOGLEVEL_ALERT:
158 return mi_lttng_loglevel_str_alert;
159 case LTTNG_LOGLEVEL_CRIT:
160 return mi_lttng_loglevel_str_crit;
161 case LTTNG_LOGLEVEL_ERR:
162 return mi_lttng_loglevel_str_err;
163 case LTTNG_LOGLEVEL_WARNING:
164 return mi_lttng_loglevel_str_warning;
165 case LTTNG_LOGLEVEL_NOTICE:
166 return mi_lttng_loglevel_str_notice;
167 case LTTNG_LOGLEVEL_INFO:
168 return mi_lttng_loglevel_str_info;
169 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
170 return mi_lttng_loglevel_str_debug_system;
171 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
172 return mi_lttng_loglevel_str_debug_program;
173 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
174 return mi_lttng_loglevel_str_debug_process;
175 case LTTNG_LOGLEVEL_DEBUG_MODULE:
176 return mi_lttng_loglevel_str_debug_module;
177 case LTTNG_LOGLEVEL_DEBUG_UNIT:
178 return mi_lttng_loglevel_str_debug_unit;
179 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
180 return mi_lttng_loglevel_str_debug_function;
181 case LTTNG_LOGLEVEL_DEBUG_LINE:
182 return mi_lttng_loglevel_str_debug_line;
183 case LTTNG_LOGLEVEL_DEBUG:
184 return mi_lttng_loglevel_str_debug;
185 case LTTNG_LOGLEVEL_JUL_OFF:
186 return mi_lttng_loglevel_str_jul_off;
187 case LTTNG_LOGLEVEL_JUL_SEVERE:
188 return mi_lttng_loglevel_str_jul_severe;
189 case LTTNG_LOGLEVEL_JUL_WARNING:
190 return mi_lttng_loglevel_str_jul_warning;
191 case LTTNG_LOGLEVEL_JUL_INFO:
192 return mi_lttng_loglevel_str_jul_info;
193 case LTTNG_LOGLEVEL_JUL_CONFIG:
194 return mi_lttng_loglevel_str_jul_config;
195 case LTTNG_LOGLEVEL_JUL_FINE:
196 return mi_lttng_loglevel_str_jul_fine;
197 case LTTNG_LOGLEVEL_JUL_FINER:
198 return mi_lttng_loglevel_str_jul_finer;
199 case LTTNG_LOGLEVEL_JUL_FINEST:
200 return mi_lttng_loglevel_str_jul_finest;
201 case LTTNG_LOGLEVEL_JUL_ALL:
202 return mi_lttng_loglevel_str_jul_all;
203 default:
204 return mi_lttng_loglevel_str_unknown;
205 }
206 }
207
208 const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
209 {
210 switch (value) {
211 case LTTNG_EVENT_LOGLEVEL_ALL:
212 return mi_lttng_loglevel_type_all;
213 case LTTNG_EVENT_LOGLEVEL_RANGE:
214 return mi_lttng_loglevel_type_range;
215 case LTTNG_EVENT_LOGLEVEL_SINGLE:
216 return mi_lttng_loglevel_type_single;
217 default:
218 return mi_lttng_loglevel_type_unknown;
219 }
220 }
221
222 const char *mi_lttng_eventtype_string(enum lttng_event_type value)
223 {
224 switch (value) {
225 case LTTNG_EVENT_ALL:
226 return config_event_type_all;
227 case LTTNG_EVENT_TRACEPOINT:
228 return config_event_type_tracepoint;
229 case LTTNG_EVENT_PROBE:
230 return config_event_type_probe;
231 case LTTNG_EVENT_FUNCTION:
232 return config_event_type_function;
233 case LTTNG_EVENT_FUNCTION_ENTRY:
234 return config_event_type_function_entry;
235 case LTTNG_EVENT_SYSCALL:
236 return config_event_type_syscall;
237 case LTTNG_EVENT_NOOP:
238 return config_event_type_noop;
239 default:
240 return mi_lttng_element_empty;
241 }
242 }
243
244 const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
245 {
246 switch (val) {
247 case LTTNG_EVENT_CONTEXT_PID:
248 return config_event_context_pid;
249 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
250 return mi_lttng_context_type_perf_counter;
251 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
252 return mi_lttng_context_type_perf_thread_counter;
253 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
254 return mi_lttng_context_type_perf_cpu_counter;
255 case LTTNG_EVENT_CONTEXT_PROCNAME:
256 return config_event_context_procname;
257 case LTTNG_EVENT_CONTEXT_PRIO:
258 return config_event_context_prio;
259 case LTTNG_EVENT_CONTEXT_NICE:
260 return config_event_context_nice;
261 case LTTNG_EVENT_CONTEXT_VPID:
262 return config_event_context_vpid;
263 case LTTNG_EVENT_CONTEXT_TID:
264 return config_event_context_tid;
265 case LTTNG_EVENT_CONTEXT_VTID:
266 return config_event_context_vtid;
267 case LTTNG_EVENT_CONTEXT_PPID:
268 return config_event_context_ppid;
269 case LTTNG_EVENT_CONTEXT_VPPID:
270 return config_event_context_vppid;
271 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
272 return config_event_context_pthread_id;
273 case LTTNG_EVENT_CONTEXT_HOSTNAME:
274 return config_event_context_hostname;
275 case LTTNG_EVENT_CONTEXT_IP:
276 return config_event_context_ip;
277 default:
278 return NULL;
279 }
280 }
281
282 const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
283 {
284 switch (val) {
285 case(LTTNG_EVENT_FIELD_INTEGER):
286 return mi_lttng_element_type_integer;
287 case(LTTNG_EVENT_FIELD_ENUM):
288 return mi_lttng_element_type_enum;
289 case(LTTNG_EVENT_FIELD_FLOAT):
290 return mi_lttng_element_type_float;
291 case(LTTNG_EVENT_FIELD_STRING):
292 return mi_lttng_element_type_string;
293 default:
294 return mi_lttng_element_type_other;
295 }
296 }
297
298 const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
299 {
300 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
301 switch (value) {
302 case LTTNG_DOMAIN_KERNEL:
303 return config_domain_type_kernel;
304 case LTTNG_DOMAIN_UST:
305 return config_domain_type_ust;
306 case LTTNG_DOMAIN_JUL:
307 return config_domain_type_jul;
308 case LTTNG_DOMAIN_LOG4J:
309 return config_domain_type_log4j;
310 default:
311 /* Should not have an unknown domain */
312 assert(0);
313 }
314 }
315
316 const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
317 {
318 switch (value) {
319 case LTTNG_BUFFER_PER_PID:
320 return config_buffer_type_per_pid;
321 case LTTNG_BUFFER_PER_UID:
322 return config_buffer_type_per_uid;
323 case LTTNG_BUFFER_GLOBAL:
324 return config_buffer_type_global;
325 default:
326 /* Should not have an unknow buffer type */
327 assert(0);
328 }
329 }
330
331 const char *mi_lttng_calibratetype_string(enum lttng_calibrate_type val)
332 {
333 const char *ret;
334
335 switch (val) {
336 case LTTNG_CALIBRATE_FUNCTION:
337 ret = mi_lttng_element_calibrate_function;
338 break;
339 default:
340 ret = mi_lttng_element_empty;
341 break;
342 }
343 return ret;
344 }
345
346 LTTNG_HIDDEN
347 struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
348 {
349 struct mi_writer *mi_writer;
350
351 mi_writer = zmalloc(sizeof(struct mi_writer));
352 if (!mi_writer) {
353 PERROR("zmalloc mi_writer_create");
354 goto end;
355 }
356 if (mi_output_type == LTTNG_MI_XML) {
357 mi_writer->writer = config_writer_create(fd_output, 0);
358 if (!mi_writer->writer) {
359 goto err_destroy;
360 }
361 mi_writer->type = LTTNG_MI_XML;
362 } else {
363 goto err_destroy;
364 }
365
366 end:
367 return mi_writer;
368
369 err_destroy:
370 free(mi_writer);
371 return NULL;
372 }
373
374 LTTNG_HIDDEN
375 int mi_lttng_writer_destroy(struct mi_writer *writer)
376 {
377 int ret;
378
379 if (!writer) {
380 ret = -EINVAL;
381 goto end;
382 }
383
384 ret = config_writer_destroy(writer->writer);
385 if (ret < 0) {
386 goto end;
387 }
388
389 free(writer);
390 end:
391 return ret;
392 }
393
394 LTTNG_HIDDEN
395 int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
396 {
397 int ret;
398
399 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command);
400 if (ret) {
401 goto end;
402 }
403 ret = mi_lttng_writer_write_element_string(writer,
404 mi_lttng_element_command_name, command);
405 end:
406 return ret;
407 }
408
409 LTTNG_HIDDEN
410 int mi_lttng_writer_command_close(struct mi_writer *writer)
411 {
412 return mi_lttng_writer_close_element(writer);
413 }
414
415 LTTNG_HIDDEN
416 int mi_lttng_writer_open_element(struct mi_writer *writer,
417 const char *element_name)
418 {
419 return config_writer_open_element(writer->writer, element_name);
420 }
421
422 LTTNG_HIDDEN
423 int mi_lttng_writer_close_element(struct mi_writer *writer)
424 {
425 return config_writer_close_element(writer->writer);
426 }
427
428 LTTNG_HIDDEN
429 int mi_lttng_close_multi_element(struct mi_writer *writer,
430 unsigned int nb_element)
431 {
432 int ret, i;
433
434 if (nb_element < 1) {
435 ret = 0;
436 goto end;
437 }
438 for (i = 0; i < nb_element; i++) {
439 ret = mi_lttng_writer_close_element(writer);
440 if (ret) {
441 goto end;
442 }
443 }
444 end:
445 return ret;
446 }
447
448 LTTNG_HIDDEN
449 int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
450 const char *element_name, uint64_t value)
451 {
452 return config_writer_write_element_unsigned_int(writer->writer,
453 element_name, value);
454 }
455
456 LTTNG_HIDDEN
457 int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
458 const char *element_name, int64_t value)
459 {
460 return config_writer_write_element_signed_int(writer->writer,
461 element_name, value);
462 }
463
464 LTTNG_HIDDEN
465 int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
466 const char *element_name, int value)
467 {
468 return config_writer_write_element_bool(writer->writer,
469 element_name, value);
470 }
471
472 LTTNG_HIDDEN
473 int mi_lttng_writer_write_element_string(struct mi_writer *writer,
474 const char *element_name, const char *value)
475 {
476 return config_writer_write_element_string(writer->writer,
477 element_name, value);
478 }
479
480 LTTNG_HIDDEN
481 int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
482 const char *lttng_description, const char *lttng_license)
483 {
484 int ret;
485
486 /* Open version */
487 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
488 if (ret) {
489 goto end;
490 }
491
492 /* Version string (contain info like rc etc.) */
493 ret = mi_lttng_writer_write_element_string(writer,
494 mi_lttng_element_version_str, version->version);
495 if (ret) {
496 goto end;
497 }
498
499 /* Major version number */
500 ret = mi_lttng_writer_write_element_unsigned_int(writer,
501 mi_lttng_element_version_major, version->version_major);
502 if (ret) {
503 goto end;
504 }
505
506 /* Minor version number */
507 ret = mi_lttng_writer_write_element_unsigned_int(writer,
508 mi_lttng_element_version_minor, version->version_minor);
509 if (ret) {
510 goto end;
511 }
512
513 /* Commit version number */
514 ret = mi_lttng_writer_write_element_string(writer,
515 mi_lttng_element_version_commit, version->version_commit);
516 if (ret) {
517 goto end;
518 }
519
520 /* Patch number */
521 ret = mi_lttng_writer_write_element_unsigned_int(writer,
522 mi_lttng_element_version_patch_level, version->version_patchlevel);
523 if (ret) {
524 goto end;
525 }
526
527 /* Name of the version */
528 ret = mi_lttng_writer_write_element_string(writer,
529 config_element_name, version->version_name);
530 if (ret) {
531 goto end;
532 }
533
534 /* Description mostly related to beer... */
535 ret = mi_lttng_writer_write_element_string(writer,
536 mi_lttng_element_version_description, lttng_description);
537 if (ret) {
538 goto end;
539 }
540
541 /* url */
542 ret = mi_lttng_writer_write_element_string(writer,
543 mi_lttng_element_version_web, version->package_url);
544 if (ret) {
545 goto end;
546 }
547
548 /* License: free as in free beer...no...*speech* */
549 ret = mi_lttng_writer_write_element_string(writer,
550 mi_lttng_element_version_license, lttng_license);
551 if (ret) {
552 goto end;
553 }
554
555 /* Close version element */
556 ret = mi_lttng_writer_close_element(writer);
557
558 end:
559 return ret;
560 }
561
562 LTTNG_HIDDEN
563 int mi_lttng_sessions_open(struct mi_writer *writer)
564 {
565 return mi_lttng_writer_open_element(writer, config_element_sessions);
566 }
567
568 LTTNG_HIDDEN
569 int mi_lttng_session(struct mi_writer *writer,
570 struct lttng_session *session, int is_open)
571 {
572 int ret;
573
574 assert(session);
575
576 /* Open sessions element */
577 ret = mi_lttng_writer_open_element(writer,
578 config_element_session);
579 if (ret) {
580 goto end;
581 }
582
583 /* Name of the session */
584 ret = mi_lttng_writer_write_element_string(writer,
585 config_element_name, session->name);
586 if (ret) {
587 goto end;
588 }
589
590 /* Path */
591 ret = mi_lttng_writer_write_element_string(writer,
592 config_element_path, session->path);
593 if (ret) {
594 goto end;
595 }
596
597 /* Enabled ? */
598 ret = mi_lttng_writer_write_element_bool(writer,
599 config_element_enabled, session->enabled);
600 if (ret) {
601 goto end;
602 }
603
604 /* Snapshot mode */
605 ret = mi_lttng_writer_write_element_unsigned_int(writer,
606 config_element_snapshot_mode, session->snapshot_mode);
607 if (ret) {
608 goto end;
609 }
610
611 /* Live timer interval in usec */
612 ret = mi_lttng_writer_write_element_unsigned_int(writer,
613 config_element_live_timer_interval,
614 session->live_timer_interval);
615 if (ret) {
616 goto end;
617 }
618
619 if (!is_open) {
620 /* Closing session element */
621 ret = mi_lttng_writer_close_element(writer);
622 }
623 end:
624 return ret;
625
626 }
627
628 LTTNG_HIDDEN
629 int mi_lttng_domains_open(struct mi_writer *writer)
630 {
631 return mi_lttng_writer_open_element(writer, config_element_domains);
632 }
633
634 LTTNG_HIDDEN
635 int mi_lttng_domain(struct mi_writer *writer,
636 struct lttng_domain *domain, int is_open)
637 {
638 int ret = 0;
639 const char *str_domain;
640 const char *str_buffer;
641
642 assert(domain);
643
644 /* Open domain element */
645 ret = mi_lttng_writer_open_element(writer, config_element_domain);
646 if (ret) {
647 goto end;
648 }
649
650 /* Domain Type */
651 str_domain = mi_lttng_domaintype_string(domain->type);
652 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
653 str_domain);
654 if (ret) {
655 goto end;
656 }
657
658 /* Buffer Type */
659 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
660 ret = mi_lttng_writer_write_element_string(writer,
661 config_element_buffer_type, str_buffer);
662 if (ret) {
663 goto end;
664 }
665
666 /* TODO: union attr
667 * This union is not currently used and was added for
668 * future ust domain support.
669 * Date: 25-06-2014
670 * */
671
672 if (!is_open) {
673 /* Closing domain element */
674 ret = mi_lttng_writer_close_element(writer);
675 }
676
677 end:
678 return ret;
679
680 }
681
682 LTTNG_HIDDEN
683 int mi_lttng_channels_open(struct mi_writer *writer)
684 {
685 return mi_lttng_writer_open_element(writer, config_element_channels);
686 }
687
688 LTTNG_HIDDEN
689 int mi_lttng_channel(struct mi_writer *writer,
690 struct lttng_channel *channel, int is_open)
691 {
692 int ret = 0;
693
694 assert(channel);
695
696 /* Opening channel element */
697 ret = mi_lttng_writer_open_element(writer, config_element_channel);
698 if (ret) {
699 goto end;
700 }
701
702 /* Name */
703 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
704 channel->name);
705 if (ret) {
706 goto end;
707 }
708
709 /* Enabled ? */
710 ret = mi_lttng_writer_write_element_bool(writer,
711 config_element_enabled, channel->enabled);
712 if (ret) {
713 goto end;
714 }
715
716 /* Attribute */
717 ret = mi_lttng_channel_attr(writer, &channel->attr);
718 if (ret) {
719 goto end;
720 }
721
722 if (!is_open) {
723 /* Closing channel element */
724 ret = mi_lttng_writer_close_element(writer);
725 if (ret) {
726 goto end;
727 }
728 }
729 end:
730 return ret;
731 }
732
733 LTTNG_HIDDEN
734 int mi_lttng_channel_attr(struct mi_writer *writer,
735 struct lttng_channel_attr *attr)
736 {
737 int ret = 0;
738
739 assert(attr);
740
741 /* Opening Attributes */
742 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
743 if (ret) {
744 goto end;
745 }
746
747 /* Overwrite */
748 ret = mi_lttng_writer_write_element_string(writer,
749 config_element_overwrite_mode,
750 attr->overwrite ? config_overwrite_mode_overwrite :
751 config_overwrite_mode_discard);
752 if (ret) {
753 goto end;
754 }
755
756 /* Sub buffer size in byte */
757 ret = mi_lttng_writer_write_element_unsigned_int(writer,
758 config_element_subbuf_size, attr->subbuf_size);
759 if (ret) {
760 goto end;
761 }
762
763 /* Number of subbuffer (power of two) */
764 ret = mi_lttng_writer_write_element_unsigned_int(writer,
765 config_element_num_subbuf,
766 attr->num_subbuf);
767 if (ret) {
768 goto end;
769 }
770
771 /* Switch timer interval in usec */
772 ret = mi_lttng_writer_write_element_unsigned_int(writer,
773 config_element_switch_timer_interval,
774 attr->switch_timer_interval);
775 if (ret) {
776 goto end;
777 }
778
779 /* Read timer interval in usec */
780 ret = mi_lttng_writer_write_element_unsigned_int(writer,
781 config_element_read_timer_interval,
782 attr->read_timer_interval);
783 if (ret) {
784 goto end;
785 }
786
787 /* Event output */
788 ret = mi_lttng_writer_write_element_string(writer,
789 config_element_output_type,
790 attr->output == LTTNG_EVENT_SPLICE ?
791 config_output_type_splice : config_output_type_mmap);
792 if (ret) {
793 goto end;
794 }
795
796 /* Tracefile size in bytes */
797 ret = mi_lttng_writer_write_element_unsigned_int(writer,
798 config_element_tracefile_size, attr->tracefile_size);
799 if (ret) {
800 goto end;
801 }
802
803 /* Count of tracefiles */
804 ret = mi_lttng_writer_write_element_unsigned_int(writer,
805 config_element_tracefile_count,
806 attr->tracefile_count);
807 if (ret) {
808 goto end;
809 }
810
811 /* Live timer interval in usec*/
812 ret = mi_lttng_writer_write_element_unsigned_int(writer,
813 config_element_live_timer_interval,
814 attr->live_timer_interval);
815 if (ret) {
816 goto end;
817 }
818
819 /* Closing attributes */
820 ret = mi_lttng_writer_close_element(writer);
821 if (ret) {
822 goto end;
823 }
824 end:
825 return ret;
826
827 }
828
829 LTTNG_HIDDEN
830 int mi_lttng_event_common_attributes(struct mi_writer *writer,
831 struct lttng_event *event)
832 {
833 int ret;
834
835 /* Open event element */
836 ret = mi_lttng_writer_open_element(writer, config_element_event);
837 if (ret) {
838 goto end;
839 }
840
841 /* Event name */
842 ret = mi_lttng_writer_write_element_string(writer,
843 config_element_name, event->name);
844 if (ret) {
845 goto end;
846 }
847
848 /* Event type */
849 ret = mi_lttng_writer_write_element_string(writer,
850 config_element_type, mi_lttng_eventtype_string(event->type));
851 if (ret) {
852 goto end;
853 }
854
855 /* Is event enabled */
856 ret = mi_lttng_writer_write_element_bool(writer,
857 config_element_enabled, event->enabled);
858 if (ret) {
859 goto end;
860 }
861
862 /* Event filter enabled? */
863 ret = mi_lttng_writer_write_element_bool(writer,
864 config_element_filter, event->filter);
865
866 end:
867 return ret;
868 }
869
870 LTTNG_HIDDEN
871 int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
872 struct lttng_event *event)
873 {
874 int ret;
875
876 /* Event loglevel */
877 ret = mi_lttng_writer_write_element_string(writer,
878 config_element_loglevel, mi_lttng_loglevel_string(event->loglevel));
879 if (ret) {
880 goto end;
881 }
882
883 /* Log level type */
884 ret = mi_lttng_writer_write_element_string(writer,
885 config_element_loglevel_type,
886 mi_lttng_logleveltype_string(event->loglevel_type));
887 if (ret) {
888 goto end;
889 }
890
891 /* event exclusion filter */
892 ret = mi_lttng_writer_write_element_bool(writer,
893 config_element_exclusion, event->exclusion);
894 if (ret) {
895 goto end;
896 }
897
898 end:
899 return ret;
900 }
901
902 LTTNG_HIDDEN
903 int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
904 struct lttng_event *event)
905 {
906 /* event exclusion filter */
907 return mi_lttng_writer_write_element_bool(writer,
908 config_element_exclusion, event->exclusion);
909 }
910
911 LTTNG_HIDDEN
912 int mi_lttng_event_function_probe(struct mi_writer *writer,
913 struct lttng_event *event)
914 {
915 int ret;
916
917 if (event->attr.probe.addr != 0) {
918 /* event probe address */
919 ret = mi_lttng_writer_write_element_unsigned_int(writer,
920 config_element_address, event->attr.probe.addr);
921 if (ret) {
922 goto end;
923 }
924 } else {
925 /* event probe offset */
926 ret = mi_lttng_writer_write_element_unsigned_int(writer,
927 config_element_offset, event->attr.probe.offset);
928 if (ret) {
929 goto end;
930 }
931
932 /* event probe symbol_name */
933 ret = mi_lttng_writer_write_element_string(writer,
934 config_element_symbol_name, event->attr.probe.symbol_name);
935 if (ret) {
936 goto end;
937 }
938 }
939 end:
940 return ret;
941 }
942
943 LTTNG_HIDDEN
944 int mi_lttng_event_function_entry(struct mi_writer *writer,
945 struct lttng_event *event)
946 {
947 /* event probe symbol_name */
948 return mi_lttng_writer_write_element_string(writer,
949 config_element_symbol_name, event->attr.ftrace.symbol_name);
950 }
951
952 LTTNG_HIDDEN
953 int mi_lttng_events_open(struct mi_writer *writer)
954 {
955 return mi_lttng_writer_open_element(writer, config_element_events);
956 }
957
958 LTTNG_HIDDEN
959 int mi_lttng_event(struct mi_writer *writer,
960 struct lttng_event *event, int is_open)
961 {
962 int ret;
963
964 ret = mi_lttng_event_common_attributes(writer, event);
965 if (ret) {
966 goto end;
967 }
968
969 switch (event->type) {
970 case LTTNG_EVENT_TRACEPOINT:
971 {
972 if (event->loglevel != -1) {
973 ret = mi_lttng_event_tracepoint_loglevel(writer, event);
974 } else {
975 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
976 }
977 break;
978 }
979 case LTTNG_EVENT_PROBE:
980 ret = mi_lttng_event_function_probe(writer, event);
981 break;
982 case LTTNG_EVENT_FUNCTION_ENTRY:
983 ret = mi_lttng_event_function_entry(writer, event);
984 break;
985 case LTTNG_EVENT_ALL:
986 /* Fallthrough */
987 default:
988 break;
989 }
990
991 if (!is_open) {
992 ret = mi_lttng_writer_close_element(writer);
993 }
994
995 end:
996 return ret;
997 }
998
999 LTTNG_HIDDEN
1000 int mi_lttng_pids_open(struct mi_writer *writer)
1001 {
1002 return mi_lttng_writer_open_element(writer, mi_lttng_element_pids);
1003 }
1004
1005 LTTNG_HIDDEN
1006 int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *cmdline,
1007 int is_open)
1008 {
1009 int ret;
1010
1011 /* Open element pid */
1012 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_pid);
1013 if (ret) {
1014 goto end;
1015 }
1016
1017 /* Writing pid number */
1018 ret = mi_lttng_writer_write_element_signed_int(writer,
1019 mi_lttng_element_pid_id, (int)pid);
1020 if (ret) {
1021 goto end;
1022 }
1023
1024 /* Writing name of the process */
1025 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1026 cmdline);
1027 if (ret) {
1028 goto end;
1029 }
1030
1031 if (!is_open) {
1032 /* Closing Pid */
1033 ret = mi_lttng_writer_close_element(writer);
1034 }
1035
1036 end:
1037 return ret;
1038 }
1039
1040 LTTNG_HIDDEN
1041 int mi_lttng_event_fields_open(struct mi_writer *writer)
1042 {
1043 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1044 }
1045
1046 LTTNG_HIDDEN
1047 int mi_lttng_event_field(struct mi_writer *writer,
1048 struct lttng_event_field *field)
1049 {
1050 int ret;
1051
1052 if (!field->field_name[0]) {
1053 ret = 0;
1054 goto end;
1055 }
1056
1057 /* Open field */
1058 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1059 if (ret) {
1060 goto end;
1061 }
1062
1063 if (!field->field_name[0]) {
1064 goto close;
1065 }
1066
1067 /* Name */
1068 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1069 field->field_name);
1070 if (ret) {
1071 goto end;
1072 }
1073
1074 /* Type */
1075 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1076 mi_lttng_eventfieldtype_string(field->type));
1077 if (ret) {
1078 goto end;
1079 }
1080
1081 /* nowrite */
1082 ret = mi_lttng_writer_write_element_signed_int(writer,
1083 mi_lttng_element_nowrite, field->nowrite);
1084 if (ret) {
1085 goto end;
1086 }
1087
1088 close:
1089 /* Close field element */
1090 ret = mi_lttng_writer_close_element(writer);
1091
1092 end:
1093 return ret;
1094 }
1095
1096 LTTNG_HIDDEN
1097 int mi_lttng_calibrate(struct mi_writer *writer,
1098 struct lttng_calibrate *calibrate)
1099 {
1100 int ret;
1101
1102 /* Open calibrate element */
1103 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_calibrate);
1104 if (ret) {
1105 goto end;
1106 }
1107
1108 /* Calibration type */
1109 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1110 mi_lttng_calibratetype_string(calibrate->type));
1111 if (ret) {
1112 goto end;
1113 }
1114
1115 /* Closing calibrate element */
1116 ret = mi_lttng_writer_close_element(writer);
1117 end:
1118 return ret;
1119 }
1120 LTTNG_HIDDEN
1121 int mi_lttng_context(struct mi_writer *writer,
1122 struct lttng_event_context *context, int is_open)
1123 {
1124 int ret;
1125 const char *type_string;
1126 struct lttng_event_perf_counter_ctx *perf_context;
1127 /* Open context */
1128 ret = mi_lttng_writer_open_element(writer , config_element_context);
1129 if (ret) {
1130 goto end;
1131 }
1132
1133 type_string = mi_lttng_event_contexttype_string(context->ctx);
1134 if (!type_string) {
1135 ret = -LTTNG_ERR_INVALID;
1136 goto end;
1137 }
1138
1139 /* Print context type */
1140 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1141 type_string);
1142
1143 /* Special case for PERF_*_COUNTER
1144 * print the lttng_event_perf_counter_ctx*/
1145 switch (context->ctx) {
1146 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1147 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1148 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1149 perf_context = &context->u.perf_counter;
1150 ret = mi_lttng_perf_counter_context(writer, perf_context);
1151 if (ret) {
1152 goto end;
1153 }
1154 break;
1155 default:
1156 break;
1157 }
1158
1159 /* Close context */
1160 if (!is_open) {
1161 ret = mi_lttng_writer_close_element(writer);
1162 }
1163
1164 end:
1165 return ret;
1166 }
1167
1168 LTTNG_HIDDEN
1169 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1170 struct lttng_event_perf_counter_ctx *perf_context)
1171 {
1172 int ret;
1173
1174 /* Open perf_counter_context */
1175 ret = mi_lttng_writer_open_element(writer,
1176 mi_lttng_element_perf_counter_context);
1177 if (ret) {
1178 goto end;
1179 }
1180
1181 /* Type */
1182 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1183 config_element_type, perf_context->type);
1184 if (ret) {
1185 goto end;
1186 }
1187
1188 /* Config */
1189 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1190 config_element_config, perf_context->config);
1191 if (ret) {
1192 goto end;
1193 }
1194
1195 /* Name of the perf counter */
1196 ret = mi_lttng_writer_write_element_string(writer,
1197 config_element_name, perf_context->name);
1198 if (ret) {
1199 goto end;
1200 }
1201
1202 /* Close perf_counter_context */
1203 ret = mi_lttng_writer_close_element(writer);
1204 end:
1205 return ret;
1206 }
1207
1208 LTTNG_HIDDEN
1209 int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
1210 const char *session_name)
1211 {
1212 int ret;
1213
1214 /* Open session element */
1215 ret = mi_lttng_writer_open_element(writer, config_element_session);
1216 if (ret) {
1217 goto end;
1218 }
1219
1220 /* Snapshot output list for current session name */
1221 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1222 session_name);
1223
1224 /* Open element snapshots (sequence one snapshot) */
1225 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
1226 if (ret) {
1227 goto end;
1228 }
1229
1230 end:
1231 return ret;
1232 }
1233
1234 LTTNG_HIDDEN
1235 int mi_lttng_snapshot_list_output(struct mi_writer *writer,
1236 struct lttng_snapshot_output *output)
1237 {
1238 int ret;
1239
1240 /* Open element snapshot output */
1241 ret = mi_lttng_writer_open_element(writer,
1242 mi_lttng_element_command_snapshot);
1243 if (ret) {
1244 goto end;
1245 }
1246
1247 /* ID of the snapshot output */
1248 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1249 mi_lttng_element_id, output->id);
1250 if (ret) {
1251 goto end;
1252 }
1253
1254 /* Name of the output */
1255 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1256 output->name);
1257 if (ret) {
1258 goto end;
1259 }
1260
1261 /* Destination of the output (ctrl_url)*/
1262 ret = mi_lttng_writer_write_element_string(writer,
1263 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1264 if (ret) {
1265 goto end;
1266 }
1267
1268 /* Destination of the output (data_url) */
1269 ret = mi_lttng_writer_write_element_string(writer,
1270 mi_lttng_element_snapshot_data_url, output->data_url);
1271 if (ret) {
1272 goto end;
1273 }
1274
1275 /* total size of all stream combined */
1276 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1277 mi_lttng_element_snapshot_max_size, output->max_size);
1278 if (ret) {
1279 goto end;
1280 }
1281
1282 /* Close snapshot output element */
1283 ret = mi_lttng_writer_close_element(writer);
1284
1285 end:
1286 return ret;
1287 }
1288
1289 LTTNG_HIDDEN
1290 int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
1291 const char *name, const char *current_session_name)
1292 {
1293 int ret;
1294
1295 /* Open element del_snapshot */
1296 ret = mi_lttng_writer_open_element(writer,
1297 mi_lttng_element_command_snapshot);
1298 if (ret) {
1299 goto end;
1300 }
1301
1302
1303 if (id != UINT32_MAX) {
1304 /* "Snapshot output "id" successfully deleted
1305 * for "current_session_name"
1306 * ID of the snapshot output
1307 */
1308 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1309 mi_lttng_element_id, id);
1310 if (ret) {
1311 goto end;
1312 }
1313 } else {
1314 /* "Snapshot output "name" successfully deleted
1315 * for session "current_session_name"
1316 * Name of the output
1317 */
1318 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1319 name);
1320 if (ret) {
1321 goto end;
1322 }
1323 }
1324
1325 /* Snapshot was deleted for session "current_session_name"*/
1326 ret = mi_lttng_writer_write_element_string(writer,
1327 mi_lttng_element_snapshot_session_name,
1328 current_session_name);
1329 if (ret) {
1330 goto end;
1331 }
1332
1333 /* Close snapshot element */
1334 ret = mi_lttng_writer_close_element(writer);
1335
1336 end:
1337 return ret;
1338 }
1339
1340 LTTNG_HIDDEN
1341 int mi_lttng_snapshot_add_output(struct mi_writer *writer,
1342 const char *current_session_name, const char *n_ptr,
1343 struct lttng_snapshot_output *output)
1344 {
1345 int ret;
1346
1347 /* Open element snapshot */
1348 ret = mi_lttng_writer_open_element(writer,
1349 mi_lttng_element_command_snapshot);
1350 if (ret) {
1351 goto end;
1352 }
1353
1354 /* Snapshot output id */
1355 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1356 mi_lttng_element_id, output->id);
1357 if (ret) {
1358 goto end;
1359 }
1360
1361 /* Snapshot output names */
1362 ret = mi_lttng_writer_write_element_string(writer,
1363 config_element_name, n_ptr);
1364 if (ret) {
1365 goto end;
1366 }
1367
1368 /* Destination of the output (ctrl_url)*/
1369 ret = mi_lttng_writer_write_element_string(writer,
1370 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1371 if (ret) {
1372 goto end;
1373 }
1374
1375 /* Snapshot added for session "current_session_name"*/
1376 ret = mi_lttng_writer_write_element_string(writer,
1377 mi_lttng_element_snapshot_session_name, current_session_name);
1378 if (ret) {
1379 goto end;
1380 }
1381
1382 /* total size of all stream combined */
1383 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1384 mi_lttng_element_snapshot_max_size, output->max_size);
1385 if (ret) {
1386 goto end;
1387 }
1388
1389 /* Close snapshot element */
1390 ret = mi_lttng_writer_close_element(writer);
1391
1392 end:
1393 return ret;
1394 }
1395
1396 LTTNG_HIDDEN
1397 int mi_lttng_snapshot_record(struct mi_writer *writer,
1398 const char *current_session_name, const char *url,
1399 const char *cmdline_ctrl_url, const char *cmdline_data_url)
1400 {
1401 int ret;
1402
1403 /* Open element snapshot */
1404 ret = mi_lttng_writer_open_element(writer,
1405 mi_lttng_element_command_snapshot);
1406 if (ret) {
1407 goto end;
1408 }
1409
1410 /*
1411 * If a valid an URL was given, serialize it,
1412 * else take the command line data and ctrl urls*/
1413 if (url) {
1414 /* Destination of the output (ctrl_url)*/
1415 ret = mi_lttng_writer_write_element_string(writer,
1416 mi_lttng_element_snapshot_ctrl_url, url);
1417 if (ret) {
1418 goto end;
1419 }
1420 } else if (cmdline_ctrl_url) {
1421 /* Destination of the output (ctrl_url)*/
1422 ret = mi_lttng_writer_write_element_string(writer,
1423 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
1424 if (ret) {
1425 goto end;
1426 }
1427
1428 /* Destination of the output (data_url) */
1429 ret = mi_lttng_writer_write_element_string(writer,
1430 mi_lttng_element_snapshot_data_url, cmdline_data_url);
1431 if (ret) {
1432 goto end;
1433 }
1434 }
1435
1436 /* Close record_snapshot element */
1437 ret = mi_lttng_writer_close_element(writer);
1438
1439 end:
1440 return ret;
1441 }
This page took 0.063984 seconds and 6 git commands to generate.