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