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