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