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