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