515f16941b52cb881ad2644991b8b592d4eb4766
[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 "mi-lttng.h"
23
24 #include <assert.h>
25
26 /* Strings related to command */
27 const char * const mi_lttng_element_command = "command";
28 const char * const mi_lttng_element_command_name = "name";
29 const char * const mi_lttng_element_command_version = "version";
30 const char * const mi_lttng_element_command_list = "list";
31 const char * const mi_lttng_element_command_save = "save";
32 const char * const mi_lttng_element_command_load = "load";
33 const char * const mi_lttng_element_command_start = "start";
34 const char * const mi_lttng_element_command_stop = "stop";
35 const char * const mi_lttng_element_command_create = "create";
36 const char * const mi_lttng_element_command_destroy = "destroy";
37 const char * const mi_lttng_element_command_calibrate = "calibrate";
38 const char * const mi_lttng_element_command_add_context = "add-context";
39 const char * const mi_lttng_element_command_output = "output";
40 const char * const mi_lttng_element_command_success = "success";
41
42 /* Strings related to version command */
43 const char * const mi_lttng_element_version = "version";
44 const char * const mi_lttng_element_version_str = "string";
45 const char * const mi_lttng_element_version_web = "url";
46 const char * const mi_lttng_element_version_major = "major";
47 const char * const mi_lttng_element_version_minor = "minor";
48 const char * const mi_lttng_element_version_commit = "commit";
49 const char * const mi_lttng_element_version_license = "license";
50 const char * const mi_lttng_element_version_patch_level = "patchLevel";
51 const char * const mi_lttng_element_version_description = "description";
52
53 /* Strings related to pid */
54 const char * const mi_lttng_element_pids = "pids";
55 const char * const mi_lttng_element_pid = "pid";
56 const char * const mi_lttng_element_pid_id = "id";
57
58 /* Strings related to save command */
59 const char * const mi_lttng_element_save = "save";
60
61 /* Strings related to load command */
62 const char * const mi_lttng_element_load = "load";
63
64 /* String related to a lttng_event_field */
65 const char * const mi_lttng_element_event_field = "event_field";
66 const char * const mi_lttng_element_event_fields = "event_fields";
67
68 /* String related to lttng_event_context */
69 const char * const mi_lttng_context_type_perf_counter = "PERF_COUNTER";
70 const char * const mi_lttng_context_type_perf_cpu_counter = "PERF_CPU_COUNTER";
71 const char * const mi_lttng_context_type_perf_thread_counter = "PERF_THREAD_COUNTER";
72
73 /* String related to lttng_event_perf_counter_ctx */
74 const char * const mi_lttng_element_perf_counter_context = "perf_counter_context";
75
76 /* General elements of mi_lttng */
77 const char * const mi_lttng_element_type_other = "OTHER";
78 const char * const mi_lttng_element_type_integer = "INTEGER";
79 const char * const mi_lttng_element_type_enum = "ENUM";
80 const char * const mi_lttng_element_type_float = "FLOAT";
81 const char * const mi_lttng_element_type_string = "STRING";
82 const char * const mi_lttng_element_nowrite = "nowrite";
83 const char * const mi_lttng_element_success = "success";
84
85 /* String related to loglevel */
86 const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
87 const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
88 const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
89 const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
90 const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
91 const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
92 const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
93 const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
94 const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
95 const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
96 const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
97 const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
98 const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
99 const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
100 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
101 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
102
103 /* String related to loglevel type */
104 const char * const mi_lttng_loglevel_type_all = "ALL";
105 const char * const mi_lttng_loglevel_type_range = "RANGE";
106 const char * const mi_lttng_loglevel_type_single = "SINGLE";
107 const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
108
109 /* String related to lttng_calibrate */
110 const char * const mi_lttng_element_calibrate = "calibrate";
111 const char * const mi_lttng_element_calibrate_function = "FUNCTION";
112
113 const char * const mi_lttng_element_empty = "";
114
115 const char *mi_lttng_loglevel_string(int value)
116 {
117 switch (value) {
118 case -1:
119 return mi_lttng_element_empty;
120 case LTTNG_LOGLEVEL_EMERG:
121 return mi_lttng_loglevel_str_emerg;
122 case LTTNG_LOGLEVEL_ALERT:
123 return mi_lttng_loglevel_str_alert;
124 case LTTNG_LOGLEVEL_CRIT:
125 return mi_lttng_loglevel_str_crit;
126 case LTTNG_LOGLEVEL_ERR:
127 return mi_lttng_loglevel_str_err;
128 case LTTNG_LOGLEVEL_WARNING:
129 return mi_lttng_loglevel_str_warning;
130 case LTTNG_LOGLEVEL_NOTICE:
131 return mi_lttng_loglevel_str_notice;
132 case LTTNG_LOGLEVEL_INFO:
133 return mi_lttng_loglevel_str_info;
134 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
135 return mi_lttng_loglevel_str_debug_system;
136 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
137 return mi_lttng_loglevel_str_debug_program;
138 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
139 return mi_lttng_loglevel_str_debug_process;
140 case LTTNG_LOGLEVEL_DEBUG_MODULE:
141 return mi_lttng_loglevel_str_debug_module;
142 case LTTNG_LOGLEVEL_DEBUG_UNIT:
143 return mi_lttng_loglevel_str_debug_unit;
144 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
145 return mi_lttng_loglevel_str_debug_function;
146 case LTTNG_LOGLEVEL_DEBUG_LINE:
147 return mi_lttng_loglevel_str_debug_line;
148 case LTTNG_LOGLEVEL_DEBUG:
149 return mi_lttng_loglevel_str_debug;
150 default:
151 return mi_lttng_loglevel_str_unknown;
152 }
153 }
154
155 const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
156 {
157 switch (value) {
158 case LTTNG_EVENT_LOGLEVEL_ALL:
159 return mi_lttng_loglevel_type_all;
160 case LTTNG_EVENT_LOGLEVEL_RANGE:
161 return mi_lttng_loglevel_type_range;
162 case LTTNG_EVENT_LOGLEVEL_SINGLE:
163 return mi_lttng_loglevel_type_single;
164 default:
165 return mi_lttng_loglevel_type_unknown;
166 }
167 }
168
169 const char *mi_lttng_eventtype_string(enum lttng_event_type value)
170 {
171 switch (value) {
172 case LTTNG_EVENT_ALL:
173 return config_event_type_all;
174 case LTTNG_EVENT_TRACEPOINT:
175 return config_event_type_tracepoint;
176 case LTTNG_EVENT_PROBE:
177 return config_event_type_probe;
178 case LTTNG_EVENT_FUNCTION:
179 return config_event_type_function;
180 case LTTNG_EVENT_FUNCTION_ENTRY:
181 return config_event_type_function_entry;
182 case LTTNG_EVENT_SYSCALL:
183 return config_event_type_syscall;
184 case LTTNG_EVENT_NOOP:
185 return config_event_type_noop;
186 default:
187 return mi_lttng_element_empty;
188 }
189 }
190
191 const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
192 {
193 switch (val) {
194 case LTTNG_EVENT_CONTEXT_PID:
195 return config_event_context_pid;
196 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
197 return mi_lttng_context_type_perf_counter;
198 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
199 return mi_lttng_context_type_perf_thread_counter;
200 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
201 return mi_lttng_context_type_perf_cpu_counter;
202 case LTTNG_EVENT_CONTEXT_PROCNAME:
203 return config_event_context_procname;
204 case LTTNG_EVENT_CONTEXT_PRIO:
205 return config_event_context_prio;
206 case LTTNG_EVENT_CONTEXT_NICE:
207 return config_event_context_nice;
208 case LTTNG_EVENT_CONTEXT_VPID:
209 return config_event_context_vpid;
210 case LTTNG_EVENT_CONTEXT_TID:
211 return config_event_context_tid;
212 case LTTNG_EVENT_CONTEXT_VTID:
213 return config_event_context_vtid;
214 case LTTNG_EVENT_CONTEXT_PPID:
215 return config_event_context_ppid;
216 case LTTNG_EVENT_CONTEXT_VPPID:
217 return config_event_context_vppid;
218 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
219 return config_event_context_pthread_id;
220 case LTTNG_EVENT_CONTEXT_HOSTNAME:
221 return config_event_context_hostname;
222 case LTTNG_EVENT_CONTEXT_IP:
223 return config_event_context_ip;
224 default:
225 return NULL;
226 }
227 }
228
229 const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
230 {
231 switch (val) {
232 case(LTTNG_EVENT_FIELD_INTEGER):
233 return mi_lttng_element_type_integer;
234 case(LTTNG_EVENT_FIELD_ENUM):
235 return mi_lttng_element_type_enum;
236 case(LTTNG_EVENT_FIELD_FLOAT):
237 return mi_lttng_element_type_float;
238 case(LTTNG_EVENT_FIELD_STRING):
239 return mi_lttng_element_type_string;
240 default:
241 return mi_lttng_element_type_other;
242 }
243 }
244
245 const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
246 {
247 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
248 switch (value) {
249 case LTTNG_DOMAIN_KERNEL:
250 return config_domain_type_kernel;
251 case LTTNG_DOMAIN_UST:
252 return config_domain_type_ust;
253 case LTTNG_DOMAIN_JUL:
254 return config_domain_type_jul;
255 default:
256 /* Should not have an unknown domain */
257 assert(0);
258 }
259 }
260
261 const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
262 {
263 switch (value) {
264 case LTTNG_BUFFER_PER_PID:
265 return config_buffer_type_per_pid;
266 case LTTNG_BUFFER_PER_UID:
267 return config_buffer_type_per_uid;
268 case LTTNG_BUFFER_GLOBAL:
269 return config_buffer_type_global;
270 default:
271 /* Should not have an unknow buffer type */
272 assert(0);
273 }
274 }
275
276 const char *mi_lttng_calibratetype_string(enum lttng_calibrate_type val)
277 {
278 const char *ret;
279
280 switch (val) {
281 case LTTNG_CALIBRATE_FUNCTION:
282 ret = mi_lttng_element_calibrate_function;
283 break;
284 default:
285 ret = mi_lttng_element_empty;
286 break;
287 }
288 return ret;
289 }
290
291 LTTNG_HIDDEN
292 struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
293 {
294 struct mi_writer *mi_writer;
295
296 mi_writer = zmalloc(sizeof(struct mi_writer));
297 if (!mi_writer) {
298 PERROR("zmalloc mi_writer_create");
299 goto end;
300 }
301 if (mi_output_type == LTTNG_MI_XML) {
302 mi_writer->writer = config_writer_create(fd_output);
303 if (!mi_writer->writer) {
304 goto err_destroy;
305 }
306 mi_writer->type = LTTNG_MI_XML;
307 } else {
308 goto err_destroy;
309 }
310
311 end:
312 return mi_writer;
313
314 err_destroy:
315 free(mi_writer);
316 return NULL;
317 }
318
319 LTTNG_HIDDEN
320 int mi_lttng_writer_destroy(struct mi_writer *writer)
321 {
322 int ret;
323
324 if (!writer) {
325 ret = -EINVAL;
326 goto end;
327 }
328
329 ret = config_writer_destroy(writer->writer);
330 if (ret < 0) {
331 goto end;
332 }
333
334 free(writer);
335 end:
336 return ret;
337 }
338
339 LTTNG_HIDDEN
340 int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
341 {
342 int ret;
343
344 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command);
345 if (ret) {
346 goto end;
347 }
348 ret = mi_lttng_writer_write_element_string(writer,
349 mi_lttng_element_command_name, command);
350 end:
351 return ret;
352 }
353
354 LTTNG_HIDDEN
355 int mi_lttng_writer_command_close(struct mi_writer *writer)
356 {
357 return mi_lttng_writer_close_element(writer);
358 }
359
360 LTTNG_HIDDEN
361 int mi_lttng_writer_open_element(struct mi_writer *writer,
362 const char *element_name)
363 {
364 return config_writer_open_element(writer->writer, element_name);
365 }
366
367 LTTNG_HIDDEN
368 int mi_lttng_writer_close_element(struct mi_writer *writer)
369 {
370 return config_writer_close_element(writer->writer);
371 }
372
373 LTTNG_HIDDEN
374 int mi_lttng_close_multi_element(struct mi_writer *writer,
375 unsigned int nb_element)
376 {
377 int ret, i;
378
379 if (nb_element < 1) {
380 ret = 0;
381 goto end;
382 }
383 for (i = 0; i < nb_element; i++) {
384 ret = mi_lttng_writer_close_element(writer);
385 if (ret) {
386 goto end;
387 }
388 }
389 end:
390 return ret;
391 }
392
393 LTTNG_HIDDEN
394 int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
395 const char *element_name, uint64_t value)
396 {
397 return config_writer_write_element_unsigned_int(writer->writer,
398 element_name, value);
399 }
400
401 LTTNG_HIDDEN
402 int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
403 const char *element_name, int64_t value)
404 {
405 return config_writer_write_element_signed_int(writer->writer,
406 element_name, value);
407 }
408
409 LTTNG_HIDDEN
410 int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
411 const char *element_name, int value)
412 {
413 return config_writer_write_element_bool(writer->writer,
414 element_name, value);
415 }
416
417 LTTNG_HIDDEN
418 int mi_lttng_writer_write_element_string(struct mi_writer *writer,
419 const char *element_name, const char *value)
420 {
421 return config_writer_write_element_string(writer->writer,
422 element_name, value);
423 }
424
425 LTTNG_HIDDEN
426 int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
427 const char *lttng_description, const char *lttng_license)
428 {
429 int ret;
430
431 /* Open version */
432 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
433 if (ret) {
434 goto end;
435 }
436
437 /* Version string (contain info like rc etc.) */
438 ret = mi_lttng_writer_write_element_string(writer,
439 mi_lttng_element_version_str, VERSION);
440 if (ret) {
441 goto end;
442 }
443
444 /* Major version number */
445 ret = mi_lttng_writer_write_element_unsigned_int(writer,
446 mi_lttng_element_version_major, version->version_major);
447 if (ret) {
448 goto end;
449 }
450
451 /* Minor version number */
452 ret = mi_lttng_writer_write_element_unsigned_int(writer,
453 mi_lttng_element_version_minor, version->version_minor);
454 if (ret) {
455 goto end;
456 }
457
458 /* Commit version number */
459 ret = mi_lttng_writer_write_element_string(writer,
460 mi_lttng_element_version_commit, version->version_commit);
461 if (ret) {
462 goto end;
463 }
464
465 /* Patch number */
466 ret = mi_lttng_writer_write_element_unsigned_int(writer,
467 mi_lttng_element_version_patch_level, version->version_patchlevel);
468 if (ret) {
469 goto end;
470 }
471
472 /* Name of the version */
473 ret = mi_lttng_writer_write_element_string(writer,
474 config_element_name, version->version_name);
475 if (ret) {
476 goto end;
477 }
478
479 /* Description mostly related to beer... */
480 ret = mi_lttng_writer_write_element_string(writer,
481 mi_lttng_element_version_description, lttng_description);
482 if (ret) {
483 goto end;
484 }
485
486 /* url */
487 ret = mi_lttng_writer_write_element_string(writer,
488 mi_lttng_element_version_web, version->package_url);
489 if (ret) {
490 goto end;
491 }
492
493 /* License: free as in free beer...no...*speech* */
494 ret = mi_lttng_writer_write_element_string(writer,
495 mi_lttng_element_version_license, lttng_license);
496 if (ret) {
497 goto end;
498 }
499
500 /* Close version element */
501 ret = mi_lttng_writer_close_element(writer);
502
503 end:
504 return ret;
505 }
506
507 LTTNG_HIDDEN
508 int mi_lttng_sessions_open(struct mi_writer *writer)
509 {
510 return mi_lttng_writer_open_element(writer, config_element_sessions);
511 }
512
513 LTTNG_HIDDEN
514 int mi_lttng_session(struct mi_writer *writer,
515 struct lttng_session *session, int is_open)
516 {
517 int ret;
518
519 assert(session);
520
521 /* Open sessions element */
522 ret = mi_lttng_writer_open_element(writer,
523 config_element_session);
524 if (ret) {
525 goto end;
526 }
527
528 /* Name of the session */
529 ret = mi_lttng_writer_write_element_string(writer,
530 config_element_name, session->name);
531 if (ret) {
532 goto end;
533 }
534
535 /* Path */
536 ret = mi_lttng_writer_write_element_string(writer,
537 config_element_path, session->path);
538 if (ret) {
539 goto end;
540 }
541
542 /* Enabled ? */
543 ret = mi_lttng_writer_write_element_bool(writer,
544 config_element_enabled, session->enabled);
545 if (ret) {
546 goto end;
547 }
548
549 /* Snapshot mode */
550 ret = mi_lttng_writer_write_element_unsigned_int(writer,
551 config_element_snapshot_mode, session->snapshot_mode);
552 if (ret) {
553 goto end;
554 }
555
556 /* Live timer interval in usec */
557 ret = mi_lttng_writer_write_element_unsigned_int(writer,
558 config_element_live_timer_interval,
559 session->live_timer_interval);
560 if (ret) {
561 goto end;
562 }
563
564 if (!is_open) {
565 /* Closing session element */
566 ret = mi_lttng_writer_close_element(writer);
567 }
568 end:
569 return ret;
570
571 }
572
573 LTTNG_HIDDEN
574 int mi_lttng_domains_open(struct mi_writer *writer)
575 {
576 return mi_lttng_writer_open_element(writer, config_element_domains);
577 }
578
579 LTTNG_HIDDEN
580 int mi_lttng_domain(struct mi_writer *writer,
581 struct lttng_domain *domain, int is_open)
582 {
583 int ret = 0;
584 const char *str_domain;
585 const char *str_buffer;
586
587 assert(domain);
588
589 /* Open domain element */
590 ret = mi_lttng_writer_open_element(writer, config_element_domain);
591 if (ret) {
592 goto end;
593 }
594
595 /* Domain Type */
596 str_domain = mi_lttng_domaintype_string(domain->type);
597 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
598 str_domain);
599 if (ret) {
600 goto end;
601 }
602
603 /* Buffer Type */
604 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
605 ret = mi_lttng_writer_write_element_string(writer,
606 config_element_buffer_type, str_buffer);
607 if (ret) {
608 goto end;
609 }
610
611 /* TODO: attr... not sure how to use the union.... */
612
613 if (!is_open) {
614 /* Closing domain element */
615 ret = mi_lttng_writer_close_element(writer);
616 }
617
618 end:
619 return ret;
620
621 }
622
623 LTTNG_HIDDEN
624 int mi_lttng_channels_open(struct mi_writer *writer)
625 {
626 return mi_lttng_writer_open_element(writer, config_element_channels);
627 }
628
629 LTTNG_HIDDEN
630 int mi_lttng_channel(struct mi_writer *writer,
631 struct lttng_channel *channel, int is_open)
632 {
633 int ret = 0;
634
635 assert(channel);
636
637 /* Opening channel element */
638 ret = mi_lttng_writer_open_element(writer, config_element_channel);
639 if (ret) {
640 goto end;
641 }
642
643 /* Name */
644 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
645 channel->name);
646 if (ret) {
647 goto end;
648 }
649
650 /* Enabled ? */
651 ret = mi_lttng_writer_write_element_bool(writer,
652 config_element_enabled, channel->enabled);
653 if (ret) {
654 goto end;
655 }
656
657 /* Attribute */
658 ret = mi_lttng_channel_attr(writer, &channel->attr);
659 if (ret) {
660 goto end;
661 }
662
663 if (!is_open) {
664 /* Closing channel element */
665 ret = mi_lttng_writer_close_element(writer);
666 if (ret) {
667 goto end;
668 }
669 }
670 end:
671 return ret;
672 }
673
674 LTTNG_HIDDEN
675 int mi_lttng_channel_attr(struct mi_writer *writer,
676 struct lttng_channel_attr *attr)
677 {
678 int ret = 0;
679
680 assert(attr);
681
682 /* Opening Attributes */
683 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
684 if (ret) {
685 goto end;
686 }
687
688 /* Overwrite */
689 ret = mi_lttng_writer_write_element_string(writer,
690 config_element_overwrite_mode,
691 attr->overwrite ? config_overwrite_mode_overwrite :
692 config_overwrite_mode_discard);
693 if (ret) {
694 goto end;
695 }
696
697 /* Sub buffer size in byte */
698 ret = mi_lttng_writer_write_element_unsigned_int(writer,
699 config_element_subbuf_size, attr->subbuf_size);
700 if (ret) {
701 goto end;
702 }
703
704 /* Number of subbuffer (power of two) */
705 ret = mi_lttng_writer_write_element_unsigned_int(writer,
706 config_element_num_subbuf,
707 attr->num_subbuf);
708 if (ret) {
709 goto end;
710 }
711
712 /* Switch timer interval in usec */
713 ret = mi_lttng_writer_write_element_unsigned_int(writer,
714 config_element_switch_timer_interval,
715 attr->switch_timer_interval);
716 if (ret) {
717 goto end;
718 }
719
720 /* Read timer interval in usec */
721 ret = mi_lttng_writer_write_element_unsigned_int(writer,
722 config_element_read_timer_interval,
723 attr->read_timer_interval);
724 if (ret) {
725 goto end;
726 }
727
728 /* Event output */
729 ret = mi_lttng_writer_write_element_string(writer,
730 config_element_output_type,
731 attr->output == LTTNG_EVENT_SPLICE ?
732 config_output_type_splice : config_output_type_mmap);
733 if (ret) {
734 goto end;
735 }
736
737 /* Tracefile size in bytes */
738 ret = mi_lttng_writer_write_element_unsigned_int(writer,
739 config_element_tracefile_size, attr->tracefile_size);
740 if (ret) {
741 goto end;
742 }
743
744 /* Count of tracefiles */
745 ret = mi_lttng_writer_write_element_unsigned_int(writer,
746 config_element_tracefile_count,
747 attr->tracefile_count);
748 if (ret) {
749 goto end;
750 }
751
752 /* Live timer interval in usec*/
753 ret = mi_lttng_writer_write_element_unsigned_int(writer,
754 config_element_live_timer_interval,
755 attr->live_timer_interval);
756 if (ret) {
757 goto end;
758 }
759
760 /* Closing attributes */
761 ret = mi_lttng_writer_close_element(writer);
762 if (ret) {
763 goto end;
764 }
765 end:
766 return ret;
767
768 }
769
770 LTTNG_HIDDEN
771 int mi_lttng_event_common_attributes(struct mi_writer *writer,
772 struct lttng_event *event)
773 {
774 int ret;
775
776 /* Open event element */
777 ret = mi_lttng_writer_open_element(writer, config_element_event);
778 if (ret) {
779 goto end;
780 }
781
782 /* event name */
783 ret = mi_lttng_writer_write_element_string(writer,
784 config_element_name, event->name);
785 if (ret) {
786 goto end;
787 }
788
789 /* event type */
790 ret = mi_lttng_writer_write_element_string(writer,
791 config_element_type, mi_lttng_eventtype_string(event->type));
792 if (ret) {
793 goto end;
794 }
795
796 /* is event enabled */
797 ret = mi_lttng_writer_write_element_bool(writer,
798 config_element_enabled, event->enabled);
799 if (ret) {
800 goto end;
801 }
802
803 /* event filter enabled? */
804 ret = mi_lttng_writer_write_element_bool(writer,
805 config_element_filter, event->filter);
806
807 end:
808 return ret;
809 }
810
811 LTTNG_HIDDEN
812 int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
813 struct lttng_event *event)
814 {
815 int ret;
816
817 /* event loglevel */
818 ret = mi_lttng_writer_write_element_string(writer,
819 config_element_loglevel, mi_lttng_loglevel_string(event->loglevel));
820 if (ret) {
821 goto end;
822 }
823
824 ret = mi_lttng_writer_write_element_string(writer,
825 config_element_loglevel_type,
826 mi_lttng_logleveltype_string(event->loglevel_type));
827 if (ret) {
828 goto end;
829 }
830
831 /* event exclusion filter */
832 ret = mi_lttng_writer_write_element_bool(writer,
833 config_element_exclusion, event->exclusion);
834 if (ret) {
835 goto end;
836 }
837
838 end:
839 return ret;
840 }
841
842 LTTNG_HIDDEN
843 int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
844 struct lttng_event *event)
845 {
846 /* event exclusion filter */
847 return mi_lttng_writer_write_element_bool(writer,
848 config_element_exclusion, event->exclusion);
849 }
850
851 LTTNG_HIDDEN
852 int mi_lttng_event_function_probe(struct mi_writer *writer,
853 struct lttng_event *event)
854 {
855 int ret;
856
857 if (event->attr.probe.addr != 0) {
858 /* event probe address */
859 ret = mi_lttng_writer_write_element_unsigned_int(writer,
860 config_element_address, event->attr.probe.addr);
861 if (ret) {
862 goto end;
863 }
864 } else {
865 /* event probe offset */
866 ret = mi_lttng_writer_write_element_unsigned_int(writer,
867 config_element_offset, event->attr.probe.offset);
868 if (ret) {
869 goto end;
870 }
871
872 /* event probe symbol_name */
873 ret = mi_lttng_writer_write_element_string(writer,
874 config_element_symbol_name, event->attr.probe.symbol_name);
875 if (ret) {
876 goto end;
877 }
878 }
879 end:
880 return ret;
881 }
882
883 LTTNG_HIDDEN
884 int mi_lttng_event_function_entry(struct mi_writer *writer,
885 struct lttng_event *event)
886 {
887 /* event probe symbol_name */
888 return mi_lttng_writer_write_element_string(writer,
889 config_element_symbol_name, event->attr.ftrace.symbol_name);
890 }
891
892 LTTNG_HIDDEN
893 int mi_lttng_events_open(struct mi_writer *writer)
894 {
895 return mi_lttng_writer_open_element(writer, config_element_events);
896 }
897
898 LTTNG_HIDDEN
899 int mi_lttng_event(struct mi_writer *writer,
900 struct lttng_event *event, int is_open)
901 {
902 int ret;
903
904 ret = mi_lttng_event_common_attributes(writer, event);
905 if (ret) {
906 goto end;
907 }
908
909 switch (event->type) {
910 case LTTNG_EVENT_ALL:
911 /* We should never have "all" events in list. */
912 assert(0);
913 break;
914 case LTTNG_EVENT_TRACEPOINT:
915 {
916 if (event->loglevel != -1) {
917 ret = mi_lttng_event_tracepoint_loglevel(writer, event);
918 } else {
919 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
920 }
921 break;
922 }
923 case LTTNG_EVENT_PROBE:
924 ret = mi_lttng_event_function_probe(writer, event);
925 break;
926 case LTTNG_EVENT_FUNCTION_ENTRY:
927 ret = mi_lttng_event_function_entry(writer, event);
928 break;
929 default:
930 break;
931 }
932
933 if (!is_open) {
934 ret = mi_lttng_writer_close_element(writer);
935 }
936
937 end:
938 return ret;
939 }
940
941 LTTNG_HIDDEN
942 int mi_lttng_pids_open(struct mi_writer *writer)
943 {
944 return mi_lttng_writer_open_element(writer, mi_lttng_element_pids);
945 }
946
947 LTTNG_HIDDEN
948 int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *cmdline,
949 int is_open)
950 {
951 int ret;
952
953 /* Open element pid */
954 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_pid);
955 if (ret) {
956 goto end;
957 }
958
959 /* Writing pid number */
960 ret = mi_lttng_writer_write_element_signed_int(writer,
961 mi_lttng_element_pid_id, (int)pid);
962 if (ret) {
963 goto end;
964 }
965
966 /* Writing name of the process */
967 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
968 cmdline);
969 if (ret) {
970 goto end;
971 }
972
973 if (!is_open) {
974 /* Closing Pid */
975 ret = mi_lttng_writer_close_element(writer);
976 }
977
978 end:
979 return ret;
980 }
981
982 LTTNG_HIDDEN
983 int mi_lttng_event_fields_open(struct mi_writer *writer)
984 {
985 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
986 }
987
988 LTTNG_HIDDEN
989 int mi_lttng_event_field(struct mi_writer *writer,
990 struct lttng_event_field *field)
991 {
992 int ret;
993
994 if (!field->field_name[0]) {
995 /* To Review: not sure if legal david ?
996 * how should this be handle ?
997 */
998 ret = 0;
999 goto end;
1000 }
1001
1002 /* Open field */
1003 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1004 if (ret) {
1005 goto end;
1006 }
1007
1008 if (!field->field_name[0]) {
1009 goto close;
1010 }
1011
1012 /* Name */
1013 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1014 field->field_name);
1015 if (ret) {
1016 goto end;
1017 }
1018
1019 /* Type */
1020 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1021 mi_lttng_eventfieldtype_string(field->type));
1022 if (ret) {
1023 goto end;
1024 }
1025
1026 /* nowrite */
1027 ret = mi_lttng_writer_write_element_signed_int(writer,
1028 mi_lttng_element_nowrite, field->nowrite);
1029 if (ret) {
1030 goto end;
1031 }
1032
1033 close:
1034 /* Close field element */
1035 ret = mi_lttng_writer_close_element(writer);
1036
1037 end:
1038 return ret;
1039 }
1040
1041 LTTNG_HIDDEN
1042 int mi_lttng_calibrate(struct mi_writer *writer,
1043 struct lttng_calibrate *calibrate)
1044 {
1045 int ret;
1046
1047 /* Open calibrate element */
1048 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_calibrate);
1049 if (ret) {
1050 goto end;
1051 }
1052
1053 /* Calibration type */
1054 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1055 mi_lttng_calibratetype_string(calibrate->type));
1056 if (ret) {
1057 goto end;
1058 }
1059
1060 /* Closing calibrate element */
1061 ret = mi_lttng_writer_close_element(writer);
1062 end:
1063 return ret;
1064 }
1065 LTTNG_HIDDEN
1066 int mi_lttng_context(struct mi_writer *writer,
1067 struct lttng_event_context *context, int is_open)
1068 {
1069 int ret;
1070 const char *type_string;
1071 struct lttng_event_perf_counter_ctx *perf_context;
1072 /* Open context */
1073 ret = mi_lttng_writer_open_element(writer , config_element_context);
1074 if (ret) {
1075 goto end;
1076 }
1077
1078 type_string = mi_lttng_event_contexttype_string(context->ctx);
1079 if (!type_string) {
1080 ret = -LTTNG_ERR_INVALID;
1081 goto end;
1082 }
1083
1084 /* Print context type */
1085 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1086 type_string);
1087
1088 /* Special case for PERF_*_COUNTER
1089 * print the lttng_event_perf_counter_ctx*/
1090 switch (context->ctx) {
1091 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1092 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1093 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1094 perf_context = &context->u.perf_counter;
1095 ret = mi_lttng_perf_counter_context(writer, perf_context);
1096 if (ret) {
1097 goto end;
1098 }
1099 break;
1100 default:
1101 break;
1102 }
1103
1104 /* Close context */
1105 if (!is_open) {
1106 ret = mi_lttng_writer_close_element(writer);
1107 }
1108
1109 end:
1110 return ret;
1111 }
1112
1113 LTTNG_HIDDEN
1114 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1115 struct lttng_event_perf_counter_ctx *perf_context)
1116 {
1117 int ret;
1118
1119 /* Open perf_counter_context */
1120 ret = mi_lttng_writer_open_element(writer,
1121 mi_lttng_element_perf_counter_context);
1122 if (ret) {
1123 goto end;
1124 }
1125
1126 /* Type */
1127 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1128 config_element_type, perf_context->type);
1129 if (ret) {
1130 goto end;
1131 }
1132
1133 /* Config */
1134 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1135 config_element_config, perf_context->config);
1136 if (ret) {
1137 goto end;
1138 }
1139
1140 /* Name of the perf counter */
1141 ret = mi_lttng_writer_write_element_string(writer,
1142 config_element_name, perf_context->name);
1143 if (ret) {
1144 goto end;
1145 }
1146
1147 /* Close perf_counter_context */
1148 ret = mi_lttng_writer_close_element(writer);
1149 end:
1150 return ret;
1151 }
This page took 0.050115 seconds and 3 git commands to generate.