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