lttng.h API update: set filter becomes enable event with filter
[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 #include <errno.h>
20 #include <urcu/list.h>
21 #include <string.h>
22
23 #include <lttng/lttng.h>
24 #include <common/error.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26
27 #include "channel.h"
28 #include "event.h"
29 #include "kernel.h"
30 #include "ust-ctl.h"
31 #include "ust-app.h"
32 #include "trace-kernel.h"
33 #include "trace-ust.h"
34
35 /*
36 * Add unique UST event based on the event name, filter bytecode and loglevel.
37 */
38 static void add_unique_ust_event(struct lttng_ht *ht,
39 struct ltt_ust_event *event)
40 {
41 struct cds_lfht_node *node_ptr;
42 struct ltt_ust_ht_key key;
43
44 assert(ht);
45 assert(ht->ht);
46 assert(event);
47
48 key.name = event->attr.name;
49 key.filter = (struct lttng_filter_bytecode *) event->filter;
50 key.loglevel = event->attr.loglevel;
51
52 node_ptr = cds_lfht_add_unique(ht->ht,
53 ht->hash_fct(event->node.key, lttng_ht_seed),
54 trace_ust_ht_match_event, &key, &event->node.node);
55 assert(node_ptr == &event->node.node);
56 }
57
58 /*
59 * Setup a lttng_event used to enable *all* syscall tracing.
60 */
61 static void init_syscalls_kernel_event(struct lttng_event *event)
62 {
63 event->name[0] = '\0';
64 /*
65 * We use LTTNG_EVENT* here since the trace kernel creation will make the
66 * right changes for the kernel.
67 */
68 event->type = LTTNG_EVENT_SYSCALL;
69 }
70
71 /*
72 * Disable kernel tracepoint event for a channel from the kernel session.
73 */
74 int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
75 struct ltt_kernel_channel *kchan, char *event_name)
76 {
77 int ret;
78 struct ltt_kernel_event *kevent;
79
80 kevent = trace_kernel_get_event_by_name(event_name, kchan);
81 if (kevent == NULL) {
82 ret = LTTNG_ERR_NO_EVENT;
83 goto error;
84 }
85
86 ret = kernel_disable_event(kevent);
87 if (ret < 0) {
88 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
89 goto error;
90 }
91
92 DBG("Kernel event %s disable for channel %s.",
93 kevent->event->name, kchan->channel->name);
94
95 ret = LTTNG_OK;
96
97 error:
98 return ret;
99 }
100
101 /*
102 * Disable kernel tracepoint events for a channel from the kernel session.
103 */
104 int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
105 struct ltt_kernel_channel *kchan)
106 {
107 int ret;
108 struct ltt_kernel_event *kevent;
109
110 /* For each event in the kernel session */
111 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
112 ret = kernel_disable_event(kevent);
113 if (ret < 0) {
114 /* We continue disabling the rest */
115 continue;
116 }
117 }
118 ret = LTTNG_OK;
119 return ret;
120 }
121
122 /*
123 * Disable kernel syscall events for a channel from the kernel session.
124 */
125 int event_kernel_disable_all_syscalls(struct ltt_kernel_session *ksession,
126 struct ltt_kernel_channel *kchan)
127 {
128 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
129 return LTTNG_OK; /* Return OK so disable all succeeds */
130 }
131
132 /*
133 * Disable all kernel event for a channel from the kernel session.
134 */
135 int event_kernel_disable_all(struct ltt_kernel_session *ksession,
136 struct ltt_kernel_channel *kchan)
137 {
138 int ret;
139
140 ret = event_kernel_disable_all_tracepoints(ksession, kchan);
141 if (ret != LTTNG_OK)
142 return ret;
143 ret = event_kernel_disable_all_syscalls(ksession, kchan);
144 return ret;
145 }
146
147 /*
148 * Enable kernel tracepoint event for a channel from the kernel session.
149 */
150 int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
151 struct ltt_kernel_channel *kchan, struct lttng_event *event)
152 {
153 int ret;
154 struct ltt_kernel_event *kevent;
155
156 kevent = trace_kernel_get_event_by_name(event->name, kchan);
157 if (kevent == NULL) {
158 ret = kernel_create_event(event, kchan);
159 if (ret < 0) {
160 switch (-ret) {
161 case EEXIST:
162 ret = LTTNG_ERR_KERN_EVENT_EXIST;
163 break;
164 case ENOSYS:
165 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
166 break;
167 default:
168 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
169 break;
170 }
171 goto end;
172 }
173 } else if (kevent->enabled == 0) {
174 ret = kernel_enable_event(kevent);
175 if (ret < 0) {
176 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
177 goto end;
178 }
179 } else {
180 /* At this point, the event is considered enabled */
181 ret = LTTNG_ERR_KERN_EVENT_EXIST;
182 goto end;
183 }
184
185 ret = LTTNG_OK;
186 end:
187 return ret;
188 }
189
190 /*
191 * Enable all kernel tracepoint events of a channel of the kernel session.
192 */
193 int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
194 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
195 {
196 int size, i, ret;
197 struct ltt_kernel_event *kevent;
198 struct lttng_event *event_list = NULL;
199
200 /* For each event in the kernel session */
201 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
202 if (kevent->enabled == 0) {
203 ret = kernel_enable_event(kevent);
204 if (ret < 0) {
205 /* Enable failed but still continue */
206 continue;
207 }
208 }
209 }
210
211 size = kernel_list_events(kernel_tracer_fd, &event_list);
212 if (size < 0) {
213 ret = LTTNG_ERR_KERN_LIST_FAIL;
214 goto end;
215 }
216
217 for (i = 0; i < size; i++) {
218 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
219 if (kevent == NULL) {
220 /* Default event type for enable all */
221 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
222 /* Enable each single tracepoint event */
223 ret = kernel_create_event(&event_list[i], kchan);
224 if (ret < 0) {
225 /* Ignore error here and continue */
226 }
227 }
228 }
229 free(event_list);
230
231 ret = LTTNG_OK;
232 end:
233 return ret;
234
235 }
236
237 /*
238 * Enable all kernel tracepoint events of a channel of the kernel session.
239 */
240 int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
241 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
242 {
243 int ret;
244 struct lttng_event event;
245
246 init_syscalls_kernel_event(&event);
247
248 DBG("Enabling all syscall tracing");
249
250 ret = kernel_create_event(&event, kchan);
251 if (ret < 0) {
252 if (ret == -EEXIST) {
253 ret = LTTNG_ERR_KERN_EVENT_EXIST;
254 } else {
255 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
256 }
257 goto end;
258 }
259
260 ret = LTTNG_OK;
261 end:
262 return ret;
263 }
264
265 /*
266 * Enable all kernel events of a channel of the kernel session.
267 */
268 int event_kernel_enable_all(struct ltt_kernel_session *ksession,
269 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
270 {
271 int tp_ret;
272
273 tp_ret = event_kernel_enable_all_tracepoints(ksession, kchan, kernel_tracer_fd);
274 if (tp_ret != LTTNG_OK) {
275 goto end;
276 }
277
278 /*
279 * Reaching this code path means that all tracepoints were enabled without
280 * errors so we ignore the error value of syscalls.
281 *
282 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
283 * is not considered an error that need to be returned to the client since
284 * tracepoints did not fail. Future work will allow us to send back
285 * multiple errors to the client in one API call.
286 */
287 (void) event_kernel_enable_all_syscalls(ksession, kchan, kernel_tracer_fd);
288
289 end:
290 return tp_ret;
291 }
292
293 /*
294 * ============================
295 * UST : The Ultimate Frontier!
296 * ============================
297 */
298
299 /*
300 * Enable all UST tracepoints for a channel from a UST session.
301 */
302 int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess, int domain,
303 struct ltt_ust_channel *uchan, struct lttng_filter_bytecode *filter)
304 {
305 int ret, i, size;
306 struct lttng_ht_iter iter;
307 struct ltt_ust_event *uevent = NULL;
308 struct lttng_event *events = NULL;
309
310 rcu_read_lock();
311
312 switch (domain) {
313 case LTTNG_DOMAIN_UST:
314 {
315 /* Enable existing events */
316 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
317 node.node) {
318 if (uevent->enabled == 0) {
319 ret = ust_app_enable_event_glb(usess, uchan, uevent);
320 if (ret < 0) {
321 continue;
322 }
323 uevent->enabled = 1;
324 }
325 }
326
327 /* Get all UST available events */
328 size = ust_app_list_events(&events);
329 if (size < 0) {
330 ret = LTTNG_ERR_UST_LIST_FAIL;
331 goto error;
332 }
333
334 for (i = 0; i < size; i++) {
335 /*
336 * Check if event exist and if so, continue since it was enable
337 * previously.
338 */
339 uevent = trace_ust_find_event(uchan->events, events[i].name, filter,
340 events[i].loglevel);
341 if (uevent != NULL) {
342 ret = ust_app_enable_event_pid(usess, uchan, uevent,
343 events[i].pid);
344 if (ret < 0) {
345 if (ret != -LTTNG_UST_ERR_EXIST) {
346 ret = LTTNG_ERR_UST_ENABLE_FAIL;
347 goto error;
348 }
349 }
350 continue;
351 }
352
353 /* Create ust event */
354 uevent = trace_ust_create_event(&events[i], filter);
355 if (uevent == NULL) {
356 ret = LTTNG_ERR_FATAL;
357 goto error_destroy;
358 }
359
360 /* Create event for the specific PID */
361 ret = ust_app_enable_event_pid(usess, uchan, uevent,
362 events[i].pid);
363 if (ret < 0) {
364 if (ret == -LTTNG_UST_ERR_EXIST) {
365 ret = LTTNG_ERR_UST_EVENT_EXIST;
366 goto error;
367 } else {
368 ret = LTTNG_ERR_UST_ENABLE_FAIL;
369 goto error_destroy;
370 }
371 }
372
373 uevent->enabled = 1;
374 /* Add ltt ust event to channel */
375 rcu_read_lock();
376 add_unique_ust_event(uchan->events, uevent);
377 rcu_read_unlock();
378 }
379
380 free(events);
381 break;
382 }
383 #if 0
384 case LTTNG_DOMAIN_UST_EXEC_NAME:
385 case LTTNG_DOMAIN_UST_PID:
386 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
387 #endif
388 default:
389 ret = LTTNG_ERR_UND;
390 goto error;
391 }
392
393 rcu_read_unlock();
394 return LTTNG_OK;
395
396 error_destroy:
397 trace_ust_destroy_event(uevent);
398
399 error:
400 free(events);
401 rcu_read_unlock();
402 return ret;
403 }
404
405 /*
406 * Enable UST tracepoint event for a channel from a UST session.
407 */
408 int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
409 struct ltt_ust_channel *uchan, struct lttng_event *event,
410 struct lttng_filter_bytecode *filter)
411 {
412 int ret = LTTNG_OK, to_create = 0;
413 struct ltt_ust_event *uevent;
414
415 rcu_read_lock();
416
417 uevent = trace_ust_find_event(uchan->events, event->name, filter,
418 event->loglevel);
419 if (uevent == NULL) {
420 uevent = trace_ust_create_event(event, filter);
421 if (uevent == NULL) {
422 ret = LTTNG_ERR_UST_ENABLE_FAIL;
423 goto error;
424 }
425
426 /* Valid to set it after the goto error since uevent is still NULL */
427 to_create = 1;
428 }
429
430 if (uevent->enabled) {
431 /* It's already enabled so everything is OK */
432 ret = LTTNG_OK;
433 goto end;
434 }
435
436 uevent->enabled = 1;
437
438 switch (domain) {
439 case LTTNG_DOMAIN_UST:
440 {
441 if (to_create) {
442 /* Create event on all UST registered apps for session */
443 ret = ust_app_create_event_glb(usess, uchan, uevent);
444 } else {
445 /* Enable event on all UST registered apps for session */
446 ret = ust_app_enable_event_glb(usess, uchan, uevent);
447 }
448
449 if (ret < 0) {
450 if (ret == -LTTNG_UST_ERR_EXIST) {
451 ret = LTTNG_ERR_UST_EVENT_EXIST;
452 goto end;
453 } else {
454 ret = LTTNG_ERR_UST_ENABLE_FAIL;
455 goto error;
456 }
457 }
458 break;
459 }
460 #if 0
461 case LTTNG_DOMAIN_UST_EXEC_NAME:
462 case LTTNG_DOMAIN_UST_PID:
463 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
464 #endif
465 default:
466 ret = LTTNG_ERR_UND;
467 goto end;
468 }
469
470 if (to_create) {
471 /* Add ltt ust event to channel */
472 add_unique_ust_event(uchan->events, uevent);
473 }
474
475 DBG("Event UST %s %s in channel %s", uevent->attr.name,
476 to_create ? "created" : "enabled", uchan->name);
477
478 ret = LTTNG_OK;
479
480 end:
481 rcu_read_unlock();
482 return ret;
483
484 error:
485 /*
486 * Only destroy event on creation time (not enabling time) because if the
487 * event is found in the channel (to_create == 0), it means that at some
488 * point the enable_event worked and it's thus valid to keep it alive.
489 * Destroying it also implies that we also destroy it's shadow copy to sync
490 * everyone up.
491 */
492 if (to_create) {
493 /* In this code path, the uevent was not added to the hash table */
494 trace_ust_destroy_event(uevent);
495 }
496 rcu_read_unlock();
497 return ret;
498 }
499
500 /*
501 * Disable UST tracepoint of a channel from a UST session.
502 */
503 int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
504 struct ltt_ust_channel *uchan, char *event_name)
505 {
506 int ret;
507 struct ltt_ust_event *uevent;
508 struct lttng_ht_node_str *node;
509 struct lttng_ht_iter iter;
510 struct lttng_ht *ht;
511
512 ht = uchan->events;
513
514 rcu_read_lock();
515
516 /*
517 * We use a custom lookup since we need the iterator for the next_duplicate
518 * call in the do while loop below.
519 */
520 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
521 trace_ust_ht_match_event_by_name, event_name, &iter.iter);
522 node = lttng_ht_iter_get_node_str(&iter);
523 if (node == NULL) {
524 DBG2("Trace UST event NOT found by name %s", event_name);
525 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
526 goto error;
527 }
528
529 do {
530 uevent = caa_container_of(node, struct ltt_ust_event, node);
531 assert(uevent);
532
533 if (uevent->enabled == 0) {
534 /* It's already disabled so everything is OK */
535 ret = LTTNG_OK;
536 continue;
537 }
538
539 switch (domain) {
540 case LTTNG_DOMAIN_UST:
541 ret = ust_app_disable_event_glb(usess, uchan, uevent);
542 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
543 ret = LTTNG_ERR_UST_DISABLE_FAIL;
544 goto error;
545 }
546 break;
547 #if 0
548 case LTTNG_DOMAIN_UST_EXEC_NAME:
549 case LTTNG_DOMAIN_UST_PID:
550 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
551 #endif
552 default:
553 ret = LTTNG_ERR_UND;
554 goto error;
555 }
556
557 uevent->enabled = 0;
558
559 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
560 uchan->name);
561
562 /* Get next duplicate event by name. */
563 cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
564 event_name, &iter.iter);
565 node = lttng_ht_iter_get_node_str(&iter);
566 } while (node);
567
568 ret = LTTNG_OK;
569
570 error:
571 rcu_read_unlock();
572 return ret;
573 }
574
575 /*
576 * Disable all UST tracepoints for a channel from a UST session.
577 */
578 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
579 struct ltt_ust_channel *uchan)
580 {
581 int ret, i, size;
582 struct lttng_ht_iter iter;
583 struct ltt_ust_event *uevent = NULL;
584 struct lttng_event *events = NULL;
585
586 rcu_read_lock();
587
588 switch (domain) {
589 case LTTNG_DOMAIN_UST:
590 {
591 /* Disabling existing events */
592 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
593 node.node) {
594 if (uevent->enabled == 1) {
595 ret = event_ust_disable_tracepoint(usess, domain, uchan,
596 uevent->attr.name);
597 if (ret < 0) {
598 continue;
599 }
600 }
601 }
602
603 /* Get all UST available events */
604 size = ust_app_list_events(&events);
605 if (size < 0) {
606 ret = LTTNG_ERR_UST_LIST_FAIL;
607 goto error;
608 }
609
610 for (i = 0; i < size; i++) {
611 ret = event_ust_disable_tracepoint(usess, domain, uchan,
612 events[i].name);
613 if (ret != LTTNG_OK) {
614 /* Continue to disable the rest... */
615 continue;
616 }
617 }
618
619 free(events);
620 break;
621 }
622 #if 0
623 case LTTNG_DOMAIN_UST_EXEC_NAME:
624 case LTTNG_DOMAIN_UST_PID:
625 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
626 #endif
627 default:
628 ret = LTTNG_ERR_UND;
629 goto error;
630 }
631
632 rcu_read_unlock();
633 return LTTNG_OK;
634
635 error:
636 free(events);
637 rcu_read_unlock();
638 return ret;
639 }
This page took 0.067016 seconds and 4 git commands to generate.