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