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
[20]; /* 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
= zmalloc(PATH_MAX
);
121 perror("malloc cmdline");
124 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
126 perror("fread proc list");
135 const char *active_string(int value
)
138 case 0: return "inactive";
139 case 1: return "active";
141 default: return NULL
;
145 static const char *snapshot_string(int value
)
156 const char *enabled_string(int value
)
159 case 0: return " [disabled]";
160 case 1: return " [enabled]";
162 default: return NULL
;
167 const char *filter_string(int value
)
170 case 1: return " [with filter]";
176 const char *exclusion_string(int value
)
179 case 1: return " [has exclusions]";
184 static const char *loglevel_string(int value
)
189 case LTTNG_LOGLEVEL_EMERG
:
190 return "TRACE_EMERG";
191 case LTTNG_LOGLEVEL_ALERT
:
192 return "TRACE_ALERT";
193 case LTTNG_LOGLEVEL_CRIT
:
195 case LTTNG_LOGLEVEL_ERR
:
197 case LTTNG_LOGLEVEL_WARNING
:
198 return "TRACE_WARNING";
199 case LTTNG_LOGLEVEL_NOTICE
:
200 return "TRACE_NOTICE";
201 case LTTNG_LOGLEVEL_INFO
:
203 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
204 return "TRACE_DEBUG_SYSTEM";
205 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
206 return "TRACE_DEBUG_PROGRAM";
207 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
208 return "TRACE_DEBUG_PROCESS";
209 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
210 return "TRACE_DEBUG_MODULE";
211 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
212 return "TRACE_DEBUG_UNIT";
213 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
214 return "TRACE_DEBUG_FUNCTION";
215 case LTTNG_LOGLEVEL_DEBUG_LINE
:
216 return "TRACE_DEBUG_LINE";
217 case LTTNG_LOGLEVEL_DEBUG
:
218 return "TRACE_DEBUG";
220 return "<<UNKNOWN>>";
224 static const char *logleveltype_string(enum lttng_loglevel_type value
)
227 case LTTNG_EVENT_LOGLEVEL_ALL
:
229 case LTTNG_EVENT_LOGLEVEL_RANGE
:
231 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
234 return " <<TYPE UNKN>>";
239 * Pretty print single event.
241 static void print_events(struct lttng_event
*event
)
243 switch (event
->type
) {
244 case LTTNG_EVENT_TRACEPOINT
:
246 if (event
->loglevel
!= -1) {
247 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
250 logleveltype_string(event
->loglevel_type
),
251 loglevel_string(event
->loglevel
),
253 enabled_string(event
->enabled
),
254 exclusion_string(event
->exclusion
),
255 filter_string(event
->filter
));
257 MSG("%s%s (type: tracepoint)%s%s%s",
260 enabled_string(event
->enabled
),
261 exclusion_string(event
->exclusion
),
262 filter_string(event
->filter
));
266 case LTTNG_EVENT_FUNCTION
:
267 MSG("%s%s (type: function)%s%s", indent6
,
268 event
->name
, enabled_string(event
->enabled
),
269 filter_string(event
->filter
));
270 if (event
->attr
.probe
.addr
!= 0) {
271 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
273 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
274 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
277 case LTTNG_EVENT_PROBE
:
278 MSG("%s%s (type: probe)%s%s", indent6
,
279 event
->name
, enabled_string(event
->enabled
),
280 filter_string(event
->filter
));
281 if (event
->attr
.probe
.addr
!= 0) {
282 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
284 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
285 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
288 case LTTNG_EVENT_FUNCTION_ENTRY
:
289 MSG("%s%s (type: function)%s%s", indent6
,
290 event
->name
, enabled_string(event
->enabled
),
291 filter_string(event
->filter
));
292 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
294 case LTTNG_EVENT_SYSCALL
:
295 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
296 enabled_string(event
->enabled
),
297 filter_string(event
->filter
));
299 case LTTNG_EVENT_NOOP
:
300 MSG("%s (type: noop)%s%s", indent6
,
301 enabled_string(event
->enabled
),
302 filter_string(event
->filter
));
304 case LTTNG_EVENT_ALL
:
305 /* We should never have "all" events in list. */
311 static const char *field_type(struct lttng_event_field
*field
)
313 switch(field
->type
) {
314 case LTTNG_EVENT_FIELD_INTEGER
:
316 case LTTNG_EVENT_FIELD_ENUM
:
318 case LTTNG_EVENT_FIELD_FLOAT
:
320 case LTTNG_EVENT_FIELD_STRING
:
322 case LTTNG_EVENT_FIELD_OTHER
:
323 default: /* fall-through */
329 * Pretty print single event fields.
331 static void print_event_field(struct lttng_event_field
*field
)
333 if (!field
->field_name
[0]) {
336 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
337 field_type(field
), field
->nowrite
? " [no write]" : "");
340 static int list_jul_events(void)
343 struct lttng_domain domain
;
344 struct lttng_handle
*handle
;
345 struct lttng_event
*event_list
;
347 char *cmdline
= NULL
;
349 DBG("Getting JUL tracing events");
351 memset(&domain
, 0, sizeof(domain
));
352 domain
.type
= LTTNG_DOMAIN_JUL
;
354 handle
= lttng_create_handle(NULL
, &domain
);
355 if (handle
== NULL
) {
359 size
= lttng_list_tracepoints(handle
, &event_list
);
361 ERR("Unable to list JUL events: %s", lttng_strerror(size
));
362 lttng_destroy_handle(handle
);
366 MSG("JUL events (Logger name):\n-------------------------");
372 for (i
= 0; i
< size
; i
++) {
373 if (cur_pid
!= event_list
[i
].pid
) {
374 cur_pid
= event_list
[i
].pid
;
375 cmdline
= get_cmdline_by_pid(cur_pid
);
376 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
379 MSG("%s- %s", indent6
, event_list
[i
].name
);
385 lttng_destroy_handle(handle
);
390 lttng_destroy_handle(handle
);
395 * Ask session daemon for all user space tracepoints available.
397 static int list_ust_events(void)
400 struct lttng_domain domain
;
401 struct lttng_handle
*handle
;
402 struct lttng_event
*event_list
;
404 char *cmdline
= NULL
;
406 memset(&domain
, 0, sizeof(domain
));
408 DBG("Getting UST tracing events");
410 domain
.type
= LTTNG_DOMAIN_UST
;
412 handle
= lttng_create_handle(NULL
, &domain
);
413 if (handle
== NULL
) {
417 size
= lttng_list_tracepoints(handle
, &event_list
);
419 ERR("Unable to list UST events: %s", lttng_strerror(size
));
420 lttng_destroy_handle(handle
);
424 MSG("UST events:\n-------------");
430 for (i
= 0; i
< size
; i
++) {
431 if (cur_pid
!= event_list
[i
].pid
) {
432 cur_pid
= event_list
[i
].pid
;
433 cmdline
= get_cmdline_by_pid(cur_pid
);
434 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
437 print_events(&event_list
[i
]);
443 lttng_destroy_handle(handle
);
448 lttng_destroy_handle(handle
);
453 * Ask session daemon for all user space tracepoint fields available.
455 static int list_ust_event_fields(void)
458 struct lttng_domain domain
;
459 struct lttng_handle
*handle
;
460 struct lttng_event_field
*event_field_list
;
462 char *cmdline
= NULL
;
464 struct lttng_event cur_event
;
466 memset(&domain
, 0, sizeof(domain
));
467 memset(&cur_event
, 0, sizeof(cur_event
));
469 DBG("Getting UST tracing event fields");
471 domain
.type
= LTTNG_DOMAIN_UST
;
473 handle
= lttng_create_handle(NULL
, &domain
);
474 if (handle
== NULL
) {
478 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
480 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
481 lttng_destroy_handle(handle
);
485 MSG("UST events:\n-------------");
491 for (i
= 0; i
< size
; i
++) {
492 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
493 cur_pid
= event_field_list
[i
].event
.pid
;
494 cmdline
= get_cmdline_by_pid(cur_pid
);
495 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
497 /* Wipe current event since we are about to print a new PID. */
498 memset(&cur_event
, 0, sizeof(cur_event
));
500 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
501 print_events(&event_field_list
[i
].event
);
502 memcpy(&cur_event
, &event_field_list
[i
].event
,
505 print_event_field(&event_field_list
[i
]);
510 free(event_field_list
);
511 lttng_destroy_handle(handle
);
516 lttng_destroy_handle(handle
);
521 * Ask for all trace events in the kernel and pretty print them.
523 static int list_kernel_events(void)
526 struct lttng_domain domain
;
527 struct lttng_handle
*handle
;
528 struct lttng_event
*event_list
;
530 memset(&domain
, 0, sizeof(domain
));
532 DBG("Getting kernel tracing events");
534 domain
.type
= LTTNG_DOMAIN_KERNEL
;
536 handle
= lttng_create_handle(NULL
, &domain
);
537 if (handle
== NULL
) {
541 size
= lttng_list_tracepoints(handle
, &event_list
);
543 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
544 lttng_destroy_handle(handle
);
548 MSG("Kernel events:\n-------------");
550 for (i
= 0; i
< size
; i
++) {
551 print_events(&event_list
[i
]);
558 lttng_destroy_handle(handle
);
562 lttng_destroy_handle(handle
);
567 * List JUL events for a specific session using the handle.
569 * Return CMD_SUCCESS on success else a negative value.
571 static int list_session_jul_events(void)
574 struct lttng_event
*events
= NULL
;
576 count
= lttng_list_events(handle
, "", &events
);
579 ERR("%s", lttng_strerror(ret
));
583 MSG("Events (Logger name):\n---------------------");
585 MSG("%sNone\n", indent6
);
589 for (i
= 0; i
< count
; i
++) {
590 MSG("%s- %s%s", indent4
, events
[i
].name
,
591 enabled_string(events
[i
].enabled
));
605 * List events of channel of session and domain.
607 static int list_events(const char *channel_name
)
610 struct lttng_event
*events
= NULL
;
612 count
= lttng_list_events(handle
, channel_name
, &events
);
615 ERR("%s", lttng_strerror(ret
));
619 MSG("\n%sEvents:", indent4
);
621 MSG("%sNone\n", indent6
);
625 for (i
= 0; i
< count
; i
++) {
626 print_events(&events
[i
]);
640 * Pretty print channel
642 static void print_channel(struct lttng_channel
*channel
)
644 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
646 MSG("%sAttributes:", indent4
);
647 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
648 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
649 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
650 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
651 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
652 switch (channel
->attr
.output
) {
653 case LTTNG_EVENT_SPLICE
:
654 MSG("%soutput: splice()", indent6
);
656 case LTTNG_EVENT_MMAP
:
657 MSG("%soutput: mmap()", indent6
);
663 * List channel(s) of session and domain.
665 * If channel_name is NULL, all channels are listed.
667 static int list_channels(const char *channel_name
)
669 int count
, i
, ret
= CMD_SUCCESS
;
670 unsigned int chan_found
= 0;
671 struct lttng_channel
*channels
= NULL
;
673 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
675 count
= lttng_list_channels(handle
, &channels
);
678 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
680 WARN("No kernel channel");
683 /* We had a real error */
685 ERR("%s", lttng_strerror(ret
));
692 MSG("Channels:\n-------------");
695 for (i
= 0; i
< count
; i
++) {
696 if (channel_name
!= NULL
) {
697 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
703 print_channel(&channels
[i
]);
705 /* Listing events per channel */
706 ret
= list_events(channels
[i
].name
);
708 ERR("%s", lttng_strerror(ret
));
716 if (!chan_found
&& channel_name
!= NULL
) {
717 ERR("Channel %s not found", channel_name
);
731 * List available tracing session. List only basic information.
733 * If session_name is NULL, all sessions are listed.
735 static int list_sessions(const char *session_name
)
738 unsigned int session_found
= 0;
739 struct lttng_session
*sessions
;
741 count
= lttng_list_sessions(&sessions
);
742 DBG("Session count %d", count
);
745 ERR("%s", lttng_strerror(ret
));
747 } else if (count
== 0) {
748 MSG("Currently no available tracing session");
752 if (session_name
== NULL
) {
753 MSG("Available tracing sessions:");
756 for (i
= 0; i
< count
; i
++) {
757 if (session_name
!= NULL
) {
758 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
760 MSG("Tracing session %s: [%s%s]", session_name
,
761 active_string(sessions
[i
].enabled
),
762 snapshot_string(sessions
[i
].snapshot_mode
));
763 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
767 MSG(" %d) %s (%s) [%s%s]", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
768 active_string(sessions
[i
].enabled
),
769 snapshot_string(sessions
[i
].snapshot_mode
));
775 if (!session_found
&& session_name
!= NULL
) {
776 ERR("Session '%s' not found", session_name
);
781 if (session_name
== NULL
) {
782 MSG("\nUse lttng list <session_name> for more details");
793 * List available domain(s) for a session.
795 static int list_domains(const char *session_name
)
797 int i
, count
, ret
= CMD_SUCCESS
;
798 struct lttng_domain
*domains
= NULL
;
800 MSG("Domains:\n-------------");
802 count
= lttng_list_domains(session_name
, &domains
);
805 ERR("%s", lttng_strerror(ret
));
807 } else if (count
== 0) {
812 for (i
= 0; i
< count
; i
++) {
813 switch (domains
[i
].type
) {
814 case LTTNG_DOMAIN_KERNEL
:
817 case LTTNG_DOMAIN_UST
:
818 MSG(" - UST global");
820 case LTTNG_DOMAIN_JUL
:
821 MSG(" - JUL (Java Util Logging)");
836 * The 'list <options>' first level command
838 int cmd_list(int argc
, const char **argv
)
840 int opt
, ret
= CMD_SUCCESS
;
841 const char *session_name
;
842 static poptContext pc
;
843 struct lttng_domain domain
;
844 struct lttng_domain
*domains
= NULL
;
846 memset(&domain
, 0, sizeof(domain
));
854 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
855 poptReadDefaultConfig(pc
, 0);
857 while ((opt
= poptGetNextOpt(pc
)) != -1) {
865 case OPT_LIST_OPTIONS
:
866 list_cmd_options(stdout
, long_options
);
875 /* Get session name (trailing argument) */
876 session_name
= poptGetArg(pc
);
877 DBG2("Session name: %s", session_name
);
880 domain
.type
= LTTNG_DOMAIN_KERNEL
;
881 } else if (opt_userspace
) {
882 DBG2("Listing userspace global domain");
883 domain
.type
= LTTNG_DOMAIN_UST
;
884 } else if (opt_jul
) {
885 DBG2("Listing JUL domain");
886 domain
.type
= LTTNG_DOMAIN_JUL
;
889 if (opt_kernel
|| opt_userspace
|| opt_jul
) {
890 handle
= lttng_create_handle(session_name
, &domain
);
891 if (handle
== NULL
) {
897 if (session_name
== NULL
) {
898 if (!opt_kernel
&& !opt_userspace
&& !opt_jul
) {
899 ret
= list_sessions(NULL
);
905 ret
= list_kernel_events();
913 ret
= list_ust_event_fields();
915 ret
= list_ust_events();
923 ret
= list_jul_events();
930 /* List session attributes */
931 ret
= list_sessions(session_name
);
938 ret
= list_domains(session_name
);
942 if (opt_kernel
|| opt_userspace
) {
943 /* Channel listing */
944 ret
= list_channels(opt_channel
);
951 /* We want all domain(s) */
952 nb_domain
= lttng_list_domains(session_name
, &domains
);
955 ERR("%s", lttng_strerror(ret
));
959 for (i
= 0; i
< nb_domain
; i
++) {
960 switch (domains
[i
].type
) {
961 case LTTNG_DOMAIN_KERNEL
:
962 MSG("=== Domain: Kernel ===\n");
964 case LTTNG_DOMAIN_UST
:
965 MSG("=== Domain: UST global ===\n");
966 MSG("Buffer type: %s\n",
967 domains
[i
].buf_type
==
968 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
970 case LTTNG_DOMAIN_JUL
:
971 MSG("=== Domain: JUL (Java Util Logging) ===\n");
974 MSG("=== Domain: Unimplemented ===\n");
978 /* Clean handle before creating a new one */
980 lttng_destroy_handle(handle
);
983 handle
= lttng_create_handle(session_name
, &domains
[i
]);
984 if (handle
== NULL
) {
989 if (domains
[i
].type
== LTTNG_DOMAIN_JUL
) {
990 ret
= list_session_jul_events();
997 ret
= list_channels(opt_channel
);
1008 lttng_destroy_handle(handle
);
1011 poptFreeContext(pc
);