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