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