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