2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "../command.hpp"
13 #include <common/mi-lttng.hpp>
14 #include <common/time.hpp>
15 #include <common/tracker.hpp>
17 #include <lttng/domain-internal.hpp>
18 #include <lttng/lttng.h>
26 static int opt_userspace
;
27 static int opt_kernel
;
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
;
36 const char *indent4
= " ";
37 const char *indent6
= " ";
38 const char *indent8
= " ";
40 #ifdef LTTNG_EMBED_HELP
41 static const char help_msg
[] =
42 #include <lttng-list.1.h>
52 static struct lttng_handle
*the_handle
;
53 static struct mi_writer
*the_writer
;
55 /* Only set when listing a single session. */
56 static struct lttng_session the_listed_session
;
58 static struct poptOption long_options
[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
61 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
62 { "jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, nullptr, nullptr },
63 { "log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, nullptr, nullptr },
64 { "python", 'p', POPT_ARG_VAL
, &opt_python
, 1, nullptr, nullptr },
65 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
66 { "channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, nullptr, nullptr },
67 { "domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, nullptr, nullptr },
68 { "fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, nullptr, nullptr },
69 { "syscall", 'S', POPT_ARG_VAL
, &opt_syscall
, 1, nullptr, nullptr },
70 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
71 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
75 * Get command line from /proc for a specific pid.
77 * On success, return an allocated string pointer to the proc cmdline.
78 * On error, return NULL.
80 static char *get_cmdline_by_pid(pid_t pid
)
84 char *cmdline
= nullptr;
85 /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */
86 char path
[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR
) - 1];
88 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
89 fp
= fopen(path
, "r");
94 /* Caller must free() *cmdline */
95 cmdline
= zmalloc
<char>(PATH_MAX
);
97 PERROR("malloc cmdline");
101 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
103 PERROR("fread proc list");
113 static const char *active_string(int value
)
127 static const char *snapshot_string(int value
)
137 static const char *enabled_string(int value
)
141 return " [disabled]";
151 static const char *safe_string(const char *str
)
153 return str
? str
: "";
156 static const char *logleveltype_string(enum lttng_loglevel_type value
)
159 case LTTNG_EVENT_LOGLEVEL_ALL
:
161 case LTTNG_EVENT_LOGLEVEL_RANGE
:
163 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
166 return " <<TYPE UNKN>>";
170 static const char *bitness_event(enum lttng_event_flag flags
)
172 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_32
) {
173 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
174 return " [32/64-bit]";
178 } else if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
186 * Get exclusion names message for a single event.
188 * Returned pointer must be freed by caller. Returns NULL on error.
190 static char *get_exclusion_names_msg(struct lttng_event
*event
)
194 char *exclusion_msg
= nullptr;
197 const char *const exclusion_fmt
= " [exclusions: ";
198 const size_t exclusion_fmt_len
= strlen(exclusion_fmt
);
200 exclusion_count
= lttng_event_get_exclusion_name_count(event
);
201 if (exclusion_count
< 0) {
203 } else if (exclusion_count
== 0) {
205 * No exclusions: return copy of empty string so that
206 * it can be freed by caller.
208 exclusion_msg
= strdup("");
213 * exclusion_msg's size is bounded by the exclusion_fmt string,
214 * a comma per entry, the entry count (fixed-size), a closing
215 * bracket, and a trailing \0.
217 exclusion_msg
= (char *) malloc(exclusion_count
+ exclusion_count
* LTTNG_SYMBOL_NAME_LEN
+
218 exclusion_fmt_len
+ 1);
219 if (!exclusion_msg
) {
223 at
= strcpy(exclusion_msg
, exclusion_fmt
) + exclusion_fmt_len
;
224 for (i
= 0; i
< exclusion_count
; ++i
) {
227 /* Append comma between exclusion names */
233 ret
= lttng_event_get_exclusion_name(event
, i
, &name
);
235 /* Prints '?' on local error; should never happen */
241 /* Append exclusion name */
242 at
+= sprintf(at
, "%s", name
);
245 /* This also puts a final '\0' at the end of exclusion_msg */
249 return exclusion_msg
;
252 static void print_userspace_probe_location(struct lttng_event
*event
)
254 const struct lttng_userspace_probe_location
*location
;
255 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
256 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
258 location
= lttng_event_get_userspace_probe_location(event
);
260 MSG("Event has no userspace probe location");
264 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
265 if (!lookup_method
) {
266 MSG("Event has no userspace probe location lookup method");
270 MSG("%s%s (type: userspace-probe)%s", indent6
, event
->name
, enabled_string(event
->enabled
));
272 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
274 switch (lttng_userspace_probe_location_get_type(location
)) {
275 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
:
276 MSG("%sType: Unknown", indent8
);
278 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
280 const char *function_name
;
283 MSG("%sType: Function", indent8
);
284 function_name
= lttng_userspace_probe_location_function_get_function_name(location
);
285 binary_path
= realpath(
286 lttng_userspace_probe_location_function_get_binary_path(location
), nullptr);
288 MSG("%sBinary path: %s", indent8
, binary_path
? binary_path
: "NULL");
289 MSG("%sFunction: %s()", indent8
, function_name
? function_name
: "NULL");
290 switch (lookup_type
) {
291 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
292 MSG("%sLookup method: ELF", indent8
);
294 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
295 MSG("%sLookup method: default", indent8
);
298 MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8
);
305 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
307 const char *probe_name
, *provider_name
;
310 MSG("%sType: Tracepoint", indent8
);
311 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(location
);
313 lttng_userspace_probe_location_tracepoint_get_provider_name(location
);
314 binary_path
= realpath(
315 lttng_userspace_probe_location_tracepoint_get_binary_path(location
),
317 MSG("%sBinary path: %s", indent8
, binary_path
? binary_path
: "NULL");
318 MSG("%sTracepoint: %s:%s",
320 provider_name
? provider_name
: "NULL",
321 probe_name
? probe_name
: "NULL");
322 switch (lookup_type
) {
323 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
324 MSG("%sLookup method: SDT", indent8
);
327 MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8
);
335 ERR("Invalid probe type encountered");
340 * Pretty print single event.
342 static void print_events(struct lttng_event
*event
)
345 const char *filter_str
;
346 char *filter_msg
= nullptr;
347 char *exclusion_msg
= nullptr;
349 ret
= lttng_event_get_filter_expression(event
, &filter_str
);
352 filter_msg
= strdup(" [failed to retrieve filter]");
353 } else if (filter_str
) {
354 if (asprintf(&filter_msg
, " [filter: '%s']", filter_str
) == -1) {
355 filter_msg
= nullptr;
359 exclusion_msg
= get_exclusion_names_msg(event
);
360 if (!exclusion_msg
) {
361 exclusion_msg
= strdup(" [failed to retrieve exclusions]");
364 switch (event
->type
) {
365 case LTTNG_EVENT_TRACEPOINT
:
367 if (event
->loglevel
!= -1) {
368 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
371 logleveltype_string(event
->loglevel_type
),
372 mi_lttng_loglevel_string(event
->loglevel
, the_handle
->domain
.type
),
374 enabled_string(event
->enabled
),
375 safe_string(exclusion_msg
),
376 safe_string(filter_msg
));
378 MSG("%s%s (type: tracepoint)%s%s%s",
381 enabled_string(event
->enabled
),
382 safe_string(exclusion_msg
),
383 safe_string(filter_msg
));
387 case LTTNG_EVENT_FUNCTION
:
388 MSG("%s%s (type: function)%s%s",
391 enabled_string(event
->enabled
),
392 safe_string(filter_msg
));
393 if (event
->attr
.probe
.addr
!= 0) {
394 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
396 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
397 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
400 case LTTNG_EVENT_PROBE
:
401 MSG("%s%s (type: probe)%s%s",
404 enabled_string(event
->enabled
),
405 safe_string(filter_msg
));
406 if (event
->attr
.probe
.addr
!= 0) {
407 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
409 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
410 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
413 case LTTNG_EVENT_USERSPACE_PROBE
:
414 print_userspace_probe_location(event
);
416 case LTTNG_EVENT_FUNCTION_ENTRY
:
417 MSG("%s%s (type: function)%s%s",
420 enabled_string(event
->enabled
),
421 safe_string(filter_msg
));
422 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
424 case LTTNG_EVENT_SYSCALL
:
428 (opt_syscall
? "" : " (type:syscall)"),
429 enabled_string(event
->enabled
),
430 bitness_event(event
->flags
),
431 safe_string(filter_msg
));
433 case LTTNG_EVENT_NOOP
:
434 MSG("%s (type: noop)%s%s",
436 enabled_string(event
->enabled
),
437 safe_string(filter_msg
));
439 case LTTNG_EVENT_ALL
:
442 /* We should never have "all" events in list. */
451 static const char *field_type(struct lttng_event_field
*field
)
453 switch (field
->type
) {
454 case LTTNG_EVENT_FIELD_INTEGER
:
456 case LTTNG_EVENT_FIELD_ENUM
:
458 case LTTNG_EVENT_FIELD_FLOAT
:
460 case LTTNG_EVENT_FIELD_STRING
:
462 case LTTNG_EVENT_FIELD_OTHER
:
463 default: /* fall-through */
469 * Pretty print single event fields.
471 static void print_event_field(struct lttng_event_field
*field
)
473 if (!field
->field_name
[0]) {
476 MSG("%sfield: %s (%s)%s",
480 field
->nowrite
? " [no write]" : "");
485 * Jul and ust event listing
488 mi_list_agent_ust_events(struct lttng_event
*events
, int count
, struct lttng_domain
*domain
)
492 char *cmdline
= nullptr;
493 int pid_element_open
= 0;
495 /* Open domains element */
496 ret
= mi_lttng_domains_open(the_writer
);
502 ret
= mi_lttng_domain(the_writer
, domain
, 1);
507 /* Open pids element element */
508 ret
= mi_lttng_pids_open(the_writer
);
513 for (i
= 0; i
< count
; i
++) {
514 if (cur_pid
!= events
[i
].pid
) {
515 if (pid_element_open
) {
516 /* Close the previous events and pid element */
517 ret
= mi_lttng_close_multi_element(the_writer
, 2);
521 pid_element_open
= 0;
524 cur_pid
= events
[i
].pid
;
525 cmdline
= get_cmdline_by_pid(cur_pid
);
531 if (!pid_element_open
) {
532 /* Open and write a pid element */
533 ret
= mi_lttng_pid(the_writer
, cur_pid
, cmdline
, 1);
538 /* Open events element */
539 ret
= mi_lttng_events_open(the_writer
);
544 pid_element_open
= 1;
550 ret
= mi_lttng_event(the_writer
, &events
[i
], 0, the_handle
->domain
.type
);
557 ret
= mi_lttng_writer_close_element(the_writer
);
562 /* Close domain, domains */
563 ret
= mi_lttng_close_multi_element(the_writer
, 2);
571 static int list_agent_events()
573 int i
, size
, ret
= CMD_SUCCESS
;
574 struct lttng_domain domain
;
575 struct lttng_handle
*handle
= nullptr;
576 struct lttng_event
*event_list
= nullptr;
578 char *cmdline
= nullptr;
579 const char *agent_domain_str
;
581 memset(&domain
, 0, sizeof(domain
));
583 domain
.type
= LTTNG_DOMAIN_JUL
;
584 } else if (opt_log4j
) {
585 domain
.type
= LTTNG_DOMAIN_LOG4J
;
586 } else if (opt_python
) {
587 domain
.type
= LTTNG_DOMAIN_PYTHON
;
589 ERR("Invalid agent domain selected.");
594 agent_domain_str
= lttng_domain_type_str(domain
.type
);
596 DBG("Getting %s tracing events", agent_domain_str
);
598 handle
= lttng_create_handle(nullptr, &domain
);
599 if (handle
== nullptr) {
604 size
= lttng_list_tracepoints(handle
, &event_list
);
606 ERR("Unable to list %s events: %s", agent_domain_str
, lttng_strerror(size
));
613 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
620 MSG("%s events (Logger name):\n-------------------------", agent_domain_str
);
626 for (i
= 0; i
< size
; i
++) {
627 if (cur_pid
!= event_list
[i
].pid
) {
628 cur_pid
= event_list
[i
].pid
;
629 cmdline
= get_cmdline_by_pid(cur_pid
);
630 if (cmdline
== nullptr) {
634 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
637 MSG("%s- %s", indent6
, event_list
[i
].name
);
646 lttng_destroy_handle(handle
);
651 * Ask session daemon for all user space tracepoints available.
653 static int list_ust_events()
655 int i
, size
, ret
= CMD_SUCCESS
;
656 struct lttng_domain domain
;
657 struct lttng_handle
*handle
;
658 struct lttng_event
*event_list
= nullptr;
660 char *cmdline
= nullptr;
662 memset(&domain
, 0, sizeof(domain
));
664 DBG("Getting UST tracing events");
666 domain
.type
= LTTNG_DOMAIN_UST
;
668 handle
= lttng_create_handle(nullptr, &domain
);
669 if (handle
== nullptr) {
674 size
= lttng_list_tracepoints(handle
, &event_list
);
676 ERR("Unable to list UST events: %s", lttng_strerror(size
));
683 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
686 MSG("UST events:\n-------------");
692 for (i
= 0; i
< size
; i
++) {
693 if (cur_pid
!= event_list
[i
].pid
) {
694 cur_pid
= event_list
[i
].pid
;
695 cmdline
= get_cmdline_by_pid(cur_pid
);
696 if (cmdline
== nullptr) {
700 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
703 print_events(&event_list
[i
]);
712 lttng_destroy_handle(handle
);
718 * List all ust event with their fields
721 mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
, struct lttng_domain
*domain
)
725 char *cmdline
= nullptr;
726 int pid_element_open
= 0;
727 int event_element_open
= 0;
728 struct lttng_event cur_event
;
730 memset(&cur_event
, 0, sizeof(cur_event
));
732 /* Open domains element */
733 ret
= mi_lttng_domains_open(the_writer
);
739 ret
= mi_lttng_domain(the_writer
, domain
, 1);
744 /* Open pids element */
745 ret
= mi_lttng_pids_open(the_writer
);
750 for (i
= 0; i
< count
; i
++) {
751 if (cur_pid
!= fields
[i
].event
.pid
) {
752 if (pid_element_open
) {
753 if (event_element_open
) {
754 /* Close the previous field element and event. */
755 ret
= mi_lttng_close_multi_element(the_writer
, 2);
759 event_element_open
= 0;
761 /* Close the previous events, pid element */
762 ret
= mi_lttng_close_multi_element(the_writer
, 2);
766 pid_element_open
= 0;
769 cur_pid
= fields
[i
].event
.pid
;
770 cmdline
= get_cmdline_by_pid(cur_pid
);
771 if (!pid_element_open
) {
772 /* Open and write a pid element */
773 ret
= mi_lttng_pid(the_writer
, cur_pid
, cmdline
, 1);
778 /* Open events element */
779 ret
= mi_lttng_events_open(the_writer
);
783 pid_element_open
= 1;
786 /* Wipe current event since we are about to print a new PID. */
787 memset(&cur_event
, 0, sizeof(cur_event
));
790 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
791 if (event_element_open
) {
792 /* Close the previous fields element and the previous event */
793 ret
= mi_lttng_close_multi_element(the_writer
, 2);
797 event_element_open
= 0;
800 memcpy(&cur_event
, &fields
[i
].event
, sizeof(cur_event
));
802 if (!event_element_open
) {
803 /* Open and write the event */
804 ret
= mi_lttng_event(
805 the_writer
, &cur_event
, 1, the_handle
->domain
.type
);
810 /* Open a fields element */
811 ret
= mi_lttng_event_fields_open(the_writer
);
815 event_element_open
= 1;
819 /* Print the event_field */
820 ret
= mi_lttng_event_field(the_writer
, &fields
[i
]);
826 /* Close pids, domain, domains */
827 ret
= mi_lttng_close_multi_element(the_writer
, 3);
836 * Ask session daemon for all user space tracepoint fields available.
838 static int list_ust_event_fields()
840 int i
, size
, ret
= CMD_SUCCESS
;
841 struct lttng_domain domain
;
842 struct lttng_handle
*handle
;
843 struct lttng_event_field
*event_field_list
;
845 char *cmdline
= nullptr;
847 struct lttng_event cur_event
;
849 memset(&domain
, 0, sizeof(domain
));
850 memset(&cur_event
, 0, sizeof(cur_event
));
852 DBG("Getting UST tracing event fields");
854 domain
.type
= LTTNG_DOMAIN_UST
;
856 handle
= lttng_create_handle(nullptr, &domain
);
857 if (handle
== nullptr) {
862 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
864 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
871 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
878 MSG("UST events:\n-------------");
884 for (i
= 0; i
< size
; i
++) {
885 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
886 cur_pid
= event_field_list
[i
].event
.pid
;
887 cmdline
= get_cmdline_by_pid(cur_pid
);
888 if (cmdline
== nullptr) {
892 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
894 /* Wipe current event since we are about to print a new PID. */
895 memset(&cur_event
, 0, sizeof(cur_event
));
897 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
898 print_events(&event_field_list
[i
].event
);
899 memcpy(&cur_event
, &event_field_list
[i
].event
, sizeof(cur_event
));
901 print_event_field(&event_field_list
[i
]);
908 free(event_field_list
);
910 lttng_destroy_handle(handle
);
916 * Print a list of kernel events
918 static int mi_list_kernel_events(struct lttng_event
*events
, int count
, struct lttng_domain
*domain
)
922 /* Open domains element */
923 ret
= mi_lttng_domains_open(the_writer
);
929 ret
= mi_lttng_domain(the_writer
, domain
, 1);
935 ret
= mi_lttng_events_open(the_writer
);
940 for (i
= 0; i
< count
; i
++) {
941 ret
= mi_lttng_event(the_writer
, &events
[i
], 0, the_handle
->domain
.type
);
947 /* close events, domain and domains */
948 ret
= mi_lttng_close_multi_element(the_writer
, 3);
958 * Ask for all trace events in the kernel
960 static int list_kernel_events()
962 int i
, size
, ret
= CMD_SUCCESS
;
963 struct lttng_domain domain
;
964 struct lttng_handle
*handle
;
965 struct lttng_event
*event_list
;
967 memset(&domain
, 0, sizeof(domain
));
969 DBG("Getting kernel tracing events");
971 domain
.type
= LTTNG_DOMAIN_KERNEL
;
973 handle
= lttng_create_handle(nullptr, &domain
);
974 if (handle
== nullptr) {
979 size
= lttng_list_tracepoints(handle
, &event_list
);
981 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
982 lttng_destroy_handle(handle
);
988 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
994 MSG("Kernel events:\n-------------");
996 for (i
= 0; i
< size
; i
++) {
997 print_events(&event_list
[i
]);
1006 lttng_destroy_handle(handle
);
1010 lttng_destroy_handle(handle
);
1016 * Print a list of system calls.
1018 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
1023 ret
= mi_lttng_events_open(the_writer
);
1028 for (i
= 0; i
< count
; i
++) {
1029 ret
= mi_lttng_event(the_writer
, &events
[i
], 0, the_handle
->domain
.type
);
1036 ret
= mi_lttng_writer_close_element(the_writer
);
1046 * Ask for kernel system calls.
1048 static int list_syscalls()
1050 int i
, size
, ret
= CMD_SUCCESS
;
1051 struct lttng_event
*event_list
;
1053 DBG("Getting kernel system call events");
1055 size
= lttng_list_syscalls(&event_list
);
1057 ERR("Unable to list system calls: %s", lttng_strerror(size
));
1064 ret
= mi_list_syscalls(event_list
, size
);
1070 MSG("System calls:\n-------------");
1072 for (i
= 0; i
< size
; i
++) {
1073 print_events(&event_list
[i
]);
1089 * Print a list of agent events
1091 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1095 /* Open events element */
1096 ret
= mi_lttng_events_open(the_writer
);
1101 for (i
= 0; i
< count
; i
++) {
1102 ret
= mi_lttng_event(the_writer
, &events
[i
], 0, the_handle
->domain
.type
);
1108 /* Close events element */
1109 ret
= mi_lttng_writer_close_element(the_writer
);
1116 * List agent events for a specific session using the handle.
1118 * Return CMD_SUCCESS on success else a negative value.
1120 static int list_session_agent_events()
1122 int ret
= CMD_SUCCESS
, count
, i
;
1123 struct lttng_event
*events
= nullptr;
1125 count
= lttng_list_events(the_handle
, "", &events
);
1128 ERR("%s", lttng_strerror(count
));
1134 ret
= mi_list_session_agent_events(events
, count
);
1141 MSG("Events (Logger name):\n---------------------");
1143 MSG("%sNone\n", indent6
);
1147 for (i
= 0; i
< count
; i
++) {
1148 const char *filter_str
;
1149 char *filter_msg
= nullptr;
1150 struct lttng_event
*event
= &events
[i
];
1152 ret
= lttng_event_get_filter_expression(event
, &filter_str
);
1154 filter_msg
= strdup(" [failed to retrieve filter]");
1155 } else if (filter_str
) {
1156 if (asprintf(&filter_msg
, " [filter: '%s']", filter_str
) == -1) {
1157 filter_msg
= nullptr;
1161 if (event
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1162 MSG("%s- %s%s (loglevel%s %s)%s",
1165 enabled_string(event
->enabled
),
1166 logleveltype_string(event
->loglevel_type
),
1167 mi_lttng_loglevel_string(event
->loglevel
,
1168 the_handle
->domain
.type
),
1169 safe_string(filter_msg
));
1174 enabled_string(event
->enabled
),
1175 safe_string(filter_msg
));
1191 * print a list of event
1193 static int mi_list_events(struct lttng_event
*events
, int count
)
1197 /* Open events element */
1198 ret
= mi_lttng_events_open(the_writer
);
1203 for (i
= 0; i
< count
; i
++) {
1204 ret
= mi_lttng_event(the_writer
, &events
[i
], 0, the_handle
->domain
.type
);
1210 /* Close events element */
1211 ret
= mi_lttng_writer_close_element(the_writer
);
1218 * List events of channel of session and domain.
1220 static int list_events(const char *channel_name
)
1222 int ret
= CMD_SUCCESS
, count
, i
;
1223 struct lttng_event
*events
= nullptr;
1225 count
= lttng_list_events(the_handle
, channel_name
, &events
);
1228 ERR("%s", lttng_strerror(count
));
1234 ret
= mi_list_events(events
, count
);
1241 MSG("\n%sRecording event rules:", indent4
);
1243 MSG("%sNone\n", indent6
);
1247 for (i
= 0; i
< count
; i
++) {
1248 print_events(&events
[i
]);
1259 static void print_timer(const char *timer_name
, uint32_t space_count
, int64_t value
)
1263 _MSG("%s%s:", indent6
, timer_name
);
1264 for (i
= 0; i
< space_count
; i
++) {
1269 MSG("%" PRId64
" %s", value
, USEC_UNIT
);
1276 * Pretty print channel
1278 static void print_channel(struct lttng_channel
*channel
)
1281 uint64_t discarded_events
, lost_packets
, monitor_timer_interval
;
1282 int64_t blocking_timeout
;
1284 ret
= lttng_channel_get_discarded_event_count(channel
, &discarded_events
);
1286 ERR("Failed to retrieve discarded event count of channel");
1290 ret
= lttng_channel_get_lost_packet_count(channel
, &lost_packets
);
1292 ERR("Failed to retrieve lost packet count of channel");
1296 ret
= lttng_channel_get_monitor_timer_interval(channel
, &monitor_timer_interval
);
1298 ERR("Failed to retrieve monitor interval of channel");
1302 ret
= lttng_channel_get_blocking_timeout(channel
, &blocking_timeout
);
1304 ERR("Failed to retrieve blocking timeout of channel");
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
);
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
);
1318 if (!channel
->attr
.overwrite
) {
1319 if (blocking_timeout
== -1) {
1320 MSG("%sBlocking timeout: infinite", indent6
);
1322 MSG("%sBlocking timeout: %" PRId64
" %s",
1329 MSG("%sTrace file count: %" PRIu64
" per stream",
1331 channel
->attr
.tracefile_count
== 0 ? 1 : channel
->attr
.tracefile_count
);
1332 if (channel
->attr
.tracefile_size
!= 0) {
1333 MSG("%sTrace file size: %" PRIu64
" bytes", indent6
, channel
->attr
.tracefile_size
);
1335 MSG("%sTrace file size: %s", indent6
, "unlimited");
1337 switch (channel
->attr
.output
) {
1338 case LTTNG_EVENT_SPLICE
:
1339 MSG("%sOutput mode: splice", indent6
);
1341 case LTTNG_EVENT_MMAP
:
1342 MSG("%sOutput mode: mmap", indent6
);
1346 MSG("\n%sStatistics:", indent4
);
1347 if (the_listed_session
.snapshot_mode
) {
1349 * The lost packet count is omitted for sessions in snapshot
1350 * mode as it is misleading: it would indicate the number of
1351 * packets that the consumer could not extract during the
1352 * course of recording the snapshot. It does not have the
1353 * same meaning as the "regular" lost packet count that
1354 * would result from the consumer not keeping up with
1355 * event production in an overwrite-mode channel.
1357 * A more interesting statistic would be the number of
1358 * packets lost between the first and last extracted
1359 * packets of a given snapshot (which prevents most analyses).
1361 MSG("%sNone", indent6
);
1362 goto skip_stats_printing
;
1365 if (!channel
->attr
.overwrite
) {
1366 MSG("%sDiscarded events: %" PRIu64
, indent6
, discarded_events
);
1368 MSG("%sLost packets: %" PRIu64
, indent6
, lost_packets
);
1370 skip_stats_printing
:
1376 * Print a list of channel
1379 static int mi_list_channels(struct lttng_channel
*channels
, int count
, const char *channel_name
)
1382 unsigned int chan_found
= 0;
1384 /* Open channels element */
1385 ret
= mi_lttng_channels_open(the_writer
);
1390 for (i
= 0; i
< count
; i
++) {
1391 if (channel_name
!= nullptr) {
1392 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1399 /* Write channel element and leave it open */
1400 ret
= mi_lttng_channel(the_writer
, &channels
[i
], 1);
1405 /* Listing events per channel */
1406 ret
= list_events(channels
[i
].name
);
1411 /* Closing the channel element we opened earlier */
1412 ret
= mi_lttng_writer_close_element(the_writer
);
1422 /* Close channels element */
1423 ret
= mi_lttng_writer_close_element(the_writer
);
1433 * List channel(s) of session and domain.
1435 * If channel_name is NULL, all channels are listed.
1437 static int list_channels(const char *channel_name
)
1439 int count
, i
, ret
= CMD_SUCCESS
;
1440 unsigned int chan_found
= 0;
1441 struct lttng_channel
*channels
= nullptr;
1443 DBG("Listing channel(s) (%s)", channel_name
?: "<all>");
1445 count
= lttng_list_channels(the_handle
, &channels
);
1448 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1450 /* When printing mi this is not an error
1451 * but an empty channels element */
1455 goto error_channels
;
1459 /* We had a real error */
1461 ERR("%s", lttng_strerror(count
));
1462 goto error_channels
;
1469 ret
= mi_list_channels(channels
, count
, channel_name
);
1477 MSG("Channels:\n-------------");
1480 for (i
= 0; i
< count
; i
++) {
1481 if (channel_name
!= nullptr) {
1482 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1488 print_channel(&channels
[i
]);
1490 /* Listing events per channel */
1491 ret
= list_events(channels
[i
].name
);
1501 if (!chan_found
&& channel_name
!= nullptr) {
1503 ERR("Channel %s not found", channel_name
);
1514 static const char *get_capitalized_process_attr_str(enum lttng_process_attr process_attr
)
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
:
1523 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1524 return "Virtual user ID";
1525 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1527 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1528 return "Virtual group ID";
1535 static int handle_process_attr_status(enum lttng_process_attr process_attr
,
1536 enum lttng_process_attr_tracker_handle_status status
)
1538 int ret
= CMD_SUCCESS
;
1541 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
:
1542 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
:
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
));
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
);
1557 ERR("Unknown error occurred while fetching the inclusion set of the %s tracker",
1558 lttng_process_attr_to_string(process_attr
));
1566 static int mi_output_empty_tracker(enum lttng_process_attr process_attr
)
1570 ret
= mi_lttng_process_attribute_tracker_open(the_writer
, process_attr
);
1575 ret
= mi_lttng_close_multi_element(the_writer
, 2);
1580 static inline bool is_value_type_name(enum lttng_process_attr_value_type value_type
)
1582 return value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
||
1583 value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
;
1587 * List a process attribute tracker for a session and domain tuple.
1589 static int list_process_attr_tracker(enum lttng_process_attr process_attr
)
1592 unsigned int count
, i
;
1593 enum lttng_tracking_policy policy
;
1594 enum lttng_error_code ret_code
;
1595 enum lttng_process_attr_tracker_handle_status handle_status
;
1596 enum lttng_process_attr_values_status values_status
;
1597 const struct lttng_process_attr_values
*values
;
1598 struct lttng_process_attr_tracker_handle
*tracker_handle
= nullptr;
1600 ret_code
= lttng_session_get_tracker_handle(
1601 the_handle
->session_name
, the_handle
->domain
.type
, process_attr
, &tracker_handle
);
1602 if (ret_code
!= LTTNG_OK
) {
1603 ERR("Failed to get process attribute tracker handle: %s", lttng_strerror(ret_code
));
1609 lttng_process_attr_tracker_handle_get_inclusion_set(tracker_handle
, &values
);
1610 ret
= handle_process_attr_status(process_attr
, handle_status
);
1611 if (ret
!= CMD_SUCCESS
) {
1616 lttng_process_attr_tracker_handle_get_tracking_policy(tracker_handle
, &policy
);
1617 ret
= handle_process_attr_status(process_attr
, handle_status
);
1618 if (ret
!= CMD_SUCCESS
) {
1623 char *process_attr_name
;
1624 const int print_ret
= asprintf(
1625 &process_attr_name
, "%ss:", get_capitalized_process_attr_str(process_attr
));
1627 if (print_ret
== -1) {
1631 _MSG(" %-22s", process_attr_name
);
1632 free(process_attr_name
);
1635 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1637 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1639 mi_output_empty_tracker(process_attr
);
1644 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1649 ERR("Unknown tracking policy encoutered while listing the %s process attribute tracker of session `%s`",
1650 lttng_process_attr_to_string(process_attr
),
1651 the_handle
->session_name
);
1656 values_status
= lttng_process_attr_values_get_count(values
, &count
);
1657 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
1658 ERR("Failed to get the count of values in the inclusion set of the %s process attribute tracker of session `%s`",
1659 lttng_process_attr_to_string(process_attr
),
1660 the_handle
->session_name
);
1666 /* Functionally equivalent to the 'exclude all' policy. */
1668 mi_output_empty_tracker(process_attr
);
1675 /* Mi tracker_id element */
1677 /* Open tracker_id and targets elements */
1678 ret
= mi_lttng_process_attribute_tracker_open(the_writer
, process_attr
);
1684 for (i
= 0; i
< count
; i
++) {
1685 const enum lttng_process_attr_value_type value_type
=
1686 lttng_process_attr_values_get_type_at_index(values
, i
);
1687 int64_t integral_value
= INT64_MAX
;
1688 const char *name
= "error";
1693 switch (value_type
) {
1694 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
1698 values_status
= lttng_process_attr_values_get_pid_at_index(values
, i
, &pid
);
1699 integral_value
= (int64_t) pid
;
1702 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
1706 values_status
= lttng_process_attr_values_get_uid_at_index(values
, i
, &uid
);
1707 integral_value
= (int64_t) uid
;
1710 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
1714 values_status
= lttng_process_attr_values_get_gid_at_index(values
, i
, &gid
);
1715 integral_value
= (int64_t) gid
;
1718 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
1720 lttng_process_attr_values_get_user_name_at_index(values
, i
, &name
);
1722 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
1724 lttng_process_attr_values_get_group_name_at_index(values
, i
, &name
);
1731 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
1733 * Not possible given the current liblttng-ctl
1736 ERR("Unknown error occurred while fetching process attribute value in inclusion list");
1741 if (is_value_type_name(value_type
)) {
1744 _MSG("%" PRIi64
, integral_value
);
1749 ret
= is_value_type_name(value_type
) ?
1750 mi_lttng_string_process_attribute_value(
1751 the_writer
, process_attr
, name
, false) :
1752 mi_lttng_integral_process_attribute_value(
1753 the_writer
, process_attr
, integral_value
, false);
1761 /* Mi close tracker_id and targets */
1763 ret
= mi_lttng_close_multi_element(the_writer
, 2);
1769 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
1774 * List all trackers of a domain
1776 static int list_trackers(const struct lttng_domain
*domain
)
1780 MSG("Tracked process attributes");
1781 /* Trackers listing */
1783 ret
= mi_lttng_trackers_open(the_writer
);
1789 switch (domain
->type
) {
1790 case LTTNG_DOMAIN_KERNEL
:
1792 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_PROCESS_ID
);
1797 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
1802 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_USER_ID
);
1807 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
1812 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_GROUP_ID
);
1817 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
1822 case LTTNG_DOMAIN_UST
:
1824 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
);
1829 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
);
1834 ret
= list_process_attr_tracker(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
);
1844 /* Close trackers element */
1845 ret
= mi_lttng_writer_close_element(the_writer
);
1855 static enum cmd_error_code
1856 print_periodic_rotation_schedule(const struct lttng_rotation_schedule
*schedule
)
1858 enum cmd_error_code ret
;
1859 enum lttng_rotation_status status
;
1862 status
= lttng_rotation_schedule_periodic_get_period(schedule
, &value
);
1863 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1864 ERR("Failed to retrieve period parameter from periodic rotation schedule.");
1869 MSG(" timer period: %" PRIu64
" %s", value
, USEC_UNIT
);
1875 static enum cmd_error_code
1876 print_size_threshold_rotation_schedule(const struct lttng_rotation_schedule
*schedule
)
1878 enum cmd_error_code ret
;
1879 enum lttng_rotation_status status
;
1882 status
= lttng_rotation_schedule_size_threshold_get_threshold(schedule
, &value
);
1883 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1884 ERR("Failed to retrieve size parameter from size-based rotation schedule.");
1889 MSG(" size threshold: %" PRIu64
" bytes", value
);
1895 static enum cmd_error_code
print_rotation_schedule(const struct lttng_rotation_schedule
*schedule
)
1897 enum cmd_error_code ret
;
1899 switch (lttng_rotation_schedule_get_type(schedule
)) {
1900 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
1901 ret
= print_size_threshold_rotation_schedule(schedule
);
1903 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
1904 ret
= print_periodic_rotation_schedule(schedule
);
1913 * List the automatic rotation settings.
1915 static enum cmd_error_code
list_rotate_settings(const char *session_name
)
1918 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
1919 unsigned int count
, i
;
1920 struct lttng_rotation_schedules
*schedules
= nullptr;
1921 enum lttng_rotation_status status
;
1923 ret
= lttng_session_list_rotation_schedules(session_name
, &schedules
);
1924 if (ret
!= LTTNG_OK
) {
1925 ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret
));
1926 cmd_ret
= CMD_ERROR
;
1930 status
= lttng_rotation_schedules_get_count(schedules
, &count
);
1931 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1932 ERR("Failed to retrieve the number of session rotation schedules.");
1933 cmd_ret
= CMD_ERROR
;
1938 cmd_ret
= CMD_SUCCESS
;
1942 MSG("Automatic rotation schedules:");
1944 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_rotation_schedules
);
1946 cmd_ret
= CMD_ERROR
;
1951 for (i
= 0; i
< count
; i
++) {
1952 enum cmd_error_code tmp_ret
= CMD_SUCCESS
;
1953 const struct lttng_rotation_schedule
*schedule
;
1955 schedule
= lttng_rotation_schedules_get_at_index(schedules
, i
);
1957 ERR("Failed to retrieve session rotation schedule.");
1958 cmd_ret
= CMD_ERROR
;
1963 ret
= mi_lttng_rotation_schedule(the_writer
, schedule
);
1965 tmp_ret
= CMD_ERROR
;
1968 tmp_ret
= print_rotation_schedule(schedule
);
1972 * Report an error if the serialization of any of the
1973 * descriptors failed.
1975 cmd_ret
= cmd_ret
? cmd_ret
: tmp_ret
;
1980 /* Close the rotation_schedules element. */
1981 ret
= mi_lttng_writer_close_element(the_writer
);
1983 cmd_ret
= CMD_ERROR
;
1988 lttng_rotation_schedules_destroy(schedules
);
1994 * Find the session with session_name as name
1995 * and print his informations.
1997 static int mi_list_session(const char *session_name
, struct lttng_session
*sessions
, int count
)
2000 unsigned int session_found
= 0;
2002 if (session_name
== nullptr) {
2003 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
2007 for (i
= 0; i
< count
; i
++) {
2008 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
2009 /* We need to leave it open to append other informations
2010 * like domain, channel, events etc.*/
2012 ret
= mi_lttng_session(the_writer
, &sessions
[i
], 1);
2020 if (!session_found
) {
2021 ERR("Session '%s' not found", session_name
);
2022 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
2032 * List all availables session
2034 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
2038 /* Opening sessions element */
2039 ret
= mi_lttng_sessions_open(the_writer
);
2044 /* Listing sessions */
2045 for (i
= 0; i
< count
; i
++) {
2046 ret
= mi_lttng_session(the_writer
, &sessions
[i
], 0);
2052 /* Closing sessions element */
2053 ret
= mi_lttng_writer_close_element(the_writer
);
2063 * List available tracing session. List only basic information.
2065 * If session_name is NULL, all sessions are listed.
2067 static int list_sessions(const char *session_name
)
2069 int ret
= CMD_SUCCESS
;
2071 unsigned int session_found
= 0;
2072 struct lttng_session
*sessions
= nullptr;
2074 count
= lttng_list_sessions(&sessions
);
2075 DBG("Session count %d", count
);
2078 ERR("%s", lttng_strerror(count
));
2084 if (session_name
== nullptr) {
2085 /* List all sessions */
2086 ret
= mi_list_sessions(sessions
, count
);
2088 /* Note : this return an open session element */
2089 ret
= mi_list_session(session_name
, sessions
, count
);
2098 MSG("Currently no available recording session");
2102 if (session_name
== nullptr) {
2103 MSG("Available recording sessions:");
2106 for (i
= 0; i
< count
; i
++) {
2107 if (session_name
!= nullptr) {
2108 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
2110 MSG("Recording session %s: [%s%s]",
2112 active_string(sessions
[i
].enabled
),
2113 snapshot_string(sessions
[i
].snapshot_mode
));
2114 if (*sessions
[i
].path
) {
2115 MSG("%sTrace output: %s\n",
2119 memcpy(&the_listed_session
,
2121 sizeof(the_listed_session
));
2125 MSG(" %d) %s [%s%s]",
2128 active_string(sessions
[i
].enabled
),
2129 snapshot_string(sessions
[i
].snapshot_mode
));
2130 if (*sessions
[i
].path
) {
2131 MSG("%sTrace output: %s", indent4
, sessions
[i
].path
);
2133 if (sessions
[i
].live_timer_interval
!= 0) {
2134 MSG("%sLive timer interval: %u %s",
2136 sessions
[i
].live_timer_interval
,
2143 if (!session_found
&& session_name
!= nullptr) {
2144 ERR("Session '%s' not found", session_name
);
2149 if (session_name
== nullptr) {
2150 MSG("\nUse lttng list <session_name> for more details");
2161 * list available domain(s) for a session.
2163 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
2166 /* Open domains element */
2167 ret
= mi_lttng_domains_open(the_writer
);
2172 for (i
= 0; i
< count
; i
++) {
2173 ret
= mi_lttng_domain(the_writer
, &domains
[i
], 0);
2179 /* Closing domains element */
2180 ret
= mi_lttng_writer_close_element(the_writer
);
2189 * List available domain(s) for a session.
2191 static int list_domains(const char *session_name
)
2193 int i
, count
, ret
= CMD_SUCCESS
;
2194 struct lttng_domain
*domains
= nullptr;
2196 count
= lttng_list_domains(session_name
, &domains
);
2199 ERR("%s", lttng_strerror(count
));
2205 ret
= mi_list_domains(domains
, count
);
2212 MSG("Domains:\n-------------");
2218 for (i
= 0; i
< count
; i
++) {
2219 switch (domains
[i
].type
) {
2220 case LTTNG_DOMAIN_KERNEL
:
2223 case LTTNG_DOMAIN_UST
:
2224 MSG(" - UST global");
2226 case LTTNG_DOMAIN_JUL
:
2227 MSG(" - JUL (Java Util Logging)");
2229 case LTTNG_DOMAIN_LOG4J
:
2230 MSG(" - LOG4j (Logging for Java)");
2232 case LTTNG_DOMAIN_PYTHON
:
2233 MSG(" - Python (logging)");
2249 * The 'list <options>' first level command
2251 int cmd_list(int argc
, const char **argv
)
2253 int opt
, ret
= CMD_SUCCESS
;
2254 const char *arg_session_name
, *leftover
= nullptr;
2255 static poptContext pc
;
2256 struct lttng_domain domain
;
2257 struct lttng_domain
*domains
= nullptr;
2259 memset(&domain
, 0, sizeof(domain
));
2266 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
2267 poptReadDefaultConfig(pc
, 0);
2269 while ((opt
= poptGetNextOpt(pc
)) != -1) {
2277 case OPT_LIST_OPTIONS
:
2278 list_cmd_options(stdout
, long_options
);
2281 ret
= CMD_UNDEFINED
;
2288 the_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
2294 /* Open command element */
2295 ret
= mi_lttng_writer_command_open(the_writer
, mi_lttng_element_command_list
);
2301 /* Open output element */
2302 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_command_output
);
2309 /* Get session name (trailing argument) */
2310 arg_session_name
= poptGetArg(pc
);
2311 DBG2("Session name: %s", arg_session_name
);
2313 leftover
= poptGetArg(pc
);
2315 ERR("Unknown argument: %s", leftover
);
2321 domain
.type
= LTTNG_DOMAIN_KERNEL
;
2322 } else if (opt_userspace
) {
2323 DBG2("Listing userspace global domain");
2324 domain
.type
= LTTNG_DOMAIN_UST
;
2325 } else if (opt_jul
) {
2326 DBG2("Listing JUL domain");
2327 domain
.type
= LTTNG_DOMAIN_JUL
;
2328 } else if (opt_log4j
) {
2329 domain
.type
= LTTNG_DOMAIN_LOG4J
;
2330 } else if (opt_python
) {
2331 domain
.type
= LTTNG_DOMAIN_PYTHON
;
2334 if (!opt_kernel
&& opt_syscall
) {
2335 WARN("--syscall will only work with the Kernel domain (-k)");
2340 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
2341 the_handle
= lttng_create_handle(arg_session_name
, &domain
);
2342 if (the_handle
== nullptr) {
2348 if (arg_session_name
== nullptr) {
2349 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
&& !opt_python
) {
2350 ret
= list_sessions(nullptr);
2357 ret
= list_syscalls();
2362 ret
= list_kernel_events();
2368 if (opt_userspace
) {
2370 ret
= list_ust_event_fields();
2372 ret
= list_ust_events();
2378 if (opt_jul
|| opt_log4j
|| opt_python
) {
2379 ret
= list_agent_events();
2385 /* List session attributes */
2387 /* Open element sessions
2388 * Present for xml consistency */
2389 ret
= mi_lttng_sessions_open(the_writer
);
2394 /* MI: the ouptut of list_sessions is an unclosed session element */
2395 ret
= list_sessions(arg_session_name
);
2400 ret
= list_rotate_settings(arg_session_name
);
2405 /* Domain listing */
2407 ret
= list_domains(arg_session_name
);
2411 /* Channel listing */
2412 if (opt_kernel
|| opt_userspace
) {
2414 /* Add of domains and domain element for xml
2415 * consistency and validation
2417 ret
= mi_lttng_domains_open(the_writer
);
2422 /* Open domain and leave it open for
2423 * nested channels printing */
2424 ret
= mi_lttng_domain(the_writer
, &domain
, 1);
2431 ret
= list_trackers(&domain
);
2437 ret
= list_channels(opt_channel
);
2443 /* Close domain and domain element */
2444 ret
= mi_lttng_close_multi_element(the_writer
, 2);
2453 /* We want all domain(s) */
2454 nb_domain
= lttng_list_domains(arg_session_name
, &domains
);
2455 if (nb_domain
< 0) {
2457 ERR("%s", lttng_strerror(nb_domain
));
2462 ret
= mi_lttng_domains_open(the_writer
);
2469 for (i
= 0; i
< nb_domain
; i
++) {
2470 switch (domains
[i
].type
) {
2471 case LTTNG_DOMAIN_KERNEL
:
2472 MSG("=== Domain: Linux kernel ===\n");
2474 case LTTNG_DOMAIN_UST
:
2475 MSG("=== Domain: User space ===\n");
2476 MSG("Buffering scheme: %s\n",
2477 domains
[i
].buf_type
== LTTNG_BUFFER_PER_PID
?
2481 case LTTNG_DOMAIN_JUL
:
2482 MSG("=== Domain: java.util.logging (JUL) ===\n");
2484 case LTTNG_DOMAIN_LOG4J
:
2485 MSG("=== Domain: log4j ===\n");
2487 case LTTNG_DOMAIN_PYTHON
:
2488 MSG("=== Domain: Python logging ===\n");
2491 MSG("=== Domain: Unimplemented ===\n");
2496 ret
= mi_lttng_domain(the_writer
, &domains
[i
], 1);
2503 /* Clean handle before creating a new one */
2505 lttng_destroy_handle(the_handle
);
2508 the_handle
= lttng_create_handle(arg_session_name
, &domains
[i
]);
2509 if (the_handle
== nullptr) {
2514 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
2515 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
2516 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
2517 ret
= list_session_agent_events();
2525 switch (domains
[i
].type
) {
2526 case LTTNG_DOMAIN_KERNEL
:
2527 case LTTNG_DOMAIN_UST
:
2528 ret
= list_trackers(&domains
[i
]);
2537 ret
= list_channels(opt_channel
);
2544 /* Close domain element */
2545 ret
= mi_lttng_writer_close_element(the_writer
);
2553 /* Close the domains, session and sessions element */
2554 ret
= mi_lttng_close_multi_element(the_writer
, 3);
2565 /* Close output element */
2566 ret
= mi_lttng_writer_close_element(the_writer
);
2572 /* Command element close */
2573 ret
= mi_lttng_writer_command_close(the_writer
);
2581 if (the_writer
&& mi_lttng_writer_destroy(the_writer
)) {
2582 /* Preserve original error code */
2583 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2588 lttng_destroy_handle(the_handle
);
2591 poptFreeContext(pc
);