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
= " ";
51 static struct lttng_handle
*handle
;
52 static struct mi_writer
*writer
;
54 static struct poptOption long_options
[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
57 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
58 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
59 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
60 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
61 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
62 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
63 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
64 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
65 {"syscall", 'S', POPT_ARG_VAL
, &opt_syscall
, 1, 0, 0},
66 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
73 static void usage(FILE *ofp
)
75 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
77 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
79 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
80 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
82 fprintf(ofp
, " -h, --help Show this help\n");
83 fprintf(ofp
, " --list-options Simple listing of options\n");
84 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
85 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
86 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
87 fprintf(ofp
, " -l, --log4j Apply for Java application using LOG4J\n");
88 fprintf(ofp
, " -p, --python Apply for Python application using logging\n");
89 fprintf(ofp
, " -f, --fields List event fields.\n");
90 fprintf(ofp
, " --syscall List available system calls.\n");
92 fprintf(ofp
, "Session Options:\n");
93 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
94 fprintf(ofp
, " -d, --domain List available domain(s)\n");
99 * Get command line from /proc for a specific pid.
101 * On success, return an allocated string pointer to the proc cmdline.
102 * On error, return NULL.
104 static char *get_cmdline_by_pid(pid_t pid
)
108 char *cmdline
= NULL
;
109 /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */
110 char path
[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR
) - 1];
112 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
113 fp
= fopen(path
, "r");
118 /* Caller must free() *cmdline */
119 cmdline
= zmalloc(PATH_MAX
);
121 PERROR("malloc cmdline");
124 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
126 PERROR("fread proc list");
137 const char *active_string(int value
)
140 case 0: return "inactive";
141 case 1: return "active";
143 default: return NULL
;
147 static const char *snapshot_string(int value
)
158 const char *enabled_string(int value
)
161 case 0: return " [disabled]";
162 case 1: return " [enabled]";
164 default: return NULL
;
169 const char *safe_string(const char *str
)
171 return str
? str
: "";
174 static const char *logleveltype_string(enum lttng_loglevel_type value
)
177 case LTTNG_EVENT_LOGLEVEL_ALL
:
179 case LTTNG_EVENT_LOGLEVEL_RANGE
:
181 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
184 return " <<TYPE UNKN>>";
188 static const char *bitness_event(enum lttng_event_flag flags
)
190 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_32
) {
191 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
192 return " [32/64-bit]";
196 } else if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
204 * Get exclusion names message for a single event.
206 * Returned pointer must be freed by caller. Returns NULL on error.
208 static char *get_exclusion_names_msg(struct lttng_event
*event
)
212 char *exclusion_msg
= NULL
;
215 const char * const exclusion_fmt
= " [exclusions: ";
216 const size_t exclusion_fmt_len
= strlen(exclusion_fmt
);
218 exclusion_count
= lttng_event_get_exclusion_name_count(event
);
219 if (exclusion_count
< 0) {
221 } else if (exclusion_count
== 0) {
223 * No exclusions: return copy of empty string so that
224 * it can be freed by caller.
226 exclusion_msg
= strdup("");
231 * exclusion_msg's size is bounded by the exclusion_fmt string,
232 * a comma per entry, the entry count (fixed-size), a closing
233 * bracket, and a trailing \0.
235 exclusion_msg
= malloc(exclusion_count
+
236 exclusion_count
* LTTNG_SYMBOL_NAME_LEN
+
237 exclusion_fmt_len
+ 1);
238 if (!exclusion_msg
) {
242 at
= strcpy(exclusion_msg
, exclusion_fmt
) + exclusion_fmt_len
;
243 for (i
= 0; i
< exclusion_count
; ++i
) {
246 /* Append comma between exclusion names */
252 ret
= lttng_event_get_exclusion_name(event
, i
, &name
);
254 /* Prints '?' on local error; should never happen */
260 /* Append exclusion name */
261 at
+= sprintf(at
, "%s", name
);
264 /* This also puts a final '\0' at the end of exclusion_msg */
268 return exclusion_msg
;
272 * Pretty print single event.
274 static void print_events(struct lttng_event
*event
)
277 const char *filter_str
;
278 char *filter_msg
= NULL
;
279 char *exclusion_msg
= NULL
;
281 ret
= lttng_event_get_filter_expression(event
, &filter_str
);
284 filter_msg
= strdup(" [failed to retrieve filter]");
285 } else if (filter_str
) {
286 const char * const filter_fmt
= " [filter: '%s']";
288 filter_msg
= malloc(strlen(filter_str
) +
289 strlen(filter_fmt
) + 1);
291 sprintf(filter_msg
, filter_fmt
,
296 exclusion_msg
= get_exclusion_names_msg(event
);
297 if (!exclusion_msg
) {
298 exclusion_msg
= strdup(" [failed to retrieve exclusions]");
301 switch (event
->type
) {
302 case LTTNG_EVENT_TRACEPOINT
:
304 if (event
->loglevel
!= -1) {
305 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
308 logleveltype_string(event
->loglevel_type
),
309 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
311 enabled_string(event
->enabled
),
312 safe_string(exclusion_msg
),
313 safe_string(filter_msg
));
315 MSG("%s%s (type: tracepoint)%s%s%s",
318 enabled_string(event
->enabled
),
319 safe_string(exclusion_msg
),
320 safe_string(filter_msg
));
324 case LTTNG_EVENT_FUNCTION
:
325 MSG("%s%s (type: function)%s%s", indent6
,
326 event
->name
, enabled_string(event
->enabled
),
327 safe_string(filter_msg
));
328 if (event
->attr
.probe
.addr
!= 0) {
329 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
331 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
332 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
335 case LTTNG_EVENT_PROBE
:
336 MSG("%s%s (type: probe)%s%s", indent6
,
337 event
->name
, enabled_string(event
->enabled
),
338 safe_string(filter_msg
));
339 if (event
->attr
.probe
.addr
!= 0) {
340 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
342 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
343 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
346 case LTTNG_EVENT_FUNCTION_ENTRY
:
347 MSG("%s%s (type: function)%s%s", indent6
,
348 event
->name
, enabled_string(event
->enabled
),
349 safe_string(filter_msg
));
350 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
352 case LTTNG_EVENT_SYSCALL
:
353 MSG("%s%s%s%s%s%s", indent6
, event
->name
,
354 (opt_syscall
? "" : " (type:syscall)"),
355 enabled_string(event
->enabled
),
356 bitness_event(event
->flags
),
357 safe_string(filter_msg
));
359 case LTTNG_EVENT_NOOP
:
360 MSG("%s (type: noop)%s%s", indent6
,
361 enabled_string(event
->enabled
),
362 safe_string(filter_msg
));
364 case LTTNG_EVENT_ALL
:
365 /* We should never have "all" events in list. */
374 static const char *field_type(struct lttng_event_field
*field
)
376 switch(field
->type
) {
377 case LTTNG_EVENT_FIELD_INTEGER
:
379 case LTTNG_EVENT_FIELD_ENUM
:
381 case LTTNG_EVENT_FIELD_FLOAT
:
383 case LTTNG_EVENT_FIELD_STRING
:
385 case LTTNG_EVENT_FIELD_OTHER
:
386 default: /* fall-through */
392 * Pretty print single event fields.
394 static void print_event_field(struct lttng_event_field
*field
)
396 if (!field
->field_name
[0]) {
399 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
400 field_type(field
), field
->nowrite
? " [no write]" : "");
405 * Jul and ust event listing
407 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
408 struct lttng_domain
*domain
)
412 char *cmdline
= NULL
;
413 int pid_element_open
= 0;
415 /* Open domains element */
416 ret
= mi_lttng_domains_open(writer
);
422 ret
= mi_lttng_domain(writer
, domain
, 1);
427 /* Open pids element element */
428 ret
= mi_lttng_pids_open(writer
);
433 for (i
= 0; i
< count
; i
++) {
434 if (cur_pid
!= events
[i
].pid
) {
435 if (pid_element_open
) {
436 /* Close the previous events and pid element */
437 ret
= mi_lttng_close_multi_element(writer
, 2);
441 pid_element_open
= 0;
444 cur_pid
= events
[i
].pid
;
445 cmdline
= get_cmdline_by_pid(cur_pid
);
451 if (!pid_element_open
) {
452 /* Open and write a pid element */
453 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
458 /* Open events element */
459 ret
= mi_lttng_events_open(writer
);
464 pid_element_open
= 1;
470 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
477 ret
= mi_lttng_writer_close_element(writer
);
482 /* Close domain, domains */
483 ret
= mi_lttng_close_multi_element(writer
, 2);
491 static int list_agent_events(void)
493 int i
, size
, ret
= CMD_SUCCESS
;
494 struct lttng_domain domain
;
495 struct lttng_handle
*handle
= NULL
;
496 struct lttng_event
*event_list
= NULL
;
498 char *cmdline
= NULL
;
499 const char *agent_domain_str
;
501 memset(&domain
, 0, sizeof(domain
));
503 domain
.type
= LTTNG_DOMAIN_JUL
;
504 } else if (opt_log4j
) {
505 domain
.type
= LTTNG_DOMAIN_LOG4J
;
506 } else if (opt_python
) {
507 domain
.type
= LTTNG_DOMAIN_PYTHON
;
509 ERR("Invalid agent domain selected.");
514 agent_domain_str
= get_domain_str(domain
.type
);
516 DBG("Getting %s tracing events", agent_domain_str
);
518 handle
= lttng_create_handle(NULL
, &domain
);
519 if (handle
== NULL
) {
524 size
= lttng_list_tracepoints(handle
, &event_list
);
526 ERR("Unable to list %s events: %s", agent_domain_str
,
527 lttng_strerror(size
));
534 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
541 MSG("%s events (Logger name):\n-------------------------",
548 for (i
= 0; i
< size
; i
++) {
549 if (cur_pid
!= event_list
[i
].pid
) {
550 cur_pid
= event_list
[i
].pid
;
551 cmdline
= get_cmdline_by_pid(cur_pid
);
552 if (cmdline
== NULL
) {
556 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
559 MSG("%s- %s", indent6
, event_list
[i
].name
);
568 lttng_destroy_handle(handle
);
573 * Ask session daemon for all user space tracepoints available.
575 static int list_ust_events(void)
577 int i
, size
, ret
= CMD_SUCCESS
;
578 struct lttng_domain domain
;
579 struct lttng_handle
*handle
;
580 struct lttng_event
*event_list
= NULL
;
582 char *cmdline
= NULL
;
584 memset(&domain
, 0, sizeof(domain
));
586 DBG("Getting UST tracing events");
588 domain
.type
= LTTNG_DOMAIN_UST
;
590 handle
= lttng_create_handle(NULL
, &domain
);
591 if (handle
== NULL
) {
596 size
= lttng_list_tracepoints(handle
, &event_list
);
598 ERR("Unable to list UST events: %s", lttng_strerror(size
));
605 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
608 MSG("UST events:\n-------------");
614 for (i
= 0; i
< size
; i
++) {
615 if (cur_pid
!= event_list
[i
].pid
) {
616 cur_pid
= event_list
[i
].pid
;
617 cmdline
= get_cmdline_by_pid(cur_pid
);
618 if (cmdline
== NULL
) {
622 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
625 print_events(&event_list
[i
]);
634 lttng_destroy_handle(handle
);
640 * List all ust event with their fields
642 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
643 struct lttng_domain
*domain
)
647 char *cmdline
= NULL
;
648 int pid_element_open
= 0;
649 int event_element_open
= 0;
650 struct lttng_event cur_event
;
652 /* Open domains element */
653 ret
= mi_lttng_domains_open(writer
);
659 ret
= mi_lttng_domain(writer
, domain
, 1);
664 /* Open pids element */
665 ret
= mi_lttng_pids_open(writer
);
670 for (i
= 0; i
< count
; i
++) {
671 if (cur_pid
!= fields
[i
].event
.pid
) {
672 if (pid_element_open
) {
673 if (event_element_open
) {
675 /* Close the previous field element and event. */
676 ret
= mi_lttng_close_multi_element(writer
, 2);
680 event_element_open
= 0;
682 /* Close the previous events, pid element */
683 ret
= mi_lttng_close_multi_element(writer
, 2);
687 pid_element_open
= 0;
690 cur_pid
= fields
[i
].event
.pid
;
691 cmdline
= get_cmdline_by_pid(cur_pid
);
692 if (!pid_element_open
) {
693 /* Open and write a pid element */
694 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
699 /* Open events element */
700 ret
= mi_lttng_events_open(writer
);
704 pid_element_open
= 1;
707 /* Wipe current event since we are about to print a new PID. */
708 memset(&cur_event
, 0, sizeof(cur_event
));
711 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
712 if (event_element_open
) {
713 /* Close the previous fields element and the previous event */
714 ret
= mi_lttng_close_multi_element(writer
, 2);
718 event_element_open
= 0;
721 memcpy(&cur_event
, &fields
[i
].event
,
724 if (!event_element_open
) {
725 /* Open and write the event */
726 ret
= mi_lttng_event(writer
, &cur_event
, 1,
727 handle
->domain
.type
);
732 /* Open a fields element */
733 ret
= mi_lttng_event_fields_open(writer
);
737 event_element_open
= 1;
741 /* Print the event_field */
742 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
748 /* Close pid, domain, domains */
749 ret
= mi_lttng_close_multi_element(writer
, 3);
758 * Ask session daemon for all user space tracepoint fields available.
760 static int list_ust_event_fields(void)
762 int i
, size
, ret
= CMD_SUCCESS
;
763 struct lttng_domain domain
;
764 struct lttng_handle
*handle
;
765 struct lttng_event_field
*event_field_list
;
767 char *cmdline
= NULL
;
769 struct lttng_event cur_event
;
771 memset(&domain
, 0, sizeof(domain
));
772 memset(&cur_event
, 0, sizeof(cur_event
));
774 DBG("Getting UST tracing event fields");
776 domain
.type
= LTTNG_DOMAIN_UST
;
778 handle
= lttng_create_handle(NULL
, &domain
);
779 if (handle
== NULL
) {
784 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
786 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
793 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
800 MSG("UST events:\n-------------");
806 for (i
= 0; i
< size
; i
++) {
807 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
808 cur_pid
= event_field_list
[i
].event
.pid
;
809 cmdline
= get_cmdline_by_pid(cur_pid
);
810 if (cmdline
== NULL
) {
814 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
816 /* Wipe current event since we are about to print a new PID. */
817 memset(&cur_event
, 0, sizeof(cur_event
));
819 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
820 print_events(&event_field_list
[i
].event
);
821 memcpy(&cur_event
, &event_field_list
[i
].event
,
824 print_event_field(&event_field_list
[i
]);
831 free(event_field_list
);
833 lttng_destroy_handle(handle
);
839 * Print a list of kernel events
841 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
842 struct lttng_domain
*domain
)
846 /* Open domains element */
847 ret
= mi_lttng_domains_open(writer
);
853 ret
= mi_lttng_domain(writer
, domain
, 1);
859 ret
= mi_lttng_events_open(writer
);
864 for (i
= 0; i
< count
; i
++) {
865 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
871 /* close events, domain and domains */
872 ret
= mi_lttng_close_multi_element(writer
, 3);
882 * Ask for all trace events in the kernel
884 static int list_kernel_events(void)
886 int i
, size
, ret
= CMD_SUCCESS
;
887 struct lttng_domain domain
;
888 struct lttng_handle
*handle
;
889 struct lttng_event
*event_list
;
891 memset(&domain
, 0, sizeof(domain
));
893 DBG("Getting kernel tracing events");
895 domain
.type
= LTTNG_DOMAIN_KERNEL
;
897 handle
= lttng_create_handle(NULL
, &domain
);
898 if (handle
== NULL
) {
903 size
= lttng_list_tracepoints(handle
, &event_list
);
905 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
906 lttng_destroy_handle(handle
);
912 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
918 MSG("Kernel events:\n-------------");
920 for (i
= 0; i
< size
; i
++) {
921 print_events(&event_list
[i
]);
930 lttng_destroy_handle(handle
);
934 lttng_destroy_handle(handle
);
940 * Print a list of system calls.
942 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
947 ret
= mi_lttng_events_open(writer
);
952 for (i
= 0; i
< count
; i
++) {
953 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
960 ret
= mi_lttng_writer_close_element(writer
);
970 * Ask for kernel system calls.
972 static int list_syscalls(void)
974 int i
, size
, ret
= CMD_SUCCESS
;
975 struct lttng_event
*event_list
;
977 DBG("Getting kernel system call events");
979 size
= lttng_list_syscalls(&event_list
);
981 ERR("Unable to list system calls: %s", lttng_strerror(size
));
988 ret
= mi_list_syscalls(event_list
, size
);
994 MSG("System calls:\n-------------");
996 for (i
= 0; i
< size
; i
++) {
997 print_events(&event_list
[i
]);
1013 * Print a list of agent events
1015 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1019 /* Open events element */
1020 ret
= mi_lttng_events_open(writer
);
1025 for (i
= 0; i
< count
; i
++) {
1026 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1032 /* Close events element */
1033 ret
= mi_lttng_writer_close_element(writer
);
1040 * List agent events for a specific session using the handle.
1042 * Return CMD_SUCCESS on success else a negative value.
1044 static int list_session_agent_events(void)
1046 int ret
= CMD_SUCCESS
, count
, i
;
1047 struct lttng_event
*events
= NULL
;
1049 count
= lttng_list_events(handle
, "", &events
);
1052 ERR("%s", lttng_strerror(count
));
1058 ret
= mi_list_session_agent_events(events
, count
);
1065 MSG("Events (Logger name):\n---------------------");
1067 MSG("%sNone\n", indent6
);
1071 for (i
= 0; i
< count
; i
++) {
1072 const char *filter_str
;
1073 char *filter_msg
= NULL
;
1074 struct lttng_event
*event
= &events
[i
];
1076 ret
= lttng_event_get_filter_expression(event
,
1079 filter_msg
= strdup(" [failed to retrieve filter]");
1080 } else if (filter_str
) {
1081 const char * const filter_fmt
=
1084 filter_msg
= malloc(strlen(filter_str
) +
1085 strlen(filter_fmt
) + 1);
1087 sprintf(filter_msg
, filter_fmt
,
1092 if (event
->loglevel_type
!=
1093 LTTNG_EVENT_LOGLEVEL_ALL
) {
1094 MSG("%s- %s%s (loglevel%s %s)%s", indent4
,
1096 enabled_string(event
->enabled
),
1097 logleveltype_string(
1098 event
->loglevel_type
),
1099 mi_lttng_loglevel_string(
1101 handle
->domain
.type
),
1102 safe_string(filter_msg
));
1104 MSG("%s- %s%s%s", indent4
, event
->name
,
1105 enabled_string(event
->enabled
),
1106 safe_string(filter_msg
));
1122 * print a list of event
1124 static int mi_list_events(struct lttng_event
*events
, int count
)
1128 /* Open events element */
1129 ret
= mi_lttng_events_open(writer
);
1134 for (i
= 0; i
< count
; i
++) {
1135 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1141 /* Close events element */
1142 ret
= mi_lttng_writer_close_element(writer
);
1149 * List events of channel of session and domain.
1151 static int list_events(const char *channel_name
)
1153 int ret
= CMD_SUCCESS
, count
, i
;
1154 struct lttng_event
*events
= NULL
;
1156 count
= lttng_list_events(handle
, channel_name
, &events
);
1159 ERR("%s", lttng_strerror(count
));
1165 ret
= mi_list_events(events
, count
);
1172 MSG("\n%sEvents:", indent4
);
1174 MSG("%sNone\n", indent6
);
1178 for (i
= 0; i
< count
; i
++) {
1179 print_events(&events
[i
]);
1191 * Pretty print channel
1193 static void print_channel(struct lttng_channel
*channel
)
1196 uint64_t discarded_events
, lost_packets
;
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 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1214 MSG("%sAttributes:", indent4
);
1215 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1216 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1217 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1218 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1219 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1220 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1221 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1222 MSG("%sdiscarded events: %" PRIu64
, indent6
, discarded_events
);
1223 MSG("%slost packets: %" PRIu64
, indent6
, lost_packets
);
1224 switch (channel
->attr
.output
) {
1225 case LTTNG_EVENT_SPLICE
:
1226 MSG("%soutput: splice()", indent6
);
1228 case LTTNG_EVENT_MMAP
:
1229 MSG("%soutput: mmap()", indent6
);
1236 * Print a list of channel
1239 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1240 const char *channel_name
)
1243 unsigned int chan_found
= 0;
1245 /* Open channels element */
1246 ret
= mi_lttng_channels_open(writer
);
1251 for (i
= 0; i
< count
; i
++) {
1252 if (channel_name
!= NULL
) {
1253 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1260 /* Write channel element and leave it open */
1261 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1266 /* Listing events per channel */
1267 ret
= list_events(channels
[i
].name
);
1272 /* Closing the channel element we opened earlier */
1273 ret
= mi_lttng_writer_close_element(writer
);
1283 /* Close channels element */
1284 ret
= mi_lttng_writer_close_element(writer
);
1294 * List channel(s) of session and domain.
1296 * If channel_name is NULL, all channels are listed.
1298 static int list_channels(const char *channel_name
)
1300 int count
, i
, ret
= CMD_SUCCESS
;
1301 unsigned int chan_found
= 0;
1302 struct lttng_channel
*channels
= NULL
;
1304 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1306 count
= lttng_list_channels(handle
, &channels
);
1309 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1311 /* When printing mi this is not an error
1312 * but an empty channels element */
1316 WARN("No kernel channel");
1317 goto error_channels
;
1321 /* We had a real error */
1323 ERR("%s", lttng_strerror(count
));
1324 goto error_channels
;
1331 ret
= mi_list_channels(channels
, count
, channel_name
);
1339 MSG("Channels:\n-------------");
1342 for (i
= 0; i
< count
; i
++) {
1343 if (channel_name
!= NULL
) {
1344 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1350 print_channel(&channels
[i
]);
1352 /* Listing events per channel */
1353 ret
= list_events(channels
[i
].name
);
1363 if (!chan_found
&& channel_name
!= NULL
) {
1365 ERR("Channel %s not found", channel_name
);
1377 * List tracker PID(s) of session and domain.
1379 static int list_tracker_pids(void)
1386 ret
= lttng_list_tracker_pids(handle
,
1387 &enabled
, &pids
, &nr_pids
);
1393 _MSG("PID tracker: [");
1395 /* Mi tracker_pid element*/
1397 /* Open tracker_pid and targets elements */
1398 ret
= mi_lttng_pid_tracker_open(writer
);
1404 for (i
= 0; i
< nr_pids
; i
++) {
1408 _MSG(" %d", pids
[i
]);
1412 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1420 /* Mi close tracker_pid and targets */
1422 ret
= mi_lttng_close_multi_element(writer
,2);
1435 * List all tracker of a domain
1437 static int list_trackers(void)
1441 /* Trackers listing */
1443 ret
= mi_lttng_trackers_open(writer
);
1450 ret
= list_tracker_pids();
1456 /* Close trackers element */
1457 ret
= mi_lttng_writer_close_element(writer
);
1469 * Find the session with session_name as name
1470 * and print his informations.
1472 static int mi_list_session(const char *session_name
,
1473 struct lttng_session
*sessions
, int count
)
1476 unsigned int session_found
= 0;
1478 if (session_name
== NULL
) {
1479 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1483 for (i
= 0; i
< count
; i
++) {
1484 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1485 /* We need to leave it open to append other informations
1486 * like domain, channel, events etc.*/
1488 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1496 if (!session_found
) {
1497 ERR("Session '%s' not found", session_name
);
1498 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1508 * List all availables session
1510 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1514 /* Opening sessions element */
1515 ret
= mi_lttng_sessions_open(writer
);
1520 /* Listing sessions */
1521 for (i
= 0; i
< count
; i
++) {
1522 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1528 /* Closing sessions element */
1529 ret
= mi_lttng_writer_close_element(writer
);
1539 * List available tracing session. List only basic information.
1541 * If session_name is NULL, all sessions are listed.
1543 static int list_sessions(const char *session_name
)
1545 int ret
= CMD_SUCCESS
;
1547 unsigned int session_found
= 0;
1548 struct lttng_session
*sessions
;
1550 count
= lttng_list_sessions(&sessions
);
1551 DBG("Session count %d", count
);
1554 ERR("%s", lttng_strerror(count
));
1560 if (session_name
== NULL
) {
1561 /* List all session */
1562 ret
= mi_list_sessions(sessions
, count
);
1564 /* Note : this return an open session element */
1565 ret
= mi_list_session(session_name
, sessions
, count
);
1574 MSG("Currently no available tracing session");
1578 if (session_name
== NULL
) {
1579 MSG("Available tracing sessions:");
1583 for (i
= 0; i
< count
; i
++) {
1584 if (session_name
!= NULL
) {
1585 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1587 MSG("Tracing session %s: [%s%s]", session_name
,
1588 active_string(sessions
[i
].enabled
),
1589 snapshot_string(sessions
[i
].snapshot_mode
));
1590 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1594 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1595 sessions
[i
].name
, sessions
[i
].path
,
1596 active_string(sessions
[i
].enabled
),
1597 snapshot_string(sessions
[i
].snapshot_mode
));
1598 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1599 if (sessions
[i
].live_timer_interval
!= 0) {
1600 MSG("%sLive timer interval (usec): %u", indent4
,
1601 sessions
[i
].live_timer_interval
);
1607 if (!session_found
&& session_name
!= NULL
) {
1608 ERR("Session '%s' not found", session_name
);
1613 if (session_name
== NULL
) {
1614 MSG("\nUse lttng list <session_name> for more details");
1627 * list available domain(s) for a session.
1629 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1632 /* Open domains element */
1633 ret
= mi_lttng_domains_open(writer
);
1638 for (i
= 0; i
< count
; i
++) {
1639 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1645 /* Closing domains element */
1646 ret
= mi_lttng_writer_close_element(writer
);
1655 * List available domain(s) for a session.
1657 static int list_domains(const char *session_name
)
1659 int i
, count
, ret
= CMD_SUCCESS
;
1660 struct lttng_domain
*domains
= NULL
;
1663 count
= lttng_list_domains(session_name
, &domains
);
1666 ERR("%s", lttng_strerror(count
));
1672 ret
= mi_list_domains(domains
, count
);
1679 MSG("Domains:\n-------------");
1685 for (i
= 0; i
< count
; i
++) {
1686 switch (domains
[i
].type
) {
1687 case LTTNG_DOMAIN_KERNEL
:
1690 case LTTNG_DOMAIN_UST
:
1691 MSG(" - UST global");
1693 case LTTNG_DOMAIN_JUL
:
1694 MSG(" - JUL (Java Util Logging)");
1696 case LTTNG_DOMAIN_LOG4J
:
1697 MSG(" - LOG4j (Logging for Java)");
1699 case LTTNG_DOMAIN_PYTHON
:
1700 MSG(" - Python (logging)");
1716 * The 'list <options>' first level command
1718 int cmd_list(int argc
, const char **argv
)
1720 int opt
, ret
= CMD_SUCCESS
;
1721 const char *session_name
;
1722 static poptContext pc
;
1723 struct lttng_domain domain
;
1724 struct lttng_domain
*domains
= NULL
;
1726 memset(&domain
, 0, sizeof(domain
));
1734 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1735 poptReadDefaultConfig(pc
, 0);
1737 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1745 case OPT_LIST_OPTIONS
:
1746 list_cmd_options(stdout
, long_options
);
1750 ret
= CMD_UNDEFINED
;
1757 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1763 /* Open command element */
1764 ret
= mi_lttng_writer_command_open(writer
,
1765 mi_lttng_element_command_list
);
1771 /* Open output element */
1772 ret
= mi_lttng_writer_open_element(writer
,
1773 mi_lttng_element_command_output
);
1780 /* Get session name (trailing argument) */
1781 session_name
= poptGetArg(pc
);
1782 DBG2("Session name: %s", session_name
);
1785 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1786 } else if (opt_userspace
) {
1787 DBG2("Listing userspace global domain");
1788 domain
.type
= LTTNG_DOMAIN_UST
;
1789 } else if (opt_jul
) {
1790 DBG2("Listing JUL domain");
1791 domain
.type
= LTTNG_DOMAIN_JUL
;
1792 } else if (opt_log4j
) {
1793 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1794 } else if (opt_python
) {
1795 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1798 if (!opt_kernel
&& opt_syscall
) {
1799 WARN("--syscall will only work with the Kernel domain (-k)");
1804 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1805 handle
= lttng_create_handle(session_name
, &domain
);
1806 if (handle
== NULL
) {
1812 if (session_name
== NULL
) {
1813 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1815 ret
= list_sessions(NULL
);
1822 ret
= list_syscalls();
1827 ret
= list_kernel_events();
1833 if (opt_userspace
) {
1835 ret
= list_ust_event_fields();
1837 ret
= list_ust_events();
1843 if (opt_jul
|| opt_log4j
|| opt_python
) {
1844 ret
= list_agent_events();
1850 /* List session attributes */
1852 /* Open element sessions
1853 * Present for xml consistency */
1854 ret
= mi_lttng_sessions_open(writer
);
1859 /* MI: the ouptut of list_sessions is an unclosed session element */
1860 ret
= list_sessions(session_name
);
1865 /* Domain listing */
1867 ret
= list_domains(session_name
);
1871 /* Channel listing */
1872 if (opt_kernel
|| opt_userspace
) {
1874 /* Add of domains and domain element for xml
1875 * consistency and validation
1877 ret
= mi_lttng_domains_open(writer
);
1882 /* Open domain and leave it open for
1883 * nested channels printing */
1884 ret
= mi_lttng_domain(writer
, &domain
, 1);
1893 ret
= list_trackers();
1899 ret
= list_channels(opt_channel
);
1905 /* Close domain and domain element */
1906 ret
= mi_lttng_close_multi_element(writer
, 2);
1916 /* We want all domain(s) */
1917 nb_domain
= lttng_list_domains(session_name
, &domains
);
1918 if (nb_domain
< 0) {
1920 ERR("%s", lttng_strerror(nb_domain
));
1925 ret
= mi_lttng_domains_open(writer
);
1932 for (i
= 0; i
< nb_domain
; i
++) {
1933 switch (domains
[i
].type
) {
1934 case LTTNG_DOMAIN_KERNEL
:
1935 MSG("=== Domain: Kernel ===\n");
1937 case LTTNG_DOMAIN_UST
:
1938 MSG("=== Domain: UST global ===\n");
1939 MSG("Buffer type: %s\n",
1940 domains
[i
].buf_type
==
1941 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1943 case LTTNG_DOMAIN_JUL
:
1944 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1946 case LTTNG_DOMAIN_LOG4J
:
1947 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1949 case LTTNG_DOMAIN_PYTHON
:
1950 MSG("=== Domain: Python (logging) ===\n");
1953 MSG("=== Domain: Unimplemented ===\n");
1958 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1965 /* Clean handle before creating a new one */
1967 lttng_destroy_handle(handle
);
1970 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1971 if (handle
== NULL
) {
1976 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1977 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
1978 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
1979 ret
= list_session_agent_events();
1987 switch (domains
[i
].type
) {
1988 case LTTNG_DOMAIN_KERNEL
:
1989 case LTTNG_DOMAIN_UST
:
1990 ret
= list_trackers();
1999 ret
= list_channels(opt_channel
);
2006 /* Close domain element */
2007 ret
= mi_lttng_writer_close_element(writer
);
2016 /* Close the domains, session and sessions element */
2017 ret
= mi_lttng_close_multi_element(writer
, 3);
2028 /* Close output element */
2029 ret
= mi_lttng_writer_close_element(writer
);
2035 /* Command element close */
2036 ret
= mi_lttng_writer_command_close(writer
);
2044 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2045 /* Preserve original error code */
2046 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2051 lttng_destroy_handle(handle
);
2054 poptFreeContext(pc
);