Fix: define _LGPL_SOURCE in C files
[deliverable/lttng-tools.git] / src / bin / lttng-sessiond / event.c
CommitLineData
54d01ffb
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
54d01ffb 7 *
d14d33bf
AM
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.
54d01ffb 12 *
d14d33bf
AM
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.
54d01ffb
DG
16 */
17
be040666 18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
d87bfb32 20#include <errno.h>
54d01ffb 21#include <urcu/list.h>
2bdd86d4 22#include <string.h>
54d01ffb
DG
23
24#include <lttng/lttng.h>
db758600 25#include <common/error.h>
10a8a223 26#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
27
28#include "channel.h"
29#include "event.h"
4771f025 30#include "kernel.h"
be6a6276 31#include "lttng-sessiond.h"
9df8df5e 32#include "ust-ctl.h"
edb67388
DG
33#include "ust-app.h"
34#include "trace-kernel.h"
35#include "trace-ust.h"
54d01ffb 36
025faf73
DG
37/*
38 * Add unique UST event based on the event name, filter bytecode and loglevel.
39 */
18eace3b
DG
40static void add_unique_ust_event(struct lttng_ht *ht,
41 struct ltt_ust_event *event)
42{
43 struct cds_lfht_node *node_ptr;
44 struct ltt_ust_ht_key key;
45
46 assert(ht);
47 assert(ht->ht);
48 assert(event);
49
50 key.name = event->attr.name;
51 key.filter = (struct lttng_filter_bytecode *) event->filter;
52 key.loglevel = event->attr.loglevel;
4031e53e 53 key.exclusion = event->exclusion;
18eace3b
DG
54
55 node_ptr = cds_lfht_add_unique(ht->ht,
56 ht->hash_fct(event->node.key, lttng_ht_seed),
57 trace_ust_ht_match_event, &key, &event->node.node);
58 assert(node_ptr == &event->node.node);
59}
60
54d01ffb 61/*
7a3d1328 62 * Disable kernel tracepoint event for a channel from the kernel session.
54d01ffb 63 */
d3a56674
DG
64int event_kernel_disable_tracepoint(struct ltt_kernel_channel *kchan,
65 char *event_name)
54d01ffb
DG
66{
67 int ret;
68 struct ltt_kernel_event *kevent;
69
0525e9ae
DG
70 assert(kchan);
71
54d01ffb
DG
72 kevent = trace_kernel_get_event_by_name(event_name, kchan);
73 if (kevent == NULL) {
f73fabfd 74 ret = LTTNG_ERR_NO_EVENT;
54d01ffb
DG
75 goto error;
76 }
77
78 ret = kernel_disable_event(kevent);
79 if (ret < 0) {
f73fabfd 80 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
54d01ffb
DG
81 goto error;
82 }
83
84 DBG("Kernel event %s disable for channel %s.",
85 kevent->event->name, kchan->channel->name);
86
f73fabfd 87 ret = LTTNG_OK;
54d01ffb
DG
88
89error:
90 return ret;
91}
92
6e911cad
MD
93/*
94 * Enable kernel system call for a channel from the kernel session.
95 */
96int event_kernel_enable_syscall(struct ltt_kernel_channel *kchan,
97 char *syscall_name)
98{
99 int ret;
100
101 assert(kchan);
102
103 ret = kernel_enable_syscall(syscall_name, kchan);
104 if (ret < 0) {
105 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
106 goto error;
107 }
108
109 DBG("Kernel event %s enable for channel %s.",
110 syscall_name, kchan->channel->name);
111
112 ret = LTTNG_OK;
113
114error:
115 return ret;
116}
117
118/*
119 * Disable kernel system call for a channel from the kernel session.
120 */
121int event_kernel_disable_syscall(struct ltt_kernel_channel *kchan,
122 char *syscall_name)
123{
124 int ret;
125
126 assert(kchan);
127
128 ret = kernel_disable_syscall(syscall_name, kchan);
129 if (ret < 0) {
130 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
131 goto error;
132 }
133
134 DBG("Kernel syscall %s disable for channel %s.",
135 syscall_name[0] == '\0' ? "<all>" : syscall_name,
136 kchan->channel->name);
137
138 ret = LTTNG_OK;
139
140error:
141 return ret;
142}
143
54d01ffb 144/*
7a3d1328 145 * Disable kernel tracepoint events for a channel from the kernel session.
54d01ffb 146 */
d3a56674 147int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel *kchan)
54d01ffb
DG
148{
149 int ret;
150 struct ltt_kernel_event *kevent;
151
0525e9ae
DG
152 assert(kchan);
153
54d01ffb
DG
154 /* For each event in the kernel session */
155 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
156 ret = kernel_disable_event(kevent);
157 if (ret < 0) {
158 /* We continue disabling the rest */
159 continue;
160 }
161 }
f73fabfd 162 ret = LTTNG_OK;
7a3d1328
MD
163 return ret;
164}
165
7a3d1328
MD
166/*
167 * Disable all kernel event for a channel from the kernel session.
168 */
d3a56674 169int event_kernel_disable_all(struct ltt_kernel_channel *kchan)
7a3d1328
MD
170{
171 int ret;
172
0525e9ae
DG
173 assert(kchan);
174
d3a56674 175 ret = event_kernel_disable_all_tracepoints(kchan);
f73fabfd 176 if (ret != LTTNG_OK)
7a3d1328 177 return ret;
6e911cad 178 ret = event_kernel_disable_syscall(kchan, "");
54d01ffb
DG
179 return ret;
180}
181
182/*
7a3d1328 183 * Enable kernel tracepoint event for a channel from the kernel session.
54d01ffb 184 */
d3a56674
DG
185int event_kernel_enable_tracepoint(struct ltt_kernel_channel *kchan,
186 struct lttng_event *event)
54d01ffb
DG
187{
188 int ret;
189 struct ltt_kernel_event *kevent;
190
0525e9ae
DG
191 assert(kchan);
192 assert(event);
193
54d01ffb
DG
194 kevent = trace_kernel_get_event_by_name(event->name, kchan);
195 if (kevent == NULL) {
196 ret = kernel_create_event(event, kchan);
197 if (ret < 0) {
bd29c13d
DG
198 switch (-ret) {
199 case EEXIST:
f73fabfd 200 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
201 break;
202 case ENOSYS:
f73fabfd 203 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d
DG
204 break;
205 default:
f73fabfd 206 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
bd29c13d 207 break;
d87bfb32 208 }
7a3d1328 209 goto end;
54d01ffb
DG
210 }
211 } else if (kevent->enabled == 0) {
212 ret = kernel_enable_event(kevent);
213 if (ret < 0) {
f73fabfd 214 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
7a3d1328 215 goto end;
54d01ffb 216 }
42224349
DG
217 } else {
218 /* At this point, the event is considered enabled */
f73fabfd 219 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349 220 goto end;
54d01ffb 221 }
42224349 222
f73fabfd 223 ret = LTTNG_OK;
7a3d1328 224end:
54d01ffb
DG
225 return ret;
226}
227
228/*
7a3d1328 229 * Enable all kernel tracepoint events of a channel of the kernel session.
54d01ffb 230 */
d3a56674
DG
231int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel *kchan,
232 int kernel_tracer_fd)
54d01ffb
DG
233{
234 int size, i, ret;
235 struct ltt_kernel_event *kevent;
8f69e5eb 236 struct lttng_event *event_list = NULL;
54d01ffb 237
0525e9ae
DG
238 assert(kchan);
239
54d01ffb
DG
240 /* For each event in the kernel session */
241 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
8f69e5eb
DG
242 if (kevent->enabled == 0) {
243 ret = kernel_enable_event(kevent);
244 if (ret < 0) {
245 /* Enable failed but still continue */
246 continue;
247 }
54d01ffb
DG
248 }
249 }
250
251 size = kernel_list_events(kernel_tracer_fd, &event_list);
252 if (size < 0) {
f73fabfd 253 ret = LTTNG_ERR_KERN_LIST_FAIL;
7a3d1328 254 goto end;
54d01ffb
DG
255 }
256
257 for (i = 0; i < size; i++) {
258 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
259 if (kevent == NULL) {
260 /* Default event type for enable all */
261 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
262 /* Enable each single tracepoint event */
263 ret = kernel_create_event(&event_list[i], kchan);
264 if (ret < 0) {
265 /* Ignore error here and continue */
266 }
267 }
268 }
54d01ffb 269 free(event_list);
8f69e5eb 270
f73fabfd 271 ret = LTTNG_OK;
7a3d1328 272end:
54d01ffb
DG
273 return ret;
274}
8c9ae521 275
7a3d1328
MD
276/*
277 * Enable all kernel events of a channel of the kernel session.
278 */
d3a56674
DG
279int event_kernel_enable_all(struct ltt_kernel_channel *kchan,
280 int kernel_tracer_fd)
7a3d1328 281{
6bd8707a 282 int tp_ret;
7a3d1328 283
0525e9ae
DG
284 assert(kchan);
285
d3a56674 286 tp_ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
f73fabfd 287 if (tp_ret != LTTNG_OK) {
7a3d1328
MD
288 goto end;
289 }
bd29c13d
DG
290
291 /*
292 * Reaching this code path means that all tracepoints were enabled without
293 * errors so we ignore the error value of syscalls.
294 *
295 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
296 * is not considered an error that need to be returned to the client since
297 * tracepoints did not fail. Future work will allow us to send back
298 * multiple errors to the client in one API call.
299 */
6e911cad 300 (void) event_kernel_enable_syscall(kchan, "");
bd29c13d 301
7a3d1328 302end:
bd29c13d 303 return tp_ret;
8c9ae521 304}
2bdd86d4 305
7f79d3a1
DG
306/*
307 * ============================
308 * UST : The Ultimate Frontier!
309 * ============================
310 */
311
76d45b40
DG
312/*
313 * Enable all UST tracepoints for a channel from a UST session.
314 */
7972aab2 315int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess,
6b453b5e
JG
316 struct ltt_ust_channel *uchan,
317 char *filter_expression,
318 struct lttng_filter_bytecode *filter)
76d45b40 319{
6775595e 320 int ret, i, size;
bec39940 321 struct lttng_ht_iter iter;
76d45b40 322 struct ltt_ust_event *uevent = NULL;
7f79d3a1 323 struct lttng_event *events = NULL;
76d45b40 324
0525e9ae
DG
325 assert(usess);
326 assert(uchan);
327
025faf73
DG
328 rcu_read_lock();
329
7972aab2
DG
330 /* Enable existing events */
331 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
332 node.node) {
333 if (uevent->enabled == 0) {
334 ret = ust_app_enable_event_glb(usess, uchan, uevent);
335 if (ret < 0) {
76d45b40
DG
336 continue;
337 }
7972aab2
DG
338 uevent->enabled = 1;
339 }
340 }
76d45b40 341
7972aab2
DG
342 /* Get all UST available events */
343 size = ust_app_list_events(&events);
344 if (size < 0) {
345 ret = LTTNG_ERR_UST_LIST_FAIL;
346 goto error;
347 }
76d45b40 348
7972aab2
DG
349 for (i = 0; i < size; i++) {
350 /*
351 * Check if event exist and if so, continue since it was enable
352 * previously.
353 */
354 uevent = trace_ust_find_event(uchan->events, events[i].name, filter,
10646003 355 events[i].loglevel, NULL);
7972aab2 356 if (uevent != NULL) {
76d45b40
DG
357 ret = ust_app_enable_event_pid(usess, uchan, uevent,
358 events[i].pid);
359 if (ret < 0) {
7972aab2 360 if (ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 361 ret = LTTNG_ERR_UST_ENABLE_FAIL;
7972aab2 362 goto error;
76d45b40 363 }
76d45b40 364 }
7972aab2
DG
365 continue;
366 }
76d45b40 367
7972aab2 368 /* Create ust event */
6b453b5e
JG
369 uevent = trace_ust_create_event(&events[i], filter_expression,
370 filter, NULL);
7972aab2
DG
371 if (uevent == NULL) {
372 ret = LTTNG_ERR_FATAL;
373 goto error_destroy;
76d45b40
DG
374 }
375
7972aab2
DG
376 /* Create event for the specific PID */
377 ret = ust_app_enable_event_pid(usess, uchan, uevent,
378 events[i].pid);
379 if (ret < 0) {
380 if (ret == -LTTNG_UST_ERR_EXIST) {
381 ret = LTTNG_ERR_UST_EVENT_EXIST;
382 goto error;
383 } else {
384 ret = LTTNG_ERR_UST_ENABLE_FAIL;
385 goto error_destroy;
386 }
387 }
388
389 uevent->enabled = 1;
390 /* Add ltt ust event to channel */
391 rcu_read_lock();
392 add_unique_ust_event(uchan->events, uevent);
393 rcu_read_unlock();
76d45b40 394 }
7972aab2 395 free(events);
76d45b40 396
025faf73 397 rcu_read_unlock();
f73fabfd 398 return LTTNG_OK;
76d45b40 399
7f79d3a1 400error_destroy:
76d45b40 401 trace_ust_destroy_event(uevent);
7f79d3a1
DG
402
403error:
404 free(events);
025faf73 405 rcu_read_unlock();
76d45b40
DG
406 return ret;
407}
408
2bdd86d4
MD
409/*
410 * Enable UST tracepoint event for a channel from a UST session.
411 */
7972aab2 412int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
025faf73 413 struct ltt_ust_channel *uchan, struct lttng_event *event,
6b453b5e 414 char *filter_expression,
f1613f52
JI
415 struct lttng_filter_bytecode *filter,
416 struct lttng_event_exclusion *exclusion)
2bdd86d4 417{
f73fabfd 418 int ret = LTTNG_OK, to_create = 0;
edb67388
DG
419 struct ltt_ust_event *uevent;
420
0525e9ae
DG
421 assert(usess);
422 assert(uchan);
423 assert(event);
424
18eace3b
DG
425 rcu_read_lock();
426
025faf73 427 uevent = trace_ust_find_event(uchan->events, event->name, filter,
10646003 428 event->loglevel, exclusion);
edb67388 429 if (uevent == NULL) {
6b453b5e
JG
430 uevent = trace_ust_create_event(event, filter_expression,
431 filter, exclusion);
edb67388 432 if (uevent == NULL) {
95a82664 433 ret = LTTNG_ERR_UST_ENABLE_FAIL;
edb67388
DG
434 goto error;
435 }
025faf73 436
fc34caaa 437 /* Valid to set it after the goto error since uevent is still NULL */
edb67388
DG
438 to_create = 1;
439 }
2bdd86d4 440
7f79d3a1
DG
441 if (uevent->enabled) {
442 /* It's already enabled so everything is OK */
5bcdda4f 443 ret = LTTNG_ERR_UST_EVENT_ENABLED;
7f79d3a1
DG
444 goto end;
445 }
446
fc34caaa
DG
447 uevent->enabled = 1;
448
7972aab2
DG
449 if (to_create) {
450 /* Create event on all UST registered apps for session */
451 ret = ust_app_create_event_glb(usess, uchan, uevent);
452 } else {
453 /* Enable event on all UST registered apps for session */
454 ret = ust_app_enable_event_glb(usess, uchan, uevent);
455 }
48842b30 456
7972aab2
DG
457 if (ret < 0) {
458 if (ret == -LTTNG_UST_ERR_EXIST) {
459 ret = LTTNG_ERR_UST_EVENT_EXIST;
460 goto end;
461 } else {
462 ret = LTTNG_ERR_UST_ENABLE_FAIL;
463 goto error;
edb67388 464 }
2bdd86d4 465 }
48842b30 466
7f79d3a1 467 if (to_create) {
fc34caaa 468 /* Add ltt ust event to channel */
18eace3b 469 add_unique_ust_event(uchan->events, uevent);
7f79d3a1 470 }
edb67388 471
7f79d3a1
DG
472 DBG("Event UST %s %s in channel %s", uevent->attr.name,
473 to_create ? "created" : "enabled", uchan->name);
474
f73fabfd 475 ret = LTTNG_OK;
fc34caaa 476
fb89d070 477end:
18eace3b 478 rcu_read_unlock();
fc34caaa 479 return ret;
edb67388
DG
480
481error:
fc34caaa
DG
482 /*
483 * Only destroy event on creation time (not enabling time) because if the
484 * event is found in the channel (to_create == 0), it means that at some
485 * point the enable_event worked and it's thus valid to keep it alive.
486 * Destroying it also implies that we also destroy it's shadow copy to sync
487 * everyone up.
488 */
489 if (to_create) {
490 /* In this code path, the uevent was not added to the hash table */
491 trace_ust_destroy_event(uevent);
492 }
18eace3b 493 rcu_read_unlock();
2bdd86d4
MD
494 return ret;
495}
496
7f79d3a1
DG
497/*
498 * Disable UST tracepoint of a channel from a UST session.
499 */
7972aab2 500int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
7f79d3a1 501 struct ltt_ust_channel *uchan, char *event_name)
2bdd86d4
MD
502{
503 int ret;
7f79d3a1 504 struct ltt_ust_event *uevent;
18eace3b
DG
505 struct lttng_ht_node_str *node;
506 struct lttng_ht_iter iter;
18eace3b 507 struct lttng_ht *ht;
2bdd86d4 508
0525e9ae
DG
509 assert(usess);
510 assert(uchan);
511 assert(event_name);
512
18eace3b
DG
513 ht = uchan->events;
514
18eace3b 515 rcu_read_lock();
025faf73
DG
516
517 /*
518 * We use a custom lookup since we need the iterator for the next_duplicate
519 * call in the do while loop below.
520 */
521 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
522 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
18eace3b
DG
523 node = lttng_ht_iter_get_node_str(&iter);
524 if (node == NULL) {
525 DBG2("Trace UST event NOT found by name %s", event_name);
f73fabfd 526 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
7f79d3a1 527 goto error;
2bdd86d4 528 }
7f79d3a1 529
18eace3b
DG
530 do {
531 uevent = caa_container_of(node, struct ltt_ust_event, node);
025faf73
DG
532 assert(uevent);
533
18eace3b
DG
534 if (uevent->enabled == 0) {
535 /* It's already disabled so everything is OK */
a1dcaf0f 536 goto next;
7f79d3a1 537 }
18eace3b 538
7972aab2
DG
539 ret = ust_app_disable_event_glb(usess, uchan, uevent);
540 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
541 ret = LTTNG_ERR_UST_DISABLE_FAIL;
18eace3b
DG
542 goto error;
543 }
18eace3b
DG
544 uevent->enabled = 0;
545
025faf73
DG
546 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
547 uchan->name);
548
a1dcaf0f 549next:
18eace3b
DG
550 /* Get next duplicate event by name. */
551 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
552 event_name, &iter.iter);
553 node = lttng_ht_iter_get_node_str(&iter);
554 } while (node);
7f79d3a1 555
f73fabfd 556 ret = LTTNG_OK;
7f79d3a1 557
7f79d3a1 558error:
18eace3b 559 rcu_read_unlock();
7f79d3a1
DG
560 return ret;
561}
562
563/*
564 * Disable all UST tracepoints for a channel from a UST session.
565 */
7972aab2 566int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
7f79d3a1
DG
567 struct ltt_ust_channel *uchan)
568{
6775595e 569 int ret, i, size;
bec39940 570 struct lttng_ht_iter iter;
7f79d3a1
DG
571 struct ltt_ust_event *uevent = NULL;
572 struct lttng_event *events = NULL;
573
0525e9ae
DG
574 assert(usess);
575 assert(uchan);
576
025faf73
DG
577 rcu_read_lock();
578
7972aab2
DG
579 /* Disabling existing events */
580 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
581 node.node) {
582 if (uevent->enabled == 1) {
583 ret = event_ust_disable_tracepoint(usess, uchan,
584 uevent->attr.name);
585 if (ret < 0) {
7f79d3a1
DG
586 continue;
587 }
588 }
7f79d3a1 589 }
7972aab2
DG
590
591 /* Get all UST available events */
592 size = ust_app_list_events(&events);
593 if (size < 0) {
594 ret = LTTNG_ERR_UST_LIST_FAIL;
7f79d3a1
DG
595 goto error;
596 }
597
7972aab2
DG
598 for (i = 0; i < size; i++) {
599 ret = event_ust_disable_tracepoint(usess, uchan,
600 events[i].name);
601 if (ret != LTTNG_OK) {
602 /* Continue to disable the rest... */
603 continue;
604 }
605 }
606 free(events);
607
025faf73 608 rcu_read_unlock();
f73fabfd 609 return LTTNG_OK;
7f79d3a1
DG
610
611error:
612 free(events);
025faf73 613 rcu_read_unlock();
2bdd86d4
MD
614 return ret;
615}
f20baf8e
DG
616
617/*
022d91ba 618 * Enable all agent event for a given UST session.
f20baf8e
DG
619 *
620 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
621 */
022d91ba 622int event_agent_enable_all(struct ltt_ust_session *usess,
fefd409b
DG
623 struct agent *agt, struct lttng_event *event,
624 struct lttng_filter_bytecode *filter)
f20baf8e
DG
625{
626 int ret;
fefd409b 627 struct agent_event *aevent;
f20baf8e
DG
628 struct lttng_ht_iter iter;
629
630 assert(usess);
631
022d91ba 632 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
f20baf8e 633
022d91ba 634 /* Enable event on agent application through TCP socket. */
fefd409b 635 ret = event_agent_enable(usess, agt, event, filter);
f20baf8e
DG
636 if (ret != LTTNG_OK) {
637 goto error;
638 }
639
640 /* Flag every event that they are now enabled. */
641 rcu_read_lock();
fefd409b 642 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
f20baf8e 643 node.node) {
fefd409b 644 aevent->enabled = 1;
f20baf8e
DG
645 }
646 rcu_read_unlock();
647
648 ret = LTTNG_OK;
649
650error:
651 return ret;
652}
653
654/*
022d91ba 655 * Enable a single agent event for a given UST session.
f20baf8e
DG
656 *
657 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
658 */
fefd409b
DG
659int event_agent_enable(struct ltt_ust_session *usess,
660 struct agent *agt, struct lttng_event *event,
be6a6276 661 struct lttng_filter_bytecode *filter)
f20baf8e
DG
662{
663 int ret, created = 0;
fefd409b 664 struct agent_event *aevent;
f20baf8e
DG
665
666 assert(usess);
667 assert(event);
fefd409b 668 assert(agt);
f20baf8e 669
022d91ba 670 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
b2064f54
DG
671 "and loglevel %d", event->name, usess->id, event->loglevel_type,
672 event->loglevel);
f20baf8e 673
fefd409b
DG
674 aevent = agent_find_event(event->name, event->loglevel, agt);
675 if (!aevent) {
676 aevent = agent_create_event(event->name, filter);
677 if (!aevent) {
f20baf8e
DG
678 ret = LTTNG_ERR_NOMEM;
679 goto error;
680 }
fefd409b
DG
681 aevent->loglevel = event->loglevel;
682 aevent->loglevel_type = event->loglevel_type;
f20baf8e
DG
683 created = 1;
684 }
685
686 /* Already enabled? */
fefd409b 687 if (aevent->enabled) {
f20baf8e
DG
688 goto end;
689 }
690
fefd409b 691 ret = agent_enable_event(aevent, agt->domain);
f20baf8e
DG
692 if (ret != LTTNG_OK) {
693 goto error;
694 }
695
696 /* If the event was created prior to the enable, add it to the domain. */
697 if (created) {
fefd409b 698 agent_add_event(aevent, agt);
f20baf8e
DG
699 }
700
701end:
702 return LTTNG_OK;
703
704error:
705 if (created) {
fefd409b 706 agent_destroy_event(aevent);
f20baf8e
DG
707 }
708 return ret;
709}
710
da6c3a50
DG
711/*
712 * Return the agent default event name to use by testing if the process is root
713 * or not. Return NULL on error.
714 */
715const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
716{
717 const char *default_event_name = NULL;
718
0e115563
DG
719 switch (domain) {
720 case LTTNG_DOMAIN_LOG4J:
da6c3a50 721 if (is_root) {
0e115563 722 default_event_name = DEFAULT_SYS_LOG4J_EVENT_NAME;
da6c3a50 723 } else {
0e115563 724 default_event_name = DEFAULT_USER_LOG4J_EVENT_NAME;
da6c3a50 725 }
0e115563
DG
726 break;
727 case LTTNG_DOMAIN_JUL:
da6c3a50 728 if (is_root) {
0e115563 729 default_event_name = DEFAULT_SYS_JUL_EVENT_NAME;
da6c3a50 730 } else {
0e115563 731 default_event_name = DEFAULT_USER_JUL_EVENT_NAME;
da6c3a50 732 }
0e115563
DG
733 break;
734 case LTTNG_DOMAIN_PYTHON:
735 default_event_name = DEFAULT_USER_PYTHON_EVENT_NAME;
736 break;
737 default:
da6c3a50
DG
738 assert(0);
739 }
740
741 return default_event_name;
742}
743
f20baf8e 744/*
022d91ba 745 * Disable a single agent event for a given UST session.
f20baf8e
DG
746 *
747 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
748 */
fefd409b
DG
749int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
750 char *event_name)
f20baf8e
DG
751{
752 int ret;
fefd409b 753 struct agent_event *aevent;
be6a6276
DG
754 struct ltt_ust_event *uevent = NULL;
755 struct ltt_ust_channel *uchan = NULL;
71aecbf8 756 const char *ust_event_name, *ust_channel_name;
f20baf8e 757
fefd409b 758 assert(agt);
f20baf8e
DG
759 assert(usess);
760 assert(event_name);
761
022d91ba 762 DBG("Event agent disabling %s for session %" PRIu64, event_name, usess->id);
f20baf8e 763
fefd409b
DG
764 aevent = agent_find_event_by_name(event_name, agt);
765 if (!aevent) {
f20baf8e
DG
766 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
767 goto error;
768 }
769
770 /* Already disabled? */
fefd409b 771 if (!aevent->enabled) {
f20baf8e
DG
772 goto end;
773 }
774
71aecbf8
DG
775 if (agt->domain == LTTNG_DOMAIN_JUL) {
776 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
777 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
778 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
779 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
780 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
71aecbf8
DG
781 } else {
782 ret = LTTNG_ERR_INVALID;
783 goto error;
784 }
785
be6a6276
DG
786 /*
787 * Disable it on the UST side. First get the channel reference then find
788 * the event and finally disable it.
789 */
790 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
71aecbf8 791 (char *) ust_channel_name);
be6a6276
DG
792 if (!uchan) {
793 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
794 goto error;
795 }
796
da6c3a50
DG
797 ust_event_name = event_get_default_agent_ust_name(agt->domain);
798 if (!ust_event_name) {
799 ret = LTTNG_ERR_FATAL;
800 goto error;
be6a6276
DG
801 }
802
803 /*
022d91ba 804 * The loglevel is hardcoded with 0 here since the agent ust event is set
be6a6276 805 * with the loglevel type to ALL thus the loglevel stays 0. The event's
022d91ba 806 * filter is the one handling the loglevel for agent.
be6a6276 807 */
da6c3a50 808 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
fefd409b 809 aevent->filter, 0, NULL);
022d91ba 810 /* If the agent event exists, it must be available on the UST side. */
be6a6276
DG
811 assert(uevent);
812
813 ret = ust_app_disable_event_glb(usess, uchan, uevent);
814 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
815 ret = LTTNG_ERR_UST_DISABLE_FAIL;
816 goto error;
817 }
818
0dcfcf94
DG
819 /*
820 * Flag event that it's disabled so the shadow copy on the ust app side
821 * will disable it if an application shows up.
822 */
823 uevent->enabled = 0;
824
fefd409b 825 ret = agent_disable_event(aevent, agt->domain);
f20baf8e
DG
826 if (ret != LTTNG_OK) {
827 goto error;
828 }
829
830end:
831 return LTTNG_OK;
832
833error:
834 return ret;
835}
836/*
022d91ba 837 * Disable all agent event for a given UST session.
f20baf8e
DG
838 *
839 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
840 */
fefd409b
DG
841int event_agent_disable_all(struct ltt_ust_session *usess,
842 struct agent *agt)
f20baf8e 843{
0dcfcf94 844 int ret;
fefd409b 845 struct agent_event *aevent;
f20baf8e
DG
846 struct lttng_ht_iter iter;
847
fefd409b 848 assert(agt);
f20baf8e
DG
849 assert(usess);
850
0dcfcf94
DG
851 /*
852 * Disable event on agent application. Continue to disable all other events
853 * if the * event is not found.
854 */
fefd409b 855 ret = event_agent_disable(usess, agt, "*");
0dcfcf94
DG
856 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
857 goto error;
f20baf8e
DG
858 }
859
860 /* Flag every event that they are now enabled. */
861 rcu_read_lock();
fefd409b 862 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
f20baf8e 863 node.node) {
0dcfcf94
DG
864 if (!aevent->enabled) {
865 continue;
866 }
867
868 ret = event_agent_disable(usess, agt, aevent->name);
869 if (ret != LTTNG_OK) {
870 rcu_read_unlock();
871 goto error;
f20baf8e 872 }
f20baf8e
DG
873 }
874 rcu_read_unlock();
875
876 ret = LTTNG_OK;
877
878error:
879 return ret;
880}
This page took 0.095899 seconds and 5 git commands to generate.