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