69671bd714d1c6854d2303a08e7fe9044a4c5bbc
[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: return "";
171 case 0: return "TRACE_EMERG";
172 case 1: return "TRACE_ALERT";
173 case 2: return "TRACE_CRIT";
174 case 3: return "TRACE_ERR";
175 case 4: return "TRACE_WARNING";
176 case 5: return "TRACE_NOTICE";
177 case 6: return "TRACE_INFO";
178 case 7: return "TRACE_DEBUG_SYSTEM";
179 case 8: return "TRACE_DEBUG_PROGRAM";
180 case 9: return "TRACE_DEBUG_PROCESS";
181 case 10: return "TRACE_DEBUG_MODULE";
182 case 11: return "TRACE_DEBUG_UNIT";
183 case 12: return "TRACE_DEBUG_FUNCTION";
184 case 13: return "TRACE_DEBUG_LINE";
185 case 14: return "TRACE_DEBUG";
186 default: return "<<UNKNOWN>>";
187 }
188 }
189
190 /*
191 * Pretty print single event.
192 */
193 static void print_events(struct lttng_event *event)
194 {
195 switch (event->type) {
196 case LTTNG_EVENT_TRACEPOINT:
197 {
198 MSG("%s%s%s%s%s (type: tracepoint)%s", indent6,
199 event->name,
200 loglevel_string_pre(event->loglevel),
201 loglevel_string(event->loglevel),
202 loglevel_string_post(event->loglevel),
203 enabled_string(event->enabled));
204 break;
205 }
206 case LTTNG_EVENT_PROBE:
207 MSG("%s%s (type: probe)%s", indent6,
208 event->name, enabled_string(event->enabled));
209 if (event->attr.probe.addr != 0) {
210 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
211 } else {
212 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
213 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
214 }
215 break;
216 case LTTNG_EVENT_FUNCTION:
217 case LTTNG_EVENT_FUNCTION_ENTRY:
218 MSG("%s%s (type: function)%s", indent6,
219 event->name, enabled_string(event->enabled));
220 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
221 break;
222 case LTTNG_EVENT_SYSCALL:
223 MSG("%ssyscalls (type: syscall)%s", indent6,
224 enabled_string(event->enabled));
225 break;
226 case LTTNG_EVENT_NOOP:
227 MSG("%s (type: noop)%s", indent6,
228 enabled_string(event->enabled));
229 break;
230 case LTTNG_EVENT_ALL:
231 /* We should never have "all" events in list. */
232 assert(0);
233 break;
234 }
235 }
236
237 /*
238 * Ask session daemon for all user space tracepoints available.
239 */
240 static int list_ust_events(void)
241 {
242 int i, size;
243 struct lttng_domain domain;
244 struct lttng_handle *handle;
245 struct lttng_event *event_list;
246 pid_t cur_pid = 0;
247
248 memset(&domain, 0, sizeof(domain));
249
250 DBG("Getting UST tracing events");
251
252 domain.type = LTTNG_DOMAIN_UST;
253
254 handle = lttng_create_handle(NULL, &domain);
255 if (handle == NULL) {
256 goto error;
257 }
258
259 size = lttng_list_tracepoints(handle, &event_list);
260 if (size < 0) {
261 ERR("Unable to list UST events");
262 lttng_destroy_handle(handle);
263 return size;
264 }
265
266 MSG("UST events:\n-------------");
267
268 if (size == 0) {
269 MSG("None");
270 }
271
272 for (i = 0; i < size; i++) {
273 if (cur_pid != event_list[i].pid) {
274 cur_pid = event_list[i].pid;
275 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
276 }
277 print_events(&event_list[i]);
278 }
279
280 MSG("");
281
282 free(event_list);
283 lttng_destroy_handle(handle);
284
285 return CMD_SUCCESS;
286
287 error:
288 lttng_destroy_handle(handle);
289 return -1;
290 }
291
292 /*
293 * Ask for all trace events in the kernel and pretty print them.
294 */
295 static int list_kernel_events(void)
296 {
297 int i, size;
298 struct lttng_domain domain;
299 struct lttng_handle *handle;
300 struct lttng_event *event_list;
301
302 memset(&domain, 0, sizeof(domain));
303
304 DBG("Getting kernel tracing events");
305
306 domain.type = LTTNG_DOMAIN_KERNEL;
307
308 handle = lttng_create_handle(NULL, &domain);
309 if (handle == NULL) {
310 goto error;
311 }
312
313 size = lttng_list_tracepoints(handle, &event_list);
314 if (size < 0) {
315 ERR("Unable to list kernel events");
316 lttng_destroy_handle(handle);
317 return size;
318 }
319
320 MSG("Kernel events:\n-------------");
321
322 for (i = 0; i < size; i++) {
323 print_events(&event_list[i]);
324 }
325
326 MSG("");
327
328 free(event_list);
329
330 lttng_destroy_handle(handle);
331 return CMD_SUCCESS;
332
333 error:
334 lttng_destroy_handle(handle);
335 return -1;
336 }
337
338 /*
339 * List events of channel of session and domain.
340 */
341 static int list_events(const char *channel_name)
342 {
343 int ret, count, i;
344 struct lttng_event *events = NULL;
345
346 count = lttng_list_events(handle, channel_name, &events);
347 if (count < 0) {
348 ret = count;
349 goto error;
350 }
351
352 MSG("\n%sEvents:", indent4);
353 if (count == 0) {
354 MSG("%sNone\n", indent6);
355 goto end;
356 }
357
358 for (i = 0; i < count; i++) {
359 print_events(&events[i]);
360 }
361
362 MSG("");
363
364 end:
365 if (events) {
366 free(events);
367 }
368 ret = CMD_SUCCESS;
369
370 error:
371 return ret;
372 }
373
374 /*
375 * Pretty print channel
376 */
377 static void print_channel(struct lttng_channel *channel)
378 {
379 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
380
381 MSG("%sAttributes:", indent4);
382 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
383 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
384 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
385 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
386 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
387 switch (channel->attr.output) {
388 case LTTNG_EVENT_SPLICE:
389 MSG("%soutput: splice()", indent6);
390 break;
391 case LTTNG_EVENT_MMAP:
392 MSG("%soutput: mmap()", indent6);
393 break;
394 }
395 }
396
397 /*
398 * List channel(s) of session and domain.
399 *
400 * If channel_name is NULL, all channels are listed.
401 */
402 static int list_channels(const char *channel_name)
403 {
404 int count, i, ret = CMD_SUCCESS;
405 unsigned int chan_found = 0;
406 struct lttng_channel *channels = NULL;
407
408 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
409
410 count = lttng_list_channels(handle, &channels);
411 if (count < 0) {
412 ret = count;
413 goto error_channels;
414 } else if (count == 0) {
415 ERR("Channel %s not found", channel_name);
416 goto error;
417 }
418
419 if (channel_name == NULL) {
420 MSG("Channels:\n-------------");
421 }
422
423 for (i = 0; i < count; i++) {
424 if (channel_name != NULL) {
425 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
426 chan_found = 1;
427 } else {
428 continue;
429 }
430 }
431 print_channel(&channels[i]);
432
433 /* Listing events per channel */
434 ret = list_events(channels[i].name);
435 if (ret < 0) {
436 MSG("%s", lttng_strerror(ret));
437 }
438
439 if (chan_found) {
440 break;
441 }
442 }
443
444 if (!chan_found && channel_name != NULL) {
445 ERR("Channel %s not found", channel_name);
446 goto error;
447 }
448
449 ret = CMD_SUCCESS;
450
451 error:
452 free(channels);
453
454 error_channels:
455 return ret;
456 }
457
458 /*
459 * List available tracing session. List only basic information.
460 *
461 * If session_name is NULL, all sessions are listed.
462 */
463 static int list_sessions(const char *session_name)
464 {
465 int ret, count, i;
466 unsigned int session_found = 0;
467 struct lttng_session *sessions;
468
469 count = lttng_list_sessions(&sessions);
470 DBG("Session count %d", count);
471 if (count < 0) {
472 ret = count;
473 goto error;
474 }
475
476 if (session_name == NULL) {
477 MSG("Available tracing sessions:");
478 }
479
480 for (i = 0; i < count; i++) {
481 if (session_name != NULL) {
482 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
483 session_found = 1;
484 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
485 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
486 break;
487 }
488 continue;
489 }
490
491 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
492 active_string(sessions[i].enabled));
493
494 if (session_found) {
495 break;
496 }
497 }
498
499 free(sessions);
500
501 if (!session_found && session_name != NULL) {
502 ERR("Session '%s' not found", session_name);
503 ret = CMD_ERROR;
504 goto error;
505 }
506
507 if (session_name == NULL) {
508 MSG("\nUse lttng list <session_name> for more details");
509 }
510
511 return CMD_SUCCESS;
512
513 error:
514 return ret;
515 }
516
517 /*
518 * List available domain(s) for a session.
519 */
520 static int list_domains(const char *session_name)
521 {
522 int i, count, ret = CMD_SUCCESS;
523 struct lttng_domain *domains = NULL;
524
525 MSG("Domains:\n-------------");
526
527 count = lttng_list_domains(session_name, &domains);
528 if (count < 0) {
529 ret = count;
530 goto error;
531 } else if (count == 0) {
532 MSG(" None");
533 goto end;
534 }
535
536 for (i = 0; i < count; i++) {
537 switch (domains[i].type) {
538 case LTTNG_DOMAIN_KERNEL:
539 MSG(" - Kernel");
540 break;
541 case LTTNG_DOMAIN_UST:
542 MSG(" - UST global");
543 break;
544 default:
545 break;
546 }
547 }
548
549 end:
550 free(domains);
551
552 error:
553 return ret;
554 }
555
556 /*
557 * The 'list <options>' first level command
558 */
559 int cmd_list(int argc, const char **argv)
560 {
561 int opt, i, ret = CMD_SUCCESS;
562 int nb_domain;
563 const char *session_name;
564 static poptContext pc;
565 struct lttng_domain domain;
566 struct lttng_domain *domains = NULL;
567
568 memset(&domain, 0, sizeof(domain));
569
570 if (argc < 1) {
571 usage(stderr);
572 ret = CMD_ERROR;
573 goto end;
574 }
575
576 pc = poptGetContext(NULL, argc, argv, long_options, 0);
577 poptReadDefaultConfig(pc, 0);
578
579 while ((opt = poptGetNextOpt(pc)) != -1) {
580 switch (opt) {
581 case OPT_HELP:
582 usage(stdout);
583 goto end;
584 case OPT_USERSPACE:
585 opt_userspace = 1;
586 break;
587 case OPT_LIST_OPTIONS:
588 list_cmd_options(stdout, long_options);
589 goto end;
590 default:
591 usage(stderr);
592 ret = CMD_UNDEFINED;
593 goto end;
594 }
595 }
596
597 /* Get session name (trailing argument) */
598 session_name = poptGetArg(pc);
599 DBG2("Session name: %s", session_name);
600
601 if (opt_kernel) {
602 domain.type = LTTNG_DOMAIN_KERNEL;
603 } else if (opt_userspace) {
604 DBG2("Listing userspace global domain");
605 domain.type = LTTNG_DOMAIN_UST;
606 }
607
608 if (opt_kernel || opt_userspace) {
609 handle = lttng_create_handle(session_name, &domain);
610 if (handle == NULL) {
611 ret = CMD_FATAL;
612 goto end;
613 }
614 }
615
616 if (session_name == NULL) {
617 if (!opt_kernel && !opt_userspace) {
618 ret = list_sessions(NULL);
619 if (ret != 0) {
620 goto end;
621 }
622 }
623 if (opt_kernel) {
624 ret = list_kernel_events();
625 if (ret < 0) {
626 goto end;
627 }
628 }
629 if (opt_userspace) {
630 ret = list_ust_events();
631 if (ret < 0) {
632 goto end;
633 }
634 }
635 } else {
636 /* List session attributes */
637 ret = list_sessions(session_name);
638 if (ret != 0) {
639 goto end;
640 }
641
642 /* Domain listing */
643 if (opt_domain) {
644 ret = list_domains(session_name);
645 goto end;
646 }
647
648 if (opt_kernel) {
649 /* Channel listing */
650 ret = list_channels(opt_channel);
651 if (ret < 0) {
652 goto end;
653 }
654 } else {
655 /* We want all domain(s) */
656 nb_domain = lttng_list_domains(session_name, &domains);
657 if (nb_domain < 0) {
658 ret = nb_domain;
659 goto end;
660 }
661
662 for (i = 0; i < nb_domain; i++) {
663 switch (domains[i].type) {
664 case LTTNG_DOMAIN_KERNEL:
665 MSG("=== Domain: Kernel ===\n");
666 break;
667 case LTTNG_DOMAIN_UST:
668 MSG("=== Domain: UST global ===\n");
669 break;
670 default:
671 MSG("=== Domain: Unimplemented ===\n");
672 break;
673 }
674
675 /* Clean handle before creating a new one */
676 if (handle) {
677 lttng_destroy_handle(handle);
678 }
679
680 handle = lttng_create_handle(session_name, &domains[i]);
681 if (handle == NULL) {
682 ret = CMD_FATAL;
683 goto end;
684 }
685
686 ret = list_channels(opt_channel);
687 if (ret < 0) {
688 goto end;
689 }
690 }
691 }
692 }
693
694 end:
695 if (domains) {
696 free(domains);
697 }
698 if (handle) {
699 lttng_destroy_handle(handle);
700 }
701
702 poptFreeContext(pc);
703 return ret;
704 }
This page took 0.043304 seconds and 3 git commands to generate.