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
;
216 const char * const exclusion_fmt
= " [exclusions: ";
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 strlen(exclusion_fmt
) + 1);
238 if (!exclusion_msg
) {
243 count
= sprintf(at
, exclusion_fmt
);
246 for (i
= 0; i
< exclusion_count
; ++i
) {
249 /* Append comma between exclusion names */
255 ret
= lttng_event_get_exclusion_name(event
, i
, &name
);
257 /* Prints '?' on local error; should never happen */
263 /* Append exclusion name */
264 count
= sprintf(at
, "%s", name
);
268 /* This also puts a final '\0' at the end of exclusion_msg */
272 return exclusion_msg
;
276 * Pretty print single event.
278 static void print_events(struct lttng_event
*event
)
281 const char *filter_str
;
282 char *filter_msg
= NULL
;
283 char *exclusion_msg
= NULL
;
285 ret
= lttng_event_get_filter_string(event
, &filter_str
);
288 filter_msg
= strdup(" [failed to retrieve filter]");
289 } else if (filter_str
) {
290 const char * const filter_fmt
= " [filter: '%s']";
292 filter_msg
= malloc(strlen(filter_str
) +
293 strlen(filter_fmt
) + 1);
295 sprintf(filter_msg
, filter_fmt
,
300 exclusion_msg
= get_exclusion_names_msg(event
);
301 if (!exclusion_msg
) {
302 exclusion_msg
= strdup(" [failed to retrieve exclusions]");
305 switch (event
->type
) {
306 case LTTNG_EVENT_TRACEPOINT
:
308 if (event
->loglevel
!= -1) {
309 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
312 logleveltype_string(event
->loglevel_type
),
313 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
315 enabled_string(event
->enabled
),
316 safe_string(exclusion_msg
),
317 safe_string(filter_msg
));
319 MSG("%s%s (type: tracepoint)%s%s%s",
322 enabled_string(event
->enabled
),
323 safe_string(exclusion_msg
),
324 safe_string(filter_msg
));
328 case LTTNG_EVENT_FUNCTION
:
329 MSG("%s%s (type: function)%s%s", indent6
,
330 event
->name
, enabled_string(event
->enabled
),
331 safe_string(filter_msg
));
332 if (event
->attr
.probe
.addr
!= 0) {
333 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
335 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
336 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
339 case LTTNG_EVENT_PROBE
:
340 MSG("%s%s (type: probe)%s%s", indent6
,
341 event
->name
, enabled_string(event
->enabled
),
342 safe_string(filter_msg
));
343 if (event
->attr
.probe
.addr
!= 0) {
344 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
346 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
347 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
350 case LTTNG_EVENT_FUNCTION_ENTRY
:
351 MSG("%s%s (type: function)%s%s", indent6
,
352 event
->name
, enabled_string(event
->enabled
),
353 safe_string(filter_msg
));
354 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
356 case LTTNG_EVENT_SYSCALL
:
357 MSG("%s%s%s%s%s", indent6
, event
->name
,
358 (opt_syscall
? "" : " (type:syscall)"),
359 enabled_string(event
->enabled
),
360 bitness_event(event
->flags
));
362 case LTTNG_EVENT_NOOP
:
363 MSG("%s (type: noop)%s%s", indent6
,
364 enabled_string(event
->enabled
),
365 safe_string(filter_msg
));
367 case LTTNG_EVENT_ALL
:
368 /* We should never have "all" events in list. */
377 static const char *field_type(struct lttng_event_field
*field
)
379 switch(field
->type
) {
380 case LTTNG_EVENT_FIELD_INTEGER
:
382 case LTTNG_EVENT_FIELD_ENUM
:
384 case LTTNG_EVENT_FIELD_FLOAT
:
386 case LTTNG_EVENT_FIELD_STRING
:
388 case LTTNG_EVENT_FIELD_OTHER
:
389 default: /* fall-through */
395 * Pretty print single event fields.
397 static void print_event_field(struct lttng_event_field
*field
)
399 if (!field
->field_name
[0]) {
402 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
403 field_type(field
), field
->nowrite
? " [no write]" : "");
408 * Jul and ust event listing
410 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
411 struct lttng_domain
*domain
)
415 char *cmdline
= NULL
;
416 int pid_element_open
= 0;
418 /* Open domains element */
419 ret
= mi_lttng_domains_open(writer
);
425 ret
= mi_lttng_domain(writer
, domain
, 1);
430 /* Open pids element element */
431 ret
= mi_lttng_pids_open(writer
);
436 for (i
= 0; i
< count
; i
++) {
437 if (cur_pid
!= events
[i
].pid
) {
438 if (pid_element_open
) {
439 /* Close the previous events and pid element */
440 ret
= mi_lttng_close_multi_element(writer
, 2);
444 pid_element_open
= 0;
447 cur_pid
= events
[i
].pid
;
448 cmdline
= get_cmdline_by_pid(cur_pid
);
454 if (!pid_element_open
) {
455 /* Open and write a pid element */
456 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
461 /* Open events element */
462 ret
= mi_lttng_events_open(writer
);
467 pid_element_open
= 1;
473 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
480 ret
= mi_lttng_writer_close_element(writer
);
485 /* Close domain, domains */
486 ret
= mi_lttng_close_multi_element(writer
, 2);
494 static int list_agent_events(void)
496 int i
, size
, ret
= CMD_SUCCESS
;
497 struct lttng_domain domain
;
498 struct lttng_handle
*handle
= NULL
;
499 struct lttng_event
*event_list
= NULL
;
501 char *cmdline
= NULL
;
502 const char *agent_domain_str
;
504 memset(&domain
, 0, sizeof(domain
));
506 domain
.type
= LTTNG_DOMAIN_JUL
;
507 } else if (opt_log4j
) {
508 domain
.type
= LTTNG_DOMAIN_LOG4J
;
509 } else if (opt_python
) {
510 domain
.type
= LTTNG_DOMAIN_PYTHON
;
512 ERR("Invalid agent domain selected.");
517 agent_domain_str
= get_domain_str(domain
.type
);
519 DBG("Getting %s tracing events", agent_domain_str
);
521 handle
= lttng_create_handle(NULL
, &domain
);
522 if (handle
== NULL
) {
527 size
= lttng_list_tracepoints(handle
, &event_list
);
529 ERR("Unable to list %s events: %s", agent_domain_str
,
530 lttng_strerror(size
));
537 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
544 MSG("%s events (Logger name):\n-------------------------",
551 for (i
= 0; i
< size
; i
++) {
552 if (cur_pid
!= event_list
[i
].pid
) {
553 cur_pid
= event_list
[i
].pid
;
554 cmdline
= get_cmdline_by_pid(cur_pid
);
555 if (cmdline
== NULL
) {
559 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
562 MSG("%s- %s", indent6
, event_list
[i
].name
);
571 lttng_destroy_handle(handle
);
576 * Ask session daemon for all user space tracepoints available.
578 static int list_ust_events(void)
580 int i
, size
, ret
= CMD_SUCCESS
;
581 struct lttng_domain domain
;
582 struct lttng_handle
*handle
;
583 struct lttng_event
*event_list
= NULL
;
585 char *cmdline
= NULL
;
587 memset(&domain
, 0, sizeof(domain
));
589 DBG("Getting UST tracing events");
591 domain
.type
= LTTNG_DOMAIN_UST
;
593 handle
= lttng_create_handle(NULL
, &domain
);
594 if (handle
== NULL
) {
599 size
= lttng_list_tracepoints(handle
, &event_list
);
601 ERR("Unable to list UST events: %s", lttng_strerror(size
));
608 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
611 MSG("UST events:\n-------------");
617 for (i
= 0; i
< size
; i
++) {
618 if (cur_pid
!= event_list
[i
].pid
) {
619 cur_pid
= event_list
[i
].pid
;
620 cmdline
= get_cmdline_by_pid(cur_pid
);
621 if (cmdline
== NULL
) {
625 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
628 print_events(&event_list
[i
]);
637 lttng_destroy_handle(handle
);
643 * List all ust event with their fields
645 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
646 struct lttng_domain
*domain
)
650 char *cmdline
= NULL
;
651 int pid_element_open
= 0;
652 int event_element_open
= 0;
653 struct lttng_event cur_event
;
655 /* Open domains element */
656 ret
= mi_lttng_domains_open(writer
);
662 ret
= mi_lttng_domain(writer
, domain
, 1);
667 /* Open pids element */
668 ret
= mi_lttng_pids_open(writer
);
673 for (i
= 0; i
< count
; i
++) {
674 if (cur_pid
!= fields
[i
].event
.pid
) {
675 if (pid_element_open
) {
676 if (event_element_open
) {
678 /* Close the previous field element and event. */
679 ret
= mi_lttng_close_multi_element(writer
, 2);
683 event_element_open
= 0;
685 /* Close the previous events, pid element */
686 ret
= mi_lttng_close_multi_element(writer
, 2);
690 pid_element_open
= 0;
693 cur_pid
= fields
[i
].event
.pid
;
694 cmdline
= get_cmdline_by_pid(cur_pid
);
695 if (!pid_element_open
) {
696 /* Open and write a pid element */
697 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
702 /* Open events element */
703 ret
= mi_lttng_events_open(writer
);
707 pid_element_open
= 1;
710 /* Wipe current event since we are about to print a new PID. */
711 memset(&cur_event
, 0, sizeof(cur_event
));
714 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
715 if (event_element_open
) {
716 /* Close the previous fields element and the previous event */
717 ret
= mi_lttng_close_multi_element(writer
, 2);
721 event_element_open
= 0;
724 memcpy(&cur_event
, &fields
[i
].event
,
727 if (!event_element_open
) {
728 /* Open and write the event */
729 ret
= mi_lttng_event(writer
, &cur_event
, 1,
730 handle
->domain
.type
);
735 /* Open a fields element */
736 ret
= mi_lttng_event_fields_open(writer
);
740 event_element_open
= 1;
744 /* Print the event_field */
745 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
751 /* Close pid, domain, domains */
752 ret
= mi_lttng_close_multi_element(writer
, 3);
761 * Ask session daemon for all user space tracepoint fields available.
763 static int list_ust_event_fields(void)
765 int i
, size
, ret
= CMD_SUCCESS
;
766 struct lttng_domain domain
;
767 struct lttng_handle
*handle
;
768 struct lttng_event_field
*event_field_list
;
770 char *cmdline
= NULL
;
772 struct lttng_event cur_event
;
774 memset(&domain
, 0, sizeof(domain
));
775 memset(&cur_event
, 0, sizeof(cur_event
));
777 DBG("Getting UST tracing event fields");
779 domain
.type
= LTTNG_DOMAIN_UST
;
781 handle
= lttng_create_handle(NULL
, &domain
);
782 if (handle
== NULL
) {
787 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
789 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
796 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
803 MSG("UST events:\n-------------");
809 for (i
= 0; i
< size
; i
++) {
810 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
811 cur_pid
= event_field_list
[i
].event
.pid
;
812 cmdline
= get_cmdline_by_pid(cur_pid
);
813 if (cmdline
== NULL
) {
817 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
819 /* Wipe current event since we are about to print a new PID. */
820 memset(&cur_event
, 0, sizeof(cur_event
));
822 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
823 print_events(&event_field_list
[i
].event
);
824 memcpy(&cur_event
, &event_field_list
[i
].event
,
827 print_event_field(&event_field_list
[i
]);
834 free(event_field_list
);
836 lttng_destroy_handle(handle
);
842 * Print a list of kernel events
844 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
845 struct lttng_domain
*domain
)
849 /* Open domains element */
850 ret
= mi_lttng_domains_open(writer
);
856 ret
= mi_lttng_domain(writer
, domain
, 1);
862 ret
= mi_lttng_events_open(writer
);
867 for (i
= 0; i
< count
; i
++) {
868 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
874 /* close events, domain and domains */
875 ret
= mi_lttng_close_multi_element(writer
, 3);
885 * Ask for all trace events in the kernel
887 static int list_kernel_events(void)
889 int i
, size
, ret
= CMD_SUCCESS
;
890 struct lttng_domain domain
;
891 struct lttng_handle
*handle
;
892 struct lttng_event
*event_list
;
894 memset(&domain
, 0, sizeof(domain
));
896 DBG("Getting kernel tracing events");
898 domain
.type
= LTTNG_DOMAIN_KERNEL
;
900 handle
= lttng_create_handle(NULL
, &domain
);
901 if (handle
== NULL
) {
906 size
= lttng_list_tracepoints(handle
, &event_list
);
908 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
909 lttng_destroy_handle(handle
);
915 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
921 MSG("Kernel events:\n-------------");
923 for (i
= 0; i
< size
; i
++) {
924 print_events(&event_list
[i
]);
933 lttng_destroy_handle(handle
);
937 lttng_destroy_handle(handle
);
943 * Print a list of system calls.
945 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
950 ret
= mi_lttng_events_open(writer
);
955 for (i
= 0; i
< count
; i
++) {
956 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
963 ret
= mi_lttng_writer_close_element(writer
);
973 * Ask for kernel system calls.
975 static int list_syscalls(void)
977 int i
, size
, ret
= CMD_SUCCESS
;
978 struct lttng_event
*event_list
;
980 DBG("Getting kernel system call events");
982 size
= lttng_list_syscalls(&event_list
);
984 ERR("Unable to list system calls: %s", lttng_strerror(size
));
991 ret
= mi_list_syscalls(event_list
, size
);
997 MSG("System calls:\n-------------");
999 for (i
= 0; i
< size
; i
++) {
1000 print_events(&event_list
[i
]);
1016 * Print a list of agent events
1018 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1022 /* Open events element */
1023 ret
= mi_lttng_events_open(writer
);
1028 for (i
= 0; i
< count
; i
++) {
1029 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1035 /* Close events element */
1036 ret
= mi_lttng_writer_close_element(writer
);
1043 * List agent events for a specific session using the handle.
1045 * Return CMD_SUCCESS on success else a negative value.
1047 static int list_session_agent_events(void)
1049 int ret
= CMD_SUCCESS
, count
, i
;
1050 struct lttng_event
*events
= NULL
;
1052 count
= lttng_list_events(handle
, "", &events
);
1055 ERR("%s", lttng_strerror(count
));
1061 ret
= mi_list_session_agent_events(events
, count
);
1068 MSG("Events (Logger name):\n---------------------");
1070 MSG("%sNone\n", indent6
);
1074 for (i
= 0; i
< count
; i
++) {
1075 const char *filter_str
;
1076 char *filter_msg
= NULL
;
1077 struct lttng_event
*event
= &events
[i
];
1079 ret
= lttng_event_get_filter_string(event
, &filter_str
);
1081 filter_msg
= strdup(" [failed to retrieve filter]");
1082 } else if (filter_str
) {
1083 const char * const filter_fmt
=
1086 filter_msg
= malloc(strlen(filter_str
) +
1087 strlen(filter_fmt
) + 1);
1089 sprintf(filter_msg
, " [filter: '%s']",
1094 if (event
->loglevel_type
!=
1095 LTTNG_EVENT_LOGLEVEL_ALL
) {
1096 MSG("%s- %s%s (loglevel%s %s)%s", indent4
,
1098 enabled_string(event
->enabled
),
1099 logleveltype_string(
1100 event
->loglevel_type
),
1101 mi_lttng_loglevel_string(
1103 handle
->domain
.type
),
1104 safe_string(filter_msg
));
1106 MSG("%s- %s%s%s", indent4
, event
->name
,
1107 enabled_string(event
->enabled
),
1108 safe_string(filter_msg
));
1124 * print a list of event
1126 static int mi_list_events(struct lttng_event
*events
, int count
)
1130 /* Open events element */
1131 ret
= mi_lttng_events_open(writer
);
1136 for (i
= 0; i
< count
; i
++) {
1137 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1143 /* Close events element */
1144 ret
= mi_lttng_writer_close_element(writer
);
1151 * List events of channel of session and domain.
1153 static int list_events(const char *channel_name
)
1155 int ret
= CMD_SUCCESS
, count
, i
;
1156 struct lttng_event
*events
= NULL
;
1158 count
= lttng_list_events(handle
, channel_name
, &events
);
1161 ERR("%s", lttng_strerror(count
));
1167 ret
= mi_list_events(events
, count
);
1174 MSG("\n%sEvents:", indent4
);
1176 MSG("%sNone\n", indent6
);
1180 for (i
= 0; i
< count
; i
++) {
1181 print_events(&events
[i
]);
1193 * Pretty print channel
1195 static void print_channel(struct lttng_channel
*channel
)
1197 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1199 MSG("%sAttributes:", indent4
);
1200 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1201 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1202 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1203 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1204 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1205 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1206 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1207 switch (channel
->attr
.output
) {
1208 case LTTNG_EVENT_SPLICE
:
1209 MSG("%soutput: splice()", indent6
);
1211 case LTTNG_EVENT_MMAP
:
1212 MSG("%soutput: mmap()", indent6
);
1219 * Print a list of channel
1222 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1223 const char *channel_name
)
1226 unsigned int chan_found
= 0;
1228 /* Open channels element */
1229 ret
= mi_lttng_channels_open(writer
);
1234 for (i
= 0; i
< count
; i
++) {
1235 if (channel_name
!= NULL
) {
1236 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1243 /* Write channel element and leave it open */
1244 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1249 /* Listing events per channel */
1250 ret
= list_events(channels
[i
].name
);
1255 /* Closing the channel element we opened earlier */
1256 ret
= mi_lttng_writer_close_element(writer
);
1266 /* Close channels element */
1267 ret
= mi_lttng_writer_close_element(writer
);
1277 * List channel(s) of session and domain.
1279 * If channel_name is NULL, all channels are listed.
1281 static int list_channels(const char *channel_name
)
1283 int count
, i
, ret
= CMD_SUCCESS
;
1284 unsigned int chan_found
= 0;
1285 struct lttng_channel
*channels
= NULL
;
1287 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1289 count
= lttng_list_channels(handle
, &channels
);
1292 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1294 /* When printing mi this is not an error
1295 * but an empty channels element */
1299 WARN("No kernel channel");
1300 goto error_channels
;
1304 /* We had a real error */
1306 ERR("%s", lttng_strerror(count
));
1307 goto error_channels
;
1314 ret
= mi_list_channels(channels
, count
, channel_name
);
1322 MSG("Channels:\n-------------");
1325 for (i
= 0; i
< count
; i
++) {
1326 if (channel_name
!= NULL
) {
1327 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1333 print_channel(&channels
[i
]);
1335 /* Listing events per channel */
1336 ret
= list_events(channels
[i
].name
);
1346 if (!chan_found
&& channel_name
!= NULL
) {
1348 ERR("Channel %s not found", channel_name
);
1360 * List tracker PID(s) of session and domain.
1362 static int list_tracker_pids(void)
1369 ret
= lttng_list_tracker_pids(handle
,
1370 &enabled
, &pids
, &nr_pids
);
1376 _MSG("PID tracker: [");
1378 /* Mi tracker_pid element*/
1380 /* Open tracker_pid and targets elements */
1381 ret
= mi_lttng_pid_tracker_open(writer
);
1387 for (i
= 0; i
< nr_pids
; i
++) {
1391 _MSG(" %d", pids
[i
]);
1395 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1403 /* Mi close tracker_pid and targets */
1405 ret
= mi_lttng_close_multi_element(writer
,2);
1418 * List all tracker of a domain
1420 static int list_trackers(void)
1424 /* Trackers listing */
1426 ret
= mi_lttng_trackers_open(writer
);
1433 ret
= list_tracker_pids();
1439 /* Close trackers element */
1440 ret
= mi_lttng_writer_close_element(writer
);
1452 * Find the session with session_name as name
1453 * and print his informations.
1455 static int mi_list_session(const char *session_name
,
1456 struct lttng_session
*sessions
, int count
)
1459 unsigned int session_found
= 0;
1461 if (session_name
== NULL
) {
1462 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1466 for (i
= 0; i
< count
; i
++) {
1467 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1468 /* We need to leave it open to append other informations
1469 * like domain, channel, events etc.*/
1471 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1479 if (!session_found
) {
1480 ERR("Session '%s' not found", session_name
);
1481 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1491 * List all availables session
1493 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1497 /* Opening sessions element */
1498 ret
= mi_lttng_sessions_open(writer
);
1503 /* Listing sessions */
1504 for (i
= 0; i
< count
; i
++) {
1505 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1511 /* Closing sessions element */
1512 ret
= mi_lttng_writer_close_element(writer
);
1522 * List available tracing session. List only basic information.
1524 * If session_name is NULL, all sessions are listed.
1526 static int list_sessions(const char *session_name
)
1528 int ret
= CMD_SUCCESS
;
1530 unsigned int session_found
= 0;
1531 struct lttng_session
*sessions
;
1533 count
= lttng_list_sessions(&sessions
);
1534 DBG("Session count %d", count
);
1537 ERR("%s", lttng_strerror(count
));
1543 if (session_name
== NULL
) {
1544 /* List all session */
1545 ret
= mi_list_sessions(sessions
, count
);
1547 /* Note : this return an open session element */
1548 ret
= mi_list_session(session_name
, sessions
, count
);
1557 MSG("Currently no available tracing session");
1561 if (session_name
== NULL
) {
1562 MSG("Available tracing sessions:");
1566 for (i
= 0; i
< count
; i
++) {
1567 if (session_name
!= NULL
) {
1568 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1570 MSG("Tracing session %s: [%s%s]", session_name
,
1571 active_string(sessions
[i
].enabled
),
1572 snapshot_string(sessions
[i
].snapshot_mode
));
1573 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1577 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1578 sessions
[i
].name
, sessions
[i
].path
,
1579 active_string(sessions
[i
].enabled
),
1580 snapshot_string(sessions
[i
].snapshot_mode
));
1581 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1582 if (sessions
[i
].live_timer_interval
!= 0) {
1583 MSG("%sLive timer interval (usec): %u", indent4
,
1584 sessions
[i
].live_timer_interval
);
1590 if (!session_found
&& session_name
!= NULL
) {
1591 ERR("Session '%s' not found", session_name
);
1596 if (session_name
== NULL
) {
1597 MSG("\nUse lttng list <session_name> for more details");
1610 * list available domain(s) for a session.
1612 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1615 /* Open domains element */
1616 ret
= mi_lttng_domains_open(writer
);
1621 for (i
= 0; i
< count
; i
++) {
1622 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1628 /* Closing domains element */
1629 ret
= mi_lttng_writer_close_element(writer
);
1638 * List available domain(s) for a session.
1640 static int list_domains(const char *session_name
)
1642 int i
, count
, ret
= CMD_SUCCESS
;
1643 struct lttng_domain
*domains
= NULL
;
1646 count
= lttng_list_domains(session_name
, &domains
);
1649 ERR("%s", lttng_strerror(count
));
1655 ret
= mi_list_domains(domains
, count
);
1662 MSG("Domains:\n-------------");
1668 for (i
= 0; i
< count
; i
++) {
1669 switch (domains
[i
].type
) {
1670 case LTTNG_DOMAIN_KERNEL
:
1673 case LTTNG_DOMAIN_UST
:
1674 MSG(" - UST global");
1676 case LTTNG_DOMAIN_JUL
:
1677 MSG(" - JUL (Java Util Logging)");
1679 case LTTNG_DOMAIN_LOG4J
:
1680 MSG(" - LOG4j (Logging for Java)");
1682 case LTTNG_DOMAIN_PYTHON
:
1683 MSG(" - Python (logging)");
1699 * The 'list <options>' first level command
1701 int cmd_list(int argc
, const char **argv
)
1703 int opt
, ret
= CMD_SUCCESS
;
1704 const char *session_name
;
1705 static poptContext pc
;
1706 struct lttng_domain domain
;
1707 struct lttng_domain
*domains
= NULL
;
1709 memset(&domain
, 0, sizeof(domain
));
1717 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1718 poptReadDefaultConfig(pc
, 0);
1720 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1728 case OPT_LIST_OPTIONS
:
1729 list_cmd_options(stdout
, long_options
);
1733 ret
= CMD_UNDEFINED
;
1740 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1746 /* Open command element */
1747 ret
= mi_lttng_writer_command_open(writer
,
1748 mi_lttng_element_command_list
);
1754 /* Open output element */
1755 ret
= mi_lttng_writer_open_element(writer
,
1756 mi_lttng_element_command_output
);
1763 /* Get session name (trailing argument) */
1764 session_name
= poptGetArg(pc
);
1765 DBG2("Session name: %s", session_name
);
1768 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1769 } else if (opt_userspace
) {
1770 DBG2("Listing userspace global domain");
1771 domain
.type
= LTTNG_DOMAIN_UST
;
1772 } else if (opt_jul
) {
1773 DBG2("Listing JUL domain");
1774 domain
.type
= LTTNG_DOMAIN_JUL
;
1775 } else if (opt_log4j
) {
1776 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1777 } else if (opt_python
) {
1778 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1781 if (!opt_kernel
&& opt_syscall
) {
1782 WARN("--syscall will only work with the Kernel domain (-k)");
1787 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1788 handle
= lttng_create_handle(session_name
, &domain
);
1789 if (handle
== NULL
) {
1795 if (session_name
== NULL
) {
1796 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1798 ret
= list_sessions(NULL
);
1805 ret
= list_syscalls();
1810 ret
= list_kernel_events();
1816 if (opt_userspace
) {
1818 ret
= list_ust_event_fields();
1820 ret
= list_ust_events();
1826 if (opt_jul
|| opt_log4j
|| opt_python
) {
1827 ret
= list_agent_events();
1833 /* List session attributes */
1835 /* Open element sessions
1836 * Present for xml consistency */
1837 ret
= mi_lttng_sessions_open(writer
);
1842 /* MI: the ouptut of list_sessions is an unclosed session element */
1843 ret
= list_sessions(session_name
);
1848 /* Domain listing */
1850 ret
= list_domains(session_name
);
1854 /* Channel listing */
1855 if (opt_kernel
|| opt_userspace
) {
1857 /* Add of domains and domain element for xml
1858 * consistency and validation
1860 ret
= mi_lttng_domains_open(writer
);
1865 /* Open domain and leave it open for
1866 * nested channels printing */
1867 ret
= mi_lttng_domain(writer
, &domain
, 1);
1876 ret
= list_trackers();
1882 ret
= list_channels(opt_channel
);
1888 /* Close domain and domain element */
1889 ret
= mi_lttng_close_multi_element(writer
, 2);
1899 /* We want all domain(s) */
1900 nb_domain
= lttng_list_domains(session_name
, &domains
);
1901 if (nb_domain
< 0) {
1903 ERR("%s", lttng_strerror(nb_domain
));
1908 ret
= mi_lttng_domains_open(writer
);
1915 for (i
= 0; i
< nb_domain
; i
++) {
1916 switch (domains
[i
].type
) {
1917 case LTTNG_DOMAIN_KERNEL
:
1918 MSG("=== Domain: Kernel ===\n");
1920 case LTTNG_DOMAIN_UST
:
1921 MSG("=== Domain: UST global ===\n");
1922 MSG("Buffer type: %s\n",
1923 domains
[i
].buf_type
==
1924 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1926 case LTTNG_DOMAIN_JUL
:
1927 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1929 case LTTNG_DOMAIN_LOG4J
:
1930 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1932 case LTTNG_DOMAIN_PYTHON
:
1933 MSG("=== Domain: Python (logging) ===\n");
1936 MSG("=== Domain: Unimplemented ===\n");
1941 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1948 /* Clean handle before creating a new one */
1950 lttng_destroy_handle(handle
);
1953 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1954 if (handle
== NULL
) {
1959 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1960 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
1961 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
1962 ret
= list_session_agent_events();
1970 switch (domains
[i
].type
) {
1971 case LTTNG_DOMAIN_KERNEL
:
1972 case LTTNG_DOMAIN_UST
:
1973 ret
= list_trackers();
1982 ret
= list_channels(opt_channel
);
1989 /* Close domain element */
1990 ret
= mi_lttng_writer_close_element(writer
);
1999 /* Close the domains, session and sessions element */
2000 ret
= mi_lttng_close_multi_element(writer
, 3);
2011 /* Close output element */
2012 ret
= mi_lttng_writer_close_element(writer
);
2018 /* Command element close */
2019 ret
= mi_lttng_writer_command_close(writer
);
2027 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2028 /* Preserve original error code */
2029 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2034 lttng_destroy_handle(handle
);
2037 poptFreeContext(pc
);