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 static int opt_userspace
;
29 static int opt_kernel
;
30 static char *opt_channel
;
31 static int opt_domain
;
33 /* Not implemented yet */
34 static char *opt_cmd_name
;
38 const char *indent4
= " ";
39 const char *indent6
= " ";
40 const char *indent8
= " ";
48 static struct lttng_handle
*handle
;
50 static struct poptOption long_options
[] = {
51 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
52 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
53 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
55 /* Not implemented yet */
56 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
57 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
59 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
61 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
62 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
63 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
70 static void usage(FILE *ofp
)
72 fprintf(ofp
, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
74 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
76 fprintf(ofp
, "Without a session, -k lists available kernel events\n");
77 fprintf(ofp
, "Without a session, -u lists available userspace events\n");
79 fprintf(ofp
, " -h, --help Show this help\n");
80 fprintf(ofp
, " --list-options Simple listing of options\n");
81 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
82 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
84 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
87 fprintf(ofp
, "Session Options:\n");
88 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
89 fprintf(ofp
, " -d, --domain List available domain(s)\n");
94 * Get command line from /proc for a specific pid.
96 * On success, return an allocated string pointer to the proc cmdline.
97 * On error, return NULL.
99 static char *get_cmdline_by_pid(pid_t pid
)
103 char *cmdline
= NULL
;
104 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
106 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
107 fp
= fopen(path
, "r");
112 /* Caller must free() *cmdline */
113 cmdline
= malloc(PATH_MAX
);
114 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
116 perror("fread proc list");
125 const char *active_string(int value
)
128 case 0: return " [inactive]";
129 case 1: return " [active]";
131 default: return NULL
;
136 const char *enabled_string(int value
)
139 case 0: return " [disabled]";
140 case 1: return " [enabled]";
142 default: return NULL
;
146 static const char *loglevel_string(int value
)
151 case LTTNG_LOGLEVEL_EMERG
:
152 return "TRACE_EMERG";
153 case LTTNG_LOGLEVEL_ALERT
:
154 return "TRACE_ALERT";
155 case LTTNG_LOGLEVEL_CRIT
:
157 case LTTNG_LOGLEVEL_ERR
:
159 case LTTNG_LOGLEVEL_WARNING
:
160 return "TRACE_WARNING";
161 case LTTNG_LOGLEVEL_NOTICE
:
162 return "TRACE_NOTICE";
163 case LTTNG_LOGLEVEL_INFO
:
165 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
166 return "TRACE_DEBUG_SYSTEM";
167 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
168 return "TRACE_DEBUG_PROGRAM";
169 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
170 return "TRACE_DEBUG_PROCESS";
171 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
172 return "TRACE_DEBUG_MODULE";
173 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
174 return "TRACE_DEBUG_UNIT";
175 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
176 return "TRACE_DEBUG_FUNCTION";
177 case LTTNG_LOGLEVEL_DEBUG_LINE
:
178 return "TRACE_DEBUG_LINE";
179 case LTTNG_LOGLEVEL_DEBUG
:
180 return "TRACE_DEBUG";
182 return "<<UNKNOWN>>";
187 * Pretty print single event.
189 static void print_events(struct lttng_event
*event
)
191 switch (event
->type
) {
192 case LTTNG_EVENT_TRACEPOINT
:
194 if (event
->loglevel
!= -1) {
195 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
198 loglevel_string(event
->loglevel
),
200 enabled_string(event
->enabled
));
202 MSG("%s%s (type: tracepoint)%s",
205 enabled_string(event
->enabled
));
209 case LTTNG_EVENT_PROBE
:
210 MSG("%s%s (type: probe)%s", indent6
,
211 event
->name
, enabled_string(event
->enabled
));
212 if (event
->attr
.probe
.addr
!= 0) {
213 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
215 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
216 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
219 case LTTNG_EVENT_FUNCTION
:
220 case LTTNG_EVENT_FUNCTION_ENTRY
:
221 MSG("%s%s (type: function)%s", indent6
,
222 event
->name
, enabled_string(event
->enabled
));
223 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
225 case LTTNG_EVENT_SYSCALL
:
226 MSG("%ssyscalls (type: syscall)%s", indent6
,
227 enabled_string(event
->enabled
));
229 case LTTNG_EVENT_NOOP
:
230 MSG("%s (type: noop)%s", indent6
,
231 enabled_string(event
->enabled
));
233 case LTTNG_EVENT_ALL
:
234 /* We should never have "all" events in list. */
241 * Ask session daemon for all user space tracepoints available.
243 static int list_ust_events(void)
246 struct lttng_domain domain
;
247 struct lttng_handle
*handle
;
248 struct lttng_event
*event_list
;
251 memset(&domain
, 0, sizeof(domain
));
253 DBG("Getting UST tracing events");
255 domain
.type
= LTTNG_DOMAIN_UST
;
257 handle
= lttng_create_handle(NULL
, &domain
);
258 if (handle
== NULL
) {
262 size
= lttng_list_tracepoints(handle
, &event_list
);
264 ERR("Unable to list UST events");
265 lttng_destroy_handle(handle
);
269 MSG("UST events:\n-------------");
275 for (i
= 0; i
< size
; i
++) {
276 if (cur_pid
!= event_list
[i
].pid
) {
277 cur_pid
= event_list
[i
].pid
;
278 MSG("\nPID: %d - Name: %s", cur_pid
, get_cmdline_by_pid(cur_pid
));
280 print_events(&event_list
[i
]);
286 lttng_destroy_handle(handle
);
291 lttng_destroy_handle(handle
);
296 * Ask for all trace events in the kernel and pretty print them.
298 static int list_kernel_events(void)
301 struct lttng_domain domain
;
302 struct lttng_handle
*handle
;
303 struct lttng_event
*event_list
;
305 memset(&domain
, 0, sizeof(domain
));
307 DBG("Getting kernel tracing events");
309 domain
.type
= LTTNG_DOMAIN_KERNEL
;
311 handle
= lttng_create_handle(NULL
, &domain
);
312 if (handle
== NULL
) {
316 size
= lttng_list_tracepoints(handle
, &event_list
);
318 ERR("Unable to list kernel events");
319 lttng_destroy_handle(handle
);
323 MSG("Kernel events:\n-------------");
325 for (i
= 0; i
< size
; i
++) {
326 print_events(&event_list
[i
]);
333 lttng_destroy_handle(handle
);
337 lttng_destroy_handle(handle
);
342 * List events of channel of session and domain.
344 static int list_events(const char *channel_name
)
347 struct lttng_event
*events
= NULL
;
349 count
= lttng_list_events(handle
, channel_name
, &events
);
355 MSG("\n%sEvents:", indent4
);
357 MSG("%sNone\n", indent6
);
361 for (i
= 0; i
< count
; i
++) {
362 print_events(&events
[i
]);
378 * Pretty print channel
380 static void print_channel(struct lttng_channel
*channel
)
382 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
384 MSG("%sAttributes:", indent4
);
385 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
386 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
387 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
388 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
389 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
390 switch (channel
->attr
.output
) {
391 case LTTNG_EVENT_SPLICE
:
392 MSG("%soutput: splice()", indent6
);
394 case LTTNG_EVENT_MMAP
:
395 MSG("%soutput: mmap()", indent6
);
401 * List channel(s) of session and domain.
403 * If channel_name is NULL, all channels are listed.
405 static int list_channels(const char *channel_name
)
407 int count
, i
, ret
= CMD_SUCCESS
;
408 unsigned int chan_found
= 0;
409 struct lttng_channel
*channels
= NULL
;
411 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
413 count
= lttng_list_channels(handle
, &channels
);
417 } else if (count
== 0) {
418 ERR("Channel %s not found", channel_name
);
422 if (channel_name
== NULL
) {
423 MSG("Channels:\n-------------");
426 for (i
= 0; i
< count
; i
++) {
427 if (channel_name
!= NULL
) {
428 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
434 print_channel(&channels
[i
]);
436 /* Listing events per channel */
437 ret
= list_events(channels
[i
].name
);
439 MSG("%s", lttng_strerror(ret
));
447 if (!chan_found
&& channel_name
!= NULL
) {
448 ERR("Channel %s not found", channel_name
);
462 * List available tracing session. List only basic information.
464 * If session_name is NULL, all sessions are listed.
466 static int list_sessions(const char *session_name
)
469 unsigned int session_found
= 0;
470 struct lttng_session
*sessions
;
472 count
= lttng_list_sessions(&sessions
);
473 DBG("Session count %d", count
);
477 } else if (count
== 0) {
478 MSG("Currently no available tracing session");
482 if (session_name
== NULL
) {
483 MSG("Available tracing sessions:");
486 for (i
= 0; i
< count
; i
++) {
487 if (session_name
!= NULL
) {
488 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
490 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
491 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
497 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
498 active_string(sessions
[i
].enabled
));
507 if (!session_found
&& session_name
!= NULL
) {
508 ERR("Session '%s' not found", session_name
);
513 if (session_name
== NULL
) {
514 MSG("\nUse lttng list <session_name> for more details");
525 * List available domain(s) for a session.
527 static int list_domains(const char *session_name
)
529 int i
, count
, ret
= CMD_SUCCESS
;
530 struct lttng_domain
*domains
= NULL
;
532 MSG("Domains:\n-------------");
534 count
= lttng_list_domains(session_name
, &domains
);
538 } else if (count
== 0) {
543 for (i
= 0; i
< count
; i
++) {
544 switch (domains
[i
].type
) {
545 case LTTNG_DOMAIN_KERNEL
:
548 case LTTNG_DOMAIN_UST
:
549 MSG(" - UST global");
564 * The 'list <options>' first level command
566 int cmd_list(int argc
, const char **argv
)
568 int opt
, i
, ret
= CMD_SUCCESS
;
570 const char *session_name
;
571 static poptContext pc
;
572 struct lttng_domain domain
;
573 struct lttng_domain
*domains
= NULL
;
575 memset(&domain
, 0, sizeof(domain
));
583 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
584 poptReadDefaultConfig(pc
, 0);
586 while ((opt
= poptGetNextOpt(pc
)) != -1) {
594 case OPT_LIST_OPTIONS
:
595 list_cmd_options(stdout
, long_options
);
604 /* Get session name (trailing argument) */
605 session_name
= poptGetArg(pc
);
606 DBG2("Session name: %s", session_name
);
609 domain
.type
= LTTNG_DOMAIN_KERNEL
;
610 } else if (opt_userspace
) {
611 DBG2("Listing userspace global domain");
612 domain
.type
= LTTNG_DOMAIN_UST
;
615 if (opt_kernel
|| opt_userspace
) {
616 handle
= lttng_create_handle(session_name
, &domain
);
617 if (handle
== NULL
) {
623 if (session_name
== NULL
) {
624 if (!opt_kernel
&& !opt_userspace
) {
625 ret
= list_sessions(NULL
);
631 ret
= list_kernel_events();
637 ret
= list_ust_events();
643 /* List session attributes */
644 ret
= list_sessions(session_name
);
651 ret
= list_domains(session_name
);
656 /* Channel listing */
657 ret
= list_channels(opt_channel
);
662 /* We want all domain(s) */
663 nb_domain
= lttng_list_domains(session_name
, &domains
);
669 for (i
= 0; i
< nb_domain
; i
++) {
670 switch (domains
[i
].type
) {
671 case LTTNG_DOMAIN_KERNEL
:
672 MSG("=== Domain: Kernel ===\n");
674 case LTTNG_DOMAIN_UST
:
675 MSG("=== Domain: UST global ===\n");
678 MSG("=== Domain: Unimplemented ===\n");
682 /* Clean handle before creating a new one */
684 lttng_destroy_handle(handle
);
687 handle
= lttng_create_handle(session_name
, &domains
[i
]);
688 if (handle
== NULL
) {
693 ret
= list_channels(opt_channel
);
706 lttng_destroy_handle(handle
);