2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "../command.h"
29 static int opt_userspace
;
30 static int opt_kernel
;
31 static char *opt_channel
;
32 static int opt_domain
;
34 /* Not implemented yet */
35 static char *opt_cmd_name
;
39 const char *indent4
= " ";
40 const char *indent6
= " ";
41 const char *indent8
= " ";
48 static struct lttng_handle
*handle
;
50 static struct poptOption long_options
[] = {
51 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
52 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
53 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
55 /* Not implemented yet */
56 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
57 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
59 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
61 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
62 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
69 static void usage(FILE *ofp
)
71 fprintf(ofp
, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
73 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
75 fprintf(ofp
, "With -k alone, list available kernel events\n");
76 fprintf(ofp
, "With -u alone, list available userspace events\n");
78 fprintf(ofp
, " -h, --help Show this help\n");
79 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
80 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
82 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
85 fprintf(ofp
, "Options:\n");
86 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
87 fprintf(ofp
, " -d, --domain List available domain(s)\n");
92 * Get command line from /proc for a specific pid.
94 * On success, return an allocated string pointer to the proc cmdline.
95 * On error, return NULL.
97 static char *get_cmdline_by_pid(pid_t pid
)
101 char *cmdline
= NULL
;
102 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
104 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
105 fp
= fopen(path
, "r");
110 /* Caller must free() *cmdline */
111 cmdline
= malloc(PATH_MAX
);
112 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
114 perror("fread proc list");
123 const char *active_string(int value
)
126 case 0: return " [inactive]";
127 case 1: return " [active]";
129 default: return NULL
;
134 const char *enabled_string(int value
)
137 case 0: return " [disabled]";
138 case 1: return " [enabled]";
140 default: return NULL
;
145 const char *loglevel_string_pre(const char *loglevel
)
147 if (loglevel
[0] == '\0') {
150 return " (loglevel: ";
155 const char *loglevel_string_post(const char *loglevel
)
157 if (loglevel
[0] == '\0') {
165 * Pretty print single event.
167 static void print_events(struct lttng_event
*event
)
169 switch (event
->type
) {
170 case LTTNG_EVENT_TRACEPOINT
:
172 char ll_value
[LTTNG_SYMBOL_NAME_LEN
] = "";
174 if (event
->loglevel
[0] != '\0') {
177 ret
= snprintf(ll_value
, LTTNG_SYMBOL_NAME_LEN
,
178 " (%lld)", (long long) event
->loglevel_value
);
180 ERR("snprintf error");
182 MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6
,
184 loglevel_string_pre(event
->loglevel
),
187 loglevel_string_post(event
->loglevel
),
188 enabled_string(event
->enabled
));
191 case LTTNG_EVENT_PROBE
:
192 MSG("%s%s (type: probe)%s", indent6
,
193 event
->name
, enabled_string(event
->enabled
));
194 if (event
->attr
.probe
.addr
!= 0) {
195 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
197 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
198 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
201 case LTTNG_EVENT_FUNCTION
:
202 case LTTNG_EVENT_FUNCTION_ENTRY
:
203 MSG("%s%s (type: function)%s", indent6
,
204 event
->name
, enabled_string(event
->enabled
));
205 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
207 case LTTNG_EVENT_SYSCALL
:
208 MSG("%s (type: syscall)%s", indent6
,
209 enabled_string(event
->enabled
));
211 case LTTNG_EVENT_NOOP
:
212 MSG("%s (type: noop)%s", indent6
,
213 enabled_string(event
->enabled
));
215 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL
:
216 MSG("%s%s (type: tracepoint loglevel)%s", indent6
,
218 enabled_string(event
->enabled
));
220 case LTTNG_EVENT_ALL
:
221 /* We should never have "all" events in list. */
228 * Ask session daemon for all user space tracepoints available.
230 static int list_ust_events(void)
233 struct lttng_domain domain
;
234 struct lttng_handle
*handle
;
235 struct lttng_event
*event_list
;
238 DBG("Getting UST tracing events");
240 domain
.type
= LTTNG_DOMAIN_UST
;
242 handle
= lttng_create_handle(NULL
, &domain
);
243 if (handle
== NULL
) {
247 size
= lttng_list_tracepoints(handle
, &event_list
);
249 ERR("Unable to list UST events");
253 MSG("UST events:\n-------------");
259 for (i
= 0; i
< size
; i
++) {
260 if (cur_pid
!= event_list
[i
].pid
) {
261 cur_pid
= event_list
[i
].pid
;
262 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
264 print_events(&event_list
[i
]);
278 * Ask for all trace events in the kernel and pretty print them.
280 static int list_kernel_events(void)
283 struct lttng_domain domain
;
284 struct lttng_handle
*handle
;
285 struct lttng_event
*event_list
;
287 DBG("Getting kernel tracing events");
289 domain
.type
= LTTNG_DOMAIN_KERNEL
;
291 handle
= lttng_create_handle(NULL
, &domain
);
292 if (handle
== NULL
) {
296 size
= lttng_list_tracepoints(handle
, &event_list
);
298 ERR("Unable to list kernel events");
302 MSG("Kernel events:\n-------------");
304 for (i
= 0; i
< size
; i
++) {
305 print_events(&event_list
[i
]);
319 * List events of channel of session and domain.
321 static int list_events(const char *channel_name
)
324 struct lttng_event
*events
= NULL
;
326 count
= lttng_list_events(handle
, channel_name
, &events
);
332 MSG("\n%sEvents:", indent4
);
334 MSG("%sNone\n", indent6
);
338 for (i
= 0; i
< count
; i
++) {
339 print_events(&events
[i
]);
355 * Pretty print channel
357 static void print_channel(struct lttng_channel
*channel
)
359 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
361 MSG("%sAttributes:", indent4
);
362 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
363 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
364 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
365 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
366 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
367 switch (channel
->attr
.output
) {
368 case LTTNG_EVENT_SPLICE
:
369 MSG("%soutput: splice()", indent6
);
371 case LTTNG_EVENT_MMAP
:
372 MSG("%soutput: mmap()", indent6
);
378 * List channel(s) of session and domain.
380 * If channel_name is NULL, all channels are listed.
382 static int list_channels(const char *channel_name
)
384 int count
, i
, ret
= CMD_SUCCESS
;
385 unsigned int chan_found
= 0;
386 struct lttng_channel
*channels
= NULL
;
388 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
390 count
= lttng_list_channels(handle
, &channels
);
394 } else if (count
== 0) {
395 MSG("No channel found");
399 if (channel_name
== NULL
) {
400 MSG("Channels:\n-------------");
403 for (i
= 0; i
< count
; i
++) {
404 if (channel_name
!= NULL
) {
405 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
411 print_channel(&channels
[i
]);
413 /* Listing events per channel */
414 ret
= list_events(channels
[i
].name
);
416 MSG("%s", lttng_strerror(ret
));
424 if (!chan_found
&& channel_name
!= NULL
) {
425 MSG("Channel %s not found", channel_name
);
437 * List available tracing session. List only basic information.
439 * If session_name is NULL, all sessions are listed.
441 static int list_sessions(const char *session_name
)
444 unsigned int session_found
= 0;
445 struct lttng_session
*sessions
;
447 count
= lttng_list_sessions(&sessions
);
448 DBG("Session count %d", count
);
454 if (session_name
== NULL
) {
455 MSG("Available tracing sessions:");
458 for (i
= 0; i
< count
; i
++) {
459 if (session_name
!= NULL
) {
460 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
462 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
463 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
469 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
478 if (!session_found
&& session_name
!= NULL
) {
479 MSG("Session %s not found", session_name
);
482 if (session_name
== NULL
) {
483 MSG("\nUse lttng list <session_name> for more details");
493 * List available domain(s) for a session.
495 static int list_domains(const char *session_name
)
497 int i
, count
, ret
= CMD_SUCCESS
;
498 struct lttng_domain
*domains
= NULL
;
500 MSG("Domains:\n-------------");
502 count
= lttng_list_domains(session_name
, &domains
);
506 } else if (count
== 0) {
511 for (i
= 0; i
< count
; i
++) {
512 switch (domains
[i
].type
) {
513 case LTTNG_DOMAIN_KERNEL
:
516 case LTTNG_DOMAIN_UST
:
517 MSG(" - UST global");
532 * The 'list <options>' first level command
534 int cmd_list(int argc
, const char **argv
)
536 int opt
, i
, ret
= CMD_SUCCESS
;
538 const char *session_name
;
539 static poptContext pc
;
540 struct lttng_domain domain
;
541 struct lttng_domain
*domains
= NULL
;
548 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
549 poptReadDefaultConfig(pc
, 0);
551 while ((opt
= poptGetNextOpt(pc
)) != -1) {
566 /* Get session name (trailing argument) */
567 session_name
= poptGetArg(pc
);
568 DBG2("Session name: %s", session_name
);
571 domain
.type
= LTTNG_DOMAIN_KERNEL
;
572 } else if (opt_userspace
) {
573 DBG2("Listing userspace global domain");
574 domain
.type
= LTTNG_DOMAIN_UST
;
577 handle
= lttng_create_handle(session_name
, &domain
);
578 if (handle
== NULL
) {
582 if (session_name
== NULL
) {
583 if (!opt_kernel
&& !opt_userspace
) {
584 ret
= list_sessions(NULL
);
590 ret
= list_kernel_events();
596 ret
= list_ust_events();
602 /* List session attributes */
603 ret
= list_sessions(session_name
);
610 ret
= list_domains(session_name
);
615 /* Channel listing */
616 ret
= list_channels(opt_channel
);
621 /* We want all domain(s) */
622 nb_domain
= lttng_list_domains(session_name
, &domains
);
628 for (i
= 0; i
< nb_domain
; i
++) {
629 switch (domains
[i
].type
) {
630 case LTTNG_DOMAIN_KERNEL
:
631 MSG("=== Domain: Kernel ===\n");
633 case LTTNG_DOMAIN_UST
:
634 MSG("=== Domain: UST global ===\n");
637 MSG("=== Domain: Unimplemented ===\n");
641 /* Clean handle before creating a new one */
642 lttng_destroy_handle(handle
);
644 handle
= lttng_create_handle(session_name
, &domains
[i
]);
645 if (handle
== NULL
) {
649 ret
= list_channels(opt_channel
);
661 lttng_destroy_handle(handle
);