Only flush when there are readers active
[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;
ff0f5728 406 int wakeup_fd;
74d81a6c
MD
407 uint64_t len;
408 int ret;
409 enum lttng_ust_chan_type type;
410
411 chan_data = uargs->channel.chan_data;
ff0f5728 412 wakeup_fd = uargs->channel.wakeup_fd;
74d81a6c
MD
413 len = ust_chan->len;
414 type = ust_chan->type;
415
416 switch (type) {
417 case LTTNG_UST_CHAN_PER_CPU:
74d81a6c
MD
418 break;
419 default:
ff0f5728
MD
420 ret = -EINVAL;
421 goto invalid;
74d81a6c
MD
422 }
423
424 if (session->been_active) {
425 ret = -EBUSY;
426 goto active; /* Refuse to add channel to active session */
427 }
f4681817 428
ff0f5728 429 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
74d81a6c
MD
430 if (!channel_handle) {
431 ret = -EINVAL;
432 goto handle_error;
433 }
434
435 chan = shmp(channel_handle, channel_handle->chan);
436 assert(chan);
03d2d293 437 chan->handle = channel_handle;
74d81a6c
MD
438 config = &chan->backend.config;
439 lttng_chan = channel_get_private(chan);
440 if (!lttng_chan) {
441 ret = -EINVAL;
442 goto alloc_error;
443 }
444
445 /* Lookup transport name */
446 switch (type) {
447 case LTTNG_UST_CHAN_PER_CPU:
448 if (config->output == RING_BUFFER_MMAP) {
449 transport_name = config->mode == RING_BUFFER_OVERWRITE ?
f4681817
MD
450 "relay-overwrite-mmap" : "relay-discard-mmap";
451 } else {
74d81a6c
MD
452 ret = -EINVAL;
453 goto notransport;
f4681817 454 }
74d81a6c 455 chan_name = "channel";
f4681817 456 break;
f4681817
MD
457 default:
458 transport_name = "<unknown>";
74d81a6c
MD
459 chan_name = "<unknown>";
460 ret = -EINVAL;
461 goto notransport;
fe38d4af 462 }
74d81a6c
MD
463 transport = lttng_transport_find(transport_name);
464 if (!transport) {
465 DBG("LTTng transport %s not found\n",
466 transport_name);
467 ret = -EINVAL;
468 goto notransport;
469 }
470
471 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
fe38d4af
MD
472 if (chan_objd < 0) {
473 ret = chan_objd;
474 goto objd_error;
f4681817 475 }
74d81a6c
MD
476
477 /* Initialize our lttng chan */
478 lttng_chan->chan = chan;
ac6b4ac6 479 lttng_chan->tstate = 1;
74d81a6c
MD
480 lttng_chan->enabled = 1;
481 lttng_chan->ctx = NULL;
482 lttng_chan->session = session;
74d81a6c
MD
483 lttng_chan->ops = &transport->ops;
484 memcpy(&lttng_chan->chan->backend.config,
485 transport->client_config,
486 sizeof(lttng_chan->chan->backend.config));
487 cds_list_add(&lttng_chan->node, &session->chan_head);
488 lttng_chan->header_type = 0;
489 lttng_chan->handle = channel_handle;
74d81a6c
MD
490 lttng_chan->type = type;
491
f4681817
MD
492 /*
493 * We tolerate no failure path after channel creation. It will stay
494 * invariant for the rest of the session.
495 */
74d81a6c
MD
496 objd_set_private(chan_objd, lttng_chan);
497 lttng_chan->objd = chan_objd;
f4681817
MD
498 /* The channel created holds a reference on the session */
499 objd_ref(session_objd);
f4681817
MD
500 return chan_objd;
501
ff0f5728 502 /* error path after channel was created */
f4681817 503objd_error:
74d81a6c
MD
504notransport:
505 free(lttng_chan);
506alloc_error:
507 channel_destroy(chan, channel_handle, 0);
ff0f5728
MD
508 return ret;
509
510 /*
511 * error path before channel creation (owning chan_data and
512 * wakeup_fd).
513 */
74d81a6c
MD
514handle_error:
515active:
ff0f5728
MD
516invalid:
517 {
518 int close_ret;
519
520 close_ret = close(wakeup_fd);
521 if (close_ret) {
522 PERROR("close");
523 }
524 }
525 free(chan_data);
f4681817
MD
526 return ret;
527}
528
529/**
530 * lttng_session_cmd - lttng session object command
531 *
532 * @obj: the object
533 * @cmd: the command
534 * @arg: command arg
ef9ff354 535 * @uargs: UST arguments (internal)
f59ed768 536 * @owner: objd owner
f4681817
MD
537 *
538 * This descriptor implements lttng commands:
539 * LTTNG_UST_CHANNEL
540 * Returns a LTTng channel object descriptor
541 * LTTNG_UST_ENABLE
542 * Enables tracing for a session (weak enable)
543 * LTTNG_UST_DISABLE
544 * Disables tracing for a session (strong disable)
f4681817
MD
545 *
546 * The returned channel will be deleted when its file descriptor is closed.
547 */
548static
ef9ff354 549long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 550 union ust_args *uargs, void *owner)
f4681817 551{
7dd08bec 552 struct lttng_session *session = objd_private(objd);
f4681817
MD
553
554 switch (cmd) {
555 case LTTNG_UST_CHANNEL:
74d81a6c 556 return lttng_abi_map_channel(objd,
f4681817 557 (struct lttng_ust_channel *) arg,
74d81a6c 558 uargs, owner);
f4681817
MD
559 case LTTNG_UST_SESSION_START:
560 case LTTNG_UST_ENABLE:
7dd08bec 561 return lttng_session_enable(session);
f4681817
MD
562 case LTTNG_UST_SESSION_STOP:
563 case LTTNG_UST_DISABLE:
7dd08bec 564 return lttng_session_disable(session);
f4681817
MD
565 default:
566 return -EINVAL;
567 }
568}
569
570/*
571 * Called when the last file reference is dropped.
572 *
573 * Big fat note: channels and events are invariant for the whole session after
574 * their creation. So this session destruction also destroys all channel and
575 * event structures specific to this session (they are not destroyed when their
576 * individual file is released).
577 */
578static
1ea11eab 579int lttng_release_session(int objd)
f4681817 580{
7dd08bec 581 struct lttng_session *session = objd_private(objd);
f4681817 582
1ea11eab 583 if (session) {
7dd08bec 584 lttng_session_destroy(session);
1ea11eab
MD
585 return 0;
586 } else {
587 return -EINVAL;
588 }
f4681817
MD
589}
590
b61ce3b2 591static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 592 .release = lttng_release_session,
f4681817
MD
593 .cmd = lttng_session_cmd,
594};
595
51489cad 596static
ef9ff354 597long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 598 union ust_args *uargs, void *owner)
51489cad 599{
c8fcf224 600 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
601 struct lttng_ust_tracepoint_iter *tp =
602 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 603 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
604
605 switch (cmd) {
606 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224 607 {
c8fcf224
MD
608 iter = lttng_ust_tracepoint_list_get_iter_next(list);
609 if (!iter)
7bc53e94 610 return -LTTNG_UST_ERR_NOENT;
c8fcf224 611 memcpy(tp, iter, sizeof(*tp));
51489cad 612 return 0;
c8fcf224 613 }
51489cad
MD
614 default:
615 return -EINVAL;
616 }
617}
618
619static
f59ed768 620int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
621{
622 int list_objd, ret;
c8fcf224 623 struct lttng_ust_tracepoint_list *list;
51489cad 624
7047f3da 625 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
626 if (list_objd < 0) {
627 ret = list_objd;
628 goto objd_error;
629 }
630 list = zmalloc(sizeof(*list));
631 if (!list) {
632 ret = -ENOMEM;
633 goto alloc_error;
634 }
635 objd_set_private(list_objd, list);
636
c8fcf224 637 /* populate list by walking on all registered probes. */
7dd08bec 638 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
639 if (ret) {
640 goto list_error;
641 }
51489cad
MD
642 return list_objd;
643
c8fcf224
MD
644list_error:
645 free(list);
51489cad
MD
646alloc_error:
647 {
648 int err;
649
1849ef7c 650 err = lttng_ust_objd_unref(list_objd, 1);
51489cad
MD
651 assert(!err);
652 }
653objd_error:
654 return ret;
655}
656
657static
658int lttng_release_tracepoint_list(int objd)
659{
c8fcf224 660 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
661
662 if (list) {
7dd08bec 663 lttng_probes_prune_event_list(list);
51489cad
MD
664 free(list);
665 return 0;
666 } else {
667 return -EINVAL;
668 }
669}
670
671static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
672 .release = lttng_release_tracepoint_list,
673 .cmd = lttng_tracepoint_list_cmd,
674};
675
06d4f27e
MD
676static
677long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
f59ed768 678 unsigned long arg, union ust_args *uargs, void *owner)
06d4f27e
MD
679{
680 struct lttng_ust_field_list *list = objd_private(objd);
40003310 681 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
682 struct lttng_ust_field_iter *iter;
683
684 switch (cmd) {
685 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
686 {
06d4f27e
MD
687 iter = lttng_ust_field_list_get_iter_next(list);
688 if (!iter)
7bc53e94 689 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
690 memcpy(tp, iter, sizeof(*tp));
691 return 0;
692 }
693 default:
694 return -EINVAL;
695 }
696}
697
698static
f59ed768 699int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
700{
701 int list_objd, ret;
702 struct lttng_ust_field_list *list;
703
7047f3da
MD
704 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
705 "tp_field_list");
06d4f27e
MD
706 if (list_objd < 0) {
707 ret = list_objd;
708 goto objd_error;
709 }
710 list = zmalloc(sizeof(*list));
711 if (!list) {
712 ret = -ENOMEM;
713 goto alloc_error;
714 }
715 objd_set_private(list_objd, list);
716
717 /* populate list by walking on all registered probes. */
7dd08bec 718 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
719 if (ret) {
720 goto list_error;
721 }
722 return list_objd;
723
724list_error:
725 free(list);
726alloc_error:
727 {
728 int err;
729
1849ef7c 730 err = lttng_ust_objd_unref(list_objd, 1);
06d4f27e
MD
731 assert(!err);
732 }
733objd_error:
734 return ret;
735}
736
737static
738int lttng_release_tracepoint_field_list(int objd)
739{
740 struct lttng_ust_field_list *list = objd_private(objd);
741
742 if (list) {
7dd08bec 743 lttng_probes_prune_field_list(list);
06d4f27e
MD
744 free(list);
745 return 0;
746 } else {
747 return -EINVAL;
748 }
749}
750
751static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
752 .release = lttng_release_tracepoint_field_list,
753 .cmd = lttng_tracepoint_field_list_cmd,
754};
755
f4681817 756static
74d81a6c 757int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
f59ed768 758 union ust_args *uargs, void *owner)
f4681817 759{
7dd08bec 760 struct lttng_channel *channel = objd_private(channel_objd);
74d81a6c 761 int ret;
f4681817 762
74d81a6c
MD
763 ret = channel_handle_add_stream(channel->handle,
764 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
765 info->stream_nr, info->len);
766 if (ret)
767 goto error_add_stream;
768
769 return 0;
770
771error_add_stream:
f4681817
MD
772 return ret;
773}
f4681817
MD
774
775static
e58095ef 776int lttng_abi_create_enabler(int channel_objd,
f59ed768 777 struct lttng_ust_event *event_param,
e58095ef
MD
778 void *owner,
779 enum lttng_enabler_type type)
f4681817 780{
7dd08bec 781 struct lttng_channel *channel = objd_private(channel_objd);
e58095ef 782 struct lttng_enabler *enabler;
f4681817
MD
783 int event_objd, ret;
784
785 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
7047f3da 786 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
f4681817
MD
787 if (event_objd < 0) {
788 ret = event_objd;
789 goto objd_error;
790 }
791 /*
792 * We tolerate no failure path after event creation. It will stay
793 * invariant for the rest of the session.
794 */
e58095ef
MD
795 enabler = lttng_enabler_create(type, event_param, channel);
796 if (!enabler) {
797 ret = -ENOMEM;
f4681817
MD
798 goto event_error;
799 }
e58095ef 800 objd_set_private(event_objd, enabler);
f4681817
MD
801 /* The event holds a reference on the channel */
802 objd_ref(channel_objd);
803 return event_objd;
804
805event_error:
1ea11eab
MD
806 {
807 int err;
808
1849ef7c 809 err = lttng_ust_objd_unref(event_objd, 1);
1ea11eab
MD
810 assert(!err);
811 }
f4681817
MD
812objd_error:
813 return ret;
814}
815
816/**
817 * lttng_channel_cmd - lttng control through object descriptors
818 *
819 * @objd: the object descriptor
820 * @cmd: the command
821 * @arg: command arg
ef9ff354 822 * @uargs: UST arguments (internal)
f59ed768 823 * @owner: objd owner
f4681817
MD
824 *
825 * This object descriptor implements lttng commands:
826 * LTTNG_UST_STREAM
827 * Returns an event stream object descriptor or failure.
828 * (typically, one event stream records events from one CPU)
829 * LTTNG_UST_EVENT
830 * Returns an event object descriptor or failure.
831 * LTTNG_UST_CONTEXT
832 * Prepend a context field to each event in the channel
833 * LTTNG_UST_ENABLE
834 * Enable recording for events in this channel (weak enable)
835 * LTTNG_UST_DISABLE
836 * Disable recording for events in this channel (strong disable)
837 *
838 * Channel and event file descriptors also hold a reference on the session.
839 */
840static
ef9ff354 841long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 842 union ust_args *uargs, void *owner)
f4681817 843{
7dd08bec 844 struct lttng_channel *channel = objd_private(objd);
f4681817 845
74d81a6c
MD
846 if (cmd != LTTNG_UST_STREAM) {
847 /*
848 * Check if channel received all streams.
849 */
850 if (!lttng_is_channel_ready(channel))
851 return -EPERM;
852 }
853
f4681817
MD
854 switch (cmd) {
855 case LTTNG_UST_STREAM:
381c0f1e
MD
856 {
857 struct lttng_ust_stream *stream;
858
859 stream = (struct lttng_ust_stream *) arg;
860 /* stream used as output */
74d81a6c 861 return lttng_abi_map_stream(objd, stream, uargs, owner);
381c0f1e 862 }
f4681817 863 case LTTNG_UST_EVENT:
1f18504e
MD
864 {
865 struct lttng_ust_event *event_param =
866 (struct lttng_ust_event *) arg;
6b0e60f1
MD
867 if (event_param->name[strlen(event_param->name) - 1] == '*') {
868 /* If ends with wildcard, create wildcard. */
e58095ef
MD
869 return lttng_abi_create_enabler(objd, event_param,
870 owner, LTTNG_ENABLER_WILDCARD);
1f18504e 871 } else {
e58095ef
MD
872 return lttng_abi_create_enabler(objd, event_param,
873 owner, LTTNG_ENABLER_EVENT);
1f18504e
MD
874 }
875 }
f4681817
MD
876 case LTTNG_UST_CONTEXT:
877 return lttng_abi_add_context(objd,
878 (struct lttng_ust_context *) arg,
879 &channel->ctx, channel->session);
880 case LTTNG_UST_ENABLE:
7dd08bec 881 return lttng_channel_enable(channel);
f4681817 882 case LTTNG_UST_DISABLE:
7dd08bec 883 return lttng_channel_disable(channel);
43d330a4
MD
884 case LTTNG_UST_FLUSH_BUFFER:
885 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
886 default:
887 return -EINVAL;
888 }
889}
890
f4681817
MD
891static
892int lttng_channel_release(int objd)
893{
7dd08bec 894 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
895
896 if (channel)
1849ef7c 897 return lttng_ust_objd_unref(channel->session->objd, 0);
f4681817
MD
898 return 0;
899}
900
b61ce3b2 901static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817 902 .release = lttng_channel_release,
f4681817
MD
903 .cmd = lttng_channel_cmd,
904};
905
f4681817 906/**
e58095ef 907 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
908 *
909 * @objd: the object descriptor
910 * @cmd: the command
911 * @arg: command arg
ef9ff354 912 * @uargs: UST arguments (internal)
f59ed768 913 * @owner: objd owner
e6c12e3d
MD
914 *
915 * This object descriptor implements lttng commands:
916 * LTTNG_UST_CONTEXT
917 * Prepend a context field to each record of events of this
e58095ef 918 * enabler.
e6c12e3d 919 * LTTNG_UST_ENABLE
e58095ef 920 * Enable recording for this enabler
e6c12e3d 921 * LTTNG_UST_DISABLE
e58095ef 922 * Disable recording for this enabler
2d78951a 923 * LTTNG_UST_FILTER
e58095ef 924 * Attach a filter to an enabler.
e6c12e3d
MD
925 */
926static
e58095ef 927long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 928 union ust_args *uargs, void *owner)
e6c12e3d 929{
e58095ef 930 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d
MD
931
932 switch (cmd) {
933 case LTTNG_UST_CONTEXT:
e58095ef
MD
934 return lttng_enabler_attach_context(enabler,
935 (struct lttng_ust_context *) arg);
e6c12e3d 936 case LTTNG_UST_ENABLE:
e58095ef 937 return lttng_enabler_enable(enabler);
e6c12e3d 938 case LTTNG_UST_DISABLE:
e58095ef 939 return lttng_enabler_disable(enabler);
2d78951a
MD
940 case LTTNG_UST_FILTER:
941 {
942 int ret;
943
e58095ef 944 ret = lttng_enabler_attach_bytecode(enabler,
f488575f 945 (struct lttng_ust_filter_bytecode_node *) arg);
2d78951a
MD
946 if (ret)
947 return ret;
2d78951a
MD
948 return 0;
949 }
e6c12e3d
MD
950 default:
951 return -EINVAL;
952 }
953}
954
955static
e58095ef 956int lttng_enabler_release(int objd)
e6c12e3d 957{
e58095ef 958 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d 959
e58095ef 960 if (enabler)
1849ef7c 961 return lttng_ust_objd_unref(enabler->chan->objd, 0);
e6c12e3d
MD
962 return 0;
963}
964
e58095ef
MD
965static const struct lttng_ust_objd_ops lttng_enabler_ops = {
966 .release = lttng_enabler_release,
967 .cmd = lttng_enabler_cmd,
e6c12e3d
MD
968};
969
b35d179d 970void lttng_ust_abi_exit(void)
f4681817 971{
45e9e699 972 lttng_ust_abi_close_in_progress = 1;
f4681817 973 objd_table_destroy();
45e9e699 974 lttng_ust_abi_close_in_progress = 0;
f4681817 975}
This page took 0.081814 seconds and 4 git commands to generate.