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