f4c48179df4c0c8606bd437aac46dca84fd0fa67
[lttng-tools.git] / 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 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-sessiond-comm.h>
25 #include <lttngerr.h>
26
27 #include "channel.h"
28 #include "event.h"
29 #include "../common/hashtable.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 */
39 static 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 */
52 int 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
75 error:
76 return ret;
77 }
78
79 /*
80 * Disable kernel tracepoint events for a channel from the kernel session.
81 */
82 int 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 */
103 int 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 */
113 int 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 */
128 int 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;
153 end:
154 return ret;
155 }
156
157 /*
158 * Enable all kernel tracepoint events of a channel of the kernel session.
159 */
160 int 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;
199 end:
200 return ret;
201
202 }
203
204 /*
205 * Enable all kernel tracepoint events of a channel of the kernel session.
206 */
207 int 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;
228 end:
229 return ret;
230 }
231
232 /*
233 * Enable all kernel events of a channel of the kernel session.
234 */
235 int 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);
245 end:
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 */
258 int 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 cds_lfht_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, &iter, uevent, node) {
272 if (uevent->enabled == 0) {
273 ret = ust_app_enable_event_glb(usess, uchan, uevent);
274 if (ret < 0) {
275 continue;
276 }
277 uevent->enabled = 1;
278 }
279 }
280
281 /* Get all UST available events */
282 size = ust_app_list_events(&events);
283 if (size < 0) {
284 ret = LTTCOMM_UST_LIST_FAIL;
285 goto error;
286 }
287
288 for (i = 0; i < size; i++) {
289 /*
290 * Check if event exist and if so, continue since it was enable
291 * previously.
292 */
293 uevent = trace_ust_find_event_by_name(uchan->events,
294 events[i].name);
295 if (uevent != NULL) {
296 ret = ust_app_enable_event_pid(usess, uchan, uevent,
297 events[i].pid);
298 if (ret < 0) {
299 if (ret != -EEXIST) {
300 ret = LTTCOMM_UST_ENABLE_FAIL;
301 goto error;
302 }
303 }
304 continue;
305 }
306
307 /* Create ust event */
308 uevent = trace_ust_create_event(&events[i]);
309 if (uevent == NULL) {
310 ret = LTTCOMM_FATAL;
311 goto error_destroy;
312 }
313
314 /* Create event for the specific PID */
315 ret = ust_app_enable_event_pid(usess, uchan, uevent,
316 events[i].pid);
317 if (ret < 0) {
318 if (ret == -EEXIST) {
319 ret = LTTCOMM_UST_EVENT_EXIST;
320 goto error;
321 } else {
322 ret = LTTCOMM_UST_ENABLE_FAIL;
323 goto error_destroy;
324 }
325 }
326
327 uevent->enabled = 1;
328 /* Add ltt ust event to channel */
329 rcu_read_lock();
330 hashtable_add_unique(uchan->events, &uevent->node);
331 rcu_read_unlock();
332 }
333
334 free(events);
335 break;
336 }
337 case LTTNG_DOMAIN_UST_EXEC_NAME:
338 case LTTNG_DOMAIN_UST_PID:
339 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
340 default:
341 ret = LTTCOMM_NOT_IMPLEMENTED;
342 goto error;
343 }
344
345 return LTTCOMM_OK;
346
347 error_destroy:
348 trace_ust_destroy_event(uevent);
349
350 error:
351 free(events);
352 return ret;
353 }
354
355 /*
356 * Enable UST tracepoint event for a channel from a UST session.
357 */
358 int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
359 struct ltt_ust_channel *uchan, struct lttng_event *event)
360 {
361 int ret, to_create = 0;
362 struct ltt_ust_event *uevent;
363
364 uevent = trace_ust_find_event_by_name(uchan->events, event->name);
365 if (uevent == NULL) {
366 uevent = trace_ust_create_event(event);
367 if (uevent == NULL) {
368 ret = LTTCOMM_FATAL;
369 goto error;
370 }
371 to_create = 1;
372 }
373
374 if (uevent->enabled) {
375 /* It's already enabled so everything is OK */
376 goto end;
377 }
378
379 switch (domain) {
380 case LTTNG_DOMAIN_UST:
381 {
382 if (to_create) {
383 /* Create event on all UST registered apps for session */
384 ret = ust_app_create_event_glb(usess, uchan, uevent);
385 } else {
386 /* Enable event on all UST registered apps for session */
387 ret = ust_app_enable_event_glb(usess, uchan, uevent);
388 }
389
390 if (ret < 0) {
391 if (ret == -EEXIST) {
392 ret = LTTCOMM_UST_EVENT_EXIST;
393 goto end;
394 } else {
395 ret = LTTCOMM_UST_ENABLE_FAIL;
396 goto error;
397 }
398 }
399 break;
400 }
401 case LTTNG_DOMAIN_UST_EXEC_NAME:
402 case LTTNG_DOMAIN_UST_PID:
403 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
404 default:
405 ret = LTTCOMM_NOT_IMPLEMENTED;
406 goto end;
407 }
408
409 uevent->enabled = 1;
410 /* Add ltt ust event to channel */
411 if (to_create) {
412 rcu_read_lock();
413 hashtable_add_unique(uchan->events, &uevent->node);
414 rcu_read_unlock();
415 }
416
417 DBG("Event UST %s %s in channel %s", uevent->attr.name,
418 to_create ? "created" : "enabled", uchan->name);
419
420 end:
421 return LTTCOMM_OK;
422
423 error:
424 trace_ust_destroy_event(uevent);
425 return ret;
426 }
427
428 /*
429 * Disable UST tracepoint of a channel from a UST session.
430 */
431 int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
432 struct ltt_ust_channel *uchan, char *event_name)
433 {
434 int ret;
435 struct ltt_ust_event *uevent;
436
437 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
438 if (uevent == NULL) {
439 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
440 goto error;
441 }
442
443 if (uevent->enabled == 0) {
444 /* It's already enabled so everything is OK */
445 ret = LTTCOMM_OK;
446 goto end;
447 }
448
449 switch (domain) {
450 case LTTNG_DOMAIN_UST:
451 ret = ust_app_disable_event_glb(usess, uchan, uevent);
452 if (ret < 0 && ret != -EEXIST) {
453 ret = LTTCOMM_UST_DISABLE_FAIL;
454 goto error;
455 }
456 break;
457 case LTTNG_DOMAIN_UST_EXEC_NAME:
458 case LTTNG_DOMAIN_UST_PID:
459 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
460 default:
461 ret = LTTCOMM_NOT_IMPLEMENTED;
462 goto error;
463 }
464
465 uevent->enabled = 0;
466 ret = LTTCOMM_OK;
467
468 end:
469 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
470 uchan->name);
471
472 error:
473 return ret;
474 }
475
476 /*
477 * Disable all UST tracepoints for a channel from a UST session.
478 */
479 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
480 struct ltt_ust_channel *uchan)
481 {
482 int ret, i;
483 size_t size;
484 struct cds_lfht_iter iter;
485 struct ltt_ust_event *uevent = NULL;
486 struct lttng_event *events = NULL;
487
488 switch (domain) {
489 case LTTNG_DOMAIN_UST:
490 {
491 /* Disabling existing events */
492 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
493 if (uevent->enabled == 1) {
494 ret = ust_app_disable_event_glb(usess, uchan, uevent);
495 if (ret < 0) {
496 continue;
497 }
498 uevent->enabled = 0;
499 }
500 }
501
502 /* Get all UST available events */
503 size = ust_app_list_events(&events);
504 if (size < 0) {
505 ret = LTTCOMM_UST_LIST_FAIL;
506 goto error;
507 }
508
509 for (i = 0; i < size; i++) {
510 uevent = trace_ust_find_event_by_name(uchan->events,
511 events[i].name);
512 if (uevent != NULL && uevent->enabled == 1) {
513 ret = ust_app_disable_event_pid(usess, uchan, uevent,
514 events[i].pid);
515 if (ret < 0 && ret != -EEXIST) {
516 ret = LTTCOMM_UST_DISABLE_FAIL;
517 goto error;
518 }
519 uevent->enabled = 0;
520 continue;
521 }
522 }
523
524 free(events);
525 break;
526 }
527 case LTTNG_DOMAIN_UST_EXEC_NAME:
528 case LTTNG_DOMAIN_UST_PID:
529 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
530 default:
531 ret = LTTCOMM_NOT_IMPLEMENTED;
532 goto error;
533 }
534
535 return LTTCOMM_OK;
536
537 error:
538 free(events);
539 return ret;
540 }
This page took 0.039959 seconds and 3 git commands to generate.