Distinguish UST return codes from transport return codes
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817
MD
1/*
2 * lttng-ust-abi.c
3 *
f4681817
MD
4 * LTTng UST ABI
5 *
e92f3e28
MD
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
f4681817
MD
23 * Mimic system calls for:
24 * - session creation, returns an object descriptor or failure.
25 * - channel creation, returns an object descriptor or failure.
26 * - Operates on a session object descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns an object descriptor or failure.
29 * - Operates on a channel object descriptor.
30 * - stream notifier get, returns an object descriptor or failure.
31 * - Operates on a channel object descriptor.
32 * - event creation, returns an object descriptor or failure.
33 * - Operates on a channel object descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
f4681817
MD
38 */
39
4318ae1b 40#include <lttng/ust-abi.h>
7bc53e94 41#include <lttng/ust-error.h>
f4681817
MD
42#include <urcu/compiler.h>
43#include <urcu/list.h>
4318ae1b 44#include <lttng/ust-events.h>
23c8854a 45#include <lttng/ust-version.h>
902e1379 46#include <lttng/tracepoint.h>
d8de1354 47#include "tracepoint-internal.h"
44c72f10
MD
48#include <usterr-signal-safe.h>
49#include <helper.h>
f4681817 50#include "ltt-tracer.h"
4ab44fbe 51
45e9e699
MD
52static int lttng_ust_abi_close_in_progress;
53
51489cad
MD
54static
55int lttng_abi_tracepoint_list(void);
06d4f27e
MD
56static
57int lttng_abi_tracepoint_field_list(void);
51489cad 58
f4681817
MD
59/*
60 * Object descriptor table. Should be protected from concurrent access
61 * by the caller.
62 */
63
b61ce3b2 64struct lttng_ust_obj {
f4681817
MD
65 union {
66 struct {
67 void *private_data;
b61ce3b2 68 const struct lttng_ust_objd_ops *ops;
f4681817
MD
69 int f_count;
70 } s;
71 int freelist_next; /* offset freelist. end is -1. */
72 } u;
73};
74
b61ce3b2
MD
75struct lttng_ust_objd_table {
76 struct lttng_ust_obj *array;
f4681817
MD
77 unsigned int len, allocated_len;
78 int freelist_head; /* offset freelist head. end is -1 */
79};
80
b61ce3b2 81static struct lttng_ust_objd_table objd_table = {
f4681817
MD
82 .freelist_head = -1,
83};
84
85static
b61ce3b2 86int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops)
f4681817 87{
b61ce3b2 88 struct lttng_ust_obj *obj;
f4681817
MD
89
90 if (objd_table.freelist_head != -1) {
91 obj = &objd_table.array[objd_table.freelist_head];
92 objd_table.freelist_head = obj->u.freelist_next;
93 goto end;
94 }
95
96 if (objd_table.len >= objd_table.allocated_len) {
97 unsigned int new_allocated_len, old_allocated_len;
b61ce3b2 98 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
99
100 old_allocated_len = objd_table.allocated_len;
101 old_table = objd_table.array;
102 if (!old_allocated_len)
103 new_allocated_len = 1;
104 else
105 new_allocated_len = old_allocated_len << 1;
b61ce3b2 106 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
107 if (!new_table)
108 return -ENOMEM;
109 memcpy(new_table, old_table,
b61ce3b2 110 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
111 free(old_table);
112 objd_table.array = new_table;
113 objd_table.allocated_len = new_allocated_len;
114 }
115 obj = &objd_table.array[objd_table.len];
116 objd_table.len++;
117end:
118 obj->u.s.private_data = private_data;
119 obj->u.s.ops = ops;
a4be8962
MD
120 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
121 /* count == 2 : allocated + hold ref */
f4681817
MD
122 return obj - objd_table.array;
123}
124
125static
b61ce3b2 126struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
127{
128 if (id >= objd_table.len)
129 return NULL;
a4be8962
MD
130 if (!objd_table.array[id].u.s.f_count)
131 return NULL;
f4681817
MD
132 return &objd_table.array[id];
133}
134
135static
136void *objd_private(int id)
137{
b61ce3b2 138 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
139 assert(obj);
140 return obj->u.s.private_data;
141}
142
143static
144void objd_set_private(int id, void *private_data)
145{
b61ce3b2 146 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
147 assert(obj);
148 obj->u.s.private_data = private_data;
149}
150
b61ce3b2 151const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 152{
b61ce3b2 153 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 154
a4be8962
MD
155 if (!obj)
156 return NULL;
f4681817
MD
157 return obj->u.s.ops;
158}
159
160static
161void objd_free(int id)
162{
b61ce3b2 163 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
164
165 assert(obj);
166 obj->u.freelist_next = objd_table.freelist_head;
167 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
168 assert(obj->u.s.f_count == 1);
169 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
170}
171
172static
173void objd_ref(int id)
174{
b61ce3b2 175 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
176 obj->u.s.f_count++;
177}
178
d4419b81 179int lttng_ust_objd_unref(int id)
f4681817 180{
b61ce3b2 181 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 182
1ea11eab
MD
183 if (!obj)
184 return -EINVAL;
a4be8962
MD
185 if (obj->u.s.f_count == 1) {
186 ERR("Reference counting error\n");
187 return -EINVAL;
188 }
189 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 190 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 191
f4681817
MD
192 if (ops->release)
193 ops->release(id);
194 objd_free(id);
195 }
1ea11eab 196 return 0;
f4681817
MD
197}
198
199static
200void objd_table_destroy(void)
201{
a4be8962
MD
202 int i;
203
fb50c39d 204 for (i = 0; i < objd_table.allocated_len; i++)
d4419b81 205 (void) lttng_ust_objd_unref(i);
f4681817 206 free(objd_table.array);
02fb3381
MD
207 objd_table.array = NULL;
208 objd_table.len = 0;
209 objd_table.allocated_len = 0;
17dfb34b 210 objd_table.freelist_head = -1;
f4681817
MD
211}
212
213/*
214 * This is LTTng's own personal way to create an ABI for sessiond.
215 * We send commands over a socket.
216 */
217
b61ce3b2
MD
218static const struct lttng_ust_objd_ops lttng_ops;
219static const struct lttng_ust_objd_ops lttng_session_ops;
220static const struct lttng_ust_objd_ops lttng_channel_ops;
221static const struct lttng_ust_objd_ops lttng_metadata_ops;
222static const struct lttng_ust_objd_ops lttng_event_ops;
e6c12e3d 223static const struct lttng_ust_objd_ops lttng_wildcard_ops;
b61ce3b2 224static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
51489cad 225static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 226static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817
MD
227
228enum channel_type {
229 PER_CPU_CHANNEL,
230 METADATA_CHANNEL,
231};
232
46050b1a
MD
233int lttng_abi_create_root_handle(void)
234{
235 int root_handle;
236
237 root_handle = objd_alloc(NULL, &lttng_ops);
46050b1a
MD
238 return root_handle;
239}
240
818173b9 241static
f4681817
MD
242int lttng_abi_create_session(void)
243{
244 struct ltt_session *session;
245 int session_objd, ret;
246
247 session = ltt_session_create();
248 if (!session)
249 return -ENOMEM;
250 session_objd = objd_alloc(session, &lttng_session_ops);
251 if (session_objd < 0) {
252 ret = session_objd;
253 goto objd_error;
254 }
255 session->objd = session_objd;
256 return session_objd;
257
258objd_error:
259 ltt_session_destroy(session);
260 return ret;
261}
262
f4681817
MD
263static
264long lttng_abi_tracer_version(int objd,
265 struct lttng_ust_tracer_version *v)
266{
0f4eaec3
MD
267 v->major = LTTNG_UST_INTERNAL_MAJOR_VERSION;
268 v->minor = LTTNG_UST_INTERNAL_MINOR_VERSION;
269 v->patchlevel = LTTNG_UST_INTERNAL_PATCHLEVEL_VERSION;
f4681817
MD
270 return 0;
271}
272
273static
274long lttng_abi_add_context(int objd,
275 struct lttng_ust_context *context_param,
276 struct lttng_ctx **ctx, struct ltt_session *session)
277{
278 if (session->been_active)
279 return -EPERM;
280
281 switch (context_param->ctx) {
8de38cf7
MD
282 case LTTNG_UST_CONTEXT_PTHREAD_ID:
283 return lttng_add_pthread_id_to_ctx(ctx);
3b402b40
MD
284 case LTTNG_UST_CONTEXT_VTID:
285 return lttng_add_vtid_to_ctx(ctx);
c1ef86f0
MD
286 case LTTNG_UST_CONTEXT_VPID:
287 return lttng_add_vpid_to_ctx(ctx);
4847e9bb
MD
288 case LTTNG_UST_CONTEXT_PROCNAME:
289 return lttng_add_procname_to_ctx(ctx);
f4681817
MD
290 default:
291 return -EINVAL;
292 }
293}
294
295/**
296 * lttng_cmd - lttng control through socket commands
297 *
298 * @objd: the object descriptor
299 * @cmd: the command
300 * @arg: command arg
ef9ff354 301 * @uargs: UST arguments (internal)
f4681817
MD
302 *
303 * This descriptor implements lttng commands:
304 * LTTNG_UST_SESSION
305 * Returns a LTTng trace session object descriptor
306 * LTTNG_UST_TRACER_VERSION
307 * Returns the LTTng kernel tracer version
308 * LTTNG_UST_TRACEPOINT_LIST
309 * Returns a file descriptor listing available tracepoints
06d4f27e
MD
310 * LTTNG_UST_TRACEPOINT_FIELD_LIST
311 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
312 * LTTNG_UST_WAIT_QUIESCENT
313 * Returns after all previously running probes have completed
314 *
315 * The returned session will be deleted when its file descriptor is closed.
316 */
317static
ef9ff354
MD
318long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
319 union ust_args *uargs)
f4681817
MD
320{
321 switch (cmd) {
322 case LTTNG_UST_SESSION:
323 return lttng_abi_create_session();
324 case LTTNG_UST_TRACER_VERSION:
325 return lttng_abi_tracer_version(objd,
326 (struct lttng_ust_tracer_version *) arg);
327 case LTTNG_UST_TRACEPOINT_LIST:
51489cad 328 return lttng_abi_tracepoint_list();
06d4f27e
MD
329 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
330 return lttng_abi_tracepoint_field_list();
f4681817
MD
331 case LTTNG_UST_WAIT_QUIESCENT:
332 synchronize_trace();
333 return 0;
334 default:
335 return -EINVAL;
336 }
337}
338
b61ce3b2 339static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
340 .cmd = lttng_cmd,
341};
342
343/*
344 * We tolerate no failure in this function (if one happens, we print a dmesg
345 * error, but cannot return any error, because the channel information is
346 * invariant.
347 */
348static
349void lttng_metadata_create_events(int channel_objd)
350{
351 struct ltt_channel *channel = objd_private(channel_objd);
352 static struct lttng_ust_event metadata_params = {
353 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 354 .name = "lttng_ust:metadata",
457a6b58
MD
355 .loglevel_type = LTTNG_UST_LOGLEVEL_ALL,
356 .loglevel = TRACE_DEFAULT,
f4681817
MD
357 };
358 struct ltt_event *event;
576599a0 359 int ret;
f4681817
MD
360
361 /*
362 * We tolerate no failure path after event creation. It will stay
363 * invariant for the rest of the session.
364 */
2d78951a 365 ret = ltt_event_create(channel, &metadata_params, &event);
576599a0 366 if (ret < 0) {
f4681817
MD
367 goto create_error;
368 }
369 return;
370
371create_error:
372 WARN_ON(1);
373 return; /* not allowed to return error */
374}
375
f4681817
MD
376int lttng_abi_create_channel(int session_objd,
377 struct lttng_ust_channel *chan_param,
ef9ff354
MD
378 enum channel_type channel_type,
379 union ust_args *uargs)
f4681817
MD
380{
381 struct ltt_session *session = objd_private(session_objd);
b61ce3b2 382 const struct lttng_ust_objd_ops *ops;
f4681817
MD
383 const char *transport_name;
384 struct ltt_channel *chan;
385 int chan_objd;
386 int ret = 0;
d028eddb 387 struct ltt_channel chan_priv_init;
f4681817 388
f4681817
MD
389 switch (channel_type) {
390 case PER_CPU_CHANNEL:
391 if (chan_param->output == LTTNG_UST_MMAP) {
392 transport_name = chan_param->overwrite ?
393 "relay-overwrite-mmap" : "relay-discard-mmap";
394 } else {
395 return -EINVAL;
396 }
397 ops = &lttng_channel_ops;
398 break;
399 case METADATA_CHANNEL:
400 if (chan_param->output == LTTNG_UST_MMAP)
401 transport_name = "relay-metadata-mmap";
402 else
403 return -EINVAL;
404 ops = &lttng_metadata_ops;
405 break;
406 default:
407 transport_name = "<unknown>";
fe38d4af
MD
408 return -EINVAL;
409 }
410 chan_objd = objd_alloc(NULL, ops);
411 if (chan_objd < 0) {
412 ret = chan_objd;
413 goto objd_error;
f4681817 414 }
d028eddb
MD
415 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
416 /* Copy of session UUID for consumer (availability through shm) */
417 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
418
f4681817
MD
419 /*
420 * We tolerate no failure path after channel creation. It will stay
421 * invariant for the rest of the session.
422 */
423 chan = ltt_channel_create(session, transport_name, NULL,
424 chan_param->subbuf_size,
425 chan_param->num_subbuf,
426 chan_param->switch_timer_interval,
193183fb 427 chan_param->read_timer_interval,
ef9ff354
MD
428 &uargs->channel.shm_fd,
429 &uargs->channel.wait_fd,
430 &uargs->channel.memory_map_size,
d028eddb 431 &chan_priv_init);
f4681817
MD
432 if (!chan) {
433 ret = -EINVAL;
434 goto chan_error;
435 }
436 objd_set_private(chan_objd, chan);
437 chan->objd = chan_objd;
438 if (channel_type == METADATA_CHANNEL) {
439 session->metadata = chan;
440 lttng_metadata_create_events(chan_objd);
441 }
f4681817
MD
442 /* The channel created holds a reference on the session */
443 objd_ref(session_objd);
444
445 return chan_objd;
446
447chan_error:
1ea11eab
MD
448 {
449 int err;
450
d4419b81 451 err = lttng_ust_objd_unref(chan_objd);
1ea11eab
MD
452 assert(!err);
453 }
f4681817
MD
454objd_error:
455 return ret;
456}
457
458/**
459 * lttng_session_cmd - lttng session object command
460 *
461 * @obj: the object
462 * @cmd: the command
463 * @arg: command arg
ef9ff354 464 * @uargs: UST arguments (internal)
f4681817
MD
465 *
466 * This descriptor implements lttng commands:
467 * LTTNG_UST_CHANNEL
468 * Returns a LTTng channel object descriptor
469 * LTTNG_UST_ENABLE
470 * Enables tracing for a session (weak enable)
471 * LTTNG_UST_DISABLE
472 * Disables tracing for a session (strong disable)
473 * LTTNG_UST_METADATA
474 * Returns a LTTng metadata object descriptor
475 *
476 * The returned channel will be deleted when its file descriptor is closed.
477 */
478static
ef9ff354
MD
479long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
480 union ust_args *uargs)
f4681817
MD
481{
482 struct ltt_session *session = objd_private(objd);
483
484 switch (cmd) {
485 case LTTNG_UST_CHANNEL:
486 return lttng_abi_create_channel(objd,
487 (struct lttng_ust_channel *) arg,
ef9ff354 488 PER_CPU_CHANNEL, uargs);
f4681817
MD
489 case LTTNG_UST_SESSION_START:
490 case LTTNG_UST_ENABLE:
491 return ltt_session_enable(session);
492 case LTTNG_UST_SESSION_STOP:
493 case LTTNG_UST_DISABLE:
494 return ltt_session_disable(session);
495 case LTTNG_UST_METADATA:
496 return lttng_abi_create_channel(objd,
497 (struct lttng_ust_channel *) arg,
ef9ff354 498 METADATA_CHANNEL, uargs);
f4681817
MD
499 default:
500 return -EINVAL;
501 }
502}
503
504/*
505 * Called when the last file reference is dropped.
506 *
507 * Big fat note: channels and events are invariant for the whole session after
508 * their creation. So this session destruction also destroys all channel and
509 * event structures specific to this session (they are not destroyed when their
510 * individual file is released).
511 */
512static
1ea11eab 513int lttng_release_session(int objd)
f4681817
MD
514{
515 struct ltt_session *session = objd_private(objd);
516
1ea11eab 517 if (session) {
f4681817 518 ltt_session_destroy(session);
1ea11eab
MD
519 return 0;
520 } else {
521 return -EINVAL;
522 }
f4681817
MD
523}
524
b61ce3b2 525static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 526 .release = lttng_release_session,
f4681817
MD
527 .cmd = lttng_session_cmd,
528};
529
51489cad 530static
ef9ff354
MD
531long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
532 union ust_args *uargs)
51489cad 533{
c8fcf224 534 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
535 struct lttng_ust_tracepoint_iter *tp =
536 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 537 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
538
539 switch (cmd) {
540 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224
MD
541 {
542 retry:
543 iter = lttng_ust_tracepoint_list_get_iter_next(list);
544 if (!iter)
7bc53e94 545 return -LTTNG_UST_ERR_NOENT;
c8fcf224
MD
546 if (!strcmp(iter->name, "lttng_ust:metadata"))
547 goto retry;
548 memcpy(tp, iter, sizeof(*tp));
51489cad 549 return 0;
c8fcf224 550 }
51489cad
MD
551 default:
552 return -EINVAL;
553 }
554}
555
556static
557int lttng_abi_tracepoint_list(void)
558{
559 int list_objd, ret;
c8fcf224 560 struct lttng_ust_tracepoint_list *list;
51489cad
MD
561
562 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
563 if (list_objd < 0) {
564 ret = list_objd;
565 goto objd_error;
566 }
567 list = zmalloc(sizeof(*list));
568 if (!list) {
569 ret = -ENOMEM;
570 goto alloc_error;
571 }
572 objd_set_private(list_objd, list);
573
c8fcf224
MD
574 /* populate list by walking on all registered probes. */
575 ret = ltt_probes_get_event_list(list);
576 if (ret) {
577 goto list_error;
578 }
51489cad
MD
579 return list_objd;
580
c8fcf224
MD
581list_error:
582 free(list);
51489cad
MD
583alloc_error:
584 {
585 int err;
586
587 err = lttng_ust_objd_unref(list_objd);
588 assert(!err);
589 }
590objd_error:
591 return ret;
592}
593
594static
595int lttng_release_tracepoint_list(int objd)
596{
c8fcf224 597 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
598
599 if (list) {
c8fcf224 600 ltt_probes_prune_event_list(list);
51489cad
MD
601 free(list);
602 return 0;
603 } else {
604 return -EINVAL;
605 }
606}
607
608static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
609 .release = lttng_release_tracepoint_list,
610 .cmd = lttng_tracepoint_list_cmd,
611};
612
06d4f27e
MD
613static
614long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
615 unsigned long arg, union ust_args *uargs)
616{
617 struct lttng_ust_field_list *list = objd_private(objd);
40003310 618 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
619 struct lttng_ust_field_iter *iter;
620
621 switch (cmd) {
622 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
623 {
624 retry:
625 iter = lttng_ust_field_list_get_iter_next(list);
626 if (!iter)
7bc53e94 627 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
628 if (!strcmp(iter->event_name, "lttng_ust:metadata"))
629 goto retry;
630 memcpy(tp, iter, sizeof(*tp));
631 return 0;
632 }
633 default:
634 return -EINVAL;
635 }
636}
637
638static
639int lttng_abi_tracepoint_field_list(void)
640{
641 int list_objd, ret;
642 struct lttng_ust_field_list *list;
643
644 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops);
645 if (list_objd < 0) {
646 ret = list_objd;
647 goto objd_error;
648 }
649 list = zmalloc(sizeof(*list));
650 if (!list) {
651 ret = -ENOMEM;
652 goto alloc_error;
653 }
654 objd_set_private(list_objd, list);
655
656 /* populate list by walking on all registered probes. */
657 ret = ltt_probes_get_field_list(list);
658 if (ret) {
659 goto list_error;
660 }
661 return list_objd;
662
663list_error:
664 free(list);
665alloc_error:
666 {
667 int err;
668
669 err = lttng_ust_objd_unref(list_objd);
670 assert(!err);
671 }
672objd_error:
673 return ret;
674}
675
676static
677int lttng_release_tracepoint_field_list(int objd)
678{
679 struct lttng_ust_field_list *list = objd_private(objd);
680
681 if (list) {
682 ltt_probes_prune_field_list(list);
683 free(list);
684 return 0;
685 } else {
686 return -EINVAL;
687 }
688}
689
690static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
691 .release = lttng_release_tracepoint_field_list,
692 .cmd = lttng_tracepoint_field_list_cmd,
693};
694
381c0f1e 695struct stream_priv_data {
4cfec15c 696 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
697 struct ltt_channel *ltt_chan;
698};
699
f4681817 700static
ef9ff354
MD
701int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
702 union ust_args *uargs)
f4681817
MD
703{
704 struct ltt_channel *channel = objd_private(channel_objd);
4cfec15c 705 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e 706 struct stream_priv_data *priv;
f4681817
MD
707 int stream_objd, ret;
708
381c0f1e 709 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
ef9ff354
MD
710 &uargs->stream.shm_fd,
711 &uargs->stream.wait_fd,
712 &uargs->stream.memory_map_size);
f4681817
MD
713 if (!buf)
714 return -ENOENT;
715
381c0f1e
MD
716 priv = zmalloc(sizeof(*priv));
717 if (!priv) {
718 ret = -ENOMEM;
719 goto alloc_error;
720 }
721 priv->buf = buf;
722 priv->ltt_chan = channel;
723 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
f4681817
MD
724 if (stream_objd < 0) {
725 ret = stream_objd;
726 goto objd_error;
727 }
381c0f1e
MD
728 /* Hold a reference on the channel object descriptor */
729 objd_ref(channel_objd);
f4681817
MD
730 return stream_objd;
731
732objd_error:
381c0f1e
MD
733 free(priv);
734alloc_error:
735 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
736 return ret;
737}
f4681817
MD
738
739static
740int lttng_abi_create_event(int channel_objd,
741 struct lttng_ust_event *event_param)
742{
743 struct ltt_channel *channel = objd_private(channel_objd);
744 struct ltt_event *event;
745 int event_objd, ret;
746
747 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
748 event_objd = objd_alloc(NULL, &lttng_event_ops);
749 if (event_objd < 0) {
750 ret = event_objd;
751 goto objd_error;
752 }
753 /*
754 * We tolerate no failure path after event creation. It will stay
755 * invariant for the rest of the session.
756 */
2d78951a 757 ret = ltt_event_create(channel, event_param, &event);
576599a0 758 if (ret < 0) {
f4681817
MD
759 goto event_error;
760 }
761 objd_set_private(event_objd, event);
762 /* The event holds a reference on the channel */
763 objd_ref(channel_objd);
764 return event_objd;
765
766event_error:
1ea11eab
MD
767 {
768 int err;
769
d4419b81 770 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
771 assert(!err);
772 }
f4681817
MD
773objd_error:
774 return ret;
775}
776
e6c12e3d
MD
777static
778int lttng_abi_create_wildcard(int channel_objd,
779 struct lttng_ust_event *event_param)
780{
781 struct ltt_channel *channel = objd_private(channel_objd);
782 struct session_wildcard *wildcard;
783 int wildcard_objd, ret;
784
785 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
786 wildcard_objd = objd_alloc(NULL, &lttng_wildcard_ops);
787 if (wildcard_objd < 0) {
788 ret = wildcard_objd;
789 goto objd_error;
790 }
791 /*
792 * We tolerate no failure path after wildcard creation. It will
793 * stay invariant for the rest of the session.
794 */
795 ret = ltt_wildcard_create(channel, event_param, &wildcard);
796 if (ret < 0) {
797 goto wildcard_error;
798 }
799 objd_set_private(wildcard_objd, wildcard);
800 /* The wildcard holds a reference on the channel */
801 objd_ref(channel_objd);
802 return wildcard_objd;
803
804wildcard_error:
805 {
806 int err;
807
808 err = lttng_ust_objd_unref(wildcard_objd);
809 assert(!err);
810 }
811objd_error:
812 return ret;
813}
814
f4681817
MD
815/**
816 * lttng_channel_cmd - lttng control through object descriptors
817 *
818 * @objd: the object descriptor
819 * @cmd: the command
820 * @arg: command arg
ef9ff354 821 * @uargs: UST arguments (internal)
f4681817
MD
822 *
823 * This object descriptor implements lttng commands:
824 * LTTNG_UST_STREAM
825 * Returns an event stream object descriptor or failure.
826 * (typically, one event stream records events from one CPU)
827 * LTTNG_UST_EVENT
828 * Returns an event object descriptor or failure.
829 * LTTNG_UST_CONTEXT
830 * Prepend a context field to each event in the channel
831 * LTTNG_UST_ENABLE
832 * Enable recording for events in this channel (weak enable)
833 * LTTNG_UST_DISABLE
834 * Disable recording for events in this channel (strong disable)
835 *
836 * Channel and event file descriptors also hold a reference on the session.
837 */
838static
ef9ff354
MD
839long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
840 union ust_args *uargs)
f4681817
MD
841{
842 struct ltt_channel *channel = objd_private(objd);
843
844 switch (cmd) {
845 case LTTNG_UST_STREAM:
381c0f1e
MD
846 {
847 struct lttng_ust_stream *stream;
848
849 stream = (struct lttng_ust_stream *) arg;
850 /* stream used as output */
ef9ff354 851 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 852 }
f4681817 853 case LTTNG_UST_EVENT:
1f18504e
MD
854 {
855 struct lttng_ust_event *event_param =
856 (struct lttng_ust_event *) arg;
6b0e60f1
MD
857 if (event_param->name[strlen(event_param->name) - 1] == '*') {
858 /* If ends with wildcard, create wildcard. */
859 return lttng_abi_create_wildcard(objd, event_param);
1f18504e 860 } else {
6b0e60f1 861 return lttng_abi_create_event(objd, event_param);
1f18504e
MD
862 }
863 }
f4681817
MD
864 case LTTNG_UST_CONTEXT:
865 return lttng_abi_add_context(objd,
866 (struct lttng_ust_context *) arg,
867 &channel->ctx, channel->session);
868 case LTTNG_UST_ENABLE:
869 return ltt_channel_enable(channel);
870 case LTTNG_UST_DISABLE:
871 return ltt_channel_disable(channel);
43d330a4
MD
872 case LTTNG_UST_FLUSH_BUFFER:
873 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
874 default:
875 return -EINVAL;
876 }
877}
878
879/**
880 * lttng_metadata_cmd - lttng control through object descriptors
881 *
882 * @objd: the object descriptor
883 * @cmd: the command
884 * @arg: command arg
ef9ff354 885 * @uargs: UST arguments (internal)
f4681817
MD
886 *
887 * This object descriptor implements lttng commands:
888 * LTTNG_UST_STREAM
889 * Returns an event stream file descriptor or failure.
890 *
891 * Channel and event file descriptors also hold a reference on the session.
892 */
893static
ef9ff354
MD
894long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
895 union ust_args *uargs)
f4681817 896{
43861eab
MD
897 struct ltt_channel *channel = objd_private(objd);
898
f4681817
MD
899 switch (cmd) {
900 case LTTNG_UST_STREAM:
381c0f1e
MD
901 {
902 struct lttng_ust_stream *stream;
903
904 stream = (struct lttng_ust_stream *) arg;
905 /* stream used as output */
ef9ff354 906 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 907 }
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
915#if 0
916/**
917 * lttng_channel_poll - lttng stream addition/removal monitoring
918 *
919 * @file: the file
920 * @wait: poll table
921 */
922unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
923{
924 struct ltt_channel *channel = file->private_data;
925 unsigned int mask = 0;
926
927 if (file->f_mode & FMODE_READ) {
928 poll_wait_set_exclusive(wait);
929 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
930 wait);
931
932 if (channel->ops->is_disabled(channel->chan))
933 return POLLERR;
934 if (channel->ops->is_finalized(channel->chan))
935 return POLLHUP;
936 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
937 return POLLIN | POLLRDNORM;
938 return 0;
939 }
940 return mask;
941
942}
943#endif //0
944
945static
946int lttng_channel_release(int objd)
947{
948 struct ltt_channel *channel = objd_private(objd);
949
950 if (channel)
d4419b81 951 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
952 return 0;
953}
954
b61ce3b2 955static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817
MD
956 .release = lttng_channel_release,
957 //.poll = lttng_channel_poll,
958 .cmd = lttng_channel_cmd,
959};
960
b61ce3b2 961static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
962 .release = lttng_channel_release,
963 .cmd = lttng_metadata_cmd,
964};
965
381c0f1e
MD
966/**
967 * lttng_rb_cmd - lttng ring buffer control through object descriptors
968 *
969 * @objd: the object descriptor
970 * @cmd: the command
971 * @arg: command arg
ef9ff354 972 * @uargs: UST arguments (internal)
381c0f1e
MD
973 *
974 * This object descriptor implements lttng commands:
975 * (None for now. Access is done directly though shm.)
381c0f1e
MD
976 */
977static
ef9ff354
MD
978long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
979 union ust_args *uargs)
381c0f1e 980{
381c0f1e
MD
981 switch (cmd) {
982 default:
983 return -EINVAL;
984 }
985}
986
987static
988int lttng_rb_release(int objd)
989{
990 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 991 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
992 struct ltt_channel *channel;
993
994 if (priv) {
995 buf = priv->buf;
996 channel = priv->ltt_chan;
997 free(priv);
45e9e699
MD
998 /*
999 * If we are at ABI exit, we don't want to close the
1000 * buffer opened for read: it is being shared between
1001 * the parent and child (right after fork), and we don't
1002 * want the child to close it for the parent. For a real
1003 * exit, we don't care about marking it as closed, as
1004 * the consumer daemon (if there is one) will do fine
1005 * even if we don't mark it as "closed" for reading on
1006 * our side.
1007 * We only mark it as closed if it is being explicitely
1008 * released by the session daemon with an explicit
1009 * release command.
1010 */
1011 if (!lttng_ust_abi_close_in_progress)
1012 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 1013
d4419b81 1014 return lttng_ust_objd_unref(channel->objd);
381c0f1e
MD
1015 }
1016 return 0;
1017}
1018
b61ce3b2 1019static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
1020 .release = lttng_rb_release,
1021 .cmd = lttng_rb_cmd,
1022};
1023
f4681817
MD
1024/**
1025 * lttng_event_cmd - lttng control through object descriptors
1026 *
1027 * @objd: the object descriptor
1028 * @cmd: the command
1029 * @arg: command arg
ef9ff354 1030 * @uargs: UST arguments (internal)
f4681817
MD
1031 *
1032 * This object descriptor implements lttng commands:
1033 * LTTNG_UST_CONTEXT
1034 * Prepend a context field to each record of this event
1035 * LTTNG_UST_ENABLE
1036 * Enable recording for this event (weak enable)
1037 * LTTNG_UST_DISABLE
1038 * Disable recording for this event (strong disable)
2d78951a
MD
1039 * LTTNG_UST_FILTER
1040 * Attach a filter to an event.
f4681817
MD
1041 */
1042static
ef9ff354
MD
1043long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg,
1044 union ust_args *uargs)
f4681817
MD
1045{
1046 struct ltt_event *event = objd_private(objd);
1047
1048 switch (cmd) {
1049 case LTTNG_UST_CONTEXT:
1050 return lttng_abi_add_context(objd,
1051 (struct lttng_ust_context *) arg,
1052 &event->ctx, event->chan->session);
1053 case LTTNG_UST_ENABLE:
1054 return ltt_event_enable(event);
1055 case LTTNG_UST_DISABLE:
1056 return ltt_event_disable(event);
2d78951a
MD
1057 case LTTNG_UST_FILTER:
1058 {
1059 int ret;
1060 ret = lttng_filter_event_attach_bytecode(event,
1061 (struct lttng_ust_filter_bytecode *) arg);
1062 if (ret)
1063 return ret;
1064 lttng_filter_event_link_bytecode(event,
1065 event->filter_bytecode);
1066 return 0;
1067 }
f4681817
MD
1068 default:
1069 return -EINVAL;
1070 }
1071}
1072
1073static
1074int lttng_event_release(int objd)
1075{
1076 struct ltt_event *event = objd_private(objd);
1077
1078 if (event)
d4419b81 1079 return lttng_ust_objd_unref(event->chan->objd);
f4681817
MD
1080 return 0;
1081}
1082
1083/* TODO: filter control ioctl */
b61ce3b2 1084static const struct lttng_ust_objd_ops lttng_event_ops = {
f4681817
MD
1085 .release = lttng_event_release,
1086 .cmd = lttng_event_cmd,
1087};
1088
e6c12e3d
MD
1089/**
1090 * lttng_wildcard_cmd - lttng control through object descriptors
1091 *
1092 * @objd: the object descriptor
1093 * @cmd: the command
1094 * @arg: command arg
ef9ff354 1095 * @uargs: UST arguments (internal)
e6c12e3d
MD
1096 *
1097 * This object descriptor implements lttng commands:
1098 * LTTNG_UST_CONTEXT
1099 * Prepend a context field to each record of events of this
1100 * wildcard.
1101 * LTTNG_UST_ENABLE
1102 * Enable recording for these wildcard events (weak enable)
1103 * LTTNG_UST_DISABLE
1104 * Disable recording for these wildcard events (strong disable)
2d78951a
MD
1105 * LTTNG_UST_FILTER
1106 * Attach a filter to a wildcard.
e6c12e3d
MD
1107 */
1108static
ef9ff354
MD
1109long lttng_wildcard_cmd(int objd, unsigned int cmd, unsigned long arg,
1110 union ust_args *uargs)
e6c12e3d
MD
1111{
1112 struct session_wildcard *wildcard = objd_private(objd);
1113
1114 switch (cmd) {
1115 case LTTNG_UST_CONTEXT:
1116 return -ENOSYS; /* not implemented yet */
1117#if 0
1118 return lttng_abi_add_context(objd,
1119 (struct lttng_ust_context *) arg,
1120 &wildcard->ctx, wildcard->chan->session);
1121#endif
1122 case LTTNG_UST_ENABLE:
1123 return ltt_wildcard_enable(wildcard);
1124 case LTTNG_UST_DISABLE:
1125 return ltt_wildcard_disable(wildcard);
2d78951a
MD
1126 case LTTNG_UST_FILTER:
1127 {
1128 int ret;
1129
1130 ret = lttng_filter_wildcard_attach_bytecode(wildcard,
1131 (struct lttng_ust_filter_bytecode *) arg);
1132 if (ret)
1133 return ret;
1134 lttng_filter_wildcard_link_bytecode(wildcard);
1135 return 0;
1136 }
e6c12e3d
MD
1137 default:
1138 return -EINVAL;
1139 }
1140}
1141
1142static
1143int lttng_wildcard_release(int objd)
1144{
1145 struct session_wildcard *wildcard = objd_private(objd);
1146
1147 if (wildcard)
1148 return lttng_ust_objd_unref(wildcard->chan->objd);
1149 return 0;
1150}
1151
1152/* TODO: filter control ioctl */
1153static const struct lttng_ust_objd_ops lttng_wildcard_ops = {
1154 .release = lttng_wildcard_release,
1155 .cmd = lttng_wildcard_cmd,
1156};
1157
b35d179d 1158void lttng_ust_abi_exit(void)
f4681817 1159{
45e9e699 1160 lttng_ust_abi_close_in_progress = 1;
f4681817 1161 objd_table_destroy();
45e9e699 1162 lttng_ust_abi_close_in_progress = 0;
f4681817 1163}
This page took 0.101766 seconds and 4 git commands to generate.