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
),
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);
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);
649 /* Open a fields element */
650 ret
= mi_lttng_event_fields_open(writer
);
654 event_element_open
= 1;
658 /* Print the event_field */
659 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
665 /* Close pids, domain, domains */
666 ret
= mi_lttng_close_multi_element(writer
, 3);
675 * Ask session daemon for all user space tracepoint fields available.
677 static int list_ust_event_fields(void)
679 int i
, size
, ret
= CMD_SUCCESS
;
680 struct lttng_domain domain
;
681 struct lttng_handle
*handle
;
682 struct lttng_event_field
*event_field_list
;
684 char *cmdline
= NULL
;
686 struct lttng_event cur_event
;
688 memset(&domain
, 0, sizeof(domain
));
689 memset(&cur_event
, 0, sizeof(cur_event
));
691 DBG("Getting UST tracing event fields");
693 domain
.type
= LTTNG_DOMAIN_UST
;
695 handle
= lttng_create_handle(NULL
, &domain
);
696 if (handle
== NULL
) {
701 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
703 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
710 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
717 MSG("UST events:\n-------------");
723 for (i
= 0; i
< size
; i
++) {
724 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
725 cur_pid
= event_field_list
[i
].event
.pid
;
726 cmdline
= get_cmdline_by_pid(cur_pid
);
727 if (cmdline
== NULL
) {
731 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
733 /* Wipe current event since we are about to print a new PID. */
734 memset(&cur_event
, 0, sizeof(cur_event
));
736 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
737 print_events(&event_field_list
[i
].event
);
738 memcpy(&cur_event
, &event_field_list
[i
].event
,
741 print_event_field(&event_field_list
[i
]);
748 free(event_field_list
);
750 lttng_destroy_handle(handle
);
756 * Print a list of kernel events
758 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
759 struct lttng_domain
*domain
)
763 /* Open domains element */
764 ret
= mi_lttng_domains_open(writer
);
770 ret
= mi_lttng_domain(writer
, domain
, 1);
776 ret
= mi_lttng_events_open(writer
);
781 for (i
= 0; i
< count
; i
++) {
782 ret
= mi_lttng_event(writer
, &events
[i
], 0);
788 /* close events, domain and domains */
789 ret
= mi_lttng_close_multi_element(writer
, 3);
799 * Ask for all trace events in the kernel
801 static int list_kernel_events(void)
803 int i
, size
, ret
= CMD_SUCCESS
;
804 struct lttng_domain domain
;
805 struct lttng_handle
*handle
;
806 struct lttng_event
*event_list
;
808 memset(&domain
, 0, sizeof(domain
));
810 DBG("Getting kernel tracing events");
812 domain
.type
= LTTNG_DOMAIN_KERNEL
;
814 handle
= lttng_create_handle(NULL
, &domain
);
815 if (handle
== NULL
) {
820 size
= lttng_list_tracepoints(handle
, &event_list
);
822 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
823 lttng_destroy_handle(handle
);
829 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
835 MSG("Kernel events:\n-------------");
837 for (i
= 0; i
< size
; i
++) {
838 print_events(&event_list
[i
]);
847 lttng_destroy_handle(handle
);
851 lttng_destroy_handle(handle
);
857 * Print a list of system calls.
859 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
864 ret
= mi_lttng_events_open(writer
);
869 for (i
= 0; i
< count
; i
++) {
870 ret
= mi_lttng_event(writer
, &events
[i
], 0);
877 ret
= mi_lttng_writer_close_element(writer
);
887 * Ask for kernel system calls.
889 static int list_syscalls(void)
891 int i
, size
, ret
= CMD_SUCCESS
;
892 struct lttng_event
*event_list
;
894 DBG("Getting kernel system call events");
896 size
= lttng_list_syscalls(&event_list
);
898 ERR("Unable to list system calls: %s", lttng_strerror(size
));
905 ret
= mi_list_syscalls(event_list
, size
);
911 MSG("System calls:\n-------------");
913 for (i
= 0; i
< size
; i
++) {
914 print_events(&event_list
[i
]);
930 * Print a list of agent events
932 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
936 /* Open events element */
937 ret
= mi_lttng_events_open(writer
);
942 for (i
= 0; i
< count
; i
++) {
943 ret
= mi_lttng_event(writer
, &events
[i
], 0);
949 /* Close events element */
950 ret
= mi_lttng_writer_close_element(writer
);
957 * List agent events for a specific session using the handle.
959 * Return CMD_SUCCESS on success else a negative value.
961 static int list_session_agent_events(void)
963 int ret
= CMD_SUCCESS
, count
, i
;
964 struct lttng_event
*events
= NULL
;
966 count
= lttng_list_events(handle
, "", &events
);
969 ERR("%s", lttng_strerror(count
));
975 ret
= mi_list_session_agent_events(events
, count
);
982 MSG("Events (Logger name):\n---------------------");
984 MSG("%sNone\n", indent6
);
988 for (i
= 0; i
< count
; i
++) {
989 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
990 enabled_string(events
[i
].enabled
),
991 logleveltype_string(events
[i
].loglevel_type
),
992 mi_lttng_loglevel_string(events
[i
].loglevel
));
1006 * print a list of event
1008 static int mi_list_events(struct lttng_event
*events
, int count
)
1012 /* Open events element */
1013 ret
= mi_lttng_events_open(writer
);
1018 for (i
= 0; i
< count
; i
++) {
1019 ret
= mi_lttng_event(writer
, &events
[i
], 0);
1025 /* Close events element */
1026 ret
= mi_lttng_writer_close_element(writer
);
1033 * List events of channel of session and domain.
1035 static int list_events(const char *channel_name
)
1037 int ret
= CMD_SUCCESS
, count
, i
;
1038 struct lttng_event
*events
= NULL
;
1040 count
= lttng_list_events(handle
, channel_name
, &events
);
1043 ERR("%s", lttng_strerror(count
));
1049 ret
= mi_list_events(events
, count
);
1056 MSG("\n%sEvents:", indent4
);
1058 MSG("%sNone\n", indent6
);
1062 for (i
= 0; i
< count
; i
++) {
1063 print_events(&events
[i
]);
1075 * Pretty print channel
1077 static void print_channel(struct lttng_channel
*channel
)
1079 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1081 MSG("%sAttributes:", indent4
);
1082 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1083 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1084 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1085 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1086 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1087 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1088 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1089 switch (channel
->attr
.output
) {
1090 case LTTNG_EVENT_SPLICE
:
1091 MSG("%soutput: splice()", indent6
);
1093 case LTTNG_EVENT_MMAP
:
1094 MSG("%soutput: mmap()", indent6
);
1101 * Print a list of channel
1104 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1105 const char *channel_name
)
1108 unsigned int chan_found
= 0;
1110 /* Open channels element */
1111 ret
= mi_lttng_channels_open(writer
);
1116 for (i
= 0; i
< count
; i
++) {
1117 if (channel_name
!= NULL
) {
1118 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1125 /* Write channel element and leave it open */
1126 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1131 /* Listing events per channel */
1132 ret
= list_events(channels
[i
].name
);
1137 /* Closing the channel element we opened earlier */
1138 ret
= mi_lttng_writer_close_element(writer
);
1148 /* Close channels element */
1149 ret
= mi_lttng_writer_close_element(writer
);
1159 * List channel(s) of session and domain.
1161 * If channel_name is NULL, all channels are listed.
1163 static int list_channels(const char *channel_name
)
1165 int count
, i
, ret
= CMD_SUCCESS
;
1166 unsigned int chan_found
= 0;
1167 struct lttng_channel
*channels
= NULL
;
1169 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1171 count
= lttng_list_channels(handle
, &channels
);
1174 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1176 /* When printing mi this is not an error
1177 * but an empty channels element */
1181 WARN("No kernel channel");
1182 goto error_channels
;
1186 /* We had a real error */
1188 ERR("%s", lttng_strerror(count
));
1189 goto error_channels
;
1196 ret
= mi_list_channels(channels
, count
, channel_name
);
1203 if (channel_name
== NULL
) {
1204 MSG("Channels:\n-------------");
1207 for (i
= 0; i
< count
; i
++) {
1208 if (channel_name
!= NULL
) {
1209 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1215 print_channel(&channels
[i
]);
1217 /* Listing events per channel */
1218 ret
= list_events(channels
[i
].name
);
1228 if (!chan_found
&& channel_name
!= NULL
) {
1230 ERR("Channel %s not found", channel_name
);
1243 * Find the session with session_name as name
1244 * and print his informations.
1246 static int mi_list_session(const char *session_name
,
1247 struct lttng_session
*sessions
, int count
)
1250 unsigned int session_found
= 0;
1252 if (session_name
== NULL
) {
1253 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1257 for (i
= 0; i
< count
; i
++) {
1258 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1259 /* We need to leave it open to append other informations
1260 * like domain, channel, events etc.*/
1262 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1270 if (!session_found
) {
1271 ERR("Session '%s' not found", session_name
);
1272 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1282 * List all availables session
1284 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1288 /* Opening sessions element */
1289 ret
= mi_lttng_sessions_open(writer
);
1294 /* Listing sessions */
1295 for (i
= 0; i
< count
; i
++) {
1296 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1302 /* Closing sessions element */
1303 ret
= mi_lttng_writer_close_element(writer
);
1313 * List available tracing session. List only basic information.
1315 * If session_name is NULL, all sessions are listed.
1317 static int list_sessions(const char *session_name
)
1319 int ret
= CMD_SUCCESS
;
1321 unsigned int session_found
= 0;
1322 struct lttng_session
*sessions
;
1324 count
= lttng_list_sessions(&sessions
);
1325 DBG("Session count %d", count
);
1328 ERR("%s", lttng_strerror(count
));
1334 if (session_name
== NULL
) {
1335 /* List all session */
1336 ret
= mi_list_sessions(sessions
, count
);
1338 /* Note : this return an open session element */
1339 ret
= mi_list_session(session_name
, sessions
, count
);
1348 MSG("Currently no available tracing session");
1352 if (session_name
== NULL
) {
1353 MSG("Available tracing sessions:");
1357 for (i
= 0; i
< count
; i
++) {
1358 if (session_name
!= NULL
) {
1359 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1361 MSG("Tracing session %s: [%s%s]", session_name
,
1362 active_string(sessions
[i
].enabled
),
1363 snapshot_string(sessions
[i
].snapshot_mode
));
1364 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1368 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1369 sessions
[i
].name
, sessions
[i
].path
,
1370 active_string(sessions
[i
].enabled
),
1371 snapshot_string(sessions
[i
].snapshot_mode
));
1372 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1373 MSG("%sLive timer interval (usec): %u\n", indent4
,
1374 sessions
[i
].live_timer_interval
);
1378 if (!session_found
&& session_name
!= NULL
) {
1379 ERR("Session '%s' not found", session_name
);
1384 if (session_name
== NULL
) {
1385 MSG("\nUse lttng list <session_name> for more details");
1398 * list available domain(s) for a session.
1400 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1403 /* Open domains element */
1404 ret
= mi_lttng_domains_open(writer
);
1409 for (i
= 0; i
< count
; i
++) {
1410 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1416 /* Closing domains element */
1417 ret
= mi_lttng_writer_close_element(writer
);
1426 * List available domain(s) for a session.
1428 static int list_domains(const char *session_name
)
1430 int i
, count
, ret
= CMD_SUCCESS
;
1431 struct lttng_domain
*domains
= NULL
;
1434 count
= lttng_list_domains(session_name
, &domains
);
1437 ERR("%s", lttng_strerror(count
));
1443 ret
= mi_list_domains(domains
, count
);
1450 MSG("Domains:\n-------------");
1456 for (i
= 0; i
< count
; i
++) {
1457 switch (domains
[i
].type
) {
1458 case LTTNG_DOMAIN_KERNEL
:
1461 case LTTNG_DOMAIN_UST
:
1462 MSG(" - UST global");
1464 case LTTNG_DOMAIN_JUL
:
1465 MSG(" - JUL (Java Util Logging)");
1467 case LTTNG_DOMAIN_LOG4J
:
1468 MSG(" - LOG4j (Logging for Java)");
1484 * The 'list <options>' first level command
1486 int cmd_list(int argc
, const char **argv
)
1488 int opt
, ret
= CMD_SUCCESS
;
1489 const char *session_name
;
1490 static poptContext pc
;
1491 struct lttng_domain domain
;
1492 struct lttng_domain
*domains
= NULL
;
1494 memset(&domain
, 0, sizeof(domain
));
1502 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1503 poptReadDefaultConfig(pc
, 0);
1505 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1513 case OPT_LIST_OPTIONS
:
1514 list_cmd_options(stdout
, long_options
);
1518 ret
= CMD_UNDEFINED
;
1525 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1531 /* Open command element */
1532 ret
= mi_lttng_writer_command_open(writer
,
1533 mi_lttng_element_command_list
);
1539 /* Open output element */
1540 ret
= mi_lttng_writer_open_element(writer
,
1541 mi_lttng_element_command_output
);
1548 /* Get session name (trailing argument) */
1549 session_name
= poptGetArg(pc
);
1550 DBG2("Session name: %s", session_name
);
1553 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1554 } else if (opt_userspace
) {
1555 DBG2("Listing userspace global domain");
1556 domain
.type
= LTTNG_DOMAIN_UST
;
1557 } else if (opt_jul
) {
1558 DBG2("Listing JUL domain");
1559 domain
.type
= LTTNG_DOMAIN_JUL
;
1560 } else if (opt_log4j
) {
1561 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1564 if (!opt_kernel
&& opt_syscall
) {
1565 WARN("--syscall will only work with the Kernel domain (-k)");
1570 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
) {
1571 handle
= lttng_create_handle(session_name
, &domain
);
1572 if (handle
== NULL
) {
1578 if (session_name
== NULL
) {
1579 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
) {
1580 ret
= list_sessions(NULL
);
1587 ret
= list_syscalls();
1592 ret
= list_kernel_events();
1598 if (opt_userspace
) {
1600 ret
= list_ust_event_fields();
1602 ret
= list_ust_events();
1608 if (opt_jul
|| opt_log4j
) {
1609 ret
= list_agent_events();
1615 /* List session attributes */
1617 /* Open element sessions
1618 * Present for xml consistency */
1619 ret
= mi_lttng_sessions_open(writer
);
1624 /* MI: the ouptut of list_sessions is an unclosed session element */
1625 ret
= list_sessions(session_name
);
1630 /* Domain listing */
1632 ret
= list_domains(session_name
);
1636 /* Channel listing */
1637 if (opt_kernel
|| opt_userspace
) {
1639 /* Add of domains and domain element for xml
1640 * consistency and validation
1642 ret
= mi_lttng_domains_open(writer
);
1647 /* Open domain and leave it open for
1648 * nested channels printing */
1649 ret
= mi_lttng_domain(writer
, &domain
, 1);
1656 ret
= list_channels(opt_channel
);
1662 /* Close domain and domain element */
1663 ret
= mi_lttng_close_multi_element(writer
, 2);
1673 /* We want all domain(s) */
1674 nb_domain
= lttng_list_domains(session_name
, &domains
);
1675 if (nb_domain
< 0) {
1677 ERR("%s", lttng_strerror(nb_domain
));
1682 ret
= mi_lttng_domains_open(writer
);
1689 for (i
= 0; i
< nb_domain
; i
++) {
1690 switch (domains
[i
].type
) {
1691 case LTTNG_DOMAIN_KERNEL
:
1692 MSG("=== Domain: Kernel ===\n");
1694 case LTTNG_DOMAIN_UST
:
1695 MSG("=== Domain: UST global ===\n");
1696 MSG("Buffer type: %s\n",
1697 domains
[i
].buf_type
==
1698 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1700 case LTTNG_DOMAIN_JUL
:
1701 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1703 case LTTNG_DOMAIN_LOG4J
:
1704 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1707 MSG("=== Domain: Unimplemented ===\n");
1712 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1719 /* Clean handle before creating a new one */
1721 lttng_destroy_handle(handle
);
1724 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1725 if (handle
== NULL
) {
1730 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1731 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
) {
1732 ret
= list_session_agent_events();
1739 ret
= list_channels(opt_channel
);
1745 /* Close domain element */
1746 ret
= mi_lttng_writer_close_element(writer
);
1755 /* Close the domains, session and sessions element */
1756 ret
= mi_lttng_close_multi_element(writer
, 3);
1767 /* Close output element */
1768 ret
= mi_lttng_writer_close_element(writer
);
1774 /* Command element close */
1775 ret
= mi_lttng_writer_command_close(writer
);
1783 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1784 /* Preserve original error code */
1785 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1790 lttng_destroy_handle(handle
);
1793 poptFreeContext(pc
);