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