Fix: refcount issue in lttng-ust-abi.c
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817
MD
1/*
2 * lttng-ust-abi.c
3 *
f4681817
MD
4 * LTTng UST ABI
5 *
e92f3e28
MD
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
f4681817
MD
23 * Mimic system calls for:
24 * - session creation, returns an object descriptor or failure.
25 * - channel creation, returns an object descriptor or failure.
26 * - Operates on a session object descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns an object descriptor or failure.
29 * - Operates on a channel object descriptor.
30 * - stream notifier get, returns an object descriptor or failure.
31 * - Operates on a channel object descriptor.
32 * - event creation, returns an object descriptor or failure.
33 * - Operates on a channel object descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
f4681817
MD
38 */
39
4318ae1b 40#include <lttng/ust-abi.h>
7bc53e94 41#include <lttng/ust-error.h>
f4681817
MD
42#include <urcu/compiler.h>
43#include <urcu/list.h>
4318ae1b 44#include <lttng/ust-events.h>
23c8854a 45#include <lttng/ust-version.h>
902e1379 46#include <lttng/tracepoint.h>
d8de1354 47#include "tracepoint-internal.h"
44c72f10
MD
48#include <usterr-signal-safe.h>
49#include <helper.h>
7dd08bec 50#include "lttng-tracer.h"
74d81a6c
MD
51#include "../libringbuffer/shm.h"
52#include "../libringbuffer/frontend_types.h"
4ab44fbe 53
7047f3da
MD
54#define OBJ_NAME_LEN 16
55
45e9e699
MD
56static int lttng_ust_abi_close_in_progress;
57
51489cad 58static
f59ed768 59int lttng_abi_tracepoint_list(void *owner);
06d4f27e 60static
f59ed768 61int lttng_abi_tracepoint_field_list(void *owner);
51489cad 62
f4681817
MD
63/*
64 * Object descriptor table. Should be protected from concurrent access
65 * by the caller.
66 */
67
b61ce3b2 68struct lttng_ust_obj {
f4681817
MD
69 union {
70 struct {
71 void *private_data;
b61ce3b2 72 const struct lttng_ust_objd_ops *ops;
f4681817 73 int f_count;
1849ef7c 74 int owner_ref; /* has ref from owner */
f59ed768 75 void *owner;
7047f3da 76 char name[OBJ_NAME_LEN];
f4681817
MD
77 } s;
78 int freelist_next; /* offset freelist. end is -1. */
79 } u;
80};
81
b61ce3b2
MD
82struct lttng_ust_objd_table {
83 struct lttng_ust_obj *array;
f4681817
MD
84 unsigned int len, allocated_len;
85 int freelist_head; /* offset freelist head. end is -1 */
86};
87
b61ce3b2 88static struct lttng_ust_objd_table objd_table = {
f4681817
MD
89 .freelist_head = -1,
90};
91
92static
f59ed768 93int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
7047f3da 94 void *owner, const char *name)
f4681817 95{
b61ce3b2 96 struct lttng_ust_obj *obj;
f4681817
MD
97
98 if (objd_table.freelist_head != -1) {
99 obj = &objd_table.array[objd_table.freelist_head];
100 objd_table.freelist_head = obj->u.freelist_next;
101 goto end;
102 }
103
104 if (objd_table.len >= objd_table.allocated_len) {
105 unsigned int new_allocated_len, old_allocated_len;
b61ce3b2 106 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
107
108 old_allocated_len = objd_table.allocated_len;
109 old_table = objd_table.array;
110 if (!old_allocated_len)
111 new_allocated_len = 1;
112 else
113 new_allocated_len = old_allocated_len << 1;
b61ce3b2 114 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
115 if (!new_table)
116 return -ENOMEM;
117 memcpy(new_table, old_table,
b61ce3b2 118 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
119 free(old_table);
120 objd_table.array = new_table;
121 objd_table.allocated_len = new_allocated_len;
122 }
123 obj = &objd_table.array[objd_table.len];
124 objd_table.len++;
125end:
126 obj->u.s.private_data = private_data;
127 obj->u.s.ops = ops;
a4be8962
MD
128 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
129 /* count == 2 : allocated + hold ref */
1849ef7c 130 obj->u.s.owner_ref = 1; /* One owner reference */
f59ed768 131 obj->u.s.owner = owner;
7047f3da
MD
132 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
133 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
f4681817
MD
134 return obj - objd_table.array;
135}
136
137static
b61ce3b2 138struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
139{
140 if (id >= objd_table.len)
141 return NULL;
a4be8962
MD
142 if (!objd_table.array[id].u.s.f_count)
143 return NULL;
f4681817
MD
144 return &objd_table.array[id];
145}
146
147static
148void *objd_private(int id)
149{
b61ce3b2 150 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
151 assert(obj);
152 return obj->u.s.private_data;
153}
154
155static
156void objd_set_private(int id, void *private_data)
157{
b61ce3b2 158 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
159 assert(obj);
160 obj->u.s.private_data = private_data;
161}
162
b61ce3b2 163const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 164{
b61ce3b2 165 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 166
a4be8962
MD
167 if (!obj)
168 return NULL;
f4681817
MD
169 return obj->u.s.ops;
170}
171
172static
173void objd_free(int id)
174{
b61ce3b2 175 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
176
177 assert(obj);
178 obj->u.freelist_next = objd_table.freelist_head;
179 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
180 assert(obj->u.s.f_count == 1);
181 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
182}
183
184static
185void objd_ref(int id)
186{
b61ce3b2 187 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
188 obj->u.s.f_count++;
189}
190
1849ef7c 191int lttng_ust_objd_unref(int id, int is_owner)
f4681817 192{
b61ce3b2 193 struct lttng_ust_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) {
b61ce3b2 209 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 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
MD
223 for (i = 0; i < objd_table.allocated_len; i++) {
224 struct lttng_ust_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_objd_unref(i, 1);
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{
242 struct lttng_ust_obj *obj = _objd_get(id);
243
244 if (!obj)
245 return NULL;
246 return obj->u.s.name;
247}
248
f59ed768
MD
249void lttng_ust_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_obj *obj;
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)
1849ef7c 264 (void) lttng_ust_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
b61ce3b2
MD
273static const struct lttng_ust_objd_ops lttng_ops;
274static const struct lttng_ust_objd_ops lttng_session_ops;
275static const struct lttng_ust_objd_ops lttng_channel_ops;
e58095ef 276static const struct lttng_ust_objd_ops lttng_enabler_ops;
51489cad 277static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 278static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817 279
46050b1a
MD
280int lttng_abi_create_root_handle(void)
281{
282 int root_handle;
283
f59ed768 284 /* root handles have NULL owners */
7047f3da 285 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
46050b1a
MD
286 return root_handle;
287}
288
74d81a6c
MD
289static
290int lttng_is_channel_ready(struct lttng_channel *lttng_chan)
291{
292 struct channel *chan;
293 unsigned int nr_streams, exp_streams;
294
295 chan = lttng_chan->chan;
296 nr_streams = channel_handle_get_nr_streams(lttng_chan->handle);
297 exp_streams = chan->nr_streams;
298 return nr_streams == exp_streams;
299}
300
818173b9 301static
f59ed768 302int lttng_abi_create_session(void *owner)
f4681817 303{
7dd08bec 304 struct lttng_session *session;
f4681817
MD
305 int session_objd, ret;
306
7dd08bec 307 session = lttng_session_create();
f4681817
MD
308 if (!session)
309 return -ENOMEM;
7047f3da 310 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
f4681817
MD
311 if (session_objd < 0) {
312 ret = session_objd;
313 goto objd_error;
314 }
315 session->objd = session_objd;
32ce8569 316 session->owner = owner;
f4681817
MD
317 return session_objd;
318
319objd_error:
7dd08bec 320 lttng_session_destroy(session);
f4681817
MD
321 return ret;
322}
323
f4681817
MD
324static
325long lttng_abi_tracer_version(int objd,
326 struct lttng_ust_tracer_version *v)
327{
32ce8569
MD
328 v->major = LTTNG_UST_MAJOR_VERSION;
329 v->minor = LTTNG_UST_MINOR_VERSION;
330 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
331 return 0;
332}
333
334static
335long lttng_abi_add_context(int objd,
336 struct lttng_ust_context *context_param,
7dd08bec 337 struct lttng_ctx **ctx, struct lttng_session *session)
f4681817 338{
e58095ef 339 return lttng_attach_context(context_param, ctx, session);
f4681817
MD
340}
341
342/**
343 * lttng_cmd - lttng control through socket commands
344 *
345 * @objd: the object descriptor
346 * @cmd: the command
347 * @arg: command arg
ef9ff354 348 * @uargs: UST arguments (internal)
f59ed768 349 * @owner: objd owner
f4681817
MD
350 *
351 * This descriptor implements lttng commands:
352 * LTTNG_UST_SESSION
353 * Returns a LTTng trace session object descriptor
354 * LTTNG_UST_TRACER_VERSION
355 * Returns the LTTng kernel tracer version
356 * LTTNG_UST_TRACEPOINT_LIST
357 * Returns a file descriptor listing available tracepoints
06d4f27e
MD
358 * LTTNG_UST_TRACEPOINT_FIELD_LIST
359 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
360 * LTTNG_UST_WAIT_QUIESCENT
361 * Returns after all previously running probes have completed
362 *
363 * The returned session will be deleted when its file descriptor is closed.
364 */
365static
ef9ff354 366long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 367 union ust_args *uargs, void *owner)
f4681817
MD
368{
369 switch (cmd) {
370 case LTTNG_UST_SESSION:
f59ed768 371 return lttng_abi_create_session(owner);
f4681817
MD
372 case LTTNG_UST_TRACER_VERSION:
373 return lttng_abi_tracer_version(objd,
374 (struct lttng_ust_tracer_version *) arg);
375 case LTTNG_UST_TRACEPOINT_LIST:
f59ed768 376 return lttng_abi_tracepoint_list(owner);
06d4f27e 377 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
f59ed768 378 return lttng_abi_tracepoint_field_list(owner);
f4681817
MD
379 case LTTNG_UST_WAIT_QUIESCENT:
380 synchronize_trace();
381 return 0;
382 default:
383 return -EINVAL;
384 }
385}
386
b61ce3b2 387static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
388 .cmd = lttng_cmd,
389};
390
74d81a6c
MD
391int lttng_abi_map_channel(int session_objd,
392 struct lttng_ust_channel *ust_chan,
393 union ust_args *uargs,
394 void *owner)
f4681817 395{
7dd08bec 396 struct lttng_session *session = objd_private(session_objd);
f4681817 397 const char *transport_name;
74d81a6c
MD
398 const struct lttng_transport *transport;
399 const char *chan_name;
f4681817 400 int chan_objd;
74d81a6c
MD
401 struct lttng_ust_shm_handle *channel_handle;
402 struct lttng_channel *lttng_chan;
403 struct channel *chan;
404 struct lttng_ust_lib_ring_buffer_config *config;
405 void *chan_data;
406 uint64_t len;
407 int ret;
408 enum lttng_ust_chan_type type;
409
410 chan_data = uargs->channel.chan_data;
411 len = ust_chan->len;
412 type = ust_chan->type;
413
414 switch (type) {
415 case LTTNG_UST_CHAN_PER_CPU:
74d81a6c
MD
416 break;
417 default:
418 return -EINVAL;
419 }
420
421 if (session->been_active) {
422 ret = -EBUSY;
423 goto active; /* Refuse to add channel to active session */
424 }
f4681817 425
74d81a6c
MD
426 channel_handle = channel_handle_create(chan_data, len);
427 if (!channel_handle) {
428 ret = -EINVAL;
429 goto handle_error;
430 }
431
432 chan = shmp(channel_handle, channel_handle->chan);
433 assert(chan);
03d2d293 434 chan->handle = channel_handle;
74d81a6c
MD
435 config = &chan->backend.config;
436 lttng_chan = channel_get_private(chan);
437 if (!lttng_chan) {
438 ret = -EINVAL;
439 goto alloc_error;
440 }
441
442 /* Lookup transport name */
443 switch (type) {
444 case LTTNG_UST_CHAN_PER_CPU:
445 if (config->output == RING_BUFFER_MMAP) {
446 transport_name = config->mode == RING_BUFFER_OVERWRITE ?
f4681817
MD
447 "relay-overwrite-mmap" : "relay-discard-mmap";
448 } else {
74d81a6c
MD
449 ret = -EINVAL;
450 goto notransport;
f4681817 451 }
74d81a6c 452 chan_name = "channel";
f4681817 453 break;
f4681817
MD
454 default:
455 transport_name = "<unknown>";
74d81a6c
MD
456 chan_name = "<unknown>";
457 ret = -EINVAL;
458 goto notransport;
fe38d4af 459 }
74d81a6c
MD
460 transport = lttng_transport_find(transport_name);
461 if (!transport) {
462 DBG("LTTng transport %s not found\n",
463 transport_name);
464 ret = -EINVAL;
465 goto notransport;
466 }
467
468 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
fe38d4af
MD
469 if (chan_objd < 0) {
470 ret = chan_objd;
471 goto objd_error;
f4681817 472 }
74d81a6c
MD
473
474 /* Initialize our lttng chan */
475 lttng_chan->chan = chan;
ac6b4ac6 476 lttng_chan->tstate = 1;
74d81a6c
MD
477 lttng_chan->enabled = 1;
478 lttng_chan->ctx = NULL;
479 lttng_chan->session = session;
74d81a6c
MD
480 lttng_chan->ops = &transport->ops;
481 memcpy(&lttng_chan->chan->backend.config,
482 transport->client_config,
483 sizeof(lttng_chan->chan->backend.config));
484 cds_list_add(&lttng_chan->node, &session->chan_head);
485 lttng_chan->header_type = 0;
486 lttng_chan->handle = channel_handle;
74d81a6c
MD
487 lttng_chan->type = type;
488
f4681817
MD
489 /*
490 * We tolerate no failure path after channel creation. It will stay
491 * invariant for the rest of the session.
492 */
74d81a6c
MD
493 objd_set_private(chan_objd, lttng_chan);
494 lttng_chan->objd = chan_objd;
f4681817
MD
495 /* The channel created holds a reference on the session */
496 objd_ref(session_objd);
f4681817
MD
497 return chan_objd;
498
f4681817 499objd_error:
74d81a6c
MD
500notransport:
501 free(lttng_chan);
502alloc_error:
503 channel_destroy(chan, channel_handle, 0);
504handle_error:
505active:
f4681817
MD
506 return ret;
507}
508
509/**
510 * lttng_session_cmd - lttng session object command
511 *
512 * @obj: the object
513 * @cmd: the command
514 * @arg: command arg
ef9ff354 515 * @uargs: UST arguments (internal)
f59ed768 516 * @owner: objd owner
f4681817
MD
517 *
518 * This descriptor implements lttng commands:
519 * LTTNG_UST_CHANNEL
520 * Returns a LTTng channel object descriptor
521 * LTTNG_UST_ENABLE
522 * Enables tracing for a session (weak enable)
523 * LTTNG_UST_DISABLE
524 * Disables tracing for a session (strong disable)
f4681817
MD
525 *
526 * The returned channel will be deleted when its file descriptor is closed.
527 */
528static
ef9ff354 529long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 530 union ust_args *uargs, void *owner)
f4681817 531{
7dd08bec 532 struct lttng_session *session = objd_private(objd);
f4681817
MD
533
534 switch (cmd) {
535 case LTTNG_UST_CHANNEL:
74d81a6c 536 return lttng_abi_map_channel(objd,
f4681817 537 (struct lttng_ust_channel *) arg,
74d81a6c 538 uargs, owner);
f4681817
MD
539 case LTTNG_UST_SESSION_START:
540 case LTTNG_UST_ENABLE:
7dd08bec 541 return lttng_session_enable(session);
f4681817
MD
542 case LTTNG_UST_SESSION_STOP:
543 case LTTNG_UST_DISABLE:
7dd08bec 544 return lttng_session_disable(session);
f4681817
MD
545 default:
546 return -EINVAL;
547 }
548}
549
550/*
551 * Called when the last file reference is dropped.
552 *
553 * Big fat note: channels and events are invariant for the whole session after
554 * their creation. So this session destruction also destroys all channel and
555 * event structures specific to this session (they are not destroyed when their
556 * individual file is released).
557 */
558static
1ea11eab 559int lttng_release_session(int objd)
f4681817 560{
7dd08bec 561 struct lttng_session *session = objd_private(objd);
f4681817 562
1ea11eab 563 if (session) {
7dd08bec 564 lttng_session_destroy(session);
1ea11eab
MD
565 return 0;
566 } else {
567 return -EINVAL;
568 }
f4681817
MD
569}
570
b61ce3b2 571static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 572 .release = lttng_release_session,
f4681817
MD
573 .cmd = lttng_session_cmd,
574};
575
51489cad 576static
ef9ff354 577long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 578 union ust_args *uargs, void *owner)
51489cad 579{
c8fcf224 580 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
581 struct lttng_ust_tracepoint_iter *tp =
582 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 583 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
584
585 switch (cmd) {
586 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224 587 {
c8fcf224
MD
588 iter = lttng_ust_tracepoint_list_get_iter_next(list);
589 if (!iter)
7bc53e94 590 return -LTTNG_UST_ERR_NOENT;
c8fcf224 591 memcpy(tp, iter, sizeof(*tp));
51489cad 592 return 0;
c8fcf224 593 }
51489cad
MD
594 default:
595 return -EINVAL;
596 }
597}
598
599static
f59ed768 600int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
601{
602 int list_objd, ret;
c8fcf224 603 struct lttng_ust_tracepoint_list *list;
51489cad 604
7047f3da 605 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
606 if (list_objd < 0) {
607 ret = list_objd;
608 goto objd_error;
609 }
610 list = zmalloc(sizeof(*list));
611 if (!list) {
612 ret = -ENOMEM;
613 goto alloc_error;
614 }
615 objd_set_private(list_objd, list);
616
c8fcf224 617 /* populate list by walking on all registered probes. */
7dd08bec 618 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
619 if (ret) {
620 goto list_error;
621 }
51489cad
MD
622 return list_objd;
623
c8fcf224
MD
624list_error:
625 free(list);
51489cad
MD
626alloc_error:
627 {
628 int err;
629
1849ef7c 630 err = lttng_ust_objd_unref(list_objd, 1);
51489cad
MD
631 assert(!err);
632 }
633objd_error:
634 return ret;
635}
636
637static
638int lttng_release_tracepoint_list(int objd)
639{
c8fcf224 640 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
641
642 if (list) {
7dd08bec 643 lttng_probes_prune_event_list(list);
51489cad
MD
644 free(list);
645 return 0;
646 } else {
647 return -EINVAL;
648 }
649}
650
651static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
652 .release = lttng_release_tracepoint_list,
653 .cmd = lttng_tracepoint_list_cmd,
654};
655
06d4f27e
MD
656static
657long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
f59ed768 658 unsigned long arg, union ust_args *uargs, void *owner)
06d4f27e
MD
659{
660 struct lttng_ust_field_list *list = objd_private(objd);
40003310 661 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
662 struct lttng_ust_field_iter *iter;
663
664 switch (cmd) {
665 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
666 {
06d4f27e
MD
667 iter = lttng_ust_field_list_get_iter_next(list);
668 if (!iter)
7bc53e94 669 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
670 memcpy(tp, iter, sizeof(*tp));
671 return 0;
672 }
673 default:
674 return -EINVAL;
675 }
676}
677
678static
f59ed768 679int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
680{
681 int list_objd, ret;
682 struct lttng_ust_field_list *list;
683
7047f3da
MD
684 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
685 "tp_field_list");
06d4f27e
MD
686 if (list_objd < 0) {
687 ret = list_objd;
688 goto objd_error;
689 }
690 list = zmalloc(sizeof(*list));
691 if (!list) {
692 ret = -ENOMEM;
693 goto alloc_error;
694 }
695 objd_set_private(list_objd, list);
696
697 /* populate list by walking on all registered probes. */
7dd08bec 698 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
699 if (ret) {
700 goto list_error;
701 }
702 return list_objd;
703
704list_error:
705 free(list);
706alloc_error:
707 {
708 int err;
709
1849ef7c 710 err = lttng_ust_objd_unref(list_objd, 1);
06d4f27e
MD
711 assert(!err);
712 }
713objd_error:
714 return ret;
715}
716
717static
718int lttng_release_tracepoint_field_list(int objd)
719{
720 struct lttng_ust_field_list *list = objd_private(objd);
721
722 if (list) {
7dd08bec 723 lttng_probes_prune_field_list(list);
06d4f27e
MD
724 free(list);
725 return 0;
726 } else {
727 return -EINVAL;
728 }
729}
730
731static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
732 .release = lttng_release_tracepoint_field_list,
733 .cmd = lttng_tracepoint_field_list_cmd,
734};
735
f4681817 736static
74d81a6c 737int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
f59ed768 738 union ust_args *uargs, void *owner)
f4681817 739{
7dd08bec 740 struct lttng_channel *channel = objd_private(channel_objd);
74d81a6c 741 int ret;
f4681817 742
74d81a6c
MD
743 ret = channel_handle_add_stream(channel->handle,
744 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
745 info->stream_nr, info->len);
746 if (ret)
747 goto error_add_stream;
748
749 return 0;
750
751error_add_stream:
f4681817
MD
752 return ret;
753}
f4681817
MD
754
755static
e58095ef 756int lttng_abi_create_enabler(int channel_objd,
f59ed768 757 struct lttng_ust_event *event_param,
e58095ef
MD
758 void *owner,
759 enum lttng_enabler_type type)
f4681817 760{
7dd08bec 761 struct lttng_channel *channel = objd_private(channel_objd);
e58095ef 762 struct lttng_enabler *enabler;
f4681817
MD
763 int event_objd, ret;
764
765 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
7047f3da 766 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
f4681817
MD
767 if (event_objd < 0) {
768 ret = event_objd;
769 goto objd_error;
770 }
771 /*
772 * We tolerate no failure path after event creation. It will stay
773 * invariant for the rest of the session.
774 */
e58095ef
MD
775 enabler = lttng_enabler_create(type, event_param, channel);
776 if (!enabler) {
777 ret = -ENOMEM;
f4681817
MD
778 goto event_error;
779 }
e58095ef 780 objd_set_private(event_objd, enabler);
f4681817
MD
781 /* The event holds a reference on the channel */
782 objd_ref(channel_objd);
783 return event_objd;
784
785event_error:
1ea11eab
MD
786 {
787 int err;
788
1849ef7c 789 err = lttng_ust_objd_unref(event_objd, 1);
1ea11eab
MD
790 assert(!err);
791 }
f4681817
MD
792objd_error:
793 return ret;
794}
795
796/**
797 * lttng_channel_cmd - lttng control through object descriptors
798 *
799 * @objd: the object descriptor
800 * @cmd: the command
801 * @arg: command arg
ef9ff354 802 * @uargs: UST arguments (internal)
f59ed768 803 * @owner: objd owner
f4681817
MD
804 *
805 * This object descriptor implements lttng commands:
806 * LTTNG_UST_STREAM
807 * Returns an event stream object descriptor or failure.
808 * (typically, one event stream records events from one CPU)
809 * LTTNG_UST_EVENT
810 * Returns an event object descriptor or failure.
811 * LTTNG_UST_CONTEXT
812 * Prepend a context field to each event in the channel
813 * LTTNG_UST_ENABLE
814 * Enable recording for events in this channel (weak enable)
815 * LTTNG_UST_DISABLE
816 * Disable recording for events in this channel (strong disable)
817 *
818 * Channel and event file descriptors also hold a reference on the session.
819 */
820static
ef9ff354 821long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 822 union ust_args *uargs, void *owner)
f4681817 823{
7dd08bec 824 struct lttng_channel *channel = objd_private(objd);
f4681817 825
74d81a6c
MD
826 if (cmd != LTTNG_UST_STREAM) {
827 /*
828 * Check if channel received all streams.
829 */
830 if (!lttng_is_channel_ready(channel))
831 return -EPERM;
832 }
833
f4681817
MD
834 switch (cmd) {
835 case LTTNG_UST_STREAM:
381c0f1e
MD
836 {
837 struct lttng_ust_stream *stream;
838
839 stream = (struct lttng_ust_stream *) arg;
840 /* stream used as output */
74d81a6c 841 return lttng_abi_map_stream(objd, stream, uargs, owner);
381c0f1e 842 }
f4681817 843 case LTTNG_UST_EVENT:
1f18504e
MD
844 {
845 struct lttng_ust_event *event_param =
846 (struct lttng_ust_event *) arg;
6b0e60f1
MD
847 if (event_param->name[strlen(event_param->name) - 1] == '*') {
848 /* If ends with wildcard, create wildcard. */
e58095ef
MD
849 return lttng_abi_create_enabler(objd, event_param,
850 owner, LTTNG_ENABLER_WILDCARD);
1f18504e 851 } else {
e58095ef
MD
852 return lttng_abi_create_enabler(objd, event_param,
853 owner, LTTNG_ENABLER_EVENT);
1f18504e
MD
854 }
855 }
f4681817
MD
856 case LTTNG_UST_CONTEXT:
857 return lttng_abi_add_context(objd,
858 (struct lttng_ust_context *) arg,
859 &channel->ctx, channel->session);
860 case LTTNG_UST_ENABLE:
7dd08bec 861 return lttng_channel_enable(channel);
f4681817 862 case LTTNG_UST_DISABLE:
7dd08bec 863 return lttng_channel_disable(channel);
43d330a4
MD
864 case LTTNG_UST_FLUSH_BUFFER:
865 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
866 default:
867 return -EINVAL;
868 }
869}
870
f4681817
MD
871static
872int lttng_channel_release(int objd)
873{
7dd08bec 874 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
875
876 if (channel)
1849ef7c 877 return lttng_ust_objd_unref(channel->session->objd, 0);
f4681817
MD
878 return 0;
879}
880
b61ce3b2 881static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817 882 .release = lttng_channel_release,
f4681817
MD
883 .cmd = lttng_channel_cmd,
884};
885
f4681817 886/**
e58095ef 887 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
888 *
889 * @objd: the object descriptor
890 * @cmd: the command
891 * @arg: command arg
ef9ff354 892 * @uargs: UST arguments (internal)
f59ed768 893 * @owner: objd owner
e6c12e3d
MD
894 *
895 * This object descriptor implements lttng commands:
896 * LTTNG_UST_CONTEXT
897 * Prepend a context field to each record of events of this
e58095ef 898 * enabler.
e6c12e3d 899 * LTTNG_UST_ENABLE
e58095ef 900 * Enable recording for this enabler
e6c12e3d 901 * LTTNG_UST_DISABLE
e58095ef 902 * Disable recording for this enabler
2d78951a 903 * LTTNG_UST_FILTER
e58095ef 904 * Attach a filter to an enabler.
e6c12e3d
MD
905 */
906static
e58095ef 907long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 908 union ust_args *uargs, void *owner)
e6c12e3d 909{
e58095ef 910 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d
MD
911
912 switch (cmd) {
913 case LTTNG_UST_CONTEXT:
e58095ef
MD
914 return lttng_enabler_attach_context(enabler,
915 (struct lttng_ust_context *) arg);
e6c12e3d 916 case LTTNG_UST_ENABLE:
e58095ef 917 return lttng_enabler_enable(enabler);
e6c12e3d 918 case LTTNG_UST_DISABLE:
e58095ef 919 return lttng_enabler_disable(enabler);
2d78951a
MD
920 case LTTNG_UST_FILTER:
921 {
922 int ret;
923
e58095ef 924 ret = lttng_enabler_attach_bytecode(enabler,
f488575f 925 (struct lttng_ust_filter_bytecode_node *) arg);
2d78951a
MD
926 if (ret)
927 return ret;
2d78951a
MD
928 return 0;
929 }
e6c12e3d
MD
930 default:
931 return -EINVAL;
932 }
933}
934
935static
e58095ef 936int lttng_enabler_release(int objd)
e6c12e3d 937{
e58095ef 938 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d 939
e58095ef 940 if (enabler)
1849ef7c 941 return lttng_ust_objd_unref(enabler->chan->objd, 0);
e6c12e3d
MD
942 return 0;
943}
944
e58095ef
MD
945static const struct lttng_ust_objd_ops lttng_enabler_ops = {
946 .release = lttng_enabler_release,
947 .cmd = lttng_enabler_cmd,
e6c12e3d
MD
948};
949
b35d179d 950void lttng_ust_abi_exit(void)
f4681817 951{
45e9e699 952 lttng_ust_abi_close_in_progress = 1;
f4681817 953 objd_table_destroy();
45e9e699 954 lttng_ust_abi_close_in_progress = 0;
f4681817 955}
This page took 0.07577 seconds and 4 git commands to generate.