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