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