418fccf7c9102387b0c7ede6a74c36ed6d569634
[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
649 /* Close the previous field element and event. */
650 ret = mi_lttng_close_multi_element(writer, 2);
651 if (ret) {
652 goto end;
653 }
654 event_element_open = 0;
655 }
656 /* Close the previous events, pid element */
657 ret = mi_lttng_close_multi_element(writer, 2);
658 if (ret) {
659 goto end;
660 }
661 pid_element_open = 0;
662 }
663
664 cur_pid = fields[i].event.pid;
665 cmdline = get_cmdline_by_pid(cur_pid);
666 if (!pid_element_open) {
667 /* Open and write a pid element */
668 ret = mi_lttng_pid(writer, cur_pid, cmdline, 1);
669 if (ret) {
670 goto error;
671 }
672
673 /* Open events element */
674 ret = mi_lttng_events_open(writer);
675 if (ret) {
676 goto error;
677 }
678 pid_element_open = 1;
679 }
680 free(cmdline);
681 /* Wipe current event since we are about to print a new PID. */
682 memset(&cur_event, 0, sizeof(cur_event));
683 }
684
685 if (strcmp(cur_event.name, fields[i].event.name) != 0) {
686 if (event_element_open) {
687 /* Close the previous fields element and the previous event */
688 ret = mi_lttng_close_multi_element(writer, 2);
689 if (ret) {
690 goto end;
691 }
692 event_element_open = 0;
693 }
694
695 memcpy(&cur_event, &fields[i].event,
696 sizeof(cur_event));
697
698 if (!event_element_open) {
699 /* Open and write the event */
700 ret = mi_lttng_event(writer, &cur_event, 1,
701 handle->domain.type);
702 if (ret) {
703 goto end;
704 }
705
706 /* Open a fields element */
707 ret = mi_lttng_event_fields_open(writer);
708 if (ret) {
709 goto end;
710 }
711 event_element_open = 1;
712 }
713 }
714
715 /* Print the event_field */
716 ret = mi_lttng_event_field(writer, &fields[i]);
717 if (ret) {
718 goto end;
719 }
720 }
721
722 /* Close pid, domain, domains */
723 ret = mi_lttng_close_multi_element(writer, 3);
724 end:
725 return ret;
726 error:
727 free(cmdline);
728 return ret;
729 }
730
731 /*
732 * Ask session daemon for all user space tracepoint fields available.
733 */
734 static int list_ust_event_fields(void)
735 {
736 int i, size, ret = CMD_SUCCESS;
737 struct lttng_domain domain;
738 struct lttng_handle *handle;
739 struct lttng_event_field *event_field_list;
740 pid_t cur_pid = 0;
741 char *cmdline = NULL;
742
743 struct lttng_event cur_event;
744
745 memset(&domain, 0, sizeof(domain));
746 memset(&cur_event, 0, sizeof(cur_event));
747
748 DBG("Getting UST tracing event fields");
749
750 domain.type = LTTNG_DOMAIN_UST;
751
752 handle = lttng_create_handle(NULL, &domain);
753 if (handle == NULL) {
754 ret = CMD_ERROR;
755 goto end;
756 }
757
758 size = lttng_list_tracepoint_fields(handle, &event_field_list);
759 if (size < 0) {
760 ERR("Unable to list UST event fields: %s", lttng_strerror(size));
761 ret = CMD_ERROR;
762 goto end;
763 }
764
765 if (lttng_opt_mi) {
766 /* Mi print */
767 ret = mi_list_ust_event_fields(event_field_list, size, &domain);
768 if (ret) {
769 ret = CMD_ERROR;
770 goto error;
771 }
772 } else {
773 /* Pretty print */
774 MSG("UST events:\n-------------");
775
776 if (size == 0) {
777 MSG("None");
778 }
779
780 for (i = 0; i < size; i++) {
781 if (cur_pid != event_field_list[i].event.pid) {
782 cur_pid = event_field_list[i].event.pid;
783 cmdline = get_cmdline_by_pid(cur_pid);
784 if (cmdline == NULL) {
785 ret = CMD_ERROR;
786 goto error;
787 }
788 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
789 free(cmdline);
790 /* Wipe current event since we are about to print a new PID. */
791 memset(&cur_event, 0, sizeof(cur_event));
792 }
793 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
794 print_events(&event_field_list[i].event);
795 memcpy(&cur_event, &event_field_list[i].event,
796 sizeof(cur_event));
797 }
798 print_event_field(&event_field_list[i]);
799 }
800
801 MSG("");
802 }
803
804 error:
805 free(event_field_list);
806 end:
807 lttng_destroy_handle(handle);
808 return ret;
809 }
810
811 /*
812 * Machine interface
813 * Print a list of kernel events
814 */
815 static int mi_list_kernel_events(struct lttng_event *events, int count,
816 struct lttng_domain *domain)
817 {
818 int ret, i;
819
820 /* Open domains element */
821 ret = mi_lttng_domains_open(writer);
822 if (ret) {
823 goto end;
824 }
825
826 /* Write domain */
827 ret = mi_lttng_domain(writer, domain, 1);
828 if (ret) {
829 goto end;
830 }
831
832 /* Open events */
833 ret = mi_lttng_events_open(writer);
834 if (ret) {
835 goto end;
836 }
837
838 for (i = 0; i < count; i++) {
839 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
840 if (ret) {
841 goto end;
842 }
843 }
844
845 /* close events, domain and domains */
846 ret = mi_lttng_close_multi_element(writer, 3);
847 if (ret) {
848 goto end;
849 }
850
851 end:
852 return ret;
853 }
854
855 /*
856 * Ask for all trace events in the kernel
857 */
858 static int list_kernel_events(void)
859 {
860 int i, size, ret = CMD_SUCCESS;
861 struct lttng_domain domain;
862 struct lttng_handle *handle;
863 struct lttng_event *event_list;
864
865 memset(&domain, 0, sizeof(domain));
866
867 DBG("Getting kernel tracing events");
868
869 domain.type = LTTNG_DOMAIN_KERNEL;
870
871 handle = lttng_create_handle(NULL, &domain);
872 if (handle == NULL) {
873 ret = CMD_ERROR;
874 goto error;
875 }
876
877 size = lttng_list_tracepoints(handle, &event_list);
878 if (size < 0) {
879 ERR("Unable to list kernel events: %s", lttng_strerror(size));
880 lttng_destroy_handle(handle);
881 return CMD_ERROR;
882 }
883
884 if (lttng_opt_mi) {
885 /* Mi print */
886 ret = mi_list_kernel_events(event_list, size, &domain);
887 if (ret) {
888 ret = CMD_ERROR;
889 goto end;
890 }
891 } else {
892 MSG("Kernel events:\n-------------");
893
894 for (i = 0; i < size; i++) {
895 print_events(&event_list[i]);
896 }
897
898 MSG("");
899 }
900
901 end:
902 free(event_list);
903
904 lttng_destroy_handle(handle);
905 return ret;
906
907 error:
908 lttng_destroy_handle(handle);
909 return ret;
910 }
911
912 /*
913 * Machine interface
914 * Print a list of system calls.
915 */
916 static int mi_list_syscalls(struct lttng_event *events, int count)
917 {
918 int ret, i;
919
920 /* Open events */
921 ret = mi_lttng_events_open(writer);
922 if (ret) {
923 goto end;
924 }
925
926 for (i = 0; i < count; i++) {
927 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
928 if (ret) {
929 goto end;
930 }
931 }
932
933 /* Close events. */
934 ret = mi_lttng_writer_close_element(writer);
935 if (ret) {
936 goto end;
937 }
938
939 end:
940 return ret;
941 }
942
943 /*
944 * Ask for kernel system calls.
945 */
946 static int list_syscalls(void)
947 {
948 int i, size, ret = CMD_SUCCESS;
949 struct lttng_event *event_list;
950
951 DBG("Getting kernel system call events");
952
953 size = lttng_list_syscalls(&event_list);
954 if (size < 0) {
955 ERR("Unable to list system calls: %s", lttng_strerror(size));
956 ret = CMD_ERROR;
957 goto error;
958 }
959
960 if (lttng_opt_mi) {
961 /* Mi print */
962 ret = mi_list_syscalls(event_list, size);
963 if (ret) {
964 ret = CMD_ERROR;
965 goto end;
966 }
967 } else {
968 MSG("System calls:\n-------------");
969
970 for (i = 0; i < size; i++) {
971 print_events(&event_list[i]);
972 }
973
974 MSG("");
975 }
976
977 end:
978 free(event_list);
979 return ret;
980
981 error:
982 return ret;
983 }
984
985 /*
986 * Machine Interface
987 * Print a list of agent events
988 */
989 static int mi_list_session_agent_events(struct lttng_event *events, int count)
990 {
991 int ret, i;
992
993 /* Open events element */
994 ret = mi_lttng_events_open(writer);
995 if (ret) {
996 goto end;
997 }
998
999 for (i = 0; i < count; i++) {
1000 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1001 if (ret) {
1002 goto end;
1003 }
1004 }
1005
1006 /* Close events element */
1007 ret = mi_lttng_writer_close_element(writer);
1008
1009 end:
1010 return ret;
1011 }
1012
1013 /*
1014 * List agent events for a specific session using the handle.
1015 *
1016 * Return CMD_SUCCESS on success else a negative value.
1017 */
1018 static int list_session_agent_events(void)
1019 {
1020 int ret = CMD_SUCCESS, count, i;
1021 struct lttng_event *events = NULL;
1022
1023 count = lttng_list_events(handle, "", &events);
1024 if (count < 0) {
1025 ret = CMD_ERROR;
1026 ERR("%s", lttng_strerror(count));
1027 goto error;
1028 }
1029
1030 if (lttng_opt_mi) {
1031 /* Mi print */
1032 ret = mi_list_session_agent_events(events, count);
1033 if (ret) {
1034 ret = CMD_ERROR;
1035 goto end;
1036 }
1037 } else {
1038 /* Pretty print */
1039 MSG("Events (Logger name):\n---------------------");
1040 if (count == 0) {
1041 MSG("%sNone\n", indent6);
1042 goto end;
1043 }
1044
1045 for (i = 0; i < count; i++) {
1046 const char *filter_str;
1047 char *filter_msg = NULL;
1048 struct lttng_event *event = &events[i];
1049
1050 ret = lttng_event_get_filter_expression(event,
1051 &filter_str);
1052 if (ret) {
1053 filter_msg = strdup(" [failed to retrieve filter]");
1054 } else if (filter_str) {
1055 const char * const filter_fmt =
1056 " [filter: '%s']";
1057
1058 filter_msg = malloc(strlen(filter_str) +
1059 strlen(filter_fmt) + 1);
1060 if (filter_msg) {
1061 sprintf(filter_msg, filter_fmt,
1062 filter_str);
1063 }
1064 }
1065
1066 if (event->loglevel_type !=
1067 LTTNG_EVENT_LOGLEVEL_ALL) {
1068 MSG("%s- %s%s (loglevel%s %s)%s", indent4,
1069 event->name,
1070 enabled_string(event->enabled),
1071 logleveltype_string(
1072 event->loglevel_type),
1073 mi_lttng_loglevel_string(
1074 event->loglevel,
1075 handle->domain.type),
1076 safe_string(filter_msg));
1077 } else {
1078 MSG("%s- %s%s%s", indent4, event->name,
1079 enabled_string(event->enabled),
1080 safe_string(filter_msg));
1081 }
1082 free(filter_msg);
1083 }
1084
1085 MSG("");
1086 }
1087
1088 end:
1089 free(events);
1090 error:
1091 return ret;
1092 }
1093
1094 /*
1095 * Machine interface
1096 * print a list of event
1097 */
1098 static int mi_list_events(struct lttng_event *events, int count)
1099 {
1100 int ret, i;
1101
1102 /* Open events element */
1103 ret = mi_lttng_events_open(writer);
1104 if (ret) {
1105 goto end;
1106 }
1107
1108 for (i = 0; i < count; i++) {
1109 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1110 if (ret) {
1111 goto end;
1112 }
1113 }
1114
1115 /* Close events element */
1116 ret = mi_lttng_writer_close_element(writer);
1117
1118 end:
1119 return ret;
1120 }
1121
1122 /*
1123 * List events of channel of session and domain.
1124 */
1125 static int list_events(const char *channel_name)
1126 {
1127 int ret = CMD_SUCCESS, count, i;
1128 struct lttng_event *events = NULL;
1129
1130 count = lttng_list_events(handle, channel_name, &events);
1131 if (count < 0) {
1132 ret = CMD_ERROR;
1133 ERR("%s", lttng_strerror(count));
1134 goto error;
1135 }
1136
1137 if (lttng_opt_mi) {
1138 /* Mi print */
1139 ret = mi_list_events(events, count);
1140 if (ret) {
1141 ret = CMD_ERROR;
1142 goto end;
1143 }
1144 } else {
1145 /* Pretty print */
1146 MSG("\n%sEvents:", indent4);
1147 if (count == 0) {
1148 MSG("%sNone\n", indent6);
1149 goto end;
1150 }
1151
1152 for (i = 0; i < count; i++) {
1153 print_events(&events[i]);
1154 }
1155
1156 MSG("");
1157 }
1158 end:
1159 free(events);
1160 error:
1161 return ret;
1162 }
1163
1164 /*
1165 * Pretty print channel
1166 */
1167 static void print_channel(struct lttng_channel *channel)
1168 {
1169 int ret;
1170 uint64_t discarded_events, lost_packets;
1171
1172 ret = lttng_channel_get_discarded_event_count(channel,
1173 &discarded_events);
1174 if (ret) {
1175 ERR("Failed to retrieve discarded event count of channel");
1176 return;
1177 }
1178
1179 ret = lttng_channel_get_lost_packet_count(channel,
1180 &lost_packets);
1181 if (ret) {
1182 ERR("Failed to retrieve lost packet count of channel");
1183 return;
1184 }
1185
1186 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
1187
1188 MSG("%sAttributes:", indent4);
1189 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
1190 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
1191 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
1192 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
1193 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
1194 MSG("%strace file count: %" PRIu64, indent6, channel->attr.tracefile_count);
1195 MSG("%strace file size (bytes): %" PRIu64, indent6, channel->attr.tracefile_size);
1196 MSG("%sdiscarded events: %" PRIu64, indent6, discarded_events);
1197 MSG("%slost packets: %" PRIu64, indent6, lost_packets);
1198 switch (channel->attr.output) {
1199 case LTTNG_EVENT_SPLICE:
1200 MSG("%soutput: splice()", indent6);
1201 break;
1202 case LTTNG_EVENT_MMAP:
1203 MSG("%soutput: mmap()", indent6);
1204 break;
1205 }
1206 }
1207
1208 /*
1209 * Machine interface
1210 * Print a list of channel
1211 *
1212 */
1213 static int mi_list_channels(struct lttng_channel *channels, int count,
1214 const char *channel_name)
1215 {
1216 int i, ret;
1217 unsigned int chan_found = 0;
1218
1219 /* Open channels element */
1220 ret = mi_lttng_channels_open(writer);
1221 if (ret) {
1222 goto error;
1223 }
1224
1225 for (i = 0; i < count; i++) {
1226 if (channel_name != NULL) {
1227 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1228 chan_found = 1;
1229 } else {
1230 continue;
1231 }
1232 }
1233
1234 /* Write channel element and leave it open */
1235 ret = mi_lttng_channel(writer, &channels[i], 1);
1236 if (ret) {
1237 goto error;
1238 }
1239
1240 /* Listing events per channel */
1241 ret = list_events(channels[i].name);
1242 if (ret) {
1243 goto error;
1244 }
1245
1246 /* Closing the channel element we opened earlier */
1247 ret = mi_lttng_writer_close_element(writer);
1248 if (ret) {
1249 goto error;
1250 }
1251
1252 if (chan_found) {
1253 break;
1254 }
1255 }
1256
1257 /* Close channels element */
1258 ret = mi_lttng_writer_close_element(writer);
1259 if (ret) {
1260 goto error;
1261 }
1262
1263 error:
1264 return ret;
1265 }
1266
1267 /*
1268 * List channel(s) of session and domain.
1269 *
1270 * If channel_name is NULL, all channels are listed.
1271 */
1272 static int list_channels(const char *channel_name)
1273 {
1274 int count, i, ret = CMD_SUCCESS;
1275 unsigned int chan_found = 0;
1276 struct lttng_channel *channels = NULL;
1277
1278 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
1279
1280 count = lttng_list_channels(handle, &channels);
1281 if (count < 0) {
1282 switch (-count) {
1283 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
1284 if (lttng_opt_mi) {
1285 /* When printing mi this is not an error
1286 * but an empty channels element */
1287 count = 0;
1288 } else {
1289 ret = CMD_SUCCESS;
1290 WARN("No kernel channel");
1291 goto error_channels;
1292 }
1293 break;
1294 default:
1295 /* We had a real error */
1296 ret = CMD_ERROR;
1297 ERR("%s", lttng_strerror(count));
1298 goto error_channels;
1299 break;
1300 }
1301 }
1302
1303 if (lttng_opt_mi) {
1304 /* Mi print */
1305 ret = mi_list_channels(channels, count, channel_name);
1306 if (ret) {
1307 ret = CMD_ERROR;
1308 goto error;
1309 }
1310 } else {
1311 /* Pretty print */
1312 if (count) {
1313 MSG("Channels:\n-------------");
1314 }
1315
1316 for (i = 0; i < count; i++) {
1317 if (channel_name != NULL) {
1318 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1319 chan_found = 1;
1320 } else {
1321 continue;
1322 }
1323 }
1324 print_channel(&channels[i]);
1325
1326 /* Listing events per channel */
1327 ret = list_events(channels[i].name);
1328 if (ret) {
1329 goto error;
1330 }
1331
1332 if (chan_found) {
1333 break;
1334 }
1335 }
1336
1337 if (!chan_found && channel_name != NULL) {
1338 ret = CMD_ERROR;
1339 ERR("Channel %s not found", channel_name);
1340 goto error;
1341 }
1342 }
1343 error:
1344 free(channels);
1345
1346 error_channels:
1347 return ret;
1348 }
1349
1350 /*
1351 * List tracker PID(s) of session and domain.
1352 */
1353 static int list_tracker_pids(void)
1354 {
1355 int ret = 0;
1356 int enabled;
1357 int *pids = NULL;
1358 size_t nr_pids;
1359
1360 ret = lttng_list_tracker_pids(handle,
1361 &enabled, &pids, &nr_pids);
1362 if (ret) {
1363 return ret;
1364 }
1365 if (enabled) {
1366 int i;
1367 _MSG("PID tracker: [");
1368
1369 /* Mi tracker_pid element*/
1370 if (writer) {
1371 /* Open tracker_pid and targets elements */
1372 ret = mi_lttng_pid_tracker_open(writer);
1373 if (ret) {
1374 goto end;
1375 }
1376 }
1377
1378 for (i = 0; i < nr_pids; i++) {
1379 if (i) {
1380 _MSG(",");
1381 }
1382 _MSG(" %d", pids[i]);
1383
1384 /* Mi */
1385 if (writer) {
1386 ret = mi_lttng_pid_target(writer, pids[i], 0);
1387 if (ret) {
1388 goto end;
1389 }
1390 }
1391 }
1392 _MSG(" ]\n\n");
1393
1394 /* Mi close tracker_pid and targets */
1395 if (writer) {
1396 ret = mi_lttng_close_multi_element(writer,2);
1397 if (ret) {
1398 goto end;
1399 }
1400 }
1401 }
1402 end:
1403 free(pids);
1404 return ret;
1405
1406 }
1407
1408 /*
1409 * List all tracker of a domain
1410 */
1411 static int list_trackers(void)
1412 {
1413 int ret;
1414
1415 /* Trackers listing */
1416 if (lttng_opt_mi) {
1417 ret = mi_lttng_trackers_open(writer);
1418 if (ret) {
1419 goto end;
1420 }
1421 }
1422
1423 /* pid tracker */
1424 ret = list_tracker_pids();
1425 if (ret) {
1426 goto end;
1427 }
1428
1429 if (lttng_opt_mi) {
1430 /* Close trackers element */
1431 ret = mi_lttng_writer_close_element(writer);
1432 if (ret) {
1433 goto end;
1434 }
1435 }
1436
1437 end:
1438 return ret;
1439 }
1440
1441 /*
1442 * Machine interface
1443 * Find the session with session_name as name
1444 * and print his informations.
1445 */
1446 static int mi_list_session(const char *session_name,
1447 struct lttng_session *sessions, int count)
1448 {
1449 int ret, i;
1450 unsigned int session_found = 0;
1451
1452 if (session_name == NULL) {
1453 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1454 goto end;
1455 }
1456
1457 for (i = 0; i < count; i++) {
1458 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1459 /* We need to leave it open to append other informations
1460 * like domain, channel, events etc.*/
1461 session_found = 1;
1462 ret = mi_lttng_session(writer, &sessions[i], 1);
1463 if (ret) {
1464 goto end;
1465 }
1466 break;
1467 }
1468 }
1469
1470 if (!session_found) {
1471 ERR("Session '%s' not found", session_name);
1472 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1473 goto end;
1474 }
1475
1476 end:
1477 return ret;
1478 }
1479
1480 /*
1481 * Machine interface
1482 * List all availables session
1483 */
1484 static int mi_list_sessions(struct lttng_session *sessions, int count)
1485 {
1486 int ret, i;
1487
1488 /* Opening sessions element */
1489 ret = mi_lttng_sessions_open(writer);
1490 if (ret) {
1491 goto end;
1492 }
1493
1494 /* Listing sessions */
1495 for (i = 0; i < count; i++) {
1496 ret = mi_lttng_session(writer, &sessions[i], 0);
1497 if (ret) {
1498 goto end;
1499 }
1500 }
1501
1502 /* Closing sessions element */
1503 ret = mi_lttng_writer_close_element(writer);
1504 if (ret) {
1505 goto end;
1506 }
1507
1508 end:
1509 return ret;
1510 }
1511
1512 /*
1513 * List available tracing session. List only basic information.
1514 *
1515 * If session_name is NULL, all sessions are listed.
1516 */
1517 static int list_sessions(const char *session_name)
1518 {
1519 int ret = CMD_SUCCESS;
1520 int count, i;
1521 unsigned int session_found = 0;
1522 struct lttng_session *sessions;
1523
1524 count = lttng_list_sessions(&sessions);
1525 DBG("Session count %d", count);
1526 if (count < 0) {
1527 ret = CMD_ERROR;
1528 ERR("%s", lttng_strerror(count));
1529 goto end;
1530 }
1531
1532 if (lttng_opt_mi) {
1533 /* Mi */
1534 if (session_name == NULL) {
1535 /* List all session */
1536 ret = mi_list_sessions(sessions, count);
1537 } else {
1538 /* Note : this return an open session element */
1539 ret = mi_list_session(session_name, sessions, count);
1540 }
1541 if (ret) {
1542 ret = CMD_ERROR;
1543 goto error;
1544 }
1545 } else {
1546 /* Pretty print */
1547 if (count == 0) {
1548 MSG("Currently no available tracing session");
1549 goto end;
1550 }
1551
1552 if (session_name == NULL) {
1553 MSG("Available tracing sessions:");
1554 }
1555
1556
1557 for (i = 0; i < count; i++) {
1558 if (session_name != NULL) {
1559 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1560 session_found = 1;
1561 MSG("Tracing session %s: [%s%s]", session_name,
1562 active_string(sessions[i].enabled),
1563 snapshot_string(sessions[i].snapshot_mode));
1564 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
1565 break;
1566 }
1567 } else {
1568 MSG(" %d) %s (%s) [%s%s]", i + 1,
1569 sessions[i].name, sessions[i].path,
1570 active_string(sessions[i].enabled),
1571 snapshot_string(sessions[i].snapshot_mode));
1572 MSG("%sTrace path: %s", indent4, sessions[i].path);
1573 if (sessions[i].live_timer_interval != 0) {
1574 MSG("%sLive timer interval (usec): %u", indent4,
1575 sessions[i].live_timer_interval);
1576 }
1577 MSG("");
1578 }
1579 }
1580
1581 if (!session_found && session_name != NULL) {
1582 ERR("Session '%s' not found", session_name);
1583 ret = CMD_ERROR;
1584 goto error;
1585 }
1586
1587 if (session_name == NULL) {
1588 MSG("\nUse lttng list <session_name> for more details");
1589 }
1590 }
1591
1592 error:
1593 free(sessions);
1594 end:
1595 return ret;
1596 }
1597
1598
1599 /*
1600 * Machine Interface
1601 * list available domain(s) for a session.
1602 */
1603 static int mi_list_domains(struct lttng_domain *domains, int count)
1604 {
1605 int i, ret;
1606 /* Open domains element */
1607 ret = mi_lttng_domains_open(writer);
1608 if (ret) {
1609 goto end;
1610 }
1611
1612 for (i = 0; i < count; i++) {
1613 ret = mi_lttng_domain(writer, &domains[i] , 0);
1614 if (ret) {
1615 goto end;
1616 }
1617 }
1618
1619 /* Closing domains element */
1620 ret = mi_lttng_writer_close_element(writer);
1621 if (ret) {
1622 goto end;
1623 }
1624 end:
1625 return ret;
1626 }
1627
1628 /*
1629 * List available domain(s) for a session.
1630 */
1631 static int list_domains(const char *session_name)
1632 {
1633 int i, count, ret = CMD_SUCCESS;
1634 struct lttng_domain *domains = NULL;
1635
1636
1637 count = lttng_list_domains(session_name, &domains);
1638 if (count < 0) {
1639 ret = CMD_ERROR;
1640 ERR("%s", lttng_strerror(count));
1641 goto end;
1642 }
1643
1644 if (lttng_opt_mi) {
1645 /* Mi output */
1646 ret = mi_list_domains(domains, count);
1647 if (ret) {
1648 ret = CMD_ERROR;
1649 goto error;
1650 }
1651 } else {
1652 /* Pretty print */
1653 MSG("Domains:\n-------------");
1654 if (count == 0) {
1655 MSG(" None");
1656 goto end;
1657 }
1658
1659 for (i = 0; i < count; i++) {
1660 switch (domains[i].type) {
1661 case LTTNG_DOMAIN_KERNEL:
1662 MSG(" - Kernel");
1663 break;
1664 case LTTNG_DOMAIN_UST:
1665 MSG(" - UST global");
1666 break;
1667 case LTTNG_DOMAIN_JUL:
1668 MSG(" - JUL (Java Util Logging)");
1669 break;
1670 case LTTNG_DOMAIN_LOG4J:
1671 MSG(" - LOG4j (Logging for Java)");
1672 break;
1673 case LTTNG_DOMAIN_PYTHON:
1674 MSG(" - Python (logging)");
1675 break;
1676 default:
1677 break;
1678 }
1679 }
1680 }
1681
1682 error:
1683 free(domains);
1684
1685 end:
1686 return ret;
1687 }
1688
1689 /*
1690 * The 'list <options>' first level command
1691 */
1692 int cmd_list(int argc, const char **argv)
1693 {
1694 int opt, ret = CMD_SUCCESS;
1695 const char *session_name;
1696 static poptContext pc;
1697 struct lttng_domain domain;
1698 struct lttng_domain *domains = NULL;
1699
1700 memset(&domain, 0, sizeof(domain));
1701
1702 if (argc < 1) {
1703 ret = CMD_ERROR;
1704 goto end;
1705 }
1706
1707 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1708 poptReadDefaultConfig(pc, 0);
1709
1710 while ((opt = poptGetNextOpt(pc)) != -1) {
1711 switch (opt) {
1712 case OPT_HELP:
1713 SHOW_HELP();
1714 goto end;
1715 case OPT_USERSPACE:
1716 opt_userspace = 1;
1717 break;
1718 case OPT_LIST_OPTIONS:
1719 list_cmd_options(stdout, long_options);
1720 goto end;
1721 default:
1722 ret = CMD_UNDEFINED;
1723 goto end;
1724 }
1725 }
1726
1727 /* Mi check */
1728 if (lttng_opt_mi) {
1729 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1730 if (!writer) {
1731 ret = CMD_ERROR;
1732 goto end;
1733 }
1734
1735 /* Open command element */
1736 ret = mi_lttng_writer_command_open(writer,
1737 mi_lttng_element_command_list);
1738 if (ret) {
1739 ret = CMD_ERROR;
1740 goto end;
1741 }
1742
1743 /* Open output element */
1744 ret = mi_lttng_writer_open_element(writer,
1745 mi_lttng_element_command_output);
1746 if (ret) {
1747 ret = CMD_ERROR;
1748 goto end;
1749 }
1750 }
1751
1752 /* Get session name (trailing argument) */
1753 session_name = poptGetArg(pc);
1754 DBG2("Session name: %s", session_name);
1755
1756 if (opt_kernel) {
1757 domain.type = LTTNG_DOMAIN_KERNEL;
1758 } else if (opt_userspace) {
1759 DBG2("Listing userspace global domain");
1760 domain.type = LTTNG_DOMAIN_UST;
1761 } else if (opt_jul) {
1762 DBG2("Listing JUL domain");
1763 domain.type = LTTNG_DOMAIN_JUL;
1764 } else if (opt_log4j) {
1765 domain.type = LTTNG_DOMAIN_LOG4J;
1766 } else if (opt_python) {
1767 domain.type = LTTNG_DOMAIN_PYTHON;
1768 }
1769
1770 if (!opt_kernel && opt_syscall) {
1771 WARN("--syscall will only work with the Kernel domain (-k)");
1772 ret = CMD_ERROR;
1773 goto end;
1774 }
1775
1776 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
1777 handle = lttng_create_handle(session_name, &domain);
1778 if (handle == NULL) {
1779 ret = CMD_FATAL;
1780 goto end;
1781 }
1782 }
1783
1784 if (session_name == NULL) {
1785 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
1786 && !opt_python) {
1787 ret = list_sessions(NULL);
1788 if (ret) {
1789 goto end;
1790 }
1791 }
1792 if (opt_kernel) {
1793 if (opt_syscall) {
1794 ret = list_syscalls();
1795 if (ret) {
1796 goto end;
1797 }
1798 } else {
1799 ret = list_kernel_events();
1800 if (ret) {
1801 goto end;
1802 }
1803 }
1804 }
1805 if (opt_userspace) {
1806 if (opt_fields) {
1807 ret = list_ust_event_fields();
1808 } else {
1809 ret = list_ust_events();
1810 }
1811 if (ret) {
1812 goto end;
1813 }
1814 }
1815 if (opt_jul || opt_log4j || opt_python) {
1816 ret = list_agent_events();
1817 if (ret) {
1818 goto end;
1819 }
1820 }
1821 } else {
1822 /* List session attributes */
1823 if (lttng_opt_mi) {
1824 /* Open element sessions
1825 * Present for xml consistency */
1826 ret = mi_lttng_sessions_open(writer);
1827 if (ret) {
1828 goto end;
1829 }
1830 }
1831 /* MI: the ouptut of list_sessions is an unclosed session element */
1832 ret = list_sessions(session_name);
1833 if (ret) {
1834 goto end;
1835 }
1836
1837 /* Domain listing */
1838 if (opt_domain) {
1839 ret = list_domains(session_name);
1840 goto end;
1841 }
1842
1843 /* Channel listing */
1844 if (opt_kernel || opt_userspace) {
1845 if (lttng_opt_mi) {
1846 /* Add of domains and domain element for xml
1847 * consistency and validation
1848 */
1849 ret = mi_lttng_domains_open(writer);
1850 if (ret) {
1851 goto end;
1852 }
1853
1854 /* Open domain and leave it open for
1855 * nested channels printing */
1856 ret = mi_lttng_domain(writer, &domain, 1);
1857 if (ret) {
1858 goto end;
1859 }
1860
1861 }
1862
1863
1864 /* Trackers */
1865 ret = list_trackers();
1866 if (ret) {
1867 goto end;
1868 }
1869
1870 /* Channels */
1871 ret = list_channels(opt_channel);
1872 if (ret) {
1873 goto end;
1874 }
1875
1876 if (lttng_opt_mi) {
1877 /* Close domain and domain element */
1878 ret = mi_lttng_close_multi_element(writer, 2);
1879 }
1880 if (ret) {
1881 goto end;
1882 }
1883
1884
1885 } else {
1886 int i, nb_domain;
1887
1888 /* We want all domain(s) */
1889 nb_domain = lttng_list_domains(session_name, &domains);
1890 if (nb_domain < 0) {
1891 ret = CMD_ERROR;
1892 ERR("%s", lttng_strerror(nb_domain));
1893 goto end;
1894 }
1895
1896 if (lttng_opt_mi) {
1897 ret = mi_lttng_domains_open(writer);
1898 if (ret) {
1899 ret = CMD_ERROR;
1900 goto end;
1901 }
1902 }
1903
1904 for (i = 0; i < nb_domain; i++) {
1905 switch (domains[i].type) {
1906 case LTTNG_DOMAIN_KERNEL:
1907 MSG("=== Domain: Kernel ===\n");
1908 break;
1909 case LTTNG_DOMAIN_UST:
1910 MSG("=== Domain: UST global ===\n");
1911 MSG("Buffer type: %s\n",
1912 domains[i].buf_type ==
1913 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
1914 break;
1915 case LTTNG_DOMAIN_JUL:
1916 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1917 break;
1918 case LTTNG_DOMAIN_LOG4J:
1919 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1920 break;
1921 case LTTNG_DOMAIN_PYTHON:
1922 MSG("=== Domain: Python (logging) ===\n");
1923 break;
1924 default:
1925 MSG("=== Domain: Unimplemented ===\n");
1926 break;
1927 }
1928
1929 if (lttng_opt_mi) {
1930 ret = mi_lttng_domain(writer, &domains[i], 1);
1931 if (ret) {
1932 ret = CMD_ERROR;
1933 goto end;
1934 }
1935 }
1936
1937 /* Clean handle before creating a new one */
1938 if (handle) {
1939 lttng_destroy_handle(handle);
1940 }
1941
1942 handle = lttng_create_handle(session_name, &domains[i]);
1943 if (handle == NULL) {
1944 ret = CMD_FATAL;
1945 goto end;
1946 }
1947
1948 if (domains[i].type == LTTNG_DOMAIN_JUL ||
1949 domains[i].type == LTTNG_DOMAIN_LOG4J ||
1950 domains[i].type == LTTNG_DOMAIN_PYTHON) {
1951 ret = list_session_agent_events();
1952 if (ret) {
1953 goto end;
1954 }
1955
1956 goto next_domain;
1957 }
1958
1959 switch (domains[i].type) {
1960 case LTTNG_DOMAIN_KERNEL:
1961 case LTTNG_DOMAIN_UST:
1962 ret = list_trackers();
1963 if (ret) {
1964 goto end;
1965 }
1966 break;
1967 default:
1968 break;
1969 }
1970
1971 ret = list_channels(opt_channel);
1972 if (ret) {
1973 goto end;
1974 }
1975
1976 next_domain:
1977 if (lttng_opt_mi) {
1978 /* Close domain element */
1979 ret = mi_lttng_writer_close_element(writer);
1980 if (ret) {
1981 ret = CMD_ERROR;
1982 goto end;
1983 }
1984 }
1985
1986 }
1987 if (lttng_opt_mi) {
1988 /* Close the domains, session and sessions element */
1989 ret = mi_lttng_close_multi_element(writer, 3);
1990 if (ret) {
1991 ret = CMD_ERROR;
1992 goto end;
1993 }
1994 }
1995 }
1996 }
1997
1998 /* Mi closing */
1999 if (lttng_opt_mi) {
2000 /* Close output element */
2001 ret = mi_lttng_writer_close_element(writer);
2002 if (ret) {
2003 ret = CMD_ERROR;
2004 goto end;
2005 }
2006
2007 /* Command element close */
2008 ret = mi_lttng_writer_command_close(writer);
2009 if (ret) {
2010 ret = CMD_ERROR;
2011 goto end;
2012 }
2013 }
2014 end:
2015 /* Mi clean-up */
2016 if (writer && mi_lttng_writer_destroy(writer)) {
2017 /* Preserve original error code */
2018 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2019 }
2020
2021 free(domains);
2022 if (handle) {
2023 lttng_destroy_handle(handle);
2024 }
2025
2026 poptFreeContext(pc);
2027 return ret;
2028 }
This page took 0.129923 seconds and 4 git commands to generate.