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