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