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 *loglevel_jul_string(int value
)
192 case LTTNG_LOGLEVEL_JUL_OFF
:
194 case LTTNG_LOGLEVEL_JUL_SEVERE
:
196 case LTTNG_LOGLEVEL_JUL_WARNING
:
197 return "JUL_WARNING";
198 case LTTNG_LOGLEVEL_JUL_INFO
:
200 case LTTNG_LOGLEVEL_JUL_CONFIG
:
202 case LTTNG_LOGLEVEL_JUL_FINE
:
204 case LTTNG_LOGLEVEL_JUL_FINER
:
206 case LTTNG_LOGLEVEL_JUL_FINEST
:
208 case LTTNG_LOGLEVEL_JUL_ALL
:
211 return "<<UNKNOWN>>";
215 static const char *logleveltype_string(enum lttng_loglevel_type value
)
218 case LTTNG_EVENT_LOGLEVEL_ALL
:
220 case LTTNG_EVENT_LOGLEVEL_RANGE
:
222 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
225 return " <<TYPE UNKN>>";
230 * Pretty print single event.
232 static void print_events(struct lttng_event
*event
)
234 switch (event
->type
) {
235 case LTTNG_EVENT_TRACEPOINT
:
237 if (event
->loglevel
!= -1) {
238 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
241 logleveltype_string(event
->loglevel_type
),
242 mi_lttng_loglevel_string(event
->loglevel
),
244 enabled_string(event
->enabled
),
245 exclusion_string(event
->exclusion
),
246 filter_string(event
->filter
));
248 MSG("%s%s (type: tracepoint)%s%s%s",
251 enabled_string(event
->enabled
),
252 exclusion_string(event
->exclusion
),
253 filter_string(event
->filter
));
257 case LTTNG_EVENT_FUNCTION
:
258 MSG("%s%s (type: function)%s%s", indent6
,
259 event
->name
, enabled_string(event
->enabled
),
260 filter_string(event
->filter
));
261 if (event
->attr
.probe
.addr
!= 0) {
262 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
264 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
265 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
268 case LTTNG_EVENT_PROBE
:
269 MSG("%s%s (type: probe)%s%s", indent6
,
270 event
->name
, enabled_string(event
->enabled
),
271 filter_string(event
->filter
));
272 if (event
->attr
.probe
.addr
!= 0) {
273 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
275 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
276 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
279 case LTTNG_EVENT_FUNCTION_ENTRY
:
280 MSG("%s%s (type: function)%s%s", indent6
,
281 event
->name
, enabled_string(event
->enabled
),
282 filter_string(event
->filter
));
283 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
285 case LTTNG_EVENT_SYSCALL
:
286 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
287 enabled_string(event
->enabled
),
288 filter_string(event
->filter
));
290 case LTTNG_EVENT_NOOP
:
291 MSG("%s (type: noop)%s%s", indent6
,
292 enabled_string(event
->enabled
),
293 filter_string(event
->filter
));
295 case LTTNG_EVENT_ALL
:
296 /* We should never have "all" events in list. */
302 static const char *field_type(struct lttng_event_field
*field
)
304 switch(field
->type
) {
305 case LTTNG_EVENT_FIELD_INTEGER
:
307 case LTTNG_EVENT_FIELD_ENUM
:
309 case LTTNG_EVENT_FIELD_FLOAT
:
311 case LTTNG_EVENT_FIELD_STRING
:
313 case LTTNG_EVENT_FIELD_OTHER
:
314 default: /* fall-through */
320 * Pretty print single event fields.
322 static void print_event_field(struct lttng_event_field
*field
)
324 if (!field
->field_name
[0]) {
327 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
328 field_type(field
), field
->nowrite
? " [no write]" : "");
333 * Jul and ust event listing
335 static int mi_list_jul_ust_events(struct lttng_event
*events
, int count
,
336 struct lttng_domain
*domain
)
340 char *cmdline
= NULL
;
341 int pid_element_open
= 0;
343 /* Open domains element */
344 ret
= mi_lttng_domains_open(writer
);
350 ret
= mi_lttng_domain(writer
, domain
, 1);
355 /* Open pids element */
356 ret
= mi_lttng_pids_open(writer
);
361 for (i
= 0; i
< count
; i
++) {
362 if (cur_pid
!= events
[i
].pid
) {
363 if (pid_element_open
) {
364 /* Close the previous events and pid element */
365 ret
= mi_lttng_close_multi_element(writer
, 2);
369 pid_element_open
= 0;
372 cur_pid
= events
[i
].pid
;
373 cmdline
= get_cmdline_by_pid(cur_pid
);
379 if (!pid_element_open
) {
380 /* Open and write a pid element */
381 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
386 /* Open events element */
387 ret
= mi_lttng_events_open(writer
);
392 pid_element_open
= 1;
398 ret
= mi_lttng_event(writer
, &events
[i
], 0);
405 ret
= mi_lttng_writer_close_element(writer
);
410 /* Close domain, domains */
411 ret
= mi_lttng_close_multi_element(writer
, 2);
419 static int list_jul_events(void)
421 int i
, size
, ret
= CMD_SUCCESS
;
422 struct lttng_domain domain
;
423 struct lttng_handle
*handle
;
424 struct lttng_event
*event_list
;
426 char *cmdline
= NULL
;
428 DBG("Getting JUL tracing events");
430 memset(&domain
, 0, sizeof(domain
));
431 domain
.type
= LTTNG_DOMAIN_JUL
;
433 handle
= lttng_create_handle(NULL
, &domain
);
434 if (handle
== NULL
) {
439 size
= lttng_list_tracepoints(handle
, &event_list
);
441 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
448 ret
= mi_list_jul_ust_events(event_list
, size
, &domain
);
455 MSG("JUL events (Logger name):\n-------------------------");
461 for (i
= 0; i
< size
; i
++) {
462 if (cur_pid
!= event_list
[i
].pid
) {
463 cur_pid
= event_list
[i
].pid
;
464 cmdline
= get_cmdline_by_pid(cur_pid
);
465 if (cmdline
== NULL
) {
469 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
472 MSG("%s- %s", indent6
, event_list
[i
].name
);
481 lttng_destroy_handle(handle
);
486 * Ask session daemon for all user space tracepoints available.
488 static int list_ust_events(void)
490 int i
, size
, ret
= CMD_SUCCESS
;
491 struct lttng_domain domain
;
492 struct lttng_handle
*handle
;
493 struct lttng_event
*event_list
;
495 char *cmdline
= NULL
;
497 memset(&domain
, 0, sizeof(domain
));
499 DBG("Getting UST tracing events");
501 domain
.type
= LTTNG_DOMAIN_UST
;
503 handle
= lttng_create_handle(NULL
, &domain
);
504 if (handle
== NULL
) {
509 size
= lttng_list_tracepoints(handle
, &event_list
);
511 ERR("Unable to list UST events: %s", lttng_strerror(size
));
518 ret
= mi_list_jul_ust_events(event_list
, size
, &domain
);
521 MSG("UST events:\n-------------");
527 for (i
= 0; i
< size
; i
++) {
528 if (cur_pid
!= event_list
[i
].pid
) {
529 cur_pid
= event_list
[i
].pid
;
530 cmdline
= get_cmdline_by_pid(cur_pid
);
531 if (cmdline
== NULL
) {
535 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
538 print_events(&event_list
[i
]);
547 lttng_destroy_handle(handle
);
553 * List all ust event with their fields
555 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
556 struct lttng_domain
*domain
)
560 char *cmdline
= NULL
;
561 int pid_element_open
= 0;
562 int event_element_open
= 0;
563 struct lttng_event cur_event
;
565 /* Open domains element */
566 ret
= mi_lttng_domains_open(writer
);
572 ret
= mi_lttng_domain(writer
, domain
, 1);
577 /* Open pids element */
578 ret
= mi_lttng_pids_open(writer
);
583 for (i
= 0; i
< count
; i
++) {
584 if (cur_pid
!= fields
[i
].event
.pid
) {
585 if (pid_element_open
) {
586 if (event_element_open
) {
588 * Close the previous fields element
589 * and the previous event
591 ret
= mi_lttng_close_multi_element(writer
, 2);
595 event_element_open
= 0;
597 /* Close the previous events, pid element */
598 ret
= mi_lttng_close_multi_element(writer
, 2);
602 pid_element_open
= 0;
605 cur_pid
= fields
[i
].event
.pid
;
606 cmdline
= get_cmdline_by_pid(cur_pid
);
607 if (!pid_element_open
) {
608 /* Open and write a pid element */
609 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
614 /* Open events element */
615 ret
= mi_lttng_events_open(writer
);
619 pid_element_open
= 1;
622 /* Wipe current event since we are about to print a new PID. */
623 memset(&cur_event
, 0, sizeof(cur_event
));
626 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
627 if (event_element_open
) {
628 /* Close the previous fields element and the previous event */
629 ret
= mi_lttng_close_multi_element(writer
, 2);
633 event_element_open
= 0;
636 memcpy(&cur_event
, &fields
[i
].event
,
639 if (!event_element_open
) {
640 /* Open and write the event */
641 ret
= mi_lttng_event(writer
, &cur_event
, 1);
646 /* Open a fields element */
647 ret
= mi_lttng_event_fields_open(writer
);
651 event_element_open
= 1;
655 /* Print the event_field */
656 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
662 /* Close pids, domain, domains */
663 ret
= mi_lttng_close_multi_element(writer
, 3);
672 * Ask session daemon for all user space tracepoint fields available.
674 static int list_ust_event_fields(void)
676 int i
, size
, ret
= CMD_SUCCESS
;
677 struct lttng_domain domain
;
678 struct lttng_handle
*handle
;
679 struct lttng_event_field
*event_field_list
;
681 char *cmdline
= NULL
;
683 struct lttng_event cur_event
;
685 memset(&domain
, 0, sizeof(domain
));
686 memset(&cur_event
, 0, sizeof(cur_event
));
688 DBG("Getting UST tracing event fields");
690 domain
.type
= LTTNG_DOMAIN_UST
;
692 handle
= lttng_create_handle(NULL
, &domain
);
693 if (handle
== NULL
) {
698 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
700 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
707 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
714 MSG("UST events:\n-------------");
720 for (i
= 0; i
< size
; i
++) {
721 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
722 cur_pid
= event_field_list
[i
].event
.pid
;
723 cmdline
= get_cmdline_by_pid(cur_pid
);
724 if (cmdline
== NULL
) {
728 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
730 /* Wipe current event since we are about to print a new PID. */
731 memset(&cur_event
, 0, sizeof(cur_event
));
733 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
734 print_events(&event_field_list
[i
].event
);
735 memcpy(&cur_event
, &event_field_list
[i
].event
,
738 print_event_field(&event_field_list
[i
]);
745 free(event_field_list
);
747 lttng_destroy_handle(handle
);
753 * Print a list of kernel events
755 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
756 struct lttng_domain
*domain
)
760 /* Open domains element */
761 ret
= mi_lttng_domains_open(writer
);
767 ret
= mi_lttng_domain(writer
, domain
, 1);
773 ret
= mi_lttng_events_open(writer
);
778 for (i
= 0; i
< count
; i
++) {
779 mi_lttng_event(writer
, &events
[i
], 0);
785 /* close events, domain and domains */
786 ret
= mi_lttng_close_multi_element(writer
, 3);
796 * Ask for all trace events in the kernel
798 static int list_kernel_events(void)
800 int i
, size
, ret
= CMD_SUCCESS
;
801 struct lttng_domain domain
;
802 struct lttng_handle
*handle
;
803 struct lttng_event
*event_list
;
805 memset(&domain
, 0, sizeof(domain
));
807 DBG("Getting kernel tracing events");
809 domain
.type
= LTTNG_DOMAIN_KERNEL
;
811 handle
= lttng_create_handle(NULL
, &domain
);
812 if (handle
== NULL
) {
817 size
= lttng_list_tracepoints(handle
, &event_list
);
819 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
820 lttng_destroy_handle(handle
);
826 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
832 MSG("Kernel events:\n-------------");
834 for (i
= 0; i
< size
; i
++) {
835 print_events(&event_list
[i
]);
844 lttng_destroy_handle(handle
);
848 lttng_destroy_handle(handle
);
854 * Print a list of jul events
856 static int mi_list_session_jul_events(struct lttng_event
*events
, int count
)
860 /* Open events element */
861 ret
= mi_lttng_events_open(writer
);
866 for (i
= 0; i
< count
; i
++) {
867 ret
= mi_lttng_event(writer
, &events
[i
], 0);
873 /* Close events element */
874 ret
= mi_lttng_writer_close_element(writer
);
881 * List JUL events for a specific session using the handle.
883 * Return CMD_SUCCESS on success else a negative value.
885 static int list_session_jul_events(void)
887 int ret
= CMD_SUCCESS
, count
, i
;
888 struct lttng_event
*events
= NULL
;
890 count
= lttng_list_events(handle
, "", &events
);
893 ERR("%s", lttng_strerror(count
));
899 ret
= mi_list_session_jul_events(events
, count
);
906 MSG("Events (Logger name):\n---------------------");
908 MSG("%sNone\n", indent6
);
912 for (i
= 0; i
< count
; i
++) {
913 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
914 enabled_string(events
[i
].enabled
),
915 logleveltype_string(events
[i
].loglevel_type
),
916 loglevel_jul_string(events
[i
].loglevel
));
930 * print a list of event
932 static int mi_list_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 events of channel of session and domain.
959 static int list_events(const char *channel_name
)
961 int ret
= CMD_SUCCESS
, count
, i
;
962 struct lttng_event
*events
= NULL
;
964 count
= lttng_list_events(handle
, channel_name
, &events
);
967 ERR("%s", lttng_strerror(count
));
973 ret
= mi_list_events(events
, count
);
980 MSG("\n%sEvents:", indent4
);
982 MSG("%sNone\n", indent6
);
986 for (i
= 0; i
< count
; i
++) {
987 print_events(&events
[i
]);
999 * Pretty print channel
1001 static void print_channel(struct lttng_channel
*channel
)
1003 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1005 MSG("%sAttributes:", indent4
);
1006 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1007 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1008 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1009 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1010 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1011 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1012 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1013 switch (channel
->attr
.output
) {
1014 case LTTNG_EVENT_SPLICE
:
1015 MSG("%soutput: splice()", indent6
);
1017 case LTTNG_EVENT_MMAP
:
1018 MSG("%soutput: mmap()", indent6
);
1025 * Print a list of channel
1028 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1029 const char *channel_name
)
1032 unsigned int chan_found
= 0;
1034 /* Open channels element */
1035 ret
= mi_lttng_channels_open(writer
);
1040 for (i
= 0; i
< count
; i
++) {
1041 if (channel_name
!= NULL
) {
1042 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1049 /* Write channel element and leave it open */
1050 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1055 /* Listing events per channel */
1056 ret
= list_events(channels
[i
].name
);
1061 /* Closing the channel element we opened earlier */
1062 ret
= mi_lttng_writer_close_element(writer
);
1072 /* Close channels element */
1073 ret
= mi_lttng_writer_close_element(writer
);
1083 * List channel(s) of session and domain.
1085 * If channel_name is NULL, all channels are listed.
1087 static int list_channels(const char *channel_name
)
1089 int count
, i
, ret
= CMD_SUCCESS
;
1090 unsigned int chan_found
= 0;
1091 struct lttng_channel
*channels
= NULL
;
1093 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1095 count
= lttng_list_channels(handle
, &channels
);
1098 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1100 /* When printing mi this is not an error
1101 * but an empty channels element */
1105 WARN("No kernel channel");
1106 goto error_channels
;
1110 /* We had a real error */
1112 ERR("%s", lttng_strerror(count
));
1113 goto error_channels
;
1120 ret
= mi_list_channels(channels
, count
, channel_name
);
1127 if (channel_name
== NULL
) {
1128 MSG("Channels:\n-------------");
1131 for (i
= 0; i
< count
; i
++) {
1132 if (channel_name
!= NULL
) {
1133 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1139 print_channel(&channels
[i
]);
1141 /* Listing events per channel */
1142 ret
= list_events(channels
[i
].name
);
1152 if (!chan_found
&& channel_name
!= NULL
) {
1154 ERR("Channel %s not found", channel_name
);
1167 * Find the session with session_name as name
1168 * and print his informations.
1170 static int mi_list_session(const char *session_name
,
1171 struct lttng_session
*sessions
, int count
)
1174 unsigned int session_found
= 0;
1176 if (session_name
== NULL
) {
1177 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1181 for (i
= 0; i
< count
; i
++) {
1182 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1183 /* We need to leave it open to append other informations
1184 * like domain, channel, events etc.*/
1186 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1194 if (!session_found
) {
1195 ERR("Session '%s' not found", session_name
);
1196 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1206 * List all availables session
1208 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1212 /* Opening sessions element */
1213 ret
= mi_lttng_sessions_open(writer
);
1218 /* Listing sessions */
1219 for (i
= 0; i
< count
; i
++) {
1220 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1226 /* Closing sessions element */
1227 ret
= mi_lttng_writer_close_element(writer
);
1237 * List available tracing session. List only basic information.
1239 * If session_name is NULL, all sessions are listed.
1241 static int list_sessions(const char *session_name
)
1243 int ret
= CMD_SUCCESS
;
1245 unsigned int session_found
= 0;
1246 struct lttng_session
*sessions
;
1248 count
= lttng_list_sessions(&sessions
);
1249 DBG("Session count %d", count
);
1252 ERR("%s", lttng_strerror(count
));
1258 if (session_name
== NULL
) {
1259 /* List all session */
1260 ret
= mi_list_sessions(sessions
, count
);
1262 /* Note : this return an open session element */
1263 ret
= mi_list_session(session_name
, sessions
, count
);
1272 MSG("Currently no available tracing session");
1277 if (session_name
== NULL
) {
1278 MSG("Available tracing sessions:");
1282 for (i
= 0; i
< count
; i
++) {
1283 if (session_name
!= NULL
) {
1284 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1286 MSG("Tracing session %s: [%s%s]", session_name
,
1287 active_string(sessions
[i
].enabled
),
1288 snapshot_string(sessions
[i
].snapshot_mode
));
1289 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1293 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1294 sessions
[i
].name
, sessions
[i
].path
,
1295 active_string(sessions
[i
].enabled
),
1296 snapshot_string(sessions
[i
].snapshot_mode
));
1297 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1298 MSG("%sLive timer interval (usec): %u\n", indent4
,
1299 sessions
[i
].live_timer_interval
);
1304 if (!session_found
&& session_name
!= NULL
) {
1305 ERR("Session '%s' not found", session_name
);
1310 if (session_name
== NULL
) {
1311 MSG("\nUse lttng list <session_name> for more details");
1324 * list available domain(s) for a session.
1326 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1329 /* Open domains element */
1330 ret
= mi_lttng_domains_open(writer
);
1335 for (i
= 0; i
< count
; i
++) {
1336 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1342 /* Closing domains element */
1343 ret
= mi_lttng_writer_close_element(writer
);
1352 * List available domain(s) for a session.
1354 static int list_domains(const char *session_name
)
1356 int i
, count
, ret
= CMD_SUCCESS
;
1357 struct lttng_domain
*domains
= NULL
;
1360 count
= lttng_list_domains(session_name
, &domains
);
1363 ERR("%s", lttng_strerror(count
));
1369 ret
= mi_list_domains(domains
, count
);
1376 MSG("Domains:\n-------------");
1382 for (i
= 0; i
< count
; i
++) {
1383 switch (domains
[i
].type
) {
1384 case LTTNG_DOMAIN_KERNEL
:
1387 case LTTNG_DOMAIN_UST
:
1388 MSG(" - UST global");
1390 case LTTNG_DOMAIN_JUL
:
1391 MSG(" - JUL (Java Util Logging)");
1407 * The 'list <options>' first level command
1409 int cmd_list(int argc
, const char **argv
)
1411 int opt
, ret
= CMD_SUCCESS
;
1412 const char *session_name
;
1413 static poptContext pc
;
1414 struct lttng_domain domain
;
1415 struct lttng_domain
*domains
= NULL
;
1417 memset(&domain
, 0, sizeof(domain
));
1425 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1426 poptReadDefaultConfig(pc
, 0);
1428 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1436 case OPT_LIST_OPTIONS
:
1437 list_cmd_options(stdout
, long_options
);
1441 ret
= CMD_UNDEFINED
;
1448 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1454 /* Open command element */
1455 ret
= mi_lttng_writer_command_open(writer
,
1456 mi_lttng_element_command_list
);
1462 /* Open output element */
1463 ret
= mi_lttng_writer_open_element(writer
,
1464 mi_lttng_element_command_output
);
1471 /* Get session name (trailing argument) */
1472 session_name
= poptGetArg(pc
);
1473 DBG2("Session name: %s", session_name
);
1476 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1477 } else if (opt_userspace
) {
1478 DBG2("Listing userspace global domain");
1479 domain
.type
= LTTNG_DOMAIN_UST
;
1480 } else if (opt_jul
) {
1481 DBG2("Listing JUL domain");
1482 domain
.type
= LTTNG_DOMAIN_JUL
;
1485 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
1486 handle
= lttng_create_handle(session_name
, &domain
);
1487 if (handle
== NULL
) {
1493 if (session_name
== NULL
) {
1494 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
1495 ret
= list_sessions(NULL
);
1501 ret
= list_kernel_events();
1506 if (opt_userspace
) {
1508 ret
= list_ust_event_fields();
1510 ret
= list_ust_events();
1517 ret
= list_jul_events();
1523 /* List session attributes */
1525 /* Open element sessions
1526 * Present for xml consistency */
1527 ret
= mi_lttng_sessions_open(writer
);
1532 /* MI: the ouptut of list_sessions is an unclosed session element */
1533 ret
= list_sessions(session_name
);
1538 /* Domain listing */
1540 ret
= list_domains(session_name
);
1544 /* Channel listing */
1545 if (opt_kernel
|| opt_userspace
) {
1547 /* Add of domains and domain element for xml
1548 * consistency and validation
1550 ret
= mi_lttng_domains_open(writer
);
1555 /* Open domain and leave it open for
1556 * nested channels printing */
1557 ret
= mi_lttng_domain(writer
, &domain
, 1);
1564 ret
= list_channels(opt_channel
);
1570 /* Close domain and domain element */
1571 ret
= mi_lttng_close_multi_element(writer
, 2);
1581 /* We want all domain(s) */
1582 nb_domain
= lttng_list_domains(session_name
, &domains
);
1583 if (nb_domain
< 0) {
1585 ERR("%s", lttng_strerror(nb_domain
));
1590 ret
= mi_lttng_domains_open(writer
);
1597 for (i
= 0; i
< nb_domain
; i
++) {
1598 switch (domains
[i
].type
) {
1599 case LTTNG_DOMAIN_KERNEL
:
1600 MSG("=== Domain: Kernel ===\n");
1602 case LTTNG_DOMAIN_UST
:
1603 MSG("=== Domain: UST global ===\n");
1604 MSG("Buffer type: %s\n",
1605 domains
[i
].buf_type
==
1606 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1608 case LTTNG_DOMAIN_JUL
:
1609 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1612 MSG("=== Domain: Unimplemented ===\n");
1617 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1624 /* Clean handle before creating a new one */
1626 lttng_destroy_handle(handle
);
1629 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1630 if (handle
== NULL
) {
1635 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
1636 ret
= list_session_jul_events();
1643 ret
= list_channels(opt_channel
);
1649 /* Close domain element */
1650 ret
= mi_lttng_writer_close_element(writer
);
1659 /* Close the domains, session and sessions element */
1660 ret
= mi_lttng_close_multi_element(writer
, 3);
1671 /* Close output element */
1672 ret
= mi_lttng_writer_close_element(writer
);
1678 /* Command element close */
1679 ret
= mi_lttng_writer_command_close(writer
);
1687 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1688 /* Preserve original error code */
1689 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1694 lttng_destroy_handle(handle
);
1697 poptFreeContext(pc
);