Add UST test ust-nprocesses
[lttng-tools.git] / 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>
bec39940 24#include <lttng-ht.h>
54d01ffb
DG
25#include <lttng-sessiond-comm.h>
26#include <lttngerr.h>
27
28#include "channel.h"
29#include "event.h"
4771f025 30#include "kernel.h"
9df8df5e 31#include "ust-ctl.h"
edb67388
DG
32#include "ust-app.h"
33#include "trace-kernel.h"
34#include "trace-ust.h"
54d01ffb 35
8c9ae521
DG
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
54d01ffb 49/*
7a3d1328 50 * Disable kernel tracepoint event for a channel from the kernel session.
54d01ffb 51 */
7a3d1328 52int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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/*
7a3d1328 80 * Disable kernel tracepoint events for a channel from the kernel session.
54d01ffb 81 */
7a3d1328 82int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
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 }
54d01ffb 96 ret = LTTCOMM_OK;
7a3d1328
MD
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}
54d01ffb 109
7a3d1328
MD
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);
54d01ffb
DG
122 return ret;
123}
124
125/*
7a3d1328 126 * Enable kernel tracepoint event for a channel from the kernel session.
54d01ffb 127 */
7a3d1328 128int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
54d01ffb
DG
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) {
d87bfb32
DG
138 if (ret == -EEXIST) {
139 ret = LTTCOMM_KERN_EVENT_EXIST;
140 } else {
141 ret = LTTCOMM_KERN_ENABLE_FAIL;
142 }
7a3d1328 143 goto end;
54d01ffb
DG
144 }
145 } else if (kevent->enabled == 0) {
146 ret = kernel_enable_event(kevent);
147 if (ret < 0) {
148 ret = LTTCOMM_KERN_ENABLE_FAIL;
7a3d1328 149 goto end;
54d01ffb
DG
150 }
151 }
54d01ffb 152 ret = LTTCOMM_OK;
7a3d1328 153end:
54d01ffb
DG
154 return ret;
155}
156
157/*
7a3d1328 158 * Enable all kernel tracepoint events of a channel of the kernel session.
54d01ffb 159 */
7a3d1328 160int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
54d01ffb
DG
161 struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
162{
163 int size, i, ret;
164 struct ltt_kernel_event *kevent;
8f69e5eb 165 struct lttng_event *event_list = NULL;
54d01ffb
DG
166
167 /* For each event in the kernel session */
168 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
8f69e5eb
DG
169 if (kevent->enabled == 0) {
170 ret = kernel_enable_event(kevent);
171 if (ret < 0) {
172 /* Enable failed but still continue */
173 continue;
174 }
54d01ffb
DG
175 }
176 }
177
178 size = kernel_list_events(kernel_tracer_fd, &event_list);
179 if (size < 0) {
180 ret = LTTCOMM_KERN_LIST_FAIL;
7a3d1328 181 goto end;
54d01ffb
DG
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 }
54d01ffb 196 free(event_list);
8f69e5eb 197
54d01ffb 198 ret = LTTCOMM_OK;
7a3d1328 199end:
54d01ffb 200 return ret;
7a3d1328 201
54d01ffb 202}
8c9ae521
DG
203
204/*
7a3d1328 205 * Enable all kernel tracepoint events of a channel of the kernel session.
8c9ae521 206 */
7a3d1328 207int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
8c9ae521
DG
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) {
8f69e5eb
DG
219 if (ret == -EEXIST) {
220 ret = LTTCOMM_KERN_EVENT_EXIST;
221 } else {
222 ret = LTTCOMM_KERN_ENABLE_FAIL;
223 }
7a3d1328 224 goto end;
8c9ae521 225 }
8f69e5eb 226
8c9ae521 227 ret = LTTCOMM_OK;
7a3d1328
MD
228end:
229 return ret;
230}
8c9ae521 231
7a3d1328
MD
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:
8c9ae521
DG
246 return ret;
247}
2bdd86d4 248
7f79d3a1
DG
249/*
250 * ============================
251 * UST : The Ultimate Frontier!
252 * ============================
253 */
254
76d45b40
DG
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;
bec39940 263 struct lttng_ht_iter iter;
76d45b40 264 struct ltt_ust_event *uevent = NULL;
7f79d3a1 265 struct lttng_event *events = NULL;
76d45b40
DG
266
267 switch (domain) {
268 case LTTNG_DOMAIN_UST:
269 {
270 /* Enable existing events */
bec39940
DG
271 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
272 node.node) {
76d45b40
DG
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;
7f79d3a1 312 goto error_destroy;
76d45b40
DG
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;
7f79d3a1 321 goto error;
76d45b40
DG
322 } else {
323 ret = LTTCOMM_UST_ENABLE_FAIL;
7f79d3a1 324 goto error_destroy;
76d45b40 325 }
76d45b40
DG
326 }
327
328 uevent->enabled = 1;
329 /* Add ltt ust event to channel */
330 rcu_read_lock();
bec39940 331 lttng_ht_add_unique_str(uchan->events, &uevent->node);
76d45b40
DG
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
7f79d3a1 348error_destroy:
76d45b40 349 trace_ust_destroy_event(uevent);
7f79d3a1
DG
350
351error:
352 free(events);
76d45b40
DG
353 return ret;
354}
355
2bdd86d4
MD
356/*
357 * Enable UST tracepoint event for a channel from a UST session.
358 */
edb67388
DG
359int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
360 struct ltt_ust_channel *uchan, struct lttng_event *event)
2bdd86d4 361{
fc34caaa 362 int ret = LTTCOMM_OK, to_create = 0;
edb67388
DG
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 }
fc34caaa 372 /* Valid to set it after the goto error since uevent is still NULL */
edb67388
DG
373 to_create = 1;
374 }
2bdd86d4 375
7f79d3a1
DG
376 if (uevent->enabled) {
377 /* It's already enabled so everything is OK */
7f79d3a1
DG
378 goto end;
379 }
380
fc34caaa
DG
381 uevent->enabled = 1;
382
edb67388
DG
383 switch (domain) {
384 case LTTNG_DOMAIN_UST:
385 {
386 if (to_create) {
387 /* Create event on all UST registered apps for session */
35a9059d 388 ret = ust_app_create_event_glb(usess, uchan, uevent);
edb67388
DG
389 } else {
390 /* Enable event on all UST registered apps for session */
35a9059d 391 ret = ust_app_enable_event_glb(usess, uchan, uevent);
edb67388 392 }
48842b30 393
edb67388
DG
394 if (ret < 0) {
395 if (ret == -EEXIST) {
396 ret = LTTCOMM_UST_EVENT_EXIST;
7f79d3a1 397 goto end;
edb67388
DG
398 } else {
399 ret = LTTCOMM_UST_ENABLE_FAIL;
7f79d3a1 400 goto error;
edb67388 401 }
edb67388 402 }
edb67388
DG
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;
7f79d3a1 410 goto end;
2bdd86d4 411 }
48842b30 412
7f79d3a1
DG
413 if (to_create) {
414 rcu_read_lock();
fc34caaa 415 /* Add ltt ust event to channel */
bec39940 416 lttng_ht_add_unique_str(uchan->events, &uevent->node);
7f79d3a1
DG
417 rcu_read_unlock();
418 }
edb67388 419
7f79d3a1
DG
420 DBG("Event UST %s %s in channel %s", uevent->attr.name,
421 to_create ? "created" : "enabled", uchan->name);
422
fc34caaa
DG
423 ret = LTTCOMM_OK;
424
fb89d070 425end:
fc34caaa 426 return ret;
edb67388
DG
427
428error:
fc34caaa
DG
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 }
2bdd86d4
MD
440 return ret;
441}
442
7f79d3a1
DG
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)
2bdd86d4
MD
448{
449 int ret;
7f79d3a1 450 struct ltt_ust_event *uevent;
2bdd86d4 451
7f79d3a1
DG
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;
2bdd86d4 456 }
7f79d3a1
DG
457
458 if (uevent->enabled == 0) {
459 /* It's already enabled so everything is OK */
460 ret = LTTCOMM_OK;
2bdd86d4
MD
461 goto end;
462 }
7f79d3a1
DG
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;
2bdd86d4 481 ret = LTTCOMM_OK;
7f79d3a1 482
2bdd86d4 483end:
7f79d3a1
DG
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;
bec39940 499 struct lttng_ht_iter iter;
7f79d3a1
DG
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 */
bec39940
DG
507 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
508 node.node) {
7f79d3a1
DG
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);
2bdd86d4
MD
555 return ret;
556}
This page took 0.048215 seconds and 4 git commands to generate.