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