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%sEvents:", indent4
);
1156 MSG("%sNone\n", indent6
);
1160 for (i
= 0; i
< count
; i
++) {
1161 print_events(&events
[i
]);
1173 * Pretty print channel
1175 static void print_channel(struct lttng_channel
*channel
)
1178 uint64_t discarded_events
, lost_packets
, monitor_timer_interval
;
1179 int64_t blocking_timeout
;
1181 ret
= lttng_channel_get_discarded_event_count(channel
,
1184 ERR("Failed to retrieve discarded event count of channel");
1188 ret
= lttng_channel_get_lost_packet_count(channel
,
1191 ERR("Failed to retrieve lost packet count of channel");
1195 ret
= lttng_channel_get_monitor_timer_interval(channel
,
1196 &monitor_timer_interval
);
1198 ERR("Failed to retrieve monitor interval of channel");
1202 ret
= lttng_channel_get_blocking_timeout(channel
,
1205 ERR("Failed to retrieve blocking timeout of channel");
1209 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1211 MSG("%sAttributes:", indent4
);
1212 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1213 MSG("%ssubbuffers size: %" PRIu64
" bytes", indent6
, channel
->attr
.subbuf_size
);
1214 MSG("%snumber of subbuffers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1215 MSG("%sswitch timer interval: %u µs", indent6
, channel
->attr
.switch_timer_interval
);
1216 MSG("%sread timer interval: %u µs", indent6
, channel
->attr
.read_timer_interval
);
1217 MSG("%smonitor timer interval: %" PRIu64
" µs", indent6
, monitor_timer_interval
);
1218 MSG("%sblocking timeout: %" PRId64
" µs", indent6
, blocking_timeout
);
1219 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1220 MSG("%strace file size: %" PRIu64
" bytes", indent6
, channel
->attr
.tracefile_size
);
1221 switch (channel
->attr
.output
) {
1222 case LTTNG_EVENT_SPLICE
:
1223 MSG("%soutput: splice()", indent6
);
1225 case LTTNG_EVENT_MMAP
:
1226 MSG("%soutput: mmap()", indent6
);
1230 if (!listed_session
.snapshot_mode
) {
1232 * The lost packet count is omitted for sessions in snapshot
1233 * mode as it is misleading: it would indicate the number of
1234 * packets that the consumer could not extract during the
1235 * course of recording the snapshot. It does not have the
1236 * same meaning as the "regular" lost packet count that
1237 * would result from the consumer not keeping up with
1238 * event production in an overwrite-mode channel.
1240 * A more interesting statistic would be the number of
1241 * packets lost between the first and last extracted
1242 * packets of a given snapshot (which prevents most analyses).
1244 goto skip_stats_printing
;
1247 if (!channel
->attr
.overwrite
) {
1248 MSG("%sdiscarded events: %" PRIu64
, indent6
, discarded_events
);
1250 MSG("%slost packets: %" PRIu64
, indent6
, lost_packets
);
1252 skip_stats_printing
:
1258 * Print a list of channel
1261 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1262 const char *channel_name
)
1265 unsigned int chan_found
= 0;
1267 /* Open channels element */
1268 ret
= mi_lttng_channels_open(writer
);
1273 for (i
= 0; i
< count
; i
++) {
1274 if (channel_name
!= NULL
) {
1275 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1282 /* Write channel element and leave it open */
1283 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1288 /* Listing events per channel */
1289 ret
= list_events(channels
[i
].name
);
1294 /* Closing the channel element we opened earlier */
1295 ret
= mi_lttng_writer_close_element(writer
);
1305 /* Close channels element */
1306 ret
= mi_lttng_writer_close_element(writer
);
1316 * List channel(s) of session and domain.
1318 * If channel_name is NULL, all channels are listed.
1320 static int list_channels(const char *channel_name
)
1322 int count
, i
, ret
= CMD_SUCCESS
;
1323 unsigned int chan_found
= 0;
1324 struct lttng_channel
*channels
= NULL
;
1326 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1328 count
= lttng_list_channels(handle
, &channels
);
1331 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1333 /* When printing mi this is not an error
1334 * but an empty channels element */
1338 WARN("No kernel channel");
1339 goto error_channels
;
1343 /* We had a real error */
1345 ERR("%s", lttng_strerror(count
));
1346 goto error_channels
;
1353 ret
= mi_list_channels(channels
, count
, channel_name
);
1361 MSG("Channels:\n-------------");
1364 for (i
= 0; i
< count
; i
++) {
1365 if (channel_name
!= NULL
) {
1366 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1372 print_channel(&channels
[i
]);
1374 /* Listing events per channel */
1375 ret
= list_events(channels
[i
].name
);
1385 if (!chan_found
&& channel_name
!= NULL
) {
1387 ERR("Channel %s not found", channel_name
);
1399 * List tracker PID(s) of session and domain.
1401 static int list_tracker_pids(void)
1408 ret
= lttng_list_tracker_pids(handle
,
1409 &enabled
, &pids
, &nr_pids
);
1415 _MSG("PID tracker: [");
1417 /* Mi tracker_pid element*/
1419 /* Open tracker_pid and targets elements */
1420 ret
= mi_lttng_pid_tracker_open(writer
);
1426 for (i
= 0; i
< nr_pids
; i
++) {
1430 _MSG(" %d", pids
[i
]);
1434 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1442 /* Mi close tracker_pid and targets */
1444 ret
= mi_lttng_close_multi_element(writer
,2);
1457 * List all tracker of a domain
1459 static int list_trackers(void)
1463 /* Trackers listing */
1465 ret
= mi_lttng_trackers_open(writer
);
1472 ret
= list_tracker_pids();
1478 /* Close trackers element */
1479 ret
= mi_lttng_writer_close_element(writer
);
1491 * Find the session with session_name as name
1492 * and print his informations.
1494 static int mi_list_session(const char *session_name
,
1495 struct lttng_session
*sessions
, int count
)
1498 unsigned int session_found
= 0;
1500 if (session_name
== NULL
) {
1501 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1505 for (i
= 0; i
< count
; i
++) {
1506 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1507 /* We need to leave it open to append other informations
1508 * like domain, channel, events etc.*/
1510 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1518 if (!session_found
) {
1519 ERR("Session '%s' not found", session_name
);
1520 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1530 * List all availables session
1532 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1536 /* Opening sessions element */
1537 ret
= mi_lttng_sessions_open(writer
);
1542 /* Listing sessions */
1543 for (i
= 0; i
< count
; i
++) {
1544 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1550 /* Closing sessions element */
1551 ret
= mi_lttng_writer_close_element(writer
);
1561 * List available tracing session. List only basic information.
1563 * If session_name is NULL, all sessions are listed.
1565 static int list_sessions(const char *session_name
)
1567 int ret
= CMD_SUCCESS
;
1569 unsigned int session_found
= 0;
1570 struct lttng_session
*sessions
;
1572 count
= lttng_list_sessions(&sessions
);
1573 DBG("Session count %d", count
);
1576 ERR("%s", lttng_strerror(count
));
1582 if (session_name
== NULL
) {
1583 /* List all session */
1584 ret
= mi_list_sessions(sessions
, count
);
1586 /* Note : this return an open session element */
1587 ret
= mi_list_session(session_name
, sessions
, count
);
1596 MSG("Currently no available tracing session");
1600 if (session_name
== NULL
) {
1601 MSG("Available tracing sessions:");
1605 for (i
= 0; i
< count
; i
++) {
1606 if (session_name
!= NULL
) {
1607 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1609 MSG("Tracing session %s: [%s%s]", session_name
,
1610 active_string(sessions
[i
].enabled
),
1611 snapshot_string(sessions
[i
].snapshot_mode
));
1612 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1613 memcpy(&listed_session
, &sessions
[i
],
1614 sizeof(listed_session
));
1618 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1619 sessions
[i
].name
, sessions
[i
].path
,
1620 active_string(sessions
[i
].enabled
),
1621 snapshot_string(sessions
[i
].snapshot_mode
));
1622 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1623 if (sessions
[i
].live_timer_interval
!= 0) {
1624 MSG("%sLive timer interval: %u µs", indent4
,
1625 sessions
[i
].live_timer_interval
);
1631 if (!session_found
&& session_name
!= NULL
) {
1632 ERR("Session '%s' not found", session_name
);
1637 if (session_name
== NULL
) {
1638 MSG("\nUse lttng list <session_name> for more details");
1651 * list available domain(s) for a session.
1653 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1656 /* Open domains element */
1657 ret
= mi_lttng_domains_open(writer
);
1662 for (i
= 0; i
< count
; i
++) {
1663 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1669 /* Closing domains element */
1670 ret
= mi_lttng_writer_close_element(writer
);
1679 * List available domain(s) for a session.
1681 static int list_domains(const char *session_name
)
1683 int i
, count
, ret
= CMD_SUCCESS
;
1684 struct lttng_domain
*domains
= NULL
;
1687 count
= lttng_list_domains(session_name
, &domains
);
1690 ERR("%s", lttng_strerror(count
));
1696 ret
= mi_list_domains(domains
, count
);
1703 MSG("Domains:\n-------------");
1709 for (i
= 0; i
< count
; i
++) {
1710 switch (domains
[i
].type
) {
1711 case LTTNG_DOMAIN_KERNEL
:
1714 case LTTNG_DOMAIN_UST
:
1715 MSG(" - UST global");
1717 case LTTNG_DOMAIN_JUL
:
1718 MSG(" - JUL (Java Util Logging)");
1720 case LTTNG_DOMAIN_LOG4J
:
1721 MSG(" - LOG4j (Logging for Java)");
1723 case LTTNG_DOMAIN_PYTHON
:
1724 MSG(" - Python (logging)");
1740 * The 'list <options>' first level command
1742 int cmd_list(int argc
, const char **argv
)
1744 int opt
, ret
= CMD_SUCCESS
;
1745 const char *session_name
;
1746 static poptContext pc
;
1747 struct lttng_domain domain
;
1748 struct lttng_domain
*domains
= NULL
;
1750 memset(&domain
, 0, sizeof(domain
));
1757 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1758 poptReadDefaultConfig(pc
, 0);
1760 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1768 case OPT_LIST_OPTIONS
:
1769 list_cmd_options(stdout
, long_options
);
1772 ret
= CMD_UNDEFINED
;
1779 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1785 /* Open command element */
1786 ret
= mi_lttng_writer_command_open(writer
,
1787 mi_lttng_element_command_list
);
1793 /* Open output element */
1794 ret
= mi_lttng_writer_open_element(writer
,
1795 mi_lttng_element_command_output
);
1802 /* Get session name (trailing argument) */
1803 session_name
= poptGetArg(pc
);
1804 DBG2("Session name: %s", session_name
);
1807 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1808 } else if (opt_userspace
) {
1809 DBG2("Listing userspace global domain");
1810 domain
.type
= LTTNG_DOMAIN_UST
;
1811 } else if (opt_jul
) {
1812 DBG2("Listing JUL domain");
1813 domain
.type
= LTTNG_DOMAIN_JUL
;
1814 } else if (opt_log4j
) {
1815 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1816 } else if (opt_python
) {
1817 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1820 if (!opt_kernel
&& opt_syscall
) {
1821 WARN("--syscall will only work with the Kernel domain (-k)");
1826 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1827 handle
= lttng_create_handle(session_name
, &domain
);
1828 if (handle
== NULL
) {
1834 if (session_name
== NULL
) {
1835 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1837 ret
= list_sessions(NULL
);
1844 ret
= list_syscalls();
1849 ret
= list_kernel_events();
1855 if (opt_userspace
) {
1857 ret
= list_ust_event_fields();
1859 ret
= list_ust_events();
1865 if (opt_jul
|| opt_log4j
|| opt_python
) {
1866 ret
= list_agent_events();
1872 /* List session attributes */
1874 /* Open element sessions
1875 * Present for xml consistency */
1876 ret
= mi_lttng_sessions_open(writer
);
1881 /* MI: the ouptut of list_sessions is an unclosed session element */
1882 ret
= list_sessions(session_name
);
1887 /* Domain listing */
1889 ret
= list_domains(session_name
);
1893 /* Channel listing */
1894 if (opt_kernel
|| opt_userspace
) {
1896 /* Add of domains and domain element for xml
1897 * consistency and validation
1899 ret
= mi_lttng_domains_open(writer
);
1904 /* Open domain and leave it open for
1905 * nested channels printing */
1906 ret
= mi_lttng_domain(writer
, &domain
, 1);
1915 ret
= list_trackers();
1921 ret
= list_channels(opt_channel
);
1927 /* Close domain and domain element */
1928 ret
= mi_lttng_close_multi_element(writer
, 2);
1938 /* We want all domain(s) */
1939 nb_domain
= lttng_list_domains(session_name
, &domains
);
1940 if (nb_domain
< 0) {
1942 ERR("%s", lttng_strerror(nb_domain
));
1947 ret
= mi_lttng_domains_open(writer
);
1954 for (i
= 0; i
< nb_domain
; i
++) {
1955 switch (domains
[i
].type
) {
1956 case LTTNG_DOMAIN_KERNEL
:
1957 MSG("=== Domain: Kernel ===\n");
1959 case LTTNG_DOMAIN_UST
:
1960 MSG("=== Domain: UST global ===\n");
1961 MSG("Buffer type: %s\n",
1962 domains
[i
].buf_type
==
1963 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1965 case LTTNG_DOMAIN_JUL
:
1966 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1968 case LTTNG_DOMAIN_LOG4J
:
1969 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1971 case LTTNG_DOMAIN_PYTHON
:
1972 MSG("=== Domain: Python (logging) ===\n");
1975 MSG("=== Domain: Unimplemented ===\n");
1980 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1987 /* Clean handle before creating a new one */
1989 lttng_destroy_handle(handle
);
1992 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1993 if (handle
== NULL
) {
1998 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1999 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
2000 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
2001 ret
= list_session_agent_events();
2009 switch (domains
[i
].type
) {
2010 case LTTNG_DOMAIN_KERNEL
:
2011 case LTTNG_DOMAIN_UST
:
2012 ret
= list_trackers();
2021 ret
= list_channels(opt_channel
);
2028 /* Close domain element */
2029 ret
= mi_lttng_writer_close_element(writer
);
2038 /* Close the domains, session and sessions element */
2039 ret
= mi_lttng_close_multi_element(writer
, 3);
2050 /* Close output element */
2051 ret
= mi_lttng_writer_close_element(writer
);
2057 /* Command element close */
2058 ret
= mi_lttng_writer_command_close(writer
);
2066 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2067 /* Preserve original error code */
2068 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2073 lttng_destroy_handle(handle
);
2076 poptFreeContext(pc
);