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
;
32 static int opt_fields
;
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 {"fields", 'f', POPT_ARG_VAL
, &opt_fields
, 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 [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");
85 fprintf(ofp
, " -f, --fields List event fields.\n");
87 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
90 fprintf(ofp
, "Session Options:\n");
91 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
92 fprintf(ofp
, " -d, --domain List available domain(s)\n");
97 * Get command line from /proc for a specific pid.
99 * On success, return an allocated string pointer to the proc cmdline.
100 * On error, return NULL.
102 static char *get_cmdline_by_pid(pid_t pid
)
106 char *cmdline
= NULL
;
107 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
109 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
110 fp
= fopen(path
, "r");
115 /* Caller must free() *cmdline */
116 cmdline
= malloc(PATH_MAX
);
117 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
119 perror("fread proc list");
128 const char *active_string(int value
)
131 case 0: return "inactive";
132 case 1: return "active";
134 default: return NULL
;
138 static const char *snapshot_string(int value
)
149 const char *enabled_string(int value
)
152 case 0: return " [disabled]";
153 case 1: return " [enabled]";
155 default: return NULL
;
160 const char *filter_string(int value
)
163 case 1: return " [with filter]";
168 static const char *loglevel_string(int value
)
173 case LTTNG_LOGLEVEL_EMERG
:
174 return "TRACE_EMERG";
175 case LTTNG_LOGLEVEL_ALERT
:
176 return "TRACE_ALERT";
177 case LTTNG_LOGLEVEL_CRIT
:
179 case LTTNG_LOGLEVEL_ERR
:
181 case LTTNG_LOGLEVEL_WARNING
:
182 return "TRACE_WARNING";
183 case LTTNG_LOGLEVEL_NOTICE
:
184 return "TRACE_NOTICE";
185 case LTTNG_LOGLEVEL_INFO
:
187 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
188 return "TRACE_DEBUG_SYSTEM";
189 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
190 return "TRACE_DEBUG_PROGRAM";
191 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
192 return "TRACE_DEBUG_PROCESS";
193 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
194 return "TRACE_DEBUG_MODULE";
195 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
196 return "TRACE_DEBUG_UNIT";
197 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
198 return "TRACE_DEBUG_FUNCTION";
199 case LTTNG_LOGLEVEL_DEBUG_LINE
:
200 return "TRACE_DEBUG_LINE";
201 case LTTNG_LOGLEVEL_DEBUG
:
202 return "TRACE_DEBUG";
204 return "<<UNKNOWN>>";
209 * Pretty print single event.
211 static void print_events(struct lttng_event
*event
)
213 switch (event
->type
) {
214 case LTTNG_EVENT_TRACEPOINT
:
216 if (event
->loglevel
!= -1) {
217 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
220 loglevel_string(event
->loglevel
),
222 enabled_string(event
->enabled
),
223 filter_string(event
->filter
));
225 MSG("%s%s (type: tracepoint)%s%s",
228 enabled_string(event
->enabled
),
229 filter_string(event
->filter
));
233 case LTTNG_EVENT_FUNCTION
:
234 MSG("%s%s (type: function)%s%s", indent6
,
235 event
->name
, enabled_string(event
->enabled
),
236 filter_string(event
->filter
));
237 if (event
->attr
.probe
.addr
!= 0) {
238 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
240 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
241 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
244 case LTTNG_EVENT_PROBE
:
245 MSG("%s%s (type: probe)%s%s", indent6
,
246 event
->name
, enabled_string(event
->enabled
),
247 filter_string(event
->filter
));
248 if (event
->attr
.probe
.addr
!= 0) {
249 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
251 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
252 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
255 case LTTNG_EVENT_FUNCTION_ENTRY
:
256 MSG("%s%s (type: function)%s%s", indent6
,
257 event
->name
, enabled_string(event
->enabled
),
258 filter_string(event
->filter
));
259 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
261 case LTTNG_EVENT_SYSCALL
:
262 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
263 enabled_string(event
->enabled
),
264 filter_string(event
->filter
));
266 case LTTNG_EVENT_NOOP
:
267 MSG("%s (type: noop)%s%s", indent6
,
268 enabled_string(event
->enabled
),
269 filter_string(event
->filter
));
271 case LTTNG_EVENT_ALL
:
272 /* We should never have "all" events in list. */
278 static const char *field_type(struct lttng_event_field
*field
)
280 switch(field
->type
) {
281 case LTTNG_EVENT_FIELD_INTEGER
:
283 case LTTNG_EVENT_FIELD_ENUM
:
285 case LTTNG_EVENT_FIELD_FLOAT
:
287 case LTTNG_EVENT_FIELD_STRING
:
289 case LTTNG_EVENT_FIELD_OTHER
:
290 default: /* fall-through */
296 * Pretty print single event fields.
298 static void print_event_field(struct lttng_event_field
*field
)
300 if (!field
->field_name
[0]) {
303 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
304 field_type(field
), field
->nowrite
? " [no write]" : "");
308 * Ask session daemon for all user space tracepoints available.
310 static int list_ust_events(void)
313 struct lttng_domain domain
;
314 struct lttng_handle
*handle
;
315 struct lttng_event
*event_list
;
317 char *cmdline
= NULL
;
319 memset(&domain
, 0, sizeof(domain
));
321 DBG("Getting UST tracing events");
323 domain
.type
= LTTNG_DOMAIN_UST
;
325 handle
= lttng_create_handle(NULL
, &domain
);
326 if (handle
== NULL
) {
330 size
= lttng_list_tracepoints(handle
, &event_list
);
332 ERR("Unable to list UST events: %s", lttng_strerror(size
));
333 lttng_destroy_handle(handle
);
337 MSG("UST events:\n-------------");
343 for (i
= 0; i
< size
; i
++) {
344 if (cur_pid
!= event_list
[i
].pid
) {
345 cur_pid
= event_list
[i
].pid
;
346 cmdline
= get_cmdline_by_pid(cur_pid
);
347 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
350 print_events(&event_list
[i
]);
356 lttng_destroy_handle(handle
);
361 lttng_destroy_handle(handle
);
366 * Ask session daemon for all user space tracepoint fields available.
368 static int list_ust_event_fields(void)
371 struct lttng_domain domain
;
372 struct lttng_handle
*handle
;
373 struct lttng_event_field
*event_field_list
;
375 char *cmdline
= NULL
;
377 struct lttng_event cur_event
;
379 memset(&domain
, 0, sizeof(domain
));
380 memset(&cur_event
, 0, sizeof(cur_event
));
382 DBG("Getting UST tracing event fields");
384 domain
.type
= LTTNG_DOMAIN_UST
;
386 handle
= lttng_create_handle(NULL
, &domain
);
387 if (handle
== NULL
) {
391 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
393 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
394 lttng_destroy_handle(handle
);
398 MSG("UST events:\n-------------");
404 for (i
= 0; i
< size
; i
++) {
405 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
406 cur_pid
= event_field_list
[i
].event
.pid
;
407 cmdline
= get_cmdline_by_pid(cur_pid
);
408 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
410 /* Wipe current event since we are about to print a new PID. */
411 memset(&cur_event
, 0, sizeof(cur_event
));
413 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
414 print_events(&event_field_list
[i
].event
);
415 memcpy(&cur_event
, &event_field_list
[i
].event
,
418 print_event_field(&event_field_list
[i
]);
423 free(event_field_list
);
424 lttng_destroy_handle(handle
);
429 lttng_destroy_handle(handle
);
434 * Ask for all trace events in the kernel and pretty print them.
436 static int list_kernel_events(void)
439 struct lttng_domain domain
;
440 struct lttng_handle
*handle
;
441 struct lttng_event
*event_list
;
443 memset(&domain
, 0, sizeof(domain
));
445 DBG("Getting kernel tracing events");
447 domain
.type
= LTTNG_DOMAIN_KERNEL
;
449 handle
= lttng_create_handle(NULL
, &domain
);
450 if (handle
== NULL
) {
454 size
= lttng_list_tracepoints(handle
, &event_list
);
456 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
457 lttng_destroy_handle(handle
);
461 MSG("Kernel events:\n-------------");
463 for (i
= 0; i
< size
; i
++) {
464 print_events(&event_list
[i
]);
471 lttng_destroy_handle(handle
);
475 lttng_destroy_handle(handle
);
480 * List events of channel of session and domain.
482 static int list_events(const char *channel_name
)
485 struct lttng_event
*events
= NULL
;
487 count
= lttng_list_events(handle
, channel_name
, &events
);
490 ERR("%s", lttng_strerror(ret
));
494 MSG("\n%sEvents:", indent4
);
496 MSG("%sNone\n", indent6
);
500 for (i
= 0; i
< count
; i
++) {
501 print_events(&events
[i
]);
515 * Pretty print channel
517 static void print_channel(struct lttng_channel
*channel
)
519 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
521 MSG("%sAttributes:", indent4
);
522 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
523 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
524 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
525 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
526 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
527 switch (channel
->attr
.output
) {
528 case LTTNG_EVENT_SPLICE
:
529 MSG("%soutput: splice()", indent6
);
531 case LTTNG_EVENT_MMAP
:
532 MSG("%soutput: mmap()", indent6
);
538 * List channel(s) of session and domain.
540 * If channel_name is NULL, all channels are listed.
542 static int list_channels(const char *channel_name
)
544 int count
, i
, ret
= CMD_SUCCESS
;
545 unsigned int chan_found
= 0;
546 struct lttng_channel
*channels
= NULL
;
548 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
550 count
= lttng_list_channels(handle
, &channels
);
553 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
555 WARN("No kernel channel");
558 /* We had a real error */
560 ERR("%s", lttng_strerror(ret
));
566 if (channel_name
== NULL
) {
567 MSG("Channels:\n-------------");
570 for (i
= 0; i
< count
; i
++) {
571 if (channel_name
!= NULL
) {
572 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
578 print_channel(&channels
[i
]);
580 /* Listing events per channel */
581 ret
= list_events(channels
[i
].name
);
583 ERR("%s", lttng_strerror(ret
));
591 if (!chan_found
&& channel_name
!= NULL
) {
592 ERR("Channel %s not found", channel_name
);
606 * List available tracing session. List only basic information.
608 * If session_name is NULL, all sessions are listed.
610 static int list_sessions(const char *session_name
)
613 unsigned int session_found
= 0;
614 struct lttng_session
*sessions
;
616 count
= lttng_list_sessions(&sessions
);
617 DBG("Session count %d", count
);
620 ERR("%s", lttng_strerror(ret
));
622 } else if (count
== 0) {
623 MSG("Currently no available tracing session");
627 if (session_name
== NULL
) {
628 MSG("Available tracing sessions:");
631 for (i
= 0; i
< count
; i
++) {
632 if (session_name
!= NULL
) {
633 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
635 MSG("Tracing session %s: [%s%s]", session_name
,
636 active_string(sessions
[i
].enabled
),
637 snapshot_string(sessions
[i
].snapshot_mode
));
638 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
642 MSG(" %d) %s (%s) [%s%s]", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
643 active_string(sessions
[i
].enabled
),
644 snapshot_string(sessions
[i
].snapshot_mode
));
650 if (!session_found
&& session_name
!= NULL
) {
651 ERR("Session '%s' not found", session_name
);
656 if (session_name
== NULL
) {
657 MSG("\nUse lttng list <session_name> for more details");
668 * List available domain(s) for a session.
670 static int list_domains(const char *session_name
)
672 int i
, count
, ret
= CMD_SUCCESS
;
673 struct lttng_domain
*domains
= NULL
;
675 MSG("Domains:\n-------------");
677 count
= lttng_list_domains(session_name
, &domains
);
680 ERR("%s", lttng_strerror(ret
));
682 } else if (count
== 0) {
687 for (i
= 0; i
< count
; i
++) {
688 switch (domains
[i
].type
) {
689 case LTTNG_DOMAIN_KERNEL
:
692 case LTTNG_DOMAIN_UST
:
693 MSG(" - UST global");
708 * The 'list <options>' first level command
710 int cmd_list(int argc
, const char **argv
)
712 int opt
, ret
= CMD_SUCCESS
;
713 const char *session_name
;
714 static poptContext pc
;
715 struct lttng_domain domain
;
716 struct lttng_domain
*domains
= NULL
;
718 memset(&domain
, 0, sizeof(domain
));
726 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
727 poptReadDefaultConfig(pc
, 0);
729 while ((opt
= poptGetNextOpt(pc
)) != -1) {
737 case OPT_LIST_OPTIONS
:
738 list_cmd_options(stdout
, long_options
);
747 /* Get session name (trailing argument) */
748 session_name
= poptGetArg(pc
);
749 DBG2("Session name: %s", session_name
);
752 domain
.type
= LTTNG_DOMAIN_KERNEL
;
753 } else if (opt_userspace
) {
754 DBG2("Listing userspace global domain");
755 domain
.type
= LTTNG_DOMAIN_UST
;
758 if (opt_kernel
|| opt_userspace
) {
759 handle
= lttng_create_handle(session_name
, &domain
);
760 if (handle
== NULL
) {
766 if (session_name
== NULL
) {
767 if (!opt_kernel
&& !opt_userspace
) {
768 ret
= list_sessions(NULL
);
774 ret
= list_kernel_events();
782 ret
= list_ust_event_fields();
784 ret
= list_ust_events();
792 /* List session attributes */
793 ret
= list_sessions(session_name
);
800 ret
= list_domains(session_name
);
804 if (opt_kernel
|| opt_userspace
) {
805 /* Channel listing */
806 ret
= list_channels(opt_channel
);
813 /* We want all domain(s) */
814 nb_domain
= lttng_list_domains(session_name
, &domains
);
817 ERR("%s", lttng_strerror(ret
));
821 for (i
= 0; i
< nb_domain
; i
++) {
822 switch (domains
[i
].type
) {
823 case LTTNG_DOMAIN_KERNEL
:
824 MSG("=== Domain: Kernel ===\n");
826 case LTTNG_DOMAIN_UST
:
827 MSG("=== Domain: UST global ===\n");
828 MSG("Buffer type: %s\n",
829 domains
[i
].buf_type
==
830 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
833 MSG("=== Domain: Unimplemented ===\n");
837 /* Clean handle before creating a new one */
839 lttng_destroy_handle(handle
);
842 handle
= lttng_create_handle(session_name
, &domains
[i
]);
843 if (handle
== NULL
) {
848 ret
= list_channels(opt_channel
);
859 lttng_destroy_handle(handle
);