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