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_string(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", indent6
, event
->name
,
354 (opt_syscall
? "" : " (type:syscall)"),
355 enabled_string(event
->enabled
),
356 bitness_event(event
->flags
));
358 case LTTNG_EVENT_NOOP
:
359 MSG("%s (type: noop)%s%s", indent6
,
360 enabled_string(event
->enabled
),
361 safe_string(filter_msg
));
363 case LTTNG_EVENT_ALL
:
364 /* We should never have "all" events in list. */
373 static const char *field_type(struct lttng_event_field
*field
)
375 switch(field
->type
) {
376 case LTTNG_EVENT_FIELD_INTEGER
:
378 case LTTNG_EVENT_FIELD_ENUM
:
380 case LTTNG_EVENT_FIELD_FLOAT
:
382 case LTTNG_EVENT_FIELD_STRING
:
384 case LTTNG_EVENT_FIELD_OTHER
:
385 default: /* fall-through */
391 * Pretty print single event fields.
393 static void print_event_field(struct lttng_event_field
*field
)
395 if (!field
->field_name
[0]) {
398 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
399 field_type(field
), field
->nowrite
? " [no write]" : "");
404 * Jul and ust event listing
406 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
407 struct lttng_domain
*domain
)
411 char *cmdline
= NULL
;
412 int pid_element_open
= 0;
414 /* Open domains element */
415 ret
= mi_lttng_domains_open(writer
);
421 ret
= mi_lttng_domain(writer
, domain
, 1);
426 /* Open pids element element */
427 ret
= mi_lttng_pids_open(writer
);
432 for (i
= 0; i
< count
; i
++) {
433 if (cur_pid
!= events
[i
].pid
) {
434 if (pid_element_open
) {
435 /* Close the previous events and pid element */
436 ret
= mi_lttng_close_multi_element(writer
, 2);
440 pid_element_open
= 0;
443 cur_pid
= events
[i
].pid
;
444 cmdline
= get_cmdline_by_pid(cur_pid
);
450 if (!pid_element_open
) {
451 /* Open and write a pid element */
452 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
457 /* Open events element */
458 ret
= mi_lttng_events_open(writer
);
463 pid_element_open
= 1;
469 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
476 ret
= mi_lttng_writer_close_element(writer
);
481 /* Close domain, domains */
482 ret
= mi_lttng_close_multi_element(writer
, 2);
490 static int list_agent_events(void)
492 int i
, size
, ret
= CMD_SUCCESS
;
493 struct lttng_domain domain
;
494 struct lttng_handle
*handle
= NULL
;
495 struct lttng_event
*event_list
= NULL
;
497 char *cmdline
= NULL
;
498 const char *agent_domain_str
;
500 memset(&domain
, 0, sizeof(domain
));
502 domain
.type
= LTTNG_DOMAIN_JUL
;
503 } else if (opt_log4j
) {
504 domain
.type
= LTTNG_DOMAIN_LOG4J
;
505 } else if (opt_python
) {
506 domain
.type
= LTTNG_DOMAIN_PYTHON
;
508 ERR("Invalid agent domain selected.");
513 agent_domain_str
= get_domain_str(domain
.type
);
515 DBG("Getting %s tracing events", agent_domain_str
);
517 handle
= lttng_create_handle(NULL
, &domain
);
518 if (handle
== NULL
) {
523 size
= lttng_list_tracepoints(handle
, &event_list
);
525 ERR("Unable to list %s events: %s", agent_domain_str
,
526 lttng_strerror(size
));
533 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
540 MSG("%s events (Logger name):\n-------------------------",
547 for (i
= 0; i
< size
; i
++) {
548 if (cur_pid
!= event_list
[i
].pid
) {
549 cur_pid
= event_list
[i
].pid
;
550 cmdline
= get_cmdline_by_pid(cur_pid
);
551 if (cmdline
== NULL
) {
555 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
558 MSG("%s- %s", indent6
, event_list
[i
].name
);
567 lttng_destroy_handle(handle
);
572 * Ask session daemon for all user space tracepoints available.
574 static int list_ust_events(void)
576 int i
, size
, ret
= CMD_SUCCESS
;
577 struct lttng_domain domain
;
578 struct lttng_handle
*handle
;
579 struct lttng_event
*event_list
= NULL
;
581 char *cmdline
= NULL
;
583 memset(&domain
, 0, sizeof(domain
));
585 DBG("Getting UST tracing events");
587 domain
.type
= LTTNG_DOMAIN_UST
;
589 handle
= lttng_create_handle(NULL
, &domain
);
590 if (handle
== NULL
) {
595 size
= lttng_list_tracepoints(handle
, &event_list
);
597 ERR("Unable to list UST events: %s", lttng_strerror(size
));
604 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
607 MSG("UST events:\n-------------");
613 for (i
= 0; i
< size
; i
++) {
614 if (cur_pid
!= event_list
[i
].pid
) {
615 cur_pid
= event_list
[i
].pid
;
616 cmdline
= get_cmdline_by_pid(cur_pid
);
617 if (cmdline
== NULL
) {
621 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
624 print_events(&event_list
[i
]);
633 lttng_destroy_handle(handle
);
639 * List all ust event with their fields
641 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
642 struct lttng_domain
*domain
)
646 char *cmdline
= NULL
;
647 int pid_element_open
= 0;
648 int event_element_open
= 0;
649 struct lttng_event cur_event
;
651 /* Open domains element */
652 ret
= mi_lttng_domains_open(writer
);
658 ret
= mi_lttng_domain(writer
, domain
, 1);
663 /* Open pids element */
664 ret
= mi_lttng_pids_open(writer
);
669 for (i
= 0; i
< count
; i
++) {
670 if (cur_pid
!= fields
[i
].event
.pid
) {
671 if (pid_element_open
) {
672 if (event_element_open
) {
674 /* Close the previous field element and event. */
675 ret
= mi_lttng_close_multi_element(writer
, 2);
679 event_element_open
= 0;
681 /* Close the previous events, pid element */
682 ret
= mi_lttng_close_multi_element(writer
, 2);
686 pid_element_open
= 0;
689 cur_pid
= fields
[i
].event
.pid
;
690 cmdline
= get_cmdline_by_pid(cur_pid
);
691 if (!pid_element_open
) {
692 /* Open and write a pid element */
693 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
698 /* Open events element */
699 ret
= mi_lttng_events_open(writer
);
703 pid_element_open
= 1;
706 /* Wipe current event since we are about to print a new PID. */
707 memset(&cur_event
, 0, sizeof(cur_event
));
710 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
711 if (event_element_open
) {
712 /* Close the previous fields element and the previous event */
713 ret
= mi_lttng_close_multi_element(writer
, 2);
717 event_element_open
= 0;
720 memcpy(&cur_event
, &fields
[i
].event
,
723 if (!event_element_open
) {
724 /* Open and write the event */
725 ret
= mi_lttng_event(writer
, &cur_event
, 1,
726 handle
->domain
.type
);
731 /* Open a fields element */
732 ret
= mi_lttng_event_fields_open(writer
);
736 event_element_open
= 1;
740 /* Print the event_field */
741 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
747 /* Close pid, domain, domains */
748 ret
= mi_lttng_close_multi_element(writer
, 3);
757 * Ask session daemon for all user space tracepoint fields available.
759 static int list_ust_event_fields(void)
761 int i
, size
, ret
= CMD_SUCCESS
;
762 struct lttng_domain domain
;
763 struct lttng_handle
*handle
;
764 struct lttng_event_field
*event_field_list
;
766 char *cmdline
= NULL
;
768 struct lttng_event cur_event
;
770 memset(&domain
, 0, sizeof(domain
));
771 memset(&cur_event
, 0, sizeof(cur_event
));
773 DBG("Getting UST tracing event fields");
775 domain
.type
= LTTNG_DOMAIN_UST
;
777 handle
= lttng_create_handle(NULL
, &domain
);
778 if (handle
== NULL
) {
783 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
785 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
792 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
799 MSG("UST events:\n-------------");
805 for (i
= 0; i
< size
; i
++) {
806 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
807 cur_pid
= event_field_list
[i
].event
.pid
;
808 cmdline
= get_cmdline_by_pid(cur_pid
);
809 if (cmdline
== NULL
) {
813 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
815 /* Wipe current event since we are about to print a new PID. */
816 memset(&cur_event
, 0, sizeof(cur_event
));
818 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
819 print_events(&event_field_list
[i
].event
);
820 memcpy(&cur_event
, &event_field_list
[i
].event
,
823 print_event_field(&event_field_list
[i
]);
830 free(event_field_list
);
832 lttng_destroy_handle(handle
);
838 * Print a list of kernel events
840 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
841 struct lttng_domain
*domain
)
845 /* Open domains element */
846 ret
= mi_lttng_domains_open(writer
);
852 ret
= mi_lttng_domain(writer
, domain
, 1);
858 ret
= mi_lttng_events_open(writer
);
863 for (i
= 0; i
< count
; i
++) {
864 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
870 /* close events, domain and domains */
871 ret
= mi_lttng_close_multi_element(writer
, 3);
881 * Ask for all trace events in the kernel
883 static int list_kernel_events(void)
885 int i
, size
, ret
= CMD_SUCCESS
;
886 struct lttng_domain domain
;
887 struct lttng_handle
*handle
;
888 struct lttng_event
*event_list
;
890 memset(&domain
, 0, sizeof(domain
));
892 DBG("Getting kernel tracing events");
894 domain
.type
= LTTNG_DOMAIN_KERNEL
;
896 handle
= lttng_create_handle(NULL
, &domain
);
897 if (handle
== NULL
) {
902 size
= lttng_list_tracepoints(handle
, &event_list
);
904 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
905 lttng_destroy_handle(handle
);
911 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
917 MSG("Kernel events:\n-------------");
919 for (i
= 0; i
< size
; i
++) {
920 print_events(&event_list
[i
]);
929 lttng_destroy_handle(handle
);
933 lttng_destroy_handle(handle
);
939 * Print a list of system calls.
941 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
946 ret
= mi_lttng_events_open(writer
);
951 for (i
= 0; i
< count
; i
++) {
952 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
959 ret
= mi_lttng_writer_close_element(writer
);
969 * Ask for kernel system calls.
971 static int list_syscalls(void)
973 int i
, size
, ret
= CMD_SUCCESS
;
974 struct lttng_event
*event_list
;
976 DBG("Getting kernel system call events");
978 size
= lttng_list_syscalls(&event_list
);
980 ERR("Unable to list system calls: %s", lttng_strerror(size
));
987 ret
= mi_list_syscalls(event_list
, size
);
993 MSG("System calls:\n-------------");
995 for (i
= 0; i
< size
; i
++) {
996 print_events(&event_list
[i
]);
1012 * Print a list of agent events
1014 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
1018 /* Open events element */
1019 ret
= mi_lttng_events_open(writer
);
1024 for (i
= 0; i
< count
; i
++) {
1025 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1031 /* Close events element */
1032 ret
= mi_lttng_writer_close_element(writer
);
1039 * List agent events for a specific session using the handle.
1041 * Return CMD_SUCCESS on success else a negative value.
1043 static int list_session_agent_events(void)
1045 int ret
= CMD_SUCCESS
, count
, i
;
1046 struct lttng_event
*events
= NULL
;
1048 count
= lttng_list_events(handle
, "", &events
);
1051 ERR("%s", lttng_strerror(count
));
1057 ret
= mi_list_session_agent_events(events
, count
);
1064 MSG("Events (Logger name):\n---------------------");
1066 MSG("%sNone\n", indent6
);
1070 for (i
= 0; i
< count
; i
++) {
1071 const char *filter_str
;
1072 char *filter_msg
= NULL
;
1073 struct lttng_event
*event
= &events
[i
];
1075 ret
= lttng_event_get_filter_string(event
, &filter_str
);
1077 filter_msg
= strdup(" [failed to retrieve filter]");
1078 } else if (filter_str
) {
1079 const char * const filter_fmt
=
1082 filter_msg
= malloc(strlen(filter_str
) +
1083 strlen(filter_fmt
) + 1);
1085 sprintf(filter_msg
, filter_fmt
,
1090 if (event
->loglevel_type
!=
1091 LTTNG_EVENT_LOGLEVEL_ALL
) {
1092 MSG("%s- %s%s (loglevel%s %s)%s", indent4
,
1094 enabled_string(event
->enabled
),
1095 logleveltype_string(
1096 event
->loglevel_type
),
1097 mi_lttng_loglevel_string(
1099 handle
->domain
.type
),
1100 safe_string(filter_msg
));
1102 MSG("%s- %s%s%s", indent4
, event
->name
,
1103 enabled_string(event
->enabled
),
1104 safe_string(filter_msg
));
1120 * print a list of event
1122 static int mi_list_events(struct lttng_event
*events
, int count
)
1126 /* Open events element */
1127 ret
= mi_lttng_events_open(writer
);
1132 for (i
= 0; i
< count
; i
++) {
1133 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1139 /* Close events element */
1140 ret
= mi_lttng_writer_close_element(writer
);
1147 * List events of channel of session and domain.
1149 static int list_events(const char *channel_name
)
1151 int ret
= CMD_SUCCESS
, count
, i
;
1152 struct lttng_event
*events
= NULL
;
1154 count
= lttng_list_events(handle
, channel_name
, &events
);
1157 ERR("%s", lttng_strerror(count
));
1163 ret
= mi_list_events(events
, count
);
1170 MSG("\n%sEvents:", indent4
);
1172 MSG("%sNone\n", indent6
);
1176 for (i
= 0; i
< count
; i
++) {
1177 print_events(&events
[i
]);
1189 * Pretty print channel
1191 static void print_channel(struct lttng_channel
*channel
)
1193 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1195 MSG("%sAttributes:", indent4
);
1196 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1197 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1198 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1199 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1200 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1201 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1202 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1203 switch (channel
->attr
.output
) {
1204 case LTTNG_EVENT_SPLICE
:
1205 MSG("%soutput: splice()", indent6
);
1207 case LTTNG_EVENT_MMAP
:
1208 MSG("%soutput: mmap()", indent6
);
1215 * Print a list of channel
1218 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1219 const char *channel_name
)
1222 unsigned int chan_found
= 0;
1224 /* Open channels element */
1225 ret
= mi_lttng_channels_open(writer
);
1230 for (i
= 0; i
< count
; i
++) {
1231 if (channel_name
!= NULL
) {
1232 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1239 /* Write channel element and leave it open */
1240 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1245 /* Listing events per channel */
1246 ret
= list_events(channels
[i
].name
);
1251 /* Closing the channel element we opened earlier */
1252 ret
= mi_lttng_writer_close_element(writer
);
1262 /* Close channels element */
1263 ret
= mi_lttng_writer_close_element(writer
);
1273 * List channel(s) of session and domain.
1275 * If channel_name is NULL, all channels are listed.
1277 static int list_channels(const char *channel_name
)
1279 int count
, i
, ret
= CMD_SUCCESS
;
1280 unsigned int chan_found
= 0;
1281 struct lttng_channel
*channels
= NULL
;
1283 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1285 count
= lttng_list_channels(handle
, &channels
);
1288 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1290 /* When printing mi this is not an error
1291 * but an empty channels element */
1295 WARN("No kernel channel");
1296 goto error_channels
;
1300 /* We had a real error */
1302 ERR("%s", lttng_strerror(count
));
1303 goto error_channels
;
1310 ret
= mi_list_channels(channels
, count
, channel_name
);
1318 MSG("Channels:\n-------------");
1321 for (i
= 0; i
< count
; i
++) {
1322 if (channel_name
!= NULL
) {
1323 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1329 print_channel(&channels
[i
]);
1331 /* Listing events per channel */
1332 ret
= list_events(channels
[i
].name
);
1342 if (!chan_found
&& channel_name
!= NULL
) {
1344 ERR("Channel %s not found", channel_name
);
1356 * List tracker PID(s) of session and domain.
1358 static int list_tracker_pids(void)
1365 ret
= lttng_list_tracker_pids(handle
,
1366 &enabled
, &pids
, &nr_pids
);
1372 _MSG("PID tracker: [");
1374 /* Mi tracker_pid element*/
1376 /* Open tracker_pid and targets elements */
1377 ret
= mi_lttng_pid_tracker_open(writer
);
1383 for (i
= 0; i
< nr_pids
; i
++) {
1387 _MSG(" %d", pids
[i
]);
1391 ret
= mi_lttng_pid_target(writer
, pids
[i
], 0);
1399 /* Mi close tracker_pid and targets */
1401 ret
= mi_lttng_close_multi_element(writer
,2);
1414 * List all tracker of a domain
1416 static int list_trackers(void)
1420 /* Trackers listing */
1422 ret
= mi_lttng_trackers_open(writer
);
1429 ret
= list_tracker_pids();
1435 /* Close trackers element */
1436 ret
= mi_lttng_writer_close_element(writer
);
1448 * Find the session with session_name as name
1449 * and print his informations.
1451 static int mi_list_session(const char *session_name
,
1452 struct lttng_session
*sessions
, int count
)
1455 unsigned int session_found
= 0;
1457 if (session_name
== NULL
) {
1458 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1462 for (i
= 0; i
< count
; i
++) {
1463 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1464 /* We need to leave it open to append other informations
1465 * like domain, channel, events etc.*/
1467 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1475 if (!session_found
) {
1476 ERR("Session '%s' not found", session_name
);
1477 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1487 * List all availables session
1489 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1493 /* Opening sessions element */
1494 ret
= mi_lttng_sessions_open(writer
);
1499 /* Listing sessions */
1500 for (i
= 0; i
< count
; i
++) {
1501 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1507 /* Closing sessions element */
1508 ret
= mi_lttng_writer_close_element(writer
);
1518 * List available tracing session. List only basic information.
1520 * If session_name is NULL, all sessions are listed.
1522 static int list_sessions(const char *session_name
)
1524 int ret
= CMD_SUCCESS
;
1526 unsigned int session_found
= 0;
1527 struct lttng_session
*sessions
;
1529 count
= lttng_list_sessions(&sessions
);
1530 DBG("Session count %d", count
);
1533 ERR("%s", lttng_strerror(count
));
1539 if (session_name
== NULL
) {
1540 /* List all session */
1541 ret
= mi_list_sessions(sessions
, count
);
1543 /* Note : this return an open session element */
1544 ret
= mi_list_session(session_name
, sessions
, count
);
1553 MSG("Currently no available tracing session");
1557 if (session_name
== NULL
) {
1558 MSG("Available tracing sessions:");
1562 for (i
= 0; i
< count
; i
++) {
1563 if (session_name
!= NULL
) {
1564 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1566 MSG("Tracing session %s: [%s%s]", session_name
,
1567 active_string(sessions
[i
].enabled
),
1568 snapshot_string(sessions
[i
].snapshot_mode
));
1569 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1573 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1574 sessions
[i
].name
, sessions
[i
].path
,
1575 active_string(sessions
[i
].enabled
),
1576 snapshot_string(sessions
[i
].snapshot_mode
));
1577 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1578 if (sessions
[i
].live_timer_interval
!= 0) {
1579 MSG("%sLive timer interval (usec): %u", indent4
,
1580 sessions
[i
].live_timer_interval
);
1586 if (!session_found
&& session_name
!= NULL
) {
1587 ERR("Session '%s' not found", session_name
);
1592 if (session_name
== NULL
) {
1593 MSG("\nUse lttng list <session_name> for more details");
1606 * list available domain(s) for a session.
1608 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1611 /* Open domains element */
1612 ret
= mi_lttng_domains_open(writer
);
1617 for (i
= 0; i
< count
; i
++) {
1618 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1624 /* Closing domains element */
1625 ret
= mi_lttng_writer_close_element(writer
);
1634 * List available domain(s) for a session.
1636 static int list_domains(const char *session_name
)
1638 int i
, count
, ret
= CMD_SUCCESS
;
1639 struct lttng_domain
*domains
= NULL
;
1642 count
= lttng_list_domains(session_name
, &domains
);
1645 ERR("%s", lttng_strerror(count
));
1651 ret
= mi_list_domains(domains
, count
);
1658 MSG("Domains:\n-------------");
1664 for (i
= 0; i
< count
; i
++) {
1665 switch (domains
[i
].type
) {
1666 case LTTNG_DOMAIN_KERNEL
:
1669 case LTTNG_DOMAIN_UST
:
1670 MSG(" - UST global");
1672 case LTTNG_DOMAIN_JUL
:
1673 MSG(" - JUL (Java Util Logging)");
1675 case LTTNG_DOMAIN_LOG4J
:
1676 MSG(" - LOG4j (Logging for Java)");
1678 case LTTNG_DOMAIN_PYTHON
:
1679 MSG(" - Python (logging)");
1695 * The 'list <options>' first level command
1697 int cmd_list(int argc
, const char **argv
)
1699 int opt
, ret
= CMD_SUCCESS
;
1700 const char *session_name
;
1701 static poptContext pc
;
1702 struct lttng_domain domain
;
1703 struct lttng_domain
*domains
= NULL
;
1705 memset(&domain
, 0, sizeof(domain
));
1713 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1714 poptReadDefaultConfig(pc
, 0);
1716 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1724 case OPT_LIST_OPTIONS
:
1725 list_cmd_options(stdout
, long_options
);
1729 ret
= CMD_UNDEFINED
;
1736 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1742 /* Open command element */
1743 ret
= mi_lttng_writer_command_open(writer
,
1744 mi_lttng_element_command_list
);
1750 /* Open output element */
1751 ret
= mi_lttng_writer_open_element(writer
,
1752 mi_lttng_element_command_output
);
1759 /* Get session name (trailing argument) */
1760 session_name
= poptGetArg(pc
);
1761 DBG2("Session name: %s", session_name
);
1764 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1765 } else if (opt_userspace
) {
1766 DBG2("Listing userspace global domain");
1767 domain
.type
= LTTNG_DOMAIN_UST
;
1768 } else if (opt_jul
) {
1769 DBG2("Listing JUL domain");
1770 domain
.type
= LTTNG_DOMAIN_JUL
;
1771 } else if (opt_log4j
) {
1772 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1773 } else if (opt_python
) {
1774 domain
.type
= LTTNG_DOMAIN_PYTHON
;
1777 if (!opt_kernel
&& opt_syscall
) {
1778 WARN("--syscall will only work with the Kernel domain (-k)");
1783 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
) {
1784 handle
= lttng_create_handle(session_name
, &domain
);
1785 if (handle
== NULL
) {
1791 if (session_name
== NULL
) {
1792 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
1794 ret
= list_sessions(NULL
);
1801 ret
= list_syscalls();
1806 ret
= list_kernel_events();
1812 if (opt_userspace
) {
1814 ret
= list_ust_event_fields();
1816 ret
= list_ust_events();
1822 if (opt_jul
|| opt_log4j
|| opt_python
) {
1823 ret
= list_agent_events();
1829 /* List session attributes */
1831 /* Open element sessions
1832 * Present for xml consistency */
1833 ret
= mi_lttng_sessions_open(writer
);
1838 /* MI: the ouptut of list_sessions is an unclosed session element */
1839 ret
= list_sessions(session_name
);
1844 /* Domain listing */
1846 ret
= list_domains(session_name
);
1850 /* Channel listing */
1851 if (opt_kernel
|| opt_userspace
) {
1853 /* Add of domains and domain element for xml
1854 * consistency and validation
1856 ret
= mi_lttng_domains_open(writer
);
1861 /* Open domain and leave it open for
1862 * nested channels printing */
1863 ret
= mi_lttng_domain(writer
, &domain
, 1);
1872 ret
= list_trackers();
1878 ret
= list_channels(opt_channel
);
1884 /* Close domain and domain element */
1885 ret
= mi_lttng_close_multi_element(writer
, 2);
1895 /* We want all domain(s) */
1896 nb_domain
= lttng_list_domains(session_name
, &domains
);
1897 if (nb_domain
< 0) {
1899 ERR("%s", lttng_strerror(nb_domain
));
1904 ret
= mi_lttng_domains_open(writer
);
1911 for (i
= 0; i
< nb_domain
; i
++) {
1912 switch (domains
[i
].type
) {
1913 case LTTNG_DOMAIN_KERNEL
:
1914 MSG("=== Domain: Kernel ===\n");
1916 case LTTNG_DOMAIN_UST
:
1917 MSG("=== Domain: UST global ===\n");
1918 MSG("Buffer type: %s\n",
1919 domains
[i
].buf_type
==
1920 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1922 case LTTNG_DOMAIN_JUL
:
1923 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1925 case LTTNG_DOMAIN_LOG4J
:
1926 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1928 case LTTNG_DOMAIN_PYTHON
:
1929 MSG("=== Domain: Python (logging) ===\n");
1932 MSG("=== Domain: Unimplemented ===\n");
1937 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1944 /* Clean handle before creating a new one */
1946 lttng_destroy_handle(handle
);
1949 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1950 if (handle
== NULL
) {
1955 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1956 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
||
1957 domains
[i
].type
== LTTNG_DOMAIN_PYTHON
) {
1958 ret
= list_session_agent_events();
1966 switch (domains
[i
].type
) {
1967 case LTTNG_DOMAIN_KERNEL
:
1968 case LTTNG_DOMAIN_UST
:
1969 ret
= list_trackers();
1978 ret
= list_channels(opt_channel
);
1985 /* Close domain element */
1986 ret
= mi_lttng_writer_close_element(writer
);
1995 /* Close the domains, session and sessions element */
1996 ret
= mi_lttng_close_multi_element(writer
, 3);
2007 /* Close output element */
2008 ret
= mi_lttng_writer_close_element(writer
);
2014 /* Command element close */
2015 ret
= mi_lttng_writer_command_close(writer
);
2023 if (writer
&& mi_lttng_writer_destroy(writer
)) {
2024 /* Preserve original error code */
2025 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
2030 lttng_destroy_handle(handle
);
2033 poptFreeContext(pc
);