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