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