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