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