Verify UST comm proto version
[lttng-tools.git] / src / bin / lttng-sessiond / event.c
CommitLineData
54d01ffb
DG
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
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
54d01ffb 48/*
7a3d1328 49 * Disable kernel tracepoint event for a channel from the kernel session.
54d01ffb 50 */
7a3d1328 51int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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
74error:
75 return ret;
76}
77
78/*
7a3d1328 79 * Disable kernel tracepoint events for a channel from the kernel session.
54d01ffb 80 */
7a3d1328 81int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
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 }
54d01ffb 95 ret = LTTCOMM_OK;
7a3d1328
MD
96 return ret;
97}
98
99/*
100 * Disable kernel syscall events for a channel from the kernel session.
101 */
102int 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}
54d01ffb 108
7a3d1328
MD
109/*
110 * Disable all kernel event for a channel from the kernel session.
111 */
112int 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);
54d01ffb
DG
121 return ret;
122}
123
124/*
7a3d1328 125 * Enable kernel tracepoint event for a channel from the kernel session.
54d01ffb 126 */
7a3d1328 127int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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) {
d87bfb32
DG
137 if (ret == -EEXIST) {
138 ret = LTTCOMM_KERN_EVENT_EXIST;
139 } else {
140 ret = LTTCOMM_KERN_ENABLE_FAIL;
141 }
7a3d1328 142 goto end;
54d01ffb
DG
143 }
144 } else if (kevent->enabled == 0) {
145 ret = kernel_enable_event(kevent);
146 if (ret < 0) {
147 ret = LTTCOMM_KERN_ENABLE_FAIL;
7a3d1328 148 goto end;
54d01ffb
DG
149 }
150 }
54d01ffb 151 ret = LTTCOMM_OK;
7a3d1328 152end:
54d01ffb
DG
153 return ret;
154}
155
156/*
7a3d1328 157 * Enable all kernel tracepoint events of a channel of the kernel session.
54d01ffb 158 */
7a3d1328 159int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
160 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
161{
162 int size, i, ret;
163 struct ltt_kernel_event *kevent;
8f69e5eb 164 struct lttng_event *event_list = NULL;
54d01ffb
DG
165
166 /* For each event in the kernel session */
167 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
8f69e5eb
DG
168 if (kevent->enabled == 0) {
169 ret = kernel_enable_event(kevent);
170 if (ret < 0) {
171 /* Enable failed but still continue */
172 continue;
173 }
54d01ffb
DG
174 }
175 }
176
177 size = kernel_list_events(kernel_tracer_fd, &event_list);
178 if (size < 0) {
179 ret = LTTCOMM_KERN_LIST_FAIL;
7a3d1328 180 goto end;
54d01ffb
DG
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 }
54d01ffb 195 free(event_list);
8f69e5eb 196
54d01ffb 197 ret = LTTCOMM_OK;
7a3d1328 198end:
54d01ffb 199 return ret;
7a3d1328 200
54d01ffb 201}
8c9ae521
DG
202
203/*
7a3d1328 204 * Enable all kernel tracepoint events of a channel of the kernel session.
8c9ae521 205 */
7a3d1328 206int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
8c9ae521
DG
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) {
8f69e5eb
DG
218 if (ret == -EEXIST) {
219 ret = LTTCOMM_KERN_EVENT_EXIST;
220 } else {
221 ret = LTTCOMM_KERN_ENABLE_FAIL;
222 }
7a3d1328 223 goto end;
8c9ae521 224 }
8f69e5eb 225
8c9ae521 226 ret = LTTCOMM_OK;
7a3d1328
MD
227end:
228 return ret;
229}
8c9ae521 230
7a3d1328
MD
231/*
232 * Enable all kernel events of a channel of the kernel session.
233 */
234int 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);
244end:
8c9ae521
DG
245 return ret;
246}
2bdd86d4 247
7f79d3a1
DG
248/*
249 * ============================
250 * UST : The Ultimate Frontier!
251 * ============================
252 */
253
76d45b40
DG
254/*
255 * Enable all UST tracepoints for a channel from a UST session.
256 */
257int 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;
bec39940 262 struct lttng_ht_iter iter;
76d45b40 263 struct ltt_ust_event *uevent = NULL;
7f79d3a1 264 struct lttng_event *events = NULL;
76d45b40
DG
265
266 switch (domain) {
267 case LTTNG_DOMAIN_UST:
268 {
269 /* Enable existing events */
bec39940
DG
270 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
271 node.node) {
76d45b40
DG
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;
7f79d3a1 311 goto error_destroy;
76d45b40
DG
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;
7f79d3a1 320 goto error;
76d45b40
DG
321 } else {
322 ret = LTTCOMM_UST_ENABLE_FAIL;
7f79d3a1 323 goto error_destroy;
76d45b40 324 }
76d45b40
DG
325 }
326
327 uevent->enabled = 1;
328 /* Add ltt ust event to channel */
329 rcu_read_lock();
bec39940 330 lttng_ht_add_unique_str(uchan->events, &uevent->node);
76d45b40
DG
331 rcu_read_unlock();
332 }
333
334 free(events);
335 break;
336 }
d78d6610 337#if 0
76d45b40
DG
338 case LTTNG_DOMAIN_UST_EXEC_NAME:
339 case LTTNG_DOMAIN_UST_PID:
340 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 341#endif
76d45b40 342 default:
1ab1ea0b 343 ret = LTTCOMM_UND;
76d45b40
DG
344 goto error;
345 }
346
347 return LTTCOMM_OK;
348
7f79d3a1 349error_destroy:
76d45b40 350 trace_ust_destroy_event(uevent);
7f79d3a1
DG
351
352error:
353 free(events);
76d45b40
DG
354 return ret;
355}
356
2bdd86d4
MD
357/*
358 * Enable UST tracepoint event for a channel from a UST session.
359 */
edb67388
DG
360int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
361 struct ltt_ust_channel *uchan, struct lttng_event *event)
2bdd86d4 362{
fc34caaa 363 int ret = LTTCOMM_OK, to_create = 0;
edb67388
DG
364 struct ltt_ust_event *uevent;
365
366 uevent = trace_ust_find_event_by_name(uchan->events, event->name);
367 if (uevent == NULL) {
368 uevent = trace_ust_create_event(event);
369 if (uevent == NULL) {
370 ret = LTTCOMM_FATAL;
371 goto error;
372 }
fc34caaa 373 /* Valid to set it after the goto error since uevent is still NULL */
edb67388
DG
374 to_create = 1;
375 }
2bdd86d4 376
7f79d3a1
DG
377 if (uevent->enabled) {
378 /* It's already enabled so everything is OK */
7f79d3a1
DG
379 goto end;
380 }
381
fc34caaa
DG
382 uevent->enabled = 1;
383
edb67388
DG
384 switch (domain) {
385 case LTTNG_DOMAIN_UST:
386 {
387 if (to_create) {
388 /* Create event on all UST registered apps for session */
35a9059d 389 ret = ust_app_create_event_glb(usess, uchan, uevent);
edb67388
DG
390 } else {
391 /* Enable event on all UST registered apps for session */
35a9059d 392 ret = ust_app_enable_event_glb(usess, uchan, uevent);
edb67388 393 }
48842b30 394
edb67388
DG
395 if (ret < 0) {
396 if (ret == -EEXIST) {
397 ret = LTTCOMM_UST_EVENT_EXIST;
7f79d3a1 398 goto end;
edb67388
DG
399 } else {
400 ret = LTTCOMM_UST_ENABLE_FAIL;
7f79d3a1 401 goto error;
edb67388 402 }
edb67388 403 }
edb67388
DG
404 break;
405 }
d78d6610 406#if 0
edb67388
DG
407 case LTTNG_DOMAIN_UST_EXEC_NAME:
408 case LTTNG_DOMAIN_UST_PID:
409 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 410#endif
edb67388 411 default:
1ab1ea0b 412 ret = LTTCOMM_UND;
7f79d3a1 413 goto end;
2bdd86d4 414 }
48842b30 415
7f79d3a1
DG
416 if (to_create) {
417 rcu_read_lock();
fc34caaa 418 /* Add ltt ust event to channel */
bec39940 419 lttng_ht_add_unique_str(uchan->events, &uevent->node);
7f79d3a1
DG
420 rcu_read_unlock();
421 }
edb67388 422
7f79d3a1
DG
423 DBG("Event UST %s %s in channel %s", uevent->attr.name,
424 to_create ? "created" : "enabled", uchan->name);
425
fc34caaa
DG
426 ret = LTTCOMM_OK;
427
fb89d070 428end:
fc34caaa 429 return ret;
edb67388
DG
430
431error:
fc34caaa
DG
432 /*
433 * Only destroy event on creation time (not enabling time) because if the
434 * event is found in the channel (to_create == 0), it means that at some
435 * point the enable_event worked and it's thus valid to keep it alive.
436 * Destroying it also implies that we also destroy it's shadow copy to sync
437 * everyone up.
438 */
439 if (to_create) {
440 /* In this code path, the uevent was not added to the hash table */
441 trace_ust_destroy_event(uevent);
442 }
2bdd86d4
MD
443 return ret;
444}
445
7f79d3a1
DG
446/*
447 * Disable UST tracepoint of a channel from a UST session.
448 */
449int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
450 struct ltt_ust_channel *uchan, char *event_name)
2bdd86d4
MD
451{
452 int ret;
7f79d3a1 453 struct ltt_ust_event *uevent;
2bdd86d4 454
7f79d3a1
DG
455 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
456 if (uevent == NULL) {
457 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
458 goto error;
2bdd86d4 459 }
7f79d3a1
DG
460
461 if (uevent->enabled == 0) {
462 /* It's already enabled so everything is OK */
463 ret = LTTCOMM_OK;
2bdd86d4
MD
464 goto end;
465 }
7f79d3a1
DG
466
467 switch (domain) {
468 case LTTNG_DOMAIN_UST:
469 ret = ust_app_disable_event_glb(usess, uchan, uevent);
470 if (ret < 0 && ret != -EEXIST) {
471 ret = LTTCOMM_UST_DISABLE_FAIL;
472 goto error;
473 }
474 break;
d78d6610 475#if 0
7f79d3a1
DG
476 case LTTNG_DOMAIN_UST_EXEC_NAME:
477 case LTTNG_DOMAIN_UST_PID:
478 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 479#endif
7f79d3a1 480 default:
1ab1ea0b 481 ret = LTTCOMM_UND;
7f79d3a1
DG
482 goto error;
483 }
484
485 uevent->enabled = 0;
2bdd86d4 486 ret = LTTCOMM_OK;
7f79d3a1 487
2bdd86d4 488end:
7f79d3a1
DG
489 DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
490 uchan->name);
491
492error:
493 return ret;
494}
495
496/*
497 * Disable all UST tracepoints for a channel from a UST session.
498 */
499int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
500 struct ltt_ust_channel *uchan)
501{
502 int ret, i;
503 size_t size;
bec39940 504 struct lttng_ht_iter iter;
7f79d3a1
DG
505 struct ltt_ust_event *uevent = NULL;
506 struct lttng_event *events = NULL;
507
508 switch (domain) {
509 case LTTNG_DOMAIN_UST:
510 {
511 /* Disabling existing events */
bec39940
DG
512 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
513 node.node) {
7f79d3a1
DG
514 if (uevent->enabled == 1) {
515 ret = ust_app_disable_event_glb(usess, uchan, uevent);
516 if (ret < 0) {
517 continue;
518 }
519 uevent->enabled = 0;
520 }
521 }
522
523 /* Get all UST available events */
524 size = ust_app_list_events(&events);
525 if (size < 0) {
526 ret = LTTCOMM_UST_LIST_FAIL;
527 goto error;
528 }
529
530 for (i = 0; i < size; i++) {
531 uevent = trace_ust_find_event_by_name(uchan->events,
532 events[i].name);
533 if (uevent != NULL && uevent->enabled == 1) {
534 ret = ust_app_disable_event_pid(usess, uchan, uevent,
535 events[i].pid);
536 if (ret < 0 && ret != -EEXIST) {
537 ret = LTTCOMM_UST_DISABLE_FAIL;
538 goto error;
539 }
540 uevent->enabled = 0;
541 continue;
542 }
543 }
544
545 free(events);
546 break;
547 }
d78d6610 548#if 0
7f79d3a1
DG
549 case LTTNG_DOMAIN_UST_EXEC_NAME:
550 case LTTNG_DOMAIN_UST_PID:
551 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 552#endif
7f79d3a1 553 default:
1ab1ea0b 554 ret = LTTCOMM_UND;
7f79d3a1
DG
555 goto error;
556 }
557
558 return LTTCOMM_OK;
559
560error:
561 free(events);
2bdd86d4
MD
562 return ret;
563}
This page took 0.050923 seconds and 4 git commands to generate.