Commit | Line | Data |
---|---|---|
54d01ffb | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa | 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
54d01ffb | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
54d01ffb | 6 | * |
54d01ffb DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
28ab034a | 10 | #include "agent.hpp" |
c9e313bc SM |
11 | #include "channel.hpp" |
12 | #include "event.hpp" | |
13 | #include "kernel.hpp" | |
14 | #include "lttng-sessiond.hpp" | |
15 | #include "lttng-ust-ctl.hpp" | |
16 | #include "lttng-ust-error.hpp" | |
c9e313bc SM |
17 | #include "trace-kernel.hpp" |
18 | #include "trace-ust.hpp" | |
28ab034a | 19 | #include "ust-app.hpp" |
c9e313bc | 20 | #include "utils.hpp" |
54d01ffb | 21 | |
28ab034a JG |
22 | #include <common/bytecode/bytecode.hpp> |
23 | #include <common/compat/errno.hpp> | |
24 | #include <common/context.hpp> | |
25 | #include <common/error.hpp> | |
26 | #include <common/filter.hpp> | |
27 | #include <common/sessiond-comm/sessiond-comm.hpp> | |
56047f5a | 28 | #include <common/urcu.hpp> |
28ab034a JG |
29 | |
30 | #include <lttng/condition/condition.h> | |
31 | #include <lttng/condition/event-rule-matches.h> | |
32 | #include <lttng/event-rule/event-rule-internal.hpp> | |
33 | #include <lttng/event-rule/event-rule.h> | |
34 | #include <lttng/lttng.h> | |
35 | ||
36 | #include <string.h> | |
37 | #include <urcu/list.h> | |
38 | ||
025faf73 DG |
39 | /* |
40 | * Add unique UST event based on the event name, filter bytecode and loglevel. | |
41 | */ | |
28ab034a | 42 | static void add_unique_ust_event(struct lttng_ht *ht, struct ltt_ust_event *event) |
18eace3b DG |
43 | { |
44 | struct cds_lfht_node *node_ptr; | |
45 | struct ltt_ust_ht_key key; | |
46 | ||
a0377dfe FD |
47 | LTTNG_ASSERT(ht); |
48 | LTTNG_ASSERT(ht->ht); | |
49 | LTTNG_ASSERT(event); | |
18eace3b DG |
50 | |
51 | key.name = event->attr.name; | |
2b00d462 | 52 | key.filter = (struct lttng_bytecode *) event->filter; |
7966af57 | 53 | key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel_type; |
b953b8cd | 54 | key.loglevel_value = event->attr.loglevel; |
4031e53e | 55 | key.exclusion = event->exclusion; |
18eace3b DG |
56 | |
57 | node_ptr = cds_lfht_add_unique(ht->ht, | |
28ab034a JG |
58 | ht->hash_fct(event->node.key, lttng_ht_seed), |
59 | trace_ust_ht_match_event, | |
60 | &key, | |
61 | &event->node.node); | |
a0377dfe | 62 | LTTNG_ASSERT(node_ptr == &event->node.node); |
18eace3b DG |
63 | } |
64 | ||
54d01ffb | 65 | /* |
9550ee81 JR |
66 | * Disable kernel tracepoint events for a channel from the kernel session of |
67 | * a specified event_name and event type. | |
68 | * On type LTTNG_EVENT_ALL all events with event_name are disabled. | |
69 | * If event_name is NULL all events of the specified type are disabled. | |
54d01ffb | 70 | */ |
d0ae4ea8 | 71 | int event_kernel_disable_event(struct ltt_kernel_channel *kchan, |
28ab034a JG |
72 | const char *event_name, |
73 | enum lttng_event_type type) | |
54d01ffb | 74 | { |
9550ee81 | 75 | int ret, error = 0, found = 0; |
54d01ffb DG |
76 | struct ltt_kernel_event *kevent; |
77 | ||
a0377dfe | 78 | LTTNG_ASSERT(kchan); |
0525e9ae | 79 | |
54d01ffb | 80 | /* For each event in the kernel session */ |
28ab034a | 81 | cds_list_for_each_entry (kevent, &kchan->events_list.head, list) { |
d0ae4ea8 MD |
82 | if (type != LTTNG_EVENT_ALL && kevent->type != type) |
83 | continue; | |
5c7248cd | 84 | if (event_name != nullptr && strcmp(event_name, kevent->event->name) != 0) { |
9550ee81 JR |
85 | continue; |
86 | } | |
87 | found++; | |
54d01ffb DG |
88 | ret = kernel_disable_event(kevent); |
89 | if (ret < 0) { | |
9550ee81 | 90 | error = 1; |
54d01ffb DG |
91 | continue; |
92 | } | |
93 | } | |
9550ee81 | 94 | DBG("Disable kernel event: found %d events with name: %s and type: %d", |
28ab034a JG |
95 | found, |
96 | event_name ? event_name : "NULL", | |
97 | type); | |
7a3d1328 | 98 | |
cd9adb8b | 99 | if (event_name != nullptr && !found) { |
9550ee81 JR |
100 | ret = LTTNG_ERR_NO_EVENT; |
101 | } else { | |
102 | ret = error ? LTTNG_ERR_KERN_DISABLE_FAIL : LTTNG_OK; | |
103 | } | |
104 | ||
105 | return ret; | |
54d01ffb DG |
106 | } |
107 | ||
108 | /* | |
7a3d1328 | 109 | * Enable kernel tracepoint event for a channel from the kernel session. |
49d21f93 | 110 | * We own filter_expression and filter. |
54d01ffb | 111 | */ |
d0ae4ea8 | 112 | int event_kernel_enable_event(struct ltt_kernel_channel *kchan, |
28ab034a JG |
113 | struct lttng_event *event, |
114 | char *filter_expression, | |
115 | struct lttng_bytecode *filter) | |
54d01ffb DG |
116 | { |
117 | int ret; | |
118 | struct ltt_kernel_event *kevent; | |
119 | ||
a0377dfe FD |
120 | LTTNG_ASSERT(kchan); |
121 | LTTNG_ASSERT(event); | |
0525e9ae | 122 | |
28ab034a | 123 | kevent = trace_kernel_find_event(event->name, kchan, event->type, filter); |
cd9adb8b | 124 | if (kevent == nullptr) { |
71a3bb01 | 125 | ret = kernel_create_event(event, kchan, filter_expression, filter); |
a969e101 | 126 | /* We have passed ownership */ |
cd9adb8b JG |
127 | filter_expression = nullptr; |
128 | filter = nullptr; | |
71a3bb01 | 129 | if (ret) { |
7a3d1328 | 130 | goto end; |
54d01ffb | 131 | } |
66cefebd | 132 | } else if (!kevent->enabled) { |
54d01ffb DG |
133 | ret = kernel_enable_event(kevent); |
134 | if (ret < 0) { | |
f73fabfd | 135 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
7a3d1328 | 136 | goto end; |
54d01ffb | 137 | } |
42224349 DG |
138 | } else { |
139 | /* At this point, the event is considered enabled */ | |
f73fabfd | 140 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 | 141 | goto end; |
54d01ffb | 142 | } |
42224349 | 143 | |
f73fabfd | 144 | ret = LTTNG_OK; |
7a3d1328 | 145 | end: |
a969e101 MD |
146 | free(filter_expression); |
147 | free(filter); | |
54d01ffb DG |
148 | return ret; |
149 | } | |
150 | ||
7f79d3a1 DG |
151 | /* |
152 | * ============================ | |
153 | * UST : The Ultimate Frontier! | |
154 | * ============================ | |
155 | */ | |
156 | ||
2bdd86d4 MD |
157 | /* |
158 | * Enable UST tracepoint event for a channel from a UST session. | |
49d21f93 | 159 | * We own filter_expression, filter, and exclusion. |
2bdd86d4 | 160 | */ |
7972aab2 | 161 | int event_ust_enable_tracepoint(struct ltt_ust_session *usess, |
28ab034a JG |
162 | struct ltt_ust_channel *uchan, |
163 | struct lttng_event *event, | |
164 | char *filter_expression, | |
165 | struct lttng_bytecode *filter, | |
166 | struct lttng_event_exclusion *exclusion, | |
167 | bool internal_event) | |
2bdd86d4 | 168 | { |
88e3c2f5 | 169 | int ret = LTTNG_OK, to_create = 0; |
edb67388 DG |
170 | struct ltt_ust_event *uevent; |
171 | ||
a0377dfe FD |
172 | LTTNG_ASSERT(usess); |
173 | LTTNG_ASSERT(uchan); | |
174 | LTTNG_ASSERT(event); | |
0525e9ae | 175 | |
56047f5a | 176 | lttng::urcu::read_lock_guard read_lock; |
18eace3b | 177 | |
28ab034a JG |
178 | uevent = trace_ust_find_event(uchan->events, |
179 | event->name, | |
180 | filter, | |
181 | (enum lttng_ust_abi_loglevel_type) event->loglevel_type, | |
182 | event->loglevel, | |
183 | exclusion); | |
87c6e328 | 184 | if (!uevent) { |
28ab034a JG |
185 | ret = trace_ust_create_event( |
186 | event, filter_expression, filter, exclusion, internal_event, &uevent); | |
49d21f93 | 187 | /* We have passed ownership */ |
cd9adb8b JG |
188 | filter_expression = nullptr; |
189 | filter = nullptr; | |
190 | exclusion = nullptr; | |
39687410 | 191 | if (ret != LTTNG_OK) { |
3a243403 | 192 | goto end; |
edb67388 | 193 | } |
025faf73 | 194 | |
fc34caaa | 195 | /* Valid to set it after the goto error since uevent is still NULL */ |
edb67388 DG |
196 | to_create = 1; |
197 | } | |
2bdd86d4 | 198 | |
7f79d3a1 DG |
199 | if (uevent->enabled) { |
200 | /* It's already enabled so everything is OK */ | |
a0377dfe | 201 | LTTNG_ASSERT(!to_create); |
5bcdda4f | 202 | ret = LTTNG_ERR_UST_EVENT_ENABLED; |
7f79d3a1 DG |
203 | goto end; |
204 | } | |
205 | ||
66cefebd | 206 | uevent->enabled = true; |
88e3c2f5 JG |
207 | if (to_create) { |
208 | /* Add ltt ust event to channel */ | |
209 | add_unique_ust_event(uchan->events, uevent); | |
210 | } | |
211 | ||
212 | if (!usess->active) { | |
213 | goto end; | |
214 | } | |
fc34caaa | 215 | |
7972aab2 DG |
216 | if (to_create) { |
217 | /* Create event on all UST registered apps for session */ | |
218 | ret = ust_app_create_event_glb(usess, uchan, uevent); | |
219 | } else { | |
220 | /* Enable event on all UST registered apps for session */ | |
221 | ret = ust_app_enable_event_glb(usess, uchan, uevent); | |
222 | } | |
48842b30 | 223 | |
7972aab2 DG |
224 | if (ret < 0) { |
225 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
226 | ret = LTTNG_ERR_UST_EVENT_EXIST; | |
7972aab2 DG |
227 | } else { |
228 | ret = LTTNG_ERR_UST_ENABLE_FAIL; | |
edb67388 | 229 | } |
3a243403 | 230 | goto end; |
2bdd86d4 | 231 | } |
48842b30 | 232 | |
28ab034a JG |
233 | DBG("Event UST %s %s in channel %s", |
234 | uevent->attr.name, | |
235 | to_create ? "created" : "enabled", | |
236 | uchan->name); | |
7f79d3a1 | 237 | |
f73fabfd | 238 | ret = LTTNG_OK; |
fc34caaa | 239 | |
fb89d070 | 240 | end: |
49d21f93 MD |
241 | free(filter_expression); |
242 | free(filter); | |
243 | free(exclusion); | |
fc34caaa | 244 | return ret; |
2bdd86d4 MD |
245 | } |
246 | ||
7f79d3a1 DG |
247 | /* |
248 | * Disable UST tracepoint of a channel from a UST session. | |
249 | */ | |
7972aab2 | 250 | int event_ust_disable_tracepoint(struct ltt_ust_session *usess, |
28ab034a JG |
251 | struct ltt_ust_channel *uchan, |
252 | const char *event_name) | |
2bdd86d4 MD |
253 | { |
254 | int ret; | |
7f79d3a1 | 255 | struct ltt_ust_event *uevent; |
18eace3b DG |
256 | struct lttng_ht_node_str *node; |
257 | struct lttng_ht_iter iter; | |
18eace3b | 258 | struct lttng_ht *ht; |
2bdd86d4 | 259 | |
a0377dfe FD |
260 | LTTNG_ASSERT(usess); |
261 | LTTNG_ASSERT(uchan); | |
262 | LTTNG_ASSERT(event_name); | |
0525e9ae | 263 | |
18eace3b DG |
264 | ht = uchan->events; |
265 | ||
56047f5a | 266 | lttng::urcu::read_lock_guard read_lock; |
025faf73 DG |
267 | |
268 | /* | |
269 | * We use a custom lookup since we need the iterator for the next_duplicate | |
270 | * call in the do while loop below. | |
271 | */ | |
28ab034a JG |
272 | cds_lfht_lookup(ht->ht, |
273 | ht->hash_fct((void *) event_name, lttng_ht_seed), | |
274 | trace_ust_ht_match_event_by_name, | |
275 | event_name, | |
276 | &iter.iter); | |
00d7d903 | 277 | node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
cd9adb8b | 278 | if (node == nullptr) { |
18eace3b | 279 | DBG2("Trace UST event NOT found by name %s", event_name); |
f73fabfd | 280 | ret = LTTNG_ERR_UST_EVENT_NOT_FOUND; |
7f79d3a1 | 281 | goto error; |
2bdd86d4 | 282 | } |
7f79d3a1 | 283 | |
18eace3b | 284 | do { |
0114db0e | 285 | uevent = lttng::utils::container_of(node, <t_ust_event::node); |
a0377dfe | 286 | LTTNG_ASSERT(uevent); |
025faf73 | 287 | |
66cefebd | 288 | if (!uevent->enabled) { |
18eace3b | 289 | /* It's already disabled so everything is OK */ |
a1dcaf0f | 290 | goto next; |
7f79d3a1 | 291 | } |
66cefebd | 292 | uevent->enabled = false; |
28ab034a | 293 | DBG2("Event UST %s disabled in channel %s", uevent->attr.name, uchan->name); |
18eace3b | 294 | |
88e3c2f5 JG |
295 | if (!usess->active) { |
296 | goto next; | |
297 | } | |
7972aab2 DG |
298 | ret = ust_app_disable_event_glb(usess, uchan, uevent); |
299 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { | |
300 | ret = LTTNG_ERR_UST_DISABLE_FAIL; | |
18eace3b DG |
301 | goto error; |
302 | } | |
28ab034a | 303 | next: |
18eace3b | 304 | /* Get next duplicate event by name. */ |
28ab034a JG |
305 | cds_lfht_next_duplicate( |
306 | ht->ht, trace_ust_ht_match_event_by_name, event_name, &iter.iter); | |
00d7d903 | 307 | node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
18eace3b | 308 | } while (node); |
7f79d3a1 | 309 | |
f73fabfd | 310 | ret = LTTNG_OK; |
7f79d3a1 | 311 | |
7f79d3a1 DG |
312 | error: |
313 | return ret; | |
314 | } | |
315 | ||
b1e7fe2d JR |
316 | /* |
317 | * Disable all UST tracepoints for a channel from a UST session. | |
318 | */ | |
28ab034a | 319 | int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan) |
b1e7fe2d | 320 | { |
3e81c0f2 | 321 | int ret, i, size, error = 0; |
b1e7fe2d | 322 | struct lttng_ht_iter iter; |
cd9adb8b JG |
323 | struct ltt_ust_event *uevent = nullptr; |
324 | struct lttng_event *events = nullptr; | |
b1e7fe2d | 325 | |
a0377dfe FD |
326 | LTTNG_ASSERT(usess); |
327 | LTTNG_ASSERT(uchan); | |
b1e7fe2d | 328 | |
b1e7fe2d | 329 | /* Disabling existing events */ |
56047f5a JG |
330 | { |
331 | lttng::urcu::read_lock_guard read_lock; | |
332 | ||
333 | cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) { | |
334 | if (uevent->enabled) { | |
335 | ret = event_ust_disable_tracepoint(usess, uchan, uevent->attr.name); | |
336 | if (ret < 0) { | |
337 | error = LTTNG_ERR_UST_DISABLE_FAIL; | |
338 | continue; | |
339 | } | |
b1e7fe2d JR |
340 | } |
341 | } | |
342 | } | |
343 | ||
344 | /* Get all UST available events */ | |
345 | size = ust_app_list_events(&events); | |
346 | if (size < 0) { | |
347 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
348 | goto error; | |
349 | } | |
350 | ||
351 | for (i = 0; i < size; i++) { | |
28ab034a | 352 | ret = event_ust_disable_tracepoint(usess, uchan, events[i].name); |
3e81c0f2 | 353 | if (ret < 0) { |
b1e7fe2d | 354 | /* Continue to disable the rest... */ |
3e81c0f2 | 355 | error = LTTNG_ERR_UST_DISABLE_FAIL; |
b1e7fe2d JR |
356 | continue; |
357 | } | |
358 | } | |
359 | ||
3e81c0f2 | 360 | ret = error ? error : LTTNG_OK; |
b1e7fe2d | 361 | error: |
b1e7fe2d JR |
362 | free(events); |
363 | return ret; | |
364 | } | |
365 | ||
44760c20 JR |
366 | static void agent_enable_all(struct agent *agt) |
367 | { | |
368 | struct agent_event *aevent; | |
369 | struct lttng_ht_iter iter; | |
370 | ||
56047f5a JG |
371 | { |
372 | /* Flag every event as enabled. */ | |
373 | lttng::urcu::read_lock_guard read_lock; | |
374 | ||
375 | cds_lfht_for_each_entry (agt->events->ht, &iter.iter, aevent, node.node) { | |
376 | aevent->enabled_count++; | |
377 | } | |
44760c20 | 378 | } |
44760c20 JR |
379 | } |
380 | ||
f20baf8e | 381 | /* |
022d91ba | 382 | * Enable all agent event for a given UST session. |
f20baf8e DG |
383 | * |
384 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
385 | */ | |
022d91ba | 386 | int event_agent_enable_all(struct ltt_ust_session *usess, |
28ab034a JG |
387 | struct agent *agt, |
388 | struct lttng_event *event, | |
389 | struct lttng_bytecode *filter, | |
390 | char *filter_expression) | |
f20baf8e DG |
391 | { |
392 | int ret; | |
f20baf8e | 393 | |
a0377dfe | 394 | LTTNG_ASSERT(usess); |
f20baf8e | 395 | |
022d91ba | 396 | DBG("Event agent enabling ALL events for session %" PRIu64, usess->id); |
f20baf8e | 397 | |
022d91ba | 398 | /* Enable event on agent application through TCP socket. */ |
8404118c | 399 | ret = event_agent_enable(usess, agt, event, filter, filter_expression); |
f20baf8e DG |
400 | if (ret != LTTNG_OK) { |
401 | goto error; | |
402 | } | |
403 | ||
44760c20 | 404 | agent_enable_all(agt); |
f20baf8e DG |
405 | |
406 | ret = LTTNG_OK; | |
407 | ||
408 | error: | |
409 | return ret; | |
410 | } | |
411 | ||
71a559f8 JG |
412 | /* |
413 | * Check if this event's filter requires the activation of application contexts | |
414 | * and enable them in the agent. | |
b4bc01f7 MD |
415 | * TODO: bytecode iterator does not support non-legacy application |
416 | * contexts yet. Not an issue for now, since they are not generated by | |
417 | * the lttng-ctl library. | |
71a559f8 | 418 | */ |
2b00d462 | 419 | static int add_filter_app_ctx(struct lttng_bytecode *bytecode, |
28ab034a JG |
420 | const char *filter_expression, |
421 | struct agent *agt) | |
71a559f8 JG |
422 | { |
423 | int ret = LTTNG_OK; | |
cd9adb8b | 424 | char *provider_name = nullptr, *ctx_name = nullptr; |
28ab034a | 425 | struct bytecode_symbol_iterator *it = bytecode_symbol_iterator_create(bytecode); |
71a559f8 JG |
426 | |
427 | if (!it) { | |
428 | ret = LTTNG_ERR_NOMEM; | |
429 | goto end; | |
430 | } | |
431 | ||
432 | do { | |
433 | struct lttng_event_context ctx; | |
28ab034a | 434 | const char *symbol_name = bytecode_symbol_iterator_get_name(it); |
71a559f8 | 435 | |
28ab034a | 436 | if (parse_application_context(symbol_name, &provider_name, &ctx_name)) { |
71a559f8 JG |
437 | /* Not an application context. */ |
438 | continue; | |
439 | } | |
440 | ||
441 | ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT; | |
442 | ctx.u.app_ctx.provider_name = provider_name; | |
443 | ctx.u.app_ctx.ctx_name = ctx_name; | |
444 | ||
445 | /* Recognized an application context. */ | |
446 | DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.", | |
28ab034a JG |
447 | filter_expression, |
448 | provider_name, | |
449 | ctx_name); | |
71a559f8 JG |
450 | |
451 | ret = agent_add_context(&ctx, agt); | |
452 | if (ret != LTTNG_OK) { | |
28ab034a | 453 | ERR("Failed to add application context %s:%s.", provider_name, ctx_name); |
71a559f8 JG |
454 | goto end; |
455 | } | |
456 | ||
457 | ret = agent_enable_context(&ctx, agt->domain); | |
458 | if (ret != LTTNG_OK) { | |
28ab034a | 459 | ERR("Failed to enable application context %s:%s.", provider_name, ctx_name); |
71a559f8 JG |
460 | goto end; |
461 | } | |
462 | ||
463 | free(provider_name); | |
464 | free(ctx_name); | |
cd9adb8b | 465 | provider_name = ctx_name = nullptr; |
71a559f8 JG |
466 | } while (bytecode_symbol_iterator_next(it) == 0); |
467 | end: | |
468 | free(provider_name); | |
469 | free(ctx_name); | |
470 | bytecode_symbol_iterator_destroy(it); | |
471 | return ret; | |
472 | } | |
473 | ||
44760c20 | 474 | static int agent_enable(struct agent *agt, |
28ab034a JG |
475 | struct lttng_event *event, |
476 | struct lttng_bytecode *filter, | |
477 | char *filter_expression) | |
f20baf8e DG |
478 | { |
479 | int ret, created = 0; | |
fefd409b | 480 | struct agent_event *aevent; |
f20baf8e | 481 | |
a0377dfe FD |
482 | LTTNG_ASSERT(event); |
483 | LTTNG_ASSERT(agt); | |
f20baf8e | 484 | |
56047f5a | 485 | lttng::urcu::read_lock_guard read_lock; |
28ab034a JG |
486 | aevent = agent_find_event( |
487 | event->name, event->loglevel_type, event->loglevel, filter_expression, agt); | |
fefd409b | 488 | if (!aevent) { |
28ab034a JG |
489 | aevent = agent_create_event(event->name, |
490 | event->loglevel_type, | |
491 | event->loglevel, | |
492 | filter, | |
493 | filter_expression); | |
fefd409b | 494 | if (!aevent) { |
f20baf8e DG |
495 | ret = LTTNG_ERR_NOMEM; |
496 | goto error; | |
497 | } | |
cd9adb8b JG |
498 | filter = nullptr; |
499 | filter_expression = nullptr; | |
f20baf8e | 500 | created = 1; |
a0377dfe | 501 | LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(aevent)); |
f20baf8e DG |
502 | } |
503 | ||
bf7886d8 | 504 | if (created && aevent->filter) { |
28ab034a | 505 | ret = add_filter_app_ctx(aevent->filter, aevent->filter_expression, agt); |
71a559f8 JG |
506 | if (ret != LTTNG_OK) { |
507 | goto error; | |
508 | } | |
509 | } | |
510 | ||
88e3c2f5 | 511 | /* Already enabled? */ |
44760c20 | 512 | if (AGENT_EVENT_IS_ENABLED(aevent)) { |
88e3c2f5 JG |
513 | ret = LTTNG_OK; |
514 | goto end; | |
515 | } | |
516 | ||
fefd409b | 517 | ret = agent_enable_event(aevent, agt->domain); |
f20baf8e DG |
518 | if (ret != LTTNG_OK) { |
519 | goto error; | |
520 | } | |
521 | ||
522 | /* If the event was created prior to the enable, add it to the domain. */ | |
523 | if (created) { | |
fefd409b | 524 | agent_add_event(aevent, agt); |
f20baf8e DG |
525 | } |
526 | ||
b0a23296 JG |
527 | ret = LTTNG_OK; |
528 | goto end; | |
f20baf8e DG |
529 | |
530 | error: | |
531 | if (created) { | |
fefd409b | 532 | agent_destroy_event(aevent); |
f20baf8e | 533 | } |
b0a23296 JG |
534 | end: |
535 | free(filter); | |
536 | free(filter_expression); | |
f20baf8e DG |
537 | return ret; |
538 | } | |
539 | ||
44760c20 JR |
540 | /* |
541 | * Enable a single agent event for a given UST session. | |
542 | * | |
543 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
544 | */ | |
545 | int event_agent_enable(struct ltt_ust_session *usess, | |
28ab034a JG |
546 | struct agent *agt, |
547 | struct lttng_event *event, | |
548 | struct lttng_bytecode *filter, | |
549 | char *filter_expression) | |
44760c20 | 550 | { |
a0377dfe FD |
551 | LTTNG_ASSERT(usess); |
552 | LTTNG_ASSERT(event); | |
553 | LTTNG_ASSERT(agt); | |
44760c20 | 554 | |
28ab034a JG |
555 | DBG("Enabling agent event: event pattern = '%s', session id = %" PRIu64 |
556 | ", loglevel type = %d, loglevel = %d, filter expression = '%s'", | |
557 | event->name, | |
558 | usess->id, | |
559 | event->loglevel_type, | |
560 | event->loglevel, | |
561 | filter_expression ? filter_expression : "(none)"); | |
44760c20 JR |
562 | |
563 | return agent_enable(agt, event, filter, filter_expression); | |
564 | } | |
565 | ||
566 | /* | |
567 | * Enable a single agent event for a trigger. | |
568 | * | |
569 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
570 | */ | |
571 | int trigger_agent_enable(const struct lttng_trigger *trigger, struct agent *agt) | |
572 | { | |
573 | int ret; | |
574 | enum lttng_condition_status c_status; | |
575 | enum lttng_trigger_status t_status; | |
576 | enum lttng_domain_type d_type; | |
577 | const struct lttng_condition *condition; | |
578 | const struct lttng_event_rule *rule; | |
579 | const char *filter_expression; | |
cd9adb8b | 580 | char *filter_expression_copy = nullptr; |
2b00d462 | 581 | const struct lttng_bytecode *filter_bytecode; |
cd9adb8b JG |
582 | struct lttng_bytecode *filter_bytecode_copy = nullptr; |
583 | struct lttng_event *event = nullptr; | |
44760c20 JR |
584 | uid_t trigger_owner_uid = 0; |
585 | const char *trigger_name; | |
586 | ||
a0377dfe FD |
587 | LTTNG_ASSERT(trigger); |
588 | LTTNG_ASSERT(agt); | |
44760c20 JR |
589 | |
590 | t_status = lttng_trigger_get_name(trigger, &trigger_name); | |
591 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
0efb2ad7 | 592 | trigger_name = "(anonymous)"; |
44760c20 JR |
593 | } |
594 | ||
595 | t_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner_uid); | |
a0377dfe | 596 | LTTNG_ASSERT(t_status == LTTNG_TRIGGER_STATUS_OK); |
44760c20 JR |
597 | |
598 | condition = lttng_trigger_get_const_condition(trigger); | |
599 | ||
a0377dfe | 600 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
28ab034a | 601 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
44760c20 | 602 | |
28ab034a | 603 | c_status = lttng_condition_event_rule_matches_get_rule(condition, &rule); |
a0377dfe | 604 | LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK); |
44760c20 | 605 | |
695f7044 JR |
606 | switch (lttng_event_rule_get_type(rule)) { |
607 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: | |
608 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: | |
609 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: | |
610 | break; | |
611 | default: | |
612 | abort(); | |
613 | break; | |
614 | } | |
44760c20 JR |
615 | |
616 | d_type = lttng_event_rule_get_domain_type(rule); | |
a0377dfe | 617 | LTTNG_ASSERT(d_type == agt->domain); |
44760c20 JR |
618 | |
619 | event = lttng_event_rule_generate_lttng_event(rule); | |
620 | if (!event) { | |
621 | ret = LTTNG_ERR_NOMEM; | |
622 | goto end; | |
623 | } | |
624 | ||
625 | /* Get the internal filter expression and bytecode. */ | |
626 | filter_expression = lttng_event_rule_get_filter(rule); | |
627 | if (filter_expression) { | |
628 | filter_expression_copy = strdup(filter_expression); | |
629 | if (!filter_expression_copy) { | |
630 | ret = LTTNG_ERR_NOMEM; | |
631 | goto end; | |
632 | } | |
633 | ||
634 | /* Get the filter bytecode */ | |
635 | filter_bytecode = lttng_event_rule_get_filter_bytecode(rule); | |
636 | if (filter_bytecode) { | |
28ab034a | 637 | filter_bytecode_copy = lttng_bytecode_copy(filter_bytecode); |
44760c20 JR |
638 | if (!filter_bytecode_copy) { |
639 | ret = LTTNG_ERR_NOMEM; | |
640 | goto end; | |
641 | } | |
642 | } | |
643 | } | |
644 | ||
645 | DBG("Enabling agent event from trigger: trigger name = '%s', trigger owner uid = %d, token = %" PRIu64, | |
28ab034a JG |
646 | trigger_name, |
647 | trigger_owner_uid, | |
648 | lttng_trigger_get_tracer_token(trigger)); | |
44760c20 | 649 | |
28ab034a | 650 | ret = agent_enable(agt, event, filter_bytecode_copy, filter_expression_copy); |
44760c20 | 651 | /* Ownership was passed even in case of error. */ |
cd9adb8b JG |
652 | filter_expression_copy = nullptr; |
653 | filter_bytecode_copy = nullptr; | |
44760c20 JR |
654 | |
655 | end: | |
656 | free(filter_expression_copy); | |
657 | free(filter_bytecode_copy); | |
658 | free(event); | |
659 | return ret; | |
660 | } | |
661 | ||
da6c3a50 | 662 | /* |
d1ec8e20 JG |
663 | * Return the default event name associated with the provided UST domain. Return |
664 | * NULL on error. | |
da6c3a50 DG |
665 | */ |
666 | const char *event_get_default_agent_ust_name(enum lttng_domain_type domain) | |
667 | { | |
cd9adb8b | 668 | const char *default_event_name = nullptr; |
da6c3a50 | 669 | |
0e115563 DG |
670 | switch (domain) { |
671 | case LTTNG_DOMAIN_LOG4J: | |
d1ec8e20 | 672 | default_event_name = DEFAULT_LOG4J_EVENT_NAME; |
0e115563 DG |
673 | break; |
674 | case LTTNG_DOMAIN_JUL: | |
d1ec8e20 | 675 | default_event_name = DEFAULT_JUL_EVENT_NAME; |
0e115563 DG |
676 | break; |
677 | case LTTNG_DOMAIN_PYTHON: | |
e7c82272 | 678 | default_event_name = DEFAULT_PYTHON_EVENT_NAME; |
0e115563 DG |
679 | break; |
680 | default: | |
a0377dfe | 681 | abort(); |
da6c3a50 DG |
682 | } |
683 | ||
684 | return default_event_name; | |
685 | } | |
686 | ||
44760c20 | 687 | static int trigger_agent_disable_one(const struct lttng_trigger *trigger, |
28ab034a JG |
688 | struct agent *agt, |
689 | struct agent_event *aevent) | |
44760c20 JR |
690 | |
691 | { | |
692 | int ret; | |
693 | ||
a0377dfe FD |
694 | LTTNG_ASSERT(agt); |
695 | LTTNG_ASSERT(trigger); | |
696 | LTTNG_ASSERT(aevent); | |
44760c20 JR |
697 | |
698 | /* | |
699 | * Actual ust event un-registration happens on the trigger | |
700 | * un-registration at that point. | |
701 | */ | |
702 | ||
703 | DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for trigger %" PRIu64, | |
28ab034a JG |
704 | aevent->name, |
705 | aevent->loglevel_type, | |
706 | aevent->loglevel_value, | |
707 | lttng_trigger_get_tracer_token(trigger)); | |
44760c20 JR |
708 | |
709 | /* Already disabled? */ | |
710 | if (!AGENT_EVENT_IS_ENABLED(aevent)) { | |
711 | goto end; | |
712 | } | |
713 | ||
714 | ret = agent_disable_event(aevent, agt->domain); | |
715 | if (ret != LTTNG_OK) { | |
716 | goto error; | |
717 | } | |
718 | ||
719 | end: | |
720 | return LTTNG_OK; | |
721 | ||
722 | error: | |
723 | return ret; | |
724 | } | |
725 | ||
f20baf8e | 726 | /* |
e261a6cc | 727 | * Disable a given agent event for a given UST session. |
f20baf8e | 728 | * |
e261a6cc | 729 | * Must be called with the RCU read lock held. |
f20baf8e DG |
730 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. |
731 | */ | |
e261a6cc | 732 | static int event_agent_disable_one(struct ltt_ust_session *usess, |
28ab034a JG |
733 | struct agent *agt, |
734 | struct agent_event *aevent) | |
f20baf8e DG |
735 | { |
736 | int ret; | |
cd9adb8b JG |
737 | struct ltt_ust_event *uevent = nullptr; |
738 | struct ltt_ust_channel *uchan = nullptr; | |
71aecbf8 | 739 | const char *ust_event_name, *ust_channel_name; |
f20baf8e | 740 | |
a0377dfe FD |
741 | LTTNG_ASSERT(agt); |
742 | LTTNG_ASSERT(usess); | |
743 | LTTNG_ASSERT(aevent); | |
f20baf8e | 744 | |
e261a6cc | 745 | DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64, |
28ab034a JG |
746 | aevent->name, |
747 | aevent->loglevel_type, | |
748 | aevent->loglevel_value, | |
749 | usess->id); | |
f20baf8e DG |
750 | |
751 | /* Already disabled? */ | |
44760c20 | 752 | if (!AGENT_EVENT_IS_ENABLED(aevent)) { |
f20baf8e DG |
753 | goto end; |
754 | } | |
755 | ||
71aecbf8 DG |
756 | if (agt->domain == LTTNG_DOMAIN_JUL) { |
757 | ust_channel_name = DEFAULT_JUL_CHANNEL_NAME; | |
758 | } else if (agt->domain == LTTNG_DOMAIN_LOG4J) { | |
759 | ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME; | |
0e115563 DG |
760 | } else if (agt->domain == LTTNG_DOMAIN_PYTHON) { |
761 | ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
71aecbf8 DG |
762 | } else { |
763 | ret = LTTNG_ERR_INVALID; | |
764 | goto error; | |
765 | } | |
766 | ||
be6a6276 DG |
767 | /* |
768 | * Disable it on the UST side. First get the channel reference then find | |
769 | * the event and finally disable it. | |
770 | */ | |
771 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
28ab034a | 772 | (char *) ust_channel_name); |
be6a6276 DG |
773 | if (!uchan) { |
774 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
775 | goto error; | |
776 | } | |
777 | ||
da6c3a50 DG |
778 | ust_event_name = event_get_default_agent_ust_name(agt->domain); |
779 | if (!ust_event_name) { | |
780 | ret = LTTNG_ERR_FATAL; | |
781 | goto error; | |
be6a6276 DG |
782 | } |
783 | ||
784 | /* | |
b953b8cd PP |
785 | * Agent UST event has its loglevel type forced to |
786 | * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering | |
787 | * happens thanks to an UST filter. The following -1 is actually | |
788 | * ignored since the type is LTTNG_UST_LOGLEVEL_ALL. | |
be6a6276 | 789 | */ |
28ab034a JG |
790 | uevent = trace_ust_find_event(uchan->events, |
791 | (char *) ust_event_name, | |
792 | aevent->filter, | |
793 | LTTNG_UST_ABI_LOGLEVEL_ALL, | |
794 | -1, | |
cd9adb8b | 795 | nullptr); |
022d91ba | 796 | /* If the agent event exists, it must be available on the UST side. */ |
a0377dfe | 797 | LTTNG_ASSERT(uevent); |
be6a6276 | 798 | |
88e3c2f5 JG |
799 | if (usess->active) { |
800 | ret = ust_app_disable_event_glb(usess, uchan, uevent); | |
801 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) { | |
802 | ret = LTTNG_ERR_UST_DISABLE_FAIL; | |
803 | goto error; | |
804 | } | |
be6a6276 DG |
805 | } |
806 | ||
0dcfcf94 DG |
807 | /* |
808 | * Flag event that it's disabled so the shadow copy on the ust app side | |
809 | * will disable it if an application shows up. | |
810 | */ | |
66cefebd | 811 | uevent->enabled = false; |
0dcfcf94 | 812 | |
fefd409b | 813 | ret = agent_disable_event(aevent, agt->domain); |
f20baf8e DG |
814 | if (ret != LTTNG_OK) { |
815 | goto error; | |
816 | } | |
817 | ||
818 | end: | |
819 | return LTTNG_OK; | |
820 | ||
821 | error: | |
822 | return ret; | |
823 | } | |
e261a6cc | 824 | |
44760c20 JR |
825 | /* |
826 | * Disable agent event matching a given trigger. | |
827 | * | |
828 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
829 | */ | |
28ab034a | 830 | int trigger_agent_disable(const struct lttng_trigger *trigger, struct agent *agt) |
44760c20 JR |
831 | { |
832 | int ret = LTTNG_OK; | |
833 | struct agent_event *aevent; | |
834 | ||
a0377dfe FD |
835 | LTTNG_ASSERT(trigger); |
836 | LTTNG_ASSERT(agt); | |
44760c20 | 837 | |
28ab034a | 838 | DBG("Event agent disabling for trigger %" PRIu64, lttng_trigger_get_tracer_token(trigger)); |
44760c20 | 839 | |
56047f5a | 840 | lttng::urcu::read_lock_guard read_lock; |
44760c20 JR |
841 | aevent = agent_find_event_by_trigger(trigger, agt); |
842 | ||
cd9adb8b | 843 | if (aevent == nullptr) { |
44760c20 | 844 | DBG2("Event agent NOT found by trigger %" PRIu64, |
28ab034a | 845 | lttng_trigger_get_tracer_token(trigger)); |
44760c20 JR |
846 | ret = LTTNG_ERR_UST_EVENT_NOT_FOUND; |
847 | goto end; | |
848 | } | |
849 | ||
850 | ret = trigger_agent_disable_one(trigger, agt, aevent); | |
851 | ||
852 | if (ret != LTTNG_OK) { | |
853 | goto end; | |
854 | } | |
855 | ||
856 | end: | |
44760c20 JR |
857 | return ret; |
858 | } | |
859 | ||
e261a6cc PP |
860 | /* |
861 | * Disable all agent events matching a given name for a given UST session. | |
862 | * | |
863 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
864 | */ | |
28ab034a | 865 | int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt, const char *event_name) |
e261a6cc PP |
866 | { |
867 | int ret = LTTNG_OK; | |
868 | struct agent_event *aevent; | |
869 | struct lttng_ht_iter iter; | |
870 | struct lttng_ht_node_str *node; | |
871 | ||
a0377dfe FD |
872 | LTTNG_ASSERT(agt); |
873 | LTTNG_ASSERT(usess); | |
874 | LTTNG_ASSERT(event_name); | |
e261a6cc PP |
875 | |
876 | DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id); | |
877 | ||
56047f5a | 878 | lttng::urcu::read_lock_guard read_lock; |
e261a6cc | 879 | agent_find_events_by_name(event_name, agt, &iter); |
00d7d903 | 880 | node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
e261a6cc | 881 | |
cd9adb8b | 882 | if (node == nullptr) { |
e261a6cc PP |
883 | DBG2("Event agent NOT found by name %s", event_name); |
884 | ret = LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
885 | goto end; | |
886 | } | |
887 | ||
888 | do { | |
0114db0e | 889 | aevent = lttng::utils::container_of(node, &agent_event::node); |
e261a6cc PP |
890 | ret = event_agent_disable_one(usess, agt, aevent); |
891 | ||
892 | if (ret != LTTNG_OK) { | |
893 | goto end; | |
894 | } | |
895 | ||
896 | /* Get next duplicate agent event by name. */ | |
897 | agent_event_next_duplicate(event_name, agt, &iter); | |
00d7d903 | 898 | node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter); |
e261a6cc PP |
899 | } while (node); |
900 | end: | |
e261a6cc PP |
901 | return ret; |
902 | } | |
f20baf8e | 903 | /* |
022d91ba | 904 | * Disable all agent event for a given UST session. |
f20baf8e DG |
905 | * |
906 | * Return LTTNG_OK on success or else a LTTNG_ERR* code. | |
907 | */ | |
28ab034a | 908 | int event_agent_disable_all(struct ltt_ust_session *usess, struct agent *agt) |
f20baf8e | 909 | { |
0dcfcf94 | 910 | int ret; |
fefd409b | 911 | struct agent_event *aevent; |
f20baf8e DG |
912 | struct lttng_ht_iter iter; |
913 | ||
a0377dfe FD |
914 | LTTNG_ASSERT(agt); |
915 | LTTNG_ASSERT(usess); | |
f20baf8e | 916 | |
0dcfcf94 DG |
917 | /* |
918 | * Disable event on agent application. Continue to disable all other events | |
919 | * if the * event is not found. | |
920 | */ | |
fefd409b | 921 | ret = event_agent_disable(usess, agt, "*"); |
0dcfcf94 DG |
922 | if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) { |
923 | goto error; | |
f20baf8e DG |
924 | } |
925 | ||
b9866e08 | 926 | /* Disable every event. */ |
56047f5a JG |
927 | { |
928 | lttng::urcu::read_lock_guard read_lock; | |
0dcfcf94 | 929 | |
56047f5a JG |
930 | cds_lfht_for_each_entry (agt->events->ht, &iter.iter, aevent, node.node) { |
931 | if (!AGENT_EVENT_IS_ENABLED(aevent)) { | |
932 | continue; | |
933 | } | |
934 | ||
935 | ret = event_agent_disable(usess, agt, aevent->name); | |
936 | if (ret != LTTNG_OK) { | |
937 | goto error_unlock; | |
938 | } | |
f20baf8e | 939 | } |
f20baf8e | 940 | } |
56047f5a | 941 | |
f20baf8e DG |
942 | ret = LTTNG_OK; |
943 | ||
b9866e08 | 944 | error_unlock: |
f20baf8e DG |
945 | error: |
946 | return ret; | |
947 | } |