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
;
38 /* Not implemented yet */
39 static char *opt_cmd_name
;
43 const char *indent4
= " ";
44 const char *indent6
= " ";
45 const char *indent8
= " ";
53 static struct lttng_handle
*handle
;
54 static struct mi_writer
*writer
;
56 static struct poptOption long_options
[] = {
57 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
58 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
59 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
60 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
61 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
63 /* Not implemented yet */
64 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
65 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
67 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
69 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
70 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
71 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
72 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
79 static void usage(FILE *ofp
)
81 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
83 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
85 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
86 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
88 fprintf(ofp
, " -h, --help Show this help\n");
89 fprintf(ofp
, " --list-options Simple listing of options\n");
90 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
91 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
92 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
93 fprintf(ofp
, " -f, --fields List event fields.\n");
95 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
98 fprintf(ofp
, "Session Options:\n");
99 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
100 fprintf(ofp
, " -d, --domain List available domain(s)\n");
105 * Get command line from /proc for a specific pid.
107 * On success, return an allocated string pointer to the proc cmdline.
108 * On error, return NULL.
110 static char *get_cmdline_by_pid(pid_t pid
)
114 char *cmdline
= NULL
;
115 char path
[20]; /* Can't go bigger than /proc/65535/cmdline */
117 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
118 fp
= fopen(path
, "r");
123 /* Caller must free() *cmdline */
124 cmdline
= malloc(PATH_MAX
);
126 perror("malloc cmdline");
129 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
131 perror("fread proc list");
140 const char *active_string(int value
)
143 case 0: return "inactive";
144 case 1: return "active";
146 default: return NULL
;
150 static const char *snapshot_string(int value
)
161 const char *enabled_string(int value
)
164 case 0: return " [disabled]";
165 case 1: return " [enabled]";
167 default: return NULL
;
172 const char *filter_string(int value
)
175 case 1: return " [with filter]";
181 const char *exclusion_string(int value
)
184 case 1: return " [has exclusions]";
189 static const char *logleveltype_string(enum lttng_loglevel_type value
)
192 case LTTNG_EVENT_LOGLEVEL_ALL
:
194 case LTTNG_EVENT_LOGLEVEL_RANGE
:
196 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
199 return " <<TYPE UNKN>>";
204 * Pretty print single event.
206 static void print_events(struct lttng_event
*event
)
208 switch (event
->type
) {
209 case LTTNG_EVENT_TRACEPOINT
:
211 if (event
->loglevel
!= -1) {
212 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
215 logleveltype_string(event
->loglevel_type
),
216 mi_lttng_loglevel_string(event
->loglevel
),
218 enabled_string(event
->enabled
),
219 exclusion_string(event
->exclusion
),
220 filter_string(event
->filter
));
222 MSG("%s%s (type: tracepoint)%s%s%s",
225 enabled_string(event
->enabled
),
226 exclusion_string(event
->exclusion
),
227 filter_string(event
->filter
));
231 case LTTNG_EVENT_FUNCTION
:
232 MSG("%s%s (type: function)%s%s", indent6
,
233 event
->name
, enabled_string(event
->enabled
),
234 filter_string(event
->filter
));
235 if (event
->attr
.probe
.addr
!= 0) {
236 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
238 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
239 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
242 case LTTNG_EVENT_PROBE
:
243 MSG("%s%s (type: probe)%s%s", indent6
,
244 event
->name
, enabled_string(event
->enabled
),
245 filter_string(event
->filter
));
246 if (event
->attr
.probe
.addr
!= 0) {
247 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
249 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
250 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
253 case LTTNG_EVENT_FUNCTION_ENTRY
:
254 MSG("%s%s (type: function)%s%s", indent6
,
255 event
->name
, enabled_string(event
->enabled
),
256 filter_string(event
->filter
));
257 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
259 case LTTNG_EVENT_SYSCALL
:
260 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
261 enabled_string(event
->enabled
),
262 filter_string(event
->filter
));
264 case LTTNG_EVENT_NOOP
:
265 MSG("%s (type: noop)%s%s", indent6
,
266 enabled_string(event
->enabled
),
267 filter_string(event
->filter
));
269 case LTTNG_EVENT_ALL
:
270 /* We should never have "all" events in list. */
276 static const char *field_type(struct lttng_event_field
*field
)
278 switch(field
->type
) {
279 case LTTNG_EVENT_FIELD_INTEGER
:
281 case LTTNG_EVENT_FIELD_ENUM
:
283 case LTTNG_EVENT_FIELD_FLOAT
:
285 case LTTNG_EVENT_FIELD_STRING
:
287 case LTTNG_EVENT_FIELD_OTHER
:
288 default: /* fall-through */
294 * Pretty print single event fields.
296 static void print_event_field(struct lttng_event_field
*field
)
298 if (!field
->field_name
[0]) {
301 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
302 field_type(field
), field
->nowrite
? " [no write]" : "");
307 * Jul and ust event listing
309 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
310 struct lttng_domain
*domain
)
314 char *cmdline
= NULL
;
315 int pid_element_open
= 0;
317 /* Open domains element */
318 ret
= mi_lttng_domains_open(writer
);
324 ret
= mi_lttng_domain(writer
, domain
, 1);
329 /* Open pids element */
330 ret
= mi_lttng_pids_open(writer
);
335 for (i
= 0; i
< count
; i
++) {
336 if (cur_pid
!= events
[i
].pid
) {
337 if (pid_element_open
) {
338 /* Close the previous events and pid element */
339 ret
= mi_lttng_close_multi_element(writer
, 2);
343 pid_element_open
= 0;
346 cur_pid
= events
[i
].pid
;
347 cmdline
= get_cmdline_by_pid(cur_pid
);
353 if (!pid_element_open
) {
354 /* Open and write a pid element */
355 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
360 /* Open events element */
361 ret
= mi_lttng_events_open(writer
);
366 pid_element_open
= 1;
372 ret
= mi_lttng_event(writer
, &events
[i
], 0);
379 ret
= mi_lttng_writer_close_element(writer
);
384 /* Close domain, domains */
385 ret
= mi_lttng_close_multi_element(writer
, 2);
393 static int list_agent_events(void)
395 int i
, size
, ret
= CMD_SUCCESS
;
396 struct lttng_domain domain
;
397 struct lttng_handle
*handle
;
398 struct lttng_event
*event_list
;
400 char *cmdline
= NULL
;
401 const char *agent_domain_str
;
403 memset(&domain
, 0, sizeof(domain
));
405 domain
.type
= LTTNG_DOMAIN_JUL
;
406 } else if (opt_log4j
) {
407 domain
.type
= LTTNG_DOMAIN_LOG4J
;
410 agent_domain_str
= get_domain_str(domain
.type
);
412 DBG("Getting %s tracing events", agent_domain_str
);
414 handle
= lttng_create_handle(NULL
, &domain
);
415 if (handle
== NULL
) {
420 size
= lttng_list_tracepoints(handle
, &event_list
);
422 ERR("Unable to list %s events: %s", agent_domain_str
,
423 lttng_strerror(size
));
430 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
437 MSG("%s events (Logger name):\n-------------------------",
444 for (i
= 0; i
< size
; i
++) {
445 if (cur_pid
!= event_list
[i
].pid
) {
446 cur_pid
= event_list
[i
].pid
;
447 cmdline
= get_cmdline_by_pid(cur_pid
);
448 if (cmdline
== NULL
) {
452 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
455 MSG("%s- %s", indent6
, event_list
[i
].name
);
464 lttng_destroy_handle(handle
);
469 * Ask session daemon for all user space tracepoints available.
471 static int list_ust_events(void)
473 int i
, size
, ret
= CMD_SUCCESS
;
474 struct lttng_domain domain
;
475 struct lttng_handle
*handle
;
476 struct lttng_event
*event_list
;
478 char *cmdline
= NULL
;
480 memset(&domain
, 0, sizeof(domain
));
482 DBG("Getting UST tracing events");
484 domain
.type
= LTTNG_DOMAIN_UST
;
486 handle
= lttng_create_handle(NULL
, &domain
);
487 if (handle
== NULL
) {
492 size
= lttng_list_tracepoints(handle
, &event_list
);
494 ERR("Unable to list UST events: %s", lttng_strerror(size
));
501 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
504 MSG("UST events:\n-------------");
510 for (i
= 0; i
< size
; i
++) {
511 if (cur_pid
!= event_list
[i
].pid
) {
512 cur_pid
= event_list
[i
].pid
;
513 cmdline
= get_cmdline_by_pid(cur_pid
);
514 if (cmdline
== NULL
) {
518 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
521 print_events(&event_list
[i
]);
530 lttng_destroy_handle(handle
);
536 * List all ust event with their fields
538 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
539 struct lttng_domain
*domain
)
543 char *cmdline
= NULL
;
544 int pid_element_open
= 0;
545 int event_element_open
= 0;
546 struct lttng_event cur_event
;
548 /* Open domains element */
549 ret
= mi_lttng_domains_open(writer
);
555 ret
= mi_lttng_domain(writer
, domain
, 1);
560 /* Open pids element */
561 ret
= mi_lttng_pids_open(writer
);
566 for (i
= 0; i
< count
; i
++) {
567 if (cur_pid
!= fields
[i
].event
.pid
) {
568 if (pid_element_open
) {
569 if (event_element_open
) {
571 /* Close the previous field element and event. */
572 ret
= mi_lttng_close_multi_element(writer
, 2);
576 event_element_open
= 0;
578 /* Close the previous events, pid element */
579 ret
= mi_lttng_close_multi_element(writer
, 2);
583 pid_element_open
= 0;
586 cur_pid
= fields
[i
].event
.pid
;
587 cmdline
= get_cmdline_by_pid(cur_pid
);
588 if (!pid_element_open
) {
589 /* Open and write a pid element */
590 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
595 /* Open events element */
596 ret
= mi_lttng_events_open(writer
);
600 pid_element_open
= 1;
603 /* Wipe current event since we are about to print a new PID. */
604 memset(&cur_event
, 0, sizeof(cur_event
));
607 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
608 if (event_element_open
) {
609 /* Close the previous fields element and the previous event */
610 ret
= mi_lttng_close_multi_element(writer
, 2);
614 event_element_open
= 0;
617 memcpy(&cur_event
, &fields
[i
].event
,
620 if (!event_element_open
) {
621 /* Open and write the event */
622 ret
= mi_lttng_event(writer
, &cur_event
, 1);
627 /* Open a fields element */
628 ret
= mi_lttng_event_fields_open(writer
);
632 event_element_open
= 1;
636 /* Print the event_field */
637 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
643 /* Close pids, domain, domains */
644 ret
= mi_lttng_close_multi_element(writer
, 3);
653 * Ask session daemon for all user space tracepoint fields available.
655 static int list_ust_event_fields(void)
657 int i
, size
, ret
= CMD_SUCCESS
;
658 struct lttng_domain domain
;
659 struct lttng_handle
*handle
;
660 struct lttng_event_field
*event_field_list
;
662 char *cmdline
= NULL
;
664 struct lttng_event cur_event
;
666 memset(&domain
, 0, sizeof(domain
));
667 memset(&cur_event
, 0, sizeof(cur_event
));
669 DBG("Getting UST tracing event fields");
671 domain
.type
= LTTNG_DOMAIN_UST
;
673 handle
= lttng_create_handle(NULL
, &domain
);
674 if (handle
== NULL
) {
679 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
681 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
688 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
695 MSG("UST events:\n-------------");
701 for (i
= 0; i
< size
; i
++) {
702 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
703 cur_pid
= event_field_list
[i
].event
.pid
;
704 cmdline
= get_cmdline_by_pid(cur_pid
);
705 if (cmdline
== NULL
) {
709 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
711 /* Wipe current event since we are about to print a new PID. */
712 memset(&cur_event
, 0, sizeof(cur_event
));
714 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
715 print_events(&event_field_list
[i
].event
);
716 memcpy(&cur_event
, &event_field_list
[i
].event
,
719 print_event_field(&event_field_list
[i
]);
726 free(event_field_list
);
728 lttng_destroy_handle(handle
);
734 * Print a list of kernel events
736 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
737 struct lttng_domain
*domain
)
741 /* Open domains element */
742 ret
= mi_lttng_domains_open(writer
);
748 ret
= mi_lttng_domain(writer
, domain
, 1);
754 ret
= mi_lttng_events_open(writer
);
759 for (i
= 0; i
< count
; i
++) {
760 ret
= mi_lttng_event(writer
, &events
[i
], 0);
766 /* close events, domain and domains */
767 ret
= mi_lttng_close_multi_element(writer
, 3);
777 * Ask for all trace events in the kernel
779 static int list_kernel_events(void)
781 int i
, size
, ret
= CMD_SUCCESS
;
782 struct lttng_domain domain
;
783 struct lttng_handle
*handle
;
784 struct lttng_event
*event_list
;
786 memset(&domain
, 0, sizeof(domain
));
788 DBG("Getting kernel tracing events");
790 domain
.type
= LTTNG_DOMAIN_KERNEL
;
792 handle
= lttng_create_handle(NULL
, &domain
);
793 if (handle
== NULL
) {
798 size
= lttng_list_tracepoints(handle
, &event_list
);
800 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
801 lttng_destroy_handle(handle
);
807 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
813 MSG("Kernel events:\n-------------");
815 for (i
= 0; i
< size
; i
++) {
816 print_events(&event_list
[i
]);
825 lttng_destroy_handle(handle
);
829 lttng_destroy_handle(handle
);
835 * Print a list of agent events
837 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
841 /* Open events element */
842 ret
= mi_lttng_events_open(writer
);
847 for (i
= 0; i
< count
; i
++) {
848 ret
= mi_lttng_event(writer
, &events
[i
], 0);
854 /* Close events element */
855 ret
= mi_lttng_writer_close_element(writer
);
862 * List agent events for a specific session using the handle.
864 * Return CMD_SUCCESS on success else a negative value.
866 static int list_session_agent_events(void)
868 int ret
= CMD_SUCCESS
, count
, i
;
869 struct lttng_event
*events
= NULL
;
871 count
= lttng_list_events(handle
, "", &events
);
874 ERR("%s", lttng_strerror(count
));
880 ret
= mi_list_session_agent_events(events
, count
);
887 MSG("Events (Logger name):\n---------------------");
889 MSG("%sNone\n", indent6
);
893 for (i
= 0; i
< count
; i
++) {
894 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
895 enabled_string(events
[i
].enabled
),
896 logleveltype_string(events
[i
].loglevel_type
),
897 mi_lttng_loglevel_string(events
[i
].loglevel
));
911 * print a list of event
913 static int mi_list_events(struct lttng_event
*events
, int count
)
917 /* Open events element */
918 ret
= mi_lttng_events_open(writer
);
923 for (i
= 0; i
< count
; i
++) {
924 ret
= mi_lttng_event(writer
, &events
[i
], 0);
930 /* Close events element */
931 ret
= mi_lttng_writer_close_element(writer
);
938 * List events of channel of session and domain.
940 static int list_events(const char *channel_name
)
942 int ret
= CMD_SUCCESS
, count
, i
;
943 struct lttng_event
*events
= NULL
;
945 count
= lttng_list_events(handle
, channel_name
, &events
);
948 ERR("%s", lttng_strerror(count
));
954 ret
= mi_list_events(events
, count
);
961 MSG("\n%sEvents:", indent4
);
963 MSG("%sNone\n", indent6
);
967 for (i
= 0; i
< count
; i
++) {
968 print_events(&events
[i
]);
980 * Pretty print channel
982 static void print_channel(struct lttng_channel
*channel
)
984 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
986 MSG("%sAttributes:", indent4
);
987 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
988 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
989 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
990 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
991 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
992 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
993 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
994 switch (channel
->attr
.output
) {
995 case LTTNG_EVENT_SPLICE
:
996 MSG("%soutput: splice()", indent6
);
998 case LTTNG_EVENT_MMAP
:
999 MSG("%soutput: mmap()", indent6
);
1006 * Print a list of channel
1009 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1010 const char *channel_name
)
1013 unsigned int chan_found
= 0;
1015 /* Open channels element */
1016 ret
= mi_lttng_channels_open(writer
);
1021 for (i
= 0; i
< count
; i
++) {
1022 if (channel_name
!= NULL
) {
1023 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1030 /* Write channel element and leave it open */
1031 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1036 /* Listing events per channel */
1037 ret
= list_events(channels
[i
].name
);
1042 /* Closing the channel element we opened earlier */
1043 ret
= mi_lttng_writer_close_element(writer
);
1053 /* Close channels element */
1054 ret
= mi_lttng_writer_close_element(writer
);
1064 * List channel(s) of session and domain.
1066 * If channel_name is NULL, all channels are listed.
1068 static int list_channels(const char *channel_name
)
1070 int count
, i
, ret
= CMD_SUCCESS
;
1071 unsigned int chan_found
= 0;
1072 struct lttng_channel
*channels
= NULL
;
1074 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1076 count
= lttng_list_channels(handle
, &channels
);
1079 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1081 /* When printing mi this is not an error
1082 * but an empty channels element */
1086 WARN("No kernel channel");
1087 goto error_channels
;
1091 /* We had a real error */
1093 ERR("%s", lttng_strerror(count
));
1094 goto error_channels
;
1101 ret
= mi_list_channels(channels
, count
, channel_name
);
1108 if (channel_name
== NULL
) {
1109 MSG("Channels:\n-------------");
1112 for (i
= 0; i
< count
; i
++) {
1113 if (channel_name
!= NULL
) {
1114 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1120 print_channel(&channels
[i
]);
1122 /* Listing events per channel */
1123 ret
= list_events(channels
[i
].name
);
1133 if (!chan_found
&& channel_name
!= NULL
) {
1135 ERR("Channel %s not found", channel_name
);
1148 * Find the session with session_name as name
1149 * and print his informations.
1151 static int mi_list_session(const char *session_name
,
1152 struct lttng_session
*sessions
, int count
)
1155 unsigned int session_found
= 0;
1157 if (session_name
== NULL
) {
1158 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1162 for (i
= 0; i
< count
; i
++) {
1163 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1164 /* We need to leave it open to append other informations
1165 * like domain, channel, events etc.*/
1167 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1175 if (!session_found
) {
1176 ERR("Session '%s' not found", session_name
);
1177 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1187 * List all availables session
1189 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1193 /* Opening sessions element */
1194 ret
= mi_lttng_sessions_open(writer
);
1199 /* Listing sessions */
1200 for (i
= 0; i
< count
; i
++) {
1201 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1207 /* Closing sessions element */
1208 ret
= mi_lttng_writer_close_element(writer
);
1218 * List available tracing session. List only basic information.
1220 * If session_name is NULL, all sessions are listed.
1222 static int list_sessions(const char *session_name
)
1224 int ret
= CMD_SUCCESS
;
1226 unsigned int session_found
= 0;
1227 struct lttng_session
*sessions
;
1229 count
= lttng_list_sessions(&sessions
);
1230 DBG("Session count %d", count
);
1233 ERR("%s", lttng_strerror(count
));
1239 if (session_name
== NULL
) {
1240 /* List all session */
1241 ret
= mi_list_sessions(sessions
, count
);
1243 /* Note : this return an open session element */
1244 ret
= mi_list_session(session_name
, sessions
, count
);
1253 MSG("Currently no available tracing session");
1257 if (session_name
== NULL
) {
1258 MSG("Available tracing sessions:");
1262 for (i
= 0; i
< count
; i
++) {
1263 if (session_name
!= NULL
) {
1264 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1266 MSG("Tracing session %s: [%s%s]", session_name
,
1267 active_string(sessions
[i
].enabled
),
1268 snapshot_string(sessions
[i
].snapshot_mode
));
1269 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1273 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1274 sessions
[i
].name
, sessions
[i
].path
,
1275 active_string(sessions
[i
].enabled
),
1276 snapshot_string(sessions
[i
].snapshot_mode
));
1277 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1278 MSG("%sLive timer interval (usec): %u\n", indent4
,
1279 sessions
[i
].live_timer_interval
);
1284 if (!session_found
&& session_name
!= NULL
) {
1285 ERR("Session '%s' not found", session_name
);
1290 if (session_name
== NULL
) {
1291 MSG("\nUse lttng list <session_name> for more details");
1304 * list available domain(s) for a session.
1306 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1309 /* Open domains element */
1310 ret
= mi_lttng_domains_open(writer
);
1315 for (i
= 0; i
< count
; i
++) {
1316 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1322 /* Closing domains element */
1323 ret
= mi_lttng_writer_close_element(writer
);
1332 * List available domain(s) for a session.
1334 static int list_domains(const char *session_name
)
1336 int i
, count
, ret
= CMD_SUCCESS
;
1337 struct lttng_domain
*domains
= NULL
;
1340 count
= lttng_list_domains(session_name
, &domains
);
1343 ERR("%s", lttng_strerror(count
));
1349 ret
= mi_list_domains(domains
, count
);
1356 MSG("Domains:\n-------------");
1362 for (i
= 0; i
< count
; i
++) {
1363 switch (domains
[i
].type
) {
1364 case LTTNG_DOMAIN_KERNEL
:
1367 case LTTNG_DOMAIN_UST
:
1368 MSG(" - UST global");
1370 case LTTNG_DOMAIN_JUL
:
1371 MSG(" - JUL (Java Util Logging)");
1373 case LTTNG_DOMAIN_LOG4J
:
1374 MSG(" - LOG4j (Logging for Java)");
1390 * The 'list <options>' first level command
1392 int cmd_list(int argc
, const char **argv
)
1394 int opt
, ret
= CMD_SUCCESS
;
1395 const char *session_name
;
1396 static poptContext pc
;
1397 struct lttng_domain domain
;
1398 struct lttng_domain
*domains
= NULL
;
1400 memset(&domain
, 0, sizeof(domain
));
1408 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1409 poptReadDefaultConfig(pc
, 0);
1411 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1419 case OPT_LIST_OPTIONS
:
1420 list_cmd_options(stdout
, long_options
);
1424 ret
= CMD_UNDEFINED
;
1431 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1437 /* Open command element */
1438 ret
= mi_lttng_writer_command_open(writer
,
1439 mi_lttng_element_command_list
);
1445 /* Open output element */
1446 ret
= mi_lttng_writer_open_element(writer
,
1447 mi_lttng_element_command_output
);
1454 /* Get session name (trailing argument) */
1455 session_name
= poptGetArg(pc
);
1456 DBG2("Session name: %s", session_name
);
1459 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1460 } else if (opt_userspace
) {
1461 DBG2("Listing userspace global domain");
1462 domain
.type
= LTTNG_DOMAIN_UST
;
1463 } else if (opt_jul
) {
1464 DBG2("Listing JUL domain");
1465 domain
.type
= LTTNG_DOMAIN_JUL
;
1466 } else if (opt_log4j
) {
1467 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1470 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
) {
1471 handle
= lttng_create_handle(session_name
, &domain
);
1472 if (handle
== NULL
) {
1478 if (session_name
== NULL
) {
1479 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
) {
1480 ret
= list_sessions(NULL
);
1486 ret
= list_kernel_events();
1491 if (opt_userspace
) {
1493 ret
= list_ust_event_fields();
1495 ret
= list_ust_events();
1501 if (opt_jul
|| opt_log4j
) {
1502 ret
= list_agent_events();
1508 /* List session attributes */
1510 /* Open element sessions
1511 * Present for xml consistency */
1512 ret
= mi_lttng_sessions_open(writer
);
1517 /* MI: the ouptut of list_sessions is an unclosed session element */
1518 ret
= list_sessions(session_name
);
1523 /* Domain listing */
1525 ret
= list_domains(session_name
);
1529 /* Channel listing */
1530 if (opt_kernel
|| opt_userspace
) {
1532 /* Add of domains and domain element for xml
1533 * consistency and validation
1535 ret
= mi_lttng_domains_open(writer
);
1540 /* Open domain and leave it open for
1541 * nested channels printing */
1542 ret
= mi_lttng_domain(writer
, &domain
, 1);
1549 ret
= list_channels(opt_channel
);
1555 /* Close domain and domain element */
1556 ret
= mi_lttng_close_multi_element(writer
, 2);
1566 /* We want all domain(s) */
1567 nb_domain
= lttng_list_domains(session_name
, &domains
);
1568 if (nb_domain
< 0) {
1570 ERR("%s", lttng_strerror(nb_domain
));
1575 ret
= mi_lttng_domains_open(writer
);
1582 for (i
= 0; i
< nb_domain
; i
++) {
1583 switch (domains
[i
].type
) {
1584 case LTTNG_DOMAIN_KERNEL
:
1585 MSG("=== Domain: Kernel ===\n");
1587 case LTTNG_DOMAIN_UST
:
1588 MSG("=== Domain: UST global ===\n");
1589 MSG("Buffer type: %s\n",
1590 domains
[i
].buf_type
==
1591 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1593 case LTTNG_DOMAIN_JUL
:
1594 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1596 case LTTNG_DOMAIN_LOG4J
:
1597 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1600 MSG("=== Domain: Unimplemented ===\n");
1605 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1612 /* Clean handle before creating a new one */
1614 lttng_destroy_handle(handle
);
1617 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1618 if (handle
== NULL
) {
1623 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1624 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
) {
1625 ret
= list_session_agent_events();
1632 ret
= list_channels(opt_channel
);
1638 /* Close domain element */
1639 ret
= mi_lttng_writer_close_element(writer
);
1648 /* Close the domains, session and sessions element */
1649 ret
= mi_lttng_close_multi_element(writer
, 3);
1660 /* Close output element */
1661 ret
= mi_lttng_writer_close_element(writer
);
1667 /* Command element close */
1668 ret
= mi_lttng_writer_command_close(writer
);
1676 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1677 /* Preserve original error code */
1678 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1683 lttng_destroy_handle(handle
);
1686 poptFreeContext(pc
);