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