Fix: lttng-list: don't warn when the kernel domain has no channels
[lttng-tools.git] / src / bin / lttng / commands / list.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #include <stdint.h>
10 #define _LGPL_SOURCE
11 #include <inttypes.h>
12 #include <popt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <assert.h>
17
18 #include <common/mi-lttng.h>
19 #include <common/time.h>
20 #include <common/tracker.h>
21 #include <lttng/constant.h>
22 #include <lttng/tracker.h>
23
24 #include "../command.h"
25
26 static int opt_userspace;
27 static int opt_kernel;
28 static int opt_jul;
29 static int opt_log4j;
30 static int opt_python;
31 static char *opt_channel;
32 static int opt_domain;
33 static int opt_fields;
34 static int opt_syscall;
35
36 const char *indent4 = " ";
37 const char *indent6 = " ";
38 const char *indent8 = " ";
39
40 #ifdef LTTNG_EMBED_HELP
41 static const char help_msg[] =
42 #include <lttng-list.1.h>
43 ;
44 #endif
45
46 enum {
47 OPT_HELP = 1,
48 OPT_USERSPACE,
49 OPT_LIST_OPTIONS,
50 };
51
52 static struct lttng_handle *handle;
53 static struct mi_writer *writer;
54
55 /* Only set when listing a single session. */
56 static struct lttng_session listed_session;
57
58 static struct poptOption long_options[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
61 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
62 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
63 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
64 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
65 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
66 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
67 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
68 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
69 {"syscall", 'S', POPT_ARG_VAL, &opt_syscall, 1, 0, 0},
70 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
71 {0, 0, 0, 0, 0, 0, 0}
72 };
73
74 /*
75 * Get command line from /proc for a specific pid.
76 *
77 * On success, return an allocated string pointer to the proc cmdline.
78 * On error, return NULL.
79 */
80 static char *get_cmdline_by_pid(pid_t pid)
81 {
82 int ret;
83 FILE *fp = NULL;
84 char *cmdline = NULL;
85 /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */
86 char path[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR) - 1];
87
88 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
89 fp = fopen(path, "r");
90 if (fp == NULL) {
91 goto end;
92 }
93
94 /* Caller must free() *cmdline */
95 cmdline = zmalloc(PATH_MAX);
96 if (!cmdline) {
97 PERROR("malloc cmdline");
98 goto end;
99 }
100 ret = fread(cmdline, 1, PATH_MAX, fp);
101 if (ret < 0) {
102 PERROR("fread proc list");
103 }
104
105 end:
106 if (fp) {
107 fclose(fp);
108 }
109 return cmdline;
110 }
111
112 static
113 const char *active_string(int value)
114 {
115 switch (value) {
116 case 0: return "inactive";
117 case 1: return "active";
118 case -1: return "";
119 default: return NULL;
120 }
121 }
122
123 static const char *snapshot_string(int value)
124 {
125 switch (value) {
126 case 1:
127 return " snapshot";
128 default:
129 return "";
130 }
131 }
132
133 static
134 const char *enabled_string(int value)
135 {
136 switch (value) {
137 case 0: return " [disabled]";
138 case 1: return " [enabled]";
139 case -1: return "";
140 default: return NULL;
141 }
142 }
143
144 static
145 const char *safe_string(const char *str)
146 {
147 return str ? str : "";
148 }
149
150 static const char *logleveltype_string(enum lttng_loglevel_type value)
151 {
152 switch (value) {
153 case LTTNG_EVENT_LOGLEVEL_ALL:
154 return ":";
155 case LTTNG_EVENT_LOGLEVEL_RANGE:
156 return " <=";
157 case LTTNG_EVENT_LOGLEVEL_SINGLE:
158 return " ==";
159 default:
160 return " <<TYPE UNKN>>";
161 }
162 }
163
164 static const char *bitness_event(enum lttng_event_flag flags)
165 {
166 if (flags & LTTNG_EVENT_FLAG_SYSCALL_32) {
167 if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) {
168 return " [32/64-bit]";
169 } else {
170 return " [32-bit]";
171 }
172 } else if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) {
173 return " [64-bit]";
174 } else {
175 return "";
176 }
177 }
178
179 /*
180 * Get exclusion names message for a single event.
181 *
182 * Returned pointer must be freed by caller. Returns NULL on error.
183 */
184 static char *get_exclusion_names_msg(struct lttng_event *event)
185 {
186 int ret;
187 int exclusion_count;
188 char *exclusion_msg = NULL;
189 char *at;
190 size_t i;
191 const char * const exclusion_fmt = " [exclusions: ";
192 const size_t exclusion_fmt_len = strlen(exclusion_fmt);
193
194 exclusion_count = lttng_event_get_exclusion_name_count(event);
195 if (exclusion_count < 0) {
196 goto end;
197 } else if (exclusion_count == 0) {
198 /*
199 * No exclusions: return copy of empty string so that
200 * it can be freed by caller.
201 */
202 exclusion_msg = strdup("");
203 goto end;
204 }
205
206 /*
207 * exclusion_msg's size is bounded by the exclusion_fmt string,
208 * a comma per entry, the entry count (fixed-size), a closing
209 * bracket, and a trailing \0.
210 */
211 exclusion_msg = malloc(exclusion_count +
212 exclusion_count * LTTNG_SYMBOL_NAME_LEN +
213 exclusion_fmt_len + 1);
214 if (!exclusion_msg) {
215 goto end;
216 }
217
218 at = strcpy(exclusion_msg, exclusion_fmt) + exclusion_fmt_len;
219 for (i = 0; i < exclusion_count; ++i) {
220 const char *name;
221
222 /* Append comma between exclusion names */
223 if (i > 0) {
224 *at = ',';
225 at++;
226 }
227
228 ret = lttng_event_get_exclusion_name(event, i, &name);
229 if (ret) {
230 /* Prints '?' on local error; should never happen */
231 *at = '?';
232 at++;
233 continue;
234 }
235
236 /* Append exclusion name */
237 at += sprintf(at, "%s", name);
238 }
239
240 /* This also puts a final '\0' at the end of exclusion_msg */
241 strcpy(at, "]");
242
243 end:
244 return exclusion_msg;
245 }
246
247 static void print_userspace_probe_location(struct lttng_event *event)
248 {
249 const struct lttng_userspace_probe_location *location;
250 const struct lttng_userspace_probe_location_lookup_method *lookup_method;
251 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
252
253 location = lttng_event_get_userspace_probe_location(event);
254 if (!location) {
255 MSG("Event has no userspace probe location");
256 return;
257 }
258
259 lookup_method = lttng_userspace_probe_location_get_lookup_method(location);
260 if (!lookup_method) {
261 MSG("Event has no userspace probe location lookup method");
262 return;
263 }
264
265 MSG("%s%s (type: userspace-probe)%s", indent6, event->name, enabled_string(event->enabled));
266
267 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
268
269 switch (lttng_userspace_probe_location_get_type(location)) {
270 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN:
271 MSG("%sType: Unknown", indent8);
272 break;
273 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
274 {
275 const char *function_name;
276 const char *binary_path;
277
278 MSG("%sType: Function", indent8);
279 function_name = lttng_userspace_probe_location_function_get_function_name(location);
280 binary_path = realpath(lttng_userspace_probe_location_function_get_binary_path(location), NULL);
281
282 MSG("%sBinary path: %s", indent8, binary_path ? binary_path : "NULL");
283 MSG("%sFunction: %s()", indent8, function_name ? function_name : "NULL");
284 switch (lookup_type) {
285 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
286 MSG("%sLookup method: ELF", indent8);
287 break;
288 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
289 MSG("%sLookup method: default", indent8);
290 break;
291 default:
292 MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8);
293 break;
294 }
295 break;
296 }
297 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
298 {
299 const char *probe_name, *provider_name;
300 const char *binary_path;
301
302 MSG("%sType: Tracepoint", indent8);
303 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
304 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
305 binary_path = realpath(lttng_userspace_probe_location_tracepoint_get_binary_path(location), NULL);
306 MSG("%sBinary path: %s", indent8, binary_path ? binary_path : "NULL");
307 MSG("%sTracepoint: %s:%s", indent8, provider_name ? provider_name : "NULL", probe_name ? probe_name : "NULL");
308 switch (lookup_type) {
309 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
310 MSG("%sLookup method: SDT", indent8);
311 break;
312 default:
313 MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8);
314 break;
315 }
316 break;
317 }
318 default:
319 ERR("Invalid probe type encountered");
320 }
321 }
322
323 /*
324 * Pretty print single event.
325 */
326 static void print_events(struct lttng_event *event)
327 {
328 int ret;
329 const char *filter_str;
330 char *filter_msg = NULL;
331 char *exclusion_msg = NULL;
332
333 ret = lttng_event_get_filter_expression(event, &filter_str);
334
335 if (ret) {
336 filter_msg = strdup(" [failed to retrieve filter]");
337 } else if (filter_str) {
338 const char * const filter_fmt = " [filter: '%s']";
339
340 filter_msg = malloc(strlen(filter_str) +
341 strlen(filter_fmt) + 1);
342 if (filter_msg) {
343 sprintf(filter_msg, filter_fmt,
344 filter_str);
345 }
346 }
347
348 exclusion_msg = get_exclusion_names_msg(event);
349 if (!exclusion_msg) {
350 exclusion_msg = strdup(" [failed to retrieve exclusions]");
351 }
352
353 switch (event->type) {
354 case LTTNG_EVENT_TRACEPOINT:
355 {
356 if (event->loglevel != -1) {
357 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
358 indent6,
359 event->name,
360 logleveltype_string(event->loglevel_type),
361 mi_lttng_loglevel_string(event->loglevel, handle->domain.type),
362 event->loglevel,
363 enabled_string(event->enabled),
364 safe_string(exclusion_msg),
365 safe_string(filter_msg));
366 } else {
367 MSG("%s%s (type: tracepoint)%s%s%s",
368 indent6,
369 event->name,
370 enabled_string(event->enabled),
371 safe_string(exclusion_msg),
372 safe_string(filter_msg));
373 }
374 break;
375 }
376 case LTTNG_EVENT_FUNCTION:
377 MSG("%s%s (type: function)%s%s", indent6,
378 event->name, enabled_string(event->enabled),
379 safe_string(filter_msg));
380 if (event->attr.probe.addr != 0) {
381 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
382 } else {
383 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
384 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
385 }
386 break;
387 case LTTNG_EVENT_PROBE:
388 MSG("%s%s (type: probe)%s%s", indent6,
389 event->name, enabled_string(event->enabled),
390 safe_string(filter_msg));
391 if (event->attr.probe.addr != 0) {
392 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
393 } else {
394 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
395 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
396 }
397 break;
398 case LTTNG_EVENT_USERSPACE_PROBE:
399 print_userspace_probe_location(event);
400 break;
401 case LTTNG_EVENT_FUNCTION_ENTRY:
402 MSG("%s%s (type: function)%s%s", indent6,
403 event->name, enabled_string(event->enabled),
404 safe_string(filter_msg));
405 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
406 break;
407 case LTTNG_EVENT_SYSCALL:
408 MSG("%s%s%s%s%s%s", indent6, event->name,
409 (opt_syscall ? "" : " (type:syscall)"),
410 enabled_string(event->enabled),
411 bitness_event(event->flags),
412 safe_string(filter_msg));
413 break;
414 case LTTNG_EVENT_NOOP:
415 MSG("%s (type: noop)%s%s", indent6,
416 enabled_string(event->enabled),
417 safe_string(filter_msg));
418 break;
419 case LTTNG_EVENT_ALL:
420 /* Fall-through. */
421 default:
422 /* We should never have "all" events in list. */
423 assert(0);
424 break;
425 }
426
427 free(filter_msg);
428 free(exclusion_msg);
429 }
430
431 static const char *field_type(struct lttng_event_field *field)
432 {
433 switch(field->type) {
434 case LTTNG_EVENT_FIELD_INTEGER:
435 return "integer";
436 case LTTNG_EVENT_FIELD_ENUM:
437 return "enum";
438 case LTTNG_EVENT_FIELD_FLOAT:
439 return "float";
440 case LTTNG_EVENT_FIELD_STRING:
441 return "string";
442 case LTTNG_EVENT_FIELD_OTHER:
443 default: /* fall-through */
444 return "unknown";
445 }
446 }
447
448 /*
449 * Pretty print single event fields.
450 */
451 static void print_event_field(struct lttng_event_field *field)
452 {
453 if (!field->field_name[0]) {
454 return;
455 }
456 MSG("%sfield: %s (%s)%s", indent8, field->field_name,
457 field_type(field), field->nowrite ? " [no write]" : "");
458 }
459
460 /*
461 * Machine interface
462 * Jul and ust event listing
463 */
464 static int mi_list_agent_ust_events(struct lttng_event *events, int count,
465 struct lttng_domain *domain)
466 {
467 int ret, i;
468 pid_t cur_pid = 0;
469 char *cmdline = NULL;
470 int pid_element_open = 0;
471
472 /* Open domains element */
473 ret = mi_lttng_domains_open(writer);
474 if (ret) {
475 goto end;
476 }
477
478 /* Write domain */
479 ret = mi_lttng_domain(writer, domain, 1);
480 if (ret) {
481 goto end;
482 }
483
484 /* Open pids element element */
485 ret = mi_lttng_pids_open(writer);
486 if (ret) {
487 goto end;
488 }
489
490 for (i = 0; i < count; i++) {
491 if (cur_pid != events[i].pid) {
492 if (pid_element_open) {
493 /* Close the previous events and pid element */
494 ret = mi_lttng_close_multi_element(writer, 2);
495 if (ret) {
496 goto end;
497 }
498 pid_element_open = 0;
499 }
500
501 cur_pid = events[i].pid;
502 cmdline = get_cmdline_by_pid(cur_pid);
503 if (!cmdline) {
504 ret = CMD_ERROR;
505 goto end;
506 }
507
508 if (!pid_element_open) {
509 /* Open and write a pid element */
510 ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
511 if (ret) {
512 goto error;
513 }
514
515 /* Open events element */
516 ret = mi_lttng_events_open(writer);
517 if (ret) {
518 goto error;
519 }
520
521 pid_element_open = 1;
522 }
523 free(cmdline);
524 }
525
526 /* Write an event */
527 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
528 if (ret) {
529 goto end;
530 }
531 }
532
533 /* Close pids */
534 ret = mi_lttng_writer_close_element(writer);
535 if (ret) {
536 goto end;
537 }
538
539 /* Close domain, domains */
540 ret = mi_lttng_close_multi_element(writer, 2);
541 end:
542 return ret;
543 error:
544 free(cmdline);
545 return ret;
546 }
547
548 static int list_agent_events(void)
549 {
550 int i, size, ret = CMD_SUCCESS;
551 struct lttng_domain domain;
552 struct lttng_handle *handle = NULL;
553 struct lttng_event *event_list = NULL;
554 pid_t cur_pid = 0;
555 char *cmdline = NULL;
556 const char *agent_domain_str;
557
558 memset(&domain, 0, sizeof(domain));
559 if (opt_jul) {
560 domain.type = LTTNG_DOMAIN_JUL;
561 } else if (opt_log4j) {
562 domain.type = LTTNG_DOMAIN_LOG4J;
563 } else if (opt_python) {
564 domain.type = LTTNG_DOMAIN_PYTHON;
565 } else {
566 ERR("Invalid agent domain selected.");
567 ret = CMD_ERROR;
568 goto error;
569 }
570
571 agent_domain_str = get_domain_str(domain.type);
572
573 DBG("Getting %s tracing events", agent_domain_str);
574
575 handle = lttng_create_handle(NULL, &domain);
576 if (handle == NULL) {
577 ret = CMD_ERROR;
578 goto end;
579 }
580
581 size = lttng_list_tracepoints(handle, &event_list);
582 if (size < 0) {
583 ERR("Unable to list %s events: %s", agent_domain_str,
584 lttng_strerror(size));
585 ret = CMD_ERROR;
586 goto end;
587 }
588
589 if (lttng_opt_mi) {
590 /* Mi print */
591 ret = mi_list_agent_ust_events(event_list, size, &domain);
592 if (ret) {
593 ret = CMD_ERROR;
594 goto error;
595 }
596 } else {
597 /* Pretty print */
598 MSG("%s events (Logger name):\n-------------------------",
599 agent_domain_str);
600
601 if (size == 0) {
602 MSG("None");
603 }
604
605 for (i = 0; i < size; i++) {
606 if (cur_pid != event_list[i].pid) {
607 cur_pid = event_list[i].pid;
608 cmdline = get_cmdline_by_pid(cur_pid);
609 if (cmdline == NULL) {
610 ret = CMD_ERROR;
611 goto error;
612 }
613 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
614 free(cmdline);
615 }
616 MSG("%s- %s", indent6, event_list[i].name);
617 }
618
619 MSG("");
620 }
621
622 error:
623 free(event_list);
624 end:
625 lttng_destroy_handle(handle);
626 return ret;
627 }
628
629 /*
630 * Ask session daemon for all user space tracepoints available.
631 */
632 static int list_ust_events(void)
633 {
634 int i, size, ret = CMD_SUCCESS;
635 struct lttng_domain domain;
636 struct lttng_handle *handle;
637 struct lttng_event *event_list = NULL;
638 pid_t cur_pid = 0;
639 char *cmdline = NULL;
640
641 memset(&domain, 0, sizeof(domain));
642
643 DBG("Getting UST tracing events");
644
645 domain.type = LTTNG_DOMAIN_UST;
646
647 handle = lttng_create_handle(NULL, &domain);
648 if (handle == NULL) {
649 ret = CMD_ERROR;
650 goto end;
651 }
652
653 size = lttng_list_tracepoints(handle, &event_list);
654 if (size < 0) {
655 ERR("Unable to list UST events: %s", lttng_strerror(size));
656 ret = CMD_ERROR;
657 goto error;
658 }
659
660 if (lttng_opt_mi) {
661 /* Mi print */
662 ret = mi_list_agent_ust_events(event_list, size, &domain);
663 } else {
664 /* Pretty print */
665 MSG("UST events:\n-------------");
666
667 if (size == 0) {
668 MSG("None");
669 }
670
671 for (i = 0; i < size; i++) {
672 if (cur_pid != event_list[i].pid) {
673 cur_pid = event_list[i].pid;
674 cmdline = get_cmdline_by_pid(cur_pid);
675 if (cmdline == NULL) {
676 ret = CMD_ERROR;
677 goto error;
678 }
679 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
680 free(cmdline);
681 }
682 print_events(&event_list[i]);
683 }
684
685 MSG("");
686 }
687
688 error:
689 free(event_list);
690 end:
691 lttng_destroy_handle(handle);
692 return ret;
693 }
694
695 /*
696 * Machine interface
697 * List all ust event with their fields
698 */
699 static int mi_list_ust_event_fields(struct lttng_event_field *fields, int count,
700 struct lttng_domain *domain)
701 {
702 int ret, i;
703 pid_t cur_pid = 0;
704 char *cmdline = NULL;
705 int pid_element_open = 0;
706 int event_element_open = 0;
707 struct lttng_event cur_event;
708
709 memset(&cur_event, 0, sizeof(cur_event));
710
711 /* Open domains element */
712 ret = mi_lttng_domains_open(writer);
713 if (ret) {
714 goto end;
715 }
716
717 /* Write domain */
718 ret = mi_lttng_domain(writer, domain, 1);
719 if (ret) {
720 goto end;
721 }
722
723 /* Open pids element */
724 ret = mi_lttng_pids_open(writer);
725 if (ret) {
726 goto end;
727 }
728
729 for (i = 0; i < count; i++) {
730 if (cur_pid != fields[i].event.pid) {
731 if (pid_element_open) {
732 if (event_element_open) {
733 /* Close the previous field element and event. */
734 ret = mi_lttng_close_multi_element(writer, 2);
735 if (ret) {
736 goto end;
737 }
738 event_element_open = 0;
739 }
740 /* Close the previous events, pid element */
741 ret = mi_lttng_close_multi_element(writer, 2);
742 if (ret) {
743 goto end;
744 }
745 pid_element_open = 0;
746 }
747
748 cur_pid = fields[i].event.pid;
749 cmdline = get_cmdline_by_pid(cur_pid);
750 if (!pid_element_open) {
751 /* Open and write a pid element */
752 ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
753 if (ret) {
754 goto error;
755 }
756
757 /* Open events element */
758 ret = mi_lttng_events_open(writer);
759 if (ret) {
760 goto error;
761 }
762 pid_element_open = 1;
763 }
764 free(cmdline);
765 /* Wipe current event since we are about to print a new PID. */
766 memset(&cur_event, 0, sizeof(cur_event));
767 }
768
769 if (strcmp(cur_event.name, fields[i].event.name) != 0) {
770 if (event_element_open) {
771 /* Close the previous fields element and the previous event */
772 ret = mi_lttng_close_multi_element(writer, 2);
773 if (ret) {
774 goto end;
775 }
776 event_element_open = 0;
777 }
778
779 memcpy(&cur_event, &fields[i].event,
780 sizeof(cur_event));
781
782 if (!event_element_open) {
783 /* Open and write the event */
784 ret = mi_lttng_event(writer, &cur_event, 1,
785 handle->domain.type);
786 if (ret) {
787 goto end;
788 }
789
790 /* Open a fields element */
791 ret = mi_lttng_event_fields_open(writer);
792 if (ret) {
793 goto end;
794 }
795 event_element_open = 1;
796 }
797 }
798
799 /* Print the event_field */
800 ret = mi_lttng_event_field(writer, &fields[i]);
801 if (ret) {
802 goto end;
803 }
804 }
805
806 /* Close pids, domain, domains */
807 ret = mi_lttng_close_multi_element(writer, 3);
808 end:
809 return ret;
810 error:
811 free(cmdline);
812 return ret;
813 }
814
815 /*
816 * Ask session daemon for all user space tracepoint fields available.
817 */
818 static int list_ust_event_fields(void)
819 {
820 int i, size, ret = CMD_SUCCESS;
821 struct lttng_domain domain;
822 struct lttng_handle *handle;
823 struct lttng_event_field *event_field_list;
824 pid_t cur_pid = 0;
825 char *cmdline = NULL;
826
827 struct lttng_event cur_event;
828
829 memset(&domain, 0, sizeof(domain));
830 memset(&cur_event, 0, sizeof(cur_event));
831
832 DBG("Getting UST tracing event fields");
833
834 domain.type = LTTNG_DOMAIN_UST;
835
836 handle = lttng_create_handle(NULL, &domain);
837 if (handle == NULL) {
838 ret = CMD_ERROR;
839 goto end;
840 }
841
842 size = lttng_list_tracepoint_fields(handle, &event_field_list);
843 if (size < 0) {
844 ERR("Unable to list UST event fields: %s", lttng_strerror(size));
845 ret = CMD_ERROR;
846 goto end;
847 }
848
849 if (lttng_opt_mi) {
850 /* Mi print */
851 ret = mi_list_ust_event_fields(event_field_list, size, &domain);
852 if (ret) {
853 ret = CMD_ERROR;
854 goto error;
855 }
856 } else {
857 /* Pretty print */
858 MSG("UST events:\n-------------");
859
860 if (size == 0) {
861 MSG("None");
862 }
863
864 for (i = 0; i < size; i++) {
865 if (cur_pid != event_field_list[i].event.pid) {
866 cur_pid = event_field_list[i].event.pid;
867 cmdline = get_cmdline_by_pid(cur_pid);
868 if (cmdline == NULL) {
869 ret = CMD_ERROR;
870 goto error;
871 }
872 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
873 free(cmdline);
874 /* Wipe current event since we are about to print a new PID. */
875 memset(&cur_event, 0, sizeof(cur_event));
876 }
877 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
878 print_events(&event_field_list[i].event);
879 memcpy(&cur_event, &event_field_list[i].event,
880 sizeof(cur_event));
881 }
882 print_event_field(&event_field_list[i]);
883 }
884
885 MSG("");
886 }
887
888 error:
889 free(event_field_list);
890 end:
891 lttng_destroy_handle(handle);
892 return ret;
893 }
894
895 /*
896 * Machine interface
897 * Print a list of kernel events
898 */
899 static int mi_list_kernel_events(struct lttng_event *events, int count,
900 struct lttng_domain *domain)
901 {
902 int ret, i;
903
904 /* Open domains element */
905 ret = mi_lttng_domains_open(writer);
906 if (ret) {
907 goto end;
908 }
909
910 /* Write domain */
911 ret = mi_lttng_domain(writer, domain, 1);
912 if (ret) {
913 goto end;
914 }
915
916 /* Open events */
917 ret = mi_lttng_events_open(writer);
918 if (ret) {
919 goto end;
920 }
921
922 for (i = 0; i < count; i++) {
923 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
924 if (ret) {
925 goto end;
926 }
927 }
928
929 /* close events, domain and domains */
930 ret = mi_lttng_close_multi_element(writer, 3);
931 if (ret) {
932 goto end;
933 }
934
935 end:
936 return ret;
937 }
938
939 /*
940 * Ask for all trace events in the kernel
941 */
942 static int list_kernel_events(void)
943 {
944 int i, size, ret = CMD_SUCCESS;
945 struct lttng_domain domain;
946 struct lttng_handle *handle;
947 struct lttng_event *event_list;
948
949 memset(&domain, 0, sizeof(domain));
950
951 DBG("Getting kernel tracing events");
952
953 domain.type = LTTNG_DOMAIN_KERNEL;
954
955 handle = lttng_create_handle(NULL, &domain);
956 if (handle == NULL) {
957 ret = CMD_ERROR;
958 goto error;
959 }
960
961 size = lttng_list_tracepoints(handle, &event_list);
962 if (size < 0) {
963 ERR("Unable to list kernel events: %s", lttng_strerror(size));
964 lttng_destroy_handle(handle);
965 return CMD_ERROR;
966 }
967
968 if (lttng_opt_mi) {
969 /* Mi print */
970 ret = mi_list_kernel_events(event_list, size, &domain);
971 if (ret) {
972 ret = CMD_ERROR;
973 goto end;
974 }
975 } else {
976 MSG("Kernel events:\n-------------");
977
978 for (i = 0; i < size; i++) {
979 print_events(&event_list[i]);
980 }
981
982 MSG("");
983 }
984
985 end:
986 free(event_list);
987
988 lttng_destroy_handle(handle);
989 return ret;
990
991 error:
992 lttng_destroy_handle(handle);
993 return ret;
994 }
995
996 /*
997 * Machine interface
998 * Print a list of system calls.
999 */
1000 static int mi_list_syscalls(struct lttng_event *events, int count)
1001 {
1002 int ret, i;
1003
1004 /* Open events */
1005 ret = mi_lttng_events_open(writer);
1006 if (ret) {
1007 goto end;
1008 }
1009
1010 for (i = 0; i < count; i++) {
1011 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1012 if (ret) {
1013 goto end;
1014 }
1015 }
1016
1017 /* Close events. */
1018 ret = mi_lttng_writer_close_element(writer);
1019 if (ret) {
1020 goto end;
1021 }
1022
1023 end:
1024 return ret;
1025 }
1026
1027 /*
1028 * Ask for kernel system calls.
1029 */
1030 static int list_syscalls(void)
1031 {
1032 int i, size, ret = CMD_SUCCESS;
1033 struct lttng_event *event_list;
1034
1035 DBG("Getting kernel system call events");
1036
1037 size = lttng_list_syscalls(&event_list);
1038 if (size < 0) {
1039 ERR("Unable to list system calls: %s", lttng_strerror(size));
1040 ret = CMD_ERROR;
1041 goto error;
1042 }
1043
1044 if (lttng_opt_mi) {
1045 /* Mi print */
1046 ret = mi_list_syscalls(event_list, size);
1047 if (ret) {
1048 ret = CMD_ERROR;
1049 goto end;
1050 }
1051 } else {
1052 MSG("System calls:\n-------------");
1053
1054 for (i = 0; i < size; i++) {
1055 print_events(&event_list[i]);
1056 }
1057
1058 MSG("");
1059 }
1060
1061 end:
1062 free(event_list);
1063 return ret;
1064
1065 error:
1066 return ret;
1067 }
1068
1069 /*
1070 * Machine Interface
1071 * Print a list of agent events
1072 */
1073 static int mi_list_session_agent_events(struct lttng_event *events, int count)
1074 {
1075 int ret, i;
1076
1077 /* Open events element */
1078 ret = mi_lttng_events_open(writer);
1079 if (ret) {
1080 goto end;
1081 }
1082
1083 for (i = 0; i < count; i++) {
1084 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1085 if (ret) {
1086 goto end;
1087 }
1088 }
1089
1090 /* Close events element */
1091 ret = mi_lttng_writer_close_element(writer);
1092
1093 end:
1094 return ret;
1095 }
1096
1097 /*
1098 * List agent events for a specific session using the handle.
1099 *
1100 * Return CMD_SUCCESS on success else a negative value.
1101 */
1102 static int list_session_agent_events(void)
1103 {
1104 int ret = CMD_SUCCESS, count, i;
1105 struct lttng_event *events = NULL;
1106
1107 count = lttng_list_events(handle, "", &events);
1108 if (count < 0) {
1109 ret = CMD_ERROR;
1110 ERR("%s", lttng_strerror(count));
1111 goto error;
1112 }
1113
1114 if (lttng_opt_mi) {
1115 /* Mi print */
1116 ret = mi_list_session_agent_events(events, count);
1117 if (ret) {
1118 ret = CMD_ERROR;
1119 goto end;
1120 }
1121 } else {
1122 /* Pretty print */
1123 MSG("Events (Logger name):\n---------------------");
1124 if (count == 0) {
1125 MSG("%sNone\n", indent6);
1126 goto end;
1127 }
1128
1129 for (i = 0; i < count; i++) {
1130 const char *filter_str;
1131 char *filter_msg = NULL;
1132 struct lttng_event *event = &events[i];
1133
1134 ret = lttng_event_get_filter_expression(event,
1135 &filter_str);
1136 if (ret) {
1137 filter_msg = strdup(" [failed to retrieve filter]");
1138 } else if (filter_str) {
1139 const char * const filter_fmt =
1140 " [filter: '%s']";
1141
1142 filter_msg = malloc(strlen(filter_str) +
1143 strlen(filter_fmt) + 1);
1144 if (filter_msg) {
1145 sprintf(filter_msg, filter_fmt,
1146 filter_str);
1147 }
1148 }
1149
1150 if (event->loglevel_type !=
1151 LTTNG_EVENT_LOGLEVEL_ALL) {
1152 MSG("%s- %s%s (loglevel%s %s)%s", indent4,
1153 event->name,
1154 enabled_string(event->enabled),
1155 logleveltype_string(
1156 event->loglevel_type),
1157 mi_lttng_loglevel_string(
1158 event->loglevel,
1159 handle->domain.type),
1160 safe_string(filter_msg));
1161 } else {
1162 MSG("%s- %s%s%s", indent4, event->name,
1163 enabled_string(event->enabled),
1164 safe_string(filter_msg));
1165 }
1166 free(filter_msg);
1167 }
1168
1169 MSG("");
1170 }
1171
1172 end:
1173 free(events);
1174 error:
1175 return ret;
1176 }
1177
1178 /*
1179 * Machine interface
1180 * print a list of event
1181 */
1182 static int mi_list_events(struct lttng_event *events, int count)
1183 {
1184 int ret, i;
1185
1186 /* Open events element */
1187 ret = mi_lttng_events_open(writer);
1188 if (ret) {
1189 goto end;
1190 }
1191
1192 for (i = 0; i < count; i++) {
1193 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1194 if (ret) {
1195 goto end;
1196 }
1197 }
1198
1199 /* Close events element */
1200 ret = mi_lttng_writer_close_element(writer);
1201
1202 end:
1203 return ret;
1204 }
1205
1206 /*
1207 * List events of channel of session and domain.
1208 */
1209 static int list_events(const char *channel_name)
1210 {
1211 int ret = CMD_SUCCESS, count, i;
1212 struct lttng_event *events = NULL;
1213
1214 count = lttng_list_events(handle, channel_name, &events);
1215 if (count < 0) {
1216 ret = CMD_ERROR;
1217 ERR("%s", lttng_strerror(count));
1218 goto error;
1219 }
1220
1221 if (lttng_opt_mi) {
1222 /* Mi print */
1223 ret = mi_list_events(events, count);
1224 if (ret) {
1225 ret = CMD_ERROR;
1226 goto end;
1227 }
1228 } else {
1229 /* Pretty print */
1230 MSG("\n%sEvent rules:", indent4);
1231 if (count == 0) {
1232 MSG("%sNone\n", indent6);
1233 goto end;
1234 }
1235
1236 for (i = 0; i < count; i++) {
1237 print_events(&events[i]);
1238 }
1239
1240 MSG("");
1241 }
1242 end:
1243 free(events);
1244 error:
1245 return ret;
1246 }
1247
1248 static
1249 void print_timer(const char *timer_name, uint32_t space_count, int64_t value)
1250 {
1251 uint32_t i;
1252
1253 _MSG("%s%s:", indent6, timer_name);
1254 for (i = 0; i < space_count; i++) {
1255 _MSG(" ");
1256 }
1257
1258 if (value) {
1259 MSG("%" PRId64 " %s", value, USEC_UNIT);
1260 } else {
1261 MSG("inactive");
1262 }
1263 }
1264
1265 /*
1266 * Pretty print channel
1267 */
1268 static void print_channel(struct lttng_channel *channel)
1269 {
1270 int ret;
1271 uint64_t discarded_events, lost_packets, monitor_timer_interval;
1272 int64_t blocking_timeout;
1273
1274 ret = lttng_channel_get_discarded_event_count(channel,
1275 &discarded_events);
1276 if (ret) {
1277 ERR("Failed to retrieve discarded event count of channel");
1278 return;
1279 }
1280
1281 ret = lttng_channel_get_lost_packet_count(channel,
1282 &lost_packets);
1283 if (ret) {
1284 ERR("Failed to retrieve lost packet count of channel");
1285 return;
1286 }
1287
1288 ret = lttng_channel_get_monitor_timer_interval(channel,
1289 &monitor_timer_interval);
1290 if (ret) {
1291 ERR("Failed to retrieve monitor interval of channel");
1292 return;
1293 }
1294
1295 ret = lttng_channel_get_blocking_timeout(channel,
1296 &blocking_timeout);
1297 if (ret) {
1298 ERR("Failed to retrieve blocking timeout of channel");
1299 return;
1300 }
1301
1302 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
1303 MSG("%sAttributes:", indent4);
1304 MSG("%sEvent-loss mode: %s", indent6, channel->attr.overwrite ? "overwrite" : "discard");
1305 MSG("%sSub-buffer size: %" PRIu64 " bytes", indent6, channel->attr.subbuf_size);
1306 MSG("%sSub-buffer count: %" PRIu64, indent6, channel->attr.num_subbuf);
1307
1308 print_timer("Switch timer", 5, channel->attr.switch_timer_interval);
1309 print_timer("Read timer", 7, channel->attr.read_timer_interval);
1310 print_timer("Monitor timer", 4, monitor_timer_interval);
1311
1312 if (!channel->attr.overwrite) {
1313 if (blocking_timeout == -1) {
1314 MSG("%sBlocking timeout: infinite", indent6);
1315 } else {
1316 MSG("%sBlocking timeout: %" PRId64 " %s", indent6,
1317 blocking_timeout, USEC_UNIT);
1318 }
1319 }
1320
1321 MSG("%sTrace file count: %" PRIu64 " per stream", indent6,
1322 channel->attr.tracefile_count == 0 ?
1323 1 : channel->attr.tracefile_count);
1324 if (channel->attr.tracefile_size != 0 ) {
1325 MSG("%sTrace file size: %" PRIu64 " bytes", indent6,
1326 channel->attr.tracefile_size);
1327 } else {
1328 MSG("%sTrace file size: %s", indent6, "unlimited");
1329 }
1330 switch (channel->attr.output) {
1331 case LTTNG_EVENT_SPLICE:
1332 MSG("%sOutput mode: splice", indent6);
1333 break;
1334 case LTTNG_EVENT_MMAP:
1335 MSG("%sOutput mode: mmap", indent6);
1336 break;
1337 }
1338
1339 MSG("\n%sStatistics:", indent4);
1340 if (listed_session.snapshot_mode) {
1341 /*
1342 * The lost packet count is omitted for sessions in snapshot
1343 * mode as it is misleading: it would indicate the number of
1344 * packets that the consumer could not extract during the
1345 * course of recording the snapshot. It does not have the
1346 * same meaning as the "regular" lost packet count that
1347 * would result from the consumer not keeping up with
1348 * event production in an overwrite-mode channel.
1349 *
1350 * A more interesting statistic would be the number of
1351 * packets lost between the first and last extracted
1352 * packets of a given snapshot (which prevents most analyses).
1353 */
1354 MSG("%sNone", indent6);
1355 goto skip_stats_printing;
1356 }
1357
1358 if (!channel->attr.overwrite) {
1359 MSG("%sDiscarded events: %" PRIu64, indent6, discarded_events);
1360 } else {
1361 MSG("%sLost packets: %" PRIu64, indent6, lost_packets);
1362 }
1363 skip_stats_printing:
1364 return;
1365 }
1366
1367 /*
1368 * Machine interface
1369 * Print a list of channel
1370 *
1371 */
1372 static int mi_list_channels(struct lttng_channel *channels, int count,
1373 const char *channel_name)
1374 {
1375 int i, ret;
1376 unsigned int chan_found = 0;
1377
1378 /* Open channels element */
1379 ret = mi_lttng_channels_open(writer);
1380 if (ret) {
1381 goto error;
1382 }
1383
1384 for (i = 0; i < count; i++) {
1385 if (channel_name != NULL) {
1386 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1387 chan_found = 1;
1388 } else {
1389 continue;
1390 }
1391 }
1392
1393 /* Write channel element and leave it open */
1394 ret = mi_lttng_channel(writer, &channels[i], 1);
1395 if (ret) {
1396 goto error;
1397 }
1398
1399 /* Listing events per channel */
1400 ret = list_events(channels[i].name);
1401 if (ret) {
1402 goto error;
1403 }
1404
1405 /* Closing the channel element we opened earlier */
1406 ret = mi_lttng_writer_close_element(writer);
1407 if (ret) {
1408 goto error;
1409 }
1410
1411 if (chan_found) {
1412 break;
1413 }
1414 }
1415
1416 /* Close channels element */
1417 ret = mi_lttng_writer_close_element(writer);
1418 if (ret) {
1419 goto error;
1420 }
1421
1422 error:
1423 return ret;
1424 }
1425
1426 /*
1427 * List channel(s) of session and domain.
1428 *
1429 * If channel_name is NULL, all channels are listed.
1430 */
1431 static int list_channels(const char *channel_name)
1432 {
1433 int count, i, ret = CMD_SUCCESS;
1434 unsigned int chan_found = 0;
1435 struct lttng_channel *channels = NULL;
1436
1437 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
1438
1439 count = lttng_list_channels(handle, &channels);
1440 if (count < 0) {
1441 switch (-count) {
1442 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
1443 if (lttng_opt_mi) {
1444 /* When printing mi this is not an error
1445 * but an empty channels element */
1446 count = 0;
1447 } else {
1448 ret = CMD_SUCCESS;
1449 goto error_channels;
1450 }
1451 break;
1452 default:
1453 /* We had a real error */
1454 ret = CMD_ERROR;
1455 ERR("%s", lttng_strerror(count));
1456 goto error_channels;
1457 break;
1458 }
1459 }
1460
1461 if (lttng_opt_mi) {
1462 /* Mi print */
1463 ret = mi_list_channels(channels, count, channel_name);
1464 if (ret) {
1465 ret = CMD_ERROR;
1466 goto error;
1467 }
1468 } else {
1469 /* Pretty print */
1470 if (count) {
1471 MSG("Channels:\n-------------");
1472 }
1473
1474 for (i = 0; i < count; i++) {
1475 if (channel_name != NULL) {
1476 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1477 chan_found = 1;
1478 } else {
1479 continue;
1480 }
1481 }
1482 print_channel(&channels[i]);
1483
1484 /* Listing events per channel */
1485 ret = list_events(channels[i].name);
1486 if (ret) {
1487 goto error;
1488 }
1489
1490 if (chan_found) {
1491 break;
1492 }
1493 }
1494
1495 if (!chan_found && channel_name != NULL) {
1496 ret = CMD_ERROR;
1497 ERR("Channel %s not found", channel_name);
1498 goto error;
1499 }
1500 }
1501 error:
1502 free(channels);
1503
1504 error_channels:
1505 return ret;
1506 }
1507
1508 static const char *get_capitalized_process_attr_str(enum lttng_process_attr process_attr)
1509 {
1510 switch (process_attr) {
1511 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1512 return "Process ID";
1513 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1514 return "Virtual process ID";
1515 case LTTNG_PROCESS_ATTR_USER_ID:
1516 return "User ID";
1517 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1518 return "Virtual user ID";
1519 case LTTNG_PROCESS_ATTR_GROUP_ID:
1520 return "Group ID";
1521 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1522 return "Virtual group ID";
1523 default:
1524 return "Unknown";
1525 }
1526 return NULL;
1527 }
1528
1529 static int handle_process_attr_status(enum lttng_process_attr process_attr,
1530 enum lttng_process_attr_tracker_handle_status status)
1531 {
1532 int ret = CMD_SUCCESS;
1533
1534 switch (status) {
1535 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY:
1536 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK:
1537 /* Carry on. */
1538 break;
1539 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR:
1540 ERR("Communication occurred while fetching %s tracker",
1541 lttng_process_attr_to_string(process_attr));
1542 ret = CMD_ERROR;
1543 break;
1544 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST:
1545 ERR("Failed to get the inclusion set of the %s tracker: session `%s` no longer exists",
1546 lttng_process_attr_to_string(process_attr),
1547 handle->session_name);
1548 ret = CMD_ERROR;
1549 break;
1550 default:
1551 ERR("Unknown error occurred while fetching the inclusion set of the %s tracker",
1552 lttng_process_attr_to_string(process_attr));
1553 ret = CMD_ERROR;
1554 break;
1555 }
1556
1557 return ret;
1558 }
1559
1560 static int mi_output_empty_tracker(enum lttng_process_attr process_attr)
1561 {
1562 int ret;
1563
1564 ret = mi_lttng_process_attribute_tracker_open(writer, process_attr);
1565 if (ret) {
1566 goto end;
1567 }
1568
1569 ret = mi_lttng_close_multi_element(writer, 2);
1570 end:
1571 return ret;
1572 }
1573
1574 static inline bool is_value_type_name(
1575 enum lttng_process_attr_value_type value_type)
1576 {
1577 return value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ||
1578 value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME;
1579 }
1580
1581 /*
1582 * List a process attribute tracker for a session and domain tuple.
1583 */
1584 static int list_process_attr_tracker(enum lttng_process_attr process_attr)
1585 {
1586 int ret = 0;
1587 unsigned int count, i;
1588 enum lttng_tracking_policy policy;
1589 enum lttng_error_code ret_code;
1590 enum lttng_process_attr_tracker_handle_status handle_status;
1591 enum lttng_process_attr_values_status values_status;
1592 const struct lttng_process_attr_values *values;
1593 struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
1594
1595 ret_code = lttng_session_get_tracker_handle(handle->session_name,
1596 handle->domain.type, process_attr, &tracker_handle);
1597 if (ret_code != LTTNG_OK) {
1598 ERR("Failed to get process attribute tracker handle: %s",
1599 lttng_strerror(ret_code));
1600 ret = CMD_ERROR;
1601 goto end;
1602 }
1603
1604 handle_status = lttng_process_attr_tracker_handle_get_inclusion_set(
1605 tracker_handle, &values);
1606 ret = handle_process_attr_status(process_attr, handle_status);
1607 if (ret != CMD_SUCCESS) {
1608 goto end;
1609 }
1610
1611 handle_status = lttng_process_attr_tracker_handle_get_tracking_policy(
1612 tracker_handle, &policy);
1613 ret = handle_process_attr_status(process_attr, handle_status);
1614 if (ret != CMD_SUCCESS) {
1615 goto end;
1616 }
1617
1618 {
1619 char *process_attr_name;
1620 const int print_ret = asprintf(&process_attr_name, "%ss:",
1621 get_capitalized_process_attr_str(process_attr));
1622
1623 if (print_ret == -1) {
1624 ret = CMD_FATAL;
1625 goto end;
1626 }
1627 _MSG(" %-22s", process_attr_name);
1628 free(process_attr_name);
1629 }
1630 switch (policy) {
1631 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1632 break;
1633 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1634 if (writer) {
1635 mi_output_empty_tracker(process_attr);
1636 }
1637 MSG("none");
1638 ret = CMD_SUCCESS;
1639 goto end;
1640 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1641 MSG("all");
1642 ret = CMD_SUCCESS;
1643 goto end;
1644 default:
1645 ERR("Unknown tracking policy encoutered while listing the %s process attribute tracker of session `%s`",
1646 lttng_process_attr_to_string(process_attr),
1647 handle->session_name);
1648 ret = CMD_FATAL;
1649 goto end;
1650 }
1651
1652 values_status = lttng_process_attr_values_get_count(values, &count);
1653 if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) {
1654 ERR("Failed to get the count of values in the inclusion set of the %s process attribute tracker of session `%s`",
1655 lttng_process_attr_to_string(process_attr),
1656 handle->session_name);
1657 ret = CMD_FATAL;
1658 goto end;
1659 }
1660
1661 if (count == 0) {
1662 /* Functionally equivalent to the 'exclude all' policy. */
1663 if (writer) {
1664 mi_output_empty_tracker(process_attr);
1665 }
1666 MSG("none");
1667 ret = CMD_SUCCESS;
1668 goto end;
1669 }
1670
1671 /* Mi tracker_id element */
1672 if (writer) {
1673 /* Open tracker_id and targets elements */
1674 ret = mi_lttng_process_attribute_tracker_open(
1675 writer, process_attr);
1676 if (ret) {
1677 goto end;
1678 }
1679 }
1680
1681 for (i = 0; i < count; i++) {
1682 const enum lttng_process_attr_value_type value_type =
1683 lttng_process_attr_values_get_type_at_index(
1684 values, i);
1685 int64_t integral_value = INT64_MAX;
1686 const char *name = "error";
1687
1688 if (i >= 1) {
1689 _MSG(", ");
1690 }
1691 switch (value_type) {
1692 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
1693 {
1694 pid_t pid;
1695
1696 values_status = lttng_process_attr_values_get_pid_at_index(
1697 values, i, &pid);
1698 integral_value = (int64_t) pid;
1699 break;
1700 }
1701 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
1702 {
1703 uid_t uid;
1704
1705 values_status = lttng_process_attr_values_get_uid_at_index(
1706 values, i, &uid);
1707 integral_value = (int64_t) uid;
1708 break;
1709 }
1710 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
1711 {
1712 gid_t gid;
1713
1714 values_status = lttng_process_attr_values_get_gid_at_index(
1715 values, i, &gid);
1716 integral_value = (int64_t) gid;
1717 break;
1718 }
1719 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
1720 values_status = lttng_process_attr_values_get_user_name_at_index(
1721 values, i, &name);
1722 break;
1723 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
1724 values_status = lttng_process_attr_values_get_group_name_at_index(
1725 values, i, &name);
1726 break;
1727 default:
1728 ret = CMD_ERROR;
1729 goto end;
1730 }
1731
1732 if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) {
1733 /*
1734 * Not possible given the current liblttng-ctl
1735 * implementation.
1736 */
1737 ERR("Unknown error occurred while fetching process attribute value in inclusion list");
1738 ret = CMD_FATAL;
1739 goto end;
1740 }
1741
1742 if (is_value_type_name(value_type)) {
1743 _MSG("`%s`", name);
1744 } else {
1745 _MSG("%" PRIi64, integral_value);
1746 }
1747
1748 /* Mi */
1749 if (writer) {
1750 ret = is_value_type_name(value_type) ?
1751 mi_lttng_string_process_attribute_value(
1752 writer,
1753 process_attr,
1754 name, false) :
1755 mi_lttng_integral_process_attribute_value(
1756 writer,
1757 process_attr,
1758 integral_value,
1759 false);
1760 if (ret) {
1761 goto end;
1762 }
1763 }
1764 }
1765 MSG("");
1766
1767 /* Mi close tracker_id and targets */
1768 if (writer) {
1769 ret = mi_lttng_close_multi_element(writer, 2);
1770 if (ret) {
1771 goto end;
1772 }
1773 }
1774 end:
1775 lttng_process_attr_tracker_handle_destroy(tracker_handle);
1776 return ret;
1777 }
1778
1779 /*
1780 * List all trackers of a domain
1781 */
1782 static int list_trackers(const struct lttng_domain *domain)
1783 {
1784 int ret = 0;
1785
1786 MSG("Tracked process attributes");
1787 /* Trackers listing */
1788 if (lttng_opt_mi) {
1789 ret = mi_lttng_trackers_open(writer);
1790 if (ret) {
1791 goto end;
1792 }
1793 }
1794
1795 switch (domain->type) {
1796 case LTTNG_DOMAIN_KERNEL:
1797 /* pid tracker */
1798 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_PROCESS_ID);
1799 if (ret) {
1800 goto end;
1801 }
1802 /* vpid tracker */
1803 ret = list_process_attr_tracker(
1804 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID);
1805 if (ret) {
1806 goto end;
1807 }
1808 /* uid tracker */
1809 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_USER_ID);
1810 if (ret) {
1811 goto end;
1812 }
1813 /* vuid tracker */
1814 ret = list_process_attr_tracker(
1815 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID);
1816 if (ret) {
1817 goto end;
1818 }
1819 /* gid tracker */
1820 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_GROUP_ID);
1821 if (ret) {
1822 goto end;
1823 }
1824 /* vgid tracker */
1825 ret = list_process_attr_tracker(
1826 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID);
1827 if (ret) {
1828 goto end;
1829 }
1830 break;
1831 case LTTNG_DOMAIN_UST:
1832 /* vpid tracker */
1833 ret = list_process_attr_tracker(
1834 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID);
1835 if (ret) {
1836 goto end;
1837 }
1838 /* vuid tracker */
1839 ret = list_process_attr_tracker(
1840 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID);
1841 if (ret) {
1842 goto end;
1843 }
1844 /* vgid tracker */
1845 ret = list_process_attr_tracker(
1846 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID);
1847 if (ret) {
1848 goto end;
1849 }
1850 break;
1851 default:
1852 break;
1853 }
1854 MSG();
1855 if (lttng_opt_mi) {
1856 /* Close trackers element */
1857 ret = mi_lttng_writer_close_element(writer);
1858 if (ret) {
1859 goto end;
1860 }
1861 }
1862
1863 end:
1864 return ret;
1865 }
1866
1867 static enum cmd_error_code print_periodic_rotation_schedule(
1868 const struct lttng_rotation_schedule *schedule)
1869 {
1870 enum cmd_error_code ret;
1871 enum lttng_rotation_status status;
1872 uint64_t value;
1873
1874 status = lttng_rotation_schedule_periodic_get_period(schedule,
1875 &value);
1876 if (status != LTTNG_ROTATION_STATUS_OK) {
1877 ERR("Failed to retrieve period parameter from periodic rotation schedule.");
1878 ret = CMD_ERROR;
1879 goto end;
1880 }
1881
1882 MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT);
1883 ret = CMD_SUCCESS;
1884 end:
1885 return ret;
1886 }
1887
1888 static enum cmd_error_code print_size_threshold_rotation_schedule(
1889 const struct lttng_rotation_schedule *schedule)
1890 {
1891 enum cmd_error_code ret;
1892 enum lttng_rotation_status status;
1893 uint64_t value;
1894
1895 status = lttng_rotation_schedule_size_threshold_get_threshold(schedule,
1896 &value);
1897 if (status != LTTNG_ROTATION_STATUS_OK) {
1898 ERR("Failed to retrieve size parameter from size-based rotation schedule.");
1899 ret = CMD_ERROR;
1900 goto end;
1901 }
1902
1903 MSG(" size threshold: %" PRIu64" bytes", value);
1904 ret = CMD_SUCCESS;
1905 end:
1906 return ret;
1907 }
1908
1909 static enum cmd_error_code print_rotation_schedule(
1910 const struct lttng_rotation_schedule *schedule)
1911 {
1912 enum cmd_error_code ret;
1913
1914 switch (lttng_rotation_schedule_get_type(schedule)) {
1915 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
1916 ret = print_size_threshold_rotation_schedule(schedule);
1917 break;
1918 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
1919 ret = print_periodic_rotation_schedule(schedule);
1920 break;
1921 default:
1922 ret = CMD_ERROR;
1923 }
1924 return ret;
1925 }
1926
1927 /*
1928 * List the automatic rotation settings.
1929 */
1930 static enum cmd_error_code list_rotate_settings(const char *session_name)
1931 {
1932 int ret;
1933 enum cmd_error_code cmd_ret = CMD_SUCCESS;
1934 unsigned int count, i;
1935 struct lttng_rotation_schedules *schedules = NULL;
1936 enum lttng_rotation_status status;
1937
1938 ret = lttng_session_list_rotation_schedules(session_name, &schedules);
1939 if (ret != LTTNG_OK) {
1940 ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret));
1941 cmd_ret = CMD_ERROR;
1942 goto end;
1943 }
1944
1945 status = lttng_rotation_schedules_get_count(schedules, &count);
1946 if (status != LTTNG_ROTATION_STATUS_OK) {
1947 ERR("Failed to retrieve the number of session rotation schedules.");
1948 cmd_ret = CMD_ERROR;
1949 goto end;
1950 }
1951
1952 if (count == 0) {
1953 cmd_ret = CMD_SUCCESS;
1954 goto end;
1955 }
1956
1957 MSG("Automatic rotation schedules:");
1958 if (lttng_opt_mi) {
1959 ret = mi_lttng_writer_open_element(writer,
1960 mi_lttng_element_rotation_schedules);
1961 if (ret) {
1962 cmd_ret = CMD_ERROR;
1963 goto end;
1964 }
1965 }
1966
1967 for (i = 0; i < count; i++) {
1968 enum cmd_error_code tmp_ret = CMD_SUCCESS;
1969 const struct lttng_rotation_schedule *schedule;
1970
1971 schedule = lttng_rotation_schedules_get_at_index(schedules, i);
1972 if (!schedule) {
1973 ERR("Failed to retrieve session rotation schedule.");
1974 cmd_ret = CMD_ERROR;
1975 goto end;
1976 }
1977
1978 if (lttng_opt_mi) {
1979 ret = mi_lttng_rotation_schedule(writer, schedule);
1980 if (ret) {
1981 tmp_ret = CMD_ERROR;
1982 }
1983 } else {
1984 tmp_ret = print_rotation_schedule(schedule);
1985 }
1986
1987 /*
1988 * Report an error if the serialization of any of the
1989 * descriptors failed.
1990 */
1991 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
1992 }
1993
1994 _MSG("\n");
1995 if (lttng_opt_mi) {
1996 /* Close the rotation_schedules element. */
1997 ret = mi_lttng_writer_close_element(writer);
1998 if (ret) {
1999 cmd_ret = CMD_ERROR;
2000 goto end;
2001 }
2002 }
2003 end:
2004 lttng_rotation_schedules_destroy(schedules);
2005 return cmd_ret;
2006 }
2007
2008 /*
2009 * Machine interface
2010 * Find the session with session_name as name
2011 * and print his informations.
2012 */
2013 static int mi_list_session(const char *session_name,
2014 struct lttng_session *sessions, int count)
2015 {
2016 int ret, i;
2017 unsigned int session_found = 0;
2018
2019 if (session_name == NULL) {
2020 ret = -LTTNG_ERR_SESS_NOT_FOUND;
2021 goto end;
2022 }
2023
2024 for (i = 0; i < count; i++) {
2025 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
2026 /* We need to leave it open to append other informations
2027 * like domain, channel, events etc.*/
2028 session_found = 1;
2029 ret = mi_lttng_session(writer, &sessions[i], 1);
2030 if (ret) {
2031 goto end;
2032 }
2033 break;
2034 }
2035 }
2036
2037 if (!session_found) {
2038 ERR("Session '%s' not found", session_name);
2039 ret = -LTTNG_ERR_SESS_NOT_FOUND;
2040 goto end;
2041 }
2042
2043 end:
2044 return ret;
2045 }
2046
2047 /*
2048 * Machine interface
2049 * List all availables session
2050 */
2051 static int mi_list_sessions(struct lttng_session *sessions, int count)
2052 {
2053 int ret, i;
2054
2055 /* Opening sessions element */
2056 ret = mi_lttng_sessions_open(writer);
2057 if (ret) {
2058 goto end;
2059 }
2060
2061 /* Listing sessions */
2062 for (i = 0; i < count; i++) {
2063 ret = mi_lttng_session(writer, &sessions[i], 0);
2064 if (ret) {
2065 goto end;
2066 }
2067 }
2068
2069 /* Closing sessions element */
2070 ret = mi_lttng_writer_close_element(writer);
2071 if (ret) {
2072 goto end;
2073 }
2074
2075 end:
2076 return ret;
2077 }
2078
2079 /*
2080 * List available tracing session. List only basic information.
2081 *
2082 * If session_name is NULL, all sessions are listed.
2083 */
2084 static int list_sessions(const char *session_name)
2085 {
2086 int ret = CMD_SUCCESS;
2087 int count, i;
2088 unsigned int session_found = 0;
2089 struct lttng_session *sessions = NULL;
2090
2091 count = lttng_list_sessions(&sessions);
2092 DBG("Session count %d", count);
2093 if (count < 0) {
2094 ret = CMD_ERROR;
2095 ERR("%s", lttng_strerror(count));
2096 goto end;
2097 }
2098
2099 if (lttng_opt_mi) {
2100 /* Mi */
2101 if (session_name == NULL) {
2102 /* List all sessions */
2103 ret = mi_list_sessions(sessions, count);
2104 } else {
2105 /* Note : this return an open session element */
2106 ret = mi_list_session(session_name, sessions, count);
2107 }
2108 if (ret) {
2109 ret = CMD_ERROR;
2110 goto end;
2111 }
2112 } else {
2113 /* Pretty print */
2114 if (count == 0) {
2115 MSG("Currently no available tracing session");
2116 goto end;
2117 }
2118
2119 if (session_name == NULL) {
2120 MSG("Available tracing sessions:");
2121 }
2122
2123 for (i = 0; i < count; i++) {
2124 if (session_name != NULL) {
2125 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
2126 session_found = 1;
2127 MSG("Tracing session %s: [%s%s]", session_name,
2128 active_string(sessions[i].enabled),
2129 snapshot_string(sessions[i].snapshot_mode));
2130 if (*sessions[i].path) {
2131 MSG("%sTrace output: %s\n", indent4, sessions[i].path);
2132 }
2133 memcpy(&listed_session, &sessions[i],
2134 sizeof(listed_session));
2135 break;
2136 }
2137 } else {
2138 MSG(" %d) %s [%s%s]", i + 1,
2139 sessions[i].name,
2140 active_string(sessions[i].enabled),
2141 snapshot_string(sessions[i].snapshot_mode));
2142 if (*sessions[i].path) {
2143 MSG("%sTrace output: %s", indent4, sessions[i].path);
2144 }
2145 if (sessions[i].live_timer_interval != 0) {
2146 MSG("%sLive timer interval: %u %s", indent4,
2147 sessions[i].live_timer_interval,
2148 USEC_UNIT);
2149 }
2150 MSG("");
2151 }
2152 }
2153
2154 if (!session_found && session_name != NULL) {
2155 ERR("Session '%s' not found", session_name);
2156 ret = CMD_ERROR;
2157 goto end;
2158 }
2159
2160 if (session_name == NULL) {
2161 MSG("\nUse lttng list <session_name> for more details");
2162 }
2163 }
2164
2165 end:
2166 free(sessions);
2167 return ret;
2168 }
2169
2170
2171 /*
2172 * Machine Interface
2173 * list available domain(s) for a session.
2174 */
2175 static int mi_list_domains(struct lttng_domain *domains, int count)
2176 {
2177 int i, ret;
2178 /* Open domains element */
2179 ret = mi_lttng_domains_open(writer);
2180 if (ret) {
2181 goto end;
2182 }
2183
2184 for (i = 0; i < count; i++) {
2185 ret = mi_lttng_domain(writer, &domains[i] , 0);
2186 if (ret) {
2187 goto end;
2188 }
2189 }
2190
2191 /* Closing domains element */
2192 ret = mi_lttng_writer_close_element(writer);
2193 if (ret) {
2194 goto end;
2195 }
2196 end:
2197 return ret;
2198 }
2199
2200 /*
2201 * List available domain(s) for a session.
2202 */
2203 static int list_domains(const char *session_name)
2204 {
2205 int i, count, ret = CMD_SUCCESS;
2206 struct lttng_domain *domains = NULL;
2207
2208
2209 count = lttng_list_domains(session_name, &domains);
2210 if (count < 0) {
2211 ret = CMD_ERROR;
2212 ERR("%s", lttng_strerror(count));
2213 goto end;
2214 }
2215
2216 if (lttng_opt_mi) {
2217 /* Mi output */
2218 ret = mi_list_domains(domains, count);
2219 if (ret) {
2220 ret = CMD_ERROR;
2221 goto error;
2222 }
2223 } else {
2224 /* Pretty print */
2225 MSG("Domains:\n-------------");
2226 if (count == 0) {
2227 MSG(" None");
2228 goto end;
2229 }
2230
2231 for (i = 0; i < count; i++) {
2232 switch (domains[i].type) {
2233 case LTTNG_DOMAIN_KERNEL:
2234 MSG(" - Kernel");
2235 break;
2236 case LTTNG_DOMAIN_UST:
2237 MSG(" - UST global");
2238 break;
2239 case LTTNG_DOMAIN_JUL:
2240 MSG(" - JUL (Java Util Logging)");
2241 break;
2242 case LTTNG_DOMAIN_LOG4J:
2243 MSG(" - LOG4j (Logging for Java)");
2244 break;
2245 case LTTNG_DOMAIN_PYTHON:
2246 MSG(" - Python (logging)");
2247 break;
2248 default:
2249 break;
2250 }
2251 }
2252 }
2253
2254 error:
2255 free(domains);
2256
2257 end:
2258 return ret;
2259 }
2260
2261 /*
2262 * The 'list <options>' first level command
2263 */
2264 int cmd_list(int argc, const char **argv)
2265 {
2266 int opt, ret = CMD_SUCCESS;
2267 const char *session_name, *leftover = NULL;
2268 static poptContext pc;
2269 struct lttng_domain domain;
2270 struct lttng_domain *domains = NULL;
2271
2272 memset(&domain, 0, sizeof(domain));
2273
2274 if (argc < 1) {
2275 ret = CMD_ERROR;
2276 goto end;
2277 }
2278
2279 pc = poptGetContext(NULL, argc, argv, long_options, 0);
2280 poptReadDefaultConfig(pc, 0);
2281
2282 while ((opt = poptGetNextOpt(pc)) != -1) {
2283 switch (opt) {
2284 case OPT_HELP:
2285 SHOW_HELP();
2286 goto end;
2287 case OPT_USERSPACE:
2288 opt_userspace = 1;
2289 break;
2290 case OPT_LIST_OPTIONS:
2291 list_cmd_options(stdout, long_options);
2292 goto end;
2293 default:
2294 ret = CMD_UNDEFINED;
2295 goto end;
2296 }
2297 }
2298
2299 /* Mi check */
2300 if (lttng_opt_mi) {
2301 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
2302 if (!writer) {
2303 ret = CMD_ERROR;
2304 goto end;
2305 }
2306
2307 /* Open command element */
2308 ret = mi_lttng_writer_command_open(writer,
2309 mi_lttng_element_command_list);
2310 if (ret) {
2311 ret = CMD_ERROR;
2312 goto end;
2313 }
2314
2315 /* Open output element */
2316 ret = mi_lttng_writer_open_element(writer,
2317 mi_lttng_element_command_output);
2318 if (ret) {
2319 ret = CMD_ERROR;
2320 goto end;
2321 }
2322 }
2323
2324 /* Get session name (trailing argument) */
2325 session_name = poptGetArg(pc);
2326 DBG2("Session name: %s", session_name);
2327
2328 leftover = poptGetArg(pc);
2329 if (leftover) {
2330 ERR("Unknown argument: %s", leftover);
2331 ret = CMD_ERROR;
2332 goto end;
2333 }
2334
2335 if (opt_kernel) {
2336 domain.type = LTTNG_DOMAIN_KERNEL;
2337 } else if (opt_userspace) {
2338 DBG2("Listing userspace global domain");
2339 domain.type = LTTNG_DOMAIN_UST;
2340 } else if (opt_jul) {
2341 DBG2("Listing JUL domain");
2342 domain.type = LTTNG_DOMAIN_JUL;
2343 } else if (opt_log4j) {
2344 domain.type = LTTNG_DOMAIN_LOG4J;
2345 } else if (opt_python) {
2346 domain.type = LTTNG_DOMAIN_PYTHON;
2347 }
2348
2349 if (!opt_kernel && opt_syscall) {
2350 WARN("--syscall will only work with the Kernel domain (-k)");
2351 ret = CMD_ERROR;
2352 goto end;
2353 }
2354
2355 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
2356 handle = lttng_create_handle(session_name, &domain);
2357 if (handle == NULL) {
2358 ret = CMD_FATAL;
2359 goto end;
2360 }
2361 }
2362
2363 if (session_name == NULL) {
2364 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
2365 && !opt_python) {
2366 ret = list_sessions(NULL);
2367 if (ret) {
2368 goto end;
2369 }
2370 }
2371 if (opt_kernel) {
2372 if (opt_syscall) {
2373 ret = list_syscalls();
2374 if (ret) {
2375 goto end;
2376 }
2377 } else {
2378 ret = list_kernel_events();
2379 if (ret) {
2380 goto end;
2381 }
2382 }
2383 }
2384 if (opt_userspace) {
2385 if (opt_fields) {
2386 ret = list_ust_event_fields();
2387 } else {
2388 ret = list_ust_events();
2389 }
2390 if (ret) {
2391 goto end;
2392 }
2393 }
2394 if (opt_jul || opt_log4j || opt_python) {
2395 ret = list_agent_events();
2396 if (ret) {
2397 goto end;
2398 }
2399 }
2400 } else {
2401 /* List session attributes */
2402 if (lttng_opt_mi) {
2403 /* Open element sessions
2404 * Present for xml consistency */
2405 ret = mi_lttng_sessions_open(writer);
2406 if (ret) {
2407 goto end;
2408 }
2409 }
2410 /* MI: the ouptut of list_sessions is an unclosed session element */
2411 ret = list_sessions(session_name);
2412 if (ret) {
2413 goto end;
2414 }
2415
2416 ret = list_rotate_settings(session_name);
2417 if (ret) {
2418 goto end;
2419 }
2420
2421 /* Domain listing */
2422 if (opt_domain) {
2423 ret = list_domains(session_name);
2424 goto end;
2425 }
2426
2427 /* Channel listing */
2428 if (opt_kernel || opt_userspace) {
2429 if (lttng_opt_mi) {
2430 /* Add of domains and domain element for xml
2431 * consistency and validation
2432 */
2433 ret = mi_lttng_domains_open(writer);
2434 if (ret) {
2435 goto end;
2436 }
2437
2438 /* Open domain and leave it open for
2439 * nested channels printing */
2440 ret = mi_lttng_domain(writer, &domain, 1);
2441 if (ret) {
2442 goto end;
2443 }
2444
2445 }
2446
2447
2448 /* Trackers */
2449 ret = list_trackers(&domain);
2450 if (ret) {
2451 goto end;
2452 }
2453
2454 /* Channels */
2455 ret = list_channels(opt_channel);
2456 if (ret) {
2457 goto end;
2458 }
2459
2460 if (lttng_opt_mi) {
2461 /* Close domain and domain element */
2462 ret = mi_lttng_close_multi_element(writer, 2);
2463 }
2464 if (ret) {
2465 goto end;
2466 }
2467
2468
2469 } else {
2470 int i, nb_domain;
2471
2472 /* We want all domain(s) */
2473 nb_domain = lttng_list_domains(session_name, &domains);
2474 if (nb_domain < 0) {
2475 ret = CMD_ERROR;
2476 ERR("%s", lttng_strerror(nb_domain));
2477 goto end;
2478 }
2479
2480 if (lttng_opt_mi) {
2481 ret = mi_lttng_domains_open(writer);
2482 if (ret) {
2483 ret = CMD_ERROR;
2484 goto end;
2485 }
2486 }
2487
2488 for (i = 0; i < nb_domain; i++) {
2489 switch (domains[i].type) {
2490 case LTTNG_DOMAIN_KERNEL:
2491 MSG("=== Domain: Linux kernel ===\n");
2492 break;
2493 case LTTNG_DOMAIN_UST:
2494 MSG("=== Domain: User space ===\n");
2495 MSG("Buffering scheme: %s\n",
2496 domains[i].buf_type ==
2497 LTTNG_BUFFER_PER_PID ? "per-process" : "per-user");
2498 break;
2499 case LTTNG_DOMAIN_JUL:
2500 MSG("=== Domain: java.util.logging (JUL) ===\n");
2501 break;
2502 case LTTNG_DOMAIN_LOG4J:
2503 MSG("=== Domain: log4j ===\n");
2504 break;
2505 case LTTNG_DOMAIN_PYTHON:
2506 MSG("=== Domain: Python logging ===\n");
2507 break;
2508 default:
2509 MSG("=== Domain: Unimplemented ===\n");
2510 break;
2511 }
2512
2513 if (lttng_opt_mi) {
2514 ret = mi_lttng_domain(writer, &domains[i], 1);
2515 if (ret) {
2516 ret = CMD_ERROR;
2517 goto end;
2518 }
2519 }
2520
2521 /* Clean handle before creating a new one */
2522 if (handle) {
2523 lttng_destroy_handle(handle);
2524 }
2525
2526 handle = lttng_create_handle(session_name, &domains[i]);
2527 if (handle == NULL) {
2528 ret = CMD_FATAL;
2529 goto end;
2530 }
2531
2532 if (domains[i].type == LTTNG_DOMAIN_JUL ||
2533 domains[i].type == LTTNG_DOMAIN_LOG4J ||
2534 domains[i].type == LTTNG_DOMAIN_PYTHON) {
2535 ret = list_session_agent_events();
2536 if (ret) {
2537 goto end;
2538 }
2539
2540 goto next_domain;
2541 }
2542
2543 switch (domains[i].type) {
2544 case LTTNG_DOMAIN_KERNEL:
2545 case LTTNG_DOMAIN_UST:
2546 ret = list_trackers(&domains[i]);
2547 if (ret) {
2548 goto end;
2549 }
2550 break;
2551 default:
2552 break;
2553 }
2554
2555 ret = list_channels(opt_channel);
2556 if (ret) {
2557 goto end;
2558 }
2559
2560 next_domain:
2561 if (lttng_opt_mi) {
2562 /* Close domain element */
2563 ret = mi_lttng_writer_close_element(writer);
2564 if (ret) {
2565 ret = CMD_ERROR;
2566 goto end;
2567 }
2568 }
2569
2570 }
2571 if (lttng_opt_mi) {
2572 /* Close the domains, session and sessions element */
2573 ret = mi_lttng_close_multi_element(writer, 3);
2574 if (ret) {
2575 ret = CMD_ERROR;
2576 goto end;
2577 }
2578 }
2579 }
2580 }
2581
2582 /* Mi closing */
2583 if (lttng_opt_mi) {
2584 /* Close output element */
2585 ret = mi_lttng_writer_close_element(writer);
2586 if (ret) {
2587 ret = CMD_ERROR;
2588 goto end;
2589 }
2590
2591 /* Command element close */
2592 ret = mi_lttng_writer_command_close(writer);
2593 if (ret) {
2594 ret = CMD_ERROR;
2595 goto end;
2596 }
2597 }
2598 end:
2599 /* Mi clean-up */
2600 if (writer && mi_lttng_writer_destroy(writer)) {
2601 /* Preserve original error code */
2602 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2603 }
2604
2605 free(domains);
2606 if (handle) {
2607 lttng_destroy_handle(handle);
2608 }
2609
2610 poptFreeContext(pc);
2611 return ret;
2612 }
This page took 0.11424 seconds and 5 git commands to generate.