Report error if any disable action fails
[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, error = 0;
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 error = LTTNG_ERR_UST_DISABLE_FAIL;
380 continue;
381 }
382 }
383 }
384
385 /* Get all UST available events */
386 size = ust_app_list_events(&events);
387 if (size < 0) {
388 ret = LTTNG_ERR_UST_LIST_FAIL;
389 goto error;
390 }
391
392 for (i = 0; i < size; i++) {
393 ret = event_ust_disable_tracepoint(usess, uchan,
394 events[i].name);
395 if (ret < 0) {
396 /* Continue to disable the rest... */
397 error = LTTNG_ERR_UST_DISABLE_FAIL;
398 continue;
399 }
400 }
401
402 ret = error ? error : LTTNG_OK;
403 error:
404 rcu_read_unlock();
405 free(events);
406 return ret;
407 }
408
409 /*
410 * Enable all agent event for a given UST session.
411 *
412 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
413 */
414 int event_agent_enable_all(struct ltt_ust_session *usess,
415 struct agent *agt, struct lttng_event *event,
416 struct lttng_filter_bytecode *filter ,char *filter_expression)
417 {
418 int ret;
419 struct agent_event *aevent;
420 struct lttng_ht_iter iter;
421
422 assert(usess);
423
424 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
425
426 /* Enable event on agent application through TCP socket. */
427 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
428 if (ret != LTTNG_OK) {
429 goto error;
430 }
431
432 /* Flag every event that they are now enabled. */
433 rcu_read_lock();
434 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
435 node.node) {
436 aevent->enabled = 1;
437 }
438 rcu_read_unlock();
439
440 ret = LTTNG_OK;
441
442 error:
443 return ret;
444 }
445
446 /*
447 * Enable a single agent event for a given UST session.
448 *
449 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
450 */
451 int event_agent_enable(struct ltt_ust_session *usess,
452 struct agent *agt, struct lttng_event *event,
453 struct lttng_filter_bytecode *filter,
454 char *filter_expression)
455 {
456 int ret, created = 0;
457 struct agent_event *aevent;
458
459 assert(usess);
460 assert(event);
461 assert(agt);
462
463 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
464 ", loglevel %d and filter \"%s\"", event->name,
465 usess->id, event->loglevel_type, event->loglevel,
466 filter_expression ? filter_expression : "NULL");
467
468 aevent = agent_find_event(event->name, event->loglevel_type,
469 event->loglevel, agt);
470 if (!aevent) {
471 aevent = agent_create_event(event->name, event->loglevel_type,
472 event->loglevel, filter,
473 filter_expression);
474 if (!aevent) {
475 ret = LTTNG_ERR_NOMEM;
476 goto error;
477 }
478 created = 1;
479 }
480
481 /* Already enabled? */
482 if (aevent->enabled) {
483 goto end;
484 }
485
486 ret = agent_enable_event(aevent, agt->domain);
487 if (ret != LTTNG_OK) {
488 goto error;
489 }
490
491 /* If the event was created prior to the enable, add it to the domain. */
492 if (created) {
493 agent_add_event(aevent, agt);
494 }
495
496 end:
497 return LTTNG_OK;
498
499 error:
500 if (created) {
501 agent_destroy_event(aevent);
502 }
503 return ret;
504 }
505
506 /*
507 * Return the default event name associated with the provided UST domain. Return
508 * NULL on error.
509 */
510 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
511 {
512 const char *default_event_name = NULL;
513
514 switch (domain) {
515 case LTTNG_DOMAIN_LOG4J:
516 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
517 break;
518 case LTTNG_DOMAIN_JUL:
519 default_event_name = DEFAULT_JUL_EVENT_NAME;
520 break;
521 case LTTNG_DOMAIN_PYTHON:
522 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
523 break;
524 default:
525 assert(0);
526 }
527
528 return default_event_name;
529 }
530
531 /*
532 * Disable a given agent event for a given UST session.
533 *
534 * Must be called with the RCU read lock held.
535 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
536 */
537 static int event_agent_disable_one(struct ltt_ust_session *usess,
538 struct agent *agt, struct agent_event *aevent)
539 {
540 int ret;
541 struct ltt_ust_event *uevent = NULL;
542 struct ltt_ust_channel *uchan = NULL;
543 const char *ust_event_name, *ust_channel_name;
544
545 assert(agt);
546 assert(usess);
547 assert(aevent);
548
549 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
550 aevent->name, aevent->loglevel_type, aevent->loglevel_value,
551 usess->id);
552
553 /* Already disabled? */
554 if (!aevent->enabled) {
555 goto end;
556 }
557
558 if (agt->domain == LTTNG_DOMAIN_JUL) {
559 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
560 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
561 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
562 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
563 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
564 } else {
565 ret = LTTNG_ERR_INVALID;
566 goto error;
567 }
568
569 /*
570 * Disable it on the UST side. First get the channel reference then find
571 * the event and finally disable it.
572 */
573 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
574 (char *) ust_channel_name);
575 if (!uchan) {
576 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
577 goto error;
578 }
579
580 ust_event_name = event_get_default_agent_ust_name(agt->domain);
581 if (!ust_event_name) {
582 ret = LTTNG_ERR_FATAL;
583 goto error;
584 }
585
586 /*
587 * Agent UST event has its loglevel type forced to
588 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
589 * happens thanks to an UST filter. The following -1 is actually
590 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
591 */
592 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
593 aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
594 /* If the agent event exists, it must be available on the UST side. */
595 assert(uevent);
596
597 ret = ust_app_disable_event_glb(usess, uchan, uevent);
598 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
599 ret = LTTNG_ERR_UST_DISABLE_FAIL;
600 goto error;
601 }
602
603 /*
604 * Flag event that it's disabled so the shadow copy on the ust app side
605 * will disable it if an application shows up.
606 */
607 uevent->enabled = 0;
608
609 ret = agent_disable_event(aevent, agt->domain);
610 if (ret != LTTNG_OK) {
611 goto error;
612 }
613
614 end:
615 return LTTNG_OK;
616
617 error:
618 return ret;
619 }
620
621 /*
622 * Disable all agent events matching a given name for a given UST session.
623 *
624 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
625 */
626 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
627 char *event_name)
628 {
629 int ret = LTTNG_OK;
630 struct agent_event *aevent;
631 struct lttng_ht_iter iter;
632 struct lttng_ht_node_str *node;
633
634 assert(agt);
635 assert(usess);
636 assert(event_name);
637
638 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
639
640 rcu_read_lock();
641 agent_find_events_by_name(event_name, agt, &iter);
642 node = lttng_ht_iter_get_node_str(&iter);
643
644 if (node == NULL) {
645 DBG2("Event agent NOT found by name %s", event_name);
646 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
647 goto end;
648 }
649
650 do {
651 aevent = caa_container_of(node, struct agent_event, node);
652 ret = event_agent_disable_one(usess, agt, aevent);
653
654 if (ret != LTTNG_OK) {
655 goto end;
656 }
657
658 /* Get next duplicate agent event by name. */
659 agent_event_next_duplicate(event_name, agt, &iter);
660 node = lttng_ht_iter_get_node_str(&iter);
661 } while (node);
662 end:
663 rcu_read_unlock();
664 return ret;
665 }
666 /*
667 * Disable all agent event for a given UST session.
668 *
669 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
670 */
671 int event_agent_disable_all(struct ltt_ust_session *usess,
672 struct agent *agt)
673 {
674 int ret;
675 struct agent_event *aevent;
676 struct lttng_ht_iter iter;
677
678 assert(agt);
679 assert(usess);
680
681 /*
682 * Disable event on agent application. Continue to disable all other events
683 * if the * event is not found.
684 */
685 ret = event_agent_disable(usess, agt, "*");
686 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
687 goto error;
688 }
689
690 /* Disable every event. */
691 rcu_read_lock();
692 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
693 node.node) {
694 if (!aevent->enabled) {
695 continue;
696 }
697
698 ret = event_agent_disable(usess, agt, aevent->name);
699 if (ret != LTTNG_OK) {
700 goto error_unlock;
701 }
702 }
703 ret = LTTNG_OK;
704
705 error_unlock:
706 rcu_read_unlock();
707 error:
708 return ret;
709 }
This page took 0.070375 seconds and 4 git commands to generate.