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 "../command.h"
28 static int opt_userspace
;
29 static int opt_kernel
;
31 static char *opt_channel
;
32 static int opt_domain
;
33 static int opt_fields
;
35 /* Not implemented yet */
36 static char *opt_cmd_name
;
40 const char *indent4
= " ";
41 const char *indent6
= " ";
42 const char *indent8
= " ";
50 static struct lttng_handle
*handle
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
56 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
58 /* Not implemented yet */
59 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
60 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
62 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
64 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
65 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
66 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 1, 0, 0},
67 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
74 static void usage(FILE *ofp
)
76 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
78 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
80 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
81 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
83 fprintf(ofp
, " -h, --help Show this help\n");
84 fprintf(ofp
, " --list-options Simple listing of options\n");
85 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
86 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
87 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
88 fprintf(ofp
, " -f, --fields List event fields.\n");
90 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
93 fprintf(ofp
, "Session Options:\n");
94 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
95 fprintf(ofp
, " -d, --domain List available domain(s)\n");
100 * Get command line from /proc for a specific pid.
102 * On success, return an allocated string pointer to the proc cmdline.
103 * On error, return NULL.
105 static char *get_cmdline_by_pid(pid_t pid
)
109 char *cmdline
= NULL
;
110 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
112 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
113 fp
= fopen(path
, "r");
118 /* Caller must free() *cmdline */
119 cmdline
= malloc(PATH_MAX
);
120 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
122 perror("fread proc list");
131 const char *active_string(int value
)
134 case 0: return "inactive";
135 case 1: return "active";
137 default: return NULL
;
141 static const char *snapshot_string(int value
)
152 const char *enabled_string(int value
)
155 case 0: return " [disabled]";
156 case 1: return " [enabled]";
158 default: return NULL
;
163 const char *filter_string(int value
)
166 case 1: return " [with filter]";
171 static const char *loglevel_string(int value
)
176 case LTTNG_LOGLEVEL_EMERG
:
177 return "TRACE_EMERG";
178 case LTTNG_LOGLEVEL_ALERT
:
179 return "TRACE_ALERT";
180 case LTTNG_LOGLEVEL_CRIT
:
182 case LTTNG_LOGLEVEL_ERR
:
184 case LTTNG_LOGLEVEL_WARNING
:
185 return "TRACE_WARNING";
186 case LTTNG_LOGLEVEL_NOTICE
:
187 return "TRACE_NOTICE";
188 case LTTNG_LOGLEVEL_INFO
:
190 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
191 return "TRACE_DEBUG_SYSTEM";
192 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
193 return "TRACE_DEBUG_PROGRAM";
194 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
195 return "TRACE_DEBUG_PROCESS";
196 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
197 return "TRACE_DEBUG_MODULE";
198 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
199 return "TRACE_DEBUG_UNIT";
200 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
201 return "TRACE_DEBUG_FUNCTION";
202 case LTTNG_LOGLEVEL_DEBUG_LINE
:
203 return "TRACE_DEBUG_LINE";
204 case LTTNG_LOGLEVEL_DEBUG
:
205 return "TRACE_DEBUG";
207 return "<<UNKNOWN>>";
212 * Pretty print single event.
214 static void print_events(struct lttng_event
*event
)
216 switch (event
->type
) {
217 case LTTNG_EVENT_TRACEPOINT
:
219 if (event
->loglevel
!= -1) {
220 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
223 loglevel_string(event
->loglevel
),
225 enabled_string(event
->enabled
),
226 filter_string(event
->filter
));
228 MSG("%s%s (type: tracepoint)%s%s",
231 enabled_string(event
->enabled
),
232 filter_string(event
->filter
));
236 case LTTNG_EVENT_FUNCTION
:
237 MSG("%s%s (type: function)%s%s", indent6
,
238 event
->name
, enabled_string(event
->enabled
),
239 filter_string(event
->filter
));
240 if (event
->attr
.probe
.addr
!= 0) {
241 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
243 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
244 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
247 case LTTNG_EVENT_PROBE
:
248 MSG("%s%s (type: probe)%s%s", indent6
,
249 event
->name
, enabled_string(event
->enabled
),
250 filter_string(event
->filter
));
251 if (event
->attr
.probe
.addr
!= 0) {
252 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
254 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
255 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
258 case LTTNG_EVENT_FUNCTION_ENTRY
:
259 MSG("%s%s (type: function)%s%s", indent6
,
260 event
->name
, enabled_string(event
->enabled
),
261 filter_string(event
->filter
));
262 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
264 case LTTNG_EVENT_SYSCALL
:
265 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
266 enabled_string(event
->enabled
),
267 filter_string(event
->filter
));
269 case LTTNG_EVENT_NOOP
:
270 MSG("%s (type: noop)%s%s", indent6
,
271 enabled_string(event
->enabled
),
272 filter_string(event
->filter
));
274 case LTTNG_EVENT_ALL
:
275 /* We should never have "all" events in list. */
281 static const char *field_type(struct lttng_event_field
*field
)
283 switch(field
->type
) {
284 case LTTNG_EVENT_FIELD_INTEGER
:
286 case LTTNG_EVENT_FIELD_ENUM
:
288 case LTTNG_EVENT_FIELD_FLOAT
:
290 case LTTNG_EVENT_FIELD_STRING
:
292 case LTTNG_EVENT_FIELD_OTHER
:
293 default: /* fall-through */
299 * Pretty print single event fields.
301 static void print_event_field(struct lttng_event_field
*field
)
303 if (!field
->field_name
[0]) {
306 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
307 field_type(field
), field
->nowrite
? " [no write]" : "");
310 static int list_jul_events(void)
313 struct lttng_domain domain
;
314 struct lttng_handle
*handle
;
315 struct lttng_event
*event_list
;
317 char *cmdline
= NULL
;
319 DBG("Getting JUL tracing events");
321 memset(&domain
, 0, sizeof(domain
));
322 domain
.type
= LTTNG_DOMAIN_JUL
;
324 handle
= lttng_create_handle(NULL
, &domain
);
325 if (handle
== NULL
) {
329 size
= lttng_list_tracepoints(handle
, &event_list
);
331 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
332 lttng_destroy_handle(handle
);
336 MSG("JUL events (Logger name):\n-------------------------");
342 for (i
= 0; i
< size
; i
++) {
343 if (cur_pid
!= event_list
[i
].pid
) {
344 cur_pid
= event_list
[i
].pid
;
345 cmdline
= get_cmdline_by_pid(cur_pid
);
346 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
349 MSG("%s- %s", indent6
, event_list
[i
].name
);
355 lttng_destroy_handle(handle
);
360 lttng_destroy_handle(handle
);
365 * Ask session daemon for all user space tracepoints available.
367 static int list_ust_events(void)
370 struct lttng_domain domain
;
371 struct lttng_handle
*handle
;
372 struct lttng_event
*event_list
;
374 char *cmdline
= NULL
;
376 memset(&domain
, 0, sizeof(domain
));
378 DBG("Getting UST tracing events");
380 domain
.type
= LTTNG_DOMAIN_UST
;
382 handle
= lttng_create_handle(NULL
, &domain
);
383 if (handle
== NULL
) {
387 size
= lttng_list_tracepoints(handle
, &event_list
);
389 ERR("Unable to list UST events: %s", lttng_strerror(size
));
390 lttng_destroy_handle(handle
);
394 MSG("UST events:\n-------------");
400 for (i
= 0; i
< size
; i
++) {
401 if (cur_pid
!= event_list
[i
].pid
) {
402 cur_pid
= event_list
[i
].pid
;
403 cmdline
= get_cmdline_by_pid(cur_pid
);
404 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
407 print_events(&event_list
[i
]);
413 lttng_destroy_handle(handle
);
418 lttng_destroy_handle(handle
);
423 * Ask session daemon for all user space tracepoint fields available.
425 static int list_ust_event_fields(void)
428 struct lttng_domain domain
;
429 struct lttng_handle
*handle
;
430 struct lttng_event_field
*event_field_list
;
432 char *cmdline
= NULL
;
434 struct lttng_event cur_event
;
436 memset(&domain
, 0, sizeof(domain
));
437 memset(&cur_event
, 0, sizeof(cur_event
));
439 DBG("Getting UST tracing event fields");
441 domain
.type
= LTTNG_DOMAIN_UST
;
443 handle
= lttng_create_handle(NULL
, &domain
);
444 if (handle
== NULL
) {
448 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
450 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
451 lttng_destroy_handle(handle
);
455 MSG("UST events:\n-------------");
461 for (i
= 0; i
< size
; i
++) {
462 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
463 cur_pid
= event_field_list
[i
].event
.pid
;
464 cmdline
= get_cmdline_by_pid(cur_pid
);
465 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
468 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
469 print_events(&event_field_list
[i
].event
);
470 memcpy(&cur_event
, &event_field_list
[i
].event
,
473 print_event_field(&event_field_list
[i
]);
478 free(event_field_list
);
479 lttng_destroy_handle(handle
);
484 lttng_destroy_handle(handle
);
489 * Ask for all trace events in the kernel and pretty print them.
491 static int list_kernel_events(void)
494 struct lttng_domain domain
;
495 struct lttng_handle
*handle
;
496 struct lttng_event
*event_list
;
498 memset(&domain
, 0, sizeof(domain
));
500 DBG("Getting kernel tracing events");
502 domain
.type
= LTTNG_DOMAIN_KERNEL
;
504 handle
= lttng_create_handle(NULL
, &domain
);
505 if (handle
== NULL
) {
509 size
= lttng_list_tracepoints(handle
, &event_list
);
511 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
512 lttng_destroy_handle(handle
);
516 MSG("Kernel events:\n-------------");
518 for (i
= 0; i
< size
; i
++) {
519 print_events(&event_list
[i
]);
526 lttng_destroy_handle(handle
);
530 lttng_destroy_handle(handle
);
535 * List JUL events for a specific session using the handle.
537 * Return CMD_SUCCESS on success else a negative value.
539 static int list_session_jul_events(void)
542 struct lttng_event
*events
= NULL
;
544 count
= lttng_list_events(handle
, "", &events
);
547 ERR("%s", lttng_strerror(ret
));
551 MSG("Events (Logger name):\n---------------------");
553 MSG("%sNone\n", indent6
);
557 for (i
= 0; i
< count
; i
++) {
558 MSG("%s- %s%s", indent4
, events
[i
].name
,
559 enabled_string(events
[i
].enabled
));
573 * List events of channel of session and domain.
575 static int list_events(const char *channel_name
)
578 struct lttng_event
*events
= NULL
;
580 count
= lttng_list_events(handle
, channel_name
, &events
);
583 ERR("%s", lttng_strerror(ret
));
587 MSG("\n%sEvents:", indent4
);
589 MSG("%sNone\n", indent6
);
593 for (i
= 0; i
< count
; i
++) {
594 print_events(&events
[i
]);
608 * Pretty print channel
610 static void print_channel(struct lttng_channel
*channel
)
612 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
614 MSG("%sAttributes:", indent4
);
615 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
616 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
617 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
618 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
619 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
620 switch (channel
->attr
.output
) {
621 case LTTNG_EVENT_SPLICE
:
622 MSG("%soutput: splice()", indent6
);
624 case LTTNG_EVENT_MMAP
:
625 MSG("%soutput: mmap()", indent6
);
631 * List channel(s) of session and domain.
633 * If channel_name is NULL, all channels are listed.
635 static int list_channels(const char *channel_name
)
637 int count
, i
, ret
= CMD_SUCCESS
;
638 unsigned int chan_found
= 0;
639 struct lttng_channel
*channels
= NULL
;
641 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
643 count
= lttng_list_channels(handle
, &channels
);
646 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
648 WARN("No kernel channel");
651 /* We had a real error */
653 ERR("%s", lttng_strerror(ret
));
659 if (channel_name
== NULL
) {
660 MSG("Channels:\n-------------");
663 for (i
= 0; i
< count
; i
++) {
664 if (channel_name
!= NULL
) {
665 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
671 print_channel(&channels
[i
]);
673 /* Listing events per channel */
674 ret
= list_events(channels
[i
].name
);
676 ERR("%s", lttng_strerror(ret
));
684 if (!chan_found
&& channel_name
!= NULL
) {
685 ERR("Channel %s not found", channel_name
);
699 * List available tracing session. List only basic information.
701 * If session_name is NULL, all sessions are listed.
703 static int list_sessions(const char *session_name
)
706 unsigned int session_found
= 0;
707 struct lttng_session
*sessions
;
709 count
= lttng_list_sessions(&sessions
);
710 DBG("Session count %d", count
);
713 ERR("%s", lttng_strerror(ret
));
715 } else if (count
== 0) {
716 MSG("Currently no available tracing session");
720 if (session_name
== NULL
) {
721 MSG("Available tracing sessions:");
724 for (i
= 0; i
< count
; i
++) {
725 if (session_name
!= NULL
) {
726 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
728 MSG("Tracing session %s: [%s%s]", session_name
,
729 active_string(sessions
[i
].enabled
),
730 snapshot_string(sessions
[i
].snapshot_mode
));
731 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
735 MSG(" %d) %s (%s) [%s%s]", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
736 active_string(sessions
[i
].enabled
),
737 snapshot_string(sessions
[i
].snapshot_mode
));
743 if (!session_found
&& session_name
!= NULL
) {
744 ERR("Session '%s' not found", session_name
);
749 if (session_name
== NULL
) {
750 MSG("\nUse lttng list <session_name> for more details");
761 * List available domain(s) for a session.
763 static int list_domains(const char *session_name
)
765 int i
, count
, ret
= CMD_SUCCESS
;
766 struct lttng_domain
*domains
= NULL
;
768 MSG("Domains:\n-------------");
770 count
= lttng_list_domains(session_name
, &domains
);
773 ERR("%s", lttng_strerror(ret
));
775 } else if (count
== 0) {
780 for (i
= 0; i
< count
; i
++) {
781 switch (domains
[i
].type
) {
782 case LTTNG_DOMAIN_KERNEL
:
785 case LTTNG_DOMAIN_UST
:
786 MSG(" - UST global");
788 case LTTNG_DOMAIN_JUL
:
789 MSG(" - JUL (Java Util Logging)");
804 * The 'list <options>' first level command
806 int cmd_list(int argc
, const char **argv
)
808 int opt
, ret
= CMD_SUCCESS
;
809 const char *session_name
;
810 static poptContext pc
;
811 struct lttng_domain domain
;
812 struct lttng_domain
*domains
= NULL
;
814 memset(&domain
, 0, sizeof(domain
));
822 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
823 poptReadDefaultConfig(pc
, 0);
825 while ((opt
= poptGetNextOpt(pc
)) != -1) {
833 case OPT_LIST_OPTIONS
:
834 list_cmd_options(stdout
, long_options
);
843 /* Get session name (trailing argument) */
844 session_name
= poptGetArg(pc
);
845 DBG2("Session name: %s", session_name
);
848 domain
.type
= LTTNG_DOMAIN_KERNEL
;
849 } else if (opt_userspace
) {
850 DBG2("Listing userspace global domain");
851 domain
.type
= LTTNG_DOMAIN_UST
;
852 } else if (opt_jul
) {
853 DBG2("Listing JUL domain");
854 domain
.type
= LTTNG_DOMAIN_JUL
;
857 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
858 handle
= lttng_create_handle(session_name
, &domain
);
859 if (handle
== NULL
) {
865 if (session_name
== NULL
) {
866 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
867 ret
= list_sessions(NULL
);
873 ret
= list_kernel_events();
881 ret
= list_ust_event_fields();
883 ret
= list_ust_events();
891 ret
= list_jul_events();
898 /* List session attributes */
899 ret
= list_sessions(session_name
);
906 ret
= list_domains(session_name
);
911 /* Channel listing */
912 ret
= list_channels(opt_channel
);
919 /* We want all domain(s) */
920 nb_domain
= lttng_list_domains(session_name
, &domains
);
923 ERR("%s", lttng_strerror(ret
));
927 for (i
= 0; i
< nb_domain
; i
++) {
928 switch (domains
[i
].type
) {
929 case LTTNG_DOMAIN_KERNEL
:
930 MSG("=== Domain: Kernel ===\n");
932 case LTTNG_DOMAIN_UST
:
933 MSG("=== Domain: UST global ===\n");
934 MSG("Buffer type: %s\n",
935 domains
[i
].buf_type
==
936 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
938 case LTTNG_DOMAIN_JUL
:
939 MSG("=== Domain: JUL (Java Util Logging) ===\n");
942 MSG("=== Domain: Unimplemented ===\n");
946 /* Clean handle before creating a new one */
948 lttng_destroy_handle(handle
);
951 handle
= lttng_create_handle(session_name
, &domains
[i
]);
952 if (handle
== NULL
) {
957 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
958 ret
= list_session_jul_events();
965 ret
= list_channels(opt_channel
);
976 lttng_destroy_handle(handle
);