2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/mi-lttng.h>
27 #include <lttng/constant.h>
29 #include "../command.h"
31 static int opt_userspace
;
32 static int opt_kernel
;
35 static int opt_python
;
36 static char *opt_channel
;
37 static int opt_domain
;
38 static int opt_fields
;
39 static int opt_syscall
;
41 const char *indent4
= " ";
42 const char *indent6
= " ";
43 const char *indent8
= " ";
45 #ifdef LTTNG_EMBED_HELP
46 static const char help_msg
[] =
47 #include <lttng-list.1.h>
57 static struct lttng_handle
*handle
;
58 static struct mi_writer
*writer
;
60 /* Only set when listing a single session. */
61 static struct lttng_session listed_session
;
63 static struct poptOption long_options
[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
65 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
66 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
67 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
68 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
69 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
70 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
71 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
72 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
73 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
74 {"syscall", 'S', POPT_ARG_VAL
, &opt_syscall
, 1, 0, 0},
75 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
80 * Get command line from /proc for a specific pid.
82 * On success, return an allocated string pointer to the proc cmdline.
83 * On error, return NULL.
85 static char *get_cmdline_by_pid(pid_t pid
)
90 /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */
91 char path
[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR
) - 1];
93 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
94 fp
= fopen(path
, "r");
99 /* Caller must free() *cmdline */
100 cmdline
= zmalloc(PATH_MAX
);
102 PERROR("malloc cmdline");
105 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
107 PERROR("fread proc list");
118 const char *active_string(int value
)
121 case 0: return "inactive";
122 case 1: return "active";
124 default: return NULL
;
128 static const char *snapshot_string(int value
)
139 const char *enabled_string(int value
)
142 case 0: return " [disabled]";
143 case 1: return " [enabled]";
145 default: return NULL
;
150 const char *safe_string(const char *str
)
152 return str
? str
: "";
155 static const char *logleveltype_string(enum lttng_loglevel_type value
)
158 case LTTNG_EVENT_LOGLEVEL_ALL
:
160 case LTTNG_EVENT_LOGLEVEL_RANGE
:
162 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
165 return " <<TYPE UNKN>>";
169 static const char *bitness_event(enum lttng_event_flag flags
)
171 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_32
) {
172 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
173 return " [32/64-bit]";
177 } else if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
185 * Get exclusion names message for a single event.
187 * Returned pointer must be freed by caller. Returns NULL on error.
189 static char *get_exclusion_names_msg(struct lttng_event
*event
)
193 char *exclusion_msg
= NULL
;
196 const char * const exclusion_fmt
= " [exclusions: ";
197 const size_t exclusion_fmt_len
= strlen(exclusion_fmt
);
199 exclusion_count
= lttng_event_get_exclusion_name_count(event
);
200 if (exclusion_count
< 0) {
202 } else if (exclusion_count
== 0) {
204 * No exclusions: return copy of empty string so that
205 * it can be freed by caller.
207 exclusion_msg
= strdup("");
212 * exclusion_msg's size is bounded by the exclusion_fmt string,
213 * a comma per entry, the entry count (fixed-size), a closing
214 * bracket, and a trailing \0.
216 exclusion_msg
= malloc(exclusion_count
+
217 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
;
253 * Pretty print single event.
255 static void print_events(struct lttng_event
*event
)
258 const char *filter_str
;
259 char *filter_msg
= NULL
;
260 char *exclusion_msg
= NULL
;
262 ret
= lttng_event_get_filter_expression(event
, &filter_str
);
265 filter_msg
= strdup(" [failed to retrieve filter]");
266 } else if (filter_str
) {
267 const char * const filter_fmt
= " [filter: '%s']";
269 filter_msg
= malloc(strlen(filter_str
) +
270 strlen(filter_fmt
) + 1);
272 sprintf(filter_msg
, filter_fmt
,
277 exclusion_msg
= get_exclusion_names_msg(event
);
278 if (!exclusion_msg
) {
279 exclusion_msg
= strdup(" [failed to retrieve exclusions]");
282 switch (event
->type
) {
283 case LTTNG_EVENT_TRACEPOINT
:
285 if (event
->loglevel
!= -1) {
286 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
289 logleveltype_string(event
->loglevel_type
),
290 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
292 enabled_string(event
->enabled
),
293 safe_string(exclusion_msg
),
294 safe_string(filter_msg
));
296 MSG("%s%s (type: tracepoint)%s%s%s",
299 enabled_string(event
->enabled
),
300 safe_string(exclusion_msg
),
301 safe_string(filter_msg
));
305 case LTTNG_EVENT_FUNCTION
:
306 MSG("%s%s (type: function)%s%s", indent6
,
307 event
->name
, enabled_string(event
->enabled
),
308 safe_string(filter_msg
));
309 if (event
->attr
.probe
.addr
!= 0) {
310 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
312 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
313 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
316 case LTTNG_EVENT_PROBE
:
317 MSG("%s%s (type: probe)%s%s", indent6
,
318 event
->name
, enabled_string(event
->enabled
),
319 safe_string(filter_msg
));
320 if (event
->attr
.probe
.addr
!= 0) {
321 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
323 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
324 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
327 case LTTNG_EVENT_FUNCTION_ENTRY
:
328 MSG("%s%s (type: function)%s%s", indent6
,
329 event
->name
, enabled_string(event
->enabled
),
330 safe_string(filter_msg
));
331 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
333 case LTTNG_EVENT_SYSCALL
:
334 MSG("%s%s%s%s%s%s", indent6
, event
->name
,
335 (opt_syscall
? "" : " (type:syscall)"),
336 enabled_string(event
->enabled
),
337 bitness_event(event
->flags
),
338 safe_string(filter_msg
));
340 case LTTNG_EVENT_NOOP
:
341 MSG("%s (type: noop)%s%s", indent6
,
342 enabled_string(event
->enabled
),
343 safe_string(filter_msg
));
345 case LTTNG_EVENT_ALL
:
346 /* We should never have "all" events in list. */
355 static const char *field_type(struct lttng_event_field
*field
)
357 switch(field
->type
) {
358 case LTTNG_EVENT_FIELD_INTEGER
:
360 case LTTNG_EVENT_FIELD_ENUM
:
362 case LTTNG_EVENT_FIELD_FLOAT
:
364 case LTTNG_EVENT_FIELD_STRING
:
366 case LTTNG_EVENT_FIELD_OTHER
:
367 default: /* fall-through */
373 * Pretty print single event fields.
375 static void print_event_field(struct lttng_event_field
*field
)
377 if (!field
->field_name
[0]) {
380 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
381 field_type(field
), field
->nowrite
? " [no write]" : "");
386 * Jul and ust event listing
388 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
389 struct lttng_domain
*domain
)
393 char *cmdline
= NULL
;
394 int pid_element_open
= 0;
396 /* Open domains element */
397 ret
= mi_lttng_domains_open(writer
);
403 ret
= mi_lttng_domain(writer
, domain
, 1);
408 /* Open pids element element */
409 ret
= mi_lttng_pids_open(writer
);
414 for (i
= 0; i
< count
; i
++) {
415 if (cur_pid
!= events
[i
].pid
) {
416 if (pid_element_open
) {
417 /* Close the previous events and pid element */
418 ret
= mi_lttng_close_multi_element(writer
, 2);
422 pid_element_open
= 0;
425 cur_pid
= events
[i
].pid
;
426 cmdline
= get_cmdline_by_pid(cur_pid
);
432 if (!pid_element_open
) {
433 /* Open and write a pid element */
434 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
439 /* Open events element */
440 ret
= mi_lttng_events_open(writer
);
445 pid_element_open
= 1;
451 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
458 ret
= mi_lttng_writer_close_element(writer
);
463 /* Close domain, domains */
464 ret
= mi_lttng_close_multi_element(writer
, 2);
472 static int list_agent_events(void)
474 int i
, size
, ret
= CMD_SUCCESS
;
475 struct lttng_domain domain
;
476 struct lttng_handle
*handle
= NULL
;
477 struct lttng_event
*event_list
= NULL
;
479 char *cmdline
= NULL
;
480 const char *agent_domain_str
;
482 memset(&domain
, 0, sizeof(domain
));
484 domain
.type
= LTTNG_DOMAIN_JUL
;
485 } else if (opt_log4j
) {
486 domain
.type
= LTTNG_DOMAIN_LOG4J
;
487 } else if (opt_python
) {
488 domain
.type
= LTTNG_DOMAIN_PYTHON
;
490 ERR("Invalid agent domain selected.");
495 agent_domain_str
= get_domain_str(domain
.type
);
497 DBG("Getting %s tracing events", agent_domain_str
);
499 handle
= lttng_create_handle(NULL
, &domain
);
500 if (handle
== NULL
) {
505 size
= lttng_list_tracepoints(handle
, &event_list
);
507 ERR("Unable to list %s events: %s", agent_domain_str
,
508 lttng_strerror(size
));
515 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
522 MSG("%s events (Logger name):\n-------------------------",
529 for (i
= 0; i
< size
; i
++) {
530 if (cur_pid
!= event_list
[i
].pid
) {
531 cur_pid
= event_list
[i
].pid
;
532 cmdline
= get_cmdline_by_pid(cur_pid
);
533 if (cmdline
== NULL
) {
537 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
540 MSG("%s- %s", indent6
, event_list
[i
].name
);
549 lttng_destroy_handle(handle
);
554 * Ask session daemon for all user space tracepoints available.
556 static int list_ust_events(void)
558 int i
, size
, ret
= CMD_SUCCESS
;
559 struct lttng_domain domain
;
560 struct lttng_handle
*handle
;
561 struct lttng_event
*event_list
= NULL
;
563 char *cmdline
= NULL
;
565 memset(&domain
, 0, sizeof(domain
));
567 DBG("Getting UST tracing events");
569 domain
.type
= LTTNG_DOMAIN_UST
;
571 handle
= lttng_create_handle(NULL
, &domain
);
572 if (handle
== NULL
) {
577 size
= lttng_list_tracepoints(handle
, &event_list
);
579 ERR("Unable to list UST events: %s", lttng_strerror(size
));
586 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
589 MSG("UST events:\n-------------");
595 for (i
= 0; i
< size
; i
++) {
596 if (cur_pid
!= event_list
[i
].pid
) {
597 cur_pid
= event_list
[i
].pid
;
598 cmdline
= get_cmdline_by_pid(cur_pid
);
599 if (cmdline
== NULL
) {
603 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
606 print_events(&event_list
[i
]);
615 lttng_destroy_handle(handle
);
621 * List all ust event with their fields
623 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
624 struct lttng_domain
*domain
)
628 char *cmdline
= NULL
;
629 int pid_element_open
= 0;
630 int event_element_open
= 0;
631 struct lttng_event cur_event
;
633 memset(&cur_event
, 0, sizeof(cur_event
));
635 /* Open domains element */
636 ret
= mi_lttng_domains_open(writer
);
642 ret
= mi_lttng_domain(writer
, domain
, 1);
647 /* Open pids element */
648 ret
= mi_lttng_pids_open(writer
);
653 for (i
= 0; i
< count
; i
++) {
654 if (cur_pid
!= fields
[i
].event
.pid
) {
655 if (pid_element_open
) {
656 if (event_element_open
) {
657 /* Close the previous field element and event. */
658 ret
= mi_lttng_close_multi_element(writer
, 2);
662 event_element_open
= 0;
664 /* Close the previous events, pid element */
665 ret
= mi_lttng_close_multi_element(writer
, 2);
669 pid_element_open
= 0;
672 cur_pid
= fields
[i
].event
.pid
;
673 cmdline
= get_cmdline_by_pid(cur_pid
);
674 if (!pid_element_open
) {
675 /* Open and write a pid element */
676 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
681 /* Open events element */
682 ret
= mi_lttng_events_open(writer
);
686 pid_element_open
= 1;
689 /* Wipe current event since we are about to print a new PID. */
690 memset(&cur_event
, 0, sizeof(cur_event
));
693 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
694 if (event_element_open
) {
695 /* Close the previous fields element and the previous event */
696 ret
= mi_lttng_close_multi_element(writer
, 2);
700 event_element_open
= 0;
703 memcpy(&cur_event
, &fields
[i
].event
,
706 if (!event_element_open
) {
707 /* Open and write the event */
708 ret
= mi_lttng_event(writer
, &cur_event
, 1,
709 handle
->domain
.type
);
714 /* Open a fields element */
715 ret
= mi_lttng_event_fields_open(writer
);
719 event_element_open
= 1;
723 /* Print the event_field */
724 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
730 /* Close pid, domain, domains */
731 ret
= mi_lttng_close_multi_element(writer
, 3);
740 * Ask session daemon for all user space tracepoint fields available.
742 static int list_ust_event_fields(void)
744 int i
, size
, ret
= CMD_SUCCESS
;
745 struct lttng_domain domain
;
746 struct lttng_handle
*handle
;
747 struct lttng_event_field
*event_field_list
;
749 char *cmdline
= NULL
;
751 struct lttng_event cur_event
;
753 memset(&domain
, 0, sizeof(domain
));
754 memset(&cur_event
, 0, sizeof(cur_event
));
756 DBG("Getting UST tracing event fields");
758 domain
.type
= LTTNG_DOMAIN_UST
;
760 handle
= lttng_create_handle(NULL
, &domain
);
761 if (handle
== NULL
) {
766 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
768 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
775 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
782 MSG("UST events:\n-------------");
788 for (i
= 0; i
< size
; i
++) {
789 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
790 cur_pid
= event_field_list
[i
].event
.pid
;
791 cmdline
= get_cmdline_by_pid(cur_pid
);
792 if (cmdline
== NULL
) {
796 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
798 /* Wipe current event since we are about to print a new PID. */
799 memset(&cur_event
, 0, sizeof(cur_event
));
801 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
802 print_events(&event_field_list
[i
].event
);
803 memcpy(&cur_event
, &event_field_list
[i
].event
,
806 print_event_field(&event_field_list
[i
]);
813 free(event_field_list
);
815 lttng_destroy_handle(handle
);
821 * Print a list of kernel events
823 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
824 struct lttng_domain
*domain
)
828 /* Open domains element */
829 ret
= mi_lttng_domains_open(writer
);
835 ret
= mi_lttng_domain(writer
, domain
, 1);
841 ret
= mi_lttng_events_open(writer
);
846 for (i
= 0; i
< count
; i
++) {
847 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
853 /* close events, domain and domains */
854 ret
= mi_lttng_close_multi_element(writer
, 3);
864 * Ask for all trace events in the kernel
866 static int list_kernel_events(void)
868 int i
, size
, ret
= CMD_SUCCESS
;
869 struct lttng_domain domain
;
870 struct lttng_handle
*handle
;
871 struct lttng_event
*event_list
;
873 memset(&domain
, 0, sizeof(domain
));
875 DBG("Getting kernel tracing events");
877 domain
.type
= LTTNG_DOMAIN_KERNEL
;
879 handle
= lttng_create_handle(NULL
, &domain
);
880 if (handle
== NULL
) {
885 size
= lttng_list_tracepoints(handle
, &event_list
);
887 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
888 lttng_destroy_handle(handle
);
894 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
900 MSG("Kernel events:\n-------------");
902 for (i
= 0; i
< size
; i
++) {
903 print_events(&event_list
[i
]);
912 lttng_destroy_handle(handle
);
916 lttng_destroy_handle(handle
);
922 * Print a list of system calls.
924 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
929 ret
= mi_lttng_events_open(writer
);
934 for (i
= 0; i
< count
; i
++) {
935 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
942 ret
= mi_lttng_writer_close_element(writer
);
952 * Ask for kernel system calls.
954 static int list_syscalls(void)
956 int i
, size
, ret
= CMD_SUCCESS
;
957 struct lttng_event
*event_list
;
959 DBG("Getting kernel system call events");
961 size
= lttng_list_syscalls(&event_list
);
963 ERR("Unable to list system calls: %s", lttng_strerror(size
));
970 ret
= mi_list_syscalls(event_list
, size
);
976 MSG("System calls:\n-------------");
978 for (i
= 0; i
< size
; i
++) {
979 print_events(&event_list
[i
]);
995 * Print a list of agent events
997 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1001 /* Open events element */
1002 ret
= mi_lttng_events_open(writer
);
1007 for (i
= 0; i
< count
; i
++) {
1008 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1014 /* Close events element */
1015 ret
= mi_lttng_writer_close_element(writer
);
1022 * List agent events for a specific session using the handle.
1024 * Return CMD_SUCCESS on success else a negative value.
1026 static int list_session_agent_events(void)
1028 int ret
= CMD_SUCCESS
, count
, i
;
1029 struct lttng_event
*events
= NULL
;
1031 count
= lttng_list_events(handle
, "", &events
);
1034 ERR("%s", lttng_strerror(count
));
1040 ret
= mi_list_session_agent_events(events
, count
);
1047 MSG("Events (Logger name):\n---------------------");
1049 MSG("%sNone\n", indent6
);
1053 for (i
= 0; i
< count
; i
++) {
1054 const char *filter_str
;
1055 char *filter_msg
= NULL
;
1056 struct lttng_event
*event
= &events
[i
];
1058 ret
= lttng_event_get_filter_expression(event
,
1061 filter_msg
= strdup(" [failed to retrieve filter]");
1062 } else if (filter_str
) {
1063 const char * const filter_fmt
=
1066 filter_msg
= malloc(strlen(filter_str
) +
1067 strlen(filter_fmt
) + 1);
1069 sprintf(filter_msg
, filter_fmt
,
1074 if (event
->loglevel_type
!=
1075 LTTNG_EVENT_LOGLEVEL_ALL
) {
1076 MSG("%s- %s%s (loglevel%s %s)%s", indent4
,
1078 enabled_string(event
->enabled
),
1079 logleveltype_string(
1080 event
->loglevel_type
),
1081 mi_lttng_loglevel_string(
1083 handle
->domain
.type
),
1084 safe_string(filter_msg
));
1086 MSG("%s- %s%s%s", indent4
, event
->name
,
1087 enabled_string(event
->enabled
),
1088 safe_string(filter_msg
));
1104 * print a list of event
1106 static int mi_list_events(struct lttng_event
*events
, int count
)
1110 /* Open events element */
1111 ret
= mi_lttng_events_open(writer
);
1116 for (i
= 0; i
< count
; i
++) {
1117 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1123 /* Close events element */
1124 ret
= mi_lttng_writer_close_element(writer
);
1131 * List events of channel of session and domain.
1133 static int list_events(const char *channel_name
)
1135 int ret
= CMD_SUCCESS
, count
, i
;
1136 struct lttng_event
*events
= NULL
;
1138 count
= lttng_list_events(handle
, channel_name
, &events
);
1141 ERR("%s", lttng_strerror(count
));
1147 ret
= mi_list_events(events
, count
);
1154 MSG("\n%sEvent rules:", indent4
);
1156 MSG("%sNone\n", indent6
);
1160 for (i
= 0; i
< count
; i
++) {
1161 print_events(&events
[i
]);
1173 void print_timer(const char *timer_name
, uint32_t space_count
, int64_t value
)
1177 _MSG("%s%s:", indent6
, timer_name
);
1178 for (i
= 0; i
< space_count
; i
++) {
1183 MSG("%" PRId64
" µs", value
);
1190 * Pretty print channel
1192 static void print_channel(struct lttng_channel
*channel
)
1195 uint64_t discarded_events
, lost_packets
, monitor_timer_interval
;
1196 int64_t blocking_timeout
;
1198 ret
= lttng_channel_get_discarded_event_count(channel
,
1201 ERR("Failed to retrieve discarded event count of channel");
1205 ret
= lttng_channel_get_lost_packet_count(channel
,
1208 ERR("Failed to retrieve lost packet count of channel");
1212 ret
= lttng_channel_get_monitor_timer_interval(channel
,
1213 &monitor_timer_interval
);
1215 ERR("Failed to retrieve monitor interval of channel");
1219 ret
= lttng_channel_get_blocking_timeout(channel
,
1222 ERR("Failed to retrieve blocking timeout of channel");
1226 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1227 MSG("%sAttributes:", indent4
);
1228 MSG("%sEvent-loss mode: %s", indent6
, channel
->attr
.overwrite
? "overwrite" : "discard");
1229 MSG("%sSub-buffer size: %" PRIu64
" bytes", indent6
, channel
->attr
.subbuf_size
);
1230 MSG("%sSub-buffer count: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1232 print_timer("Switch timer", 5, channel
->attr
.switch_timer_interval
);
1233 print_timer("Read timer", 7, channel
->attr
.read_timer_interval
);
1234 print_timer("Monitor timer", 4, monitor_timer_interval
);
1236 if (!channel
->attr
.overwrite
) {
1237 if (blocking_timeout
== -1) {
1238 MSG("%sBlocking timeout: infinite", indent6
);
1240 MSG("%sBlocking timeout: %" PRId64
" µs", indent6
, blocking_timeout
);
1244 MSG("%sTrace file count: %" PRIu64
" per stream", indent6
,
1245 channel
->attr
.tracefile_count
== 0 ?
1246 1 : channel
->attr
.tracefile_count
);
1247 if (channel
->attr
.tracefile_size
!= 0 ) {
1248 MSG("%sTrace file size: %" PRIu64
" bytes", indent6
,
1249 channel
->attr
.tracefile_size
);
1251 MSG("%sTrace file size: %s", indent6
, "unlimited");
1253 switch (channel
->attr
.output
) {
1254 case LTTNG_EVENT_SPLICE
:
1255 MSG("%sOutput mode: splice", indent6
);
1257 case LTTNG_EVENT_MMAP
:
1258 MSG("%sOutput mode: mmap", indent6
);
1262 MSG("\n%sStatistics:", indent4
);
1263 if (listed_session
.snapshot_mode
) {
1265 * The lost packet count is omitted for sessions in snapshot
1266 * mode as it is misleading: it would indicate the number of
1267 * packets that the consumer could not extract during the
1268 * course of recording the snapshot. It does not have the
1269 * same meaning as the "regular" lost packet count that
1270 * would result from the consumer not keeping up with
1271 * event production in an overwrite-mode channel.
1273 * A more interesting statistic would be the number of
1274 * packets lost between the first and last extracted
1275 * packets of a given snapshot (which prevents most analyses).
1277 MSG("%sNone", indent6
);
1278 goto skip_stats_printing
;
1281 if (!channel
->attr
.overwrite
) {
1282 MSG("%sDiscarded events: %" PRIu64
, indent6
, discarded_events
);
1284 MSG("%sLost packets: %" PRIu64
, indent6
, lost_packets
);
1286 skip_stats_printing
:
1292 * Print a list of channel
1295 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1296 const char *channel_name
)
1299 unsigned int chan_found
= 0;
1301 /* Open channels element */
1302 ret
= mi_lttng_channels_open(writer
);
1307 for (i
= 0; i
< count
; i
++) {
1308 if (channel_name
!= NULL
) {
1309 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1316 /* Write channel element and leave it open */
1317 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1322 /* Listing events per channel */
1323 ret
= list_events(channels
[i
].name
);
1328 /* Closing the channel element we opened earlier */
1329 ret
= mi_lttng_writer_close_element(writer
);
1339 /* Close channels element */
1340 ret
= mi_lttng_writer_close_element(writer
);
1350 * List channel(s) of session and domain.
1352 * If channel_name is NULL, all channels are listed.
1354 static int list_channels(const char *channel_name
)
1356 int count
, i
, ret
= CMD_SUCCESS
;
1357 unsigned int chan_found
= 0;
1358 struct lttng_channel
*channels
= NULL
;
1360 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1362 count
= lttng_list_channels(handle
, &channels
);
1365 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1367 /* When printing mi this is not an error
1368 * but an empty channels element */
1372 WARN("No kernel channel");
1373 goto error_channels
;
1377 /* We had a real error */
1379 ERR("%s", lttng_strerror(count
));
1380 goto error_channels
;
1387 ret
= mi_list_channels(channels
, count
, channel_name
);
1395 MSG("Channels:\n-------------");
1398 for (i
= 0; i
< count
; i
++) {
1399 if (channel_name
!= NULL
) {
1400 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1406 print_channel(&channels
[i
]);
1408 /* Listing events per channel */
1409 ret
= list_events(channels
[i
].name
);
1419 if (!chan_found
&& channel_name
!= NULL
) {
1421 ERR("Channel %s not found", channel_name
);
1433 * List tracker PID(s) of session and domain.
1435 static int list_tracker_pids(void)
1442 ret
= lttng_list_tracker_pids(handle
,
1443 &enabled
, &pids
, &nr_pids
);
1449 _MSG("PID tracker: [");
1451 /* Mi tracker_pid element*/
1453 /* Open tracker_pid and targets elements */
1454 ret
= mi_lttng_pid_tracker_open(writer
);
1460 for (i
= 0; i
< nr_pids
; i
++) {
1464 _MSG(" %d", pids
[i
]);
1468 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1476 /* Mi close tracker_pid and targets */
1478 ret
= mi_lttng_close_multi_element(writer
,2);
1491 * List all tracker of a domain
1493 static int list_trackers(void)
1497 /* Trackers listing */
1499 ret
= mi_lttng_trackers_open(writer
);
1506 ret
= list_tracker_pids();
1512 /* Close trackers element */
1513 ret
= mi_lttng_writer_close_element(writer
);
1524 * List the rotate settings (timer/size if any).
1526 static int list_rotate_settings(const char *session_name
)
1529 uint64_t size
, timer
;
1531 ret
= lttng_rotation_schedule_get_timer_period(session_name
, &timer
);
1536 ret
= lttng_rotation_schedule_get_size(session_name
, &size
);
1541 if (!timer
&& !size
) {
1546 _MSG("Automatic rotation schedule settings:\n");
1549 _MSG(" timer period: %" PRIu64
" µs\n", timer
);
1551 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
1552 config_element_rotation_timer_interval
, timer
);
1559 _MSG(" size threshold: %" PRIu64
" bytes\n", size
);
1561 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
1562 config_element_rotation_size
, size
);
1576 * Find the session with session_name as name
1577 * and print his informations.
1579 static int mi_list_session(const char *session_name
,
1580 struct lttng_session
*sessions
, int count
)
1583 unsigned int session_found
= 0;
1585 if (session_name
== NULL
) {
1586 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1590 for (i
= 0; i
< count
; i
++) {
1591 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1592 /* We need to leave it open to append other informations
1593 * like domain, channel, events etc.*/
1595 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1603 if (!session_found
) {
1604 ERR("Session '%s' not found", session_name
);
1605 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1615 * List all availables session
1617 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1621 /* Opening sessions element */
1622 ret
= mi_lttng_sessions_open(writer
);
1627 /* Listing sessions */
1628 for (i
= 0; i
< count
; i
++) {
1629 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1635 /* Closing sessions element */
1636 ret
= mi_lttng_writer_close_element(writer
);
1646 * List available tracing session. List only basic information.
1648 * If session_name is NULL, all sessions are listed.
1650 static int list_sessions(const char *session_name
)
1652 int ret
= CMD_SUCCESS
;
1654 unsigned int session_found
= 0;
1655 struct lttng_session
*sessions
;
1657 count
= lttng_list_sessions(&sessions
);
1658 DBG("Session count %d", count
);
1661 ERR("%s", lttng_strerror(count
));
1667 if (session_name
== NULL
) {
1668 /* List all session */
1669 ret
= mi_list_sessions(sessions
, count
);
1671 /* Note : this return an open session element */
1672 ret
= mi_list_session(session_name
, sessions
, count
);
1681 MSG("Currently no available tracing session");
1685 if (session_name
== NULL
) {
1686 MSG("Available tracing sessions:");
1690 for (i
= 0; i
< count
; i
++) {
1691 if (session_name
!= NULL
) {
1692 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1694 MSG("Tracing session %s: [%s%s]", session_name
,
1695 active_string(sessions
[i
].enabled
),
1696 snapshot_string(sessions
[i
].snapshot_mode
));
1697 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1698 memcpy(&listed_session
, &sessions
[i
],
1699 sizeof(listed_session
));
1703 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1704 sessions
[i
].name
, sessions
[i
].path
,
1705 active_string(sessions
[i
].enabled
),
1706 snapshot_string(sessions
[i
].snapshot_mode
));
1707 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1708 if (sessions
[i
].live_timer_interval
!= 0) {
1709 MSG("%sLive timer interval: %u µs", indent4
,
1710 sessions
[i
].live_timer_interval
);
1716 if (!session_found
&& session_name
!= NULL
) {
1717 ERR("Session '%s' not found", session_name
);
1722 if (session_name
== NULL
) {
1723 MSG("\nUse lttng list <session_name> for more details");
1736 * list available domain(s) for a session.
1738 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1741 /* Open domains element */
1742 ret
= mi_lttng_domains_open(writer
);
1747 for (i
= 0; i
< count
; i
++) {
1748 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1754 /* Closing domains element */
1755 ret
= mi_lttng_writer_close_element(writer
);
1764 * List available domain(s) for a session.
1766 static int list_domains(const char *session_name
)
1768 int i
, count
, ret
= CMD_SUCCESS
;
1769 struct lttng_domain
*domains
= NULL
;
1772 count
= lttng_list_domains(session_name
, &domains
);
1775 ERR("%s", lttng_strerror(count
));
1781 ret
= mi_list_domains(domains
, count
);
1788 MSG("Domains:\n-------------");
1794 for (i
= 0; i
< count
; i
++) {
1795 switch (domains
[i
].type
) {
1796 case LTTNG_DOMAIN_KERNEL
:
1799 case LTTNG_DOMAIN_UST
:
1800 MSG(" - UST global");
1802 case LTTNG_DOMAIN_JUL
:
1803 MSG(" - JUL (Java Util Logging)");
1805 case LTTNG_DOMAIN_LOG4J
:
1806 MSG(" - LOG4j (Logging for Java)");
1808 case LTTNG_DOMAIN_PYTHON
:
1809 MSG(" - Python (logging)");
1825 * The 'list <options>' first level command
1827 int cmd_list(int argc
, const char **argv
)
1829 int opt
, ret
= CMD_SUCCESS
;
1830 const char *session_name
, *leftover
= NULL
;
1831 static poptContext pc
;
1832 struct lttng_domain domain
;
1833 struct lttng_domain
*domains
= NULL
;
1835 memset(&domain
, 0, sizeof(domain
));
1842 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1843 poptReadDefaultConfig(pc
, 0);
1845 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1853 case OPT_LIST_OPTIONS
:
1854 list_cmd_options(stdout
, long_options
);
1857 ret
= CMD_UNDEFINED
;
1864 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1870 /* Open command element */
1871 ret
= mi_lttng_writer_command_open(writer
,
1872 mi_lttng_element_command_list
);
1878 /* Open output element */
1879 ret
= mi_lttng_writer_open_element(writer
,
1880 mi_lttng_element_command_output
);
1887 /* Get session name (trailing argument) */
1888 session_name
= poptGetArg(pc
);
1889 DBG2("Session name: %s", session_name
);
1891 leftover
= poptGetArg(pc
);
1893 ERR("Unknown argument: %s", leftover
);
1899 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1900 } else if (opt_userspace
) {
1901 DBG2("Listing userspace global domain");
1902 domain
.type
= LTTNG_DOMAIN_UST
;
1903 } else if (opt_jul
) {
1904 DBG2("Listing JUL domain");
1905 domain
.type
= LTTNG_DOMAIN_JUL
;
1906 } else if (opt_log4j
) {
1907 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1908 } else if (opt_python
) {
1909 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1912 if (!opt_kernel
&& opt_syscall
) {
1913 WARN("--syscall will only work with the Kernel domain (-k)");
1918 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1919 handle
= lttng_create_handle(session_name
, &domain
);
1920 if (handle
== NULL
) {
1926 if (session_name
== NULL
) {
1927 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1929 ret
= list_sessions(NULL
);
1936 ret
= list_syscalls();
1941 ret
= list_kernel_events();
1947 if (opt_userspace
) {
1949 ret
= list_ust_event_fields();
1951 ret
= list_ust_events();
1957 if (opt_jul
|| opt_log4j
|| opt_python
) {
1958 ret
= list_agent_events();
1964 /* List session attributes */
1966 /* Open element sessions
1967 * Present for xml consistency */
1968 ret
= mi_lttng_sessions_open(writer
);
1973 /* MI: the ouptut of list_sessions is an unclosed session element */
1974 ret
= list_sessions(session_name
);
1979 ret
= list_rotate_settings(session_name
);
1984 /* Domain listing */
1986 ret
= list_domains(session_name
);
1990 /* Channel listing */
1991 if (opt_kernel
|| opt_userspace
) {
1993 /* Add of domains and domain element for xml
1994 * consistency and validation
1996 ret
= mi_lttng_domains_open(writer
);
2001 /* Open domain and leave it open for
2002 * nested channels printing */
2003 ret
= mi_lttng_domain(writer
, &domain
, 1);
2012 ret
= list_trackers();
2018 ret
= list_channels(opt_channel
);
2024 /* Close domain and domain element */
2025 ret
= mi_lttng_close_multi_element(writer
, 2);
2035 /* We want all domain(s) */
2036 nb_domain
= lttng_list_domains(session_name
, &domains
);
2037 if (nb_domain
< 0) {
2039 ERR("%s", lttng_strerror(nb_domain
));
2044 ret
= mi_lttng_domains_open(writer
);
2051 for (i
= 0; i
< nb_domain
; i
++) {
2052 switch (domains
[i
].type
) {
2053 case LTTNG_DOMAIN_KERNEL
:
2054 MSG("=== Domain: Kernel ===\n");
2056 case LTTNG_DOMAIN_UST
:
2057 MSG("=== Domain: UST global ===\n");
2058 MSG("Buffer type: %s\n",
2059 domains
[i
].buf_type
==
2060 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
2062 case LTTNG_DOMAIN_JUL
:
2063 MSG("=== Domain: JUL (Java Util Logging) ===\n");
2065 case LTTNG_DOMAIN_LOG4J
:
2066 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
2068 case LTTNG_DOMAIN_PYTHON
:
2069 MSG("=== Domain: Python (logging) ===\n");
2072 MSG("=== Domain: Unimplemented ===\n");
2077 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
2084 /* Clean handle before creating a new one */
2086 lttng_destroy_handle(handle
);
2089 handle
= lttng_create_handle(session_name
, &domains
[i
]);
2090 if (handle
== NULL
) {
2095 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
2096 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
2097 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
2098 ret
= list_session_agent_events();
2106 switch (domains
[i
].type
) {
2107 case LTTNG_DOMAIN_KERNEL
:
2108 case LTTNG_DOMAIN_UST
:
2109 ret
= list_trackers();
2118 ret
= list_channels(opt_channel
);
2125 /* Close domain element */
2126 ret
= mi_lttng_writer_close_element(writer
);
2135 /* Close the domains, session and sessions element */
2136 ret
= mi_lttng_close_multi_element(writer
, 3);
2147 /* Close output element */
2148 ret
= mi_lttng_writer_close_element(writer
);
2154 /* Command element close */
2155 ret
= mi_lttng_writer_command_close(writer
);
2163 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2164 /* Preserve original error code */
2165 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2170 lttng_destroy_handle(handle
);
2173 poptFreeContext(pc
);