0b1eff265b4635f4d1ffb0a568e5314d856256f8
[lttng-tools.git] / src / bin / lttng / commands / list.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <inttypes.h>
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "../command.h"
27
28 static int opt_userspace;
29 static int opt_kernel;
30 static int opt_jul;
31 static char *opt_channel;
32 static int opt_domain;
33 static int opt_fields;
34 #if 0
35 /* Not implemented yet */
36 static char *opt_cmd_name;
37 static pid_t opt_pid;
38 #endif
39
40 const char *indent4 = " ";
41 const char *indent6 = " ";
42 const char *indent8 = " ";
43
44 enum {
45 OPT_HELP = 1,
46 OPT_USERSPACE,
47 OPT_LIST_OPTIONS,
48 };
49
50 static struct lttng_handle *handle;
51
52 static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
56 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
57 #if 0
58 /* Not implemented yet */
59 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
61 #else
62 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
63 #endif
64 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
65 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
66 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
67 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
68 {0, 0, 0, 0, 0, 0, 0}
69 };
70
71 /*
72 * usage
73 */
74 static void usage(FILE *ofp)
75 {
76 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
77 fprintf(ofp, "\n");
78 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
79 fprintf(ofp, "\n");
80 fprintf(ofp, "Without a session, -k lists available kernel events\n");
81 fprintf(ofp, "Without a session, -u lists available userspace events\n");
82 fprintf(ofp, "\n");
83 fprintf(ofp, " -h, --help Show this help\n");
84 fprintf(ofp, " --list-options Simple listing of options\n");
85 fprintf(ofp, " -k, --kernel Select kernel domain\n");
86 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
87 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
88 fprintf(ofp, " -f, --fields List event fields.\n");
89 #if 0
90 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
91 #endif
92 fprintf(ofp, "\n");
93 fprintf(ofp, "Session Options:\n");
94 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
95 fprintf(ofp, " -d, --domain List available domain(s)\n");
96 fprintf(ofp, "\n");
97 }
98
99 /*
100 * Get command line from /proc for a specific pid.
101 *
102 * On success, return an allocated string pointer to the proc cmdline.
103 * On error, return NULL.
104 */
105 static char *get_cmdline_by_pid(pid_t pid)
106 {
107 int ret;
108 FILE *fp;
109 char *cmdline = NULL;
110 char path[20]; /* Can't go bigger than /proc/65535/cmdline */
111
112 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
113 fp = fopen(path, "r");
114 if (fp == NULL) {
115 goto end;
116 }
117
118 /* Caller must free() *cmdline */
119 cmdline = malloc(PATH_MAX);
120 if (!cmdline) {
121 perror("malloc cmdline");
122 goto end;
123 }
124 ret = fread(cmdline, 1, PATH_MAX, fp);
125 if (ret < 0) {
126 perror("fread proc list");
127 }
128 fclose(fp);
129
130 end:
131 return cmdline;
132 }
133
134 static
135 const char *active_string(int value)
136 {
137 switch (value) {
138 case 0: return "inactive";
139 case 1: return "active";
140 case -1: return "";
141 default: return NULL;
142 }
143 }
144
145 static const char *snapshot_string(int value)
146 {
147 switch (value) {
148 case 1:
149 return " snapshot";
150 default:
151 return "";
152 }
153 }
154
155 static
156 const char *enabled_string(int value)
157 {
158 switch (value) {
159 case 0: return " [disabled]";
160 case 1: return " [enabled]";
161 case -1: return "";
162 default: return NULL;
163 }
164 }
165
166 static
167 const char *filter_string(int value)
168 {
169 switch (value) {
170 case 1: return " [with filter]";
171 default: return "";
172 }
173 }
174
175 static
176 const char *exclusion_string(int value)
177 {
178 switch (value) {
179 case 1: return " [has exclusions]";
180 default: return "";
181 }
182 }
183
184 static const char *loglevel_string(int value)
185 {
186 switch (value) {
187 case -1:
188 return "";
189 case LTTNG_LOGLEVEL_EMERG:
190 return "TRACE_EMERG";
191 case LTTNG_LOGLEVEL_ALERT:
192 return "TRACE_ALERT";
193 case LTTNG_LOGLEVEL_CRIT:
194 return "TRACE_CRIT";
195 case LTTNG_LOGLEVEL_ERR:
196 return "TRACE_ERR";
197 case LTTNG_LOGLEVEL_WARNING:
198 return "TRACE_WARNING";
199 case LTTNG_LOGLEVEL_NOTICE:
200 return "TRACE_NOTICE";
201 case LTTNG_LOGLEVEL_INFO:
202 return "TRACE_INFO";
203 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
204 return "TRACE_DEBUG_SYSTEM";
205 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
206 return "TRACE_DEBUG_PROGRAM";
207 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
208 return "TRACE_DEBUG_PROCESS";
209 case LTTNG_LOGLEVEL_DEBUG_MODULE:
210 return "TRACE_DEBUG_MODULE";
211 case LTTNG_LOGLEVEL_DEBUG_UNIT:
212 return "TRACE_DEBUG_UNIT";
213 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
214 return "TRACE_DEBUG_FUNCTION";
215 case LTTNG_LOGLEVEL_DEBUG_LINE:
216 return "TRACE_DEBUG_LINE";
217 case LTTNG_LOGLEVEL_DEBUG:
218 return "TRACE_DEBUG";
219 default:
220 return "<<UNKNOWN>>";
221 }
222 }
223
224 static const char *logleveltype_string(enum lttng_loglevel_type value)
225 {
226 switch (value) {
227 case LTTNG_EVENT_LOGLEVEL_ALL:
228 return ":";
229 case LTTNG_EVENT_LOGLEVEL_RANGE:
230 return " <=";
231 case LTTNG_EVENT_LOGLEVEL_SINGLE:
232 return " ==";
233 default:
234 return " <<TYPE UNKN>>";
235 }
236 }
237
238 /*
239 * Pretty print single event.
240 */
241 static void print_events(struct lttng_event *event)
242 {
243 switch (event->type) {
244 case LTTNG_EVENT_TRACEPOINT:
245 {
246 if (event->loglevel != -1) {
247 MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s",
248 indent6,
249 event->name,
250 logleveltype_string(event->loglevel_type),
251 loglevel_string(event->loglevel),
252 event->loglevel,
253 enabled_string(event->enabled),
254 exclusion_string(event->exclusion),
255 filter_string(event->filter));
256 } else {
257 MSG("%s%s (type: tracepoint)%s%s%s",
258 indent6,
259 event->name,
260 enabled_string(event->enabled),
261 exclusion_string(event->exclusion),
262 filter_string(event->filter));
263 }
264 break;
265 }
266 case LTTNG_EVENT_FUNCTION:
267 MSG("%s%s (type: function)%s%s", indent6,
268 event->name, enabled_string(event->enabled),
269 filter_string(event->filter));
270 if (event->attr.probe.addr != 0) {
271 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
272 } else {
273 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
274 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
275 }
276 break;
277 case LTTNG_EVENT_PROBE:
278 MSG("%s%s (type: probe)%s%s", indent6,
279 event->name, enabled_string(event->enabled),
280 filter_string(event->filter));
281 if (event->attr.probe.addr != 0) {
282 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
283 } else {
284 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
285 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
286 }
287 break;
288 case LTTNG_EVENT_FUNCTION_ENTRY:
289 MSG("%s%s (type: function)%s%s", indent6,
290 event->name, enabled_string(event->enabled),
291 filter_string(event->filter));
292 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
293 break;
294 case LTTNG_EVENT_SYSCALL:
295 MSG("%ssyscalls (type: syscall)%s%s", indent6,
296 enabled_string(event->enabled),
297 filter_string(event->filter));
298 break;
299 case LTTNG_EVENT_NOOP:
300 MSG("%s (type: noop)%s%s", indent6,
301 enabled_string(event->enabled),
302 filter_string(event->filter));
303 break;
304 case LTTNG_EVENT_ALL:
305 /* We should never have "all" events in list. */
306 assert(0);
307 break;
308 }
309 }
310
311 static const char *field_type(struct lttng_event_field *field)
312 {
313 switch(field->type) {
314 case LTTNG_EVENT_FIELD_INTEGER:
315 return "integer";
316 case LTTNG_EVENT_FIELD_ENUM:
317 return "enum";
318 case LTTNG_EVENT_FIELD_FLOAT:
319 return "float";
320 case LTTNG_EVENT_FIELD_STRING:
321 return "string";
322 case LTTNG_EVENT_FIELD_OTHER:
323 default: /* fall-through */
324 return "unknown";
325 }
326 }
327
328 /*
329 * Pretty print single event fields.
330 */
331 static void print_event_field(struct lttng_event_field *field)
332 {
333 if (!field->field_name[0]) {
334 return;
335 }
336 MSG("%sfield: %s (%s)%s", indent8, field->field_name,
337 field_type(field), field->nowrite ? " [no write]" : "");
338 }
339
340 static int list_jul_events(void)
341 {
342 int i, size;
343 struct lttng_domain domain;
344 struct lttng_handle *handle;
345 struct lttng_event *event_list;
346 pid_t cur_pid = 0;
347 char *cmdline = NULL;
348
349 DBG("Getting JUL tracing events");
350
351 memset(&domain, 0, sizeof(domain));
352 domain.type = LTTNG_DOMAIN_JUL;
353
354 handle = lttng_create_handle(NULL, &domain);
355 if (handle == NULL) {
356 goto error;
357 }
358
359 size = lttng_list_tracepoints(handle, &event_list);
360 if (size < 0) {
361 ERR("Unable to list JUL events: %s", lttng_strerror(size));
362 lttng_destroy_handle(handle);
363 return size;
364 }
365
366 MSG("JUL events (Logger name):\n-------------------------");
367
368 if (size == 0) {
369 MSG("None");
370 }
371
372 for (i = 0; i < size; i++) {
373 if (cur_pid != event_list[i].pid) {
374 cur_pid = event_list[i].pid;
375 cmdline = get_cmdline_by_pid(cur_pid);
376 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
377 free(cmdline);
378 }
379 MSG("%s- %s", indent6, event_list[i].name);
380 }
381
382 MSG("");
383
384 free(event_list);
385 lttng_destroy_handle(handle);
386
387 return CMD_SUCCESS;
388
389 error:
390 lttng_destroy_handle(handle);
391 return -1;
392 }
393
394 /*
395 * Ask session daemon for all user space tracepoints available.
396 */
397 static int list_ust_events(void)
398 {
399 int i, size;
400 struct lttng_domain domain;
401 struct lttng_handle *handle;
402 struct lttng_event *event_list;
403 pid_t cur_pid = 0;
404 char *cmdline = NULL;
405
406 memset(&domain, 0, sizeof(domain));
407
408 DBG("Getting UST tracing events");
409
410 domain.type = LTTNG_DOMAIN_UST;
411
412 handle = lttng_create_handle(NULL, &domain);
413 if (handle == NULL) {
414 goto error;
415 }
416
417 size = lttng_list_tracepoints(handle, &event_list);
418 if (size < 0) {
419 ERR("Unable to list UST events: %s", lttng_strerror(size));
420 lttng_destroy_handle(handle);
421 return size;
422 }
423
424 MSG("UST events:\n-------------");
425
426 if (size == 0) {
427 MSG("None");
428 }
429
430 for (i = 0; i < size; i++) {
431 if (cur_pid != event_list[i].pid) {
432 cur_pid = event_list[i].pid;
433 cmdline = get_cmdline_by_pid(cur_pid);
434 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
435 free(cmdline);
436 }
437 print_events(&event_list[i]);
438 }
439
440 MSG("");
441
442 free(event_list);
443 lttng_destroy_handle(handle);
444
445 return CMD_SUCCESS;
446
447 error:
448 lttng_destroy_handle(handle);
449 return -1;
450 }
451
452 /*
453 * Ask session daemon for all user space tracepoint fields available.
454 */
455 static int list_ust_event_fields(void)
456 {
457 int i, size;
458 struct lttng_domain domain;
459 struct lttng_handle *handle;
460 struct lttng_event_field *event_field_list;
461 pid_t cur_pid = 0;
462 char *cmdline = NULL;
463
464 struct lttng_event cur_event;
465
466 memset(&domain, 0, sizeof(domain));
467 memset(&cur_event, 0, sizeof(cur_event));
468
469 DBG("Getting UST tracing event fields");
470
471 domain.type = LTTNG_DOMAIN_UST;
472
473 handle = lttng_create_handle(NULL, &domain);
474 if (handle == NULL) {
475 goto error;
476 }
477
478 size = lttng_list_tracepoint_fields(handle, &event_field_list);
479 if (size < 0) {
480 ERR("Unable to list UST event fields: %s", lttng_strerror(size));
481 lttng_destroy_handle(handle);
482 return size;
483 }
484
485 MSG("UST events:\n-------------");
486
487 if (size == 0) {
488 MSG("None");
489 }
490
491 for (i = 0; i < size; i++) {
492 if (cur_pid != event_field_list[i].event.pid) {
493 cur_pid = event_field_list[i].event.pid;
494 cmdline = get_cmdline_by_pid(cur_pid);
495 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
496 free(cmdline);
497 /* Wipe current event since we are about to print a new PID. */
498 memset(&cur_event, 0, sizeof(cur_event));
499 }
500 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
501 print_events(&event_field_list[i].event);
502 memcpy(&cur_event, &event_field_list[i].event,
503 sizeof(cur_event));
504 }
505 print_event_field(&event_field_list[i]);
506 }
507
508 MSG("");
509
510 free(event_field_list);
511 lttng_destroy_handle(handle);
512
513 return CMD_SUCCESS;
514
515 error:
516 lttng_destroy_handle(handle);
517 return -1;
518 }
519
520 /*
521 * Ask for all trace events in the kernel and pretty print them.
522 */
523 static int list_kernel_events(void)
524 {
525 int i, size;
526 struct lttng_domain domain;
527 struct lttng_handle *handle;
528 struct lttng_event *event_list;
529
530 memset(&domain, 0, sizeof(domain));
531
532 DBG("Getting kernel tracing events");
533
534 domain.type = LTTNG_DOMAIN_KERNEL;
535
536 handle = lttng_create_handle(NULL, &domain);
537 if (handle == NULL) {
538 goto error;
539 }
540
541 size = lttng_list_tracepoints(handle, &event_list);
542 if (size < 0) {
543 ERR("Unable to list kernel events: %s", lttng_strerror(size));
544 lttng_destroy_handle(handle);
545 return size;
546 }
547
548 MSG("Kernel events:\n-------------");
549
550 for (i = 0; i < size; i++) {
551 print_events(&event_list[i]);
552 }
553
554 MSG("");
555
556 free(event_list);
557
558 lttng_destroy_handle(handle);
559 return CMD_SUCCESS;
560
561 error:
562 lttng_destroy_handle(handle);
563 return -1;
564 }
565
566 /*
567 * List JUL events for a specific session using the handle.
568 *
569 * Return CMD_SUCCESS on success else a negative value.
570 */
571 static int list_session_jul_events(void)
572 {
573 int ret, count, i;
574 struct lttng_event *events = NULL;
575
576 count = lttng_list_events(handle, "", &events);
577 if (count < 0) {
578 ret = count;
579 ERR("%s", lttng_strerror(ret));
580 goto error;
581 }
582
583 MSG("Events (Logger name):\n---------------------");
584 if (count == 0) {
585 MSG("%sNone\n", indent6);
586 goto end;
587 }
588
589 for (i = 0; i < count; i++) {
590 MSG("%s- %s%s", indent4, events[i].name,
591 enabled_string(events[i].enabled));
592 }
593
594 MSG("");
595
596 end:
597 free(events);
598 ret = CMD_SUCCESS;
599
600 error:
601 return ret;
602 }
603
604 /*
605 * List events of channel of session and domain.
606 */
607 static int list_events(const char *channel_name)
608 {
609 int ret, count, i;
610 struct lttng_event *events = NULL;
611
612 count = lttng_list_events(handle, channel_name, &events);
613 if (count < 0) {
614 ret = count;
615 ERR("%s", lttng_strerror(ret));
616 goto error;
617 }
618
619 MSG("\n%sEvents:", indent4);
620 if (count == 0) {
621 MSG("%sNone\n", indent6);
622 goto end;
623 }
624
625 for (i = 0; i < count; i++) {
626 print_events(&events[i]);
627 }
628
629 MSG("");
630
631 end:
632 free(events);
633 ret = CMD_SUCCESS;
634
635 error:
636 return ret;
637 }
638
639 /*
640 * Pretty print channel
641 */
642 static void print_channel(struct lttng_channel *channel)
643 {
644 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
645
646 MSG("%sAttributes:", indent4);
647 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
648 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
649 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
650 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
651 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
652 switch (channel->attr.output) {
653 case LTTNG_EVENT_SPLICE:
654 MSG("%soutput: splice()", indent6);
655 break;
656 case LTTNG_EVENT_MMAP:
657 MSG("%soutput: mmap()", indent6);
658 break;
659 }
660 }
661
662 /*
663 * List channel(s) of session and domain.
664 *
665 * If channel_name is NULL, all channels are listed.
666 */
667 static int list_channels(const char *channel_name)
668 {
669 int count, i, ret = CMD_SUCCESS;
670 unsigned int chan_found = 0;
671 struct lttng_channel *channels = NULL;
672
673 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
674
675 count = lttng_list_channels(handle, &channels);
676 if (count < 0) {
677 switch (-count) {
678 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
679 ret = CMD_SUCCESS;
680 WARN("No kernel channel");
681 break;
682 default:
683 /* We had a real error */
684 ret = count;
685 ERR("%s", lttng_strerror(ret));
686 break;
687 }
688 goto error_channels;
689 }
690
691 if (channel_name == NULL) {
692 MSG("Channels:\n-------------");
693 }
694
695 for (i = 0; i < count; i++) {
696 if (channel_name != NULL) {
697 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
698 chan_found = 1;
699 } else {
700 continue;
701 }
702 }
703 print_channel(&channels[i]);
704
705 /* Listing events per channel */
706 ret = list_events(channels[i].name);
707 if (ret < 0) {
708 ERR("%s", lttng_strerror(ret));
709 }
710
711 if (chan_found) {
712 break;
713 }
714 }
715
716 if (!chan_found && channel_name != NULL) {
717 ERR("Channel %s not found", channel_name);
718 goto error;
719 }
720
721 ret = CMD_SUCCESS;
722
723 error:
724 free(channels);
725
726 error_channels:
727 return ret;
728 }
729
730 /*
731 * List available tracing session. List only basic information.
732 *
733 * If session_name is NULL, all sessions are listed.
734 */
735 static int list_sessions(const char *session_name)
736 {
737 int ret, count, i;
738 unsigned int session_found = 0;
739 struct lttng_session *sessions;
740
741 count = lttng_list_sessions(&sessions);
742 DBG("Session count %d", count);
743 if (count < 0) {
744 ret = count;
745 ERR("%s", lttng_strerror(ret));
746 goto error;
747 } else if (count == 0) {
748 MSG("Currently no available tracing session");
749 goto end;
750 }
751
752 if (session_name == NULL) {
753 MSG("Available tracing sessions:");
754 }
755
756 for (i = 0; i < count; i++) {
757 if (session_name != NULL) {
758 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
759 session_found = 1;
760 MSG("Tracing session %s: [%s%s]", session_name,
761 active_string(sessions[i].enabled),
762 snapshot_string(sessions[i].snapshot_mode));
763 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
764 break;
765 }
766 } else {
767 MSG(" %d) %s (%s) [%s%s]", i + 1, sessions[i].name, sessions[i].path,
768 active_string(sessions[i].enabled),
769 snapshot_string(sessions[i].snapshot_mode));
770 }
771 }
772
773 free(sessions);
774
775 if (!session_found && session_name != NULL) {
776 ERR("Session '%s' not found", session_name);
777 ret = CMD_ERROR;
778 goto error;
779 }
780
781 if (session_name == NULL) {
782 MSG("\nUse lttng list <session_name> for more details");
783 }
784
785 end:
786 return CMD_SUCCESS;
787
788 error:
789 return ret;
790 }
791
792 /*
793 * List available domain(s) for a session.
794 */
795 static int list_domains(const char *session_name)
796 {
797 int i, count, ret = CMD_SUCCESS;
798 struct lttng_domain *domains = NULL;
799
800 MSG("Domains:\n-------------");
801
802 count = lttng_list_domains(session_name, &domains);
803 if (count < 0) {
804 ret = count;
805 ERR("%s", lttng_strerror(ret));
806 goto error;
807 } else if (count == 0) {
808 MSG(" None");
809 goto end;
810 }
811
812 for (i = 0; i < count; i++) {
813 switch (domains[i].type) {
814 case LTTNG_DOMAIN_KERNEL:
815 MSG(" - Kernel");
816 break;
817 case LTTNG_DOMAIN_UST:
818 MSG(" - UST global");
819 break;
820 case LTTNG_DOMAIN_JUL:
821 MSG(" - JUL (Java Util Logging)");
822 break;
823 default:
824 break;
825 }
826 }
827
828 end:
829 free(domains);
830
831 error:
832 return ret;
833 }
834
835 /*
836 * The 'list <options>' first level command
837 */
838 int cmd_list(int argc, const char **argv)
839 {
840 int opt, ret = CMD_SUCCESS;
841 const char *session_name;
842 static poptContext pc;
843 struct lttng_domain domain;
844 struct lttng_domain *domains = NULL;
845
846 memset(&domain, 0, sizeof(domain));
847
848 if (argc < 1) {
849 usage(stderr);
850 ret = CMD_ERROR;
851 goto end;
852 }
853
854 pc = poptGetContext(NULL, argc, argv, long_options, 0);
855 poptReadDefaultConfig(pc, 0);
856
857 while ((opt = poptGetNextOpt(pc)) != -1) {
858 switch (opt) {
859 case OPT_HELP:
860 usage(stdout);
861 goto end;
862 case OPT_USERSPACE:
863 opt_userspace = 1;
864 break;
865 case OPT_LIST_OPTIONS:
866 list_cmd_options(stdout, long_options);
867 goto end;
868 default:
869 usage(stderr);
870 ret = CMD_UNDEFINED;
871 goto end;
872 }
873 }
874
875 /* Get session name (trailing argument) */
876 session_name = poptGetArg(pc);
877 DBG2("Session name: %s", session_name);
878
879 if (opt_kernel) {
880 domain.type = LTTNG_DOMAIN_KERNEL;
881 } else if (opt_userspace) {
882 DBG2("Listing userspace global domain");
883 domain.type = LTTNG_DOMAIN_UST;
884 } else if (opt_jul) {
885 DBG2("Listing JUL domain");
886 domain.type = LTTNG_DOMAIN_JUL;
887 }
888
889 if (opt_kernel || opt_userspace || opt_jul) {
890 handle = lttng_create_handle(session_name, &domain);
891 if (handle == NULL) {
892 ret = CMD_FATAL;
893 goto end;
894 }
895 }
896
897 if (session_name == NULL) {
898 if (!opt_kernel && !opt_userspace && !opt_jul) {
899 ret = list_sessions(NULL);
900 if (ret != 0) {
901 goto end;
902 }
903 }
904 if (opt_kernel) {
905 ret = list_kernel_events();
906 if (ret < 0) {
907 ret = CMD_ERROR;
908 goto end;
909 }
910 }
911 if (opt_userspace) {
912 if (opt_fields) {
913 ret = list_ust_event_fields();
914 } else {
915 ret = list_ust_events();
916 }
917 if (ret < 0) {
918 ret = CMD_ERROR;
919 goto end;
920 }
921 }
922 if (opt_jul) {
923 ret = list_jul_events();
924 if (ret < 0) {
925 ret = CMD_ERROR;
926 goto end;
927 }
928 }
929 } else {
930 /* List session attributes */
931 ret = list_sessions(session_name);
932 if (ret != 0) {
933 goto end;
934 }
935
936 /* Domain listing */
937 if (opt_domain) {
938 ret = list_domains(session_name);
939 goto end;
940 }
941
942 if (opt_kernel || opt_userspace) {
943 /* Channel listing */
944 ret = list_channels(opt_channel);
945 if (ret < 0) {
946 goto end;
947 }
948 } else {
949 int i, nb_domain;
950
951 /* We want all domain(s) */
952 nb_domain = lttng_list_domains(session_name, &domains);
953 if (nb_domain < 0) {
954 ret = nb_domain;
955 ERR("%s", lttng_strerror(ret));
956 goto end;
957 }
958
959 for (i = 0; i < nb_domain; i++) {
960 switch (domains[i].type) {
961 case LTTNG_DOMAIN_KERNEL:
962 MSG("=== Domain: Kernel ===\n");
963 break;
964 case LTTNG_DOMAIN_UST:
965 MSG("=== Domain: UST global ===\n");
966 MSG("Buffer type: %s\n",
967 domains[i].buf_type ==
968 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
969 break;
970 case LTTNG_DOMAIN_JUL:
971 MSG("=== Domain: JUL (Java Util Logging) ===\n");
972 break;
973 default:
974 MSG("=== Domain: Unimplemented ===\n");
975 break;
976 }
977
978 /* Clean handle before creating a new one */
979 if (handle) {
980 lttng_destroy_handle(handle);
981 }
982
983 handle = lttng_create_handle(session_name, &domains[i]);
984 if (handle == NULL) {
985 ret = CMD_FATAL;
986 goto end;
987 }
988
989 if (domains[i].type == LTTNG_DOMAIN_JUL) {
990 ret = list_session_jul_events();
991 if (ret < 0) {
992 goto end;
993 }
994 continue;
995 }
996
997 ret = list_channels(opt_channel);
998 if (ret < 0) {
999 goto end;
1000 }
1001 }
1002 }
1003 }
1004
1005 end:
1006 free(domains);
1007 if (handle) {
1008 lttng_destroy_handle(handle);
1009 }
1010
1011 poptFreeContext(pc);
1012 return ret;
1013 }
This page took 0.048068 seconds and 3 git commands to generate.