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
, &opt_cmd_name
, 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 const char *active_string(int value
)
116 case 0: return " [inactive]";
117 case 1: return " [active]";
119 default: return NULL
;
124 const char *enabled_string(int value
)
127 case 0: return " [disabled]";
128 case 1: return " [enabled]";
130 default: return NULL
;
135 const char *loglevel_string_pre(const char *loglevel
)
137 if (loglevel
[0] == '\0') {
140 return " (loglevel: ";
145 const char *loglevel_string_post(const char *loglevel
)
147 if (loglevel
[0] == '\0') {
155 * Pretty print single event.
157 static void print_events(struct lttng_event
*event
)
159 switch (event
->type
) {
160 case LTTNG_EVENT_TRACEPOINT
:
162 char ll_value
[LTTNG_SYMBOL_NAME_LEN
] = "";
164 if (event
->loglevel
[0] != '\0') {
167 ret
= snprintf(ll_value
, LTTNG_SYMBOL_NAME_LEN
,
168 " (%lld)", (long long) event
->loglevel_value
);
170 ERR("snprintf error");
172 MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6
,
174 loglevel_string_pre(event
->loglevel
),
177 loglevel_string_post(event
->loglevel
),
178 enabled_string(event
->enabled
));
181 case LTTNG_EVENT_PROBE
:
182 MSG("%s%s (type: probe)%s", indent6
,
183 event
->name
, enabled_string(event
->enabled
));
184 if (event
->attr
.probe
.addr
!= 0) {
185 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
187 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
188 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
191 case LTTNG_EVENT_FUNCTION
:
192 case LTTNG_EVENT_FUNCTION_ENTRY
:
193 MSG("%s%s (type: function)%s", indent6
,
194 event
->name
, enabled_string(event
->enabled
));
195 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
197 case LTTNG_EVENT_SYSCALL
:
198 MSG("%s (type: syscall)%s", indent6
,
199 enabled_string(event
->enabled
));
201 case LTTNG_EVENT_NOOP
:
202 MSG("%s (type: noop)%s", indent6
,
203 enabled_string(event
->enabled
));
205 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL
:
206 MSG("%s%s (type: tracepoint loglevel)%s", indent6
,
208 enabled_string(event
->enabled
));
210 case LTTNG_EVENT_ALL
:
211 /* We should never have "all" events in list. */
218 * Ask session daemon for all user space tracepoints available.
220 static int list_ust_events(void)
223 struct lttng_domain domain
;
224 struct lttng_handle
*handle
;
225 struct lttng_event
*event_list
;
228 DBG("Getting UST tracing events");
230 domain
.type
= LTTNG_DOMAIN_UST
;
232 handle
= lttng_create_handle(NULL
, &domain
);
233 if (handle
== NULL
) {
237 size
= lttng_list_tracepoints(handle
, &event_list
);
239 ERR("Unable to list UST events");
243 MSG("UST events:\n-------------");
249 for (i
= 0; i
< size
; i
++) {
250 if (cur_pid
!= event_list
[i
].pid
) {
251 cur_pid
= event_list
[i
].pid
;
252 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
254 print_events(&event_list
[i
]);
268 * Ask for all trace events in the kernel and pretty print them.
270 static int list_kernel_events(void)
273 struct lttng_domain domain
;
274 struct lttng_handle
*handle
;
275 struct lttng_event
*event_list
;
277 DBG("Getting kernel tracing events");
279 domain
.type
= LTTNG_DOMAIN_KERNEL
;
281 handle
= lttng_create_handle(NULL
, &domain
);
282 if (handle
== NULL
) {
286 size
= lttng_list_tracepoints(handle
, &event_list
);
288 ERR("Unable to list kernel events");
292 MSG("Kernel events:\n-------------");
294 for (i
= 0; i
< size
; i
++) {
295 print_events(&event_list
[i
]);
309 * List events of channel of session and domain.
311 static int list_events(const char *channel_name
)
314 struct lttng_event
*events
= NULL
;
316 count
= lttng_list_events(handle
, channel_name
, &events
);
322 MSG("\n%sEvents:", indent4
);
324 MSG("%sNone\n", indent6
);
328 for (i
= 0; i
< count
; i
++) {
329 print_events(&events
[i
]);
345 * Pretty print channel
347 static void print_channel(struct lttng_channel
*channel
)
349 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
351 MSG("%sAttributes:", indent4
);
352 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
353 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
354 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
355 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
356 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
357 switch (channel
->attr
.output
) {
358 case LTTNG_EVENT_SPLICE
:
359 MSG("%soutput: splice()", indent6
);
361 case LTTNG_EVENT_MMAP
:
362 MSG("%soutput: mmap()", indent6
);
368 * List channel(s) of session and domain.
370 * If channel_name is NULL, all channels are listed.
372 static int list_channels(const char *channel_name
)
374 int count
, i
, ret
= CMD_SUCCESS
;
375 unsigned int chan_found
= 0;
376 struct lttng_channel
*channels
= NULL
;
378 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
380 count
= lttng_list_channels(handle
, &channels
);
384 } else if (count
== 0) {
385 MSG("No channel found");
389 if (channel_name
== NULL
) {
390 MSG("Channels:\n-------------");
393 for (i
= 0; i
< count
; i
++) {
394 if (channel_name
!= NULL
) {
395 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
401 print_channel(&channels
[i
]);
403 /* Listing events per channel */
404 ret
= list_events(channels
[i
].name
);
406 MSG("%s", lttng_strerror(ret
));
414 if (!chan_found
&& channel_name
!= NULL
) {
415 MSG("Channel %s not found", channel_name
);
427 * List available tracing session. List only basic information.
429 * If session_name is NULL, all sessions are listed.
431 static int list_sessions(const char *session_name
)
434 unsigned int session_found
= 0;
435 struct lttng_session
*sessions
;
437 count
= lttng_list_sessions(&sessions
);
438 DBG("Session count %d", count
);
444 if (session_name
== NULL
) {
445 MSG("Available tracing sessions:");
448 for (i
= 0; i
< count
; i
++) {
449 if (session_name
!= NULL
) {
450 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
452 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
453 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
459 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
, active_string(sessions
[i
].enabled
));
468 if (!session_found
&& session_name
!= NULL
) {
469 MSG("Session %s not found", session_name
);
472 if (session_name
== NULL
) {
473 MSG("\nUse lttng list <session_name> for more details");
483 * List available domain(s) for a session.
485 static int list_domains(void)
487 int i
, count
, ret
= CMD_SUCCESS
;
488 struct lttng_domain
*domains
= NULL
;
490 MSG("Domains:\n-------------");
492 count
= lttng_list_domains(handle
, &domains
);
496 } else if (count
== 0) {
501 for (i
= 0; i
< count
; i
++) {
502 switch (domains
[i
].type
) {
503 case LTTNG_DOMAIN_KERNEL
:
506 case LTTNG_DOMAIN_UST
:
507 MSG(" - UST global");
522 * The 'list <options>' first level command
524 int cmd_list(int argc
, const char **argv
)
526 int opt
, i
, ret
= CMD_SUCCESS
;
528 const char *session_name
;
529 static poptContext pc
;
530 struct lttng_domain domain
;
531 struct lttng_domain
*domains
= NULL
;
538 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
539 poptReadDefaultConfig(pc
, 0);
541 while ((opt
= poptGetNextOpt(pc
)) != -1) {
557 MSG("*** Userspace tracing not implemented for PID ***\n");
560 /* Get session name (trailing argument) */
561 session_name
= poptGetArg(pc
);
562 DBG2("Session name: %s", session_name
);
565 domain
.type
= LTTNG_DOMAIN_KERNEL
;
566 } else if (opt_userspace
) {
567 DBG2("Listing userspace global domain");
568 domain
.type
= LTTNG_DOMAIN_UST
;
571 handle
= lttng_create_handle(session_name
, &domain
);
572 if (handle
== NULL
) {
576 if (session_name
== NULL
) {
577 if (!opt_kernel
&& !opt_userspace
) {
578 ret
= list_sessions(NULL
);
584 ret
= list_kernel_events();
590 ret
= list_ust_events();
596 /* List session attributes */
597 ret
= list_sessions(session_name
);
604 ret
= list_domains();
609 /* Channel listing */
610 ret
= list_channels(opt_channel
);
615 /* We want all domain(s) */
616 nb_domain
= lttng_list_domains(handle
, &domains
);
622 for (i
= 0; i
< nb_domain
; i
++) {
623 switch (domains
[i
].type
) {
624 case LTTNG_DOMAIN_KERNEL
:
625 MSG("=== Domain: Kernel ===\n");
627 case LTTNG_DOMAIN_UST
:
628 MSG("=== Domain: UST global ===\n");
631 MSG("=== Domain: Unimplemented ===\n");
635 /* Clean handle before creating a new one */
636 lttng_destroy_handle(handle
);
638 handle
= lttng_create_handle(session_name
, &domains
[i
]);
639 if (handle
== NULL
) {
643 ret
= list_channels(opt_channel
);
655 lttng_destroy_handle(handle
);