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