Fix double free and out of order delete session
[lttng-tools.git] / 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 it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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 <lttng-ht.h>
25#include <lttng-sessiond-comm.h>
26#include <lttngerr.h>
27
28#include "channel.h"
29#include "event.h"
30#include "kernel.h"
31#include "ust-ctl.h"
32#include "ust-app.h"
33#include "trace-kernel.h"
34#include "trace-ust.h"
35
36/*
37 * Setup a lttng_event used to enable *all* syscall tracing.
38 */
39static void init_syscalls_kernel_event(struct lttng_event *event)
40{
41 event->name[0] = '\0';
42 /*
43 * We use LTTNG_EVENT* here since the trace kernel creation will make the
44 * right changes for the kernel.
45 */
46 event->type = LTTNG_EVENT_SYSCALL;
47}
48
49/*
50 * Disable kernel tracepoint event for a channel from the kernel session.
51 */
52int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
53 struct ltt_kernel_channel *kchan, char *event_name)
54{
55 int ret;
56 struct ltt_kernel_event *kevent;
57
58 kevent = trace_kernel_get_event_by_name(event_name, kchan);
59 if (kevent == NULL) {
60 ret = LTTCOMM_NO_EVENT;
61 goto error;
62 }
63
64 ret = kernel_disable_event(kevent);
65 if (ret < 0) {
66 ret = LTTCOMM_KERN_DISABLE_FAIL;
67 goto error;
68 }
69
70 DBG("Kernel event %s disable for channel %s.",
71 kevent->event->name, kchan->channel->name);
72
73 ret = LTTCOMM_OK;
74
75error:
76 return ret;
77}
78
79/*
80 * Disable kernel tracepoint events for a channel from the kernel session.
81 */
82int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
83 struct ltt_kernel_channel *kchan)
84{
85 int ret;
86 struct ltt_kernel_event *kevent;
87
88 /* For each event in the kernel session */
89 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
90 ret = kernel_disable_event(kevent);
91 if (ret < 0) {
92 /* We continue disabling the rest */
93 continue;
94 }
95 }
96 ret = LTTCOMM_OK;
97 return ret;
98}
99
100/*
101 * Disable kernel syscall events for a channel from the kernel session.
102 */
103int event_kernel_disable_all_syscalls(struct ltt_kernel_session *ksession,
104 struct ltt_kernel_channel *kchan)
105{
106 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
107 return LTTCOMM_OK; /* Return OK so disable all succeeds */
108}
109
110/*
111 * Disable all kernel event for a channel from the kernel session.
112 */
113int event_kernel_disable_all(struct ltt_kernel_session *ksession,
114 struct ltt_kernel_channel *kchan)
115{
116 int ret;
117
118 ret = event_kernel_disable_all_tracepoints(ksession, kchan);
119 if (ret != LTTCOMM_OK)
120 return ret;
121 ret = event_kernel_disable_all_syscalls(ksession, kchan);
122 return ret;
123}
124
125/*
126 * Enable kernel tracepoint event for a channel from the kernel session.
127 */
128int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
129 struct ltt_kernel_channel *kchan, struct lttng_event *event)
130{
131 int ret;
132 struct ltt_kernel_event *kevent;
133
134 kevent = trace_kernel_get_event_by_name(event->name, kchan);
135 if (kevent == NULL) {
136 ret = kernel_create_event(event, kchan);
137 if (ret < 0) {
138 if (ret == -EEXIST) {
139 ret = LTTCOMM_KERN_EVENT_EXIST;
140 } else {
141 ret = LTTCOMM_KERN_ENABLE_FAIL;
142 }
143 goto end;
144 }
145 } else if (kevent->enabled == 0) {
146 ret = kernel_enable_event(kevent);
147 if (ret < 0) {
148 ret = LTTCOMM_KERN_ENABLE_FAIL;
149 goto end;
150 }
151 }
152 ret = LTTCOMM_OK;
153end:
154 return ret;
155}
156
157/*
158 * Enable all kernel tracepoint events of a channel of the kernel session.
159 */
160int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
161 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
162{
163 int size, i, ret;
164 struct ltt_kernel_event *kevent;
165 struct lttng_event *event_list = NULL;
166
167 /* For each event in the kernel session */
168 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
169 if (kevent->enabled == 0) {
170 ret = kernel_enable_event(kevent);
171 if (ret < 0) {
172 /* Enable failed but still continue */
173 continue;
174 }
175 }
176 }
177
178 size = kernel_list_events(kernel_tracer_fd, &event_list);
179 if (size < 0) {
180 ret = LTTCOMM_KERN_LIST_FAIL;
181 goto end;
182 }
183
184 for (i = 0; i < size; i++) {
185 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
186 if (kevent == NULL) {
187 /* Default event type for enable all */
188 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
189 /* Enable each single tracepoint event */
190 ret = kernel_create_event(&event_list[i], kchan);
191 if (ret < 0) {
192 /* Ignore error here and continue */
193 }
194 }
195 }
196 free(event_list);
197
198 ret = LTTCOMM_OK;
199end:
200 return ret;
201
202}
203
204/*
205 * Enable all kernel tracepoint events of a channel of the kernel session.
206 */
207int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
208 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
209{
210 int ret;
211 struct lttng_event event;
212
213 init_syscalls_kernel_event(&event);
214
215 DBG("Enabling all syscall tracing");
216
217 ret = kernel_create_event(&event, kchan);
218 if (ret < 0) {
219 if (ret == -EEXIST) {
220 ret = LTTCOMM_KERN_EVENT_EXIST;
221 } else {
222 ret = LTTCOMM_KERN_ENABLE_FAIL;
223 }
224 goto end;
225 }
226
227 ret = LTTCOMM_OK;
228end:
229 return ret;
230}
231
232/*
233 * Enable all kernel events of a channel of the kernel session.
234 */
235int event_kernel_enable_all(struct ltt_kernel_session *ksession,
236 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
237{
238 int ret;
239
240 ret = event_kernel_enable_all_tracepoints(ksession, kchan, kernel_tracer_fd);
241 if (ret != LTTCOMM_OK) {
242 goto end;
243 }
244 ret = event_kernel_enable_all_syscalls(ksession, kchan, kernel_tracer_fd);
245end:
246 return ret;
247}
248
249/*
250 * ============================
251 * UST : The Ultimate Frontier!
252 * ============================
253 */
254
255/*
256 * Enable all UST tracepoints for a channel from a UST session.
257 */
258int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess, int domain,
259 struct ltt_ust_channel *uchan)
260{
261 int ret, i;
262 size_t size;
263 struct lttng_ht_iter iter;
264 struct ltt_ust_event *uevent = NULL;
265 struct lttng_event *events = NULL;
266
267 switch (domain) {
268 case LTTNG_DOMAIN_UST:
269 {
270 /* Enable existing events */
271 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
272 node.node) {
273 if (uevent->enabled == 0) {
274 ret = ust_app_enable_event_glb(usess, uchan, uevent);
275 if (ret < 0) {
276 continue;
277 }
278 uevent->enabled = 1;
279 }
280 }
281
282 /* Get all UST available events */
283 size = ust_app_list_events(&events);
284 if (size < 0) {
285 ret = LTTCOMM_UST_LIST_FAIL;
286 goto error;
287 }
288
289 for (i = 0; i < size; i++) {
290 /*
291 * Check if event exist and if so, continue since it was enable
292 * previously.
293 */
294 uevent = trace_ust_find_event_by_name(uchan->events,
295 events[i].name);
296 if (uevent != NULL) {
297 ret = ust_app_enable_event_pid(usess, uchan, uevent,
298 events[i].pid);
299 if (ret < 0) {
300 if (ret != -EEXIST) {
301 ret = LTTCOMM_UST_ENABLE_FAIL;
302 goto error;
303 }
304 }
305 continue;
306 }
307
308 /* Create ust event */
309 uevent = trace_ust_create_event(&events[i]);
310 if (uevent == NULL) {
311 ret = LTTCOMM_FATAL;
312 goto error_destroy;
313 }
314
315 /* Create event for the specific PID */
316 ret = ust_app_enable_event_pid(usess, uchan, uevent,
317 events[i].pid);
318 if (ret < 0) {
319 if (ret == -EEXIST) {
320 ret = LTTCOMM_UST_EVENT_EXIST;
321 goto error;
322 } else {
323 ret = LTTCOMM_UST_ENABLE_FAIL;
324 goto error_destroy;
325 }
326 }
327
328 uevent->enabled = 1;
329 /* Add ltt ust event to channel */
330 rcu_read_lock();
331 lttng_ht_add_unique_str(uchan->events, &uevent->node);
332 rcu_read_unlock();
333 }
334
335 free(events);
336 break;
337 }
338 case LTTNG_DOMAIN_UST_EXEC_NAME:
339 case LTTNG_DOMAIN_UST_PID:
340 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
341 default:
342 ret = LTTCOMM_NOT_IMPLEMENTED;
343 goto error;
344 }
345
346 return LTTCOMM_OK;
347
348error_destroy:
349 trace_ust_destroy_event(uevent);
350
351error:
352 free(events);
353 return ret;
354}
355
356/*
357 * Enable UST tracepoint event for a channel from a UST session.
358 */
359int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
360 struct ltt_ust_channel *uchan, struct lttng_event *event)
361{
362 int ret = LTTCOMM_OK, to_create = 0;
363 struct ltt_ust_event *uevent;
364
365 uevent = trace_ust_find_event_by_name(uchan->events, event->name);
366 if (uevent == NULL) {
367 uevent = trace_ust_create_event(event);
368 if (uevent == NULL) {
369 ret = LTTCOMM_FATAL;
370 goto error;
371 }
372 /* Valid to set it after the goto error since uevent is still NULL */
373 to_create = 1;
374 }
375
376 if (uevent->enabled) {
377 /* It's already enabled so everything is OK */
378 goto end;
379 }
380
381 uevent->enabled = 1;
382
383 switch (domain) {
384 case LTTNG_DOMAIN_UST:
385 {
386 if (to_create) {
387 /* Create event on all UST registered apps for session */
388 ret = ust_app_create_event_glb(usess, uchan, uevent);
389 } else {
390 /* Enable event on all UST registered apps for session */
391 ret = ust_app_enable_event_glb(usess, uchan, uevent);
392 }
393
394 if (ret < 0) {
395 if (ret == -EEXIST) {
396 ret = LTTCOMM_UST_EVENT_EXIST;
397 goto end;
398 } else {
399 ret = LTTCOMM_UST_ENABLE_FAIL;
400 goto error;
401 }
402 }
403 break;
404 }
405 case LTTNG_DOMAIN_UST_EXEC_NAME:
406 case LTTNG_DOMAIN_UST_PID:
407 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
408 default:
409 ret = LTTCOMM_NOT_IMPLEMENTED;
410 goto end;
411 }
412
413 if (to_create) {
414 rcu_read_lock();
415 /* Add ltt ust event to channel */
416 lttng_ht_add_unique_str(uchan->events, &uevent->node);
417 rcu_read_unlock();
418 }
419
420 DBG("Event UST %s %s in channel %s", uevent->attr.name,
421 to_create ? "created" : "enabled", uchan->name);
422
423 ret = LTTCOMM_OK;
424
425end:
426 return ret;
427
428error:
429 /*
430 * Only destroy event on creation time (not enabling time) because if the
431 * event is found in the channel (to_create == 0), it means that at some
432 * point the enable_event worked and it's thus valid to keep it alive.
433 * Destroying it also implies that we also destroy it's shadow copy to sync
434 * everyone up.
435 */
436 if (to_create) {
437 /* In this code path, the uevent was not added to the hash table */
438 trace_ust_destroy_event(uevent);
439 }
440 return ret;
441}
442
443/*
444 * Disable UST tracepoint of a channel from a UST session.
445 */
446int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
447 struct ltt_ust_channel *uchan, char *event_name)
448{
449 int ret;
450 struct ltt_ust_event *uevent;
451
452 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
453 if (uevent == NULL) {
454 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
455 goto error;
456 }
457
458 if (uevent->enabled == 0) {
459 /* It's already enabled so everything is OK */
460 ret = LTTCOMM_OK;
461 goto end;
462 }
463
464 switch (domain) {
465 case LTTNG_DOMAIN_UST:
466 ret = ust_app_disable_event_glb(usess, uchan, uevent);
467 if (ret < 0 && ret != -EEXIST) {
468 ret = LTTCOMM_UST_DISABLE_FAIL;
469 goto error;
470 }
471 break;
472 case LTTNG_DOMAIN_UST_EXEC_NAME:
473 case LTTNG_DOMAIN_UST_PID:
474 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
475 default:
476 ret = LTTCOMM_NOT_IMPLEMENTED;
477 goto error;
478 }
479
480 uevent->enabled = 0;
481 ret = LTTCOMM_OK;
482
483end:
484 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
485 uchan->name);
486
487error:
488 return ret;
489}
490
491/*
492 * Disable all UST tracepoints for a channel from a UST session.
493 */
494int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
495 struct ltt_ust_channel *uchan)
496{
497 int ret, i;
498 size_t size;
499 struct lttng_ht_iter iter;
500 struct ltt_ust_event *uevent = NULL;
501 struct lttng_event *events = NULL;
502
503 switch (domain) {
504 case LTTNG_DOMAIN_UST:
505 {
506 /* Disabling existing events */
507 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
508 node.node) {
509 if (uevent->enabled == 1) {
510 ret = ust_app_disable_event_glb(usess, uchan, uevent);
511 if (ret < 0) {
512 continue;
513 }
514 uevent->enabled = 0;
515 }
516 }
517
518 /* Get all UST available events */
519 size = ust_app_list_events(&events);
520 if (size < 0) {
521 ret = LTTCOMM_UST_LIST_FAIL;
522 goto error;
523 }
524
525 for (i = 0; i < size; i++) {
526 uevent = trace_ust_find_event_by_name(uchan->events,
527 events[i].name);
528 if (uevent != NULL && uevent->enabled == 1) {
529 ret = ust_app_disable_event_pid(usess, uchan, uevent,
530 events[i].pid);
531 if (ret < 0 && ret != -EEXIST) {
532 ret = LTTCOMM_UST_DISABLE_FAIL;
533 goto error;
534 }
535 uevent->enabled = 0;
536 continue;
537 }
538 }
539
540 free(events);
541 break;
542 }
543 case LTTNG_DOMAIN_UST_EXEC_NAME:
544 case LTTNG_DOMAIN_UST_PID:
545 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
546 default:
547 ret = LTTCOMM_NOT_IMPLEMENTED;
548 goto error;
549 }
550
551 return LTTCOMM_OK;
552
553error:
554 free(events);
555 return ret;
556}
This page took 0.025726 seconds and 4 git commands to generate.