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