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