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