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