1cce29b71d9d0919c7dd9b0342c65f0622f9a528
[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_expression(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_expression(event,
1076 &filter_str);
1077 if (ret) {
1078 filter_msg = strdup(" [failed to retrieve filter]");
1079 } else if (filter_str) {
1080 const char * const filter_fmt =
1081 " [filter: '%s']";
1082
1083 filter_msg = malloc(strlen(filter_str) +
1084 strlen(filter_fmt) + 1);
1085 if (filter_msg) {
1086 sprintf(filter_msg, filter_fmt,
1087 filter_str);
1088 }
1089 }
1090
1091 if (event->loglevel_type !=
1092 LTTNG_EVENT_LOGLEVEL_ALL) {
1093 MSG("%s- %s%s (loglevel%s %s)%s", indent4,
1094 event->name,
1095 enabled_string(event->enabled),
1096 logleveltype_string(
1097 event->loglevel_type),
1098 mi_lttng_loglevel_string(
1099 event->loglevel,
1100 handle->domain.type),
1101 safe_string(filter_msg));
1102 } else {
1103 MSG("%s- %s%s%s", indent4, event->name,
1104 enabled_string(event->enabled),
1105 safe_string(filter_msg));
1106 }
1107 free(filter_msg);
1108 }
1109
1110 MSG("");
1111 }
1112
1113 end:
1114 free(events);
1115 error:
1116 return ret;
1117 }
1118
1119 /*
1120 * Machine interface
1121 * print a list of event
1122 */
1123 static int mi_list_events(struct lttng_event *events, int count)
1124 {
1125 int ret, i;
1126
1127 /* Open events element */
1128 ret = mi_lttng_events_open(writer);
1129 if (ret) {
1130 goto end;
1131 }
1132
1133 for (i = 0; i < count; i++) {
1134 ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type);
1135 if (ret) {
1136 goto end;
1137 }
1138 }
1139
1140 /* Close events element */
1141 ret = mi_lttng_writer_close_element(writer);
1142
1143 end:
1144 return ret;
1145 }
1146
1147 /*
1148 * List events of channel of session and domain.
1149 */
1150 static int list_events(const char *channel_name)
1151 {
1152 int ret = CMD_SUCCESS, count, i;
1153 struct lttng_event *events = NULL;
1154
1155 count = lttng_list_events(handle, channel_name, &events);
1156 if (count < 0) {
1157 ret = CMD_ERROR;
1158 ERR("%s", lttng_strerror(count));
1159 goto error;
1160 }
1161
1162 if (lttng_opt_mi) {
1163 /* Mi print */
1164 ret = mi_list_events(events, count);
1165 if (ret) {
1166 ret = CMD_ERROR;
1167 goto end;
1168 }
1169 } else {
1170 /* Pretty print */
1171 MSG("\n%sEvents:", indent4);
1172 if (count == 0) {
1173 MSG("%sNone\n", indent6);
1174 goto end;
1175 }
1176
1177 for (i = 0; i < count; i++) {
1178 print_events(&events[i]);
1179 }
1180
1181 MSG("");
1182 }
1183 end:
1184 free(events);
1185 error:
1186 return ret;
1187 }
1188
1189 /*
1190 * Pretty print channel
1191 */
1192 static void print_channel(struct lttng_channel *channel)
1193 {
1194 int ret;
1195 uint64_t discarded_events, lost_packets;
1196
1197 ret = lttng_channel_get_discarded_event_count(channel,
1198 &discarded_events);
1199 if (ret) {
1200 ERR("Failed to retrieve discarded event count of channel");
1201 return;
1202 }
1203
1204 ret = lttng_channel_get_lost_packet_count(channel,
1205 &lost_packets);
1206 if (ret) {
1207 ERR("Failed to retrieve lost packet count of channel");
1208 return;
1209 }
1210
1211 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
1212
1213 MSG("%sAttributes:", indent4);
1214 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
1215 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
1216 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
1217 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
1218 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
1219 MSG("%strace file count: %" PRIu64, indent6, channel->attr.tracefile_count);
1220 MSG("%strace file size (bytes): %" PRIu64, indent6, channel->attr.tracefile_size);
1221 MSG("%sdiscarded events: %" PRIu64, indent6, discarded_events);
1222 MSG("%slost packets: %" PRIu64, indent6, lost_packets);
1223 switch (channel->attr.output) {
1224 case LTTNG_EVENT_SPLICE:
1225 MSG("%soutput: splice()", indent6);
1226 break;
1227 case LTTNG_EVENT_MMAP:
1228 MSG("%soutput: mmap()", indent6);
1229 break;
1230 }
1231 }
1232
1233 /*
1234 * Machine interface
1235 * Print a list of channel
1236 *
1237 */
1238 static int mi_list_channels(struct lttng_channel *channels, int count,
1239 const char *channel_name)
1240 {
1241 int i, ret;
1242 unsigned int chan_found = 0;
1243
1244 /* Open channels element */
1245 ret = mi_lttng_channels_open(writer);
1246 if (ret) {
1247 goto error;
1248 }
1249
1250 for (i = 0; i < count; i++) {
1251 if (channel_name != NULL) {
1252 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1253 chan_found = 1;
1254 } else {
1255 continue;
1256 }
1257 }
1258
1259 /* Write channel element and leave it open */
1260 ret = mi_lttng_channel(writer, &channels[i], 1);
1261 if (ret) {
1262 goto error;
1263 }
1264
1265 /* Listing events per channel */
1266 ret = list_events(channels[i].name);
1267 if (ret) {
1268 goto error;
1269 }
1270
1271 /* Closing the channel element we opened earlier */
1272 ret = mi_lttng_writer_close_element(writer);
1273 if (ret) {
1274 goto error;
1275 }
1276
1277 if (chan_found) {
1278 break;
1279 }
1280 }
1281
1282 /* Close channels element */
1283 ret = mi_lttng_writer_close_element(writer);
1284 if (ret) {
1285 goto error;
1286 }
1287
1288 error:
1289 return ret;
1290 }
1291
1292 /*
1293 * List channel(s) of session and domain.
1294 *
1295 * If channel_name is NULL, all channels are listed.
1296 */
1297 static int list_channels(const char *channel_name)
1298 {
1299 int count, i, ret = CMD_SUCCESS;
1300 unsigned int chan_found = 0;
1301 struct lttng_channel *channels = NULL;
1302
1303 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
1304
1305 count = lttng_list_channels(handle, &channels);
1306 if (count < 0) {
1307 switch (-count) {
1308 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
1309 if (lttng_opt_mi) {
1310 /* When printing mi this is not an error
1311 * but an empty channels element */
1312 count = 0;
1313 } else {
1314 ret = CMD_SUCCESS;
1315 WARN("No kernel channel");
1316 goto error_channels;
1317 }
1318 break;
1319 default:
1320 /* We had a real error */
1321 ret = CMD_ERROR;
1322 ERR("%s", lttng_strerror(count));
1323 goto error_channels;
1324 break;
1325 }
1326 }
1327
1328 if (lttng_opt_mi) {
1329 /* Mi print */
1330 ret = mi_list_channels(channels, count, channel_name);
1331 if (ret) {
1332 ret = CMD_ERROR;
1333 goto error;
1334 }
1335 } else {
1336 /* Pretty print */
1337 if (count) {
1338 MSG("Channels:\n-------------");
1339 }
1340
1341 for (i = 0; i < count; i++) {
1342 if (channel_name != NULL) {
1343 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1344 chan_found = 1;
1345 } else {
1346 continue;
1347 }
1348 }
1349 print_channel(&channels[i]);
1350
1351 /* Listing events per channel */
1352 ret = list_events(channels[i].name);
1353 if (ret) {
1354 goto error;
1355 }
1356
1357 if (chan_found) {
1358 break;
1359 }
1360 }
1361
1362 if (!chan_found && channel_name != NULL) {
1363 ret = CMD_ERROR;
1364 ERR("Channel %s not found", channel_name);
1365 goto error;
1366 }
1367 }
1368 error:
1369 free(channels);
1370
1371 error_channels:
1372 return ret;
1373 }
1374
1375 /*
1376 * List tracker PID(s) of session and domain.
1377 */
1378 static int list_tracker_pids(void)
1379 {
1380 int ret = 0;
1381 int enabled;
1382 int *pids = NULL;
1383 size_t nr_pids;
1384
1385 ret = lttng_list_tracker_pids(handle,
1386 &enabled, &pids, &nr_pids);
1387 if (ret) {
1388 return ret;
1389 }
1390 if (enabled) {
1391 int i;
1392 _MSG("PID tracker: [");
1393
1394 /* Mi tracker_pid element*/
1395 if (writer) {
1396 /* Open tracker_pid and targets elements */
1397 ret = mi_lttng_pid_tracker_open(writer);
1398 if (ret) {
1399 goto end;
1400 }
1401 }
1402
1403 for (i = 0; i < nr_pids; i++) {
1404 if (i) {
1405 _MSG(",");
1406 }
1407 _MSG(" %d", pids[i]);
1408
1409 /* Mi */
1410 if (writer) {
1411 ret = mi_lttng_pid_target(writer, pids[i], 0);
1412 if (ret) {
1413 goto end;
1414 }
1415 }
1416 }
1417 _MSG(" ]\n\n");
1418
1419 /* Mi close tracker_pid and targets */
1420 if (writer) {
1421 ret = mi_lttng_close_multi_element(writer,2);
1422 if (ret) {
1423 goto end;
1424 }
1425 }
1426 }
1427 end:
1428 free(pids);
1429 return ret;
1430
1431 }
1432
1433 /*
1434 * List all tracker of a domain
1435 */
1436 static int list_trackers(void)
1437 {
1438 int ret;
1439
1440 /* Trackers listing */
1441 if (lttng_opt_mi) {
1442 ret = mi_lttng_trackers_open(writer);
1443 if (ret) {
1444 goto end;
1445 }
1446 }
1447
1448 /* pid tracker */
1449 ret = list_tracker_pids();
1450 if (ret) {
1451 goto end;
1452 }
1453
1454 if (lttng_opt_mi) {
1455 /* Close trackers element */
1456 ret = mi_lttng_writer_close_element(writer);
1457 if (ret) {
1458 goto end;
1459 }
1460 }
1461
1462 end:
1463 return ret;
1464 }
1465
1466 /*
1467 * Machine interface
1468 * Find the session with session_name as name
1469 * and print his informations.
1470 */
1471 static int mi_list_session(const char *session_name,
1472 struct lttng_session *sessions, int count)
1473 {
1474 int ret, i;
1475 unsigned int session_found = 0;
1476
1477 if (session_name == NULL) {
1478 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1479 goto end;
1480 }
1481
1482 for (i = 0; i < count; i++) {
1483 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1484 /* We need to leave it open to append other informations
1485 * like domain, channel, events etc.*/
1486 session_found = 1;
1487 ret = mi_lttng_session(writer, &sessions[i], 1);
1488 if (ret) {
1489 goto end;
1490 }
1491 break;
1492 }
1493 }
1494
1495 if (!session_found) {
1496 ERR("Session '%s' not found", session_name);
1497 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1498 goto end;
1499 }
1500
1501 end:
1502 return ret;
1503 }
1504
1505 /*
1506 * Machine interface
1507 * List all availables session
1508 */
1509 static int mi_list_sessions(struct lttng_session *sessions, int count)
1510 {
1511 int ret, i;
1512
1513 /* Opening sessions element */
1514 ret = mi_lttng_sessions_open(writer);
1515 if (ret) {
1516 goto end;
1517 }
1518
1519 /* Listing sessions */
1520 for (i = 0; i < count; i++) {
1521 ret = mi_lttng_session(writer, &sessions[i], 0);
1522 if (ret) {
1523 goto end;
1524 }
1525 }
1526
1527 /* Closing sessions element */
1528 ret = mi_lttng_writer_close_element(writer);
1529 if (ret) {
1530 goto end;
1531 }
1532
1533 end:
1534 return ret;
1535 }
1536
1537 /*
1538 * List available tracing session. List only basic information.
1539 *
1540 * If session_name is NULL, all sessions are listed.
1541 */
1542 static int list_sessions(const char *session_name)
1543 {
1544 int ret = CMD_SUCCESS;
1545 int count, i;
1546 unsigned int session_found = 0;
1547 struct lttng_session *sessions;
1548
1549 count = lttng_list_sessions(&sessions);
1550 DBG("Session count %d", count);
1551 if (count < 0) {
1552 ret = CMD_ERROR;
1553 ERR("%s", lttng_strerror(count));
1554 goto end;
1555 }
1556
1557 if (lttng_opt_mi) {
1558 /* Mi */
1559 if (session_name == NULL) {
1560 /* List all session */
1561 ret = mi_list_sessions(sessions, count);
1562 } else {
1563 /* Note : this return an open session element */
1564 ret = mi_list_session(session_name, sessions, count);
1565 }
1566 if (ret) {
1567 ret = CMD_ERROR;
1568 goto error;
1569 }
1570 } else {
1571 /* Pretty print */
1572 if (count == 0) {
1573 MSG("Currently no available tracing session");
1574 goto end;
1575 }
1576
1577 if (session_name == NULL) {
1578 MSG("Available tracing sessions:");
1579 }
1580
1581
1582 for (i = 0; i < count; i++) {
1583 if (session_name != NULL) {
1584 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1585 session_found = 1;
1586 MSG("Tracing session %s: [%s%s]", session_name,
1587 active_string(sessions[i].enabled),
1588 snapshot_string(sessions[i].snapshot_mode));
1589 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
1590 break;
1591 }
1592 } else {
1593 MSG(" %d) %s (%s) [%s%s]", i + 1,
1594 sessions[i].name, sessions[i].path,
1595 active_string(sessions[i].enabled),
1596 snapshot_string(sessions[i].snapshot_mode));
1597 MSG("%sTrace path: %s", indent4, sessions[i].path);
1598 if (sessions[i].live_timer_interval != 0) {
1599 MSG("%sLive timer interval (usec): %u", indent4,
1600 sessions[i].live_timer_interval);
1601 }
1602 MSG("");
1603 }
1604 }
1605
1606 if (!session_found && session_name != NULL) {
1607 ERR("Session '%s' not found", session_name);
1608 ret = CMD_ERROR;
1609 goto error;
1610 }
1611
1612 if (session_name == NULL) {
1613 MSG("\nUse lttng list <session_name> for more details");
1614 }
1615 }
1616
1617 error:
1618 free(sessions);
1619 end:
1620 return ret;
1621 }
1622
1623
1624 /*
1625 * Machine Interface
1626 * list available domain(s) for a session.
1627 */
1628 static int mi_list_domains(struct lttng_domain *domains, int count)
1629 {
1630 int i, ret;
1631 /* Open domains element */
1632 ret = mi_lttng_domains_open(writer);
1633 if (ret) {
1634 goto end;
1635 }
1636
1637 for (i = 0; i < count; i++) {
1638 ret = mi_lttng_domain(writer, &domains[i] , 0);
1639 if (ret) {
1640 goto end;
1641 }
1642 }
1643
1644 /* Closing domains element */
1645 ret = mi_lttng_writer_close_element(writer);
1646 if (ret) {
1647 goto end;
1648 }
1649 end:
1650 return ret;
1651 }
1652
1653 /*
1654 * List available domain(s) for a session.
1655 */
1656 static int list_domains(const char *session_name)
1657 {
1658 int i, count, ret = CMD_SUCCESS;
1659 struct lttng_domain *domains = NULL;
1660
1661
1662 count = lttng_list_domains(session_name, &domains);
1663 if (count < 0) {
1664 ret = CMD_ERROR;
1665 ERR("%s", lttng_strerror(count));
1666 goto end;
1667 }
1668
1669 if (lttng_opt_mi) {
1670 /* Mi output */
1671 ret = mi_list_domains(domains, count);
1672 if (ret) {
1673 ret = CMD_ERROR;
1674 goto error;
1675 }
1676 } else {
1677 /* Pretty print */
1678 MSG("Domains:\n-------------");
1679 if (count == 0) {
1680 MSG(" None");
1681 goto end;
1682 }
1683
1684 for (i = 0; i < count; i++) {
1685 switch (domains[i].type) {
1686 case LTTNG_DOMAIN_KERNEL:
1687 MSG(" - Kernel");
1688 break;
1689 case LTTNG_DOMAIN_UST:
1690 MSG(" - UST global");
1691 break;
1692 case LTTNG_DOMAIN_JUL:
1693 MSG(" - JUL (Java Util Logging)");
1694 break;
1695 case LTTNG_DOMAIN_LOG4J:
1696 MSG(" - LOG4j (Logging for Java)");
1697 break;
1698 case LTTNG_DOMAIN_PYTHON:
1699 MSG(" - Python (logging)");
1700 break;
1701 default:
1702 break;
1703 }
1704 }
1705 }
1706
1707 error:
1708 free(domains);
1709
1710 end:
1711 return ret;
1712 }
1713
1714 /*
1715 * The 'list <options>' first level command
1716 */
1717 int cmd_list(int argc, const char **argv)
1718 {
1719 int opt, ret = CMD_SUCCESS;
1720 const char *session_name;
1721 static poptContext pc;
1722 struct lttng_domain domain;
1723 struct lttng_domain *domains = NULL;
1724
1725 memset(&domain, 0, sizeof(domain));
1726
1727 if (argc < 1) {
1728 usage(stderr);
1729 ret = CMD_ERROR;
1730 goto end;
1731 }
1732
1733 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1734 poptReadDefaultConfig(pc, 0);
1735
1736 while ((opt = poptGetNextOpt(pc)) != -1) {
1737 switch (opt) {
1738 case OPT_HELP:
1739 usage(stdout);
1740 goto end;
1741 case OPT_USERSPACE:
1742 opt_userspace = 1;
1743 break;
1744 case OPT_LIST_OPTIONS:
1745 list_cmd_options(stdout, long_options);
1746 goto end;
1747 default:
1748 usage(stderr);
1749 ret = CMD_UNDEFINED;
1750 goto end;
1751 }
1752 }
1753
1754 /* Mi check */
1755 if (lttng_opt_mi) {
1756 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1757 if (!writer) {
1758 ret = CMD_ERROR;
1759 goto end;
1760 }
1761
1762 /* Open command element */
1763 ret = mi_lttng_writer_command_open(writer,
1764 mi_lttng_element_command_list);
1765 if (ret) {
1766 ret = CMD_ERROR;
1767 goto end;
1768 }
1769
1770 /* Open output element */
1771 ret = mi_lttng_writer_open_element(writer,
1772 mi_lttng_element_command_output);
1773 if (ret) {
1774 ret = CMD_ERROR;
1775 goto end;
1776 }
1777 }
1778
1779 /* Get session name (trailing argument) */
1780 session_name = poptGetArg(pc);
1781 DBG2("Session name: %s", session_name);
1782
1783 if (opt_kernel) {
1784 domain.type = LTTNG_DOMAIN_KERNEL;
1785 } else if (opt_userspace) {
1786 DBG2("Listing userspace global domain");
1787 domain.type = LTTNG_DOMAIN_UST;
1788 } else if (opt_jul) {
1789 DBG2("Listing JUL domain");
1790 domain.type = LTTNG_DOMAIN_JUL;
1791 } else if (opt_log4j) {
1792 domain.type = LTTNG_DOMAIN_LOG4J;
1793 } else if (opt_python) {
1794 domain.type = LTTNG_DOMAIN_PYTHON;
1795 }
1796
1797 if (!opt_kernel && opt_syscall) {
1798 WARN("--syscall will only work with the Kernel domain (-k)");
1799 ret = CMD_ERROR;
1800 goto end;
1801 }
1802
1803 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
1804 handle = lttng_create_handle(session_name, &domain);
1805 if (handle == NULL) {
1806 ret = CMD_FATAL;
1807 goto end;
1808 }
1809 }
1810
1811 if (session_name == NULL) {
1812 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
1813 && !opt_python) {
1814 ret = list_sessions(NULL);
1815 if (ret) {
1816 goto end;
1817 }
1818 }
1819 if (opt_kernel) {
1820 if (opt_syscall) {
1821 ret = list_syscalls();
1822 if (ret) {
1823 goto end;
1824 }
1825 } else {
1826 ret = list_kernel_events();
1827 if (ret) {
1828 goto end;
1829 }
1830 }
1831 }
1832 if (opt_userspace) {
1833 if (opt_fields) {
1834 ret = list_ust_event_fields();
1835 } else {
1836 ret = list_ust_events();
1837 }
1838 if (ret) {
1839 goto end;
1840 }
1841 }
1842 if (opt_jul || opt_log4j || opt_python) {
1843 ret = list_agent_events();
1844 if (ret) {
1845 goto end;
1846 }
1847 }
1848 } else {
1849 /* List session attributes */
1850 if (lttng_opt_mi) {
1851 /* Open element sessions
1852 * Present for xml consistency */
1853 ret = mi_lttng_sessions_open(writer);
1854 if (ret) {
1855 goto end;
1856 }
1857 }
1858 /* MI: the ouptut of list_sessions is an unclosed session element */
1859 ret = list_sessions(session_name);
1860 if (ret) {
1861 goto end;
1862 }
1863
1864 /* Domain listing */
1865 if (opt_domain) {
1866 ret = list_domains(session_name);
1867 goto end;
1868 }
1869
1870 /* Channel listing */
1871 if (opt_kernel || opt_userspace) {
1872 if (lttng_opt_mi) {
1873 /* Add of domains and domain element for xml
1874 * consistency and validation
1875 */
1876 ret = mi_lttng_domains_open(writer);
1877 if (ret) {
1878 goto end;
1879 }
1880
1881 /* Open domain and leave it open for
1882 * nested channels printing */
1883 ret = mi_lttng_domain(writer, &domain, 1);
1884 if (ret) {
1885 goto end;
1886 }
1887
1888 }
1889
1890
1891 /* Trackers */
1892 ret = list_trackers();
1893 if (ret) {
1894 goto end;
1895 }
1896
1897 /* Channels */
1898 ret = list_channels(opt_channel);
1899 if (ret) {
1900 goto end;
1901 }
1902
1903 if (lttng_opt_mi) {
1904 /* Close domain and domain element */
1905 ret = mi_lttng_close_multi_element(writer, 2);
1906 }
1907 if (ret) {
1908 goto end;
1909 }
1910
1911
1912 } else {
1913 int i, nb_domain;
1914
1915 /* We want all domain(s) */
1916 nb_domain = lttng_list_domains(session_name, &domains);
1917 if (nb_domain < 0) {
1918 ret = CMD_ERROR;
1919 ERR("%s", lttng_strerror(nb_domain));
1920 goto end;
1921 }
1922
1923 if (lttng_opt_mi) {
1924 ret = mi_lttng_domains_open(writer);
1925 if (ret) {
1926 ret = CMD_ERROR;
1927 goto end;
1928 }
1929 }
1930
1931 for (i = 0; i < nb_domain; i++) {
1932 switch (domains[i].type) {
1933 case LTTNG_DOMAIN_KERNEL:
1934 MSG("=== Domain: Kernel ===\n");
1935 break;
1936 case LTTNG_DOMAIN_UST:
1937 MSG("=== Domain: UST global ===\n");
1938 MSG("Buffer type: %s\n",
1939 domains[i].buf_type ==
1940 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
1941 break;
1942 case LTTNG_DOMAIN_JUL:
1943 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1944 break;
1945 case LTTNG_DOMAIN_LOG4J:
1946 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1947 break;
1948 case LTTNG_DOMAIN_PYTHON:
1949 MSG("=== Domain: Python (logging) ===\n");
1950 break;
1951 default:
1952 MSG("=== Domain: Unimplemented ===\n");
1953 break;
1954 }
1955
1956 if (lttng_opt_mi) {
1957 ret = mi_lttng_domain(writer, &domains[i], 1);
1958 if (ret) {
1959 ret = CMD_ERROR;
1960 goto end;
1961 }
1962 }
1963
1964 /* Clean handle before creating a new one */
1965 if (handle) {
1966 lttng_destroy_handle(handle);
1967 }
1968
1969 handle = lttng_create_handle(session_name, &domains[i]);
1970 if (handle == NULL) {
1971 ret = CMD_FATAL;
1972 goto end;
1973 }
1974
1975 if (domains[i].type == LTTNG_DOMAIN_JUL ||
1976 domains[i].type == LTTNG_DOMAIN_LOG4J ||
1977 domains[i].type == LTTNG_DOMAIN_PYTHON) {
1978 ret = list_session_agent_events();
1979 if (ret) {
1980 goto end;
1981 }
1982
1983 goto next_domain;
1984 }
1985
1986 switch (domains[i].type) {
1987 case LTTNG_DOMAIN_KERNEL:
1988 case LTTNG_DOMAIN_UST:
1989 ret = list_trackers();
1990 if (ret) {
1991 goto end;
1992 }
1993 break;
1994 default:
1995 break;
1996 }
1997
1998 ret = list_channels(opt_channel);
1999 if (ret) {
2000 goto end;
2001 }
2002
2003 next_domain:
2004 if (lttng_opt_mi) {
2005 /* Close domain element */
2006 ret = mi_lttng_writer_close_element(writer);
2007 if (ret) {
2008 ret = CMD_ERROR;
2009 goto end;
2010 }
2011 }
2012
2013 }
2014 if (lttng_opt_mi) {
2015 /* Close the domains, session and sessions element */
2016 ret = mi_lttng_close_multi_element(writer, 3);
2017 if (ret) {
2018 ret = CMD_ERROR;
2019 goto end;
2020 }
2021 }
2022 }
2023 }
2024
2025 /* Mi closing */
2026 if (lttng_opt_mi) {
2027 /* Close output element */
2028 ret = mi_lttng_writer_close_element(writer);
2029 if (ret) {
2030 ret = CMD_ERROR;
2031 goto end;
2032 }
2033
2034 /* Command element close */
2035 ret = mi_lttng_writer_command_close(writer);
2036 if (ret) {
2037 ret = CMD_ERROR;
2038 goto end;
2039 }
2040 }
2041 end:
2042 /* Mi clean-up */
2043 if (writer && mi_lttng_writer_destroy(writer)) {
2044 /* Preserve original error code */
2045 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2046 }
2047
2048 free(domains);
2049 if (handle) {
2050 lttng_destroy_handle(handle);
2051 }
2052
2053 poptFreeContext(pc);
2054 return ret;
2055 }
This page took 0.085421 seconds and 3 git commands to generate.