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
= " ";
49 static struct lttng_handle
*handle
;
51 static struct poptOption long_options
[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
54 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
56 /* Not implemented yet */
57 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
58 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
60 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
62 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
63 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
64 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
71 static void usage(FILE *ofp
)
73 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
75 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
78 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
80 fprintf(ofp
, " -h, --help Show this help\n");
81 fprintf(ofp
, " --list-options Simple listing of options\n");
82 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
83 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
85 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
88 fprintf(ofp
, "Session Options:\n");
89 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
90 fprintf(ofp
, " -d, --domain List available domain(s)\n");
95 * Get command line from /proc for a specific pid.
97 * On success, return an allocated string pointer to the proc cmdline.
98 * On error, return NULL.
100 static char *get_cmdline_by_pid(pid_t pid
)
104 char *cmdline
= NULL
;
105 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
107 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
108 fp
= fopen(path
, "r");
113 /* Caller must free() *cmdline */
114 cmdline
= malloc(PATH_MAX
);
115 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
117 perror("fread proc list");
126 const char *active_string(int value
)
129 case 0: return " [inactive]";
130 case 1: return " [active]";
132 default: return NULL
;
137 const char *enabled_string(int value
)
140 case 0: return " [disabled]";
141 case 1: return " [enabled]";
143 default: return NULL
;
148 const char *loglevel_string_pre(int loglevel
)
150 if (loglevel
== -1) {
153 return " (loglevel: ";
158 const char *loglevel_string_post(int loglevel
)
160 if (loglevel
== -1) {
167 static const char *loglevel_string(int value
)
171 case 0: return "TRACE_EMERG";
172 case 1: return "TRACE_ALERT";
173 case 2: return "TRACE_CRIT";
174 case 3: return "TRACE_ERR";
175 case 4: return "TRACE_WARNING";
176 case 5: return "TRACE_NOTICE";
177 case 6: return "TRACE_INFO";
178 case 7: return "TRACE_SYSTEM";
179 case 8: return "TRACE_PROCESS";
180 case 9: return "TRACE_MODULE";
181 case 10: return "TRACE_UNIT";
182 case 11: return "TRACE_CLASS";
183 case 12: return "TRACE_OBJECT";
184 case 13: return "TRACE_FUNCTION";
185 case 14: return "TRACE_PRINTF";
186 case 15: return "TRACE_DEBUG";
187 default: return "<<UNKNOWN>>";
192 * Pretty print single event.
194 static void print_events(struct lttng_event
*event
)
196 switch (event
->type
) {
197 case LTTNG_EVENT_TRACEPOINT
:
199 MSG("%s%s%s%s%d%s (type: tracepoint)%s", indent6
,
201 loglevel_string_pre(event
->loglevel
),
202 loglevel_string(event
->loglevel
),
204 loglevel_string_post(event
->loglevel
),
205 enabled_string(event
->enabled
));
208 case LTTNG_EVENT_PROBE
:
209 MSG("%s%s (type: probe)%s", indent6
,
210 event
->name
, enabled_string(event
->enabled
));
211 if (event
->attr
.probe
.addr
!= 0) {
212 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
214 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
215 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
218 case LTTNG_EVENT_FUNCTION
:
219 case LTTNG_EVENT_FUNCTION_ENTRY
:
220 MSG("%s%s (type: function)%s", indent6
,
221 event
->name
, enabled_string(event
->enabled
));
222 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
224 case LTTNG_EVENT_SYSCALL
:
225 MSG("%s (type: syscall)%s", indent6
,
226 enabled_string(event
->enabled
));
228 case LTTNG_EVENT_NOOP
:
229 MSG("%s (type: noop)%s", indent6
,
230 enabled_string(event
->enabled
));
232 case LTTNG_EVENT_ALL
:
233 /* We should never have "all" events in list. */
240 * Ask session daemon for all user space tracepoints available.
242 static int list_ust_events(void)
245 struct lttng_domain domain
;
246 struct lttng_handle
*handle
;
247 struct lttng_event
*event_list
;
250 DBG("Getting UST tracing events");
252 domain
.type
= LTTNG_DOMAIN_UST
;
254 handle
= lttng_create_handle(NULL
, &domain
);
255 if (handle
== NULL
) {
259 size
= lttng_list_tracepoints(handle
, &event_list
);
261 ERR("Unable to list UST events");
265 MSG("UST events:\n-------------");
271 for (i
= 0; i
< size
; i
++) {
272 if (cur_pid
!= event_list
[i
].pid
) {
273 cur_pid
= event_list
[i
].pid
;
274 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
276 print_events(&event_list
[i
]);
290 * Ask for all trace events in the kernel and pretty print them.
292 static int list_kernel_events(void)
295 struct lttng_domain domain
;
296 struct lttng_handle
*handle
;
297 struct lttng_event
*event_list
;
299 DBG("Getting kernel tracing events");
301 domain
.type
= LTTNG_DOMAIN_KERNEL
;
303 handle
= lttng_create_handle(NULL
, &domain
);
304 if (handle
== NULL
) {
308 size
= lttng_list_tracepoints(handle
, &event_list
);
310 ERR("Unable to list kernel events");
314 MSG("Kernel events:\n-------------");
316 for (i
= 0; i
< size
; i
++) {
317 print_events(&event_list
[i
]);
331 * List events of channel of session and domain.
333 static int list_events(const char *channel_name
)
336 struct lttng_event
*events
= NULL
;
338 count
= lttng_list_events(handle
, channel_name
, &events
);
344 MSG("\n%sEvents:", indent4
);
346 MSG("%sNone\n", indent6
);
350 for (i
= 0; i
< count
; i
++) {
351 print_events(&events
[i
]);
367 * Pretty print channel
369 static void print_channel(struct lttng_channel
*channel
)
371 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
373 MSG("%sAttributes:", indent4
);
374 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
375 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
376 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
377 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
378 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
379 switch (channel
->attr
.output
) {
380 case LTTNG_EVENT_SPLICE
:
381 MSG("%soutput: splice()", indent6
);
383 case LTTNG_EVENT_MMAP
:
384 MSG("%soutput: mmap()", indent6
);
390 * List channel(s) of session and domain.
392 * If channel_name is NULL, all channels are listed.
394 static int list_channels(const char *channel_name
)
396 int count
, i
, ret
= CMD_SUCCESS
;
397 unsigned int chan_found
= 0;
398 struct lttng_channel
*channels
= NULL
;
400 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
402 count
= lttng_list_channels(handle
, &channels
);
406 } else if (count
== 0) {
407 ERR("Channel %s not found", channel_name
);
411 if (channel_name
== NULL
) {
412 MSG("Channels:\n-------------");
415 for (i
= 0; i
< count
; i
++) {
416 if (channel_name
!= NULL
) {
417 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
423 print_channel(&channels
[i
]);
425 /* Listing events per channel */
426 ret
= list_events(channels
[i
].name
);
428 MSG("%s", lttng_strerror(ret
));
436 if (!chan_found
&& channel_name
!= NULL
) {
437 ERR("Channel %s not found", channel_name
);
451 * List available tracing session. List only basic information.
453 * If session_name is NULL, all sessions are listed.
455 static int list_sessions(const char *session_name
)
458 unsigned int session_found
= 0;
459 struct lttng_session
*sessions
;
461 count
= lttng_list_sessions(&sessions
);
462 DBG("Session count %d", count
);
468 if (session_name
== NULL
) {
469 MSG("Available tracing sessions:");
472 for (i
= 0; i
< count
; i
++) {
473 if (session_name
!= NULL
) {
474 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
476 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
477 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
483 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
492 if (!session_found
&& session_name
!= NULL
) {
493 ERR("Session %s not found", session_name
);
496 if (session_name
== NULL
) {
497 MSG("\nUse lttng list <session_name> for more details");
507 * List available domain(s) for a session.
509 static int list_domains(const char *session_name
)
511 int i
, count
, ret
= CMD_SUCCESS
;
512 struct lttng_domain
*domains
= NULL
;
514 MSG("Domains:\n-------------");
516 count
= lttng_list_domains(session_name
, &domains
);
520 } else if (count
== 0) {
525 for (i
= 0; i
< count
; i
++) {
526 switch (domains
[i
].type
) {
527 case LTTNG_DOMAIN_KERNEL
:
530 case LTTNG_DOMAIN_UST
:
531 MSG(" - UST global");
546 * The 'list <options>' first level command
548 int cmd_list(int argc
, const char **argv
)
550 int opt
, i
, ret
= CMD_SUCCESS
;
552 const char *session_name
;
553 static poptContext pc
;
554 struct lttng_domain domain
;
555 struct lttng_domain
*domains
= NULL
;
563 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
564 poptReadDefaultConfig(pc
, 0);
566 while ((opt
= poptGetNextOpt(pc
)) != -1) {
574 case OPT_LIST_OPTIONS
:
575 list_cmd_options(stdout
, long_options
);
584 /* Get session name (trailing argument) */
585 session_name
= poptGetArg(pc
);
586 DBG2("Session name: %s", session_name
);
589 domain
.type
= LTTNG_DOMAIN_KERNEL
;
590 } else if (opt_userspace
) {
591 DBG2("Listing userspace global domain");
592 domain
.type
= LTTNG_DOMAIN_UST
;
595 handle
= lttng_create_handle(session_name
, &domain
);
596 if (handle
== NULL
) {
601 if (session_name
== NULL
) {
602 if (!opt_kernel
&& !opt_userspace
) {
603 ret
= list_sessions(NULL
);
610 ret
= list_kernel_events();
617 ret
= list_ust_events();
624 /* List session attributes */
625 ret
= list_sessions(session_name
);
633 ret
= list_domains(session_name
);
641 /* Channel listing */
642 ret
= list_channels(opt_channel
);
648 /* We want all domain(s) */
649 nb_domain
= lttng_list_domains(session_name
, &domains
);
655 for (i
= 0; i
< nb_domain
; i
++) {
656 switch (domains
[i
].type
) {
657 case LTTNG_DOMAIN_KERNEL
:
658 MSG("=== Domain: Kernel ===\n");
660 case LTTNG_DOMAIN_UST
:
661 MSG("=== Domain: UST global ===\n");
664 MSG("=== Domain: Unimplemented ===\n");
668 /* Clean handle before creating a new one */
669 lttng_destroy_handle(handle
);
671 handle
= lttng_create_handle(session_name
, &domains
[i
]);
672 if (handle
== NULL
) {
677 ret
= list_channels(opt_channel
);
690 lttng_destroy_handle(handle
);