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