| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_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 <assert.h> |
| 28 | |
| 29 | #include <common/mi-lttng.h> |
| 30 | |
| 31 | #include "../command.h" |
| 32 | |
| 33 | static char *opt_event_list; |
| 34 | static int opt_kernel; |
| 35 | static char *opt_channel_name; |
| 36 | static char *opt_session_name; |
| 37 | static int opt_userspace; |
| 38 | static int opt_disable_all; |
| 39 | static int opt_jul; |
| 40 | static int opt_log4j; |
| 41 | static int opt_python; |
| 42 | static int opt_event_type; |
| 43 | |
| 44 | enum { |
| 45 | OPT_HELP = 1, |
| 46 | OPT_USERSPACE, |
| 47 | OPT_SYSCALL, |
| 48 | OPT_LIST_OPTIONS, |
| 49 | }; |
| 50 | |
| 51 | static struct lttng_handle *handle; |
| 52 | static struct mi_writer *writer; |
| 53 | |
| 54 | static struct poptOption long_options[] = { |
| 55 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 56 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 57 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 58 | {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0}, |
| 59 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
| 60 | {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0}, |
| 61 | {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0}, |
| 62 | {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0}, |
| 63 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 64 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
| 65 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 66 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 67 | {0, 0, 0, 0, 0, 0, 0} |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * usage |
| 72 | */ |
| 73 | static void usage(FILE *ofp) |
| 74 | { |
| 75 | fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n"); |
| 76 | fprintf(ofp, "\n"); |
| 77 | fprintf(ofp, "Options:\n"); |
| 78 | fprintf(ofp, " -h, --help Show this help\n"); |
| 79 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 80 | fprintf(ofp, " -s, --session NAME Apply to session name\n"); |
| 81 | fprintf(ofp, " -c, --channel NAME Apply to this channel\n"); |
| 82 | fprintf(ofp, " -a, --all-events Disable all tracepoints\n"); |
| 83 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 84 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
| 85 | fprintf(ofp, " -j, --jul Apply for Java application using JUL\n"); |
| 86 | fprintf(ofp, " -l, --log4j Apply to Java application using LOG4j\n"); |
| 87 | fprintf(ofp, " -p, --python Apply to Python application using logging\n"); |
| 88 | fprintf(ofp, "\n"); |
| 89 | fprintf(ofp, "Event options:\n"); |
| 90 | fprintf(ofp, " --syscall System call event\n"); |
| 91 | fprintf(ofp, "\n"); |
| 92 | } |
| 93 | |
| 94 | static |
| 95 | const char *print_channel_name(const char *name) |
| 96 | { |
| 97 | return name ? : DEFAULT_CHANNEL_NAME; |
| 98 | } |
| 99 | |
| 100 | static |
| 101 | const char *print_raw_channel_name(const char *name) |
| 102 | { |
| 103 | return name ? : "<default>"; |
| 104 | } |
| 105 | |
| 106 | /* Mi print a partial event. |
| 107 | * enabled is 0 or 1 |
| 108 | * success is 0 or 1 |
| 109 | */ |
| 110 | static int mi_print_event(char *event_name, int enabled, int success) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | assert(writer); |
| 115 | assert(event_name); |
| 116 | |
| 117 | /* Open event element */ |
| 118 | ret = mi_lttng_writer_open_element(writer, config_element_event); |
| 119 | if (ret) { |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | /* Print the name of event */ |
| 124 | ret = mi_lttng_writer_write_element_string(writer, |
| 125 | config_element_name, event_name); |
| 126 | if (ret) { |
| 127 | goto end; |
| 128 | } |
| 129 | |
| 130 | /* Print enabled ? */ |
| 131 | ret = mi_lttng_writer_write_element_bool(writer, |
| 132 | config_element_enabled, enabled); |
| 133 | if (ret) { |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | /* Success ? */ |
| 138 | ret = mi_lttng_writer_write_element_bool(writer, |
| 139 | mi_lttng_element_command_success, success); |
| 140 | if (ret) { |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | /* Close event element */ |
| 145 | ret = mi_lttng_writer_close_element(writer); |
| 146 | end: |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * disable_events |
| 152 | * |
| 153 | * Disabling event using the lttng API. |
| 154 | */ |
| 155 | static int disable_events(char *session_name) |
| 156 | { |
| 157 | int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS; |
| 158 | int enabled = 1, success = 1; |
| 159 | char *event_name, *channel_name = NULL; |
| 160 | struct lttng_domain dom; |
| 161 | struct lttng_event event; |
| 162 | |
| 163 | memset(&dom, 0, sizeof(dom)); |
| 164 | |
| 165 | /* Create lttng domain */ |
| 166 | if (opt_kernel) { |
| 167 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 168 | } else if (opt_userspace) { |
| 169 | dom.type = LTTNG_DOMAIN_UST; |
| 170 | } else if (opt_jul) { |
| 171 | dom.type = LTTNG_DOMAIN_JUL; |
| 172 | } else if (opt_log4j) { |
| 173 | dom.type = LTTNG_DOMAIN_LOG4J; |
| 174 | } else if (opt_python) { |
| 175 | dom.type = LTTNG_DOMAIN_PYTHON; |
| 176 | } else { |
| 177 | /* Checked by the caller. */ |
| 178 | assert(0); |
| 179 | } |
| 180 | |
| 181 | channel_name = opt_channel_name; |
| 182 | |
| 183 | handle = lttng_create_handle(session_name, &dom); |
| 184 | if (handle == NULL) { |
| 185 | ret = -1; |
| 186 | goto error; |
| 187 | } |
| 188 | |
| 189 | /* Mi print the channel and open the events element */ |
| 190 | if (lttng_opt_mi) { |
| 191 | ret = mi_lttng_writer_open_element(writer, config_element_channel); |
| 192 | if (ret) { |
| 193 | ret = CMD_ERROR; |
| 194 | goto end; |
| 195 | } |
| 196 | |
| 197 | ret = mi_lttng_writer_write_element_string(writer, |
| 198 | config_element_name, print_channel_name(channel_name)); |
| 199 | if (ret) { |
| 200 | ret = CMD_ERROR; |
| 201 | goto end; |
| 202 | } |
| 203 | |
| 204 | /* Open events element */ |
| 205 | ret = mi_lttng_writer_open_element(writer, config_element_events); |
| 206 | if (ret) { |
| 207 | ret = CMD_ERROR; |
| 208 | goto end; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | memset(&event, 0, sizeof(event)); |
| 213 | /* Set default loglevel to any/unknown */ |
| 214 | event.loglevel = -1; |
| 215 | |
| 216 | switch (opt_event_type) { |
| 217 | case LTTNG_EVENT_SYSCALL: |
| 218 | event.type = LTTNG_EVENT_SYSCALL; |
| 219 | break; |
| 220 | default: |
| 221 | event.type = LTTNG_EVENT_ALL; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | if (opt_disable_all) { |
| 226 | command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL); |
| 227 | if (command_ret < 0) { |
| 228 | ERR("%s", lttng_strerror(command_ret)); |
| 229 | enabled = 1; |
| 230 | success = 0; |
| 231 | |
| 232 | } else { |
| 233 | enabled = 0; |
| 234 | success = 1; |
| 235 | MSG("All %s %s are disabled in channel %s", |
| 236 | get_domain_str(dom.type), |
| 237 | opt_event_type == LTTNG_EVENT_SYSCALL ? "system calls" : "events", |
| 238 | print_channel_name(channel_name)); |
| 239 | } |
| 240 | |
| 241 | if (lttng_opt_mi) { |
| 242 | ret = mi_print_event("*", enabled, success); |
| 243 | if (ret) { |
| 244 | ret = CMD_ERROR; |
| 245 | goto error; |
| 246 | } |
| 247 | } |
| 248 | } else { |
| 249 | /* Strip event list */ |
| 250 | event_name = strtok(opt_event_list, ","); |
| 251 | while (event_name != NULL) { |
| 252 | DBG("Disabling event %s", event_name); |
| 253 | |
| 254 | strncpy(event.name, event_name, sizeof(event.name)); |
| 255 | event.name[sizeof(event.name) - 1] = '\0'; |
| 256 | command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL); |
| 257 | if (command_ret < 0) { |
| 258 | ERR("%s %s: %s (channel %s, session %s)", |
| 259 | opt_event_type == LTTNG_EVENT_SYSCALL ? "System call" : "Event", |
| 260 | event_name, |
| 261 | lttng_strerror(command_ret), |
| 262 | command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME |
| 263 | ? print_raw_channel_name(channel_name) |
| 264 | : print_channel_name(channel_name), |
| 265 | session_name); |
| 266 | warn = 1; |
| 267 | success = 0; |
| 268 | /* |
| 269 | * If an error occurred we assume that the event is still |
| 270 | * enabled. |
| 271 | */ |
| 272 | enabled = 1; |
| 273 | } else { |
| 274 | MSG("%s %s %s disabled in channel %s for session %s", |
| 275 | get_domain_str(dom.type), |
| 276 | opt_event_type == LTTNG_EVENT_SYSCALL ? "system call" : "event", |
| 277 | event_name, |
| 278 | print_channel_name(channel_name), |
| 279 | session_name); |
| 280 | success = 1; |
| 281 | enabled = 0; |
| 282 | } |
| 283 | |
| 284 | if (lttng_opt_mi) { |
| 285 | ret = mi_print_event(event_name, enabled, success); |
| 286 | if (ret) { |
| 287 | ret = CMD_ERROR; |
| 288 | goto error; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* Next event */ |
| 293 | event_name = strtok(NULL, ","); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | end: |
| 298 | if (lttng_opt_mi) { |
| 299 | /* Close events element and channel element */ |
| 300 | ret = mi_lttng_close_multi_element(writer, 2); |
| 301 | if (ret) { |
| 302 | ret = CMD_ERROR; |
| 303 | } |
| 304 | } |
| 305 | error: |
| 306 | /* if there is already an error preserve it */ |
| 307 | if (warn && !ret) { |
| 308 | ret = CMD_WARNING; |
| 309 | } |
| 310 | |
| 311 | /* Overwrite ret if an error occurred */ |
| 312 | ret = command_ret ? command_ret : ret; |
| 313 | |
| 314 | lttng_destroy_handle(handle); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * cmd_disable_events |
| 320 | * |
| 321 | * Disable event to trace session |
| 322 | */ |
| 323 | int cmd_disable_events(int argc, const char **argv) |
| 324 | { |
| 325 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 326 | static poptContext pc; |
| 327 | char *session_name = NULL; |
| 328 | int event_type = -1; |
| 329 | |
| 330 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 331 | poptReadDefaultConfig(pc, 0); |
| 332 | |
| 333 | /* Default event type */ |
| 334 | opt_event_type = LTTNG_EVENT_ALL; |
| 335 | |
| 336 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 337 | switch (opt) { |
| 338 | case OPT_HELP: |
| 339 | usage(stdout); |
| 340 | goto end; |
| 341 | case OPT_USERSPACE: |
| 342 | opt_userspace = 1; |
| 343 | break; |
| 344 | case OPT_SYSCALL: |
| 345 | opt_event_type = LTTNG_EVENT_SYSCALL; |
| 346 | break; |
| 347 | case OPT_LIST_OPTIONS: |
| 348 | list_cmd_options(stdout, long_options); |
| 349 | goto end; |
| 350 | default: |
| 351 | usage(stderr); |
| 352 | ret = CMD_UNDEFINED; |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | /* Validate event type. Multiple event type are not supported. */ |
| 357 | if (event_type == -1) { |
| 358 | event_type = opt_event_type; |
| 359 | } else { |
| 360 | if (event_type != opt_event_type) { |
| 361 | ERR("Multiple event type not supported."); |
| 362 | ret = CMD_ERROR; |
| 363 | goto end; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | ret = print_missing_or_multiple_domains( |
| 369 | opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python); |
| 370 | if (ret) { |
| 371 | ret = CMD_ERROR; |
| 372 | goto end; |
| 373 | } |
| 374 | |
| 375 | opt_event_list = (char*) poptGetArg(pc); |
| 376 | if (opt_event_list == NULL && opt_disable_all == 0) { |
| 377 | ERR("Missing event name(s).\n"); |
| 378 | usage(stderr); |
| 379 | ret = CMD_ERROR; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | if (!opt_session_name) { |
| 384 | session_name = get_session_name(); |
| 385 | if (session_name == NULL) { |
| 386 | ret = CMD_ERROR; |
| 387 | goto end; |
| 388 | } |
| 389 | } else { |
| 390 | session_name = opt_session_name; |
| 391 | } |
| 392 | |
| 393 | /* Mi check */ |
| 394 | if (lttng_opt_mi) { |
| 395 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 396 | if (!writer) { |
| 397 | ret = -LTTNG_ERR_NOMEM; |
| 398 | goto end; |
| 399 | } |
| 400 | |
| 401 | /* Open command element */ |
| 402 | ret = mi_lttng_writer_command_open(writer, |
| 403 | mi_lttng_element_command_disable_event); |
| 404 | if (ret) { |
| 405 | ret = CMD_ERROR; |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | /* Open output element */ |
| 410 | ret = mi_lttng_writer_open_element(writer, |
| 411 | mi_lttng_element_command_output); |
| 412 | if (ret) { |
| 413 | ret = CMD_ERROR; |
| 414 | goto end; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | command_ret = disable_events(session_name); |
| 419 | if (command_ret) { |
| 420 | success = 0; |
| 421 | } |
| 422 | |
| 423 | /* Mi closing */ |
| 424 | if (lttng_opt_mi) { |
| 425 | /* Close output element */ |
| 426 | ret = mi_lttng_writer_close_element(writer); |
| 427 | if (ret) { |
| 428 | ret = CMD_ERROR; |
| 429 | goto end; |
| 430 | } |
| 431 | |
| 432 | ret = mi_lttng_writer_write_element_bool(writer, |
| 433 | mi_lttng_element_command_success, success); |
| 434 | if (ret) { |
| 435 | ret = CMD_ERROR; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | /* Command element close */ |
| 440 | ret = mi_lttng_writer_command_close(writer); |
| 441 | if (ret) { |
| 442 | ret = CMD_ERROR; |
| 443 | goto end; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | end: |
| 448 | if (!opt_session_name && session_name) { |
| 449 | free(session_name); |
| 450 | } |
| 451 | |
| 452 | /* Mi clean-up */ |
| 453 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 454 | /* Preserve original error code */ |
| 455 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; |
| 456 | } |
| 457 | |
| 458 | /* Overwrite ret if an error occured in disable_events */ |
| 459 | ret = command_ret ? command_ret : ret; |
| 460 | |
| 461 | poptFreeContext(pc); |
| 462 | return ret; |
| 463 | } |