sessiond: use `loglevel_value` and `loglevel_type` names
[lttng-tools.git] / src / bin / lttng-sessiond / event.c
... / ...
CommitLineData
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 */
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_type = event->attr.loglevel;
54 key.exclusion = event->exclusion;
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
62/*
63 * Disable kernel tracepoint event for a channel from the kernel session.
64 */
65int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
66 char *event_name)
67{
68 int ret;
69 struct ltt_kernel_event *kevent;
70
71 assert(kchan);
72
73 kevent = trace_kernel_get_event_by_name(event_name, kchan,
74 LTTNG_EVENT_ALL);
75 if (kevent == NULL) {
76 ret = LTTNG_ERR_NO_EVENT;
77 goto error;
78 }
79
80 ret = kernel_disable_event(kevent);
81 if (ret < 0) {
82 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
83 goto error;
84 }
85
86 DBG("Kernel event %s disable for channel %s.",
87 kevent->event->name, kchan->channel->name);
88
89 ret = LTTNG_OK;
90
91error:
92 return ret;
93}
94
95/*
96 * Disable kernel tracepoint events for a channel from the kernel session.
97 */
98int event_kernel_disable_event_type(struct ltt_kernel_channel *kchan,
99 enum lttng_event_type type)
100{
101 int ret;
102 struct ltt_kernel_event *kevent;
103
104 assert(kchan);
105
106 /* For each event in the kernel session */
107 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
108 if (type != LTTNG_EVENT_ALL && kevent->type != type)
109 continue;
110 ret = kernel_disable_event(kevent);
111 if (ret < 0) {
112 /* We continue disabling the rest */
113 continue;
114 }
115 }
116 ret = LTTNG_OK;
117 return ret;
118}
119
120/*
121 * Disable all kernel event for a channel from the kernel session.
122 */
123int event_kernel_disable_event_all(struct ltt_kernel_channel *kchan)
124{
125 return event_kernel_disable_event_type(kchan, LTTNG_EVENT_ALL);
126}
127
128/*
129 * Enable kernel tracepoint event for a channel from the kernel session.
130 * We own filter_expression and filter.
131 */
132int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
133 struct lttng_event *event, char *filter_expression,
134 struct lttng_filter_bytecode *filter)
135{
136 int ret;
137 struct ltt_kernel_event *kevent;
138
139 assert(kchan);
140 assert(event);
141
142 kevent = trace_kernel_find_event(event->name, kchan,
143 event->type, filter);
144 if (kevent == NULL) {
145 ret = kernel_create_event(event, kchan,
146 filter_expression, filter);
147 /* We have passed ownership */
148 filter_expression = NULL;
149 filter = NULL;
150 if (ret < 0) {
151 switch (-ret) {
152 case EEXIST:
153 ret = LTTNG_ERR_KERN_EVENT_EXIST;
154 break;
155 case ENOSYS:
156 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
157 break;
158 default:
159 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
160 break;
161 }
162 goto end;
163 }
164 } else if (kevent->enabled == 0) {
165 ret = kernel_enable_event(kevent);
166 if (ret < 0) {
167 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
168 goto end;
169 }
170 } else {
171 /* At this point, the event is considered enabled */
172 ret = LTTNG_ERR_KERN_EVENT_EXIST;
173 goto end;
174 }
175
176 ret = LTTNG_OK;
177end:
178 free(filter_expression);
179 free(filter);
180 return ret;
181}
182
183/*
184 * ============================
185 * UST : The Ultimate Frontier!
186 * ============================
187 */
188
189/*
190 * Enable UST tracepoint event for a channel from a UST session.
191 * We own filter_expression, filter, and exclusion.
192 */
193int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
194 struct ltt_ust_channel *uchan, struct lttng_event *event,
195 char *filter_expression,
196 struct lttng_filter_bytecode *filter,
197 struct lttng_event_exclusion *exclusion,
198 bool internal_event)
199{
200 int ret = LTTNG_OK, to_create = 0;
201 struct ltt_ust_event *uevent;
202
203 assert(usess);
204 assert(uchan);
205 assert(event);
206
207 rcu_read_lock();
208
209 uevent = trace_ust_find_event(uchan->events, event->name, filter,
210 event->loglevel, exclusion);
211 if (!uevent) {
212 uevent = trace_ust_create_event(event, filter_expression,
213 filter, exclusion, internal_event);
214 /* We have passed ownership */
215 filter_expression = NULL;
216 filter = NULL;
217 exclusion = NULL;
218 if (uevent == NULL) {
219 ret = LTTNG_ERR_UST_ENABLE_FAIL;
220 goto error;
221 }
222
223 /* Valid to set it after the goto error since uevent is still NULL */
224 to_create = 1;
225 }
226
227 if (uevent->enabled) {
228 /* It's already enabled so everything is OK */
229 ret = LTTNG_ERR_UST_EVENT_ENABLED;
230 goto end;
231 }
232
233 uevent->enabled = 1;
234
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 }
242
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;
250 }
251 }
252
253 if (to_create) {
254 /* Add ltt ust event to channel */
255 add_unique_ust_event(uchan->events, uevent);
256 }
257
258 DBG("Event UST %s %s in channel %s", uevent->attr.name,
259 to_create ? "created" : "enabled", uchan->name);
260
261 ret = LTTNG_OK;
262
263end:
264 rcu_read_unlock();
265 free(filter_expression);
266 free(filter);
267 free(exclusion);
268 return ret;
269
270error:
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 }
282 rcu_read_unlock();
283 free(filter_expression);
284 free(filter);
285 free(exclusion);
286 return ret;
287}
288
289/*
290 * Disable UST tracepoint of a channel from a UST session.
291 */
292int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
293 struct ltt_ust_channel *uchan, char *event_name)
294{
295 int ret;
296 struct ltt_ust_event *uevent;
297 struct lttng_ht_node_str *node;
298 struct lttng_ht_iter iter;
299 struct lttng_ht *ht;
300
301 assert(usess);
302 assert(uchan);
303 assert(event_name);
304
305 ht = uchan->events;
306
307 rcu_read_lock();
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);
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);
318 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
319 goto error;
320 }
321
322 do {
323 uevent = caa_container_of(node, struct ltt_ust_event, node);
324 assert(uevent);
325
326 if (uevent->enabled == 0) {
327 /* It's already disabled so everything is OK */
328 goto next;
329 }
330
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;
334 goto error;
335 }
336 uevent->enabled = 0;
337
338 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
339 uchan->name);
340
341next:
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);
347
348 ret = LTTNG_OK;
349
350error:
351 rcu_read_unlock();
352 return ret;
353}
354
355/*
356 * Enable all agent event for a given UST session.
357 *
358 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
359 */
360int event_agent_enable_all(struct ltt_ust_session *usess,
361 struct agent *agt, struct lttng_event *event,
362 struct lttng_filter_bytecode *filter ,char *filter_expression)
363{
364 int ret;
365 struct agent_event *aevent;
366 struct lttng_ht_iter iter;
367
368 assert(usess);
369
370 DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
371
372 /* Enable event on agent application through TCP socket. */
373 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
374 if (ret != LTTNG_OK) {
375 goto error;
376 }
377
378 /* Flag every event that they are now enabled. */
379 rcu_read_lock();
380 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
381 node.node) {
382 aevent->enabled = 1;
383 }
384 rcu_read_unlock();
385
386 ret = LTTNG_OK;
387
388error:
389 return ret;
390}
391
392/*
393 * Enable a single agent event for a given UST session.
394 *
395 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
396 */
397int event_agent_enable(struct ltt_ust_session *usess,
398 struct agent *agt, struct lttng_event *event,
399 struct lttng_filter_bytecode *filter,
400 char *filter_expression)
401{
402 int ret, created = 0;
403 struct agent_event *aevent;
404
405 assert(usess);
406 assert(event);
407 assert(agt);
408
409 DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
410 ", loglevel %d and filter \"%s\"", event->name,
411 usess->id, event->loglevel_type, event->loglevel,
412 filter_expression ? filter_expression : "NULL");
413
414 aevent = agent_find_event(event->name, event->loglevel, agt);
415 if (!aevent) {
416 aevent = agent_create_event(event->name, event->loglevel,
417 event->loglevel_type, filter,
418 filter_expression);
419 if (!aevent) {
420 ret = LTTNG_ERR_NOMEM;
421 goto error;
422 }
423 created = 1;
424 }
425
426 /* Already enabled? */
427 if (aevent->enabled) {
428 goto end;
429 }
430
431 ret = agent_enable_event(aevent, agt->domain);
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) {
438 agent_add_event(aevent, agt);
439 }
440
441end:
442 return LTTNG_OK;
443
444error:
445 if (created) {
446 agent_destroy_event(aevent);
447 }
448 return ret;
449}
450
451/*
452 * Return the default event name associated with the provided UST domain. Return
453 * NULL on error.
454 */
455const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
456{
457 const char *default_event_name = NULL;
458
459 switch (domain) {
460 case LTTNG_DOMAIN_LOG4J:
461 default_event_name = DEFAULT_LOG4J_EVENT_NAME;
462 break;
463 case LTTNG_DOMAIN_JUL:
464 default_event_name = DEFAULT_JUL_EVENT_NAME;
465 break;
466 case LTTNG_DOMAIN_PYTHON:
467 default_event_name = DEFAULT_PYTHON_EVENT_NAME;
468 break;
469 default:
470 assert(0);
471 }
472
473 return default_event_name;
474}
475
476/*
477 * Disable a single agent event for a given UST session.
478 *
479 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
480 */
481int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
482 char *event_name)
483{
484 int ret;
485 struct agent_event *aevent;
486 struct ltt_ust_event *uevent = NULL;
487 struct ltt_ust_channel *uchan = NULL;
488 const char *ust_event_name, *ust_channel_name;
489
490 assert(agt);
491 assert(usess);
492 assert(event_name);
493
494 DBG("Event agent disabling %s for session %" PRIu64, event_name, usess->id);
495
496 aevent = agent_find_event_by_name(event_name, agt);
497 if (!aevent) {
498 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
499 goto error;
500 }
501
502 /* Already disabled? */
503 if (!aevent->enabled) {
504 goto end;
505 }
506
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;
511 } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
512 ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
513 } else {
514 ret = LTTNG_ERR_INVALID;
515 goto error;
516 }
517
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,
523 (char *) ust_channel_name);
524 if (!uchan) {
525 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
526 goto error;
527 }
528
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;
533 }
534
535 /*
536 * The loglevel is hardcoded with 0 here since the agent ust event is set
537 * with the loglevel type to ALL thus the loglevel stays 0. The event's
538 * filter is the one handling the loglevel for agent.
539 */
540 uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
541 aevent->filter, 0, NULL);
542 /* If the agent event exists, it must be available on the UST side. */
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
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
557 ret = agent_disable_event(aevent, agt->domain);
558 if (ret != LTTNG_OK) {
559 goto error;
560 }
561
562end:
563 return LTTNG_OK;
564
565error:
566 return ret;
567}
568/*
569 * Disable all agent event for a given UST session.
570 *
571 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
572 */
573int event_agent_disable_all(struct ltt_ust_session *usess,
574 struct agent *agt)
575{
576 int ret;
577 struct agent_event *aevent;
578 struct lttng_ht_iter iter;
579
580 assert(agt);
581 assert(usess);
582
583 /*
584 * Disable event on agent application. Continue to disable all other events
585 * if the * event is not found.
586 */
587 ret = event_agent_disable(usess, agt, "*");
588 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
589 goto error;
590 }
591
592 /* Flag every event that they are now enabled. */
593 rcu_read_lock();
594 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
595 node.node) {
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;
604 }
605 }
606 rcu_read_unlock();
607
608 ret = LTTNG_OK;
609
610error:
611 return ret;
612}
This page took 0.024885 seconds and 4 git commands to generate.