| 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 <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <ctype.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static char *opt_event_list; |
| 33 | static int opt_event_type; |
| 34 | static int opt_kernel; |
| 35 | static char *opt_session_name; |
| 36 | static int opt_userspace; |
| 37 | static int opt_enable_all; |
| 38 | static char *opt_probe; |
| 39 | static char *opt_function; |
| 40 | static char *opt_function_entry_symbol; |
| 41 | static char *opt_channel_name; |
| 42 | #if 0 |
| 43 | /* Not implemented yet */ |
| 44 | static char *opt_cmd_name; |
| 45 | static pid_t opt_pid; |
| 46 | #endif |
| 47 | |
| 48 | enum { |
| 49 | OPT_HELP = 1, |
| 50 | OPT_TRACEPOINT, |
| 51 | OPT_PROBE, |
| 52 | OPT_FUNCTION, |
| 53 | OPT_FUNCTION_ENTRY, |
| 54 | OPT_SYSCALL, |
| 55 | OPT_USERSPACE, |
| 56 | OPT_TRACEPOINT_LOGLEVEL, |
| 57 | }; |
| 58 | |
| 59 | static struct lttng_handle *handle; |
| 60 | |
| 61 | static struct poptOption long_options[] = { |
| 62 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 63 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 64 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 65 | {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0}, |
| 66 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
| 67 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 68 | #if 0 |
| 69 | /* Not implemented yet */ |
| 70 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
| 71 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 72 | #else |
| 73 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 74 | #endif |
| 75 | {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0}, |
| 76 | {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0}, |
| 77 | {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0}, |
| 78 | #if 0 |
| 79 | /* |
| 80 | * Currently removed from lttng kernel tracer. Removed from |
| 81 | * lttng UI to discourage its use. |
| 82 | */ |
| 83 | {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0}, |
| 84 | #endif |
| 85 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
| 86 | {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0}, |
| 87 | {0, 0, 0, 0, 0, 0, 0} |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * usage |
| 92 | */ |
| 93 | static void usage(FILE *ofp) |
| 94 | { |
| 95 | fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n"); |
| 96 | fprintf(ofp, "\n"); |
| 97 | fprintf(ofp, " -h, --help Show this help\n"); |
| 98 | fprintf(ofp, " -s, --session Apply on session name\n"); |
| 99 | fprintf(ofp, " -c, --channel Apply on this channel\n"); |
| 100 | fprintf(ofp, " -a, --all Enable all tracepoints\n"); |
| 101 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 102 | #if 0 |
| 103 | fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); |
| 104 | fprintf(ofp, " If no CMD, the domain used is UST global\n"); |
| 105 | fprintf(ofp, " or else the domain is UST EXEC_NAME\n"); |
| 106 | fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n"); |
| 107 | #else |
| 108 | fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n"); |
| 109 | #endif |
| 110 | fprintf(ofp, "\n"); |
| 111 | fprintf(ofp, "Event options:\n"); |
| 112 | fprintf(ofp, " --tracepoint Tracepoint event (default)\n"); |
| 113 | fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n"); |
| 114 | fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n"); |
| 115 | fprintf(ofp, " e.g.:\n"); |
| 116 | fprintf(ofp, " \"*\"\n"); |
| 117 | fprintf(ofp, " \"app_component:na*\"\n"); |
| 118 | fprintf(ofp, " --loglevel Tracepoint loglevel\n"); |
| 119 | fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n"); |
| 120 | fprintf(ofp, " Dynamic probe.\n"); |
| 121 | fprintf(ofp, " Addr and offset can be octal (0NNN...),\n"); |
| 122 | fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n"); |
| 123 | fprintf(ofp, " --function [addr | symbol | symbol+offset]\n"); |
| 124 | fprintf(ofp, " Dynamic function entry/return probe.\n"); |
| 125 | fprintf(ofp, " Addr and offset can be octal (0NNN...),\n"); |
| 126 | fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n"); |
| 127 | #if 0 |
| 128 | fprintf(ofp, " --function:entry symbol\n"); |
| 129 | fprintf(ofp, " Function tracer event\n"); |
| 130 | #endif |
| 131 | fprintf(ofp, " --syscall System call event\n"); |
| 132 | fprintf(ofp, "\n"); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Parse probe options. |
| 137 | */ |
| 138 | static int parse_probe_opts(struct lttng_event *ev, char *opt) |
| 139 | { |
| 140 | int ret; |
| 141 | char s_hex[19]; |
| 142 | char name[LTTNG_SYMBOL_NAME_LEN]; |
| 143 | |
| 144 | if (opt == NULL) { |
| 145 | ret = -1; |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | /* Check for symbol+offset */ |
| 150 | ret = sscanf(opt, "%[^'+']+%s", name, s_hex); |
| 151 | if (ret == 2) { |
| 152 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
| 153 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 154 | DBG("probe symbol %s", ev->attr.probe.symbol_name); |
| 155 | if (strlen(s_hex) == 0) { |
| 156 | ERR("Invalid probe offset %s", s_hex); |
| 157 | ret = -1; |
| 158 | goto end; |
| 159 | } |
| 160 | ev->attr.probe.offset = strtoul(s_hex, NULL, 0); |
| 161 | DBG("probe offset %" PRIu64, ev->attr.probe.offset); |
| 162 | ev->attr.probe.addr = 0; |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | /* Check for symbol */ |
| 167 | if (isalpha(name[0])) { |
| 168 | ret = sscanf(opt, "%s", name); |
| 169 | if (ret == 1) { |
| 170 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
| 171 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 172 | DBG("probe symbol %s", ev->attr.probe.symbol_name); |
| 173 | ev->attr.probe.offset = 0; |
| 174 | DBG("probe offset %" PRIu64, ev->attr.probe.offset); |
| 175 | ev->attr.probe.addr = 0; |
| 176 | goto end; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* Check for address */ |
| 181 | ret = sscanf(opt, "%s", s_hex); |
| 182 | if (ret > 0) { |
| 183 | if (strlen(s_hex) == 0) { |
| 184 | ERR("Invalid probe address %s", s_hex); |
| 185 | ret = -1; |
| 186 | goto end; |
| 187 | } |
| 188 | ev->attr.probe.addr = strtoul(s_hex, NULL, 0); |
| 189 | DBG("probe addr %" PRIu64, ev->attr.probe.addr); |
| 190 | ev->attr.probe.offset = 0; |
| 191 | memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN); |
| 192 | goto end; |
| 193 | } |
| 194 | |
| 195 | /* No match */ |
| 196 | ret = -1; |
| 197 | |
| 198 | end: |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Enabling event using the lttng API. |
| 204 | */ |
| 205 | static int enable_events(char *session_name) |
| 206 | { |
| 207 | int err, ret = CMD_SUCCESS; |
| 208 | char *event_name, *channel_name = NULL; |
| 209 | struct lttng_event ev; |
| 210 | struct lttng_domain dom; |
| 211 | |
| 212 | if (opt_channel_name == NULL) { |
| 213 | err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME); |
| 214 | if (err < 0) { |
| 215 | ret = CMD_FATAL; |
| 216 | goto error; |
| 217 | } |
| 218 | } else { |
| 219 | channel_name = opt_channel_name; |
| 220 | } |
| 221 | |
| 222 | if (opt_kernel && opt_userspace) { |
| 223 | ERR("Can't use -k/--kernel and -u/--userspace together"); |
| 224 | ret = CMD_FATAL; |
| 225 | goto error; |
| 226 | } |
| 227 | |
| 228 | /* Create lttng domain */ |
| 229 | if (opt_kernel) { |
| 230 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 231 | } else if (opt_userspace) { |
| 232 | dom.type = LTTNG_DOMAIN_UST; |
| 233 | } else { |
| 234 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
| 235 | ret = CMD_NOT_IMPLEMENTED; |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | handle = lttng_create_handle(session_name, &dom); |
| 240 | if (handle == NULL) { |
| 241 | ret = -1; |
| 242 | goto error; |
| 243 | } |
| 244 | |
| 245 | if (opt_enable_all) { |
| 246 | /* Default setup for enable all */ |
| 247 | |
| 248 | if (opt_kernel) { |
| 249 | ev.type = opt_event_type; |
| 250 | ev.name[0] = '\0'; |
| 251 | } else { |
| 252 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 253 | strcpy(ev.name, "*"); |
| 254 | } |
| 255 | |
| 256 | ret = lttng_enable_event(handle, &ev, channel_name); |
| 257 | if (ret < 0) { |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | switch (opt_event_type) { |
| 262 | case LTTNG_EVENT_TRACEPOINT: |
| 263 | MSG("All %s tracepoints are enabled in channel %s", |
| 264 | opt_kernel ? "kernel" : "UST", channel_name); |
| 265 | break; |
| 266 | case LTTNG_EVENT_SYSCALL: |
| 267 | if (opt_kernel) { |
| 268 | MSG("All kernel system calls are enabled in channel %s", |
| 269 | channel_name); |
| 270 | } |
| 271 | break; |
| 272 | case LTTNG_EVENT_ALL: |
| 273 | MSG("All %s events are enabled in channel %s", |
| 274 | opt_kernel ? "kernel" : "UST", channel_name); |
| 275 | break; |
| 276 | default: |
| 277 | /* |
| 278 | * We should not be here since lttng_enable_event should have |
| 279 | * failed on the event type. |
| 280 | */ |
| 281 | goto error; |
| 282 | } |
| 283 | goto end; |
| 284 | } |
| 285 | |
| 286 | /* Strip event list */ |
| 287 | event_name = strtok(opt_event_list, ","); |
| 288 | while (event_name != NULL) { |
| 289 | /* Copy name and type of the event */ |
| 290 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 291 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 292 | ev.type = opt_event_type; |
| 293 | |
| 294 | /* Kernel tracer action */ |
| 295 | if (opt_kernel) { |
| 296 | DBG("Enabling kernel event %s for channel %s", |
| 297 | event_name, channel_name); |
| 298 | |
| 299 | switch (opt_event_type) { |
| 300 | case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */ |
| 301 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 302 | /* Fall-through */ |
| 303 | case LTTNG_EVENT_TRACEPOINT: |
| 304 | break; |
| 305 | case LTTNG_EVENT_PROBE: |
| 306 | ret = parse_probe_opts(&ev, opt_probe); |
| 307 | if (ret < 0) { |
| 308 | ERR("Unable to parse probe options"); |
| 309 | ret = 0; |
| 310 | goto error; |
| 311 | } |
| 312 | break; |
| 313 | case LTTNG_EVENT_FUNCTION: |
| 314 | ret = parse_probe_opts(&ev, opt_function); |
| 315 | if (ret < 0) { |
| 316 | ERR("Unable to parse function probe options"); |
| 317 | ret = 0; |
| 318 | goto error; |
| 319 | } |
| 320 | break; |
| 321 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 322 | strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol, |
| 323 | LTTNG_SYMBOL_NAME_LEN); |
| 324 | ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 325 | break; |
| 326 | case LTTNG_EVENT_SYSCALL: |
| 327 | MSG("per-syscall selection not supported yet. Use \"-a\" " |
| 328 | "for all syscalls."); |
| 329 | default: |
| 330 | ret = CMD_NOT_IMPLEMENTED; |
| 331 | goto error; |
| 332 | } |
| 333 | } else if (opt_userspace) { /* User-space tracer action */ |
| 334 | #if 0 |
| 335 | if (opt_cmd_name != NULL || opt_pid) { |
| 336 | MSG("Only supporting tracing all UST processes (-u) for now."); |
| 337 | ret = CMD_NOT_IMPLEMENTED; |
| 338 | goto error; |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | DBG("Enabling UST event %s for channel %s", event_name, |
| 343 | channel_name); |
| 344 | |
| 345 | switch (opt_event_type) { |
| 346 | case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */ |
| 347 | /* Fall-through */ |
| 348 | case LTTNG_EVENT_TRACEPOINT: |
| 349 | /* Copy name and type of the event */ |
| 350 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 351 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 352 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 353 | break; |
| 354 | case LTTNG_EVENT_TRACEPOINT_LOGLEVEL: |
| 355 | /* Copy name and type of the event */ |
| 356 | ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL; |
| 357 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 358 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 359 | break; |
| 360 | case LTTNG_EVENT_PROBE: |
| 361 | case LTTNG_EVENT_FUNCTION: |
| 362 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 363 | case LTTNG_EVENT_SYSCALL: |
| 364 | default: |
| 365 | ret = CMD_NOT_IMPLEMENTED; |
| 366 | goto error; |
| 367 | } |
| 368 | } else { |
| 369 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
| 370 | goto error; |
| 371 | } |
| 372 | |
| 373 | ret = lttng_enable_event(handle, &ev, channel_name); |
| 374 | if (ret == 0) { |
| 375 | MSG("%s event %s created in channel %s", |
| 376 | opt_kernel ? "kernel": "UST", event_name, channel_name); |
| 377 | } |
| 378 | |
| 379 | /* Next event */ |
| 380 | event_name = strtok(NULL, ","); |
| 381 | } |
| 382 | |
| 383 | end: |
| 384 | error: |
| 385 | if (opt_channel_name == NULL) { |
| 386 | free(channel_name); |
| 387 | } |
| 388 | lttng_destroy_handle(handle); |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Add event to trace session |
| 395 | */ |
| 396 | int cmd_enable_events(int argc, const char **argv) |
| 397 | { |
| 398 | int opt, ret; |
| 399 | static poptContext pc; |
| 400 | char *session_name = NULL; |
| 401 | |
| 402 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 403 | poptReadDefaultConfig(pc, 0); |
| 404 | |
| 405 | /* Default event type */ |
| 406 | opt_event_type = LTTNG_EVENT_ALL; |
| 407 | |
| 408 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 409 | switch (opt) { |
| 410 | case OPT_HELP: |
| 411 | usage(stderr); |
| 412 | ret = CMD_SUCCESS; |
| 413 | goto end; |
| 414 | case OPT_TRACEPOINT: |
| 415 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
| 416 | break; |
| 417 | case OPT_PROBE: |
| 418 | opt_event_type = LTTNG_EVENT_PROBE; |
| 419 | break; |
| 420 | case OPT_FUNCTION: |
| 421 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 422 | break; |
| 423 | case OPT_FUNCTION_ENTRY: |
| 424 | opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY; |
| 425 | break; |
| 426 | case OPT_SYSCALL: |
| 427 | opt_event_type = LTTNG_EVENT_SYSCALL; |
| 428 | break; |
| 429 | case OPT_USERSPACE: |
| 430 | opt_userspace = 1; |
| 431 | break; |
| 432 | case OPT_TRACEPOINT_LOGLEVEL: |
| 433 | opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL; |
| 434 | break; |
| 435 | default: |
| 436 | usage(stderr); |
| 437 | ret = CMD_UNDEFINED; |
| 438 | goto end; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | opt_event_list = (char*) poptGetArg(pc); |
| 443 | if (opt_event_list == NULL && opt_enable_all == 0) { |
| 444 | ERR("Missing event name(s).\n"); |
| 445 | usage(stderr); |
| 446 | ret = CMD_SUCCESS; |
| 447 | goto end; |
| 448 | } |
| 449 | |
| 450 | if (!opt_session_name) { |
| 451 | session_name = get_session_name(); |
| 452 | if (session_name == NULL) { |
| 453 | ret = -1; |
| 454 | goto end; |
| 455 | } |
| 456 | } else { |
| 457 | session_name = opt_session_name; |
| 458 | } |
| 459 | |
| 460 | ret = enable_events(session_name); |
| 461 | |
| 462 | end: |
| 463 | if (opt_session_name == NULL) { |
| 464 | free(session_name); |
| 465 | } |
| 466 | |
| 467 | return ret; |
| 468 | } |