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
;
116 struct lttng_domain dom
;
118 DBG("Getting all tracing events");
120 dom
.type
= LTTNG_DOMAIN_KERNEL
;
122 size
= lttng_list_tracepoints(&dom
, &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(struct lttng_domain
*dom
,
143 const char *session_name
, const char *channel_name
)
146 struct lttng_event
*events
= NULL
;
148 count
= lttng_list_events(dom
, session_name
, channel_name
, &events
);
154 MSG("\n%sEvents:", indent4
);
156 MSG("%sNone", indent6
);
160 for (i
= 0; i
< count
; i
++) {
161 switch (events
[i
].type
) {
162 case LTTNG_EVENT_TRACEPOINT
:
163 MSG("%s%s (type: tracepoint) [enabled: %d]", indent6
,
164 events
[i
].name
, events
[i
].enabled
);
166 case LTTNG_EVENT_PROBE
:
167 MSG("%s%s (type: probe) [enabled: %d]", indent6
,
168 events
[i
].name
, events
[i
].enabled
);
169 if (events
[i
].attr
.probe
.addr
!= 0) {
170 MSG("%saddr: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.addr
);
172 MSG("%soffset: 0x%" PRIx64
, indent8
, events
[i
].attr
.probe
.offset
);
173 MSG("%ssymbol: %s", indent8
, events
[i
].attr
.probe
.symbol_name
);
176 case LTTNG_EVENT_FUNCTION
:
177 case LTTNG_EVENT_FUNCTION_ENTRY
:
178 MSG("%s%s (type: function) [enabled: %d]", indent6
,
179 events
[i
].name
, events
[i
].enabled
);
180 MSG("%ssymbol: \"%s\"", indent8
, events
[i
].attr
.ftrace
.symbol_name
);
198 * Pretty print channel
200 static void print_channel(struct lttng_channel
*channel
)
202 MSG("- %s (enabled: %d):\n", channel
->name
, channel
->enabled
);
204 MSG("%sAttributes:", indent4
);
205 MSG("%soverwrite mode: %d", indent6
, channel
->attr
.overwrite
);
206 MSG("%ssubbufers size: %" PRIu64
, indent6
, channel
->attr
.subbuf_size
);
207 MSG("%snumber of subbufers: %" PRIu64
, indent6
, channel
->attr
.num_subbuf
);
208 MSG("%sswitch timer interval: %u", indent6
, channel
->attr
.switch_timer_interval
);
209 MSG("%sread timer interval: %u", indent6
, channel
->attr
.read_timer_interval
);
210 switch (channel
->attr
.output
) {
211 case LTTNG_EVENT_SPLICE
:
212 MSG("%soutput: splice()", indent6
);
214 case LTTNG_EVENT_MMAP
:
215 MSG("%soutput: mmap()", indent6
);
221 * List channel(s) of session and domain.
223 * If channel_name is NULL, all channels are listed.
225 static int list_channels(struct lttng_domain
*dom
,
226 const char *session_name
, const char *channel_name
)
228 int count
, i
, ret
= CMD_SUCCESS
;
229 unsigned int chan_found
= 0;
230 struct lttng_channel
*channels
= NULL
;
232 DBG("Listing channel(s) (%s)", channel_name
);
234 count
= lttng_list_channels(dom
, session_name
, &channels
);
238 } else if (count
== 0) {
239 MSG("No channel found");
243 if (channel_name
== NULL
) {
244 MSG("Channels:\n-------------");
247 for (i
= 0; i
< count
; i
++) {
248 if (channel_name
!= NULL
) {
249 if (strncmp(channels
[i
].name
, channel_name
, NAME_MAX
) == 0) {
255 print_channel(&channels
[i
]);
257 /* Listing events per channel */
258 ret
= list_events(dom
, session_name
, channels
[i
].name
);
260 MSG("%s", lttng_get_readable_code(ret
));
268 if (!chan_found
&& channel_name
!= NULL
) {
269 MSG("Channel %s not found", channel_name
);
281 * List available tracing session. List only basic information.
283 * If session_name is NULL, all sessions are listed.
285 static int list_sessions(const char *session_name
)
288 unsigned int session_found
= 0;
289 struct lttng_session
*sessions
;
291 count
= lttng_list_sessions(&sessions
);
292 DBG("Session count %d", count
);
298 if (session_name
== NULL
) {
299 MSG("Available tracing sessions:");
302 for (i
= 0; i
< count
; i
++) {
303 if (session_name
!= NULL
) {
304 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
306 MSG("Tracing session %s:", session_name
);
307 MSG("%sTrace path: %s\n", indent4
, sessions
[i
].path
);
312 MSG(" %d) %s (%s)", i
+ 1, sessions
[i
].name
, sessions
[i
].path
);
321 if (!session_found
&& session_name
!= NULL
) {
322 MSG("Session %s not found", session_name
);
325 if (session_name
== NULL
) {
326 MSG("\nUse lttng list <session_name> for more details");
336 * List available domain(s) for a session.
338 static int list_domains(const char *session_name
)
340 int i
, count
, ret
= CMD_SUCCESS
;
341 struct lttng_domain
*domains
= NULL
;
343 MSG("Domains:\n-------------");
345 count
= lttng_list_domains(session_name
, &domains
);
349 } else if (count
== 0) {
354 for (i
= 0; i
< count
; i
++) {
355 switch (domains
[i
].type
) {
356 case LTTNG_DOMAIN_KERNEL
:
371 * The 'list <options>' first level command
373 int cmd_list(int argc
, const char **argv
)
375 int opt
, i
, ret
= CMD_SUCCESS
;
376 const char *session_name
;
377 static poptContext pc
;
378 struct lttng_domain domain
;
379 struct lttng_domain
*domains
= NULL
;
386 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
387 poptReadDefaultConfig(pc
, 0);
389 while ((opt
= poptGetNextOpt(pc
)) != -1) {
401 if (opt_userspace
|| opt_pid
!= 0) {
402 MSG("*** Userspace tracing not implemented ***\n");
405 /* Get session name (trailing argument) */
406 session_name
= poptGetArg(pc
);
407 DBG("Session name: %s", session_name
);
409 if (session_name
== NULL
) {
411 ret
= list_kernel_events();
416 ret
= list_sessions(NULL
);
422 /* List session attributes */
423 ret
= list_sessions(session_name
);
430 ret
= list_domains(session_name
);
435 domain
.type
= LTTNG_DOMAIN_KERNEL
;
436 /* Channel listing */
437 ret
= list_channels(&domain
, session_name
, opt_channel
);
441 } else if (opt_userspace
) {
442 /* TODO: Userspace domain */
444 /* We want all domain(s) */
445 ret
= lttng_list_domains(session_name
, &domains
);
450 for (i
= 0; i
< ret
; i
++) {
451 switch (domains
[i
].type
) {
452 case LTTNG_DOMAIN_KERNEL
:
453 MSG("=== Domain: Kernel ===\n");
456 MSG("=== Domain: Unimplemented ===\n");
460 ret
= list_channels(&domains
[i
], session_name
, opt_channel
);