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