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