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