Commit | Line | Data |
---|---|---|
f3ed775e DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
f3ed775e DG |
8 | * |
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. | |
13 | * | |
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. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
9f19cc17 | 20 | #include <inttypes.h> |
f3ed775e DG |
21 | #include <popt.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
7a3d1328 | 25 | #include <assert.h> |
f3ed775e | 26 | |
6e2d116c | 27 | #include "../cmd.h" |
f3ed775e DG |
28 | |
29 | static int opt_pid; | |
9f19cc17 DG |
30 | static int opt_userspace; |
31 | static int opt_kernel; | |
32 | static char *opt_channel; | |
33 | static int opt_domain; | |
34 | ||
35 | const char *indent4 = " "; | |
36 | const char *indent6 = " "; | |
37 | const char *indent8 = " "; | |
f3ed775e DG |
38 | |
39 | enum { | |
40 | OPT_HELP = 1, | |
f3ed775e DG |
41 | }; |
42 | ||
cd80958d DG |
43 | static struct lttng_handle *handle; |
44 | ||
f3ed775e DG |
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}, | |
9f19cc17 DG |
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}, | |
f3ed775e DG |
53 | {0, 0, 0, 0, 0, 0, 0} |
54 | }; | |
55 | ||
56 | /* | |
57 | * usage | |
58 | */ | |
59 | static void usage(FILE *ofp) | |
60 | { | |
9f19cc17 DG |
61 | fprintf(ofp, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n"); |
62 | fprintf(ofp, "\n"); | |
63 | fprintf(ofp, "With no arguments, list available tracing session(s)\n"); | |
64 | fprintf(ofp, "\n"); | |
65 | fprintf(ofp, "With -k alone, list available kernel events\n"); | |
66 | fprintf(ofp, "With -u alone, list available userspace events\n"); | |
67 | fprintf(ofp, "\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"); | |
f3ed775e | 72 | fprintf(ofp, "\n"); |
9f19cc17 DG |
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"); | |
f3ed775e DG |
76 | fprintf(ofp, "\n"); |
77 | } | |
78 | ||
79 | /* | |
9f19cc17 | 80 | * Get command line from /proc for a specific pid. |
f3ed775e | 81 | * |
9f19cc17 DG |
82 | * On success, return an allocated string pointer to the proc cmdline. |
83 | * On error, return NULL. | |
f3ed775e DG |
84 | */ |
85 | static char *get_cmdline_by_pid(pid_t pid) | |
86 | { | |
87 | int ret; | |
88 | FILE *fp; | |
89 | char *cmdline = NULL; | |
90 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ | |
91 | ||
92 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); | |
93 | fp = fopen(path, "r"); | |
94 | if (fp == NULL) { | |
95 | goto end; | |
96 | } | |
97 | ||
98 | /* Caller must free() *cmdline */ | |
99 | cmdline = malloc(PATH_MAX); | |
100 | ret = fread(cmdline, 1, PATH_MAX, fp); | |
f40799e8 DG |
101 | if (ret < 0) { |
102 | perror("fread proc list"); | |
103 | } | |
f3ed775e DG |
104 | fclose(fp); |
105 | ||
106 | end: | |
107 | return cmdline; | |
108 | } | |
b551a063 DG |
109 | |
110 | /* | |
111 | * Pretty print single event. | |
112 | */ | |
113 | static void print_events(struct lttng_event *event) | |
114 | { | |
115 | switch (event->type) { | |
116 | case LTTNG_EVENT_TRACEPOINT: | |
117 | MSG("%s%s (type: tracepoint) [enabled: %d]", indent6, | |
118 | event->name, event->enabled); | |
119 | break; | |
120 | case LTTNG_EVENT_PROBE: | |
121 | MSG("%s%s (type: probe) [enabled: %d]", indent6, | |
122 | event->name, event->enabled); | |
123 | if (event->attr.probe.addr != 0) { | |
124 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); | |
125 | } else { | |
126 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); | |
127 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); | |
128 | } | |
129 | break; | |
130 | case LTTNG_EVENT_FUNCTION: | |
131 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
132 | MSG("%s%s (type: function) [enabled: %d]", indent6, | |
133 | event->name, event->enabled); | |
134 | MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name); | |
135 | break; | |
136 | case LTTNG_EVENT_SYSCALL: | |
137 | MSG("%s (type: syscall) [enabled: %d]", indent6, | |
138 | event->enabled); | |
139 | break; | |
140 | case LTTNG_EVENT_NOOP: | |
141 | MSG("%s (type: noop) [enabled: %d]", indent6, | |
142 | event->enabled); | |
143 | break; | |
144 | case LTTNG_EVENT_ALL: | |
145 | /* We should never have "all" events in list. */ | |
146 | assert(0); | |
147 | break; | |
148 | } | |
149 | } | |
150 | ||
151 | /* | |
152 | * Ask session daemon for all user space tracepoints available. | |
153 | */ | |
154 | static int list_ust_events(void) | |
155 | { | |
156 | int i, size; | |
157 | struct lttng_domain domain; | |
158 | struct lttng_handle *handle; | |
159 | struct lttng_event *event_list; | |
160 | pid_t cur_pid = 0; | |
161 | ||
162 | DBG("Getting UST tracing events"); | |
163 | ||
164 | domain.type = LTTNG_DOMAIN_UST; | |
165 | ||
166 | handle = lttng_create_handle(NULL, &domain); | |
167 | if (handle == NULL) { | |
168 | goto error; | |
169 | } | |
170 | ||
171 | size = lttng_list_tracepoints(handle, &event_list); | |
172 | if (size < 0) { | |
173 | ERR("Unable to list UST events"); | |
174 | return size; | |
175 | } | |
176 | ||
177 | MSG("UST events:\n-------------"); | |
178 | ||
179 | if (size == 0) { | |
180 | MSG("None"); | |
181 | } | |
182 | ||
183 | for (i = 0; i < size; i++) { | |
184 | if (cur_pid != event_list[i].pid) { | |
185 | cur_pid = event_list[i].pid; | |
186 | MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid)); | |
187 | } | |
188 | print_events(&event_list[i]); | |
189 | } | |
190 | ||
191 | MSG(""); | |
192 | ||
193 | free(event_list); | |
194 | ||
195 | return CMD_SUCCESS; | |
196 | ||
197 | error: | |
198 | return -1; | |
199 | } | |
f3ed775e DG |
200 | |
201 | /* | |
9f19cc17 | 202 | * Ask for all trace events in the kernel and pretty print them. |
f3ed775e | 203 | */ |
9f19cc17 | 204 | static int list_kernel_events(void) |
f3ed775e | 205 | { |
9f19cc17 | 206 | int i, size; |
b551a063 DG |
207 | struct lttng_domain domain; |
208 | struct lttng_handle *handle; | |
9f19cc17 | 209 | struct lttng_event *event_list; |
f3ed775e | 210 | |
b551a063 DG |
211 | DBG("Getting kernel tracing events"); |
212 | ||
213 | domain.type = LTTNG_DOMAIN_KERNEL; | |
214 | ||
215 | handle = lttng_create_handle(NULL, &domain); | |
216 | if (handle == NULL) { | |
217 | goto error; | |
218 | } | |
f3ed775e | 219 | |
cd80958d | 220 | size = lttng_list_tracepoints(handle, &event_list); |
9f19cc17 DG |
221 | if (size < 0) { |
222 | ERR("Unable to list kernel events"); | |
223 | return size; | |
f3ed775e DG |
224 | } |
225 | ||
9f19cc17 | 226 | MSG("Kernel events:\n-------------"); |
f3ed775e | 227 | |
9f19cc17 | 228 | for (i = 0; i < size; i++) { |
b551a063 | 229 | print_events(&event_list[i]); |
f3ed775e DG |
230 | } |
231 | ||
b551a063 DG |
232 | MSG(""); |
233 | ||
f3ed775e DG |
234 | free(event_list); |
235 | ||
236 | return CMD_SUCCESS; | |
b551a063 DG |
237 | |
238 | error: | |
239 | return -1; | |
f3ed775e DG |
240 | } |
241 | ||
242 | /* | |
9f19cc17 | 243 | * List events of channel of session and domain. |
f3ed775e | 244 | */ |
cd80958d | 245 | static int list_events(const char *channel_name) |
f3ed775e DG |
246 | { |
247 | int ret, count, i; | |
9f19cc17 | 248 | struct lttng_event *events = NULL; |
f3ed775e | 249 | |
cd80958d | 250 | count = lttng_list_events(handle, channel_name, &events); |
f3ed775e DG |
251 | if (count < 0) { |
252 | ret = count; | |
253 | goto error; | |
254 | } | |
255 | ||
9f19cc17 DG |
256 | MSG("\n%sEvents:", indent4); |
257 | if (count == 0) { | |
258 | MSG("%sNone", indent6); | |
259 | goto end; | |
260 | } | |
261 | ||
f3ed775e | 262 | for (i = 0; i < count; i++) { |
b551a063 | 263 | print_events(&events[i]); |
f3ed775e DG |
264 | } |
265 | ||
9f19cc17 | 266 | MSG(""); |
f3ed775e | 267 | |
9f19cc17 DG |
268 | end: |
269 | if (events) { | |
270 | free(events); | |
271 | } | |
272 | ret = CMD_SUCCESS; | |
f3ed775e DG |
273 | |
274 | error: | |
275 | return ret; | |
276 | } | |
277 | ||
278 | /* | |
9f19cc17 DG |
279 | * Pretty print channel |
280 | */ | |
281 | static void print_channel(struct lttng_channel *channel) | |
282 | { | |
283 | MSG("- %s (enabled: %d):\n", channel->name, channel->enabled); | |
284 | ||
285 | MSG("%sAttributes:", indent4); | |
286 | MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite); | |
287 | MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size); | |
288 | MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf); | |
289 | MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval); | |
290 | MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval); | |
291 | switch (channel->attr.output) { | |
292 | case LTTNG_EVENT_SPLICE: | |
293 | MSG("%soutput: splice()", indent6); | |
294 | break; | |
295 | case LTTNG_EVENT_MMAP: | |
296 | MSG("%soutput: mmap()", indent6); | |
297 | break; | |
298 | } | |
299 | } | |
300 | ||
301 | /* | |
302 | * List channel(s) of session and domain. | |
f3ed775e | 303 | * |
9f19cc17 | 304 | * If channel_name is NULL, all channels are listed. |
f3ed775e | 305 | */ |
cd80958d | 306 | static int list_channels(const char *channel_name) |
f3ed775e | 307 | { |
9f19cc17 DG |
308 | int count, i, ret = CMD_SUCCESS; |
309 | unsigned int chan_found = 0; | |
310 | struct lttng_channel *channels = NULL; | |
f3ed775e | 311 | |
9f19cc17 DG |
312 | DBG("Listing channel(s) (%s)", channel_name); |
313 | ||
cd80958d | 314 | count = lttng_list_channels(handle, &channels); |
f3ed775e DG |
315 | if (count < 0) { |
316 | ret = count; | |
317 | goto error; | |
9f19cc17 DG |
318 | } else if (count == 0) { |
319 | MSG("No channel found"); | |
320 | goto end; | |
f3ed775e DG |
321 | } |
322 | ||
9f19cc17 DG |
323 | if (channel_name == NULL) { |
324 | MSG("Channels:\n-------------"); | |
325 | } | |
326 | ||
327 | for (i = 0; i < count; i++) { | |
328 | if (channel_name != NULL) { | |
329 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { | |
330 | chan_found = 1; | |
331 | } else { | |
332 | continue; | |
333 | } | |
334 | } | |
335 | print_channel(&channels[i]); | |
336 | ||
337 | /* Listing events per channel */ | |
cd80958d | 338 | ret = list_events(channels[i].name); |
9f19cc17 | 339 | if (ret < 0) { |
9a745bc7 | 340 | MSG("%s", lttng_strerror(ret)); |
9f19cc17 DG |
341 | } |
342 | ||
343 | if (chan_found) { | |
344 | break; | |
f3ed775e | 345 | } |
f3ed775e DG |
346 | } |
347 | ||
9f19cc17 DG |
348 | if (!chan_found && channel_name != NULL) { |
349 | MSG("Channel %s not found", channel_name); | |
350 | } | |
f3ed775e | 351 | |
9f19cc17 DG |
352 | end: |
353 | free(channels); | |
354 | ret = CMD_SUCCESS; | |
f3ed775e DG |
355 | |
356 | error: | |
357 | return ret; | |
358 | } | |
359 | ||
360 | /* | |
9f19cc17 | 361 | * List available tracing session. List only basic information. |
f3ed775e | 362 | * |
9f19cc17 | 363 | * If session_name is NULL, all sessions are listed. |
f3ed775e | 364 | */ |
9f19cc17 | 365 | static int list_sessions(const char *session_name) |
f3ed775e | 366 | { |
9f19cc17 DG |
367 | int ret, count, i; |
368 | unsigned int session_found = 0; | |
369 | struct lttng_session *sessions; | |
370 | ||
371 | count = lttng_list_sessions(&sessions); | |
372 | DBG("Session count %d", count); | |
373 | if (count < 0) { | |
374 | ret = count; | |
375 | goto error; | |
376 | } | |
377 | ||
378 | if (session_name == NULL) { | |
379 | MSG("Available tracing sessions:"); | |
380 | } | |
381 | ||
382 | for (i = 0; i < count; i++) { | |
383 | if (session_name != NULL) { | |
384 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
385 | session_found = 1; | |
386 | MSG("Tracing session %s:", session_name); | |
387 | MSG("%sTrace path: %s\n", indent4, sessions[i].path); | |
388 | break; | |
389 | } | |
cd80958d | 390 | continue; |
9f19cc17 DG |
391 | } |
392 | ||
393 | MSG(" %d) %s (%s)", i + 1, sessions[i].name, sessions[i].path); | |
394 | ||
395 | if (session_found) { | |
396 | break; | |
397 | } | |
398 | } | |
399 | ||
400 | free(sessions); | |
401 | ||
402 | if (!session_found && session_name != NULL) { | |
403 | MSG("Session %s not found", session_name); | |
404 | } | |
405 | ||
406 | if (session_name == NULL) { | |
6e0de39b | 407 | MSG("\nUse lttng list <session_name> for more details"); |
9f19cc17 | 408 | } |
f3ed775e DG |
409 | |
410 | return CMD_SUCCESS; | |
411 | ||
412 | error: | |
413 | return ret; | |
414 | } | |
f3ed775e DG |
415 | |
416 | /* | |
9f19cc17 | 417 | * List available domain(s) for a session. |
f3ed775e | 418 | */ |
cd80958d | 419 | static int list_domains(void) |
f3ed775e | 420 | { |
9f19cc17 DG |
421 | int i, count, ret = CMD_SUCCESS; |
422 | struct lttng_domain *domains = NULL; | |
423 | ||
424 | MSG("Domains:\n-------------"); | |
425 | ||
cd80958d | 426 | count = lttng_list_domains(handle, &domains); |
9f19cc17 DG |
427 | if (count < 0) { |
428 | ret = count; | |
429 | goto error; | |
430 | } else if (count == 0) { | |
431 | MSG(" None"); | |
432 | goto end; | |
433 | } | |
434 | ||
435 | for (i = 0; i < count; i++) { | |
436 | switch (domains[i].type) { | |
437 | case LTTNG_DOMAIN_KERNEL: | |
438 | MSG(" - Kernel"); | |
b551a063 DG |
439 | break; |
440 | case LTTNG_DOMAIN_UST: | |
441 | MSG(" - UST global"); | |
442 | break; | |
9f19cc17 DG |
443 | default: |
444 | break; | |
445 | } | |
446 | } | |
447 | ||
448 | end: | |
449 | free(domains); | |
450 | ||
451 | error: | |
452 | return ret; | |
f3ed775e | 453 | } |
f3ed775e DG |
454 | |
455 | /* | |
9f19cc17 | 456 | * The 'list <options>' first level command |
f3ed775e DG |
457 | */ |
458 | int cmd_list(int argc, const char **argv) | |
459 | { | |
9f19cc17 | 460 | int opt, i, ret = CMD_SUCCESS; |
b551a063 | 461 | unsigned int nb_domain; |
9f19cc17 | 462 | const char *session_name; |
f3ed775e | 463 | static poptContext pc; |
9f19cc17 DG |
464 | struct lttng_domain domain; |
465 | struct lttng_domain *domains = NULL; | |
f3ed775e | 466 | |
9f19cc17 | 467 | if (argc < 1) { |
f3ed775e DG |
468 | usage(stderr); |
469 | goto end; | |
470 | } | |
471 | ||
472 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
473 | poptReadDefaultConfig(pc, 0); | |
474 | ||
475 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
476 | switch (opt) { | |
477 | case OPT_HELP: | |
478 | usage(stderr); | |
479 | goto end; | |
f3ed775e DG |
480 | default: |
481 | usage(stderr); | |
482 | ret = CMD_UNDEFINED; | |
483 | goto end; | |
484 | } | |
485 | } | |
486 | ||
b551a063 DG |
487 | if (opt_pid != 0) { |
488 | MSG("*** Userspace tracing not implemented for PID ***\n"); | |
f3ed775e DG |
489 | } |
490 | ||
9f19cc17 DG |
491 | /* Get session name (trailing argument) */ |
492 | session_name = poptGetArg(pc); | |
b551a063 | 493 | DBG2("Session name: %s", session_name); |
9f19cc17 | 494 | |
cd80958d DG |
495 | if (opt_kernel) { |
496 | domain.type = LTTNG_DOMAIN_KERNEL; | |
b551a063 DG |
497 | } else if (opt_userspace) { |
498 | DBG2("Listing userspace global domain"); | |
499 | domain.type = LTTNG_DOMAIN_UST; | |
cd80958d DG |
500 | } |
501 | ||
502 | handle = lttng_create_handle(session_name, &domain); | |
503 | if (handle == NULL) { | |
504 | goto end; | |
505 | } | |
506 | ||
9f19cc17 | 507 | if (session_name == NULL) { |
b551a063 DG |
508 | if (!opt_kernel && !opt_userspace) { |
509 | ret = list_sessions(NULL); | |
510 | if (ret < 0) { | |
511 | goto end; | |
512 | } | |
513 | } | |
9f19cc17 DG |
514 | if (opt_kernel) { |
515 | ret = list_kernel_events(); | |
516 | if (ret < 0) { | |
517 | goto end; | |
518 | } | |
b551a063 DG |
519 | } |
520 | if (opt_userspace) { | |
521 | ret = list_ust_events(); | |
9f19cc17 DG |
522 | if (ret < 0) { |
523 | goto end; | |
524 | } | |
525 | } | |
526 | } else { | |
527 | /* List session attributes */ | |
528 | ret = list_sessions(session_name); | |
529 | if (ret < 0) { | |
530 | goto end; | |
531 | } | |
532 | ||
533 | /* Domain listing */ | |
534 | if (opt_domain) { | |
cd80958d | 535 | ret = list_domains(); |
9f19cc17 DG |
536 | goto end; |
537 | } | |
538 | ||
539 | if (opt_kernel) { | |
9f19cc17 | 540 | /* Channel listing */ |
cd80958d | 541 | ret = list_channels(opt_channel); |
9f19cc17 DG |
542 | if (ret < 0) { |
543 | goto end; | |
544 | } | |
9f19cc17 DG |
545 | } else { |
546 | /* We want all domain(s) */ | |
b551a063 DG |
547 | nb_domain = lttng_list_domains(handle, &domains); |
548 | if (nb_domain < 0) { | |
549 | ret = nb_domain; | |
9f19cc17 DG |
550 | goto end; |
551 | } | |
552 | ||
b551a063 | 553 | for (i = 0; i < nb_domain; i++) { |
9f19cc17 DG |
554 | switch (domains[i].type) { |
555 | case LTTNG_DOMAIN_KERNEL: | |
556 | MSG("=== Domain: Kernel ===\n"); | |
557 | break; | |
b551a063 DG |
558 | case LTTNG_DOMAIN_UST: |
559 | MSG("=== Domain: UST global ===\n"); | |
560 | break; | |
9f19cc17 DG |
561 | default: |
562 | MSG("=== Domain: Unimplemented ===\n"); | |
563 | break; | |
564 | } | |
565 | ||
cd80958d DG |
566 | /* Clean handle before creating a new one */ |
567 | lttng_destroy_handle(handle); | |
568 | ||
569 | handle = lttng_create_handle(session_name, &domains[i]); | |
570 | if (handle == NULL) { | |
571 | goto end; | |
572 | } | |
573 | ||
574 | ret = list_channels(opt_channel); | |
9f19cc17 DG |
575 | if (ret < 0) { |
576 | goto end; | |
577 | } | |
578 | } | |
579 | } | |
f3ed775e DG |
580 | } |
581 | ||
582 | end: | |
9f19cc17 DG |
583 | if (domains) { |
584 | free(domains); | |
585 | } | |
cd80958d DG |
586 | lttng_destroy_handle(handle); |
587 | ||
f3ed775e DG |
588 | return ret; |
589 | } |