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>
27 #include <lttng/constant.h>
29 #include "../command.h"
31 static int opt_userspace
;
32 static int opt_kernel
;
35 static char *opt_channel
;
36 static int opt_domain
;
37 static int opt_fields
;
38 static int opt_syscall
;
40 /* Not implemented yet */
41 static char *opt_cmd_name
;
45 const char *indent4
= " ";
46 const char *indent6
= " ";
47 const char *indent8
= " ";
55 static struct lttng_handle
*handle
;
56 static struct mi_writer
*writer
;
58 static struct poptOption long_options
[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
61 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
62 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
63 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
65 /* Not implemented yet */
66 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
67 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
69 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
71 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
72 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
73 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
74 {"syscall", 'S', POPT_ARG_VAL
, &opt_syscall
, 1, 0, 0},
75 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
82 static void usage(FILE *ofp
)
84 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
86 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
88 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
89 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
91 fprintf(ofp
, " -h, --help Show this help\n");
92 fprintf(ofp
, " --list-options Simple listing of options\n");
93 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
94 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
95 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
96 fprintf(ofp
, " -l, --log4j Apply for Java application using LOG4J\n");
97 fprintf(ofp
, " -f, --fields List event fields.\n");
98 fprintf(ofp
, " --syscall List available system calls.\n");
100 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
103 fprintf(ofp
, "Session Options:\n");
104 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
105 fprintf(ofp
, " -d, --domain List available domain(s)\n");
110 * Get command line from /proc for a specific pid.
112 * On success, return an allocated string pointer to the proc cmdline.
113 * On error, return NULL.
115 static char *get_cmdline_by_pid(pid_t pid
)
119 char *cmdline
= NULL
;
120 /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */
121 char path
[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR
) - 1];
123 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
124 fp
= fopen(path
, "r");
129 /* Caller must free() *cmdline */
130 cmdline
= zmalloc(PATH_MAX
);
132 perror("malloc cmdline");
135 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
137 perror("fread proc list");
148 const char *active_string(int value
)
151 case 0: return "inactive";
152 case 1: return "active";
154 default: return NULL
;
158 static const char *snapshot_string(int value
)
169 const char *enabled_string(int value
)
172 case 0: return " [disabled]";
173 case 1: return " [enabled]";
175 default: return NULL
;
180 const char *filter_string(int value
)
183 case 1: return " [with filter]";
189 const char *exclusion_string(int value
)
192 case 1: return " [has exclusions]";
197 static const char *logleveltype_string(enum lttng_loglevel_type value
)
200 case LTTNG_EVENT_LOGLEVEL_ALL
:
202 case LTTNG_EVENT_LOGLEVEL_RANGE
:
204 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
207 return " <<TYPE UNKN>>";
211 static const char *bitness_event(enum lttng_event_flag flags
)
213 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_32
) {
214 if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
215 return " [32/64-bit]";
219 } else if (flags
& LTTNG_EVENT_FLAG_SYSCALL_64
) {
227 * Pretty print single event.
229 static void print_events(struct lttng_event
*event
)
231 switch (event
->type
) {
232 case LTTNG_EVENT_TRACEPOINT
:
234 if (event
->loglevel
!= -1) {
235 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
238 logleveltype_string(event
->loglevel_type
),
239 mi_lttng_loglevel_string(event
->loglevel
, handle
->domain
.type
),
241 enabled_string(event
->enabled
),
242 exclusion_string(event
->exclusion
),
243 filter_string(event
->filter
));
245 MSG("%s%s (type: tracepoint)%s%s%s",
248 enabled_string(event
->enabled
),
249 exclusion_string(event
->exclusion
),
250 filter_string(event
->filter
));
254 case LTTNG_EVENT_FUNCTION
:
255 MSG("%s%s (type: function)%s%s", indent6
,
256 event
->name
, enabled_string(event
->enabled
),
257 filter_string(event
->filter
));
258 if (event
->attr
.probe
.addr
!= 0) {
259 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
261 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
262 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
265 case LTTNG_EVENT_PROBE
:
266 MSG("%s%s (type: probe)%s%s", indent6
,
267 event
->name
, enabled_string(event
->enabled
),
268 filter_string(event
->filter
));
269 if (event
->attr
.probe
.addr
!= 0) {
270 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
272 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
273 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
276 case LTTNG_EVENT_FUNCTION_ENTRY
:
277 MSG("%s%s (type: function)%s%s", indent6
,
278 event
->name
, enabled_string(event
->enabled
),
279 filter_string(event
->filter
));
280 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
282 case LTTNG_EVENT_SYSCALL
:
283 MSG("%s%s%s%s%s", indent6
, event
->name
,
284 (opt_syscall
? "" : " (type:syscall)"),
285 enabled_string(event
->enabled
),
286 bitness_event(event
->flags
));
288 case LTTNG_EVENT_NOOP
:
289 MSG("%s (type: noop)%s%s", indent6
,
290 enabled_string(event
->enabled
),
291 filter_string(event
->filter
));
293 case LTTNG_EVENT_ALL
:
294 /* We should never have "all" events in list. */
300 static const char *field_type(struct lttng_event_field
*field
)
302 switch(field
->type
) {
303 case LTTNG_EVENT_FIELD_INTEGER
:
305 case LTTNG_EVENT_FIELD_ENUM
:
307 case LTTNG_EVENT_FIELD_FLOAT
:
309 case LTTNG_EVENT_FIELD_STRING
:
311 case LTTNG_EVENT_FIELD_OTHER
:
312 default: /* fall-through */
318 * Pretty print single event fields.
320 static void print_event_field(struct lttng_event_field
*field
)
322 if (!field
->field_name
[0]) {
325 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
326 field_type(field
), field
->nowrite
? " [no write]" : "");
331 * Jul and ust event listing
333 static int mi_list_agent_ust_events(struct lttng_event
*events
, int count
,
334 struct lttng_domain
*domain
)
338 char *cmdline
= NULL
;
339 int pid_element_open
= 0;
341 /* Open domains element */
342 ret
= mi_lttng_domains_open(writer
);
348 ret
= mi_lttng_domain(writer
, domain
, 1);
353 /* Open pids element */
354 ret
= mi_lttng_pids_open(writer
);
359 for (i
= 0; i
< count
; i
++) {
360 if (cur_pid
!= events
[i
].pid
) {
361 if (pid_element_open
) {
362 /* Close the previous events and pid element */
363 ret
= mi_lttng_close_multi_element(writer
, 2);
367 pid_element_open
= 0;
370 cur_pid
= events
[i
].pid
;
371 cmdline
= get_cmdline_by_pid(cur_pid
);
377 if (!pid_element_open
) {
378 /* Open and write a pid element */
379 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
384 /* Open events element */
385 ret
= mi_lttng_events_open(writer
);
390 pid_element_open
= 1;
396 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
403 ret
= mi_lttng_writer_close_element(writer
);
408 /* Close domain, domains */
409 ret
= mi_lttng_close_multi_element(writer
, 2);
417 static int list_agent_events(void)
419 int i
, size
, ret
= CMD_SUCCESS
;
420 struct lttng_domain domain
;
421 struct lttng_handle
*handle
;
422 struct lttng_event
*event_list
;
424 char *cmdline
= NULL
;
425 const char *agent_domain_str
;
427 memset(&domain
, 0, sizeof(domain
));
429 domain
.type
= LTTNG_DOMAIN_JUL
;
430 } else if (opt_log4j
) {
431 domain
.type
= LTTNG_DOMAIN_LOG4J
;
434 agent_domain_str
= get_domain_str(domain
.type
);
436 DBG("Getting %s tracing events", agent_domain_str
);
438 handle
= lttng_create_handle(NULL
, &domain
);
439 if (handle
== NULL
) {
444 size
= lttng_list_tracepoints(handle
, &event_list
);
446 ERR("Unable to list %s events: %s", agent_domain_str
,
447 lttng_strerror(size
));
454 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
461 MSG("%s events (Logger name):\n-------------------------",
468 for (i
= 0; i
< size
; i
++) {
469 if (cur_pid
!= event_list
[i
].pid
) {
470 cur_pid
= event_list
[i
].pid
;
471 cmdline
= get_cmdline_by_pid(cur_pid
);
472 if (cmdline
== NULL
) {
476 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
479 MSG("%s- %s", indent6
, event_list
[i
].name
);
488 lttng_destroy_handle(handle
);
493 * Ask session daemon for all user space tracepoints available.
495 static int list_ust_events(void)
497 int i
, size
, ret
= CMD_SUCCESS
;
498 struct lttng_domain domain
;
499 struct lttng_handle
*handle
;
500 struct lttng_event
*event_list
;
502 char *cmdline
= NULL
;
504 memset(&domain
, 0, sizeof(domain
));
506 DBG("Getting UST tracing events");
508 domain
.type
= LTTNG_DOMAIN_UST
;
510 handle
= lttng_create_handle(NULL
, &domain
);
511 if (handle
== NULL
) {
516 size
= lttng_list_tracepoints(handle
, &event_list
);
518 ERR("Unable to list UST events: %s", lttng_strerror(size
));
525 ret
= mi_list_agent_ust_events(event_list
, size
, &domain
);
528 MSG("UST events:\n-------------");
534 for (i
= 0; i
< size
; i
++) {
535 if (cur_pid
!= event_list
[i
].pid
) {
536 cur_pid
= event_list
[i
].pid
;
537 cmdline
= get_cmdline_by_pid(cur_pid
);
538 if (cmdline
== NULL
) {
542 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
545 print_events(&event_list
[i
]);
554 lttng_destroy_handle(handle
);
560 * List all ust event with their fields
562 static int mi_list_ust_event_fields(struct lttng_event_field
*fields
, int count
,
563 struct lttng_domain
*domain
)
567 char *cmdline
= NULL
;
568 int pid_element_open
= 0;
569 int event_element_open
= 0;
570 struct lttng_event cur_event
;
572 /* Open domains element */
573 ret
= mi_lttng_domains_open(writer
);
579 ret
= mi_lttng_domain(writer
, domain
, 1);
584 /* Open pids element */
585 ret
= mi_lttng_pids_open(writer
);
590 for (i
= 0; i
< count
; i
++) {
591 if (cur_pid
!= fields
[i
].event
.pid
) {
592 if (pid_element_open
) {
593 if (event_element_open
) {
595 /* Close the previous field element and event. */
596 ret
= mi_lttng_close_multi_element(writer
, 2);
600 event_element_open
= 0;
602 /* Close the previous events, pid element */
603 ret
= mi_lttng_close_multi_element(writer
, 2);
607 pid_element_open
= 0;
610 cur_pid
= fields
[i
].event
.pid
;
611 cmdline
= get_cmdline_by_pid(cur_pid
);
612 if (!pid_element_open
) {
613 /* Open and write a pid element */
614 ret
= mi_lttng_pid(writer
, cur_pid
, cmdline
, 1);
619 /* Open events element */
620 ret
= mi_lttng_events_open(writer
);
624 pid_element_open
= 1;
627 /* Wipe current event since we are about to print a new PID. */
628 memset(&cur_event
, 0, sizeof(cur_event
));
631 if (strcmp(cur_event
.name
, fields
[i
].event
.name
) != 0) {
632 if (event_element_open
) {
633 /* Close the previous fields element and the previous event */
634 ret
= mi_lttng_close_multi_element(writer
, 2);
638 event_element_open
= 0;
641 memcpy(&cur_event
, &fields
[i
].event
,
644 if (!event_element_open
) {
645 /* Open and write the event */
646 ret
= mi_lttng_event(writer
, &cur_event
, 1,
647 handle
->domain
.type
);
652 /* Open a fields element */
653 ret
= mi_lttng_event_fields_open(writer
);
657 event_element_open
= 1;
661 /* Print the event_field */
662 ret
= mi_lttng_event_field(writer
, &fields
[i
]);
668 /* Close pids, domain, domains */
669 ret
= mi_lttng_close_multi_element(writer
, 3);
678 * Ask session daemon for all user space tracepoint fields available.
680 static int list_ust_event_fields(void)
682 int i
, size
, ret
= CMD_SUCCESS
;
683 struct lttng_domain domain
;
684 struct lttng_handle
*handle
;
685 struct lttng_event_field
*event_field_list
;
687 char *cmdline
= NULL
;
689 struct lttng_event cur_event
;
691 memset(&domain
, 0, sizeof(domain
));
692 memset(&cur_event
, 0, sizeof(cur_event
));
694 DBG("Getting UST tracing event fields");
696 domain
.type
= LTTNG_DOMAIN_UST
;
698 handle
= lttng_create_handle(NULL
, &domain
);
699 if (handle
== NULL
) {
704 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
706 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
713 ret
= mi_list_ust_event_fields(event_field_list
, size
, &domain
);
720 MSG("UST events:\n-------------");
726 for (i
= 0; i
< size
; i
++) {
727 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
728 cur_pid
= event_field_list
[i
].event
.pid
;
729 cmdline
= get_cmdline_by_pid(cur_pid
);
730 if (cmdline
== NULL
) {
734 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
736 /* Wipe current event since we are about to print a new PID. */
737 memset(&cur_event
, 0, sizeof(cur_event
));
739 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
740 print_events(&event_field_list
[i
].event
);
741 memcpy(&cur_event
, &event_field_list
[i
].event
,
744 print_event_field(&event_field_list
[i
]);
751 free(event_field_list
);
753 lttng_destroy_handle(handle
);
759 * Print a list of kernel events
761 static int mi_list_kernel_events(struct lttng_event
*events
, int count
,
762 struct lttng_domain
*domain
)
766 /* Open domains element */
767 ret
= mi_lttng_domains_open(writer
);
773 ret
= mi_lttng_domain(writer
, domain
, 1);
779 ret
= mi_lttng_events_open(writer
);
784 for (i
= 0; i
< count
; i
++) {
785 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
791 /* close events, domain and domains */
792 ret
= mi_lttng_close_multi_element(writer
, 3);
802 * Ask for all trace events in the kernel
804 static int list_kernel_events(void)
806 int i
, size
, ret
= CMD_SUCCESS
;
807 struct lttng_domain domain
;
808 struct lttng_handle
*handle
;
809 struct lttng_event
*event_list
;
811 memset(&domain
, 0, sizeof(domain
));
813 DBG("Getting kernel tracing events");
815 domain
.type
= LTTNG_DOMAIN_KERNEL
;
817 handle
= lttng_create_handle(NULL
, &domain
);
818 if (handle
== NULL
) {
823 size
= lttng_list_tracepoints(handle
, &event_list
);
825 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
826 lttng_destroy_handle(handle
);
832 ret
= mi_list_kernel_events(event_list
, size
, &domain
);
838 MSG("Kernel events:\n-------------");
840 for (i
= 0; i
< size
; i
++) {
841 print_events(&event_list
[i
]);
850 lttng_destroy_handle(handle
);
854 lttng_destroy_handle(handle
);
860 * Print a list of system calls.
862 static int mi_list_syscalls(struct lttng_event
*events
, int count
)
867 ret
= mi_lttng_events_open(writer
);
872 for (i
= 0; i
< count
; i
++) {
873 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
880 ret
= mi_lttng_writer_close_element(writer
);
890 * Ask for kernel system calls.
892 static int list_syscalls(void)
894 int i
, size
, ret
= CMD_SUCCESS
;
895 struct lttng_event
*event_list
;
897 DBG("Getting kernel system call events");
899 size
= lttng_list_syscalls(&event_list
);
901 ERR("Unable to list system calls: %s", lttng_strerror(size
));
908 ret
= mi_list_syscalls(event_list
, size
);
914 MSG("System calls:\n-------------");
916 for (i
= 0; i
< size
; i
++) {
917 print_events(&event_list
[i
]);
933 * Print a list of agent events
935 static int mi_list_session_agent_events(struct lttng_event
*events
, int count
)
939 /* Open events element */
940 ret
= mi_lttng_events_open(writer
);
945 for (i
= 0; i
< count
; i
++) {
946 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
952 /* Close events element */
953 ret
= mi_lttng_writer_close_element(writer
);
960 * List agent events for a specific session using the handle.
962 * Return CMD_SUCCESS on success else a negative value.
964 static int list_session_agent_events(void)
966 int ret
= CMD_SUCCESS
, count
, i
;
967 struct lttng_event
*events
= NULL
;
969 count
= lttng_list_events(handle
, "", &events
);
972 ERR("%s", lttng_strerror(count
));
978 ret
= mi_list_session_agent_events(events
, count
);
985 MSG("Events (Logger name):\n---------------------");
987 MSG("%sNone\n", indent6
);
991 for (i
= 0; i
< count
; i
++) {
992 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
993 enabled_string(events
[i
].enabled
),
994 logleveltype_string(events
[i
].loglevel_type
),
995 mi_lttng_loglevel_string(events
[i
].loglevel
,
996 handle
->domain
.type
));
1010 * print a list of event
1012 static int mi_list_events(struct lttng_event
*events
, int count
)
1016 /* Open events element */
1017 ret
= mi_lttng_events_open(writer
);
1022 for (i
= 0; i
< count
; i
++) {
1023 ret
= mi_lttng_event(writer
, &events
[i
], 0, handle
->domain
.type
);
1029 /* Close events element */
1030 ret
= mi_lttng_writer_close_element(writer
);
1037 * List events of channel of session and domain.
1039 static int list_events(const char *channel_name
)
1041 int ret
= CMD_SUCCESS
, count
, i
;
1042 struct lttng_event
*events
= NULL
;
1044 count
= lttng_list_events(handle
, channel_name
, &events
);
1047 ERR("%s", lttng_strerror(count
));
1053 ret
= mi_list_events(events
, count
);
1060 MSG("\n%sEvents:", indent4
);
1062 MSG("%sNone\n", indent6
);
1066 for (i
= 0; i
< count
; i
++) {
1067 print_events(&events
[i
]);
1079 * Pretty print channel
1081 static void print_channel(struct lttng_channel
*channel
)
1083 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
1085 MSG("%sAttributes:", indent4
);
1086 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
1087 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
1088 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
1089 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
1090 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
1091 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
1092 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
1093 switch (channel
->attr
.output
) {
1094 case LTTNG_EVENT_SPLICE
:
1095 MSG("%soutput: splice()", indent6
);
1097 case LTTNG_EVENT_MMAP
:
1098 MSG("%soutput: mmap()", indent6
);
1105 * Print a list of channel
1108 static int mi_list_channels(struct lttng_channel
*channels
, int count
,
1109 const char *channel_name
)
1112 unsigned int chan_found
= 0;
1114 /* Open channels element */
1115 ret
= mi_lttng_channels_open(writer
);
1120 for (i
= 0; i
< count
; i
++) {
1121 if (channel_name
!= NULL
) {
1122 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1129 /* Write channel element and leave it open */
1130 ret
= mi_lttng_channel(writer
, &channels
[i
], 1);
1135 /* Listing events per channel */
1136 ret
= list_events(channels
[i
].name
);
1141 /* Closing the channel element we opened earlier */
1142 ret
= mi_lttng_writer_close_element(writer
);
1152 /* Close channels element */
1153 ret
= mi_lttng_writer_close_element(writer
);
1163 * List channel(s) of session and domain.
1165 * If channel_name is NULL, all channels are listed.
1167 static int list_channels(const char *channel_name
)
1169 int count
, i
, ret
= CMD_SUCCESS
;
1170 unsigned int chan_found
= 0;
1171 struct lttng_channel
*channels
= NULL
;
1173 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
1175 count
= lttng_list_channels(handle
, &channels
);
1178 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
1180 /* When printing mi this is not an error
1181 * but an empty channels element */
1185 WARN("No kernel channel");
1186 goto error_channels
;
1190 /* We had a real error */
1192 ERR("%s", lttng_strerror(count
));
1193 goto error_channels
;
1200 ret
= mi_list_channels(channels
, count
, channel_name
);
1208 MSG("Channels:\n-------------");
1211 for (i
= 0; i
< count
; i
++) {
1212 if (channel_name
!= NULL
) {
1213 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
1219 print_channel(&channels
[i
]);
1221 /* Listing events per channel */
1222 ret
= list_events(channels
[i
].name
);
1232 if (!chan_found
&& channel_name
!= NULL
) {
1234 ERR("Channel %s not found", channel_name
);
1247 * Find the session with session_name as name
1248 * and print his informations.
1250 static int mi_list_session(const char *session_name
,
1251 struct lttng_session
*sessions
, int count
)
1254 unsigned int session_found
= 0;
1256 if (session_name
== NULL
) {
1257 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1261 for (i
= 0; i
< count
; i
++) {
1262 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1263 /* We need to leave it open to append other informations
1264 * like domain, channel, events etc.*/
1266 ret
= mi_lttng_session(writer
, &sessions
[i
], 1);
1274 if (!session_found
) {
1275 ERR("Session '%s' not found", session_name
);
1276 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
1286 * List all availables session
1288 static int mi_list_sessions(struct lttng_session
*sessions
, int count
)
1292 /* Opening sessions element */
1293 ret
= mi_lttng_sessions_open(writer
);
1298 /* Listing sessions */
1299 for (i
= 0; i
< count
; i
++) {
1300 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
1306 /* Closing sessions element */
1307 ret
= mi_lttng_writer_close_element(writer
);
1317 * List available tracing session. List only basic information.
1319 * If session_name is NULL, all sessions are listed.
1321 static int list_sessions(const char *session_name
)
1323 int ret
= CMD_SUCCESS
;
1325 unsigned int session_found
= 0;
1326 struct lttng_session
*sessions
;
1328 count
= lttng_list_sessions(&sessions
);
1329 DBG("Session count %d", count
);
1332 ERR("%s", lttng_strerror(count
));
1338 if (session_name
== NULL
) {
1339 /* List all session */
1340 ret
= mi_list_sessions(sessions
, count
);
1342 /* Note : this return an open session element */
1343 ret
= mi_list_session(session_name
, sessions
, count
);
1352 MSG("Currently no available tracing session");
1356 if (session_name
== NULL
) {
1357 MSG("Available tracing sessions:");
1361 for (i
= 0; i
< count
; i
++) {
1362 if (session_name
!= NULL
) {
1363 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
1365 MSG("Tracing session %s: [%s%s]", session_name
,
1366 active_string(sessions
[i
].enabled
),
1367 snapshot_string(sessions
[i
].snapshot_mode
));
1368 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
1372 MSG(" %d) %s (%s) [%s%s]", i
+ 1,
1373 sessions
[i
].name
, sessions
[i
].path
,
1374 active_string(sessions
[i
].enabled
),
1375 snapshot_string(sessions
[i
].snapshot_mode
));
1376 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
1377 MSG("%sLive timer interval (usec): %u\n", indent4
,
1378 sessions
[i
].live_timer_interval
);
1382 if (!session_found
&& session_name
!= NULL
) {
1383 ERR("Session '%s' not found", session_name
);
1388 if (session_name
== NULL
) {
1389 MSG("\nUse lttng list <session_name> for more details");
1402 * list available domain(s) for a session.
1404 static int mi_list_domains(struct lttng_domain
*domains
, int count
)
1407 /* Open domains element */
1408 ret
= mi_lttng_domains_open(writer
);
1413 for (i
= 0; i
< count
; i
++) {
1414 ret
= mi_lttng_domain(writer
, &domains
[i
] , 0);
1420 /* Closing domains element */
1421 ret
= mi_lttng_writer_close_element(writer
);
1430 * List available domain(s) for a session.
1432 static int list_domains(const char *session_name
)
1434 int i
, count
, ret
= CMD_SUCCESS
;
1435 struct lttng_domain
*domains
= NULL
;
1438 count
= lttng_list_domains(session_name
, &domains
);
1441 ERR("%s", lttng_strerror(count
));
1447 ret
= mi_list_domains(domains
, count
);
1454 MSG("Domains:\n-------------");
1460 for (i
= 0; i
< count
; i
++) {
1461 switch (domains
[i
].type
) {
1462 case LTTNG_DOMAIN_KERNEL
:
1465 case LTTNG_DOMAIN_UST
:
1466 MSG(" - UST global");
1468 case LTTNG_DOMAIN_JUL
:
1469 MSG(" - JUL (Java Util Logging)");
1471 case LTTNG_DOMAIN_LOG4J
:
1472 MSG(" - LOG4j (Logging for Java)");
1488 * The 'list <options>' first level command
1490 int cmd_list(int argc
, const char **argv
)
1492 int opt
, ret
= CMD_SUCCESS
;
1493 const char *session_name
;
1494 static poptContext pc
;
1495 struct lttng_domain domain
;
1496 struct lttng_domain
*domains
= NULL
;
1498 memset(&domain
, 0, sizeof(domain
));
1506 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1507 poptReadDefaultConfig(pc
, 0);
1509 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1517 case OPT_LIST_OPTIONS
:
1518 list_cmd_options(stdout
, long_options
);
1522 ret
= CMD_UNDEFINED
;
1529 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1535 /* Open command element */
1536 ret
= mi_lttng_writer_command_open(writer
,
1537 mi_lttng_element_command_list
);
1543 /* Open output element */
1544 ret
= mi_lttng_writer_open_element(writer
,
1545 mi_lttng_element_command_output
);
1552 /* Get session name (trailing argument) */
1553 session_name
= poptGetArg(pc
);
1554 DBG2("Session name: %s", session_name
);
1557 domain
.type
= LTTNG_DOMAIN_KERNEL
;
1558 } else if (opt_userspace
) {
1559 DBG2("Listing userspace global domain");
1560 domain
.type
= LTTNG_DOMAIN_UST
;
1561 } else if (opt_jul
) {
1562 DBG2("Listing JUL domain");
1563 domain
.type
= LTTNG_DOMAIN_JUL
;
1564 } else if (opt_log4j
) {
1565 domain
.type
= LTTNG_DOMAIN_LOG4J
;
1568 if (!opt_kernel
&& opt_syscall
) {
1569 WARN("--syscall will only work with the Kernel domain (-k)");
1574 if (opt_kernel
|| opt_userspace
|| opt_jul
|| opt_log4j
) {
1575 handle
= lttng_create_handle(session_name
, &domain
);
1576 if (handle
== NULL
) {
1582 if (session_name
== NULL
) {
1583 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
&& !opt_log4j
) {
1584 ret
= list_sessions(NULL
);
1591 ret
= list_syscalls();
1596 ret
= list_kernel_events();
1602 if (opt_userspace
) {
1604 ret
= list_ust_event_fields();
1606 ret
= list_ust_events();
1612 if (opt_jul
|| opt_log4j
) {
1613 ret
= list_agent_events();
1619 /* List session attributes */
1621 /* Open element sessions
1622 * Present for xml consistency */
1623 ret
= mi_lttng_sessions_open(writer
);
1628 /* MI: the ouptut of list_sessions is an unclosed session element */
1629 ret
= list_sessions(session_name
);
1634 /* Domain listing */
1636 ret
= list_domains(session_name
);
1640 /* Channel listing */
1641 if (opt_kernel
|| opt_userspace
) {
1643 /* Add of domains and domain element for xml
1644 * consistency and validation
1646 ret
= mi_lttng_domains_open(writer
);
1651 /* Open domain and leave it open for
1652 * nested channels printing */
1653 ret
= mi_lttng_domain(writer
, &domain
, 1);
1660 ret
= list_channels(opt_channel
);
1666 /* Close domain and domain element */
1667 ret
= mi_lttng_close_multi_element(writer
, 2);
1677 /* We want all domain(s) */
1678 nb_domain
= lttng_list_domains(session_name
, &domains
);
1679 if (nb_domain
< 0) {
1681 ERR("%s", lttng_strerror(nb_domain
));
1686 ret
= mi_lttng_domains_open(writer
);
1693 for (i
= 0; i
< nb_domain
; i
++) {
1694 switch (domains
[i
].type
) {
1695 case LTTNG_DOMAIN_KERNEL
:
1696 MSG("=== Domain: Kernel ===\n");
1698 case LTTNG_DOMAIN_UST
:
1699 MSG("=== Domain: UST global ===\n");
1700 MSG("Buffer type: %s\n",
1701 domains
[i
].buf_type
==
1702 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1704 case LTTNG_DOMAIN_JUL
:
1705 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1707 case LTTNG_DOMAIN_LOG4J
:
1708 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1711 MSG("=== Domain: Unimplemented ===\n");
1716 ret
= mi_lttng_domain(writer
, &domains
[i
], 1);
1723 /* Clean handle before creating a new one */
1725 lttng_destroy_handle(handle
);
1728 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1729 if (handle
== NULL
) {
1734 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
||
1735 domains
[i
].type
== LTTNG_DOMAIN_LOG4J
) {
1736 ret
= list_session_agent_events();
1743 ret
= list_channels(opt_channel
);
1749 /* Close domain element */
1750 ret
= mi_lttng_writer_close_element(writer
);
1759 /* Close the domains, session and sessions element */
1760 ret
= mi_lttng_close_multi_element(writer
, 3);
1771 /* Close output element */
1772 ret
= mi_lttng_writer_close_element(writer
);
1778 /* Command element close */
1779 ret
= mi_lttng_writer_command_close(writer
);
1787 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1788 /* Preserve original error code */
1789 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1794 lttng_destroy_handle(handle
);
1797 poptFreeContext(pc
);