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