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");
142 const char *active_string(int value
)
145 case 0: return "inactive";
146 case 1: return "active";
148 default: return NULL
;
152 static const char *snapshot_string(int value
)
163 const char *enabled_string(int value
)
166 case 0: return " [disabled]";
167 case 1: return " [enabled]";
169 default: return NULL
;
174 const char *filter_string(int value
)
177 case 1: return " [with filter]";
183 const char *exclusion_string(int value
)
186 case 1: return " [has exclusions]";
191 static const char *logleveltype_string(enum lttng_loglevel_type value
)
194 case LTTNG_EVENT_LOGLEVEL_ALL
:
196 case LTTNG_EVENT_LOGLEVEL_RANGE
:
198 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
201 return " <<TYPE UNKN>>";
206 * Pretty print single event.
208 static void print_events(struct lttng_event
*event
)
210 switch (event
->type
) {
211 case LTTNG_EVENT_TRACEPOINT
:
213 if (event
->loglevel
!= -1) {
214 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
217 logleveltype_string(event
->loglevel_type
),
218 mi_lttng_loglevel_string(event
->loglevel
),
220 enabled_string(event
->enabled
),
221 exclusion_string(event
->exclusion
),
222 filter_string(event
->filter
));
224 MSG("%s%s (type: tracepoint)%s%s%s",
227 enabled_string(event
->enabled
),
228 exclusion_string(event
->exclusion
),
229 filter_string(event
->filter
));
233 case LTTNG_EVENT_FUNCTION
:
234 MSG("%s%s (type: function)%s%s", indent6
,
235 event
->name
, enabled_string(event
->enabled
),
236 filter_string(event
->filter
));
237 if (event
->attr
.probe
.addr
!= 0) {
238 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
240 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
241 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
244 case LTTNG_EVENT_PROBE
:
245 MSG("%s%s (type: probe)%s%s", indent6
,
246 event
->name
, enabled_string(event
->enabled
),
247 filter_string(event
->filter
));
248 if (event
->attr
.probe
.addr
!= 0) {
249 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
251 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
252 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
255 case LTTNG_EVENT_FUNCTION_ENTRY
:
256 MSG("%s%s (type: function)%s%s", indent6
,
257 event
->name
, enabled_string(event
->enabled
),
258 filter_string(event
->filter
));
259 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
261 case LTTNG_EVENT_SYSCALL
:
262 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
263 enabled_string(event
->enabled
),
264 filter_string(event
->filter
));
266 case LTTNG_EVENT_NOOP
:
267 MSG("%s (type: noop)%s%s", indent6
,
268 enabled_string(event
->enabled
),
269 filter_string(event
->filter
));
271 case LTTNG_EVENT_ALL
:
272 /* We should never have "all" events in list. */
278 static const char *field_type(struct lttng_event_field
*field
)
280 switch(field
->type
) {
281 case LTTNG_EVENT_FIELD_INTEGER
:
283 case LTTNG_EVENT_FIELD_ENUM
:
285 case LTTNG_EVENT_FIELD_FLOAT
:
287 case LTTNG_EVENT_FIELD_STRING
:
289 case LTTNG_EVENT_FIELD_OTHER
:
290 default: /* fall-through */
296 * Pretty print single event fields.
298 static void print_event_field(struct lttng_event_field
*field
)
300 if (!field
->field_name
[0]) {
303 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
304 field_type(field
), field
->nowrite
? " [no write]" : "");
309 * Jul and ust event listing
311 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
312 struct lttng_domain
*domain
)
316 char *cmdline
= NULL
;
317 int pid_element_open
= 0;
319 /* Open domains element */
320 ret
= mi_lttng_domains_open(writer
);
326 ret
= mi_lttng_domain(writer
, domain
, 1);
331 /* Open pids element */
332 ret
= mi_lttng_pids_open(writer
);
337 for (i
= 0; i
< count
; i
++) {
338 if (cur_pid
!= events
[i
].pid
) {
339 if (pid_element_open
) {
340 /* Close the previous events and pid element */
341 ret
= mi_lttng_close_multi_element(writer
, 2);
345 pid_element_open
= 0;
348 cur_pid
= events
[i
].pid
;
349 cmdline
= get_cmdline_by_pid(cur_pid
);
355 if (!pid_element_open
) {
356 /* Open and write a pid element */
357 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
362 /* Open events element */
363 ret
= mi_lttng_events_open(writer
);
368 pid_element_open
= 1;
374 ret
= mi_lttng_event(writer
, &events
[i
], 0);
381 ret
= mi_lttng_writer_close_element(writer
);
386 /* Close domain, domains */
387 ret
= mi_lttng_close_multi_element(writer
, 2);
395 static int list_agent_events(void)
397 int i
, size
, ret
= CMD_SUCCESS
;
398 struct lttng_domain domain
;
399 struct lttng_handle
*handle
;
400 struct lttng_event
*event_list
;
402 char *cmdline
= NULL
;
403 const char *agent_domain_str
;
405 memset(&domain
, 0, sizeof(domain
));
407 domain
.type
= LTTNG_DOMAIN_JUL
;
408 } else if (opt_log4j
) {
409 domain
.type
= LTTNG_DOMAIN_LOG4J
;
412 agent_domain_str
= get_domain_str(domain
.type
);
414 DBG("Getting %s tracing events", agent_domain_str
);
416 handle
= lttng_create_handle(NULL
, &domain
);
417 if (handle
== NULL
) {
422 size
= lttng_list_tracepoints(handle
, &event_list
);
424 ERR("Unable to list %s events: %s", agent_domain_str
,
425 lttng_strerror(size
));
432 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
439 MSG("%s events (Logger name):\n-------------------------",
446 for (i
= 0; i
< size
; i
++) {
447 if (cur_pid
!= event_list
[i
].pid
) {
448 cur_pid
= event_list
[i
].pid
;
449 cmdline
= get_cmdline_by_pid(cur_pid
);
450 if (cmdline
== NULL
) {
454 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
457 MSG("%s- %s", indent6
, event_list
[i
].name
);
466 lttng_destroy_handle(handle
);
471 * Ask session daemon for all user space tracepoints available.
473 static int list_ust_events(void)
475 int i
, size
, ret
= CMD_SUCCESS
;
476 struct lttng_domain domain
;
477 struct lttng_handle
*handle
;
478 struct lttng_event
*event_list
;
480 char *cmdline
= NULL
;
482 memset(&domain
, 0, sizeof(domain
));
484 DBG("Getting UST tracing events");
486 domain
.type
= LTTNG_DOMAIN_UST
;
488 handle
= lttng_create_handle(NULL
, &domain
);
489 if (handle
== NULL
) {
494 size
= lttng_list_tracepoints(handle
, &event_list
);
496 ERR("Unable to list UST events: %s", lttng_strerror(size
));
503 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
506 MSG("UST events:\n-------------");
512 for (i
= 0; i
< size
; i
++) {
513 if (cur_pid
!= event_list
[i
].pid
) {
514 cur_pid
= event_list
[i
].pid
;
515 cmdline
= get_cmdline_by_pid(cur_pid
);
516 if (cmdline
== NULL
) {
520 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
523 print_events(&event_list
[i
]);
532 lttng_destroy_handle(handle
);
538 * List all ust event with their fields
540 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
541 struct lttng_domain
*domain
)
545 char *cmdline
= NULL
;
546 int pid_element_open
= 0;
547 int event_element_open
= 0;
548 struct lttng_event cur_event
;
550 /* Open domains element */
551 ret
= mi_lttng_domains_open(writer
);
557 ret
= mi_lttng_domain(writer
, domain
, 1);
562 /* Open pids element */
563 ret
= mi_lttng_pids_open(writer
);
568 for (i
= 0; i
< count
; i
++) {
569 if (cur_pid
!= fields
[i
].event
.pid
) {
570 if (pid_element_open
) {
571 if (event_element_open
) {
573 /* Close the previous field element and event. */
574 ret
= mi_lttng_close_multi_element(writer
, 2);
578 event_element_open
= 0;
580 /* Close the previous events, pid element */
581 ret
= mi_lttng_close_multi_element(writer
, 2);
585 pid_element_open
= 0;
588 cur_pid
= fields
[i
].event
.pid
;
589 cmdline
= get_cmdline_by_pid(cur_pid
);
590 if (!pid_element_open
) {
591 /* Open and write a pid element */
592 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
597 /* Open events element */
598 ret
= mi_lttng_events_open(writer
);
602 pid_element_open
= 1;
605 /* Wipe current event since we are about to print a new PID. */
606 memset(&cur_event
, 0, sizeof(cur_event
));
609 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
610 if (event_element_open
) {
611 /* Close the previous fields element and the previous event */
612 ret
= mi_lttng_close_multi_element(writer
, 2);
616 event_element_open
= 0;
619 memcpy(&cur_event
, &fields
[i
].event
,
622 if (!event_element_open
) {
623 /* Open and write the event */
624 ret
= mi_lttng_event(writer
, &cur_event
, 1);
629 /* Open a fields element */
630 ret
= mi_lttng_event_fields_open(writer
);
634 event_element_open
= 1;
638 /* Print the event_field */
639 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
645 /* Close pids, domain, domains */
646 ret
= mi_lttng_close_multi_element(writer
, 3);
655 * Ask session daemon for all user space tracepoint fields available.
657 static int list_ust_event_fields(void)
659 int i
, size
, ret
= CMD_SUCCESS
;
660 struct lttng_domain domain
;
661 struct lttng_handle
*handle
;
662 struct lttng_event_field
*event_field_list
;
664 char *cmdline
= NULL
;
666 struct lttng_event cur_event
;
668 memset(&domain
, 0, sizeof(domain
));
669 memset(&cur_event
, 0, sizeof(cur_event
));
671 DBG("Getting UST tracing event fields");
673 domain
.type
= LTTNG_DOMAIN_UST
;
675 handle
= lttng_create_handle(NULL
, &domain
);
676 if (handle
== NULL
) {
681 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
683 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
690 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
697 MSG("UST events:\n-------------");
703 for (i
= 0; i
< size
; i
++) {
704 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
705 cur_pid
= event_field_list
[i
].event
.pid
;
706 cmdline
= get_cmdline_by_pid(cur_pid
);
707 if (cmdline
== NULL
) {
711 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
713 /* Wipe current event since we are about to print a new PID. */
714 memset(&cur_event
, 0, sizeof(cur_event
));
716 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
717 print_events(&event_field_list
[i
].event
);
718 memcpy(&cur_event
, &event_field_list
[i
].event
,
721 print_event_field(&event_field_list
[i
]);
728 free(event_field_list
);
730 lttng_destroy_handle(handle
);
736 * Print a list of kernel events
738 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
739 struct lttng_domain
*domain
)
743 /* Open domains element */
744 ret
= mi_lttng_domains_open(writer
);
750 ret
= mi_lttng_domain(writer
, domain
, 1);
756 ret
= mi_lttng_events_open(writer
);
761 for (i
= 0; i
< count
; i
++) {
762 ret
= mi_lttng_event(writer
, &events
[i
], 0);
768 /* close events, domain and domains */
769 ret
= mi_lttng_close_multi_element(writer
, 3);
779 * Ask for all trace events in the kernel
781 static int list_kernel_events(void)
783 int i
, size
, ret
= CMD_SUCCESS
;
784 struct lttng_domain domain
;
785 struct lttng_handle
*handle
;
786 struct lttng_event
*event_list
;
788 memset(&domain
, 0, sizeof(domain
));
790 DBG("Getting kernel tracing events");
792 domain
.type
= LTTNG_DOMAIN_KERNEL
;
794 handle
= lttng_create_handle(NULL
, &domain
);
795 if (handle
== NULL
) {
800 size
= lttng_list_tracepoints(handle
, &event_list
);
802 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
803 lttng_destroy_handle(handle
);
809 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
815 MSG("Kernel events:\n-------------");
817 for (i
= 0; i
< size
; i
++) {
818 print_events(&event_list
[i
]);
827 lttng_destroy_handle(handle
);
831 lttng_destroy_handle(handle
);
837 * Print a list of agent events
839 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
843 /* Open events element */
844 ret
= mi_lttng_events_open(writer
);
849 for (i
= 0; i
< count
; i
++) {
850 ret
= mi_lttng_event(writer
, &events
[i
], 0);
856 /* Close events element */
857 ret
= mi_lttng_writer_close_element(writer
);
864 * List agent events for a specific session using the handle.
866 * Return CMD_SUCCESS on success else a negative value.
868 static int list_session_agent_events(void)
870 int ret
= CMD_SUCCESS
, count
, i
;
871 struct lttng_event
*events
= NULL
;
873 count
= lttng_list_events(handle
, "", &events
);
876 ERR("%s", lttng_strerror(count
));
882 ret
= mi_list_session_agent_events(events
, count
);
889 MSG("Events (Logger name):\n---------------------");
891 MSG("%sNone\n", indent6
);
895 for (i
= 0; i
< count
; i
++) {
896 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
897 enabled_string(events
[i
].enabled
),
898 logleveltype_string(events
[i
].loglevel_type
),
899 mi_lttng_loglevel_string(events
[i
].loglevel
));
913 * print a list of event
915 static int mi_list_events(struct lttng_event
*events
, int count
)
919 /* Open events element */
920 ret
= mi_lttng_events_open(writer
);
925 for (i
= 0; i
< count
; i
++) {
926 ret
= mi_lttng_event(writer
, &events
[i
], 0);
932 /* Close events element */
933 ret
= mi_lttng_writer_close_element(writer
);
940 * List events of channel of session and domain.
942 static int list_events(const char *channel_name
)
944 int ret
= CMD_SUCCESS
, count
, i
;
945 struct lttng_event
*events
= NULL
;
947 count
= lttng_list_events(handle
, channel_name
, &events
);
950 ERR("%s", lttng_strerror(count
));
956 ret
= mi_list_events(events
, count
);
963 MSG("\n%sEvents:", indent4
);
965 MSG("%sNone\n", indent6
);
969 for (i
= 0; i
< count
; i
++) {
970 print_events(&events
[i
]);
982 * Pretty print channel
984 static void print_channel(struct lttng_channel
*channel
)
986 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
988 MSG("%sAttributes:", indent4
);
989 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
990 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
991 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
992 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
993 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
994 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
995 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
996 switch (channel
->attr
.output
) {
997 case LTTNG_EVENT_SPLICE
:
998 MSG("%soutput: splice()", indent6
);
1000 case LTTNG_EVENT_MMAP
:
1001 MSG("%soutput: mmap()", indent6
);
1008 * Print a list of channel
1011 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1012 const char *channel_name
)
1015 unsigned int chan_found
= 0;
1017 /* Open channels element */
1018 ret
= mi_lttng_channels_open(writer
);
1023 for (i
= 0; i
< count
; i
++) {
1024 if (channel_name
!= NULL
) {
1025 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1032 /* Write channel element and leave it open */
1033 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1038 /* Listing events per channel */
1039 ret
= list_events(channels
[i
].name
);
1044 /* Closing the channel element we opened earlier */
1045 ret
= mi_lttng_writer_close_element(writer
);
1055 /* Close channels element */
1056 ret
= mi_lttng_writer_close_element(writer
);
1066 * List channel(s) of session and domain.
1068 * If channel_name is NULL, all channels are listed.
1070 static int list_channels(const char *channel_name
)
1072 int count
, i
, ret
= CMD_SUCCESS
;
1073 unsigned int chan_found
= 0;
1074 struct lttng_channel
*channels
= NULL
;
1076 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1078 count
= lttng_list_channels(handle
, &channels
);
1081 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1083 /* When printing mi this is not an error
1084 * but an empty channels element */
1088 WARN("No kernel channel");
1089 goto error_channels
;
1093 /* We had a real error */
1095 ERR("%s", lttng_strerror(count
));
1096 goto error_channels
;
1103 ret
= mi_list_channels(channels
, count
, channel_name
);
1110 if (channel_name
== NULL
) {
1111 MSG("Channels:\n-------------");
1114 for (i
= 0; i
< count
; i
++) {
1115 if (channel_name
!= NULL
) {
1116 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1122 print_channel(&channels
[i
]);
1124 /* Listing events per channel */
1125 ret
= list_events(channels
[i
].name
);
1135 if (!chan_found
&& channel_name
!= NULL
) {
1137 ERR("Channel %s not found", channel_name
);
1150 * Find the session with session_name as name
1151 * and print his informations.
1153 static int mi_list_session(const char *session_name
,
1154 struct lttng_session
*sessions
, int count
)
1157 unsigned int session_found
= 0;
1159 if (session_name
== NULL
) {
1160 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1164 for (i
= 0; i
< count
; i
++) {
1165 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1166 /* We need to leave it open to append other informations
1167 * like domain, channel, events etc.*/
1169 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1177 if (!session_found
) {
1178 ERR("Session '%s' not found", session_name
);
1179 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1189 * List all availables session
1191 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1195 /* Opening sessions element */
1196 ret
= mi_lttng_sessions_open(writer
);
1201 /* Listing sessions */
1202 for (i
= 0; i
< count
; i
++) {
1203 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1209 /* Closing sessions element */
1210 ret
= mi_lttng_writer_close_element(writer
);
1220 * List available tracing session. List only basic information.
1222 * If session_name is NULL, all sessions are listed.
1224 static int list_sessions(const char *session_name
)
1226 int ret
= CMD_SUCCESS
;
1228 unsigned int session_found
= 0;
1229 struct lttng_session
*sessions
;
1231 count
= lttng_list_sessions(&sessions
);
1232 DBG("Session count %d", count
);
1235 ERR("%s", lttng_strerror(count
));
1241 if (session_name
== NULL
) {
1242 /* List all session */
1243 ret
= mi_list_sessions(sessions
, count
);
1245 /* Note : this return an open session element */
1246 ret
= mi_list_session(session_name
, sessions
, count
);
1255 MSG("Currently no available tracing session");
1259 if (session_name
== NULL
) {
1260 MSG("Available tracing sessions:");
1264 for (i
= 0; i
< count
; i
++) {
1265 if (session_name
!= NULL
) {
1266 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1268 MSG("Tracing session %s: [%s%s]", session_name
,
1269 active_string(sessions
[i
].enabled
),
1270 snapshot_string(sessions
[i
].snapshot_mode
));
1271 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1275 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1276 sessions
[i
].name
, sessions
[i
].path
,
1277 active_string(sessions
[i
].enabled
),
1278 snapshot_string(sessions
[i
].snapshot_mode
));
1279 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1280 MSG("%sLive timer interval (usec): %u\n", indent4
,
1281 sessions
[i
].live_timer_interval
);
1285 if (!session_found
&& session_name
!= NULL
) {
1286 ERR("Session '%s' not found", session_name
);
1291 if (session_name
== NULL
) {
1292 MSG("\nUse lttng list <session_name> for more details");
1305 * list available domain(s) for a session.
1307 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1310 /* Open domains element */
1311 ret
= mi_lttng_domains_open(writer
);
1316 for (i
= 0; i
< count
; i
++) {
1317 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1323 /* Closing domains element */
1324 ret
= mi_lttng_writer_close_element(writer
);
1333 * List available domain(s) for a session.
1335 static int list_domains(const char *session_name
)
1337 int i
, count
, ret
= CMD_SUCCESS
;
1338 struct lttng_domain
*domains
= NULL
;
1341 count
= lttng_list_domains(session_name
, &domains
);
1344 ERR("%s", lttng_strerror(count
));
1350 ret
= mi_list_domains(domains
, count
);
1357 MSG("Domains:\n-------------");
1363 for (i
= 0; i
< count
; i
++) {
1364 switch (domains
[i
].type
) {
1365 case LTTNG_DOMAIN_KERNEL
:
1368 case LTTNG_DOMAIN_UST
:
1369 MSG(" - UST global");
1371 case LTTNG_DOMAIN_JUL
:
1372 MSG(" - JUL (Java Util Logging)");
1374 case LTTNG_DOMAIN_LOG4J
:
1375 MSG(" - LOG4j (Logging for Java)");
1391 * The 'list <options>' first level command
1393 int cmd_list(int argc
, const char **argv
)
1395 int opt
, ret
= CMD_SUCCESS
;
1396 const char *session_name
;
1397 static poptContext pc
;
1398 struct lttng_domain domain
;
1399 struct lttng_domain
*domains
= NULL
;
1401 memset(&domain
, 0, sizeof(domain
));
1409 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1410 poptReadDefaultConfig(pc
, 0);
1412 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1420 case OPT_LIST_OPTIONS
:
1421 list_cmd_options(stdout
, long_options
);
1425 ret
= CMD_UNDEFINED
;
1432 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1438 /* Open command element */
1439 ret
= mi_lttng_writer_command_open(writer
,
1440 mi_lttng_element_command_list
);
1446 /* Open output element */
1447 ret
= mi_lttng_writer_open_element(writer
,
1448 mi_lttng_element_command_output
);
1455 /* Get session name (trailing argument) */
1456 session_name
= poptGetArg(pc
);
1457 DBG2("Session name: %s", session_name
);
1460 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1461 } else if (opt_userspace
) {
1462 DBG2("Listing userspace global domain");
1463 domain
.type
= LTTNG_DOMAIN_UST
;
1464 } else if (opt_jul
) {
1465 DBG2("Listing JUL domain");
1466 domain
.type
= LTTNG_DOMAIN_JUL
;
1467 } else if (opt_log4j
) {
1468 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1471 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
) {
1472 handle
= lttng_create_handle(session_name
, &domain
);
1473 if (handle
== NULL
) {
1479 if (session_name
== NULL
) {
1480 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
) {
1481 ret
= list_sessions(NULL
);
1487 ret
= list_kernel_events();
1492 if (opt_userspace
) {
1494 ret
= list_ust_event_fields();
1496 ret
= list_ust_events();
1502 if (opt_jul
|| opt_log4j
) {
1503 ret
= list_agent_events();
1509 /* List session attributes */
1511 /* Open element sessions
1512 * Present for xml consistency */
1513 ret
= mi_lttng_sessions_open(writer
);
1518 /* MI: the ouptut of list_sessions is an unclosed session element */
1519 ret
= list_sessions(session_name
);
1524 /* Domain listing */
1526 ret
= list_domains(session_name
);
1530 /* Channel listing */
1531 if (opt_kernel
|| opt_userspace
) {
1533 /* Add of domains and domain element for xml
1534 * consistency and validation
1536 ret
= mi_lttng_domains_open(writer
);
1541 /* Open domain and leave it open for
1542 * nested channels printing */
1543 ret
= mi_lttng_domain(writer
, &domain
, 1);
1550 ret
= list_channels(opt_channel
);
1556 /* Close domain and domain element */
1557 ret
= mi_lttng_close_multi_element(writer
, 2);
1567 /* We want all domain(s) */
1568 nb_domain
= lttng_list_domains(session_name
, &domains
);
1569 if (nb_domain
< 0) {
1571 ERR("%s", lttng_strerror(nb_domain
));
1576 ret
= mi_lttng_domains_open(writer
);
1583 for (i
= 0; i
< nb_domain
; i
++) {
1584 switch (domains
[i
].type
) {
1585 case LTTNG_DOMAIN_KERNEL
:
1586 MSG("=== Domain: Kernel ===\n");
1588 case LTTNG_DOMAIN_UST
:
1589 MSG("=== Domain: UST global ===\n");
1590 MSG("Buffer type: %s\n",
1591 domains
[i
].buf_type
==
1592 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1594 case LTTNG_DOMAIN_JUL
:
1595 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1597 case LTTNG_DOMAIN_LOG4J
:
1598 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1601 MSG("=== Domain: Unimplemented ===\n");
1606 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1613 /* Clean handle before creating a new one */
1615 lttng_destroy_handle(handle
);
1618 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1619 if (handle
== NULL
) {
1624 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1625 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
) {
1626 ret
= list_session_agent_events();
1633 ret
= list_channels(opt_channel
);
1639 /* Close domain element */
1640 ret
= mi_lttng_writer_close_element(writer
);
1649 /* Close the domains, session and sessions element */
1650 ret
= mi_lttng_close_multi_element(writer
, 3);
1661 /* Close output element */
1662 ret
= mi_lttng_writer_close_element(writer
);
1668 /* Command element close */
1669 ret
= mi_lttng_writer_command_close(writer
);
1677 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1678 /* Preserve original error code */
1679 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1684 lttng_destroy_handle(handle
);
1687 poptFreeContext(pc
);