Refactor: lttng-ctl: follow terminology of the tracker documentation
[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 WARN("No kernel channel");
1450 goto error_channels;
1451 }
1452 break;
1453 default:
1454 /* We had a real error */
1455 ret = CMD_ERROR;
1456 ERR("%s", lttng_strerror(count));
1457 goto error_channels;
1458 break;
1459 }
1460 }
1461
1462 if (lttng_opt_mi) {
1463 /* Mi print */
1464 ret = mi_list_channels(channels, count, channel_name);
1465 if (ret) {
1466 ret = CMD_ERROR;
1467 goto error;
1468 }
1469 } else {
1470 /* Pretty print */
1471 if (count) {
1472 MSG("Channels:\n-------------");
1473 }
1474
1475 for (i = 0; i < count; i++) {
1476 if (channel_name != NULL) {
1477 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1478 chan_found = 1;
1479 } else {
1480 continue;
1481 }
1482 }
1483 print_channel(&channels[i]);
1484
1485 /* Listing events per channel */
1486 ret = list_events(channels[i].name);
1487 if (ret) {
1488 goto error;
1489 }
1490
1491 if (chan_found) {
1492 break;
1493 }
1494 }
1495
1496 if (!chan_found && channel_name != NULL) {
1497 ret = CMD_ERROR;
1498 ERR("Channel %s not found", channel_name);
1499 goto error;
1500 }
1501 }
1502 error:
1503 free(channels);
1504
1505 error_channels:
1506 return ret;
1507 }
1508
1509 static const char *get_capitalized_process_attr_str(enum lttng_process_attr process_attr)
1510 {
1511 switch (process_attr) {
1512 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1513 return "Process ID";
1514 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1515 return "Virtual process ID";
1516 case LTTNG_PROCESS_ATTR_USER_ID:
1517 return "User ID";
1518 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1519 return "Virtual user ID";
1520 case LTTNG_PROCESS_ATTR_GROUP_ID:
1521 return "Group ID";
1522 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1523 return "Virtual group ID";
1524 default:
1525 return "Unknown";
1526 }
1527 return NULL;
1528 }
1529
1530 static int handle_process_attr_status(enum lttng_process_attr process_attr,
1531 enum lttng_process_attr_tracker_handle_status status)
1532 {
1533 int ret = CMD_SUCCESS;
1534
1535 switch (status) {
1536 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY:
1537 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK:
1538 /* Carry on. */
1539 break;
1540 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR:
1541 ERR("Communication occurred while fetching %s tracker",
1542 lttng_process_attr_to_string(process_attr));
1543 ret = CMD_ERROR;
1544 break;
1545 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST:
1546 ERR("Failed to get the inclusion set of the %s tracker: session `%s` no longer exists",
1547 lttng_process_attr_to_string(process_attr),
1548 handle->session_name);
1549 ret = CMD_ERROR;
1550 break;
1551 default:
1552 ERR("Unknown error occurred while fetching the inclusion set of the %s tracker",
1553 lttng_process_attr_to_string(process_attr));
1554 ret = CMD_ERROR;
1555 break;
1556 }
1557
1558 return ret;
1559 }
1560
1561 static int mi_output_empty_tracker(enum lttng_process_attr process_attr)
1562 {
1563 int ret;
1564
1565 ret = mi_lttng_process_attribute_tracker_open(writer, process_attr);
1566 if (ret) {
1567 goto end;
1568 }
1569
1570 ret = mi_lttng_close_multi_element(writer, 2);
1571 end:
1572 return ret;
1573 }
1574
1575 static inline bool is_value_type_name(
1576 enum lttng_process_attr_value_type value_type)
1577 {
1578 return value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ||
1579 value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME;
1580 }
1581
1582 /*
1583 * List a process attribute tracker for a session and domain tuple.
1584 */
1585 static int list_process_attr_tracker(enum lttng_process_attr process_attr)
1586 {
1587 int ret = 0;
1588 unsigned int count, i;
1589 enum lttng_tracking_policy policy;
1590 enum lttng_error_code ret_code;
1591 enum lttng_process_attr_tracker_handle_status handle_status;
1592 enum lttng_process_attr_values_status values_status;
1593 const struct lttng_process_attr_values *values;
1594 struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
1595
1596 ret_code = lttng_session_get_tracker_handle(handle->session_name,
1597 handle->domain.type, process_attr, &tracker_handle);
1598 if (ret_code != LTTNG_OK) {
1599 ERR("Failed to get process attribute tracker handle: %s",
1600 lttng_strerror(ret_code));
1601 ret = CMD_ERROR;
1602 goto end;
1603 }
1604
1605 handle_status = lttng_process_attr_tracker_handle_get_inclusion_set(
1606 tracker_handle, &values);
1607 ret = handle_process_attr_status(process_attr, handle_status);
1608 if (ret != CMD_SUCCESS) {
1609 goto end;
1610 }
1611
1612 handle_status = lttng_process_attr_tracker_handle_get_tracking_policy(
1613 tracker_handle, &policy);
1614 ret = handle_process_attr_status(process_attr, handle_status);
1615 if (ret != CMD_SUCCESS) {
1616 goto end;
1617 }
1618
1619 {
1620 char *process_attr_name;
1621 const int print_ret = asprintf(&process_attr_name, "%ss:",
1622 get_capitalized_process_attr_str(process_attr));
1623
1624 if (print_ret == -1) {
1625 ret = CMD_FATAL;
1626 goto end;
1627 }
1628 _MSG(" %-22s", process_attr_name);
1629 free(process_attr_name);
1630 }
1631 switch (policy) {
1632 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1633 break;
1634 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1635 if (writer) {
1636 mi_output_empty_tracker(process_attr);
1637 }
1638 MSG("none");
1639 ret = CMD_SUCCESS;
1640 goto end;
1641 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1642 MSG("all");
1643 ret = CMD_SUCCESS;
1644 goto end;
1645 default:
1646 ERR("Unknown tracking policy encoutered while listing the %s process attribute tracker of session `%s`",
1647 lttng_process_attr_to_string(process_attr),
1648 handle->session_name);
1649 ret = CMD_FATAL;
1650 goto end;
1651 }
1652
1653 values_status = lttng_process_attr_values_get_count(values, &count);
1654 if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) {
1655 ERR("Failed to get the count of values in the inclusion set of the %s process attribute tracker of session `%s`",
1656 lttng_process_attr_to_string(process_attr),
1657 handle->session_name);
1658 ret = CMD_FATAL;
1659 goto end;
1660 }
1661
1662 if (count == 0) {
1663 /* Functionally equivalent to the 'exclude all' policy. */
1664 if (writer) {
1665 mi_output_empty_tracker(process_attr);
1666 }
1667 MSG("none");
1668 ret = CMD_SUCCESS;
1669 goto end;
1670 }
1671
1672 /* Mi tracker_id element */
1673 if (writer) {
1674 /* Open tracker_id and targets elements */
1675 ret = mi_lttng_process_attribute_tracker_open(
1676 writer, process_attr);
1677 if (ret) {
1678 goto end;
1679 }
1680 }
1681
1682 for (i = 0; i < count; i++) {
1683 const enum lttng_process_attr_value_type value_type =
1684 lttng_process_attr_values_get_type_at_index(
1685 values, i);
1686 int64_t integral_value = INT64_MAX;
1687 const char *name = "error";
1688
1689 if (i >= 1) {
1690 _MSG(", ");
1691 }
1692 switch (value_type) {
1693 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
1694 {
1695 pid_t pid;
1696
1697 values_status = lttng_process_attr_values_get_pid_at_index(
1698 values, i, &pid);
1699 integral_value = (int64_t) pid;
1700 break;
1701 }
1702 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
1703 {
1704 uid_t uid;
1705
1706 values_status = lttng_process_attr_values_get_uid_at_index(
1707 values, i, &uid);
1708 integral_value = (int64_t) uid;
1709 break;
1710 }
1711 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
1712 {
1713 gid_t gid;
1714
1715 values_status = lttng_process_attr_values_get_gid_at_index(
1716 values, i, &gid);
1717 integral_value = (int64_t) gid;
1718 break;
1719 }
1720 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
1721 values_status = lttng_process_attr_values_get_user_name_at_index(
1722 values, i, &name);
1723 break;
1724 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
1725 values_status = lttng_process_attr_values_get_group_name_at_index(
1726 values, i, &name);
1727 break;
1728 default:
1729 ret = CMD_ERROR;
1730 goto end;
1731 }
1732
1733 if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) {
1734 /*
1735 * Not possible given the current liblttng-ctl
1736 * implementation.
1737 */
1738 ERR("Unknown error occurred while fetching process attribute value in inclusion list");
1739 ret = CMD_FATAL;
1740 goto end;
1741 }
1742
1743 if (is_value_type_name(value_type)) {
1744 _MSG("`%s`", name);
1745 } else {
1746 _MSG("%" PRIi64, integral_value);
1747 }
1748
1749 /* Mi */
1750 if (writer) {
1751 ret = is_value_type_name(value_type) ?
1752 mi_lttng_string_process_attribute_value(
1753 writer,
1754 process_attr,
1755 name, false) :
1756 mi_lttng_integral_process_attribute_value(
1757 writer,
1758 process_attr,
1759 integral_value,
1760 false);
1761 if (ret) {
1762 goto end;
1763 }
1764 }
1765 }
1766 MSG("");
1767
1768 /* Mi close tracker_id and targets */
1769 if (writer) {
1770 ret = mi_lttng_close_multi_element(writer, 2);
1771 if (ret) {
1772 goto end;
1773 }
1774 }
1775 end:
1776 lttng_process_attr_tracker_handle_destroy(tracker_handle);
1777 return ret;
1778 }
1779
1780 /*
1781 * List all trackers of a domain
1782 */
1783 static int list_trackers(const struct lttng_domain *domain)
1784 {
1785 int ret = 0;
1786
1787 MSG("Tracked process attributes");
1788 /* Trackers listing */
1789 if (lttng_opt_mi) {
1790 ret = mi_lttng_trackers_open(writer);
1791 if (ret) {
1792 goto end;
1793 }
1794 }
1795
1796 switch (domain->type) {
1797 case LTTNG_DOMAIN_KERNEL:
1798 /* pid tracker */
1799 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_PROCESS_ID);
1800 if (ret) {
1801 goto end;
1802 }
1803 /* vpid tracker */
1804 ret = list_process_attr_tracker(
1805 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID);
1806 if (ret) {
1807 goto end;
1808 }
1809 /* uid tracker */
1810 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_USER_ID);
1811 if (ret) {
1812 goto end;
1813 }
1814 /* vuid tracker */
1815 ret = list_process_attr_tracker(
1816 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID);
1817 if (ret) {
1818 goto end;
1819 }
1820 /* gid tracker */
1821 ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_GROUP_ID);
1822 if (ret) {
1823 goto end;
1824 }
1825 /* vgid tracker */
1826 ret = list_process_attr_tracker(
1827 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID);
1828 if (ret) {
1829 goto end;
1830 }
1831 break;
1832 case LTTNG_DOMAIN_UST:
1833 /* vpid tracker */
1834 ret = list_process_attr_tracker(
1835 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID);
1836 if (ret) {
1837 goto end;
1838 }
1839 /* vuid tracker */
1840 ret = list_process_attr_tracker(
1841 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID);
1842 if (ret) {
1843 goto end;
1844 }
1845 /* vgid tracker */
1846 ret = list_process_attr_tracker(
1847 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID);
1848 if (ret) {
1849 goto end;
1850 }
1851 break;
1852 default:
1853 break;
1854 }
1855 MSG();
1856 if (lttng_opt_mi) {
1857 /* Close trackers element */
1858 ret = mi_lttng_writer_close_element(writer);
1859 if (ret) {
1860 goto end;
1861 }
1862 }
1863
1864 end:
1865 return ret;
1866 }
1867
1868 static enum cmd_error_code print_periodic_rotation_schedule(
1869 const struct lttng_rotation_schedule *schedule)
1870 {
1871 enum cmd_error_code ret;
1872 enum lttng_rotation_status status;
1873 uint64_t value;
1874
1875 status = lttng_rotation_schedule_periodic_get_period(schedule,
1876 &value);
1877 if (status != LTTNG_ROTATION_STATUS_OK) {
1878 ERR("Failed to retrieve period parameter from periodic rotation schedule.");
1879 ret = CMD_ERROR;
1880 goto end;
1881 }
1882
1883 MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT);
1884 ret = CMD_SUCCESS;
1885 end:
1886 return ret;
1887 }
1888
1889 static enum cmd_error_code print_size_threshold_rotation_schedule(
1890 const struct lttng_rotation_schedule *schedule)
1891 {
1892 enum cmd_error_code ret;
1893 enum lttng_rotation_status status;
1894 uint64_t value;
1895
1896 status = lttng_rotation_schedule_size_threshold_get_threshold(schedule,
1897 &value);
1898 if (status != LTTNG_ROTATION_STATUS_OK) {
1899 ERR("Failed to retrieve size parameter from size-based rotation schedule.");
1900 ret = CMD_ERROR;
1901 goto end;
1902 }
1903
1904 MSG(" size threshold: %" PRIu64" bytes", value);
1905 ret = CMD_SUCCESS;
1906 end:
1907 return ret;
1908 }
1909
1910 static enum cmd_error_code print_rotation_schedule(
1911 const struct lttng_rotation_schedule *schedule)
1912 {
1913 enum cmd_error_code ret;
1914
1915 switch (lttng_rotation_schedule_get_type(schedule)) {
1916 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
1917 ret = print_size_threshold_rotation_schedule(schedule);
1918 break;
1919 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
1920 ret = print_periodic_rotation_schedule(schedule);
1921 break;
1922 default:
1923 ret = CMD_ERROR;
1924 }
1925 return ret;
1926 }
1927
1928 /*
1929 * List the automatic rotation settings.
1930 */
1931 static enum cmd_error_code list_rotate_settings(const char *session_name)
1932 {
1933 int ret;
1934 enum cmd_error_code cmd_ret = CMD_SUCCESS;
1935 unsigned int count, i;
1936 struct lttng_rotation_schedules *schedules = NULL;
1937 enum lttng_rotation_status status;
1938
1939 ret = lttng_session_list_rotation_schedules(session_name, &schedules);
1940 if (ret != LTTNG_OK) {
1941 ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret));
1942 cmd_ret = CMD_ERROR;
1943 goto end;
1944 }
1945
1946 status = lttng_rotation_schedules_get_count(schedules, &count);
1947 if (status != LTTNG_ROTATION_STATUS_OK) {
1948 ERR("Failed to retrieve the number of session rotation schedules.");
1949 cmd_ret = CMD_ERROR;
1950 goto end;
1951 }
1952
1953 if (count == 0) {
1954 cmd_ret = CMD_SUCCESS;
1955 goto end;
1956 }
1957
1958 MSG("Automatic rotation schedules:");
1959 if (lttng_opt_mi) {
1960 ret = mi_lttng_writer_open_element(writer,
1961 mi_lttng_element_rotation_schedules);
1962 if (ret) {
1963 cmd_ret = CMD_ERROR;
1964 goto end;
1965 }
1966 }
1967
1968 for (i = 0; i < count; i++) {
1969 enum cmd_error_code tmp_ret = CMD_SUCCESS;
1970 const struct lttng_rotation_schedule *schedule;
1971
1972 schedule = lttng_rotation_schedules_get_at_index(schedules, i);
1973 if (!schedule) {
1974 ERR("Failed to retrieve session rotation schedule.");
1975 cmd_ret = CMD_ERROR;
1976 goto end;
1977 }
1978
1979 if (lttng_opt_mi) {
1980 ret = mi_lttng_rotation_schedule(writer, schedule);
1981 if (ret) {
1982 tmp_ret = CMD_ERROR;
1983 }
1984 } else {
1985 tmp_ret = print_rotation_schedule(schedule);
1986 }
1987
1988 /*
1989 * Report an error if the serialization of any of the
1990 * descriptors failed.
1991 */
1992 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
1993 }
1994
1995 _MSG("\n");
1996 if (lttng_opt_mi) {
1997 /* Close the rotation_schedules element. */
1998 ret = mi_lttng_writer_close_element(writer);
1999 if (ret) {
2000 cmd_ret = CMD_ERROR;
2001 goto end;
2002 }
2003 }
2004 end:
2005 lttng_rotation_schedules_destroy(schedules);
2006 return cmd_ret;
2007 }
2008
2009 /*
2010 * Machine interface
2011 * Find the session with session_name as name
2012 * and print his informations.
2013 */
2014 static int mi_list_session(const char *session_name,
2015 struct lttng_session *sessions, int count)
2016 {
2017 int ret, i;
2018 unsigned int session_found = 0;
2019
2020 if (session_name == NULL) {
2021 ret = -LTTNG_ERR_SESS_NOT_FOUND;
2022 goto end;
2023 }
2024
2025 for (i = 0; i < count; i++) {
2026 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
2027 /* We need to leave it open to append other informations
2028 * like domain, channel, events etc.*/
2029 session_found = 1;
2030 ret = mi_lttng_session(writer, &sessions[i], 1);
2031 if (ret) {
2032 goto end;
2033 }
2034 break;
2035 }
2036 }
2037
2038 if (!session_found) {
2039 ERR("Session '%s' not found", session_name);
2040 ret = -LTTNG_ERR_SESS_NOT_FOUND;
2041 goto end;
2042 }
2043
2044 end:
2045 return ret;
2046 }
2047
2048 /*
2049 * Machine interface
2050 * List all availables session
2051 */
2052 static int mi_list_sessions(struct lttng_session *sessions, int count)
2053 {
2054 int ret, i;
2055
2056 /* Opening sessions element */
2057 ret = mi_lttng_sessions_open(writer);
2058 if (ret) {
2059 goto end;
2060 }
2061
2062 /* Listing sessions */
2063 for (i = 0; i < count; i++) {
2064 ret = mi_lttng_session(writer, &sessions[i], 0);
2065 if (ret) {
2066 goto end;
2067 }
2068 }
2069
2070 /* Closing sessions element */
2071 ret = mi_lttng_writer_close_element(writer);
2072 if (ret) {
2073 goto end;
2074 }
2075
2076 end:
2077 return ret;
2078 }
2079
2080 /*
2081 * List available tracing session. List only basic information.
2082 *
2083 * If session_name is NULL, all sessions are listed.
2084 */
2085 static int list_sessions(const char *session_name)
2086 {
2087 int ret = CMD_SUCCESS;
2088 int count, i;
2089 unsigned int session_found = 0;
2090 struct lttng_session *sessions = NULL;
2091
2092 count = lttng_list_sessions(&sessions);
2093 DBG("Session count %d", count);
2094 if (count < 0) {
2095 ret = CMD_ERROR;
2096 ERR("%s", lttng_strerror(count));
2097 goto end;
2098 }
2099
2100 if (lttng_opt_mi) {
2101 /* Mi */
2102 if (session_name == NULL) {
2103 /* List all sessions */
2104 ret = mi_list_sessions(sessions, count);
2105 } else {
2106 /* Note : this return an open session element */
2107 ret = mi_list_session(session_name, sessions, count);
2108 }
2109 if (ret) {
2110 ret = CMD_ERROR;
2111 goto end;
2112 }
2113 } else {
2114 /* Pretty print */
2115 if (count == 0) {
2116 MSG("Currently no available tracing session");
2117 goto end;
2118 }
2119
2120 if (session_name == NULL) {
2121 MSG("Available tracing sessions:");
2122 }
2123
2124 for (i = 0; i < count; i++) {
2125 if (session_name != NULL) {
2126 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
2127 session_found = 1;
2128 MSG("Tracing session %s: [%s%s]", session_name,
2129 active_string(sessions[i].enabled),
2130 snapshot_string(sessions[i].snapshot_mode));
2131 if (*sessions[i].path) {
2132 MSG("%sTrace output: %s\n", indent4, sessions[i].path);
2133 }
2134 memcpy(&listed_session, &sessions[i],
2135 sizeof(listed_session));
2136 break;
2137 }
2138 } else {
2139 MSG(" %d) %s [%s%s]", i + 1,
2140 sessions[i].name,
2141 active_string(sessions[i].enabled),
2142 snapshot_string(sessions[i].snapshot_mode));
2143 if (*sessions[i].path) {
2144 MSG("%sTrace output: %s", indent4, sessions[i].path);
2145 }
2146 if (sessions[i].live_timer_interval != 0) {
2147 MSG("%sLive timer interval: %u %s", indent4,
2148 sessions[i].live_timer_interval,
2149 USEC_UNIT);
2150 }
2151 MSG("");
2152 }
2153 }
2154
2155 if (!session_found && session_name != NULL) {
2156 ERR("Session '%s' not found", session_name);
2157 ret = CMD_ERROR;
2158 goto end;
2159 }
2160
2161 if (session_name == NULL) {
2162 MSG("\nUse lttng list <session_name> for more details");
2163 }
2164 }
2165
2166 end:
2167 free(sessions);
2168 return ret;
2169 }
2170
2171
2172 /*
2173 * Machine Interface
2174 * list available domain(s) for a session.
2175 */
2176 static int mi_list_domains(struct lttng_domain *domains, int count)
2177 {
2178 int i, ret;
2179 /* Open domains element */
2180 ret = mi_lttng_domains_open(writer);
2181 if (ret) {
2182 goto end;
2183 }
2184
2185 for (i = 0; i < count; i++) {
2186 ret = mi_lttng_domain(writer, &domains[i] , 0);
2187 if (ret) {
2188 goto end;
2189 }
2190 }
2191
2192 /* Closing domains element */
2193 ret = mi_lttng_writer_close_element(writer);
2194 if (ret) {
2195 goto end;
2196 }
2197 end:
2198 return ret;
2199 }
2200
2201 /*
2202 * List available domain(s) for a session.
2203 */
2204 static int list_domains(const char *session_name)
2205 {
2206 int i, count, ret = CMD_SUCCESS;
2207 struct lttng_domain *domains = NULL;
2208
2209
2210 count = lttng_list_domains(session_name, &domains);
2211 if (count < 0) {
2212 ret = CMD_ERROR;
2213 ERR("%s", lttng_strerror(count));
2214 goto end;
2215 }
2216
2217 if (lttng_opt_mi) {
2218 /* Mi output */
2219 ret = mi_list_domains(domains, count);
2220 if (ret) {
2221 ret = CMD_ERROR;
2222 goto error;
2223 }
2224 } else {
2225 /* Pretty print */
2226 MSG("Domains:\n-------------");
2227 if (count == 0) {
2228 MSG(" None");
2229 goto end;
2230 }
2231
2232 for (i = 0; i < count; i++) {
2233 switch (domains[i].type) {
2234 case LTTNG_DOMAIN_KERNEL:
2235 MSG(" - Kernel");
2236 break;
2237 case LTTNG_DOMAIN_UST:
2238 MSG(" - UST global");
2239 break;
2240 case LTTNG_DOMAIN_JUL:
2241 MSG(" - JUL (Java Util Logging)");
2242 break;
2243 case LTTNG_DOMAIN_LOG4J:
2244 MSG(" - LOG4j (Logging for Java)");
2245 break;
2246 case LTTNG_DOMAIN_PYTHON:
2247 MSG(" - Python (logging)");
2248 break;
2249 default:
2250 break;
2251 }
2252 }
2253 }
2254
2255 error:
2256 free(domains);
2257
2258 end:
2259 return ret;
2260 }
2261
2262 /*
2263 * The 'list <options>' first level command
2264 */
2265 int cmd_list(int argc, const char **argv)
2266 {
2267 int opt, ret = CMD_SUCCESS;
2268 const char *session_name, *leftover = NULL;
2269 static poptContext pc;
2270 struct lttng_domain domain;
2271 struct lttng_domain *domains = NULL;
2272
2273 memset(&domain, 0, sizeof(domain));
2274
2275 if (argc < 1) {
2276 ret = CMD_ERROR;
2277 goto end;
2278 }
2279
2280 pc = poptGetContext(NULL, argc, argv, long_options, 0);
2281 poptReadDefaultConfig(pc, 0);
2282
2283 while ((opt = poptGetNextOpt(pc)) != -1) {
2284 switch (opt) {
2285 case OPT_HELP:
2286 SHOW_HELP();
2287 goto end;
2288 case OPT_USERSPACE:
2289 opt_userspace = 1;
2290 break;
2291 case OPT_LIST_OPTIONS:
2292 list_cmd_options(stdout, long_options);
2293 goto end;
2294 default:
2295 ret = CMD_UNDEFINED;
2296 goto end;
2297 }
2298 }
2299
2300 /* Mi check */
2301 if (lttng_opt_mi) {
2302 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
2303 if (!writer) {
2304 ret = CMD_ERROR;
2305 goto end;
2306 }
2307
2308 /* Open command element */
2309 ret = mi_lttng_writer_command_open(writer,
2310 mi_lttng_element_command_list);
2311 if (ret) {
2312 ret = CMD_ERROR;
2313 goto end;
2314 }
2315
2316 /* Open output element */
2317 ret = mi_lttng_writer_open_element(writer,
2318 mi_lttng_element_command_output);
2319 if (ret) {
2320 ret = CMD_ERROR;
2321 goto end;
2322 }
2323 }
2324
2325 /* Get session name (trailing argument) */
2326 session_name = poptGetArg(pc);
2327 DBG2("Session name: %s", session_name);
2328
2329 leftover = poptGetArg(pc);
2330 if (leftover) {
2331 ERR("Unknown argument: %s", leftover);
2332 ret = CMD_ERROR;
2333 goto end;
2334 }
2335
2336 if (opt_kernel) {
2337 domain.type = LTTNG_DOMAIN_KERNEL;
2338 } else if (opt_userspace) {
2339 DBG2("Listing userspace global domain");
2340 domain.type = LTTNG_DOMAIN_UST;
2341 } else if (opt_jul) {
2342 DBG2("Listing JUL domain");
2343 domain.type = LTTNG_DOMAIN_JUL;
2344 } else if (opt_log4j) {
2345 domain.type = LTTNG_DOMAIN_LOG4J;
2346 } else if (opt_python) {
2347 domain.type = LTTNG_DOMAIN_PYTHON;
2348 }
2349
2350 if (!opt_kernel && opt_syscall) {
2351 WARN("--syscall will only work with the Kernel domain (-k)");
2352 ret = CMD_ERROR;
2353 goto end;
2354 }
2355
2356 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
2357 handle = lttng_create_handle(session_name, &domain);
2358 if (handle == NULL) {
2359 ret = CMD_FATAL;
2360 goto end;
2361 }
2362 }
2363
2364 if (session_name == NULL) {
2365 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
2366 && !opt_python) {
2367 ret = list_sessions(NULL);
2368 if (ret) {
2369 goto end;
2370 }
2371 }
2372 if (opt_kernel) {
2373 if (opt_syscall) {
2374 ret = list_syscalls();
2375 if (ret) {
2376 goto end;
2377 }
2378 } else {
2379 ret = list_kernel_events();
2380 if (ret) {
2381 goto end;
2382 }
2383 }
2384 }
2385 if (opt_userspace) {
2386 if (opt_fields) {
2387 ret = list_ust_event_fields();
2388 } else {
2389 ret = list_ust_events();
2390 }
2391 if (ret) {
2392 goto end;
2393 }
2394 }
2395 if (opt_jul || opt_log4j || opt_python) {
2396 ret = list_agent_events();
2397 if (ret) {
2398 goto end;
2399 }
2400 }
2401 } else {
2402 /* List session attributes */
2403 if (lttng_opt_mi) {
2404 /* Open element sessions
2405 * Present for xml consistency */
2406 ret = mi_lttng_sessions_open(writer);
2407 if (ret) {
2408 goto end;
2409 }
2410 }
2411 /* MI: the ouptut of list_sessions is an unclosed session element */
2412 ret = list_sessions(session_name);
2413 if (ret) {
2414 goto end;
2415 }
2416
2417 ret = list_rotate_settings(session_name);
2418 if (ret) {
2419 goto end;
2420 }
2421
2422 /* Domain listing */
2423 if (opt_domain) {
2424 ret = list_domains(session_name);
2425 goto end;
2426 }
2427
2428 /* Channel listing */
2429 if (opt_kernel || opt_userspace) {
2430 if (lttng_opt_mi) {
2431 /* Add of domains and domain element for xml
2432 * consistency and validation
2433 */
2434 ret = mi_lttng_domains_open(writer);
2435 if (ret) {
2436 goto end;
2437 }
2438
2439 /* Open domain and leave it open for
2440 * nested channels printing */
2441 ret = mi_lttng_domain(writer, &domain, 1);
2442 if (ret) {
2443 goto end;
2444 }
2445
2446 }
2447
2448
2449 /* Trackers */
2450 ret = list_trackers(&domain);
2451 if (ret) {
2452 goto end;
2453 }
2454
2455 /* Channels */
2456 ret = list_channels(opt_channel);
2457 if (ret) {
2458 goto end;
2459 }
2460
2461 if (lttng_opt_mi) {
2462 /* Close domain and domain element */
2463 ret = mi_lttng_close_multi_element(writer, 2);
2464 }
2465 if (ret) {
2466 goto end;
2467 }
2468
2469
2470 } else {
2471 int i, nb_domain;
2472
2473 /* We want all domain(s) */
2474 nb_domain = lttng_list_domains(session_name, &domains);
2475 if (nb_domain < 0) {
2476 ret = CMD_ERROR;
2477 ERR("%s", lttng_strerror(nb_domain));
2478 goto end;
2479 }
2480
2481 if (lttng_opt_mi) {
2482 ret = mi_lttng_domains_open(writer);
2483 if (ret) {
2484 ret = CMD_ERROR;
2485 goto end;
2486 }
2487 }
2488
2489 for (i = 0; i < nb_domain; i++) {
2490 switch (domains[i].type) {
2491 case LTTNG_DOMAIN_KERNEL:
2492 MSG("=== Domain: Linux kernel ===\n");
2493 break;
2494 case LTTNG_DOMAIN_UST:
2495 MSG("=== Domain: User space ===\n");
2496 MSG("Buffering scheme: %s\n",
2497 domains[i].buf_type ==
2498 LTTNG_BUFFER_PER_PID ? "per-process" : "per-user");
2499 break;
2500 case LTTNG_DOMAIN_JUL:
2501 MSG("=== Domain: java.util.logging (JUL) ===\n");
2502 break;
2503 case LTTNG_DOMAIN_LOG4J:
2504 MSG("=== Domain: log4j ===\n");
2505 break;
2506 case LTTNG_DOMAIN_PYTHON:
2507 MSG("=== Domain: Python logging ===\n");
2508 break;
2509 default:
2510 MSG("=== Domain: Unimplemented ===\n");
2511 break;
2512 }
2513
2514 if (lttng_opt_mi) {
2515 ret = mi_lttng_domain(writer, &domains[i], 1);
2516 if (ret) {
2517 ret = CMD_ERROR;
2518 goto end;
2519 }
2520 }
2521
2522 /* Clean handle before creating a new one */
2523 if (handle) {
2524 lttng_destroy_handle(handle);
2525 }
2526
2527 handle = lttng_create_handle(session_name, &domains[i]);
2528 if (handle == NULL) {
2529 ret = CMD_FATAL;
2530 goto end;
2531 }
2532
2533 if (domains[i].type == LTTNG_DOMAIN_JUL ||
2534 domains[i].type == LTTNG_DOMAIN_LOG4J ||
2535 domains[i].type == LTTNG_DOMAIN_PYTHON) {
2536 ret = list_session_agent_events();
2537 if (ret) {
2538 goto end;
2539 }
2540
2541 goto next_domain;
2542 }
2543
2544 switch (domains[i].type) {
2545 case LTTNG_DOMAIN_KERNEL:
2546 case LTTNG_DOMAIN_UST:
2547 ret = list_trackers(&domains[i]);
2548 if (ret) {
2549 goto end;
2550 }
2551 break;
2552 default:
2553 break;
2554 }
2555
2556 ret = list_channels(opt_channel);
2557 if (ret) {
2558 goto end;
2559 }
2560
2561 next_domain:
2562 if (lttng_opt_mi) {
2563 /* Close domain element */
2564 ret = mi_lttng_writer_close_element(writer);
2565 if (ret) {
2566 ret = CMD_ERROR;
2567 goto end;
2568 }
2569 }
2570
2571 }
2572 if (lttng_opt_mi) {
2573 /* Close the domains, session and sessions element */
2574 ret = mi_lttng_close_multi_element(writer, 3);
2575 if (ret) {
2576 ret = CMD_ERROR;
2577 goto end;
2578 }
2579 }
2580 }
2581 }
2582
2583 /* Mi closing */
2584 if (lttng_opt_mi) {
2585 /* Close output element */
2586 ret = mi_lttng_writer_close_element(writer);
2587 if (ret) {
2588 ret = CMD_ERROR;
2589 goto end;
2590 }
2591
2592 /* Command element close */
2593 ret = mi_lttng_writer_command_close(writer);
2594 if (ret) {
2595 ret = CMD_ERROR;
2596 goto end;
2597 }
2598 }
2599 end:
2600 /* Mi clean-up */
2601 if (writer && mi_lttng_writer_destroy(writer)) {
2602 /* Preserve original error code */
2603 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2604 }
2605
2606 free(domains);
2607 if (handle) {
2608 lttng_destroy_handle(handle);
2609 }
2610
2611 poptFreeContext(pc);
2612 return ret;
2613 }
This page took 0.149637 seconds and 4 git commands to generate.