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