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_string(int value
)
185 case LTTNG_LOGLEVEL_EMERG
:
186 return "TRACE_EMERG";
187 case LTTNG_LOGLEVEL_ALERT
:
188 return "TRACE_ALERT";
189 case LTTNG_LOGLEVEL_CRIT
:
191 case LTTNG_LOGLEVEL_ERR
:
193 case LTTNG_LOGLEVEL_WARNING
:
194 return "TRACE_WARNING";
195 case LTTNG_LOGLEVEL_NOTICE
:
196 return "TRACE_NOTICE";
197 case LTTNG_LOGLEVEL_INFO
:
199 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
200 return "TRACE_DEBUG_SYSTEM";
201 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
202 return "TRACE_DEBUG_PROGRAM";
203 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
204 return "TRACE_DEBUG_PROCESS";
205 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
206 return "TRACE_DEBUG_MODULE";
207 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
208 return "TRACE_DEBUG_UNIT";
209 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
210 return "TRACE_DEBUG_FUNCTION";
211 case LTTNG_LOGLEVEL_DEBUG_LINE
:
212 return "TRACE_DEBUG_LINE";
213 case LTTNG_LOGLEVEL_DEBUG
:
214 return "TRACE_DEBUG";
216 return "<<UNKNOWN>>";
220 static const char *logleveltype_string(enum lttng_loglevel_type value
)
223 case LTTNG_EVENT_LOGLEVEL_RANGE
:
225 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
228 return "<<TYPE UNKN>>";
233 * Pretty print single event.
235 static void print_events(struct lttng_event
*event
)
237 switch (event
->type
) {
238 case LTTNG_EVENT_TRACEPOINT
:
240 if (event
->loglevel
!= -1) {
241 MSG("%s%s (loglevel %s %s (%d)) (type: tracepoint)%s%s%s",
244 logleveltype_string(event
->loglevel_type
),
245 loglevel_string(event
->loglevel
),
247 enabled_string(event
->enabled
),
248 exclusion_string(event
->exclusion
),
249 filter_string(event
->filter
));
251 MSG("%s%s (type: tracepoint)%s%s%s",
254 enabled_string(event
->enabled
),
255 exclusion_string(event
->exclusion
),
256 filter_string(event
->filter
));
260 case LTTNG_EVENT_FUNCTION
:
261 MSG("%s%s (type: function)%s%s", indent6
,
262 event
->name
, enabled_string(event
->enabled
),
263 filter_string(event
->filter
));
264 if (event
->attr
.probe
.addr
!= 0) {
265 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
267 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
268 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
271 case LTTNG_EVENT_PROBE
:
272 MSG("%s%s (type: probe)%s%s", indent6
,
273 event
->name
, enabled_string(event
->enabled
),
274 filter_string(event
->filter
));
275 if (event
->attr
.probe
.addr
!= 0) {
276 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
278 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
279 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
282 case LTTNG_EVENT_FUNCTION_ENTRY
:
283 MSG("%s%s (type: function)%s%s", indent6
,
284 event
->name
, enabled_string(event
->enabled
),
285 filter_string(event
->filter
));
286 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
288 case LTTNG_EVENT_SYSCALL
:
289 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
290 enabled_string(event
->enabled
),
291 filter_string(event
->filter
));
293 case LTTNG_EVENT_NOOP
:
294 MSG("%s (type: noop)%s%s", indent6
,
295 enabled_string(event
->enabled
),
296 filter_string(event
->filter
));
298 case LTTNG_EVENT_ALL
:
299 /* We should never have "all" events in list. */
305 static const char *field_type(struct lttng_event_field
*field
)
307 switch(field
->type
) {
308 case LTTNG_EVENT_FIELD_INTEGER
:
310 case LTTNG_EVENT_FIELD_ENUM
:
312 case LTTNG_EVENT_FIELD_FLOAT
:
314 case LTTNG_EVENT_FIELD_STRING
:
316 case LTTNG_EVENT_FIELD_OTHER
:
317 default: /* fall-through */
323 * Pretty print single event fields.
325 static void print_event_field(struct lttng_event_field
*field
)
327 if (!field
->field_name
[0]) {
330 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
331 field_type(field
), field
->nowrite
? " [no write]" : "");
334 static int list_jul_events(void)
337 struct lttng_domain domain
;
338 struct lttng_handle
*handle
;
339 struct lttng_event
*event_list
;
341 char *cmdline
= NULL
;
343 DBG("Getting JUL tracing events");
345 memset(&domain
, 0, sizeof(domain
));
346 domain
.type
= LTTNG_DOMAIN_JUL
;
348 handle
= lttng_create_handle(NULL
, &domain
);
349 if (handle
== NULL
) {
353 size
= lttng_list_tracepoints(handle
, &event_list
);
355 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
356 lttng_destroy_handle(handle
);
360 MSG("JUL events (Logger name):\n-------------------------");
366 for (i
= 0; i
< size
; i
++) {
367 if (cur_pid
!= event_list
[i
].pid
) {
368 cur_pid
= event_list
[i
].pid
;
369 cmdline
= get_cmdline_by_pid(cur_pid
);
370 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
373 MSG("%s- %s", indent6
, event_list
[i
].name
);
379 lttng_destroy_handle(handle
);
384 lttng_destroy_handle(handle
);
389 * Ask session daemon for all user space tracepoints available.
391 static int list_ust_events(void)
394 struct lttng_domain domain
;
395 struct lttng_handle
*handle
;
396 struct lttng_event
*event_list
;
398 char *cmdline
= NULL
;
400 memset(&domain
, 0, sizeof(domain
));
402 DBG("Getting UST tracing events");
404 domain
.type
= LTTNG_DOMAIN_UST
;
406 handle
= lttng_create_handle(NULL
, &domain
);
407 if (handle
== NULL
) {
411 size
= lttng_list_tracepoints(handle
, &event_list
);
413 ERR("Unable to list UST events: %s", lttng_strerror(size
));
414 lttng_destroy_handle(handle
);
418 MSG("UST events:\n-------------");
424 for (i
= 0; i
< size
; i
++) {
425 if (cur_pid
!= event_list
[i
].pid
) {
426 cur_pid
= event_list
[i
].pid
;
427 cmdline
= get_cmdline_by_pid(cur_pid
);
428 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
431 print_events(&event_list
[i
]);
437 lttng_destroy_handle(handle
);
442 lttng_destroy_handle(handle
);
447 * Ask session daemon for all user space tracepoint fields available.
449 static int list_ust_event_fields(void)
452 struct lttng_domain domain
;
453 struct lttng_handle
*handle
;
454 struct lttng_event_field
*event_field_list
;
456 char *cmdline
= NULL
;
458 struct lttng_event cur_event
;
460 memset(&domain
, 0, sizeof(domain
));
461 memset(&cur_event
, 0, sizeof(cur_event
));
463 DBG("Getting UST tracing event fields");
465 domain
.type
= LTTNG_DOMAIN_UST
;
467 handle
= lttng_create_handle(NULL
, &domain
);
468 if (handle
== NULL
) {
472 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
474 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
475 lttng_destroy_handle(handle
);
479 MSG("UST events:\n-------------");
485 for (i
= 0; i
< size
; i
++) {
486 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
487 cur_pid
= event_field_list
[i
].event
.pid
;
488 cmdline
= get_cmdline_by_pid(cur_pid
);
489 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
491 /* Wipe current event since we are about to print a new PID. */
492 memset(&cur_event
, 0, sizeof(cur_event
));
494 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
495 print_events(&event_field_list
[i
].event
);
496 memcpy(&cur_event
, &event_field_list
[i
].event
,
499 print_event_field(&event_field_list
[i
]);
504 free(event_field_list
);
505 lttng_destroy_handle(handle
);
510 lttng_destroy_handle(handle
);
515 * Ask for all trace events in the kernel and pretty print them.
517 static int list_kernel_events(void)
520 struct lttng_domain domain
;
521 struct lttng_handle
*handle
;
522 struct lttng_event
*event_list
;
524 memset(&domain
, 0, sizeof(domain
));
526 DBG("Getting kernel tracing events");
528 domain
.type
= LTTNG_DOMAIN_KERNEL
;
530 handle
= lttng_create_handle(NULL
, &domain
);
531 if (handle
== NULL
) {
535 size
= lttng_list_tracepoints(handle
, &event_list
);
537 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
538 lttng_destroy_handle(handle
);
542 MSG("Kernel events:\n-------------");
544 for (i
= 0; i
< size
; i
++) {
545 print_events(&event_list
[i
]);
552 lttng_destroy_handle(handle
);
556 lttng_destroy_handle(handle
);
561 * List JUL events for a specific session using the handle.
563 * Return CMD_SUCCESS on success else a negative value.
565 static int list_session_jul_events(void)
568 struct lttng_event
*events
= NULL
;
570 count
= lttng_list_events(handle
, "", &events
);
573 ERR("%s", lttng_strerror(ret
));
577 MSG("Events (Logger name):\n---------------------");
579 MSG("%sNone\n", indent6
);
583 for (i
= 0; i
< count
; i
++) {
584 MSG("%s- %s%s", indent4
, events
[i
].name
,
585 enabled_string(events
[i
].enabled
));
599 * List events of channel of session and domain.
601 static int list_events(const char *channel_name
)
604 struct lttng_event
*events
= NULL
;
606 count
= lttng_list_events(handle
, channel_name
, &events
);
609 ERR("%s", lttng_strerror(ret
));
613 MSG("\n%sEvents:", indent4
);
615 MSG("%sNone\n", indent6
);
619 for (i
= 0; i
< count
; i
++) {
620 print_events(&events
[i
]);
634 * Pretty print channel
636 static void print_channel(struct lttng_channel
*channel
)
638 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
640 MSG("%sAttributes:", indent4
);
641 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
642 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
643 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
644 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
645 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
646 switch (channel
->attr
.output
) {
647 case LTTNG_EVENT_SPLICE
:
648 MSG("%soutput: splice()", indent6
);
650 case LTTNG_EVENT_MMAP
:
651 MSG("%soutput: mmap()", indent6
);
657 * List channel(s) of session and domain.
659 * If channel_name is NULL, all channels are listed.
661 static int list_channels(const char *channel_name
)
663 int count
, i
, ret
= CMD_SUCCESS
;
664 unsigned int chan_found
= 0;
665 struct lttng_channel
*channels
= NULL
;
667 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
669 count
= lttng_list_channels(handle
, &channels
);
672 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
674 WARN("No kernel channel");
677 /* We had a real error */
679 ERR("%s", lttng_strerror(ret
));
685 if (channel_name
== NULL
) {
686 MSG("Channels:\n-------------");
689 for (i
= 0; i
< count
; i
++) {
690 if (channel_name
!= NULL
) {
691 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
697 print_channel(&channels
[i
]);
699 /* Listing events per channel */
700 ret
= list_events(channels
[i
].name
);
702 ERR("%s", lttng_strerror(ret
));
710 if (!chan_found
&& channel_name
!= NULL
) {
711 ERR("Channel %s not found", channel_name
);
725 * List available tracing session. List only basic information.
727 * If session_name is NULL, all sessions are listed.
729 static int list_sessions(const char *session_name
)
732 unsigned int session_found
= 0;
733 struct lttng_session
*sessions
;
735 count
= lttng_list_sessions(&sessions
);
736 DBG("Session count %d", count
);
739 ERR("%s", lttng_strerror(ret
));
741 } else if (count
== 0) {
742 MSG("Currently no available tracing session");
746 if (session_name
== NULL
) {
747 MSG("Available tracing sessions:");
750 for (i
= 0; i
< count
; i
++) {
751 if (session_name
!= NULL
) {
752 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
754 MSG("Tracing session %s: [%s%s]", session_name
,
755 active_string(sessions
[i
].enabled
),
756 snapshot_string(sessions
[i
].snapshot_mode
));
757 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
761 MSG(" %d) %s (%s) [%s%s]", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
762 active_string(sessions
[i
].enabled
),
763 snapshot_string(sessions
[i
].snapshot_mode
));
769 if (!session_found
&& session_name
!= NULL
) {
770 ERR("Session '%s' not found", session_name
);
775 if (session_name
== NULL
) {
776 MSG("\nUse lttng list <session_name> for more details");
787 * List available domain(s) for a session.
789 static int list_domains(const char *session_name
)
791 int i
, count
, ret
= CMD_SUCCESS
;
792 struct lttng_domain
*domains
= NULL
;
794 MSG("Domains:\n-------------");
796 count
= lttng_list_domains(session_name
, &domains
);
799 ERR("%s", lttng_strerror(ret
));
801 } else if (count
== 0) {
806 for (i
= 0; i
< count
; i
++) {
807 switch (domains
[i
].type
) {
808 case LTTNG_DOMAIN_KERNEL
:
811 case LTTNG_DOMAIN_UST
:
812 MSG(" - UST global");
814 case LTTNG_DOMAIN_JUL
:
815 MSG(" - JUL (Java Util Logging)");
830 * The 'list <options>' first level command
832 int cmd_list(int argc
, const char **argv
)
834 int opt
, ret
= CMD_SUCCESS
;
835 const char *session_name
;
836 static poptContext pc
;
837 struct lttng_domain domain
;
838 struct lttng_domain
*domains
= NULL
;
840 memset(&domain
, 0, sizeof(domain
));
848 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
849 poptReadDefaultConfig(pc
, 0);
851 while ((opt
= poptGetNextOpt(pc
)) != -1) {
859 case OPT_LIST_OPTIONS
:
860 list_cmd_options(stdout
, long_options
);
869 /* Get session name (trailing argument) */
870 session_name
= poptGetArg(pc
);
871 DBG2("Session name: %s", session_name
);
874 domain
.type
= LTTNG_DOMAIN_KERNEL
;
875 } else if (opt_userspace
) {
876 DBG2("Listing userspace global domain");
877 domain
.type
= LTTNG_DOMAIN_UST
;
878 } else if (opt_jul
) {
879 DBG2("Listing JUL domain");
880 domain
.type
= LTTNG_DOMAIN_JUL
;
883 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
884 handle
= lttng_create_handle(session_name
, &domain
);
885 if (handle
== NULL
) {
891 if (session_name
== NULL
) {
892 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
893 ret
= list_sessions(NULL
);
899 ret
= list_kernel_events();
907 ret
= list_ust_event_fields();
909 ret
= list_ust_events();
917 ret
= list_jul_events();
924 /* List session attributes */
925 ret
= list_sessions(session_name
);
932 ret
= list_domains(session_name
);
936 if (opt_kernel
|| opt_userspace
) {
937 /* Channel listing */
938 ret
= list_channels(opt_channel
);
945 /* We want all domain(s) */
946 nb_domain
= lttng_list_domains(session_name
, &domains
);
949 ERR("%s", lttng_strerror(ret
));
953 for (i
= 0; i
< nb_domain
; i
++) {
954 switch (domains
[i
].type
) {
955 case LTTNG_DOMAIN_KERNEL
:
956 MSG("=== Domain: Kernel ===\n");
958 case LTTNG_DOMAIN_UST
:
959 MSG("=== Domain: UST global ===\n");
960 MSG("Buffer type: %s\n",
961 domains
[i
].buf_type
==
962 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
964 case LTTNG_DOMAIN_JUL
:
965 MSG("=== Domain: JUL (Java Util Logging) ===\n");
968 MSG("=== Domain: Unimplemented ===\n");
972 /* Clean handle before creating a new one */
974 lttng_destroy_handle(handle
);
977 handle
= lttng_create_handle(session_name
, &domains
[i
]);
978 if (handle
== NULL
) {
983 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
984 ret
= list_session_jul_events();
991 ret
= list_channels(opt_channel
);
1002 lttng_destroy_handle(handle
);
1005 poptFreeContext(pc
);