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 int opt_kernel
;
32 static char *opt_channel
;
33 static int opt_domain
;
35 const char *indent4
= " ";
36 const char *indent6
= " ";
37 const char *indent8
= " ";
43 static struct lttng_handle
*handle
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
49 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
50 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
51 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
52 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
59 static void usage(FILE *ofp
)
61 fprintf(ofp
, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
63 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
65 fprintf(ofp
, "With -k alone, list available kernel events\n");
66 fprintf(ofp
, "With -u alone, list available userspace events\n");
68 fprintf(ofp
, " -h, --help Show this help\n");
69 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
70 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
71 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
73 fprintf(ofp
, "Options:\n");
74 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
75 fprintf(ofp
, " -d, --domain List available domain(s)\n");
80 * Get command line from /proc for a specific pid.
82 * On success, return an allocated string pointer to the proc cmdline.
83 * On error, return NULL.
85 static char *get_cmdline_by_pid(pid_t pid
)
90 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
92 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
93 fp
= fopen(path
, "r");
98 /* Caller must free() *cmdline */
99 cmdline
= malloc(PATH_MAX
);
100 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
102 perror("fread proc list");
111 * Pretty print single event.
113 static void print_events(struct lttng_event
*event
)
115 switch (event
->type
) {
116 case LTTNG_EVENT_TRACEPOINT
:
117 MSG("%s%s (type: tracepoint) [enabled: %d]", indent6
,
118 event
->name
, event
->enabled
);
120 case LTTNG_EVENT_PROBE
:
121 MSG("%s%s (type: probe) [enabled: %d]", indent6
,
122 event
->name
, event
->enabled
);
123 if (event
->attr
.probe
.addr
!= 0) {
124 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
126 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
127 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
130 case LTTNG_EVENT_FUNCTION
:
131 case LTTNG_EVENT_FUNCTION_ENTRY
:
132 MSG("%s%s (type: function) [enabled: %d]", indent6
,
133 event
->name
, event
->enabled
);
134 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
136 case LTTNG_EVENT_SYSCALL
:
137 MSG("%s (type: syscall) [enabled: %d]", indent6
,
140 case LTTNG_EVENT_NOOP
:
141 MSG("%s (type: noop) [enabled: %d]", indent6
,
144 case LTTNG_EVENT_ALL
:
145 /* We should never have "all" events in list. */
152 * Ask session daemon for all user space tracepoints available.
154 static int list_ust_events(void)
157 struct lttng_domain domain
;
158 struct lttng_handle
*handle
;
159 struct lttng_event
*event_list
;
162 DBG("Getting UST tracing events");
164 domain
.type
= LTTNG_DOMAIN_UST
;
166 handle
= lttng_create_handle(NULL
, &domain
);
167 if (handle
== NULL
) {
171 size
= lttng_list_tracepoints(handle
, &event_list
);
173 ERR("Unable to list UST events");
177 MSG("UST events:\n-------------");
183 for (i
= 0; i
< size
; i
++) {
184 if (cur_pid
!= event_list
[i
].pid
) {
185 cur_pid
= event_list
[i
].pid
;
186 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
188 print_events(&event_list
[i
]);
202 * Ask for all trace events in the kernel and pretty print them.
204 static int list_kernel_events(void)
207 struct lttng_domain domain
;
208 struct lttng_handle
*handle
;
209 struct lttng_event
*event_list
;
211 DBG("Getting kernel tracing events");
213 domain
.type
= LTTNG_DOMAIN_KERNEL
;
215 handle
= lttng_create_handle(NULL
, &domain
);
216 if (handle
== NULL
) {
220 size
= lttng_list_tracepoints(handle
, &event_list
);
222 ERR("Unable to list kernel events");
226 MSG("Kernel events:\n-------------");
228 for (i
= 0; i
< size
; i
++) {
229 print_events(&event_list
[i
]);
243 * List events of channel of session and domain.
245 static int list_events(const char *channel_name
)
248 struct lttng_event
*events
= NULL
;
250 count
= lttng_list_events(handle
, channel_name
, &events
);
256 MSG("\n%sEvents:", indent4
);
258 MSG("%sNone", indent6
);
262 for (i
= 0; i
< count
; i
++) {
263 print_events(&events
[i
]);
279 * Pretty print channel
281 static void print_channel(struct lttng_channel
*channel
)
283 MSG("- %s (enabled: %d):\n", channel
->name
, channel
->enabled
);
285 MSG("%sAttributes:", indent4
);
286 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
287 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
288 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
289 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
290 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
291 switch (channel
->attr
.output
) {
292 case LTTNG_EVENT_SPLICE
:
293 MSG("%soutput: splice()", indent6
);
295 case LTTNG_EVENT_MMAP
:
296 MSG("%soutput: mmap()", indent6
);
302 * List channel(s) of session and domain.
304 * If channel_name is NULL, all channels are listed.
306 static int list_channels(const char *channel_name
)
308 int count
, i
, ret
= CMD_SUCCESS
;
309 unsigned int chan_found
= 0;
310 struct lttng_channel
*channels
= NULL
;
312 DBG("Listing channel(s) (%s)", channel_name
);
314 count
= lttng_list_channels(handle
, &channels
);
318 } else if (count
== 0) {
319 MSG("No channel found");
323 if (channel_name
== NULL
) {
324 MSG("Channels:\n-------------");
327 for (i
= 0; i
< count
; i
++) {
328 if (channel_name
!= NULL
) {
329 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
335 print_channel(&channels
[i
]);
337 /* Listing events per channel */
338 ret
= list_events(channels
[i
].name
);
340 MSG("%s", lttng_strerror(ret
));
348 if (!chan_found
&& channel_name
!= NULL
) {
349 MSG("Channel %s not found", channel_name
);
361 * List available tracing session. List only basic information.
363 * If session_name is NULL, all sessions are listed.
365 static int list_sessions(const char *session_name
)
368 unsigned int session_found
= 0;
369 struct lttng_session
*sessions
;
371 count
= lttng_list_sessions(&sessions
);
372 DBG("Session count %d", count
);
378 if (session_name
== NULL
) {
379 MSG("Available tracing sessions:");
382 for (i
= 0; i
< count
; i
++) {
383 if (session_name
!= NULL
) {
384 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
386 MSG("Tracing session %s:", session_name
);
387 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
393 MSG(" %d) %s (%s)", i
+ 1, sessions
[i
].name
, sessions
[i
].path
);
402 if (!session_found
&& session_name
!= NULL
) {
403 MSG("Session %s not found", session_name
);
406 if (session_name
== NULL
) {
407 MSG("\nUse lttng list <session_name> for more details");
417 * List available domain(s) for a session.
419 static int list_domains(void)
421 int i
, count
, ret
= CMD_SUCCESS
;
422 struct lttng_domain
*domains
= NULL
;
424 MSG("Domains:\n-------------");
426 count
= lttng_list_domains(handle
, &domains
);
430 } else if (count
== 0) {
435 for (i
= 0; i
< count
; i
++) {
436 switch (domains
[i
].type
) {
437 case LTTNG_DOMAIN_KERNEL
:
440 case LTTNG_DOMAIN_UST
:
441 MSG(" - UST global");
456 * The 'list <options>' first level command
458 int cmd_list(int argc
, const char **argv
)
460 int opt
, i
, ret
= CMD_SUCCESS
;
461 unsigned int nb_domain
;
462 const char *session_name
;
463 static poptContext pc
;
464 struct lttng_domain domain
;
465 struct lttng_domain
*domains
= NULL
;
472 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
473 poptReadDefaultConfig(pc
, 0);
475 while ((opt
= poptGetNextOpt(pc
)) != -1) {
488 MSG("*** Userspace tracing not implemented for PID ***\n");
491 /* Get session name (trailing argument) */
492 session_name
= poptGetArg(pc
);
493 DBG2("Session name: %s", session_name
);
496 domain
.type
= LTTNG_DOMAIN_KERNEL
;
497 } else if (opt_userspace
) {
498 DBG2("Listing userspace global domain");
499 domain
.type
= LTTNG_DOMAIN_UST
;
502 handle
= lttng_create_handle(session_name
, &domain
);
503 if (handle
== NULL
) {
507 if (session_name
== NULL
) {
508 if (!opt_kernel
&& !opt_userspace
) {
509 ret
= list_sessions(NULL
);
515 ret
= list_kernel_events();
521 ret
= list_ust_events();
527 /* List session attributes */
528 ret
= list_sessions(session_name
);
535 ret
= list_domains();
540 /* Channel listing */
541 ret
= list_channels(opt_channel
);
546 /* We want all domain(s) */
547 nb_domain
= lttng_list_domains(handle
, &domains
);
553 for (i
= 0; i
< nb_domain
; i
++) {
554 switch (domains
[i
].type
) {
555 case LTTNG_DOMAIN_KERNEL
:
556 MSG("=== Domain: Kernel ===\n");
558 case LTTNG_DOMAIN_UST
:
559 MSG("=== Domain: UST global ===\n");
562 MSG("=== Domain: Unimplemented ===\n");
566 /* Clean handle before creating a new one */
567 lttng_destroy_handle(handle
);
569 handle
= lttng_create_handle(session_name
, &domains
[i
]);
570 if (handle
== NULL
) {
574 ret
= list_channels(opt_channel
);
586 lttng_destroy_handle(handle
);