Fix: lttng-crash: segfault when parsing options
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
16 */
17
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
b2064f54 20#include <assert.h>
f3ed775e
DG
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>
5a0de755 28#include <inttypes.h>
8f0d098b 29#include <ctype.h>
f3ed775e 30
42224349 31#include <src/common/sessiond-comm/sessiond-comm.h>
f3ed775e 32
89476427
JRJ
33/* Mi dependancy */
34#include <common/mi-lttng.h>
35
36#include "../command.h"
37
8ab7c0d9
MD
38#if (LTTNG_SYMBOL_NAME_LEN == 256)
39#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
40#endif
41
f3ed775e
DG
42static char *opt_event_list;
43static int opt_event_type;
0cda4f28
MD
44static const char *opt_loglevel;
45static int opt_loglevel_type;
6181537c 46static int opt_kernel;
5440dc42 47static char *opt_session_name;
f3ed775e 48static int opt_userspace;
b9dfb167 49static int opt_jul;
5cdb6027 50static int opt_log4j;
0e115563 51static int opt_python;
f3ed775e 52static int opt_enable_all;
cf0e5467 53static char *opt_probe;
8f0d098b 54static char *opt_function;
f3ed775e 55static char *opt_channel_name;
53a80697 56static char *opt_filter;
fac3366c 57static char *opt_exclude;
f3ed775e
DG
58
59enum {
60 OPT_HELP = 1,
f3ed775e 61 OPT_TRACEPOINT,
cf0e5467 62 OPT_PROBE,
f3ed775e 63 OPT_FUNCTION,
a54bd42d 64 OPT_SYSCALL,
eeac7d46 65 OPT_USERSPACE,
0cda4f28
MD
66 OPT_LOGLEVEL,
67 OPT_LOGLEVEL_ONLY,
679b4943 68 OPT_LIST_OPTIONS,
53a80697 69 OPT_FILTER,
fac3366c 70 OPT_EXCLUDE,
f3ed775e
DG
71};
72
cd80958d 73static struct lttng_handle *handle;
89476427 74static struct mi_writer *writer;
cd80958d 75
f3ed775e
DG
76static struct poptOption long_options[] = {
77 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
78 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 79 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 80 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
81 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
82 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 83 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
b9dfb167 84 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 85 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 86 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
f3ed775e 87 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 88 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 89 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
7ebae521 90 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
91 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
92 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 93 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 94 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
fac3366c 95 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
f3ed775e
DG
96 {0, 0, 0, 0, 0, 0, 0}
97};
98
99/*
100 * usage
101 */
102static void usage(FILE *ofp)
103{
313f1ac7 104 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] (-k | -u | -j | -l | -p) [OPTIONS] \n");
f3ed775e 105 fprintf(ofp, "\n");
32a6298d 106 fprintf(ofp, "Options:\n");
f3ed775e 107 fprintf(ofp, " -h, --help Show this help\n");
679b4943 108 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d
DG
109 fprintf(ofp, " -s, --session NAME Apply to session name\n");
110 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
e08bff8d 111 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
f3ed775e 112 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
27221701 113 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
b9dfb167 114 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
5cdb6027 115 fprintf(ofp, " -l, --log4j Apply for Java application using LOG4j\n");
23171d8c 116 fprintf(ofp, " -p, --python Apply for Python application\n");
f3ed775e
DG
117 fprintf(ofp, "\n");
118 fprintf(ofp, "Event options:\n");
119 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
be49af1e
MD
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");
6a240cd9 125 fprintf(ofp, " --probe (addr | symbol | symbol+offset)\n");
cf0e5467
MD
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");
6a240cd9 129 fprintf(ofp, " --function (addr | symbol | symbol+offset)\n");
8f0d098b
MD
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");
a54bd42d 133 fprintf(ofp, " --syscall System call event\n");
f3ed775e 134 fprintf(ofp, "\n");
300b8fd5 135 fprintf(ofp, " --loglevel name\n");
f9e8873b 136 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel.\n");
372a6d2a 137 fprintf(ofp, " For JUL/LOG4j/Python domains, see the table below for the range values.\n");
300b8fd5
MD
138 fprintf(ofp, " --loglevel-only name\n");
139 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
140 fprintf(ofp, "\n");
0c8477c8
MD
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");
46839cc2
MD
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");
b2064f54
DG
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");
5cdb6027
DG
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");
0e115563
DG
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");
f2611ab0 195 fprintf(ofp, " -f, --filter \'expression\'\n");
ee8ccafa
MD
196 fprintf(ofp, " Filter expression on event fields and context.\n");
197 fprintf(ofp, " Event recording depends on evaluation.\n");
919e300c
MD
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");
9bd578f5
MD
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");
6a240cd9 210 fprintf(ofp, " '(strfield == \"test\" || intfield != 10) && intfield > 33'\n");
9bd578f5
MD
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");
ee8ccafa
MD
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");
c2063d0b
JI
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");
300b8fd5 237 fprintf(ofp, "\n");
f3ed775e
DG
238}
239
0d63dd19 240/*
6181537c 241 * Parse probe options.
0d63dd19 242 */
cf0e5467 243static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19 244{
49d4e302
JRJ
245 int ret = CMD_SUCCESS;
246 int match;
8ff0bbd0 247 char s_hex[19];
8ab7c0d9 248#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
0d63dd19
DG
249 char name[LTTNG_SYMBOL_NAME_LEN];
250
251 if (opt == NULL) {
49d4e302 252 ret = CMD_ERROR;
8f0d098b 253 goto end;
0d63dd19
DG
254 }
255
256 /* Check for symbol+offset */
49d4e302 257 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
8ab7c0d9 258 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
49d4e302 259 if (match == 2) {
7d29a247 260 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 261 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 262 DBG("probe symbol %s", ev->attr.probe.symbol_name);
9d035200 263 if (*s_hex == '\0') {
8ff0bbd0 264 ERR("Invalid probe offset %s", s_hex);
49d4e302 265 ret = CMD_ERROR;
8f0d098b 266 goto end;
0d63dd19 267 }
8ff0bbd0 268 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 269 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 270 ev->attr.probe.addr = 0;
8f0d098b
MD
271 goto end;
272 }
273
274 /* Check for symbol */
275 if (isalpha(name[0])) {
49d4e302 276 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
8ab7c0d9 277 name);
49d4e302 278 if (match == 1) {
8f0d098b 279 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 280 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
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 }
0d63dd19
DG
287 }
288
289 /* Check for address */
49d4e302
JRJ
290 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
291 if (match > 0) {
9d035200 292 if (*s_hex == '\0') {
8ff0bbd0 293 ERR("Invalid probe address %s", s_hex);
49d4e302 294 ret = CMD_ERROR;
8f0d098b 295 goto end;
0d63dd19 296 }
8ff0bbd0 297 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 298 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
299 ev->attr.probe.offset = 0;
300 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 301 goto end;
0d63dd19
DG
302 }
303
304 /* No match */
49d4e302 305 ret = CMD_ERROR;
0d63dd19 306
8f0d098b 307end:
0d63dd19
DG
308 return ret;
309}
310
5cdb6027
DG
311/*
312 * Maps LOG4j loglevel from string to value
313 */
314static int loglevel_log4j_str_to_value(const char *inputstr)
315{
316 int i = 0;
317 char str[LTTNG_SYMBOL_NAME_LEN];
318
3712b110
JG
319 if (!inputstr || strlen(inputstr) == 0) {
320 return -1;
321 }
322
5cdb6027
DG
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
b2064f54
DG
354/*
355 * Maps JUL loglevel from string to value
356 */
357static int loglevel_jul_str_to_value(const char *inputstr)
358{
359 int i = 0;
360 char str[LTTNG_SYMBOL_NAME_LEN];
361
3712b110
JG
362 if (!inputstr || strlen(inputstr) == 0) {
363 return -1;
364 }
365
b2064f54
DG
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
0e115563
DG
399/*
400 * Maps Python loglevel from string to value
401 */
402static int loglevel_python_str_to_value(const char *inputstr)
403{
404 int i = 0;
405 char str[LTTNG_SYMBOL_NAME_LEN];
406
3712b110
JG
407 if (!inputstr || strlen(inputstr) == 0) {
408 return -1;
409 }
410
0e115563
DG
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
8005f29a 438/*
0c8477c8 439 * Maps loglevel from string to value
8005f29a
MD
440 */
441static
46839cc2 442int loglevel_str_to_value(const char *inputstr)
8005f29a 443{
46839cc2
MD
444 int i = 0;
445 char str[LTTNG_SYMBOL_NAME_LEN];
446
3712b110
JG
447 if (!inputstr || strlen(inputstr) == 0) {
448 return -1;
449 }
450
e051e07c
DG
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') {
46839cc2
MD
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;
8005f29a
MD
490 } else {
491 return -1;
492 }
493}
494
85076754
MD
495static
496const char *print_channel_name(const char *name)
497{
498 return name ? : DEFAULT_CHANNEL_NAME;
499}
500
501static
502const char *print_raw_channel_name(const char *name)
503{
504 return name ? : "<default>";
505}
506
89476427
JRJ
507/*
508 * Mi print exlcusion list
509 */
510static
511int 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
537end:
538 return ret;
539}
540
9c48cab3
JI
541/*
542 * Return allocated string for pretty-printing exclusion names.
543 */
544static
545char *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);
fd591b18
MD
563 ret = zmalloc(length);
564 if (!ret) {
565 return NULL;
566 }
9c48cab3
JI
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 }
89476427 574
9c48cab3
JI
575 return ret;
576}
577
748bde76
JI
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 */
583static
584int 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 == '*') {
5ef79758
MD
625 char *string;
626 char **new_exclusion_list;
627
748bde76 628 /* Excluder is a proper subset of event */
5ef79758
MD
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,
1940b29e 635 sizeof(char *) * (exclusion_count + 1));
5ef79758
MD
636 if (!new_exclusion_list) {
637 PERROR("realloc");
638 free(string);
639 goto error;
640 }
641 exclusion_list = new_exclusion_list;
748bde76 642 exclusion_count++;
5ef79758 643 exclusion_list[exclusion_count - 1] = string;
748bde76
JI
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;
665error:
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;
675end:
676 *exclusion_count_ptr = exclusion_count;
677 *exclusion_list_ptr = exclusion_list;
678 return ret;
679}
f3ed775e 680/*
6181537c 681 * Enabling event using the lttng API.
89476427 682 * Note: in case of error only the last error code will be return.
f3ed775e 683 */
cd80958d 684static int enable_events(char *session_name)
f3ed775e 685{
89476427
JRJ
686 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
687 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
b73d0b29 688 char *event_name, *channel_name = NULL;
f3ed775e 689 struct lttng_event ev;
7d29a247 690 struct lttng_domain dom;
7ed70bc9
JI
691 int exclusion_count = 0;
692 char **exclusion_list = NULL;
f3ed775e 693
441c16a7
MD
694 memset(&ev, 0, sizeof(ev));
695 memset(&dom, 0, sizeof(dom));
696
53a80697 697 if (opt_kernel) {
67b58630
JG
698 if (opt_loglevel) {
699 WARN("Kernel loglevels are not supported.");
700 }
53a80697
MD
701 }
702
7d29a247
DG
703 /* Create lttng domain */
704 if (opt_kernel) {
705 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 706 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 707 } else if (opt_userspace) {
2bdd86d4 708 dom.type = LTTNG_DOMAIN_UST;
7972aab2 709 /* Default. */
8692d4e5 710 dom.buf_type = LTTNG_BUFFER_PER_UID;
b9dfb167
DG
711 } else if (opt_jul) {
712 dom.type = LTTNG_DOMAIN_JUL;
713 /* Default. */
714 dom.buf_type = LTTNG_BUFFER_PER_UID;
5cdb6027
DG
715 } else if (opt_log4j) {
716 dom.type = LTTNG_DOMAIN_LOG4J;
717 /* Default. */
718 dom.buf_type = LTTNG_BUFFER_PER_UID;
0e115563
DG
719 } else if (opt_python) {
720 dom.type = LTTNG_DOMAIN_PYTHON;
721 /* Default. */
722 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 723 } else {
b9dfb167 724 print_missing_domain();
d16c1a4c 725 ret = CMD_ERROR;
6181537c 726 goto error;
2bdd86d4 727 }
7d29a247 728
8b08c525
AB
729 if (opt_exclude) {
730 switch (dom.type) {
731 case LTTNG_DOMAIN_KERNEL:
732 case LTTNG_DOMAIN_JUL:
733 case LTTNG_DOMAIN_LOG4J:
734 case LTTNG_DOMAIN_PYTHON:
735 ERR("Event name exclusions are not yet implemented for %s events",
736 get_domain_str(dom.type));
737 ret = CMD_ERROR;
738 goto error;
739 case LTTNG_DOMAIN_UST:
740 /* Exclusions supported */
741 break;
742 default:
743 assert(0);
744 }
d5dd17fd
JI
745 }
746
85076754 747 channel_name = opt_channel_name;
ae856491 748
cd80958d
DG
749 handle = lttng_create_handle(session_name, &dom);
750 if (handle == NULL) {
751 ret = -1;
752 goto error;
753 }
1aef21b6 754
89476427
JRJ
755 /* Prepare Mi */
756 if (lttng_opt_mi) {
757 /* Open a events element */
758 ret = mi_lttng_writer_open_element(writer, config_element_events);
759 if (ret) {
760 ret = CMD_ERROR;
761 goto error;
762 }
763 }
764
cd80958d 765 if (opt_enable_all) {
8c9ae521 766 /* Default setup for enable all */
75e8c5ab
MD
767 if (opt_kernel) {
768 ev.type = opt_event_type;
29c62722 769 strcpy(ev.name, "*");
300b8fd5
MD
770 /* kernel loglevels not implemented */
771 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
772 } else {
773 ev.type = LTTNG_EVENT_TRACEPOINT;
774 strcpy(ev.name, "*");
300b8fd5
MD
775 ev.loglevel_type = opt_loglevel_type;
776 if (opt_loglevel) {
0e115563 777 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54
DG
778 if (opt_userspace) {
779 ev.loglevel = loglevel_str_to_value(opt_loglevel);
780 } else if (opt_jul) {
781 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027
DG
782 } else if (opt_log4j) {
783 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563
DG
784 } else if (opt_python) {
785 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
b2064f54 786 }
300b8fd5
MD
787 if (ev.loglevel == -1) {
788 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 789 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
790 goto error;
791 }
22e25b71 792 } else {
0e115563 793 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54
DG
794 if (opt_userspace) {
795 ev.loglevel = -1;
34aa3685 796 } else if (opt_jul) {
b2064f54 797 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
34aa3685
JG
798 } else if (opt_log4j) {
799 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563
DG
800 } else if (opt_python) {
801 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
b2064f54 802 }
300b8fd5 803 }
75e8c5ab 804 }
8c9ae521 805
7ed70bc9
JI
806 if (opt_exclude) {
807 ret = check_exclusion_subsets("*", opt_exclude,
808 &exclusion_count, &exclusion_list);
809 if (ret == CMD_ERROR) {
810 goto error;
811 }
89476427 812 ev.exclusion = 1;
7ed70bc9 813 }
025faf73 814 if (!opt_filter) {
7ed70bc9
JI
815 ret = lttng_enable_event_with_exclusions(handle,
816 &ev, channel_name,
817 NULL,
818 exclusion_count, exclusion_list);
025faf73
DG
819 if (ret < 0) {
820 switch (-ret) {
821 case LTTNG_ERR_KERN_EVENT_EXIST:
822 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 823 print_channel_name(channel_name), session_name);
89476427 824 warn = 1;
025faf73 825 break;
45d5d421
CB
826 case LTTNG_ERR_TRACE_ALREADY_STARTED:
827 {
828 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
829 ERR("Events: %s (channel %s, session %s)",
830 msg,
831 print_channel_name(channel_name),
832 session_name);
833 error = 1;
834 break;
835 }
025faf73
DG
836 default:
837 ERR("Events: %s (channel %s, session %s)",
85076754
MD
838 lttng_strerror(ret),
839 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
840 ? print_raw_channel_name(channel_name)
841 : print_channel_name(channel_name),
842 session_name);
89476427 843 error = 1;
025faf73
DG
844 break;
845 }
846 goto end;
42224349 847 }
8c9ae521 848
025faf73
DG
849 switch (opt_event_type) {
850 case LTTNG_EVENT_TRACEPOINT:
67b58630 851 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9c48cab3 852 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
853
854 if (!exclusion_string) {
855 PERROR("Cannot allocate exclusion_string");
856 error = 1;
857 goto end;
858 }
9c48cab3 859 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
b9dfb167 860 get_domain_str(dom.type),
9c48cab3 861 exclusion_string,
85076754 862 print_channel_name(channel_name),
025faf73 863 opt_loglevel);
9c48cab3 864 free(exclusion_string);
025faf73 865 } else {
9c48cab3 866 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
867
868 if (!exclusion_string) {
869 PERROR("Cannot allocate exclusion_string");
870 error = 1;
871 goto end;
872 }
9c48cab3 873 MSG("All %s tracepoints%s are enabled in channel %s",
b9dfb167 874 get_domain_str(dom.type),
9c48cab3 875 exclusion_string,
85076754 876 print_channel_name(channel_name));
9c48cab3 877 free(exclusion_string);
025faf73
DG
878 }
879 break;
880 case LTTNG_EVENT_SYSCALL:
881 if (opt_kernel) {
6e911cad
MD
882 MSG("All %s system calls are enabled in channel %s",
883 get_domain_str(dom.type),
85076754 884 print_channel_name(channel_name));
025faf73
DG
885 }
886 break;
887 case LTTNG_EVENT_ALL:
67b58630 888 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9c48cab3 889 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
890
891 if (!exclusion_string) {
892 PERROR("Cannot allocate exclusion_string");
893 error = 1;
894 goto end;
895 }
9c48cab3 896 MSG("All %s events%s are enabled in channel %s for loglevel %s",
b9dfb167 897 get_domain_str(dom.type),
9c48cab3 898 exclusion_string,
85076754 899 print_channel_name(channel_name),
025faf73 900 opt_loglevel);
9c48cab3 901 free(exclusion_string);
025faf73 902 } else {
9c48cab3 903 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
904
905 if (!exclusion_string) {
906 PERROR("Cannot allocate exclusion_string");
907 error = 1;
908 goto end;
909 }
9c48cab3 910 MSG("All %s events%s are enabled in channel %s",
b9dfb167 911 get_domain_str(dom.type),
9c48cab3 912 exclusion_string,
85076754 913 print_channel_name(channel_name));
9c48cab3 914 free(exclusion_string);
025faf73
DG
915 }
916 break;
917 default:
918 /*
919 * We should not be here since lttng_enable_event should have
920 * failed on the event type.
921 */
922 goto error;
57064ada 923 }
f3ed775e 924 }
89476427 925
025faf73 926 if (opt_filter) {
89476427 927 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
7ed70bc9 928 opt_filter, exclusion_count, exclusion_list);
89476427
JRJ
929 if (command_ret < 0) {
930 switch (-command_ret) {
16363652 931 case LTTNG_ERR_FILTER_EXIST:
85076754 932 WARN("Filter on all events is already enabled"
16363652 933 " (channel %s, session %s)",
85076754 934 print_channel_name(channel_name), session_name);
89476427 935 warn = 1;
16363652 936 break;
45d5d421
CB
937 case LTTNG_ERR_TRACE_ALREADY_STARTED:
938 {
939 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
940 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
941 msg,
942 print_channel_name(channel_name),
943 session_name, opt_filter);
944 error = 1;
945 break;
946 }
16363652 947 default:
85076754 948 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
da3d7d0e
JRJ
949 lttng_strerror(command_ret),
950 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
951 ? print_raw_channel_name(channel_name)
952 : print_channel_name(channel_name),
953 session_name, opt_filter);
89476427 954 error = 1;
16363652
DG
955 break;
956 }
89476427 957 error_holder = command_ret;
16363652 958 } else {
89476427 959 ev.filter = 1;
16363652
DG
960 MSG("Filter '%s' successfully set", opt_filter);
961 }
962 }
89476427
JRJ
963
964 if (lttng_opt_mi) {
965 /* The wildcard * is used for kernel and ust domain to
966 * represent ALL. We copy * in event name to force the wildcard use
967 * for kernel domain
968 *
969 * Note: this is strictly for semantic and printing while in
970 * machine interface mode.
971 */
972 strcpy(ev.name, "*");
973
974 /* If we reach here the events are enabled */
975 if (!error && !warn) {
976 ev.enabled = 1;
977 } else {
978 ev.enabled = 0;
979 success = 0;
980 }
970d848b 981 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
89476427
JRJ
982 if (ret) {
983 ret = CMD_ERROR;
984 goto error;
985 }
986
987 /* print exclusion */
988 ret = mi_print_exclusion(exclusion_count, exclusion_list);
989 if (ret) {
990 ret = CMD_ERROR;
991 goto error;
992 }
993
994 /* Success ? */
995 ret = mi_lttng_writer_write_element_bool(writer,
996 mi_lttng_element_command_success, success);
997 if (ret) {
998 ret = CMD_ERROR;
999 goto error;
1000 }
1001
1002 /* Close event element */
1003 ret = mi_lttng_writer_close_element(writer);
1004 if (ret) {
1005 ret = CMD_ERROR;
1006 goto error;
1007 }
1008 }
1009
8c9ae521 1010 goto end;
f3ed775e
DG
1011 }
1012
1013 /* Strip event list */
1014 event_name = strtok(opt_event_list, ",");
1015 while (event_name != NULL) {
6181537c
DG
1016 /* Copy name and type of the event */
1017 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1018 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1019 ev.type = opt_event_type;
1020
f3ed775e
DG
1021 /* Kernel tracer action */
1022 if (opt_kernel) {
1023 DBG("Enabling kernel event %s for channel %s",
85076754
MD
1024 event_name,
1025 print_channel_name(channel_name));
f3ed775e
DG
1026
1027 switch (opt_event_type) {
29c62722
MD
1028 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
1029 /* If event name differs from *, select tracepoint. */
1030 if (strcmp(ev.name, "*")) {
1031 ev.type = LTTNG_EVENT_TRACEPOINT;
1032 }
1033 break;
e6ddca71 1034 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 1035 break;
7d29a247 1036 case LTTNG_EVENT_PROBE:
cf0e5467 1037 ret = parse_probe_opts(&ev, opt_probe);
49d4e302 1038 if (ret) {
cf0e5467 1039 ERR("Unable to parse probe options");
0d63dd19
DG
1040 ret = 0;
1041 goto error;
1042 }
f3ed775e
DG
1043 break;
1044 case LTTNG_EVENT_FUNCTION:
8f0d098b 1045 ret = parse_probe_opts(&ev, opt_function);
49d4e302 1046 if (ret) {
8f0d098b
MD
1047 ERR("Unable to parse function probe options");
1048 ret = 0;
1049 goto error;
1050 }
1051 break;
a54bd42d 1052 case LTTNG_EVENT_SYSCALL:
c6aa2d41
MD
1053 ev.type = LTTNG_EVENT_SYSCALL;
1054 break;
f3ed775e 1055 default:
1ab1ea0b 1056 ret = CMD_UNDEFINED;
f3ed775e
DG
1057 goto error;
1058 }
0cda4f28 1059
0cda4f28 1060 /* kernel loglevels not implemented */
8005f29a 1061 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 1062 } else if (opt_userspace) { /* User-space tracer action */
300b8fd5 1063 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 1064 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
1065
1066 switch (opt_event_type) {
1067 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
1068 /* Fall-through */
1069 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
1070 /* Copy name and type of the event */
1071 ev.type = LTTNG_EVENT_TRACEPOINT;
1072 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1073 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
1074 break;
1075 case LTTNG_EVENT_PROBE:
1076 case LTTNG_EVENT_FUNCTION:
2bdd86d4
MD
1077 case LTTNG_EVENT_SYSCALL:
1078 default:
cc62c0c0 1079 ERR("Event type not available for user-space tracing");
4ce78777 1080 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
1081 goto error;
1082 }
0cda4f28 1083
7ed70bc9 1084 if (opt_exclude) {
89476427 1085 ev.exclusion = 1;
d5dd17fd
JI
1086 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1087 ERR("Exclusion option can only be used with tracepoint events");
1088 ret = CMD_ERROR;
1089 goto error;
1090 }
7ed70bc9
JI
1091 /* Free previously allocated items */
1092 if (exclusion_list != NULL) {
1093 while (exclusion_count--) {
1094 free(exclusion_list[exclusion_count]);
1095 }
1096 free(exclusion_list);
1097 exclusion_list = NULL;
1098 }
1099 /* Check for proper subsets */
1100 ret = check_exclusion_subsets(event_name, opt_exclude,
1101 &exclusion_count, &exclusion_list);
1102 if (ret == CMD_ERROR) {
1103 goto error;
1104 }
1105 }
1106
0cda4f28 1107 ev.loglevel_type = opt_loglevel_type;
ed7f4083 1108 if (opt_loglevel) {
8005f29a
MD
1109 ev.loglevel = loglevel_str_to_value(opt_loglevel);
1110 if (ev.loglevel == -1) {
1111 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1112 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
1113 goto error;
1114 }
22e25b71
MD
1115 } else {
1116 ev.loglevel = -1;
ed7f4083 1117 }
0e115563 1118 } else if (opt_jul || opt_log4j || opt_python) {
b9dfb167
DG
1119 if (opt_event_type != LTTNG_EVENT_ALL &&
1120 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
5cdb6027 1121 ERR("Event type not supported for domain.");
b9dfb167
DG
1122 ret = CMD_UNSUPPORTED;
1123 goto error;
1124 }
b2064f54
DG
1125
1126 ev.loglevel_type = opt_loglevel_type;
1127 if (opt_loglevel) {
5cdb6027
DG
1128 if (opt_jul) {
1129 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
1130 } else if (opt_log4j) {
1131 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563
DG
1132 } else if (opt_python) {
1133 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
5cdb6027 1134 }
b2064f54
DG
1135 if (ev.loglevel == -1) {
1136 ERR("Unknown loglevel %s", opt_loglevel);
1137 ret = -LTTNG_ERR_INVALID;
1138 goto error;
1139 }
1140 } else {
5cdb6027
DG
1141 if (opt_jul) {
1142 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1143 } else if (opt_log4j) {
1144 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563
DG
1145 } else if (opt_python) {
1146 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
5cdb6027 1147 }
b2064f54 1148 }
b9dfb167
DG
1149 ev.type = LTTNG_EVENT_TRACEPOINT;
1150 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1151 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1152 } else {
b9dfb167 1153 print_missing_domain();
300b8fd5 1154 ret = CMD_ERROR;
f3ed775e
DG
1155 goto error;
1156 }
1157
025faf73 1158 if (!opt_filter) {
9c48cab3
JI
1159 char *exclusion_string;
1160
89476427 1161 command_ret = lttng_enable_event_with_exclusions(handle,
7ed70bc9
JI
1162 &ev, channel_name,
1163 NULL, exclusion_count, exclusion_list);
9c48cab3 1164 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
1165 if (!exclusion_string) {
1166 PERROR("Cannot allocate exclusion_string");
1167 error = 1;
1168 goto end;
1169 }
89476427 1170 if (command_ret < 0) {
025faf73 1171 /* Turn ret to positive value to handle the positive error code */
89476427 1172 switch (-command_ret) {
025faf73 1173 case LTTNG_ERR_KERN_EVENT_EXIST:
9c48cab3 1174 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
85076754 1175 event_name,
9c48cab3 1176 exclusion_string,
85076754 1177 print_channel_name(channel_name), session_name);
89476427 1178 warn = 1;
025faf73 1179 break;
45d5d421
CB
1180 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1181 {
1182 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1183 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1184 exclusion_string,
1185 msg,
1186 print_channel_name(channel_name),
1187 session_name);
1188 error = 1;
1189 break;
1190 }
025faf73 1191 default:
9c48cab3
JI
1192 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1193 exclusion_string,
da3d7d0e
JRJ
1194 lttng_strerror(command_ret),
1195 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1196 ? print_raw_channel_name(channel_name)
1197 : print_channel_name(channel_name),
1198 session_name);
89476427 1199 error = 1;
025faf73
DG
1200 break;
1201 }
89476427 1202 error_holder = command_ret;
025faf73 1203 } else {
8274eeba
AB
1204 switch (dom.type) {
1205 case LTTNG_DOMAIN_KERNEL:
1206 case LTTNG_DOMAIN_UST:
49ceaa70 1207 MSG("%s event %s%s created in channel %s",
8274eeba
AB
1208 get_domain_str(dom.type),
1209 event_name,
1210 exclusion_string,
1211 print_channel_name(channel_name));
1212 break;
1213 case LTTNG_DOMAIN_JUL:
1214 case LTTNG_DOMAIN_LOG4J:
1215 case LTTNG_DOMAIN_PYTHON:
1216 /*
1217 * Don't print the default channel
1218 * name for agent domains.
1219 */
895707da 1220 MSG("%s event %s%s enabled",
8274eeba
AB
1221 get_domain_str(dom.type),
1222 event_name,
1223 exclusion_string);
1224 break;
1225 default:
1226 assert(0);
49ceaa70 1227 }
42224349 1228 }
9c48cab3 1229 free(exclusion_string);
6181537c 1230 }
025faf73
DG
1231
1232 if (opt_filter) {
9c48cab3
JI
1233 char *exclusion_string;
1234
89476427
JRJ
1235 /* Filter present */
1236 ev.filter = 1;
1237
1238 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
7ed70bc9 1239 opt_filter, exclusion_count, exclusion_list);
9c48cab3 1240 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
1241 if (!exclusion_string) {
1242 PERROR("Cannot allocate exclusion_string");
1243 error = 1;
1244 goto end;
1245 }
89476427
JRJ
1246 if (command_ret < 0) {
1247 switch (-command_ret) {
7671f53c 1248 case LTTNG_ERR_FILTER_EXIST:
9c48cab3 1249 WARN("Filter on event %s%s is already enabled"
7671f53c 1250 " (channel %s, session %s)",
85076754 1251 event_name,
9c48cab3 1252 exclusion_string,
85076754 1253 print_channel_name(channel_name), session_name);
89476427 1254 warn = 1;
7671f53c 1255 break;
45d5d421
CB
1256 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1257 {
1258 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1259 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1260 exclusion_string,
1261 msg,
1262 print_channel_name(channel_name),
1263 session_name, opt_filter);
1264 error = 1;
1265 break;
1266 }
7671f53c 1267 default:
9c48cab3
JI
1268 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1269 exclusion_string,
da3d7d0e
JRJ
1270 lttng_strerror(command_ret),
1271 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1272 ? print_raw_channel_name(channel_name)
1273 : print_channel_name(channel_name),
1274 session_name, opt_filter);
89476427 1275 error = 1;
7671f53c
CB
1276 break;
1277 }
89476427
JRJ
1278 error_holder = command_ret;
1279
16363652 1280 } else {
9c48cab3
JI
1281 MSG("Event %s%s: Filter '%s' successfully set",
1282 event_name, exclusion_string,
1283 opt_filter);
53a80697 1284 }
9c48cab3 1285 free(exclusion_string);
53a80697 1286 }
6181537c 1287
89476427
JRJ
1288 if (lttng_opt_mi) {
1289 if (command_ret) {
1290 success = 0;
1291 ev.enabled = 0;
1292 } else {
1293 ev.enabled = 1;
1294 }
1295
970d848b 1296 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
89476427
JRJ
1297 if (ret) {
1298 ret = CMD_ERROR;
1299 goto error;
1300 }
1301
1302 /* print exclusion */
1303 ret = mi_print_exclusion(exclusion_count, exclusion_list);
1304 if (ret) {
1305 ret = CMD_ERROR;
1306 goto error;
1307 }
1308
1309 /* Success ? */
1310 ret = mi_lttng_writer_write_element_bool(writer,
1311 mi_lttng_element_command_success, success);
1312 if (ret) {
1313 ret = CMD_ERROR;
1314 goto end;
1315 }
1316
1317 /* Close event element */
1318 ret = mi_lttng_writer_close_element(writer);
1319 if (ret) {
1320 ret = CMD_ERROR;
1321 goto end;
1322 }
1323 }
1324
f3ed775e
DG
1325 /* Next event */
1326 event_name = strtok(NULL, ",");
89476427
JRJ
1327 /* Reset warn, error and success */
1328 success = 1;
f3ed775e
DG
1329 }
1330
8c9ae521 1331end:
89476427
JRJ
1332 /* Close Mi */
1333 if (lttng_opt_mi) {
1334 /* Close events element */
1335 ret = mi_lttng_writer_close_element(writer);
1336 if (ret) {
1337 ret = CMD_ERROR;
1338 goto error;
1339 }
1340 }
f3ed775e 1341error:
ae856491
DG
1342 if (warn) {
1343 ret = CMD_WARNING;
1344 }
89476427
JRJ
1345 if (error) {
1346 ret = CMD_ERROR;
1347 }
cd80958d
DG
1348 lttng_destroy_handle(handle);
1349
7ed70bc9
JI
1350 if (exclusion_list != NULL) {
1351 while (exclusion_count--) {
1352 free(exclusion_list[exclusion_count]);
1353 }
1354 free(exclusion_list);
1355 }
1356
89476427
JRJ
1357 /* Overwrite ret with error_holder if there was an actual error with
1358 * enabling an event.
1359 */
1360 ret = error_holder ? error_holder : ret;
1361
f3ed775e
DG
1362 return ret;
1363}
1364
1365/*
6181537c 1366 * Add event to trace session
f3ed775e
DG
1367 */
1368int cmd_enable_events(int argc, const char **argv)
1369{
89476427 1370 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 1371 static poptContext pc;
cd80958d 1372 char *session_name = NULL;
de044b7a 1373 int event_type = -1;
f3ed775e
DG
1374
1375 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1376 poptReadDefaultConfig(pc, 0);
1377
1378 /* Default event type */
7a3d1328 1379 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
1380
1381 while ((opt = poptGetNextOpt(pc)) != -1) {
1382 switch (opt) {
1383 case OPT_HELP:
ca1c3607 1384 usage(stdout);
f3ed775e 1385 goto end;
f3ed775e 1386 case OPT_TRACEPOINT:
e6ddca71 1387 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 1388 break;
cf0e5467 1389 case OPT_PROBE:
7d29a247 1390 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
1391 break;
1392 case OPT_FUNCTION:
1393 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b 1394 break;
a54bd42d
MD
1395 case OPT_SYSCALL:
1396 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 1397 break;
eeac7d46
MD
1398 case OPT_USERSPACE:
1399 opt_userspace = 1;
eeac7d46 1400 break;
0cda4f28 1401 case OPT_LOGLEVEL:
8005f29a 1402 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
1403 opt_loglevel = poptGetOptArg(pc);
1404 break;
1405 case OPT_LOGLEVEL_ONLY:
8005f29a 1406 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 1407 opt_loglevel = poptGetOptArg(pc);
13dce3b7 1408 break;
679b4943
SM
1409 case OPT_LIST_OPTIONS:
1410 list_cmd_options(stdout, long_options);
679b4943 1411 goto end;
53a80697
MD
1412 case OPT_FILTER:
1413 break;
fac3366c
JI
1414 case OPT_EXCLUDE:
1415 break;
f3ed775e
DG
1416 default:
1417 usage(stderr);
1418 ret = CMD_UNDEFINED;
1419 goto end;
1420 }
de044b7a
DG
1421
1422 /* Validate event type. Multiple event type are not supported. */
1423 if (event_type == -1) {
1424 event_type = opt_event_type;
1425 } else {
1426 if (event_type != opt_event_type) {
1427 ERR("Multiple event type not supported.");
1428 ret = CMD_ERROR;
1429 goto end;
1430 }
1431 }
f3ed775e
DG
1432 }
1433
89476427
JRJ
1434 /* Mi check */
1435 if (lttng_opt_mi) {
1436 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1437 if (!writer) {
1438 ret = -LTTNG_ERR_NOMEM;
1439 goto end;
1440 }
1441
1442 /* Open command element */
1443 ret = mi_lttng_writer_command_open(writer,
1444 mi_lttng_element_command_enable_event);
1445 if (ret) {
1446 ret = CMD_ERROR;
1447 goto end;
1448 }
1449
1450 /* Open output element */
1451 ret = mi_lttng_writer_open_element(writer,
1452 mi_lttng_element_command_output);
1453 if (ret) {
1454 ret = CMD_ERROR;
1455 goto end;
1456 }
1457 }
1458
f3ed775e
DG
1459 opt_event_list = (char*) poptGetArg(pc);
1460 if (opt_event_list == NULL && opt_enable_all == 0) {
1461 ERR("Missing event name(s).\n");
1462 usage(stderr);
ca1c3607 1463 ret = CMD_ERROR;
f3ed775e
DG
1464 goto end;
1465 }
1466
cd80958d
DG
1467 if (!opt_session_name) {
1468 session_name = get_session_name();
1469 if (session_name == NULL) {
89476427
JRJ
1470 command_ret = CMD_ERROR;
1471 success = 0;
1472 goto mi_closing;
cd80958d
DG
1473 }
1474 } else {
1475 session_name = opt_session_name;
1476 }
1477
89476427
JRJ
1478 command_ret = enable_events(session_name);
1479 if (command_ret) {
1480 success = 0;
1481 goto mi_closing;
1482 }
1483
1484mi_closing:
1485 /* Mi closing */
1486 if (lttng_opt_mi) {
1487 /* Close output element */
1488 ret = mi_lttng_writer_close_element(writer);
1489 if (ret) {
1490 ret = CMD_ERROR;
1491 goto end;
1492 }
1493
1494 ret = mi_lttng_writer_write_element_bool(writer,
1495 mi_lttng_element_command_success, success);
1496 if (ret) {
1497 ret = CMD_ERROR;
1498 goto end;
1499 }
1500
1501 /* Command element close */
1502 ret = mi_lttng_writer_command_close(writer);
1503 if (ret) {
1504 ret = CMD_ERROR;
1505 goto end;
1506 }
1507 }
f3ed775e
DG
1508
1509end:
89476427
JRJ
1510 /* Mi clean-up */
1511 if (writer && mi_lttng_writer_destroy(writer)) {
1512 /* Preserve original error code */
1513 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1514 }
1515
cd80958d
DG
1516 if (opt_session_name == NULL) {
1517 free(session_name);
1518 }
1519
89476427
JRJ
1520 /* Overwrite ret if an error occurred in enable_events */
1521 ret = command_ret ? command_ret : ret;
1522
ca1c3607 1523 poptFreeContext(pc);
f3ed775e
DG
1524 return ret;
1525}
This page took 0.132008 seconds and 5 git commands to generate.