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