sessiond: clean-up: silence warning that agent event is leaked
[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 * 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 */
43 static 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 */
71 int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
72 const 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 */
109 int 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, filter_expression, filter);
123 /* We have passed ownership */
124 filter_expression = NULL;
125 filter = NULL;
126 if (ret) {
127 goto end;
128 }
129 } else if (kevent->enabled == 0) {
130 ret = kernel_enable_event(kevent);
131 if (ret < 0) {
132 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
133 goto end;
134 }
135 } else {
136 /* At this point, the event is considered enabled */
137 ret = LTTNG_ERR_KERN_EVENT_EXIST;
138 goto end;
139 }
140
141 ret = LTTNG_OK;
142 end:
143 free(filter_expression);
144 free(filter);
145 return ret;
146 }
147
148 /*
149 * ============================
150 * UST : The Ultimate Frontier!
151 * ============================
152 */
153
154 /*
155 * Enable UST tracepoint event for a channel from a UST session.
156 * We own filter_expression, filter, and exclusion.
157 */
158 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
159 struct ltt_ust_channel *uchan, struct lttng_event *event,
160 char *filter_expression,
161 struct lttng_filter_bytecode *filter,
162 struct lttng_event_exclusion *exclusion,
163 bool internal_event)
164 {
165 int ret = LTTNG_OK, to_create = 0;
166 struct ltt_ust_event *uevent;
167
168 assert(usess);
169 assert(uchan);
170 assert(event);
171
172 rcu_read_lock();
173
174 uevent = trace_ust_find_event(uchan->events, event->name, filter,
175 (enum lttng_ust_loglevel_type) event->loglevel_type,
176 event->loglevel, exclusion);
177 if (!uevent) {
178 ret = trace_ust_create_event(event, filter_expression,
179 filter, exclusion, internal_event, &uevent);
180 /* We have passed ownership */
181 filter_expression = NULL;
182 filter = NULL;
183 exclusion = NULL;
184 if (ret != LTTNG_OK) {
185 goto error;
186 }
187
188 /* Valid to set it after the goto error since uevent is still NULL */
189 to_create = 1;
190 }
191
192 if (uevent->enabled) {
193 /* It's already enabled so everything is OK */
194 assert(!to_create);
195 ret = LTTNG_ERR_UST_EVENT_ENABLED;
196 goto end;
197 }
198
199 uevent->enabled = 1;
200 if (to_create) {
201 /* Add ltt ust event to channel */
202 add_unique_ust_event(uchan->events, uevent);
203 }
204
205 if (!usess->active) {
206 goto end;
207 }
208
209 if (to_create) {
210 /* Create event on all UST registered apps for session */
211 ret = ust_app_create_event_glb(usess, uchan, uevent);
212 } else {
213 /* Enable event on all UST registered apps for session */
214 ret = ust_app_enable_event_glb(usess, uchan, uevent);
215 }
216
217 if (ret < 0) {
218 if (ret == -LTTNG_UST_ERR_EXIST) {
219 ret = LTTNG_ERR_UST_EVENT_EXIST;
220 goto end;
221 } else {
222 ret = LTTNG_ERR_UST_ENABLE_FAIL;
223 goto error;
224 }
225 }
226
227 DBG("Event UST %s %s in channel %s", uevent->attr.name,
228 to_create ? "created" : "enabled", uchan->name);
229
230 ret = LTTNG_OK;
231
232 end:
233 rcu_read_unlock();
234 free(filter_expression);
235 free(filter);
236 free(exclusion);
237 return ret;
238
239 error:
240 /*
241 * Only destroy event on creation time (not enabling time) because if the
242 * event is found in the channel (to_create == 0), it means that at some
243 * point the enable_event worked and it's thus valid to keep it alive.
244 * Destroying it also implies that we also destroy it's shadow copy to sync
245 * everyone up.
246 */
247 if (to_create) {
248 /* In this code path, the uevent was not added to the hash table */
249 trace_ust_destroy_event(uevent);
250 }
251 rcu_read_unlock();
252 free(filter_expression);
253 free(filter);
254 free(exclusion);
255 return ret;
256 }
257
258 /*
259 * Disable UST tracepoint of a channel from a UST session.
260 */
261 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
262 struct ltt_ust_channel *uchan, const char *event_name)
263 {
264 int ret;
265 struct ltt_ust_event *uevent;
266 struct lttng_ht_node_str *node;
267 struct lttng_ht_iter iter;
268 struct lttng_ht *ht;
269
270 assert(usess);
271 assert(uchan);
272 assert(event_name);
273
274 ht = uchan->events;
275
276 rcu_read_lock();
277
278 /*
279 * We use a custom lookup since we need the iterator for the next_duplicate
280 * call in the do while loop below.
281 */
282 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
283 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
284 node = lttng_ht_iter_get_node_str(&iter);
285 if (node == NULL) {
286 DBG2("Trace UST event NOT found by name %s", event_name);
287 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
288 goto error;
289 }
290
291 do {
292 uevent = caa_container_of(node, struct ltt_ust_event, node);
293 assert(uevent);
294
295 if (uevent->enabled == 0) {
296 /* It's already disabled so everything is OK */
297 goto next;
298 }
299 uevent->enabled = 0;
300 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
301 uchan->name);
302
303 if (!usess->active) {
304 goto next;
305 }
306 ret = ust_app_disable_event_glb(usess, uchan, uevent);
307 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
308 ret = LTTNG_ERR_UST_DISABLE_FAIL;
309 goto error;
310 }
311 next:
312 /* Get next duplicate event by name. */
313 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
314 event_name, &iter.iter);
315 node = lttng_ht_iter_get_node_str(&iter);
316 } while (node);
317
318 ret = LTTNG_OK;
319
320 error:
321 rcu_read_unlock();
322 return ret;
323 }
324
325 /*
326 * Disable all UST tracepoints for a channel from a UST session.
327 */
328 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
329 struct ltt_ust_channel *uchan)
330 {
331 int ret, i, size, error = 0;
332 struct lttng_ht_iter iter;
333 struct ltt_ust_event *uevent = NULL;
334 struct lttng_event *events = NULL;
335
336 assert(usess);
337 assert(uchan);
338
339 rcu_read_lock();
340
341 /* Disabling existing events */
342 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
343 node.node) {
344 if (uevent->enabled == 1) {
345 ret = event_ust_disable_tracepoint(usess, uchan,
346 uevent->attr.name);
347 if (ret < 0) {
348 error = LTTNG_ERR_UST_DISABLE_FAIL;
349 continue;
350 }
351 }
352 }
353
354 /* Get all UST available events */
355 size = ust_app_list_events(&events);
356 if (size < 0) {
357 ret = LTTNG_ERR_UST_LIST_FAIL;
358 goto error;
359 }
360
361 for (i = 0; i < size; i++) {
362 ret = event_ust_disable_tracepoint(usess, uchan,
363 events[i].name);
364 if (ret < 0) {
365 /* Continue to disable the rest... */
366 error = LTTNG_ERR_UST_DISABLE_FAIL;
367 continue;
368 }
369 }
370
371 ret = error ? error : LTTNG_OK;
372 error:
373 rcu_read_unlock();
374 free(events);
375 return ret;
376 }
377
378 /*
379 * Enable all agent event for a given UST session.
380 *
381 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
382 */
383 int event_agent_enable_all(struct ltt_ust_session *usess,
384 struct agent *agt, struct lttng_event *event,
385 struct lttng_filter_bytecode *filter ,char *filter_expression)
386 {
387 int ret;
388 struct agent_event *aevent;
389 struct lttng_ht_iter iter;
390
391 assert(usess);
392
393 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
394
395 /* Enable event on agent application through TCP socket. */
396 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
397 if (ret != LTTNG_OK) {
398 goto error;
399 }
400
401 /* Flag every event that they are now enabled. */
402 rcu_read_lock();
403 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
404 node.node) {
405 aevent->enabled = 1;
406 }
407 rcu_read_unlock();
408
409 ret = LTTNG_OK;
410
411 error:
412 return ret;
413 }
414
415 /*
416 * Check if this event's filter requires the activation of application contexts
417 * and enable them in the agent.
418 * TODO: bytecode iterator does not support non-legacy application
419 * contexts yet. Not an issue for now, since they are not generated by
420 * the lttng-ctl library.
421 */
422 static int add_filter_app_ctx(struct lttng_filter_bytecode *bytecode,
423 const char *filter_expression, struct agent *agt)
424 {
425 int ret = LTTNG_OK;
426 char *provider_name = NULL, *ctx_name = NULL;
427 struct bytecode_symbol_iterator *it =
428 bytecode_symbol_iterator_create(bytecode);
429
430 if (!it) {
431 ret = LTTNG_ERR_NOMEM;
432 goto end;
433 }
434
435 do {
436 struct lttng_event_context ctx;
437 const char *symbol_name =
438 bytecode_symbol_iterator_get_name(it);
439
440 if (parse_application_context(symbol_name, &provider_name,
441 &ctx_name)) {
442 /* Not an application context. */
443 continue;
444 }
445
446 ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
447 ctx.u.app_ctx.provider_name = provider_name;
448 ctx.u.app_ctx.ctx_name = ctx_name;
449
450 /* Recognized an application context. */
451 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
452 filter_expression, provider_name, ctx_name);
453
454 ret = agent_add_context(&ctx, agt);
455 if (ret != LTTNG_OK) {
456 ERR("Failed to add application context %s:%s.",
457 provider_name, ctx_name);
458 goto end;
459 }
460
461 ret = agent_enable_context(&ctx, agt->domain);
462 if (ret != LTTNG_OK) {
463 ERR("Failed to enable application context %s:%s.",
464 provider_name, ctx_name);
465 goto end;
466 }
467
468 free(provider_name);
469 free(ctx_name);
470 provider_name = ctx_name = NULL;
471 } while (bytecode_symbol_iterator_next(it) == 0);
472 end:
473 free(provider_name);
474 free(ctx_name);
475 bytecode_symbol_iterator_destroy(it);
476 return ret;
477 }
478
479 /*
480 * Enable a single agent event for a given UST session.
481 *
482 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
483 */
484 int event_agent_enable(struct ltt_ust_session *usess,
485 struct agent *agt, struct lttng_event *event,
486 struct lttng_filter_bytecode *filter,
487 char *filter_expression)
488 {
489 int ret, created = 0;
490 struct agent_event *aevent;
491
492 assert(usess);
493 assert(event);
494 assert(agt);
495
496 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
497 ", loglevel %d and filter \"%s\"", event->name,
498 usess->id, event->loglevel_type, event->loglevel,
499 filter_expression ? filter_expression : "NULL");
500
501 aevent = agent_find_event(event->name, event->loglevel_type,
502 event->loglevel, filter_expression, agt);
503 if (!aevent) {
504 aevent = agent_create_event(event->name, event->loglevel_type,
505 event->loglevel, filter,
506 filter_expression);
507 if (!aevent) {
508 ret = LTTNG_ERR_NOMEM;
509 goto error;
510 }
511 filter = NULL;
512 filter_expression = NULL;
513 created = 1;
514 assert(!aevent->enabled);
515 }
516
517 if (created && aevent->filter) {
518 ret = add_filter_app_ctx(
519 aevent->filter, aevent->filter_expression, agt);
520 if (ret != LTTNG_OK) {
521 goto error;
522 }
523 }
524
525 /* Already enabled? */
526 if (aevent->enabled) {
527 ret = LTTNG_OK;
528 goto end;
529 }
530
531 ret = agent_enable_event(aevent, agt->domain);
532 if (ret != LTTNG_OK) {
533 goto error;
534 }
535
536 /* If the event was created prior to the enable, add it to the domain. */
537 if (created) {
538 agent_add_event(aevent, agt);
539 }
540
541 ret = LTTNG_OK;
542 goto end;
543
544 error:
545 if (created) {
546 agent_destroy_event(aevent);
547 }
548 end:
549 free(filter);
550 free(filter_expression);
551 return ret;
552 }
553
554 /*
555 * Return the default event name associated with the provided UST domain. Return
556 * NULL on error.
557 */
558 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
559 {
560 const char *default_event_name = NULL;
561
562 switch (domain) {
563 case LTTNG_DOMAIN_LOG4J:
564 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
565 break;
566 case LTTNG_DOMAIN_JUL:
567 default_event_name = DEFAULT_JUL_EVENT_NAME;
568 break;
569 case LTTNG_DOMAIN_PYTHON:
570 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
571 break;
572 default:
573 assert(0);
574 }
575
576 return default_event_name;
577 }
578
579 /*
580 * Disable a given agent event for a given UST session.
581 *
582 * Must be called with the RCU read lock held.
583 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
584 */
585 static int event_agent_disable_one(struct ltt_ust_session *usess,
586 struct agent *agt, struct agent_event *aevent)
587 {
588 int ret;
589 struct ltt_ust_event *uevent = NULL;
590 struct ltt_ust_channel *uchan = NULL;
591 const char *ust_event_name, *ust_channel_name;
592
593 assert(agt);
594 assert(usess);
595 assert(aevent);
596
597 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
598 aevent->name, aevent->loglevel_type, aevent->loglevel_value,
599 usess->id);
600
601 /* Already disabled? */
602 if (!aevent->enabled) {
603 goto end;
604 }
605
606 if (agt->domain == LTTNG_DOMAIN_JUL) {
607 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
608 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
609 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
610 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
611 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
612 } else {
613 ret = LTTNG_ERR_INVALID;
614 goto error;
615 }
616
617 /*
618 * Disable it on the UST side. First get the channel reference then find
619 * the event and finally disable it.
620 */
621 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
622 (char *) ust_channel_name);
623 if (!uchan) {
624 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
625 goto error;
626 }
627
628 ust_event_name = event_get_default_agent_ust_name(agt->domain);
629 if (!ust_event_name) {
630 ret = LTTNG_ERR_FATAL;
631 goto error;
632 }
633
634 /*
635 * Agent UST event has its loglevel type forced to
636 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
637 * happens thanks to an UST filter. The following -1 is actually
638 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
639 */
640 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
641 aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
642 /* If the agent event exists, it must be available on the UST side. */
643 assert(uevent);
644
645 if (usess->active) {
646 ret = ust_app_disable_event_glb(usess, uchan, uevent);
647 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
648 ret = LTTNG_ERR_UST_DISABLE_FAIL;
649 goto error;
650 }
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
664 end:
665 return LTTNG_OK;
666
667 error:
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 */
676 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
677 const 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);
712 end:
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 */
721 int 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
755 error_unlock:
756 rcu_read_unlock();
757 error:
758 return ret;
759 }
This page took 0.045262 seconds and 6 git commands to generate.