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