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