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