Print UTF-8 SI suffix only when allowed by the locale
[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 const struct lttng_userspace_probe_location *location;
255 const 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, USEC_UNIT);
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,
1322 blocking_timeout, USEC_UNIT);
1323 }
1324 }
1325
1326 MSG("%sTrace file count: %" PRIu64 " per stream", indent6,
1327 channel->attr.tracefile_count == 0 ?
1328 1 : channel->attr.tracefile_count);
1329 if (channel->attr.tracefile_size != 0 ) {
1330 MSG("%sTrace file size: %" PRIu64 " bytes", indent6,
1331 channel->attr.tracefile_size);
1332 } else {
1333 MSG("%sTrace file size: %s", indent6, "unlimited");
1334 }
1335 switch (channel->attr.output) {
1336 case LTTNG_EVENT_SPLICE:
1337 MSG("%sOutput mode: splice", indent6);
1338 break;
1339 case LTTNG_EVENT_MMAP:
1340 MSG("%sOutput mode: mmap", indent6);
1341 break;
1342 }
1343
1344 MSG("\n%sStatistics:", indent4);
1345 if (listed_session.snapshot_mode) {
1346 /*
1347 * The lost packet count is omitted for sessions in snapshot
1348 * mode as it is misleading: it would indicate the number of
1349 * packets that the consumer could not extract during the
1350 * course of recording the snapshot. It does not have the
1351 * same meaning as the "regular" lost packet count that
1352 * would result from the consumer not keeping up with
1353 * event production in an overwrite-mode channel.
1354 *
1355 * A more interesting statistic would be the number of
1356 * packets lost between the first and last extracted
1357 * packets of a given snapshot (which prevents most analyses).
1358 */
1359 MSG("%sNone", indent6);
1360 goto skip_stats_printing;
1361 }
1362
1363 if (!channel->attr.overwrite) {
1364 MSG("%sDiscarded events: %" PRIu64, indent6, discarded_events);
1365 } else {
1366 MSG("%sLost packets: %" PRIu64, indent6, lost_packets);
1367 }
1368 skip_stats_printing:
1369 return;
1370 }
1371
1372 /*
1373 * Machine interface
1374 * Print a list of channel
1375 *
1376 */
1377 static int mi_list_channels(struct lttng_channel *channels, int count,
1378 const char *channel_name)
1379 {
1380 int i, ret;
1381 unsigned int chan_found = 0;
1382
1383 /* Open channels element */
1384 ret = mi_lttng_channels_open(writer);
1385 if (ret) {
1386 goto error;
1387 }
1388
1389 for (i = 0; i < count; i++) {
1390 if (channel_name != NULL) {
1391 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1392 chan_found = 1;
1393 } else {
1394 continue;
1395 }
1396 }
1397
1398 /* Write channel element and leave it open */
1399 ret = mi_lttng_channel(writer, &channels[i], 1);
1400 if (ret) {
1401 goto error;
1402 }
1403
1404 /* Listing events per channel */
1405 ret = list_events(channels[i].name);
1406 if (ret) {
1407 goto error;
1408 }
1409
1410 /* Closing the channel element we opened earlier */
1411 ret = mi_lttng_writer_close_element(writer);
1412 if (ret) {
1413 goto error;
1414 }
1415
1416 if (chan_found) {
1417 break;
1418 }
1419 }
1420
1421 /* Close channels element */
1422 ret = mi_lttng_writer_close_element(writer);
1423 if (ret) {
1424 goto error;
1425 }
1426
1427 error:
1428 return ret;
1429 }
1430
1431 /*
1432 * List channel(s) of session and domain.
1433 *
1434 * If channel_name is NULL, all channels are listed.
1435 */
1436 static int list_channels(const char *channel_name)
1437 {
1438 int count, i, ret = CMD_SUCCESS;
1439 unsigned int chan_found = 0;
1440 struct lttng_channel *channels = NULL;
1441
1442 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
1443
1444 count = lttng_list_channels(handle, &channels);
1445 if (count < 0) {
1446 switch (-count) {
1447 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
1448 if (lttng_opt_mi) {
1449 /* When printing mi this is not an error
1450 * but an empty channels element */
1451 count = 0;
1452 } else {
1453 ret = CMD_SUCCESS;
1454 WARN("No kernel channel");
1455 goto error_channels;
1456 }
1457 break;
1458 default:
1459 /* We had a real error */
1460 ret = CMD_ERROR;
1461 ERR("%s", lttng_strerror(count));
1462 goto error_channels;
1463 break;
1464 }
1465 }
1466
1467 if (lttng_opt_mi) {
1468 /* Mi print */
1469 ret = mi_list_channels(channels, count, channel_name);
1470 if (ret) {
1471 ret = CMD_ERROR;
1472 goto error;
1473 }
1474 } else {
1475 /* Pretty print */
1476 if (count) {
1477 MSG("Channels:\n-------------");
1478 }
1479
1480 for (i = 0; i < count; i++) {
1481 if (channel_name != NULL) {
1482 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1483 chan_found = 1;
1484 } else {
1485 continue;
1486 }
1487 }
1488 print_channel(&channels[i]);
1489
1490 /* Listing events per channel */
1491 ret = list_events(channels[i].name);
1492 if (ret) {
1493 goto error;
1494 }
1495
1496 if (chan_found) {
1497 break;
1498 }
1499 }
1500
1501 if (!chan_found && channel_name != NULL) {
1502 ret = CMD_ERROR;
1503 ERR("Channel %s not found", channel_name);
1504 goto error;
1505 }
1506 }
1507 error:
1508 free(channels);
1509
1510 error_channels:
1511 return ret;
1512 }
1513
1514 /*
1515 * List tracker PID(s) of session and domain.
1516 */
1517 static int list_tracker_pids(void)
1518 {
1519 int ret = 0;
1520 int enabled;
1521 int *pids = NULL;
1522 size_t nr_pids;
1523
1524 ret = lttng_list_tracker_pids(handle,
1525 &enabled, &pids, &nr_pids);
1526 if (ret) {
1527 return ret;
1528 }
1529 if (enabled) {
1530 int i;
1531 _MSG("PID tracker: [");
1532
1533 /* Mi tracker_pid element*/
1534 if (writer) {
1535 /* Open tracker_pid and targets elements */
1536 ret = mi_lttng_pid_tracker_open(writer);
1537 if (ret) {
1538 goto end;
1539 }
1540 }
1541
1542 for (i = 0; i < nr_pids; i++) {
1543 if (i) {
1544 _MSG(",");
1545 }
1546 _MSG(" %d", pids[i]);
1547
1548 /* Mi */
1549 if (writer) {
1550 ret = mi_lttng_pid_target(writer, pids[i], 0);
1551 if (ret) {
1552 goto end;
1553 }
1554 }
1555 }
1556 _MSG(" ]\n\n");
1557
1558 /* Mi close tracker_pid and targets */
1559 if (writer) {
1560 ret = mi_lttng_close_multi_element(writer,2);
1561 if (ret) {
1562 goto end;
1563 }
1564 }
1565 }
1566 end:
1567 free(pids);
1568 return ret;
1569
1570 }
1571
1572 /*
1573 * List all tracker of a domain
1574 */
1575 static int list_trackers(void)
1576 {
1577 int ret;
1578
1579 /* Trackers listing */
1580 if (lttng_opt_mi) {
1581 ret = mi_lttng_trackers_open(writer);
1582 if (ret) {
1583 goto end;
1584 }
1585 }
1586
1587 /* pid tracker */
1588 ret = list_tracker_pids();
1589 if (ret) {
1590 goto end;
1591 }
1592
1593 if (lttng_opt_mi) {
1594 /* Close trackers element */
1595 ret = mi_lttng_writer_close_element(writer);
1596 if (ret) {
1597 goto end;
1598 }
1599 }
1600
1601 end:
1602 return ret;
1603 }
1604
1605 static enum cmd_error_code print_periodic_rotation_schedule(
1606 const struct lttng_rotation_schedule *schedule)
1607 {
1608 enum cmd_error_code ret;
1609 enum lttng_rotation_status status;
1610 uint64_t value;
1611
1612 status = lttng_rotation_schedule_periodic_get_period(schedule,
1613 &value);
1614 if (status != LTTNG_ROTATION_STATUS_OK) {
1615 ERR("Failed to retrieve period parameter from periodic rotation schedule.");
1616 ret = CMD_ERROR;
1617 goto end;
1618 }
1619
1620 MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT);
1621 ret = CMD_SUCCESS;
1622 end:
1623 return ret;
1624 }
1625
1626 static enum cmd_error_code print_size_threshold_rotation_schedule(
1627 const struct lttng_rotation_schedule *schedule)
1628 {
1629 enum cmd_error_code ret;
1630 enum lttng_rotation_status status;
1631 uint64_t value;
1632
1633 status = lttng_rotation_schedule_size_threshold_get_threshold(schedule,
1634 &value);
1635 if (status != LTTNG_ROTATION_STATUS_OK) {
1636 ERR("Failed to retrieve size parameter from size-based rotation schedule.");
1637 ret = CMD_ERROR;
1638 goto end;
1639 }
1640
1641 MSG(" size threshold: %" PRIu64" bytes", value);
1642 ret = CMD_SUCCESS;
1643 end:
1644 return ret;
1645 }
1646
1647 static enum cmd_error_code print_rotation_schedule(
1648 const struct lttng_rotation_schedule *schedule)
1649 {
1650 enum cmd_error_code ret;
1651
1652 switch (lttng_rotation_schedule_get_type(schedule)) {
1653 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
1654 ret = print_size_threshold_rotation_schedule(schedule);
1655 break;
1656 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
1657 ret = print_periodic_rotation_schedule(schedule);
1658 break;
1659 default:
1660 ret = CMD_ERROR;
1661 }
1662 return ret;
1663 }
1664
1665 /*
1666 * List the automatic rotation settings.
1667 */
1668 static enum cmd_error_code list_rotate_settings(const char *session_name)
1669 {
1670 int ret;
1671 enum cmd_error_code cmd_ret = CMD_SUCCESS;
1672 unsigned int count, i;
1673 struct lttng_rotation_schedules *schedules = NULL;
1674 enum lttng_rotation_status status;
1675
1676 ret = lttng_session_list_rotation_schedules(session_name, &schedules);
1677 if (ret != LTTNG_OK) {
1678 ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret));
1679 cmd_ret = CMD_ERROR;
1680 goto end;
1681 }
1682
1683 status = lttng_rotation_schedules_get_count(schedules, &count);
1684 if (status != LTTNG_ROTATION_STATUS_OK) {
1685 ERR("Failed to retrieve the number of session rotation schedules.");
1686 cmd_ret = CMD_ERROR;
1687 goto end;
1688 }
1689
1690 if (count == 0) {
1691 cmd_ret = CMD_SUCCESS;
1692 goto end;
1693 }
1694
1695 MSG("Automatic rotation schedules:");
1696 if (lttng_opt_mi) {
1697 ret = mi_lttng_writer_open_element(writer,
1698 mi_lttng_element_rotation_schedules);
1699 if (ret) {
1700 cmd_ret = CMD_ERROR;
1701 goto end;
1702 }
1703 }
1704
1705 for (i = 0; i < count; i++) {
1706 enum cmd_error_code tmp_ret = CMD_SUCCESS;
1707 const struct lttng_rotation_schedule *schedule;
1708
1709 schedule = lttng_rotation_schedules_get_at_index(schedules, i);
1710 if (!schedule) {
1711 ERR("Failed to retrieve session rotation schedule.");
1712 cmd_ret = CMD_ERROR;
1713 goto end;
1714 }
1715
1716 if (lttng_opt_mi) {
1717 ret = mi_lttng_rotation_schedule(writer, schedule);
1718 if (ret) {
1719 tmp_ret = CMD_ERROR;
1720 }
1721 } else {
1722 tmp_ret = print_rotation_schedule(schedule);
1723 }
1724
1725 /*
1726 * Report an error if the serialization of any of the
1727 * descriptors failed.
1728 */
1729 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
1730 }
1731
1732 _MSG("\n");
1733 if (lttng_opt_mi) {
1734 /* Close the rotation_schedules element. */
1735 ret = mi_lttng_writer_close_element(writer);
1736 if (ret) {
1737 cmd_ret = CMD_ERROR;
1738 goto end;
1739 }
1740 }
1741 end:
1742 lttng_rotation_schedules_destroy(schedules);
1743 return cmd_ret;
1744 }
1745
1746 /*
1747 * Machine interface
1748 * Find the session with session_name as name
1749 * and print his informations.
1750 */
1751 static int mi_list_session(const char *session_name,
1752 struct lttng_session *sessions, int count)
1753 {
1754 int ret, i;
1755 unsigned int session_found = 0;
1756
1757 if (session_name == NULL) {
1758 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1759 goto end;
1760 }
1761
1762 for (i = 0; i < count; i++) {
1763 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1764 /* We need to leave it open to append other informations
1765 * like domain, channel, events etc.*/
1766 session_found = 1;
1767 ret = mi_lttng_session(writer, &sessions[i], 1);
1768 if (ret) {
1769 goto end;
1770 }
1771 break;
1772 }
1773 }
1774
1775 if (!session_found) {
1776 ERR("Session '%s' not found", session_name);
1777 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1778 goto end;
1779 }
1780
1781 end:
1782 return ret;
1783 }
1784
1785 /*
1786 * Machine interface
1787 * List all availables session
1788 */
1789 static int mi_list_sessions(struct lttng_session *sessions, int count)
1790 {
1791 int ret, i;
1792
1793 /* Opening sessions element */
1794 ret = mi_lttng_sessions_open(writer);
1795 if (ret) {
1796 goto end;
1797 }
1798
1799 /* Listing sessions */
1800 for (i = 0; i < count; i++) {
1801 ret = mi_lttng_session(writer, &sessions[i], 0);
1802 if (ret) {
1803 goto end;
1804 }
1805 }
1806
1807 /* Closing sessions element */
1808 ret = mi_lttng_writer_close_element(writer);
1809 if (ret) {
1810 goto end;
1811 }
1812
1813 end:
1814 return ret;
1815 }
1816
1817 /*
1818 * List available tracing session. List only basic information.
1819 *
1820 * If session_name is NULL, all sessions are listed.
1821 */
1822 static int list_sessions(const char *session_name)
1823 {
1824 int ret = CMD_SUCCESS;
1825 int count, i;
1826 unsigned int session_found = 0;
1827 struct lttng_session *sessions;
1828
1829 count = lttng_list_sessions(&sessions);
1830 DBG("Session count %d", count);
1831 if (count < 0) {
1832 ret = CMD_ERROR;
1833 ERR("%s", lttng_strerror(count));
1834 goto end;
1835 }
1836
1837 if (lttng_opt_mi) {
1838 /* Mi */
1839 if (session_name == NULL) {
1840 /* List all session */
1841 ret = mi_list_sessions(sessions, count);
1842 } else {
1843 /* Note : this return an open session element */
1844 ret = mi_list_session(session_name, sessions, count);
1845 }
1846 if (ret) {
1847 ret = CMD_ERROR;
1848 goto error;
1849 }
1850 } else {
1851 /* Pretty print */
1852 if (count == 0) {
1853 MSG("Currently no available tracing session");
1854 goto end;
1855 }
1856
1857 if (session_name == NULL) {
1858 MSG("Available tracing sessions:");
1859 }
1860
1861
1862 for (i = 0; i < count; i++) {
1863 if (session_name != NULL) {
1864 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1865 session_found = 1;
1866 MSG("Tracing session %s: [%s%s]", session_name,
1867 active_string(sessions[i].enabled),
1868 snapshot_string(sessions[i].snapshot_mode));
1869 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
1870 memcpy(&listed_session, &sessions[i],
1871 sizeof(listed_session));
1872 break;
1873 }
1874 } else {
1875 MSG(" %d) %s (%s) [%s%s]", i + 1,
1876 sessions[i].name, sessions[i].path,
1877 active_string(sessions[i].enabled),
1878 snapshot_string(sessions[i].snapshot_mode));
1879 MSG("%sTrace path: %s", indent4, sessions[i].path);
1880 if (sessions[i].live_timer_interval != 0) {
1881 MSG("%sLive timer interval: %u µs", indent4,
1882 sessions[i].live_timer_interval);
1883 }
1884 MSG("");
1885 }
1886 }
1887
1888 if (!session_found && session_name != NULL) {
1889 ERR("Session '%s' not found", session_name);
1890 ret = CMD_ERROR;
1891 goto error;
1892 }
1893
1894 if (session_name == NULL) {
1895 MSG("\nUse lttng list <session_name> for more details");
1896 }
1897 }
1898
1899 error:
1900 free(sessions);
1901 end:
1902 return ret;
1903 }
1904
1905
1906 /*
1907 * Machine Interface
1908 * list available domain(s) for a session.
1909 */
1910 static int mi_list_domains(struct lttng_domain *domains, int count)
1911 {
1912 int i, ret;
1913 /* Open domains element */
1914 ret = mi_lttng_domains_open(writer);
1915 if (ret) {
1916 goto end;
1917 }
1918
1919 for (i = 0; i < count; i++) {
1920 ret = mi_lttng_domain(writer, &domains[i] , 0);
1921 if (ret) {
1922 goto end;
1923 }
1924 }
1925
1926 /* Closing domains element */
1927 ret = mi_lttng_writer_close_element(writer);
1928 if (ret) {
1929 goto end;
1930 }
1931 end:
1932 return ret;
1933 }
1934
1935 /*
1936 * List available domain(s) for a session.
1937 */
1938 static int list_domains(const char *session_name)
1939 {
1940 int i, count, ret = CMD_SUCCESS;
1941 struct lttng_domain *domains = NULL;
1942
1943
1944 count = lttng_list_domains(session_name, &domains);
1945 if (count < 0) {
1946 ret = CMD_ERROR;
1947 ERR("%s", lttng_strerror(count));
1948 goto end;
1949 }
1950
1951 if (lttng_opt_mi) {
1952 /* Mi output */
1953 ret = mi_list_domains(domains, count);
1954 if (ret) {
1955 ret = CMD_ERROR;
1956 goto error;
1957 }
1958 } else {
1959 /* Pretty print */
1960 MSG("Domains:\n-------------");
1961 if (count == 0) {
1962 MSG(" None");
1963 goto end;
1964 }
1965
1966 for (i = 0; i < count; i++) {
1967 switch (domains[i].type) {
1968 case LTTNG_DOMAIN_KERNEL:
1969 MSG(" - Kernel");
1970 break;
1971 case LTTNG_DOMAIN_UST:
1972 MSG(" - UST global");
1973 break;
1974 case LTTNG_DOMAIN_JUL:
1975 MSG(" - JUL (Java Util Logging)");
1976 break;
1977 case LTTNG_DOMAIN_LOG4J:
1978 MSG(" - LOG4j (Logging for Java)");
1979 break;
1980 case LTTNG_DOMAIN_PYTHON:
1981 MSG(" - Python (logging)");
1982 break;
1983 default:
1984 break;
1985 }
1986 }
1987 }
1988
1989 error:
1990 free(domains);
1991
1992 end:
1993 return ret;
1994 }
1995
1996 /*
1997 * The 'list <options>' first level command
1998 */
1999 int cmd_list(int argc, const char **argv)
2000 {
2001 int opt, ret = CMD_SUCCESS;
2002 const char *session_name, *leftover = NULL;
2003 static poptContext pc;
2004 struct lttng_domain domain;
2005 struct lttng_domain *domains = NULL;
2006
2007 memset(&domain, 0, sizeof(domain));
2008
2009 if (argc < 1) {
2010 ret = CMD_ERROR;
2011 goto end;
2012 }
2013
2014 pc = poptGetContext(NULL, argc, argv, long_options, 0);
2015 poptReadDefaultConfig(pc, 0);
2016
2017 while ((opt = poptGetNextOpt(pc)) != -1) {
2018 switch (opt) {
2019 case OPT_HELP:
2020 SHOW_HELP();
2021 goto end;
2022 case OPT_USERSPACE:
2023 opt_userspace = 1;
2024 break;
2025 case OPT_LIST_OPTIONS:
2026 list_cmd_options(stdout, long_options);
2027 goto end;
2028 default:
2029 ret = CMD_UNDEFINED;
2030 goto end;
2031 }
2032 }
2033
2034 /* Mi check */
2035 if (lttng_opt_mi) {
2036 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
2037 if (!writer) {
2038 ret = CMD_ERROR;
2039 goto end;
2040 }
2041
2042 /* Open command element */
2043 ret = mi_lttng_writer_command_open(writer,
2044 mi_lttng_element_command_list);
2045 if (ret) {
2046 ret = CMD_ERROR;
2047 goto end;
2048 }
2049
2050 /* Open output element */
2051 ret = mi_lttng_writer_open_element(writer,
2052 mi_lttng_element_command_output);
2053 if (ret) {
2054 ret = CMD_ERROR;
2055 goto end;
2056 }
2057 }
2058
2059 /* Get session name (trailing argument) */
2060 session_name = poptGetArg(pc);
2061 DBG2("Session name: %s", session_name);
2062
2063 leftover = poptGetArg(pc);
2064 if (leftover) {
2065 ERR("Unknown argument: %s", leftover);
2066 ret = CMD_ERROR;
2067 goto end;
2068 }
2069
2070 if (opt_kernel) {
2071 domain.type = LTTNG_DOMAIN_KERNEL;
2072 } else if (opt_userspace) {
2073 DBG2("Listing userspace global domain");
2074 domain.type = LTTNG_DOMAIN_UST;
2075 } else if (opt_jul) {
2076 DBG2("Listing JUL domain");
2077 domain.type = LTTNG_DOMAIN_JUL;
2078 } else if (opt_log4j) {
2079 domain.type = LTTNG_DOMAIN_LOG4J;
2080 } else if (opt_python) {
2081 domain.type = LTTNG_DOMAIN_PYTHON;
2082 }
2083
2084 if (!opt_kernel && opt_syscall) {
2085 WARN("--syscall will only work with the Kernel domain (-k)");
2086 ret = CMD_ERROR;
2087 goto end;
2088 }
2089
2090 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
2091 handle = lttng_create_handle(session_name, &domain);
2092 if (handle == NULL) {
2093 ret = CMD_FATAL;
2094 goto end;
2095 }
2096 }
2097
2098 if (session_name == NULL) {
2099 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
2100 && !opt_python) {
2101 ret = list_sessions(NULL);
2102 if (ret) {
2103 goto end;
2104 }
2105 }
2106 if (opt_kernel) {
2107 if (opt_syscall) {
2108 ret = list_syscalls();
2109 if (ret) {
2110 goto end;
2111 }
2112 } else {
2113 ret = list_kernel_events();
2114 if (ret) {
2115 goto end;
2116 }
2117 }
2118 }
2119 if (opt_userspace) {
2120 if (opt_fields) {
2121 ret = list_ust_event_fields();
2122 } else {
2123 ret = list_ust_events();
2124 }
2125 if (ret) {
2126 goto end;
2127 }
2128 }
2129 if (opt_jul || opt_log4j || opt_python) {
2130 ret = list_agent_events();
2131 if (ret) {
2132 goto end;
2133 }
2134 }
2135 } else {
2136 /* List session attributes */
2137 if (lttng_opt_mi) {
2138 /* Open element sessions
2139 * Present for xml consistency */
2140 ret = mi_lttng_sessions_open(writer);
2141 if (ret) {
2142 goto end;
2143 }
2144 }
2145 /* MI: the ouptut of list_sessions is an unclosed session element */
2146 ret = list_sessions(session_name);
2147 if (ret) {
2148 goto end;
2149 }
2150
2151 ret = list_rotate_settings(session_name);
2152 if (ret) {
2153 goto end;
2154 }
2155
2156 /* Domain listing */
2157 if (opt_domain) {
2158 ret = list_domains(session_name);
2159 goto end;
2160 }
2161
2162 /* Channel listing */
2163 if (opt_kernel || opt_userspace) {
2164 if (lttng_opt_mi) {
2165 /* Add of domains and domain element for xml
2166 * consistency and validation
2167 */
2168 ret = mi_lttng_domains_open(writer);
2169 if (ret) {
2170 goto end;
2171 }
2172
2173 /* Open domain and leave it open for
2174 * nested channels printing */
2175 ret = mi_lttng_domain(writer, &domain, 1);
2176 if (ret) {
2177 goto end;
2178 }
2179
2180 }
2181
2182
2183 /* Trackers */
2184 ret = list_trackers();
2185 if (ret) {
2186 goto end;
2187 }
2188
2189 /* Channels */
2190 ret = list_channels(opt_channel);
2191 if (ret) {
2192 goto end;
2193 }
2194
2195 if (lttng_opt_mi) {
2196 /* Close domain and domain element */
2197 ret = mi_lttng_close_multi_element(writer, 2);
2198 }
2199 if (ret) {
2200 goto end;
2201 }
2202
2203
2204 } else {
2205 int i, nb_domain;
2206
2207 /* We want all domain(s) */
2208 nb_domain = lttng_list_domains(session_name, &domains);
2209 if (nb_domain < 0) {
2210 ret = CMD_ERROR;
2211 ERR("%s", lttng_strerror(nb_domain));
2212 goto end;
2213 }
2214
2215 if (lttng_opt_mi) {
2216 ret = mi_lttng_domains_open(writer);
2217 if (ret) {
2218 ret = CMD_ERROR;
2219 goto end;
2220 }
2221 }
2222
2223 for (i = 0; i < nb_domain; i++) {
2224 switch (domains[i].type) {
2225 case LTTNG_DOMAIN_KERNEL:
2226 MSG("=== Domain: Kernel ===\n");
2227 break;
2228 case LTTNG_DOMAIN_UST:
2229 MSG("=== Domain: UST global ===\n");
2230 MSG("Buffer type: %s\n",
2231 domains[i].buf_type ==
2232 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
2233 break;
2234 case LTTNG_DOMAIN_JUL:
2235 MSG("=== Domain: JUL (Java Util Logging) ===\n");
2236 break;
2237 case LTTNG_DOMAIN_LOG4J:
2238 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
2239 break;
2240 case LTTNG_DOMAIN_PYTHON:
2241 MSG("=== Domain: Python (logging) ===\n");
2242 break;
2243 default:
2244 MSG("=== Domain: Unimplemented ===\n");
2245 break;
2246 }
2247
2248 if (lttng_opt_mi) {
2249 ret = mi_lttng_domain(writer, &domains[i], 1);
2250 if (ret) {
2251 ret = CMD_ERROR;
2252 goto end;
2253 }
2254 }
2255
2256 /* Clean handle before creating a new one */
2257 if (handle) {
2258 lttng_destroy_handle(handle);
2259 }
2260
2261 handle = lttng_create_handle(session_name, &domains[i]);
2262 if (handle == NULL) {
2263 ret = CMD_FATAL;
2264 goto end;
2265 }
2266
2267 if (domains[i].type == LTTNG_DOMAIN_JUL ||
2268 domains[i].type == LTTNG_DOMAIN_LOG4J ||
2269 domains[i].type == LTTNG_DOMAIN_PYTHON) {
2270 ret = list_session_agent_events();
2271 if (ret) {
2272 goto end;
2273 }
2274
2275 goto next_domain;
2276 }
2277
2278 switch (domains[i].type) {
2279 case LTTNG_DOMAIN_KERNEL:
2280 case LTTNG_DOMAIN_UST:
2281 ret = list_trackers();
2282 if (ret) {
2283 goto end;
2284 }
2285 break;
2286 default:
2287 break;
2288 }
2289
2290 ret = list_channels(opt_channel);
2291 if (ret) {
2292 goto end;
2293 }
2294
2295 next_domain:
2296 if (lttng_opt_mi) {
2297 /* Close domain element */
2298 ret = mi_lttng_writer_close_element(writer);
2299 if (ret) {
2300 ret = CMD_ERROR;
2301 goto end;
2302 }
2303 }
2304
2305 }
2306 if (lttng_opt_mi) {
2307 /* Close the domains, session and sessions element */
2308 ret = mi_lttng_close_multi_element(writer, 3);
2309 if (ret) {
2310 ret = CMD_ERROR;
2311 goto end;
2312 }
2313 }
2314 }
2315 }
2316
2317 /* Mi closing */
2318 if (lttng_opt_mi) {
2319 /* Close output element */
2320 ret = mi_lttng_writer_close_element(writer);
2321 if (ret) {
2322 ret = CMD_ERROR;
2323 goto end;
2324 }
2325
2326 /* Command element close */
2327 ret = mi_lttng_writer_command_close(writer);
2328 if (ret) {
2329 ret = CMD_ERROR;
2330 goto end;
2331 }
2332 }
2333 end:
2334 /* Mi clean-up */
2335 if (writer && mi_lttng_writer_destroy(writer)) {
2336 /* Preserve original error code */
2337 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2338 }
2339
2340 free(domains);
2341 if (handle) {
2342 lttng_destroy_handle(handle);
2343 }
2344
2345 poptFreeContext(pc);
2346 return ret;
2347 }
This page took 0.115019 seconds and 5 git commands to generate.