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>
28 #include "../command.h"
30 static int opt_userspace
;
31 static int opt_kernel
;
34 static char *opt_channel
;
35 static int opt_domain
;
36 static int opt_fields
;
37 static int opt_syscall
;
39 /* Not implemented yet */
40 static char *opt_cmd_name
;
44 const char *indent4
= " ";
45 const char *indent6
= " ";
46 const char *indent8
= " ";
54 static struct lttng_handle
*handle
;
55 static struct mi_writer
*writer
;
57 static struct poptOption long_options
[] = {
58 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
59 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
60 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
61 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
62 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
64 /* Not implemented yet */
65 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
66 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
68 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
70 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
71 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
72 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
73 {"syscall", 'S', POPT_ARG_VAL
, &opt_syscall
, 1, 0, 0},
74 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
81 static void usage(FILE *ofp
)
83 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
85 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
87 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
88 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
90 fprintf(ofp
, " -h, --help Show this help\n");
91 fprintf(ofp
, " --list-options Simple listing of options\n");
92 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
93 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
94 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
95 fprintf(ofp
, " -l, --log4j Apply for Java application using LOG4J\n");
96 fprintf(ofp
, " -f, --fields List event fields.\n");
97 fprintf(ofp
, " --syscall List available system calls.\n");
99 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
102 fprintf(ofp
, "Session Options:\n");
103 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
104 fprintf(ofp
, " -d, --domain List available domain(s)\n");
109 * Get command line from /proc for a specific pid.
111 * On success, return an allocated string pointer to the proc cmdline.
112 * On error, return NULL.
114 static char *get_cmdline_by_pid(pid_t pid
)
118 char *cmdline
= NULL
;
119 char path
[20]; /* Can't go bigger than /proc/65535/cmdline */
121 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
122 fp
= fopen(path
, "r");
127 /* Caller must free() *cmdline */
128 cmdline
= malloc(PATH_MAX
);
130 perror("malloc cmdline");
133 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
135 perror("fread proc list");
146 const char *active_string(int value
)
149 case 0: return "inactive";
150 case 1: return "active";
152 default: return NULL
;
156 static const char *snapshot_string(int value
)
167 const char *enabled_string(int value
)
170 case 0: return " [disabled]";
171 case 1: return " [enabled]";
173 default: return NULL
;
178 const char *filter_string(int value
)
181 case 1: return " [with filter]";
187 const char *exclusion_string(int value
)
190 case 1: return " [has exclusions]";
195 static const char *logleveltype_string(enum lttng_loglevel_type value
)
198 case LTTNG_EVENT_LOGLEVEL_ALL
:
200 case LTTNG_EVENT_LOGLEVEL_RANGE
:
202 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
205 return " <<TYPE UNKN>>";
209 static const char *bitness_event(enum lttng_event_flag flags
)
211 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_32
) {
212 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
213 return " [32/64-bit]";
217 } else if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
225 * Pretty print single event.
227 static void print_events(struct lttng_event
*event
)
229 switch (event
->type
) {
230 case LTTNG_EVENT_TRACEPOINT
:
232 if (event
->loglevel
!= -1) {
233 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
236 logleveltype_string(event
->loglevel_type
),
237 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
239 enabled_string(event
->enabled
),
240 exclusion_string(event
->exclusion
),
241 filter_string(event
->filter
));
243 MSG("%s%s (type: tracepoint)%s%s%s",
246 enabled_string(event
->enabled
),
247 exclusion_string(event
->exclusion
),
248 filter_string(event
->filter
));
252 case LTTNG_EVENT_FUNCTION
:
253 MSG("%s%s (type: function)%s%s", indent6
,
254 event
->name
, enabled_string(event
->enabled
),
255 filter_string(event
->filter
));
256 if (event
->attr
.probe
.addr
!= 0) {
257 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
259 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
260 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
263 case LTTNG_EVENT_PROBE
:
264 MSG("%s%s (type: probe)%s%s", indent6
,
265 event
->name
, enabled_string(event
->enabled
),
266 filter_string(event
->filter
));
267 if (event
->attr
.probe
.addr
!= 0) {
268 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
270 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
271 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
274 case LTTNG_EVENT_FUNCTION_ENTRY
:
275 MSG("%s%s (type: function)%s%s", indent6
,
276 event
->name
, enabled_string(event
->enabled
),
277 filter_string(event
->filter
));
278 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
280 case LTTNG_EVENT_SYSCALL
:
281 MSG("%s%s%s%s%s", indent6
, event
->name
,
282 (opt_syscall
? "" : " (type:syscall)"),
283 enabled_string(event
->enabled
),
284 bitness_event(event
->flags
));
286 case LTTNG_EVENT_NOOP
:
287 MSG("%s (type: noop)%s%s", indent6
,
288 enabled_string(event
->enabled
),
289 filter_string(event
->filter
));
291 case LTTNG_EVENT_ALL
:
292 /* We should never have "all" events in list. */
298 static const char *field_type(struct lttng_event_field
*field
)
300 switch(field
->type
) {
301 case LTTNG_EVENT_FIELD_INTEGER
:
303 case LTTNG_EVENT_FIELD_ENUM
:
305 case LTTNG_EVENT_FIELD_FLOAT
:
307 case LTTNG_EVENT_FIELD_STRING
:
309 case LTTNG_EVENT_FIELD_OTHER
:
310 default: /* fall-through */
316 * Pretty print single event fields.
318 static void print_event_field(struct lttng_event_field
*field
)
320 if (!field
->field_name
[0]) {
323 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
324 field_type(field
), field
->nowrite
? " [no write]" : "");
329 * Jul and ust event listing
331 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
332 struct lttng_domain
*domain
)
336 char *cmdline
= NULL
;
337 int pid_element_open
= 0;
339 /* Open domains element */
340 ret
= mi_lttng_domains_open(writer
);
346 ret
= mi_lttng_domain(writer
, domain
, 1);
351 /* Open pids element */
352 ret
= mi_lttng_pids_open(writer
);
357 for (i
= 0; i
< count
; i
++) {
358 if (cur_pid
!= events
[i
].pid
) {
359 if (pid_element_open
) {
360 /* Close the previous events and pid element */
361 ret
= mi_lttng_close_multi_element(writer
, 2);
365 pid_element_open
= 0;
368 cur_pid
= events
[i
].pid
;
369 cmdline
= get_cmdline_by_pid(cur_pid
);
375 if (!pid_element_open
) {
376 /* Open and write a pid element */
377 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
382 /* Open events element */
383 ret
= mi_lttng_events_open(writer
);
388 pid_element_open
= 1;
394 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
401 ret
= mi_lttng_writer_close_element(writer
);
406 /* Close domain, domains */
407 ret
= mi_lttng_close_multi_element(writer
, 2);
415 static int list_agent_events(void)
417 int i
, size
, ret
= CMD_SUCCESS
;
418 struct lttng_domain domain
;
419 struct lttng_handle
*handle
;
420 struct lttng_event
*event_list
;
422 char *cmdline
= NULL
;
423 const char *agent_domain_str
;
425 memset(&domain
, 0, sizeof(domain
));
427 domain
.type
= LTTNG_DOMAIN_JUL
;
428 } else if (opt_log4j
) {
429 domain
.type
= LTTNG_DOMAIN_LOG4J
;
432 agent_domain_str
= get_domain_str(domain
.type
);
434 DBG("Getting %s tracing events", agent_domain_str
);
436 handle
= lttng_create_handle(NULL
, &domain
);
437 if (handle
== NULL
) {
442 size
= lttng_list_tracepoints(handle
, &event_list
);
444 ERR("Unable to list %s events: %s", agent_domain_str
,
445 lttng_strerror(size
));
452 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
459 MSG("%s events (Logger name):\n-------------------------",
466 for (i
= 0; i
< size
; i
++) {
467 if (cur_pid
!= event_list
[i
].pid
) {
468 cur_pid
= event_list
[i
].pid
;
469 cmdline
= get_cmdline_by_pid(cur_pid
);
470 if (cmdline
== NULL
) {
474 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
477 MSG("%s- %s", indent6
, event_list
[i
].name
);
486 lttng_destroy_handle(handle
);
491 * Ask session daemon for all user space tracepoints available.
493 static int list_ust_events(void)
495 int i
, size
, ret
= CMD_SUCCESS
;
496 struct lttng_domain domain
;
497 struct lttng_handle
*handle
;
498 struct lttng_event
*event_list
;
500 char *cmdline
= NULL
;
502 memset(&domain
, 0, sizeof(domain
));
504 DBG("Getting UST tracing events");
506 domain
.type
= LTTNG_DOMAIN_UST
;
508 handle
= lttng_create_handle(NULL
, &domain
);
509 if (handle
== NULL
) {
514 size
= lttng_list_tracepoints(handle
, &event_list
);
516 ERR("Unable to list UST events: %s", lttng_strerror(size
));
523 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
526 MSG("UST events:\n-------------");
532 for (i
= 0; i
< size
; i
++) {
533 if (cur_pid
!= event_list
[i
].pid
) {
534 cur_pid
= event_list
[i
].pid
;
535 cmdline
= get_cmdline_by_pid(cur_pid
);
536 if (cmdline
== NULL
) {
540 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
543 print_events(&event_list
[i
]);
552 lttng_destroy_handle(handle
);
558 * List all ust event with their fields
560 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
561 struct lttng_domain
*domain
)
565 char *cmdline
= NULL
;
566 int pid_element_open
= 0;
567 int event_element_open
= 0;
568 struct lttng_event cur_event
;
570 /* Open domains element */
571 ret
= mi_lttng_domains_open(writer
);
577 ret
= mi_lttng_domain(writer
, domain
, 1);
582 /* Open pids element */
583 ret
= mi_lttng_pids_open(writer
);
588 for (i
= 0; i
< count
; i
++) {
589 if (cur_pid
!= fields
[i
].event
.pid
) {
590 if (pid_element_open
) {
591 if (event_element_open
) {
593 /* Close the previous field element and event. */
594 ret
= mi_lttng_close_multi_element(writer
, 2);
598 event_element_open
= 0;
600 /* Close the previous events, pid element */
601 ret
= mi_lttng_close_multi_element(writer
, 2);
605 pid_element_open
= 0;
608 cur_pid
= fields
[i
].event
.pid
;
609 cmdline
= get_cmdline_by_pid(cur_pid
);
610 if (!pid_element_open
) {
611 /* Open and write a pid element */
612 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
617 /* Open events element */
618 ret
= mi_lttng_events_open(writer
);
622 pid_element_open
= 1;
625 /* Wipe current event since we are about to print a new PID. */
626 memset(&cur_event
, 0, sizeof(cur_event
));
629 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
630 if (event_element_open
) {
631 /* Close the previous fields element and the previous event */
632 ret
= mi_lttng_close_multi_element(writer
, 2);
636 event_element_open
= 0;
639 memcpy(&cur_event
, &fields
[i
].event
,
642 if (!event_element_open
) {
643 /* Open and write the event */
644 ret
= mi_lttng_event(writer
, &cur_event
, 1,
645 handle
->domain
.type
);
650 /* Open a fields element */
651 ret
= mi_lttng_event_fields_open(writer
);
655 event_element_open
= 1;
659 /* Print the event_field */
660 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
666 /* Close pids, domain, domains */
667 ret
= mi_lttng_close_multi_element(writer
, 3);
676 * Ask session daemon for all user space tracepoint fields available.
678 static int list_ust_event_fields(void)
680 int i
, size
, ret
= CMD_SUCCESS
;
681 struct lttng_domain domain
;
682 struct lttng_handle
*handle
;
683 struct lttng_event_field
*event_field_list
;
685 char *cmdline
= NULL
;
687 struct lttng_event cur_event
;
689 memset(&domain
, 0, sizeof(domain
));
690 memset(&cur_event
, 0, sizeof(cur_event
));
692 DBG("Getting UST tracing event fields");
694 domain
.type
= LTTNG_DOMAIN_UST
;
696 handle
= lttng_create_handle(NULL
, &domain
);
697 if (handle
== NULL
) {
702 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
704 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
711 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
718 MSG("UST events:\n-------------");
724 for (i
= 0; i
< size
; i
++) {
725 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
726 cur_pid
= event_field_list
[i
].event
.pid
;
727 cmdline
= get_cmdline_by_pid(cur_pid
);
728 if (cmdline
== NULL
) {
732 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
734 /* Wipe current event since we are about to print a new PID. */
735 memset(&cur_event
, 0, sizeof(cur_event
));
737 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
738 print_events(&event_field_list
[i
].event
);
739 memcpy(&cur_event
, &event_field_list
[i
].event
,
742 print_event_field(&event_field_list
[i
]);
749 free(event_field_list
);
751 lttng_destroy_handle(handle
);
757 * Print a list of kernel events
759 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
760 struct lttng_domain
*domain
)
764 /* Open domains element */
765 ret
= mi_lttng_domains_open(writer
);
771 ret
= mi_lttng_domain(writer
, domain
, 1);
777 ret
= mi_lttng_events_open(writer
);
782 for (i
= 0; i
< count
; i
++) {
783 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
789 /* close events, domain and domains */
790 ret
= mi_lttng_close_multi_element(writer
, 3);
800 * Ask for all trace events in the kernel
802 static int list_kernel_events(void)
804 int i
, size
, ret
= CMD_SUCCESS
;
805 struct lttng_domain domain
;
806 struct lttng_handle
*handle
;
807 struct lttng_event
*event_list
;
809 memset(&domain
, 0, sizeof(domain
));
811 DBG("Getting kernel tracing events");
813 domain
.type
= LTTNG_DOMAIN_KERNEL
;
815 handle
= lttng_create_handle(NULL
, &domain
);
816 if (handle
== NULL
) {
821 size
= lttng_list_tracepoints(handle
, &event_list
);
823 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
824 lttng_destroy_handle(handle
);
830 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
836 MSG("Kernel events:\n-------------");
838 for (i
= 0; i
< size
; i
++) {
839 print_events(&event_list
[i
]);
848 lttng_destroy_handle(handle
);
852 lttng_destroy_handle(handle
);
858 * Print a list of system calls.
860 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
865 ret
= mi_lttng_events_open(writer
);
870 for (i
= 0; i
< count
; i
++) {
871 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
878 ret
= mi_lttng_writer_close_element(writer
);
888 * Ask for kernel system calls.
890 static int list_syscalls(void)
892 int i
, size
, ret
= CMD_SUCCESS
;
893 struct lttng_event
*event_list
;
895 DBG("Getting kernel system call events");
897 size
= lttng_list_syscalls(&event_list
);
899 ERR("Unable to list system calls: %s", lttng_strerror(size
));
906 ret
= mi_list_syscalls(event_list
, size
);
912 MSG("System calls:\n-------------");
914 for (i
= 0; i
< size
; i
++) {
915 print_events(&event_list
[i
]);
931 * Print a list of agent events
933 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
937 /* Open events element */
938 ret
= mi_lttng_events_open(writer
);
943 for (i
= 0; i
< count
; i
++) {
944 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
950 /* Close events element */
951 ret
= mi_lttng_writer_close_element(writer
);
958 * List agent events for a specific session using the handle.
960 * Return CMD_SUCCESS on success else a negative value.
962 static int list_session_agent_events(void)
964 int ret
= CMD_SUCCESS
, count
, i
;
965 struct lttng_event
*events
= NULL
;
967 count
= lttng_list_events(handle
, "", &events
);
970 ERR("%s", lttng_strerror(count
));
976 ret
= mi_list_session_agent_events(events
, count
);
983 MSG("Events (Logger name):\n---------------------");
985 MSG("%sNone\n", indent6
);
989 for (i
= 0; i
< count
; i
++) {
990 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
991 enabled_string(events
[i
].enabled
),
992 logleveltype_string(events
[i
].loglevel_type
),
993 mi_lttng_loglevel_string(events
[i
].loglevel
,
994 handle
->domain
.type
));
1008 * print a list of event
1010 static int mi_list_events(struct lttng_event
*events
, int count
)
1014 /* Open events element */
1015 ret
= mi_lttng_events_open(writer
);
1020 for (i
= 0; i
< count
; i
++) {
1021 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1027 /* Close events element */
1028 ret
= mi_lttng_writer_close_element(writer
);
1035 * List events of channel of session and domain.
1037 static int list_events(const char *channel_name
)
1039 int ret
= CMD_SUCCESS
, count
, i
;
1040 struct lttng_event
*events
= NULL
;
1042 count
= lttng_list_events(handle
, channel_name
, &events
);
1045 ERR("%s", lttng_strerror(count
));
1051 ret
= mi_list_events(events
, count
);
1058 MSG("\n%sEvents:", indent4
);
1060 MSG("%sNone\n", indent6
);
1064 for (i
= 0; i
< count
; i
++) {
1065 print_events(&events
[i
]);
1077 * Pretty print channel
1079 static void print_channel(struct lttng_channel
*channel
)
1081 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1083 MSG("%sAttributes:", indent4
);
1084 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1085 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1086 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1087 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1088 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1089 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1090 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1091 switch (channel
->attr
.output
) {
1092 case LTTNG_EVENT_SPLICE
:
1093 MSG("%soutput: splice()", indent6
);
1095 case LTTNG_EVENT_MMAP
:
1096 MSG("%soutput: mmap()", indent6
);
1103 * Print a list of channel
1106 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1107 const char *channel_name
)
1110 unsigned int chan_found
= 0;
1112 /* Open channels element */
1113 ret
= mi_lttng_channels_open(writer
);
1118 for (i
= 0; i
< count
; i
++) {
1119 if (channel_name
!= NULL
) {
1120 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1127 /* Write channel element and leave it open */
1128 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1133 /* Listing events per channel */
1134 ret
= list_events(channels
[i
].name
);
1139 /* Closing the channel element we opened earlier */
1140 ret
= mi_lttng_writer_close_element(writer
);
1150 /* Close channels element */
1151 ret
= mi_lttng_writer_close_element(writer
);
1161 * List channel(s) of session and domain.
1163 * If channel_name is NULL, all channels are listed.
1165 static int list_channels(const char *channel_name
)
1167 int count
, i
, ret
= CMD_SUCCESS
;
1168 unsigned int chan_found
= 0;
1169 struct lttng_channel
*channels
= NULL
;
1171 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1173 count
= lttng_list_channels(handle
, &channels
);
1176 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1178 /* When printing mi this is not an error
1179 * but an empty channels element */
1183 WARN("No kernel channel");
1184 goto error_channels
;
1188 /* We had a real error */
1190 ERR("%s", lttng_strerror(count
));
1191 goto error_channels
;
1198 ret
= mi_list_channels(channels
, count
, channel_name
);
1206 MSG("Channels:\n-------------");
1209 for (i
= 0; i
< count
; i
++) {
1210 if (channel_name
!= NULL
) {
1211 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1217 print_channel(&channels
[i
]);
1219 /* Listing events per channel */
1220 ret
= list_events(channels
[i
].name
);
1230 if (!chan_found
&& channel_name
!= NULL
) {
1232 ERR("Channel %s not found", channel_name
);
1245 * Find the session with session_name as name
1246 * and print his informations.
1248 static int mi_list_session(const char *session_name
,
1249 struct lttng_session
*sessions
, int count
)
1252 unsigned int session_found
= 0;
1254 if (session_name
== NULL
) {
1255 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1259 for (i
= 0; i
< count
; i
++) {
1260 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1261 /* We need to leave it open to append other informations
1262 * like domain, channel, events etc.*/
1264 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1272 if (!session_found
) {
1273 ERR("Session '%s' not found", session_name
);
1274 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1284 * List all availables session
1286 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1290 /* Opening sessions element */
1291 ret
= mi_lttng_sessions_open(writer
);
1296 /* Listing sessions */
1297 for (i
= 0; i
< count
; i
++) {
1298 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1304 /* Closing sessions element */
1305 ret
= mi_lttng_writer_close_element(writer
);
1315 * List available tracing session. List only basic information.
1317 * If session_name is NULL, all sessions are listed.
1319 static int list_sessions(const char *session_name
)
1321 int ret
= CMD_SUCCESS
;
1323 unsigned int session_found
= 0;
1324 struct lttng_session
*sessions
;
1326 count
= lttng_list_sessions(&sessions
);
1327 DBG("Session count %d", count
);
1330 ERR("%s", lttng_strerror(count
));
1336 if (session_name
== NULL
) {
1337 /* List all session */
1338 ret
= mi_list_sessions(sessions
, count
);
1340 /* Note : this return an open session element */
1341 ret
= mi_list_session(session_name
, sessions
, count
);
1350 MSG("Currently no available tracing session");
1354 if (session_name
== NULL
) {
1355 MSG("Available tracing sessions:");
1359 for (i
= 0; i
< count
; i
++) {
1360 if (session_name
!= NULL
) {
1361 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1363 MSG("Tracing session %s: [%s%s]", session_name
,
1364 active_string(sessions
[i
].enabled
),
1365 snapshot_string(sessions
[i
].snapshot_mode
));
1366 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1370 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1371 sessions
[i
].name
, sessions
[i
].path
,
1372 active_string(sessions
[i
].enabled
),
1373 snapshot_string(sessions
[i
].snapshot_mode
));
1374 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1375 MSG("%sLive timer interval (usec): %u\n", indent4
,
1376 sessions
[i
].live_timer_interval
);
1380 if (!session_found
&& session_name
!= NULL
) {
1381 ERR("Session '%s' not found", session_name
);
1386 if (session_name
== NULL
) {
1387 MSG("\nUse lttng list <session_name> for more details");
1400 * list available domain(s) for a session.
1402 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1405 /* Open domains element */
1406 ret
= mi_lttng_domains_open(writer
);
1411 for (i
= 0; i
< count
; i
++) {
1412 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1418 /* Closing domains element */
1419 ret
= mi_lttng_writer_close_element(writer
);
1428 * List available domain(s) for a session.
1430 static int list_domains(const char *session_name
)
1432 int i
, count
, ret
= CMD_SUCCESS
;
1433 struct lttng_domain
*domains
= NULL
;
1436 count
= lttng_list_domains(session_name
, &domains
);
1439 ERR("%s", lttng_strerror(count
));
1445 ret
= mi_list_domains(domains
, count
);
1452 MSG("Domains:\n-------------");
1458 for (i
= 0; i
< count
; i
++) {
1459 switch (domains
[i
].type
) {
1460 case LTTNG_DOMAIN_KERNEL
:
1463 case LTTNG_DOMAIN_UST
:
1464 MSG(" - UST global");
1466 case LTTNG_DOMAIN_JUL
:
1467 MSG(" - JUL (Java Util Logging)");
1469 case LTTNG_DOMAIN_LOG4J
:
1470 MSG(" - LOG4j (Logging for Java)");
1486 * The 'list <options>' first level command
1488 int cmd_list(int argc
, const char **argv
)
1490 int opt
, ret
= CMD_SUCCESS
;
1491 const char *session_name
;
1492 static poptContext pc
;
1493 struct lttng_domain domain
;
1494 struct lttng_domain
*domains
= NULL
;
1496 memset(&domain
, 0, sizeof(domain
));
1504 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1505 poptReadDefaultConfig(pc
, 0);
1507 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1515 case OPT_LIST_OPTIONS
:
1516 list_cmd_options(stdout
, long_options
);
1520 ret
= CMD_UNDEFINED
;
1527 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1533 /* Open command element */
1534 ret
= mi_lttng_writer_command_open(writer
,
1535 mi_lttng_element_command_list
);
1541 /* Open output element */
1542 ret
= mi_lttng_writer_open_element(writer
,
1543 mi_lttng_element_command_output
);
1550 /* Get session name (trailing argument) */
1551 session_name
= poptGetArg(pc
);
1552 DBG2("Session name: %s", session_name
);
1555 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1556 } else if (opt_userspace
) {
1557 DBG2("Listing userspace global domain");
1558 domain
.type
= LTTNG_DOMAIN_UST
;
1559 } else if (opt_jul
) {
1560 DBG2("Listing JUL domain");
1561 domain
.type
= LTTNG_DOMAIN_JUL
;
1562 } else if (opt_log4j
) {
1563 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1566 if (!opt_kernel
&& opt_syscall
) {
1567 WARN("--syscall will only work with the Kernel domain (-k)");
1572 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
) {
1573 handle
= lttng_create_handle(session_name
, &domain
);
1574 if (handle
== NULL
) {
1580 if (session_name
== NULL
) {
1581 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
) {
1582 ret
= list_sessions(NULL
);
1589 ret
= list_syscalls();
1594 ret
= list_kernel_events();
1600 if (opt_userspace
) {
1602 ret
= list_ust_event_fields();
1604 ret
= list_ust_events();
1610 if (opt_jul
|| opt_log4j
) {
1611 ret
= list_agent_events();
1617 /* List session attributes */
1619 /* Open element sessions
1620 * Present for xml consistency */
1621 ret
= mi_lttng_sessions_open(writer
);
1626 /* MI: the ouptut of list_sessions is an unclosed session element */
1627 ret
= list_sessions(session_name
);
1632 /* Domain listing */
1634 ret
= list_domains(session_name
);
1638 /* Channel listing */
1639 if (opt_kernel
|| opt_userspace
) {
1641 /* Add of domains and domain element for xml
1642 * consistency and validation
1644 ret
= mi_lttng_domains_open(writer
);
1649 /* Open domain and leave it open for
1650 * nested channels printing */
1651 ret
= mi_lttng_domain(writer
, &domain
, 1);
1658 ret
= list_channels(opt_channel
);
1664 /* Close domain and domain element */
1665 ret
= mi_lttng_close_multi_element(writer
, 2);
1675 /* We want all domain(s) */
1676 nb_domain
= lttng_list_domains(session_name
, &domains
);
1677 if (nb_domain
< 0) {
1679 ERR("%s", lttng_strerror(nb_domain
));
1684 ret
= mi_lttng_domains_open(writer
);
1691 for (i
= 0; i
< nb_domain
; i
++) {
1692 switch (domains
[i
].type
) {
1693 case LTTNG_DOMAIN_KERNEL
:
1694 MSG("=== Domain: Kernel ===\n");
1696 case LTTNG_DOMAIN_UST
:
1697 MSG("=== Domain: UST global ===\n");
1698 MSG("Buffer type: %s\n",
1699 domains
[i
].buf_type
==
1700 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1702 case LTTNG_DOMAIN_JUL
:
1703 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1705 case LTTNG_DOMAIN_LOG4J
:
1706 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1709 MSG("=== Domain: Unimplemented ===\n");
1714 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1721 /* Clean handle before creating a new one */
1723 lttng_destroy_handle(handle
);
1726 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1727 if (handle
== NULL
) {
1732 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1733 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
) {
1734 ret
= list_session_agent_events();
1741 ret
= list_channels(opt_channel
);
1747 /* Close domain element */
1748 ret
= mi_lttng_writer_close_element(writer
);
1757 /* Close the domains, session and sessions element */
1758 ret
= mi_lttng_close_multi_element(writer
, 3);
1769 /* Close output element */
1770 ret
= mi_lttng_writer_close_element(writer
);
1776 /* Command element close */
1777 ret
= mi_lttng_writer_command_close(writer
);
1785 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1786 /* Preserve original error code */
1787 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1792 lttng_destroy_handle(handle
);
1795 poptFreeContext(pc
);