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