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]";
172 const char *exclusion_string(int value
)
175 case 1: return " [has exclusions]";
180 static const char *loglevel_jul_string(int value
)
185 case LTTNG_LOGLEVEL_JUL_OFF
:
187 case LTTNG_LOGLEVEL_JUL_SEVERE
:
189 case LTTNG_LOGLEVEL_JUL_WARNING
:
190 return "JUL_WARNING";
191 case LTTNG_LOGLEVEL_JUL_INFO
:
193 case LTTNG_LOGLEVEL_JUL_CONFIG
:
195 case LTTNG_LOGLEVEL_JUL_FINE
:
197 case LTTNG_LOGLEVEL_JUL_FINER
:
199 case LTTNG_LOGLEVEL_JUL_FINEST
:
201 case LTTNG_LOGLEVEL_JUL_ALL
:
204 return "<<UNKNOWN>>";
208 static const char *loglevel_string(int value
)
213 case LTTNG_LOGLEVEL_EMERG
:
214 return "TRACE_EMERG";
215 case LTTNG_LOGLEVEL_ALERT
:
216 return "TRACE_ALERT";
217 case LTTNG_LOGLEVEL_CRIT
:
219 case LTTNG_LOGLEVEL_ERR
:
221 case LTTNG_LOGLEVEL_WARNING
:
222 return "TRACE_WARNING";
223 case LTTNG_LOGLEVEL_NOTICE
:
224 return "TRACE_NOTICE";
225 case LTTNG_LOGLEVEL_INFO
:
227 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
228 return "TRACE_DEBUG_SYSTEM";
229 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
230 return "TRACE_DEBUG_PROGRAM";
231 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
232 return "TRACE_DEBUG_PROCESS";
233 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
234 return "TRACE_DEBUG_MODULE";
235 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
236 return "TRACE_DEBUG_UNIT";
237 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
238 return "TRACE_DEBUG_FUNCTION";
239 case LTTNG_LOGLEVEL_DEBUG_LINE
:
240 return "TRACE_DEBUG_LINE";
241 case LTTNG_LOGLEVEL_DEBUG
:
242 return "TRACE_DEBUG";
244 return "<<UNKNOWN>>";
248 static const char *logleveltype_string(enum lttng_loglevel_type value
)
251 case LTTNG_EVENT_LOGLEVEL_ALL
:
253 case LTTNG_EVENT_LOGLEVEL_RANGE
:
255 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
258 return " <<TYPE UNKN>>";
263 * Pretty print single event.
265 static void print_events(struct lttng_event
*event
)
267 switch (event
->type
) {
268 case LTTNG_EVENT_TRACEPOINT
:
270 if (event
->loglevel
!= -1) {
271 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
274 logleveltype_string(event
->loglevel_type
),
275 loglevel_string(event
->loglevel
),
277 enabled_string(event
->enabled
),
278 exclusion_string(event
->exclusion
),
279 filter_string(event
->filter
));
281 MSG("%s%s (type: tracepoint)%s%s%s",
284 enabled_string(event
->enabled
),
285 exclusion_string(event
->exclusion
),
286 filter_string(event
->filter
));
290 case LTTNG_EVENT_FUNCTION
:
291 MSG("%s%s (type: function)%s%s", indent6
,
292 event
->name
, enabled_string(event
->enabled
),
293 filter_string(event
->filter
));
294 if (event
->attr
.probe
.addr
!= 0) {
295 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
297 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
298 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
301 case LTTNG_EVENT_PROBE
:
302 MSG("%s%s (type: probe)%s%s", indent6
,
303 event
->name
, enabled_string(event
->enabled
),
304 filter_string(event
->filter
));
305 if (event
->attr
.probe
.addr
!= 0) {
306 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
308 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
309 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
312 case LTTNG_EVENT_FUNCTION_ENTRY
:
313 MSG("%s%s (type: function)%s%s", indent6
,
314 event
->name
, enabled_string(event
->enabled
),
315 filter_string(event
->filter
));
316 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
318 case LTTNG_EVENT_SYSCALL
:
319 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
320 enabled_string(event
->enabled
),
321 filter_string(event
->filter
));
323 case LTTNG_EVENT_NOOP
:
324 MSG("%s (type: noop)%s%s", indent6
,
325 enabled_string(event
->enabled
),
326 filter_string(event
->filter
));
328 case LTTNG_EVENT_ALL
:
329 /* We should never have "all" events in list. */
335 static const char *field_type(struct lttng_event_field
*field
)
337 switch(field
->type
) {
338 case LTTNG_EVENT_FIELD_INTEGER
:
340 case LTTNG_EVENT_FIELD_ENUM
:
342 case LTTNG_EVENT_FIELD_FLOAT
:
344 case LTTNG_EVENT_FIELD_STRING
:
346 case LTTNG_EVENT_FIELD_OTHER
:
347 default: /* fall-through */
353 * Pretty print single event fields.
355 static void print_event_field(struct lttng_event_field
*field
)
357 if (!field
->field_name
[0]) {
360 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
361 field_type(field
), field
->nowrite
? " [no write]" : "");
364 static int list_jul_events(void)
367 struct lttng_domain domain
;
368 struct lttng_handle
*handle
;
369 struct lttng_event
*event_list
;
371 char *cmdline
= NULL
;
373 DBG("Getting JUL tracing events");
375 memset(&domain
, 0, sizeof(domain
));
376 domain
.type
= LTTNG_DOMAIN_JUL
;
378 handle
= lttng_create_handle(NULL
, &domain
);
379 if (handle
== NULL
) {
383 size
= lttng_list_tracepoints(handle
, &event_list
);
385 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
386 lttng_destroy_handle(handle
);
390 MSG("JUL events (Logger name):\n-------------------------");
396 for (i
= 0; i
< size
; i
++) {
397 if (cur_pid
!= event_list
[i
].pid
) {
398 cur_pid
= event_list
[i
].pid
;
399 cmdline
= get_cmdline_by_pid(cur_pid
);
400 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
403 MSG("%s- %s", indent6
, event_list
[i
].name
);
409 lttng_destroy_handle(handle
);
414 lttng_destroy_handle(handle
);
419 * Ask session daemon for all user space tracepoints available.
421 static int list_ust_events(void)
424 struct lttng_domain domain
;
425 struct lttng_handle
*handle
;
426 struct lttng_event
*event_list
;
428 char *cmdline
= NULL
;
430 memset(&domain
, 0, sizeof(domain
));
432 DBG("Getting UST tracing events");
434 domain
.type
= LTTNG_DOMAIN_UST
;
436 handle
= lttng_create_handle(NULL
, &domain
);
437 if (handle
== NULL
) {
441 size
= lttng_list_tracepoints(handle
, &event_list
);
443 ERR("Unable to list UST events: %s", lttng_strerror(size
));
444 lttng_destroy_handle(handle
);
448 MSG("UST events:\n-------------");
454 for (i
= 0; i
< size
; i
++) {
455 if (cur_pid
!= event_list
[i
].pid
) {
456 cur_pid
= event_list
[i
].pid
;
457 cmdline
= get_cmdline_by_pid(cur_pid
);
458 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
461 print_events(&event_list
[i
]);
467 lttng_destroy_handle(handle
);
472 lttng_destroy_handle(handle
);
477 * Ask session daemon for all user space tracepoint fields available.
479 static int list_ust_event_fields(void)
482 struct lttng_domain domain
;
483 struct lttng_handle
*handle
;
484 struct lttng_event_field
*event_field_list
;
486 char *cmdline
= NULL
;
488 struct lttng_event cur_event
;
490 memset(&domain
, 0, sizeof(domain
));
491 memset(&cur_event
, 0, sizeof(cur_event
));
493 DBG("Getting UST tracing event fields");
495 domain
.type
= LTTNG_DOMAIN_UST
;
497 handle
= lttng_create_handle(NULL
, &domain
);
498 if (handle
== NULL
) {
502 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
504 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
505 lttng_destroy_handle(handle
);
509 MSG("UST events:\n-------------");
515 for (i
= 0; i
< size
; i
++) {
516 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
517 cur_pid
= event_field_list
[i
].event
.pid
;
518 cmdline
= get_cmdline_by_pid(cur_pid
);
519 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
521 /* Wipe current event since we are about to print a new PID. */
522 memset(&cur_event
, 0, sizeof(cur_event
));
524 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
525 print_events(&event_field_list
[i
].event
);
526 memcpy(&cur_event
, &event_field_list
[i
].event
,
529 print_event_field(&event_field_list
[i
]);
534 free(event_field_list
);
535 lttng_destroy_handle(handle
);
540 lttng_destroy_handle(handle
);
545 * Ask for all trace events in the kernel and pretty print them.
547 static int list_kernel_events(void)
550 struct lttng_domain domain
;
551 struct lttng_handle
*handle
;
552 struct lttng_event
*event_list
;
554 memset(&domain
, 0, sizeof(domain
));
556 DBG("Getting kernel tracing events");
558 domain
.type
= LTTNG_DOMAIN_KERNEL
;
560 handle
= lttng_create_handle(NULL
, &domain
);
561 if (handle
== NULL
) {
565 size
= lttng_list_tracepoints(handle
, &event_list
);
567 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
568 lttng_destroy_handle(handle
);
572 MSG("Kernel events:\n-------------");
574 for (i
= 0; i
< size
; i
++) {
575 print_events(&event_list
[i
]);
582 lttng_destroy_handle(handle
);
586 lttng_destroy_handle(handle
);
591 * List JUL events for a specific session using the handle.
593 * Return CMD_SUCCESS on success else a negative value.
595 static int list_session_jul_events(void)
598 struct lttng_event
*events
= NULL
;
600 count
= lttng_list_events(handle
, "", &events
);
603 ERR("%s", lttng_strerror(ret
));
607 MSG("Events (Logger name):\n---------------------");
609 MSG("%sNone\n", indent6
);
613 for (i
= 0; i
< count
; i
++) {
614 MSG("%s- %s%s (loglevel%s %s)", indent4
, events
[i
].name
,
615 enabled_string(events
[i
].enabled
),
616 logleveltype_string(events
[i
].loglevel_type
),
617 loglevel_jul_string(events
[i
].loglevel
));
631 * List events of channel of session and domain.
633 static int list_events(const char *channel_name
)
636 struct lttng_event
*events
= NULL
;
638 count
= lttng_list_events(handle
, channel_name
, &events
);
641 ERR("%s", lttng_strerror(ret
));
645 MSG("\n%sEvents:", indent4
);
647 MSG("%sNone\n", indent6
);
651 for (i
= 0; i
< count
; i
++) {
652 print_events(&events
[i
]);
666 * Pretty print channel
668 static void print_channel(struct lttng_channel
*channel
)
670 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
672 MSG("%sAttributes:", indent4
);
673 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
674 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
675 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
676 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
677 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
678 MSG("%strace file count: %" PRIu64
, indent6
, channel
->attr
.tracefile_count
);
679 MSG("%strace file size (bytes): %" PRIu64
, indent6
, channel
->attr
.tracefile_size
);
680 switch (channel
->attr
.output
) {
681 case LTTNG_EVENT_SPLICE
:
682 MSG("%soutput: splice()", indent6
);
684 case LTTNG_EVENT_MMAP
:
685 MSG("%soutput: mmap()", indent6
);
691 * List channel(s) of session and domain.
693 * If channel_name is NULL, all channels are listed.
695 static int list_channels(const char *channel_name
)
697 int count
, i
, ret
= CMD_SUCCESS
;
698 unsigned int chan_found
= 0;
699 struct lttng_channel
*channels
= NULL
;
701 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
703 count
= lttng_list_channels(handle
, &channels
);
706 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
708 WARN("No kernel channel");
711 /* We had a real error */
713 ERR("%s", lttng_strerror(ret
));
719 if (channel_name
== NULL
) {
720 MSG("Channels:\n-------------");
723 for (i
= 0; i
< count
; i
++) {
724 if (channel_name
!= NULL
) {
725 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
731 print_channel(&channels
[i
]);
733 /* Listing events per channel */
734 ret
= list_events(channels
[i
].name
);
736 ERR("%s", lttng_strerror(ret
));
744 if (!chan_found
&& channel_name
!= NULL
) {
745 ERR("Channel %s not found", channel_name
);
759 * List available tracing session. List only basic information.
761 * If session_name is NULL, all sessions are listed.
763 static int list_sessions(const char *session_name
)
766 unsigned int session_found
= 0;
767 struct lttng_session
*sessions
;
769 count
= lttng_list_sessions(&sessions
);
770 DBG("Session count %d", count
);
773 ERR("%s", lttng_strerror(ret
));
775 } else if (count
== 0) {
776 MSG("Currently no available tracing session");
780 if (session_name
== NULL
) {
781 MSG("Available tracing sessions:");
784 for (i
= 0; i
< count
; i
++) {
785 if (session_name
!= NULL
) {
786 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
788 MSG("Tracing session %s: [%s%s]", session_name
,
789 active_string(sessions
[i
].enabled
),
790 snapshot_string(sessions
[i
].snapshot_mode
));
791 MSG("%sTrace path: %s", indent4
, sessions
[i
].path
);
792 MSG("%sLive timer interval (usec): %u\n", indent4
,
793 sessions
[i
].live_timer_interval
);
797 MSG(" %d) %s (%s) [%s%s]", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
798 active_string(sessions
[i
].enabled
),
799 snapshot_string(sessions
[i
].snapshot_mode
));
805 if (!session_found
&& session_name
!= NULL
) {
806 ERR("Session '%s' not found", session_name
);
811 if (session_name
== NULL
) {
812 MSG("\nUse lttng list <session_name> for more details");
823 * List available domain(s) for a session.
825 static int list_domains(const char *session_name
)
827 int i
, count
, ret
= CMD_SUCCESS
;
828 struct lttng_domain
*domains
= NULL
;
830 MSG("Domains:\n-------------");
832 count
= lttng_list_domains(session_name
, &domains
);
835 ERR("%s", lttng_strerror(ret
));
837 } else if (count
== 0) {
842 for (i
= 0; i
< count
; i
++) {
843 switch (domains
[i
].type
) {
844 case LTTNG_DOMAIN_KERNEL
:
847 case LTTNG_DOMAIN_UST
:
848 MSG(" - UST global");
850 case LTTNG_DOMAIN_JUL
:
851 MSG(" - JUL (Java Util Logging)");
866 * The 'list <options>' first level command
868 int cmd_list(int argc
, const char **argv
)
870 int opt
, ret
= CMD_SUCCESS
;
871 const char *session_name
;
872 static poptContext pc
;
873 struct lttng_domain domain
;
874 struct lttng_domain
*domains
= NULL
;
876 memset(&domain
, 0, sizeof(domain
));
884 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
885 poptReadDefaultConfig(pc
, 0);
887 while ((opt
= poptGetNextOpt(pc
)) != -1) {
895 case OPT_LIST_OPTIONS
:
896 list_cmd_options(stdout
, long_options
);
905 /* Get session name (trailing argument) */
906 session_name
= poptGetArg(pc
);
907 DBG2("Session name: %s", session_name
);
910 domain
.type
= LTTNG_DOMAIN_KERNEL
;
911 } else if (opt_userspace
) {
912 DBG2("Listing userspace global domain");
913 domain
.type
= LTTNG_DOMAIN_UST
;
914 } else if (opt_jul
) {
915 DBG2("Listing JUL domain");
916 domain
.type
= LTTNG_DOMAIN_JUL
;
919 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
920 handle
= lttng_create_handle(session_name
, &domain
);
921 if (handle
== NULL
) {
927 if (session_name
== NULL
) {
928 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
929 ret
= list_sessions(NULL
);
935 ret
= list_kernel_events();
943 ret
= list_ust_event_fields();
945 ret
= list_ust_events();
953 ret
= list_jul_events();
960 /* List session attributes */
961 ret
= list_sessions(session_name
);
968 ret
= list_domains(session_name
);
972 if (opt_kernel
|| opt_userspace
) {
973 /* Channel listing */
974 ret
= list_channels(opt_channel
);
981 /* We want all domain(s) */
982 nb_domain
= lttng_list_domains(session_name
, &domains
);
985 ERR("%s", lttng_strerror(ret
));
989 for (i
= 0; i
< nb_domain
; i
++) {
990 switch (domains
[i
].type
) {
991 case LTTNG_DOMAIN_KERNEL
:
992 MSG("=== Domain: Kernel ===\n");
994 case LTTNG_DOMAIN_UST
:
995 MSG("=== Domain: UST global ===\n");
996 MSG("Buffer type: %s\n",
997 domains
[i
].buf_type
==
998 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
1000 case LTTNG_DOMAIN_JUL
:
1001 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1004 MSG("=== Domain: Unimplemented ===\n");
1008 /* Clean handle before creating a new one */
1010 lttng_destroy_handle(handle
);
1013 handle
= lttng_create_handle(session_name
, &domains
[i
]);
1014 if (handle
== NULL
) {
1019 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
1020 ret
= list_session_jul_events();
1027 ret
= list_channels(opt_channel
);
1038 lttng_destroy_handle(handle
);
1041 poptFreeContext(pc
);