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