Fix: streamline ret/errno of run_as()
[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"
7c1d2758 36#include "agent.h"
54d01ffb 37
025faf73
DG
38/*
39 * Add unique UST event based on the event name, filter bytecode and loglevel.
40 */
18eace3b
DG
41static 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 = event->attr.loglevel;
4031e53e 54 key.exclusion = event->exclusion;
18eace3b
DG
55
56 node_ptr = cds_lfht_add_unique(ht->ht,
57 ht->hash_fct(event->node.key, lttng_ht_seed),
58 trace_ust_ht_match_event, &key, &event->node.node);
59 assert(node_ptr == &event->node.node);
60}
61
54d01ffb 62/*
7a3d1328 63 * Disable kernel tracepoint event for a channel from the kernel session.
54d01ffb 64 */
d0ae4ea8 65int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
d3a56674 66 char *event_name)
54d01ffb
DG
67{
68 int ret;
69 struct ltt_kernel_event *kevent;
70
0525e9ae
DG
71 assert(kchan);
72
d0ae4ea8
MD
73 kevent = trace_kernel_get_event_by_name(event_name, kchan,
74 LTTNG_EVENT_ALL);
54d01ffb 75 if (kevent == NULL) {
f73fabfd 76 ret = LTTNG_ERR_NO_EVENT;
54d01ffb
DG
77 goto error;
78 }
79
80 ret = kernel_disable_event(kevent);
81 if (ret < 0) {
f73fabfd 82 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
54d01ffb
DG
83 goto error;
84 }
85
86 DBG("Kernel event %s disable for channel %s.",
87 kevent->event->name, kchan->channel->name);
88
f73fabfd 89 ret = LTTNG_OK;
54d01ffb
DG
90
91error:
92 return ret;
93}
94
95/*
7a3d1328 96 * Disable kernel tracepoint events for a channel from the kernel session.
54d01ffb 97 */
d0ae4ea8
MD
98int event_kernel_disable_event_type(struct ltt_kernel_channel *kchan,
99 enum lttng_event_type type)
54d01ffb
DG
100{
101 int ret;
102 struct ltt_kernel_event *kevent;
103
0525e9ae
DG
104 assert(kchan);
105
54d01ffb
DG
106 /* For each event in the kernel session */
107 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
d0ae4ea8
MD
108 if (type != LTTNG_EVENT_ALL && kevent->type != type)
109 continue;
54d01ffb
DG
110 ret = kernel_disable_event(kevent);
111 if (ret < 0) {
112 /* We continue disabling the rest */
113 continue;
114 }
115 }
f73fabfd 116 ret = LTTNG_OK;
7a3d1328
MD
117 return ret;
118}
119
7a3d1328
MD
120/*
121 * Disable all kernel event for a channel from the kernel session.
122 */
d0ae4ea8 123int event_kernel_disable_event_all(struct ltt_kernel_channel *kchan)
7a3d1328 124{
d0ae4ea8 125 return event_kernel_disable_event_type(kchan, LTTNG_EVENT_ALL);
54d01ffb
DG
126}
127
128/*
7a3d1328 129 * Enable kernel tracepoint event for a channel from the kernel session.
49d21f93 130 * We own filter_expression and filter.
54d01ffb 131 */
d0ae4ea8 132int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
00a62084
MD
133 struct lttng_event *event, char *filter_expression,
134 struct lttng_filter_bytecode *filter)
54d01ffb
DG
135{
136 int ret;
137 struct ltt_kernel_event *kevent;
138
0525e9ae
DG
139 assert(kchan);
140 assert(event);
141
00a62084
MD
142 kevent = trace_kernel_find_event(event->name, kchan,
143 event->type, filter);
54d01ffb 144 if (kevent == NULL) {
00a62084
MD
145 ret = kernel_create_event(event, kchan,
146 filter_expression, filter);
a969e101
MD
147 /* We have passed ownership */
148 filter_expression = NULL;
149 filter = NULL;
54d01ffb 150 if (ret < 0) {
bd29c13d
DG
151 switch (-ret) {
152 case EEXIST:
f73fabfd 153 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
154 break;
155 case ENOSYS:
f73fabfd 156 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d
DG
157 break;
158 default:
f73fabfd 159 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
bd29c13d 160 break;
d87bfb32 161 }
7a3d1328 162 goto end;
54d01ffb
DG
163 }
164 } else if (kevent->enabled == 0) {
165 ret = kernel_enable_event(kevent);
166 if (ret < 0) {
f73fabfd 167 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
7a3d1328 168 goto end;
54d01ffb 169 }
42224349
DG
170 } else {
171 /* At this point, the event is considered enabled */
f73fabfd 172 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349 173 goto end;
54d01ffb 174 }
42224349 175
f73fabfd 176 ret = LTTNG_OK;
7a3d1328 177end:
a969e101
MD
178 free(filter_expression);
179 free(filter);
54d01ffb
DG
180 return ret;
181}
182
7f79d3a1
DG
183/*
184 * ============================
185 * UST : The Ultimate Frontier!
186 * ============================
187 */
188
2bdd86d4
MD
189/*
190 * Enable UST tracepoint event for a channel from a UST session.
49d21f93 191 * We own filter_expression, filter, and exclusion.
2bdd86d4 192 */
7972aab2 193int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
025faf73 194 struct ltt_ust_channel *uchan, struct lttng_event *event,
6b453b5e 195 char *filter_expression,
f1613f52 196 struct lttng_filter_bytecode *filter,
88f06f15
JG
197 struct lttng_event_exclusion *exclusion,
198 bool internal_event)
2bdd86d4 199{
f73fabfd 200 int ret = LTTNG_OK, to_create = 0;
edb67388
DG
201 struct ltt_ust_event *uevent;
202
0525e9ae
DG
203 assert(usess);
204 assert(uchan);
205 assert(event);
206
18eace3b
DG
207 rcu_read_lock();
208
025faf73 209 uevent = trace_ust_find_event(uchan->events, event->name, filter,
10646003 210 event->loglevel, exclusion);
87c6e328 211 if (!uevent) {
6b453b5e 212 uevent = trace_ust_create_event(event, filter_expression,
87c6e328 213 filter, exclusion, internal_event);
49d21f93
MD
214 /* We have passed ownership */
215 filter_expression = NULL;
216 filter = NULL;
217 exclusion = NULL;
edb67388 218 if (uevent == NULL) {
95a82664 219 ret = LTTNG_ERR_UST_ENABLE_FAIL;
edb67388
DG
220 goto error;
221 }
025faf73 222
fc34caaa 223 /* Valid to set it after the goto error since uevent is still NULL */
edb67388
DG
224 to_create = 1;
225 }
2bdd86d4 226
7f79d3a1
DG
227 if (uevent->enabled) {
228 /* It's already enabled so everything is OK */
5bcdda4f 229 ret = LTTNG_ERR_UST_EVENT_ENABLED;
7f79d3a1
DG
230 goto end;
231 }
232
fc34caaa
DG
233 uevent->enabled = 1;
234
7972aab2
DG
235 if (to_create) {
236 /* Create event on all UST registered apps for session */
237 ret = ust_app_create_event_glb(usess, uchan, uevent);
238 } else {
239 /* Enable event on all UST registered apps for session */
240 ret = ust_app_enable_event_glb(usess, uchan, uevent);
241 }
48842b30 242
7972aab2
DG
243 if (ret < 0) {
244 if (ret == -LTTNG_UST_ERR_EXIST) {
245 ret = LTTNG_ERR_UST_EVENT_EXIST;
246 goto end;
247 } else {
248 ret = LTTNG_ERR_UST_ENABLE_FAIL;
249 goto error;
edb67388 250 }
2bdd86d4 251 }
48842b30 252
7f79d3a1 253 if (to_create) {
fc34caaa 254 /* Add ltt ust event to channel */
18eace3b 255 add_unique_ust_event(uchan->events, uevent);
7f79d3a1 256 }
edb67388 257
7f79d3a1
DG
258 DBG("Event UST %s %s in channel %s", uevent->attr.name,
259 to_create ? "created" : "enabled", uchan->name);
260
f73fabfd 261 ret = LTTNG_OK;
fc34caaa 262
fb89d070 263end:
18eace3b 264 rcu_read_unlock();
49d21f93
MD
265 free(filter_expression);
266 free(filter);
267 free(exclusion);
fc34caaa 268 return ret;
edb67388
DG
269
270error:
fc34caaa
DG
271 /*
272 * Only destroy event on creation time (not enabling time) because if the
273 * event is found in the channel (to_create == 0), it means that at some
274 * point the enable_event worked and it's thus valid to keep it alive.
275 * Destroying it also implies that we also destroy it's shadow copy to sync
276 * everyone up.
277 */
278 if (to_create) {
279 /* In this code path, the uevent was not added to the hash table */
280 trace_ust_destroy_event(uevent);
281 }
18eace3b 282 rcu_read_unlock();
49d21f93
MD
283 free(filter_expression);
284 free(filter);
285 free(exclusion);
2bdd86d4
MD
286 return ret;
287}
288
7f79d3a1
DG
289/*
290 * Disable UST tracepoint of a channel from a UST session.
291 */
7972aab2 292int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
7f79d3a1 293 struct ltt_ust_channel *uchan, char *event_name)
2bdd86d4
MD
294{
295 int ret;
7f79d3a1 296 struct ltt_ust_event *uevent;
18eace3b
DG
297 struct lttng_ht_node_str *node;
298 struct lttng_ht_iter iter;
18eace3b 299 struct lttng_ht *ht;
2bdd86d4 300
0525e9ae
DG
301 assert(usess);
302 assert(uchan);
303 assert(event_name);
304
18eace3b
DG
305 ht = uchan->events;
306
18eace3b 307 rcu_read_lock();
025faf73
DG
308
309 /*
310 * We use a custom lookup since we need the iterator for the next_duplicate
311 * call in the do while loop below.
312 */
313 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
314 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
18eace3b
DG
315 node = lttng_ht_iter_get_node_str(&iter);
316 if (node == NULL) {
317 DBG2("Trace UST event NOT found by name %s", event_name);
f73fabfd 318 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
7f79d3a1 319 goto error;
2bdd86d4 320 }
7f79d3a1 321
18eace3b
DG
322 do {
323 uevent = caa_container_of(node, struct ltt_ust_event, node);
025faf73
DG
324 assert(uevent);
325
18eace3b
DG
326 if (uevent->enabled == 0) {
327 /* It's already disabled so everything is OK */
a1dcaf0f 328 goto next;
7f79d3a1 329 }
18eace3b 330
7972aab2
DG
331 ret = ust_app_disable_event_glb(usess, uchan, uevent);
332 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
333 ret = LTTNG_ERR_UST_DISABLE_FAIL;
18eace3b
DG
334 goto error;
335 }
18eace3b
DG
336 uevent->enabled = 0;
337
025faf73
DG
338 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
339 uchan->name);
340
a1dcaf0f 341next:
18eace3b
DG
342 /* Get next duplicate event by name. */
343 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
344 event_name, &iter.iter);
345 node = lttng_ht_iter_get_node_str(&iter);
346 } while (node);
7f79d3a1 347
f73fabfd 348 ret = LTTNG_OK;
7f79d3a1 349
7f79d3a1 350error:
18eace3b 351 rcu_read_unlock();
7f79d3a1
DG
352 return ret;
353}
354
f20baf8e 355/*
022d91ba 356 * Enable all agent event for a given UST session.
f20baf8e
DG
357 *
358 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
359 */
022d91ba 360int event_agent_enable_all(struct ltt_ust_session *usess,
fefd409b 361 struct agent *agt, struct lttng_event *event,
51755dc8 362 struct lttng_filter_bytecode *filter ,char *filter_expression)
f20baf8e
DG
363{
364 int ret;
fefd409b 365 struct agent_event *aevent;
f20baf8e
DG
366 struct lttng_ht_iter iter;
367
368 assert(usess);
369
022d91ba 370 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
f20baf8e 371
022d91ba 372 /* Enable event on agent application through TCP socket. */
8404118c 373 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
f20baf8e
DG
374 if (ret != LTTNG_OK) {
375 goto error;
376 }
377
378 /* Flag every event that they are now enabled. */
379 rcu_read_lock();
fefd409b 380 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
f20baf8e 381 node.node) {
fefd409b 382 aevent->enabled = 1;
f20baf8e
DG
383 }
384 rcu_read_unlock();
385
386 ret = LTTNG_OK;
387
388error:
389 return ret;
390}
391
392/*
022d91ba 393 * Enable a single agent event for a given UST session.
f20baf8e
DG
394 *
395 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
396 */
fefd409b
DG
397int event_agent_enable(struct ltt_ust_session *usess,
398 struct agent *agt, struct lttng_event *event,
8404118c
JG
399 struct lttng_filter_bytecode *filter,
400 char *filter_expression)
f20baf8e
DG
401{
402 int ret, created = 0;
fefd409b 403 struct agent_event *aevent;
f20baf8e
DG
404
405 assert(usess);
406 assert(event);
fefd409b 407 assert(agt);
f20baf8e 408
022d91ba 409 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
51755dc8
JG
410 ", loglevel %d and filter \"%s\"", event->name,
411 usess->id, event->loglevel_type, event->loglevel,
412 filter_expression ? filter_expression : "NULL");
f20baf8e 413
fefd409b
DG
414 aevent = agent_find_event(event->name, event->loglevel, agt);
415 if (!aevent) {
51755dc8
JG
416 aevent = agent_create_event(event->name, event->loglevel,
417 event->loglevel_type, filter,
418 filter_expression);
fefd409b 419 if (!aevent) {
f20baf8e
DG
420 ret = LTTNG_ERR_NOMEM;
421 goto error;
422 }
423 created = 1;
424 }
425
426 /* Already enabled? */
fefd409b 427 if (aevent->enabled) {
f20baf8e
DG
428 goto end;
429 }
430
fefd409b 431 ret = agent_enable_event(aevent, agt->domain);
f20baf8e
DG
432 if (ret != LTTNG_OK) {
433 goto error;
434 }
435
436 /* If the event was created prior to the enable, add it to the domain. */
437 if (created) {
fefd409b 438 agent_add_event(aevent, agt);
f20baf8e
DG
439 }
440
441end:
442 return LTTNG_OK;
443
444error:
445 if (created) {
fefd409b 446 agent_destroy_event(aevent);
f20baf8e
DG
447 }
448 return ret;
449}
450
da6c3a50 451/*
d1ec8e20
JG
452 * Return the default event name associated with the provided UST domain. Return
453 * NULL on error.
da6c3a50
DG
454 */
455const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
456{
457 const char *default_event_name = NULL;
458
0e115563
DG
459 switch (domain) {
460 case LTTNG_DOMAIN_LOG4J:
d1ec8e20 461 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
0e115563
DG
462 break;
463 case LTTNG_DOMAIN_JUL:
d1ec8e20 464 default_event_name = DEFAULT_JUL_EVENT_NAME;
0e115563
DG
465 break;
466 case LTTNG_DOMAIN_PYTHON:
e7c82272 467 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
0e115563
DG
468 break;
469 default:
da6c3a50
DG
470 assert(0);
471 }
472
473 return default_event_name;
474}
475
f20baf8e 476/*
022d91ba 477 * Disable a single agent event for a given UST session.
f20baf8e
DG
478 *
479 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
480 */
fefd409b
DG
481int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
482 char *event_name)
f20baf8e
DG
483{
484 int ret;
fefd409b 485 struct agent_event *aevent;
be6a6276
DG
486 struct ltt_ust_event *uevent = NULL;
487 struct ltt_ust_channel *uchan = NULL;
71aecbf8 488 const char *ust_event_name, *ust_channel_name;
f20baf8e 489
fefd409b 490 assert(agt);
f20baf8e
DG
491 assert(usess);
492 assert(event_name);
493
022d91ba 494 DBG("Event agent disabling %s for session %" PRIu64, event_name, usess->id);
f20baf8e 495
fefd409b
DG
496 aevent = agent_find_event_by_name(event_name, agt);
497 if (!aevent) {
f20baf8e
DG
498 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
499 goto error;
500 }
501
502 /* Already disabled? */
fefd409b 503 if (!aevent->enabled) {
f20baf8e
DG
504 goto end;
505 }
506
71aecbf8
DG
507 if (agt->domain == LTTNG_DOMAIN_JUL) {
508 ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
509 } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
510 ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
511 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
512 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
71aecbf8
DG
513 } else {
514 ret = LTTNG_ERR_INVALID;
515 goto error;
516 }
517
be6a6276
DG
518 /*
519 * Disable it on the UST side. First get the channel reference then find
520 * the event and finally disable it.
521 */
522 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
71aecbf8 523 (char *) ust_channel_name);
be6a6276
DG
524 if (!uchan) {
525 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
526 goto error;
527 }
528
da6c3a50
DG
529 ust_event_name = event_get_default_agent_ust_name(agt->domain);
530 if (!ust_event_name) {
531 ret = LTTNG_ERR_FATAL;
532 goto error;
be6a6276
DG
533 }
534
535 /*
022d91ba 536 * The loglevel is hardcoded with 0 here since the agent ust event is set
be6a6276 537 * with the loglevel type to ALL thus the loglevel stays 0. The event's
022d91ba 538 * filter is the one handling the loglevel for agent.
be6a6276 539 */
da6c3a50 540 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
fefd409b 541 aevent->filter, 0, NULL);
022d91ba 542 /* If the agent event exists, it must be available on the UST side. */
be6a6276
DG
543 assert(uevent);
544
545 ret = ust_app_disable_event_glb(usess, uchan, uevent);
546 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
547 ret = LTTNG_ERR_UST_DISABLE_FAIL;
548 goto error;
549 }
550
0dcfcf94
DG
551 /*
552 * Flag event that it's disabled so the shadow copy on the ust app side
553 * will disable it if an application shows up.
554 */
555 uevent->enabled = 0;
556
fefd409b 557 ret = agent_disable_event(aevent, agt->domain);
f20baf8e
DG
558 if (ret != LTTNG_OK) {
559 goto error;
560 }
561
562end:
563 return LTTNG_OK;
564
565error:
566 return ret;
567}
568/*
022d91ba 569 * Disable all agent event for a given UST session.
f20baf8e
DG
570 *
571 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
572 */
fefd409b
DG
573int event_agent_disable_all(struct ltt_ust_session *usess,
574 struct agent *agt)
f20baf8e 575{
0dcfcf94 576 int ret;
fefd409b 577 struct agent_event *aevent;
f20baf8e
DG
578 struct lttng_ht_iter iter;
579
fefd409b 580 assert(agt);
f20baf8e
DG
581 assert(usess);
582
0dcfcf94
DG
583 /*
584 * Disable event on agent application. Continue to disable all other events
585 * if the * event is not found.
586 */
fefd409b 587 ret = event_agent_disable(usess, agt, "*");
0dcfcf94
DG
588 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
589 goto error;
f20baf8e
DG
590 }
591
592 /* Flag every event that they are now enabled. */
593 rcu_read_lock();
fefd409b 594 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
f20baf8e 595 node.node) {
0dcfcf94
DG
596 if (!aevent->enabled) {
597 continue;
598 }
599
600 ret = event_agent_disable(usess, agt, aevent->name);
601 if (ret != LTTNG_OK) {
602 rcu_read_unlock();
603 goto error;
f20baf8e 604 }
f20baf8e
DG
605 }
606 rcu_read_unlock();
607
608 ret = LTTNG_OK;
609
610error:
611 return ret;
612}
This page took 0.074047 seconds and 4 git commands to generate.