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 [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");
186 MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6
,
188 loglevel_string_pre(event
->loglevel
),
191 loglevel_string_post(event
->loglevel
),
192 enabled_string(event
->enabled
));
195 case LTTNG_EVENT_PROBE
:
196 MSG("%s%s (type: probe)%s", indent6
,
197 event
->name
, enabled_string(event
->enabled
));
198 if (event
->attr
.probe
.addr
!= 0) {
199 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
201 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
202 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
205 case LTTNG_EVENT_FUNCTION
:
206 case LTTNG_EVENT_FUNCTION_ENTRY
:
207 MSG("%s%s (type: function)%s", indent6
,
208 event
->name
, enabled_string(event
->enabled
));
209 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
211 case LTTNG_EVENT_SYSCALL
:
212 MSG("%s (type: syscall)%s", indent6
,
213 enabled_string(event
->enabled
));
215 case LTTNG_EVENT_NOOP
:
216 MSG("%s (type: noop)%s", indent6
,
217 enabled_string(event
->enabled
));
219 case LTTNG_EVENT_ALL
:
220 /* We should never have "all" events in list. */
227 * Ask session daemon for all user space tracepoints available.
229 static int list_ust_events(void)
232 struct lttng_domain domain
;
233 struct lttng_handle
*handle
;
234 struct lttng_event
*event_list
;
237 DBG("Getting UST tracing events");
239 domain
.type
= LTTNG_DOMAIN_UST
;
241 handle
= lttng_create_handle(NULL
, &domain
);
242 if (handle
== NULL
) {
246 size
= lttng_list_tracepoints(handle
, &event_list
);
248 ERR("Unable to list UST events");
252 MSG("UST events:\n-------------");
258 for (i
= 0; i
< size
; i
++) {
259 if (cur_pid
!= event_list
[i
].pid
) {
260 cur_pid
= event_list
[i
].pid
;
261 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
263 print_events(&event_list
[i
]);
277 * Ask for all trace events in the kernel and pretty print them.
279 static int list_kernel_events(void)
282 struct lttng_domain domain
;
283 struct lttng_handle
*handle
;
284 struct lttng_event
*event_list
;
286 DBG("Getting kernel tracing events");
288 domain
.type
= LTTNG_DOMAIN_KERNEL
;
290 handle
= lttng_create_handle(NULL
, &domain
);
291 if (handle
== NULL
) {
295 size
= lttng_list_tracepoints(handle
, &event_list
);
297 ERR("Unable to list kernel events");
301 MSG("Kernel events:\n-------------");
303 for (i
= 0; i
< size
; i
++) {
304 print_events(&event_list
[i
]);
318 * List events of channel of session and domain.
320 static int list_events(const char *channel_name
)
323 struct lttng_event
*events
= NULL
;
325 count
= lttng_list_events(handle
, channel_name
, &events
);
331 MSG("\n%sEvents:", indent4
);
333 MSG("%sNone\n", indent6
);
337 for (i
= 0; i
< count
; i
++) {
338 print_events(&events
[i
]);
354 * Pretty print channel
356 static void print_channel(struct lttng_channel
*channel
)
358 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
360 MSG("%sAttributes:", indent4
);
361 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
362 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
363 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
364 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
365 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
366 switch (channel
->attr
.output
) {
367 case LTTNG_EVENT_SPLICE
:
368 MSG("%soutput: splice()", indent6
);
370 case LTTNG_EVENT_MMAP
:
371 MSG("%soutput: mmap()", indent6
);
377 * List channel(s) of session and domain.
379 * If channel_name is NULL, all channels are listed.
381 static int list_channels(const char *channel_name
)
383 int count
, i
, ret
= CMD_SUCCESS
;
384 unsigned int chan_found
= 0;
385 struct lttng_channel
*channels
= NULL
;
387 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
389 count
= lttng_list_channels(handle
, &channels
);
393 } else if (count
== 0) {
394 ERR("Channel %s not found", channel_name
);
398 if (channel_name
== NULL
) {
399 MSG("Channels:\n-------------");
402 for (i
= 0; i
< count
; i
++) {
403 if (channel_name
!= NULL
) {
404 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
410 print_channel(&channels
[i
]);
412 /* Listing events per channel */
413 ret
= list_events(channels
[i
].name
);
415 MSG("%s", lttng_strerror(ret
));
423 if (!chan_found
&& channel_name
!= NULL
) {
424 ERR("Channel %s not found", channel_name
);
438 * List available tracing session. List only basic information.
440 * If session_name is NULL, all sessions are listed.
442 static int list_sessions(const char *session_name
)
445 unsigned int session_found
= 0;
446 struct lttng_session
*sessions
;
448 count
= lttng_list_sessions(&sessions
);
449 DBG("Session count %d", count
);
455 if (session_name
== NULL
) {
456 MSG("Available tracing sessions:");
459 for (i
= 0; i
< count
; i
++) {
460 if (session_name
!= NULL
) {
461 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
463 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
464 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
470 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
479 if (!session_found
&& session_name
!= NULL
) {
480 ERR("Session %s not found", session_name
);
483 if (session_name
== NULL
) {
484 MSG("\nUse lttng list <session_name> for more details");
494 * List available domain(s) for a session.
496 static int list_domains(const char *session_name
)
498 int i
, count
, ret
= CMD_SUCCESS
;
499 struct lttng_domain
*domains
= NULL
;
501 MSG("Domains:\n-------------");
503 count
= lttng_list_domains(session_name
, &domains
);
507 } else if (count
== 0) {
512 for (i
= 0; i
< count
; i
++) {
513 switch (domains
[i
].type
) {
514 case LTTNG_DOMAIN_KERNEL
:
517 case LTTNG_DOMAIN_UST
:
518 MSG(" - UST global");
533 * The 'list <options>' first level command
535 int cmd_list(int argc
, const char **argv
)
537 int opt
, i
, ret
= CMD_SUCCESS
;
539 const char *session_name
;
540 static poptContext pc
;
541 struct lttng_domain domain
;
542 struct lttng_domain
*domains
= NULL
;
550 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
551 poptReadDefaultConfig(pc
, 0);
553 while ((opt
= poptGetNextOpt(pc
)) != -1) {
561 case OPT_LIST_OPTIONS
:
562 list_cmd_options(stdout
, long_options
);
571 /* Get session name (trailing argument) */
572 session_name
= poptGetArg(pc
);
573 DBG2("Session name: %s", session_name
);
576 domain
.type
= LTTNG_DOMAIN_KERNEL
;
577 } else if (opt_userspace
) {
578 DBG2("Listing userspace global domain");
579 domain
.type
= LTTNG_DOMAIN_UST
;
582 handle
= lttng_create_handle(session_name
, &domain
);
583 if (handle
== NULL
) {
588 if (session_name
== NULL
) {
589 if (!opt_kernel
&& !opt_userspace
) {
590 ret
= list_sessions(NULL
);
597 ret
= list_kernel_events();
604 ret
= list_ust_events();
611 /* List session attributes */
612 ret
= list_sessions(session_name
);
620 ret
= list_domains(session_name
);
628 /* Channel listing */
629 ret
= list_channels(opt_channel
);
635 /* We want all domain(s) */
636 nb_domain
= lttng_list_domains(session_name
, &domains
);
642 for (i
= 0; i
< nb_domain
; i
++) {
643 switch (domains
[i
].type
) {
644 case LTTNG_DOMAIN_KERNEL
:
645 MSG("=== Domain: Kernel ===\n");
647 case LTTNG_DOMAIN_UST
:
648 MSG("=== Domain: UST global ===\n");
651 MSG("=== Domain: Unimplemented ===\n");
655 /* Clean handle before creating a new one */
656 lttng_destroy_handle(handle
);
658 handle
= lttng_create_handle(session_name
, &domains
[i
]);
659 if (handle
== NULL
) {
664 ret
= list_channels(opt_channel
);
677 lttng_destroy_handle(handle
);