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 [<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
;
139 const char *enabled_string(int value
)
142 case 0: return " [disabled]";
143 case 1: return " [enabled]";
145 default: return NULL
;
150 const char *filter_string(int value
)
153 case 1: return " [with filter]";
158 static const char *loglevel_string(int value
)
163 case LTTNG_LOGLEVEL_EMERG
:
164 return "TRACE_EMERG";
165 case LTTNG_LOGLEVEL_ALERT
:
166 return "TRACE_ALERT";
167 case LTTNG_LOGLEVEL_CRIT
:
169 case LTTNG_LOGLEVEL_ERR
:
171 case LTTNG_LOGLEVEL_WARNING
:
172 return "TRACE_WARNING";
173 case LTTNG_LOGLEVEL_NOTICE
:
174 return "TRACE_NOTICE";
175 case LTTNG_LOGLEVEL_INFO
:
177 case LTTNG_LOGLEVEL_DEBUG_SYSTEM
:
178 return "TRACE_DEBUG_SYSTEM";
179 case LTTNG_LOGLEVEL_DEBUG_PROGRAM
:
180 return "TRACE_DEBUG_PROGRAM";
181 case LTTNG_LOGLEVEL_DEBUG_PROCESS
:
182 return "TRACE_DEBUG_PROCESS";
183 case LTTNG_LOGLEVEL_DEBUG_MODULE
:
184 return "TRACE_DEBUG_MODULE";
185 case LTTNG_LOGLEVEL_DEBUG_UNIT
:
186 return "TRACE_DEBUG_UNIT";
187 case LTTNG_LOGLEVEL_DEBUG_FUNCTION
:
188 return "TRACE_DEBUG_FUNCTION";
189 case LTTNG_LOGLEVEL_DEBUG_LINE
:
190 return "TRACE_DEBUG_LINE";
191 case LTTNG_LOGLEVEL_DEBUG
:
192 return "TRACE_DEBUG";
194 return "<<UNKNOWN>>";
199 * Pretty print single event.
201 static void print_events(struct lttng_event
*event
)
203 switch (event
->type
) {
204 case LTTNG_EVENT_TRACEPOINT
:
206 if (event
->loglevel
!= -1) {
207 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
210 loglevel_string(event
->loglevel
),
212 enabled_string(event
->enabled
),
213 filter_string(event
->filter
));
215 MSG("%s%s (type: tracepoint)%s%s",
218 enabled_string(event
->enabled
),
219 filter_string(event
->filter
));
223 case LTTNG_EVENT_FUNCTION
:
224 MSG("%s%s (type: function)%s%s", indent6
,
225 event
->name
, enabled_string(event
->enabled
),
226 filter_string(event
->filter
));
227 if (event
->attr
.probe
.addr
!= 0) {
228 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
230 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
231 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
234 case LTTNG_EVENT_PROBE
:
235 MSG("%s%s (type: probe)%s%s", indent6
,
236 event
->name
, enabled_string(event
->enabled
),
237 filter_string(event
->filter
));
238 if (event
->attr
.probe
.addr
!= 0) {
239 MSG("%saddr: 0x%" PRIx64
, indent8
, event
->attr
.probe
.addr
);
241 MSG("%soffset: 0x%" PRIx64
, indent8
, event
->attr
.probe
.offset
);
242 MSG("%ssymbol: %s", indent8
, event
->attr
.probe
.symbol_name
);
245 case LTTNG_EVENT_FUNCTION_ENTRY
:
246 MSG("%s%s (type: function)%s%s", indent6
,
247 event
->name
, enabled_string(event
->enabled
),
248 filter_string(event
->filter
));
249 MSG("%ssymbol: \"%s\"", indent8
, event
->attr
.ftrace
.symbol_name
);
251 case LTTNG_EVENT_SYSCALL
:
252 MSG("%ssyscalls (type: syscall)%s%s", indent6
,
253 enabled_string(event
->enabled
),
254 filter_string(event
->filter
));
256 case LTTNG_EVENT_NOOP
:
257 MSG("%s (type: noop)%s%s", indent6
,
258 enabled_string(event
->enabled
),
259 filter_string(event
->filter
));
261 case LTTNG_EVENT_ALL
:
262 /* We should never have "all" events in list. */
268 static const char *field_type(struct lttng_event_field
*field
)
270 switch(field
->type
) {
271 case LTTNG_EVENT_FIELD_INTEGER
:
273 case LTTNG_EVENT_FIELD_ENUM
:
275 case LTTNG_EVENT_FIELD_FLOAT
:
277 case LTTNG_EVENT_FIELD_STRING
:
279 case LTTNG_EVENT_FIELD_OTHER
:
280 default: /* fall-through */
286 * Pretty print single event fields.
288 static void print_event_field(struct lttng_event_field
*field
)
290 if (!field
->field_name
[0]) {
293 MSG("%sfield: %s (%s)%s", indent8
, field
->field_name
,
294 field_type(field
), field
->nowrite
? " [no write]" : "");
298 * Ask session daemon for all user space tracepoints available.
300 static int list_ust_events(void)
303 struct lttng_domain domain
;
304 struct lttng_handle
*handle
;
305 struct lttng_event
*event_list
;
307 char *cmdline
= NULL
;
309 memset(&domain
, 0, sizeof(domain
));
311 DBG("Getting UST tracing events");
313 domain
.type
= LTTNG_DOMAIN_UST
;
315 handle
= lttng_create_handle(NULL
, &domain
);
316 if (handle
== NULL
) {
320 size
= lttng_list_tracepoints(handle
, &event_list
);
322 ERR("Unable to list UST events: %s", lttng_strerror(size
));
323 lttng_destroy_handle(handle
);
327 MSG("UST events:\n-------------");
333 for (i
= 0; i
< size
; i
++) {
334 if (cur_pid
!= event_list
[i
].pid
) {
335 cur_pid
= event_list
[i
].pid
;
336 cmdline
= get_cmdline_by_pid(cur_pid
);
337 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
340 print_events(&event_list
[i
]);
346 lttng_destroy_handle(handle
);
351 lttng_destroy_handle(handle
);
356 * Ask session daemon for all user space tracepoint fields available.
358 static int list_ust_event_fields(void)
361 struct lttng_domain domain
;
362 struct lttng_handle
*handle
;
363 struct lttng_event_field
*event_field_list
;
365 char *cmdline
= NULL
;
367 struct lttng_event cur_event
;
369 memset(&domain
, 0, sizeof(domain
));
370 memset(&cur_event
, 0, sizeof(cur_event
));
372 DBG("Getting UST tracing event fields");
374 domain
.type
= LTTNG_DOMAIN_UST
;
376 handle
= lttng_create_handle(NULL
, &domain
);
377 if (handle
== NULL
) {
381 size
= lttng_list_tracepoint_fields(handle
, &event_field_list
);
383 ERR("Unable to list UST event fields: %s", lttng_strerror(size
));
384 lttng_destroy_handle(handle
);
388 MSG("UST events:\n-------------");
394 for (i
= 0; i
< size
; i
++) {
395 if (cur_pid
!= event_field_list
[i
].event
.pid
) {
396 cur_pid
= event_field_list
[i
].event
.pid
;
397 cmdline
= get_cmdline_by_pid(cur_pid
);
398 MSG("\nPID: %d - Name: %s", cur_pid
, cmdline
);
401 if (strcmp(cur_event
.name
, event_field_list
[i
].event
.name
) != 0) {
402 print_events(&event_field_list
[i
].event
);
403 memcpy(&cur_event
, &event_field_list
[i
].event
,
406 print_event_field(&event_field_list
[i
]);
411 free(event_field_list
);
412 lttng_destroy_handle(handle
);
417 lttng_destroy_handle(handle
);
422 * Ask for all trace events in the kernel and pretty print them.
424 static int list_kernel_events(void)
427 struct lttng_domain domain
;
428 struct lttng_handle
*handle
;
429 struct lttng_event
*event_list
;
431 memset(&domain
, 0, sizeof(domain
));
433 DBG("Getting kernel tracing events");
435 domain
.type
= LTTNG_DOMAIN_KERNEL
;
437 handle
= lttng_create_handle(NULL
, &domain
);
438 if (handle
== NULL
) {
442 size
= lttng_list_tracepoints(handle
, &event_list
);
444 ERR("Unable to list kernel events: %s", lttng_strerror(size
));
445 lttng_destroy_handle(handle
);
449 MSG("Kernel events:\n-------------");
451 for (i
= 0; i
< size
; i
++) {
452 print_events(&event_list
[i
]);
459 lttng_destroy_handle(handle
);
463 lttng_destroy_handle(handle
);
468 * List events of channel of session and domain.
470 static int list_events(const char *channel_name
)
473 struct lttng_event
*events
= NULL
;
475 count
= lttng_list_events(handle
, channel_name
, &events
);
478 ERR("%s", lttng_strerror(ret
));
482 MSG("\n%sEvents:", indent4
);
484 MSG("%sNone\n", indent6
);
488 for (i
= 0; i
< count
; i
++) {
489 print_events(&events
[i
]);
503 * Pretty print channel
505 static void print_channel(struct lttng_channel
*channel
)
507 MSG("- %s:%s\n", channel
->name
, enabled_string(channel
->enabled
));
509 MSG("%sAttributes:", indent4
);
510 MSG("%swrite mode: %s", indent6
,
511 channel
->attr
.overwrite
== 0 ? "discard" : "overwrite");
512 MSG("%ssubbufers size: %" PRIu64
" bytes", indent6
,
513 channel
->attr
.subbuf_size
);
514 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
515 MSG("%sswitch timer interval: %u usec", indent6
,
516 channel
->attr
.switch_timer_interval
);
517 MSG("%sread timer interval: %u usec", indent6
,
518 channel
->attr
.read_timer_interval
);
519 switch (channel
->attr
.output
) {
520 case LTTNG_EVENT_SPLICE
:
521 MSG("%soutput: splice()", indent6
);
523 case LTTNG_EVENT_MMAP
:
524 MSG("%soutput: mmap()", indent6
);
530 * List channel(s) of session and domain.
532 * If channel_name is NULL, all channels are listed.
534 static int list_channels(const char *channel_name
)
536 int count
, i
, ret
= CMD_SUCCESS
;
537 unsigned int chan_found
= 0;
538 struct lttng_channel
*channels
= NULL
;
540 DBG("Listing channel(s) (%s)", channel_name
? : "<all>");
542 count
= lttng_list_channels(handle
, &channels
);
545 case LTTNG_ERR_KERN_CHAN_NOT_FOUND
:
547 WARN("No kernel channel");
550 /* We had a real error */
552 ERR("%s", lttng_strerror(ret
));
558 if (channel_name
== NULL
) {
559 MSG("Channels:\n-------------");
562 for (i
= 0; i
< count
; i
++) {
563 if (channel_name
!= NULL
) {
564 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
570 print_channel(&channels
[i
]);
572 /* Listing events per channel */
573 ret
= list_events(channels
[i
].name
);
575 ERR("%s", lttng_strerror(ret
));
583 if (!chan_found
&& channel_name
!= NULL
) {
584 ERR("Channel %s not found", channel_name
);
598 * List available tracing session. List only basic information.
600 * If session_name is NULL, all sessions are listed.
602 static int list_sessions(const char *session_name
)
605 unsigned int session_found
= 0;
606 struct lttng_session
*sessions
;
608 count
= lttng_list_sessions(&sessions
);
609 DBG("Session count %d", count
);
612 ERR("%s", lttng_strerror(ret
));
614 } else if (count
== 0) {
615 MSG("Currently no available tracing session");
619 if (session_name
== NULL
) {
620 MSG("Available tracing sessions:");
623 for (i
= 0; i
< count
; i
++) {
624 if (session_name
!= NULL
) {
625 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
627 MSG("Tracing session %s:%s", session_name
, active_string(sessions
[i
].enabled
));
628 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
634 MSG(" %d) %s (%s)%s", i
+ 1, sessions
[i
].name
, sessions
[i
].path
,
635 active_string(sessions
[i
].enabled
));
644 if (!session_found
&& session_name
!= NULL
) {
645 ERR("Session '%s' not found", session_name
);
650 if (session_name
== NULL
) {
651 MSG("\nUse lttng list <session_name> for more details");
662 * List available domain(s) for a session.
664 static int list_domains(const char *session_name
)
666 int i
, count
, ret
= CMD_SUCCESS
;
667 struct lttng_domain
*domains
= NULL
;
669 MSG("Domains:\n-------------");
671 count
= lttng_list_domains(session_name
, &domains
);
674 ERR("%s", lttng_strerror(ret
));
676 } else if (count
== 0) {
681 for (i
= 0; i
< count
; i
++) {
682 switch (domains
[i
].type
) {
683 case LTTNG_DOMAIN_KERNEL
:
686 case LTTNG_DOMAIN_UST
:
687 MSG(" - UST global");
702 * The 'list <options>' first level command
704 int cmd_list(int argc
, const char **argv
)
706 int opt
, ret
= CMD_SUCCESS
;
707 const char *session_name
;
708 static poptContext pc
;
709 struct lttng_domain domain
;
710 struct lttng_domain
*domains
= NULL
;
712 memset(&domain
, 0, sizeof(domain
));
720 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
721 poptReadDefaultConfig(pc
, 0);
723 while ((opt
= poptGetNextOpt(pc
)) != -1) {
731 case OPT_LIST_OPTIONS
:
732 list_cmd_options(stdout
, long_options
);
741 /* Get session name (trailing argument) */
742 session_name
= poptGetArg(pc
);
743 DBG2("Session name: %s", session_name
);
746 domain
.type
= LTTNG_DOMAIN_KERNEL
;
747 } else if (opt_userspace
) {
748 DBG2("Listing userspace global domain");
749 domain
.type
= LTTNG_DOMAIN_UST
;
752 if (opt_kernel
|| opt_userspace
) {
753 handle
= lttng_create_handle(session_name
, &domain
);
754 if (handle
== NULL
) {
760 if (session_name
== NULL
) {
761 if (!opt_kernel
&& !opt_userspace
) {
762 ret
= list_sessions(NULL
);
768 ret
= list_kernel_events();
776 ret
= list_ust_event_fields();
778 ret
= list_ust_events();
786 /* List session attributes */
787 ret
= list_sessions(session_name
);
794 ret
= list_domains(session_name
);
799 /* Channel listing */
800 ret
= list_channels(opt_channel
);
807 /* We want all domain(s) */
808 nb_domain
= lttng_list_domains(session_name
, &domains
);
811 ERR("%s", lttng_strerror(ret
));
815 for (i
= 0; i
< nb_domain
; i
++) {
816 switch (domains
[i
].type
) {
817 case LTTNG_DOMAIN_KERNEL
:
818 MSG("=== Domain: Kernel ===\n");
820 case LTTNG_DOMAIN_UST
:
821 MSG("=== Domain: UST global ===\n");
822 MSG("Buffer type: %s\n",
823 domains
[i
].buf_type
==
824 LTTNG_BUFFER_PER_PID
? "per PID" : "per UID");
827 MSG("=== Domain: Unimplemented ===\n");
831 /* Clean handle before creating a new one */
833 lttng_destroy_handle(handle
);
836 handle
= lttng_create_handle(session_name
, &domains
[i
]);
837 if (handle
== NULL
) {
842 ret
= list_channels(opt_channel
);
853 lttng_destroy_handle(handle
);