Commit | Line | Data |
---|---|---|
f3ed775e DG |
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 | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
f3ed775e DG |
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 | |
9f19cc17 | 20 | #include <inttypes.h> |
f3ed775e DG |
21 | #include <popt.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
7a3d1328 | 25 | #include <assert.h> |
f3ed775e | 26 | |
6e2d116c | 27 | #include "../cmd.h" |
f3ed775e DG |
28 | |
29 | static int opt_pid; | |
9f19cc17 DG |
30 | static int opt_userspace; |
31 | static int opt_kernel; | |
32 | static char *opt_channel; | |
33 | static int opt_domain; | |
34 | ||
35 | const char *indent4 = " "; | |
36 | const char *indent6 = " "; | |
37 | const char *indent8 = " "; | |
f3ed775e DG |
38 | |
39 | enum { | |
40 | OPT_HELP = 1, | |
f3ed775e DG |
41 | }; |
42 | ||
cd80958d DG |
43 | static struct lttng_handle *handle; |
44 | ||
f3ed775e DG |
45 | static struct poptOption long_options[] = { |
46 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
47 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
9f19cc17 DG |
48 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
49 | {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0}, | |
50 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, | |
51 | {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0}, | |
52 | {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0}, | |
f3ed775e DG |
53 | {0, 0, 0, 0, 0, 0, 0} |
54 | }; | |
55 | ||
56 | /* | |
57 | * usage | |
58 | */ | |
59 | static void usage(FILE *ofp) | |
60 | { | |
9f19cc17 DG |
61 | fprintf(ofp, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n"); |
62 | fprintf(ofp, "\n"); | |
63 | fprintf(ofp, "With no arguments, list available tracing session(s)\n"); | |
64 | fprintf(ofp, "\n"); | |
65 | fprintf(ofp, "With -k alone, list available kernel events\n"); | |
66 | fprintf(ofp, "With -u alone, list available userspace events\n"); | |
67 | fprintf(ofp, "\n"); | |
68 | fprintf(ofp, " -h, --help Show this help\n"); | |
69 | fprintf(ofp, " -k, --kernel Select kernel domain\n"); | |
70 | fprintf(ofp, " -u, --userspace Select user-space domain.\n"); | |
71 | fprintf(ofp, " -p, --pid PID List user-space events by PID\n"); | |
f3ed775e | 72 | fprintf(ofp, "\n"); |
9f19cc17 DG |
73 | fprintf(ofp, "Options:\n"); |
74 | fprintf(ofp, " -c, --channel NAME List details of a channel\n"); | |
75 | fprintf(ofp, " -d, --domain List available domain(s)\n"); | |
f3ed775e DG |
76 | fprintf(ofp, "\n"); |
77 | } | |
78 | ||
79 | /* | |
9f19cc17 | 80 | * Get command line from /proc for a specific pid. |
f3ed775e | 81 | * |
9f19cc17 DG |
82 | * On success, return an allocated string pointer to the proc cmdline. |
83 | * On error, return NULL. | |
f3ed775e | 84 | */ |
9f19cc17 | 85 | #ifdef DISABLE |
f3ed775e DG |
86 | static char *get_cmdline_by_pid(pid_t pid) |
87 | { | |
88 | int ret; | |
89 | FILE *fp; | |
90 | char *cmdline = NULL; | |
91 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ | |
92 | ||
93 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); | |
94 | fp = fopen(path, "r"); | |
95 | if (fp == NULL) { | |
96 | goto end; | |
97 | } | |
98 | ||
99 | /* Caller must free() *cmdline */ | |
100 | cmdline = malloc(PATH_MAX); | |
101 | ret = fread(cmdline, 1, PATH_MAX, fp); | |
f40799e8 DG |
102 | if (ret < 0) { |
103 | perror("fread proc list"); | |
104 | } | |
f3ed775e DG |
105 | fclose(fp); |
106 | ||
107 | end: | |
108 | return cmdline; | |
109 | } | |
9f19cc17 | 110 | #endif /* DISABLE */ |
f3ed775e DG |
111 | |
112 | /* | |
9f19cc17 | 113 | * Ask for all trace events in the kernel and pretty print them. |
f3ed775e | 114 | */ |
9f19cc17 | 115 | static int list_kernel_events(void) |
f3ed775e | 116 | { |
9f19cc17 DG |
117 | int i, size; |
118 | struct lttng_event *event_list; | |
f3ed775e DG |
119 | |
120 | DBG("Getting all tracing events"); | |
121 | ||
cd80958d | 122 | size = lttng_list_tracepoints(handle, &event_list); |
9f19cc17 DG |
123 | if (size < 0) { |
124 | ERR("Unable to list kernel events"); | |
125 | return size; | |
f3ed775e DG |
126 | } |
127 | ||
9f19cc17 | 128 | MSG("Kernel events:\n-------------"); |
f3ed775e | 129 | |
9f19cc17 DG |
130 | for (i = 0; i < size; i++) { |
131 | MSG(" %s", event_list[i].name); | |
f3ed775e DG |
132 | } |
133 | ||
134 | free(event_list); | |
135 | ||
136 | return CMD_SUCCESS; | |
137 | } | |
138 | ||
139 | /* | |
9f19cc17 | 140 | * List events of channel of session and domain. |
f3ed775e | 141 | */ |
cd80958d | 142 | static int list_events(const char *channel_name) |
f3ed775e DG |
143 | { |
144 | int ret, count, i; | |
9f19cc17 | 145 | struct lttng_event *events = NULL; |
f3ed775e | 146 | |
cd80958d | 147 | count = lttng_list_events(handle, channel_name, &events); |
f3ed775e DG |
148 | if (count < 0) { |
149 | ret = count; | |
150 | goto error; | |
151 | } | |
152 | ||
9f19cc17 DG |
153 | MSG("\n%sEvents:", indent4); |
154 | if (count == 0) { | |
155 | MSG("%sNone", indent6); | |
156 | goto end; | |
157 | } | |
158 | ||
f3ed775e | 159 | for (i = 0; i < count; i++) { |
9f19cc17 DG |
160 | switch (events[i].type) { |
161 | case LTTNG_EVENT_TRACEPOINT: | |
162 | MSG("%s%s (type: tracepoint) [enabled: %d]", indent6, | |
163 | events[i].name, events[i].enabled); | |
164 | break; | |
165 | case LTTNG_EVENT_PROBE: | |
166 | MSG("%s%s (type: probe) [enabled: %d]", indent6, | |
167 | events[i].name, events[i].enabled); | |
168 | if (events[i].attr.probe.addr != 0) { | |
169 | MSG("%saddr: 0x%" PRIx64, indent8, events[i].attr.probe.addr); | |
170 | } else { | |
171 | MSG("%soffset: 0x%" PRIx64, indent8, events[i].attr.probe.offset); | |
172 | MSG("%ssymbol: %s", indent8, events[i].attr.probe.symbol_name); | |
173 | } | |
174 | break; | |
175 | case LTTNG_EVENT_FUNCTION: | |
176 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
177 | MSG("%s%s (type: function) [enabled: %d]", indent6, | |
178 | events[i].name, events[i].enabled); | |
179 | MSG("%ssymbol: \"%s\"", indent8, events[i].attr.ftrace.symbol_name); | |
180 | break; | |
a54bd42d MD |
181 | case LTTNG_EVENT_SYSCALL: |
182 | MSG("%s (type: syscall) [enabled: %d]", indent6, | |
0133c199 MD |
183 | events[i].enabled); |
184 | break; | |
185 | case LTTNG_EVENT_NOOP: | |
186 | MSG("%s (type: noop) [enabled: %d]", indent6, | |
187 | events[i].enabled); | |
188 | break; | |
7a3d1328 MD |
189 | case LTTNG_EVENT_ALL: |
190 | /* We should never have "all" events in list. */ | |
191 | assert(0); | |
192 | break; | |
9f19cc17 | 193 | } |
f3ed775e DG |
194 | } |
195 | ||
9f19cc17 | 196 | MSG(""); |
f3ed775e | 197 | |
9f19cc17 DG |
198 | end: |
199 | if (events) { | |
200 | free(events); | |
201 | } | |
202 | ret = CMD_SUCCESS; | |
f3ed775e DG |
203 | |
204 | error: | |
205 | return ret; | |
206 | } | |
207 | ||
208 | /* | |
9f19cc17 DG |
209 | * Pretty print channel |
210 | */ | |
211 | static void print_channel(struct lttng_channel *channel) | |
212 | { | |
213 | MSG("- %s (enabled: %d):\n", channel->name, channel->enabled); | |
214 | ||
215 | MSG("%sAttributes:", indent4); | |
216 | MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite); | |
217 | MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size); | |
218 | MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf); | |
219 | MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval); | |
220 | MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval); | |
221 | switch (channel->attr.output) { | |
222 | case LTTNG_EVENT_SPLICE: | |
223 | MSG("%soutput: splice()", indent6); | |
224 | break; | |
225 | case LTTNG_EVENT_MMAP: | |
226 | MSG("%soutput: mmap()", indent6); | |
227 | break; | |
228 | } | |
229 | } | |
230 | ||
231 | /* | |
232 | * List channel(s) of session and domain. | |
f3ed775e | 233 | * |
9f19cc17 | 234 | * If channel_name is NULL, all channels are listed. |
f3ed775e | 235 | */ |
cd80958d | 236 | static int list_channels(const char *channel_name) |
f3ed775e | 237 | { |
9f19cc17 DG |
238 | int count, i, ret = CMD_SUCCESS; |
239 | unsigned int chan_found = 0; | |
240 | struct lttng_channel *channels = NULL; | |
f3ed775e | 241 | |
9f19cc17 DG |
242 | DBG("Listing channel(s) (%s)", channel_name); |
243 | ||
cd80958d | 244 | count = lttng_list_channels(handle, &channels); |
f3ed775e DG |
245 | if (count < 0) { |
246 | ret = count; | |
247 | goto error; | |
9f19cc17 DG |
248 | } else if (count == 0) { |
249 | MSG("No channel found"); | |
250 | goto end; | |
f3ed775e DG |
251 | } |
252 | ||
9f19cc17 DG |
253 | if (channel_name == NULL) { |
254 | MSG("Channels:\n-------------"); | |
255 | } | |
256 | ||
257 | for (i = 0; i < count; i++) { | |
258 | if (channel_name != NULL) { | |
259 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { | |
260 | chan_found = 1; | |
261 | } else { | |
262 | continue; | |
263 | } | |
264 | } | |
265 | print_channel(&channels[i]); | |
266 | ||
267 | /* Listing events per channel */ | |
cd80958d | 268 | ret = list_events(channels[i].name); |
9f19cc17 | 269 | if (ret < 0) { |
9a745bc7 | 270 | MSG("%s", lttng_strerror(ret)); |
9f19cc17 DG |
271 | } |
272 | ||
273 | if (chan_found) { | |
274 | break; | |
f3ed775e | 275 | } |
f3ed775e DG |
276 | } |
277 | ||
9f19cc17 DG |
278 | if (!chan_found && channel_name != NULL) { |
279 | MSG("Channel %s not found", channel_name); | |
280 | } | |
f3ed775e | 281 | |
9f19cc17 DG |
282 | end: |
283 | free(channels); | |
284 | ret = CMD_SUCCESS; | |
f3ed775e DG |
285 | |
286 | error: | |
287 | return ret; | |
288 | } | |
289 | ||
290 | /* | |
9f19cc17 | 291 | * List available tracing session. List only basic information. |
f3ed775e | 292 | * |
9f19cc17 | 293 | * If session_name is NULL, all sessions are listed. |
f3ed775e | 294 | */ |
9f19cc17 | 295 | static int list_sessions(const char *session_name) |
f3ed775e | 296 | { |
9f19cc17 DG |
297 | int ret, count, i; |
298 | unsigned int session_found = 0; | |
299 | struct lttng_session *sessions; | |
300 | ||
301 | count = lttng_list_sessions(&sessions); | |
302 | DBG("Session count %d", count); | |
303 | if (count < 0) { | |
304 | ret = count; | |
305 | goto error; | |
306 | } | |
307 | ||
308 | if (session_name == NULL) { | |
309 | MSG("Available tracing sessions:"); | |
310 | } | |
311 | ||
312 | for (i = 0; i < count; i++) { | |
313 | if (session_name != NULL) { | |
314 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
315 | session_found = 1; | |
316 | MSG("Tracing session %s:", session_name); | |
317 | MSG("%sTrace path: %s\n", indent4, sessions[i].path); | |
318 | break; | |
319 | } | |
cd80958d | 320 | continue; |
9f19cc17 DG |
321 | } |
322 | ||
323 | MSG(" %d) %s (%s)", i + 1, sessions[i].name, sessions[i].path); | |
324 | ||
325 | if (session_found) { | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | free(sessions); | |
331 | ||
332 | if (!session_found && session_name != NULL) { | |
333 | MSG("Session %s not found", session_name); | |
334 | } | |
335 | ||
336 | if (session_name == NULL) { | |
6e0de39b | 337 | MSG("\nUse lttng list <session_name> for more details"); |
9f19cc17 | 338 | } |
f3ed775e DG |
339 | |
340 | return CMD_SUCCESS; | |
341 | ||
342 | error: | |
343 | return ret; | |
344 | } | |
f3ed775e DG |
345 | |
346 | /* | |
9f19cc17 | 347 | * List available domain(s) for a session. |
f3ed775e | 348 | */ |
cd80958d | 349 | static int list_domains(void) |
f3ed775e | 350 | { |
9f19cc17 DG |
351 | int i, count, ret = CMD_SUCCESS; |
352 | struct lttng_domain *domains = NULL; | |
353 | ||
354 | MSG("Domains:\n-------------"); | |
355 | ||
cd80958d | 356 | count = lttng_list_domains(handle, &domains); |
9f19cc17 DG |
357 | if (count < 0) { |
358 | ret = count; | |
359 | goto error; | |
360 | } else if (count == 0) { | |
361 | MSG(" None"); | |
362 | goto end; | |
363 | } | |
364 | ||
365 | for (i = 0; i < count; i++) { | |
366 | switch (domains[i].type) { | |
367 | case LTTNG_DOMAIN_KERNEL: | |
368 | MSG(" - Kernel"); | |
369 | default: | |
370 | break; | |
371 | } | |
372 | } | |
373 | ||
374 | end: | |
375 | free(domains); | |
376 | ||
377 | error: | |
378 | return ret; | |
f3ed775e | 379 | } |
f3ed775e DG |
380 | |
381 | /* | |
9f19cc17 | 382 | * The 'list <options>' first level command |
f3ed775e DG |
383 | */ |
384 | int cmd_list(int argc, const char **argv) | |
385 | { | |
9f19cc17 DG |
386 | int opt, i, ret = CMD_SUCCESS; |
387 | const char *session_name; | |
f3ed775e | 388 | static poptContext pc; |
9f19cc17 DG |
389 | struct lttng_domain domain; |
390 | struct lttng_domain *domains = NULL; | |
f3ed775e | 391 | |
9f19cc17 | 392 | if (argc < 1) { |
f3ed775e DG |
393 | usage(stderr); |
394 | goto end; | |
395 | } | |
396 | ||
397 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
398 | poptReadDefaultConfig(pc, 0); | |
399 | ||
400 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
401 | switch (opt) { | |
402 | case OPT_HELP: | |
403 | usage(stderr); | |
404 | goto end; | |
f3ed775e DG |
405 | default: |
406 | usage(stderr); | |
407 | ret = CMD_UNDEFINED; | |
408 | goto end; | |
409 | } | |
410 | } | |
411 | ||
9f19cc17 DG |
412 | if (opt_userspace || opt_pid != 0) { |
413 | MSG("*** Userspace tracing not implemented ***\n"); | |
f3ed775e DG |
414 | } |
415 | ||
9f19cc17 DG |
416 | /* Get session name (trailing argument) */ |
417 | session_name = poptGetArg(pc); | |
418 | DBG("Session name: %s", session_name); | |
419 | ||
cd80958d DG |
420 | if (opt_kernel) { |
421 | domain.type = LTTNG_DOMAIN_KERNEL; | |
422 | } | |
423 | ||
424 | handle = lttng_create_handle(session_name, &domain); | |
425 | if (handle == NULL) { | |
426 | goto end; | |
427 | } | |
428 | ||
9f19cc17 DG |
429 | if (session_name == NULL) { |
430 | if (opt_kernel) { | |
431 | ret = list_kernel_events(); | |
432 | if (ret < 0) { | |
433 | goto end; | |
434 | } | |
435 | } else { | |
436 | ret = list_sessions(NULL); | |
437 | if (ret < 0) { | |
438 | goto end; | |
439 | } | |
440 | } | |
441 | } else { | |
442 | /* List session attributes */ | |
443 | ret = list_sessions(session_name); | |
444 | if (ret < 0) { | |
445 | goto end; | |
446 | } | |
447 | ||
448 | /* Domain listing */ | |
449 | if (opt_domain) { | |
cd80958d | 450 | ret = list_domains(); |
9f19cc17 DG |
451 | goto end; |
452 | } | |
453 | ||
454 | if (opt_kernel) { | |
9f19cc17 | 455 | /* Channel listing */ |
cd80958d | 456 | ret = list_channels(opt_channel); |
9f19cc17 DG |
457 | if (ret < 0) { |
458 | goto end; | |
459 | } | |
460 | } else if (opt_userspace) { | |
461 | /* TODO: Userspace domain */ | |
462 | } else { | |
463 | /* We want all domain(s) */ | |
cd80958d | 464 | ret = lttng_list_domains(handle, &domains); |
9f19cc17 DG |
465 | if (ret < 0) { |
466 | goto end; | |
467 | } | |
468 | ||
469 | for (i = 0; i < ret; i++) { | |
470 | switch (domains[i].type) { | |
471 | case LTTNG_DOMAIN_KERNEL: | |
472 | MSG("=== Domain: Kernel ===\n"); | |
473 | break; | |
474 | default: | |
475 | MSG("=== Domain: Unimplemented ===\n"); | |
476 | break; | |
477 | } | |
478 | ||
cd80958d DG |
479 | /* Clean handle before creating a new one */ |
480 | lttng_destroy_handle(handle); | |
481 | ||
482 | handle = lttng_create_handle(session_name, &domains[i]); | |
483 | if (handle == NULL) { | |
484 | goto end; | |
485 | } | |
486 | ||
487 | ret = list_channels(opt_channel); | |
9f19cc17 DG |
488 | if (ret < 0) { |
489 | goto end; | |
490 | } | |
491 | } | |
492 | } | |
f3ed775e DG |
493 | } |
494 | ||
495 | end: | |
9f19cc17 DG |
496 | if (domains) { |
497 | free(domains); | |
498 | } | |
cd80958d DG |
499 | lttng_destroy_handle(handle); |
500 | ||
f3ed775e DG |
501 | return ret; |
502 | } |