Fix: ensure all probe providers have their symbols
[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;
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
b61ce3b2
MD
81struct lttng_ust_objd_table {
82 struct lttng_ust_obj *array;
f4681817
MD
83 unsigned int len, allocated_len;
84 int freelist_head; /* offset freelist head. end is -1 */
85};
86
b61ce3b2 87static struct lttng_ust_objd_table objd_table = {
f4681817
MD
88 .freelist_head = -1,
89};
90
91static
f59ed768 92int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
7047f3da 93 void *owner, const char *name)
f4681817 94{
b61ce3b2 95 struct lttng_ust_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;
b61ce3b2 105 struct lttng_ust_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;
b61ce3b2 113 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
114 if (!new_table)
115 return -ENOMEM;
116 memcpy(new_table, old_table,
b61ce3b2 117 sizeof(struct lttng_ust_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 */
f59ed768 129 obj->u.s.owner = owner;
7047f3da
MD
130 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
131 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
f4681817
MD
132 return obj - objd_table.array;
133}
134
135static
b61ce3b2 136struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
137{
138 if (id >= objd_table.len)
139 return NULL;
a4be8962
MD
140 if (!objd_table.array[id].u.s.f_count)
141 return NULL;
f4681817
MD
142 return &objd_table.array[id];
143}
144
145static
146void *objd_private(int id)
147{
b61ce3b2 148 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
149 assert(obj);
150 return obj->u.s.private_data;
151}
152
153static
154void objd_set_private(int id, void *private_data)
155{
b61ce3b2 156 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
157 assert(obj);
158 obj->u.s.private_data = private_data;
159}
160
b61ce3b2 161const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 162{
b61ce3b2 163 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 164
a4be8962
MD
165 if (!obj)
166 return NULL;
f4681817
MD
167 return obj->u.s.ops;
168}
169
170static
171void objd_free(int id)
172{
b61ce3b2 173 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
174
175 assert(obj);
176 obj->u.freelist_next = objd_table.freelist_head;
177 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
178 assert(obj->u.s.f_count == 1);
179 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
180}
181
182static
183void objd_ref(int id)
184{
b61ce3b2 185 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
186 obj->u.s.f_count++;
187}
188
d4419b81 189int lttng_ust_objd_unref(int id)
f4681817 190{
b61ce3b2 191 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 192
1ea11eab
MD
193 if (!obj)
194 return -EINVAL;
a4be8962
MD
195 if (obj->u.s.f_count == 1) {
196 ERR("Reference counting error\n");
197 return -EINVAL;
198 }
199 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 200 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 201
f4681817
MD
202 if (ops->release)
203 ops->release(id);
204 objd_free(id);
205 }
1ea11eab 206 return 0;
f4681817
MD
207}
208
209static
210void objd_table_destroy(void)
211{
a4be8962
MD
212 int i;
213
fb50c39d 214 for (i = 0; i < objd_table.allocated_len; i++)
d4419b81 215 (void) lttng_ust_objd_unref(i);
f4681817 216 free(objd_table.array);
02fb3381
MD
217 objd_table.array = NULL;
218 objd_table.len = 0;
219 objd_table.allocated_len = 0;
17dfb34b 220 objd_table.freelist_head = -1;
f4681817
MD
221}
222
74d81a6c
MD
223const char *lttng_ust_obj_get_name(int id)
224{
225 struct lttng_ust_obj *obj = _objd_get(id);
226
227 if (!obj)
228 return NULL;
229 return obj->u.s.name;
230}
231
f59ed768
MD
232void lttng_ust_objd_table_owner_cleanup(void *owner)
233{
234 int i;
235
236 for (i = 0; i < objd_table.allocated_len; i++) {
237 struct lttng_ust_obj *obj;
238
239 obj = _objd_get(i);
240 if (!obj)
241 continue;
242 if (!obj->u.s.owner)
243 continue; /* skip root handles */
244 if (obj->u.s.owner == owner)
245 (void) lttng_ust_objd_unref(i);
246 }
247}
248
f4681817
MD
249/*
250 * This is LTTng's own personal way to create an ABI for sessiond.
251 * We send commands over a socket.
252 */
253
b61ce3b2
MD
254static const struct lttng_ust_objd_ops lttng_ops;
255static const struct lttng_ust_objd_ops lttng_session_ops;
256static const struct lttng_ust_objd_ops lttng_channel_ops;
e58095ef 257static const struct lttng_ust_objd_ops lttng_enabler_ops;
51489cad 258static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 259static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817 260
46050b1a
MD
261int lttng_abi_create_root_handle(void)
262{
263 int root_handle;
264
f59ed768 265 /* root handles have NULL owners */
7047f3da 266 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
46050b1a
MD
267 return root_handle;
268}
269
74d81a6c
MD
270static
271int lttng_is_channel_ready(struct lttng_channel *lttng_chan)
272{
273 struct channel *chan;
274 unsigned int nr_streams, exp_streams;
275
276 chan = lttng_chan->chan;
277 nr_streams = channel_handle_get_nr_streams(lttng_chan->handle);
278 exp_streams = chan->nr_streams;
279 return nr_streams == exp_streams;
280}
281
818173b9 282static
f59ed768 283int lttng_abi_create_session(void *owner)
f4681817 284{
7dd08bec 285 struct lttng_session *session;
f4681817
MD
286 int session_objd, ret;
287
7dd08bec 288 session = lttng_session_create();
f4681817
MD
289 if (!session)
290 return -ENOMEM;
7047f3da 291 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
f4681817
MD
292 if (session_objd < 0) {
293 ret = session_objd;
294 goto objd_error;
295 }
296 session->objd = session_objd;
32ce8569 297 session->owner = owner;
f4681817
MD
298 return session_objd;
299
300objd_error:
7dd08bec 301 lttng_session_destroy(session);
f4681817
MD
302 return ret;
303}
304
f4681817
MD
305static
306long lttng_abi_tracer_version(int objd,
307 struct lttng_ust_tracer_version *v)
308{
32ce8569
MD
309 v->major = LTTNG_UST_MAJOR_VERSION;
310 v->minor = LTTNG_UST_MINOR_VERSION;
311 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
312 return 0;
313}
314
315static
316long lttng_abi_add_context(int objd,
317 struct lttng_ust_context *context_param,
7dd08bec 318 struct lttng_ctx **ctx, struct lttng_session *session)
f4681817 319{
e58095ef 320 return lttng_attach_context(context_param, ctx, session);
f4681817
MD
321}
322
323/**
324 * lttng_cmd - lttng control through socket commands
325 *
326 * @objd: the object descriptor
327 * @cmd: the command
328 * @arg: command arg
ef9ff354 329 * @uargs: UST arguments (internal)
f59ed768 330 * @owner: objd owner
f4681817
MD
331 *
332 * This descriptor implements lttng commands:
333 * LTTNG_UST_SESSION
334 * Returns a LTTng trace session object descriptor
335 * LTTNG_UST_TRACER_VERSION
336 * Returns the LTTng kernel tracer version
337 * LTTNG_UST_TRACEPOINT_LIST
338 * Returns a file descriptor listing available tracepoints
06d4f27e
MD
339 * LTTNG_UST_TRACEPOINT_FIELD_LIST
340 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
341 * LTTNG_UST_WAIT_QUIESCENT
342 * Returns after all previously running probes have completed
343 *
344 * The returned session will be deleted when its file descriptor is closed.
345 */
346static
ef9ff354 347long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 348 union ust_args *uargs, void *owner)
f4681817
MD
349{
350 switch (cmd) {
351 case LTTNG_UST_SESSION:
f59ed768 352 return lttng_abi_create_session(owner);
f4681817
MD
353 case LTTNG_UST_TRACER_VERSION:
354 return lttng_abi_tracer_version(objd,
355 (struct lttng_ust_tracer_version *) arg);
356 case LTTNG_UST_TRACEPOINT_LIST:
f59ed768 357 return lttng_abi_tracepoint_list(owner);
06d4f27e 358 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
f59ed768 359 return lttng_abi_tracepoint_field_list(owner);
f4681817
MD
360 case LTTNG_UST_WAIT_QUIESCENT:
361 synchronize_trace();
362 return 0;
363 default:
364 return -EINVAL;
365 }
366}
367
b61ce3b2 368static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
369 .cmd = lttng_cmd,
370};
371
74d81a6c
MD
372int lttng_abi_map_channel(int session_objd,
373 struct lttng_ust_channel *ust_chan,
374 union ust_args *uargs,
375 void *owner)
f4681817 376{
7dd08bec 377 struct lttng_session *session = objd_private(session_objd);
f4681817 378 const char *transport_name;
74d81a6c
MD
379 const struct lttng_transport *transport;
380 const char *chan_name;
f4681817 381 int chan_objd;
74d81a6c
MD
382 struct lttng_ust_shm_handle *channel_handle;
383 struct lttng_channel *lttng_chan;
384 struct channel *chan;
385 struct lttng_ust_lib_ring_buffer_config *config;
386 void *chan_data;
387 uint64_t len;
388 int ret;
389 enum lttng_ust_chan_type type;
390
391 chan_data = uargs->channel.chan_data;
392 len = ust_chan->len;
393 type = ust_chan->type;
394
395 switch (type) {
396 case LTTNG_UST_CHAN_PER_CPU:
74d81a6c
MD
397 break;
398 default:
399 return -EINVAL;
400 }
401
402 if (session->been_active) {
403 ret = -EBUSY;
404 goto active; /* Refuse to add channel to active session */
405 }
f4681817 406
74d81a6c
MD
407 channel_handle = channel_handle_create(chan_data, len);
408 if (!channel_handle) {
409 ret = -EINVAL;
410 goto handle_error;
411 }
412
413 chan = shmp(channel_handle, channel_handle->chan);
414 assert(chan);
415 config = &chan->backend.config;
416 lttng_chan = channel_get_private(chan);
417 if (!lttng_chan) {
418 ret = -EINVAL;
419 goto alloc_error;
420 }
421
422 /* Lookup transport name */
423 switch (type) {
424 case LTTNG_UST_CHAN_PER_CPU:
425 if (config->output == RING_BUFFER_MMAP) {
426 transport_name = config->mode == RING_BUFFER_OVERWRITE ?
f4681817
MD
427 "relay-overwrite-mmap" : "relay-discard-mmap";
428 } else {
74d81a6c
MD
429 ret = -EINVAL;
430 goto notransport;
f4681817 431 }
74d81a6c 432 chan_name = "channel";
f4681817 433 break;
f4681817
MD
434 default:
435 transport_name = "<unknown>";
74d81a6c
MD
436 chan_name = "<unknown>";
437 ret = -EINVAL;
438 goto notransport;
fe38d4af 439 }
74d81a6c
MD
440 transport = lttng_transport_find(transport_name);
441 if (!transport) {
442 DBG("LTTng transport %s not found\n",
443 transport_name);
444 ret = -EINVAL;
445 goto notransport;
446 }
447
448 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
fe38d4af
MD
449 if (chan_objd < 0) {
450 ret = chan_objd;
451 goto objd_error;
f4681817 452 }
74d81a6c
MD
453
454 /* Initialize our lttng chan */
455 lttng_chan->chan = chan;
ac6b4ac6 456 lttng_chan->tstate = 1;
74d81a6c
MD
457 lttng_chan->enabled = 1;
458 lttng_chan->ctx = NULL;
459 lttng_chan->session = session;
74d81a6c
MD
460 lttng_chan->ops = &transport->ops;
461 memcpy(&lttng_chan->chan->backend.config,
462 transport->client_config,
463 sizeof(lttng_chan->chan->backend.config));
464 cds_list_add(&lttng_chan->node, &session->chan_head);
465 lttng_chan->header_type = 0;
466 lttng_chan->handle = channel_handle;
74d81a6c
MD
467 lttng_chan->type = type;
468
f4681817
MD
469 /*
470 * We tolerate no failure path after channel creation. It will stay
471 * invariant for the rest of the session.
472 */
74d81a6c
MD
473 objd_set_private(chan_objd, lttng_chan);
474 lttng_chan->objd = chan_objd;
f4681817
MD
475 /* The channel created holds a reference on the session */
476 objd_ref(session_objd);
f4681817
MD
477 return chan_objd;
478
f4681817 479objd_error:
74d81a6c
MD
480notransport:
481 free(lttng_chan);
482alloc_error:
483 channel_destroy(chan, channel_handle, 0);
484handle_error:
485active:
f4681817
MD
486 return ret;
487}
488
489/**
490 * lttng_session_cmd - lttng session object command
491 *
492 * @obj: the object
493 * @cmd: the command
494 * @arg: command arg
ef9ff354 495 * @uargs: UST arguments (internal)
f59ed768 496 * @owner: objd owner
f4681817
MD
497 *
498 * This descriptor implements lttng commands:
499 * LTTNG_UST_CHANNEL
500 * Returns a LTTng channel object descriptor
501 * LTTNG_UST_ENABLE
502 * Enables tracing for a session (weak enable)
503 * LTTNG_UST_DISABLE
504 * Disables tracing for a session (strong disable)
f4681817
MD
505 *
506 * The returned channel will be deleted when its file descriptor is closed.
507 */
508static
ef9ff354 509long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 510 union ust_args *uargs, void *owner)
f4681817 511{
7dd08bec 512 struct lttng_session *session = objd_private(objd);
f4681817
MD
513
514 switch (cmd) {
515 case LTTNG_UST_CHANNEL:
74d81a6c 516 return lttng_abi_map_channel(objd,
f4681817 517 (struct lttng_ust_channel *) arg,
74d81a6c 518 uargs, owner);
f4681817
MD
519 case LTTNG_UST_SESSION_START:
520 case LTTNG_UST_ENABLE:
7dd08bec 521 return lttng_session_enable(session);
f4681817
MD
522 case LTTNG_UST_SESSION_STOP:
523 case LTTNG_UST_DISABLE:
7dd08bec 524 return lttng_session_disable(session);
f4681817
MD
525 default:
526 return -EINVAL;
527 }
528}
529
530/*
531 * Called when the last file reference is dropped.
532 *
533 * Big fat note: channels and events are invariant for the whole session after
534 * their creation. So this session destruction also destroys all channel and
535 * event structures specific to this session (they are not destroyed when their
536 * individual file is released).
537 */
538static
1ea11eab 539int lttng_release_session(int objd)
f4681817 540{
7dd08bec 541 struct lttng_session *session = objd_private(objd);
f4681817 542
1ea11eab 543 if (session) {
7dd08bec 544 lttng_session_destroy(session);
1ea11eab
MD
545 return 0;
546 } else {
547 return -EINVAL;
548 }
f4681817
MD
549}
550
b61ce3b2 551static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 552 .release = lttng_release_session,
f4681817
MD
553 .cmd = lttng_session_cmd,
554};
555
51489cad 556static
ef9ff354 557long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 558 union ust_args *uargs, void *owner)
51489cad 559{
c8fcf224 560 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
561 struct lttng_ust_tracepoint_iter *tp =
562 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 563 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
564
565 switch (cmd) {
566 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224 567 {
c8fcf224
MD
568 iter = lttng_ust_tracepoint_list_get_iter_next(list);
569 if (!iter)
7bc53e94 570 return -LTTNG_UST_ERR_NOENT;
c8fcf224 571 memcpy(tp, iter, sizeof(*tp));
51489cad 572 return 0;
c8fcf224 573 }
51489cad
MD
574 default:
575 return -EINVAL;
576 }
577}
578
579static
f59ed768 580int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
581{
582 int list_objd, ret;
c8fcf224 583 struct lttng_ust_tracepoint_list *list;
51489cad 584
7047f3da 585 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
586 if (list_objd < 0) {
587 ret = list_objd;
588 goto objd_error;
589 }
590 list = zmalloc(sizeof(*list));
591 if (!list) {
592 ret = -ENOMEM;
593 goto alloc_error;
594 }
595 objd_set_private(list_objd, list);
596
c8fcf224 597 /* populate list by walking on all registered probes. */
7dd08bec 598 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
599 if (ret) {
600 goto list_error;
601 }
51489cad
MD
602 return list_objd;
603
c8fcf224
MD
604list_error:
605 free(list);
51489cad
MD
606alloc_error:
607 {
608 int err;
609
610 err = lttng_ust_objd_unref(list_objd);
611 assert(!err);
612 }
613objd_error:
614 return ret;
615}
616
617static
618int lttng_release_tracepoint_list(int objd)
619{
c8fcf224 620 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
621
622 if (list) {
7dd08bec 623 lttng_probes_prune_event_list(list);
51489cad
MD
624 free(list);
625 return 0;
626 } else {
627 return -EINVAL;
628 }
629}
630
631static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
632 .release = lttng_release_tracepoint_list,
633 .cmd = lttng_tracepoint_list_cmd,
634};
635
06d4f27e
MD
636static
637long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
f59ed768 638 unsigned long arg, union ust_args *uargs, void *owner)
06d4f27e
MD
639{
640 struct lttng_ust_field_list *list = objd_private(objd);
40003310 641 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
642 struct lttng_ust_field_iter *iter;
643
644 switch (cmd) {
645 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
646 {
06d4f27e
MD
647 iter = lttng_ust_field_list_get_iter_next(list);
648 if (!iter)
7bc53e94 649 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
650 memcpy(tp, iter, sizeof(*tp));
651 return 0;
652 }
653 default:
654 return -EINVAL;
655 }
656}
657
658static
f59ed768 659int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
660{
661 int list_objd, ret;
662 struct lttng_ust_field_list *list;
663
7047f3da
MD
664 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
665 "tp_field_list");
06d4f27e
MD
666 if (list_objd < 0) {
667 ret = list_objd;
668 goto objd_error;
669 }
670 list = zmalloc(sizeof(*list));
671 if (!list) {
672 ret = -ENOMEM;
673 goto alloc_error;
674 }
675 objd_set_private(list_objd, list);
676
677 /* populate list by walking on all registered probes. */
7dd08bec 678 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
679 if (ret) {
680 goto list_error;
681 }
682 return list_objd;
683
684list_error:
685 free(list);
686alloc_error:
687 {
688 int err;
689
690 err = lttng_ust_objd_unref(list_objd);
691 assert(!err);
692 }
693objd_error:
694 return ret;
695}
696
697static
698int lttng_release_tracepoint_field_list(int objd)
699{
700 struct lttng_ust_field_list *list = objd_private(objd);
701
702 if (list) {
7dd08bec 703 lttng_probes_prune_field_list(list);
06d4f27e
MD
704 free(list);
705 return 0;
706 } else {
707 return -EINVAL;
708 }
709}
710
711static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
712 .release = lttng_release_tracepoint_field_list,
713 .cmd = lttng_tracepoint_field_list_cmd,
714};
715
f4681817 716static
74d81a6c 717int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
f59ed768 718 union ust_args *uargs, void *owner)
f4681817 719{
7dd08bec 720 struct lttng_channel *channel = objd_private(channel_objd);
74d81a6c 721 int ret;
f4681817 722
74d81a6c
MD
723 ret = channel_handle_add_stream(channel->handle,
724 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
725 info->stream_nr, info->len);
726 if (ret)
727 goto error_add_stream;
728
729 return 0;
730
731error_add_stream:
f4681817
MD
732 return ret;
733}
f4681817
MD
734
735static
e58095ef 736int lttng_abi_create_enabler(int channel_objd,
f59ed768 737 struct lttng_ust_event *event_param,
e58095ef
MD
738 void *owner,
739 enum lttng_enabler_type type)
f4681817 740{
7dd08bec 741 struct lttng_channel *channel = objd_private(channel_objd);
e58095ef 742 struct lttng_enabler *enabler;
f4681817
MD
743 int event_objd, ret;
744
745 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
7047f3da 746 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
f4681817
MD
747 if (event_objd < 0) {
748 ret = event_objd;
749 goto objd_error;
750 }
751 /*
752 * We tolerate no failure path after event creation. It will stay
753 * invariant for the rest of the session.
754 */
e58095ef
MD
755 enabler = lttng_enabler_create(type, event_param, channel);
756 if (!enabler) {
757 ret = -ENOMEM;
f4681817
MD
758 goto event_error;
759 }
e58095ef 760 objd_set_private(event_objd, enabler);
f4681817
MD
761 /* The event holds a reference on the channel */
762 objd_ref(channel_objd);
763 return event_objd;
764
765event_error:
1ea11eab
MD
766 {
767 int err;
768
d4419b81 769 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
770 assert(!err);
771 }
f4681817
MD
772objd_error:
773 return ret;
774}
775
776/**
777 * lttng_channel_cmd - lttng control through object descriptors
778 *
779 * @objd: the object descriptor
780 * @cmd: the command
781 * @arg: command arg
ef9ff354 782 * @uargs: UST arguments (internal)
f59ed768 783 * @owner: objd owner
f4681817
MD
784 *
785 * This object descriptor implements lttng commands:
786 * LTTNG_UST_STREAM
787 * Returns an event stream object descriptor or failure.
788 * (typically, one event stream records events from one CPU)
789 * LTTNG_UST_EVENT
790 * Returns an event object descriptor or failure.
791 * LTTNG_UST_CONTEXT
792 * Prepend a context field to each event in the channel
793 * LTTNG_UST_ENABLE
794 * Enable recording for events in this channel (weak enable)
795 * LTTNG_UST_DISABLE
796 * Disable recording for events in this channel (strong disable)
797 *
798 * Channel and event file descriptors also hold a reference on the session.
799 */
800static
ef9ff354 801long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 802 union ust_args *uargs, void *owner)
f4681817 803{
7dd08bec 804 struct lttng_channel *channel = objd_private(objd);
f4681817 805
74d81a6c
MD
806 if (cmd != LTTNG_UST_STREAM) {
807 /*
808 * Check if channel received all streams.
809 */
810 if (!lttng_is_channel_ready(channel))
811 return -EPERM;
812 }
813
f4681817
MD
814 switch (cmd) {
815 case LTTNG_UST_STREAM:
381c0f1e
MD
816 {
817 struct lttng_ust_stream *stream;
818
819 stream = (struct lttng_ust_stream *) arg;
820 /* stream used as output */
74d81a6c 821 return lttng_abi_map_stream(objd, stream, uargs, owner);
381c0f1e 822 }
f4681817 823 case LTTNG_UST_EVENT:
1f18504e
MD
824 {
825 struct lttng_ust_event *event_param =
826 (struct lttng_ust_event *) arg;
6b0e60f1
MD
827 if (event_param->name[strlen(event_param->name) - 1] == '*') {
828 /* If ends with wildcard, create wildcard. */
e58095ef
MD
829 return lttng_abi_create_enabler(objd, event_param,
830 owner, LTTNG_ENABLER_WILDCARD);
1f18504e 831 } else {
e58095ef
MD
832 return lttng_abi_create_enabler(objd, event_param,
833 owner, LTTNG_ENABLER_EVENT);
1f18504e
MD
834 }
835 }
f4681817
MD
836 case LTTNG_UST_CONTEXT:
837 return lttng_abi_add_context(objd,
838 (struct lttng_ust_context *) arg,
839 &channel->ctx, channel->session);
840 case LTTNG_UST_ENABLE:
7dd08bec 841 return lttng_channel_enable(channel);
f4681817 842 case LTTNG_UST_DISABLE:
7dd08bec 843 return lttng_channel_disable(channel);
43d330a4
MD
844 case LTTNG_UST_FLUSH_BUFFER:
845 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
846 default:
847 return -EINVAL;
848 }
849}
850
f4681817
MD
851static
852int lttng_channel_release(int objd)
853{
7dd08bec 854 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
855
856 if (channel)
d4419b81 857 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
858 return 0;
859}
860
b61ce3b2 861static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817 862 .release = lttng_channel_release,
f4681817
MD
863 .cmd = lttng_channel_cmd,
864};
865
f4681817 866/**
e58095ef 867 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
868 *
869 * @objd: the object descriptor
870 * @cmd: the command
871 * @arg: command arg
ef9ff354 872 * @uargs: UST arguments (internal)
f59ed768 873 * @owner: objd owner
e6c12e3d
MD
874 *
875 * This object descriptor implements lttng commands:
876 * LTTNG_UST_CONTEXT
877 * Prepend a context field to each record of events of this
e58095ef 878 * enabler.
e6c12e3d 879 * LTTNG_UST_ENABLE
e58095ef 880 * Enable recording for this enabler
e6c12e3d 881 * LTTNG_UST_DISABLE
e58095ef 882 * Disable recording for this enabler
2d78951a 883 * LTTNG_UST_FILTER
e58095ef 884 * Attach a filter to an enabler.
e6c12e3d
MD
885 */
886static
e58095ef 887long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 888 union ust_args *uargs, void *owner)
e6c12e3d 889{
e58095ef 890 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d
MD
891
892 switch (cmd) {
893 case LTTNG_UST_CONTEXT:
e58095ef
MD
894 return lttng_enabler_attach_context(enabler,
895 (struct lttng_ust_context *) arg);
e6c12e3d 896 case LTTNG_UST_ENABLE:
e58095ef 897 return lttng_enabler_enable(enabler);
e6c12e3d 898 case LTTNG_UST_DISABLE:
e58095ef 899 return lttng_enabler_disable(enabler);
2d78951a
MD
900 case LTTNG_UST_FILTER:
901 {
902 int ret;
903
e58095ef 904 ret = lttng_enabler_attach_bytecode(enabler,
f488575f 905 (struct lttng_ust_filter_bytecode_node *) arg);
2d78951a
MD
906 if (ret)
907 return ret;
2d78951a
MD
908 return 0;
909 }
e6c12e3d
MD
910 default:
911 return -EINVAL;
912 }
913}
914
915static
e58095ef 916int lttng_enabler_release(int objd)
e6c12e3d 917{
e58095ef 918 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d 919
e58095ef
MD
920 if (enabler)
921 return lttng_ust_objd_unref(enabler->chan->objd);
e6c12e3d
MD
922 return 0;
923}
924
e58095ef
MD
925static const struct lttng_ust_objd_ops lttng_enabler_ops = {
926 .release = lttng_enabler_release,
927 .cmd = lttng_enabler_cmd,
e6c12e3d
MD
928};
929
b35d179d 930void lttng_ust_abi_exit(void)
f4681817 931{
45e9e699 932 lttng_ust_abi_close_in_progress = 1;
f4681817 933 objd_table_destroy();
45e9e699 934 lttng_ust_abi_close_in_progress = 0;
f4681817 935}
This page took 0.072253 seconds and 4 git commands to generate.