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: ";
217 const size_t exclusion_fmt_len
= strlen(exclusion_fmt
);
219 exclusion_count
= lttng_event_get_exclusion_name_count(event
);
220 if (exclusion_count
< 0) {
222 } else if (exclusion_count
== 0) {
224 * No exclusions: return copy of empty string so that
225 * it can be freed by caller.
227 exclusion_msg
= strdup("");
232 * exclusion_msg's size is bounded by the exclusion_fmt string,
233 * a comma per entry, the entry count (fixed-size), a closing
234 * bracket, and a trailing \0.
236 exclusion_msg
= malloc(exclusion_count
+
237 exclusion_count
* LTTNG_SYMBOL_NAME_LEN
+
238 exclusion_fmt_len
+ 1);
239 if (!exclusion_msg
) {
243 at
= strcpy(exclusion_msg
, exclusion_fmt
) + exclusion_fmt_len
;
244 for (i
= 0; i
< exclusion_count
; ++i
) {
247 /* Append comma between exclusion names */
253 ret
= lttng_event_get_exclusion_name(event
, i
, &name
);
255 /* Prints '?' on local error; should never happen */
261 /* Append exclusion name */
262 count
= sprintf(at
, "%s", name
);
266 /* This also puts a final '\0' at the end of exclusion_msg */
270 return exclusion_msg
;
274 * Pretty print single event.
276 static void print_events(struct lttng_event
*event
)
279 const char *filter_str
;
280 char *filter_msg
= NULL
;
281 char *exclusion_msg
= NULL
;
283 ret
= lttng_event_get_filter_string(event
, &filter_str
);
286 filter_msg
= strdup(" [failed to retrieve filter]");
287 } else if (filter_str
) {
288 const char * const filter_fmt
= " [filter: '%s']";
290 filter_msg
= malloc(strlen(filter_str
) +
291 strlen(filter_fmt
) + 1);
293 sprintf(filter_msg
, filter_fmt
,
298 exclusion_msg
= get_exclusion_names_msg(event
);
299 if (!exclusion_msg
) {
300 exclusion_msg
= strdup(" [failed to retrieve exclusions]");
303 switch (event
->type
) {
304 case LTTNG_EVENT_TRACEPOINT
:
306 if (event
->loglevel
!= -1) {
307 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
310 logleveltype_string(event
->loglevel_type
),
311 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
313 enabled_string(event
->enabled
),
314 safe_string(exclusion_msg
),
315 safe_string(filter_msg
));
317 MSG("%s%s (type: tracepoint)%s%s%s",
320 enabled_string(event
->enabled
),
321 safe_string(exclusion_msg
),
322 safe_string(filter_msg
));
326 case LTTNG_EVENT_FUNCTION
:
327 MSG("%s%s (type: function)%s%s", indent6
,
328 event
->name
, enabled_string(event
->enabled
),
329 safe_string(filter_msg
));
330 if (event
->attr
.probe
.addr
!= 0) {
331 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
333 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
334 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
337 case LTTNG_EVENT_PROBE
:
338 MSG("%s%s (type: probe)%s%s", indent6
,
339 event
->name
, enabled_string(event
->enabled
),
340 safe_string(filter_msg
));
341 if (event
->attr
.probe
.addr
!= 0) {
342 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
344 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
345 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
348 case LTTNG_EVENT_FUNCTION_ENTRY
:
349 MSG("%s%s (type: function)%s%s", indent6
,
350 event
->name
, enabled_string(event
->enabled
),
351 safe_string(filter_msg
));
352 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
354 case LTTNG_EVENT_SYSCALL
:
355 MSG("%s%s%s%s%s", indent6
, event
->name
,
356 (opt_syscall
? "" : " (type:syscall)"),
357 enabled_string(event
->enabled
),
358 bitness_event(event
->flags
));
360 case LTTNG_EVENT_NOOP
:
361 MSG("%s (type: noop)%s%s", indent6
,
362 enabled_string(event
->enabled
),
363 safe_string(filter_msg
));
365 case LTTNG_EVENT_ALL
:
366 /* We should never have "all" events in list. */
375 static const char *field_type(struct lttng_event_field
*field
)
377 switch(field
->type
) {
378 case LTTNG_EVENT_FIELD_INTEGER
:
380 case LTTNG_EVENT_FIELD_ENUM
:
382 case LTTNG_EVENT_FIELD_FLOAT
:
384 case LTTNG_EVENT_FIELD_STRING
:
386 case LTTNG_EVENT_FIELD_OTHER
:
387 default: /* fall-through */
393 * Pretty print single event fields.
395 static void print_event_field(struct lttng_event_field
*field
)
397 if (!field
->field_name
[0]) {
400 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
401 field_type(field
), field
->nowrite
? " [no write]" : "");
406 * Jul and ust event listing
408 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
409 struct lttng_domain
*domain
)
413 char *cmdline
= NULL
;
414 int pid_element_open
= 0;
416 /* Open domains element */
417 ret
= mi_lttng_domains_open(writer
);
423 ret
= mi_lttng_domain(writer
, domain
, 1);
428 /* Open pids element element */
429 ret
= mi_lttng_pids_open(writer
);
434 for (i
= 0; i
< count
; i
++) {
435 if (cur_pid
!= events
[i
].pid
) {
436 if (pid_element_open
) {
437 /* Close the previous events and pid element */
438 ret
= mi_lttng_close_multi_element(writer
, 2);
442 pid_element_open
= 0;
445 cur_pid
= events
[i
].pid
;
446 cmdline
= get_cmdline_by_pid(cur_pid
);
452 if (!pid_element_open
) {
453 /* Open and write a pid element */
454 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
459 /* Open events element */
460 ret
= mi_lttng_events_open(writer
);
465 pid_element_open
= 1;
471 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
478 ret
= mi_lttng_writer_close_element(writer
);
483 /* Close domain, domains */
484 ret
= mi_lttng_close_multi_element(writer
, 2);
492 static int list_agent_events(void)
494 int i
, size
, ret
= CMD_SUCCESS
;
495 struct lttng_domain domain
;
496 struct lttng_handle
*handle
= NULL
;
497 struct lttng_event
*event_list
= NULL
;
499 char *cmdline
= NULL
;
500 const char *agent_domain_str
;
502 memset(&domain
, 0, sizeof(domain
));
504 domain
.type
= LTTNG_DOMAIN_JUL
;
505 } else if (opt_log4j
) {
506 domain
.type
= LTTNG_DOMAIN_LOG4J
;
507 } else if (opt_python
) {
508 domain
.type
= LTTNG_DOMAIN_PYTHON
;
510 ERR("Invalid agent domain selected.");
515 agent_domain_str
= get_domain_str(domain
.type
);
517 DBG("Getting %s tracing events", agent_domain_str
);
519 handle
= lttng_create_handle(NULL
, &domain
);
520 if (handle
== NULL
) {
525 size
= lttng_list_tracepoints(handle
, &event_list
);
527 ERR("Unable to list %s events: %s", agent_domain_str
,
528 lttng_strerror(size
));
535 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
542 MSG("%s events (Logger name):\n-------------------------",
549 for (i
= 0; i
< size
; i
++) {
550 if (cur_pid
!= event_list
[i
].pid
) {
551 cur_pid
= event_list
[i
].pid
;
552 cmdline
= get_cmdline_by_pid(cur_pid
);
553 if (cmdline
== NULL
) {
557 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
560 MSG("%s- %s", indent6
, event_list
[i
].name
);
569 lttng_destroy_handle(handle
);
574 * Ask session daemon for all user space tracepoints available.
576 static int list_ust_events(void)
578 int i
, size
, ret
= CMD_SUCCESS
;
579 struct lttng_domain domain
;
580 struct lttng_handle
*handle
;
581 struct lttng_event
*event_list
= NULL
;
583 char *cmdline
= NULL
;
585 memset(&domain
, 0, sizeof(domain
));
587 DBG("Getting UST tracing events");
589 domain
.type
= LTTNG_DOMAIN_UST
;
591 handle
= lttng_create_handle(NULL
, &domain
);
592 if (handle
== NULL
) {
597 size
= lttng_list_tracepoints(handle
, &event_list
);
599 ERR("Unable to list UST events: %s", lttng_strerror(size
));
606 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
609 MSG("UST events:\n-------------");
615 for (i
= 0; i
< size
; i
++) {
616 if (cur_pid
!= event_list
[i
].pid
) {
617 cur_pid
= event_list
[i
].pid
;
618 cmdline
= get_cmdline_by_pid(cur_pid
);
619 if (cmdline
== NULL
) {
623 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
626 print_events(&event_list
[i
]);
635 lttng_destroy_handle(handle
);
641 * List all ust event with their fields
643 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
644 struct lttng_domain
*domain
)
648 char *cmdline
= NULL
;
649 int pid_element_open
= 0;
650 int event_element_open
= 0;
651 struct lttng_event cur_event
;
653 /* Open domains element */
654 ret
= mi_lttng_domains_open(writer
);
660 ret
= mi_lttng_domain(writer
, domain
, 1);
665 /* Open pids element */
666 ret
= mi_lttng_pids_open(writer
);
671 for (i
= 0; i
< count
; i
++) {
672 if (cur_pid
!= fields
[i
].event
.pid
) {
673 if (pid_element_open
) {
674 if (event_element_open
) {
676 /* Close the previous field element and event. */
677 ret
= mi_lttng_close_multi_element(writer
, 2);
681 event_element_open
= 0;
683 /* Close the previous events, pid element */
684 ret
= mi_lttng_close_multi_element(writer
, 2);
688 pid_element_open
= 0;
691 cur_pid
= fields
[i
].event
.pid
;
692 cmdline
= get_cmdline_by_pid(cur_pid
);
693 if (!pid_element_open
) {
694 /* Open and write a pid element */
695 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
700 /* Open events element */
701 ret
= mi_lttng_events_open(writer
);
705 pid_element_open
= 1;
708 /* Wipe current event since we are about to print a new PID. */
709 memset(&cur_event
, 0, sizeof(cur_event
));
712 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
713 if (event_element_open
) {
714 /* Close the previous fields element and the previous event */
715 ret
= mi_lttng_close_multi_element(writer
, 2);
719 event_element_open
= 0;
722 memcpy(&cur_event
, &fields
[i
].event
,
725 if (!event_element_open
) {
726 /* Open and write the event */
727 ret
= mi_lttng_event(writer
, &cur_event
, 1,
728 handle
->domain
.type
);
733 /* Open a fields element */
734 ret
= mi_lttng_event_fields_open(writer
);
738 event_element_open
= 1;
742 /* Print the event_field */
743 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
749 /* Close pid, domain, domains */
750 ret
= mi_lttng_close_multi_element(writer
, 3);
759 * Ask session daemon for all user space tracepoint fields available.
761 static int list_ust_event_fields(void)
763 int i
, size
, ret
= CMD_SUCCESS
;
764 struct lttng_domain domain
;
765 struct lttng_handle
*handle
;
766 struct lttng_event_field
*event_field_list
;
768 char *cmdline
= NULL
;
770 struct lttng_event cur_event
;
772 memset(&domain
, 0, sizeof(domain
));
773 memset(&cur_event
, 0, sizeof(cur_event
));
775 DBG("Getting UST tracing event fields");
777 domain
.type
= LTTNG_DOMAIN_UST
;
779 handle
= lttng_create_handle(NULL
, &domain
);
780 if (handle
== NULL
) {
785 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
787 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
794 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
801 MSG("UST events:\n-------------");
807 for (i
= 0; i
< size
; i
++) {
808 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
809 cur_pid
= event_field_list
[i
].event
.pid
;
810 cmdline
= get_cmdline_by_pid(cur_pid
);
811 if (cmdline
== NULL
) {
815 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
817 /* Wipe current event since we are about to print a new PID. */
818 memset(&cur_event
, 0, sizeof(cur_event
));
820 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
821 print_events(&event_field_list
[i
].event
);
822 memcpy(&cur_event
, &event_field_list
[i
].event
,
825 print_event_field(&event_field_list
[i
]);
832 free(event_field_list
);
834 lttng_destroy_handle(handle
);
840 * Print a list of kernel events
842 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
843 struct lttng_domain
*domain
)
847 /* Open domains element */
848 ret
= mi_lttng_domains_open(writer
);
854 ret
= mi_lttng_domain(writer
, domain
, 1);
860 ret
= mi_lttng_events_open(writer
);
865 for (i
= 0; i
< count
; i
++) {
866 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
872 /* close events, domain and domains */
873 ret
= mi_lttng_close_multi_element(writer
, 3);
883 * Ask for all trace events in the kernel
885 static int list_kernel_events(void)
887 int i
, size
, ret
= CMD_SUCCESS
;
888 struct lttng_domain domain
;
889 struct lttng_handle
*handle
;
890 struct lttng_event
*event_list
;
892 memset(&domain
, 0, sizeof(domain
));
894 DBG("Getting kernel tracing events");
896 domain
.type
= LTTNG_DOMAIN_KERNEL
;
898 handle
= lttng_create_handle(NULL
, &domain
);
899 if (handle
== NULL
) {
904 size
= lttng_list_tracepoints(handle
, &event_list
);
906 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
907 lttng_destroy_handle(handle
);
913 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
919 MSG("Kernel events:\n-------------");
921 for (i
= 0; i
< size
; i
++) {
922 print_events(&event_list
[i
]);
931 lttng_destroy_handle(handle
);
935 lttng_destroy_handle(handle
);
941 * Print a list of system calls.
943 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
948 ret
= mi_lttng_events_open(writer
);
953 for (i
= 0; i
< count
; i
++) {
954 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
961 ret
= mi_lttng_writer_close_element(writer
);
971 * Ask for kernel system calls.
973 static int list_syscalls(void)
975 int i
, size
, ret
= CMD_SUCCESS
;
976 struct lttng_event
*event_list
;
978 DBG("Getting kernel system call events");
980 size
= lttng_list_syscalls(&event_list
);
982 ERR("Unable to list system calls: %s", lttng_strerror(size
));
989 ret
= mi_list_syscalls(event_list
, size
);
995 MSG("System calls:\n-------------");
997 for (i
= 0; i
< size
; i
++) {
998 print_events(&event_list
[i
]);
1014 * Print a list of agent events
1016 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1020 /* Open events element */
1021 ret
= mi_lttng_events_open(writer
);
1026 for (i
= 0; i
< count
; i
++) {
1027 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1033 /* Close events element */
1034 ret
= mi_lttng_writer_close_element(writer
);
1041 * List agent events for a specific session using the handle.
1043 * Return CMD_SUCCESS on success else a negative value.
1045 static int list_session_agent_events(void)
1047 int ret
= CMD_SUCCESS
, count
, i
;
1048 struct lttng_event
*events
= NULL
;
1050 count
= lttng_list_events(handle
, "", &events
);
1053 ERR("%s", lttng_strerror(count
));
1059 ret
= mi_list_session_agent_events(events
, count
);
1066 MSG("Events (Logger name):\n---------------------");
1068 MSG("%sNone\n", indent6
);
1072 for (i
= 0; i
< count
; i
++) {
1073 const char *filter_str
;
1074 char *filter_msg
= NULL
;
1075 struct lttng_event
*event
= &events
[i
];
1077 ret
= lttng_event_get_filter_string(event
, &filter_str
);
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: '%s']",
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
)
1195 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1197 MSG("%sAttributes:", indent4
);
1198 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1199 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1200 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1201 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1202 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1203 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1204 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1205 switch (channel
->attr
.output
) {
1206 case LTTNG_EVENT_SPLICE
:
1207 MSG("%soutput: splice()", indent6
);
1209 case LTTNG_EVENT_MMAP
:
1210 MSG("%soutput: mmap()", indent6
);
1217 * Print a list of channel
1220 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1221 const char *channel_name
)
1224 unsigned int chan_found
= 0;
1226 /* Open channels element */
1227 ret
= mi_lttng_channels_open(writer
);
1232 for (i
= 0; i
< count
; i
++) {
1233 if (channel_name
!= NULL
) {
1234 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1241 /* Write channel element and leave it open */
1242 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1247 /* Listing events per channel */
1248 ret
= list_events(channels
[i
].name
);
1253 /* Closing the channel element we opened earlier */
1254 ret
= mi_lttng_writer_close_element(writer
);
1264 /* Close channels element */
1265 ret
= mi_lttng_writer_close_element(writer
);
1275 * List channel(s) of session and domain.
1277 * If channel_name is NULL, all channels are listed.
1279 static int list_channels(const char *channel_name
)
1281 int count
, i
, ret
= CMD_SUCCESS
;
1282 unsigned int chan_found
= 0;
1283 struct lttng_channel
*channels
= NULL
;
1285 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1287 count
= lttng_list_channels(handle
, &channels
);
1290 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1292 /* When printing mi this is not an error
1293 * but an empty channels element */
1297 WARN("No kernel channel");
1298 goto error_channels
;
1302 /* We had a real error */
1304 ERR("%s", lttng_strerror(count
));
1305 goto error_channels
;
1312 ret
= mi_list_channels(channels
, count
, channel_name
);
1320 MSG("Channels:\n-------------");
1323 for (i
= 0; i
< count
; i
++) {
1324 if (channel_name
!= NULL
) {
1325 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1331 print_channel(&channels
[i
]);
1333 /* Listing events per channel */
1334 ret
= list_events(channels
[i
].name
);
1344 if (!chan_found
&& channel_name
!= NULL
) {
1346 ERR("Channel %s not found", channel_name
);
1358 * List tracker PID(s) of session and domain.
1360 static int list_tracker_pids(void)
1367 ret
= lttng_list_tracker_pids(handle
,
1368 &enabled
, &pids
, &nr_pids
);
1374 _MSG("PID tracker: [");
1376 /* Mi tracker_pid element*/
1378 /* Open tracker_pid and targets elements */
1379 ret
= mi_lttng_pid_tracker_open(writer
);
1385 for (i
= 0; i
< nr_pids
; i
++) {
1389 _MSG(" %d", pids
[i
]);
1393 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1401 /* Mi close tracker_pid and targets */
1403 ret
= mi_lttng_close_multi_element(writer
,2);
1416 * List all tracker of a domain
1418 static int list_trackers(void)
1422 /* Trackers listing */
1424 ret
= mi_lttng_trackers_open(writer
);
1431 ret
= list_tracker_pids();
1437 /* Close trackers element */
1438 ret
= mi_lttng_writer_close_element(writer
);
1450 * Find the session with session_name as name
1451 * and print his informations.
1453 static int mi_list_session(const char *session_name
,
1454 struct lttng_session
*sessions
, int count
)
1457 unsigned int session_found
= 0;
1459 if (session_name
== NULL
) {
1460 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1464 for (i
= 0; i
< count
; i
++) {
1465 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1466 /* We need to leave it open to append other informations
1467 * like domain, channel, events etc.*/
1469 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1477 if (!session_found
) {
1478 ERR("Session '%s' not found", session_name
);
1479 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1489 * List all availables session
1491 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1495 /* Opening sessions element */
1496 ret
= mi_lttng_sessions_open(writer
);
1501 /* Listing sessions */
1502 for (i
= 0; i
< count
; i
++) {
1503 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1509 /* Closing sessions element */
1510 ret
= mi_lttng_writer_close_element(writer
);
1520 * List available tracing session. List only basic information.
1522 * If session_name is NULL, all sessions are listed.
1524 static int list_sessions(const char *session_name
)
1526 int ret
= CMD_SUCCESS
;
1528 unsigned int session_found
= 0;
1529 struct lttng_session
*sessions
;
1531 count
= lttng_list_sessions(&sessions
);
1532 DBG("Session count %d", count
);
1535 ERR("%s", lttng_strerror(count
));
1541 if (session_name
== NULL
) {
1542 /* List all session */
1543 ret
= mi_list_sessions(sessions
, count
);
1545 /* Note : this return an open session element */
1546 ret
= mi_list_session(session_name
, sessions
, count
);
1555 MSG("Currently no available tracing session");
1559 if (session_name
== NULL
) {
1560 MSG("Available tracing sessions:");
1564 for (i
= 0; i
< count
; i
++) {
1565 if (session_name
!= NULL
) {
1566 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1568 MSG("Tracing session %s: [%s%s]", session_name
,
1569 active_string(sessions
[i
].enabled
),
1570 snapshot_string(sessions
[i
].snapshot_mode
));
1571 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1575 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1576 sessions
[i
].name
, sessions
[i
].path
,
1577 active_string(sessions
[i
].enabled
),
1578 snapshot_string(sessions
[i
].snapshot_mode
));
1579 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1580 if (sessions
[i
].live_timer_interval
!= 0) {
1581 MSG("%sLive timer interval (usec): %u", indent4
,
1582 sessions
[i
].live_timer_interval
);
1588 if (!session_found
&& session_name
!= NULL
) {
1589 ERR("Session '%s' not found", session_name
);
1594 if (session_name
== NULL
) {
1595 MSG("\nUse lttng list <session_name> for more details");
1608 * list available domain(s) for a session.
1610 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1613 /* Open domains element */
1614 ret
= mi_lttng_domains_open(writer
);
1619 for (i
= 0; i
< count
; i
++) {
1620 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1626 /* Closing domains element */
1627 ret
= mi_lttng_writer_close_element(writer
);
1636 * List available domain(s) for a session.
1638 static int list_domains(const char *session_name
)
1640 int i
, count
, ret
= CMD_SUCCESS
;
1641 struct lttng_domain
*domains
= NULL
;
1644 count
= lttng_list_domains(session_name
, &domains
);
1647 ERR("%s", lttng_strerror(count
));
1653 ret
= mi_list_domains(domains
, count
);
1660 MSG("Domains:\n-------------");
1666 for (i
= 0; i
< count
; i
++) {
1667 switch (domains
[i
].type
) {
1668 case LTTNG_DOMAIN_KERNEL
:
1671 case LTTNG_DOMAIN_UST
:
1672 MSG(" - UST global");
1674 case LTTNG_DOMAIN_JUL
:
1675 MSG(" - JUL (Java Util Logging)");
1677 case LTTNG_DOMAIN_LOG4J
:
1678 MSG(" - LOG4j (Logging for Java)");
1680 case LTTNG_DOMAIN_PYTHON
:
1681 MSG(" - Python (logging)");
1697 * The 'list <options>' first level command
1699 int cmd_list(int argc
, const char **argv
)
1701 int opt
, ret
= CMD_SUCCESS
;
1702 const char *session_name
;
1703 static poptContext pc
;
1704 struct lttng_domain domain
;
1705 struct lttng_domain
*domains
= NULL
;
1707 memset(&domain
, 0, sizeof(domain
));
1715 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1716 poptReadDefaultConfig(pc
, 0);
1718 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1726 case OPT_LIST_OPTIONS
:
1727 list_cmd_options(stdout
, long_options
);
1731 ret
= CMD_UNDEFINED
;
1738 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1744 /* Open command element */
1745 ret
= mi_lttng_writer_command_open(writer
,
1746 mi_lttng_element_command_list
);
1752 /* Open output element */
1753 ret
= mi_lttng_writer_open_element(writer
,
1754 mi_lttng_element_command_output
);
1761 /* Get session name (trailing argument) */
1762 session_name
= poptGetArg(pc
);
1763 DBG2("Session name: %s", session_name
);
1766 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1767 } else if (opt_userspace
) {
1768 DBG2("Listing userspace global domain");
1769 domain
.type
= LTTNG_DOMAIN_UST
;
1770 } else if (opt_jul
) {
1771 DBG2("Listing JUL domain");
1772 domain
.type
= LTTNG_DOMAIN_JUL
;
1773 } else if (opt_log4j
) {
1774 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1775 } else if (opt_python
) {
1776 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1779 if (!opt_kernel
&& opt_syscall
) {
1780 WARN("--syscall will only work with the Kernel domain (-k)");
1785 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1786 handle
= lttng_create_handle(session_name
, &domain
);
1787 if (handle
== NULL
) {
1793 if (session_name
== NULL
) {
1794 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1796 ret
= list_sessions(NULL
);
1803 ret
= list_syscalls();
1808 ret
= list_kernel_events();
1814 if (opt_userspace
) {
1816 ret
= list_ust_event_fields();
1818 ret
= list_ust_events();
1824 if (opt_jul
|| opt_log4j
|| opt_python
) {
1825 ret
= list_agent_events();
1831 /* List session attributes */
1833 /* Open element sessions
1834 * Present for xml consistency */
1835 ret
= mi_lttng_sessions_open(writer
);
1840 /* MI: the ouptut of list_sessions is an unclosed session element */
1841 ret
= list_sessions(session_name
);
1846 /* Domain listing */
1848 ret
= list_domains(session_name
);
1852 /* Channel listing */
1853 if (opt_kernel
|| opt_userspace
) {
1855 /* Add of domains and domain element for xml
1856 * consistency and validation
1858 ret
= mi_lttng_domains_open(writer
);
1863 /* Open domain and leave it open for
1864 * nested channels printing */
1865 ret
= mi_lttng_domain(writer
, &domain
, 1);
1874 ret
= list_trackers();
1880 ret
= list_channels(opt_channel
);
1886 /* Close domain and domain element */
1887 ret
= mi_lttng_close_multi_element(writer
, 2);
1897 /* We want all domain(s) */
1898 nb_domain
= lttng_list_domains(session_name
, &domains
);
1899 if (nb_domain
< 0) {
1901 ERR("%s", lttng_strerror(nb_domain
));
1906 ret
= mi_lttng_domains_open(writer
);
1913 for (i
= 0; i
< nb_domain
; i
++) {
1914 switch (domains
[i
].type
) {
1915 case LTTNG_DOMAIN_KERNEL
:
1916 MSG("=== Domain: Kernel ===\n");
1918 case LTTNG_DOMAIN_UST
:
1919 MSG("=== Domain: UST global ===\n");
1920 MSG("Buffer type: %s\n",
1921 domains
[i
].buf_type
==
1922 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1924 case LTTNG_DOMAIN_JUL
:
1925 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1927 case LTTNG_DOMAIN_LOG4J
:
1928 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1930 case LTTNG_DOMAIN_PYTHON
:
1931 MSG("=== Domain: Python (logging) ===\n");
1934 MSG("=== Domain: Unimplemented ===\n");
1939 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1946 /* Clean handle before creating a new one */
1948 lttng_destroy_handle(handle
);
1951 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1952 if (handle
== NULL
) {
1957 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1958 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
1959 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
1960 ret
= list_session_agent_events();
1968 switch (domains
[i
].type
) {
1969 case LTTNG_DOMAIN_KERNEL
:
1970 case LTTNG_DOMAIN_UST
:
1971 ret
= list_trackers();
1980 ret
= list_channels(opt_channel
);
1987 /* Close domain element */
1988 ret
= mi_lttng_writer_close_element(writer
);
1997 /* Close the domains, session and sessions element */
1998 ret
= mi_lttng_close_multi_element(writer
, 3);
2009 /* Close output element */
2010 ret
= mi_lttng_writer_close_element(writer
);
2016 /* Command element close */
2017 ret
= mi_lttng_writer_command_close(writer
);
2025 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2026 /* Preserve original error code */
2027 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2032 lttng_destroy_handle(handle
);
2035 poptFreeContext(pc
);