Mi: support for command list
[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 <common/mi-lttng.h>
27
28 #include "../command.h"
29
30 static int opt_userspace;
31 static int opt_kernel;
32 static int opt_jul;
33 static char *opt_channel;
34 static int opt_domain;
35 static int opt_fields;
36 #if 0
37 /* Not implemented yet */
38 static char *opt_cmd_name;
39 static pid_t opt_pid;
40 #endif
41
42 const char *indent4 = " ";
43 const char *indent6 = " ";
44 const char *indent8 = " ";
45
46 enum {
47 OPT_HELP = 1,
48 OPT_USERSPACE,
49 OPT_LIST_OPTIONS,
50 };
51
52 static struct lttng_handle *handle;
53 static struct mi_writer *writer;
54
55 static struct poptOption long_options[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
58 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
59 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
60 #if 0
61 /* Not implemented yet */
62 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
63 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
64 #else
65 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
66 #endif
67 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
68 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
69 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
70 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
71 {0, 0, 0, 0, 0, 0, 0}
72 };
73
74 /*
75 * usage
76 */
77 static void usage(FILE *ofp)
78 {
79 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
80 fprintf(ofp, "\n");
81 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
82 fprintf(ofp, "\n");
83 fprintf(ofp, "Without a session, -k lists available kernel events\n");
84 fprintf(ofp, "Without a session, -u lists available userspace events\n");
85 fprintf(ofp, "\n");
86 fprintf(ofp, " -h, --help Show this help\n");
87 fprintf(ofp, " --list-options Simple listing of options\n");
88 fprintf(ofp, " -k, --kernel Select kernel domain\n");
89 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
90 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
91 fprintf(ofp, " -f, --fields List event fields.\n");
92 #if 0
93 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
94 #endif
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Session Options:\n");
97 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
98 fprintf(ofp, " -d, --domain List available domain(s)\n");
99 fprintf(ofp, "\n");
100 }
101
102 /*
103 * Get command line from /proc for a specific pid.
104 *
105 * On success, return an allocated string pointer to the proc cmdline.
106 * On error, return NULL.
107 */
108 static char *get_cmdline_by_pid(pid_t pid)
109 {
110 int ret;
111 FILE *fp;
112 char *cmdline = NULL;
113 char path[20]; /* Can't go bigger than /proc/65535/cmdline */
114
115 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
116 fp = fopen(path, "r");
117 if (fp == NULL) {
118 goto end;
119 }
120
121 /* Caller must free() *cmdline */
122 cmdline = malloc(PATH_MAX);
123 if (!cmdline) {
124 perror("malloc cmdline");
125 goto end;
126 }
127 ret = fread(cmdline, 1, PATH_MAX, fp);
128 if (ret < 0) {
129 perror("fread proc list");
130 }
131 fclose(fp);
132
133 end:
134 return cmdline;
135 }
136
137 static
138 const char *active_string(int value)
139 {
140 switch (value) {
141 case 0: return "inactive";
142 case 1: return "active";
143 case -1: return "";
144 default: return NULL;
145 }
146 }
147
148 static const char *snapshot_string(int value)
149 {
150 switch (value) {
151 case 1:
152 return " snapshot";
153 default:
154 return "";
155 }
156 }
157
158 static
159 const char *enabled_string(int value)
160 {
161 switch (value) {
162 case 0: return " [disabled]";
163 case 1: return " [enabled]";
164 case -1: return "";
165 default: return NULL;
166 }
167 }
168
169 static
170 const char *filter_string(int value)
171 {
172 switch (value) {
173 case 1: return " [with filter]";
174 default: return "";
175 }
176 }
177
178 static
179 const char *exclusion_string(int value)
180 {
181 switch (value) {
182 case 1: return " [has exclusions]";
183 default: return "";
184 }
185 }
186
187 static const char *loglevel_jul_string(int value)
188 {
189 switch (value) {
190 case -1:
191 return "";
192 case LTTNG_LOGLEVEL_JUL_OFF:
193 return "JUL_OFF";
194 case LTTNG_LOGLEVEL_JUL_SEVERE:
195 return "JUL_SEVERE";
196 case LTTNG_LOGLEVEL_JUL_WARNING:
197 return "JUL_WARNING";
198 case LTTNG_LOGLEVEL_JUL_INFO:
199 return "JUL_INFO";
200 case LTTNG_LOGLEVEL_JUL_CONFIG:
201 return "JUL_CONFIG";
202 case LTTNG_LOGLEVEL_JUL_FINE:
203 return "JUL_FINE";
204 case LTTNG_LOGLEVEL_JUL_FINER:
205 return "JUL_FINER";
206 case LTTNG_LOGLEVEL_JUL_FINEST:
207 return "JUL_FINEST";
208 case LTTNG_LOGLEVEL_JUL_ALL:
209 return "JUL_ALL";
210 default:
211 return "<<UNKNOWN>>";
212 }
213 }
214
215 static const char *logleveltype_string(enum lttng_loglevel_type value)
216 {
217 switch (value) {
218 case LTTNG_EVENT_LOGLEVEL_ALL:
219 return ":";
220 case LTTNG_EVENT_LOGLEVEL_RANGE:
221 return " <=";
222 case LTTNG_EVENT_LOGLEVEL_SINGLE:
223 return " ==";
224 default:
225 return " <<TYPE UNKN>>";
226 }
227 }
228
229 /*
230 * Pretty print single event.
231 */
232 static void print_events(struct lttng_event *event)
233 {
234 switch (event->type) {
235 case LTTNG_EVENT_TRACEPOINT:
236 {
237 if (event->loglevel != -1) {
238 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
239 indent6,
240 event->name,
241 logleveltype_string(event->loglevel_type),
242 mi_lttng_loglevel_string(event->loglevel),
243 event->loglevel,
244 enabled_string(event->enabled),
245 exclusion_string(event->exclusion),
246 filter_string(event->filter));
247 } else {
248 MSG("%s%s (type: tracepoint)%s%s%s",
249 indent6,
250 event->name,
251 enabled_string(event->enabled),
252 exclusion_string(event->exclusion),
253 filter_string(event->filter));
254 }
255 break;
256 }
257 case LTTNG_EVENT_FUNCTION:
258 MSG("%s%s (type: function)%s%s", indent6,
259 event->name, enabled_string(event->enabled),
260 filter_string(event->filter));
261 if (event->attr.probe.addr != 0) {
262 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
263 } else {
264 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
265 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
266 }
267 break;
268 case LTTNG_EVENT_PROBE:
269 MSG("%s%s (type: probe)%s%s", indent6,
270 event->name, enabled_string(event->enabled),
271 filter_string(event->filter));
272 if (event->attr.probe.addr != 0) {
273 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
274 } else {
275 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
276 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
277 }
278 break;
279 case LTTNG_EVENT_FUNCTION_ENTRY:
280 MSG("%s%s (type: function)%s%s", indent6,
281 event->name, enabled_string(event->enabled),
282 filter_string(event->filter));
283 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
284 break;
285 case LTTNG_EVENT_SYSCALL:
286 MSG("%ssyscalls (type: syscall)%s%s", indent6,
287 enabled_string(event->enabled),
288 filter_string(event->filter));
289 break;
290 case LTTNG_EVENT_NOOP:
291 MSG("%s (type: noop)%s%s", indent6,
292 enabled_string(event->enabled),
293 filter_string(event->filter));
294 break;
295 case LTTNG_EVENT_ALL:
296 /* We should never have "all" events in list. */
297 assert(0);
298 break;
299 }
300 }
301
302 static const char *field_type(struct lttng_event_field *field)
303 {
304 switch(field->type) {
305 case LTTNG_EVENT_FIELD_INTEGER:
306 return "integer";
307 case LTTNG_EVENT_FIELD_ENUM:
308 return "enum";
309 case LTTNG_EVENT_FIELD_FLOAT:
310 return "float";
311 case LTTNG_EVENT_FIELD_STRING:
312 return "string";
313 case LTTNG_EVENT_FIELD_OTHER:
314 default: /* fall-through */
315 return "unknown";
316 }
317 }
318
319 /*
320 * Pretty print single event fields.
321 */
322 static void print_event_field(struct lttng_event_field *field)
323 {
324 if (!field->field_name[0]) {
325 return;
326 }
327 MSG("%sfield: %s (%s)%s", indent8, field->field_name,
328 field_type(field), field->nowrite ? " [no write]" : "");
329 }
330
331 /*
332 * Machine interface
333 * Jul and ust event listing
334 */
335 static int mi_list_jul_ust_events(struct lttng_event *events, int count,
336 struct lttng_domain *domain)
337 {
338 int ret, i;
339 pid_t cur_pid = 0;
340 char *cmdline = NULL;
341 int pid_element_open = 0;
342
343 /* Open domains element */
344 ret = mi_lttng_domains_open(writer);
345 if (ret) {
346 goto end;
347 }
348
349 /* Write domain */
350 ret = mi_lttng_domain(writer, domain, 1);
351 if (ret) {
352 goto end;
353 }
354
355 /* Open pids element */
356 ret = mi_lttng_pids_open(writer);
357 if (ret) {
358 goto end;
359 }
360
361 for (i = 0; i < count; i++) {
362 if (cur_pid != events[i].pid) {
363 if (pid_element_open) {
364 /* Close the previous events and pid element */
365 ret = mi_lttng_close_multi_element(writer, 2);
366 if (ret) {
367 goto end;
368 }
369 pid_element_open = 0;
370 }
371
372 cur_pid = events[i].pid;
373 cmdline = get_cmdline_by_pid(cur_pid);
374 if (!cmdline) {
375 ret = CMD_ERROR;
376 goto end;
377 }
378
379 if (!pid_element_open) {
380 /* Open and write a pid element */
381 ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
382 if (ret) {
383 goto error;
384 }
385
386 /* Open events element */
387 ret = mi_lttng_events_open(writer);
388 if (ret) {
389 goto error;
390 }
391
392 pid_element_open = 1;
393 }
394 free(cmdline);
395 }
396
397 /* Write an event */
398 ret = mi_lttng_event(writer, &events[i], 0);
399 if (ret) {
400 goto end;
401 }
402 }
403
404 /* Close pids */
405 ret = mi_lttng_writer_close_element(writer);
406 if (ret) {
407 goto end;
408 }
409
410 /* Close domain, domains */
411 ret = mi_lttng_close_multi_element(writer, 2);
412 end:
413 return ret;
414 error:
415 free(cmdline);
416 return ret;
417 }
418
419 static int list_jul_events(void)
420 {
421 int i, size, ret = CMD_SUCCESS;
422 struct lttng_domain domain;
423 struct lttng_handle *handle;
424 struct lttng_event *event_list;
425 pid_t cur_pid = 0;
426 char *cmdline = NULL;
427
428 DBG("Getting JUL tracing events");
429
430 memset(&domain, 0, sizeof(domain));
431 domain.type = LTTNG_DOMAIN_JUL;
432
433 handle = lttng_create_handle(NULL, &domain);
434 if (handle == NULL) {
435 ret = CMD_ERROR;
436 goto end;
437 }
438
439 size = lttng_list_tracepoints(handle, &event_list);
440 if (size < 0) {
441 ERR("Unable to list JUL events: %s", lttng_strerror(size));
442 ret = CMD_ERROR;
443 goto end;
444 }
445
446 if (lttng_opt_mi) {
447 /* Mi print */
448 ret = mi_list_jul_ust_events(event_list, size, &domain);
449 if (ret) {
450 ret = CMD_ERROR;
451 goto error;
452 }
453 } else {
454 /* Pretty print */
455 MSG("JUL events (Logger name):\n-------------------------");
456
457 if (size == 0) {
458 MSG("None");
459 }
460
461 for (i = 0; i < size; i++) {
462 if (cur_pid != event_list[i].pid) {
463 cur_pid = event_list[i].pid;
464 cmdline = get_cmdline_by_pid(cur_pid);
465 if (cmdline == NULL) {
466 ret = CMD_ERROR;
467 goto error;
468 }
469 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
470 free(cmdline);
471 }
472 MSG("%s- %s", indent6, event_list[i].name);
473 }
474
475 MSG("");
476 }
477
478 error:
479 free(event_list);
480 end:
481 lttng_destroy_handle(handle);
482 return ret;
483 }
484
485 /*
486 * Ask session daemon for all user space tracepoints available.
487 */
488 static int list_ust_events(void)
489 {
490 int i, size, ret = CMD_SUCCESS;
491 struct lttng_domain domain;
492 struct lttng_handle *handle;
493 struct lttng_event *event_list;
494 pid_t cur_pid = 0;
495 char *cmdline = NULL;
496
497 memset(&domain, 0, sizeof(domain));
498
499 DBG("Getting UST tracing events");
500
501 domain.type = LTTNG_DOMAIN_UST;
502
503 handle = lttng_create_handle(NULL, &domain);
504 if (handle == NULL) {
505 ret = CMD_ERROR;
506 goto end;
507 }
508
509 size = lttng_list_tracepoints(handle, &event_list);
510 if (size < 0) {
511 ERR("Unable to list UST events: %s", lttng_strerror(size));
512 ret = CMD_ERROR;
513 goto error;
514 }
515
516 if (lttng_opt_mi) {
517 /* Mi print */
518 ret = mi_list_jul_ust_events(event_list, size, &domain);
519 } else {
520 /* Pretty print */
521 MSG("UST events:\n-------------");
522
523 if (size == 0) {
524 MSG("None");
525 }
526
527 for (i = 0; i < size; i++) {
528 if (cur_pid != event_list[i].pid) {
529 cur_pid = event_list[i].pid;
530 cmdline = get_cmdline_by_pid(cur_pid);
531 if (cmdline == NULL) {
532 ret = CMD_ERROR;
533 goto error;
534 }
535 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
536 free(cmdline);
537 }
538 print_events(&event_list[i]);
539 }
540
541 MSG("");
542 }
543
544 error:
545 free(event_list);
546 end:
547 lttng_destroy_handle(handle);
548 return ret;
549 }
550
551 /*
552 * Machine interface
553 * List all ust event with their fields
554 */
555 static int mi_list_ust_event_fields(struct lttng_event_field *fields, int count,
556 struct lttng_domain *domain)
557 {
558 int ret, i;
559 pid_t cur_pid = 0;
560 char *cmdline = NULL;
561 int pid_element_open = 0;
562 int event_element_open = 0;
563 struct lttng_event cur_event;
564
565 /* Open domains element */
566 ret = mi_lttng_domains_open(writer);
567 if (ret) {
568 goto end;
569 }
570
571 /* Write domain */
572 ret = mi_lttng_domain(writer, domain, 1);
573 if (ret) {
574 goto end;
575 }
576
577 /* Open pids element */
578 ret = mi_lttng_pids_open(writer);
579 if (ret) {
580 goto end;
581 }
582
583 for (i = 0; i < count; i++) {
584 if (cur_pid != fields[i].event.pid) {
585 if (pid_element_open) {
586 if (event_element_open) {
587 /*
588 * Close the previous fields element
589 * and the previous event
590 */
591 ret = mi_lttng_close_multi_element(writer, 2);
592 if (ret) {
593 goto end;
594 }
595 event_element_open = 0;
596 }
597 /* Close the previous events, pid element */
598 ret = mi_lttng_close_multi_element(writer, 2);
599 if (ret) {
600 goto end;
601 }
602 pid_element_open = 0;
603 }
604
605 cur_pid = fields[i].event.pid;
606 cmdline = get_cmdline_by_pid(cur_pid);
607 if (!pid_element_open) {
608 /* Open and write a pid element */
609 ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
610 if (ret) {
611 goto error;
612 }
613
614 /* Open events element */
615 ret = mi_lttng_events_open(writer);
616 if (ret) {
617 goto error;
618 }
619 pid_element_open = 1;
620 }
621 free(cmdline);
622 /* Wipe current event since we are about to print a new PID. */
623 memset(&cur_event, 0, sizeof(cur_event));
624 }
625
626 if (strcmp(cur_event.name, fields[i].event.name) != 0) {
627 if (event_element_open) {
628 /* Close the previous fields element and the previous event */
629 ret = mi_lttng_close_multi_element(writer, 2);
630 if (ret) {
631 goto end;
632 }
633 event_element_open = 0;
634 }
635
636 memcpy(&cur_event, &fields[i].event,
637 sizeof(cur_event));
638
639 if (!event_element_open) {
640 /* Open and write the event */
641 ret = mi_lttng_event(writer, &cur_event, 1);
642 if (ret) {
643 goto end;
644 }
645
646 /* Open a fields element */
647 ret = mi_lttng_event_fields_open(writer);
648 if (ret) {
649 goto end;
650 }
651 event_element_open = 1;
652 }
653 }
654
655 /* Print the event_field */
656 ret = mi_lttng_event_field(writer, &fields[i]);
657 if (ret) {
658 goto end;
659 }
660 }
661
662 /* Close pids, domain, domains */
663 ret = mi_lttng_close_multi_element(writer, 3);
664 end:
665 return ret;
666 error:
667 free(cmdline);
668 return ret;
669 }
670
671 /*
672 * Ask session daemon for all user space tracepoint fields available.
673 */
674 static int list_ust_event_fields(void)
675 {
676 int i, size, ret = CMD_SUCCESS;
677 struct lttng_domain domain;
678 struct lttng_handle *handle;
679 struct lttng_event_field *event_field_list;
680 pid_t cur_pid = 0;
681 char *cmdline = NULL;
682
683 struct lttng_event cur_event;
684
685 memset(&domain, 0, sizeof(domain));
686 memset(&cur_event, 0, sizeof(cur_event));
687
688 DBG("Getting UST tracing event fields");
689
690 domain.type = LTTNG_DOMAIN_UST;
691
692 handle = lttng_create_handle(NULL, &domain);
693 if (handle == NULL) {
694 ret = CMD_ERROR;
695 goto end;
696 }
697
698 size = lttng_list_tracepoint_fields(handle, &event_field_list);
699 if (size < 0) {
700 ERR("Unable to list UST event fields: %s", lttng_strerror(size));
701 ret = CMD_ERROR;
702 goto end;
703 }
704
705 if (lttng_opt_mi) {
706 /* Mi print */
707 ret = mi_list_ust_event_fields(event_field_list, size, &domain);
708 if (ret) {
709 ret = CMD_ERROR;
710 goto error;
711 }
712 } else {
713 /* Pretty print */
714 MSG("UST events:\n-------------");
715
716 if (size == 0) {
717 MSG("None");
718 }
719
720 for (i = 0; i < size; i++) {
721 if (cur_pid != event_field_list[i].event.pid) {
722 cur_pid = event_field_list[i].event.pid;
723 cmdline = get_cmdline_by_pid(cur_pid);
724 if (cmdline == NULL) {
725 ret = CMD_ERROR;
726 goto error;
727 }
728 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
729 free(cmdline);
730 /* Wipe current event since we are about to print a new PID. */
731 memset(&cur_event, 0, sizeof(cur_event));
732 }
733 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
734 print_events(&event_field_list[i].event);
735 memcpy(&cur_event, &event_field_list[i].event,
736 sizeof(cur_event));
737 }
738 print_event_field(&event_field_list[i]);
739 }
740
741 MSG("");
742 }
743
744 error:
745 free(event_field_list);
746 end:
747 lttng_destroy_handle(handle);
748 return ret;
749 }
750
751 /*
752 * Machine interface
753 * Print a list of kernel events
754 */
755 static int mi_list_kernel_events(struct lttng_event *events, int count,
756 struct lttng_domain *domain)
757 {
758 int ret, i;
759
760 /* Open domains element */
761 ret = mi_lttng_domains_open(writer);
762 if (ret) {
763 goto end;
764 }
765
766 /* Write domain */
767 ret = mi_lttng_domain(writer, domain, 1);
768 if (ret) {
769 goto end;
770 }
771
772 /* Open events */
773 ret = mi_lttng_events_open(writer);
774 if (ret) {
775 goto end;
776 }
777
778 for (i = 0; i < count; i++) {
779 mi_lttng_event(writer, &events[i], 0);
780 if (ret) {
781 goto end;
782 }
783 }
784
785 /* close events, domain and domains */
786 ret = mi_lttng_close_multi_element(writer, 3);
787 if (ret) {
788 goto end;
789 }
790
791 end:
792 return ret;
793 }
794
795 /*
796 * Ask for all trace events in the kernel
797 */
798 static int list_kernel_events(void)
799 {
800 int i, size, ret = CMD_SUCCESS;
801 struct lttng_domain domain;
802 struct lttng_handle *handle;
803 struct lttng_event *event_list;
804
805 memset(&domain, 0, sizeof(domain));
806
807 DBG("Getting kernel tracing events");
808
809 domain.type = LTTNG_DOMAIN_KERNEL;
810
811 handle = lttng_create_handle(NULL, &domain);
812 if (handle == NULL) {
813 ret = CMD_ERROR;
814 goto error;
815 }
816
817 size = lttng_list_tracepoints(handle, &event_list);
818 if (size < 0) {
819 ERR("Unable to list kernel events: %s", lttng_strerror(size));
820 lttng_destroy_handle(handle);
821 return CMD_ERROR;
822 }
823
824 if (lttng_opt_mi) {
825 /* Mi print */
826 ret = mi_list_kernel_events(event_list, size, &domain);
827 if (ret) {
828 ret = CMD_ERROR;
829 goto end;
830 }
831 } else {
832 MSG("Kernel events:\n-------------");
833
834 for (i = 0; i < size; i++) {
835 print_events(&event_list[i]);
836 }
837
838 MSG("");
839 }
840
841 end:
842 free(event_list);
843
844 lttng_destroy_handle(handle);
845 return ret;
846
847 error:
848 lttng_destroy_handle(handle);
849 return ret;
850 }
851
852 /*
853 * Machine Interface
854 * Print a list of jul events
855 */
856 static int mi_list_session_jul_events(struct lttng_event *events, int count)
857 {
858 int ret, i;
859
860 /* Open events element */
861 ret = mi_lttng_events_open(writer);
862 if (ret) {
863 goto end;
864 }
865
866 for (i = 0; i < count; i++) {
867 ret = mi_lttng_event(writer, &events[i], 0);
868 if (ret) {
869 goto end;
870 }
871 }
872
873 /* Close events element */
874 ret = mi_lttng_writer_close_element(writer);
875
876 end:
877 return ret;
878 }
879
880 /*
881 * List JUL events for a specific session using the handle.
882 *
883 * Return CMD_SUCCESS on success else a negative value.
884 */
885 static int list_session_jul_events(void)
886 {
887 int ret = CMD_SUCCESS, count, i;
888 struct lttng_event *events = NULL;
889
890 count = lttng_list_events(handle, "", &events);
891 if (count < 0) {
892 ret = CMD_ERROR;
893 ERR("%s", lttng_strerror(count));
894 goto error;
895 }
896
897 if (lttng_opt_mi) {
898 /* Mi print */
899 ret = mi_list_session_jul_events(events, count);
900 if (ret) {
901 ret = CMD_ERROR;
902 goto end;
903 }
904 } else {
905 /* Pretty print */
906 MSG("Events (Logger name):\n---------------------");
907 if (count == 0) {
908 MSG("%sNone\n", indent6);
909 goto end;
910 }
911
912 for (i = 0; i < count; i++) {
913 MSG("%s- %s%s (loglevel%s %s)", indent4, events[i].name,
914 enabled_string(events[i].enabled),
915 logleveltype_string(events[i].loglevel_type),
916 loglevel_jul_string(events[i].loglevel));
917 }
918
919 MSG("");
920 }
921
922 end:
923 free(events);
924 error:
925 return ret;
926 }
927
928 /*
929 * Machine interface
930 * print a list of event
931 */
932 static int mi_list_events(struct lttng_event *events, int count)
933 {
934 int ret, i;
935
936 /* Open events element */
937 ret = mi_lttng_events_open(writer);
938 if (ret) {
939 goto end;
940 }
941
942 for (i = 0; i < count; i++) {
943 ret = mi_lttng_event(writer, &events[i], 0);
944 if (ret) {
945 goto end;
946 }
947 }
948
949 /* Close events element */
950 ret = mi_lttng_writer_close_element(writer);
951
952 end:
953 return ret;
954 }
955
956 /*
957 * List events of channel of session and domain.
958 */
959 static int list_events(const char *channel_name)
960 {
961 int ret = CMD_SUCCESS, count, i;
962 struct lttng_event *events = NULL;
963
964 count = lttng_list_events(handle, channel_name, &events);
965 if (count < 0) {
966 ret = CMD_ERROR;
967 ERR("%s", lttng_strerror(count));
968 goto error;
969 }
970
971 if (lttng_opt_mi) {
972 /* Mi print */
973 ret = mi_list_events(events, count);
974 if (ret) {
975 ret = CMD_ERROR;
976 goto end;
977 }
978 } else {
979 /* Pretty print */
980 MSG("\n%sEvents:", indent4);
981 if (count == 0) {
982 MSG("%sNone\n", indent6);
983 goto end;
984 }
985
986 for (i = 0; i < count; i++) {
987 print_events(&events[i]);
988 }
989
990 MSG("");
991 }
992 end:
993 free(events);
994 error:
995 return ret;
996 }
997
998 /*
999 * Pretty print channel
1000 */
1001 static void print_channel(struct lttng_channel *channel)
1002 {
1003 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
1004
1005 MSG("%sAttributes:", indent4);
1006 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
1007 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
1008 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
1009 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
1010 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
1011 MSG("%strace file count: %" PRIu64, indent6, channel->attr.tracefile_count);
1012 MSG("%strace file size (bytes): %" PRIu64, indent6, channel->attr.tracefile_size);
1013 switch (channel->attr.output) {
1014 case LTTNG_EVENT_SPLICE:
1015 MSG("%soutput: splice()", indent6);
1016 break;
1017 case LTTNG_EVENT_MMAP:
1018 MSG("%soutput: mmap()", indent6);
1019 break;
1020 }
1021 }
1022
1023 /*
1024 * Machine interface
1025 * Print a list of channel
1026 *
1027 */
1028 static int mi_list_channels(struct lttng_channel *channels, int count,
1029 const char *channel_name)
1030 {
1031 int i, ret;
1032 unsigned int chan_found = 0;
1033
1034 /* Open channels element */
1035 ret = mi_lttng_channels_open(writer);
1036 if (ret) {
1037 goto error;
1038 }
1039
1040 for (i = 0; i < count; i++) {
1041 if (channel_name != NULL) {
1042 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1043 chan_found = 1;
1044 } else {
1045 continue;
1046 }
1047 }
1048
1049 /* Write channel element and leave it open */
1050 ret = mi_lttng_channel(writer, &channels[i], 1);
1051 if (ret) {
1052 goto error;
1053 }
1054
1055 /* Listing events per channel */
1056 ret = list_events(channels[i].name);
1057 if (ret) {
1058 goto error;
1059 }
1060
1061 /* Closing the channel element we opened earlier */
1062 ret = mi_lttng_writer_close_element(writer);
1063 if (ret) {
1064 goto error;
1065 }
1066
1067 if (chan_found) {
1068 break;
1069 }
1070 }
1071
1072 /* Close channels element */
1073 ret = mi_lttng_writer_close_element(writer);
1074 if (ret) {
1075 goto error;
1076 }
1077
1078 error:
1079 return ret;
1080 }
1081
1082 /*
1083 * List channel(s) of session and domain.
1084 *
1085 * If channel_name is NULL, all channels are listed.
1086 */
1087 static int list_channels(const char *channel_name)
1088 {
1089 int count, i, ret = CMD_SUCCESS;
1090 unsigned int chan_found = 0;
1091 struct lttng_channel *channels = NULL;
1092
1093 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
1094
1095 count = lttng_list_channels(handle, &channels);
1096 if (count < 0) {
1097 switch (-count) {
1098 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
1099 if (lttng_opt_mi) {
1100 /* When printing mi this is not an error
1101 * but an empty channels element */
1102 count = 0;
1103 } else {
1104 ret = CMD_SUCCESS;
1105 WARN("No kernel channel");
1106 goto error_channels;
1107 }
1108 break;
1109 default:
1110 /* We had a real error */
1111 ret = CMD_ERROR;
1112 ERR("%s", lttng_strerror(count));
1113 goto error_channels;
1114 break;
1115 }
1116 }
1117
1118 if (lttng_opt_mi) {
1119 /* Mi print */
1120 ret = mi_list_channels(channels, count, channel_name);
1121 if (ret) {
1122 ret = CMD_ERROR;
1123 goto error;
1124 }
1125 } else {
1126 /* Pretty print */
1127 if (channel_name == NULL) {
1128 MSG("Channels:\n-------------");
1129 }
1130
1131 for (i = 0; i < count; i++) {
1132 if (channel_name != NULL) {
1133 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1134 chan_found = 1;
1135 } else {
1136 continue;
1137 }
1138 }
1139 print_channel(&channels[i]);
1140
1141 /* Listing events per channel */
1142 ret = list_events(channels[i].name);
1143 if (ret) {
1144 goto error;
1145 }
1146
1147 if (chan_found) {
1148 break;
1149 }
1150 }
1151
1152 if (!chan_found && channel_name != NULL) {
1153 ret = CMD_ERROR;
1154 ERR("Channel %s not found", channel_name);
1155 goto error;
1156 }
1157 }
1158 error:
1159 free(channels);
1160
1161 error_channels:
1162 return ret;
1163 }
1164
1165 /*
1166 * Machine interface
1167 * Find the session with session_name as name
1168 * and print his informations.
1169 */
1170 static int mi_list_session(const char *session_name,
1171 struct lttng_session *sessions, int count)
1172 {
1173 int ret, i;
1174 unsigned int session_found = 0;
1175
1176 if (session_name == NULL) {
1177 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1178 goto end;
1179 }
1180
1181 for (i = 0; i < count; i++) {
1182 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1183 /* We need to leave it open to append other informations
1184 * like domain, channel, events etc.*/
1185 session_found = 1;
1186 ret = mi_lttng_session(writer, &sessions[i], 1);
1187 if (ret) {
1188 goto end;
1189 }
1190 break;
1191 }
1192 }
1193
1194 if (!session_found) {
1195 ERR("Session '%s' not found", session_name);
1196 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1197 goto end;
1198 }
1199
1200 end:
1201 return ret;
1202 }
1203
1204 /*
1205 * Machine interface
1206 * List all availables session
1207 */
1208 static int mi_list_sessions(struct lttng_session *sessions, int count)
1209 {
1210 int ret, i;
1211
1212 /* Opening sessions element */
1213 ret = mi_lttng_sessions_open(writer);
1214 if (ret) {
1215 goto end;
1216 }
1217
1218 /* Listing sessions */
1219 for (i = 0; i < count; i++) {
1220 ret = mi_lttng_session(writer, &sessions[i], 0);
1221 if (ret) {
1222 goto end;
1223 }
1224 }
1225
1226 /* Closing sessions element */
1227 ret = mi_lttng_writer_close_element(writer);
1228 if (ret) {
1229 goto end;
1230 }
1231
1232 end:
1233 return ret;
1234 }
1235
1236 /*
1237 * List available tracing session. List only basic information.
1238 *
1239 * If session_name is NULL, all sessions are listed.
1240 */
1241 static int list_sessions(const char *session_name)
1242 {
1243 int ret = CMD_SUCCESS;
1244 int count, i;
1245 unsigned int session_found = 0;
1246 struct lttng_session *sessions;
1247
1248 count = lttng_list_sessions(&sessions);
1249 DBG("Session count %d", count);
1250 if (count < 0) {
1251 ret = CMD_ERROR;
1252 ERR("%s", lttng_strerror(count));
1253 goto end;
1254 }
1255
1256 if (lttng_opt_mi) {
1257 /* Mi */
1258 if (session_name == NULL) {
1259 /* List all session */
1260 ret = mi_list_sessions(sessions, count);
1261 } else {
1262 /* Note : this return an open session element */
1263 ret = mi_list_session(session_name, sessions, count);
1264 }
1265 if (ret) {
1266 ret = CMD_ERROR;
1267 goto error;
1268 }
1269 } else {
1270 /* Pretty print */
1271 if (count == 0) {
1272 MSG("Currently no available tracing session");
1273 ret = CMD_ERROR;
1274 goto end;
1275 }
1276
1277 if (session_name == NULL) {
1278 MSG("Available tracing sessions:");
1279 }
1280
1281
1282 for (i = 0; i < count; i++) {
1283 if (session_name != NULL) {
1284 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1285 session_found = 1;
1286 MSG("Tracing session %s: [%s%s]", session_name,
1287 active_string(sessions[i].enabled),
1288 snapshot_string(sessions[i].snapshot_mode));
1289 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
1290 break;
1291 }
1292 } else {
1293 MSG(" %d) %s (%s) [%s%s]", i + 1,
1294 sessions[i].name, sessions[i].path,
1295 active_string(sessions[i].enabled),
1296 snapshot_string(sessions[i].snapshot_mode));
1297 MSG("%sTrace path: %s", indent4, sessions[i].path);
1298 MSG("%sLive timer interval (usec): %u\n", indent4,
1299 sessions[i].live_timer_interval);
1300 break;
1301 }
1302 }
1303
1304 if (!session_found && session_name != NULL) {
1305 ERR("Session '%s' not found", session_name);
1306 ret = CMD_ERROR;
1307 goto error;
1308 }
1309
1310 if (session_name == NULL) {
1311 MSG("\nUse lttng list <session_name> for more details");
1312 }
1313 }
1314
1315 error:
1316 free(sessions);
1317 end:
1318 return ret;
1319 }
1320
1321
1322 /*
1323 * Machine Interface
1324 * list available domain(s) for a session.
1325 */
1326 static int mi_list_domains(struct lttng_domain *domains, int count)
1327 {
1328 int i, ret;
1329 /* Open domains element */
1330 ret = mi_lttng_domains_open(writer);
1331 if (ret) {
1332 goto end;
1333 }
1334
1335 for (i = 0; i < count; i++) {
1336 ret = mi_lttng_domain(writer, &domains[i] , 0);
1337 if (ret) {
1338 goto end;
1339 }
1340 }
1341
1342 /* Closing domains element */
1343 ret = mi_lttng_writer_close_element(writer);
1344 if (ret) {
1345 goto end;
1346 }
1347 end:
1348 return ret;
1349 }
1350
1351 /*
1352 * List available domain(s) for a session.
1353 */
1354 static int list_domains(const char *session_name)
1355 {
1356 int i, count, ret = CMD_SUCCESS;
1357 struct lttng_domain *domains = NULL;
1358
1359
1360 count = lttng_list_domains(session_name, &domains);
1361 if (count < 0) {
1362 ret = CMD_ERROR;
1363 ERR("%s", lttng_strerror(count));
1364 goto end;
1365 }
1366
1367 if (lttng_opt_mi) {
1368 /* Mi output */
1369 ret = mi_list_domains(domains, count);
1370 if (ret) {
1371 ret = CMD_ERROR;
1372 goto error;
1373 }
1374 } else {
1375 /* Pretty print */
1376 MSG("Domains:\n-------------");
1377 if (count == 0) {
1378 MSG(" None");
1379 goto end;
1380 }
1381
1382 for (i = 0; i < count; i++) {
1383 switch (domains[i].type) {
1384 case LTTNG_DOMAIN_KERNEL:
1385 MSG(" - Kernel");
1386 break;
1387 case LTTNG_DOMAIN_UST:
1388 MSG(" - UST global");
1389 break;
1390 case LTTNG_DOMAIN_JUL:
1391 MSG(" - JUL (Java Util Logging)");
1392 break;
1393 default:
1394 break;
1395 }
1396 }
1397 }
1398
1399 error:
1400 free(domains);
1401
1402 end:
1403 return ret;
1404 }
1405
1406 /*
1407 * The 'list <options>' first level command
1408 */
1409 int cmd_list(int argc, const char **argv)
1410 {
1411 int opt, ret = CMD_SUCCESS;
1412 const char *session_name;
1413 static poptContext pc;
1414 struct lttng_domain domain;
1415 struct lttng_domain *domains = NULL;
1416
1417 memset(&domain, 0, sizeof(domain));
1418
1419 if (argc < 1) {
1420 usage(stderr);
1421 ret = CMD_ERROR;
1422 goto end;
1423 }
1424
1425 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1426 poptReadDefaultConfig(pc, 0);
1427
1428 while ((opt = poptGetNextOpt(pc)) != -1) {
1429 switch (opt) {
1430 case OPT_HELP:
1431 usage(stdout);
1432 goto end;
1433 case OPT_USERSPACE:
1434 opt_userspace = 1;
1435 break;
1436 case OPT_LIST_OPTIONS:
1437 list_cmd_options(stdout, long_options);
1438 goto end;
1439 default:
1440 usage(stderr);
1441 ret = CMD_UNDEFINED;
1442 goto end;
1443 }
1444 }
1445
1446 /* Mi check */
1447 if (lttng_opt_mi) {
1448 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1449 if (!writer) {
1450 ret = CMD_ERROR;
1451 goto end;
1452 }
1453
1454 /* Open command element */
1455 ret = mi_lttng_writer_command_open(writer,
1456 mi_lttng_element_command_list);
1457 if (ret) {
1458 ret = CMD_ERROR;
1459 goto end;
1460 }
1461
1462 /* Open output element */
1463 ret = mi_lttng_writer_open_element(writer,
1464 mi_lttng_element_command_output);
1465 if (ret) {
1466 ret = CMD_ERROR;
1467 goto end;
1468 }
1469 }
1470
1471 /* Get session name (trailing argument) */
1472 session_name = poptGetArg(pc);
1473 DBG2("Session name: %s", session_name);
1474
1475 if (opt_kernel) {
1476 domain.type = LTTNG_DOMAIN_KERNEL;
1477 } else if (opt_userspace) {
1478 DBG2("Listing userspace global domain");
1479 domain.type = LTTNG_DOMAIN_UST;
1480 } else if (opt_jul) {
1481 DBG2("Listing JUL domain");
1482 domain.type = LTTNG_DOMAIN_JUL;
1483 }
1484
1485 if (opt_kernel || opt_userspace || opt_jul) {
1486 handle = lttng_create_handle(session_name, &domain);
1487 if (handle == NULL) {
1488 ret = CMD_FATAL;
1489 goto end;
1490 }
1491 }
1492
1493 if (session_name == NULL) {
1494 if (!opt_kernel && !opt_userspace && !opt_jul) {
1495 ret = list_sessions(NULL);
1496 if (ret) {
1497 goto end;
1498 }
1499 }
1500 if (opt_kernel) {
1501 ret = list_kernel_events();
1502 if (ret) {
1503 goto end;
1504 }
1505 }
1506 if (opt_userspace) {
1507 if (opt_fields) {
1508 ret = list_ust_event_fields();
1509 } else {
1510 ret = list_ust_events();
1511 }
1512 if (ret) {
1513 goto end;
1514 }
1515 }
1516 if (opt_jul) {
1517 ret = list_jul_events();
1518 if (ret) {
1519 goto end;
1520 }
1521 }
1522 } else {
1523 /* List session attributes */
1524 if (lttng_opt_mi) {
1525 /* Open element sessions
1526 * Present for xml consistency */
1527 ret = mi_lttng_sessions_open(writer);
1528 if (ret) {
1529 goto end;
1530 }
1531 }
1532 /* MI: the ouptut of list_sessions is an unclosed session element */
1533 ret = list_sessions(session_name);
1534 if (ret) {
1535 goto end;
1536 }
1537
1538 /* Domain listing */
1539 if (opt_domain) {
1540 ret = list_domains(session_name);
1541 goto end;
1542 }
1543
1544 /* Channel listing */
1545 if (opt_kernel || opt_userspace) {
1546 if (lttng_opt_mi) {
1547 /* Add of domains and domain element for xml
1548 * consistency and validation
1549 */
1550 ret = mi_lttng_domains_open(writer);
1551 if (ret) {
1552 goto end;
1553 }
1554
1555 /* Open domain and leave it open for
1556 * nested channels printing */
1557 ret = mi_lttng_domain(writer, &domain, 1);
1558 if (ret) {
1559 goto end;
1560 }
1561
1562 }
1563
1564 ret = list_channels(opt_channel);
1565 if (ret) {
1566 goto end;
1567 }
1568
1569 if (lttng_opt_mi) {
1570 /* Close domain and domain element */
1571 ret = mi_lttng_close_multi_element(writer, 2);
1572 }
1573 if (ret) {
1574 goto end;
1575 }
1576
1577
1578 } else {
1579 int i, nb_domain;
1580
1581 /* We want all domain(s) */
1582 nb_domain = lttng_list_domains(session_name, &domains);
1583 if (nb_domain < 0) {
1584 ret = CMD_ERROR;
1585 ERR("%s", lttng_strerror(nb_domain));
1586 goto end;
1587 }
1588
1589 if (lttng_opt_mi) {
1590 ret = mi_lttng_domains_open(writer);
1591 if (ret) {
1592 ret = CMD_ERROR;
1593 goto end;
1594 }
1595 }
1596
1597 for (i = 0; i < nb_domain; i++) {
1598 switch (domains[i].type) {
1599 case LTTNG_DOMAIN_KERNEL:
1600 MSG("=== Domain: Kernel ===\n");
1601 break;
1602 case LTTNG_DOMAIN_UST:
1603 MSG("=== Domain: UST global ===\n");
1604 MSG("Buffer type: %s\n",
1605 domains[i].buf_type ==
1606 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
1607 break;
1608 case LTTNG_DOMAIN_JUL:
1609 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1610 break;
1611 default:
1612 MSG("=== Domain: Unimplemented ===\n");
1613 break;
1614 }
1615
1616 if (lttng_opt_mi) {
1617 ret = mi_lttng_domain(writer, &domains[i], 1);
1618 if (ret) {
1619 ret = CMD_ERROR;
1620 goto end;
1621 }
1622 }
1623
1624 /* Clean handle before creating a new one */
1625 if (handle) {
1626 lttng_destroy_handle(handle);
1627 }
1628
1629 handle = lttng_create_handle(session_name, &domains[i]);
1630 if (handle == NULL) {
1631 ret = CMD_FATAL;
1632 goto end;
1633 }
1634
1635 if (domains[i].type == LTTNG_DOMAIN_JUL) {
1636 ret = list_session_jul_events();
1637 if (ret) {
1638 goto end;
1639 }
1640 continue;
1641 }
1642
1643 ret = list_channels(opt_channel);
1644 if (ret) {
1645 goto end;
1646 }
1647
1648 if (lttng_opt_mi) {
1649 /* Close domain element */
1650 ret = mi_lttng_writer_close_element(writer);
1651 if (ret) {
1652 ret = CMD_ERROR;
1653 goto end;
1654 }
1655 }
1656
1657 }
1658 if (lttng_opt_mi) {
1659 /* Close the domains, session and sessions element */
1660 ret = mi_lttng_close_multi_element(writer, 3);
1661 if (ret) {
1662 ret = CMD_ERROR;
1663 goto end;
1664 }
1665 }
1666 }
1667 }
1668
1669 /* Mi closing */
1670 if (lttng_opt_mi) {
1671 /* Close output element */
1672 ret = mi_lttng_writer_close_element(writer);
1673 if (ret) {
1674 ret = CMD_ERROR;
1675 goto end;
1676 }
1677
1678 /* Command element close */
1679 ret = mi_lttng_writer_command_close(writer);
1680 if (ret) {
1681 ret = CMD_ERROR;
1682 goto end;
1683 }
1684 }
1685 end:
1686 /* Mi clean-up */
1687 if (writer && mi_lttng_writer_destroy(writer)) {
1688 /* Preserve original error code */
1689 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1690 }
1691
1692 free(domains);
1693 if (handle) {
1694 lttng_destroy_handle(handle);
1695 }
1696
1697 poptFreeContext(pc);
1698 return ret;
1699 }
This page took 0.102476 seconds and 4 git commands to generate.