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