Fix: file-descriptor: missing include guards
[lttng-tools.git] / src / bin / lttng-sessiond / event.cpp
CommitLineData
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 42static 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 71int 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 112int 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 145end:
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 161int 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 240end:
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 250int 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);
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, &ltt_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);
18eace3b
DG
307 node = lttng_ht_iter_get_node_str(&iter);
308 } while (node);
7f79d3a1 309
f73fabfd 310 ret = LTTNG_OK;
7f79d3a1 311
7f79d3a1
DG
312error:
313 return ret;
314}
315
b1e7fe2d
JR
316/*
317 * Disable all UST tracepoints for a channel from a UST session.
318 */
28ab034a 319int 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
JR
329
330 /* Disabling existing events */
56047f5a
JG
331 {
332 lttng::urcu::read_lock_guard read_lock;
333
334 cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) {
335 if (uevent->enabled) {
336 ret = event_ust_disable_tracepoint(usess, uchan, uevent->attr.name);
337 if (ret < 0) {
338 error = LTTNG_ERR_UST_DISABLE_FAIL;
339 continue;
340 }
b1e7fe2d
JR
341 }
342 }
343 }
344
345 /* Get all UST available events */
346 size = ust_app_list_events(&events);
347 if (size < 0) {
348 ret = LTTNG_ERR_UST_LIST_FAIL;
349 goto error;
350 }
351
352 for (i = 0; i < size; i++) {
28ab034a 353 ret = event_ust_disable_tracepoint(usess, uchan, events[i].name);
3e81c0f2 354 if (ret < 0) {
b1e7fe2d 355 /* Continue to disable the rest... */
3e81c0f2 356 error = LTTNG_ERR_UST_DISABLE_FAIL;
b1e7fe2d
JR
357 continue;
358 }
359 }
360
3e81c0f2 361 ret = error ? error : LTTNG_OK;
b1e7fe2d 362error:
b1e7fe2d
JR
363 free(events);
364 return ret;
365}
366
44760c20
JR
367static void agent_enable_all(struct agent *agt)
368{
369 struct agent_event *aevent;
370 struct lttng_ht_iter iter;
371
56047f5a
JG
372 {
373 /* Flag every event as enabled. */
374 lttng::urcu::read_lock_guard read_lock;
375
376 cds_lfht_for_each_entry (agt->events->ht, &iter.iter, aevent, node.node) {
377 aevent->enabled_count++;
378 }
44760c20 379 }
44760c20
JR
380}
381
f20baf8e 382/*
022d91ba 383 * Enable all agent event for a given UST session.
f20baf8e
DG
384 *
385 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
386 */
022d91ba 387int event_agent_enable_all(struct ltt_ust_session *usess,
28ab034a
JG
388 struct agent *agt,
389 struct lttng_event *event,
390 struct lttng_bytecode *filter,
391 char *filter_expression)
f20baf8e
DG
392{
393 int ret;
f20baf8e 394
a0377dfe 395 LTTNG_ASSERT(usess);
f20baf8e 396
022d91ba 397 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
f20baf8e 398
022d91ba 399 /* Enable event on agent application through TCP socket. */
8404118c 400 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
f20baf8e
DG
401 if (ret != LTTNG_OK) {
402 goto error;
403 }
404
44760c20 405 agent_enable_all(agt);
f20baf8e
DG
406
407 ret = LTTNG_OK;
408
409error:
410 return ret;
411}
412
71a559f8
JG
413/*
414 * Check if this event's filter requires the activation of application contexts
415 * and enable them in the agent.
b4bc01f7
MD
416 * TODO: bytecode iterator does not support non-legacy application
417 * contexts yet. Not an issue for now, since they are not generated by
418 * the lttng-ctl library.
71a559f8 419 */
2b00d462 420static int add_filter_app_ctx(struct lttng_bytecode *bytecode,
28ab034a
JG
421 const char *filter_expression,
422 struct agent *agt)
71a559f8
JG
423{
424 int ret = LTTNG_OK;
cd9adb8b 425 char *provider_name = nullptr, *ctx_name = nullptr;
28ab034a 426 struct bytecode_symbol_iterator *it = bytecode_symbol_iterator_create(bytecode);
71a559f8
JG
427
428 if (!it) {
429 ret = LTTNG_ERR_NOMEM;
430 goto end;
431 }
432
433 do {
434 struct lttng_event_context ctx;
28ab034a 435 const char *symbol_name = bytecode_symbol_iterator_get_name(it);
71a559f8 436
28ab034a 437 if (parse_application_context(symbol_name, &provider_name, &ctx_name)) {
71a559f8
JG
438 /* Not an application context. */
439 continue;
440 }
441
442 ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
443 ctx.u.app_ctx.provider_name = provider_name;
444 ctx.u.app_ctx.ctx_name = ctx_name;
445
446 /* Recognized an application context. */
447 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
28ab034a
JG
448 filter_expression,
449 provider_name,
450 ctx_name);
71a559f8
JG
451
452 ret = agent_add_context(&ctx, agt);
453 if (ret != LTTNG_OK) {
28ab034a 454 ERR("Failed to add application context %s:%s.", provider_name, ctx_name);
71a559f8
JG
455 goto end;
456 }
457
458 ret = agent_enable_context(&ctx, agt->domain);
459 if (ret != LTTNG_OK) {
28ab034a 460 ERR("Failed to enable application context %s:%s.", provider_name, ctx_name);
71a559f8
JG
461 goto end;
462 }
463
464 free(provider_name);
465 free(ctx_name);
cd9adb8b 466 provider_name = ctx_name = nullptr;
71a559f8
JG
467 } while (bytecode_symbol_iterator_next(it) == 0);
468end:
469 free(provider_name);
470 free(ctx_name);
471 bytecode_symbol_iterator_destroy(it);
472 return ret;
473}
474
44760c20 475static int agent_enable(struct agent *agt,
28ab034a
JG
476 struct lttng_event *event,
477 struct lttng_bytecode *filter,
478 char *filter_expression)
f20baf8e
DG
479{
480 int ret, created = 0;
fefd409b 481 struct agent_event *aevent;
f20baf8e 482
a0377dfe
FD
483 LTTNG_ASSERT(event);
484 LTTNG_ASSERT(agt);
f20baf8e 485
56047f5a 486 lttng::urcu::read_lock_guard read_lock;
28ab034a
JG
487 aevent = agent_find_event(
488 event->name, event->loglevel_type, event->loglevel, filter_expression, agt);
fefd409b 489 if (!aevent) {
28ab034a
JG
490 aevent = agent_create_event(event->name,
491 event->loglevel_type,
492 event->loglevel,
493 filter,
494 filter_expression);
fefd409b 495 if (!aevent) {
f20baf8e
DG
496 ret = LTTNG_ERR_NOMEM;
497 goto error;
498 }
cd9adb8b
JG
499 filter = nullptr;
500 filter_expression = nullptr;
f20baf8e 501 created = 1;
a0377dfe 502 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(aevent));
f20baf8e
DG
503 }
504
bf7886d8 505 if (created && aevent->filter) {
28ab034a 506 ret = add_filter_app_ctx(aevent->filter, aevent->filter_expression, agt);
71a559f8
JG
507 if (ret != LTTNG_OK) {
508 goto error;
509 }
510 }
511
88e3c2f5 512 /* Already enabled? */
44760c20 513 if (AGENT_EVENT_IS_ENABLED(aevent)) {
88e3c2f5
JG
514 ret = LTTNG_OK;
515 goto end;
516 }
517
fefd409b 518 ret = agent_enable_event(aevent, agt->domain);
f20baf8e
DG
519 if (ret != LTTNG_OK) {
520 goto error;
521 }
522
523 /* If the event was created prior to the enable, add it to the domain. */
524 if (created) {
fefd409b 525 agent_add_event(aevent, agt);
f20baf8e
DG
526 }
527
b0a23296
JG
528 ret = LTTNG_OK;
529 goto end;
f20baf8e
DG
530
531error:
532 if (created) {
fefd409b 533 agent_destroy_event(aevent);
f20baf8e 534 }
b0a23296
JG
535end:
536 free(filter);
537 free(filter_expression);
f20baf8e
DG
538 return ret;
539}
540
44760c20
JR
541/*
542 * Enable a single agent event for a given UST session.
543 *
544 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
545 */
546int event_agent_enable(struct ltt_ust_session *usess,
28ab034a
JG
547 struct agent *agt,
548 struct lttng_event *event,
549 struct lttng_bytecode *filter,
550 char *filter_expression)
44760c20 551{
a0377dfe
FD
552 LTTNG_ASSERT(usess);
553 LTTNG_ASSERT(event);
554 LTTNG_ASSERT(agt);
44760c20 555
28ab034a
JG
556 DBG("Enabling agent event: event pattern = '%s', session id = %" PRIu64
557 ", loglevel type = %d, loglevel = %d, filter expression = '%s'",
558 event->name,
559 usess->id,
560 event->loglevel_type,
561 event->loglevel,
562 filter_expression ? filter_expression : "(none)");
44760c20
JR
563
564 return agent_enable(agt, event, filter, filter_expression);
565}
566
567/*
568 * Enable a single agent event for a trigger.
569 *
570 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
571 */
572int trigger_agent_enable(const struct lttng_trigger *trigger, struct agent *agt)
573{
574 int ret;
575 enum lttng_condition_status c_status;
576 enum lttng_trigger_status t_status;
577 enum lttng_domain_type d_type;
578 const struct lttng_condition *condition;
579 const struct lttng_event_rule *rule;
580 const char *filter_expression;
cd9adb8b 581 char *filter_expression_copy = nullptr;
2b00d462 582 const struct lttng_bytecode *filter_bytecode;
cd9adb8b
JG
583 struct lttng_bytecode *filter_bytecode_copy = nullptr;
584 struct lttng_event *event = nullptr;
44760c20
JR
585 uid_t trigger_owner_uid = 0;
586 const char *trigger_name;
587
a0377dfe
FD
588 LTTNG_ASSERT(trigger);
589 LTTNG_ASSERT(agt);
44760c20
JR
590
591 t_status = lttng_trigger_get_name(trigger, &trigger_name);
592 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
0efb2ad7 593 trigger_name = "(anonymous)";
44760c20
JR
594 }
595
596 t_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner_uid);
a0377dfe 597 LTTNG_ASSERT(t_status == LTTNG_TRIGGER_STATUS_OK);
44760c20
JR
598
599 condition = lttng_trigger_get_const_condition(trigger);
600
a0377dfe 601 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
28ab034a 602 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
44760c20 603
28ab034a 604 c_status = lttng_condition_event_rule_matches_get_rule(condition, &rule);
a0377dfe 605 LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK);
44760c20 606
695f7044
JR
607 switch (lttng_event_rule_get_type(rule)) {
608 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
609 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
610 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
611 break;
612 default:
613 abort();
614 break;
615 }
44760c20
JR
616
617 d_type = lttng_event_rule_get_domain_type(rule);
a0377dfe 618 LTTNG_ASSERT(d_type == agt->domain);
44760c20
JR
619
620 event = lttng_event_rule_generate_lttng_event(rule);
621 if (!event) {
622 ret = LTTNG_ERR_NOMEM;
623 goto end;
624 }
625
626 /* Get the internal filter expression and bytecode. */
627 filter_expression = lttng_event_rule_get_filter(rule);
628 if (filter_expression) {
629 filter_expression_copy = strdup(filter_expression);
630 if (!filter_expression_copy) {
631 ret = LTTNG_ERR_NOMEM;
632 goto end;
633 }
634
635 /* Get the filter bytecode */
636 filter_bytecode = lttng_event_rule_get_filter_bytecode(rule);
637 if (filter_bytecode) {
28ab034a 638 filter_bytecode_copy = lttng_bytecode_copy(filter_bytecode);
44760c20
JR
639 if (!filter_bytecode_copy) {
640 ret = LTTNG_ERR_NOMEM;
641 goto end;
642 }
643 }
644 }
645
646 DBG("Enabling agent event from trigger: trigger name = '%s', trigger owner uid = %d, token = %" PRIu64,
28ab034a
JG
647 trigger_name,
648 trigger_owner_uid,
649 lttng_trigger_get_tracer_token(trigger));
44760c20 650
28ab034a 651 ret = agent_enable(agt, event, filter_bytecode_copy, filter_expression_copy);
44760c20 652 /* Ownership was passed even in case of error. */
cd9adb8b
JG
653 filter_expression_copy = nullptr;
654 filter_bytecode_copy = nullptr;
44760c20
JR
655
656end:
657 free(filter_expression_copy);
658 free(filter_bytecode_copy);
659 free(event);
660 return ret;
661}
662
da6c3a50 663/*
d1ec8e20
JG
664 * Return the default event name associated with the provided UST domain. Return
665 * NULL on error.
da6c3a50
DG
666 */
667const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
668{
cd9adb8b 669 const char *default_event_name = nullptr;
da6c3a50 670
0e115563
DG
671 switch (domain) {
672 case LTTNG_DOMAIN_LOG4J:
d1ec8e20 673 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
0e115563
DG
674 break;
675 case LTTNG_DOMAIN_JUL:
d1ec8e20 676 default_event_name = DEFAULT_JUL_EVENT_NAME;
0e115563
DG
677 break;
678 case LTTNG_DOMAIN_PYTHON:
e7c82272 679 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
0e115563
DG
680 break;
681 default:
a0377dfe 682 abort();
da6c3a50
DG
683 }
684
685 return default_event_name;
686}
687
44760c20 688static int trigger_agent_disable_one(const struct lttng_trigger *trigger,
28ab034a
JG
689 struct agent *agt,
690 struct agent_event *aevent)
44760c20
JR
691
692{
693 int ret;
694
a0377dfe
FD
695 LTTNG_ASSERT(agt);
696 LTTNG_ASSERT(trigger);
697 LTTNG_ASSERT(aevent);
44760c20
JR
698
699 /*
700 * Actual ust event un-registration happens on the trigger
701 * un-registration at that point.
702 */
703
704 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for trigger %" PRIu64,
28ab034a
JG
705 aevent->name,
706 aevent->loglevel_type,
707 aevent->loglevel_value,
708 lttng_trigger_get_tracer_token(trigger));
44760c20
JR
709
710 /* Already disabled? */
711 if (!AGENT_EVENT_IS_ENABLED(aevent)) {
712 goto end;
713 }
714
715 ret = agent_disable_event(aevent, agt->domain);
716 if (ret != LTTNG_OK) {
717 goto error;
718 }
719
720end:
721 return LTTNG_OK;
722
723error:
724 return ret;
725}
726
f20baf8e 727/*
e261a6cc 728 * Disable a given agent event for a given UST session.
f20baf8e 729 *
e261a6cc 730 * Must be called with the RCU read lock held.
f20baf8e
DG
731 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
732 */
e261a6cc 733static int event_agent_disable_one(struct ltt_ust_session *usess,
28ab034a
JG
734 struct agent *agt,
735 struct agent_event *aevent)
f20baf8e
DG
736{
737 int ret;
cd9adb8b
JG
738 struct ltt_ust_event *uevent = nullptr;
739 struct ltt_ust_channel *uchan = nullptr;
71aecbf8 740 const char *ust_event_name, *ust_channel_name;
f20baf8e 741
a0377dfe
FD
742 LTTNG_ASSERT(agt);
743 LTTNG_ASSERT(usess);
744 LTTNG_ASSERT(aevent);
f20baf8e 745
e261a6cc 746 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
28ab034a
JG
747 aevent->name,
748 aevent->loglevel_type,
749 aevent->loglevel_value,
750 usess->id);
f20baf8e
DG
751
752 /* Already disabled? */
44760c20 753 if (!AGENT_EVENT_IS_ENABLED(aevent)) {
f20baf8e
DG
754 goto end;
755 }
756
71aecbf8
DG
757 if (agt->domain == LTTNG_DOMAIN_JUL) {
758 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
759 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
760 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
761 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
762 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
71aecbf8
DG
763 } else {
764 ret = LTTNG_ERR_INVALID;
765 goto error;
766 }
767
be6a6276
DG
768 /*
769 * Disable it on the UST side. First get the channel reference then find
770 * the event and finally disable it.
771 */
772 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
28ab034a 773 (char *) ust_channel_name);
be6a6276
DG
774 if (!uchan) {
775 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
776 goto error;
777 }
778
da6c3a50
DG
779 ust_event_name = event_get_default_agent_ust_name(agt->domain);
780 if (!ust_event_name) {
781 ret = LTTNG_ERR_FATAL;
782 goto error;
be6a6276
DG
783 }
784
785 /*
b953b8cd
PP
786 * Agent UST event has its loglevel type forced to
787 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
788 * happens thanks to an UST filter. The following -1 is actually
789 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
be6a6276 790 */
28ab034a
JG
791 uevent = trace_ust_find_event(uchan->events,
792 (char *) ust_event_name,
793 aevent->filter,
794 LTTNG_UST_ABI_LOGLEVEL_ALL,
795 -1,
cd9adb8b 796 nullptr);
022d91ba 797 /* If the agent event exists, it must be available on the UST side. */
a0377dfe 798 LTTNG_ASSERT(uevent);
be6a6276 799
88e3c2f5
JG
800 if (usess->active) {
801 ret = ust_app_disable_event_glb(usess, uchan, uevent);
802 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
803 ret = LTTNG_ERR_UST_DISABLE_FAIL;
804 goto error;
805 }
be6a6276
DG
806 }
807
0dcfcf94
DG
808 /*
809 * Flag event that it's disabled so the shadow copy on the ust app side
810 * will disable it if an application shows up.
811 */
66cefebd 812 uevent->enabled = false;
0dcfcf94 813
fefd409b 814 ret = agent_disable_event(aevent, agt->domain);
f20baf8e
DG
815 if (ret != LTTNG_OK) {
816 goto error;
817 }
818
819end:
820 return LTTNG_OK;
821
822error:
823 return ret;
824}
e261a6cc 825
44760c20
JR
826/*
827 * Disable agent event matching a given trigger.
828 *
829 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
830 */
28ab034a 831int trigger_agent_disable(const struct lttng_trigger *trigger, struct agent *agt)
44760c20
JR
832{
833 int ret = LTTNG_OK;
834 struct agent_event *aevent;
835
a0377dfe
FD
836 LTTNG_ASSERT(trigger);
837 LTTNG_ASSERT(agt);
44760c20 838
28ab034a 839 DBG("Event agent disabling for trigger %" PRIu64, lttng_trigger_get_tracer_token(trigger));
44760c20 840
56047f5a 841 lttng::urcu::read_lock_guard read_lock;
44760c20
JR
842 aevent = agent_find_event_by_trigger(trigger, agt);
843
cd9adb8b 844 if (aevent == nullptr) {
44760c20 845 DBG2("Event agent NOT found by trigger %" PRIu64,
28ab034a 846 lttng_trigger_get_tracer_token(trigger));
44760c20
JR
847 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
848 goto end;
849 }
850
851 ret = trigger_agent_disable_one(trigger, agt, aevent);
852
853 if (ret != LTTNG_OK) {
854 goto end;
855 }
856
857end:
44760c20
JR
858 return ret;
859}
860
e261a6cc
PP
861/*
862 * Disable all agent events matching a given name for a given UST session.
863 *
864 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
865 */
28ab034a 866int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt, const char *event_name)
e261a6cc
PP
867{
868 int ret = LTTNG_OK;
869 struct agent_event *aevent;
870 struct lttng_ht_iter iter;
871 struct lttng_ht_node_str *node;
872
a0377dfe
FD
873 LTTNG_ASSERT(agt);
874 LTTNG_ASSERT(usess);
875 LTTNG_ASSERT(event_name);
e261a6cc
PP
876
877 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
878
56047f5a 879 lttng::urcu::read_lock_guard read_lock;
e261a6cc
PP
880 agent_find_events_by_name(event_name, agt, &iter);
881 node = lttng_ht_iter_get_node_str(&iter);
882
cd9adb8b 883 if (node == nullptr) {
e261a6cc
PP
884 DBG2("Event agent NOT found by name %s", event_name);
885 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
886 goto end;
887 }
888
889 do {
0114db0e 890 aevent = lttng::utils::container_of(node, &agent_event::node);
e261a6cc
PP
891 ret = event_agent_disable_one(usess, agt, aevent);
892
893 if (ret != LTTNG_OK) {
894 goto end;
895 }
896
897 /* Get next duplicate agent event by name. */
898 agent_event_next_duplicate(event_name, agt, &iter);
899 node = lttng_ht_iter_get_node_str(&iter);
900 } while (node);
901end:
e261a6cc
PP
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 909int 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. */
56047f5a
JG
928 {
929 lttng::urcu::read_lock_guard read_lock;
0dcfcf94 930
56047f5a
JG
931 cds_lfht_for_each_entry (agt->events->ht, &iter.iter, aevent, node.node) {
932 if (!AGENT_EVENT_IS_ENABLED(aevent)) {
933 continue;
934 }
935
936 ret = event_agent_disable(usess, agt, aevent->name);
937 if (ret != LTTNG_OK) {
938 goto error_unlock;
939 }
f20baf8e 940 }
f20baf8e 941 }
56047f5a 942
f20baf8e
DG
943 ret = LTTNG_OK;
944
b9866e08 945error_unlock:
f20baf8e
DG
946error:
947 return ret;
948}
This page took 0.137669 seconds and 4 git commands to generate.