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.
29 static int opt_userspace
;
30 static int opt_kernel
;
31 static char *opt_channel
;
32 static int opt_domain
;
34 const char *indent4
= " ";
35 const char *indent6
= " ";
36 const char *indent8
= " ";
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
46 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
47 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
48 {"channel", 'c', POPT_ARG_STRING
, &opt_channel
, 0, 0, 0},
49 {"domain", 'd', POPT_ARG_VAL
, &opt_domain
, 1, 0, 0},
56 static void usage(FILE *ofp
)
58 fprintf(ofp
, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
60 fprintf(ofp
, "With no arguments, list available tracing session(s)\n");
62 fprintf(ofp
, "With -k alone, list available kernel events\n");
63 fprintf(ofp
, "With -u alone, list available userspace events\n");
65 fprintf(ofp
, " -h, --help Show this help\n");
66 fprintf(ofp
, " -k, --kernel Select kernel domain\n");
67 fprintf(ofp
, " -u, --userspace Select user-space domain.\n");
68 fprintf(ofp
, " -p, --pid PID List user-space events by PID\n");
70 fprintf(ofp
, "Options:\n");
71 fprintf(ofp
, " -c, --channel NAME List details of a channel\n");
72 fprintf(ofp
, " -d, --domain List available domain(s)\n");
77 * Get command line from /proc for a specific pid.
79 * On success, return an allocated string pointer to the proc cmdline.
80 * On error, return NULL.
83 static char *get_cmdline_by_pid(pid_t pid
)
88 char path
[24]; /* Can't go bigger than /proc/65535/cmdline */
90 snprintf(path
, sizeof(path
), "/proc/%d/cmdline", pid
);
91 fp
= fopen(path
, "r");
96 /* Caller must free() *cmdline */
97 cmdline
= malloc(PATH_MAX
);
98 ret
= fread(cmdline
, 1, PATH_MAX
, fp
);
100 perror("fread proc list");
110 * Ask for all trace events in the kernel and pretty print them.
112 static int list_kernel_events(void)
115 struct lttng_event
*event_list
;
117 DBG("Getting all tracing events");
119 size
= lttng_list_kernel_events(&event_list
);
121 ERR("Unable to list kernel events");
125 MSG("Kernel events:\n-------------");
127 for (i
= 0; i
< size
; i
++) {
128 MSG(" %s", event_list
[i
].name
);
137 * List events of channel of session and domain.
139 static int list_events(struct lttng_domain
*dom
,
140 const char *session_name
, const char *channel_name
)
143 struct lttng_event
*events
= NULL
;
145 count
= lttng_list_events(dom
, session_name
, channel_name
, &events
);
151 MSG("\n%sEvents:", indent4
);
153 MSG("%sNone", indent6
);
157 for (i
= 0; i
< count
; i
++) {
158 switch (events
[i
].type
) {
159 case LTTNG_EVENT_TRACEPOINT
:
160 MSG("%s%s (type: tracepoint) [enabled: %d]", indent6
,
161 events
[i
].name
, events
[i
].enabled
);
163 case LTTNG_EVENT_PROBE
:
164 MSG("%s%s (type: probe) [enabled: %d]", indent6
,
165 events
[i
].name
, events
[i
].enabled
);
166 if (events
[i
].attr
.probe
.addr
!= 0) {
167 MSG("%saddr: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.addr
);
169 MSG("%soffset: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.offset
);
170 MSG("%ssymbol: %s", indent8
, events
[i
].attr
.probe
.symbol_name
);
173 case LTTNG_EVENT_FUNCTION
:
174 case LTTNG_EVENT_FUNCTION_ENTRY
:
175 MSG("%s%s (type: function) [enabled: %d]", indent6
,
176 events
[i
].name
, events
[i
].enabled
);
177 MSG("%ssymbol: \"%s\"", indent8
, events
[i
].attr
.ftrace
.symbol_name
);
195 * Pretty print channel
197 static void print_channel(struct lttng_channel
*channel
)
199 MSG("- %s (enabled: %d):\n", channel
->name
, channel
->enabled
);
201 MSG("%sAttributes:", indent4
);
202 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
203 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
204 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
205 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
206 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
207 switch (channel
->attr
.output
) {
208 case LTTNG_EVENT_SPLICE
:
209 MSG("%soutput: splice()", indent6
);
211 case LTTNG_EVENT_MMAP
:
212 MSG("%soutput: mmap()", indent6
);
218 * List channel(s) of session and domain.
220 * If channel_name is NULL, all channels are listed.
222 static int list_channels(struct lttng_domain
*dom
,
223 const char *session_name
, const char *channel_name
)
225 int count
, i
, ret
= CMD_SUCCESS
;
226 unsigned int chan_found
= 0;
227 struct lttng_channel
*channels
= NULL
;
229 DBG("Listing channel(s) (%s)", channel_name
);
231 count
= lttng_list_channels(dom
, session_name
, &channels
);
235 } else if (count
== 0) {
236 MSG("No channel found");
240 if (channel_name
== NULL
) {
241 MSG("Channels:\n-------------");
244 for (i
= 0; i
< count
; i
++) {
245 if (channel_name
!= NULL
) {
246 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
252 print_channel(&channels
[i
]);
254 /* Listing events per channel */
255 ret
= list_events(dom
, session_name
, channels
[i
].name
);
257 MSG("%s", lttng_get_readable_code(ret
));
265 if (!chan_found
&& channel_name
!= NULL
) {
266 MSG("Channel %s not found", channel_name
);
278 * List available tracing session. List only basic information.
280 * If session_name is NULL, all sessions are listed.
282 static int list_sessions(const char *session_name
)
285 unsigned int session_found
= 0;
286 struct lttng_session
*sessions
;
288 count
= lttng_list_sessions(&sessions
);
289 DBG("Session count %d", count
);
295 if (session_name
== NULL
) {
296 MSG("Available tracing sessions:");
299 for (i
= 0; i
< count
; i
++) {
300 if (session_name
!= NULL
) {
301 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
303 MSG("Tracing session %s:", session_name
);
304 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
309 MSG(" %d) %s (%s)", i
+ 1, sessions
[i
].name
, sessions
[i
].path
);
318 if (!session_found
&& session_name
!= NULL
) {
319 MSG("Session %s not found", session_name
);
322 if (session_name
== NULL
) {
323 MSG("\nUse lttng list -s <session_name> for a detail listing");
333 * List available domain(s) for a session.
335 static int list_domains(const char *session_name
)
337 int i
, count
, ret
= CMD_SUCCESS
;
338 struct lttng_domain
*domains
= NULL
;
340 MSG("Domains:\n-------------");
342 count
= lttng_list_domains(session_name
, &domains
);
346 } else if (count
== 0) {
351 for (i
= 0; i
< count
; i
++) {
352 switch (domains
[i
].type
) {
353 case LTTNG_DOMAIN_KERNEL
:
368 * The 'list <options>' first level command
370 int cmd_list(int argc
, const char **argv
)
372 int opt
, i
, ret
= CMD_SUCCESS
;
373 const char *session_name
;
374 static poptContext pc
;
375 struct lttng_domain domain
;
376 struct lttng_domain
*domains
= NULL
;
383 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
384 poptReadDefaultConfig(pc
, 0);
386 while ((opt
= poptGetNextOpt(pc
)) != -1) {
398 if (opt_userspace
|| opt_pid
!= 0) {
399 MSG("*** Userspace tracing not implemented ***\n");
402 /* Get session name (trailing argument) */
403 session_name
= poptGetArg(pc
);
404 DBG("Session name: %s", session_name
);
406 if (session_name
== NULL
) {
408 ret
= list_kernel_events();
413 ret
= list_sessions(NULL
);
419 /* List session attributes */
420 ret
= list_sessions(session_name
);
427 ret
= list_domains(session_name
);
432 domain
.type
= LTTNG_DOMAIN_KERNEL
;
433 /* Channel listing */
434 ret
= list_channels(&domain
, session_name
, opt_channel
);
438 } else if (opt_userspace
) {
439 /* TODO: Userspace domain */
441 /* We want all domain(s) */
442 ret
= lttng_list_domains(session_name
, &domains
);
447 for (i
= 0; i
< ret
; i
++) {
448 switch (domains
[i
].type
) {
449 case LTTNG_DOMAIN_KERNEL
:
450 MSG("=== Domain: Kernel ===\n");
453 MSG("=== Domain: Unimplemented ===\n");
457 ret
= list_channels(&domains
[i
], session_name
, opt_channel
);