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