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 int opt_kernel
;
32 static char *opt_channel
;
33 static int opt_domain
;
35 const char *indent4
= " ";
36 const char *indent6
= " ";
37 const char *indent8
= " ";
43 static struct lttng_handle
*handle
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
49 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
50 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
51 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
52 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
59 static void usage(FILE *ofp
)
61 fprintf(ofp
, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
63 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
65 fprintf(ofp
, "With -k alone, list available kernel events\n");
66 fprintf(ofp
, "With -u alone, list available userspace events\n");
68 fprintf(ofp
, " -h, --help Show this help\n");
69 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
70 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
71 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
73 fprintf(ofp
, "Options:\n");
74 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
75 fprintf(ofp
, " -d, --domain List available domain(s)\n");
80 * Get command line from /proc for a specific pid.
82 * On success, return an allocated string pointer to the proc cmdline.
83 * On error, return NULL.
86 static char *get_cmdline_by_pid(pid_t pid
)
91 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
93 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
94 fp
= fopen(path
, "r");
99 /* Caller must free() *cmdline */
100 cmdline
= malloc(PATH_MAX
);
101 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
103 perror("fread proc list");
113 * Ask for all trace events in the kernel and pretty print them.
115 static int list_kernel_events(void)
118 struct lttng_event
*event_list
;
120 DBG("Getting all tracing events");
122 size
= lttng_list_tracepoints(handle
, &event_list
);
124 ERR("Unable to list kernel events");
128 MSG("Kernel events:\n-------------");
130 for (i
= 0; i
< size
; i
++) {
131 MSG(" %s", event_list
[i
].name
);
140 * List events of channel of session and domain.
142 static int list_events(const char *channel_name
)
145 struct lttng_event
*events
= NULL
;
147 count
= lttng_list_events(handle
, channel_name
, &events
);
153 MSG("\n%sEvents:", indent4
);
155 MSG("%sNone", indent6
);
159 for (i
= 0; i
< count
; i
++) {
160 switch (events
[i
].type
) {
161 case LTTNG_EVENT_TRACEPOINT
:
162 MSG("%s%s (type: tracepoint) [enabled: %d]", indent6
,
163 events
[i
].name
, events
[i
].enabled
);
165 case LTTNG_EVENT_PROBE
:
166 MSG("%s%s (type: probe) [enabled: %d]", indent6
,
167 events
[i
].name
, events
[i
].enabled
);
168 if (events
[i
].attr
.probe
.addr
!= 0) {
169 MSG("%saddr: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.addr
);
171 MSG("%soffset: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.offset
);
172 MSG("%ssymbol: %s", indent8
, events
[i
].attr
.probe
.symbol_name
);
175 case LTTNG_EVENT_FUNCTION
:
176 case LTTNG_EVENT_FUNCTION_ENTRY
:
177 MSG("%s%s (type: function) [enabled: %d]", indent6
,
178 events
[i
].name
, events
[i
].enabled
);
179 MSG("%ssymbol: \"%s\"", indent8
, events
[i
].attr
.ftrace
.symbol_name
);
181 case LTTNG_EVENT_SYSCALL
:
182 MSG("%s (type: syscall) [enabled: %d]", indent6
,
185 case LTTNG_EVENT_NOOP
:
186 MSG("%s (type: noop) [enabled: %d]", indent6
,
189 case LTTNG_EVENT_ALL
:
190 /* We should never have "all" events in list. */
209 * Pretty print channel
211 static void print_channel(struct lttng_channel
*channel
)
213 MSG("- %s (enabled: %d):\n", channel
->name
, channel
->enabled
);
215 MSG("%sAttributes:", indent4
);
216 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
217 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
218 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
219 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
220 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
221 switch (channel
->attr
.output
) {
222 case LTTNG_EVENT_SPLICE
:
223 MSG("%soutput: splice()", indent6
);
225 case LTTNG_EVENT_MMAP
:
226 MSG("%soutput: mmap()", indent6
);
232 * List channel(s) of session and domain.
234 * If channel_name is NULL, all channels are listed.
236 static int list_channels(const char *channel_name
)
238 int count
, i
, ret
= CMD_SUCCESS
;
239 unsigned int chan_found
= 0;
240 struct lttng_channel
*channels
= NULL
;
242 DBG("Listing channel(s) (%s)", channel_name
);
244 count
= lttng_list_channels(handle
, &channels
);
248 } else if (count
== 0) {
249 MSG("No channel found");
253 if (channel_name
== NULL
) {
254 MSG("Channels:\n-------------");
257 for (i
= 0; i
< count
; i
++) {
258 if (channel_name
!= NULL
) {
259 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
265 print_channel(&channels
[i
]);
267 /* Listing events per channel */
268 ret
= list_events(channels
[i
].name
);
270 MSG("%s", lttng_strerror(ret
));
278 if (!chan_found
&& channel_name
!= NULL
) {
279 MSG("Channel %s not found", channel_name
);
291 * List available tracing session. List only basic information.
293 * If session_name is NULL, all sessions are listed.
295 static int list_sessions(const char *session_name
)
298 unsigned int session_found
= 0;
299 struct lttng_session
*sessions
;
301 count
= lttng_list_sessions(&sessions
);
302 DBG("Session count %d", count
);
308 if (session_name
== NULL
) {
309 MSG("Available tracing sessions:");
312 for (i
= 0; i
< count
; i
++) {
313 if (session_name
!= NULL
) {
314 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
316 MSG("Tracing session %s:", session_name
);
317 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
323 MSG(" %d) %s (%s)", i
+ 1, sessions
[i
].name
, sessions
[i
].path
);
332 if (!session_found
&& session_name
!= NULL
) {
333 MSG("Session %s not found", session_name
);
336 if (session_name
== NULL
) {
337 MSG("\nUse lttng list <session_name> for more details");
347 * List available domain(s) for a session.
349 static int list_domains(void)
351 int i
, count
, ret
= CMD_SUCCESS
;
352 struct lttng_domain
*domains
= NULL
;
354 MSG("Domains:\n-------------");
356 count
= lttng_list_domains(handle
, &domains
);
360 } else if (count
== 0) {
365 for (i
= 0; i
< count
; i
++) {
366 switch (domains
[i
].type
) {
367 case LTTNG_DOMAIN_KERNEL
:
382 * The 'list <options>' first level command
384 int cmd_list(int argc
, const char **argv
)
386 int opt
, i
, ret
= CMD_SUCCESS
;
387 const char *session_name
;
388 static poptContext pc
;
389 struct lttng_domain domain
;
390 struct lttng_domain
*domains
= NULL
;
397 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
398 poptReadDefaultConfig(pc
, 0);
400 while ((opt
= poptGetNextOpt(pc
)) != -1) {
412 if (opt_userspace
|| opt_pid
!= 0) {
413 MSG("*** Userspace tracing not implemented ***\n");
416 /* Get session name (trailing argument) */
417 session_name
= poptGetArg(pc
);
418 DBG("Session name: %s", session_name
);
421 domain
.type
= LTTNG_DOMAIN_KERNEL
;
424 handle
= lttng_create_handle(session_name
, &domain
);
425 if (handle
== NULL
) {
429 if (session_name
== NULL
) {
431 ret
= list_kernel_events();
436 ret
= list_sessions(NULL
);
442 /* List session attributes */
443 ret
= list_sessions(session_name
);
450 ret
= list_domains();
455 /* Channel listing */
456 ret
= list_channels(opt_channel
);
460 } else if (opt_userspace
) {
461 /* TODO: Userspace domain */
463 /* We want all domain(s) */
464 ret
= lttng_list_domains(handle
, &domains
);
469 for (i
= 0; i
< ret
; i
++) {
470 switch (domains
[i
].type
) {
471 case LTTNG_DOMAIN_KERNEL
:
472 MSG("=== Domain: Kernel ===\n");
475 MSG("=== Domain: Unimplemented ===\n");
479 /* Clean handle before creating a new one */
480 lttng_destroy_handle(handle
);
482 handle
= lttng_create_handle(session_name
, &domains
[i
]);
483 if (handle
== NULL
) {
487 ret
= list_channels(opt_channel
);
499 lttng_destroy_handle(handle
);