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