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