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
, 0, 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 * Pretty print single event.
115 static void print_events(struct lttng_event
*event
)
117 switch (event
->type
) {
118 case LTTNG_EVENT_TRACEPOINT
:
119 MSG("%s%s (type: tracepoint) [enabled: %d]", indent6
,
120 event
->name
, event
->enabled
);
122 case LTTNG_EVENT_PROBE
:
123 MSG("%s%s (type: probe) [enabled: %d]", indent6
,
124 event
->name
, event
->enabled
);
125 if (event
->attr
.probe
.addr
!= 0) {
126 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
128 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
129 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
132 case LTTNG_EVENT_FUNCTION
:
133 case LTTNG_EVENT_FUNCTION_ENTRY
:
134 MSG("%s%s (type: function) [enabled: %d]", indent6
,
135 event
->name
, event
->enabled
);
136 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
138 case LTTNG_EVENT_SYSCALL
:
139 MSG("%s (type: syscall) [enabled: %d]", indent6
,
142 case LTTNG_EVENT_NOOP
:
143 MSG("%s (type: noop) [enabled: %d]", indent6
,
146 case LTTNG_EVENT_ALL
:
147 /* We should never have "all" events in list. */
154 * Ask session daemon for all user space tracepoints available.
156 static int list_ust_events(void)
159 struct lttng_domain domain
;
160 struct lttng_handle
*handle
;
161 struct lttng_event
*event_list
;
164 DBG("Getting UST tracing events");
166 domain
.type
= LTTNG_DOMAIN_UST
;
168 handle
= lttng_create_handle(NULL
, &domain
);
169 if (handle
== NULL
) {
173 size
= lttng_list_tracepoints(handle
, &event_list
);
175 ERR("Unable to list UST events");
179 MSG("UST events:\n-------------");
185 for (i
= 0; i
< size
; i
++) {
186 if (cur_pid
!= event_list
[i
].pid
) {
187 cur_pid
= event_list
[i
].pid
;
188 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
190 print_events(&event_list
[i
]);
204 * Ask for all trace events in the kernel and pretty print them.
206 static int list_kernel_events(void)
209 struct lttng_domain domain
;
210 struct lttng_handle
*handle
;
211 struct lttng_event
*event_list
;
213 DBG("Getting kernel tracing events");
215 domain
.type
= LTTNG_DOMAIN_KERNEL
;
217 handle
= lttng_create_handle(NULL
, &domain
);
218 if (handle
== NULL
) {
222 size
= lttng_list_tracepoints(handle
, &event_list
);
224 ERR("Unable to list kernel events");
228 MSG("Kernel events:\n-------------");
230 for (i
= 0; i
< size
; i
++) {
231 print_events(&event_list
[i
]);
245 * List events of channel of session and domain.
247 static int list_events(const char *channel_name
)
250 struct lttng_event
*events
= NULL
;
252 count
= lttng_list_events(handle
, channel_name
, &events
);
258 MSG("\n%sEvents:", indent4
);
260 MSG("%sNone", indent6
);
264 for (i
= 0; i
< count
; i
++) {
265 print_events(&events
[i
]);
281 * Pretty print channel
283 static void print_channel(struct lttng_channel
*channel
)
285 MSG("- %s (enabled: %d):\n", channel
->name
, channel
->enabled
);
287 MSG("%sAttributes:", indent4
);
288 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
289 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
290 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
291 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
292 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
293 switch (channel
->attr
.output
) {
294 case LTTNG_EVENT_SPLICE
:
295 MSG("%soutput: splice()", indent6
);
297 case LTTNG_EVENT_MMAP
:
298 MSG("%soutput: mmap()", indent6
);
304 * List channel(s) of session and domain.
306 * If channel_name is NULL, all channels are listed.
308 static int list_channels(const char *channel_name
)
310 int count
, i
, ret
= CMD_SUCCESS
;
311 unsigned int chan_found
= 0;
312 struct lttng_channel
*channels
= NULL
;
314 DBG("Listing channel(s) (%s)", channel_name
);
316 count
= lttng_list_channels(handle
, &channels
);
320 } else if (count
== 0) {
321 MSG("No channel found");
325 if (channel_name
== NULL
) {
326 MSG("Channels:\n-------------");
329 for (i
= 0; i
< count
; i
++) {
330 if (channel_name
!= NULL
) {
331 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
337 print_channel(&channels
[i
]);
339 /* Listing events per channel */
340 ret
= list_events(channels
[i
].name
);
342 MSG("%s", lttng_strerror(ret
));
350 if (!chan_found
&& channel_name
!= NULL
) {
351 MSG("Channel %s not found", channel_name
);
363 * List available tracing session. List only basic information.
365 * If session_name is NULL, all sessions are listed.
367 static int list_sessions(const char *session_name
)
370 unsigned int session_found
= 0;
371 struct lttng_session
*sessions
;
373 count
= lttng_list_sessions(&sessions
);
374 DBG("Session count %d", count
);
380 if (session_name
== NULL
) {
381 MSG("Available tracing sessions:");
384 for (i
= 0; i
< count
; i
++) {
385 if (session_name
!= NULL
) {
386 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
388 MSG("Tracing session %s:", session_name
);
389 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
395 MSG(" %d) %s (%s)", i
+ 1, sessions
[i
].name
, sessions
[i
].path
);
404 if (!session_found
&& session_name
!= NULL
) {
405 MSG("Session %s not found", session_name
);
408 if (session_name
== NULL
) {
409 MSG("\nUse lttng list <session_name> for more details");
419 * List available domain(s) for a session.
421 static int list_domains(void)
423 int i
, count
, ret
= CMD_SUCCESS
;
424 struct lttng_domain
*domains
= NULL
;
426 MSG("Domains:\n-------------");
428 count
= lttng_list_domains(handle
, &domains
);
432 } else if (count
== 0) {
437 for (i
= 0; i
< count
; i
++) {
438 switch (domains
[i
].type
) {
439 case LTTNG_DOMAIN_KERNEL
:
442 case LTTNG_DOMAIN_UST
:
443 MSG(" - UST global");
458 * The 'list <options>' first level command
460 int cmd_list(int argc
, const char **argv
)
462 int opt
, i
, ret
= CMD_SUCCESS
;
463 unsigned int nb_domain
;
464 const char *session_name
;
465 static poptContext pc
;
466 struct lttng_domain domain
;
467 struct lttng_domain
*domains
= NULL
;
474 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
475 poptReadDefaultConfig(pc
, 0);
477 while ((opt
= poptGetNextOpt(pc
)) != -1) {
484 opt_cmd_name
= poptGetOptArg(pc
);
494 MSG("*** Userspace tracing not implemented for PID ***\n");
497 /* Get session name (trailing argument) */
498 session_name
= poptGetArg(pc
);
499 DBG2("Session name: %s", session_name
);
502 domain
.type
= LTTNG_DOMAIN_KERNEL
;
503 } else if (opt_userspace
) {
504 DBG2("Listing userspace global domain");
505 domain
.type
= LTTNG_DOMAIN_UST
;
508 handle
= lttng_create_handle(session_name
, &domain
);
509 if (handle
== NULL
) {
513 if (session_name
== NULL
) {
514 if (!opt_kernel
&& !opt_userspace
) {
515 ret
= list_sessions(NULL
);
521 ret
= list_kernel_events();
527 ret
= list_ust_events();
533 /* List session attributes */
534 ret
= list_sessions(session_name
);
541 ret
= list_domains();
546 /* Channel listing */
547 ret
= list_channels(opt_channel
);
552 /* We want all domain(s) */
553 nb_domain
= lttng_list_domains(handle
, &domains
);
559 for (i
= 0; i
< nb_domain
; i
++) {
560 switch (domains
[i
].type
) {
561 case LTTNG_DOMAIN_KERNEL
:
562 MSG("=== Domain: Kernel ===\n");
564 case LTTNG_DOMAIN_UST
:
565 MSG("=== Domain: UST global ===\n");
568 MSG("=== Domain: Unimplemented ===\n");
572 /* Clean handle before creating a new one */
573 lttng_destroy_handle(handle
);
575 handle
= lttng_create_handle(session_name
, &domains
[i
]);
576 if (handle
== NULL
) {
580 ret
= list_channels(opt_channel
);
592 lttng_destroy_handle(handle
);