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