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