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