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