| 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 _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #include <src/common/sessiond-comm/sessiond-comm.h> |
| 30 | #include <common/compat/string.h> |
| 31 | |
| 32 | /* Mi dependancy */ |
| 33 | #include <common/mi-lttng.h> |
| 34 | |
| 35 | #include "../command.h" |
| 36 | |
| 37 | #if (LTTNG_SYMBOL_NAME_LEN == 256) |
| 38 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" |
| 39 | #endif |
| 40 | |
| 41 | static char *opt_event_list; |
| 42 | static int opt_event_type; |
| 43 | static const char *opt_loglevel; |
| 44 | static int opt_loglevel_type; |
| 45 | static int opt_kernel; |
| 46 | static char *opt_session_name; |
| 47 | static int opt_userspace; |
| 48 | static int opt_jul; |
| 49 | static int opt_log4j; |
| 50 | static int opt_python; |
| 51 | static int opt_enable_all; |
| 52 | static char *opt_probe; |
| 53 | static char *opt_function; |
| 54 | static char *opt_channel_name; |
| 55 | static char *opt_filter; |
| 56 | static char *opt_exclude; |
| 57 | |
| 58 | enum { |
| 59 | OPT_HELP = 1, |
| 60 | OPT_TRACEPOINT, |
| 61 | OPT_PROBE, |
| 62 | OPT_FUNCTION, |
| 63 | OPT_SYSCALL, |
| 64 | OPT_USERSPACE, |
| 65 | OPT_LOGLEVEL, |
| 66 | OPT_LOGLEVEL_ONLY, |
| 67 | OPT_LIST_OPTIONS, |
| 68 | OPT_FILTER, |
| 69 | OPT_EXCLUDE, |
| 70 | }; |
| 71 | |
| 72 | static struct lttng_handle *handle; |
| 73 | static struct mi_writer *writer; |
| 74 | |
| 75 | static struct poptOption long_options[] = { |
| 76 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 77 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 78 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 79 | {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0}, |
| 80 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
| 81 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 82 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 83 | {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0}, |
| 84 | {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0}, |
| 85 | {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0}, |
| 86 | {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0}, |
| 87 | {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0}, |
| 88 | {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0}, |
| 89 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
| 90 | {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0}, |
| 91 | {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0}, |
| 92 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 93 | {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0}, |
| 94 | {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0}, |
| 95 | {0, 0, 0, 0, 0, 0, 0} |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * Parse probe options. |
| 100 | */ |
| 101 | static int parse_probe_opts(struct lttng_event *ev, char *opt) |
| 102 | { |
| 103 | int ret = CMD_SUCCESS; |
| 104 | int match; |
| 105 | char s_hex[19]; |
| 106 | #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */ |
| 107 | char name[LTTNG_SYMBOL_NAME_LEN]; |
| 108 | |
| 109 | if (opt == NULL) { |
| 110 | ret = CMD_ERROR; |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | /* Check for symbol+offset */ |
| 115 | match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API |
| 116 | "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex); |
| 117 | if (match == 2) { |
| 118 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
| 119 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 120 | DBG("probe symbol %s", ev->attr.probe.symbol_name); |
| 121 | if (*s_hex == '\0') { |
| 122 | ERR("Invalid probe offset %s", s_hex); |
| 123 | ret = CMD_ERROR; |
| 124 | goto end; |
| 125 | } |
| 126 | ev->attr.probe.offset = strtoul(s_hex, NULL, 0); |
| 127 | DBG("probe offset %" PRIu64, ev->attr.probe.offset); |
| 128 | ev->attr.probe.addr = 0; |
| 129 | goto end; |
| 130 | } |
| 131 | |
| 132 | /* Check for symbol */ |
| 133 | if (isalpha(name[0])) { |
| 134 | match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s", |
| 135 | name); |
| 136 | if (match == 1) { |
| 137 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
| 138 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 139 | DBG("probe symbol %s", ev->attr.probe.symbol_name); |
| 140 | ev->attr.probe.offset = 0; |
| 141 | DBG("probe offset %" PRIu64, ev->attr.probe.offset); |
| 142 | ev->attr.probe.addr = 0; |
| 143 | goto end; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Check for address */ |
| 148 | match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex); |
| 149 | if (match > 0) { |
| 150 | if (*s_hex == '\0') { |
| 151 | ERR("Invalid probe address %s", s_hex); |
| 152 | ret = CMD_ERROR; |
| 153 | goto end; |
| 154 | } |
| 155 | ev->attr.probe.addr = strtoul(s_hex, NULL, 0); |
| 156 | DBG("probe addr %" PRIu64, ev->attr.probe.addr); |
| 157 | ev->attr.probe.offset = 0; |
| 158 | memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN); |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 162 | /* No match */ |
| 163 | ret = CMD_ERROR; |
| 164 | |
| 165 | end: |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Maps LOG4j loglevel from string to value |
| 171 | */ |
| 172 | static int loglevel_log4j_str_to_value(const char *inputstr) |
| 173 | { |
| 174 | int i = 0; |
| 175 | char str[LTTNG_SYMBOL_NAME_LEN]; |
| 176 | |
| 177 | if (!inputstr || strlen(inputstr) == 0) { |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is |
| 183 | * added at the end of the loop so a the upper bound we avoid the overflow. |
| 184 | */ |
| 185 | while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') { |
| 186 | str[i] = toupper(inputstr[i]); |
| 187 | i++; |
| 188 | } |
| 189 | str[i] = '\0'; |
| 190 | |
| 191 | if (!strcmp(str, "LOG4J_OFF") || !strcmp(str, "OFF")) { |
| 192 | return LTTNG_LOGLEVEL_LOG4J_OFF; |
| 193 | } else if (!strcmp(str, "LOG4J_FATAL") || !strcmp(str, "FATAL")) { |
| 194 | return LTTNG_LOGLEVEL_LOG4J_FATAL; |
| 195 | } else if (!strcmp(str, "LOG4J_ERROR") || !strcmp(str, "ERROR")) { |
| 196 | return LTTNG_LOGLEVEL_LOG4J_ERROR; |
| 197 | } else if (!strcmp(str, "LOG4J_WARN") || !strcmp(str, "WARN")) { |
| 198 | return LTTNG_LOGLEVEL_LOG4J_WARN; |
| 199 | } else if (!strcmp(str, "LOG4J_INFO") || !strcmp(str, "INFO")) { |
| 200 | return LTTNG_LOGLEVEL_LOG4J_INFO; |
| 201 | } else if (!strcmp(str, "LOG4J_DEBUG") || !strcmp(str, "DEBUG")) { |
| 202 | return LTTNG_LOGLEVEL_LOG4J_DEBUG; |
| 203 | } else if (!strcmp(str, "LOG4J_TRACE") || !strcmp(str, "TRACE")) { |
| 204 | return LTTNG_LOGLEVEL_LOG4J_TRACE; |
| 205 | } else if (!strcmp(str, "LOG4J_ALL") || !strcmp(str, "ALL")) { |
| 206 | return LTTNG_LOGLEVEL_LOG4J_ALL; |
| 207 | } else { |
| 208 | return -1; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Maps JUL loglevel from string to value |
| 214 | */ |
| 215 | static int loglevel_jul_str_to_value(const char *inputstr) |
| 216 | { |
| 217 | int i = 0; |
| 218 | char str[LTTNG_SYMBOL_NAME_LEN]; |
| 219 | |
| 220 | if (!inputstr || strlen(inputstr) == 0) { |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is |
| 226 | * added at the end of the loop so a the upper bound we avoid the overflow. |
| 227 | */ |
| 228 | while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') { |
| 229 | str[i] = toupper(inputstr[i]); |
| 230 | i++; |
| 231 | } |
| 232 | str[i] = '\0'; |
| 233 | |
| 234 | if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) { |
| 235 | return LTTNG_LOGLEVEL_JUL_OFF; |
| 236 | } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) { |
| 237 | return LTTNG_LOGLEVEL_JUL_SEVERE; |
| 238 | } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) { |
| 239 | return LTTNG_LOGLEVEL_JUL_WARNING; |
| 240 | } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) { |
| 241 | return LTTNG_LOGLEVEL_JUL_INFO; |
| 242 | } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) { |
| 243 | return LTTNG_LOGLEVEL_JUL_CONFIG; |
| 244 | } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) { |
| 245 | return LTTNG_LOGLEVEL_JUL_FINE; |
| 246 | } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) { |
| 247 | return LTTNG_LOGLEVEL_JUL_FINER; |
| 248 | } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) { |
| 249 | return LTTNG_LOGLEVEL_JUL_FINEST; |
| 250 | } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) { |
| 251 | return LTTNG_LOGLEVEL_JUL_ALL; |
| 252 | } else { |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Maps Python loglevel from string to value |
| 259 | */ |
| 260 | static int loglevel_python_str_to_value(const char *inputstr) |
| 261 | { |
| 262 | int i = 0; |
| 263 | char str[LTTNG_SYMBOL_NAME_LEN]; |
| 264 | |
| 265 | if (!inputstr || strlen(inputstr) == 0) { |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is |
| 271 | * added at the end of the loop so a the upper bound we avoid the overflow. |
| 272 | */ |
| 273 | while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') { |
| 274 | str[i] = toupper(inputstr[i]); |
| 275 | i++; |
| 276 | } |
| 277 | str[i] = '\0'; |
| 278 | |
| 279 | if (!strcmp(str, "PYTHON_CRITICAL") || !strcmp(str, "CRITICAL")) { |
| 280 | return LTTNG_LOGLEVEL_PYTHON_CRITICAL; |
| 281 | } else if (!strcmp(str, "PYTHON_ERROR") || !strcmp(str, "ERROR")) { |
| 282 | return LTTNG_LOGLEVEL_PYTHON_ERROR; |
| 283 | } else if (!strcmp(str, "PYTHON_WARNING") || !strcmp(str, "WARNING")) { |
| 284 | return LTTNG_LOGLEVEL_PYTHON_WARNING; |
| 285 | } else if (!strcmp(str, "PYTHON_INFO") || !strcmp(str, "INFO")) { |
| 286 | return LTTNG_LOGLEVEL_PYTHON_INFO; |
| 287 | } else if (!strcmp(str, "PYTNON_DEBUG") || !strcmp(str, "DEBUG")) { |
| 288 | return LTTNG_LOGLEVEL_PYTHON_DEBUG; |
| 289 | } else if (!strcmp(str, "PYTHON_NOTSET") || !strcmp(str, "NOTSET")) { |
| 290 | return LTTNG_LOGLEVEL_PYTHON_NOTSET; |
| 291 | } else { |
| 292 | return -1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Maps loglevel from string to value |
| 298 | */ |
| 299 | static |
| 300 | int loglevel_str_to_value(const char *inputstr) |
| 301 | { |
| 302 | int i = 0; |
| 303 | char str[LTTNG_SYMBOL_NAME_LEN]; |
| 304 | |
| 305 | if (!inputstr || strlen(inputstr) == 0) { |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is |
| 311 | * added at the end of the loop so a the upper bound we avoid the overflow. |
| 312 | */ |
| 313 | while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') { |
| 314 | str[i] = toupper(inputstr[i]); |
| 315 | i++; |
| 316 | } |
| 317 | str[i] = '\0'; |
| 318 | if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) { |
| 319 | return LTTNG_LOGLEVEL_EMERG; |
| 320 | } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) { |
| 321 | return LTTNG_LOGLEVEL_ALERT; |
| 322 | } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) { |
| 323 | return LTTNG_LOGLEVEL_CRIT; |
| 324 | } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) { |
| 325 | return LTTNG_LOGLEVEL_ERR; |
| 326 | } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) { |
| 327 | return LTTNG_LOGLEVEL_WARNING; |
| 328 | } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) { |
| 329 | return LTTNG_LOGLEVEL_NOTICE; |
| 330 | } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) { |
| 331 | return LTTNG_LOGLEVEL_INFO; |
| 332 | } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) { |
| 333 | return LTTNG_LOGLEVEL_DEBUG_SYSTEM; |
| 334 | } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) { |
| 335 | return LTTNG_LOGLEVEL_DEBUG_PROGRAM; |
| 336 | } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) { |
| 337 | return LTTNG_LOGLEVEL_DEBUG_PROCESS; |
| 338 | } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) { |
| 339 | return LTTNG_LOGLEVEL_DEBUG_MODULE; |
| 340 | } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) { |
| 341 | return LTTNG_LOGLEVEL_DEBUG_UNIT; |
| 342 | } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) { |
| 343 | return LTTNG_LOGLEVEL_DEBUG_FUNCTION; |
| 344 | } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) { |
| 345 | return LTTNG_LOGLEVEL_DEBUG_LINE; |
| 346 | } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) { |
| 347 | return LTTNG_LOGLEVEL_DEBUG; |
| 348 | } else { |
| 349 | return -1; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static |
| 354 | const char *print_channel_name(const char *name) |
| 355 | { |
| 356 | return name ? : DEFAULT_CHANNEL_NAME; |
| 357 | } |
| 358 | |
| 359 | static |
| 360 | const char *print_raw_channel_name(const char *name) |
| 361 | { |
| 362 | return name ? : "<default>"; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Mi print exlcusion list |
| 367 | */ |
| 368 | static |
| 369 | int mi_print_exclusion(int count, char **names) |
| 370 | { |
| 371 | int i, ret; |
| 372 | |
| 373 | assert(writer); |
| 374 | |
| 375 | if (count == 0) { |
| 376 | ret = 0; |
| 377 | goto end; |
| 378 | } |
| 379 | ret = mi_lttng_writer_open_element(writer, config_element_exclusions); |
| 380 | if (ret) { |
| 381 | goto end; |
| 382 | } |
| 383 | |
| 384 | for (i = 0; i < count; i++) { |
| 385 | ret = mi_lttng_writer_write_element_string(writer, |
| 386 | config_element_exclusion, names[i]); |
| 387 | if (ret) { |
| 388 | goto end; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* Close exclusions element */ |
| 393 | ret = mi_lttng_writer_close_element(writer); |
| 394 | |
| 395 | end: |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Return allocated string for pretty-printing exclusion names. |
| 401 | */ |
| 402 | static |
| 403 | char *print_exclusions(int count, char **names) |
| 404 | { |
| 405 | int length = 0; |
| 406 | int i; |
| 407 | const char *preamble = " excluding "; |
| 408 | char *ret; |
| 409 | |
| 410 | if (count == 0) { |
| 411 | return strdup(""); |
| 412 | } |
| 413 | |
| 414 | /* calculate total required length */ |
| 415 | for (i = 0; i < count; i++) { |
| 416 | length += strlen(names[i]) + 1; |
| 417 | } |
| 418 | |
| 419 | /* add length of preamble + one for NUL - one for last (missing) comma */ |
| 420 | length += strlen(preamble); |
| 421 | ret = zmalloc(length); |
| 422 | if (!ret) { |
| 423 | return NULL; |
| 424 | } |
| 425 | strncpy(ret, preamble, length); |
| 426 | for (i = 0; i < count; i++) { |
| 427 | strcat(ret, names[i]); |
| 428 | if (i != count - 1) { |
| 429 | strcat(ret, ","); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Compare list of exclusions against an event name. |
| 438 | * Return a list of legal exclusion names. |
| 439 | * Produce an error or a warning about others (depending on the situation) |
| 440 | */ |
| 441 | static |
| 442 | int check_exclusion_subsets(const char *event_name, |
| 443 | const char *exclusions, |
| 444 | int *exclusion_count_ptr, |
| 445 | char ***exclusion_list_ptr) |
| 446 | { |
| 447 | const char *excluder_ptr; |
| 448 | const char *event_ptr; |
| 449 | const char *next_excluder; |
| 450 | int excluder_length; |
| 451 | int exclusion_count = 0; |
| 452 | char **exclusion_list = NULL; |
| 453 | int ret = CMD_SUCCESS; |
| 454 | |
| 455 | if (event_name[strlen(event_name) - 1] != '*') { |
| 456 | ERR("Event %s: Excluders can only be used with wildcarded events", event_name); |
| 457 | goto error; |
| 458 | } |
| 459 | |
| 460 | next_excluder = exclusions; |
| 461 | while (*next_excluder != 0) { |
| 462 | event_ptr = event_name; |
| 463 | excluder_ptr = next_excluder; |
| 464 | excluder_length = strcspn(next_excluder, ","); |
| 465 | |
| 466 | /* Scan both the excluder and the event letter by letter */ |
| 467 | while (1) { |
| 468 | char e, x; |
| 469 | |
| 470 | e = *event_ptr; |
| 471 | x = *excluder_ptr; |
| 472 | |
| 473 | if (x == '*') { |
| 474 | /* Event is a subset of the excluder */ |
| 475 | ERR("Event %s: %.*s excludes all events from %s", |
| 476 | event_name, |
| 477 | excluder_length, |
| 478 | next_excluder, |
| 479 | event_name); |
| 480 | goto error; |
| 481 | } |
| 482 | if (e == '*') { |
| 483 | char *string; |
| 484 | char **new_exclusion_list; |
| 485 | |
| 486 | /* Excluder is a proper subset of event */ |
| 487 | string = lttng_strndup(next_excluder, excluder_length); |
| 488 | if (!string) { |
| 489 | PERROR("lttng_strndup error"); |
| 490 | goto error; |
| 491 | } |
| 492 | new_exclusion_list = realloc(exclusion_list, |
| 493 | sizeof(char *) * (exclusion_count + 1)); |
| 494 | if (!new_exclusion_list) { |
| 495 | PERROR("realloc"); |
| 496 | free(string); |
| 497 | goto error; |
| 498 | } |
| 499 | exclusion_list = new_exclusion_list; |
| 500 | exclusion_count++; |
| 501 | exclusion_list[exclusion_count - 1] = string; |
| 502 | break; |
| 503 | } |
| 504 | if (x != e) { |
| 505 | /* Excluder and event sets have no common elements */ |
| 506 | WARN("Event %s: %.*s does not exclude any events from %s", |
| 507 | event_name, |
| 508 | excluder_length, |
| 509 | next_excluder, |
| 510 | event_name); |
| 511 | break; |
| 512 | } |
| 513 | excluder_ptr++; |
| 514 | event_ptr++; |
| 515 | } |
| 516 | /* next excluder */ |
| 517 | next_excluder += excluder_length; |
| 518 | if (*next_excluder == ',') { |
| 519 | next_excluder++; |
| 520 | } |
| 521 | } |
| 522 | goto end; |
| 523 | error: |
| 524 | while (exclusion_count--) { |
| 525 | free(exclusion_list[exclusion_count]); |
| 526 | } |
| 527 | if (exclusion_list != NULL) { |
| 528 | free(exclusion_list); |
| 529 | } |
| 530 | exclusion_list = NULL; |
| 531 | exclusion_count = 0; |
| 532 | ret = CMD_ERROR; |
| 533 | end: |
| 534 | *exclusion_count_ptr = exclusion_count; |
| 535 | *exclusion_list_ptr = exclusion_list; |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static void warn_on_truncated_exclusion_names(char **exclusion_list, |
| 540 | int exclusion_count, int *warn) |
| 541 | { |
| 542 | size_t i = 0; |
| 543 | |
| 544 | for (i = 0; i < exclusion_count; ++i) { |
| 545 | const char *name = exclusion_list[i]; |
| 546 | size_t len = strlen(name); |
| 547 | |
| 548 | if (len >= LTTNG_SYMBOL_NAME_LEN) { |
| 549 | WARN("Event exclusion \"%s\" will be truncated", |
| 550 | name); |
| 551 | *warn = 1; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Enabling event using the lttng API. |
| 558 | * Note: in case of error only the last error code will be return. |
| 559 | */ |
| 560 | static int enable_events(char *session_name) |
| 561 | { |
| 562 | int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; |
| 563 | int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1; |
| 564 | char *event_name, *channel_name = NULL; |
| 565 | struct lttng_event ev; |
| 566 | struct lttng_domain dom; |
| 567 | int exclusion_count = 0; |
| 568 | char **exclusion_list = NULL; |
| 569 | |
| 570 | memset(&ev, 0, sizeof(ev)); |
| 571 | memset(&dom, 0, sizeof(dom)); |
| 572 | |
| 573 | if (opt_kernel) { |
| 574 | if (opt_loglevel) { |
| 575 | WARN("Kernel loglevels are not supported."); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* Create lttng domain */ |
| 580 | if (opt_kernel) { |
| 581 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 582 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
| 583 | } else if (opt_userspace) { |
| 584 | dom.type = LTTNG_DOMAIN_UST; |
| 585 | /* Default. */ |
| 586 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 587 | } else if (opt_jul) { |
| 588 | dom.type = LTTNG_DOMAIN_JUL; |
| 589 | /* Default. */ |
| 590 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 591 | } else if (opt_log4j) { |
| 592 | dom.type = LTTNG_DOMAIN_LOG4J; |
| 593 | /* Default. */ |
| 594 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 595 | } else if (opt_python) { |
| 596 | dom.type = LTTNG_DOMAIN_PYTHON; |
| 597 | /* Default. */ |
| 598 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 599 | } else { |
| 600 | /* Checked by the caller. */ |
| 601 | assert(0); |
| 602 | } |
| 603 | |
| 604 | if (opt_exclude) { |
| 605 | switch (dom.type) { |
| 606 | case LTTNG_DOMAIN_KERNEL: |
| 607 | case LTTNG_DOMAIN_JUL: |
| 608 | case LTTNG_DOMAIN_LOG4J: |
| 609 | case LTTNG_DOMAIN_PYTHON: |
| 610 | ERR("Event name exclusions are not yet implemented for %s events", |
| 611 | get_domain_str(dom.type)); |
| 612 | ret = CMD_ERROR; |
| 613 | goto error; |
| 614 | case LTTNG_DOMAIN_UST: |
| 615 | /* Exclusions supported */ |
| 616 | break; |
| 617 | default: |
| 618 | assert(0); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | channel_name = opt_channel_name; |
| 623 | |
| 624 | handle = lttng_create_handle(session_name, &dom); |
| 625 | if (handle == NULL) { |
| 626 | ret = -1; |
| 627 | goto error; |
| 628 | } |
| 629 | |
| 630 | /* Prepare Mi */ |
| 631 | if (lttng_opt_mi) { |
| 632 | /* Open a events element */ |
| 633 | ret = mi_lttng_writer_open_element(writer, config_element_events); |
| 634 | if (ret) { |
| 635 | ret = CMD_ERROR; |
| 636 | goto error; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (opt_enable_all) { |
| 641 | /* Default setup for enable all */ |
| 642 | if (opt_kernel) { |
| 643 | ev.type = opt_event_type; |
| 644 | strcpy(ev.name, "*"); |
| 645 | /* kernel loglevels not implemented */ |
| 646 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 647 | } else { |
| 648 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 649 | strcpy(ev.name, "*"); |
| 650 | ev.loglevel_type = opt_loglevel_type; |
| 651 | if (opt_loglevel) { |
| 652 | assert(opt_userspace || opt_jul || opt_log4j || opt_python); |
| 653 | if (opt_userspace) { |
| 654 | ev.loglevel = loglevel_str_to_value(opt_loglevel); |
| 655 | } else if (opt_jul) { |
| 656 | ev.loglevel = loglevel_jul_str_to_value(opt_loglevel); |
| 657 | } else if (opt_log4j) { |
| 658 | ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel); |
| 659 | } else if (opt_python) { |
| 660 | ev.loglevel = loglevel_python_str_to_value(opt_loglevel); |
| 661 | } |
| 662 | if (ev.loglevel == -1) { |
| 663 | ERR("Unknown loglevel %s", opt_loglevel); |
| 664 | ret = -LTTNG_ERR_INVALID; |
| 665 | goto error; |
| 666 | } |
| 667 | } else { |
| 668 | assert(opt_userspace || opt_jul || opt_log4j || opt_python); |
| 669 | if (opt_userspace) { |
| 670 | ev.loglevel = -1; |
| 671 | } else if (opt_jul) { |
| 672 | ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL; |
| 673 | } else if (opt_log4j) { |
| 674 | ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL; |
| 675 | } else if (opt_python) { |
| 676 | ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (opt_exclude) { |
| 682 | ret = check_exclusion_subsets("*", opt_exclude, |
| 683 | &exclusion_count, &exclusion_list); |
| 684 | if (ret == CMD_ERROR) { |
| 685 | goto error; |
| 686 | } |
| 687 | ev.exclusion = 1; |
| 688 | |
| 689 | warn_on_truncated_exclusion_names(exclusion_list, |
| 690 | exclusion_count, &warn); |
| 691 | } |
| 692 | if (!opt_filter) { |
| 693 | ret = lttng_enable_event_with_exclusions(handle, |
| 694 | &ev, channel_name, |
| 695 | NULL, |
| 696 | exclusion_count, exclusion_list); |
| 697 | if (ret < 0) { |
| 698 | switch (-ret) { |
| 699 | case LTTNG_ERR_KERN_EVENT_EXIST: |
| 700 | WARN("Kernel events already enabled (channel %s, session %s)", |
| 701 | print_channel_name(channel_name), session_name); |
| 702 | warn = 1; |
| 703 | break; |
| 704 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
| 705 | { |
| 706 | const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once."; |
| 707 | ERR("Events: %s (channel %s, session %s)", |
| 708 | msg, |
| 709 | print_channel_name(channel_name), |
| 710 | session_name); |
| 711 | error = 1; |
| 712 | break; |
| 713 | } |
| 714 | default: |
| 715 | ERR("Events: %s (channel %s, session %s)", |
| 716 | lttng_strerror(ret), |
| 717 | ret == -LTTNG_ERR_NEED_CHANNEL_NAME |
| 718 | ? print_raw_channel_name(channel_name) |
| 719 | : print_channel_name(channel_name), |
| 720 | session_name); |
| 721 | error = 1; |
| 722 | break; |
| 723 | } |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | switch (opt_event_type) { |
| 728 | case LTTNG_EVENT_TRACEPOINT: |
| 729 | if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) { |
| 730 | char *exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 731 | |
| 732 | if (!exclusion_string) { |
| 733 | PERROR("Cannot allocate exclusion_string"); |
| 734 | error = 1; |
| 735 | goto end; |
| 736 | } |
| 737 | MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s", |
| 738 | get_domain_str(dom.type), |
| 739 | exclusion_string, |
| 740 | print_channel_name(channel_name), |
| 741 | opt_loglevel); |
| 742 | free(exclusion_string); |
| 743 | } else { |
| 744 | char *exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 745 | |
| 746 | if (!exclusion_string) { |
| 747 | PERROR("Cannot allocate exclusion_string"); |
| 748 | error = 1; |
| 749 | goto end; |
| 750 | } |
| 751 | MSG("All %s tracepoints%s are enabled in channel %s", |
| 752 | get_domain_str(dom.type), |
| 753 | exclusion_string, |
| 754 | print_channel_name(channel_name)); |
| 755 | free(exclusion_string); |
| 756 | } |
| 757 | break; |
| 758 | case LTTNG_EVENT_SYSCALL: |
| 759 | if (opt_kernel) { |
| 760 | MSG("All %s system calls are enabled in channel %s", |
| 761 | get_domain_str(dom.type), |
| 762 | print_channel_name(channel_name)); |
| 763 | } |
| 764 | break; |
| 765 | case LTTNG_EVENT_ALL: |
| 766 | if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) { |
| 767 | char *exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 768 | |
| 769 | if (!exclusion_string) { |
| 770 | PERROR("Cannot allocate exclusion_string"); |
| 771 | error = 1; |
| 772 | goto end; |
| 773 | } |
| 774 | MSG("All %s events%s are enabled in channel %s for loglevel %s", |
| 775 | get_domain_str(dom.type), |
| 776 | exclusion_string, |
| 777 | print_channel_name(channel_name), |
| 778 | opt_loglevel); |
| 779 | free(exclusion_string); |
| 780 | } else { |
| 781 | char *exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 782 | |
| 783 | if (!exclusion_string) { |
| 784 | PERROR("Cannot allocate exclusion_string"); |
| 785 | error = 1; |
| 786 | goto end; |
| 787 | } |
| 788 | MSG("All %s events%s are enabled in channel %s", |
| 789 | get_domain_str(dom.type), |
| 790 | exclusion_string, |
| 791 | print_channel_name(channel_name)); |
| 792 | free(exclusion_string); |
| 793 | } |
| 794 | break; |
| 795 | default: |
| 796 | /* |
| 797 | * We should not be here since lttng_enable_event should have |
| 798 | * failed on the event type. |
| 799 | */ |
| 800 | goto error; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | if (opt_filter) { |
| 805 | command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, |
| 806 | opt_filter, exclusion_count, exclusion_list); |
| 807 | if (command_ret < 0) { |
| 808 | switch (-command_ret) { |
| 809 | case LTTNG_ERR_FILTER_EXIST: |
| 810 | WARN("Filter on all events is already enabled" |
| 811 | " (channel %s, session %s)", |
| 812 | print_channel_name(channel_name), session_name); |
| 813 | warn = 1; |
| 814 | break; |
| 815 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
| 816 | { |
| 817 | const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once."; |
| 818 | ERR("All events: %s (channel %s, session %s, filter \'%s\')", |
| 819 | msg, |
| 820 | print_channel_name(channel_name), |
| 821 | session_name, opt_filter); |
| 822 | error = 1; |
| 823 | break; |
| 824 | } |
| 825 | default: |
| 826 | ERR("All events: %s (channel %s, session %s, filter \'%s\')", |
| 827 | lttng_strerror(command_ret), |
| 828 | command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME |
| 829 | ? print_raw_channel_name(channel_name) |
| 830 | : print_channel_name(channel_name), |
| 831 | session_name, opt_filter); |
| 832 | error = 1; |
| 833 | break; |
| 834 | } |
| 835 | error_holder = command_ret; |
| 836 | } else { |
| 837 | ev.filter = 1; |
| 838 | MSG("Filter '%s' successfully set", opt_filter); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (lttng_opt_mi) { |
| 843 | /* The wildcard * is used for kernel and ust domain to |
| 844 | * represent ALL. We copy * in event name to force the wildcard use |
| 845 | * for kernel domain |
| 846 | * |
| 847 | * Note: this is strictly for semantic and printing while in |
| 848 | * machine interface mode. |
| 849 | */ |
| 850 | strcpy(ev.name, "*"); |
| 851 | |
| 852 | /* If we reach here the events are enabled */ |
| 853 | if (!error && !warn) { |
| 854 | ev.enabled = 1; |
| 855 | } else { |
| 856 | ev.enabled = 0; |
| 857 | success = 0; |
| 858 | } |
| 859 | ret = mi_lttng_event(writer, &ev, 1, handle->domain.type); |
| 860 | if (ret) { |
| 861 | ret = CMD_ERROR; |
| 862 | goto error; |
| 863 | } |
| 864 | |
| 865 | /* print exclusion */ |
| 866 | ret = mi_print_exclusion(exclusion_count, exclusion_list); |
| 867 | if (ret) { |
| 868 | ret = CMD_ERROR; |
| 869 | goto error; |
| 870 | } |
| 871 | |
| 872 | /* Success ? */ |
| 873 | ret = mi_lttng_writer_write_element_bool(writer, |
| 874 | mi_lttng_element_command_success, success); |
| 875 | if (ret) { |
| 876 | ret = CMD_ERROR; |
| 877 | goto error; |
| 878 | } |
| 879 | |
| 880 | /* Close event element */ |
| 881 | ret = mi_lttng_writer_close_element(writer); |
| 882 | if (ret) { |
| 883 | ret = CMD_ERROR; |
| 884 | goto error; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | goto end; |
| 889 | } |
| 890 | |
| 891 | /* Strip event list */ |
| 892 | event_name = strtok(opt_event_list, ","); |
| 893 | while (event_name != NULL) { |
| 894 | /* Copy name and type of the event */ |
| 895 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 896 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 897 | ev.type = opt_event_type; |
| 898 | |
| 899 | /* Kernel tracer action */ |
| 900 | if (opt_kernel) { |
| 901 | DBG("Enabling kernel event %s for channel %s", |
| 902 | event_name, |
| 903 | print_channel_name(channel_name)); |
| 904 | |
| 905 | switch (opt_event_type) { |
| 906 | case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */ |
| 907 | /* If event name differs from *, select tracepoint. */ |
| 908 | if (strcmp(ev.name, "*")) { |
| 909 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 910 | } |
| 911 | break; |
| 912 | case LTTNG_EVENT_TRACEPOINT: |
| 913 | break; |
| 914 | case LTTNG_EVENT_PROBE: |
| 915 | ret = parse_probe_opts(&ev, opt_probe); |
| 916 | if (ret) { |
| 917 | ERR("Unable to parse probe options"); |
| 918 | ret = 0; |
| 919 | goto error; |
| 920 | } |
| 921 | break; |
| 922 | case LTTNG_EVENT_FUNCTION: |
| 923 | ret = parse_probe_opts(&ev, opt_function); |
| 924 | if (ret) { |
| 925 | ERR("Unable to parse function probe options"); |
| 926 | ret = 0; |
| 927 | goto error; |
| 928 | } |
| 929 | break; |
| 930 | case LTTNG_EVENT_SYSCALL: |
| 931 | ev.type = LTTNG_EVENT_SYSCALL; |
| 932 | break; |
| 933 | default: |
| 934 | ret = CMD_UNDEFINED; |
| 935 | goto error; |
| 936 | } |
| 937 | |
| 938 | /* kernel loglevels not implemented */ |
| 939 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 940 | } else if (opt_userspace) { /* User-space tracer action */ |
| 941 | DBG("Enabling UST event %s for channel %s, loglevel %s", event_name, |
| 942 | print_channel_name(channel_name), opt_loglevel ? : "<all>"); |
| 943 | |
| 944 | switch (opt_event_type) { |
| 945 | case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */ |
| 946 | /* Fall-through */ |
| 947 | case LTTNG_EVENT_TRACEPOINT: |
| 948 | /* Copy name and type of the event */ |
| 949 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 950 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 951 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 952 | break; |
| 953 | case LTTNG_EVENT_PROBE: |
| 954 | case LTTNG_EVENT_FUNCTION: |
| 955 | case LTTNG_EVENT_SYSCALL: |
| 956 | default: |
| 957 | ERR("Event type not available for user-space tracing"); |
| 958 | ret = CMD_UNSUPPORTED; |
| 959 | goto error; |
| 960 | } |
| 961 | |
| 962 | if (opt_exclude) { |
| 963 | ev.exclusion = 1; |
| 964 | if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) { |
| 965 | ERR("Exclusion option can only be used with tracepoint events"); |
| 966 | ret = CMD_ERROR; |
| 967 | goto error; |
| 968 | } |
| 969 | /* Free previously allocated items */ |
| 970 | if (exclusion_list != NULL) { |
| 971 | while (exclusion_count--) { |
| 972 | free(exclusion_list[exclusion_count]); |
| 973 | } |
| 974 | free(exclusion_list); |
| 975 | exclusion_list = NULL; |
| 976 | } |
| 977 | /* Check for proper subsets */ |
| 978 | ret = check_exclusion_subsets(event_name, opt_exclude, |
| 979 | &exclusion_count, &exclusion_list); |
| 980 | if (ret == CMD_ERROR) { |
| 981 | goto error; |
| 982 | } |
| 983 | |
| 984 | warn_on_truncated_exclusion_names( |
| 985 | exclusion_list, exclusion_count, &warn); |
| 986 | } |
| 987 | |
| 988 | ev.loglevel_type = opt_loglevel_type; |
| 989 | if (opt_loglevel) { |
| 990 | ev.loglevel = loglevel_str_to_value(opt_loglevel); |
| 991 | if (ev.loglevel == -1) { |
| 992 | ERR("Unknown loglevel %s", opt_loglevel); |
| 993 | ret = -LTTNG_ERR_INVALID; |
| 994 | goto error; |
| 995 | } |
| 996 | } else { |
| 997 | ev.loglevel = -1; |
| 998 | } |
| 999 | } else if (opt_jul || opt_log4j || opt_python) { |
| 1000 | if (opt_event_type != LTTNG_EVENT_ALL && |
| 1001 | opt_event_type != LTTNG_EVENT_TRACEPOINT) { |
| 1002 | ERR("Event type not supported for domain."); |
| 1003 | ret = CMD_UNSUPPORTED; |
| 1004 | goto error; |
| 1005 | } |
| 1006 | |
| 1007 | ev.loglevel_type = opt_loglevel_type; |
| 1008 | if (opt_loglevel) { |
| 1009 | if (opt_jul) { |
| 1010 | ev.loglevel = loglevel_jul_str_to_value(opt_loglevel); |
| 1011 | } else if (opt_log4j) { |
| 1012 | ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel); |
| 1013 | } else if (opt_python) { |
| 1014 | ev.loglevel = loglevel_python_str_to_value(opt_loglevel); |
| 1015 | } |
| 1016 | if (ev.loglevel == -1) { |
| 1017 | ERR("Unknown loglevel %s", opt_loglevel); |
| 1018 | ret = -LTTNG_ERR_INVALID; |
| 1019 | goto error; |
| 1020 | } |
| 1021 | } else { |
| 1022 | if (opt_jul) { |
| 1023 | ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL; |
| 1024 | } else if (opt_log4j) { |
| 1025 | ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL; |
| 1026 | } else if (opt_python) { |
| 1027 | ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG; |
| 1028 | } |
| 1029 | } |
| 1030 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 1031 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 1032 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 1033 | } else { |
| 1034 | assert(0); |
| 1035 | } |
| 1036 | |
| 1037 | if (!opt_filter) { |
| 1038 | char *exclusion_string; |
| 1039 | |
| 1040 | command_ret = lttng_enable_event_with_exclusions(handle, |
| 1041 | &ev, channel_name, |
| 1042 | NULL, exclusion_count, exclusion_list); |
| 1043 | exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 1044 | if (!exclusion_string) { |
| 1045 | PERROR("Cannot allocate exclusion_string"); |
| 1046 | error = 1; |
| 1047 | goto end; |
| 1048 | } |
| 1049 | if (command_ret < 0) { |
| 1050 | /* Turn ret to positive value to handle the positive error code */ |
| 1051 | switch (-command_ret) { |
| 1052 | case LTTNG_ERR_KERN_EVENT_EXIST: |
| 1053 | WARN("Kernel event %s%s already enabled (channel %s, session %s)", |
| 1054 | event_name, |
| 1055 | exclusion_string, |
| 1056 | print_channel_name(channel_name), session_name); |
| 1057 | warn = 1; |
| 1058 | break; |
| 1059 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
| 1060 | { |
| 1061 | const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once."; |
| 1062 | ERR("Event %s%s: %s (channel %s, session %s)", event_name, |
| 1063 | exclusion_string, |
| 1064 | msg, |
| 1065 | print_channel_name(channel_name), |
| 1066 | session_name); |
| 1067 | error = 1; |
| 1068 | break; |
| 1069 | } |
| 1070 | default: |
| 1071 | ERR("Event %s%s: %s (channel %s, session %s)", event_name, |
| 1072 | exclusion_string, |
| 1073 | lttng_strerror(command_ret), |
| 1074 | command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME |
| 1075 | ? print_raw_channel_name(channel_name) |
| 1076 | : print_channel_name(channel_name), |
| 1077 | session_name); |
| 1078 | error = 1; |
| 1079 | break; |
| 1080 | } |
| 1081 | error_holder = command_ret; |
| 1082 | } else { |
| 1083 | switch (dom.type) { |
| 1084 | case LTTNG_DOMAIN_KERNEL: |
| 1085 | case LTTNG_DOMAIN_UST: |
| 1086 | MSG("%s event %s%s created in channel %s", |
| 1087 | get_domain_str(dom.type), |
| 1088 | event_name, |
| 1089 | exclusion_string, |
| 1090 | print_channel_name(channel_name)); |
| 1091 | break; |
| 1092 | case LTTNG_DOMAIN_JUL: |
| 1093 | case LTTNG_DOMAIN_LOG4J: |
| 1094 | case LTTNG_DOMAIN_PYTHON: |
| 1095 | /* |
| 1096 | * Don't print the default channel |
| 1097 | * name for agent domains. |
| 1098 | */ |
| 1099 | MSG("%s event %s%s enabled", |
| 1100 | get_domain_str(dom.type), |
| 1101 | event_name, |
| 1102 | exclusion_string); |
| 1103 | break; |
| 1104 | default: |
| 1105 | assert(0); |
| 1106 | } |
| 1107 | } |
| 1108 | free(exclusion_string); |
| 1109 | } |
| 1110 | |
| 1111 | if (opt_filter) { |
| 1112 | char *exclusion_string; |
| 1113 | |
| 1114 | /* Filter present */ |
| 1115 | ev.filter = 1; |
| 1116 | |
| 1117 | command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, |
| 1118 | opt_filter, exclusion_count, exclusion_list); |
| 1119 | exclusion_string = print_exclusions(exclusion_count, exclusion_list); |
| 1120 | if (!exclusion_string) { |
| 1121 | PERROR("Cannot allocate exclusion_string"); |
| 1122 | error = 1; |
| 1123 | goto end; |
| 1124 | } |
| 1125 | if (command_ret < 0) { |
| 1126 | switch (-command_ret) { |
| 1127 | case LTTNG_ERR_FILTER_EXIST: |
| 1128 | WARN("Filter on event %s%s is already enabled" |
| 1129 | " (channel %s, session %s)", |
| 1130 | event_name, |
| 1131 | exclusion_string, |
| 1132 | print_channel_name(channel_name), session_name); |
| 1133 | warn = 1; |
| 1134 | break; |
| 1135 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
| 1136 | { |
| 1137 | const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once."; |
| 1138 | ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name, |
| 1139 | exclusion_string, |
| 1140 | msg, |
| 1141 | print_channel_name(channel_name), |
| 1142 | session_name, opt_filter); |
| 1143 | error = 1; |
| 1144 | break; |
| 1145 | } |
| 1146 | default: |
| 1147 | ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name, |
| 1148 | exclusion_string, |
| 1149 | lttng_strerror(command_ret), |
| 1150 | command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME |
| 1151 | ? print_raw_channel_name(channel_name) |
| 1152 | : print_channel_name(channel_name), |
| 1153 | session_name, opt_filter); |
| 1154 | error = 1; |
| 1155 | break; |
| 1156 | } |
| 1157 | error_holder = command_ret; |
| 1158 | |
| 1159 | } else { |
| 1160 | MSG("Event %s%s: Filter '%s' successfully set", |
| 1161 | event_name, exclusion_string, |
| 1162 | opt_filter); |
| 1163 | } |
| 1164 | free(exclusion_string); |
| 1165 | } |
| 1166 | |
| 1167 | if (lttng_opt_mi) { |
| 1168 | if (command_ret) { |
| 1169 | success = 0; |
| 1170 | ev.enabled = 0; |
| 1171 | } else { |
| 1172 | ev.enabled = 1; |
| 1173 | } |
| 1174 | |
| 1175 | ret = mi_lttng_event(writer, &ev, 1, handle->domain.type); |
| 1176 | if (ret) { |
| 1177 | ret = CMD_ERROR; |
| 1178 | goto error; |
| 1179 | } |
| 1180 | |
| 1181 | /* print exclusion */ |
| 1182 | ret = mi_print_exclusion(exclusion_count, exclusion_list); |
| 1183 | if (ret) { |
| 1184 | ret = CMD_ERROR; |
| 1185 | goto error; |
| 1186 | } |
| 1187 | |
| 1188 | /* Success ? */ |
| 1189 | ret = mi_lttng_writer_write_element_bool(writer, |
| 1190 | mi_lttng_element_command_success, success); |
| 1191 | if (ret) { |
| 1192 | ret = CMD_ERROR; |
| 1193 | goto end; |
| 1194 | } |
| 1195 | |
| 1196 | /* Close event element */ |
| 1197 | ret = mi_lttng_writer_close_element(writer); |
| 1198 | if (ret) { |
| 1199 | ret = CMD_ERROR; |
| 1200 | goto end; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* Next event */ |
| 1205 | event_name = strtok(NULL, ","); |
| 1206 | /* Reset warn, error and success */ |
| 1207 | success = 1; |
| 1208 | } |
| 1209 | |
| 1210 | end: |
| 1211 | /* Close Mi */ |
| 1212 | if (lttng_opt_mi) { |
| 1213 | /* Close events element */ |
| 1214 | ret = mi_lttng_writer_close_element(writer); |
| 1215 | if (ret) { |
| 1216 | ret = CMD_ERROR; |
| 1217 | goto error; |
| 1218 | } |
| 1219 | } |
| 1220 | error: |
| 1221 | if (warn) { |
| 1222 | ret = CMD_WARNING; |
| 1223 | } |
| 1224 | if (error) { |
| 1225 | ret = CMD_ERROR; |
| 1226 | } |
| 1227 | lttng_destroy_handle(handle); |
| 1228 | |
| 1229 | if (exclusion_list != NULL) { |
| 1230 | while (exclusion_count--) { |
| 1231 | free(exclusion_list[exclusion_count]); |
| 1232 | } |
| 1233 | free(exclusion_list); |
| 1234 | } |
| 1235 | |
| 1236 | /* Overwrite ret with error_holder if there was an actual error with |
| 1237 | * enabling an event. |
| 1238 | */ |
| 1239 | ret = error_holder ? error_holder : ret; |
| 1240 | |
| 1241 | return ret; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Add event to trace session |
| 1246 | */ |
| 1247 | int cmd_enable_events(int argc, const char **argv) |
| 1248 | { |
| 1249 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 1250 | static poptContext pc; |
| 1251 | char *session_name = NULL; |
| 1252 | int event_type = -1; |
| 1253 | |
| 1254 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 1255 | poptReadDefaultConfig(pc, 0); |
| 1256 | |
| 1257 | /* Default event type */ |
| 1258 | opt_event_type = LTTNG_EVENT_ALL; |
| 1259 | |
| 1260 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 1261 | switch (opt) { |
| 1262 | case OPT_HELP: |
| 1263 | SHOW_HELP(); |
| 1264 | goto end; |
| 1265 | case OPT_TRACEPOINT: |
| 1266 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
| 1267 | break; |
| 1268 | case OPT_PROBE: |
| 1269 | opt_event_type = LTTNG_EVENT_PROBE; |
| 1270 | break; |
| 1271 | case OPT_FUNCTION: |
| 1272 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 1273 | break; |
| 1274 | case OPT_SYSCALL: |
| 1275 | opt_event_type = LTTNG_EVENT_SYSCALL; |
| 1276 | break; |
| 1277 | case OPT_USERSPACE: |
| 1278 | opt_userspace = 1; |
| 1279 | break; |
| 1280 | case OPT_LOGLEVEL: |
| 1281 | opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 1282 | opt_loglevel = poptGetOptArg(pc); |
| 1283 | break; |
| 1284 | case OPT_LOGLEVEL_ONLY: |
| 1285 | opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 1286 | opt_loglevel = poptGetOptArg(pc); |
| 1287 | break; |
| 1288 | case OPT_LIST_OPTIONS: |
| 1289 | list_cmd_options(stdout, long_options); |
| 1290 | goto end; |
| 1291 | case OPT_FILTER: |
| 1292 | break; |
| 1293 | case OPT_EXCLUDE: |
| 1294 | break; |
| 1295 | default: |
| 1296 | ret = CMD_UNDEFINED; |
| 1297 | goto end; |
| 1298 | } |
| 1299 | |
| 1300 | /* Validate event type. Multiple event type are not supported. */ |
| 1301 | if (event_type == -1) { |
| 1302 | event_type = opt_event_type; |
| 1303 | } else { |
| 1304 | if (event_type != opt_event_type) { |
| 1305 | ERR("Multiple event type not supported."); |
| 1306 | ret = CMD_ERROR; |
| 1307 | goto end; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | ret = print_missing_or_multiple_domains( |
| 1313 | opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python); |
| 1314 | if (ret) { |
| 1315 | ret = CMD_ERROR; |
| 1316 | goto end; |
| 1317 | } |
| 1318 | |
| 1319 | /* Mi check */ |
| 1320 | if (lttng_opt_mi) { |
| 1321 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 1322 | if (!writer) { |
| 1323 | ret = -LTTNG_ERR_NOMEM; |
| 1324 | goto end; |
| 1325 | } |
| 1326 | |
| 1327 | /* Open command element */ |
| 1328 | ret = mi_lttng_writer_command_open(writer, |
| 1329 | mi_lttng_element_command_enable_event); |
| 1330 | if (ret) { |
| 1331 | ret = CMD_ERROR; |
| 1332 | goto end; |
| 1333 | } |
| 1334 | |
| 1335 | /* Open output element */ |
| 1336 | ret = mi_lttng_writer_open_element(writer, |
| 1337 | mi_lttng_element_command_output); |
| 1338 | if (ret) { |
| 1339 | ret = CMD_ERROR; |
| 1340 | goto end; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | opt_event_list = (char*) poptGetArg(pc); |
| 1345 | if (opt_event_list == NULL && opt_enable_all == 0) { |
| 1346 | ERR("Missing event name(s).\n"); |
| 1347 | ret = CMD_ERROR; |
| 1348 | goto end; |
| 1349 | } |
| 1350 | |
| 1351 | if (!opt_session_name) { |
| 1352 | session_name = get_session_name(); |
| 1353 | if (session_name == NULL) { |
| 1354 | command_ret = CMD_ERROR; |
| 1355 | success = 0; |
| 1356 | goto mi_closing; |
| 1357 | } |
| 1358 | } else { |
| 1359 | session_name = opt_session_name; |
| 1360 | } |
| 1361 | |
| 1362 | command_ret = enable_events(session_name); |
| 1363 | if (command_ret) { |
| 1364 | success = 0; |
| 1365 | goto mi_closing; |
| 1366 | } |
| 1367 | |
| 1368 | mi_closing: |
| 1369 | /* Mi closing */ |
| 1370 | if (lttng_opt_mi) { |
| 1371 | /* Close output element */ |
| 1372 | ret = mi_lttng_writer_close_element(writer); |
| 1373 | if (ret) { |
| 1374 | ret = CMD_ERROR; |
| 1375 | goto end; |
| 1376 | } |
| 1377 | |
| 1378 | ret = mi_lttng_writer_write_element_bool(writer, |
| 1379 | mi_lttng_element_command_success, success); |
| 1380 | if (ret) { |
| 1381 | ret = CMD_ERROR; |
| 1382 | goto end; |
| 1383 | } |
| 1384 | |
| 1385 | /* Command element close */ |
| 1386 | ret = mi_lttng_writer_command_close(writer); |
| 1387 | if (ret) { |
| 1388 | ret = CMD_ERROR; |
| 1389 | goto end; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | end: |
| 1394 | /* Mi clean-up */ |
| 1395 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 1396 | /* Preserve original error code */ |
| 1397 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; |
| 1398 | } |
| 1399 | |
| 1400 | if (opt_session_name == NULL) { |
| 1401 | free(session_name); |
| 1402 | } |
| 1403 | |
| 1404 | /* Overwrite ret if an error occurred in enable_events */ |
| 1405 | ret = command_ret ? command_ret : ret; |
| 1406 | |
| 1407 | poptFreeContext(pc); |
| 1408 | return ret; |
| 1409 | } |