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