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