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