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