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