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