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