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