Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns an object descriptor or failure.
10 * - channel creation, returns an object descriptor or failure.
11 * - Operates on a session object descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns an object descriptor or failure.
14 * - Operates on a channel object descriptor.
15 * - stream notifier get, returns an object descriptor or failure.
16 * - Operates on a channel object descriptor.
17 * - event creation, returns an object descriptor or failure.
18 * - Operates on a channel object descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
23 */
24
25 #define _LGPL_SOURCE
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <unistd.h>
29
30 #include <urcu/compiler.h>
31 #include <urcu/list.h>
32
33 #include <helper.h>
34 #include <lttng/tracepoint.h>
35 #include <lttng/ust-abi.h>
36 #include <lttng/ust-error.h>
37 #include <lttng/ust-events.h>
38 #include <lttng/ust-version.h>
39 #include <ust-fd.h>
40 #include <usterr-signal-safe.h>
41
42 #include "../libringbuffer/frontend_types.h"
43 #include "../libringbuffer/shm.h"
44 #include "../libcounter/counter.h"
45 #include "tracepoint-internal.h"
46 #include "lttng-tracer.h"
47 #include "string-utils.h"
48 #include "ust-events-internal.h"
49
50 #define OBJ_NAME_LEN 16
51
52 static int lttng_ust_abi_close_in_progress;
53
54 static
55 int lttng_abi_tracepoint_list(void *owner);
56 static
57 int lttng_abi_tracepoint_field_list(void *owner);
58
59 /*
60 * Object descriptor table. Should be protected from concurrent access
61 * by the caller.
62 */
63
64 struct lttng_ust_obj {
65 union {
66 struct {
67 void *private_data;
68 const struct lttng_ust_objd_ops *ops;
69 int f_count;
70 int owner_ref; /* has ref from owner */
71 void *owner;
72 char name[OBJ_NAME_LEN];
73 } s;
74 int freelist_next; /* offset freelist. end is -1. */
75 } u;
76 };
77
78 struct lttng_ust_objd_table {
79 struct lttng_ust_obj *array;
80 unsigned int len, allocated_len;
81 int freelist_head; /* offset freelist head. end is -1 */
82 };
83
84 static struct lttng_ust_objd_table objd_table = {
85 .freelist_head = -1,
86 };
87
88 static
89 int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
90 void *owner, const char *name)
91 {
92 struct lttng_ust_obj *obj;
93
94 if (objd_table.freelist_head != -1) {
95 obj = &objd_table.array[objd_table.freelist_head];
96 objd_table.freelist_head = obj->u.freelist_next;
97 goto end;
98 }
99
100 if (objd_table.len >= objd_table.allocated_len) {
101 unsigned int new_allocated_len, old_allocated_len;
102 struct lttng_ust_obj *new_table, *old_table;
103
104 old_allocated_len = objd_table.allocated_len;
105 old_table = objd_table.array;
106 if (!old_allocated_len)
107 new_allocated_len = 1;
108 else
109 new_allocated_len = old_allocated_len << 1;
110 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
111 if (!new_table)
112 return -ENOMEM;
113 memcpy(new_table, old_table,
114 sizeof(struct lttng_ust_obj) * old_allocated_len);
115 free(old_table);
116 objd_table.array = new_table;
117 objd_table.allocated_len = new_allocated_len;
118 }
119 obj = &objd_table.array[objd_table.len];
120 objd_table.len++;
121 end:
122 obj->u.s.private_data = private_data;
123 obj->u.s.ops = ops;
124 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
125 /* count == 2 : allocated + hold ref */
126 obj->u.s.owner_ref = 1; /* One owner reference */
127 obj->u.s.owner = owner;
128 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
129 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
130 return obj - objd_table.array;
131 }
132
133 static
134 struct lttng_ust_obj *_objd_get(int id)
135 {
136 if (id >= objd_table.len)
137 return NULL;
138 if (!objd_table.array[id].u.s.f_count)
139 return NULL;
140 return &objd_table.array[id];
141 }
142
143 static
144 void *objd_private(int id)
145 {
146 struct lttng_ust_obj *obj = _objd_get(id);
147 assert(obj);
148 return obj->u.s.private_data;
149 }
150
151 static
152 void objd_set_private(int id, void *private_data)
153 {
154 struct lttng_ust_obj *obj = _objd_get(id);
155 assert(obj);
156 obj->u.s.private_data = private_data;
157 }
158
159 const struct lttng_ust_objd_ops *objd_ops(int id)
160 {
161 struct lttng_ust_obj *obj = _objd_get(id);
162
163 if (!obj)
164 return NULL;
165 return obj->u.s.ops;
166 }
167
168 static
169 void objd_free(int id)
170 {
171 struct lttng_ust_obj *obj = _objd_get(id);
172
173 assert(obj);
174 obj->u.freelist_next = objd_table.freelist_head;
175 objd_table.freelist_head = obj - objd_table.array;
176 assert(obj->u.s.f_count == 1);
177 obj->u.s.f_count = 0; /* deallocated */
178 }
179
180 static
181 void objd_ref(int id)
182 {
183 struct lttng_ust_obj *obj = _objd_get(id);
184 assert(obj != NULL);
185 obj->u.s.f_count++;
186 }
187
188 int lttng_ust_objd_unref(int id, int is_owner)
189 {
190 struct lttng_ust_obj *obj = _objd_get(id);
191
192 if (!obj)
193 return -EINVAL;
194 if (obj->u.s.f_count == 1) {
195 ERR("Reference counting error\n");
196 return -EINVAL;
197 }
198 if (is_owner) {
199 if (!obj->u.s.owner_ref) {
200 ERR("Error decrementing owner reference");
201 return -EINVAL;
202 }
203 obj->u.s.owner_ref--;
204 }
205 if ((--obj->u.s.f_count) == 1) {
206 const struct lttng_ust_objd_ops *ops = objd_ops(id);
207
208 if (ops->release)
209 ops->release(id);
210 objd_free(id);
211 }
212 return 0;
213 }
214
215 static
216 void objd_table_destroy(void)
217 {
218 int i;
219
220 for (i = 0; i < objd_table.allocated_len; i++) {
221 struct lttng_ust_obj *obj;
222
223 obj = _objd_get(i);
224 if (!obj)
225 continue;
226 if (!obj->u.s.owner_ref)
227 continue; /* only unref owner ref. */
228 (void) lttng_ust_objd_unref(i, 1);
229 }
230 free(objd_table.array);
231 objd_table.array = NULL;
232 objd_table.len = 0;
233 objd_table.allocated_len = 0;
234 objd_table.freelist_head = -1;
235 }
236
237 const char *lttng_ust_obj_get_name(int id)
238 {
239 struct lttng_ust_obj *obj = _objd_get(id);
240
241 if (!obj)
242 return NULL;
243 return obj->u.s.name;
244 }
245
246 void lttng_ust_objd_table_owner_cleanup(void *owner)
247 {
248 int i;
249
250 for (i = 0; i < objd_table.allocated_len; i++) {
251 struct lttng_ust_obj *obj;
252
253 obj = _objd_get(i);
254 if (!obj)
255 continue;
256 if (!obj->u.s.owner)
257 continue; /* skip root handles */
258 if (!obj->u.s.owner_ref)
259 continue; /* only unref owner ref. */
260 if (obj->u.s.owner == owner)
261 (void) lttng_ust_objd_unref(i, 1);
262 }
263 }
264
265 /*
266 * This is LTTng's own personal way to create an ABI for sessiond.
267 * We send commands over a socket.
268 */
269
270 static const struct lttng_ust_objd_ops lttng_ops;
271 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops;
272 static const struct lttng_ust_objd_ops lttng_session_ops;
273 static const struct lttng_ust_objd_ops lttng_channel_ops;
274 static const struct lttng_ust_objd_ops lttng_event_enabler_ops;
275 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops;
276 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
277 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
278
279 int lttng_abi_create_root_handle(void)
280 {
281 int root_handle;
282
283 /* root handles have NULL owners */
284 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
285 return root_handle;
286 }
287
288 static
289 int lttng_is_channel_ready(struct lttng_channel *lttng_chan)
290 {
291 struct channel *chan;
292 unsigned int nr_streams, exp_streams;
293
294 chan = lttng_chan->chan;
295 nr_streams = channel_handle_get_nr_streams(lttng_chan->handle);
296 exp_streams = chan->nr_streams;
297 return nr_streams == exp_streams;
298 }
299
300 static
301 int lttng_abi_create_session(void *owner)
302 {
303 struct lttng_session *session;
304 int session_objd, ret;
305
306 session = lttng_session_create();
307 if (!session)
308 return -ENOMEM;
309 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
310 if (session_objd < 0) {
311 ret = session_objd;
312 goto objd_error;
313 }
314 session->objd = session_objd;
315 session->owner = owner;
316 return session_objd;
317
318 objd_error:
319 lttng_session_destroy(session);
320 return ret;
321 }
322
323 static
324 long lttng_abi_tracer_version(int objd,
325 struct lttng_ust_tracer_version *v)
326 {
327 v->major = LTTNG_UST_MAJOR_VERSION;
328 v->minor = LTTNG_UST_MINOR_VERSION;
329 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
330 return 0;
331 }
332
333 static
334 int lttng_abi_event_notifier_send_fd(void *owner, int *event_notifier_notif_fd)
335 {
336 struct lttng_event_notifier_group *event_notifier_group;
337 int event_notifier_group_objd, ret, fd_flag;
338
339 event_notifier_group = lttng_event_notifier_group_create();
340 if (!event_notifier_group)
341 return -ENOMEM;
342
343 /*
344 * Set this file descriptor as NON-BLOCKING.
345 */
346 fd_flag = fcntl(*event_notifier_notif_fd, F_GETFL);
347
348 fd_flag |= O_NONBLOCK;
349
350 ret = fcntl(*event_notifier_notif_fd, F_SETFL, fd_flag);
351 if (ret) {
352 ret = -errno;
353 goto fd_error;
354 }
355
356 event_notifier_group_objd = objd_alloc(event_notifier_group,
357 &lttng_event_notifier_group_ops, owner, "event_notifier_group");
358 if (event_notifier_group_objd < 0) {
359 ret = event_notifier_group_objd;
360 goto objd_error;
361 }
362
363 event_notifier_group->objd = event_notifier_group_objd;
364 event_notifier_group->owner = owner;
365 event_notifier_group->notification_fd = *event_notifier_notif_fd;
366 /* Object descriptor takes ownership of notification fd. */
367 *event_notifier_notif_fd = -1;
368
369 return event_notifier_group_objd;
370
371 objd_error:
372 lttng_event_notifier_group_destroy(event_notifier_group);
373 fd_error:
374 return ret;
375 }
376
377 static
378 long lttng_abi_add_context(int objd,
379 struct lttng_ust_context *context_param,
380 union ust_args *uargs,
381 struct lttng_ctx **ctx, struct lttng_session *session)
382 {
383 return lttng_attach_context(context_param, uargs, ctx, session);
384 }
385
386 /**
387 * lttng_cmd - lttng control through socket commands
388 *
389 * @objd: the object descriptor
390 * @cmd: the command
391 * @arg: command arg
392 * @uargs: UST arguments (internal)
393 * @owner: objd owner
394 *
395 * This descriptor implements lttng commands:
396 * LTTNG_UST_SESSION
397 * Returns a LTTng trace session object descriptor
398 * LTTNG_UST_TRACER_VERSION
399 * Returns the LTTng kernel tracer version
400 * LTTNG_UST_TRACEPOINT_LIST
401 * Returns a file descriptor listing available tracepoints
402 * LTTNG_UST_TRACEPOINT_FIELD_LIST
403 * Returns a file descriptor listing available tracepoint fields
404 * LTTNG_UST_WAIT_QUIESCENT
405 * Returns after all previously running probes have completed
406 *
407 * The returned session will be deleted when its file descriptor is closed.
408 */
409 static
410 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
411 union ust_args *uargs, void *owner)
412 {
413 switch (cmd) {
414 case LTTNG_UST_SESSION:
415 return lttng_abi_create_session(owner);
416 case LTTNG_UST_TRACER_VERSION:
417 return lttng_abi_tracer_version(objd,
418 (struct lttng_ust_tracer_version *) arg);
419 case LTTNG_UST_TRACEPOINT_LIST:
420 return lttng_abi_tracepoint_list(owner);
421 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
422 return lttng_abi_tracepoint_field_list(owner);
423 case LTTNG_UST_WAIT_QUIESCENT:
424 lttng_ust_synchronize_trace();
425 return 0;
426 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE:
427 return lttng_abi_event_notifier_send_fd(owner,
428 &uargs->event_notifier_handle.event_notifier_notif_fd);
429 default:
430 return -EINVAL;
431 }
432 }
433
434 static const struct lttng_ust_objd_ops lttng_ops = {
435 .cmd = lttng_cmd,
436 };
437
438 static
439 int lttng_abi_map_channel(int session_objd,
440 struct lttng_ust_channel *ust_chan,
441 union ust_args *uargs,
442 void *owner)
443 {
444 struct lttng_session *session = objd_private(session_objd);
445 const char *transport_name;
446 const struct lttng_transport *transport;
447 const char *chan_name;
448 int chan_objd;
449 struct lttng_ust_shm_handle *channel_handle;
450 struct lttng_channel *lttng_chan;
451 struct channel *chan;
452 struct lttng_ust_lib_ring_buffer_config *config;
453 void *chan_data;
454 int wakeup_fd;
455 uint64_t len;
456 int ret;
457 enum lttng_ust_chan_type type;
458
459 chan_data = uargs->channel.chan_data;
460 wakeup_fd = uargs->channel.wakeup_fd;
461 len = ust_chan->len;
462 type = ust_chan->type;
463
464 switch (type) {
465 case LTTNG_UST_CHAN_PER_CPU:
466 break;
467 default:
468 ret = -EINVAL;
469 goto invalid;
470 }
471
472 if (session->been_active) {
473 ret = -EBUSY;
474 goto active; /* Refuse to add channel to active session */
475 }
476
477 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
478 if (!channel_handle) {
479 ret = -EINVAL;
480 goto handle_error;
481 }
482
483 /* Ownership of chan_data and wakeup_fd taken by channel handle. */
484 uargs->channel.chan_data = NULL;
485 uargs->channel.wakeup_fd = -1;
486
487 chan = shmp(channel_handle, channel_handle->chan);
488 assert(chan);
489 chan->handle = channel_handle;
490 config = &chan->backend.config;
491 lttng_chan = channel_get_private(chan);
492 if (!lttng_chan) {
493 ret = -EINVAL;
494 goto alloc_error;
495 }
496
497 /* Lookup transport name */
498 switch (type) {
499 case LTTNG_UST_CHAN_PER_CPU:
500 if (config->output == RING_BUFFER_MMAP) {
501 if (config->mode == RING_BUFFER_OVERWRITE) {
502 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
503 transport_name = "relay-overwrite-mmap";
504 } else {
505 transport_name = "relay-overwrite-rt-mmap";
506 }
507 } else {
508 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
509 transport_name = "relay-discard-mmap";
510 } else {
511 transport_name = "relay-discard-rt-mmap";
512 }
513 }
514 } else {
515 ret = -EINVAL;
516 goto notransport;
517 }
518 chan_name = "channel";
519 break;
520 default:
521 ret = -EINVAL;
522 goto notransport;
523 }
524 transport = lttng_transport_find(transport_name);
525 if (!transport) {
526 DBG("LTTng transport %s not found\n",
527 transport_name);
528 ret = -EINVAL;
529 goto notransport;
530 }
531
532 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
533 if (chan_objd < 0) {
534 ret = chan_objd;
535 goto objd_error;
536 }
537
538 /* Initialize our lttng chan */
539 lttng_chan->chan = chan;
540 lttng_chan->tstate = 1;
541 lttng_chan->enabled = 1;
542 lttng_chan->ctx = NULL;
543 lttng_chan->session = session;
544 lttng_chan->ops = &transport->ops;
545 memcpy(&lttng_chan->chan->backend.config,
546 transport->client_config,
547 sizeof(lttng_chan->chan->backend.config));
548 cds_list_add(&lttng_chan->node, &session->chan_head);
549 lttng_chan->header_type = 0;
550 lttng_chan->handle = channel_handle;
551 lttng_chan->type = type;
552
553 /*
554 * We tolerate no failure path after channel creation. It will stay
555 * invariant for the rest of the session.
556 */
557 objd_set_private(chan_objd, lttng_chan);
558 lttng_chan->objd = chan_objd;
559 /* The channel created holds a reference on the session */
560 objd_ref(session_objd);
561 return chan_objd;
562
563 /* error path after channel was created */
564 objd_error:
565 notransport:
566 alloc_error:
567 channel_destroy(chan, channel_handle, 0);
568 return ret;
569
570 handle_error:
571 active:
572 invalid:
573 return ret;
574 }
575
576 /**
577 * lttng_session_cmd - lttng session object command
578 *
579 * @obj: the object
580 * @cmd: the command
581 * @arg: command arg
582 * @uargs: UST arguments (internal)
583 * @owner: objd owner
584 *
585 * This descriptor implements lttng commands:
586 * LTTNG_UST_CHANNEL
587 * Returns a LTTng channel object descriptor
588 * LTTNG_UST_ENABLE
589 * Enables tracing for a session (weak enable)
590 * LTTNG_UST_DISABLE
591 * Disables tracing for a session (strong disable)
592 *
593 * The returned channel will be deleted when its file descriptor is closed.
594 */
595 static
596 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
597 union ust_args *uargs, void *owner)
598 {
599 struct lttng_session *session = objd_private(objd);
600
601 switch (cmd) {
602 case LTTNG_UST_CHANNEL:
603 return lttng_abi_map_channel(objd,
604 (struct lttng_ust_channel *) arg,
605 uargs, owner);
606 case LTTNG_UST_SESSION_START:
607 case LTTNG_UST_ENABLE:
608 return lttng_session_enable(session);
609 case LTTNG_UST_SESSION_STOP:
610 case LTTNG_UST_DISABLE:
611 return lttng_session_disable(session);
612 case LTTNG_UST_SESSION_STATEDUMP:
613 return lttng_session_statedump(session);
614 case LTTNG_UST_COUNTER:
615 case LTTNG_UST_COUNTER_GLOBAL:
616 case LTTNG_UST_COUNTER_CPU:
617 /* Not implemented yet. */
618 return -EINVAL;
619 default:
620 return -EINVAL;
621 }
622 }
623
624 /*
625 * Called when the last file reference is dropped.
626 *
627 * Big fat note: channels and events are invariant for the whole session after
628 * their creation. So this session destruction also destroys all channel and
629 * event structures specific to this session (they are not destroyed when their
630 * individual file is released).
631 */
632 static
633 int lttng_release_session(int objd)
634 {
635 struct lttng_session *session = objd_private(objd);
636
637 if (session) {
638 lttng_session_destroy(session);
639 return 0;
640 } else {
641 return -EINVAL;
642 }
643 }
644
645 static const struct lttng_ust_objd_ops lttng_session_ops = {
646 .release = lttng_release_session,
647 .cmd = lttng_session_cmd,
648 };
649
650 static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj,
651 void *owner, struct lttng_ust_event_notifier *event_notifier_param,
652 enum lttng_enabler_format_type type)
653 {
654 struct lttng_event_notifier_group *event_notifier_group =
655 objd_private(event_notifier_group_obj);
656 struct lttng_event_notifier_enabler *event_notifier_enabler;
657 int event_notifier_objd, ret;
658
659 event_notifier_param->event.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
660 event_notifier_objd = objd_alloc(NULL, &lttng_event_notifier_enabler_ops, owner,
661 "event_notifier enabler");
662 if (event_notifier_objd < 0) {
663 ret = event_notifier_objd;
664 goto objd_error;
665 }
666
667 event_notifier_enabler = lttng_event_notifier_enabler_create(
668 event_notifier_group, type, event_notifier_param);
669 if (!event_notifier_enabler) {
670 ret = -ENOMEM;
671 goto event_notifier_error;
672 }
673
674 objd_set_private(event_notifier_objd, event_notifier_enabler);
675 /* The event_notifier holds a reference on the event_notifier group. */
676 objd_ref(event_notifier_enabler->group->objd);
677
678 return event_notifier_objd;
679
680 event_notifier_error:
681 {
682 int err;
683
684 err = lttng_ust_objd_unref(event_notifier_objd, 1);
685 assert(!err);
686 }
687 objd_error:
688 return ret;
689 }
690
691 static
692 long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
693 union ust_args *uargs, void *owner)
694 {
695 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
696 switch (cmd) {
697 case LTTNG_UST_FILTER:
698 return lttng_event_notifier_enabler_attach_filter_bytecode(
699 event_notifier_enabler,
700 (struct lttng_ust_bytecode_node **) arg);
701 case LTTNG_UST_EXCLUSION:
702 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
703 (struct lttng_ust_excluder_node **) arg);
704 case LTTNG_UST_CAPTURE:
705 return lttng_event_notifier_enabler_attach_capture_bytecode(
706 event_notifier_enabler,
707 (struct lttng_ust_bytecode_node **) arg);
708 case LTTNG_UST_ENABLE:
709 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
710 case LTTNG_UST_DISABLE:
711 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
712 default:
713 return -EINVAL;
714 }
715 }
716
717 /**
718 * lttng_event_notifier_group_error_counter_cmd - lttng event_notifier group error counter object command
719 *
720 * @obj: the object
721 * @cmd: the command
722 * @arg: command arg
723 * @uargs: UST arguments (internal)
724 * @owner: objd owner
725 *
726 * This descriptor implements lttng commands:
727 * LTTNG_UST_COUNTER_GLOBAL
728 * Return negative error code on error, 0 on success.
729 * LTTNG_UST_COUNTER_CPU
730 * Return negative error code on error, 0 on success.
731 */
732 static
733 long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
734 union ust_args *uargs, void *owner)
735 {
736 struct lttng_counter *counter = objd_private(objd);
737
738 switch (cmd) {
739 case LTTNG_UST_COUNTER_GLOBAL:
740 return -EINVAL; /* Unimplemented. */
741 case LTTNG_UST_COUNTER_CPU:
742 {
743 struct lttng_ust_counter_cpu *counter_cpu =
744 (struct lttng_ust_counter_cpu *)arg;
745 return lttng_counter_set_cpu_shm(counter->counter,
746 counter_cpu->cpu_nr, uargs->counter_shm.shm_fd);
747 }
748 default:
749 return -EINVAL;
750 }
751 }
752
753 LTTNG_HIDDEN
754 int lttng_release_event_notifier_group_error_counter(int objd)
755 {
756 struct lttng_counter *counter = objd_private(objd);
757
758 if (counter) {
759 return lttng_ust_objd_unref(counter->event_notifier_group->objd, 0);
760 } else {
761 return -EINVAL;
762 }
763 }
764
765 static const struct lttng_ust_objd_ops lttng_event_notifier_group_error_counter_ops = {
766 .release = lttng_release_event_notifier_group_error_counter,
767 .cmd = lttng_event_notifier_group_error_counter_cmd,
768 };
769
770 static
771 int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
772 struct lttng_ust_counter_conf *error_counter_conf)
773 {
774 const char *counter_transport_name;
775 struct lttng_event_notifier_group *event_notifier_group =
776 objd_private(event_notifier_group_objd);
777 struct lttng_counter *counter;
778 int counter_objd, ret;
779 struct lttng_counter_dimension dimensions[1];
780 size_t counter_len;
781
782 if (event_notifier_group->error_counter)
783 return -EBUSY;
784
785 if (error_counter_conf->arithmetic != LTTNG_UST_COUNTER_ARITHMETIC_MODULAR)
786 return -EINVAL;
787
788 if (error_counter_conf->number_dimensions != 1)
789 return -EINVAL;
790
791 switch (error_counter_conf->bitness) {
792 case LTTNG_UST_COUNTER_BITNESS_64:
793 counter_transport_name = "counter-per-cpu-64-modular";
794 break;
795 case LTTNG_UST_COUNTER_BITNESS_32:
796 counter_transport_name = "counter-per-cpu-32-modular";
797 break;
798 default:
799 return -EINVAL;
800 }
801
802 counter_objd = objd_alloc(NULL, &lttng_event_notifier_group_error_counter_ops, owner,
803 "event_notifier group error counter");
804 if (counter_objd < 0) {
805 ret = counter_objd;
806 goto objd_error;
807 }
808
809 counter_len = error_counter_conf->dimensions[0].size;
810 dimensions[0].size = counter_len;
811 dimensions[0].underflow_index = 0;
812 dimensions[0].overflow_index = 0;
813 dimensions[0].has_underflow = 0;
814 dimensions[0].has_overflow = 0;
815
816 counter = lttng_ust_counter_create(counter_transport_name, 1, dimensions);
817 if (!counter) {
818 ret = -EINVAL;
819 goto create_error;
820 }
821
822 event_notifier_group->error_counter_len = counter_len;
823 /*
824 * store-release to publish error counter matches load-acquire
825 * in record_error. Ensures the counter is created and the
826 * error_counter_len is set before they are used.
827 * Currently a full memory barrier is used, which could be
828 * turned into acquire-release barriers.
829 */
830 cmm_smp_mb();
831 CMM_STORE_SHARED(event_notifier_group->error_counter, counter);
832
833 counter->objd = counter_objd;
834 counter->event_notifier_group = event_notifier_group; /* owner */
835
836 objd_set_private(counter_objd, counter);
837 /* The error counter holds a reference on the event_notifier group. */
838 objd_ref(event_notifier_group->objd);
839
840 return counter_objd;
841
842 create_error:
843 {
844 int err;
845
846 err = lttng_ust_objd_unref(counter_objd, 1);
847 assert(!err);
848 }
849 objd_error:
850 return ret;
851 }
852
853 static
854 long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
855 union ust_args *uargs, void *owner)
856 {
857 switch (cmd) {
858 case LTTNG_UST_EVENT_NOTIFIER_CREATE:
859 {
860 struct lttng_ust_event_notifier *event_notifier_param =
861 (struct lttng_ust_event_notifier *) arg;
862 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
863 /*
864 * If the event name is a star globbing pattern,
865 * we create the special star globbing enabler.
866 */
867 return lttng_ust_event_notifier_enabler_create(objd,
868 owner, event_notifier_param,
869 LTTNG_ENABLER_FORMAT_STAR_GLOB);
870 } else {
871 return lttng_ust_event_notifier_enabler_create(objd,
872 owner, event_notifier_param,
873 LTTNG_ENABLER_FORMAT_EVENT);
874 }
875 }
876 case LTTNG_UST_COUNTER:
877 {
878 struct lttng_ust_counter_conf *counter_conf =
879 (struct lttng_ust_counter_conf *) uargs->counter.counter_data;
880 return lttng_ust_event_notifier_group_create_error_counter(
881 objd, owner, counter_conf);
882 }
883 default:
884 return -EINVAL;
885 }
886 }
887
888 static
889 int lttng_event_notifier_enabler_release(int objd)
890 {
891 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
892
893 if (event_notifier_enabler)
894 return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
895 return 0;
896 }
897
898 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
899 .release = lttng_event_notifier_enabler_release,
900 .cmd = lttng_event_notifier_enabler_cmd,
901 };
902
903 static
904 int lttng_release_event_notifier_group(int objd)
905 {
906 struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
907
908 if (event_notifier_group) {
909 lttng_event_notifier_group_destroy(event_notifier_group);
910 return 0;
911 } else {
912 return -EINVAL;
913 }
914 }
915
916 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
917 .release = lttng_release_event_notifier_group,
918 .cmd = lttng_event_notifier_group_cmd,
919 };
920
921 static
922 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
923 union ust_args *uargs, void *owner)
924 {
925 struct lttng_ust_tracepoint_list *list = objd_private(objd);
926 struct lttng_ust_tracepoint_iter *tp =
927 (struct lttng_ust_tracepoint_iter *) arg;
928 struct lttng_ust_tracepoint_iter *iter;
929
930 switch (cmd) {
931 case LTTNG_UST_TRACEPOINT_LIST_GET:
932 {
933 iter = lttng_ust_tracepoint_list_get_iter_next(list);
934 if (!iter)
935 return -LTTNG_UST_ERR_NOENT;
936 memcpy(tp, iter, sizeof(*tp));
937 return 0;
938 }
939 default:
940 return -EINVAL;
941 }
942 }
943
944 static
945 int lttng_abi_tracepoint_list(void *owner)
946 {
947 int list_objd, ret;
948 struct lttng_ust_tracepoint_list *list;
949
950 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
951 if (list_objd < 0) {
952 ret = list_objd;
953 goto objd_error;
954 }
955 list = zmalloc(sizeof(*list));
956 if (!list) {
957 ret = -ENOMEM;
958 goto alloc_error;
959 }
960 objd_set_private(list_objd, list);
961
962 /* populate list by walking on all registered probes. */
963 ret = lttng_probes_get_event_list(list);
964 if (ret) {
965 goto list_error;
966 }
967 return list_objd;
968
969 list_error:
970 free(list);
971 alloc_error:
972 {
973 int err;
974
975 err = lttng_ust_objd_unref(list_objd, 1);
976 assert(!err);
977 }
978 objd_error:
979 return ret;
980 }
981
982 static
983 int lttng_release_tracepoint_list(int objd)
984 {
985 struct lttng_ust_tracepoint_list *list = objd_private(objd);
986
987 if (list) {
988 lttng_probes_prune_event_list(list);
989 free(list);
990 return 0;
991 } else {
992 return -EINVAL;
993 }
994 }
995
996 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
997 .release = lttng_release_tracepoint_list,
998 .cmd = lttng_tracepoint_list_cmd,
999 };
1000
1001 static
1002 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
1003 unsigned long arg, union ust_args *uargs, void *owner)
1004 {
1005 struct lttng_ust_field_list *list = objd_private(objd);
1006 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
1007 struct lttng_ust_field_iter *iter;
1008
1009 switch (cmd) {
1010 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1011 {
1012 iter = lttng_ust_field_list_get_iter_next(list);
1013 if (!iter)
1014 return -LTTNG_UST_ERR_NOENT;
1015 memcpy(tp, iter, sizeof(*tp));
1016 return 0;
1017 }
1018 default:
1019 return -EINVAL;
1020 }
1021 }
1022
1023 static
1024 int lttng_abi_tracepoint_field_list(void *owner)
1025 {
1026 int list_objd, ret;
1027 struct lttng_ust_field_list *list;
1028
1029 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
1030 "tp_field_list");
1031 if (list_objd < 0) {
1032 ret = list_objd;
1033 goto objd_error;
1034 }
1035 list = zmalloc(sizeof(*list));
1036 if (!list) {
1037 ret = -ENOMEM;
1038 goto alloc_error;
1039 }
1040 objd_set_private(list_objd, list);
1041
1042 /* populate list by walking on all registered probes. */
1043 ret = lttng_probes_get_field_list(list);
1044 if (ret) {
1045 goto list_error;
1046 }
1047 return list_objd;
1048
1049 list_error:
1050 free(list);
1051 alloc_error:
1052 {
1053 int err;
1054
1055 err = lttng_ust_objd_unref(list_objd, 1);
1056 assert(!err);
1057 }
1058 objd_error:
1059 return ret;
1060 }
1061
1062 static
1063 int lttng_release_tracepoint_field_list(int objd)
1064 {
1065 struct lttng_ust_field_list *list = objd_private(objd);
1066
1067 if (list) {
1068 lttng_probes_prune_field_list(list);
1069 free(list);
1070 return 0;
1071 } else {
1072 return -EINVAL;
1073 }
1074 }
1075
1076 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
1077 .release = lttng_release_tracepoint_field_list,
1078 .cmd = lttng_tracepoint_field_list_cmd,
1079 };
1080
1081 static
1082 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
1083 union ust_args *uargs, void *owner)
1084 {
1085 struct lttng_channel *channel = objd_private(channel_objd);
1086 int ret;
1087
1088 ret = channel_handle_add_stream(channel->handle,
1089 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
1090 info->stream_nr, info->len);
1091 if (ret)
1092 goto error_add_stream;
1093 /* Take ownership of shm_fd and wakeup_fd. */
1094 uargs->stream.shm_fd = -1;
1095 uargs->stream.wakeup_fd = -1;
1096
1097 return 0;
1098
1099 error_add_stream:
1100 return ret;
1101 }
1102
1103 static
1104 int lttng_abi_create_event_enabler(int channel_objd,
1105 struct lttng_ust_event *event_param,
1106 void *owner,
1107 enum lttng_enabler_format_type format_type)
1108 {
1109 struct lttng_channel *channel = objd_private(channel_objd);
1110 struct lttng_event_enabler *enabler;
1111 int event_objd, ret;
1112
1113 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1114 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
1115 "event enabler");
1116 if (event_objd < 0) {
1117 ret = event_objd;
1118 goto objd_error;
1119 }
1120 /*
1121 * We tolerate no failure path after event creation. It will stay
1122 * invariant for the rest of the session.
1123 */
1124 enabler = lttng_event_enabler_create(format_type, event_param, channel);
1125 if (!enabler) {
1126 ret = -ENOMEM;
1127 goto event_error;
1128 }
1129 objd_set_private(event_objd, enabler);
1130 /* The event holds a reference on the channel */
1131 objd_ref(channel_objd);
1132 return event_objd;
1133
1134 event_error:
1135 {
1136 int err;
1137
1138 err = lttng_ust_objd_unref(event_objd, 1);
1139 assert(!err);
1140 }
1141 objd_error:
1142 return ret;
1143 }
1144
1145 /**
1146 * lttng_channel_cmd - lttng control through object descriptors
1147 *
1148 * @objd: the object descriptor
1149 * @cmd: the command
1150 * @arg: command arg
1151 * @uargs: UST arguments (internal)
1152 * @owner: objd owner
1153 *
1154 * This object descriptor implements lttng commands:
1155 * LTTNG_UST_STREAM
1156 * Returns an event stream object descriptor or failure.
1157 * (typically, one event stream records events from one CPU)
1158 * LTTNG_UST_EVENT
1159 * Returns an event object descriptor or failure.
1160 * LTTNG_UST_CONTEXT
1161 * Prepend a context field to each event in the channel
1162 * LTTNG_UST_ENABLE
1163 * Enable recording for events in this channel (weak enable)
1164 * LTTNG_UST_DISABLE
1165 * Disable recording for events in this channel (strong disable)
1166 *
1167 * Channel and event file descriptors also hold a reference on the session.
1168 */
1169 static
1170 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
1171 union ust_args *uargs, void *owner)
1172 {
1173 struct lttng_channel *channel = objd_private(objd);
1174
1175 if (cmd != LTTNG_UST_STREAM) {
1176 /*
1177 * Check if channel received all streams.
1178 */
1179 if (!lttng_is_channel_ready(channel))
1180 return -EPERM;
1181 }
1182
1183 switch (cmd) {
1184 case LTTNG_UST_STREAM:
1185 {
1186 struct lttng_ust_stream *stream;
1187
1188 stream = (struct lttng_ust_stream *) arg;
1189 /* stream used as output */
1190 return lttng_abi_map_stream(objd, stream, uargs, owner);
1191 }
1192 case LTTNG_UST_EVENT:
1193 {
1194 struct lttng_ust_event *event_param =
1195 (struct lttng_ust_event *) arg;
1196
1197 if (strutils_is_star_glob_pattern(event_param->name)) {
1198 /*
1199 * If the event name is a star globbing pattern,
1200 * we create the special star globbing enabler.
1201 */
1202 return lttng_abi_create_event_enabler(objd, event_param,
1203 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1204 } else {
1205 return lttng_abi_create_event_enabler(objd, event_param,
1206 owner, LTTNG_ENABLER_FORMAT_EVENT);
1207 }
1208 }
1209 case LTTNG_UST_CONTEXT:
1210 return lttng_abi_add_context(objd,
1211 (struct lttng_ust_context *) arg, uargs,
1212 &channel->ctx, channel->session);
1213 case LTTNG_UST_ENABLE:
1214 return lttng_channel_enable(channel);
1215 case LTTNG_UST_DISABLE:
1216 return lttng_channel_disable(channel);
1217 case LTTNG_UST_FLUSH_BUFFER:
1218 return channel->ops->flush_buffer(channel->chan, channel->handle);
1219 default:
1220 return -EINVAL;
1221 }
1222 }
1223
1224 static
1225 int lttng_channel_release(int objd)
1226 {
1227 struct lttng_channel *channel = objd_private(objd);
1228
1229 if (channel)
1230 return lttng_ust_objd_unref(channel->session->objd, 0);
1231 return 0;
1232 }
1233
1234 static const struct lttng_ust_objd_ops lttng_channel_ops = {
1235 .release = lttng_channel_release,
1236 .cmd = lttng_channel_cmd,
1237 };
1238
1239 /**
1240 * lttng_enabler_cmd - lttng control through object descriptors
1241 *
1242 * @objd: the object descriptor
1243 * @cmd: the command
1244 * @arg: command arg
1245 * @uargs: UST arguments (internal)
1246 * @owner: objd owner
1247 *
1248 * This object descriptor implements lttng commands:
1249 * LTTNG_UST_CONTEXT
1250 * Prepend a context field to each record of events of this
1251 * enabler.
1252 * LTTNG_UST_ENABLE
1253 * Enable recording for this enabler
1254 * LTTNG_UST_DISABLE
1255 * Disable recording for this enabler
1256 * LTTNG_UST_FILTER
1257 * Attach a filter to an enabler.
1258 * LTTNG_UST_EXCLUSION
1259 * Attach exclusions to an enabler.
1260 */
1261 static
1262 long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1263 union ust_args *uargs, void *owner)
1264 {
1265 struct lttng_event_enabler *enabler = objd_private(objd);
1266
1267 switch (cmd) {
1268 case LTTNG_UST_CONTEXT:
1269 return lttng_event_enabler_attach_context(enabler,
1270 (struct lttng_ust_context *) arg);
1271 case LTTNG_UST_ENABLE:
1272 return lttng_event_enabler_enable(enabler);
1273 case LTTNG_UST_DISABLE:
1274 return lttng_event_enabler_disable(enabler);
1275 case LTTNG_UST_FILTER:
1276 {
1277 int ret;
1278
1279 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
1280 (struct lttng_ust_bytecode_node **) arg);
1281 if (ret)
1282 return ret;
1283 return 0;
1284 }
1285 case LTTNG_UST_EXCLUSION:
1286 {
1287 return lttng_event_enabler_attach_exclusion(enabler,
1288 (struct lttng_ust_excluder_node **) arg);
1289 }
1290 default:
1291 return -EINVAL;
1292 }
1293 }
1294
1295 static
1296 int lttng_event_enabler_release(int objd)
1297 {
1298 struct lttng_event_enabler *event_enabler = objd_private(objd);
1299
1300 if (event_enabler)
1301 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
1302
1303 return 0;
1304 }
1305
1306 static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1307 .release = lttng_event_enabler_release,
1308 .cmd = lttng_event_enabler_cmd,
1309 };
1310
1311 void lttng_ust_abi_exit(void)
1312 {
1313 lttng_ust_abi_close_in_progress = 1;
1314 ust_lock_nocheck();
1315 objd_table_destroy();
1316 ust_unlock();
1317 lttng_ust_abi_close_in_progress = 0;
1318 }
This page took 0.08782 seconds and 4 git commands to generate.