826cef269f088164595e30240f9fd537c1db0dbe
[lttng-tools.git] / src / bin / 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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <inttypes.h>
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "../command.h"
27
28 static int opt_userspace;
29 static int opt_kernel;
30 static char *opt_channel;
31 static int opt_domain;
32 static int opt_fields;
33 #if 0
34 /* Not implemented yet */
35 static char *opt_cmd_name;
36 static pid_t opt_pid;
37 #endif
38
39 const char *indent4 = " ";
40 const char *indent6 = " ";
41 const char *indent8 = " ";
42
43 enum {
44 OPT_HELP = 1,
45 OPT_USERSPACE,
46 OPT_LIST_OPTIONS,
47 };
48
49 static struct lttng_handle *handle;
50
51 static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
55 #if 0
56 /* Not implemented yet */
57 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
58 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
59 #else
60 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
61 #endif
62 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
63 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
64 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
65 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
66 {0, 0, 0, 0, 0, 0, 0}
67 };
68
69 /*
70 * usage
71 */
72 static void usage(FILE *ofp)
73 {
74 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp, "\n");
78 fprintf(ofp, "Without a session, -k lists available kernel events\n");
79 fprintf(ofp, "Without a session, -u lists available userspace events\n");
80 fprintf(ofp, "\n");
81 fprintf(ofp, " -h, --help Show this help\n");
82 fprintf(ofp, " --list-options Simple listing of options\n");
83 fprintf(ofp, " -k, --kernel Select kernel domain\n");
84 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
85 fprintf(ofp, " -f, --fields List event fields.\n");
86 #if 0
87 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
88 #endif
89 fprintf(ofp, "\n");
90 fprintf(ofp, "Session Options:\n");
91 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
92 fprintf(ofp, " -d, --domain List available domain(s)\n");
93 fprintf(ofp, "\n");
94 }
95
96 /*
97 * Get command line from /proc for a specific pid.
98 *
99 * On success, return an allocated string pointer to the proc cmdline.
100 * On error, return NULL.
101 */
102 static char *get_cmdline_by_pid(pid_t pid)
103 {
104 int ret;
105 FILE *fp;
106 char *cmdline = NULL;
107 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
108
109 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
110 fp = fopen(path, "r");
111 if (fp == NULL) {
112 goto end;
113 }
114
115 /* Caller must free() *cmdline */
116 cmdline = malloc(PATH_MAX);
117 ret = fread(cmdline, 1, PATH_MAX, fp);
118 if (ret < 0) {
119 perror("fread proc list");
120 }
121 fclose(fp);
122
123 end:
124 return cmdline;
125 }
126
127 static
128 const char *active_string(int value)
129 {
130 switch (value) {
131 case 0: return " [inactive]";
132 case 1: return " [active]";
133 case -1: return "";
134 default: return NULL;
135 }
136 }
137
138 static
139 const char *enabled_string(int value)
140 {
141 switch (value) {
142 case 0: return " [disabled]";
143 case 1: return " [enabled]";
144 case -1: return "";
145 default: return NULL;
146 }
147 }
148
149 static const char *loglevel_string(int value)
150 {
151 switch (value) {
152 case -1:
153 return "";
154 case LTTNG_LOGLEVEL_EMERG:
155 return "TRACE_EMERG";
156 case LTTNG_LOGLEVEL_ALERT:
157 return "TRACE_ALERT";
158 case LTTNG_LOGLEVEL_CRIT:
159 return "TRACE_CRIT";
160 case LTTNG_LOGLEVEL_ERR:
161 return "TRACE_ERR";
162 case LTTNG_LOGLEVEL_WARNING:
163 return "TRACE_WARNING";
164 case LTTNG_LOGLEVEL_NOTICE:
165 return "TRACE_NOTICE";
166 case LTTNG_LOGLEVEL_INFO:
167 return "TRACE_INFO";
168 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
169 return "TRACE_DEBUG_SYSTEM";
170 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
171 return "TRACE_DEBUG_PROGRAM";
172 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
173 return "TRACE_DEBUG_PROCESS";
174 case LTTNG_LOGLEVEL_DEBUG_MODULE:
175 return "TRACE_DEBUG_MODULE";
176 case LTTNG_LOGLEVEL_DEBUG_UNIT:
177 return "TRACE_DEBUG_UNIT";
178 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
179 return "TRACE_DEBUG_FUNCTION";
180 case LTTNG_LOGLEVEL_DEBUG_LINE:
181 return "TRACE_DEBUG_LINE";
182 case LTTNG_LOGLEVEL_DEBUG:
183 return "TRACE_DEBUG";
184 default:
185 return "<<UNKNOWN>>";
186 }
187 }
188
189 /*
190 * Pretty print single event.
191 */
192 static void print_events(struct lttng_event *event)
193 {
194 switch (event->type) {
195 case LTTNG_EVENT_TRACEPOINT:
196 {
197 if (event->loglevel != -1) {
198 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
199 indent6,
200 event->name,
201 loglevel_string(event->loglevel),
202 event->loglevel,
203 enabled_string(event->enabled));
204 } else {
205 MSG("%s%s (type: tracepoint)%s",
206 indent6,
207 event->name,
208 enabled_string(event->enabled));
209 }
210 break;
211 }
212 case LTTNG_EVENT_PROBE:
213 MSG("%s%s (type: probe)%s", indent6,
214 event->name, enabled_string(event->enabled));
215 if (event->attr.probe.addr != 0) {
216 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
217 } else {
218 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
219 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
220 }
221 break;
222 case LTTNG_EVENT_FUNCTION:
223 case LTTNG_EVENT_FUNCTION_ENTRY:
224 MSG("%s%s (type: function)%s", indent6,
225 event->name, enabled_string(event->enabled));
226 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
227 break;
228 case LTTNG_EVENT_SYSCALL:
229 MSG("%ssyscalls (type: syscall)%s", indent6,
230 enabled_string(event->enabled));
231 break;
232 case LTTNG_EVENT_NOOP:
233 MSG("%s (type: noop)%s", indent6,
234 enabled_string(event->enabled));
235 break;
236 case LTTNG_EVENT_ALL:
237 /* We should never have "all" events in list. */
238 assert(0);
239 break;
240 }
241 }
242
243 static const char *field_type(struct lttng_event_field *field)
244 {
245 switch(field->type) {
246 case LTTNG_EVENT_FIELD_INTEGER:
247 return "integer";
248 case LTTNG_EVENT_FIELD_ENUM:
249 return "enum";
250 case LTTNG_EVENT_FIELD_FLOAT:
251 return "float";
252 case LTTNG_EVENT_FIELD_STRING:
253 return "string";
254 case LTTNG_EVENT_FIELD_OTHER:
255 default: /* fall-through */
256 return "unknown";
257 }
258 }
259
260 /*
261 * Pretty print single event fields.
262 */
263 static void print_event_field(struct lttng_event_field *field)
264 {
265 if (!field->field_name[0]) {
266 return;
267 }
268 MSG("%sfield: %s (%s)", indent6, field->field_name,
269 field_type(field));
270 }
271
272 /*
273 * Ask session daemon for all user space tracepoints available.
274 */
275 static int list_ust_events(void)
276 {
277 int i, size;
278 struct lttng_domain domain;
279 struct lttng_handle *handle;
280 struct lttng_event *event_list;
281 pid_t cur_pid = 0;
282
283 memset(&domain, 0, sizeof(domain));
284
285 DBG("Getting UST tracing events");
286
287 domain.type = LTTNG_DOMAIN_UST;
288
289 handle = lttng_create_handle(NULL, &domain);
290 if (handle == NULL) {
291 goto error;
292 }
293
294 size = lttng_list_tracepoints(handle, &event_list);
295 if (size < 0) {
296 ERR("Unable to list UST events");
297 lttng_destroy_handle(handle);
298 return size;
299 }
300
301 MSG("UST events:\n-------------");
302
303 if (size == 0) {
304 MSG("None");
305 }
306
307 for (i = 0; i < size; i++) {
308 if (cur_pid != event_list[i].pid) {
309 cur_pid = event_list[i].pid;
310 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
311 }
312 print_events(&event_list[i]);
313 }
314
315 MSG("");
316
317 free(event_list);
318 lttng_destroy_handle(handle);
319
320 return CMD_SUCCESS;
321
322 error:
323 lttng_destroy_handle(handle);
324 return -1;
325 }
326
327 /*
328 * Ask session daemon for all user space tracepoint fields available.
329 */
330 static int list_ust_event_fields(void)
331 {
332 int i, size;
333 struct lttng_domain domain;
334 struct lttng_handle *handle;
335 struct lttng_event_field *event_field_list;
336 pid_t cur_pid = 0;
337 struct lttng_event cur_event;
338
339 memset(&domain, 0, sizeof(domain));
340 memset(&cur_event, 0, sizeof(cur_event));
341
342 DBG("Getting UST tracing event fields");
343
344 domain.type = LTTNG_DOMAIN_UST;
345
346 handle = lttng_create_handle(NULL, &domain);
347 if (handle == NULL) {
348 goto error;
349 }
350
351 size = lttng_list_tracepoint_fields(handle, &event_field_list);
352 if (size < 0) {
353 ERR("Unable to list UST event fields");
354 lttng_destroy_handle(handle);
355 return size;
356 }
357
358 MSG("UST events:\n-------------");
359
360 if (size == 0) {
361 MSG("None");
362 }
363
364 for (i = 0; i < size; i++) {
365 if (cur_pid != event_field_list[i].event.pid) {
366 cur_pid = event_field_list[i].event.pid;
367 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
368 }
369 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
370 print_events(&event_field_list[i].event);
371 memcpy(&cur_event, &event_field_list[i].event,
372 sizeof(cur_event));
373 }
374 print_event_field(&event_field_list[i]);
375 }
376
377 MSG("");
378
379 free(event_field_list);
380 lttng_destroy_handle(handle);
381
382 return CMD_SUCCESS;
383
384 error:
385 lttng_destroy_handle(handle);
386 return -1;
387 }
388
389 /*
390 * Ask for all trace events in the kernel and pretty print them.
391 */
392 static int list_kernel_events(void)
393 {
394 int i, size;
395 struct lttng_domain domain;
396 struct lttng_handle *handle;
397 struct lttng_event *event_list;
398
399 memset(&domain, 0, sizeof(domain));
400
401 DBG("Getting kernel tracing events");
402
403 domain.type = LTTNG_DOMAIN_KERNEL;
404
405 handle = lttng_create_handle(NULL, &domain);
406 if (handle == NULL) {
407 goto error;
408 }
409
410 size = lttng_list_tracepoints(handle, &event_list);
411 if (size < 0) {
412 ERR("Unable to list kernel events");
413 lttng_destroy_handle(handle);
414 return size;
415 }
416
417 MSG("Kernel events:\n-------------");
418
419 for (i = 0; i < size; i++) {
420 print_events(&event_list[i]);
421 }
422
423 MSG("");
424
425 free(event_list);
426
427 lttng_destroy_handle(handle);
428 return CMD_SUCCESS;
429
430 error:
431 lttng_destroy_handle(handle);
432 return -1;
433 }
434
435 /*
436 * List events of channel of session and domain.
437 */
438 static int list_events(const char *channel_name)
439 {
440 int ret, count, i;
441 struct lttng_event *events = NULL;
442
443 count = lttng_list_events(handle, channel_name, &events);
444 if (count < 0) {
445 ret = count;
446 goto error;
447 }
448
449 MSG("\n%sEvents:", indent4);
450 if (count == 0) {
451 MSG("%sNone\n", indent6);
452 goto end;
453 }
454
455 for (i = 0; i < count; i++) {
456 print_events(&events[i]);
457 }
458
459 MSG("");
460
461 end:
462 if (events) {
463 free(events);
464 }
465 ret = CMD_SUCCESS;
466
467 error:
468 return ret;
469 }
470
471 /*
472 * Pretty print channel
473 */
474 static void print_channel(struct lttng_channel *channel)
475 {
476 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
477
478 MSG("%sAttributes:", indent4);
479 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
480 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
481 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
482 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
483 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
484 switch (channel->attr.output) {
485 case LTTNG_EVENT_SPLICE:
486 MSG("%soutput: splice()", indent6);
487 break;
488 case LTTNG_EVENT_MMAP:
489 MSG("%soutput: mmap()", indent6);
490 break;
491 }
492 }
493
494 /*
495 * List channel(s) of session and domain.
496 *
497 * If channel_name is NULL, all channels are listed.
498 */
499 static int list_channels(const char *channel_name)
500 {
501 int count, i, ret = CMD_SUCCESS;
502 unsigned int chan_found = 0;
503 struct lttng_channel *channels = NULL;
504
505 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
506
507 count = lttng_list_channels(handle, &channels);
508 if (count < 0) {
509 ret = count;
510 goto error_channels;
511 } else if (count == 0) {
512 ERR("Channel %s not found", channel_name);
513 goto error;
514 }
515
516 if (channel_name == NULL) {
517 MSG("Channels:\n-------------");
518 }
519
520 for (i = 0; i < count; i++) {
521 if (channel_name != NULL) {
522 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
523 chan_found = 1;
524 } else {
525 continue;
526 }
527 }
528 print_channel(&channels[i]);
529
530 /* Listing events per channel */
531 ret = list_events(channels[i].name);
532 if (ret < 0) {
533 MSG("%s", lttng_strerror(ret));
534 }
535
536 if (chan_found) {
537 break;
538 }
539 }
540
541 if (!chan_found && channel_name != NULL) {
542 ERR("Channel %s not found", channel_name);
543 goto error;
544 }
545
546 ret = CMD_SUCCESS;
547
548 error:
549 free(channels);
550
551 error_channels:
552 return ret;
553 }
554
555 /*
556 * List available tracing session. List only basic information.
557 *
558 * If session_name is NULL, all sessions are listed.
559 */
560 static int list_sessions(const char *session_name)
561 {
562 int ret, count, i;
563 unsigned int session_found = 0;
564 struct lttng_session *sessions;
565
566 count = lttng_list_sessions(&sessions);
567 DBG("Session count %d", count);
568 if (count < 0) {
569 ret = count;
570 goto error;
571 } else if (count == 0) {
572 MSG("Currently no available tracing session");
573 goto end;
574 }
575
576 if (session_name == NULL) {
577 MSG("Available tracing sessions:");
578 }
579
580 for (i = 0; i < count; i++) {
581 if (session_name != NULL) {
582 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
583 session_found = 1;
584 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
585 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
586 break;
587 }
588 continue;
589 }
590
591 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
592 active_string(sessions[i].enabled));
593
594 if (session_found) {
595 break;
596 }
597 }
598
599 free(sessions);
600
601 if (!session_found && session_name != NULL) {
602 ERR("Session '%s' not found", session_name);
603 ret = CMD_ERROR;
604 goto error;
605 }
606
607 if (session_name == NULL) {
608 MSG("\nUse lttng list <session_name> for more details");
609 }
610
611 end:
612 return CMD_SUCCESS;
613
614 error:
615 return ret;
616 }
617
618 /*
619 * List available domain(s) for a session.
620 */
621 static int list_domains(const char *session_name)
622 {
623 int i, count, ret = CMD_SUCCESS;
624 struct lttng_domain *domains = NULL;
625
626 MSG("Domains:\n-------------");
627
628 count = lttng_list_domains(session_name, &domains);
629 if (count < 0) {
630 ret = count;
631 goto error;
632 } else if (count == 0) {
633 MSG(" None");
634 goto end;
635 }
636
637 for (i = 0; i < count; i++) {
638 switch (domains[i].type) {
639 case LTTNG_DOMAIN_KERNEL:
640 MSG(" - Kernel");
641 break;
642 case LTTNG_DOMAIN_UST:
643 MSG(" - UST global");
644 break;
645 default:
646 break;
647 }
648 }
649
650 end:
651 free(domains);
652
653 error:
654 return ret;
655 }
656
657 /*
658 * The 'list <options>' first level command
659 */
660 int cmd_list(int argc, const char **argv)
661 {
662 int opt, i, ret = CMD_SUCCESS;
663 int nb_domain;
664 const char *session_name;
665 static poptContext pc;
666 struct lttng_domain domain;
667 struct lttng_domain *domains = NULL;
668
669 memset(&domain, 0, sizeof(domain));
670
671 if (argc < 1) {
672 usage(stderr);
673 ret = CMD_ERROR;
674 goto end;
675 }
676
677 pc = poptGetContext(NULL, argc, argv, long_options, 0);
678 poptReadDefaultConfig(pc, 0);
679
680 while ((opt = poptGetNextOpt(pc)) != -1) {
681 switch (opt) {
682 case OPT_HELP:
683 usage(stdout);
684 goto end;
685 case OPT_USERSPACE:
686 opt_userspace = 1;
687 break;
688 case OPT_LIST_OPTIONS:
689 list_cmd_options(stdout, long_options);
690 goto end;
691 default:
692 usage(stderr);
693 ret = CMD_UNDEFINED;
694 goto end;
695 }
696 }
697
698 /* Get session name (trailing argument) */
699 session_name = poptGetArg(pc);
700 DBG2("Session name: %s", session_name);
701
702 if (opt_kernel) {
703 domain.type = LTTNG_DOMAIN_KERNEL;
704 } else if (opt_userspace) {
705 DBG2("Listing userspace global domain");
706 domain.type = LTTNG_DOMAIN_UST;
707 }
708
709 if (opt_kernel || opt_userspace) {
710 handle = lttng_create_handle(session_name, &domain);
711 if (handle == NULL) {
712 ret = CMD_FATAL;
713 goto end;
714 }
715 }
716
717 if (session_name == NULL) {
718 if (!opt_kernel && !opt_userspace) {
719 ret = list_sessions(NULL);
720 if (ret != 0) {
721 goto end;
722 }
723 }
724 if (opt_kernel) {
725 ret = list_kernel_events();
726 if (ret < 0) {
727 goto end;
728 }
729 }
730 if (opt_userspace) {
731 if (opt_fields) {
732 ret = list_ust_event_fields();
733 } else {
734 ret = list_ust_events();
735 }
736 if (ret < 0) {
737 goto end;
738 }
739 }
740 } else {
741 /* List session attributes */
742 ret = list_sessions(session_name);
743 if (ret != 0) {
744 goto end;
745 }
746
747 /* Domain listing */
748 if (opt_domain) {
749 ret = list_domains(session_name);
750 goto end;
751 }
752
753 if (opt_kernel) {
754 /* Channel listing */
755 ret = list_channels(opt_channel);
756 if (ret < 0) {
757 goto end;
758 }
759 } else {
760 /* We want all domain(s) */
761 nb_domain = lttng_list_domains(session_name, &domains);
762 if (nb_domain < 0) {
763 ret = nb_domain;
764 goto end;
765 }
766
767 for (i = 0; i < nb_domain; i++) {
768 switch (domains[i].type) {
769 case LTTNG_DOMAIN_KERNEL:
770 MSG("=== Domain: Kernel ===\n");
771 break;
772 case LTTNG_DOMAIN_UST:
773 MSG("=== Domain: UST global ===\n");
774 break;
775 default:
776 MSG("=== Domain: Unimplemented ===\n");
777 break;
778 }
779
780 /* Clean handle before creating a new one */
781 if (handle) {
782 lttng_destroy_handle(handle);
783 }
784
785 handle = lttng_create_handle(session_name, &domains[i]);
786 if (handle == NULL) {
787 ret = CMD_FATAL;
788 goto end;
789 }
790
791 ret = list_channels(opt_channel);
792 if (ret < 0) {
793 goto end;
794 }
795 }
796 }
797 }
798
799 end:
800 if (domains) {
801 free(domains);
802 }
803 if (handle) {
804 lttng_destroy_handle(handle);
805 }
806
807 poptFreeContext(pc);
808 return ret;
809 }
This page took 0.044339 seconds and 3 git commands to generate.