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