189236beb1171a05ba21e89b3ecb51216a7b5e47
[lttng-tools.git] / src / bin / lttng-sessiond / event.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <errno.h>
11 #include <urcu/list.h>
12 #include <string.h>
13
14 #include <lttng/lttng.h>
15 #include <common/error.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/filter.h>
18 #include <common/context.h>
19
20 #include "channel.h"
21 #include "event.h"
22 #include "kernel.h"
23 #include "lttng-sessiond.h"
24 #include "lttng-ust-ctl.h"
25 #include "lttng-ust-error.h"
26 #include "ust-app.h"
27 #include "trace-kernel.h"
28 #include "trace-ust.h"
29 #include "agent.h"
30
31 /*
32 * Add unique UST event based on the event name, filter bytecode and loglevel.
33 */
34 static void add_unique_ust_event(struct lttng_ht *ht,
35 struct ltt_ust_event *event)
36 {
37 struct cds_lfht_node *node_ptr;
38 struct ltt_ust_ht_key key;
39
40 assert(ht);
41 assert(ht->ht);
42 assert(event);
43
44 key.name = event->attr.name;
45 key.filter = (struct lttng_filter_bytecode *) event->filter;
46 key.loglevel_type = event->attr.loglevel_type;
47 key.loglevel_value = event->attr.loglevel;
48 key.exclusion = event->exclusion;
49
50 node_ptr = cds_lfht_add_unique(ht->ht,
51 ht->hash_fct(event->node.key, lttng_ht_seed),
52 trace_ust_ht_match_event, &key, &event->node.node);
53 assert(node_ptr == &event->node.node);
54 }
55
56 /*
57 * Disable kernel tracepoint events for a channel from the kernel session of
58 * a specified event_name and event type.
59 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
60 * If event_name is NULL all events of the specified type are disabled.
61 */
62 int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
63 const char *event_name, enum lttng_event_type type)
64 {
65 int ret, error = 0, found = 0;
66 struct ltt_kernel_event *kevent;
67
68 assert(kchan);
69
70 /* For each event in the kernel session */
71 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
72 if (type != LTTNG_EVENT_ALL && kevent->type != type)
73 continue;
74 if (event_name != NULL && strcmp(event_name, kevent->event->name)) {
75 continue;
76 }
77 found++;
78 ret = kernel_disable_event(kevent);
79 if (ret < 0) {
80 error = 1;
81 continue;
82 }
83 }
84 DBG("Disable kernel event: found %d events with name: %s and type: %d",
85 found, event_name ? event_name : "NULL", type);
86
87 if (event_name != NULL && !found) {
88 ret = LTTNG_ERR_NO_EVENT;
89 } else {
90 ret = error ? LTTNG_ERR_KERN_DISABLE_FAIL : LTTNG_OK;
91 }
92
93 return ret;
94 }
95
96 /*
97 * Enable kernel tracepoint event for a channel from the kernel session.
98 * We own filter_expression and filter.
99 */
100 int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
101 struct lttng_event *event, char *filter_expression,
102 struct lttng_filter_bytecode *filter)
103 {
104 int ret;
105 struct ltt_kernel_event *kevent;
106
107 assert(kchan);
108 assert(event);
109
110 kevent = trace_kernel_find_event(event->name, kchan,
111 event->type, filter);
112 if (kevent == NULL) {
113 ret = kernel_create_event(event, kchan, filter_expression, filter);
114 /* We have passed ownership */
115 filter_expression = NULL;
116 filter = NULL;
117 if (ret) {
118 goto end;
119 }
120 } else if (kevent->enabled == 0) {
121 ret = kernel_enable_event(kevent);
122 if (ret < 0) {
123 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
124 goto end;
125 }
126 } else {
127 /* At this point, the event is considered enabled */
128 ret = LTTNG_ERR_KERN_EVENT_EXIST;
129 goto end;
130 }
131
132 ret = LTTNG_OK;
133 end:
134 free(filter_expression);
135 free(filter);
136 return ret;
137 }
138
139 /*
140 * ============================
141 * UST : The Ultimate Frontier!
142 * ============================
143 */
144
145 /*
146 * Enable UST tracepoint event for a channel from a UST session.
147 * We own filter_expression, filter, and exclusion.
148 */
149 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
150 struct ltt_ust_channel *uchan, struct lttng_event *event,
151 char *filter_expression,
152 struct lttng_filter_bytecode *filter,
153 struct lttng_event_exclusion *exclusion,
154 bool internal_event)
155 {
156 int ret = LTTNG_OK, to_create = 0;
157 struct ltt_ust_event *uevent;
158
159 assert(usess);
160 assert(uchan);
161 assert(event);
162
163 rcu_read_lock();
164
165 uevent = trace_ust_find_event(uchan->events, event->name, filter,
166 (enum lttng_ust_loglevel_type) event->loglevel_type,
167 event->loglevel, exclusion);
168 if (!uevent) {
169 ret = trace_ust_create_event(event, filter_expression,
170 filter, exclusion, internal_event, &uevent);
171 /* We have passed ownership */
172 filter_expression = NULL;
173 filter = NULL;
174 exclusion = NULL;
175 if (ret != LTTNG_OK) {
176 goto error;
177 }
178
179 /* Valid to set it after the goto error since uevent is still NULL */
180 to_create = 1;
181 }
182
183 if (uevent->enabled) {
184 /* It's already enabled so everything is OK */
185 assert(!to_create);
186 ret = LTTNG_ERR_UST_EVENT_ENABLED;
187 goto end;
188 }
189
190 uevent->enabled = 1;
191 if (to_create) {
192 /* Add ltt ust event to channel */
193 add_unique_ust_event(uchan->events, uevent);
194 }
195
196 if (!usess->active) {
197 goto end;
198 }
199
200 if (to_create) {
201 /* Create event on all UST registered apps for session */
202 ret = ust_app_create_event_glb(usess, uchan, uevent);
203 } else {
204 /* Enable event on all UST registered apps for session */
205 ret = ust_app_enable_event_glb(usess, uchan, uevent);
206 }
207
208 if (ret < 0) {
209 if (ret == -LTTNG_UST_ERR_EXIST) {
210 ret = LTTNG_ERR_UST_EVENT_EXIST;
211 goto end;
212 } else {
213 ret = LTTNG_ERR_UST_ENABLE_FAIL;
214 goto error;
215 }
216 }
217
218 DBG("Event UST %s %s in channel %s", uevent->attr.name,
219 to_create ? "created" : "enabled", uchan->name);
220
221 ret = LTTNG_OK;
222
223 end:
224 rcu_read_unlock();
225 free(filter_expression);
226 free(filter);
227 free(exclusion);
228 return ret;
229
230 error:
231 /*
232 * Only destroy event on creation time (not enabling time) because if the
233 * event is found in the channel (to_create == 0), it means that at some
234 * point the enable_event worked and it's thus valid to keep it alive.
235 * Destroying it also implies that we also destroy it's shadow copy to sync
236 * everyone up.
237 */
238 if (to_create) {
239 /* In this code path, the uevent was not added to the hash table */
240 trace_ust_destroy_event(uevent);
241 }
242 rcu_read_unlock();
243 free(filter_expression);
244 free(filter);
245 free(exclusion);
246 return ret;
247 }
248
249 /*
250 * Disable UST tracepoint of a channel from a UST session.
251 */
252 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
253 struct ltt_ust_channel *uchan, const char *event_name)
254 {
255 int ret;
256 struct ltt_ust_event *uevent;
257 struct lttng_ht_node_str *node;
258 struct lttng_ht_iter iter;
259 struct lttng_ht *ht;
260
261 assert(usess);
262 assert(uchan);
263 assert(event_name);
264
265 ht = uchan->events;
266
267 rcu_read_lock();
268
269 /*
270 * We use a custom lookup since we need the iterator for the next_duplicate
271 * call in the do while loop below.
272 */
273 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
274 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
275 node = lttng_ht_iter_get_node_str(&iter);
276 if (node == NULL) {
277 DBG2("Trace UST event NOT found by name %s", event_name);
278 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
279 goto error;
280 }
281
282 do {
283 uevent = caa_container_of(node, struct ltt_ust_event, node);
284 assert(uevent);
285
286 if (uevent->enabled == 0) {
287 /* It's already disabled so everything is OK */
288 goto next;
289 }
290 uevent->enabled = 0;
291 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
292 uchan->name);
293
294 if (!usess->active) {
295 goto next;
296 }
297 ret = ust_app_disable_event_glb(usess, uchan, uevent);
298 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
299 ret = LTTNG_ERR_UST_DISABLE_FAIL;
300 goto error;
301 }
302 next:
303 /* Get next duplicate event by name. */
304 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
305 event_name, &iter.iter);
306 node = lttng_ht_iter_get_node_str(&iter);
307 } while (node);
308
309 ret = LTTNG_OK;
310
311 error:
312 rcu_read_unlock();
313 return ret;
314 }
315
316 /*
317 * Disable all UST tracepoints for a channel from a UST session.
318 */
319 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
320 struct ltt_ust_channel *uchan)
321 {
322 int ret, i, size, error = 0;
323 struct lttng_ht_iter iter;
324 struct ltt_ust_event *uevent = NULL;
325 struct lttng_event *events = NULL;
326
327 assert(usess);
328 assert(uchan);
329
330 rcu_read_lock();
331
332 /* Disabling existing events */
333 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
334 node.node) {
335 if (uevent->enabled == 1) {
336 ret = event_ust_disable_tracepoint(usess, uchan,
337 uevent->attr.name);
338 if (ret < 0) {
339 error = LTTNG_ERR_UST_DISABLE_FAIL;
340 continue;
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++) {
353 ret = event_ust_disable_tracepoint(usess, uchan,
354 events[i].name);
355 if (ret < 0) {
356 /* Continue to disable the rest... */
357 error = LTTNG_ERR_UST_DISABLE_FAIL;
358 continue;
359 }
360 }
361
362 ret = error ? error : LTTNG_OK;
363 error:
364 rcu_read_unlock();
365 free(events);
366 return ret;
367 }
368
369 /*
370 * Enable all agent event for a given UST session.
371 *
372 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
373 */
374 int event_agent_enable_all(struct ltt_ust_session *usess,
375 struct agent *agt, struct lttng_event *event,
376 struct lttng_filter_bytecode *filter ,char *filter_expression)
377 {
378 int ret;
379 struct agent_event *aevent;
380 struct lttng_ht_iter iter;
381
382 assert(usess);
383
384 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
385
386 /* Enable event on agent application through TCP socket. */
387 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
388 if (ret != LTTNG_OK) {
389 goto error;
390 }
391
392 /* Flag every event that they are now enabled. */
393 rcu_read_lock();
394 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
395 node.node) {
396 aevent->enabled = 1;
397 }
398 rcu_read_unlock();
399
400 ret = LTTNG_OK;
401
402 error:
403 return ret;
404 }
405
406 /*
407 * Check if this event's filter requires the activation of application contexts
408 * and enable them in the agent.
409 * TODO: bytecode iterator does not support non-legacy application
410 * contexts yet. Not an issue for now, since they are not generated by
411 * the lttng-ctl library.
412 */
413 static int add_filter_app_ctx(struct lttng_filter_bytecode *bytecode,
414 const char *filter_expression, struct agent *agt)
415 {
416 int ret = LTTNG_OK;
417 char *provider_name = NULL, *ctx_name = NULL;
418 struct bytecode_symbol_iterator *it =
419 bytecode_symbol_iterator_create(bytecode);
420
421 if (!it) {
422 ret = LTTNG_ERR_NOMEM;
423 goto end;
424 }
425
426 do {
427 struct lttng_event_context ctx;
428 const char *symbol_name =
429 bytecode_symbol_iterator_get_name(it);
430
431 if (parse_application_context(symbol_name, &provider_name,
432 &ctx_name)) {
433 /* Not an application context. */
434 continue;
435 }
436
437 ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
438 ctx.u.app_ctx.provider_name = provider_name;
439 ctx.u.app_ctx.ctx_name = ctx_name;
440
441 /* Recognized an application context. */
442 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
443 filter_expression, provider_name, ctx_name);
444
445 ret = agent_add_context(&ctx, agt);
446 if (ret != LTTNG_OK) {
447 ERR("Failed to add application context %s:%s.",
448 provider_name, ctx_name);
449 goto end;
450 }
451
452 ret = agent_enable_context(&ctx, agt->domain);
453 if (ret != LTTNG_OK) {
454 ERR("Failed to enable application context %s:%s.",
455 provider_name, ctx_name);
456 goto end;
457 }
458
459 free(provider_name);
460 free(ctx_name);
461 provider_name = ctx_name = NULL;
462 } while (bytecode_symbol_iterator_next(it) == 0);
463 end:
464 free(provider_name);
465 free(ctx_name);
466 bytecode_symbol_iterator_destroy(it);
467 return ret;
468 }
469
470 /*
471 * Enable a single agent event for a given UST session.
472 *
473 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
474 */
475 int event_agent_enable(struct ltt_ust_session *usess,
476 struct agent *agt, struct lttng_event *event,
477 struct lttng_filter_bytecode *filter,
478 char *filter_expression)
479 {
480 int ret, created = 0;
481 struct agent_event *aevent;
482
483 assert(usess);
484 assert(event);
485 assert(agt);
486
487 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
488 ", loglevel %d and filter \"%s\"", event->name,
489 usess->id, event->loglevel_type, event->loglevel,
490 filter_expression ? filter_expression : "NULL");
491
492 aevent = agent_find_event(event->name, event->loglevel_type,
493 event->loglevel, filter_expression, agt);
494 if (!aevent) {
495 aevent = agent_create_event(event->name, event->loglevel_type,
496 event->loglevel, filter,
497 filter_expression);
498 if (!aevent) {
499 ret = LTTNG_ERR_NOMEM;
500 goto error;
501 }
502 filter = NULL;
503 filter_expression = NULL;
504 created = 1;
505 assert(!aevent->enabled);
506 }
507
508 if (created && aevent->filter) {
509 ret = add_filter_app_ctx(
510 aevent->filter, aevent->filter_expression, agt);
511 if (ret != LTTNG_OK) {
512 goto error;
513 }
514 }
515
516 /* Already enabled? */
517 if (aevent->enabled) {
518 ret = LTTNG_OK;
519 goto end;
520 }
521
522 ret = agent_enable_event(aevent, agt->domain);
523 if (ret != LTTNG_OK) {
524 goto error;
525 }
526
527 /* If the event was created prior to the enable, add it to the domain. */
528 if (created) {
529 agent_add_event(aevent, agt);
530 }
531
532 ret = LTTNG_OK;
533 goto end;
534
535 error:
536 if (created) {
537 agent_destroy_event(aevent);
538 }
539 end:
540 free(filter);
541 free(filter_expression);
542 return ret;
543 }
544
545 /*
546 * Return the default event name associated with the provided UST domain. Return
547 * NULL on error.
548 */
549 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
550 {
551 const char *default_event_name = NULL;
552
553 switch (domain) {
554 case LTTNG_DOMAIN_LOG4J:
555 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
556 break;
557 case LTTNG_DOMAIN_JUL:
558 default_event_name = DEFAULT_JUL_EVENT_NAME;
559 break;
560 case LTTNG_DOMAIN_PYTHON:
561 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
562 break;
563 default:
564 assert(0);
565 }
566
567 return default_event_name;
568 }
569
570 /*
571 * Disable a given agent event for a given UST session.
572 *
573 * Must be called with the RCU read lock held.
574 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
575 */
576 static int event_agent_disable_one(struct ltt_ust_session *usess,
577 struct agent *agt, struct agent_event *aevent)
578 {
579 int ret;
580 struct ltt_ust_event *uevent = NULL;
581 struct ltt_ust_channel *uchan = NULL;
582 const char *ust_event_name, *ust_channel_name;
583
584 assert(agt);
585 assert(usess);
586 assert(aevent);
587
588 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
589 aevent->name, aevent->loglevel_type, aevent->loglevel_value,
590 usess->id);
591
592 /* Already disabled? */
593 if (!aevent->enabled) {
594 goto end;
595 }
596
597 if (agt->domain == LTTNG_DOMAIN_JUL) {
598 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
599 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
600 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
601 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
602 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
603 } else {
604 ret = LTTNG_ERR_INVALID;
605 goto error;
606 }
607
608 /*
609 * Disable it on the UST side. First get the channel reference then find
610 * the event and finally disable it.
611 */
612 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
613 (char *) ust_channel_name);
614 if (!uchan) {
615 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
616 goto error;
617 }
618
619 ust_event_name = event_get_default_agent_ust_name(agt->domain);
620 if (!ust_event_name) {
621 ret = LTTNG_ERR_FATAL;
622 goto error;
623 }
624
625 /*
626 * Agent UST event has its loglevel type forced to
627 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
628 * happens thanks to an UST filter. The following -1 is actually
629 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
630 */
631 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
632 aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
633 /* If the agent event exists, it must be available on the UST side. */
634 assert(uevent);
635
636 if (usess->active) {
637 ret = ust_app_disable_event_glb(usess, uchan, uevent);
638 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
639 ret = LTTNG_ERR_UST_DISABLE_FAIL;
640 goto error;
641 }
642 }
643
644 /*
645 * Flag event that it's disabled so the shadow copy on the ust app side
646 * will disable it if an application shows up.
647 */
648 uevent->enabled = 0;
649
650 ret = agent_disable_event(aevent, agt->domain);
651 if (ret != LTTNG_OK) {
652 goto error;
653 }
654
655 end:
656 return LTTNG_OK;
657
658 error:
659 return ret;
660 }
661
662 /*
663 * Disable all agent events matching a given name for a given UST session.
664 *
665 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
666 */
667 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
668 const char *event_name)
669 {
670 int ret = LTTNG_OK;
671 struct agent_event *aevent;
672 struct lttng_ht_iter iter;
673 struct lttng_ht_node_str *node;
674
675 assert(agt);
676 assert(usess);
677 assert(event_name);
678
679 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
680
681 rcu_read_lock();
682 agent_find_events_by_name(event_name, agt, &iter);
683 node = lttng_ht_iter_get_node_str(&iter);
684
685 if (node == NULL) {
686 DBG2("Event agent NOT found by name %s", event_name);
687 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
688 goto end;
689 }
690
691 do {
692 aevent = caa_container_of(node, struct agent_event, node);
693 ret = event_agent_disable_one(usess, agt, aevent);
694
695 if (ret != LTTNG_OK) {
696 goto end;
697 }
698
699 /* Get next duplicate agent event by name. */
700 agent_event_next_duplicate(event_name, agt, &iter);
701 node = lttng_ht_iter_get_node_str(&iter);
702 } while (node);
703 end:
704 rcu_read_unlock();
705 return ret;
706 }
707 /*
708 * Disable all agent event for a given UST session.
709 *
710 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
711 */
712 int event_agent_disable_all(struct ltt_ust_session *usess,
713 struct agent *agt)
714 {
715 int ret;
716 struct agent_event *aevent;
717 struct lttng_ht_iter iter;
718
719 assert(agt);
720 assert(usess);
721
722 /*
723 * Disable event on agent application. Continue to disable all other events
724 * if the * event is not found.
725 */
726 ret = event_agent_disable(usess, agt, "*");
727 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
728 goto error;
729 }
730
731 /* Disable every event. */
732 rcu_read_lock();
733 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
734 node.node) {
735 if (!aevent->enabled) {
736 continue;
737 }
738
739 ret = event_agent_disable(usess, agt, aevent->name);
740 if (ret != LTTNG_OK) {
741 goto error_unlock;
742 }
743 }
744 ret = LTTNG_OK;
745
746 error_unlock:
747 rcu_read_unlock();
748 error:
749 return ret;
750 }
This page took 0.071941 seconds and 4 git commands to generate.