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