Implement kernel filter support
[lttng-tools.git] / src / bin / lttng-sessiond / event.c
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 */
40 static 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 */
64 int 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
90 error:
91 return ret;
92 }
93
94 /*
95 * Disable kernel tracepoint events for a channel from the kernel session.
96 */
97 int 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 */
122 int 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 */
131 int 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 if (ret < 0) {
147 switch (-ret) {
148 case EEXIST:
149 ret = LTTNG_ERR_KERN_EVENT_EXIST;
150 break;
151 case ENOSYS:
152 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
153 break;
154 default:
155 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
156 break;
157 }
158 goto end;
159 }
160 } else if (kevent->enabled == 0) {
161 ret = kernel_enable_event(kevent);
162 if (ret < 0) {
163 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
164 goto end;
165 }
166 } else {
167 /* At this point, the event is considered enabled */
168 ret = LTTNG_ERR_KERN_EVENT_EXIST;
169 goto end;
170 }
171
172 ret = LTTNG_OK;
173 end:
174 return ret;
175 }
176
177 /*
178 * ============================
179 * UST : The Ultimate Frontier!
180 * ============================
181 */
182
183 /*
184 * Enable all UST tracepoints for a channel from a UST session.
185 */
186 int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess,
187 struct ltt_ust_channel *uchan,
188 char *filter_expression,
189 struct lttng_filter_bytecode *filter)
190 {
191 int ret, i, size;
192 struct lttng_ht_iter iter;
193 struct ltt_ust_event *uevent = NULL;
194 struct lttng_event *events = NULL;
195
196 assert(usess);
197 assert(uchan);
198
199 rcu_read_lock();
200
201 /* Enable existing events */
202 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
203 node.node) {
204 if (uevent->enabled == 0) {
205 ret = ust_app_enable_event_glb(usess, uchan, uevent);
206 if (ret < 0) {
207 continue;
208 }
209 uevent->enabled = 1;
210 }
211 }
212
213 /* Get all UST available events */
214 size = ust_app_list_events(&events);
215 if (size < 0) {
216 ret = LTTNG_ERR_UST_LIST_FAIL;
217 goto error;
218 }
219
220 for (i = 0; i < size; i++) {
221 /*
222 * Check if event exist and if so, continue since it was enable
223 * previously.
224 */
225 uevent = trace_ust_find_event(uchan->events, events[i].name, filter,
226 events[i].loglevel, NULL);
227 if (uevent != NULL) {
228 ret = ust_app_enable_event_pid(usess, uchan, uevent,
229 events[i].pid);
230 if (ret < 0) {
231 if (ret != -LTTNG_UST_ERR_EXIST) {
232 ret = LTTNG_ERR_UST_ENABLE_FAIL;
233 goto error;
234 }
235 }
236 continue;
237 }
238
239 /* Create ust event */
240 uevent = trace_ust_create_event(&events[i], filter_expression,
241 filter, NULL);
242 if (uevent == NULL) {
243 ret = LTTNG_ERR_FATAL;
244 goto error_destroy;
245 }
246
247 /* Create event for the specific PID */
248 ret = ust_app_enable_event_pid(usess, uchan, uevent,
249 events[i].pid);
250 if (ret < 0) {
251 if (ret == -LTTNG_UST_ERR_EXIST) {
252 ret = LTTNG_ERR_UST_EVENT_EXIST;
253 goto error;
254 } else {
255 ret = LTTNG_ERR_UST_ENABLE_FAIL;
256 goto error_destroy;
257 }
258 }
259
260 uevent->enabled = 1;
261 /* Add ltt ust event to channel */
262 rcu_read_lock();
263 add_unique_ust_event(uchan->events, uevent);
264 rcu_read_unlock();
265 }
266 free(events);
267
268 rcu_read_unlock();
269 return LTTNG_OK;
270
271 error_destroy:
272 trace_ust_destroy_event(uevent);
273
274 error:
275 free(events);
276 rcu_read_unlock();
277 return ret;
278 }
279
280 /*
281 * Enable UST tracepoint event for a channel from a UST session.
282 * We own filter_expression, filter, and exclusion.
283 */
284 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
285 struct ltt_ust_channel *uchan, struct lttng_event *event,
286 char *filter_expression,
287 struct lttng_filter_bytecode *filter,
288 struct lttng_event_exclusion *exclusion)
289 {
290 int ret = LTTNG_OK, to_create = 0;
291 struct ltt_ust_event *uevent;
292
293 assert(usess);
294 assert(uchan);
295 assert(event);
296
297 rcu_read_lock();
298
299 uevent = trace_ust_find_event(uchan->events, event->name, filter,
300 event->loglevel, exclusion);
301 if (uevent == NULL) {
302 uevent = trace_ust_create_event(event, filter_expression,
303 filter, exclusion);
304 /* We have passed ownership */
305 filter_expression = NULL;
306 filter = NULL;
307 exclusion = NULL;
308 if (uevent == NULL) {
309 ret = LTTNG_ERR_UST_ENABLE_FAIL;
310 goto error;
311 }
312
313 /* Valid to set it after the goto error since uevent is still NULL */
314 to_create = 1;
315 }
316
317 if (uevent->enabled) {
318 /* It's already enabled so everything is OK */
319 ret = LTTNG_ERR_UST_EVENT_ENABLED;
320 goto end;
321 }
322
323 uevent->enabled = 1;
324
325 if (to_create) {
326 /* Create event on all UST registered apps for session */
327 ret = ust_app_create_event_glb(usess, uchan, uevent);
328 } else {
329 /* Enable event on all UST registered apps for session */
330 ret = ust_app_enable_event_glb(usess, uchan, uevent);
331 }
332
333 if (ret < 0) {
334 if (ret == -LTTNG_UST_ERR_EXIST) {
335 ret = LTTNG_ERR_UST_EVENT_EXIST;
336 goto end;
337 } else {
338 ret = LTTNG_ERR_UST_ENABLE_FAIL;
339 goto error;
340 }
341 }
342
343 if (to_create) {
344 /* Add ltt ust event to channel */
345 add_unique_ust_event(uchan->events, uevent);
346 }
347
348 DBG("Event UST %s %s in channel %s", uevent->attr.name,
349 to_create ? "created" : "enabled", uchan->name);
350
351 ret = LTTNG_OK;
352
353 end:
354 rcu_read_unlock();
355 free(filter_expression);
356 free(filter);
357 free(exclusion);
358 return ret;
359
360 error:
361 /*
362 * Only destroy event on creation time (not enabling time) because if the
363 * event is found in the channel (to_create == 0), it means that at some
364 * point the enable_event worked and it's thus valid to keep it alive.
365 * Destroying it also implies that we also destroy it's shadow copy to sync
366 * everyone up.
367 */
368 if (to_create) {
369 /* In this code path, the uevent was not added to the hash table */
370 trace_ust_destroy_event(uevent);
371 }
372 rcu_read_unlock();
373 free(filter_expression);
374 free(filter);
375 free(exclusion);
376 return ret;
377 }
378
379 /*
380 * Disable UST tracepoint of a channel from a UST session.
381 */
382 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
383 struct ltt_ust_channel *uchan, char *event_name)
384 {
385 int ret;
386 struct ltt_ust_event *uevent;
387 struct lttng_ht_node_str *node;
388 struct lttng_ht_iter iter;
389 struct lttng_ht *ht;
390
391 assert(usess);
392 assert(uchan);
393 assert(event_name);
394
395 ht = uchan->events;
396
397 rcu_read_lock();
398
399 /*
400 * We use a custom lookup since we need the iterator for the next_duplicate
401 * call in the do while loop below.
402 */
403 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
404 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
405 node = lttng_ht_iter_get_node_str(&iter);
406 if (node == NULL) {
407 DBG2("Trace UST event NOT found by name %s", event_name);
408 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
409 goto error;
410 }
411
412 do {
413 uevent = caa_container_of(node, struct ltt_ust_event, node);
414 assert(uevent);
415
416 if (uevent->enabled == 0) {
417 /* It's already disabled so everything is OK */
418 goto next;
419 }
420
421 ret = ust_app_disable_event_glb(usess, uchan, uevent);
422 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
423 ret = LTTNG_ERR_UST_DISABLE_FAIL;
424 goto error;
425 }
426 uevent->enabled = 0;
427
428 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
429 uchan->name);
430
431 next:
432 /* Get next duplicate event by name. */
433 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
434 event_name, &iter.iter);
435 node = lttng_ht_iter_get_node_str(&iter);
436 } while (node);
437
438 ret = LTTNG_OK;
439
440 error:
441 rcu_read_unlock();
442 return ret;
443 }
444
445 /*
446 * Disable all UST tracepoints for a channel from a UST session.
447 */
448 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
449 struct ltt_ust_channel *uchan)
450 {
451 int ret, i, size;
452 struct lttng_ht_iter iter;
453 struct ltt_ust_event *uevent = NULL;
454 struct lttng_event *events = NULL;
455
456 assert(usess);
457 assert(uchan);
458
459 rcu_read_lock();
460
461 /* Disabling existing events */
462 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
463 node.node) {
464 if (uevent->enabled == 1) {
465 ret = event_ust_disable_tracepoint(usess, uchan,
466 uevent->attr.name);
467 if (ret < 0) {
468 continue;
469 }
470 }
471 }
472
473 /* Get all UST available events */
474 size = ust_app_list_events(&events);
475 if (size < 0) {
476 ret = LTTNG_ERR_UST_LIST_FAIL;
477 goto error;
478 }
479
480 for (i = 0; i < size; i++) {
481 ret = event_ust_disable_tracepoint(usess, uchan,
482 events[i].name);
483 if (ret != LTTNG_OK) {
484 /* Continue to disable the rest... */
485 continue;
486 }
487 }
488 free(events);
489
490 rcu_read_unlock();
491 return LTTNG_OK;
492
493 error:
494 free(events);
495 rcu_read_unlock();
496 return ret;
497 }
498
499 /*
500 * Enable all agent event for a given UST session.
501 *
502 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
503 */
504 int event_agent_enable_all(struct ltt_ust_session *usess,
505 struct agent *agt, struct lttng_event *event,
506 struct lttng_filter_bytecode *filter)
507 {
508 int ret;
509 struct agent_event *aevent;
510 struct lttng_ht_iter iter;
511
512 assert(usess);
513
514 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
515
516 /* Enable event on agent application through TCP socket. */
517 ret = event_agent_enable(usess, agt, event, filter);
518 if (ret != LTTNG_OK) {
519 goto error;
520 }
521
522 /* Flag every event that they are now enabled. */
523 rcu_read_lock();
524 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
525 node.node) {
526 aevent->enabled = 1;
527 }
528 rcu_read_unlock();
529
530 ret = LTTNG_OK;
531
532 error:
533 return ret;
534 }
535
536 /*
537 * Enable a single agent event for a given UST session.
538 *
539 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
540 */
541 int event_agent_enable(struct ltt_ust_session *usess,
542 struct agent *agt, struct lttng_event *event,
543 struct lttng_filter_bytecode *filter)
544 {
545 int ret, created = 0;
546 struct agent_event *aevent;
547
548 assert(usess);
549 assert(event);
550 assert(agt);
551
552 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
553 "and loglevel %d", event->name, usess->id, event->loglevel_type,
554 event->loglevel);
555
556 aevent = agent_find_event(event->name, event->loglevel, agt);
557 if (!aevent) {
558 aevent = agent_create_event(event->name, filter);
559 if (!aevent) {
560 ret = LTTNG_ERR_NOMEM;
561 goto error;
562 }
563 aevent->loglevel = event->loglevel;
564 aevent->loglevel_type = event->loglevel_type;
565 created = 1;
566 }
567
568 /* Already enabled? */
569 if (aevent->enabled) {
570 goto end;
571 }
572
573 ret = agent_enable_event(aevent, agt->domain);
574 if (ret != LTTNG_OK) {
575 goto error;
576 }
577
578 /* If the event was created prior to the enable, add it to the domain. */
579 if (created) {
580 agent_add_event(aevent, agt);
581 }
582
583 end:
584 return LTTNG_OK;
585
586 error:
587 if (created) {
588 agent_destroy_event(aevent);
589 }
590 return ret;
591 }
592
593 /*
594 * Return the agent default event name to use by testing if the process is root
595 * or not. Return NULL on error.
596 */
597 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
598 {
599 const char *default_event_name = NULL;
600
601 switch (domain) {
602 case LTTNG_DOMAIN_LOG4J:
603 if (is_root) {
604 default_event_name = DEFAULT_SYS_LOG4J_EVENT_NAME;
605 } else {
606 default_event_name = DEFAULT_USER_LOG4J_EVENT_NAME;
607 }
608 break;
609 case LTTNG_DOMAIN_JUL:
610 if (is_root) {
611 default_event_name = DEFAULT_SYS_JUL_EVENT_NAME;
612 } else {
613 default_event_name = DEFAULT_USER_JUL_EVENT_NAME;
614 }
615 break;
616 case LTTNG_DOMAIN_PYTHON:
617 default_event_name = DEFAULT_USER_PYTHON_EVENT_NAME;
618 break;
619 default:
620 assert(0);
621 }
622
623 return default_event_name;
624 }
625
626 /*
627 * Disable a single agent event for a given UST session.
628 *
629 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
630 */
631 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
632 char *event_name)
633 {
634 int ret;
635 struct agent_event *aevent;
636 struct ltt_ust_event *uevent = NULL;
637 struct ltt_ust_channel *uchan = NULL;
638 const char *ust_event_name, *ust_channel_name;
639
640 assert(agt);
641 assert(usess);
642 assert(event_name);
643
644 DBG("Event agent disabling %s for session %" PRIu64, event_name, usess->id);
645
646 aevent = agent_find_event_by_name(event_name, agt);
647 if (!aevent) {
648 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
649 goto error;
650 }
651
652 /* Already disabled? */
653 if (!aevent->enabled) {
654 goto end;
655 }
656
657 if (agt->domain == LTTNG_DOMAIN_JUL) {
658 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
659 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
660 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
661 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
662 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
663 } else {
664 ret = LTTNG_ERR_INVALID;
665 goto error;
666 }
667
668 /*
669 * Disable it on the UST side. First get the channel reference then find
670 * the event and finally disable it.
671 */
672 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
673 (char *) ust_channel_name);
674 if (!uchan) {
675 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
676 goto error;
677 }
678
679 ust_event_name = event_get_default_agent_ust_name(agt->domain);
680 if (!ust_event_name) {
681 ret = LTTNG_ERR_FATAL;
682 goto error;
683 }
684
685 /*
686 * The loglevel is hardcoded with 0 here since the agent ust event is set
687 * with the loglevel type to ALL thus the loglevel stays 0. The event's
688 * filter is the one handling the loglevel for agent.
689 */
690 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
691 aevent->filter, 0, NULL);
692 /* If the agent event exists, it must be available on the UST side. */
693 assert(uevent);
694
695 ret = ust_app_disable_event_glb(usess, uchan, uevent);
696 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
697 ret = LTTNG_ERR_UST_DISABLE_FAIL;
698 goto error;
699 }
700
701 /*
702 * Flag event that it's disabled so the shadow copy on the ust app side
703 * will disable it if an application shows up.
704 */
705 uevent->enabled = 0;
706
707 ret = agent_disable_event(aevent, agt->domain);
708 if (ret != LTTNG_OK) {
709 goto error;
710 }
711
712 end:
713 return LTTNG_OK;
714
715 error:
716 return ret;
717 }
718 /*
719 * Disable all agent event for a given UST session.
720 *
721 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
722 */
723 int event_agent_disable_all(struct ltt_ust_session *usess,
724 struct agent *agt)
725 {
726 int ret;
727 struct agent_event *aevent;
728 struct lttng_ht_iter iter;
729
730 assert(agt);
731 assert(usess);
732
733 /*
734 * Disable event on agent application. Continue to disable all other events
735 * if the * event is not found.
736 */
737 ret = event_agent_disable(usess, agt, "*");
738 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
739 goto error;
740 }
741
742 /* Flag every event that they are now enabled. */
743 rcu_read_lock();
744 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
745 node.node) {
746 if (!aevent->enabled) {
747 continue;
748 }
749
750 ret = event_agent_disable(usess, agt, aevent->name);
751 if (ret != LTTNG_OK) {
752 rcu_read_unlock();
753 goto error;
754 }
755 }
756 rcu_read_unlock();
757
758 ret = LTTNG_OK;
759
760 error:
761 return ret;
762 }
This page took 0.043811 seconds and 4 git commands to generate.