8582d2f8fc4907e59745feb07c93473ee08c7733
[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-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 */
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 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
348 error_destroy:
349 trace_ust_destroy_event(uevent);
350
351 error:
352 free(events);
353 return ret;
354 }
355
356 /*
357 * Enable UST tracepoint event for a channel from a UST session.
358 */
359 int 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, 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 to_create = 1;
373 }
374
375 if (uevent->enabled) {
376 /* It's already enabled so everything is OK */
377 goto end;
378 }
379
380 switch (domain) {
381 case LTTNG_DOMAIN_UST:
382 {
383 if (to_create) {
384 /* Create event on all UST registered apps for session */
385 ret = ust_app_create_event_glb(usess, uchan, uevent);
386 } else {
387 /* Enable event on all UST registered apps for session */
388 ret = ust_app_enable_event_glb(usess, uchan, uevent);
389 }
390
391 if (ret < 0) {
392 if (ret == -EEXIST) {
393 ret = LTTCOMM_UST_EVENT_EXIST;
394 goto end;
395 } else {
396 ret = LTTCOMM_UST_ENABLE_FAIL;
397 goto error;
398 }
399 }
400 break;
401 }
402 case LTTNG_DOMAIN_UST_EXEC_NAME:
403 case LTTNG_DOMAIN_UST_PID:
404 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
405 default:
406 ret = LTTCOMM_NOT_IMPLEMENTED;
407 goto end;
408 }
409
410 uevent->enabled = 1;
411 /* Add ltt ust event to channel */
412 if (to_create) {
413 rcu_read_lock();
414 lttng_ht_add_unique_str(uchan->events, &uevent->node);
415 rcu_read_unlock();
416 }
417
418 DBG("Event UST %s %s in channel %s", uevent->attr.name,
419 to_create ? "created" : "enabled", uchan->name);
420
421 end:
422 return LTTCOMM_OK;
423
424 error:
425 trace_ust_destroy_event(uevent);
426 return ret;
427 }
428
429 /*
430 * Disable UST tracepoint of a channel from a UST session.
431 */
432 int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
433 struct ltt_ust_channel *uchan, char *event_name)
434 {
435 int ret;
436 struct ltt_ust_event *uevent;
437
438 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
439 if (uevent == NULL) {
440 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
441 goto error;
442 }
443
444 if (uevent->enabled == 0) {
445 /* It's already enabled so everything is OK */
446 ret = LTTCOMM_OK;
447 goto end;
448 }
449
450 switch (domain) {
451 case LTTNG_DOMAIN_UST:
452 ret = ust_app_disable_event_glb(usess, uchan, uevent);
453 if (ret < 0 && ret != -EEXIST) {
454 ret = LTTCOMM_UST_DISABLE_FAIL;
455 goto error;
456 }
457 break;
458 case LTTNG_DOMAIN_UST_EXEC_NAME:
459 case LTTNG_DOMAIN_UST_PID:
460 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
461 default:
462 ret = LTTCOMM_NOT_IMPLEMENTED;
463 goto error;
464 }
465
466 uevent->enabled = 0;
467 ret = LTTCOMM_OK;
468
469 end:
470 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
471 uchan->name);
472
473 error:
474 return ret;
475 }
476
477 /*
478 * Disable all UST tracepoints for a channel from a UST session.
479 */
480 int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
481 struct ltt_ust_channel *uchan)
482 {
483 int ret, i;
484 size_t size;
485 struct lttng_ht_iter iter;
486 struct ltt_ust_event *uevent = NULL;
487 struct lttng_event *events = NULL;
488
489 switch (domain) {
490 case LTTNG_DOMAIN_UST:
491 {
492 /* Disabling existing events */
493 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
494 node.node) {
495 if (uevent->enabled == 1) {
496 ret = ust_app_disable_event_glb(usess, uchan, uevent);
497 if (ret < 0) {
498 continue;
499 }
500 uevent->enabled = 0;
501 }
502 }
503
504 /* Get all UST available events */
505 size = ust_app_list_events(&events);
506 if (size < 0) {
507 ret = LTTCOMM_UST_LIST_FAIL;
508 goto error;
509 }
510
511 for (i = 0; i < size; i++) {
512 uevent = trace_ust_find_event_by_name(uchan->events,
513 events[i].name);
514 if (uevent != NULL && uevent->enabled == 1) {
515 ret = ust_app_disable_event_pid(usess, uchan, uevent,
516 events[i].pid);
517 if (ret < 0 && ret != -EEXIST) {
518 ret = LTTCOMM_UST_DISABLE_FAIL;
519 goto error;
520 }
521 uevent->enabled = 0;
522 continue;
523 }
524 }
525
526 free(events);
527 break;
528 }
529 case LTTNG_DOMAIN_UST_EXEC_NAME:
530 case LTTNG_DOMAIN_UST_PID:
531 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
532 default:
533 ret = LTTCOMM_NOT_IMPLEMENTED;
534 goto error;
535 }
536
537 return LTTCOMM_OK;
538
539 error:
540 free(events);
541 return ret;
542 }
This page took 0.058815 seconds and 3 git commands to generate.