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