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