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
:
348 /* We should never have "all" events in list. */
357 static const char *field_type(struct lttng_event_field
*field
)
359 switch(field
->type
) {
360 case LTTNG_EVENT_FIELD_INTEGER
:
362 case LTTNG_EVENT_FIELD_ENUM
:
364 case LTTNG_EVENT_FIELD_FLOAT
:
366 case LTTNG_EVENT_FIELD_STRING
:
368 case LTTNG_EVENT_FIELD_OTHER
:
369 default: /* fall-through */
375 * Pretty print single event fields.
377 static void print_event_field(struct lttng_event_field
*field
)
379 if (!field
->field_name
[0]) {
382 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
383 field_type(field
), field
->nowrite
? " [no write]" : "");
388 * Jul and ust event listing
390 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
391 struct lttng_domain
*domain
)
395 char *cmdline
= NULL
;
396 int pid_element_open
= 0;
398 /* Open domains element */
399 ret
= mi_lttng_domains_open(writer
);
405 ret
= mi_lttng_domain(writer
, domain
, 1);
410 /* Open pids element element */
411 ret
= mi_lttng_pids_open(writer
);
416 for (i
= 0; i
< count
; i
++) {
417 if (cur_pid
!= events
[i
].pid
) {
418 if (pid_element_open
) {
419 /* Close the previous events and pid element */
420 ret
= mi_lttng_close_multi_element(writer
, 2);
424 pid_element_open
= 0;
427 cur_pid
= events
[i
].pid
;
428 cmdline
= get_cmdline_by_pid(cur_pid
);
434 if (!pid_element_open
) {
435 /* Open and write a pid element */
436 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
441 /* Open events element */
442 ret
= mi_lttng_events_open(writer
);
447 pid_element_open
= 1;
453 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
460 ret
= mi_lttng_writer_close_element(writer
);
465 /* Close domain, domains */
466 ret
= mi_lttng_close_multi_element(writer
, 2);
474 static int list_agent_events(void)
476 int i
, size
, ret
= CMD_SUCCESS
;
477 struct lttng_domain domain
;
478 struct lttng_handle
*handle
= NULL
;
479 struct lttng_event
*event_list
= NULL
;
481 char *cmdline
= NULL
;
482 const char *agent_domain_str
;
484 memset(&domain
, 0, sizeof(domain
));
486 domain
.type
= LTTNG_DOMAIN_JUL
;
487 } else if (opt_log4j
) {
488 domain
.type
= LTTNG_DOMAIN_LOG4J
;
489 } else if (opt_python
) {
490 domain
.type
= LTTNG_DOMAIN_PYTHON
;
492 ERR("Invalid agent domain selected.");
497 agent_domain_str
= get_domain_str(domain
.type
);
499 DBG("Getting %s tracing events", agent_domain_str
);
501 handle
= lttng_create_handle(NULL
, &domain
);
502 if (handle
== NULL
) {
507 size
= lttng_list_tracepoints(handle
, &event_list
);
509 ERR("Unable to list %s events: %s", agent_domain_str
,
510 lttng_strerror(size
));
517 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
524 MSG("%s events (Logger name):\n-------------------------",
531 for (i
= 0; i
< size
; i
++) {
532 if (cur_pid
!= event_list
[i
].pid
) {
533 cur_pid
= event_list
[i
].pid
;
534 cmdline
= get_cmdline_by_pid(cur_pid
);
535 if (cmdline
== NULL
) {
539 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
542 MSG("%s- %s", indent6
, event_list
[i
].name
);
551 lttng_destroy_handle(handle
);
556 * Ask session daemon for all user space tracepoints available.
558 static int list_ust_events(void)
560 int i
, size
, ret
= CMD_SUCCESS
;
561 struct lttng_domain domain
;
562 struct lttng_handle
*handle
;
563 struct lttng_event
*event_list
= NULL
;
565 char *cmdline
= NULL
;
567 memset(&domain
, 0, sizeof(domain
));
569 DBG("Getting UST tracing events");
571 domain
.type
= LTTNG_DOMAIN_UST
;
573 handle
= lttng_create_handle(NULL
, &domain
);
574 if (handle
== NULL
) {
579 size
= lttng_list_tracepoints(handle
, &event_list
);
581 ERR("Unable to list UST events: %s", lttng_strerror(size
));
588 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
591 MSG("UST events:\n-------------");
597 for (i
= 0; i
< size
; i
++) {
598 if (cur_pid
!= event_list
[i
].pid
) {
599 cur_pid
= event_list
[i
].pid
;
600 cmdline
= get_cmdline_by_pid(cur_pid
);
601 if (cmdline
== NULL
) {
605 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
608 print_events(&event_list
[i
]);
617 lttng_destroy_handle(handle
);
623 * List all ust event with their fields
625 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
626 struct lttng_domain
*domain
)
630 char *cmdline
= NULL
;
631 int pid_element_open
= 0;
632 int event_element_open
= 0;
633 struct lttng_event cur_event
;
635 memset(&cur_event
, 0, sizeof(cur_event
));
637 /* Open domains element */
638 ret
= mi_lttng_domains_open(writer
);
644 ret
= mi_lttng_domain(writer
, domain
, 1);
649 /* Open pids element */
650 ret
= mi_lttng_pids_open(writer
);
655 for (i
= 0; i
< count
; i
++) {
656 if (cur_pid
!= fields
[i
].event
.pid
) {
657 if (pid_element_open
) {
658 if (event_element_open
) {
659 /* Close the previous field element and event. */
660 ret
= mi_lttng_close_multi_element(writer
, 2);
664 event_element_open
= 0;
666 /* Close the previous events, pid element */
667 ret
= mi_lttng_close_multi_element(writer
, 2);
671 pid_element_open
= 0;
674 cur_pid
= fields
[i
].event
.pid
;
675 cmdline
= get_cmdline_by_pid(cur_pid
);
676 if (!pid_element_open
) {
677 /* Open and write a pid element */
678 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
683 /* Open events element */
684 ret
= mi_lttng_events_open(writer
);
688 pid_element_open
= 1;
691 /* Wipe current event since we are about to print a new PID. */
692 memset(&cur_event
, 0, sizeof(cur_event
));
695 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
696 if (event_element_open
) {
697 /* Close the previous fields element and the previous event */
698 ret
= mi_lttng_close_multi_element(writer
, 2);
702 event_element_open
= 0;
705 memcpy(&cur_event
, &fields
[i
].event
,
708 if (!event_element_open
) {
709 /* Open and write the event */
710 ret
= mi_lttng_event(writer
, &cur_event
, 1,
711 handle
->domain
.type
);
716 /* Open a fields element */
717 ret
= mi_lttng_event_fields_open(writer
);
721 event_element_open
= 1;
725 /* Print the event_field */
726 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
732 /* Close pid, domain, domains */
733 ret
= mi_lttng_close_multi_element(writer
, 3);
742 * Ask session daemon for all user space tracepoint fields available.
744 static int list_ust_event_fields(void)
746 int i
, size
, ret
= CMD_SUCCESS
;
747 struct lttng_domain domain
;
748 struct lttng_handle
*handle
;
749 struct lttng_event_field
*event_field_list
;
751 char *cmdline
= NULL
;
753 struct lttng_event cur_event
;
755 memset(&domain
, 0, sizeof(domain
));
756 memset(&cur_event
, 0, sizeof(cur_event
));
758 DBG("Getting UST tracing event fields");
760 domain
.type
= LTTNG_DOMAIN_UST
;
762 handle
= lttng_create_handle(NULL
, &domain
);
763 if (handle
== NULL
) {
768 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
770 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
777 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
784 MSG("UST events:\n-------------");
790 for (i
= 0; i
< size
; i
++) {
791 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
792 cur_pid
= event_field_list
[i
].event
.pid
;
793 cmdline
= get_cmdline_by_pid(cur_pid
);
794 if (cmdline
== NULL
) {
798 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
800 /* Wipe current event since we are about to print a new PID. */
801 memset(&cur_event
, 0, sizeof(cur_event
));
803 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
804 print_events(&event_field_list
[i
].event
);
805 memcpy(&cur_event
, &event_field_list
[i
].event
,
808 print_event_field(&event_field_list
[i
]);
815 free(event_field_list
);
817 lttng_destroy_handle(handle
);
823 * Print a list of kernel events
825 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
826 struct lttng_domain
*domain
)
830 /* Open domains element */
831 ret
= mi_lttng_domains_open(writer
);
837 ret
= mi_lttng_domain(writer
, domain
, 1);
843 ret
= mi_lttng_events_open(writer
);
848 for (i
= 0; i
< count
; i
++) {
849 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
855 /* close events, domain and domains */
856 ret
= mi_lttng_close_multi_element(writer
, 3);
866 * Ask for all trace events in the kernel
868 static int list_kernel_events(void)
870 int i
, size
, ret
= CMD_SUCCESS
;
871 struct lttng_domain domain
;
872 struct lttng_handle
*handle
;
873 struct lttng_event
*event_list
;
875 memset(&domain
, 0, sizeof(domain
));
877 DBG("Getting kernel tracing events");
879 domain
.type
= LTTNG_DOMAIN_KERNEL
;
881 handle
= lttng_create_handle(NULL
, &domain
);
882 if (handle
== NULL
) {
887 size
= lttng_list_tracepoints(handle
, &event_list
);
889 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
890 lttng_destroy_handle(handle
);
896 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
902 MSG("Kernel events:\n-------------");
904 for (i
= 0; i
< size
; i
++) {
905 print_events(&event_list
[i
]);
914 lttng_destroy_handle(handle
);
918 lttng_destroy_handle(handle
);
924 * Print a list of system calls.
926 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
931 ret
= mi_lttng_events_open(writer
);
936 for (i
= 0; i
< count
; i
++) {
937 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
944 ret
= mi_lttng_writer_close_element(writer
);
954 * Ask for kernel system calls.
956 static int list_syscalls(void)
958 int i
, size
, ret
= CMD_SUCCESS
;
959 struct lttng_event
*event_list
;
961 DBG("Getting kernel system call events");
963 size
= lttng_list_syscalls(&event_list
);
965 ERR("Unable to list system calls: %s", lttng_strerror(size
));
972 ret
= mi_list_syscalls(event_list
, size
);
978 MSG("System calls:\n-------------");
980 for (i
= 0; i
< size
; i
++) {
981 print_events(&event_list
[i
]);
997 * Print a list of agent events
999 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1003 /* Open events element */
1004 ret
= mi_lttng_events_open(writer
);
1009 for (i
= 0; i
< count
; i
++) {
1010 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1016 /* Close events element */
1017 ret
= mi_lttng_writer_close_element(writer
);
1024 * List agent events for a specific session using the handle.
1026 * Return CMD_SUCCESS on success else a negative value.
1028 static int list_session_agent_events(void)
1030 int ret
= CMD_SUCCESS
, count
, i
;
1031 struct lttng_event
*events
= NULL
;
1033 count
= lttng_list_events(handle
, "", &events
);
1036 ERR("%s", lttng_strerror(count
));
1042 ret
= mi_list_session_agent_events(events
, count
);
1049 MSG("Events (Logger name):\n---------------------");
1051 MSG("%sNone\n", indent6
);
1055 for (i
= 0; i
< count
; i
++) {
1056 const char *filter_str
;
1057 char *filter_msg
= NULL
;
1058 struct lttng_event
*event
= &events
[i
];
1060 ret
= lttng_event_get_filter_expression(event
,
1063 filter_msg
= strdup(" [failed to retrieve filter]");
1064 } else if (filter_str
) {
1065 const char * const filter_fmt
=
1068 filter_msg
= malloc(strlen(filter_str
) +
1069 strlen(filter_fmt
) + 1);
1071 sprintf(filter_msg
, filter_fmt
,
1076 if (event
->loglevel_type
!=
1077 LTTNG_EVENT_LOGLEVEL_ALL
) {
1078 MSG("%s- %s%s (loglevel%s %s)%s", indent4
,
1080 enabled_string(event
->enabled
),
1081 logleveltype_string(
1082 event
->loglevel_type
),
1083 mi_lttng_loglevel_string(
1085 handle
->domain
.type
),
1086 safe_string(filter_msg
));
1088 MSG("%s- %s%s%s", indent4
, event
->name
,
1089 enabled_string(event
->enabled
),
1090 safe_string(filter_msg
));
1106 * print a list of event
1108 static int mi_list_events(struct lttng_event
*events
, int count
)
1112 /* Open events element */
1113 ret
= mi_lttng_events_open(writer
);
1118 for (i
= 0; i
< count
; i
++) {
1119 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1125 /* Close events element */
1126 ret
= mi_lttng_writer_close_element(writer
);
1133 * List events of channel of session and domain.
1135 static int list_events(const char *channel_name
)
1137 int ret
= CMD_SUCCESS
, count
, i
;
1138 struct lttng_event
*events
= NULL
;
1140 count
= lttng_list_events(handle
, channel_name
, &events
);
1143 ERR("%s", lttng_strerror(count
));
1149 ret
= mi_list_events(events
, count
);
1156 MSG("\n%sEvent rules:", indent4
);
1158 MSG("%sNone\n", indent6
);
1162 for (i
= 0; i
< count
; i
++) {
1163 print_events(&events
[i
]);
1175 void print_timer(const char *timer_name
, uint32_t space_count
, int64_t value
)
1179 _MSG("%s%s:", indent6
, timer_name
);
1180 for (i
= 0; i
< space_count
; i
++) {
1185 MSG("%" PRId64
" µs", value
);
1192 * Pretty print channel
1194 static void print_channel(struct lttng_channel
*channel
)
1197 uint64_t discarded_events
, lost_packets
, monitor_timer_interval
;
1198 int64_t blocking_timeout
;
1200 ret
= lttng_channel_get_discarded_event_count(channel
,
1203 ERR("Failed to retrieve discarded event count of channel");
1207 ret
= lttng_channel_get_lost_packet_count(channel
,
1210 ERR("Failed to retrieve lost packet count of channel");
1214 ret
= lttng_channel_get_monitor_timer_interval(channel
,
1215 &monitor_timer_interval
);
1217 ERR("Failed to retrieve monitor interval of channel");
1221 ret
= lttng_channel_get_blocking_timeout(channel
,
1224 ERR("Failed to retrieve blocking timeout of channel");
1228 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1229 MSG("%sAttributes:", indent4
);
1230 MSG("%sEvent-loss mode: %s", indent6
, channel
->attr
.overwrite
? "overwrite" : "discard");
1231 MSG("%sSub-buffer size: %" PRIu64
" bytes", indent6
, channel
->attr
.subbuf_size
);
1232 MSG("%sSub-buffer count: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1234 print_timer("Switch timer", 5, channel
->attr
.switch_timer_interval
);
1235 print_timer("Read timer", 7, channel
->attr
.read_timer_interval
);
1236 print_timer("Monitor timer", 4, monitor_timer_interval
);
1238 if (!channel
->attr
.overwrite
) {
1239 if (blocking_timeout
== -1) {
1240 MSG("%sBlocking timeout: infinite", indent6
);
1242 MSG("%sBlocking timeout: %" PRId64
" µs", indent6
, blocking_timeout
);
1246 MSG("%sTrace file count: %" PRIu64
" per stream", indent6
,
1247 channel
->attr
.tracefile_count
== 0 ?
1248 1 : channel
->attr
.tracefile_count
);
1249 if (channel
->attr
.tracefile_size
!= 0 ) {
1250 MSG("%sTrace file size: %" PRIu64
" bytes", indent6
,
1251 channel
->attr
.tracefile_size
);
1253 MSG("%sTrace file size: %s", indent6
, "unlimited");
1255 switch (channel
->attr
.output
) {
1256 case LTTNG_EVENT_SPLICE
:
1257 MSG("%sOutput mode: splice", indent6
);
1259 case LTTNG_EVENT_MMAP
:
1260 MSG("%sOutput mode: mmap", indent6
);
1264 MSG("\n%sStatistics:", indent4
);
1265 if (listed_session
.snapshot_mode
) {
1267 * The lost packet count is omitted for sessions in snapshot
1268 * mode as it is misleading: it would indicate the number of
1269 * packets that the consumer could not extract during the
1270 * course of recording the snapshot. It does not have the
1271 * same meaning as the "regular" lost packet count that
1272 * would result from the consumer not keeping up with
1273 * event production in an overwrite-mode channel.
1275 * A more interesting statistic would be the number of
1276 * packets lost between the first and last extracted
1277 * packets of a given snapshot (which prevents most analyses).
1279 MSG("%sNone", indent6
);
1280 goto skip_stats_printing
;
1283 if (!channel
->attr
.overwrite
) {
1284 MSG("%sDiscarded events: %" PRIu64
, indent6
, discarded_events
);
1286 MSG("%sLost packets: %" PRIu64
, indent6
, lost_packets
);
1288 skip_stats_printing
:
1294 * Print a list of channel
1297 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1298 const char *channel_name
)
1301 unsigned int chan_found
= 0;
1303 /* Open channels element */
1304 ret
= mi_lttng_channels_open(writer
);
1309 for (i
= 0; i
< count
; i
++) {
1310 if (channel_name
!= NULL
) {
1311 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1318 /* Write channel element and leave it open */
1319 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1324 /* Listing events per channel */
1325 ret
= list_events(channels
[i
].name
);
1330 /* Closing the channel element we opened earlier */
1331 ret
= mi_lttng_writer_close_element(writer
);
1341 /* Close channels element */
1342 ret
= mi_lttng_writer_close_element(writer
);
1352 * List channel(s) of session and domain.
1354 * If channel_name is NULL, all channels are listed.
1356 static int list_channels(const char *channel_name
)
1358 int count
, i
, ret
= CMD_SUCCESS
;
1359 unsigned int chan_found
= 0;
1360 struct lttng_channel
*channels
= NULL
;
1362 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1364 count
= lttng_list_channels(handle
, &channels
);
1367 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1369 /* When printing mi this is not an error
1370 * but an empty channels element */
1374 WARN("No kernel channel");
1375 goto error_channels
;
1379 /* We had a real error */
1381 ERR("%s", lttng_strerror(count
));
1382 goto error_channels
;
1389 ret
= mi_list_channels(channels
, count
, channel_name
);
1397 MSG("Channels:\n-------------");
1400 for (i
= 0; i
< count
; i
++) {
1401 if (channel_name
!= NULL
) {
1402 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1408 print_channel(&channels
[i
]);
1410 /* Listing events per channel */
1411 ret
= list_events(channels
[i
].name
);
1421 if (!chan_found
&& channel_name
!= NULL
) {
1423 ERR("Channel %s not found", channel_name
);
1435 * List tracker PID(s) of session and domain.
1437 static int list_tracker_pids(void)
1444 ret
= lttng_list_tracker_pids(handle
,
1445 &enabled
, &pids
, &nr_pids
);
1451 _MSG("PID tracker: [");
1453 /* Mi tracker_pid element*/
1455 /* Open tracker_pid and targets elements */
1456 ret
= mi_lttng_pid_tracker_open(writer
);
1462 for (i
= 0; i
< nr_pids
; i
++) {
1466 _MSG(" %d", pids
[i
]);
1470 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1478 /* Mi close tracker_pid and targets */
1480 ret
= mi_lttng_close_multi_element(writer
,2);
1493 * List all tracker of a domain
1495 static int list_trackers(void)
1499 /* Trackers listing */
1501 ret
= mi_lttng_trackers_open(writer
);
1508 ret
= list_tracker_pids();
1514 /* Close trackers element */
1515 ret
= mi_lttng_writer_close_element(writer
);
1525 static enum cmd_error_code
print_periodic_rotation_schedule(
1526 const struct lttng_rotation_schedule
*schedule
)
1528 enum cmd_error_code ret
;
1529 enum lttng_rotation_status status
;
1532 status
= lttng_rotation_schedule_periodic_get_period(schedule
,
1534 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1535 ERR("Failed to retrieve period parameter from periodic rotation schedule.");
1540 MSG(" timer period: %" PRIu64
" µs", value
);
1546 static enum cmd_error_code
print_size_threshold_rotation_schedule(
1547 const struct lttng_rotation_schedule
*schedule
)
1549 enum cmd_error_code ret
;
1550 enum lttng_rotation_status status
;
1553 status
= lttng_rotation_schedule_size_threshold_get_threshold(schedule
,
1555 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1556 ERR("Failed to retrieve size parameter from size-based rotation schedule.");
1561 MSG(" size threshold: %" PRIu64
" bytes", value
);
1567 static enum cmd_error_code
print_rotation_schedule(
1568 const struct lttng_rotation_schedule
*schedule
)
1570 enum cmd_error_code ret
;
1572 switch (lttng_rotation_schedule_get_type(schedule
)) {
1573 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
1574 ret
= print_size_threshold_rotation_schedule(schedule
);
1576 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
1577 ret
= print_periodic_rotation_schedule(schedule
);
1586 * List the automatic rotation settings.
1588 static enum cmd_error_code
list_rotate_settings(const char *session_name
)
1591 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
1592 unsigned int count
, i
;
1593 struct lttng_rotation_schedules
*schedules
= NULL
;
1594 enum lttng_rotation_status status
;
1596 ret
= lttng_session_list_rotation_schedules(session_name
, &schedules
);
1597 if (ret
!= LTTNG_OK
) {
1598 ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret
));
1599 cmd_ret
= CMD_ERROR
;
1603 status
= lttng_rotation_schedules_get_count(schedules
, &count
);
1604 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
1605 ERR("Failed to retrieve the number of session rotation schedules.");
1606 cmd_ret
= CMD_ERROR
;
1611 cmd_ret
= CMD_SUCCESS
;
1615 MSG("Automatic rotation schedules:");
1617 ret
= mi_lttng_writer_open_element(writer
,
1618 mi_lttng_element_rotation_schedules
);
1620 cmd_ret
= CMD_ERROR
;
1625 for (i
= 0; i
< count
; i
++) {
1626 enum cmd_error_code tmp_ret
= CMD_SUCCESS
;
1627 const struct lttng_rotation_schedule
*schedule
;
1629 schedule
= lttng_rotation_schedules_get_at_index(schedules
, i
);
1631 ERR("Failed to retrieve session rotation schedule.");
1632 cmd_ret
= CMD_ERROR
;
1637 ret
= mi_lttng_rotation_schedule(writer
, schedule
);
1639 tmp_ret
= CMD_ERROR
;
1642 tmp_ret
= print_rotation_schedule(schedule
);
1646 * Report an error if the serialization of any of the
1647 * descriptors failed.
1649 cmd_ret
= cmd_ret
? cmd_ret
: tmp_ret
;
1654 /* Close the rotation_schedules element. */
1655 ret
= mi_lttng_writer_close_element(writer
);
1657 cmd_ret
= CMD_ERROR
;
1662 lttng_rotation_schedules_destroy(schedules
);
1668 * Find the session with session_name as name
1669 * and print his informations.
1671 static int mi_list_session(const char *session_name
,
1672 struct lttng_session
*sessions
, int count
)
1675 unsigned int session_found
= 0;
1677 if (session_name
== NULL
) {
1678 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1682 for (i
= 0; i
< count
; i
++) {
1683 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1684 /* We need to leave it open to append other informations
1685 * like domain, channel, events etc.*/
1687 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1695 if (!session_found
) {
1696 ERR("Session '%s' not found", session_name
);
1697 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1707 * List all availables session
1709 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1713 /* Opening sessions element */
1714 ret
= mi_lttng_sessions_open(writer
);
1719 /* Listing sessions */
1720 for (i
= 0; i
< count
; i
++) {
1721 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1727 /* Closing sessions element */
1728 ret
= mi_lttng_writer_close_element(writer
);
1738 * List available tracing session. List only basic information.
1740 * If session_name is NULL, all sessions are listed.
1742 static int list_sessions(const char *session_name
)
1744 int ret
= CMD_SUCCESS
;
1746 unsigned int session_found
= 0;
1747 struct lttng_session
*sessions
;
1749 count
= lttng_list_sessions(&sessions
);
1750 DBG("Session count %d", count
);
1753 ERR("%s", lttng_strerror(count
));
1759 if (session_name
== NULL
) {
1760 /* List all session */
1761 ret
= mi_list_sessions(sessions
, count
);
1763 /* Note : this return an open session element */
1764 ret
= mi_list_session(session_name
, sessions
, count
);
1773 MSG("Currently no available tracing session");
1777 if (session_name
== NULL
) {
1778 MSG("Available tracing sessions:");
1782 for (i
= 0; i
< count
; i
++) {
1783 if (session_name
!= NULL
) {
1784 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1786 MSG("Tracing session %s: [%s%s]", session_name
,
1787 active_string(sessions
[i
].enabled
),
1788 snapshot_string(sessions
[i
].snapshot_mode
));
1789 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1790 memcpy(&listed_session
, &sessions
[i
],
1791 sizeof(listed_session
));
1795 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1796 sessions
[i
].name
, sessions
[i
].path
,
1797 active_string(sessions
[i
].enabled
),
1798 snapshot_string(sessions
[i
].snapshot_mode
));
1799 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1800 if (sessions
[i
].live_timer_interval
!= 0) {
1801 MSG("%sLive timer interval: %u µs", indent4
,
1802 sessions
[i
].live_timer_interval
);
1808 if (!session_found
&& session_name
!= NULL
) {
1809 ERR("Session '%s' not found", session_name
);
1814 if (session_name
== NULL
) {
1815 MSG("\nUse lttng list <session_name> for more details");
1828 * list available domain(s) for a session.
1830 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1833 /* Open domains element */
1834 ret
= mi_lttng_domains_open(writer
);
1839 for (i
= 0; i
< count
; i
++) {
1840 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1846 /* Closing domains element */
1847 ret
= mi_lttng_writer_close_element(writer
);
1856 * List available domain(s) for a session.
1858 static int list_domains(const char *session_name
)
1860 int i
, count
, ret
= CMD_SUCCESS
;
1861 struct lttng_domain
*domains
= NULL
;
1864 count
= lttng_list_domains(session_name
, &domains
);
1867 ERR("%s", lttng_strerror(count
));
1873 ret
= mi_list_domains(domains
, count
);
1880 MSG("Domains:\n-------------");
1886 for (i
= 0; i
< count
; i
++) {
1887 switch (domains
[i
].type
) {
1888 case LTTNG_DOMAIN_KERNEL
:
1891 case LTTNG_DOMAIN_UST
:
1892 MSG(" - UST global");
1894 case LTTNG_DOMAIN_JUL
:
1895 MSG(" - JUL (Java Util Logging)");
1897 case LTTNG_DOMAIN_LOG4J
:
1898 MSG(" - LOG4j (Logging for Java)");
1900 case LTTNG_DOMAIN_PYTHON
:
1901 MSG(" - Python (logging)");
1917 * The 'list <options>' first level command
1919 int cmd_list(int argc
, const char **argv
)
1921 int opt
, ret
= CMD_SUCCESS
;
1922 const char *session_name
, *leftover
= NULL
;
1923 static poptContext pc
;
1924 struct lttng_domain domain
;
1925 struct lttng_domain
*domains
= NULL
;
1927 memset(&domain
, 0, sizeof(domain
));
1934 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1935 poptReadDefaultConfig(pc
, 0);
1937 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1945 case OPT_LIST_OPTIONS
:
1946 list_cmd_options(stdout
, long_options
);
1949 ret
= CMD_UNDEFINED
;
1956 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1962 /* Open command element */
1963 ret
= mi_lttng_writer_command_open(writer
,
1964 mi_lttng_element_command_list
);
1970 /* Open output element */
1971 ret
= mi_lttng_writer_open_element(writer
,
1972 mi_lttng_element_command_output
);
1979 /* Get session name (trailing argument) */
1980 session_name
= poptGetArg(pc
);
1981 DBG2("Session name: %s", session_name
);
1983 leftover
= poptGetArg(pc
);
1985 ERR("Unknown argument: %s", leftover
);
1991 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1992 } else if (opt_userspace
) {
1993 DBG2("Listing userspace global domain");
1994 domain
.type
= LTTNG_DOMAIN_UST
;
1995 } else if (opt_jul
) {
1996 DBG2("Listing JUL domain");
1997 domain
.type
= LTTNG_DOMAIN_JUL
;
1998 } else if (opt_log4j
) {
1999 domain
.type
= LTTNG_DOMAIN_LOG4J
;
2000 } else if (opt_python
) {
2001 domain
.type
= LTTNG_DOMAIN_PYTHON
;
2004 if (!opt_kernel
&& opt_syscall
) {
2005 WARN("--syscall will only work with the Kernel domain (-k)");
2010 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
2011 handle
= lttng_create_handle(session_name
, &domain
);
2012 if (handle
== NULL
) {
2018 if (session_name
== NULL
) {
2019 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
2021 ret
= list_sessions(NULL
);
2028 ret
= list_syscalls();
2033 ret
= list_kernel_events();
2039 if (opt_userspace
) {
2041 ret
= list_ust_event_fields();
2043 ret
= list_ust_events();
2049 if (opt_jul
|| opt_log4j
|| opt_python
) {
2050 ret
= list_agent_events();
2056 /* List session attributes */
2058 /* Open element sessions
2059 * Present for xml consistency */
2060 ret
= mi_lttng_sessions_open(writer
);
2065 /* MI: the ouptut of list_sessions is an unclosed session element */
2066 ret
= list_sessions(session_name
);
2071 ret
= list_rotate_settings(session_name
);
2076 /* Domain listing */
2078 ret
= list_domains(session_name
);
2082 /* Channel listing */
2083 if (opt_kernel
|| opt_userspace
) {
2085 /* Add of domains and domain element for xml
2086 * consistency and validation
2088 ret
= mi_lttng_domains_open(writer
);
2093 /* Open domain and leave it open for
2094 * nested channels printing */
2095 ret
= mi_lttng_domain(writer
, &domain
, 1);
2104 ret
= list_trackers();
2110 ret
= list_channels(opt_channel
);
2116 /* Close domain and domain element */
2117 ret
= mi_lttng_close_multi_element(writer
, 2);
2127 /* We want all domain(s) */
2128 nb_domain
= lttng_list_domains(session_name
, &domains
);
2129 if (nb_domain
< 0) {
2131 ERR("%s", lttng_strerror(nb_domain
));
2136 ret
= mi_lttng_domains_open(writer
);
2143 for (i
= 0; i
< nb_domain
; i
++) {
2144 switch (domains
[i
].type
) {
2145 case LTTNG_DOMAIN_KERNEL
:
2146 MSG("=== Domain: Kernel ===\n");
2148 case LTTNG_DOMAIN_UST
:
2149 MSG("=== Domain: UST global ===\n");
2150 MSG("Buffer type: %s\n",
2151 domains
[i
].buf_type
==
2152 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
2154 case LTTNG_DOMAIN_JUL
:
2155 MSG("=== Domain: JUL (Java Util Logging) ===\n");
2157 case LTTNG_DOMAIN_LOG4J
:
2158 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
2160 case LTTNG_DOMAIN_PYTHON
:
2161 MSG("=== Domain: Python (logging) ===\n");
2164 MSG("=== Domain: Unimplemented ===\n");
2169 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
2176 /* Clean handle before creating a new one */
2178 lttng_destroy_handle(handle
);
2181 handle
= lttng_create_handle(session_name
, &domains
[i
]);
2182 if (handle
== NULL
) {
2187 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
2188 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
2189 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
2190 ret
= list_session_agent_events();
2198 switch (domains
[i
].type
) {
2199 case LTTNG_DOMAIN_KERNEL
:
2200 case LTTNG_DOMAIN_UST
:
2201 ret
= list_trackers();
2210 ret
= list_channels(opt_channel
);
2217 /* Close domain element */
2218 ret
= mi_lttng_writer_close_element(writer
);
2227 /* Close the domains, session and sessions element */
2228 ret
= mi_lttng_close_multi_element(writer
, 3);
2239 /* Close output element */
2240 ret
= mi_lttng_writer_close_element(writer
);
2246 /* Command element close */
2247 ret
= mi_lttng_writer_command_close(writer
);
2255 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2256 /* Preserve original error code */
2257 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2262 lttng_destroy_handle(handle
);
2265 poptFreeContext(pc
);