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