Fix: Returned code when listing kernel channel
[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 #include <common/sessiond-comm/sessiond-comm.h>
29
30 static int opt_userspace;
31 static int opt_kernel;
32 static char *opt_channel;
33 static int opt_domain;
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 #if 0
57 /* Not implemented yet */
58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
60 #else
61 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
62 #endif
63 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
64 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
65 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
66 {0, 0, 0, 0, 0, 0, 0}
67 };
68
69 /*
70 * usage
71 */
72 static void usage(FILE *ofp)
73 {
74 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp, "\n");
78 fprintf(ofp, "Without a session, -k lists available kernel events\n");
79 fprintf(ofp, "Without a session, -u lists available userspace events\n");
80 fprintf(ofp, "\n");
81 fprintf(ofp, " -h, --help Show this help\n");
82 fprintf(ofp, " --list-options Simple listing of options\n");
83 fprintf(ofp, " -k, --kernel Select kernel domain\n");
84 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
85 #if 0
86 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
87 #endif
88 fprintf(ofp, "\n");
89 fprintf(ofp, "Session Options:\n");
90 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
91 fprintf(ofp, " -d, --domain List available domain(s)\n");
92 fprintf(ofp, "\n");
93 }
94
95 /*
96 * Get command line from /proc for a specific pid.
97 *
98 * On success, return an allocated string pointer to the proc cmdline.
99 * On error, return NULL.
100 */
101 static char *get_cmdline_by_pid(pid_t pid)
102 {
103 int ret;
104 FILE *fp;
105 char *cmdline = NULL;
106 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
107
108 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
109 fp = fopen(path, "r");
110 if (fp == NULL) {
111 goto end;
112 }
113
114 /* Caller must free() *cmdline */
115 cmdline = malloc(PATH_MAX);
116 ret = fread(cmdline, 1, PATH_MAX, fp);
117 if (ret < 0) {
118 perror("fread proc list");
119 }
120 fclose(fp);
121
122 end:
123 return cmdline;
124 }
125
126 static
127 const char *active_string(int value)
128 {
129 switch (value) {
130 case 0: return " [inactive]";
131 case 1: return " [active]";
132 case -1: return "";
133 default: return NULL;
134 }
135 }
136
137 static
138 const char *enabled_string(int value)
139 {
140 switch (value) {
141 case 0: return " [disabled]";
142 case 1: return " [enabled]";
143 case -1: return "";
144 default: return NULL;
145 }
146 }
147
148 static const char *loglevel_string(int value)
149 {
150 switch (value) {
151 case -1:
152 return "";
153 case LTTNG_LOGLEVEL_EMERG:
154 return "TRACE_EMERG";
155 case LTTNG_LOGLEVEL_ALERT:
156 return "TRACE_ALERT";
157 case LTTNG_LOGLEVEL_CRIT:
158 return "TRACE_CRIT";
159 case LTTNG_LOGLEVEL_ERR:
160 return "TRACE_ERR";
161 case LTTNG_LOGLEVEL_WARNING:
162 return "TRACE_WARNING";
163 case LTTNG_LOGLEVEL_NOTICE:
164 return "TRACE_NOTICE";
165 case LTTNG_LOGLEVEL_INFO:
166 return "TRACE_INFO";
167 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
168 return "TRACE_DEBUG_SYSTEM";
169 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
170 return "TRACE_DEBUG_PROGRAM";
171 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
172 return "TRACE_DEBUG_PROCESS";
173 case LTTNG_LOGLEVEL_DEBUG_MODULE:
174 return "TRACE_DEBUG_MODULE";
175 case LTTNG_LOGLEVEL_DEBUG_UNIT:
176 return "TRACE_DEBUG_UNIT";
177 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
178 return "TRACE_DEBUG_FUNCTION";
179 case LTTNG_LOGLEVEL_DEBUG_LINE:
180 return "TRACE_DEBUG_LINE";
181 case LTTNG_LOGLEVEL_DEBUG:
182 return "TRACE_DEBUG";
183 default:
184 return "<<UNKNOWN>>";
185 }
186 }
187
188 /*
189 * Pretty print single event.
190 */
191 static void print_events(struct lttng_event *event)
192 {
193 switch (event->type) {
194 case LTTNG_EVENT_TRACEPOINT:
195 {
196 if (event->loglevel != -1) {
197 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
198 indent6,
199 event->name,
200 loglevel_string(event->loglevel),
201 event->loglevel,
202 enabled_string(event->enabled));
203 } else {
204 MSG("%s%s (type: tracepoint)%s",
205 indent6,
206 event->name,
207 enabled_string(event->enabled));
208 }
209 break;
210 }
211 case LTTNG_EVENT_PROBE:
212 MSG("%s%s (type: probe)%s", indent6,
213 event->name, enabled_string(event->enabled));
214 if (event->attr.probe.addr != 0) {
215 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
216 } else {
217 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
218 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
219 }
220 break;
221 case LTTNG_EVENT_FUNCTION:
222 case LTTNG_EVENT_FUNCTION_ENTRY:
223 MSG("%s%s (type: function)%s", indent6,
224 event->name, enabled_string(event->enabled));
225 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
226 break;
227 case LTTNG_EVENT_SYSCALL:
228 MSG("%ssyscalls (type: syscall)%s", indent6,
229 enabled_string(event->enabled));
230 break;
231 case LTTNG_EVENT_NOOP:
232 MSG("%s (type: noop)%s", indent6,
233 enabled_string(event->enabled));
234 break;
235 case LTTNG_EVENT_ALL:
236 /* We should never have "all" events in list. */
237 assert(0);
238 break;
239 }
240 }
241
242 /*
243 * Ask session daemon for all user space tracepoints available.
244 */
245 static int list_ust_events(void)
246 {
247 int i, size;
248 struct lttng_domain domain;
249 struct lttng_handle *handle;
250 struct lttng_event *event_list;
251 pid_t cur_pid = 0;
252
253 memset(&domain, 0, sizeof(domain));
254
255 DBG("Getting UST tracing events");
256
257 domain.type = LTTNG_DOMAIN_UST;
258
259 handle = lttng_create_handle(NULL, &domain);
260 if (handle == NULL) {
261 goto error;
262 }
263
264 size = lttng_list_tracepoints(handle, &event_list);
265 if (size < 0) {
266 ERR("Unable to list UST events");
267 lttng_destroy_handle(handle);
268 return size;
269 }
270
271 MSG("UST events:\n-------------");
272
273 if (size == 0) {
274 MSG("None");
275 }
276
277 for (i = 0; i < size; i++) {
278 if (cur_pid != event_list[i].pid) {
279 cur_pid = event_list[i].pid;
280 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
281 }
282 print_events(&event_list[i]);
283 }
284
285 MSG("");
286
287 free(event_list);
288 lttng_destroy_handle(handle);
289
290 return CMD_SUCCESS;
291
292 error:
293 lttng_destroy_handle(handle);
294 return -1;
295 }
296
297 /*
298 * Ask for all trace events in the kernel and pretty print them.
299 */
300 static int list_kernel_events(void)
301 {
302 int i, size;
303 struct lttng_domain domain;
304 struct lttng_handle *handle;
305 struct lttng_event *event_list;
306
307 memset(&domain, 0, sizeof(domain));
308
309 DBG("Getting kernel tracing events");
310
311 domain.type = LTTNG_DOMAIN_KERNEL;
312
313 handle = lttng_create_handle(NULL, &domain);
314 if (handle == NULL) {
315 goto error;
316 }
317
318 size = lttng_list_tracepoints(handle, &event_list);
319 if (size < 0) {
320 ERR("Unable to list kernel events");
321 lttng_destroy_handle(handle);
322 return size;
323 }
324
325 MSG("Kernel events:\n-------------");
326
327 for (i = 0; i < size; i++) {
328 print_events(&event_list[i]);
329 }
330
331 MSG("");
332
333 free(event_list);
334
335 lttng_destroy_handle(handle);
336 return CMD_SUCCESS;
337
338 error:
339 lttng_destroy_handle(handle);
340 return -1;
341 }
342
343 /*
344 * List events of channel of session and domain.
345 */
346 static int list_events(const char *channel_name)
347 {
348 int ret, count, i;
349 struct lttng_event *events = NULL;
350
351 count = lttng_list_events(handle, channel_name, &events);
352 if (count < 0) {
353 ret = count;
354 goto error;
355 }
356
357 MSG("\n%sEvents:", indent4);
358 if (count == 0) {
359 MSG("%sNone\n", indent6);
360 goto end;
361 }
362
363 for (i = 0; i < count; i++) {
364 print_events(&events[i]);
365 }
366
367 MSG("");
368
369 end:
370 if (events) {
371 free(events);
372 }
373 ret = CMD_SUCCESS;
374
375 error:
376 return ret;
377 }
378
379 /*
380 * Pretty print channel
381 */
382 static void print_channel(struct lttng_channel *channel)
383 {
384 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
385
386 MSG("%sAttributes:", indent4);
387 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
388 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
389 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
390 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
391 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
392 switch (channel->attr.output) {
393 case LTTNG_EVENT_SPLICE:
394 MSG("%soutput: splice()", indent6);
395 break;
396 case LTTNG_EVENT_MMAP:
397 MSG("%soutput: mmap()", indent6);
398 break;
399 }
400 }
401
402 /*
403 * List channel(s) of session and domain.
404 *
405 * If channel_name is NULL, all channels are listed.
406 */
407 static int list_channels(const char *channel_name)
408 {
409 int count, i, ret = CMD_SUCCESS;
410 unsigned int chan_found = 0;
411 struct lttng_channel *channels = NULL;
412
413 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
414
415 count = lttng_list_channels(handle, &channels);
416 if (count < 0) {
417 switch (-count) {
418 case LTTCOMM_KERN_CHAN_NOT_FOUND:
419 ret = CMD_SUCCESS;
420 WARN("No kernel channel");
421 break;
422 case LTTCOMM_UST_CHAN_NOT_FOUND:
423 ret = CMD_SUCCESS;
424 WARN("No UST channel");
425 break;
426 default:
427 /* We had a real error */
428 ret = count;
429 ERR("%s", lttng_strerror(ret));
430 }
431 goto error_channels;
432 }
433
434 if (channel_name == NULL) {
435 MSG("Channels:\n-------------");
436 }
437
438 for (i = 0; i < count; i++) {
439 if (channel_name != NULL) {
440 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
441 chan_found = 1;
442 } else {
443 continue;
444 }
445 }
446 print_channel(&channels[i]);
447
448 /* Listing events per channel */
449 ret = list_events(channels[i].name);
450 if (ret < 0) {
451 MSG("%s", lttng_strerror(ret));
452 }
453
454 if (chan_found) {
455 break;
456 }
457 }
458
459 if (!chan_found && channel_name != NULL) {
460 ERR("Channel %s not found", channel_name);
461 goto error;
462 }
463
464 ret = CMD_SUCCESS;
465
466 error:
467 free(channels);
468
469 error_channels:
470 return ret;
471 }
472
473 /*
474 * List available tracing session. List only basic information.
475 *
476 * If session_name is NULL, all sessions are listed.
477 */
478 static int list_sessions(const char *session_name)
479 {
480 int ret, count, i;
481 unsigned int session_found = 0;
482 struct lttng_session *sessions;
483
484 count = lttng_list_sessions(&sessions);
485 DBG("Session count %d", count);
486 if (count < 0) {
487 ret = count;
488 goto error;
489 } else if (count == 0) {
490 MSG("Currently no available tracing session");
491 goto end;
492 }
493
494 if (session_name == NULL) {
495 MSG("Available tracing sessions:");
496 }
497
498 for (i = 0; i < count; i++) {
499 if (session_name != NULL) {
500 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
501 session_found = 1;
502 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
503 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
504 break;
505 }
506 continue;
507 }
508
509 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
510 active_string(sessions[i].enabled));
511
512 if (session_found) {
513 break;
514 }
515 }
516
517 free(sessions);
518
519 if (!session_found && session_name != NULL) {
520 ERR("Session '%s' not found", session_name);
521 ret = CMD_ERROR;
522 goto error;
523 }
524
525 if (session_name == NULL) {
526 MSG("\nUse lttng list <session_name> for more details");
527 }
528
529 end:
530 return CMD_SUCCESS;
531
532 error:
533 return ret;
534 }
535
536 /*
537 * List available domain(s) for a session.
538 */
539 static int list_domains(const char *session_name)
540 {
541 int i, count, ret = CMD_SUCCESS;
542 struct lttng_domain *domains = NULL;
543
544 MSG("Domains:\n-------------");
545
546 count = lttng_list_domains(session_name, &domains);
547 if (count < 0) {
548 ret = count;
549 goto error;
550 } else if (count == 0) {
551 MSG(" None");
552 goto end;
553 }
554
555 for (i = 0; i < count; i++) {
556 switch (domains[i].type) {
557 case LTTNG_DOMAIN_KERNEL:
558 MSG(" - Kernel");
559 break;
560 case LTTNG_DOMAIN_UST:
561 MSG(" - UST global");
562 break;
563 default:
564 break;
565 }
566 }
567
568 end:
569 free(domains);
570
571 error:
572 return ret;
573 }
574
575 /*
576 * The 'list <options>' first level command
577 */
578 int cmd_list(int argc, const char **argv)
579 {
580 int opt, i, ret = CMD_SUCCESS;
581 int nb_domain;
582 const char *session_name;
583 static poptContext pc;
584 struct lttng_domain domain;
585 struct lttng_domain *domains = NULL;
586
587 memset(&domain, 0, sizeof(domain));
588
589 if (argc < 1) {
590 usage(stderr);
591 ret = CMD_ERROR;
592 goto end;
593 }
594
595 pc = poptGetContext(NULL, argc, argv, long_options, 0);
596 poptReadDefaultConfig(pc, 0);
597
598 while ((opt = poptGetNextOpt(pc)) != -1) {
599 switch (opt) {
600 case OPT_HELP:
601 usage(stdout);
602 goto end;
603 case OPT_USERSPACE:
604 opt_userspace = 1;
605 break;
606 case OPT_LIST_OPTIONS:
607 list_cmd_options(stdout, long_options);
608 goto end;
609 default:
610 usage(stderr);
611 ret = CMD_UNDEFINED;
612 goto end;
613 }
614 }
615
616 /* Get session name (trailing argument) */
617 session_name = poptGetArg(pc);
618 DBG2("Session name: %s", session_name);
619
620 if (opt_kernel) {
621 domain.type = LTTNG_DOMAIN_KERNEL;
622 } else if (opt_userspace) {
623 DBG2("Listing userspace global domain");
624 domain.type = LTTNG_DOMAIN_UST;
625 }
626
627 if (opt_kernel || opt_userspace) {
628 handle = lttng_create_handle(session_name, &domain);
629 if (handle == NULL) {
630 ret = CMD_FATAL;
631 goto end;
632 }
633 }
634
635 if (session_name == NULL) {
636 if (!opt_kernel && !opt_userspace) {
637 ret = list_sessions(NULL);
638 if (ret != 0) {
639 goto end;
640 }
641 }
642 if (opt_kernel) {
643 ret = list_kernel_events();
644 if (ret < 0) {
645 goto end;
646 }
647 }
648 if (opt_userspace) {
649 ret = list_ust_events();
650 if (ret < 0) {
651 goto end;
652 }
653 }
654 } else {
655 /* List session attributes */
656 ret = list_sessions(session_name);
657 if (ret != 0) {
658 goto end;
659 }
660
661 /* Domain listing */
662 if (opt_domain) {
663 ret = list_domains(session_name);
664 goto end;
665 }
666
667 if (opt_kernel) {
668 /* Channel listing */
669 ret = list_channels(opt_channel);
670 if (ret < 0) {
671 goto end;
672 }
673 } else {
674 /* We want all domain(s) */
675 nb_domain = lttng_list_domains(session_name, &domains);
676 if (nb_domain < 0) {
677 ret = nb_domain;
678 goto end;
679 }
680
681 for (i = 0; i < nb_domain; i++) {
682 switch (domains[i].type) {
683 case LTTNG_DOMAIN_KERNEL:
684 MSG("=== Domain: Kernel ===\n");
685 break;
686 case LTTNG_DOMAIN_UST:
687 MSG("=== Domain: UST global ===\n");
688 break;
689 default:
690 MSG("=== Domain: Unimplemented ===\n");
691 break;
692 }
693
694 /* Clean handle before creating a new one */
695 if (handle) {
696 lttng_destroy_handle(handle);
697 }
698
699 handle = lttng_create_handle(session_name, &domains[i]);
700 if (handle == NULL) {
701 ret = CMD_FATAL;
702 goto end;
703 }
704
705 ret = list_channels(opt_channel);
706 if (ret < 0) {
707 goto end;
708 }
709 }
710 }
711 }
712
713 end:
714 if (domains) {
715 free(domains);
716 }
717 if (handle) {
718 lttng_destroy_handle(handle);
719 }
720
721 poptFreeContext(pc);
722 return ret;
723 }
This page took 0.044079 seconds and 4 git commands to generate.