Use pipe instead of eventfd() for notification command queue
[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
e50e81d3
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 */
1154 MSG("\n%sEvents:", indent4);
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
1172/*
9f19cc17
DG
1173 * Pretty print channel
1174 */
1175static void print_channel(struct lttng_channel *channel)
1176{
fb83fe64 1177 int ret;
cf0bcb51 1178 uint64_t discarded_events, lost_packets, monitor_timer_interval;
491d1539 1179 int64_t blocking_timeout;
fb83fe64
JD
1180
1181 ret = lttng_channel_get_discarded_event_count(channel,
1182 &discarded_events);
1183 if (ret) {
1184 ERR("Failed to retrieve discarded event count of channel");
1185 return;
1186 }
1187
1188 ret = lttng_channel_get_lost_packet_count(channel,
1189 &lost_packets);
1190 if (ret) {
1191 ERR("Failed to retrieve lost packet count of channel");
1192 return;
1193 }
1194
cf0bcb51
JG
1195 ret = lttng_channel_get_monitor_timer_interval(channel,
1196 &monitor_timer_interval);
1197 if (ret) {
1198 ERR("Failed to retrieve monitor interval of channel");
1199 return;
1200 }
1201
491d1539
MD
1202 ret = lttng_channel_get_blocking_timeout(channel,
1203 &blocking_timeout);
1204 if (ret) {
1205 ERR("Failed to retrieve blocking timeout of channel");
1206 return;
1207 }
1208
464dd62d 1209 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
9f19cc17
DG
1210
1211 MSG("%sAttributes:", indent4);
a5bbe8f6 1212 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
0d5a66ff 1213 MSG("%ssubbuffers size: %" PRIu64 " bytes", indent6, channel->attr.subbuf_size);
2f16a8f9 1214 MSG("%snumber of subbuffers: %" PRIu64, indent6, channel->attr.num_subbuf);
0d5a66ff
JG
1215 MSG("%sswitch timer interval: %u µs", indent6, channel->attr.switch_timer_interval);
1216 MSG("%sread timer interval: %u µs", indent6, channel->attr.read_timer_interval);
1217 MSG("%smonitor timer interval: %" PRIu64 " µs", indent6, monitor_timer_interval);
1218 MSG("%sblocking timeout: %" PRId64 " µs", indent6, blocking_timeout);
2f785fe7 1219 MSG("%strace file count: %" PRIu64, indent6, channel->attr.tracefile_count);
0d5a66ff 1220 MSG("%strace file size: %" PRIu64 " bytes", indent6, channel->attr.tracefile_size);
9f19cc17
DG
1221 switch (channel->attr.output) {
1222 case LTTNG_EVENT_SPLICE:
1223 MSG("%soutput: splice()", indent6);
1224 break;
1225 case LTTNG_EVENT_MMAP:
1226 MSG("%soutput: mmap()", indent6);
1227 break;
1228 }
e50e81d3
JG
1229
1230 if (!listed_session.snapshot_mode) {
1231 /*
1232 * The lost packet count is omitted for sessions in snapshot
1233 * mode as it is misleading: it would indicate the number of
1234 * packets that the consumer could not extract during the
1235 * course of recording the snapshot. It does not have the
1236 * same meaning as the "regular" lost packet count that
1237 * would result from the consumer not keeping up with
1238 * event production in an overwrite-mode channel.
1239 *
1240 * A more interesting statistic would be the number of
1241 * packets lost between the first and last extracted
1242 * packets of a given snapshot (which prevents most analyses).
1243 */
1244 goto skip_stats_printing;
1245 }
1246
1247 if (!channel->attr.overwrite) {
1248 MSG("%sdiscarded events: %" PRIu64, indent6, discarded_events);
1249 } else {
1250 MSG("%slost packets: %" PRIu64, indent6, lost_packets);
1251 }
1252skip_stats_printing:
1253 return;
9f19cc17
DG
1254}
1255
fb14d0d8
JR
1256/*
1257 * Machine interface
1258 * Print a list of channel
1259 *
1260 */
1261static int mi_list_channels(struct lttng_channel *channels, int count,
1262 const char *channel_name)
1263{
1264 int i, ret;
1265 unsigned int chan_found = 0;
1266
1267 /* Open channels element */
1268 ret = mi_lttng_channels_open(writer);
1269 if (ret) {
1270 goto error;
1271 }
1272
1273 for (i = 0; i < count; i++) {
1274 if (channel_name != NULL) {
1275 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1276 chan_found = 1;
1277 } else {
1278 continue;
1279 }
1280 }
1281
1282 /* Write channel element and leave it open */
1283 ret = mi_lttng_channel(writer, &channels[i], 1);
1284 if (ret) {
1285 goto error;
1286 }
1287
1288 /* Listing events per channel */
1289 ret = list_events(channels[i].name);
1290 if (ret) {
1291 goto error;
1292 }
1293
1294 /* Closing the channel element we opened earlier */
1295 ret = mi_lttng_writer_close_element(writer);
1296 if (ret) {
1297 goto error;
1298 }
1299
1300 if (chan_found) {
1301 break;
1302 }
1303 }
1304
1305 /* Close channels element */
1306 ret = mi_lttng_writer_close_element(writer);
1307 if (ret) {
1308 goto error;
1309 }
1310
1311error:
1312 return ret;
1313}
1314
9f19cc17
DG
1315/*
1316 * List channel(s) of session and domain.
f3ed775e 1317 *
9f19cc17 1318 * If channel_name is NULL, all channels are listed.
f3ed775e 1319 */
cd80958d 1320static int list_channels(const char *channel_name)
f3ed775e 1321{
9f19cc17
DG
1322 int count, i, ret = CMD_SUCCESS;
1323 unsigned int chan_found = 0;
1324 struct lttng_channel *channels = NULL;
f3ed775e 1325
3e25cb20 1326 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
9f19cc17 1327
cd80958d 1328 count = lttng_list_channels(handle, &channels);
f3ed775e 1329 if (count < 0) {
c7d620a2
DG
1330 switch (-count) {
1331 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
fb14d0d8
JR
1332 if (lttng_opt_mi) {
1333 /* When printing mi this is not an error
1334 * but an empty channels element */
1335 count = 0;
1336 } else {
1337 ret = CMD_SUCCESS;
1338 WARN("No kernel channel");
1339 goto error_channels;
1340 }
c7d620a2
DG
1341 break;
1342 default:
1343 /* We had a real error */
fb14d0d8
JR
1344 ret = CMD_ERROR;
1345 ERR("%s", lttng_strerror(count));
1346 goto error_channels;
60e835ca 1347 break;
c7d620a2 1348 }
f3ed775e
DG
1349 }
1350
fb14d0d8
JR
1351 if (lttng_opt_mi) {
1352 /* Mi print */
1353 ret = mi_list_channels(channels, count, channel_name);
1354 if (ret) {
1355 ret = CMD_ERROR;
1356 goto error;
1357 }
1358 } else {
1359 /* Pretty print */
ade7ce52 1360 if (count) {
fb14d0d8
JR
1361 MSG("Channels:\n-------------");
1362 }
9f19cc17 1363
fb14d0d8
JR
1364 for (i = 0; i < count; i++) {
1365 if (channel_name != NULL) {
1366 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
1367 chan_found = 1;
1368 } else {
1369 continue;
1370 }
1371 }
1372 print_channel(&channels[i]);
1373
1374 /* Listing events per channel */
1375 ret = list_events(channels[i].name);
1376 if (ret) {
1377 goto error;
1378 }
1379
1380 if (chan_found) {
1381 break;
9f19cc17
DG
1382 }
1383 }
9f19cc17 1384
fb14d0d8
JR
1385 if (!chan_found && channel_name != NULL) {
1386 ret = CMD_ERROR;
1387 ERR("Channel %s not found", channel_name);
1388 goto error;
9f19cc17 1389 }
fb14d0d8
JR
1390 }
1391error:
1392 free(channels);
9f19cc17 1393
fb14d0d8
JR
1394error_channels:
1395 return ret;
1396}
1397
a5dfbb9d
MD
1398/*
1399 * List tracker PID(s) of session and domain.
1400 */
1401static int list_tracker_pids(void)
1402{
ebbf5ab7
JR
1403 int ret = 0;
1404 int enabled;
a5dfbb9d 1405 int *pids = NULL;
6223570f 1406 size_t nr_pids;
a5dfbb9d
MD
1407
1408 ret = lttng_list_tracker_pids(handle,
1409 &enabled, &pids, &nr_pids);
1410 if (ret) {
1411 return ret;
1412 }
a5dfbb9d 1413 if (enabled) {
6223570f
JG
1414 int i;
1415 _MSG("PID tracker: [");
a5dfbb9d 1416
ebbf5ab7
JR
1417 /* Mi tracker_pid element*/
1418 if (writer) {
1419 /* Open tracker_pid and targets elements */
1420 ret = mi_lttng_pid_tracker_open(writer);
1421 if (ret) {
1422 goto end;
1423 }
1424 }
1425
a5dfbb9d
MD
1426 for (i = 0; i < nr_pids; i++) {
1427 if (i) {
1428 _MSG(",");
1429 }
1430 _MSG(" %d", pids[i]);
ebbf5ab7
JR
1431
1432 /* Mi */
1433 if (writer) {
1434 ret = mi_lttng_pid_target(writer, pids[i], 0);
1435 if (ret) {
1436 goto end;
1437 }
1438 }
a5dfbb9d 1439 }
6223570f 1440 _MSG(" ]\n\n");
ebbf5ab7
JR
1441
1442 /* Mi close tracker_pid and targets */
1443 if (writer) {
1444 ret = mi_lttng_close_multi_element(writer,2);
1445 if (ret) {
1446 goto end;
1447 }
1448 }
a5dfbb9d 1449 }
ebbf5ab7 1450end:
a5dfbb9d 1451 free(pids);
ebbf5ab7
JR
1452 return ret;
1453
1454}
1455
1456/*
1457 * List all tracker of a domain
1458 */
1459static int list_trackers(void)
1460{
1461 int ret;
1462
1463 /* Trackers listing */
1464 if (lttng_opt_mi) {
1465 ret = mi_lttng_trackers_open(writer);
1466 if (ret) {
1467 goto end;
1468 }
1469 }
1470
1471 /* pid tracker */
1472 ret = list_tracker_pids();
1473 if (ret) {
1474 goto end;
1475 }
1476
1477 if (lttng_opt_mi) {
1478 /* Close trackers element */
1479 ret = mi_lttng_writer_close_element(writer);
1480 if (ret) {
1481 goto end;
1482 }
1483 }
1484
1485end:
1486 return ret;
a5dfbb9d
MD
1487}
1488
fb14d0d8
JR
1489/*
1490 * Machine interface
1491 * Find the session with session_name as name
1492 * and print his informations.
1493 */
1494static int mi_list_session(const char *session_name,
1495 struct lttng_session *sessions, int count)
1496{
1497 int ret, i;
1498 unsigned int session_found = 0;
1499
1500 if (session_name == NULL) {
1501 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1502 goto end;
1503 }
1504
1505 for (i = 0; i < count; i++) {
1506 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1507 /* We need to leave it open to append other informations
1508 * like domain, channel, events etc.*/
1509 session_found = 1;
1510 ret = mi_lttng_session(writer, &sessions[i], 1);
1511 if (ret) {
1512 goto end;
1513 }
9f19cc17 1514 break;
f3ed775e 1515 }
f3ed775e
DG
1516 }
1517
fb14d0d8
JR
1518 if (!session_found) {
1519 ERR("Session '%s' not found", session_name);
1520 ret = -LTTNG_ERR_SESS_NOT_FOUND;
1521 goto end;
9f19cc17 1522 }
f3ed775e 1523
fb14d0d8
JR
1524end:
1525 return ret;
1526}
f3ed775e 1527
fb14d0d8
JR
1528/*
1529 * Machine interface
1530 * List all availables session
1531 */
1532static int mi_list_sessions(struct lttng_session *sessions, int count)
1533{
1534 int ret, i;
ae856491 1535
fb14d0d8
JR
1536 /* Opening sessions element */
1537 ret = mi_lttng_sessions_open(writer);
1538 if (ret) {
1539 goto end;
1540 }
1541
1542 /* Listing sessions */
1543 for (i = 0; i < count; i++) {
1544 ret = mi_lttng_session(writer, &sessions[i], 0);
1545 if (ret) {
1546 goto end;
1547 }
1548 }
1549
1550 /* Closing sessions element */
1551 ret = mi_lttng_writer_close_element(writer);
1552 if (ret) {
1553 goto end;
1554 }
1555
1556end:
f3ed775e
DG
1557 return ret;
1558}
1559
1560/*
9f19cc17 1561 * List available tracing session. List only basic information.
f3ed775e 1562 *
9f19cc17 1563 * If session_name is NULL, all sessions are listed.
f3ed775e 1564 */
9f19cc17 1565static int list_sessions(const char *session_name)
f3ed775e 1566{
fb14d0d8
JR
1567 int ret = CMD_SUCCESS;
1568 int count, i;
9f19cc17
DG
1569 unsigned int session_found = 0;
1570 struct lttng_session *sessions;
1571
1572 count = lttng_list_sessions(&sessions);
1573 DBG("Session count %d", count);
1574 if (count < 0) {
fb14d0d8
JR
1575 ret = CMD_ERROR;
1576 ERR("%s", lttng_strerror(count));
d32fb093 1577 goto end;
9f19cc17
DG
1578 }
1579
fb14d0d8
JR
1580 if (lttng_opt_mi) {
1581 /* Mi */
1582 if (session_name == NULL) {
1583 /* List all session */
1584 ret = mi_list_sessions(sessions, count);
1585 } else {
1586 /* Note : this return an open session element */
1587 ret = mi_list_session(session_name, sessions, count);
1588 }
1589 if (ret) {
1590 ret = CMD_ERROR;
1591 goto error;
1592 }
1593 } else {
1594 /* Pretty print */
1595 if (count == 0) {
1596 MSG("Currently no available tracing session");
fb14d0d8
JR
1597 goto end;
1598 }
9f19cc17 1599
fb14d0d8
JR
1600 if (session_name == NULL) {
1601 MSG("Available tracing sessions:");
1602 }
1603
1604
1605 for (i = 0; i < count; i++) {
1606 if (session_name != NULL) {
1607 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
1608 session_found = 1;
1609 MSG("Tracing session %s: [%s%s]", session_name,
1610 active_string(sessions[i].enabled),
1611 snapshot_string(sessions[i].snapshot_mode));
1612 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
e50e81d3
JG
1613 memcpy(&listed_session, &sessions[i],
1614 sizeof(listed_session));
fb14d0d8
JR
1615 break;
1616 }
1617 } else {
1618 MSG(" %d) %s (%s) [%s%s]", i + 1,
1619 sessions[i].name, sessions[i].path,
2cbf8fed
DG
1620 active_string(sessions[i].enabled),
1621 snapshot_string(sessions[i].snapshot_mode));
2f785fe7 1622 MSG("%sTrace path: %s", indent4, sessions[i].path);
fcbc96a2 1623 if (sessions[i].live_timer_interval != 0) {
e50e81d3 1624 MSG("%sLive timer interval: %u µs", indent4,
fcbc96a2
JG
1625 sessions[i].live_timer_interval);
1626 }
1627 MSG("");
9f19cc17 1628 }
fb14d0d8
JR
1629 }
1630
1631 if (!session_found && session_name != NULL) {
1632 ERR("Session '%s' not found", session_name);
1633 ret = CMD_ERROR;
1634 goto error;
1635 }
1636
1637 if (session_name == NULL) {
1638 MSG("\nUse lttng list <session_name> for more details");
9f19cc17
DG
1639 }
1640 }
1641
fb14d0d8 1642error:
9f19cc17 1643 free(sessions);
fb14d0d8
JR
1644end:
1645 return ret;
1646}
9f19cc17 1647
fb14d0d8
JR
1648
1649/*
1650 * Machine Interface
1651 * list available domain(s) for a session.
1652 */
1653static int mi_list_domains(struct lttng_domain *domains, int count)
1654{
1655 int i, ret;
1656 /* Open domains element */
1657 ret = mi_lttng_domains_open(writer);
1658 if (ret) {
1659 goto end;
9f19cc17
DG
1660 }
1661
fb14d0d8
JR
1662 for (i = 0; i < count; i++) {
1663 ret = mi_lttng_domain(writer, &domains[i] , 0);
1664 if (ret) {
1665 goto end;
1666 }
9f19cc17 1667 }
f3ed775e 1668
fb14d0d8
JR
1669 /* Closing domains element */
1670 ret = mi_lttng_writer_close_element(writer);
1671 if (ret) {
1672 goto end;
1673 }
d32fb093 1674end:
f3ed775e
DG
1675 return ret;
1676}
f3ed775e
DG
1677
1678/*
9f19cc17 1679 * List available domain(s) for a session.
f3ed775e 1680 */
330be774 1681static int list_domains(const char *session_name)
f3ed775e 1682{
9f19cc17
DG
1683 int i, count, ret = CMD_SUCCESS;
1684 struct lttng_domain *domains = NULL;
1685
9f19cc17 1686
330be774 1687 count = lttng_list_domains(session_name, &domains);
9f19cc17 1688 if (count < 0) {
fb14d0d8
JR
1689 ret = CMD_ERROR;
1690 ERR("%s", lttng_strerror(count));
9f19cc17
DG
1691 goto end;
1692 }
1693
fb14d0d8
JR
1694 if (lttng_opt_mi) {
1695 /* Mi output */
1696 ret = mi_list_domains(domains, count);
1697 if (ret) {
1698 ret = CMD_ERROR;
1699 goto error;
1700 }
1701 } else {
1702 /* Pretty print */
1703 MSG("Domains:\n-------------");
1704 if (count == 0) {
1705 MSG(" None");
1706 goto end;
1707 }
1708
1709 for (i = 0; i < count; i++) {
1710 switch (domains[i].type) {
1711 case LTTNG_DOMAIN_KERNEL:
1712 MSG(" - Kernel");
1713 break;
1714 case LTTNG_DOMAIN_UST:
1715 MSG(" - UST global");
1716 break;
1717 case LTTNG_DOMAIN_JUL:
1718 MSG(" - JUL (Java Util Logging)");
1719 break;
5cdb6027
DG
1720 case LTTNG_DOMAIN_LOG4J:
1721 MSG(" - LOG4j (Logging for Java)");
1722 break;
0e115563
DG
1723 case LTTNG_DOMAIN_PYTHON:
1724 MSG(" - Python (logging)");
1725 break;
fb14d0d8
JR
1726 default:
1727 break;
1728 }
9f19cc17
DG
1729 }
1730 }
1731
fb14d0d8 1732error:
9f19cc17
DG
1733 free(domains);
1734
fb14d0d8 1735end:
9f19cc17 1736 return ret;
f3ed775e 1737}
f3ed775e
DG
1738
1739/*
9f19cc17 1740 * The 'list <options>' first level command
f3ed775e
DG
1741 */
1742int cmd_list(int argc, const char **argv)
1743{
c617c0c6 1744 int opt, ret = CMD_SUCCESS;
9f19cc17 1745 const char *session_name;
f3ed775e 1746 static poptContext pc;
9f19cc17
DG
1747 struct lttng_domain domain;
1748 struct lttng_domain *domains = NULL;
f3ed775e 1749
441c16a7
MD
1750 memset(&domain, 0, sizeof(domain));
1751
9f19cc17 1752 if (argc < 1) {
ca1c3607 1753 ret = CMD_ERROR;
f3ed775e
DG
1754 goto end;
1755 }
1756
1757 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1758 poptReadDefaultConfig(pc, 0);
1759
1760 while ((opt = poptGetNextOpt(pc)) != -1) {
1761 switch (opt) {
1762 case OPT_HELP:
4ba92f18 1763 SHOW_HELP();
f3ed775e 1764 goto end;
eeac7d46
MD
1765 case OPT_USERSPACE:
1766 opt_userspace = 1;
eeac7d46 1767 break;
679b4943
SM
1768 case OPT_LIST_OPTIONS:
1769 list_cmd_options(stdout, long_options);
679b4943 1770 goto end;
f3ed775e 1771 default:
f3ed775e
DG
1772 ret = CMD_UNDEFINED;
1773 goto end;
1774 }
1775 }
1776
fb14d0d8
JR
1777 /* Mi check */
1778 if (lttng_opt_mi) {
1779 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1780 if (!writer) {
1781 ret = CMD_ERROR;
1782 goto end;
1783 }
1784
1785 /* Open command element */
1786 ret = mi_lttng_writer_command_open(writer,
1787 mi_lttng_element_command_list);
1788 if (ret) {
1789 ret = CMD_ERROR;
1790 goto end;
1791 }
1792
1793 /* Open output element */
1794 ret = mi_lttng_writer_open_element(writer,
1795 mi_lttng_element_command_output);
1796 if (ret) {
1797 ret = CMD_ERROR;
1798 goto end;
1799 }
1800 }
1801
9f19cc17
DG
1802 /* Get session name (trailing argument) */
1803 session_name = poptGetArg(pc);
b551a063 1804 DBG2("Session name: %s", session_name);
9f19cc17 1805
cd80958d
DG
1806 if (opt_kernel) {
1807 domain.type = LTTNG_DOMAIN_KERNEL;
b551a063
DG
1808 } else if (opt_userspace) {
1809 DBG2("Listing userspace global domain");
1810 domain.type = LTTNG_DOMAIN_UST;
3c6a091f
DG
1811 } else if (opt_jul) {
1812 DBG2("Listing JUL domain");
1813 domain.type = LTTNG_DOMAIN_JUL;
edf60534 1814 } else if (opt_log4j) {
5cdb6027 1815 domain.type = LTTNG_DOMAIN_LOG4J;
0e115563
DG
1816 } else if (opt_python) {
1817 domain.type = LTTNG_DOMAIN_PYTHON;
cd80958d
DG
1818 }
1819
834978fd
DG
1820 if (!opt_kernel && opt_syscall) {
1821 WARN("--syscall will only work with the Kernel domain (-k)");
1822 ret = CMD_ERROR;
1823 goto end;
1824 }
1825
0e115563 1826 if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
aa5de748
MD
1827 handle = lttng_create_handle(session_name, &domain);
1828 if (handle == NULL) {
1829 ret = CMD_FATAL;
1830 goto end;
1831 }
cd80958d
DG
1832 }
1833
9f19cc17 1834 if (session_name == NULL) {
0e115563
DG
1835 if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
1836 && !opt_python) {
b551a063 1837 ret = list_sessions(NULL);
fb14d0d8 1838 if (ret) {
b551a063
DG
1839 goto end;
1840 }
1841 }
9f19cc17 1842 if (opt_kernel) {
834978fd
DG
1843 if (opt_syscall) {
1844 ret = list_syscalls();
1845 if (ret) {
1846 goto end;
1847 }
1848 } else {
1849 ret = list_kernel_events();
1850 if (ret) {
1851 goto end;
1852 }
9f19cc17 1853 }
b551a063
DG
1854 }
1855 if (opt_userspace) {
f37d259d
MD
1856 if (opt_fields) {
1857 ret = list_ust_event_fields();
1858 } else {
1859 ret = list_ust_events();
1860 }
fb14d0d8 1861 if (ret) {
9f19cc17
DG
1862 goto end;
1863 }
1864 }
0e115563 1865 if (opt_jul || opt_log4j || opt_python) {
5cdb6027 1866 ret = list_agent_events();
fb14d0d8 1867 if (ret) {
3c6a091f
DG
1868 goto end;
1869 }
1870 }
9f19cc17
DG
1871 } else {
1872 /* List session attributes */
fb14d0d8
JR
1873 if (lttng_opt_mi) {
1874 /* Open element sessions
1875 * Present for xml consistency */
1876 ret = mi_lttng_sessions_open(writer);
1877 if (ret) {
1878 goto end;
1879 }
1880 }
1881 /* MI: the ouptut of list_sessions is an unclosed session element */
9f19cc17 1882 ret = list_sessions(session_name);
fb14d0d8 1883 if (ret) {
9f19cc17
DG
1884 goto end;
1885 }
1886
1887 /* Domain listing */
1888 if (opt_domain) {
330be774 1889 ret = list_domains(session_name);
9f19cc17
DG
1890 goto end;
1891 }
1892
fb14d0d8 1893 /* Channel listing */
1c3de747 1894 if (opt_kernel || opt_userspace) {
fb14d0d8
JR
1895 if (lttng_opt_mi) {
1896 /* Add of domains and domain element for xml
1897 * consistency and validation
1898 */
1899 ret = mi_lttng_domains_open(writer);
1900 if (ret) {
1901 goto end;
1902 }
1903
1904 /* Open domain and leave it open for
1905 * nested channels printing */
1906 ret = mi_lttng_domain(writer, &domain, 1);
1907 if (ret) {
1908 goto end;
1909 }
1910
1911 }
1912
ebbf5ab7
JR
1913
1914 /* Trackers */
1915 ret = list_trackers();
a5dfbb9d
MD
1916 if (ret) {
1917 goto end;
1918 }
1919
ebbf5ab7 1920 /* Channels */
cd80958d 1921 ret = list_channels(opt_channel);
fb14d0d8
JR
1922 if (ret) {
1923 goto end;
1924 }
1925
1926 if (lttng_opt_mi) {
1927 /* Close domain and domain element */
1928 ret = mi_lttng_close_multi_element(writer, 2);
1929 }
1930 if (ret) {
9f19cc17
DG
1931 goto end;
1932 }
fb14d0d8
JR
1933
1934
9f19cc17 1935 } else {
c617c0c6
MD
1936 int i, nb_domain;
1937
9f19cc17 1938 /* We want all domain(s) */
330be774 1939 nb_domain = lttng_list_domains(session_name, &domains);
b551a063 1940 if (nb_domain < 0) {
fb14d0d8
JR
1941 ret = CMD_ERROR;
1942 ERR("%s", lttng_strerror(nb_domain));
9f19cc17
DG
1943 goto end;
1944 }
1945
fb14d0d8
JR
1946 if (lttng_opt_mi) {
1947 ret = mi_lttng_domains_open(writer);
1948 if (ret) {
1949 ret = CMD_ERROR;
1950 goto end;
1951 }
1952 }
1953
b551a063 1954 for (i = 0; i < nb_domain; i++) {
9f19cc17
DG
1955 switch (domains[i].type) {
1956 case LTTNG_DOMAIN_KERNEL:
1957 MSG("=== Domain: Kernel ===\n");
1958 break;
b551a063
DG
1959 case LTTNG_DOMAIN_UST:
1960 MSG("=== Domain: UST global ===\n");
88c5f0d8
DG
1961 MSG("Buffer type: %s\n",
1962 domains[i].buf_type ==
1963 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
b551a063 1964 break;
3c6a091f
DG
1965 case LTTNG_DOMAIN_JUL:
1966 MSG("=== Domain: JUL (Java Util Logging) ===\n");
1967 break;
5cdb6027
DG
1968 case LTTNG_DOMAIN_LOG4J:
1969 MSG("=== Domain: LOG4j (Logging for Java) ===\n");
1970 break;
0e115563
DG
1971 case LTTNG_DOMAIN_PYTHON:
1972 MSG("=== Domain: Python (logging) ===\n");
1973 break;
9f19cc17
DG
1974 default:
1975 MSG("=== Domain: Unimplemented ===\n");
1976 break;
1977 }
1978
fb14d0d8
JR
1979 if (lttng_opt_mi) {
1980 ret = mi_lttng_domain(writer, &domains[i], 1);
1981 if (ret) {
1982 ret = CMD_ERROR;
1983 goto end;
1984 }
1985 }
1986
cd80958d 1987 /* Clean handle before creating a new one */
aa5de748
MD
1988 if (handle) {
1989 lttng_destroy_handle(handle);
1990 }
cd80958d
DG
1991
1992 handle = lttng_create_handle(session_name, &domains[i]);
1993 if (handle == NULL) {
ca1c3607 1994 ret = CMD_FATAL;
cd80958d
DG
1995 goto end;
1996 }
1997
5cdb6027 1998 if (domains[i].type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1999 domains[i].type == LTTNG_DOMAIN_LOG4J ||
2000 domains[i].type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 2001 ret = list_session_agent_events();
fb14d0d8 2002 if (ret) {
3c6a091f
DG
2003 goto end;
2004 }
cd7b2927
PP
2005
2006 goto next_domain;
3c6a091f 2007 }
a5dfbb9d
MD
2008
2009 switch (domains[i].type) {
2010 case LTTNG_DOMAIN_KERNEL:
2011 case LTTNG_DOMAIN_UST:
ebbf5ab7 2012 ret = list_trackers();
a5dfbb9d
MD
2013 if (ret) {
2014 goto end;
2015 }
2016 break;
2017 default:
2018 break;
2019 }
3c6a091f 2020
cd80958d 2021 ret = list_channels(opt_channel);
fb14d0d8
JR
2022 if (ret) {
2023 goto end;
2024 }
2025
cd7b2927 2026next_domain:
fb14d0d8
JR
2027 if (lttng_opt_mi) {
2028 /* Close domain element */
2029 ret = mi_lttng_writer_close_element(writer);
2030 if (ret) {
2031 ret = CMD_ERROR;
2032 goto end;
2033 }
2034 }
2035
2036 }
2037 if (lttng_opt_mi) {
2038 /* Close the domains, session and sessions element */
2039 ret = mi_lttng_close_multi_element(writer, 3);
2040 if (ret) {
2041 ret = CMD_ERROR;
9f19cc17
DG
2042 goto end;
2043 }
2044 }
2045 }
f3ed775e
DG
2046 }
2047
fb14d0d8
JR
2048 /* Mi closing */
2049 if (lttng_opt_mi) {
2050 /* Close output element */
2051 ret = mi_lttng_writer_close_element(writer);
2052 if (ret) {
2053 ret = CMD_ERROR;
2054 goto end;
2055 }
2056
2057 /* Command element close */
2058 ret = mi_lttng_writer_command_close(writer);
2059 if (ret) {
2060 ret = CMD_ERROR;
2061 goto end;
2062 }
2063 }
f3ed775e 2064end:
fb14d0d8
JR
2065 /* Mi clean-up */
2066 if (writer && mi_lttng_writer_destroy(writer)) {
2067 /* Preserve original error code */
2068 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
2069 }
2070
0e428499 2071 free(domains);
aa5de748
MD
2072 if (handle) {
2073 lttng_destroy_handle(handle);
2074 }
cd80958d 2075
ca1c3607 2076 poptFreeContext(pc);
f3ed775e
DG
2077 return ret;
2078}
This page took 0.244307 seconds and 4 git commands to generate.