rotation-api: pass session name explicitly
[lttng-tools.git] / src / bin / lttng / commands / list.c
... / ...
CommitLineData
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
31static int opt_userspace;
32static int opt_kernel;
33static int opt_jul;
34static int opt_log4j;
35static int opt_python;
36static char *opt_channel;
37static int opt_domain;
38static int opt_fields;
39static int opt_syscall;
40
41const char *indent4 = " ";
42const char *indent6 = " ";
43const char *indent8 = " ";
44
45#ifdef LTTNG_EMBED_HELP
46static const char help_msg[] =
47#include <lttng-list.1.h>
48;
49#endif
50
51enum {
52 OPT_HELP = 1,
53 OPT_USERSPACE,
54 OPT_LIST_OPTIONS,
55};
56
57static struct lttng_handle *handle;
58static struct mi_writer *writer;
59
60/* Only set when listing a single session. */
61static struct lttng_session listed_session;
62
63static 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 */
85static 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
110end:
111 if (fp) {
112 fclose(fp);
113 }
114 return cmdline;
115}
116
117static
118const 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
128static const char *snapshot_string(int value)
129{
130 switch (value) {
131 case 1:
132 return " snapshot";
133 default:
134 return "";
135 }
136}
137
138static
139const 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
149static
150const char *safe_string(const char *str)
151{
152 return str ? str : "";
153}
154
155static 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
169static 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 */
189static 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
248end:
249 return exclusion_msg;
250}
251
252/*
253 * Pretty print single event.
254 */
255static 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
355static 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 */
375static 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 */
388static 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);
465end:
466 return ret;
467error:
468 free(cmdline);
469 return ret;
470}
471
472static 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
546error:
547 free(event_list);
548end:
549 lttng_destroy_handle(handle);
550 return ret;
551}
552
553/*
554 * Ask session daemon for all user space tracepoints available.
555 */
556static 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
612error:
613 free(event_list);
614end:
615 lttng_destroy_handle(handle);
616 return ret;
617}
618
619/*
620 * Machine interface
621 * List all ust event with their fields
622 */
623static 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);
732end:
733 return ret;
734error:
735 free(cmdline);
736 return ret;
737}
738
739/*
740 * Ask session daemon for all user space tracepoint fields available.
741 */
742static 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
812error:
813 free(event_field_list);
814end:
815 lttng_destroy_handle(handle);
816 return ret;
817}
818
819/*
820 * Machine interface
821 * Print a list of kernel events
822 */
823static 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
859end:
860 return ret;
861}
862
863/*
864 * Ask for all trace events in the kernel
865 */
866static 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
909end:
910 free(event_list);
911
912 lttng_destroy_handle(handle);
913 return ret;
914
915error:
916 lttng_destroy_handle(handle);
917 return ret;
918}
919
920/*
921 * Machine interface
922 * Print a list of system calls.
923 */
924static 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
947end:
948 return ret;
949}
950
951/*
952 * Ask for kernel system calls.
953 */
954static 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
985end:
986 free(event_list);
987 return ret;
988
989error:
990 return ret;
991}
992
993/*
994 * Machine Interface
995 * Print a list of agent events
996 */
997static 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
1017end:
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 */
1026static 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
1096end:
1097 free(events);
1098error:
1099 return ret;
1100}
1101
1102/*
1103 * Machine interface
1104 * print a list of event
1105 */
1106static 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
1126end:
1127 return ret;
1128}
1129
1130/*
1131 * List events of channel of session and domain.
1132 */
1133static 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 }
1166end:
1167 free(events);
1168error:
1169 return ret;
1170}
1171
1172static
1173void 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 */
1192static 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 }
1286skip_stats_printing:
1287 return;
1288}
1289
1290/*
1291 * Machine interface
1292 * Print a list of channel
1293 *
1294 */
1295static 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
1345error:
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 */
1354static 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 }
1425error:
1426 free(channels);
1427
1428error_channels:
1429 return ret;
1430}
1431
1432/*
1433 * List tracker PID(s) of session and domain.
1434 */
1435static 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 }
1484end:
1485 free(pids);
1486 return ret;
1487
1488}
1489
1490/*
1491 * List all tracker of a domain
1492 */
1493static 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
1519end:
1520 return ret;
1521}
1522
1523/*
1524 * List the rotate settings (timer/size if any).
1525 */
1526static int list_rotate_settings(const char *session_name)
1527{
1528 int ret;
1529 uint64_t size, timer;
1530
1531 ret = lttng_rotation_schedule_get_timer_period(session_name, &timer);
1532 if (ret) {
1533 goto end;
1534 }
1535
1536 ret = lttng_rotation_schedule_get_size(session_name, &size);
1537 if (ret) {
1538 goto end;
1539 }
1540
1541 if (!timer && !size) {
1542 ret = 0;
1543 goto end;
1544 }
1545
1546 _MSG("Automatic rotation schedule settings:\n");
1547
1548 if (timer) {
1549 _MSG(" timer period: %" PRIu64" µs\n", timer);
1550 if (lttng_opt_mi) {
1551 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1552 config_element_rotation_timer_interval, timer);
1553 if (ret) {
1554 goto end;
1555 }
1556 }
1557 }
1558 if (size) {
1559 _MSG(" size threshold: %" PRIu64" bytes\n", size);
1560 if (lttng_opt_mi) {
1561 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1562 config_element_rotation_size, size);
1563 if (ret) {
1564 goto end;
1565 }
1566 }
1567 }
1568 _MSG("\n");
1569
1570end:
1571 return ret;
1572}
1573
1574/*
1575 * Machine interface
1576 * Find the session with session_name as name
1577 * and print his informations.
1578 */
1579static int mi_list_session(const char *session_name,
1580 struct lttng_session *sessions, int count)
1581{
1582 int ret, i;
1583 unsigned int session_found = 0;
1584
1585 if (session_name == NULL) {
1586 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1587 goto end;
1588 }
1589
1590 for (i = 0; i < count; i++) {
1591 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1592 /* We need to leave it open to append other informations
1593 * like domain, channel, events etc.*/
1594 session_found = 1;
1595 ret = mi_lttng_session(writer, &sessions[i], 1);
1596 if (ret) {
1597 goto end;
1598 }
1599 break;
1600 }
1601 }
1602
1603 if (!session_found) {
1604 ERR("Session '%s' not found", session_name);
1605 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1606 goto end;
1607 }
1608
1609end:
1610 return ret;
1611}
1612
1613/*
1614 * Machine interface
1615 * List all availables session
1616 */
1617static int mi_list_sessions(struct lttng_session *sessions, int count)
1618{
1619 int ret, i;
1620
1621 /* Opening sessions element */
1622 ret = mi_lttng_sessions_open(writer);
1623 if (ret) {
1624 goto end;
1625 }
1626
1627 /* Listing sessions */
1628 for (i = 0; i < count; i++) {
1629 ret = mi_lttng_session(writer, &sessions[i], 0);
1630 if (ret) {
1631 goto end;
1632 }
1633 }
1634
1635 /* Closing sessions element */
1636 ret = mi_lttng_writer_close_element(writer);
1637 if (ret) {
1638 goto end;
1639 }
1640
1641end:
1642 return ret;
1643}
1644
1645/*
1646 * List available tracing session. List only basic information.
1647 *
1648 * If session_name is NULL, all sessions are listed.
1649 */
1650static int list_sessions(const char *session_name)
1651{
1652 int ret = CMD_SUCCESS;
1653 int count, i;
1654 unsigned int session_found = 0;
1655 struct lttng_session *sessions;
1656
1657 count = lttng_list_sessions(&sessions);
1658 DBG("Session count %d", count);
1659 if (count < 0) {
1660 ret = CMD_ERROR;
1661 ERR("%s", lttng_strerror(count));
1662 goto end;
1663 }
1664
1665 if (lttng_opt_mi) {
1666 /* Mi */
1667 if (session_name == NULL) {
1668 /* List all session */
1669 ret = mi_list_sessions(sessions, count);
1670 } else {
1671 /* Note : this return an open session element */
1672 ret = mi_list_session(session_name, sessions, count);
1673 }
1674 if (ret) {
1675 ret = CMD_ERROR;
1676 goto error;
1677 }
1678 } else {
1679 /* Pretty print */
1680 if (count == 0) {
1681 MSG("Currently no available tracing session");
1682 goto end;
1683 }
1684
1685 if (session_name == NULL) {
1686 MSG("Available tracing sessions:");
1687 }
1688
1689
1690 for (i = 0; i < count; i++) {
1691 if (session_name != NULL) {
1692 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1693 session_found = 1;
1694 MSG("Tracing session %s: [%s%s]", session_name,
1695 active_string(sessions[i].enabled),
1696 snapshot_string(sessions[i].snapshot_mode));
1697 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
1698 memcpy(&listed_session, &sessions[i],
1699 sizeof(listed_session));
1700 break;
1701 }
1702 } else {
1703 MSG(" %d) %s (%s) [%s%s]", i + 1,
1704 sessions[i].name, sessions[i].path,
1705 active_string(sessions[i].enabled),
1706 snapshot_string(sessions[i].snapshot_mode));
1707 MSG("%sTrace path: %s", indent4, sessions[i].path);
1708 if (sessions[i].live_timer_interval != 0) {
1709 MSG("%sLive timer interval: %u µs", indent4,
1710 sessions[i].live_timer_interval);
1711 }
1712 MSG("");
1713 }
1714 }
1715
1716 if (!session_found && session_name != NULL) {
1717 ERR("Session '%s' not found", session_name);
1718 ret = CMD_ERROR;
1719 goto error;
1720 }
1721
1722 if (session_name == NULL) {
1723 MSG("\nUse lttng list <session_name> for more details");
1724 }
1725 }
1726
1727error:
1728 free(sessions);
1729end:
1730 return ret;
1731}
1732
1733
1734/*
1735 * Machine Interface
1736 * list available domain(s) for a session.
1737 */
1738static int mi_list_domains(struct lttng_domain *domains, int count)
1739{
1740 int i, ret;
1741 /* Open domains element */
1742 ret = mi_lttng_domains_open(writer);
1743 if (ret) {
1744 goto end;
1745 }
1746
1747 for (i = 0; i < count; i++) {
1748 ret = mi_lttng_domain(writer, &domains[i] , 0);
1749 if (ret) {
1750 goto end;
1751 }
1752 }
1753
1754 /* Closing domains element */
1755 ret = mi_lttng_writer_close_element(writer);
1756 if (ret) {
1757 goto end;
1758 }
1759end:
1760 return ret;
1761}
1762
1763/*
1764 * List available domain(s) for a session.
1765 */
1766static int list_domains(const char *session_name)
1767{
1768 int i, count, ret = CMD_SUCCESS;
1769 struct lttng_domain *domains = NULL;
1770
1771
1772 count = lttng_list_domains(session_name, &domains);
1773 if (count < 0) {
1774 ret = CMD_ERROR;
1775 ERR("%s", lttng_strerror(count));
1776 goto end;
1777 }
1778
1779 if (lttng_opt_mi) {
1780 /* Mi output */
1781 ret = mi_list_domains(domains, count);
1782 if (ret) {
1783 ret = CMD_ERROR;
1784 goto error;
1785 }
1786 } else {
1787 /* Pretty print */
1788 MSG("Domains:\n-------------");
1789 if (count == 0) {
1790 MSG(" None");
1791 goto end;
1792 }
1793
1794 for (i = 0; i < count; i++) {
1795 switch (domains[i].type) {
1796 case LTTNG_DOMAIN_KERNEL:
1797 MSG(" - Kernel");
1798 break;
1799 case LTTNG_DOMAIN_UST:
1800 MSG(" - UST global");
1801 break;
1802 case LTTNG_DOMAIN_JUL:
1803 MSG(" - JUL (Java Util Logging)");
1804 break;
1805 case LTTNG_DOMAIN_LOG4J:
1806 MSG(" - LOG4j (Logging for Java)");
1807 break;
1808 case LTTNG_DOMAIN_PYTHON:
1809 MSG(" - Python (logging)");
1810 break;
1811 default:
1812 break;
1813 }
1814 }
1815 }
1816
1817error:
1818 free(domains);
1819
1820end:
1821 return ret;
1822}
1823
1824/*
1825 * The 'list <options>' first level command
1826 */
1827int cmd_list(int argc, const char **argv)
1828{
1829 int opt, ret = CMD_SUCCESS;
1830 const char *session_name, *leftover = NULL;
1831 static poptContext pc;
1832 struct lttng_domain domain;
1833 struct lttng_domain *domains = NULL;
1834
1835 memset(&domain, 0, sizeof(domain));
1836
1837 if (argc < 1) {
1838 ret = CMD_ERROR;
1839 goto end;
1840 }
1841
1842 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1843 poptReadDefaultConfig(pc, 0);
1844
1845 while ((opt = poptGetNextOpt(pc)) != -1) {
1846 switch (opt) {
1847 case OPT_HELP:
1848 SHOW_HELP();
1849 goto end;
1850 case OPT_USERSPACE:
1851 opt_userspace = 1;
1852 break;
1853 case OPT_LIST_OPTIONS:
1854 list_cmd_options(stdout, long_options);
1855 goto end;
1856 default:
1857 ret = CMD_UNDEFINED;
1858 goto end;
1859 }
1860 }
1861
1862 /* Mi check */
1863 if (lttng_opt_mi) {
1864 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1865 if (!writer) {
1866 ret = CMD_ERROR;
1867 goto end;
1868 }
1869
1870 /* Open command element */
1871 ret = mi_lttng_writer_command_open(writer,
1872 mi_lttng_element_command_list);
1873 if (ret) {
1874 ret = CMD_ERROR;
1875 goto end;
1876 }
1877
1878 /* Open output element */
1879 ret = mi_lttng_writer_open_element(writer,
1880 mi_lttng_element_command_output);
1881 if (ret) {
1882 ret = CMD_ERROR;
1883 goto end;
1884 }
1885 }
1886
1887 /* Get session name (trailing argument) */
1888 session_name = poptGetArg(pc);
1889 DBG2("Session name: %s", session_name);
1890
1891 leftover = poptGetArg(pc);
1892 if (leftover) {
1893 ERR("Unknown argument: %s", leftover);
1894 ret = CMD_ERROR;
1895 goto end;
1896 }
1897
1898 if (opt_kernel) {
1899 domain.type = LTTNG_DOMAIN_KERNEL;
1900 } else if (opt_userspace) {
1901 DBG2("Listing userspace global domain");
1902 domain.type = LTTNG_DOMAIN_UST;
1903 } else if (opt_jul) {
1904 DBG2("Listing JUL domain");
1905 domain.type = LTTNG_DOMAIN_JUL;
1906 } else if (opt_log4j) {
1907 domain.type = LTTNG_DOMAIN_LOG4J;
1908 } else if (opt_python) {
1909 domain.type = LTTNG_DOMAIN_PYTHON;
1910 }
1911
1912 if (!opt_kernel && opt_syscall) {
1913 WARN("--syscall will only work with the Kernel domain (-k)");
1914 ret = CMD_ERROR;
1915 goto end;
1916 }
1917
1918 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
1919 handle = lttng_create_handle(session_name, &domain);
1920 if (handle == NULL) {
1921 ret = CMD_FATAL;
1922 goto end;
1923 }
1924 }
1925
1926 if (session_name == NULL) {
1927 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
1928 && !opt_python) {
1929 ret = list_sessions(NULL);
1930 if (ret) {
1931 goto end;
1932 }
1933 }
1934 if (opt_kernel) {
1935 if (opt_syscall) {
1936 ret = list_syscalls();
1937 if (ret) {
1938 goto end;
1939 }
1940 } else {
1941 ret = list_kernel_events();
1942 if (ret) {
1943 goto end;
1944 }
1945 }
1946 }
1947 if (opt_userspace) {
1948 if (opt_fields) {
1949 ret = list_ust_event_fields();
1950 } else {
1951 ret = list_ust_events();
1952 }
1953 if (ret) {
1954 goto end;
1955 }
1956 }
1957 if (opt_jul || opt_log4j || opt_python) {
1958 ret = list_agent_events();
1959 if (ret) {
1960 goto end;
1961 }
1962 }
1963 } else {
1964 /* List session attributes */
1965 if (lttng_opt_mi) {
1966 /* Open element sessions
1967 * Present for xml consistency */
1968 ret = mi_lttng_sessions_open(writer);
1969 if (ret) {
1970 goto end;
1971 }
1972 }
1973 /* MI: the ouptut of list_sessions is an unclosed session element */
1974 ret = list_sessions(session_name);
1975 if (ret) {
1976 goto end;
1977 }
1978
1979 ret = list_rotate_settings(session_name);
1980 if (ret) {
1981 goto end;
1982 }
1983
1984 /* Domain listing */
1985 if (opt_domain) {
1986 ret = list_domains(session_name);
1987 goto end;
1988 }
1989
1990 /* Channel listing */
1991 if (opt_kernel || opt_userspace) {
1992 if (lttng_opt_mi) {
1993 /* Add of domains and domain element for xml
1994 * consistency and validation
1995 */
1996 ret = mi_lttng_domains_open(writer);
1997 if (ret) {
1998 goto end;
1999 }
2000
2001 /* Open domain and leave it open for
2002 * nested channels printing */
2003 ret = mi_lttng_domain(writer, &domain, 1);
2004 if (ret) {
2005 goto end;
2006 }
2007
2008 }
2009
2010
2011 /* Trackers */
2012 ret = list_trackers();
2013 if (ret) {
2014 goto end;
2015 }
2016
2017 /* Channels */
2018 ret = list_channels(opt_channel);
2019 if (ret) {
2020 goto end;
2021 }
2022
2023 if (lttng_opt_mi) {
2024 /* Close domain and domain element */
2025 ret = mi_lttng_close_multi_element(writer, 2);
2026 }
2027 if (ret) {
2028 goto end;
2029 }
2030
2031
2032 } else {
2033 int i, nb_domain;
2034
2035 /* We want all domain(s) */
2036 nb_domain = lttng_list_domains(session_name, &domains);
2037 if (nb_domain < 0) {
2038 ret = CMD_ERROR;
2039 ERR("%s", lttng_strerror(nb_domain));
2040 goto end;
2041 }
2042
2043 if (lttng_opt_mi) {
2044 ret = mi_lttng_domains_open(writer);
2045 if (ret) {
2046 ret = CMD_ERROR;
2047 goto end;
2048 }
2049 }
2050
2051 for (i = 0; i < nb_domain; i++) {
2052 switch (domains[i].type) {
2053 case LTTNG_DOMAIN_KERNEL:
2054 MSG("=== Domain: Kernel ===\n");
2055 break;
2056 case LTTNG_DOMAIN_UST:
2057 MSG("=== Domain: UST global ===\n");
2058 MSG("Buffer type: %s\n",
2059 domains[i].buf_type ==
2060 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
2061 break;
2062 case LTTNG_DOMAIN_JUL:
2063 MSG("=== Domain: JUL (Java Util Logging) ===\n");
2064 break;
2065 case LTTNG_DOMAIN_LOG4J:
2066 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
2067 break;
2068 case LTTNG_DOMAIN_PYTHON:
2069 MSG("=== Domain: Python (logging) ===\n");
2070 break;
2071 default:
2072 MSG("=== Domain: Unimplemented ===\n");
2073 break;
2074 }
2075
2076 if (lttng_opt_mi) {
2077 ret = mi_lttng_domain(writer, &domains[i], 1);
2078 if (ret) {
2079 ret = CMD_ERROR;
2080 goto end;
2081 }
2082 }
2083
2084 /* Clean handle before creating a new one */
2085 if (handle) {
2086 lttng_destroy_handle(handle);
2087 }
2088
2089 handle = lttng_create_handle(session_name, &domains[i]);
2090 if (handle == NULL) {
2091 ret = CMD_FATAL;
2092 goto end;
2093 }
2094
2095 if (domains[i].type == LTTNG_DOMAIN_JUL ||
2096 domains[i].type == LTTNG_DOMAIN_LOG4J ||
2097 domains[i].type == LTTNG_DOMAIN_PYTHON) {
2098 ret = list_session_agent_events();
2099 if (ret) {
2100 goto end;
2101 }
2102
2103 goto next_domain;
2104 }
2105
2106 switch (domains[i].type) {
2107 case LTTNG_DOMAIN_KERNEL:
2108 case LTTNG_DOMAIN_UST:
2109 ret = list_trackers();
2110 if (ret) {
2111 goto end;
2112 }
2113 break;
2114 default:
2115 break;
2116 }
2117
2118 ret = list_channels(opt_channel);
2119 if (ret) {
2120 goto end;
2121 }
2122
2123next_domain:
2124 if (lttng_opt_mi) {
2125 /* Close domain element */
2126 ret = mi_lttng_writer_close_element(writer);
2127 if (ret) {
2128 ret = CMD_ERROR;
2129 goto end;
2130 }
2131 }
2132
2133 }
2134 if (lttng_opt_mi) {
2135 /* Close the domains, session and sessions element */
2136 ret = mi_lttng_close_multi_element(writer, 3);
2137 if (ret) {
2138 ret = CMD_ERROR;
2139 goto end;
2140 }
2141 }
2142 }
2143 }
2144
2145 /* Mi closing */
2146 if (lttng_opt_mi) {
2147 /* Close output element */
2148 ret = mi_lttng_writer_close_element(writer);
2149 if (ret) {
2150 ret = CMD_ERROR;
2151 goto end;
2152 }
2153
2154 /* Command element close */
2155 ret = mi_lttng_writer_command_close(writer);
2156 if (ret) {
2157 ret = CMD_ERROR;
2158 goto end;
2159 }
2160 }
2161end:
2162 /* Mi clean-up */
2163 if (writer && mi_lttng_writer_destroy(writer)) {
2164 /* Preserve original error code */
2165 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2166 }
2167
2168 free(domains);
2169 if (handle) {
2170 lttng_destroy_handle(handle);
2171 }
2172
2173 poptFreeContext(pc);
2174 return ret;
2175}
This page took 0.05221 seconds and 4 git commands to generate.