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