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
;
33 static char *opt_channel
;
34 static int opt_domain
;
35 static int opt_fields
;
37 /* Not implemented yet */
38 static char *opt_cmd_name
;
42 const char *indent4
= " ";
43 const char *indent6
= " ";
44 const char *indent8
= " ";
52 static struct lttng_handle
*handle
;
53 static struct mi_writer
*writer
;
55 static struct poptOption long_options
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
58 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
59 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
61 /* Not implemented yet */
62 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
63 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
65 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
67 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
68 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
69 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
70 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
77 static void usage(FILE *ofp
)
79 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
81 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
83 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
84 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
86 fprintf(ofp
, " -h, --help Show this help\n");
87 fprintf(ofp
, " --list-options Simple listing of options\n");
88 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
89 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
90 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
91 fprintf(ofp
, " -f, --fields List event fields.\n");
93 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
96 fprintf(ofp
, "Session Options:\n");
97 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
98 fprintf(ofp
, " -d, --domain List available domain(s)\n");
103 * Get command line from /proc for a specific pid.
105 * On success, return an allocated string pointer to the proc cmdline.
106 * On error, return NULL.
108 static char *get_cmdline_by_pid(pid_t pid
)
112 char *cmdline
= NULL
;
113 char path
[20]; /* Can't go bigger than /proc/65535/cmdline */
115 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
116 fp
= fopen(path
, "r");
121 /* Caller must free() *cmdline */
122 cmdline
= malloc(PATH_MAX
);
124 perror("malloc cmdline");
127 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
129 perror("fread proc list");
138 const char *active_string(int value
)
141 case 0: return "inactive";
142 case 1: return "active";
144 default: return NULL
;
148 static const char *snapshot_string(int value
)
159 const char *enabled_string(int value
)
162 case 0: return " [disabled]";
163 case 1: return " [enabled]";
165 default: return NULL
;
170 const char *filter_string(int value
)
173 case 1: return " [with filter]";
179 const char *exclusion_string(int value
)
182 case 1: return " [has exclusions]";
187 static const char *logleveltype_string(enum lttng_loglevel_type value
)
190 case LTTNG_EVENT_LOGLEVEL_ALL
:
192 case LTTNG_EVENT_LOGLEVEL_RANGE
:
194 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
197 return " <<TYPE UNKN>>";
202 * Pretty print single event.
204 static void print_events(struct lttng_event
*event
)
206 switch (event
->type
) {
207 case LTTNG_EVENT_TRACEPOINT
:
209 if (event
->loglevel
!= -1) {
210 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
213 logleveltype_string(event
->loglevel_type
),
214 mi_lttng_loglevel_string(event
->loglevel
),
216 enabled_string(event
->enabled
),
217 exclusion_string(event
->exclusion
),
218 filter_string(event
->filter
));
220 MSG("%s%s (type: tracepoint)%s%s%s",
223 enabled_string(event
->enabled
),
224 exclusion_string(event
->exclusion
),
225 filter_string(event
->filter
));
229 case LTTNG_EVENT_FUNCTION
:
230 MSG("%s%s (type: function)%s%s", indent6
,
231 event
->name
, enabled_string(event
->enabled
),
232 filter_string(event
->filter
));
233 if (event
->attr
.probe
.addr
!= 0) {
234 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
236 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
237 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
240 case LTTNG_EVENT_PROBE
:
241 MSG("%s%s (type: probe)%s%s", indent6
,
242 event
->name
, enabled_string(event
->enabled
),
243 filter_string(event
->filter
));
244 if (event
->attr
.probe
.addr
!= 0) {
245 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
247 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
248 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
251 case LTTNG_EVENT_FUNCTION_ENTRY
:
252 MSG("%s%s (type: function)%s%s", indent6
,
253 event
->name
, enabled_string(event
->enabled
),
254 filter_string(event
->filter
));
255 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
257 case LTTNG_EVENT_SYSCALL
:
258 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
259 enabled_string(event
->enabled
),
260 filter_string(event
->filter
));
262 case LTTNG_EVENT_NOOP
:
263 MSG("%s (type: noop)%s%s", indent6
,
264 enabled_string(event
->enabled
),
265 filter_string(event
->filter
));
267 case LTTNG_EVENT_ALL
:
268 /* We should never have "all" events in list. */
274 static const char *field_type(struct lttng_event_field
*field
)
276 switch(field
->type
) {
277 case LTTNG_EVENT_FIELD_INTEGER
:
279 case LTTNG_EVENT_FIELD_ENUM
:
281 case LTTNG_EVENT_FIELD_FLOAT
:
283 case LTTNG_EVENT_FIELD_STRING
:
285 case LTTNG_EVENT_FIELD_OTHER
:
286 default: /* fall-through */
292 * Pretty print single event fields.
294 static void print_event_field(struct lttng_event_field
*field
)
296 if (!field
->field_name
[0]) {
299 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
300 field_type(field
), field
->nowrite
? " [no write]" : "");
305 * Jul and ust event listing
307 static int mi_list_jul_ust_events(struct lttng_event
*events
, int count
,
308 struct lttng_domain
*domain
)
312 char *cmdline
= NULL
;
313 int pid_element_open
= 0;
315 /* Open domains element */
316 ret
= mi_lttng_domains_open(writer
);
322 ret
= mi_lttng_domain(writer
, domain
, 1);
327 /* Open pids element */
328 ret
= mi_lttng_pids_open(writer
);
333 for (i
= 0; i
< count
; i
++) {
334 if (cur_pid
!= events
[i
].pid
) {
335 if (pid_element_open
) {
336 /* Close the previous events and pid element */
337 ret
= mi_lttng_close_multi_element(writer
, 2);
341 pid_element_open
= 0;
344 cur_pid
= events
[i
].pid
;
345 cmdline
= get_cmdline_by_pid(cur_pid
);
351 if (!pid_element_open
) {
352 /* Open and write a pid element */
353 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
358 /* Open events element */
359 ret
= mi_lttng_events_open(writer
);
364 pid_element_open
= 1;
370 ret
= mi_lttng_event(writer
, &events
[i
], 0);
377 ret
= mi_lttng_writer_close_element(writer
);
382 /* Close domain, domains */
383 ret
= mi_lttng_close_multi_element(writer
, 2);
391 static int list_jul_events(void)
393 int i
, size
, ret
= CMD_SUCCESS
;
394 struct lttng_domain domain
;
395 struct lttng_handle
*handle
;
396 struct lttng_event
*event_list
;
398 char *cmdline
= NULL
;
400 DBG("Getting JUL tracing events");
402 memset(&domain
, 0, sizeof(domain
));
403 domain
.type
= LTTNG_DOMAIN_JUL
;
405 handle
= lttng_create_handle(NULL
, &domain
);
406 if (handle
== NULL
) {
411 size
= lttng_list_tracepoints(handle
, &event_list
);
413 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
420 ret
= mi_list_jul_ust_events(event_list
, size
, &domain
);
427 MSG("JUL events (Logger name):\n-------------------------");
433 for (i
= 0; i
< size
; i
++) {
434 if (cur_pid
!= event_list
[i
].pid
) {
435 cur_pid
= event_list
[i
].pid
;
436 cmdline
= get_cmdline_by_pid(cur_pid
);
437 if (cmdline
== NULL
) {
441 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
444 MSG("%s- %s", indent6
, event_list
[i
].name
);
453 lttng_destroy_handle(handle
);
458 * Ask session daemon for all user space tracepoints available.
460 static int list_ust_events(void)
462 int i
, size
, ret
= CMD_SUCCESS
;
463 struct lttng_domain domain
;
464 struct lttng_handle
*handle
;
465 struct lttng_event
*event_list
;
467 char *cmdline
= NULL
;
469 memset(&domain
, 0, sizeof(domain
));
471 DBG("Getting UST tracing events");
473 domain
.type
= LTTNG_DOMAIN_UST
;
475 handle
= lttng_create_handle(NULL
, &domain
);
476 if (handle
== NULL
) {
481 size
= lttng_list_tracepoints(handle
, &event_list
);
483 ERR("Unable to list UST events: %s", lttng_strerror(size
));
490 ret
= mi_list_jul_ust_events(event_list
, size
, &domain
);
493 MSG("UST events:\n-------------");
499 for (i
= 0; i
< size
; i
++) {
500 if (cur_pid
!= event_list
[i
].pid
) {
501 cur_pid
= event_list
[i
].pid
;
502 cmdline
= get_cmdline_by_pid(cur_pid
);
503 if (cmdline
== NULL
) {
507 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
510 print_events(&event_list
[i
]);
519 lttng_destroy_handle(handle
);
525 * List all ust event with their fields
527 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
528 struct lttng_domain
*domain
)
532 char *cmdline
= NULL
;
533 int pid_element_open
= 0;
534 int event_element_open
= 0;
535 struct lttng_event cur_event
;
537 /* Open domains element */
538 ret
= mi_lttng_domains_open(writer
);
544 ret
= mi_lttng_domain(writer
, domain
, 1);
549 /* Open pids element */
550 ret
= mi_lttng_pids_open(writer
);
555 for (i
= 0; i
< count
; i
++) {
556 if (cur_pid
!= fields
[i
].event
.pid
) {
557 if (pid_element_open
) {
558 if (event_element_open
) {
560 /* Close the previous field element and event. */
561 ret
= mi_lttng_close_multi_element(writer
, 2);
565 event_element_open
= 0;
567 /* Close the previous events, pid element */
568 ret
= mi_lttng_close_multi_element(writer
, 2);
572 pid_element_open
= 0;
575 cur_pid
= fields
[i
].event
.pid
;
576 cmdline
= get_cmdline_by_pid(cur_pid
);
577 if (!pid_element_open
) {
578 /* Open and write a pid element */
579 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
584 /* Open events element */
585 ret
= mi_lttng_events_open(writer
);
589 pid_element_open
= 1;
592 /* Wipe current event since we are about to print a new PID. */
593 memset(&cur_event
, 0, sizeof(cur_event
));
596 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
597 if (event_element_open
) {
598 /* Close the previous fields element and the previous event */
599 ret
= mi_lttng_close_multi_element(writer
, 2);
603 event_element_open
= 0;
606 memcpy(&cur_event
, &fields
[i
].event
,
609 if (!event_element_open
) {
610 /* Open and write the event */
611 ret
= mi_lttng_event(writer
, &cur_event
, 1);
616 /* Open a fields element */
617 ret
= mi_lttng_event_fields_open(writer
);
621 event_element_open
= 1;
625 /* Print the event_field */
626 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
632 /* Close pids, domain, domains */
633 ret
= mi_lttng_close_multi_element(writer
, 3);
642 * Ask session daemon for all user space tracepoint fields available.
644 static int list_ust_event_fields(void)
646 int i
, size
, ret
= CMD_SUCCESS
;
647 struct lttng_domain domain
;
648 struct lttng_handle
*handle
;
649 struct lttng_event_field
*event_field_list
;
651 char *cmdline
= NULL
;
653 struct lttng_event cur_event
;
655 memset(&domain
, 0, sizeof(domain
));
656 memset(&cur_event
, 0, sizeof(cur_event
));
658 DBG("Getting UST tracing event fields");
660 domain
.type
= LTTNG_DOMAIN_UST
;
662 handle
= lttng_create_handle(NULL
, &domain
);
663 if (handle
== NULL
) {
668 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
670 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
677 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
684 MSG("UST events:\n-------------");
690 for (i
= 0; i
< size
; i
++) {
691 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
692 cur_pid
= event_field_list
[i
].event
.pid
;
693 cmdline
= get_cmdline_by_pid(cur_pid
);
694 if (cmdline
== NULL
) {
698 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
700 /* Wipe current event since we are about to print a new PID. */
701 memset(&cur_event
, 0, sizeof(cur_event
));
703 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
704 print_events(&event_field_list
[i
].event
);
705 memcpy(&cur_event
, &event_field_list
[i
].event
,
708 print_event_field(&event_field_list
[i
]);
715 free(event_field_list
);
717 lttng_destroy_handle(handle
);
723 * Print a list of kernel events
725 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
726 struct lttng_domain
*domain
)
730 /* Open domains element */
731 ret
= mi_lttng_domains_open(writer
);
737 ret
= mi_lttng_domain(writer
, domain
, 1);
743 ret
= mi_lttng_events_open(writer
);
748 for (i
= 0; i
< count
; i
++) {
749 mi_lttng_event(writer
, &events
[i
], 0);
755 /* close events, domain and domains */
756 ret
= mi_lttng_close_multi_element(writer
, 3);
766 * Ask for all trace events in the kernel
768 static int list_kernel_events(void)
770 int i
, size
, ret
= CMD_SUCCESS
;
771 struct lttng_domain domain
;
772 struct lttng_handle
*handle
;
773 struct lttng_event
*event_list
;
775 memset(&domain
, 0, sizeof(domain
));
777 DBG("Getting kernel tracing events");
779 domain
.type
= LTTNG_DOMAIN_KERNEL
;
781 handle
= lttng_create_handle(NULL
, &domain
);
782 if (handle
== NULL
) {
787 size
= lttng_list_tracepoints(handle
, &event_list
);
789 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
790 lttng_destroy_handle(handle
);
796 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
802 MSG("Kernel events:\n-------------");
804 for (i
= 0; i
< size
; i
++) {
805 print_events(&event_list
[i
]);
814 lttng_destroy_handle(handle
);
818 lttng_destroy_handle(handle
);
824 * Print a list of jul events
826 static int mi_list_session_jul_events(struct lttng_event
*events
, int count
)
830 /* Open events element */
831 ret
= mi_lttng_events_open(writer
);
836 for (i
= 0; i
< count
; i
++) {
837 ret
= mi_lttng_event(writer
, &events
[i
], 0);
843 /* Close events element */
844 ret
= mi_lttng_writer_close_element(writer
);
851 * List JUL events for a specific session using the handle.
853 * Return CMD_SUCCESS on success else a negative value.
855 static int list_session_jul_events(void)
857 int ret
= CMD_SUCCESS
, count
, i
;
858 struct lttng_event
*events
= NULL
;
860 count
= lttng_list_events(handle
, "", &events
);
863 ERR("%s", lttng_strerror(count
));
869 ret
= mi_list_session_jul_events(events
, count
);
876 MSG("Events (Logger name):\n---------------------");
878 MSG("%sNone\n", indent6
);
882 for (i
= 0; i
< count
; i
++) {
883 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
884 enabled_string(events
[i
].enabled
),
885 logleveltype_string(events
[i
].loglevel_type
),
886 mi_lttng_loglevel_string(events
[i
].loglevel
));
900 * print a list of event
902 static int mi_list_events(struct lttng_event
*events
, int count
)
906 /* Open events element */
907 ret
= mi_lttng_events_open(writer
);
912 for (i
= 0; i
< count
; i
++) {
913 ret
= mi_lttng_event(writer
, &events
[i
], 0);
919 /* Close events element */
920 ret
= mi_lttng_writer_close_element(writer
);
927 * List events of channel of session and domain.
929 static int list_events(const char *channel_name
)
931 int ret
= CMD_SUCCESS
, count
, i
;
932 struct lttng_event
*events
= NULL
;
934 count
= lttng_list_events(handle
, channel_name
, &events
);
937 ERR("%s", lttng_strerror(count
));
943 ret
= mi_list_events(events
, count
);
950 MSG("\n%sEvents:", indent4
);
952 MSG("%sNone\n", indent6
);
956 for (i
= 0; i
< count
; i
++) {
957 print_events(&events
[i
]);
969 * Pretty print channel
971 static void print_channel(struct lttng_channel
*channel
)
973 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
975 MSG("%sAttributes:", indent4
);
976 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
977 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
978 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
979 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
980 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
981 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
982 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
983 switch (channel
->attr
.output
) {
984 case LTTNG_EVENT_SPLICE
:
985 MSG("%soutput: splice()", indent6
);
987 case LTTNG_EVENT_MMAP
:
988 MSG("%soutput: mmap()", indent6
);
995 * Print a list of channel
998 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
999 const char *channel_name
)
1002 unsigned int chan_found
= 0;
1004 /* Open channels element */
1005 ret
= mi_lttng_channels_open(writer
);
1010 for (i
= 0; i
< count
; i
++) {
1011 if (channel_name
!= NULL
) {
1012 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1019 /* Write channel element and leave it open */
1020 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1025 /* Listing events per channel */
1026 ret
= list_events(channels
[i
].name
);
1031 /* Closing the channel element we opened earlier */
1032 ret
= mi_lttng_writer_close_element(writer
);
1042 /* Close channels element */
1043 ret
= mi_lttng_writer_close_element(writer
);
1053 * List channel(s) of session and domain.
1055 * If channel_name is NULL, all channels are listed.
1057 static int list_channels(const char *channel_name
)
1059 int count
, i
, ret
= CMD_SUCCESS
;
1060 unsigned int chan_found
= 0;
1061 struct lttng_channel
*channels
= NULL
;
1063 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1065 count
= lttng_list_channels(handle
, &channels
);
1068 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1070 /* When printing mi this is not an error
1071 * but an empty channels element */
1075 WARN("No kernel channel");
1076 goto error_channels
;
1080 /* We had a real error */
1082 ERR("%s", lttng_strerror(count
));
1083 goto error_channels
;
1090 ret
= mi_list_channels(channels
, count
, channel_name
);
1097 if (channel_name
== NULL
) {
1098 MSG("Channels:\n-------------");
1101 for (i
= 0; i
< count
; i
++) {
1102 if (channel_name
!= NULL
) {
1103 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1109 print_channel(&channels
[i
]);
1111 /* Listing events per channel */
1112 ret
= list_events(channels
[i
].name
);
1122 if (!chan_found
&& channel_name
!= NULL
) {
1124 ERR("Channel %s not found", channel_name
);
1137 * Find the session with session_name as name
1138 * and print his informations.
1140 static int mi_list_session(const char *session_name
,
1141 struct lttng_session
*sessions
, int count
)
1144 unsigned int session_found
= 0;
1146 if (session_name
== NULL
) {
1147 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1151 for (i
= 0; i
< count
; i
++) {
1152 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1153 /* We need to leave it open to append other informations
1154 * like domain, channel, events etc.*/
1156 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1164 if (!session_found
) {
1165 ERR("Session '%s' not found", session_name
);
1166 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1176 * List all availables session
1178 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1182 /* Opening sessions element */
1183 ret
= mi_lttng_sessions_open(writer
);
1188 /* Listing sessions */
1189 for (i
= 0; i
< count
; i
++) {
1190 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1196 /* Closing sessions element */
1197 ret
= mi_lttng_writer_close_element(writer
);
1207 * List available tracing session. List only basic information.
1209 * If session_name is NULL, all sessions are listed.
1211 static int list_sessions(const char *session_name
)
1213 int ret
= CMD_SUCCESS
;
1215 unsigned int session_found
= 0;
1216 struct lttng_session
*sessions
;
1218 count
= lttng_list_sessions(&sessions
);
1219 DBG("Session count %d", count
);
1222 ERR("%s", lttng_strerror(count
));
1228 if (session_name
== NULL
) {
1229 /* List all session */
1230 ret
= mi_list_sessions(sessions
, count
);
1232 /* Note : this return an open session element */
1233 ret
= mi_list_session(session_name
, sessions
, count
);
1242 MSG("Currently no available tracing session");
1247 if (session_name
== NULL
) {
1248 MSG("Available tracing sessions:");
1252 for (i
= 0; i
< count
; i
++) {
1253 if (session_name
!= NULL
) {
1254 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1256 MSG("Tracing session %s: [%s%s]", session_name
,
1257 active_string(sessions
[i
].enabled
),
1258 snapshot_string(sessions
[i
].snapshot_mode
));
1259 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1263 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1264 sessions
[i
].name
, sessions
[i
].path
,
1265 active_string(sessions
[i
].enabled
),
1266 snapshot_string(sessions
[i
].snapshot_mode
));
1267 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1268 MSG("%sLive timer interval (usec): %u\n", indent4
,
1269 sessions
[i
].live_timer_interval
);
1274 if (!session_found
&& session_name
!= NULL
) {
1275 ERR("Session '%s' not found", session_name
);
1280 if (session_name
== NULL
) {
1281 MSG("\nUse lttng list <session_name> for more details");
1294 * list available domain(s) for a session.
1296 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1299 /* Open domains element */
1300 ret
= mi_lttng_domains_open(writer
);
1305 for (i
= 0; i
< count
; i
++) {
1306 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1312 /* Closing domains element */
1313 ret
= mi_lttng_writer_close_element(writer
);
1322 * List available domain(s) for a session.
1324 static int list_domains(const char *session_name
)
1326 int i
, count
, ret
= CMD_SUCCESS
;
1327 struct lttng_domain
*domains
= NULL
;
1330 count
= lttng_list_domains(session_name
, &domains
);
1333 ERR("%s", lttng_strerror(count
));
1339 ret
= mi_list_domains(domains
, count
);
1346 MSG("Domains:\n-------------");
1352 for (i
= 0; i
< count
; i
++) {
1353 switch (domains
[i
].type
) {
1354 case LTTNG_DOMAIN_KERNEL
:
1357 case LTTNG_DOMAIN_UST
:
1358 MSG(" - UST global");
1360 case LTTNG_DOMAIN_JUL
:
1361 MSG(" - JUL (Java Util Logging)");
1377 * The 'list <options>' first level command
1379 int cmd_list(int argc
, const char **argv
)
1381 int opt
, ret
= CMD_SUCCESS
;
1382 const char *session_name
;
1383 static poptContext pc
;
1384 struct lttng_domain domain
;
1385 struct lttng_domain
*domains
= NULL
;
1387 memset(&domain
, 0, sizeof(domain
));
1395 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1396 poptReadDefaultConfig(pc
, 0);
1398 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1406 case OPT_LIST_OPTIONS
:
1407 list_cmd_options(stdout
, long_options
);
1411 ret
= CMD_UNDEFINED
;
1418 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1424 /* Open command element */
1425 ret
= mi_lttng_writer_command_open(writer
,
1426 mi_lttng_element_command_list
);
1432 /* Open output element */
1433 ret
= mi_lttng_writer_open_element(writer
,
1434 mi_lttng_element_command_output
);
1441 /* Get session name (trailing argument) */
1442 session_name
= poptGetArg(pc
);
1443 DBG2("Session name: %s", session_name
);
1446 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1447 } else if (opt_userspace
) {
1448 DBG2("Listing userspace global domain");
1449 domain
.type
= LTTNG_DOMAIN_UST
;
1450 } else if (opt_jul
) {
1451 DBG2("Listing JUL domain");
1452 domain
.type
= LTTNG_DOMAIN_JUL
;
1455 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
1456 handle
= lttng_create_handle(session_name
, &domain
);
1457 if (handle
== NULL
) {
1463 if (session_name
== NULL
) {
1464 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
1465 ret
= list_sessions(NULL
);
1471 ret
= list_kernel_events();
1476 if (opt_userspace
) {
1478 ret
= list_ust_event_fields();
1480 ret
= list_ust_events();
1487 ret
= list_jul_events();
1493 /* List session attributes */
1495 /* Open element sessions
1496 * Present for xml consistency */
1497 ret
= mi_lttng_sessions_open(writer
);
1502 /* MI: the ouptut of list_sessions is an unclosed session element */
1503 ret
= list_sessions(session_name
);
1508 /* Domain listing */
1510 ret
= list_domains(session_name
);
1514 /* Channel listing */
1515 if (opt_kernel
|| opt_userspace
) {
1517 /* Add of domains and domain element for xml
1518 * consistency and validation
1520 ret
= mi_lttng_domains_open(writer
);
1525 /* Open domain and leave it open for
1526 * nested channels printing */
1527 ret
= mi_lttng_domain(writer
, &domain
, 1);
1534 ret
= list_channels(opt_channel
);
1540 /* Close domain and domain element */
1541 ret
= mi_lttng_close_multi_element(writer
, 2);
1551 /* We want all domain(s) */
1552 nb_domain
= lttng_list_domains(session_name
, &domains
);
1553 if (nb_domain
< 0) {
1555 ERR("%s", lttng_strerror(nb_domain
));
1560 ret
= mi_lttng_domains_open(writer
);
1567 for (i
= 0; i
< nb_domain
; i
++) {
1568 switch (domains
[i
].type
) {
1569 case LTTNG_DOMAIN_KERNEL
:
1570 MSG("=== Domain: Kernel ===\n");
1572 case LTTNG_DOMAIN_UST
:
1573 MSG("=== Domain: UST global ===\n");
1574 MSG("Buffer type: %s\n",
1575 domains
[i
].buf_type
==
1576 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1578 case LTTNG_DOMAIN_JUL
:
1579 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1582 MSG("=== Domain: Unimplemented ===\n");
1587 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1594 /* Clean handle before creating a new one */
1596 lttng_destroy_handle(handle
);
1599 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1600 if (handle
== NULL
) {
1605 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
1606 ret
= list_session_jul_events();
1613 ret
= list_channels(opt_channel
);
1619 /* Close domain element */
1620 ret
= mi_lttng_writer_close_element(writer
);
1629 /* Close the domains, session and sessions element */
1630 ret
= mi_lttng_close_multi_element(writer
, 3);
1641 /* Close output element */
1642 ret
= mi_lttng_writer_close_element(writer
);
1648 /* Command element close */
1649 ret
= mi_lttng_writer_command_close(writer
);
1657 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1658 /* Preserve original error code */
1659 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1664 lttng_destroy_handle(handle
);
1667 poptFreeContext(pc
);