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