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