2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "../command.h"
28 #include <common/sessiond-comm/sessiond-comm.h>
30 static int opt_userspace
;
31 static int opt_kernel
;
32 static char *opt_channel
;
33 static int opt_domain
;
35 /* Not implemented yet */
36 static char *opt_cmd_name
;
40 const char *indent4
= " ";
41 const char *indent6
= " ";
42 const char *indent8
= " ";
50 static struct lttng_handle
*handle
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
57 /* Not implemented yet */
58 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
59 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
61 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
63 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
64 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
65 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
72 static void usage(FILE *ofp
)
74 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
76 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
78 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
79 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
81 fprintf(ofp
, " -h, --help Show this help\n");
82 fprintf(ofp
, " --list-options Simple listing of options\n");
83 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
84 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
86 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
89 fprintf(ofp
, "Session Options:\n");
90 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
91 fprintf(ofp
, " -d, --domain List available domain(s)\n");
96 * Get command line from /proc for a specific pid.
98 * On success, return an allocated string pointer to the proc cmdline.
99 * On error, return NULL.
101 static char *get_cmdline_by_pid(pid_t pid
)
105 char *cmdline
= NULL
;
106 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
108 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
109 fp
= fopen(path
, "r");
114 /* Caller must free() *cmdline */
115 cmdline
= malloc(PATH_MAX
);
116 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
118 perror("fread proc list");
127 const char *active_string(int value
)
130 case 0: return " [inactive]";
131 case 1: return " [active]";
133 default: return NULL
;
138 const char *enabled_string(int value
)
141 case 0: return " [disabled]";
142 case 1: return " [enabled]";
144 default: return NULL
;
148 static const char *loglevel_string(int value
)
153 case LTTNG_LOGLEVEL_EMERG
:
154 return "TRACE_EMERG";
155 case LTTNG_LOGLEVEL_ALERT
:
156 return "TRACE_ALERT";
157 case LTTNG_LOGLEVEL_CRIT
:
159 case LTTNG_LOGLEVEL_ERR
:
161 case LTTNG_LOGLEVEL_WARNING
:
162 return "TRACE_WARNING";
163 case LTTNG_LOGLEVEL_NOTICE
:
164 return "TRACE_NOTICE";
165 case LTTNG_LOGLEVEL_INFO
:
167 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
168 return "TRACE_DEBUG_SYSTEM";
169 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
170 return "TRACE_DEBUG_PROGRAM";
171 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
172 return "TRACE_DEBUG_PROCESS";
173 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
174 return "TRACE_DEBUG_MODULE";
175 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
176 return "TRACE_DEBUG_UNIT";
177 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
178 return "TRACE_DEBUG_FUNCTION";
179 case LTTNG_LOGLEVEL_DEBUG_LINE
:
180 return "TRACE_DEBUG_LINE";
181 case LTTNG_LOGLEVEL_DEBUG
:
182 return "TRACE_DEBUG";
184 return "<<UNKNOWN>>";
189 * Pretty print single event.
191 static void print_events(struct lttng_event
*event
)
193 switch (event
->type
) {
194 case LTTNG_EVENT_TRACEPOINT
:
196 if (event
->loglevel
!= -1) {
197 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
200 loglevel_string(event
->loglevel
),
202 enabled_string(event
->enabled
));
204 MSG("%s%s (type: tracepoint)%s",
207 enabled_string(event
->enabled
));
211 case LTTNG_EVENT_PROBE
:
212 MSG("%s%s (type: probe)%s", indent6
,
213 event
->name
, enabled_string(event
->enabled
));
214 if (event
->attr
.probe
.addr
!= 0) {
215 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
217 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
218 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
221 case LTTNG_EVENT_FUNCTION
:
222 case LTTNG_EVENT_FUNCTION_ENTRY
:
223 MSG("%s%s (type: function)%s", indent6
,
224 event
->name
, enabled_string(event
->enabled
));
225 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
227 case LTTNG_EVENT_SYSCALL
:
228 MSG("%ssyscalls (type: syscall)%s", indent6
,
229 enabled_string(event
->enabled
));
231 case LTTNG_EVENT_NOOP
:
232 MSG("%s (type: noop)%s", indent6
,
233 enabled_string(event
->enabled
));
235 case LTTNG_EVENT_ALL
:
236 /* We should never have "all" events in list. */
243 * Ask session daemon for all user space tracepoints available.
245 static int list_ust_events(void)
248 struct lttng_domain domain
;
249 struct lttng_handle
*handle
;
250 struct lttng_event
*event_list
;
253 memset(&domain
, 0, sizeof(domain
));
255 DBG("Getting UST tracing events");
257 domain
.type
= LTTNG_DOMAIN_UST
;
259 handle
= lttng_create_handle(NULL
, &domain
);
260 if (handle
== NULL
) {
264 size
= lttng_list_tracepoints(handle
, &event_list
);
266 ERR("Unable to list UST events");
267 lttng_destroy_handle(handle
);
271 MSG("UST events:\n-------------");
277 for (i
= 0; i
< size
; i
++) {
278 if (cur_pid
!= event_list
[i
].pid
) {
279 cur_pid
= event_list
[i
].pid
;
280 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
282 print_events(&event_list
[i
]);
288 lttng_destroy_handle(handle
);
293 lttng_destroy_handle(handle
);
298 * Ask for all trace events in the kernel and pretty print them.
300 static int list_kernel_events(void)
303 struct lttng_domain domain
;
304 struct lttng_handle
*handle
;
305 struct lttng_event
*event_list
;
307 memset(&domain
, 0, sizeof(domain
));
309 DBG("Getting kernel tracing events");
311 domain
.type
= LTTNG_DOMAIN_KERNEL
;
313 handle
= lttng_create_handle(NULL
, &domain
);
314 if (handle
== NULL
) {
318 size
= lttng_list_tracepoints(handle
, &event_list
);
320 ERR("Unable to list kernel events");
321 lttng_destroy_handle(handle
);
325 MSG("Kernel events:\n-------------");
327 for (i
= 0; i
< size
; i
++) {
328 print_events(&event_list
[i
]);
335 lttng_destroy_handle(handle
);
339 lttng_destroy_handle(handle
);
344 * List events of channel of session and domain.
346 static int list_events(const char *channel_name
)
349 struct lttng_event
*events
= NULL
;
351 count
= lttng_list_events(handle
, channel_name
, &events
);
357 MSG("\n%sEvents:", indent4
);
359 MSG("%sNone\n", indent6
);
363 for (i
= 0; i
< count
; i
++) {
364 print_events(&events
[i
]);
380 * Pretty print channel
382 static void print_channel(struct lttng_channel
*channel
)
384 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
386 MSG("%sAttributes:", indent4
);
387 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
388 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
389 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
390 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
391 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
392 switch (channel
->attr
.output
) {
393 case LTTNG_EVENT_SPLICE
:
394 MSG("%soutput: splice()", indent6
);
396 case LTTNG_EVENT_MMAP
:
397 MSG("%soutput: mmap()", indent6
);
403 * List channel(s) of session and domain.
405 * If channel_name is NULL, all channels are listed.
407 static int list_channels(const char *channel_name
)
409 int count
, i
, ret
= CMD_SUCCESS
;
410 unsigned int chan_found
= 0;
411 struct lttng_channel
*channels
= NULL
;
413 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
415 count
= lttng_list_channels(handle
, &channels
);
418 case LTTCOMM_KERN_CHAN_NOT_FOUND
:
420 WARN("No kernel channel");
422 case LTTCOMM_UST_CHAN_NOT_FOUND
:
424 WARN("No UST channel");
427 /* We had a real error */
429 ERR("%s", lttng_strerror(ret
));
434 if (channel_name
== NULL
) {
435 MSG("Channels:\n-------------");
438 for (i
= 0; i
< count
; i
++) {
439 if (channel_name
!= NULL
) {
440 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
446 print_channel(&channels
[i
]);
448 /* Listing events per channel */
449 ret
= list_events(channels
[i
].name
);
451 MSG("%s", lttng_strerror(ret
));
459 if (!chan_found
&& channel_name
!= NULL
) {
460 ERR("Channel %s not found", channel_name
);
474 * List available tracing session. List only basic information.
476 * If session_name is NULL, all sessions are listed.
478 static int list_sessions(const char *session_name
)
481 unsigned int session_found
= 0;
482 struct lttng_session
*sessions
;
484 count
= lttng_list_sessions(&sessions
);
485 DBG("Session count %d", count
);
489 } else if (count
== 0) {
490 MSG("Currently no available tracing session");
494 if (session_name
== NULL
) {
495 MSG("Available tracing sessions:");
498 for (i
= 0; i
< count
; i
++) {
499 if (session_name
!= NULL
) {
500 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
502 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
503 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
509 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
510 active_string(sessions
[i
].enabled
));
519 if (!session_found
&& session_name
!= NULL
) {
520 ERR("Session '%s' not found", session_name
);
525 if (session_name
== NULL
) {
526 MSG("\nUse lttng list <session_name> for more details");
537 * List available domain(s) for a session.
539 static int list_domains(const char *session_name
)
541 int i
, count
, ret
= CMD_SUCCESS
;
542 struct lttng_domain
*domains
= NULL
;
544 MSG("Domains:\n-------------");
546 count
= lttng_list_domains(session_name
, &domains
);
550 } else if (count
== 0) {
555 for (i
= 0; i
< count
; i
++) {
556 switch (domains
[i
].type
) {
557 case LTTNG_DOMAIN_KERNEL
:
560 case LTTNG_DOMAIN_UST
:
561 MSG(" - UST global");
576 * The 'list <options>' first level command
578 int cmd_list(int argc
, const char **argv
)
580 int opt
, i
, ret
= CMD_SUCCESS
;
582 const char *session_name
;
583 static poptContext pc
;
584 struct lttng_domain domain
;
585 struct lttng_domain
*domains
= NULL
;
587 memset(&domain
, 0, sizeof(domain
));
595 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
596 poptReadDefaultConfig(pc
, 0);
598 while ((opt
= poptGetNextOpt(pc
)) != -1) {
606 case OPT_LIST_OPTIONS
:
607 list_cmd_options(stdout
, long_options
);
616 /* Get session name (trailing argument) */
617 session_name
= poptGetArg(pc
);
618 DBG2("Session name: %s", session_name
);
621 domain
.type
= LTTNG_DOMAIN_KERNEL
;
622 } else if (opt_userspace
) {
623 DBG2("Listing userspace global domain");
624 domain
.type
= LTTNG_DOMAIN_UST
;
627 if (opt_kernel
|| opt_userspace
) {
628 handle
= lttng_create_handle(session_name
, &domain
);
629 if (handle
== NULL
) {
635 if (session_name
== NULL
) {
636 if (!opt_kernel
&& !opt_userspace
) {
637 ret
= list_sessions(NULL
);
643 ret
= list_kernel_events();
649 ret
= list_ust_events();
655 /* List session attributes */
656 ret
= list_sessions(session_name
);
663 ret
= list_domains(session_name
);
668 /* Channel listing */
669 ret
= list_channels(opt_channel
);
674 /* We want all domain(s) */
675 nb_domain
= lttng_list_domains(session_name
, &domains
);
681 for (i
= 0; i
< nb_domain
; i
++) {
682 switch (domains
[i
].type
) {
683 case LTTNG_DOMAIN_KERNEL
:
684 MSG("=== Domain: Kernel ===\n");
686 case LTTNG_DOMAIN_UST
:
687 MSG("=== Domain: UST global ===\n");
690 MSG("=== Domain: Unimplemented ===\n");
694 /* Clean handle before creating a new one */
696 lttng_destroy_handle(handle
);
699 handle
= lttng_create_handle(session_name
, &domains
[i
]);
700 if (handle
== NULL
) {
705 ret
= list_channels(opt_channel
);
718 lttng_destroy_handle(handle
);