38cbe7925cab09e1ec6b78c910e99432aa5c5ca4
[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 #include "agent.h"
37
38 /*
39 * Add unique UST event based on the event name, filter bytecode and loglevel.
40 */
41 static void add_unique_ust_event(struct lttng_ht *ht,
42 struct ltt_ust_event *event)
43 {
44 struct cds_lfht_node *node_ptr;
45 struct ltt_ust_ht_key key;
46
47 assert(ht);
48 assert(ht->ht);
49 assert(event);
50
51 key.name = event->attr.name;
52 key.filter = (struct lttng_filter_bytecode *) event->filter;
53 key.loglevel_type = event->attr.loglevel_type;
54 key.loglevel_value = event->attr.loglevel;
55 key.exclusion = event->exclusion;
56
57 node_ptr = cds_lfht_add_unique(ht->ht,
58 ht->hash_fct(event->node.key, lttng_ht_seed),
59 trace_ust_ht_match_event, &key, &event->node.node);
60 assert(node_ptr == &event->node.node);
61 }
62
63 /*
64 * Disable kernel tracepoint event for a channel from the kernel session.
65 */
66 int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
67 char *event_name)
68 {
69 int ret;
70 struct ltt_kernel_event *kevent;
71
72 assert(kchan);
73
74 kevent = trace_kernel_get_event_by_name(event_name, kchan,
75 LTTNG_EVENT_ALL);
76 if (kevent == NULL) {
77 ret = LTTNG_ERR_NO_EVENT;
78 goto error;
79 }
80
81 ret = kernel_disable_event(kevent);
82 if (ret < 0) {
83 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
84 goto error;
85 }
86
87 DBG("Kernel event %s disable for channel %s.",
88 kevent->event->name, kchan->channel->name);
89
90 ret = LTTNG_OK;
91
92 error:
93 return ret;
94 }
95
96 /*
97 * Disable kernel tracepoint events for a channel from the kernel session.
98 */
99 int event_kernel_disable_event_type(struct ltt_kernel_channel *kchan,
100 enum lttng_event_type type)
101 {
102 int ret;
103 struct ltt_kernel_event *kevent;
104
105 assert(kchan);
106
107 /* For each event in the kernel session */
108 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
109 if (type != LTTNG_EVENT_ALL && kevent->type != type)
110 continue;
111 ret = kernel_disable_event(kevent);
112 if (ret < 0) {
113 /* We continue disabling the rest */
114 continue;
115 }
116 }
117 ret = LTTNG_OK;
118 return ret;
119 }
120
121 /*
122 * Disable all kernel event for a channel from the kernel session.
123 */
124 int event_kernel_disable_event_all(struct ltt_kernel_channel *kchan)
125 {
126 return event_kernel_disable_event_type(kchan, LTTNG_EVENT_ALL);
127 }
128
129 /*
130 * Enable kernel tracepoint event for a channel from the kernel session.
131 * We own filter_expression and filter.
132 */
133 int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
134 struct lttng_event *event, char *filter_expression,
135 struct lttng_filter_bytecode *filter)
136 {
137 int ret;
138 struct ltt_kernel_event *kevent;
139
140 assert(kchan);
141 assert(event);
142
143 kevent = trace_kernel_find_event(event->name, kchan,
144 event->type, filter);
145 if (kevent == NULL) {
146 ret = kernel_create_event(event, kchan,
147 filter_expression, filter);
148 /* We have passed ownership */
149 filter_expression = NULL;
150 filter = NULL;
151 if (ret < 0) {
152 switch (-ret) {
153 case EEXIST:
154 ret = LTTNG_ERR_KERN_EVENT_EXIST;
155 break;
156 case ENOSYS:
157 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
158 break;
159 default:
160 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
161 break;
162 }
163 goto end;
164 }
165 } else if (kevent->enabled == 0) {
166 ret = kernel_enable_event(kevent);
167 if (ret < 0) {
168 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
169 goto end;
170 }
171 } else {
172 /* At this point, the event is considered enabled */
173 ret = LTTNG_ERR_KERN_EVENT_EXIST;
174 goto end;
175 }
176
177 ret = LTTNG_OK;
178 end:
179 free(filter_expression);
180 free(filter);
181 return ret;
182 }
183
184 /*
185 * ============================
186 * UST : The Ultimate Frontier!
187 * ============================
188 */
189
190 /*
191 * Enable UST tracepoint event for a channel from a UST session.
192 * We own filter_expression, filter, and exclusion.
193 */
194 int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
195 struct ltt_ust_channel *uchan, struct lttng_event *event,
196 char *filter_expression,
197 struct lttng_filter_bytecode *filter,
198 struct lttng_event_exclusion *exclusion,
199 bool internal_event)
200 {
201 int ret = LTTNG_OK, to_create = 0;
202 struct ltt_ust_event *uevent;
203
204 assert(usess);
205 assert(uchan);
206 assert(event);
207
208 rcu_read_lock();
209
210 uevent = trace_ust_find_event(uchan->events, event->name, filter,
211 event->loglevel_type, event->loglevel, exclusion);
212 if (!uevent) {
213 uevent = trace_ust_create_event(event, filter_expression,
214 filter, exclusion, internal_event);
215 /* We have passed ownership */
216 filter_expression = NULL;
217 filter = NULL;
218 exclusion = NULL;
219 if (uevent == NULL) {
220 ret = LTTNG_ERR_UST_ENABLE_FAIL;
221 goto error;
222 }
223
224 /* Valid to set it after the goto error since uevent is still NULL */
225 to_create = 1;
226 }
227
228 if (uevent->enabled) {
229 /* It's already enabled so everything is OK */
230 ret = LTTNG_ERR_UST_EVENT_ENABLED;
231 goto end;
232 }
233
234 uevent->enabled = 1;
235
236 if (to_create) {
237 /* Create event on all UST registered apps for session */
238 ret = ust_app_create_event_glb(usess, uchan, uevent);
239 } else {
240 /* Enable event on all UST registered apps for session */
241 ret = ust_app_enable_event_glb(usess, uchan, uevent);
242 }
243
244 if (ret < 0) {
245 if (ret == -LTTNG_UST_ERR_EXIST) {
246 ret = LTTNG_ERR_UST_EVENT_EXIST;
247 goto end;
248 } else {
249 ret = LTTNG_ERR_UST_ENABLE_FAIL;
250 goto error;
251 }
252 }
253
254 if (to_create) {
255 /* Add ltt ust event to channel */
256 add_unique_ust_event(uchan->events, uevent);
257 }
258
259 DBG("Event UST %s %s in channel %s", uevent->attr.name,
260 to_create ? "created" : "enabled", uchan->name);
261
262 ret = LTTNG_OK;
263
264 end:
265 rcu_read_unlock();
266 free(filter_expression);
267 free(filter);
268 free(exclusion);
269 return ret;
270
271 error:
272 /*
273 * Only destroy event on creation time (not enabling time) because if the
274 * event is found in the channel (to_create == 0), it means that at some
275 * point the enable_event worked and it's thus valid to keep it alive.
276 * Destroying it also implies that we also destroy it's shadow copy to sync
277 * everyone up.
278 */
279 if (to_create) {
280 /* In this code path, the uevent was not added to the hash table */
281 trace_ust_destroy_event(uevent);
282 }
283 rcu_read_unlock();
284 free(filter_expression);
285 free(filter);
286 free(exclusion);
287 return ret;
288 }
289
290 /*
291 * Disable UST tracepoint of a channel from a UST session.
292 */
293 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
294 struct ltt_ust_channel *uchan, char *event_name)
295 {
296 int ret;
297 struct ltt_ust_event *uevent;
298 struct lttng_ht_node_str *node;
299 struct lttng_ht_iter iter;
300 struct lttng_ht *ht;
301
302 assert(usess);
303 assert(uchan);
304 assert(event_name);
305
306 ht = uchan->events;
307
308 rcu_read_lock();
309
310 /*
311 * We use a custom lookup since we need the iterator for the next_duplicate
312 * call in the do while loop below.
313 */
314 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
315 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
316 node = lttng_ht_iter_get_node_str(&iter);
317 if (node == NULL) {
318 DBG2("Trace UST event NOT found by name %s", event_name);
319 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
320 goto error;
321 }
322
323 do {
324 uevent = caa_container_of(node, struct ltt_ust_event, node);
325 assert(uevent);
326
327 if (uevent->enabled == 0) {
328 /* It's already disabled so everything is OK */
329 goto next;
330 }
331
332 ret = ust_app_disable_event_glb(usess, uchan, uevent);
333 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
334 ret = LTTNG_ERR_UST_DISABLE_FAIL;
335 goto error;
336 }
337 uevent->enabled = 0;
338
339 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
340 uchan->name);
341
342 next:
343 /* Get next duplicate event by name. */
344 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
345 event_name, &iter.iter);
346 node = lttng_ht_iter_get_node_str(&iter);
347 } while (node);
348
349 ret = LTTNG_OK;
350
351 error:
352 rcu_read_unlock();
353 return ret;
354 }
355
356 /*
357 * Disable all UST tracepoints for a channel from a UST session.
358 */
359 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
360 struct ltt_ust_channel *uchan)
361 {
362 int ret, i, size;
363 struct lttng_ht_iter iter;
364 struct ltt_ust_event *uevent = NULL;
365 struct lttng_event *events = NULL;
366
367 assert(usess);
368 assert(uchan);
369
370 rcu_read_lock();
371
372 /* Disabling existing events */
373 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
374 node.node) {
375 if (uevent->enabled == 1) {
376 ret = event_ust_disable_tracepoint(usess, uchan,
377 uevent->attr.name);
378 if (ret < 0) {
379 continue;
380 }
381 }
382 }
383
384 /* Get all UST available events */
385 size = ust_app_list_events(&events);
386 if (size < 0) {
387 ret = LTTNG_ERR_UST_LIST_FAIL;
388 goto error;
389 }
390
391 for (i = 0; i < size; i++) {
392 ret = event_ust_disable_tracepoint(usess, uchan,
393 events[i].name);
394 if (ret != LTTNG_OK) {
395 /* Continue to disable the rest... */
396 continue;
397 }
398 }
399
400 ret = LTTNG_OK;
401 error:
402 rcu_read_unlock();
403 free(events);
404 return ret;
405 }
406
407 /*
408 * Enable all agent event for a given UST session.
409 *
410 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
411 */
412 int event_agent_enable_all(struct ltt_ust_session *usess,
413 struct agent *agt, struct lttng_event *event,
414 struct lttng_filter_bytecode *filter ,char *filter_expression)
415 {
416 int ret;
417 struct agent_event *aevent;
418 struct lttng_ht_iter iter;
419
420 assert(usess);
421
422 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
423
424 /* Enable event on agent application through TCP socket. */
425 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
426 if (ret != LTTNG_OK) {
427 goto error;
428 }
429
430 /* Flag every event that they are now enabled. */
431 rcu_read_lock();
432 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
433 node.node) {
434 aevent->enabled = 1;
435 }
436 rcu_read_unlock();
437
438 ret = LTTNG_OK;
439
440 error:
441 return ret;
442 }
443
444 /*
445 * Enable a single agent event for a given UST session.
446 *
447 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
448 */
449 int event_agent_enable(struct ltt_ust_session *usess,
450 struct agent *agt, struct lttng_event *event,
451 struct lttng_filter_bytecode *filter,
452 char *filter_expression)
453 {
454 int ret, created = 0;
455 struct agent_event *aevent;
456
457 assert(usess);
458 assert(event);
459 assert(agt);
460
461 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
462 ", loglevel %d and filter \"%s\"", event->name,
463 usess->id, event->loglevel_type, event->loglevel,
464 filter_expression ? filter_expression : "NULL");
465
466 aevent = agent_find_event(event->name, event->loglevel_type,
467 event->loglevel, agt);
468 if (!aevent) {
469 aevent = agent_create_event(event->name, event->loglevel_type,
470 event->loglevel, filter,
471 filter_expression);
472 if (!aevent) {
473 ret = LTTNG_ERR_NOMEM;
474 goto error;
475 }
476 created = 1;
477 }
478
479 /* Already enabled? */
480 if (aevent->enabled) {
481 goto end;
482 }
483
484 ret = agent_enable_event(aevent, agt->domain);
485 if (ret != LTTNG_OK) {
486 goto error;
487 }
488
489 /* If the event was created prior to the enable, add it to the domain. */
490 if (created) {
491 agent_add_event(aevent, agt);
492 }
493
494 end:
495 return LTTNG_OK;
496
497 error:
498 if (created) {
499 agent_destroy_event(aevent);
500 }
501 return ret;
502 }
503
504 /*
505 * Return the default event name associated with the provided UST domain. Return
506 * NULL on error.
507 */
508 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
509 {
510 const char *default_event_name = NULL;
511
512 switch (domain) {
513 case LTTNG_DOMAIN_LOG4J:
514 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
515 break;
516 case LTTNG_DOMAIN_JUL:
517 default_event_name = DEFAULT_JUL_EVENT_NAME;
518 break;
519 case LTTNG_DOMAIN_PYTHON:
520 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
521 break;
522 default:
523 assert(0);
524 }
525
526 return default_event_name;
527 }
528
529 /*
530 * Disable a given agent event for a given UST session.
531 *
532 * Must be called with the RCU read lock held.
533 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
534 */
535 static int event_agent_disable_one(struct ltt_ust_session *usess,
536 struct agent *agt, struct agent_event *aevent)
537 {
538 int ret;
539 struct ltt_ust_event *uevent = NULL;
540 struct ltt_ust_channel *uchan = NULL;
541 const char *ust_event_name, *ust_channel_name;
542
543 assert(agt);
544 assert(usess);
545 assert(aevent);
546
547 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
548 aevent->name, aevent->loglevel_type, aevent->loglevel_value,
549 usess->id);
550
551 /* Already disabled? */
552 if (!aevent->enabled) {
553 goto end;
554 }
555
556 if (agt->domain == LTTNG_DOMAIN_JUL) {
557 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
558 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
559 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
560 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
561 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
562 } else {
563 ret = LTTNG_ERR_INVALID;
564 goto error;
565 }
566
567 /*
568 * Disable it on the UST side. First get the channel reference then find
569 * the event and finally disable it.
570 */
571 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
572 (char *) ust_channel_name);
573 if (!uchan) {
574 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
575 goto error;
576 }
577
578 ust_event_name = event_get_default_agent_ust_name(agt->domain);
579 if (!ust_event_name) {
580 ret = LTTNG_ERR_FATAL;
581 goto error;
582 }
583
584 /*
585 * Agent UST event has its loglevel type forced to
586 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
587 * happens thanks to an UST filter. The following -1 is actually
588 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
589 */
590 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
591 aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
592 /* If the agent event exists, it must be available on the UST side. */
593 assert(uevent);
594
595 ret = ust_app_disable_event_glb(usess, uchan, uevent);
596 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
597 ret = LTTNG_ERR_UST_DISABLE_FAIL;
598 goto error;
599 }
600
601 /*
602 * Flag event that it's disabled so the shadow copy on the ust app side
603 * will disable it if an application shows up.
604 */
605 uevent->enabled = 0;
606
607 ret = agent_disable_event(aevent, agt->domain);
608 if (ret != LTTNG_OK) {
609 goto error;
610 }
611
612 end:
613 return LTTNG_OK;
614
615 error:
616 return ret;
617 }
618
619 /*
620 * Disable all agent events matching a given name for a given UST session.
621 *
622 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
623 */
624 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
625 char *event_name)
626 {
627 int ret = LTTNG_OK;
628 struct agent_event *aevent;
629 struct lttng_ht_iter iter;
630 struct lttng_ht_node_str *node;
631
632 assert(agt);
633 assert(usess);
634 assert(event_name);
635
636 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
637
638 rcu_read_lock();
639 agent_find_events_by_name(event_name, agt, &iter);
640 node = lttng_ht_iter_get_node_str(&iter);
641
642 if (node == NULL) {
643 DBG2("Event agent NOT found by name %s", event_name);
644 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
645 goto end;
646 }
647
648 do {
649 aevent = caa_container_of(node, struct agent_event, node);
650 ret = event_agent_disable_one(usess, agt, aevent);
651
652 if (ret != LTTNG_OK) {
653 goto end;
654 }
655
656 /* Get next duplicate agent event by name. */
657 agent_event_next_duplicate(event_name, agt, &iter);
658 node = lttng_ht_iter_get_node_str(&iter);
659 } while (node);
660 end:
661 rcu_read_unlock();
662 return ret;
663 }
664 /*
665 * Disable all agent event for a given UST session.
666 *
667 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
668 */
669 int event_agent_disable_all(struct ltt_ust_session *usess,
670 struct agent *agt)
671 {
672 int ret;
673 struct agent_event *aevent;
674 struct lttng_ht_iter iter;
675
676 assert(agt);
677 assert(usess);
678
679 /*
680 * Disable event on agent application. Continue to disable all other events
681 * if the * event is not found.
682 */
683 ret = event_agent_disable(usess, agt, "*");
684 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
685 goto error;
686 }
687
688 /* Disable every event. */
689 rcu_read_lock();
690 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
691 node.node) {
692 if (!aevent->enabled) {
693 continue;
694 }
695
696 ret = event_agent_disable(usess, agt, aevent->name);
697 if (ret != LTTNG_OK) {
698 goto error_unlock;
699 }
700 }
701 ret = LTTNG_OK;
702
703 error_unlock:
704 rcu_read_unlock();
705 error:
706 return ret;
707 }
This page took 0.069447 seconds and 3 git commands to generate.