Implement kernel filter support
[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");
23171d8c 130 fprintf(ofp, " -p, --python Apply for Python application\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
3712b110
JG
337 if (!inputstr || strlen(inputstr) == 0) {
338 return -1;
339 }
340
5cdb6027
DG
341 /*
342 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
343 * added at the end of the loop so a the upper bound we avoid the overflow.
344 */
345 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
346 str[i] = toupper(inputstr[i]);
347 i++;
348 }
349 str[i] = '\0';
350
351 if (!strcmp(str, "LOG4J_OFF") || !strcmp(str, "OFF")) {
352 return LTTNG_LOGLEVEL_LOG4J_OFF;
353 } else if (!strcmp(str, "LOG4J_FATAL") || !strcmp(str, "FATAL")) {
354 return LTTNG_LOGLEVEL_LOG4J_FATAL;
355 } else if (!strcmp(str, "LOG4J_ERROR") || !strcmp(str, "ERROR")) {
356 return LTTNG_LOGLEVEL_LOG4J_ERROR;
357 } else if (!strcmp(str, "LOG4J_WARN") || !strcmp(str, "WARN")) {
358 return LTTNG_LOGLEVEL_LOG4J_WARN;
359 } else if (!strcmp(str, "LOG4J_INFO") || !strcmp(str, "INFO")) {
360 return LTTNG_LOGLEVEL_LOG4J_INFO;
361 } else if (!strcmp(str, "LOG4J_DEBUG") || !strcmp(str, "DEBUG")) {
362 return LTTNG_LOGLEVEL_LOG4J_DEBUG;
363 } else if (!strcmp(str, "LOG4J_TRACE") || !strcmp(str, "TRACE")) {
364 return LTTNG_LOGLEVEL_LOG4J_TRACE;
365 } else if (!strcmp(str, "LOG4J_ALL") || !strcmp(str, "ALL")) {
366 return LTTNG_LOGLEVEL_LOG4J_ALL;
367 } else {
368 return -1;
369 }
370}
371
b2064f54
DG
372/*
373 * Maps JUL loglevel from string to value
374 */
375static int loglevel_jul_str_to_value(const char *inputstr)
376{
377 int i = 0;
378 char str[LTTNG_SYMBOL_NAME_LEN];
379
3712b110
JG
380 if (!inputstr || strlen(inputstr) == 0) {
381 return -1;
382 }
383
b2064f54
DG
384 /*
385 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
386 * added at the end of the loop so a the upper bound we avoid the overflow.
387 */
388 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
389 str[i] = toupper(inputstr[i]);
390 i++;
391 }
392 str[i] = '\0';
393
394 if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) {
395 return LTTNG_LOGLEVEL_JUL_OFF;
396 } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) {
397 return LTTNG_LOGLEVEL_JUL_SEVERE;
398 } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) {
399 return LTTNG_LOGLEVEL_JUL_WARNING;
400 } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) {
401 return LTTNG_LOGLEVEL_JUL_INFO;
402 } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) {
403 return LTTNG_LOGLEVEL_JUL_CONFIG;
404 } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) {
405 return LTTNG_LOGLEVEL_JUL_FINE;
406 } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) {
407 return LTTNG_LOGLEVEL_JUL_FINER;
408 } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) {
409 return LTTNG_LOGLEVEL_JUL_FINEST;
410 } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) {
411 return LTTNG_LOGLEVEL_JUL_ALL;
412 } else {
413 return -1;
414 }
415}
416
0e115563
DG
417/*
418 * Maps Python loglevel from string to value
419 */
420static int loglevel_python_str_to_value(const char *inputstr)
421{
422 int i = 0;
423 char str[LTTNG_SYMBOL_NAME_LEN];
424
3712b110
JG
425 if (!inputstr || strlen(inputstr) == 0) {
426 return -1;
427 }
428
0e115563
DG
429 /*
430 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
431 * added at the end of the loop so a the upper bound we avoid the overflow.
432 */
433 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
434 str[i] = toupper(inputstr[i]);
435 i++;
436 }
437 str[i] = '\0';
438
439 if (!strcmp(str, "PYTHON_CRITICAL") || !strcmp(str, "CRITICAL")) {
440 return LTTNG_LOGLEVEL_PYTHON_CRITICAL;
441 } else if (!strcmp(str, "PYTHON_ERROR") || !strcmp(str, "ERROR")) {
442 return LTTNG_LOGLEVEL_PYTHON_ERROR;
443 } else if (!strcmp(str, "PYTHON_WARNING") || !strcmp(str, "WARNING")) {
444 return LTTNG_LOGLEVEL_PYTHON_WARNING;
445 } else if (!strcmp(str, "PYTHON_INFO") || !strcmp(str, "INFO")) {
446 return LTTNG_LOGLEVEL_PYTHON_INFO;
447 } else if (!strcmp(str, "PYTNON_DEBUG") || !strcmp(str, "DEBUG")) {
448 return LTTNG_LOGLEVEL_PYTHON_DEBUG;
449 } else if (!strcmp(str, "PYTHON_NOTSET") || !strcmp(str, "NOTSET")) {
450 return LTTNG_LOGLEVEL_PYTHON_NOTSET;
451 } else {
452 return -1;
453 }
454}
455
8005f29a 456/*
0c8477c8 457 * Maps loglevel from string to value
8005f29a
MD
458 */
459static
46839cc2 460int loglevel_str_to_value(const char *inputstr)
8005f29a 461{
46839cc2
MD
462 int i = 0;
463 char str[LTTNG_SYMBOL_NAME_LEN];
464
3712b110
JG
465 if (!inputstr || strlen(inputstr) == 0) {
466 return -1;
467 }
468
e051e07c
DG
469 /*
470 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
471 * added at the end of the loop so a the upper bound we avoid the overflow.
472 */
473 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
46839cc2
MD
474 str[i] = toupper(inputstr[i]);
475 i++;
476 }
477 str[i] = '\0';
478 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
479 return LTTNG_LOGLEVEL_EMERG;
480 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
481 return LTTNG_LOGLEVEL_ALERT;
482 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
483 return LTTNG_LOGLEVEL_CRIT;
484 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
485 return LTTNG_LOGLEVEL_ERR;
486 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
487 return LTTNG_LOGLEVEL_WARNING;
488 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
489 return LTTNG_LOGLEVEL_NOTICE;
490 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
491 return LTTNG_LOGLEVEL_INFO;
492 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
493 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
494 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
495 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
496 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
497 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
498 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
499 return LTTNG_LOGLEVEL_DEBUG_MODULE;
500 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
501 return LTTNG_LOGLEVEL_DEBUG_UNIT;
502 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
503 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
504 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
505 return LTTNG_LOGLEVEL_DEBUG_LINE;
506 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
507 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
508 } else {
509 return -1;
510 }
511}
512
85076754
MD
513static
514const char *print_channel_name(const char *name)
515{
516 return name ? : DEFAULT_CHANNEL_NAME;
517}
518
519static
520const char *print_raw_channel_name(const char *name)
521{
522 return name ? : "<default>";
523}
524
89476427
JRJ
525/*
526 * Mi print exlcusion list
527 */
528static
529int mi_print_exclusion(int count, char **names)
530{
531 int i, ret;
532
533 assert(writer);
534
535 if (count == 0) {
536 ret = 0;
537 goto end;
538 }
539 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
540 if (ret) {
541 goto end;
542 }
543
544 for (i = 0; i < count; i++) {
545 ret = mi_lttng_writer_write_element_string(writer,
546 config_element_exclusion, names[i]);
547 if (ret) {
548 goto end;
549 }
550 }
551
552 /* Close exclusions element */
553 ret = mi_lttng_writer_close_element(writer);
554
555end:
556 return ret;
557}
558
9c48cab3
JI
559/*
560 * Return allocated string for pretty-printing exclusion names.
561 */
562static
563char *print_exclusions(int count, char **names)
564{
565 int length = 0;
566 int i;
567 const char *preamble = " excluding ";
568 char *ret;
569
570 if (count == 0) {
571 return strdup("");
572 }
573
574 /* calculate total required length */
575 for (i = 0; i < count; i++) {
576 length += strlen(names[i]) + 1;
577 }
578
579 /* add length of preamble + one for NUL - one for last (missing) comma */
580 length += strlen(preamble);
fd591b18
MD
581 ret = zmalloc(length);
582 if (!ret) {
583 return NULL;
584 }
9c48cab3
JI
585 strncpy(ret, preamble, length);
586 for (i = 0; i < count; i++) {
587 strcat(ret, names[i]);
588 if (i != count - 1) {
589 strcat(ret, ",");
590 }
591 }
89476427 592
9c48cab3
JI
593 return ret;
594}
595
748bde76
JI
596/*
597 * Compare list of exclusions against an event name.
598 * Return a list of legal exclusion names.
599 * Produce an error or a warning about others (depending on the situation)
600 */
601static
602int check_exclusion_subsets(const char *event_name,
603 const char *exclusions,
604 int *exclusion_count_ptr,
605 char ***exclusion_list_ptr)
606{
607 const char *excluder_ptr;
608 const char *event_ptr;
609 const char *next_excluder;
610 int excluder_length;
611 int exclusion_count = 0;
612 char **exclusion_list = NULL;
613 int ret = CMD_SUCCESS;
614
615 if (event_name[strlen(event_name) - 1] != '*') {
616 ERR("Event %s: Excluders can only be used with wildcarded events", event_name);
617 goto error;
618 }
619
620 next_excluder = exclusions;
621 while (*next_excluder != 0) {
622 event_ptr = event_name;
623 excluder_ptr = next_excluder;
624 excluder_length = strcspn(next_excluder, ",");
625
626 /* Scan both the excluder and the event letter by letter */
627 while (1) {
628 char e, x;
629
630 e = *event_ptr;
631 x = *excluder_ptr;
632
633 if (x == '*') {
634 /* Event is a subset of the excluder */
635 ERR("Event %s: %.*s excludes all events from %s",
636 event_name,
637 excluder_length,
638 next_excluder,
639 event_name);
640 goto error;
641 }
642 if (e == '*') {
5ef79758
MD
643 char *string;
644 char **new_exclusion_list;
645
748bde76 646 /* Excluder is a proper subset of event */
5ef79758
MD
647 string = strndup(next_excluder, excluder_length);
648 if (!string) {
649 PERROR("strndup error");
650 goto error;
651 }
652 new_exclusion_list = realloc(exclusion_list,
653 sizeof(char **) * (exclusion_count + 1));
654 if (!new_exclusion_list) {
655 PERROR("realloc");
656 free(string);
657 goto error;
658 }
659 exclusion_list = new_exclusion_list;
748bde76 660 exclusion_count++;
5ef79758 661 exclusion_list[exclusion_count - 1] = string;
748bde76
JI
662 break;
663 }
664 if (x != e) {
665 /* Excluder and event sets have no common elements */
666 WARN("Event %s: %.*s does not exclude any events from %s",
667 event_name,
668 excluder_length,
669 next_excluder,
670 event_name);
671 break;
672 }
673 excluder_ptr++;
674 event_ptr++;
675 }
676 /* next excluder */
677 next_excluder += excluder_length;
678 if (*next_excluder == ',') {
679 next_excluder++;
680 }
681 }
682 goto end;
683error:
684 while (exclusion_count--) {
685 free(exclusion_list[exclusion_count]);
686 }
687 if (exclusion_list != NULL) {
688 free(exclusion_list);
689 }
690 exclusion_list = NULL;
691 exclusion_count = 0;
692 ret = CMD_ERROR;
693end:
694 *exclusion_count_ptr = exclusion_count;
695 *exclusion_list_ptr = exclusion_list;
696 return ret;
697}
f3ed775e 698/*
6181537c 699 * Enabling event using the lttng API.
89476427 700 * Note: in case of error only the last error code will be return.
f3ed775e 701 */
cd80958d 702static int enable_events(char *session_name)
f3ed775e 703{
89476427
JRJ
704 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
705 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
b73d0b29 706 char *event_name, *channel_name = NULL;
f3ed775e 707 struct lttng_event ev;
7d29a247 708 struct lttng_domain dom;
7ed70bc9
JI
709 int exclusion_count = 0;
710 char **exclusion_list = NULL;
f3ed775e 711
441c16a7
MD
712 memset(&ev, 0, sizeof(ev));
713 memset(&dom, 0, sizeof(dom));
714
53a80697 715 if (opt_kernel) {
67b58630
JG
716 if (opt_loglevel) {
717 WARN("Kernel loglevels are not supported.");
718 }
53a80697
MD
719 }
720
7d29a247
DG
721 /* Create lttng domain */
722 if (opt_kernel) {
723 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 724 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 725 } else if (opt_userspace) {
2bdd86d4 726 dom.type = LTTNG_DOMAIN_UST;
7972aab2 727 /* Default. */
8692d4e5 728 dom.buf_type = LTTNG_BUFFER_PER_UID;
b9dfb167
DG
729 } else if (opt_jul) {
730 dom.type = LTTNG_DOMAIN_JUL;
731 /* Default. */
732 dom.buf_type = LTTNG_BUFFER_PER_UID;
5cdb6027
DG
733 } else if (opt_log4j) {
734 dom.type = LTTNG_DOMAIN_LOG4J;
735 /* Default. */
736 dom.buf_type = LTTNG_BUFFER_PER_UID;
0e115563
DG
737 } else if (opt_python) {
738 dom.type = LTTNG_DOMAIN_PYTHON;
739 /* Default. */
740 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 741 } else {
b9dfb167 742 print_missing_domain();
d16c1a4c 743 ret = CMD_ERROR;
6181537c 744 goto error;
2bdd86d4 745 }
7d29a247 746
d5dd17fd
JI
747 if (opt_kernel && opt_exclude) {
748 ERR("Event name exclusions are not yet implemented for kernel events");
749 ret = CMD_ERROR;
750 goto error;
751 }
752
85076754 753 channel_name = opt_channel_name;
ae856491 754
cd80958d
DG
755 handle = lttng_create_handle(session_name, &dom);
756 if (handle == NULL) {
757 ret = -1;
758 goto error;
759 }
1aef21b6 760
89476427
JRJ
761 /* Prepare Mi */
762 if (lttng_opt_mi) {
763 /* Open a events element */
764 ret = mi_lttng_writer_open_element(writer, config_element_events);
765 if (ret) {
766 ret = CMD_ERROR;
767 goto error;
768 }
769 }
770
cd80958d 771 if (opt_enable_all) {
8c9ae521 772 /* Default setup for enable all */
75e8c5ab
MD
773 if (opt_kernel) {
774 ev.type = opt_event_type;
29c62722 775 strcpy(ev.name, "*");
300b8fd5
MD
776 /* kernel loglevels not implemented */
777 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
778 } else {
779 ev.type = LTTNG_EVENT_TRACEPOINT;
780 strcpy(ev.name, "*");
300b8fd5
MD
781 ev.loglevel_type = opt_loglevel_type;
782 if (opt_loglevel) {
0e115563 783 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54
DG
784 if (opt_userspace) {
785 ev.loglevel = loglevel_str_to_value(opt_loglevel);
786 } else if (opt_jul) {
787 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027
DG
788 } else if (opt_log4j) {
789 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563
DG
790 } else if (opt_python) {
791 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
b2064f54 792 }
300b8fd5
MD
793 if (ev.loglevel == -1) {
794 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 795 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
796 goto error;
797 }
22e25b71 798 } else {
0e115563 799 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54
DG
800 if (opt_userspace) {
801 ev.loglevel = -1;
5cdb6027 802 } else if (opt_jul || opt_log4j) {
b2064f54 803 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
0e115563
DG
804 } else if (opt_python) {
805 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
b2064f54 806 }
300b8fd5 807 }
75e8c5ab 808 }
8c9ae521 809
7ed70bc9
JI
810 if (opt_exclude) {
811 ret = check_exclusion_subsets("*", opt_exclude,
812 &exclusion_count, &exclusion_list);
813 if (ret == CMD_ERROR) {
814 goto error;
815 }
89476427 816 ev.exclusion = 1;
7ed70bc9 817 }
025faf73 818 if (!opt_filter) {
7ed70bc9
JI
819 ret = lttng_enable_event_with_exclusions(handle,
820 &ev, channel_name,
821 NULL,
822 exclusion_count, exclusion_list);
025faf73
DG
823 if (ret < 0) {
824 switch (-ret) {
825 case LTTNG_ERR_KERN_EVENT_EXIST:
826 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 827 print_channel_name(channel_name), session_name);
89476427 828 warn = 1;
025faf73 829 break;
45d5d421
CB
830 case LTTNG_ERR_TRACE_ALREADY_STARTED:
831 {
832 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
833 ERR("Events: %s (channel %s, session %s)",
834 msg,
835 print_channel_name(channel_name),
836 session_name);
837 error = 1;
838 break;
839 }
025faf73
DG
840 default:
841 ERR("Events: %s (channel %s, session %s)",
85076754
MD
842 lttng_strerror(ret),
843 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
844 ? print_raw_channel_name(channel_name)
845 : print_channel_name(channel_name),
846 session_name);
89476427 847 error = 1;
025faf73
DG
848 break;
849 }
850 goto end;
42224349 851 }
8c9ae521 852
025faf73
DG
853 switch (opt_event_type) {
854 case LTTNG_EVENT_TRACEPOINT:
67b58630 855 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9c48cab3 856 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
857
858 if (!exclusion_string) {
859 PERROR("Cannot allocate exclusion_string");
860 error = 1;
861 goto end;
862 }
9c48cab3 863 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
b9dfb167 864 get_domain_str(dom.type),
9c48cab3 865 exclusion_string,
85076754 866 print_channel_name(channel_name),
025faf73 867 opt_loglevel);
9c48cab3 868 free(exclusion_string);
025faf73 869 } else {
9c48cab3 870 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
871
872 if (!exclusion_string) {
873 PERROR("Cannot allocate exclusion_string");
874 error = 1;
875 goto end;
876 }
9c48cab3 877 MSG("All %s tracepoints%s are enabled in channel %s",
b9dfb167 878 get_domain_str(dom.type),
9c48cab3 879 exclusion_string,
85076754 880 print_channel_name(channel_name));
9c48cab3 881 free(exclusion_string);
025faf73
DG
882 }
883 break;
884 case LTTNG_EVENT_SYSCALL:
885 if (opt_kernel) {
6e911cad
MD
886 MSG("All %s system calls are enabled in channel %s",
887 get_domain_str(dom.type),
85076754 888 print_channel_name(channel_name));
025faf73
DG
889 }
890 break;
891 case LTTNG_EVENT_ALL:
67b58630 892 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9c48cab3 893 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
894
895 if (!exclusion_string) {
896 PERROR("Cannot allocate exclusion_string");
897 error = 1;
898 goto end;
899 }
9c48cab3 900 MSG("All %s events%s are enabled in channel %s for loglevel %s",
b9dfb167 901 get_domain_str(dom.type),
9c48cab3 902 exclusion_string,
85076754 903 print_channel_name(channel_name),
025faf73 904 opt_loglevel);
9c48cab3 905 free(exclusion_string);
025faf73 906 } else {
9c48cab3 907 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
908
909 if (!exclusion_string) {
910 PERROR("Cannot allocate exclusion_string");
911 error = 1;
912 goto end;
913 }
9c48cab3 914 MSG("All %s events%s are enabled in channel %s",
b9dfb167 915 get_domain_str(dom.type),
9c48cab3 916 exclusion_string,
85076754 917 print_channel_name(channel_name));
9c48cab3 918 free(exclusion_string);
025faf73
DG
919 }
920 break;
921 default:
922 /*
923 * We should not be here since lttng_enable_event should have
924 * failed on the event type.
925 */
926 goto error;
57064ada 927 }
f3ed775e 928 }
89476427 929
025faf73 930 if (opt_filter) {
89476427 931 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
7ed70bc9 932 opt_filter, exclusion_count, exclusion_list);
89476427
JRJ
933 if (command_ret < 0) {
934 switch (-command_ret) {
16363652 935 case LTTNG_ERR_FILTER_EXIST:
85076754 936 WARN("Filter on all events is already enabled"
16363652 937 " (channel %s, session %s)",
85076754 938 print_channel_name(channel_name), session_name);
89476427 939 warn = 1;
16363652 940 break;
45d5d421
CB
941 case LTTNG_ERR_TRACE_ALREADY_STARTED:
942 {
943 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
944 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
945 msg,
946 print_channel_name(channel_name),
947 session_name, opt_filter);
948 error = 1;
949 break;
950 }
16363652 951 default:
85076754 952 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
da3d7d0e
JRJ
953 lttng_strerror(command_ret),
954 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
955 ? print_raw_channel_name(channel_name)
956 : print_channel_name(channel_name),
957 session_name, opt_filter);
89476427 958 error = 1;
16363652
DG
959 break;
960 }
89476427 961 error_holder = command_ret;
16363652 962 } else {
89476427 963 ev.filter = 1;
16363652
DG
964 MSG("Filter '%s' successfully set", opt_filter);
965 }
966 }
89476427
JRJ
967
968 if (lttng_opt_mi) {
969 /* The wildcard * is used for kernel and ust domain to
970 * represent ALL. We copy * in event name to force the wildcard use
971 * for kernel domain
972 *
973 * Note: this is strictly for semantic and printing while in
974 * machine interface mode.
975 */
976 strcpy(ev.name, "*");
977
978 /* If we reach here the events are enabled */
979 if (!error && !warn) {
980 ev.enabled = 1;
981 } else {
982 ev.enabled = 0;
983 success = 0;
984 }
970d848b 985 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
89476427
JRJ
986 if (ret) {
987 ret = CMD_ERROR;
988 goto error;
989 }
990
991 /* print exclusion */
992 ret = mi_print_exclusion(exclusion_count, exclusion_list);
993 if (ret) {
994 ret = CMD_ERROR;
995 goto error;
996 }
997
998 /* Success ? */
999 ret = mi_lttng_writer_write_element_bool(writer,
1000 mi_lttng_element_command_success, success);
1001 if (ret) {
1002 ret = CMD_ERROR;
1003 goto error;
1004 }
1005
1006 /* Close event element */
1007 ret = mi_lttng_writer_close_element(writer);
1008 if (ret) {
1009 ret = CMD_ERROR;
1010 goto error;
1011 }
1012 }
1013
8c9ae521 1014 goto end;
f3ed775e
DG
1015 }
1016
1017 /* Strip event list */
1018 event_name = strtok(opt_event_list, ",");
1019 while (event_name != NULL) {
6181537c
DG
1020 /* Copy name and type of the event */
1021 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1022 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1023 ev.type = opt_event_type;
1024
f3ed775e
DG
1025 /* Kernel tracer action */
1026 if (opt_kernel) {
1027 DBG("Enabling kernel event %s for channel %s",
85076754
MD
1028 event_name,
1029 print_channel_name(channel_name));
f3ed775e
DG
1030
1031 switch (opt_event_type) {
29c62722
MD
1032 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
1033 /* If event name differs from *, select tracepoint. */
1034 if (strcmp(ev.name, "*")) {
1035 ev.type = LTTNG_EVENT_TRACEPOINT;
1036 }
1037 break;
e6ddca71 1038 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 1039 break;
7d29a247 1040 case LTTNG_EVENT_PROBE:
cf0e5467 1041 ret = parse_probe_opts(&ev, opt_probe);
49d4e302 1042 if (ret) {
cf0e5467 1043 ERR("Unable to parse probe options");
0d63dd19
DG
1044 ret = 0;
1045 goto error;
1046 }
f3ed775e
DG
1047 break;
1048 case LTTNG_EVENT_FUNCTION:
8f0d098b 1049 ret = parse_probe_opts(&ev, opt_function);
49d4e302 1050 if (ret) {
8f0d098b
MD
1051 ERR("Unable to parse function probe options");
1052 ret = 0;
1053 goto error;
1054 }
1055 break;
1056 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
1057 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
1058 LTTNG_SYMBOL_NAME_LEN);
99497cd0 1059 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1060 break;
a54bd42d 1061 case LTTNG_EVENT_SYSCALL:
c6aa2d41
MD
1062 ev.type = LTTNG_EVENT_SYSCALL;
1063 break;
f3ed775e 1064 default:
1ab1ea0b 1065 ret = CMD_UNDEFINED;
f3ed775e
DG
1066 goto error;
1067 }
0cda4f28 1068
0cda4f28 1069 /* kernel loglevels not implemented */
8005f29a 1070 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 1071 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 1072#if 0
e14f64a8
DG
1073 if (opt_cmd_name != NULL || opt_pid) {
1074 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 1075 ret = CMD_UNDEFINED;
2bdd86d4
MD
1076 goto error;
1077 }
d78d6610 1078#endif
6181537c 1079
300b8fd5 1080 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 1081 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
1082
1083 switch (opt_event_type) {
1084 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
1085 /* Fall-through */
1086 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
1087 /* Copy name and type of the event */
1088 ev.type = LTTNG_EVENT_TRACEPOINT;
1089 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1090 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
1091 break;
1092 case LTTNG_EVENT_PROBE:
1093 case LTTNG_EVENT_FUNCTION:
1094 case LTTNG_EVENT_FUNCTION_ENTRY:
1095 case LTTNG_EVENT_SYSCALL:
1096 default:
cc62c0c0 1097 ERR("Event type not available for user-space tracing");
4ce78777 1098 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
1099 goto error;
1100 }
0cda4f28 1101
7ed70bc9 1102 if (opt_exclude) {
89476427 1103 ev.exclusion = 1;
d5dd17fd
JI
1104 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1105 ERR("Exclusion option can only be used with tracepoint events");
1106 ret = CMD_ERROR;
1107 goto error;
1108 }
7ed70bc9
JI
1109 /* Free previously allocated items */
1110 if (exclusion_list != NULL) {
1111 while (exclusion_count--) {
1112 free(exclusion_list[exclusion_count]);
1113 }
1114 free(exclusion_list);
1115 exclusion_list = NULL;
1116 }
1117 /* Check for proper subsets */
1118 ret = check_exclusion_subsets(event_name, opt_exclude,
1119 &exclusion_count, &exclusion_list);
1120 if (ret == CMD_ERROR) {
1121 goto error;
1122 }
1123 }
1124
0cda4f28 1125 ev.loglevel_type = opt_loglevel_type;
ed7f4083 1126 if (opt_loglevel) {
8005f29a
MD
1127 ev.loglevel = loglevel_str_to_value(opt_loglevel);
1128 if (ev.loglevel == -1) {
1129 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1130 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
1131 goto error;
1132 }
22e25b71
MD
1133 } else {
1134 ev.loglevel = -1;
ed7f4083 1135 }
0e115563 1136 } else if (opt_jul || opt_log4j || opt_python) {
b9dfb167
DG
1137 if (opt_event_type != LTTNG_EVENT_ALL &&
1138 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
5cdb6027 1139 ERR("Event type not supported for domain.");
b9dfb167
DG
1140 ret = CMD_UNSUPPORTED;
1141 goto error;
1142 }
b2064f54
DG
1143
1144 ev.loglevel_type = opt_loglevel_type;
1145 if (opt_loglevel) {
5cdb6027
DG
1146 if (opt_jul) {
1147 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
1148 } else if (opt_log4j) {
1149 ev.loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563
DG
1150 } else if (opt_python) {
1151 ev.loglevel = loglevel_python_str_to_value(opt_loglevel);
5cdb6027 1152 }
b2064f54
DG
1153 if (ev.loglevel == -1) {
1154 ERR("Unknown loglevel %s", opt_loglevel);
1155 ret = -LTTNG_ERR_INVALID;
1156 goto error;
1157 }
1158 } else {
5cdb6027
DG
1159 if (opt_jul) {
1160 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1161 } else if (opt_log4j) {
1162 ev.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563
DG
1163 } else if (opt_python) {
1164 ev.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
5cdb6027 1165 }
b2064f54 1166 }
b9dfb167
DG
1167 ev.type = LTTNG_EVENT_TRACEPOINT;
1168 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
1169 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1170 } else {
b9dfb167 1171 print_missing_domain();
300b8fd5 1172 ret = CMD_ERROR;
f3ed775e
DG
1173 goto error;
1174 }
1175
025faf73 1176 if (!opt_filter) {
9c48cab3
JI
1177 char *exclusion_string;
1178
89476427 1179 command_ret = lttng_enable_event_with_exclusions(handle,
7ed70bc9
JI
1180 &ev, channel_name,
1181 NULL, exclusion_count, exclusion_list);
9c48cab3 1182 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
1183 if (!exclusion_string) {
1184 PERROR("Cannot allocate exclusion_string");
1185 error = 1;
1186 goto end;
1187 }
89476427 1188 if (command_ret < 0) {
025faf73 1189 /* Turn ret to positive value to handle the positive error code */
89476427 1190 switch (-command_ret) {
025faf73 1191 case LTTNG_ERR_KERN_EVENT_EXIST:
9c48cab3 1192 WARN("Kernel event %s%s already enabled (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;
025faf73 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)", event_name,
1202 exclusion_string,
1203 msg,
1204 print_channel_name(channel_name),
1205 session_name);
1206 error = 1;
1207 break;
1208 }
025faf73 1209 default:
9c48cab3
JI
1210 ERR("Event %s%s: %s (channel %s, session %s)", event_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);
89476427 1217 error = 1;
025faf73
DG
1218 break;
1219 }
89476427 1220 error_holder = command_ret;
025faf73 1221 } else {
5cdb6027
DG
1222 /* So we don't print the default channel name for agent domain. */
1223 if (dom.type == LTTNG_DOMAIN_JUL ||
1224 dom.type == LTTNG_DOMAIN_LOG4J) {
49ceaa70
DG
1225 MSG("%s event %s%s enabled.",
1226 get_domain_str(dom.type), event_name,
1227 exclusion_string);
1228 } else {
1229 MSG("%s event %s%s created in channel %s",
1230 get_domain_str(dom.type), event_name,
1231 exclusion_string,
1232 print_channel_name(channel_name));
1233 }
42224349 1234 }
9c48cab3 1235 free(exclusion_string);
6181537c 1236 }
025faf73
DG
1237
1238 if (opt_filter) {
9c48cab3
JI
1239 char *exclusion_string;
1240
89476427
JRJ
1241 /* Filter present */
1242 ev.filter = 1;
1243
1244 command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
7ed70bc9 1245 opt_filter, exclusion_count, exclusion_list);
9c48cab3 1246 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
5ef79758
MD
1247 if (!exclusion_string) {
1248 PERROR("Cannot allocate exclusion_string");
1249 error = 1;
1250 goto end;
1251 }
89476427
JRJ
1252 if (command_ret < 0) {
1253 switch (-command_ret) {
7671f53c 1254 case LTTNG_ERR_FILTER_EXIST:
9c48cab3 1255 WARN("Filter on event %s%s is already enabled"
7671f53c 1256 " (channel %s, session %s)",
85076754 1257 event_name,
9c48cab3 1258 exclusion_string,
85076754 1259 print_channel_name(channel_name), session_name);
89476427 1260 warn = 1;
7671f53c 1261 break;
45d5d421
CB
1262 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1263 {
1264 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1265 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1266 exclusion_string,
1267 msg,
1268 print_channel_name(channel_name),
1269 session_name, opt_filter);
1270 error = 1;
1271 break;
1272 }
7671f53c 1273 default:
9c48cab3
JI
1274 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
1275 exclusion_string,
da3d7d0e
JRJ
1276 lttng_strerror(command_ret),
1277 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1278 ? print_raw_channel_name(channel_name)
1279 : print_channel_name(channel_name),
1280 session_name, opt_filter);
89476427 1281 error = 1;
7671f53c
CB
1282 break;
1283 }
89476427
JRJ
1284 error_holder = command_ret;
1285
16363652 1286 } else {
9c48cab3
JI
1287 MSG("Event %s%s: Filter '%s' successfully set",
1288 event_name, exclusion_string,
1289 opt_filter);
53a80697 1290 }
9c48cab3 1291 free(exclusion_string);
53a80697 1292 }
6181537c 1293
89476427
JRJ
1294 if (lttng_opt_mi) {
1295 if (command_ret) {
1296 success = 0;
1297 ev.enabled = 0;
1298 } else {
1299 ev.enabled = 1;
1300 }
1301
970d848b 1302 ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
89476427
JRJ
1303 if (ret) {
1304 ret = CMD_ERROR;
1305 goto error;
1306 }
1307
1308 /* print exclusion */
1309 ret = mi_print_exclusion(exclusion_count, exclusion_list);
1310 if (ret) {
1311 ret = CMD_ERROR;
1312 goto error;
1313 }
1314
1315 /* Success ? */
1316 ret = mi_lttng_writer_write_element_bool(writer,
1317 mi_lttng_element_command_success, success);
1318 if (ret) {
1319 ret = CMD_ERROR;
1320 goto end;
1321 }
1322
1323 /* Close event element */
1324 ret = mi_lttng_writer_close_element(writer);
1325 if (ret) {
1326 ret = CMD_ERROR;
1327 goto end;
1328 }
1329 }
1330
f3ed775e
DG
1331 /* Next event */
1332 event_name = strtok(NULL, ",");
89476427
JRJ
1333 /* Reset warn, error and success */
1334 success = 1;
f3ed775e
DG
1335 }
1336
8c9ae521 1337end:
89476427
JRJ
1338 /* Close Mi */
1339 if (lttng_opt_mi) {
1340 /* Close events element */
1341 ret = mi_lttng_writer_close_element(writer);
1342 if (ret) {
1343 ret = CMD_ERROR;
1344 goto error;
1345 }
1346 }
f3ed775e 1347error:
ae856491
DG
1348 if (warn) {
1349 ret = CMD_WARNING;
1350 }
89476427
JRJ
1351 if (error) {
1352 ret = CMD_ERROR;
1353 }
cd80958d
DG
1354 lttng_destroy_handle(handle);
1355
7ed70bc9
JI
1356 if (exclusion_list != NULL) {
1357 while (exclusion_count--) {
1358 free(exclusion_list[exclusion_count]);
1359 }
1360 free(exclusion_list);
1361 }
1362
89476427
JRJ
1363 /* Overwrite ret with error_holder if there was an actual error with
1364 * enabling an event.
1365 */
1366 ret = error_holder ? error_holder : ret;
1367
f3ed775e
DG
1368 return ret;
1369}
1370
1371/*
6181537c 1372 * Add event to trace session
f3ed775e
DG
1373 */
1374int cmd_enable_events(int argc, const char **argv)
1375{
89476427 1376 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 1377 static poptContext pc;
cd80958d 1378 char *session_name = NULL;
de044b7a 1379 int event_type = -1;
f3ed775e
DG
1380
1381 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1382 poptReadDefaultConfig(pc, 0);
1383
1384 /* Default event type */
7a3d1328 1385 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
1386
1387 while ((opt = poptGetNextOpt(pc)) != -1) {
1388 switch (opt) {
1389 case OPT_HELP:
ca1c3607 1390 usage(stdout);
f3ed775e 1391 goto end;
f3ed775e 1392 case OPT_TRACEPOINT:
e6ddca71 1393 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 1394 break;
cf0e5467 1395 case OPT_PROBE:
7d29a247 1396 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
1397 break;
1398 case OPT_FUNCTION:
1399 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
1400 break;
1401 case OPT_FUNCTION_ENTRY:
1402 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 1403 break;
a54bd42d
MD
1404 case OPT_SYSCALL:
1405 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 1406 break;
eeac7d46
MD
1407 case OPT_USERSPACE:
1408 opt_userspace = 1;
eeac7d46 1409 break;
0cda4f28 1410 case OPT_LOGLEVEL:
8005f29a 1411 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
1412 opt_loglevel = poptGetOptArg(pc);
1413 break;
1414 case OPT_LOGLEVEL_ONLY:
8005f29a 1415 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 1416 opt_loglevel = poptGetOptArg(pc);
13dce3b7 1417 break;
679b4943
SM
1418 case OPT_LIST_OPTIONS:
1419 list_cmd_options(stdout, long_options);
679b4943 1420 goto end;
53a80697
MD
1421 case OPT_FILTER:
1422 break;
fac3366c
JI
1423 case OPT_EXCLUDE:
1424 break;
f3ed775e
DG
1425 default:
1426 usage(stderr);
1427 ret = CMD_UNDEFINED;
1428 goto end;
1429 }
de044b7a
DG
1430
1431 /* Validate event type. Multiple event type are not supported. */
1432 if (event_type == -1) {
1433 event_type = opt_event_type;
1434 } else {
1435 if (event_type != opt_event_type) {
1436 ERR("Multiple event type not supported.");
1437 ret = CMD_ERROR;
1438 goto end;
1439 }
1440 }
f3ed775e
DG
1441 }
1442
89476427
JRJ
1443 /* Mi check */
1444 if (lttng_opt_mi) {
1445 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1446 if (!writer) {
1447 ret = -LTTNG_ERR_NOMEM;
1448 goto end;
1449 }
1450
1451 /* Open command element */
1452 ret = mi_lttng_writer_command_open(writer,
1453 mi_lttng_element_command_enable_event);
1454 if (ret) {
1455 ret = CMD_ERROR;
1456 goto end;
1457 }
1458
1459 /* Open output element */
1460 ret = mi_lttng_writer_open_element(writer,
1461 mi_lttng_element_command_output);
1462 if (ret) {
1463 ret = CMD_ERROR;
1464 goto end;
1465 }
1466 }
1467
f3ed775e
DG
1468 opt_event_list = (char*) poptGetArg(pc);
1469 if (opt_event_list == NULL && opt_enable_all == 0) {
1470 ERR("Missing event name(s).\n");
1471 usage(stderr);
ca1c3607 1472 ret = CMD_ERROR;
f3ed775e
DG
1473 goto end;
1474 }
1475
cd80958d
DG
1476 if (!opt_session_name) {
1477 session_name = get_session_name();
1478 if (session_name == NULL) {
89476427
JRJ
1479 command_ret = CMD_ERROR;
1480 success = 0;
1481 goto mi_closing;
cd80958d
DG
1482 }
1483 } else {
1484 session_name = opt_session_name;
1485 }
1486
89476427
JRJ
1487 command_ret = enable_events(session_name);
1488 if (command_ret) {
1489 success = 0;
1490 goto mi_closing;
1491 }
1492
1493mi_closing:
1494 /* Mi closing */
1495 if (lttng_opt_mi) {
1496 /* Close output element */
1497 ret = mi_lttng_writer_close_element(writer);
1498 if (ret) {
1499 ret = CMD_ERROR;
1500 goto end;
1501 }
1502
1503 ret = mi_lttng_writer_write_element_bool(writer,
1504 mi_lttng_element_command_success, success);
1505 if (ret) {
1506 ret = CMD_ERROR;
1507 goto end;
1508 }
1509
1510 /* Command element close */
1511 ret = mi_lttng_writer_command_close(writer);
1512 if (ret) {
1513 ret = CMD_ERROR;
1514 goto end;
1515 }
1516 }
f3ed775e
DG
1517
1518end:
89476427
JRJ
1519 /* Mi clean-up */
1520 if (writer && mi_lttng_writer_destroy(writer)) {
1521 /* Preserve original error code */
1522 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1523 }
1524
cd80958d
DG
1525 if (opt_session_name == NULL) {
1526 free(session_name);
1527 }
1528
89476427
JRJ
1529 /* Overwrite ret if an error occurred in enable_events */
1530 ret = command_ret ? command_ret : ret;
1531
ca1c3607 1532 poptFreeContext(pc);
f3ed775e
DG
1533 return ret;
1534}
This page took 0.11705 seconds and 4 git commands to generate.