Tests fix: Missing waitpid in fork test
[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, struct lttng_filter_bytecode *filter)
318 {
319 int ret, i, size;
320 struct lttng_ht_iter iter;
321 struct ltt_ust_event *uevent = NULL;
322 struct lttng_event *events = NULL;
323
324 assert(usess);
325 assert(uchan);
326
327 rcu_read_lock();
328
329 /* Enable existing events */
330 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
331 node.node) {
332 if (uevent->enabled == 0) {
333 ret = ust_app_enable_event_glb(usess, uchan, uevent);
334 if (ret < 0) {
335 continue;
336 }
337 uevent->enabled = 1;
338 }
339 }
340
341 /* Get all UST available events */
342 size = ust_app_list_events(&events);
343 if (size < 0) {
344 ret = LTTNG_ERR_UST_LIST_FAIL;
345 goto error;
346 }
347
348 for (i = 0; i < size; i++) {
349 /*
350 * Check if event exist and if so, continue since it was enable
351 * previously.
352 */
353 uevent = trace_ust_find_event(uchan->events, events[i].name, filter,
354 events[i].loglevel, NULL);
355 if (uevent != NULL) {
356 ret = ust_app_enable_event_pid(usess, uchan, uevent,
357 events[i].pid);
358 if (ret < 0) {
359 if (ret != -LTTNG_UST_ERR_EXIST) {
360 ret = LTTNG_ERR_UST_ENABLE_FAIL;
361 goto error;
362 }
363 }
364 continue;
365 }
366
367 /* Create ust event */
368 uevent = trace_ust_create_event(&events[i], filter, NULL);
369 if (uevent == NULL) {
370 ret = LTTNG_ERR_FATAL;
371 goto error_destroy;
372 }
373
374 /* Create event for the specific PID */
375 ret = ust_app_enable_event_pid(usess, uchan, uevent,
376 events[i].pid);
377 if (ret < 0) {
378 if (ret == -LTTNG_UST_ERR_EXIST) {
379 ret = LTTNG_ERR_UST_EVENT_EXIST;
380 goto error;
381 } else {
382 ret = LTTNG_ERR_UST_ENABLE_FAIL;
383 goto error_destroy;
384 }
385 }
386
387 uevent->enabled = 1;
388 /* Add ltt ust event to channel */
389 rcu_read_lock();
390 add_unique_ust_event(uchan->events, uevent);
391 rcu_read_unlock();
392 }
393 free(events);
394
395 rcu_read_unlock();
396 return LTTNG_OK;
397
398 error_destroy:
399 trace_ust_destroy_event(uevent);
400
401 error:
402 free(events);
403 rcu_read_unlock();
404 return ret;
405 }
406
407 /*
408 * Enable UST tracepoint event for a channel from a UST session.
409 */
410 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
411 struct ltt_ust_channel *uchan, struct lttng_event *event,
412 struct lttng_filter_bytecode *filter,
413 struct lttng_event_exclusion *exclusion)
414 {
415 int ret = LTTNG_OK, to_create = 0;
416 struct ltt_ust_event *uevent;
417
418 assert(usess);
419 assert(uchan);
420 assert(event);
421
422 rcu_read_lock();
423
424 uevent = trace_ust_find_event(uchan->events, event->name, filter,
425 event->loglevel, exclusion);
426 if (uevent == NULL) {
427 uevent = trace_ust_create_event(event, filter, exclusion);
428 if (uevent == NULL) {
429 ret = LTTNG_ERR_UST_ENABLE_FAIL;
430 goto error;
431 }
432
433 /* Valid to set it after the goto error since uevent is still NULL */
434 to_create = 1;
435 }
436
437 if (uevent->enabled) {
438 /* It's already enabled so everything is OK */
439 ret = LTTNG_ERR_UST_EVENT_ENABLED;
440 goto end;
441 }
442
443 uevent->enabled = 1;
444
445 if (to_create) {
446 /* Create event on all UST registered apps for session */
447 ret = ust_app_create_event_glb(usess, uchan, uevent);
448 } else {
449 /* Enable event on all UST registered apps for session */
450 ret = ust_app_enable_event_glb(usess, uchan, uevent);
451 }
452
453 if (ret < 0) {
454 if (ret == -LTTNG_UST_ERR_EXIST) {
455 ret = LTTNG_ERR_UST_EVENT_EXIST;
456 goto end;
457 } else {
458 ret = LTTNG_ERR_UST_ENABLE_FAIL;
459 goto error;
460 }
461 }
462
463 if (to_create) {
464 /* Add ltt ust event to channel */
465 add_unique_ust_event(uchan->events, uevent);
466 }
467
468 DBG("Event UST %s %s in channel %s", uevent->attr.name,
469 to_create ? "created" : "enabled", uchan->name);
470
471 ret = LTTNG_OK;
472
473 end:
474 rcu_read_unlock();
475 return ret;
476
477 error:
478 /*
479 * Only destroy event on creation time (not enabling time) because if the
480 * event is found in the channel (to_create == 0), it means that at some
481 * point the enable_event worked and it's thus valid to keep it alive.
482 * Destroying it also implies that we also destroy it's shadow copy to sync
483 * everyone up.
484 */
485 if (to_create) {
486 /* In this code path, the uevent was not added to the hash table */
487 trace_ust_destroy_event(uevent);
488 }
489 rcu_read_unlock();
490 return ret;
491 }
492
493 /*
494 * Disable UST tracepoint of a channel from a UST session.
495 */
496 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
497 struct ltt_ust_channel *uchan, char *event_name)
498 {
499 int ret;
500 struct ltt_ust_event *uevent;
501 struct lttng_ht_node_str *node;
502 struct lttng_ht_iter iter;
503 struct lttng_ht *ht;
504
505 assert(usess);
506 assert(uchan);
507 assert(event_name);
508
509 ht = uchan->events;
510
511 rcu_read_lock();
512
513 /*
514 * We use a custom lookup since we need the iterator for the next_duplicate
515 * call in the do while loop below.
516 */
517 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
518 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
519 node = lttng_ht_iter_get_node_str(&iter);
520 if (node == NULL) {
521 DBG2("Trace UST event NOT found by name %s", event_name);
522 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
523 goto error;
524 }
525
526 do {
527 uevent = caa_container_of(node, struct ltt_ust_event, node);
528 assert(uevent);
529
530 if (uevent->enabled == 0) {
531 /* It's already disabled so everything is OK */
532 goto next;
533 }
534
535 ret = ust_app_disable_event_glb(usess, uchan, uevent);
536 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
537 ret = LTTNG_ERR_UST_DISABLE_FAIL;
538 goto error;
539 }
540 uevent->enabled = 0;
541
542 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
543 uchan->name);
544
545 next:
546 /* Get next duplicate event by name. */
547 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
548 event_name, &iter.iter);
549 node = lttng_ht_iter_get_node_str(&iter);
550 } while (node);
551
552 ret = LTTNG_OK;
553
554 error:
555 rcu_read_unlock();
556 return ret;
557 }
558
559 /*
560 * Disable all UST tracepoints for a channel from a UST session.
561 */
562 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
563 struct ltt_ust_channel *uchan)
564 {
565 int ret, i, size;
566 struct lttng_ht_iter iter;
567 struct ltt_ust_event *uevent = NULL;
568 struct lttng_event *events = NULL;
569
570 assert(usess);
571 assert(uchan);
572
573 rcu_read_lock();
574
575 /* Disabling existing events */
576 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
577 node.node) {
578 if (uevent->enabled == 1) {
579 ret = event_ust_disable_tracepoint(usess, uchan,
580 uevent->attr.name);
581 if (ret < 0) {
582 continue;
583 }
584 }
585 }
586
587 /* Get all UST available events */
588 size = ust_app_list_events(&events);
589 if (size < 0) {
590 ret = LTTNG_ERR_UST_LIST_FAIL;
591 goto error;
592 }
593
594 for (i = 0; i < size; i++) {
595 ret = event_ust_disable_tracepoint(usess, uchan,
596 events[i].name);
597 if (ret != LTTNG_OK) {
598 /* Continue to disable the rest... */
599 continue;
600 }
601 }
602 free(events);
603
604 rcu_read_unlock();
605 return LTTNG_OK;
606
607 error:
608 free(events);
609 rcu_read_unlock();
610 return ret;
611 }
612
613 /*
614 * Enable all JUL event for a given UST session.
615 *
616 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
617 */
618 int event_jul_enable_all(struct ltt_ust_session *usess,
619 struct lttng_event *event)
620 {
621 int ret;
622 struct jul_event *jevent;
623 struct lttng_ht_iter iter;
624
625 assert(usess);
626
627 DBG("Event JUL enabling ALL events for session %" PRIu64, usess->id);
628
629 /* Enable event on JUL application through TCP socket. */
630 ret = event_jul_enable(usess, event);
631 if (ret != LTTNG_OK) {
632 goto error;
633 }
634
635 /* Flag every event that they are now enabled. */
636 rcu_read_lock();
637 cds_lfht_for_each_entry(usess->domain_jul.events->ht, &iter.iter, jevent,
638 node.node) {
639 jevent->enabled = 1;
640 }
641 rcu_read_unlock();
642
643 ret = LTTNG_OK;
644
645 error:
646 return ret;
647 }
648
649 /*
650 * Enable a single JUL event for a given UST session.
651 *
652 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
653 */
654 int event_jul_enable(struct ltt_ust_session *usess, struct lttng_event *event)
655 {
656 int ret, created = 0;
657 struct jul_event *jevent;
658
659 assert(usess);
660 assert(event);
661
662 DBG("Event JUL enabling %s for session %" PRIu64 " with loglevel type %d "
663 "and loglevel %d", event->name, usess->id, event->loglevel_type,
664 event->loglevel);
665
666 jevent = jul_find_by_name(event->name, &usess->domain_jul);
667 if (!jevent) {
668 jevent = jul_create_event(event->name);
669 if (!jevent) {
670 ret = LTTNG_ERR_NOMEM;
671 goto error;
672 }
673 jevent->loglevel = event->loglevel;
674 jevent->loglevel_type = event->loglevel_type;
675 created = 1;
676 }
677
678 /* Already enabled? */
679 if (jevent->enabled) {
680 goto end;
681 }
682
683 ret = jul_enable_event(jevent);
684 if (ret != LTTNG_OK) {
685 goto error;
686 }
687
688 /* If the event was created prior to the enable, add it to the domain. */
689 if (created) {
690 jul_add_event(jevent, &usess->domain_jul);
691 }
692
693 end:
694 return LTTNG_OK;
695
696 error:
697 if (created) {
698 jul_destroy_event(jevent);
699 }
700 return ret;
701 }
702
703 /*
704 * Disable a single JUL event for a given UST session.
705 *
706 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
707 */
708 int event_jul_disable(struct ltt_ust_session *usess, char *event_name)
709 {
710 int ret;
711 struct jul_event *jevent;
712
713 assert(usess);
714 assert(event_name);
715
716 DBG("Event JUL disabling %s for session %" PRIu64, event_name, usess->id);
717
718 jevent = jul_find_by_name(event_name, &usess->domain_jul);
719 if (!jevent) {
720 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
721 goto error;
722 }
723
724 /* Already disabled? */
725 if (!jevent->enabled) {
726 goto end;
727 }
728
729 ret = jul_disable_event(jevent);
730 if (ret != LTTNG_OK) {
731 goto error;
732 }
733
734 end:
735 return LTTNG_OK;
736
737 error:
738 return ret;
739 }
740 /*
741 * Disable all JUL event for a given UST session.
742 *
743 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
744 */
745 int event_jul_disable_all(struct ltt_ust_session *usess)
746 {
747 int ret, do_disable = 0;
748 struct jul_event *jevent;
749 struct lttng_ht_iter iter;
750
751 assert(usess);
752
753 /* Enable event on JUL application through TCP socket. */
754 ret = event_jul_disable(usess, "*");
755 if (ret != LTTNG_OK) {
756 if (ret == LTTNG_ERR_UST_EVENT_NOT_FOUND) {
757 /*
758 * This means that no enable all was done before but still a user
759 * could want to disable everything even though the * wild card
760 * event does not exists.
761 */
762 do_disable = 1;
763 } else {
764 goto error;
765 }
766 }
767
768 /* Flag every event that they are now enabled. */
769 rcu_read_lock();
770 cds_lfht_for_each_entry(usess->domain_jul.events->ht, &iter.iter, jevent,
771 node.node) {
772 if (jevent->enabled && do_disable) {
773 ret = event_jul_disable(usess, jevent->name);
774 if (ret != LTTNG_OK) {
775 rcu_read_unlock();
776 goto error;
777 }
778 }
779 jevent->enabled = 0;
780 }
781 rcu_read_unlock();
782
783 ret = LTTNG_OK;
784
785 error:
786 return ret;
787 }
This page took 0.060559 seconds and 4 git commands to generate.