Add liblttng-ctl destructor to cleanup memory
[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
d87bfb32 19#include <errno.h>
54d01ffb 20#include <urcu/list.h>
2bdd86d4 21#include <string.h>
54d01ffb
DG
22
23#include <lttng/lttng.h>
db758600 24#include <common/error.h>
10a8a223 25#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
26
27#include "channel.h"
28#include "event.h"
4771f025 29#include "kernel.h"
9df8df5e 30#include "ust-ctl.h"
edb67388
DG
31#include "ust-app.h"
32#include "trace-kernel.h"
33#include "trace-ust.h"
54d01ffb 34
8c9ae521
DG
35/*
36 * Setup a lttng_event used to enable *all* syscall tracing.
37 */
38static 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
a027b2d2
MD
48/*
49 * Return 1 if loglevels match or 0 on failure.
50 */
51static int loglevel_match(struct ltt_ust_event *uevent,
52 enum lttng_ust_loglevel_type log_type, int loglevel)
53{
51f1a3b9
DG
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
a027b2d2
MD
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
70no_match:
71 return 0;
72}
73
54d01ffb 74/*
7a3d1328 75 * Disable kernel tracepoint event for a channel from the kernel session.
54d01ffb 76 */
7a3d1328 77int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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) {
f73fabfd 85 ret = LTTNG_ERR_NO_EVENT;
54d01ffb
DG
86 goto error;
87 }
88
89 ret = kernel_disable_event(kevent);
90 if (ret < 0) {
f73fabfd 91 ret = LTTNG_ERR_KERN_DISABLE_FAIL;
54d01ffb
DG
92 goto error;
93 }
94
95 DBG("Kernel event %s disable for channel %s.",
96 kevent->event->name, kchan->channel->name);
97
f73fabfd 98 ret = LTTNG_OK;
54d01ffb
DG
99
100error:
101 return ret;
102}
103
104/*
7a3d1328 105 * Disable kernel tracepoint events for a channel from the kernel session.
54d01ffb 106 */
7a3d1328 107int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
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 }
f73fabfd 121 ret = LTTNG_OK;
7a3d1328
MD
122 return ret;
123}
124
125/*
126 * Disable kernel syscall events for a channel from the kernel session.
127 */
128int 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.");
f73fabfd 132 return LTTNG_OK; /* Return OK so disable all succeeds */
7a3d1328 133}
54d01ffb 134
7a3d1328
MD
135/*
136 * Disable all kernel event for a channel from the kernel session.
137 */
138int 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);
f73fabfd 144 if (ret != LTTNG_OK)
7a3d1328
MD
145 return ret;
146 ret = event_kernel_disable_all_syscalls(ksession, kchan);
54d01ffb
DG
147 return ret;
148}
149
150/*
7a3d1328 151 * Enable kernel tracepoint event for a channel from the kernel session.
54d01ffb 152 */
7a3d1328 153int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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) {
bd29c13d
DG
163 switch (-ret) {
164 case EEXIST:
f73fabfd 165 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
166 break;
167 case ENOSYS:
f73fabfd 168 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d
DG
169 break;
170 default:
f73fabfd 171 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
bd29c13d 172 break;
d87bfb32 173 }
7a3d1328 174 goto end;
54d01ffb
DG
175 }
176 } else if (kevent->enabled == 0) {
177 ret = kernel_enable_event(kevent);
178 if (ret < 0) {
f73fabfd 179 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
7a3d1328 180 goto end;
54d01ffb 181 }
42224349
DG
182 } else {
183 /* At this point, the event is considered enabled */
f73fabfd 184 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349 185 goto end;
54d01ffb 186 }
42224349 187
f73fabfd 188 ret = LTTNG_OK;
7a3d1328 189end:
54d01ffb
DG
190 return ret;
191}
192
193/*
7a3d1328 194 * Enable all kernel tracepoint events of a channel of the kernel session.
54d01ffb 195 */
7a3d1328 196int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
197 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
198{
199 int size, i, ret;
200 struct ltt_kernel_event *kevent;
8f69e5eb 201 struct lttng_event *event_list = NULL;
54d01ffb
DG
202
203 /* For each event in the kernel session */
204 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
8f69e5eb
DG
205 if (kevent->enabled == 0) {
206 ret = kernel_enable_event(kevent);
207 if (ret < 0) {
208 /* Enable failed but still continue */
209 continue;
210 }
54d01ffb
DG
211 }
212 }
213
214 size = kernel_list_events(kernel_tracer_fd, &event_list);
215 if (size < 0) {
f73fabfd 216 ret = LTTNG_ERR_KERN_LIST_FAIL;
7a3d1328 217 goto end;
54d01ffb
DG
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 }
54d01ffb 232 free(event_list);
8f69e5eb 233
f73fabfd 234 ret = LTTNG_OK;
7a3d1328 235end:
54d01ffb 236 return ret;
7a3d1328 237
54d01ffb 238}
8c9ae521
DG
239
240/*
7a3d1328 241 * Enable all kernel tracepoint events of a channel of the kernel session.
8c9ae521 242 */
7a3d1328 243int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
8c9ae521
DG
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) {
8f69e5eb 255 if (ret == -EEXIST) {
f73fabfd 256 ret = LTTNG_ERR_KERN_EVENT_EXIST;
8f69e5eb 257 } else {
f73fabfd 258 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
8f69e5eb 259 }
7a3d1328 260 goto end;
8c9ae521 261 }
8f69e5eb 262
f73fabfd 263 ret = LTTNG_OK;
7a3d1328
MD
264end:
265 return ret;
266}
8c9ae521 267
7a3d1328
MD
268/*
269 * Enable all kernel events of a channel of the kernel session.
270 */
271int event_kernel_enable_all(struct ltt_kernel_session *ksession,
272 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
273{
6bd8707a 274 int tp_ret;
7a3d1328 275
bd29c13d 276 tp_ret = event_kernel_enable_all_tracepoints(ksession, kchan, kernel_tracer_fd);
f73fabfd 277 if (tp_ret != LTTNG_OK) {
7a3d1328
MD
278 goto end;
279 }
bd29c13d
DG
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 */
6bd8707a 290 (void) event_kernel_enable_all_syscalls(ksession, kchan, kernel_tracer_fd);
bd29c13d 291
7a3d1328 292end:
bd29c13d 293 return tp_ret;
8c9ae521 294}
2bdd86d4 295
7f79d3a1
DG
296/*
297 * ============================
298 * UST : The Ultimate Frontier!
299 * ============================
300 */
301
76d45b40
DG
302/*
303 * Enable all UST tracepoints for a channel from a UST session.
304 */
305int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess, int domain,
306 struct ltt_ust_channel *uchan)
307{
6775595e 308 int ret, i, size;
bec39940 309 struct lttng_ht_iter iter;
76d45b40 310 struct ltt_ust_event *uevent = NULL;
7f79d3a1 311 struct lttng_event *events = NULL;
76d45b40
DG
312
313 switch (domain) {
314 case LTTNG_DOMAIN_UST:
315 {
316 /* Enable existing events */
bec39940
DG
317 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
318 node.node) {
76d45b40
DG
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) {
f73fabfd 331 ret = LTTNG_ERR_UST_LIST_FAIL;
76d45b40
DG
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 != -EEXIST) {
f73fabfd 347 ret = LTTNG_ERR_UST_ENABLE_FAIL;
76d45b40
DG
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) {
f73fabfd 357 ret = LTTNG_ERR_FATAL;
7f79d3a1 358 goto error_destroy;
76d45b40
DG
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 == -EEXIST) {
f73fabfd 366 ret = LTTNG_ERR_UST_EVENT_EXIST;
7f79d3a1 367 goto error;
76d45b40 368 } else {
f73fabfd 369 ret = LTTNG_ERR_UST_ENABLE_FAIL;
7f79d3a1 370 goto error_destroy;
76d45b40 371 }
76d45b40
DG
372 }
373
374 uevent->enabled = 1;
375 /* Add ltt ust event to channel */
376 rcu_read_lock();
bec39940 377 lttng_ht_add_unique_str(uchan->events, &uevent->node);
76d45b40
DG
378 rcu_read_unlock();
379 }
380
381 free(events);
382 break;
383 }
d78d6610 384#if 0
76d45b40
DG
385 case LTTNG_DOMAIN_UST_EXEC_NAME:
386 case LTTNG_DOMAIN_UST_PID:
387 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 388#endif
76d45b40 389 default:
f73fabfd 390 ret = LTTNG_ERR_UND;
76d45b40
DG
391 goto error;
392 }
393
f73fabfd 394 return LTTNG_OK;
76d45b40 395
7f79d3a1 396error_destroy:
76d45b40 397 trace_ust_destroy_event(uevent);
7f79d3a1
DG
398
399error:
400 free(events);
76d45b40
DG
401 return ret;
402}
403
2bdd86d4
MD
404/*
405 * Enable UST tracepoint event for a channel from a UST session.
406 */
edb67388
DG
407int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
408 struct ltt_ust_channel *uchan, struct lttng_event *event)
2bdd86d4 409{
f73fabfd 410 int ret = LTTNG_OK, to_create = 0;
edb67388
DG
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) {
95a82664 417 ret = LTTNG_ERR_UST_ENABLE_FAIL;
edb67388
DG
418 goto error;
419 }
fc34caaa 420 /* Valid to set it after the goto error since uevent is still NULL */
edb67388
DG
421 to_create = 1;
422 }
2bdd86d4 423
a027b2d2
MD
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);
f73fabfd 434 ret = LTTNG_ERR_EVENT_EXIST_LOGLEVEL;
a027b2d2
MD
435 goto error;
436 }
437
7f79d3a1
DG
438 if (uevent->enabled) {
439 /* It's already enabled so everything is OK */
f73fabfd 440 ret = LTTNG_OK;
7f79d3a1
DG
441 goto end;
442 }
443
fc34caaa
DG
444 uevent->enabled = 1;
445
edb67388
DG
446 switch (domain) {
447 case LTTNG_DOMAIN_UST:
448 {
449 if (to_create) {
450 /* Create event on all UST registered apps for session */
35a9059d 451 ret = ust_app_create_event_glb(usess, uchan, uevent);
edb67388
DG
452 } else {
453 /* Enable event on all UST registered apps for session */
35a9059d 454 ret = ust_app_enable_event_glb(usess, uchan, uevent);
edb67388 455 }
48842b30 456
edb67388
DG
457 if (ret < 0) {
458 if (ret == -EEXIST) {
f73fabfd 459 ret = LTTNG_ERR_UST_EVENT_EXIST;
7f79d3a1 460 goto end;
edb67388 461 } else {
f73fabfd 462 ret = LTTNG_ERR_UST_ENABLE_FAIL;
7f79d3a1 463 goto error;
edb67388 464 }
edb67388 465 }
edb67388
DG
466 break;
467 }
d78d6610 468#if 0
edb67388
DG
469 case LTTNG_DOMAIN_UST_EXEC_NAME:
470 case LTTNG_DOMAIN_UST_PID:
471 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 472#endif
edb67388 473 default:
f73fabfd 474 ret = LTTNG_ERR_UND;
7f79d3a1 475 goto end;
2bdd86d4 476 }
48842b30 477
7f79d3a1
DG
478 if (to_create) {
479 rcu_read_lock();
fc34caaa 480 /* Add ltt ust event to channel */
bec39940 481 lttng_ht_add_unique_str(uchan->events, &uevent->node);
7f79d3a1
DG
482 rcu_read_unlock();
483 }
edb67388 484
7f79d3a1
DG
485 DBG("Event UST %s %s in channel %s", uevent->attr.name,
486 to_create ? "created" : "enabled", uchan->name);
487
f73fabfd 488 ret = LTTNG_OK;
fc34caaa 489
fb89d070 490end:
fc34caaa 491 return ret;
edb67388
DG
492
493error:
fc34caaa
DG
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 }
2bdd86d4
MD
505 return ret;
506}
507
7f79d3a1
DG
508/*
509 * Disable UST tracepoint of a channel from a UST session.
510 */
511int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
512 struct ltt_ust_channel *uchan, char *event_name)
2bdd86d4
MD
513{
514 int ret;
7f79d3a1 515 struct ltt_ust_event *uevent;
2bdd86d4 516
7f79d3a1
DG
517 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
518 if (uevent == NULL) {
f73fabfd 519 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
7f79d3a1 520 goto error;
2bdd86d4 521 }
7f79d3a1
DG
522
523 if (uevent->enabled == 0) {
524 /* It's already enabled so everything is OK */
f73fabfd 525 ret = LTTNG_OK;
2bdd86d4
MD
526 goto end;
527 }
7f79d3a1
DG
528
529 switch (domain) {
530 case LTTNG_DOMAIN_UST:
531 ret = ust_app_disable_event_glb(usess, uchan, uevent);
532 if (ret < 0 && ret != -EEXIST) {
f73fabfd 533 ret = LTTNG_ERR_UST_DISABLE_FAIL;
7f79d3a1
DG
534 goto error;
535 }
536 break;
d78d6610 537#if 0
7f79d3a1
DG
538 case LTTNG_DOMAIN_UST_EXEC_NAME:
539 case LTTNG_DOMAIN_UST_PID:
540 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 541#endif
7f79d3a1 542 default:
f73fabfd 543 ret = LTTNG_ERR_UND;
7f79d3a1
DG
544 goto error;
545 }
546
547 uevent->enabled = 0;
f73fabfd 548 ret = LTTNG_OK;
7f79d3a1 549
2bdd86d4 550end:
7f79d3a1
DG
551 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
552 uchan->name);
553
554error:
555 return ret;
556}
557
558/*
559 * Disable all UST tracepoints for a channel from a UST session.
560 */
561int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
562 struct ltt_ust_channel *uchan)
563{
6775595e 564 int ret, i, size;
bec39940 565 struct lttng_ht_iter iter;
7f79d3a1
DG
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 */
bec39940
DG
573 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
574 node.node) {
7f79d3a1
DG
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) {
f73fabfd 587 ret = LTTNG_ERR_UST_LIST_FAIL;
7f79d3a1
DG
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 != -EEXIST) {
f73fabfd 598 ret = LTTNG_ERR_UST_DISABLE_FAIL;
7f79d3a1
DG
599 goto error;
600 }
601 uevent->enabled = 0;
602 continue;
603 }
604 }
605
606 free(events);
607 break;
608 }
d78d6610 609#if 0
7f79d3a1
DG
610 case LTTNG_DOMAIN_UST_EXEC_NAME:
611 case LTTNG_DOMAIN_UST_PID:
612 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 613#endif
7f79d3a1 614 default:
f73fabfd 615 ret = LTTNG_ERR_UND;
7f79d3a1
DG
616 goto error;
617 }
618
f73fabfd 619 return LTTNG_OK;
7f79d3a1
DG
620
621error:
622 free(events);
2bdd86d4
MD
623 return ret;
624}
This page took 0.074991 seconds and 4 git commands to generate.