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.
30 static int opt_userspace
;
31 static char *opt_cmd_name
;
32 static int opt_kernel
;
33 static char *opt_channel
;
34 static int opt_domain
;
36 const char *indent4
= " ";
37 const char *indent6
= " ";
38 const char *indent8
= " ";
45 static struct lttng_handle
*handle
;
47 static struct poptOption long_options
[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
50 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
51 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
52 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
53 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
54 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
61 static void usage(FILE *ofp
)
63 fprintf(ofp
, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
65 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
67 fprintf(ofp
, "With -k alone, list available kernel events\n");
68 fprintf(ofp
, "With -u alone, list available userspace events\n");
70 fprintf(ofp
, " -h, --help Show this help\n");
71 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
72 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
73 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
75 fprintf(ofp
, "Options:\n");
76 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
77 fprintf(ofp
, " -d, --domain List available domain(s)\n");
82 * Get command line from /proc for a specific pid.
84 * On success, return an allocated string pointer to the proc cmdline.
85 * On error, return NULL.
87 static char *get_cmdline_by_pid(pid_t pid
)
92 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
94 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
95 fp
= fopen(path
, "r");
100 /* Caller must free() *cmdline */
101 cmdline
= malloc(PATH_MAX
);
102 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
104 perror("fread proc list");
113 const char *active_string(int value
)
116 case 0: return " [inactive]";
117 case 1: return " [active]";
119 default: return NULL
;
124 const char *enabled_string(int value
)
127 case 0: return " [disabled]";
128 case 1: return " [enabled]";
130 default: return NULL
;
135 * Pretty print single event.
137 static void print_events(struct lttng_event
*event
)
139 switch (event
->type
) {
140 case LTTNG_EVENT_TRACEPOINT
:
141 MSG("%s%s (type: tracepoint)%s", indent6
,
142 event
->name
, enabled_string(event
->enabled
));
144 case LTTNG_EVENT_PROBE
:
145 MSG("%s%s (type: probe)%s", indent6
,
146 event
->name
, enabled_string(event
->enabled
));
147 if (event
->attr
.probe
.addr
!= 0) {
148 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
150 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
151 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
154 case LTTNG_EVENT_FUNCTION
:
155 case LTTNG_EVENT_FUNCTION_ENTRY
:
156 MSG("%s%s (type: function)%s", indent6
,
157 event
->name
, enabled_string(event
->enabled
));
158 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
160 case LTTNG_EVENT_SYSCALL
:
161 MSG("%s (type: syscall)%s", indent6
,
162 enabled_string(event
->enabled
));
164 case LTTNG_EVENT_NOOP
:
165 MSG("%s (type: noop)%s", indent6
,
166 enabled_string(event
->enabled
));
168 case LTTNG_EVENT_ALL
:
169 /* We should never have "all" events in list. */
176 * Ask session daemon for all user space tracepoints available.
178 static int list_ust_events(void)
181 struct lttng_domain domain
;
182 struct lttng_handle
*handle
;
183 struct lttng_event
*event_list
;
186 DBG("Getting UST tracing events");
188 domain
.type
= LTTNG_DOMAIN_UST
;
190 handle
= lttng_create_handle(NULL
, &domain
);
191 if (handle
== NULL
) {
195 size
= lttng_list_tracepoints(handle
, &event_list
);
197 ERR("Unable to list UST events");
201 MSG("UST events:\n-------------");
207 for (i
= 0; i
< size
; i
++) {
208 if (cur_pid
!= event_list
[i
].pid
) {
209 cur_pid
= event_list
[i
].pid
;
210 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
212 print_events(&event_list
[i
]);
226 * Ask for all trace events in the kernel and pretty print them.
228 static int list_kernel_events(void)
231 struct lttng_domain domain
;
232 struct lttng_handle
*handle
;
233 struct lttng_event
*event_list
;
235 DBG("Getting kernel tracing events");
237 domain
.type
= LTTNG_DOMAIN_KERNEL
;
239 handle
= lttng_create_handle(NULL
, &domain
);
240 if (handle
== NULL
) {
244 size
= lttng_list_tracepoints(handle
, &event_list
);
246 ERR("Unable to list kernel events");
250 MSG("Kernel events:\n-------------");
252 for (i
= 0; i
< size
; i
++) {
253 print_events(&event_list
[i
]);
267 * List events of channel of session and domain.
269 static int list_events(const char *channel_name
)
272 struct lttng_event
*events
= NULL
;
274 count
= lttng_list_events(handle
, channel_name
, &events
);
280 MSG("\n%sEvents:", indent4
);
282 MSG("%sNone\n", indent6
);
286 for (i
= 0; i
< count
; i
++) {
287 print_events(&events
[i
]);
303 * Pretty print channel
305 static void print_channel(struct lttng_channel
*channel
)
307 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
309 MSG("%sAttributes:", indent4
);
310 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
311 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
312 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
313 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
314 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
315 switch (channel
->attr
.output
) {
316 case LTTNG_EVENT_SPLICE
:
317 MSG("%soutput: splice()", indent6
);
319 case LTTNG_EVENT_MMAP
:
320 MSG("%soutput: mmap()", indent6
);
326 * List channel(s) of session and domain.
328 * If channel_name is NULL, all channels are listed.
330 static int list_channels(const char *channel_name
)
332 int count
, i
, ret
= CMD_SUCCESS
;
333 unsigned int chan_found
= 0;
334 struct lttng_channel
*channels
= NULL
;
336 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
338 count
= lttng_list_channels(handle
, &channels
);
342 } else if (count
== 0) {
343 MSG("No channel found");
347 if (channel_name
== NULL
) {
348 MSG("Channels:\n-------------");
351 for (i
= 0; i
< count
; i
++) {
352 if (channel_name
!= NULL
) {
353 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
359 print_channel(&channels
[i
]);
361 /* Listing events per channel */
362 ret
= list_events(channels
[i
].name
);
364 MSG("%s", lttng_strerror(ret
));
372 if (!chan_found
&& channel_name
!= NULL
) {
373 MSG("Channel %s not found", channel_name
);
385 * List available tracing session. List only basic information.
387 * If session_name is NULL, all sessions are listed.
389 static int list_sessions(const char *session_name
)
392 unsigned int session_found
= 0;
393 struct lttng_session
*sessions
;
395 count
= lttng_list_sessions(&sessions
);
396 DBG("Session count %d", count
);
402 if (session_name
== NULL
) {
403 MSG("Available tracing sessions:");
406 for (i
= 0; i
< count
; i
++) {
407 if (session_name
!= NULL
) {
408 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
410 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
411 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
417 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
426 if (!session_found
&& session_name
!= NULL
) {
427 MSG("Session %s not found", session_name
);
430 if (session_name
== NULL
) {
431 MSG("\nUse lttng list <session_name> for more details");
441 * List available domain(s) for a session.
443 static int list_domains(void)
445 int i
, count
, ret
= CMD_SUCCESS
;
446 struct lttng_domain
*domains
= NULL
;
448 MSG("Domains:\n-------------");
450 count
= lttng_list_domains(handle
, &domains
);
454 } else if (count
== 0) {
459 for (i
= 0; i
< count
; i
++) {
460 switch (domains
[i
].type
) {
461 case LTTNG_DOMAIN_KERNEL
:
464 case LTTNG_DOMAIN_UST
:
465 MSG(" - UST global");
480 * The 'list <options>' first level command
482 int cmd_list(int argc
, const char **argv
)
484 int opt
, i
, ret
= CMD_SUCCESS
;
486 const char *session_name
;
487 static poptContext pc
;
488 struct lttng_domain domain
;
489 struct lttng_domain
*domains
= NULL
;
496 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
497 poptReadDefaultConfig(pc
, 0);
499 while ((opt
= poptGetNextOpt(pc
)) != -1) {
515 MSG("*** Userspace tracing not implemented for PID ***\n");
518 /* Get session name (trailing argument) */
519 session_name
= poptGetArg(pc
);
520 DBG2("Session name: %s", session_name
);
523 domain
.type
= LTTNG_DOMAIN_KERNEL
;
524 } else if (opt_userspace
) {
525 DBG2("Listing userspace global domain");
526 domain
.type
= LTTNG_DOMAIN_UST
;
529 handle
= lttng_create_handle(session_name
, &domain
);
530 if (handle
== NULL
) {
534 if (session_name
== NULL
) {
535 if (!opt_kernel
&& !opt_userspace
) {
536 ret
= list_sessions(NULL
);
542 ret
= list_kernel_events();
548 ret
= list_ust_events();
554 /* List session attributes */
555 ret
= list_sessions(session_name
);
562 ret
= list_domains();
567 /* Channel listing */
568 ret
= list_channels(opt_channel
);
573 /* We want all domain(s) */
574 nb_domain
= lttng_list_domains(handle
, &domains
);
580 for (i
= 0; i
< nb_domain
; i
++) {
581 switch (domains
[i
].type
) {
582 case LTTNG_DOMAIN_KERNEL
:
583 MSG("=== Domain: Kernel ===\n");
585 case LTTNG_DOMAIN_UST
:
586 MSG("=== Domain: UST global ===\n");
589 MSG("=== Domain: Unimplemented ===\n");
593 /* Clean handle before creating a new one */
594 lttng_destroy_handle(handle
);
596 handle
= lttng_create_handle(session_name
, &domains
[i
]);
597 if (handle
== NULL
) {
601 ret
= list_channels(opt_channel
);
613 lttng_destroy_handle(handle
);