cleanup: convenience libs at root of 'src/common/'
[lttng-ust.git] / src / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
f4681817 3 *
e92f3e28
MD
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng UST ABI
e92f3e28 7 *
f4681817
MD
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.
f4681817
MD
23 */
24
3fbec7dc 25#define _LGPL_SOURCE
d8d2416d 26#include <fcntl.h>
fb31eb73 27#include <stdint.h>
4e79769f 28#include <unistd.h>
fb31eb73 29
f4681817
MD
30#include <urcu/compiler.h>
31#include <urcu/list.h>
fb31eb73 32
fb31eb73
FD
33#include <lttng/tracepoint.h>
34#include <lttng/ust-abi.h>
35#include <lttng/ust-error.h>
4318ae1b 36#include <lttng/ust-events.h>
23c8854a 37#include <lttng/ust-version.h>
9d315d6d
MJ
38
39#include "common/ust-fd.h"
40#include "common/logging.h"
fb31eb73 41
e4db8f98
MJ
42#include "common/ringbuffer/frontend_types.h"
43#include "common/ringbuffer/frontend.h"
44#include "common/ringbuffer/shm.h"
cdff92e0 45#include "common/counter/counter.h"
10544ee8 46#include "tracepoint-internal.h"
7dd08bec 47#include "lttng-tracer.h"
196ec2df 48#include "string-utils.h"
d871c65b 49#include "ust-events-internal.h"
fc80554e 50#include "context-internal.h"
9d315d6d 51#include "common/macros.h"
4ab44fbe 52
7047f3da
MD
53#define OBJ_NAME_LEN 16
54
45e9e699
MD
55static int lttng_ust_abi_close_in_progress;
56
51489cad 57static
f59ed768 58int lttng_abi_tracepoint_list(void *owner);
06d4f27e 59static
f59ed768 60int lttng_abi_tracepoint_field_list(void *owner);
51489cad 61
f4681817
MD
62/*
63 * Object descriptor table. Should be protected from concurrent access
64 * by the caller.
65 */
66
fd17d7ce 67struct lttng_ust_abi_obj {
f4681817
MD
68 union {
69 struct {
70 void *private_data;
fd17d7ce 71 const struct lttng_ust_abi_objd_ops *ops;
f4681817 72 int f_count;
1849ef7c 73 int owner_ref; /* has ref from owner */
f59ed768 74 void *owner;
7047f3da 75 char name[OBJ_NAME_LEN];
f4681817
MD
76 } s;
77 int freelist_next; /* offset freelist. end is -1. */
78 } u;
79};
80
fd17d7ce
MD
81struct lttng_ust_abi_objd_table {
82 struct lttng_ust_abi_obj *array;
f4681817
MD
83 unsigned int len, allocated_len;
84 int freelist_head; /* offset freelist head. end is -1 */
85};
86
fd17d7ce 87static struct lttng_ust_abi_objd_table objd_table = {
f4681817
MD
88 .freelist_head = -1,
89};
90
91static
fd17d7ce 92int objd_alloc(void *private_data, const struct lttng_ust_abi_objd_ops *ops,
7047f3da 93 void *owner, const char *name)
f4681817 94{
fd17d7ce 95 struct lttng_ust_abi_obj *obj;
f4681817
MD
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;
fd17d7ce 105 struct lttng_ust_abi_obj *new_table, *old_table;
f4681817
MD
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;
fd17d7ce 113 new_table = zmalloc(sizeof(struct lttng_ust_abi_obj) * new_allocated_len);
f4681817
MD
114 if (!new_table)
115 return -ENOMEM;
116 memcpy(new_table, old_table,
fd17d7ce 117 sizeof(struct lttng_ust_abi_obj) * old_allocated_len);
f4681817
MD
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++;
124end:
125 obj->u.s.private_data = private_data;
126 obj->u.s.ops = ops;
a4be8962
MD
127 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
128 /* count == 2 : allocated + hold ref */
1849ef7c 129 obj->u.s.owner_ref = 1; /* One owner reference */
f59ed768 130 obj->u.s.owner = owner;
7047f3da
MD
131 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
132 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
f4681817
MD
133 return obj - objd_table.array;
134}
135
136static
fd17d7ce 137struct lttng_ust_abi_obj *_objd_get(int id)
f4681817
MD
138{
139 if (id >= objd_table.len)
140 return NULL;
a4be8962
MD
141 if (!objd_table.array[id].u.s.f_count)
142 return NULL;
f4681817
MD
143 return &objd_table.array[id];
144}
145
146static
147void *objd_private(int id)
148{
fd17d7ce 149 struct lttng_ust_abi_obj *obj = _objd_get(id);
f4681817
MD
150 assert(obj);
151 return obj->u.s.private_data;
152}
153
154static
155void objd_set_private(int id, void *private_data)
156{
fd17d7ce 157 struct lttng_ust_abi_obj *obj = _objd_get(id);
f4681817
MD
158 assert(obj);
159 obj->u.s.private_data = private_data;
160}
161
fd17d7ce 162const struct lttng_ust_abi_objd_ops *lttng_ust_abi_objd_ops(int id)
f4681817 163{
fd17d7ce 164 struct lttng_ust_abi_obj *obj = _objd_get(id);
46050b1a 165
a4be8962
MD
166 if (!obj)
167 return NULL;
f4681817
MD
168 return obj->u.s.ops;
169}
170
171static
172void objd_free(int id)
173{
fd17d7ce 174 struct lttng_ust_abi_obj *obj = _objd_get(id);
f4681817
MD
175
176 assert(obj);
177 obj->u.freelist_next = objd_table.freelist_head;
178 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
179 assert(obj->u.s.f_count == 1);
180 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
181}
182
183static
184void objd_ref(int id)
185{
fd17d7ce 186 struct lttng_ust_abi_obj *obj = _objd_get(id);
072886c4 187 assert(obj != NULL);
f4681817
MD
188 obj->u.s.f_count++;
189}
190
fd17d7ce 191int lttng_ust_abi_objd_unref(int id, int is_owner)
f4681817 192{
fd17d7ce 193 struct lttng_ust_abi_obj *obj = _objd_get(id);
f4681817 194
1ea11eab
MD
195 if (!obj)
196 return -EINVAL;
a4be8962
MD
197 if (obj->u.s.f_count == 1) {
198 ERR("Reference counting error\n");
199 return -EINVAL;
200 }
1849ef7c
MD
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 }
a4be8962 208 if ((--obj->u.s.f_count) == 1) {
fd17d7ce 209 const struct lttng_ust_abi_objd_ops *ops = lttng_ust_abi_objd_ops(id);
196ec2df 210
f4681817
MD
211 if (ops->release)
212 ops->release(id);
213 objd_free(id);
214 }
1ea11eab 215 return 0;
f4681817
MD
216}
217
218static
219void objd_table_destroy(void)
220{
a4be8962
MD
221 int i;
222
1849ef7c 223 for (i = 0; i < objd_table.allocated_len; i++) {
fd17d7ce 224 struct lttng_ust_abi_obj *obj;
1849ef7c
MD
225
226 obj = _objd_get(i);
227 if (!obj)
228 continue;
229 if (!obj->u.s.owner_ref)
230 continue; /* only unref owner ref. */
fd17d7ce 231 (void) lttng_ust_abi_objd_unref(i, 1);
1849ef7c 232 }
f4681817 233 free(objd_table.array);
02fb3381
MD
234 objd_table.array = NULL;
235 objd_table.len = 0;
236 objd_table.allocated_len = 0;
17dfb34b 237 objd_table.freelist_head = -1;
f4681817
MD
238}
239
74d81a6c
MD
240const char *lttng_ust_obj_get_name(int id)
241{
fd17d7ce 242 struct lttng_ust_abi_obj *obj = _objd_get(id);
74d81a6c
MD
243
244 if (!obj)
245 return NULL;
246 return obj->u.s.name;
247}
248
fd17d7ce 249void lttng_ust_abi_objd_table_owner_cleanup(void *owner)
f59ed768
MD
250{
251 int i;
252
253 for (i = 0; i < objd_table.allocated_len; i++) {
fd17d7ce 254 struct lttng_ust_abi_obj *obj;
f59ed768
MD
255
256 obj = _objd_get(i);
257 if (!obj)
258 continue;
259 if (!obj->u.s.owner)
260 continue; /* skip root handles */
1849ef7c
MD
261 if (!obj->u.s.owner_ref)
262 continue; /* only unref owner ref. */
f59ed768 263 if (obj->u.s.owner == owner)
fd17d7ce 264 (void) lttng_ust_abi_objd_unref(i, 1);
f59ed768
MD
265 }
266}
267
f4681817
MD
268/*
269 * This is LTTng's own personal way to create an ABI for sessiond.
270 * We send commands over a socket.
271 */
272
fd17d7ce
MD
273static const struct lttng_ust_abi_objd_ops lttng_ops;
274static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_ops;
275static const struct lttng_ust_abi_objd_ops lttng_session_ops;
276static const struct lttng_ust_abi_objd_ops lttng_channel_ops;
277static const struct lttng_ust_abi_objd_ops lttng_event_enabler_ops;
278static const struct lttng_ust_abi_objd_ops lttng_event_notifier_enabler_ops;
279static const struct lttng_ust_abi_objd_ops lttng_tracepoint_list_ops;
280static const struct lttng_ust_abi_objd_ops lttng_tracepoint_field_list_ops;
f4681817 281
46050b1a
MD
282int lttng_abi_create_root_handle(void)
283{
284 int root_handle;
285
f59ed768 286 /* root handles have NULL owners */
7047f3da 287 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
46050b1a
MD
288 return root_handle;
289}
290
74d81a6c 291static
e7bc0ef6 292int lttng_is_channel_ready(struct lttng_ust_channel_buffer *lttng_chan)
74d81a6c 293{
5198080d 294 struct lttng_ust_lib_ring_buffer_channel *chan;
74d81a6c
MD
295 unsigned int nr_streams, exp_streams;
296
07539b34
MD
297 chan = lttng_chan->priv->rb_chan;
298 nr_streams = channel_handle_get_nr_streams(lttng_chan->priv->rb_chan->handle);
74d81a6c
MD
299 exp_streams = chan->nr_streams;
300 return nr_streams == exp_streams;
301}
302
818173b9 303static
f59ed768 304int lttng_abi_create_session(void *owner)
f4681817 305{
f69fe5fb 306 struct lttng_ust_session *session;
f4681817
MD
307 int session_objd, ret;
308
7dd08bec 309 session = lttng_session_create();
f4681817
MD
310 if (!session)
311 return -ENOMEM;
7047f3da 312 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
f4681817
MD
313 if (session_objd < 0) {
314 ret = session_objd;
315 goto objd_error;
316 }
bdb12629
MD
317 session->priv->objd = session_objd;
318 session->priv->owner = owner;
f4681817
MD
319 return session_objd;
320
321objd_error:
7dd08bec 322 lttng_session_destroy(session);
f4681817
MD
323 return ret;
324}
325
f4681817 326static
2208d8b5 327long lttng_abi_tracer_version(int objd __attribute__((unused)),
fd17d7ce 328 struct lttng_ust_abi_tracer_version *v)
f4681817 329{
32ce8569
MD
330 v->major = LTTNG_UST_MAJOR_VERSION;
331 v->minor = LTTNG_UST_MINOR_VERSION;
332 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
333 return 0;
334}
335
d8d2416d 336static
f52e5902 337int lttng_abi_event_notifier_send_fd(void *owner, int *event_notifier_notif_fd)
d8d2416d
FD
338{
339 struct lttng_event_notifier_group *event_notifier_group;
f52e5902 340 int event_notifier_group_objd, ret, fd_flag;
d8d2416d
FD
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 */
f52e5902 349 fd_flag = fcntl(*event_notifier_notif_fd, F_GETFL);
d8d2416d
FD
350
351 fd_flag |= O_NONBLOCK;
352
f52e5902 353 ret = fcntl(*event_notifier_notif_fd, F_SETFL, fd_flag);
d8d2416d
FD
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;
f52e5902
MD
368 event_notifier_group->notification_fd = *event_notifier_notif_fd;
369 /* Object descriptor takes ownership of notification fd. */
370 *event_notifier_notif_fd = -1;
d8d2416d
FD
371
372 return event_notifier_group_objd;
373
374objd_error:
375 lttng_event_notifier_group_destroy(event_notifier_group);
376fd_error:
d8d2416d
FD
377 return ret;
378}
379
f4681817 380static
2208d8b5 381long lttng_abi_add_context(int objd __attribute__((unused)),
fd17d7ce
MD
382 struct lttng_ust_abi_context *context_param,
383 union lttng_ust_abi_args *uargs,
f69fe5fb 384 struct lttng_ust_ctx **ctx, struct lttng_ust_session *session)
f4681817 385{
8e696cfa 386 return lttng_attach_context(context_param, uargs, ctx, session);
f4681817
MD
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
ef9ff354 395 * @uargs: UST arguments (internal)
f59ed768 396 * @owner: objd owner
f4681817
MD
397 *
398 * This descriptor implements lttng commands:
fd17d7ce 399 * LTTNG_UST_ABI_SESSION
f4681817 400 * Returns a LTTng trace session object descriptor
fd17d7ce 401 * LTTNG_UST_ABI_TRACER_VERSION
f4681817 402 * Returns the LTTng kernel tracer version
fd17d7ce 403 * LTTNG_UST_ABI_TRACEPOINT_LIST
f4681817 404 * Returns a file descriptor listing available tracepoints
fd17d7ce 405 * LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
06d4f27e 406 * Returns a file descriptor listing available tracepoint fields
fd17d7ce 407 * LTTNG_UST_ABI_WAIT_QUIESCENT
f4681817
MD
408 * Returns after all previously running probes have completed
409 *
410 * The returned session will be deleted when its file descriptor is closed.
411 */
412static
ef9ff354 413long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
fd17d7ce 414 union lttng_ust_abi_args *uargs, void *owner)
f4681817
MD
415{
416 switch (cmd) {
fd17d7ce 417 case LTTNG_UST_ABI_SESSION:
f59ed768 418 return lttng_abi_create_session(owner);
fd17d7ce 419 case LTTNG_UST_ABI_TRACER_VERSION:
f4681817 420 return lttng_abi_tracer_version(objd,
fd17d7ce
MD
421 (struct lttng_ust_abi_tracer_version *) arg);
422 case LTTNG_UST_ABI_TRACEPOINT_LIST:
f59ed768 423 return lttng_abi_tracepoint_list(owner);
fd17d7ce 424 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST:
f59ed768 425 return lttng_abi_tracepoint_field_list(owner);
fd17d7ce 426 case LTTNG_UST_ABI_WAIT_QUIESCENT:
b653ddc1 427 lttng_ust_urcu_synchronize_rcu();
f4681817 428 return 0;
fd17d7ce 429 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE:
d8d2416d 430 return lttng_abi_event_notifier_send_fd(owner,
f52e5902 431 &uargs->event_notifier_handle.event_notifier_notif_fd);
f4681817
MD
432 default:
433 return -EINVAL;
434 }
435}
436
fd17d7ce 437static const struct lttng_ust_abi_objd_ops lttng_ops = {
f4681817
MD
438 .cmd = lttng_cmd,
439};
440
04a351cb 441static
74d81a6c 442int lttng_abi_map_channel(int session_objd,
fd17d7ce
MD
443 struct lttng_ust_abi_channel *ust_chan,
444 union lttng_ust_abi_args *uargs,
74d81a6c 445 void *owner)
f4681817 446{
f69fe5fb 447 struct lttng_ust_session *session = objd_private(session_objd);
f4681817 448 const char *transport_name;
a084756d 449 struct lttng_transport *transport;
74d81a6c 450 const char *chan_name;
f4681817 451 int chan_objd;
74d81a6c 452 struct lttng_ust_shm_handle *channel_handle;
f0fde1c3 453 struct lttng_ust_abi_channel_config *lttng_chan_config;
e7bc0ef6 454 struct lttng_ust_channel_buffer *lttng_chan_buf;
5198080d 455 struct lttng_ust_lib_ring_buffer_channel *chan;
74d81a6c
MD
456 struct lttng_ust_lib_ring_buffer_config *config;
457 void *chan_data;
ff0f5728 458 int wakeup_fd;
74d81a6c
MD
459 uint64_t len;
460 int ret;
fd17d7ce 461 enum lttng_ust_abi_chan_type type;
74d81a6c
MD
462
463 chan_data = uargs->channel.chan_data;
ff0f5728 464 wakeup_fd = uargs->channel.wakeup_fd;
74d81a6c
MD
465 len = ust_chan->len;
466 type = ust_chan->type;
467
468 switch (type) {
fd17d7ce 469 case LTTNG_UST_ABI_CHAN_PER_CPU:
74d81a6c
MD
470 break;
471 default:
ff0f5728
MD
472 ret = -EINVAL;
473 goto invalid;
74d81a6c
MD
474 }
475
bdb12629 476 if (session->priv->been_active) {
74d81a6c
MD
477 ret = -EBUSY;
478 goto active; /* Refuse to add channel to active session */
479 }
f4681817 480
e7bc0ef6
MD
481 lttng_chan_buf = lttng_ust_alloc_channel_buffer();
482 if (!lttng_chan_buf) {
f0fde1c3 483 ret = -ENOMEM;
e7bc0ef6 484 goto lttng_chan_buf_error;
f0fde1c3
MD
485 }
486
ff0f5728 487 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
74d81a6c
MD
488 if (!channel_handle) {
489 ret = -EINVAL;
490 goto handle_error;
491 }
492
867942fd
MD
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
74d81a6c
MD
497 chan = shmp(channel_handle, channel_handle->chan);
498 assert(chan);
03d2d293 499 chan->handle = channel_handle;
74d81a6c 500 config = &chan->backend.config;
f0fde1c3
MD
501 lttng_chan_config = channel_get_private_config(chan);
502 if (!lttng_chan_config) {
74d81a6c
MD
503 ret = -EINVAL;
504 goto alloc_error;
505 }
506
9daacd1a
MD
507 if (lttng_ust_session_uuid_validate(session, lttng_chan_config->uuid)) {
508 ret = -EINVAL;
509 goto uuid_error;
510 }
511
74d81a6c
MD
512 /* Lookup transport name */
513 switch (type) {
fd17d7ce 514 case LTTNG_UST_ABI_CHAN_PER_CPU:
74d81a6c 515 if (config->output == RING_BUFFER_MMAP) {
34a91bdb
MD
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 }
f4681817 529 } else {
74d81a6c
MD
530 ret = -EINVAL;
531 goto notransport;
f4681817 532 }
74d81a6c 533 chan_name = "channel";
f4681817 534 break;
f4681817 535 default:
74d81a6c
MD
536 ret = -EINVAL;
537 goto notransport;
fe38d4af 538 }
65c48d6a 539 transport = lttng_ust_transport_find(transport_name);
74d81a6c
MD
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);
fe38d4af
MD
548 if (chan_objd < 0) {
549 ret = chan_objd;
550 goto objd_error;
f4681817 551 }
74d81a6c
MD
552
553 /* Initialize our lttng chan */
e7bc0ef6
MD
554 lttng_chan_buf->parent->enabled = 1;
555 lttng_chan_buf->parent->session = session;
556
557 lttng_chan_buf->priv->parent.tstate = 1;
0950190a 558 lttng_chan_buf->priv->ctx = NULL;
07539b34 559 lttng_chan_buf->priv->rb_chan = chan;
e7bc0ef6 560
e7bc0ef6 561 lttng_chan_buf->ops = &transport->ops;
e7bc0ef6
MD
562
563 memcpy(&chan->backend.config,
74d81a6c 564 transport->client_config,
e7bc0ef6
MD
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;
f0fde1c3 569 /* Copy fields from lttng ust chan config. */
e7bc0ef6
MD
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);
74d81a6c 573
f4681817
MD
574 /*
575 * We tolerate no failure path after channel creation. It will stay
576 * invariant for the rest of the session.
577 */
e7bc0ef6
MD
578 objd_set_private(chan_objd, lttng_chan_buf);
579 lttng_chan_buf->priv->parent.objd = chan_objd;
f4681817
MD
580 /* The channel created holds a reference on the session */
581 objd_ref(session_objd);
f4681817
MD
582 return chan_objd;
583
ff0f5728 584 /* error path after channel was created */
f4681817 585objd_error:
74d81a6c 586notransport:
9daacd1a 587uuid_error:
74d81a6c
MD
588alloc_error:
589 channel_destroy(chan, channel_handle, 0);
e7bc0ef6 590 lttng_ust_free_channel_common(lttng_chan_buf->parent);
ff0f5728
MD
591 return ret;
592
74d81a6c 593handle_error:
e7bc0ef6
MD
594 lttng_ust_free_channel_common(lttng_chan_buf->parent);
595lttng_chan_buf_error:
74d81a6c 596active:
ff0f5728 597invalid:
f4681817
MD
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
ef9ff354 607 * @uargs: UST arguments (internal)
f59ed768 608 * @owner: objd owner
f4681817
MD
609 *
610 * This descriptor implements lttng commands:
fd17d7ce 611 * LTTNG_UST_ABI_CHANNEL
f4681817 612 * Returns a LTTng channel object descriptor
fd17d7ce 613 * LTTNG_UST_ABI_ENABLE
f4681817 614 * Enables tracing for a session (weak enable)
fd17d7ce 615 * LTTNG_UST_ABI_DISABLE
f4681817 616 * Disables tracing for a session (strong disable)
f4681817
MD
617 *
618 * The returned channel will be deleted when its file descriptor is closed.
619 */
620static
ef9ff354 621long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
fd17d7ce 622 union lttng_ust_abi_args *uargs, void *owner)
f4681817 623{
f69fe5fb 624 struct lttng_ust_session *session = objd_private(objd);
f4681817
MD
625
626 switch (cmd) {
fd17d7ce 627 case LTTNG_UST_ABI_CHANNEL:
74d81a6c 628 return lttng_abi_map_channel(objd,
fd17d7ce 629 (struct lttng_ust_abi_channel *) arg,
74d81a6c 630 uargs, owner);
fd17d7ce
MD
631 case LTTNG_UST_ABI_SESSION_START:
632 case LTTNG_UST_ABI_ENABLE:
7dd08bec 633 return lttng_session_enable(session);
fd17d7ce
MD
634 case LTTNG_UST_ABI_SESSION_STOP:
635 case LTTNG_UST_ABI_DISABLE:
7dd08bec 636 return lttng_session_disable(session);
fd17d7ce 637 case LTTNG_UST_ABI_SESSION_STATEDUMP:
710b8ee3 638 return lttng_session_statedump(session);
fd17d7ce
MD
639 case LTTNG_UST_ABI_COUNTER:
640 case LTTNG_UST_ABI_COUNTER_GLOBAL:
641 case LTTNG_UST_ABI_COUNTER_CPU:
ebabbf58
MD
642 /* Not implemented yet. */
643 return -EINVAL;
f4681817
MD
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 */
657static
1ea11eab 658int lttng_release_session(int objd)
f4681817 659{
f69fe5fb 660 struct lttng_ust_session *session = objd_private(objd);
f4681817 661
1ea11eab 662 if (session) {
7dd08bec 663 lttng_session_destroy(session);
1ea11eab
MD
664 return 0;
665 } else {
666 return -EINVAL;
667 }
f4681817
MD
668}
669
fd17d7ce 670static const struct lttng_ust_abi_objd_ops lttng_session_ops = {
1ea11eab 671 .release = lttng_release_session,
f4681817
MD
672 .cmd = lttng_session_cmd,
673};
674
d8d2416d 675static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj,
fd17d7ce 676 void *owner, struct lttng_ust_abi_event_notifier *event_notifier_param,
d8d2416d
FD
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
fd17d7ce 684 event_notifier_param->event.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
d8d2416d
FD
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
705event_notifier_error:
706 {
707 int err;
708
fd17d7ce 709 err = lttng_ust_abi_objd_unref(event_notifier_objd, 1);
d8d2416d
FD
710 assert(!err);
711 }
712objd_error:
713 return ret;
714}
715
716static
717long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
2208d8b5
MJ
718 union lttng_ust_abi_args *uargs __attribute__((unused)),
719 void *owner __attribute__((unused)))
d8d2416d
FD
720{
721 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
722 switch (cmd) {
fd17d7ce 723 case LTTNG_UST_ABI_FILTER:
a56fd376
FD
724 return lttng_event_notifier_enabler_attach_filter_bytecode(
725 event_notifier_enabler,
ab89263e 726 (struct lttng_ust_bytecode_node **) arg);
fd17d7ce 727 case LTTNG_UST_ABI_EXCLUSION:
d8d2416d 728 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
e9fe6aad 729 (struct lttng_ust_excluder_node **) arg);
fd17d7ce 730 case LTTNG_UST_ABI_CAPTURE:
d37ecb3f
FD
731 return lttng_event_notifier_enabler_attach_capture_bytecode(
732 event_notifier_enabler,
49cde654 733 (struct lttng_ust_bytecode_node **) arg);
fd17d7ce 734 case LTTNG_UST_ABI_ENABLE:
d8d2416d 735 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
fd17d7ce 736 case LTTNG_UST_ABI_DISABLE:
d8d2416d
FD
737 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
738 default:
739 return -EINVAL;
740 }
741}
742
ebabbf58
MD
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:
fd17d7ce 753 * LTTNG_UST_ABI_COUNTER_GLOBAL
ebabbf58 754 * Return negative error code on error, 0 on success.
fd17d7ce 755 * LTTNG_UST_ABI_COUNTER_CPU
ebabbf58
MD
756 * Return negative error code on error, 0 on success.
757 */
758static
759long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
2208d8b5 760 union lttng_ust_abi_args *uargs, void *owner __attribute__((unused)))
ebabbf58 761{
ba5b3d2b 762 int ret;
ebabbf58
MD
763 struct lttng_counter *counter = objd_private(objd);
764
765 switch (cmd) {
fd17d7ce 766 case LTTNG_UST_ABI_COUNTER_GLOBAL:
ba5b3d2b
JR
767 ret = -EINVAL; /* Unimplemented. */
768 break;
fd17d7ce 769 case LTTNG_UST_ABI_COUNTER_CPU:
ebabbf58 770 {
fd17d7ce
MD
771 struct lttng_ust_abi_counter_cpu *counter_cpu =
772 (struct lttng_ust_abi_counter_cpu *)arg;
ba5b3d2b
JR
773
774 ret = lttng_counter_set_cpu_shm(counter->counter,
ebabbf58 775 counter_cpu->cpu_nr, uargs->counter_shm.shm_fd);
ba5b3d2b
JR
776 if (!ret) {
777 /* Take ownership of the shm_fd. */
778 uargs->counter_shm.shm_fd = -1;
779 }
780 break;
ebabbf58
MD
781 }
782 default:
ba5b3d2b
JR
783 ret = -EINVAL;
784 break;
ebabbf58 785 }
ba5b3d2b
JR
786
787 return ret;
ebabbf58
MD
788}
789
1d18d519
MJ
790int lttng_release_event_notifier_group_error_counter(int objd)
791 __attribute__((visibility("hidden")));
ebabbf58
MD
792int lttng_release_event_notifier_group_error_counter(int objd)
793{
794 struct lttng_counter *counter = objd_private(objd);
795
796 if (counter) {
fd17d7ce 797 return lttng_ust_abi_objd_unref(counter->event_notifier_group->objd, 0);
ebabbf58
MD
798 } else {
799 return -EINVAL;
800 }
801}
802
fd17d7ce 803static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_error_counter_ops = {
ebabbf58
MD
804 .release = lttng_release_event_notifier_group_error_counter,
805 .cmd = lttng_event_notifier_group_error_counter_cmd,
806};
807
808static
809int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
fd17d7ce 810 struct lttng_ust_abi_counter_conf *error_counter_conf)
ebabbf58
MD
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
fd17d7ce 823 if (error_counter_conf->arithmetic != LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR)
ebabbf58
MD
824 return -EINVAL;
825
826 if (error_counter_conf->number_dimensions != 1)
827 return -EINVAL;
828
829 switch (error_counter_conf->bitness) {
fd17d7ce 830 case LTTNG_UST_ABI_COUNTER_BITNESS_64:
ebabbf58
MD
831 counter_transport_name = "counter-per-cpu-64-modular";
832 break;
fd17d7ce 833 case LTTNG_UST_ABI_COUNTER_BITNESS_32:
ebabbf58
MD
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
ebabbf58 860 event_notifier_group->error_counter_len = counter_len;
33f43caa
MD
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);
ebabbf58
MD
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
880create_error:
881 {
882 int err;
883
fd17d7ce 884 err = lttng_ust_abi_objd_unref(counter_objd, 1);
ebabbf58
MD
885 assert(!err);
886 }
887objd_error:
888 return ret;
889}
890
d8d2416d
FD
891static
892long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
fd17d7ce 893 union lttng_ust_abi_args *uargs, void *owner)
d8d2416d
FD
894{
895 switch (cmd) {
fd17d7ce 896 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE:
d8d2416d 897 {
fd17d7ce
MD
898 struct lttng_ust_abi_event_notifier *event_notifier_param =
899 (struct lttng_ust_abi_event_notifier *) arg;
d8d2416d
FD
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 }
fd17d7ce 914 case LTTNG_UST_ABI_COUNTER:
ebabbf58 915 {
fd17d7ce
MD
916 struct lttng_ust_abi_counter_conf *counter_conf =
917 (struct lttng_ust_abi_counter_conf *) uargs->counter.counter_data;
ebabbf58
MD
918 return lttng_ust_event_notifier_group_create_error_counter(
919 objd, owner, counter_conf);
920 }
d8d2416d
FD
921 default:
922 return -EINVAL;
923 }
924}
925
926static
927int 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)
fd17d7ce 932 return lttng_ust_abi_objd_unref(event_notifier_enabler->group->objd, 0);
d8d2416d
FD
933 return 0;
934}
935
fd17d7ce 936static const struct lttng_ust_abi_objd_ops lttng_event_notifier_enabler_ops = {
d8d2416d
FD
937 .release = lttng_event_notifier_enabler_release,
938 .cmd = lttng_event_notifier_enabler_cmd,
939};
940
941static
942int 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
fd17d7ce 954static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_ops = {
d8d2416d
FD
955 .release = lttng_release_event_notifier_group,
956 .cmd = lttng_event_notifier_group_cmd,
957};
958
51489cad 959static
ef9ff354 960long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
2208d8b5
MJ
961 union lttng_ust_abi_args *uargs __attribute__((unused)),
962 void *owner __attribute__((unused)))
51489cad 963{
c8fcf224 964 struct lttng_ust_tracepoint_list *list = objd_private(objd);
fd17d7ce
MD
965 struct lttng_ust_abi_tracepoint_iter *tp =
966 (struct lttng_ust_abi_tracepoint_iter *) arg;
967 struct lttng_ust_abi_tracepoint_iter *iter;
51489cad
MD
968
969 switch (cmd) {
fd17d7ce 970 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET:
c8fcf224 971 {
c8fcf224
MD
972 iter = lttng_ust_tracepoint_list_get_iter_next(list);
973 if (!iter)
7bc53e94 974 return -LTTNG_UST_ERR_NOENT;
c8fcf224 975 memcpy(tp, iter, sizeof(*tp));
51489cad 976 return 0;
c8fcf224 977 }
51489cad
MD
978 default:
979 return -EINVAL;
980 }
981}
982
983static
f59ed768 984int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
985{
986 int list_objd, ret;
c8fcf224 987 struct lttng_ust_tracepoint_list *list;
51489cad 988
7047f3da 989 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
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
c8fcf224 1001 /* populate list by walking on all registered probes. */
7dd08bec 1002 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
1003 if (ret) {
1004 goto list_error;
1005 }
51489cad
MD
1006 return list_objd;
1007
c8fcf224
MD
1008list_error:
1009 free(list);
51489cad
MD
1010alloc_error:
1011 {
1012 int err;
1013
fd17d7ce 1014 err = lttng_ust_abi_objd_unref(list_objd, 1);
51489cad
MD
1015 assert(!err);
1016 }
1017objd_error:
1018 return ret;
1019}
1020
1021static
1022int lttng_release_tracepoint_list(int objd)
1023{
c8fcf224 1024 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
1025
1026 if (list) {
7dd08bec 1027 lttng_probes_prune_event_list(list);
51489cad
MD
1028 free(list);
1029 return 0;
1030 } else {
1031 return -EINVAL;
1032 }
1033}
1034
fd17d7ce 1035static const struct lttng_ust_abi_objd_ops lttng_tracepoint_list_ops = {
51489cad
MD
1036 .release = lttng_release_tracepoint_list,
1037 .cmd = lttng_tracepoint_list_cmd,
1038};
1039
06d4f27e
MD
1040static
1041long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
2208d8b5
MJ
1042 unsigned long arg __attribute__((unused)), union lttng_ust_abi_args *uargs,
1043 void *owner __attribute__((unused)))
06d4f27e
MD
1044{
1045 struct lttng_ust_field_list *list = objd_private(objd);
fd17d7ce
MD
1046 struct lttng_ust_abi_field_iter *tp = &uargs->field_list.entry;
1047 struct lttng_ust_abi_field_iter *iter;
06d4f27e
MD
1048
1049 switch (cmd) {
fd17d7ce 1050 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET:
06d4f27e 1051 {
06d4f27e
MD
1052 iter = lttng_ust_field_list_get_iter_next(list);
1053 if (!iter)
7bc53e94 1054 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
1055 memcpy(tp, iter, sizeof(*tp));
1056 return 0;
1057 }
1058 default:
1059 return -EINVAL;
1060 }
1061}
1062
1063static
f59ed768 1064int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
1065{
1066 int list_objd, ret;
1067 struct lttng_ust_field_list *list;
1068
7047f3da
MD
1069 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
1070 "tp_field_list");
06d4f27e
MD
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. */
7dd08bec 1083 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
1084 if (ret) {
1085 goto list_error;
1086 }
1087 return list_objd;
1088
1089list_error:
1090 free(list);
1091alloc_error:
1092 {
1093 int err;
1094
fd17d7ce 1095 err = lttng_ust_abi_objd_unref(list_objd, 1);
06d4f27e
MD
1096 assert(!err);
1097 }
1098objd_error:
1099 return ret;
1100}
1101
1102static
1103int lttng_release_tracepoint_field_list(int objd)
1104{
1105 struct lttng_ust_field_list *list = objd_private(objd);
1106
1107 if (list) {
7dd08bec 1108 lttng_probes_prune_field_list(list);
06d4f27e
MD
1109 free(list);
1110 return 0;
1111 } else {
1112 return -EINVAL;
1113 }
1114}
1115
fd17d7ce 1116static const struct lttng_ust_abi_objd_ops lttng_tracepoint_field_list_ops = {
06d4f27e
MD
1117 .release = lttng_release_tracepoint_field_list,
1118 .cmd = lttng_tracepoint_field_list_cmd,
1119};
1120
f4681817 1121static
fd17d7ce 1122int lttng_abi_map_stream(int channel_objd, struct lttng_ust_abi_stream *info,
2208d8b5 1123 union lttng_ust_abi_args *uargs, void *owner __attribute__((unused)))
f4681817 1124{
e7bc0ef6 1125 struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(channel_objd);
74d81a6c 1126 int ret;
f4681817 1127
07539b34 1128 ret = channel_handle_add_stream(lttng_chan_buf->priv->rb_chan->handle,
74d81a6c
MD
1129 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
1130 info->stream_nr, info->len);
1131 if (ret)
1132 goto error_add_stream;
118c051e
MD
1133 /* Take ownership of shm_fd and wakeup_fd. */
1134 uargs->stream.shm_fd = -1;
1135 uargs->stream.wakeup_fd = -1;
74d81a6c
MD
1136
1137 return 0;
1138
1139error_add_stream:
f4681817
MD
1140 return ret;
1141}
f4681817
MD
1142
1143static
d871c65b 1144int lttng_abi_create_event_enabler(int channel_objd,
fd17d7ce 1145 struct lttng_ust_abi_event *event_param,
e58095ef 1146 void *owner,
e450e339 1147 enum lttng_enabler_format_type format_type)
f4681817 1148{
e7bc0ef6 1149 struct lttng_ust_channel_buffer *channel = objd_private(channel_objd);
d871c65b 1150 struct lttng_event_enabler *enabler;
f4681817
MD
1151 int event_objd, ret;
1152
fd17d7ce 1153 event_param->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
d871c65b
FD
1154 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
1155 "event enabler");
f4681817
MD
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 */
d871c65b 1164 enabler = lttng_event_enabler_create(format_type, event_param, channel);
e58095ef
MD
1165 if (!enabler) {
1166 ret = -ENOMEM;
f4681817
MD
1167 goto event_error;
1168 }
e58095ef 1169 objd_set_private(event_objd, enabler);
f4681817
MD
1170 /* The event holds a reference on the channel */
1171 objd_ref(channel_objd);
1172 return event_objd;
1173
1174event_error:
1ea11eab
MD
1175 {
1176 int err;
1177
fd17d7ce 1178 err = lttng_ust_abi_objd_unref(event_objd, 1);
1ea11eab
MD
1179 assert(!err);
1180 }
f4681817
MD
1181objd_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
ef9ff354 1191 * @uargs: UST arguments (internal)
f59ed768 1192 * @owner: objd owner
f4681817
MD
1193 *
1194 * This object descriptor implements lttng commands:
fd17d7ce 1195 * LTTNG_UST_ABI_STREAM
f4681817
MD
1196 * Returns an event stream object descriptor or failure.
1197 * (typically, one event stream records events from one CPU)
fd17d7ce 1198 * LTTNG_UST_ABI_EVENT
f4681817 1199 * Returns an event object descriptor or failure.
fd17d7ce 1200 * LTTNG_UST_ABI_CONTEXT
f4681817 1201 * Prepend a context field to each event in the channel
fd17d7ce 1202 * LTTNG_UST_ABI_ENABLE
f4681817 1203 * Enable recording for events in this channel (weak enable)
fd17d7ce 1204 * LTTNG_UST_ABI_DISABLE
f4681817
MD
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 */
1209static
ef9ff354 1210long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
fd17d7ce 1211 union lttng_ust_abi_args *uargs, void *owner)
f4681817 1212{
e7bc0ef6 1213 struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(objd);
f4681817 1214
fd17d7ce 1215 if (cmd != LTTNG_UST_ABI_STREAM) {
74d81a6c
MD
1216 /*
1217 * Check if channel received all streams.
1218 */
e7bc0ef6 1219 if (!lttng_is_channel_ready(lttng_chan_buf))
74d81a6c
MD
1220 return -EPERM;
1221 }
1222
f4681817 1223 switch (cmd) {
fd17d7ce 1224 case LTTNG_UST_ABI_STREAM:
381c0f1e 1225 {
fd17d7ce 1226 struct lttng_ust_abi_stream *stream;
381c0f1e 1227
fd17d7ce 1228 stream = (struct lttng_ust_abi_stream *) arg;
381c0f1e 1229 /* stream used as output */
74d81a6c 1230 return lttng_abi_map_stream(objd, stream, uargs, owner);
381c0f1e 1231 }
fd17d7ce 1232 case LTTNG_UST_ABI_EVENT:
1f18504e 1233 {
fd17d7ce
MD
1234 struct lttng_ust_abi_event *event_param =
1235 (struct lttng_ust_abi_event *) arg;
196ec2df
PP
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 */
d871c65b 1242 return lttng_abi_create_event_enabler(objd, event_param,
e450e339 1243 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1f18504e 1244 } else {
d871c65b 1245 return lttng_abi_create_event_enabler(objd, event_param,
e450e339 1246 owner, LTTNG_ENABLER_FORMAT_EVENT);
1f18504e
MD
1247 }
1248 }
fd17d7ce 1249 case LTTNG_UST_ABI_CONTEXT:
f4681817 1250 return lttng_abi_add_context(objd,
fd17d7ce 1251 (struct lttng_ust_abi_context *) arg, uargs,
0950190a 1252 &lttng_chan_buf->priv->ctx,
e7bc0ef6 1253 lttng_chan_buf->parent->session);
fd17d7ce 1254 case LTTNG_UST_ABI_ENABLE:
e7bc0ef6 1255 return lttng_channel_enable(lttng_chan_buf->parent);
fd17d7ce 1256 case LTTNG_UST_ABI_DISABLE:
e7bc0ef6 1257 return lttng_channel_disable(lttng_chan_buf->parent);
fd17d7ce 1258 case LTTNG_UST_ABI_FLUSH_BUFFER:
07539b34 1259 return lttng_chan_buf->ops->priv->flush_buffer(lttng_chan_buf);
f4681817
MD
1260 default:
1261 return -EINVAL;
1262 }
1263}
1264
f4681817
MD
1265static
1266int lttng_channel_release(int objd)
1267{
e7bc0ef6 1268 struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(objd);
f4681817 1269
e7bc0ef6
MD
1270 if (lttng_chan_buf)
1271 return lttng_ust_abi_objd_unref(lttng_chan_buf->parent->session->priv->objd, 0);
f4681817
MD
1272 return 0;
1273}
1274
fd17d7ce 1275static const struct lttng_ust_abi_objd_ops lttng_channel_ops = {
f4681817 1276 .release = lttng_channel_release,
f4681817
MD
1277 .cmd = lttng_channel_cmd,
1278};
1279
f4681817 1280/**
e58095ef 1281 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
1282 *
1283 * @objd: the object descriptor
1284 * @cmd: the command
1285 * @arg: command arg
ef9ff354 1286 * @uargs: UST arguments (internal)
f59ed768 1287 * @owner: objd owner
e6c12e3d
MD
1288 *
1289 * This object descriptor implements lttng commands:
fd17d7ce 1290 * LTTNG_UST_ABI_CONTEXT
e6c12e3d 1291 * Prepend a context field to each record of events of this
e58095ef 1292 * enabler.
fd17d7ce 1293 * LTTNG_UST_ABI_ENABLE
e58095ef 1294 * Enable recording for this enabler
fd17d7ce 1295 * LTTNG_UST_ABI_DISABLE
e58095ef 1296 * Disable recording for this enabler
fd17d7ce 1297 * LTTNG_UST_ABI_FILTER
e58095ef 1298 * Attach a filter to an enabler.
fd17d7ce 1299 * LTTNG_UST_ABI_EXCLUSION
0bfb5cbd 1300 * Attach exclusions to an enabler.
e6c12e3d
MD
1301 */
1302static
d871c65b 1303long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
2208d8b5
MJ
1304 union lttng_ust_abi_args *uargs __attribute__((unused)),
1305 void *owner __attribute__((unused)))
e6c12e3d 1306{
d871c65b 1307 struct lttng_event_enabler *enabler = objd_private(objd);
e6c12e3d
MD
1308
1309 switch (cmd) {
fd17d7ce 1310 case LTTNG_UST_ABI_CONTEXT:
d871c65b 1311 return lttng_event_enabler_attach_context(enabler,
fd17d7ce
MD
1312 (struct lttng_ust_abi_context *) arg);
1313 case LTTNG_UST_ABI_ENABLE:
d871c65b 1314 return lttng_event_enabler_enable(enabler);
fd17d7ce 1315 case LTTNG_UST_ABI_DISABLE:
d871c65b 1316 return lttng_event_enabler_disable(enabler);
fd17d7ce 1317 case LTTNG_UST_ABI_FILTER:
2d78951a
MD
1318 {
1319 int ret;
1320
a56fd376 1321 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
ab89263e 1322 (struct lttng_ust_bytecode_node **) arg);
2d78951a
MD
1323 if (ret)
1324 return ret;
2d78951a
MD
1325 return 0;
1326 }
fd17d7ce 1327 case LTTNG_UST_ABI_EXCLUSION:
0bfb5cbd 1328 {
d871c65b 1329 return lttng_event_enabler_attach_exclusion(enabler,
e9fe6aad 1330 (struct lttng_ust_excluder_node **) arg);
0bfb5cbd 1331 }
e6c12e3d
MD
1332 default:
1333 return -EINVAL;
1334 }
1335}
1336
1337static
d871c65b 1338int lttng_event_enabler_release(int objd)
e6c12e3d 1339{
d871c65b
FD
1340 struct lttng_event_enabler *event_enabler = objd_private(objd);
1341
1342 if (event_enabler)
e7bc0ef6 1343 return lttng_ust_abi_objd_unref(event_enabler->chan->priv->parent.objd, 0);
e6c12e3d 1344
e6c12e3d
MD
1345 return 0;
1346}
1347
fd17d7ce 1348static const struct lttng_ust_abi_objd_ops lttng_event_enabler_ops = {
d871c65b
FD
1349 .release = lttng_event_enabler_release,
1350 .cmd = lttng_event_enabler_cmd,
e6c12e3d
MD
1351};
1352
b35d179d 1353void lttng_ust_abi_exit(void)
f4681817 1354{
45e9e699 1355 lttng_ust_abi_close_in_progress = 1;
701a658f 1356 ust_lock_nocheck();
f4681817 1357 objd_table_destroy();
701a658f 1358 ust_unlock();
45e9e699 1359 lttng_ust_abi_close_in_progress = 0;
f4681817 1360}
This page took 0.110438 seconds and 4 git commands to generate.