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