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