Fix: rename of perf counter context
[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 #include <errno.h>
20 #include <urcu/list.h>
21 #include <string.h>
22
23 #include <lttng/lttng.h>
24 #include <common/error.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26
27 #include "channel.h"
28 #include "event.h"
29 #include "kernel.h"
30 #include "ust-ctl.h"
31 #include "ust-app.h"
32 #include "trace-kernel.h"
33 #include "trace-ust.h"
34
35 /*
36 * Add unique UST event based on the event name, filter bytecode and loglevel.
37 */
38 static void add_unique_ust_event(struct lttng_ht *ht,
39 struct ltt_ust_event *event)
40 {
41 struct cds_lfht_node *node_ptr;
42 struct ltt_ust_ht_key key;
43
44 assert(ht);
45 assert(ht->ht);
46 assert(event);
47
48 key.name = event->attr.name;
49 key.filter = (struct lttng_filter_bytecode *) event->filter;
50 key.loglevel = event->attr.loglevel;
51 key.exclusion = event->exclusion;
52
53 node_ptr = cds_lfht_add_unique(ht->ht,
54 ht->hash_fct(event->node.key, lttng_ht_seed),
55 trace_ust_ht_match_event, &key, &event->node.node);
56 assert(node_ptr == &event->node.node);
57 }
58
59 /*
60 * Setup a lttng_event used to enable *all* syscall tracing.
61 */
62 static void init_syscalls_kernel_event(struct lttng_event *event)
63 {
64 assert(event);
65
66 event->name[0] = '\0';
67 /*
68 * We use LTTNG_EVENT* here since the trace kernel creation will make the
69 * right changes for the kernel.
70 */
71 event->type = LTTNG_EVENT_SYSCALL;
72 }
73
74 /*
75 * Disable kernel tracepoint event for a channel from the kernel session.
76 */
77 int event_kernel_disable_tracepoint(struct ltt_kernel_channel *kchan,
78 char *event_name)
79 {
80 int ret;
81 struct ltt_kernel_event *kevent;
82
83 assert(kchan);
84
85 kevent = trace_kernel_get_event_by_name(event_name, kchan);
86 if (kevent == NULL) {
87 ret = LTTNG_ERR_NO_EVENT;
88 goto error;
89 }
90
91 ret = kernel_disable_event(kevent);
92 if (ret < 0) {
93 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
94 goto error;
95 }
96
97 DBG("Kernel event %s disable for channel %s.",
98 kevent->event->name, kchan->channel->name);
99
100 ret = LTTNG_OK;
101
102 error:
103 return ret;
104 }
105
106 /*
107 * Disable kernel tracepoint events for a channel from the kernel session.
108 */
109 int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel *kchan)
110 {
111 int ret;
112 struct ltt_kernel_event *kevent;
113
114 assert(kchan);
115
116 /* For each event in the kernel session */
117 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
118 ret = kernel_disable_event(kevent);
119 if (ret < 0) {
120 /* We continue disabling the rest */
121 continue;
122 }
123 }
124 ret = LTTNG_OK;
125 return ret;
126 }
127
128 /*
129 * Disable kernel syscall events for a channel from the kernel session.
130 */
131 int event_kernel_disable_all_syscalls(struct ltt_kernel_channel *kchan)
132 {
133 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
134 return LTTNG_OK; /* Return OK so disable all succeeds */
135 }
136
137 /*
138 * Disable all kernel event for a channel from the kernel session.
139 */
140 int event_kernel_disable_all(struct ltt_kernel_channel *kchan)
141 {
142 int ret;
143
144 assert(kchan);
145
146 ret = event_kernel_disable_all_tracepoints(kchan);
147 if (ret != LTTNG_OK)
148 return ret;
149 ret = event_kernel_disable_all_syscalls(kchan);
150 return ret;
151 }
152
153 /*
154 * Enable kernel tracepoint event for a channel from the kernel session.
155 */
156 int event_kernel_enable_tracepoint(struct ltt_kernel_channel *kchan,
157 struct lttng_event *event)
158 {
159 int ret;
160 struct ltt_kernel_event *kevent;
161
162 assert(kchan);
163 assert(event);
164
165 kevent = trace_kernel_get_event_by_name(event->name, kchan);
166 if (kevent == NULL) {
167 ret = kernel_create_event(event, kchan);
168 if (ret < 0) {
169 switch (-ret) {
170 case EEXIST:
171 ret = LTTNG_ERR_KERN_EVENT_EXIST;
172 break;
173 case ENOSYS:
174 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
175 break;
176 default:
177 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
178 break;
179 }
180 goto end;
181 }
182 } else if (kevent->enabled == 0) {
183 ret = kernel_enable_event(kevent);
184 if (ret < 0) {
185 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
186 goto end;
187 }
188 } else {
189 /* At this point, the event is considered enabled */
190 ret = LTTNG_ERR_KERN_EVENT_EXIST;
191 goto end;
192 }
193
194 ret = LTTNG_OK;
195 end:
196 return ret;
197 }
198
199 /*
200 * Enable all kernel tracepoint events of a channel of the kernel session.
201 */
202 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel *kchan,
203 int kernel_tracer_fd)
204 {
205 int size, i, ret;
206 struct ltt_kernel_event *kevent;
207 struct lttng_event *event_list = NULL;
208
209 assert(kchan);
210
211 /* For each event in the kernel session */
212 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
213 if (kevent->enabled == 0) {
214 ret = kernel_enable_event(kevent);
215 if (ret < 0) {
216 /* Enable failed but still continue */
217 continue;
218 }
219 }
220 }
221
222 size = kernel_list_events(kernel_tracer_fd, &event_list);
223 if (size < 0) {
224 ret = LTTNG_ERR_KERN_LIST_FAIL;
225 goto end;
226 }
227
228 for (i = 0; i < size; i++) {
229 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
230 if (kevent == NULL) {
231 /* Default event type for enable all */
232 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
233 /* Enable each single tracepoint event */
234 ret = kernel_create_event(&event_list[i], kchan);
235 if (ret < 0) {
236 /* Ignore error here and continue */
237 }
238 }
239 }
240 free(event_list);
241
242 ret = LTTNG_OK;
243 end:
244 return ret;
245 }
246
247 /*
248 * Enable all kernel sycalls events of a channel of the kernel session.
249 */
250 int event_kernel_enable_all_syscalls(struct ltt_kernel_channel *kchan,
251 int kernel_tracer_fd)
252 {
253 int ret;
254 struct lttng_event event;
255
256 assert(kchan);
257
258 init_syscalls_kernel_event(&event);
259
260 DBG("Enabling all syscall tracing");
261
262 ret = kernel_create_event(&event, kchan);
263 if (ret < 0) {
264 if (ret == -EEXIST) {
265 ret = LTTNG_ERR_KERN_EVENT_EXIST;
266 } else {
267 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
268 }
269 goto end;
270 }
271
272 ret = LTTNG_OK;
273 end:
274 return ret;
275 }
276
277 /*
278 * Enable all kernel events of a channel of the kernel session.
279 */
280 int event_kernel_enable_all(struct ltt_kernel_channel *kchan,
281 int kernel_tracer_fd)
282 {
283 int tp_ret;
284
285 assert(kchan);
286
287 tp_ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
288 if (tp_ret != LTTNG_OK) {
289 goto end;
290 }
291
292 /*
293 * Reaching this code path means that all tracepoints were enabled without
294 * errors so we ignore the error value of syscalls.
295 *
296 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
297 * is not considered an error that need to be returned to the client since
298 * tracepoints did not fail. Future work will allow us to send back
299 * multiple errors to the client in one API call.
300 */
301 (void) event_kernel_enable_all_syscalls(kchan, kernel_tracer_fd);
302
303 end:
304 return tp_ret;
305 }
306
307 /*
308 * ============================
309 * UST : The Ultimate Frontier!
310 * ============================
311 */
312
313 /*
314 * Enable all UST tracepoints for a channel from a UST session.
315 */
316 int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess,
317 struct ltt_ust_channel *uchan,
318 char *filter_expression,
319 struct lttng_filter_bytecode *filter)
320 {
321 int ret, i, size;
322 struct lttng_ht_iter iter;
323 struct ltt_ust_event *uevent = NULL;
324 struct lttng_event *events = NULL;
325
326 assert(usess);
327 assert(uchan);
328
329 rcu_read_lock();
330
331 /* Enable existing events */
332 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
333 node.node) {
334 if (uevent->enabled == 0) {
335 ret = ust_app_enable_event_glb(usess, uchan, uevent);
336 if (ret < 0) {
337 continue;
338 }
339 uevent->enabled = 1;
340 }
341 }
342
343 /* Get all UST available events */
344 size = ust_app_list_events(&events);
345 if (size < 0) {
346 ret = LTTNG_ERR_UST_LIST_FAIL;
347 goto error;
348 }
349
350 for (i = 0; i < size; i++) {
351 /*
352 * Check if event exist and if so, continue since it was enable
353 * previously.
354 */
355 uevent = trace_ust_find_event(uchan->events, events[i].name, filter,
356 events[i].loglevel, NULL);
357 if (uevent != NULL) {
358 ret = ust_app_enable_event_pid(usess, uchan, uevent,
359 events[i].pid);
360 if (ret < 0) {
361 if (ret != -LTTNG_UST_ERR_EXIST) {
362 ret = LTTNG_ERR_UST_ENABLE_FAIL;
363 goto error;
364 }
365 }
366 continue;
367 }
368
369 /* Create ust event */
370 uevent = trace_ust_create_event(&events[i], filter_expression,
371 filter, NULL);
372 if (uevent == NULL) {
373 ret = LTTNG_ERR_FATAL;
374 goto error_destroy;
375 }
376
377 /* Create event for the specific PID */
378 ret = ust_app_enable_event_pid(usess, uchan, uevent,
379 events[i].pid);
380 if (ret < 0) {
381 if (ret == -LTTNG_UST_ERR_EXIST) {
382 ret = LTTNG_ERR_UST_EVENT_EXIST;
383 goto error;
384 } else {
385 ret = LTTNG_ERR_UST_ENABLE_FAIL;
386 goto error_destroy;
387 }
388 }
389
390 uevent->enabled = 1;
391 /* Add ltt ust event to channel */
392 rcu_read_lock();
393 add_unique_ust_event(uchan->events, uevent);
394 rcu_read_unlock();
395 }
396 free(events);
397
398 rcu_read_unlock();
399 return LTTNG_OK;
400
401 error_destroy:
402 trace_ust_destroy_event(uevent);
403
404 error:
405 free(events);
406 rcu_read_unlock();
407 return ret;
408 }
409
410 /*
411 * Enable UST tracepoint event for a channel from a UST session.
412 */
413 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
414 struct ltt_ust_channel *uchan, struct lttng_event *event,
415 char *filter_expression,
416 struct lttng_filter_bytecode *filter,
417 struct lttng_event_exclusion *exclusion)
418 {
419 int ret = LTTNG_OK, to_create = 0;
420 struct ltt_ust_event *uevent;
421
422 assert(usess);
423 assert(uchan);
424 assert(event);
425
426 rcu_read_lock();
427
428 uevent = trace_ust_find_event(uchan->events, event->name, filter,
429 event->loglevel, exclusion);
430 if (uevent == NULL) {
431 uevent = trace_ust_create_event(event, filter_expression,
432 filter, exclusion);
433 if (uevent == NULL) {
434 ret = LTTNG_ERR_UST_ENABLE_FAIL;
435 goto error;
436 }
437
438 /* Valid to set it after the goto error since uevent is still NULL */
439 to_create = 1;
440 }
441
442 if (uevent->enabled) {
443 /* It's already enabled so everything is OK */
444 ret = LTTNG_ERR_UST_EVENT_ENABLED;
445 goto end;
446 }
447
448 uevent->enabled = 1;
449
450 if (to_create) {
451 /* Create event on all UST registered apps for session */
452 ret = ust_app_create_event_glb(usess, uchan, uevent);
453 } else {
454 /* Enable event on all UST registered apps for session */
455 ret = ust_app_enable_event_glb(usess, uchan, uevent);
456 }
457
458 if (ret < 0) {
459 if (ret == -LTTNG_UST_ERR_EXIST) {
460 ret = LTTNG_ERR_UST_EVENT_EXIST;
461 goto end;
462 } else {
463 ret = LTTNG_ERR_UST_ENABLE_FAIL;
464 goto error;
465 }
466 }
467
468 if (to_create) {
469 /* Add ltt ust event to channel */
470 add_unique_ust_event(uchan->events, uevent);
471 }
472
473 DBG("Event UST %s %s in channel %s", uevent->attr.name,
474 to_create ? "created" : "enabled", uchan->name);
475
476 ret = LTTNG_OK;
477
478 end:
479 rcu_read_unlock();
480 return ret;
481
482 error:
483 /*
484 * Only destroy event on creation time (not enabling time) because if the
485 * event is found in the channel (to_create == 0), it means that at some
486 * point the enable_event worked and it's thus valid to keep it alive.
487 * Destroying it also implies that we also destroy it's shadow copy to sync
488 * everyone up.
489 */
490 if (to_create) {
491 /* In this code path, the uevent was not added to the hash table */
492 trace_ust_destroy_event(uevent);
493 }
494 rcu_read_unlock();
495 return ret;
496 }
497
498 /*
499 * Disable UST tracepoint of a channel from a UST session.
500 */
501 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
502 struct ltt_ust_channel *uchan, char *event_name)
503 {
504 int ret;
505 struct ltt_ust_event *uevent;
506 struct lttng_ht_node_str *node;
507 struct lttng_ht_iter iter;
508 struct lttng_ht *ht;
509
510 assert(usess);
511 assert(uchan);
512 assert(event_name);
513
514 ht = uchan->events;
515
516 rcu_read_lock();
517
518 /*
519 * We use a custom lookup since we need the iterator for the next_duplicate
520 * call in the do while loop below.
521 */
522 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
523 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
524 node = lttng_ht_iter_get_node_str(&iter);
525 if (node == NULL) {
526 DBG2("Trace UST event NOT found by name %s", event_name);
527 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
528 goto error;
529 }
530
531 do {
532 uevent = caa_container_of(node, struct ltt_ust_event, node);
533 assert(uevent);
534
535 if (uevent->enabled == 0) {
536 /* It's already disabled so everything is OK */
537 goto next;
538 }
539
540 ret = ust_app_disable_event_glb(usess, uchan, uevent);
541 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
542 ret = LTTNG_ERR_UST_DISABLE_FAIL;
543 goto error;
544 }
545 uevent->enabled = 0;
546
547 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
548 uchan->name);
549
550 next:
551 /* Get next duplicate event by name. */
552 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
553 event_name, &iter.iter);
554 node = lttng_ht_iter_get_node_str(&iter);
555 } while (node);
556
557 ret = LTTNG_OK;
558
559 error:
560 rcu_read_unlock();
561 return ret;
562 }
563
564 /*
565 * Disable all UST tracepoints for a channel from a UST session.
566 */
567 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
568 struct ltt_ust_channel *uchan)
569 {
570 int ret, i, size;
571 struct lttng_ht_iter iter;
572 struct ltt_ust_event *uevent = NULL;
573 struct lttng_event *events = NULL;
574
575 assert(usess);
576 assert(uchan);
577
578 rcu_read_lock();
579
580 /* Disabling existing events */
581 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
582 node.node) {
583 if (uevent->enabled == 1) {
584 ret = event_ust_disable_tracepoint(usess, uchan,
585 uevent->attr.name);
586 if (ret < 0) {
587 continue;
588 }
589 }
590 }
591
592 /* Get all UST available events */
593 size = ust_app_list_events(&events);
594 if (size < 0) {
595 ret = LTTNG_ERR_UST_LIST_FAIL;
596 goto error;
597 }
598
599 for (i = 0; i < size; i++) {
600 ret = event_ust_disable_tracepoint(usess, uchan,
601 events[i].name);
602 if (ret != LTTNG_OK) {
603 /* Continue to disable the rest... */
604 continue;
605 }
606 }
607 free(events);
608
609 rcu_read_unlock();
610 return LTTNG_OK;
611
612 error:
613 free(events);
614 rcu_read_unlock();
615 return ret;
616 }
617
618 /*
619 * Enable all JUL event for a given UST session.
620 *
621 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
622 */
623 int event_jul_enable_all(struct ltt_ust_session *usess,
624 struct lttng_event *event)
625 {
626 int ret;
627 struct jul_event *jevent;
628 struct lttng_ht_iter iter;
629
630 assert(usess);
631
632 DBG("Event JUL enabling ALL events for session %" PRIu64, usess->id);
633
634 /* Enable event on JUL application through TCP socket. */
635 ret = event_jul_enable(usess, event);
636 if (ret != LTTNG_OK) {
637 goto error;
638 }
639
640 /* Flag every event that they are now enabled. */
641 rcu_read_lock();
642 cds_lfht_for_each_entry(usess->domain_jul.events->ht, &iter.iter, jevent,
643 node.node) {
644 jevent->enabled = 1;
645 }
646 rcu_read_unlock();
647
648 ret = LTTNG_OK;
649
650 error:
651 return ret;
652 }
653
654 /*
655 * Enable a single JUL event for a given UST session.
656 *
657 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
658 */
659 int event_jul_enable(struct ltt_ust_session *usess, struct lttng_event *event)
660 {
661 int ret, created = 0;
662 struct jul_event *jevent;
663
664 assert(usess);
665 assert(event);
666
667 DBG("Event JUL enabling %s for session %" PRIu64 " with loglevel type %d "
668 "and loglevel %d", event->name, usess->id, event->loglevel_type,
669 event->loglevel);
670
671 jevent = jul_find_event(event->name, event->loglevel, &usess->domain_jul);
672 if (!jevent) {
673 jevent = jul_create_event(event->name);
674 if (!jevent) {
675 ret = LTTNG_ERR_NOMEM;
676 goto error;
677 }
678 jevent->loglevel = event->loglevel;
679 jevent->loglevel_type = event->loglevel_type;
680 created = 1;
681 }
682
683 /* Already enabled? */
684 if (jevent->enabled) {
685 goto end;
686 }
687
688 ret = jul_enable_event(jevent);
689 if (ret != LTTNG_OK) {
690 goto error;
691 }
692
693 /* If the event was created prior to the enable, add it to the domain. */
694 if (created) {
695 jul_add_event(jevent, &usess->domain_jul);
696 }
697
698 end:
699 return LTTNG_OK;
700
701 error:
702 if (created) {
703 jul_destroy_event(jevent);
704 }
705 return ret;
706 }
707
708 /*
709 * Disable a single JUL event for a given UST session.
710 *
711 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
712 */
713 int event_jul_disable(struct ltt_ust_session *usess, char *event_name)
714 {
715 int ret;
716 struct jul_event *jevent;
717
718 assert(usess);
719 assert(event_name);
720
721 DBG("Event JUL disabling %s for session %" PRIu64, event_name, usess->id);
722
723 jevent = jul_find_event_by_name(event_name, &usess->domain_jul);
724 if (!jevent) {
725 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
726 goto error;
727 }
728
729 /* Already disabled? */
730 if (!jevent->enabled) {
731 goto end;
732 }
733
734 ret = jul_disable_event(jevent);
735 if (ret != LTTNG_OK) {
736 goto error;
737 }
738
739 end:
740 return LTTNG_OK;
741
742 error:
743 return ret;
744 }
745 /*
746 * Disable all JUL event for a given UST session.
747 *
748 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
749 */
750 int event_jul_disable_all(struct ltt_ust_session *usess)
751 {
752 int ret, do_disable = 0;
753 struct jul_event *jevent;
754 struct lttng_ht_iter iter;
755
756 assert(usess);
757
758 /* Enable event on JUL application through TCP socket. */
759 ret = event_jul_disable(usess, "*");
760 if (ret != LTTNG_OK) {
761 if (ret == LTTNG_ERR_UST_EVENT_NOT_FOUND) {
762 /*
763 * This means that no enable all was done before but still a user
764 * could want to disable everything even though the * wild card
765 * event does not exists.
766 */
767 do_disable = 1;
768 } else {
769 goto error;
770 }
771 }
772
773 /* Flag every event that they are now enabled. */
774 rcu_read_lock();
775 cds_lfht_for_each_entry(usess->domain_jul.events->ht, &iter.iter, jevent,
776 node.node) {
777 if (jevent->enabled && do_disable) {
778 ret = event_jul_disable(usess, jevent->name);
779 if (ret != LTTNG_OK) {
780 rcu_read_unlock();
781 goto error;
782 }
783 }
784 jevent->enabled = 0;
785 }
786 rcu_read_unlock();
787
788 ret = LTTNG_OK;
789
790 error:
791 return ret;
792 }
This page took 0.046705 seconds and 4 git commands to generate.