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