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