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