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 [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
75 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp
, "With -k alone, list available kernel events\n");
78 fprintf(ofp
, "With -u alone, list 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
, "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(const char *loglevel
)
150 if (loglevel
[0] == '\0') {
153 return " (loglevel: ";
158 const char *loglevel_string_post(const char *loglevel
)
160 if (loglevel
[0] == '\0') {
168 * Pretty print single event.
170 static void print_events(struct lttng_event
*event
)
172 switch (event
->type
) {
173 case LTTNG_EVENT_TRACEPOINT
:
175 char ll_value
[LTTNG_SYMBOL_NAME_LEN
] = "";
177 if (event
->loglevel
[0] != '\0') {
180 ret
= snprintf(ll_value
, LTTNG_SYMBOL_NAME_LEN
,
181 " (%lld)", (long long) event
->loglevel_value
);
183 ERR("snprintf error");
185 MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6
,
187 loglevel_string_pre(event
->loglevel
),
190 loglevel_string_post(event
->loglevel
),
191 enabled_string(event
->enabled
));
194 case LTTNG_EVENT_PROBE
:
195 MSG("%s%s (type: probe)%s", indent6
,
196 event
->name
, enabled_string(event
->enabled
));
197 if (event
->attr
.probe
.addr
!= 0) {
198 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
200 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
201 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
204 case LTTNG_EVENT_FUNCTION
:
205 case LTTNG_EVENT_FUNCTION_ENTRY
:
206 MSG("%s%s (type: function)%s", indent6
,
207 event
->name
, enabled_string(event
->enabled
));
208 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
210 case LTTNG_EVENT_SYSCALL
:
211 MSG("%s (type: syscall)%s", indent6
,
212 enabled_string(event
->enabled
));
214 case LTTNG_EVENT_NOOP
:
215 MSG("%s (type: noop)%s", indent6
,
216 enabled_string(event
->enabled
));
218 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL
:
219 MSG("%s%s (type: tracepoint loglevel)%s", indent6
,
221 enabled_string(event
->enabled
));
223 case LTTNG_EVENT_ALL
:
224 /* We should never have "all" events in list. */
231 * Ask session daemon for all user space tracepoints available.
233 static int list_ust_events(void)
236 struct lttng_domain domain
;
237 struct lttng_handle
*handle
;
238 struct lttng_event
*event_list
;
241 DBG("Getting UST tracing events");
243 domain
.type
= LTTNG_DOMAIN_UST
;
245 handle
= lttng_create_handle(NULL
, &domain
);
246 if (handle
== NULL
) {
250 size
= lttng_list_tracepoints(handle
, &event_list
);
252 ERR("Unable to list UST events");
256 MSG("UST events:\n-------------");
262 for (i
= 0; i
< size
; i
++) {
263 if (cur_pid
!= event_list
[i
].pid
) {
264 cur_pid
= event_list
[i
].pid
;
265 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
267 print_events(&event_list
[i
]);
281 * Ask for all trace events in the kernel and pretty print them.
283 static int list_kernel_events(void)
286 struct lttng_domain domain
;
287 struct lttng_handle
*handle
;
288 struct lttng_event
*event_list
;
290 DBG("Getting kernel tracing events");
292 domain
.type
= LTTNG_DOMAIN_KERNEL
;
294 handle
= lttng_create_handle(NULL
, &domain
);
295 if (handle
== NULL
) {
299 size
= lttng_list_tracepoints(handle
, &event_list
);
301 ERR("Unable to list kernel events");
305 MSG("Kernel events:\n-------------");
307 for (i
= 0; i
< size
; i
++) {
308 print_events(&event_list
[i
]);
322 * List events of channel of session and domain.
324 static int list_events(const char *channel_name
)
327 struct lttng_event
*events
= NULL
;
329 count
= lttng_list_events(handle
, channel_name
, &events
);
335 MSG("\n%sEvents:", indent4
);
337 MSG("%sNone\n", indent6
);
341 for (i
= 0; i
< count
; i
++) {
342 print_events(&events
[i
]);
358 * Pretty print channel
360 static void print_channel(struct lttng_channel
*channel
)
362 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
364 MSG("%sAttributes:", indent4
);
365 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
366 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
367 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
368 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
369 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
370 switch (channel
->attr
.output
) {
371 case LTTNG_EVENT_SPLICE
:
372 MSG("%soutput: splice()", indent6
);
374 case LTTNG_EVENT_MMAP
:
375 MSG("%soutput: mmap()", indent6
);
381 * List channel(s) of session and domain.
383 * If channel_name is NULL, all channels are listed.
385 static int list_channels(const char *channel_name
)
387 int count
, i
, ret
= CMD_SUCCESS
;
388 unsigned int chan_found
= 0;
389 struct lttng_channel
*channels
= NULL
;
391 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
393 count
= lttng_list_channels(handle
, &channels
);
397 } else if (count
== 0) {
398 MSG("No channel found");
402 if (channel_name
== NULL
) {
403 MSG("Channels:\n-------------");
406 for (i
= 0; i
< count
; i
++) {
407 if (channel_name
!= NULL
) {
408 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
414 print_channel(&channels
[i
]);
416 /* Listing events per channel */
417 ret
= list_events(channels
[i
].name
);
419 MSG("%s", lttng_strerror(ret
));
427 if (!chan_found
&& channel_name
!= NULL
) {
428 MSG("Channel %s not found", channel_name
);
440 * List available tracing session. List only basic information.
442 * If session_name is NULL, all sessions are listed.
444 static int list_sessions(const char *session_name
)
447 unsigned int session_found
= 0;
448 struct lttng_session
*sessions
;
450 count
= lttng_list_sessions(&sessions
);
451 DBG("Session count %d", count
);
457 if (session_name
== NULL
) {
458 MSG("Available tracing sessions:");
461 for (i
= 0; i
< count
; i
++) {
462 if (session_name
!= NULL
) {
463 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
465 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
466 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
472 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
481 if (!session_found
&& session_name
!= NULL
) {
482 MSG("Session %s not found", session_name
);
485 if (session_name
== NULL
) {
486 MSG("\nUse lttng list <session_name> for more details");
496 * List available domain(s) for a session.
498 static int list_domains(const char *session_name
)
500 int i
, count
, ret
= CMD_SUCCESS
;
501 struct lttng_domain
*domains
= NULL
;
503 MSG("Domains:\n-------------");
505 count
= lttng_list_domains(session_name
, &domains
);
509 } else if (count
== 0) {
514 for (i
= 0; i
< count
; i
++) {
515 switch (domains
[i
].type
) {
516 case LTTNG_DOMAIN_KERNEL
:
519 case LTTNG_DOMAIN_UST
:
520 MSG(" - UST global");
535 * The 'list <options>' first level command
537 int cmd_list(int argc
, const char **argv
)
539 int opt
, i
, ret
= CMD_SUCCESS
;
541 const char *session_name
;
542 static poptContext pc
;
543 struct lttng_domain domain
;
544 struct lttng_domain
*domains
= NULL
;
551 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
552 poptReadDefaultConfig(pc
, 0);
554 while ((opt
= poptGetNextOpt(pc
)) != -1) {
562 case OPT_LIST_OPTIONS
:
563 list_cmd_options(stdout
, long_options
);
573 /* Get session name (trailing argument) */
574 session_name
= poptGetArg(pc
);
575 DBG2("Session name: %s", session_name
);
578 domain
.type
= LTTNG_DOMAIN_KERNEL
;
579 } else if (opt_userspace
) {
580 DBG2("Listing userspace global domain");
581 domain
.type
= LTTNG_DOMAIN_UST
;
584 handle
= lttng_create_handle(session_name
, &domain
);
585 if (handle
== NULL
) {
589 if (session_name
== NULL
) {
590 if (!opt_kernel
&& !opt_userspace
) {
591 ret
= list_sessions(NULL
);
597 ret
= list_kernel_events();
603 ret
= list_ust_events();
609 /* List session attributes */
610 ret
= list_sessions(session_name
);
617 ret
= list_domains(session_name
);
622 /* Channel listing */
623 ret
= list_channels(opt_channel
);
628 /* We want all domain(s) */
629 nb_domain
= lttng_list_domains(session_name
, &domains
);
635 for (i
= 0; i
< nb_domain
; i
++) {
636 switch (domains
[i
].type
) {
637 case LTTNG_DOMAIN_KERNEL
:
638 MSG("=== Domain: Kernel ===\n");
640 case LTTNG_DOMAIN_UST
:
641 MSG("=== Domain: UST global ===\n");
644 MSG("=== Domain: Unimplemented ===\n");
648 /* Clean handle before creating a new one */
649 lttng_destroy_handle(handle
);
651 handle
= lttng_create_handle(session_name
, &domains
[i
]);
652 if (handle
== NULL
) {
656 ret
= list_channels(opt_channel
);
668 lttng_destroy_handle(handle
);