Docs: document the meaning of a ust app channel key set to 0
[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, 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 ret = trace_ust_create_event(event, filter_expression,
178 filter, exclusion, internal_event, &uevent);
179 /* We have passed ownership */
180 filter_expression = NULL;
181 filter = NULL;
182 exclusion = NULL;
183 if (ret != LTTNG_OK) {
184 goto error;
185 }
186
187 /* Valid to set it after the goto error since uevent is still NULL */
188 to_create = 1;
189 }
190
191 if (uevent->enabled) {
192 /* It's already enabled so everything is OK */
193 ret = LTTNG_ERR_UST_EVENT_ENABLED;
194 goto end;
195 }
196
197 uevent->enabled = 1;
198
199 if (to_create) {
200 /* Create event on all UST registered apps for session */
201 ret = ust_app_create_event_glb(usess, uchan, uevent);
202 } else {
203 /* Enable event on all UST registered apps for session */
204 ret = ust_app_enable_event_glb(usess, uchan, uevent);
205 }
206
207 if (ret < 0) {
208 if (ret == -LTTNG_UST_ERR_EXIST) {
209 ret = LTTNG_ERR_UST_EVENT_EXIST;
210 goto end;
211 } else {
212 ret = LTTNG_ERR_UST_ENABLE_FAIL;
213 goto error;
214 }
215 }
216
217 if (to_create) {
218 /* Add ltt ust event to channel */
219 add_unique_ust_event(uchan->events, uevent);
220 }
221
222 DBG("Event UST %s %s in channel %s", uevent->attr.name,
223 to_create ? "created" : "enabled", uchan->name);
224
225 ret = LTTNG_OK;
226
227 end:
228 rcu_read_unlock();
229 free(filter_expression);
230 free(filter);
231 free(exclusion);
232 return ret;
233
234 error:
235 /*
236 * Only destroy event on creation time (not enabling time) because if the
237 * event is found in the channel (to_create == 0), it means that at some
238 * point the enable_event worked and it's thus valid to keep it alive.
239 * Destroying it also implies that we also destroy it's shadow copy to sync
240 * everyone up.
241 */
242 if (to_create) {
243 /* In this code path, the uevent was not added to the hash table */
244 trace_ust_destroy_event(uevent);
245 }
246 rcu_read_unlock();
247 free(filter_expression);
248 free(filter);
249 free(exclusion);
250 return ret;
251 }
252
253 /*
254 * Disable UST tracepoint of a channel from a UST session.
255 */
256 int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
257 struct ltt_ust_channel *uchan, char *event_name)
258 {
259 int ret;
260 struct ltt_ust_event *uevent;
261 struct lttng_ht_node_str *node;
262 struct lttng_ht_iter iter;
263 struct lttng_ht *ht;
264
265 assert(usess);
266 assert(uchan);
267 assert(event_name);
268
269 ht = uchan->events;
270
271 rcu_read_lock();
272
273 /*
274 * We use a custom lookup since we need the iterator for the next_duplicate
275 * call in the do while loop below.
276 */
277 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
278 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
279 node = lttng_ht_iter_get_node_str(&iter);
280 if (node == NULL) {
281 DBG2("Trace UST event NOT found by name %s", event_name);
282 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
283 goto error;
284 }
285
286 do {
287 uevent = caa_container_of(node, struct ltt_ust_event, node);
288 assert(uevent);
289
290 if (uevent->enabled == 0) {
291 /* It's already disabled so everything is OK */
292 goto next;
293 }
294
295 ret = ust_app_disable_event_glb(usess, uchan, uevent);
296 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
297 ret = LTTNG_ERR_UST_DISABLE_FAIL;
298 goto error;
299 }
300 uevent->enabled = 0;
301
302 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
303 uchan->name);
304
305 next:
306 /* Get next duplicate event by name. */
307 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
308 event_name, &iter.iter);
309 node = lttng_ht_iter_get_node_str(&iter);
310 } while (node);
311
312 ret = LTTNG_OK;
313
314 error:
315 rcu_read_unlock();
316 return ret;
317 }
318
319 /*
320 * Disable all UST tracepoints for a channel from a UST session.
321 */
322 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
323 struct ltt_ust_channel *uchan)
324 {
325 int ret, i, size, error = 0;
326 struct lttng_ht_iter iter;
327 struct ltt_ust_event *uevent = NULL;
328 struct lttng_event *events = NULL;
329
330 assert(usess);
331 assert(uchan);
332
333 rcu_read_lock();
334
335 /* Disabling existing events */
336 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
337 node.node) {
338 if (uevent->enabled == 1) {
339 ret = event_ust_disable_tracepoint(usess, uchan,
340 uevent->attr.name);
341 if (ret < 0) {
342 error = LTTNG_ERR_UST_DISABLE_FAIL;
343 continue;
344 }
345 }
346 }
347
348 /* Get all UST available events */
349 size = ust_app_list_events(&events);
350 if (size < 0) {
351 ret = LTTNG_ERR_UST_LIST_FAIL;
352 goto error;
353 }
354
355 for (i = 0; i < size; i++) {
356 ret = event_ust_disable_tracepoint(usess, uchan,
357 events[i].name);
358 if (ret < 0) {
359 /* Continue to disable the rest... */
360 error = LTTNG_ERR_UST_DISABLE_FAIL;
361 continue;
362 }
363 }
364
365 ret = error ? error : LTTNG_OK;
366 error:
367 rcu_read_unlock();
368 free(events);
369 return ret;
370 }
371
372 /*
373 * Enable all agent event for a given UST session.
374 *
375 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
376 */
377 int event_agent_enable_all(struct ltt_ust_session *usess,
378 struct agent *agt, struct lttng_event *event,
379 struct lttng_filter_bytecode *filter ,char *filter_expression)
380 {
381 int ret;
382 struct agent_event *aevent;
383 struct lttng_ht_iter iter;
384
385 assert(usess);
386
387 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
388
389 /* Enable event on agent application through TCP socket. */
390 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
391 if (ret != LTTNG_OK) {
392 goto error;
393 }
394
395 /* Flag every event that they are now enabled. */
396 rcu_read_lock();
397 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
398 node.node) {
399 aevent->enabled = 1;
400 }
401 rcu_read_unlock();
402
403 ret = LTTNG_OK;
404
405 error:
406 return ret;
407 }
408
409 /*
410 * Check if this event's filter requires the activation of application contexts
411 * and enable them in the agent.
412 * TODO: bytecode iterator does not support non-legacy application
413 * contexts yet. Not an issue for now, since they are not generated by
414 * the lttng-ctl library.
415 */
416 static int add_filter_app_ctx(struct lttng_filter_bytecode *bytecode,
417 const char *filter_expression, struct agent *agt)
418 {
419 int ret = LTTNG_OK;
420 char *provider_name = NULL, *ctx_name = NULL;
421 struct bytecode_symbol_iterator *it =
422 bytecode_symbol_iterator_create(bytecode);
423
424 if (!it) {
425 ret = LTTNG_ERR_NOMEM;
426 goto end;
427 }
428
429 do {
430 struct lttng_event_context ctx;
431 const char *symbol_name =
432 bytecode_symbol_iterator_get_name(it);
433
434 if (parse_application_context(symbol_name, &provider_name,
435 &ctx_name)) {
436 /* Not an application context. */
437 continue;
438 }
439
440 ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
441 ctx.u.app_ctx.provider_name = provider_name;
442 ctx.u.app_ctx.ctx_name = ctx_name;
443
444 /* Recognized an application context. */
445 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
446 filter_expression, provider_name, ctx_name);
447
448 ret = agent_add_context(&ctx, agt);
449 if (ret != LTTNG_OK) {
450 ERR("Failed to add application context %s:%s.",
451 provider_name, ctx_name);
452 goto end;
453 }
454
455 ret = agent_enable_context(&ctx, agt->domain);
456 if (ret != LTTNG_OK) {
457 ERR("Failed to enable application context %s:%s.",
458 provider_name, ctx_name);
459 goto end;
460 }
461
462 free(provider_name);
463 free(ctx_name);
464 provider_name = ctx_name = NULL;
465 } while (bytecode_symbol_iterator_next(it) == 0);
466 end:
467 free(provider_name);
468 free(ctx_name);
469 bytecode_symbol_iterator_destroy(it);
470 return ret;
471 }
472
473 /*
474 * Enable a single agent event for a given UST session.
475 *
476 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
477 */
478 int event_agent_enable(struct ltt_ust_session *usess,
479 struct agent *agt, struct lttng_event *event,
480 struct lttng_filter_bytecode *filter,
481 char *filter_expression)
482 {
483 int ret, created = 0;
484 struct agent_event *aevent;
485
486 assert(usess);
487 assert(event);
488 assert(agt);
489
490 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
491 ", loglevel %d and filter \"%s\"", event->name,
492 usess->id, event->loglevel_type, event->loglevel,
493 filter_expression ? filter_expression : "NULL");
494
495 aevent = agent_find_event(event->name, event->loglevel_type,
496 event->loglevel, filter_expression, agt);
497 if (!aevent) {
498 aevent = agent_create_event(event->name, event->loglevel_type,
499 event->loglevel, filter,
500 filter_expression);
501 if (!aevent) {
502 ret = LTTNG_ERR_NOMEM;
503 goto error;
504 }
505
506 created = 1;
507 }
508
509 /* Already enabled? */
510 if (aevent->enabled) {
511 goto end;
512 }
513
514 if (created && filter) {
515 ret = add_filter_app_ctx(filter, filter_expression, agt);
516 if (ret != LTTNG_OK) {
517 goto error;
518 }
519 }
520
521 ret = agent_enable_event(aevent, agt->domain);
522 if (ret != LTTNG_OK) {
523 goto error;
524 }
525
526 /* If the event was created prior to the enable, add it to the domain. */
527 if (created) {
528 agent_add_event(aevent, agt);
529 }
530
531 end:
532 return LTTNG_OK;
533
534 error:
535 if (created) {
536 agent_destroy_event(aevent);
537 }
538 return ret;
539 }
540
541 /*
542 * Return the default event name associated with the provided UST domain. Return
543 * NULL on error.
544 */
545 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
546 {
547 const char *default_event_name = NULL;
548
549 switch (domain) {
550 case LTTNG_DOMAIN_LOG4J:
551 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
552 break;
553 case LTTNG_DOMAIN_JUL:
554 default_event_name = DEFAULT_JUL_EVENT_NAME;
555 break;
556 case LTTNG_DOMAIN_PYTHON:
557 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
558 break;
559 default:
560 assert(0);
561 }
562
563 return default_event_name;
564 }
565
566 /*
567 * Disable a given agent event for a given UST session.
568 *
569 * Must be called with the RCU read lock held.
570 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
571 */
572 static int event_agent_disable_one(struct ltt_ust_session *usess,
573 struct agent *agt, struct agent_event *aevent)
574 {
575 int ret;
576 struct ltt_ust_event *uevent = NULL;
577 struct ltt_ust_channel *uchan = NULL;
578 const char *ust_event_name, *ust_channel_name;
579
580 assert(agt);
581 assert(usess);
582 assert(aevent);
583
584 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
585 aevent->name, aevent->loglevel_type, aevent->loglevel_value,
586 usess->id);
587
588 /* Already disabled? */
589 if (!aevent->enabled) {
590 goto end;
591 }
592
593 if (agt->domain == LTTNG_DOMAIN_JUL) {
594 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
595 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
596 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
597 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
598 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
599 } else {
600 ret = LTTNG_ERR_INVALID;
601 goto error;
602 }
603
604 /*
605 * Disable it on the UST side. First get the channel reference then find
606 * the event and finally disable it.
607 */
608 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
609 (char *) ust_channel_name);
610 if (!uchan) {
611 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
612 goto error;
613 }
614
615 ust_event_name = event_get_default_agent_ust_name(agt->domain);
616 if (!ust_event_name) {
617 ret = LTTNG_ERR_FATAL;
618 goto error;
619 }
620
621 /*
622 * Agent UST event has its loglevel type forced to
623 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
624 * happens thanks to an UST filter. The following -1 is actually
625 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
626 */
627 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
628 aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
629 /* If the agent event exists, it must be available on the UST side. */
630 assert(uevent);
631
632 ret = ust_app_disable_event_glb(usess, uchan, uevent);
633 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
634 ret = LTTNG_ERR_UST_DISABLE_FAIL;
635 goto error;
636 }
637
638 /*
639 * Flag event that it's disabled so the shadow copy on the ust app side
640 * will disable it if an application shows up.
641 */
642 uevent->enabled = 0;
643
644 ret = agent_disable_event(aevent, agt->domain);
645 if (ret != LTTNG_OK) {
646 goto error;
647 }
648
649 end:
650 return LTTNG_OK;
651
652 error:
653 return ret;
654 }
655
656 /*
657 * Disable all agent events matching a given name for a given UST session.
658 *
659 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
660 */
661 int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
662 char *event_name)
663 {
664 int ret = LTTNG_OK;
665 struct agent_event *aevent;
666 struct lttng_ht_iter iter;
667 struct lttng_ht_node_str *node;
668
669 assert(agt);
670 assert(usess);
671 assert(event_name);
672
673 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
674
675 rcu_read_lock();
676 agent_find_events_by_name(event_name, agt, &iter);
677 node = lttng_ht_iter_get_node_str(&iter);
678
679 if (node == NULL) {
680 DBG2("Event agent NOT found by name %s", event_name);
681 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
682 goto end;
683 }
684
685 do {
686 aevent = caa_container_of(node, struct agent_event, node);
687 ret = event_agent_disable_one(usess, agt, aevent);
688
689 if (ret != LTTNG_OK) {
690 goto end;
691 }
692
693 /* Get next duplicate agent event by name. */
694 agent_event_next_duplicate(event_name, agt, &iter);
695 node = lttng_ht_iter_get_node_str(&iter);
696 } while (node);
697 end:
698 rcu_read_unlock();
699 return ret;
700 }
701 /*
702 * Disable all agent event for a given UST session.
703 *
704 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
705 */
706 int event_agent_disable_all(struct ltt_ust_session *usess,
707 struct agent *agt)
708 {
709 int ret;
710 struct agent_event *aevent;
711 struct lttng_ht_iter iter;
712
713 assert(agt);
714 assert(usess);
715
716 /*
717 * Disable event on agent application. Continue to disable all other events
718 * if the * event is not found.
719 */
720 ret = event_agent_disable(usess, agt, "*");
721 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
722 goto error;
723 }
724
725 /* Disable every event. */
726 rcu_read_lock();
727 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
728 node.node) {
729 if (!aevent->enabled) {
730 continue;
731 }
732
733 ret = event_agent_disable(usess, agt, aevent->name);
734 if (ret != LTTNG_OK) {
735 goto error_unlock;
736 }
737 }
738 ret = LTTNG_OK;
739
740 error_unlock:
741 rcu_read_unlock();
742 error:
743 return ret;
744 }
This page took 0.053654 seconds and 4 git commands to generate.