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