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