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