Cleanup the JNI interface options in configure.ac
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817
MD
1/*
2 * lttng-ust-abi.c
3 *
4 * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns an object descriptor or failure.
10 * - channel creation, returns an object descriptor or failure.
11 * - Operates on a session object descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns an object descriptor or failure.
14 * - Operates on a channel object descriptor.
15 * - stream notifier get, returns an object descriptor or failure.
16 * - Operates on a channel object descriptor.
17 * - event creation, returns an object descriptor or failure.
18 * - Operates on a channel object descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
23 *
24 * Dual LGPL v2.1/GPL v2 license.
25 */
26
4318ae1b 27#include <lttng/ust-abi.h>
f4681817
MD
28#include <urcu/compiler.h>
29#include <urcu/list.h>
4318ae1b 30#include <lttng/ust-events.h>
23c8854a 31#include <lttng/ust-version.h>
44c72f10
MD
32#include <usterr-signal-safe.h>
33#include <helper.h>
f4681817 34#include "ltt-tracer.h"
4ab44fbe
MD
35#include "tracepoint-internal.h"
36
37struct ltt_tracepoint_list {
38 struct tracepoint_iter iter;
39 int got_first;
40};
f4681817 41
45e9e699
MD
42static int lttng_ust_abi_close_in_progress;
43
51489cad
MD
44static
45int lttng_abi_tracepoint_list(void);
46
f4681817
MD
47/*
48 * Object descriptor table. Should be protected from concurrent access
49 * by the caller.
50 */
51
b61ce3b2 52struct lttng_ust_obj {
f4681817
MD
53 union {
54 struct {
55 void *private_data;
b61ce3b2 56 const struct lttng_ust_objd_ops *ops;
f4681817
MD
57 int f_count;
58 } s;
59 int freelist_next; /* offset freelist. end is -1. */
60 } u;
61};
62
b61ce3b2
MD
63struct lttng_ust_objd_table {
64 struct lttng_ust_obj *array;
f4681817
MD
65 unsigned int len, allocated_len;
66 int freelist_head; /* offset freelist head. end is -1 */
67};
68
b61ce3b2 69static struct lttng_ust_objd_table objd_table = {
f4681817
MD
70 .freelist_head = -1,
71};
72
73static
b61ce3b2 74int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops)
f4681817 75{
b61ce3b2 76 struct lttng_ust_obj *obj;
f4681817
MD
77
78 if (objd_table.freelist_head != -1) {
79 obj = &objd_table.array[objd_table.freelist_head];
80 objd_table.freelist_head = obj->u.freelist_next;
81 goto end;
82 }
83
84 if (objd_table.len >= objd_table.allocated_len) {
85 unsigned int new_allocated_len, old_allocated_len;
b61ce3b2 86 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
87
88 old_allocated_len = objd_table.allocated_len;
89 old_table = objd_table.array;
90 if (!old_allocated_len)
91 new_allocated_len = 1;
92 else
93 new_allocated_len = old_allocated_len << 1;
b61ce3b2 94 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
95 if (!new_table)
96 return -ENOMEM;
97 memcpy(new_table, old_table,
b61ce3b2 98 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
99 free(old_table);
100 objd_table.array = new_table;
101 objd_table.allocated_len = new_allocated_len;
102 }
103 obj = &objd_table.array[objd_table.len];
104 objd_table.len++;
105end:
106 obj->u.s.private_data = private_data;
107 obj->u.s.ops = ops;
a4be8962
MD
108 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
109 /* count == 2 : allocated + hold ref */
f4681817
MD
110 return obj - objd_table.array;
111}
112
113static
b61ce3b2 114struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
115{
116 if (id >= objd_table.len)
117 return NULL;
a4be8962
MD
118 if (!objd_table.array[id].u.s.f_count)
119 return NULL;
f4681817
MD
120 return &objd_table.array[id];
121}
122
123static
124void *objd_private(int id)
125{
b61ce3b2 126 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
127 assert(obj);
128 return obj->u.s.private_data;
129}
130
131static
132void objd_set_private(int id, void *private_data)
133{
b61ce3b2 134 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
135 assert(obj);
136 obj->u.s.private_data = private_data;
137}
138
b61ce3b2 139const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 140{
b61ce3b2 141 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 142
a4be8962
MD
143 if (!obj)
144 return NULL;
f4681817
MD
145 return obj->u.s.ops;
146}
147
148static
149void objd_free(int id)
150{
b61ce3b2 151 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
152
153 assert(obj);
154 obj->u.freelist_next = objd_table.freelist_head;
155 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
156 assert(obj->u.s.f_count == 1);
157 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
158}
159
160static
161void objd_ref(int id)
162{
b61ce3b2 163 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
164 obj->u.s.f_count++;
165}
166
d4419b81 167int lttng_ust_objd_unref(int id)
f4681817 168{
b61ce3b2 169 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 170
1ea11eab
MD
171 if (!obj)
172 return -EINVAL;
a4be8962
MD
173 if (obj->u.s.f_count == 1) {
174 ERR("Reference counting error\n");
175 return -EINVAL;
176 }
177 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 178 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 179
f4681817
MD
180 if (ops->release)
181 ops->release(id);
182 objd_free(id);
183 }
1ea11eab 184 return 0;
f4681817
MD
185}
186
187static
188void objd_table_destroy(void)
189{
a4be8962
MD
190 int i;
191
fb50c39d 192 for (i = 0; i < objd_table.allocated_len; i++)
d4419b81 193 (void) lttng_ust_objd_unref(i);
f4681817 194 free(objd_table.array);
02fb3381
MD
195 objd_table.array = NULL;
196 objd_table.len = 0;
197 objd_table.allocated_len = 0;
17dfb34b 198 objd_table.freelist_head = -1;
f4681817
MD
199}
200
201/*
202 * This is LTTng's own personal way to create an ABI for sessiond.
203 * We send commands over a socket.
204 */
205
b61ce3b2
MD
206static const struct lttng_ust_objd_ops lttng_ops;
207static const struct lttng_ust_objd_ops lttng_session_ops;
208static const struct lttng_ust_objd_ops lttng_channel_ops;
209static const struct lttng_ust_objd_ops lttng_metadata_ops;
210static const struct lttng_ust_objd_ops lttng_event_ops;
211static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
51489cad 212static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
f4681817
MD
213
214enum channel_type {
215 PER_CPU_CHANNEL,
216 METADATA_CHANNEL,
217};
218
46050b1a
MD
219int lttng_abi_create_root_handle(void)
220{
221 int root_handle;
222
223 root_handle = objd_alloc(NULL, &lttng_ops);
46050b1a
MD
224 return root_handle;
225}
226
818173b9 227static
f4681817
MD
228int lttng_abi_create_session(void)
229{
230 struct ltt_session *session;
231 int session_objd, ret;
232
233 session = ltt_session_create();
234 if (!session)
235 return -ENOMEM;
236 session_objd = objd_alloc(session, &lttng_session_ops);
237 if (session_objd < 0) {
238 ret = session_objd;
239 goto objd_error;
240 }
241 session->objd = session_objd;
242 return session_objd;
243
244objd_error:
245 ltt_session_destroy(session);
246 return ret;
247}
248
f4681817
MD
249static
250long lttng_abi_tracer_version(int objd,
251 struct lttng_ust_tracer_version *v)
252{
23c8854a
MD
253 v->major = LTTNG_UST_MAJOR_VERSION;
254 v->minor = LTTNG_UST_MINOR_VERSION;
255 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
256 return 0;
257}
258
259static
260long lttng_abi_add_context(int objd,
261 struct lttng_ust_context *context_param,
262 struct lttng_ctx **ctx, struct ltt_session *session)
263{
264 if (session->been_active)
265 return -EPERM;
266
267 switch (context_param->ctx) {
8de38cf7
MD
268 case LTTNG_UST_CONTEXT_PTHREAD_ID:
269 return lttng_add_pthread_id_to_ctx(ctx);
3b402b40
MD
270 case LTTNG_UST_CONTEXT_VTID:
271 return lttng_add_vtid_to_ctx(ctx);
c1ef86f0
MD
272 case LTTNG_UST_CONTEXT_VPID:
273 return lttng_add_vpid_to_ctx(ctx);
4847e9bb
MD
274 case LTTNG_UST_CONTEXT_PROCNAME:
275 return lttng_add_procname_to_ctx(ctx);
f4681817
MD
276 default:
277 return -EINVAL;
278 }
279}
280
281/**
282 * lttng_cmd - lttng control through socket commands
283 *
284 * @objd: the object descriptor
285 * @cmd: the command
286 * @arg: command arg
287 *
288 * This descriptor implements lttng commands:
289 * LTTNG_UST_SESSION
290 * Returns a LTTng trace session object descriptor
291 * LTTNG_UST_TRACER_VERSION
292 * Returns the LTTng kernel tracer version
293 * LTTNG_UST_TRACEPOINT_LIST
294 * Returns a file descriptor listing available tracepoints
295 * LTTNG_UST_WAIT_QUIESCENT
296 * Returns after all previously running probes have completed
297 *
298 * The returned session will be deleted when its file descriptor is closed.
299 */
300static
301long lttng_cmd(int objd, unsigned int cmd, unsigned long arg)
302{
303 switch (cmd) {
304 case LTTNG_UST_SESSION:
305 return lttng_abi_create_session();
306 case LTTNG_UST_TRACER_VERSION:
307 return lttng_abi_tracer_version(objd,
308 (struct lttng_ust_tracer_version *) arg);
309 case LTTNG_UST_TRACEPOINT_LIST:
51489cad 310 return lttng_abi_tracepoint_list();
f4681817
MD
311 case LTTNG_UST_WAIT_QUIESCENT:
312 synchronize_trace();
313 return 0;
314 default:
315 return -EINVAL;
316 }
317}
318
b61ce3b2 319static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
320 .cmd = lttng_cmd,
321};
322
323/*
324 * We tolerate no failure in this function (if one happens, we print a dmesg
325 * error, but cannot return any error, because the channel information is
326 * invariant.
327 */
328static
329void lttng_metadata_create_events(int channel_objd)
330{
331 struct ltt_channel *channel = objd_private(channel_objd);
332 static struct lttng_ust_event metadata_params = {
333 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 334 .name = "lttng_ust:metadata",
f4681817
MD
335 };
336 struct ltt_event *event;
576599a0 337 int ret;
f4681817
MD
338
339 /*
340 * We tolerate no failure path after event creation. It will stay
341 * invariant for the rest of the session.
342 */
576599a0
MD
343 ret = ltt_event_create(channel, &metadata_params, NULL, &event);
344 if (ret < 0) {
f4681817
MD
345 goto create_error;
346 }
347 return;
348
349create_error:
350 WARN_ON(1);
351 return; /* not allowed to return error */
352}
353
f4681817
MD
354int lttng_abi_create_channel(int session_objd,
355 struct lttng_ust_channel *chan_param,
356 enum channel_type channel_type)
357{
358 struct ltt_session *session = objd_private(session_objd);
b61ce3b2 359 const struct lttng_ust_objd_ops *ops;
f4681817
MD
360 const char *transport_name;
361 struct ltt_channel *chan;
362 int chan_objd;
363 int ret = 0;
d028eddb 364 struct ltt_channel chan_priv_init;
f4681817 365
f4681817
MD
366 switch (channel_type) {
367 case PER_CPU_CHANNEL:
368 if (chan_param->output == LTTNG_UST_MMAP) {
369 transport_name = chan_param->overwrite ?
370 "relay-overwrite-mmap" : "relay-discard-mmap";
371 } else {
372 return -EINVAL;
373 }
374 ops = &lttng_channel_ops;
375 break;
376 case METADATA_CHANNEL:
377 if (chan_param->output == LTTNG_UST_MMAP)
378 transport_name = "relay-metadata-mmap";
379 else
380 return -EINVAL;
381 ops = &lttng_metadata_ops;
382 break;
383 default:
384 transport_name = "<unknown>";
fe38d4af
MD
385 return -EINVAL;
386 }
387 chan_objd = objd_alloc(NULL, ops);
388 if (chan_objd < 0) {
389 ret = chan_objd;
390 goto objd_error;
f4681817 391 }
d028eddb
MD
392 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
393 /* Copy of session UUID for consumer (availability through shm) */
394 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
395
f4681817
MD
396 /*
397 * We tolerate no failure path after channel creation. It will stay
398 * invariant for the rest of the session.
399 */
400 chan = ltt_channel_create(session, transport_name, NULL,
401 chan_param->subbuf_size,
402 chan_param->num_subbuf,
403 chan_param->switch_timer_interval,
193183fb
MD
404 chan_param->read_timer_interval,
405 &chan_param->shm_fd,
406 &chan_param->wait_fd,
d028eddb
MD
407 &chan_param->memory_map_size,
408 &chan_priv_init);
f4681817
MD
409 if (!chan) {
410 ret = -EINVAL;
411 goto chan_error;
412 }
413 objd_set_private(chan_objd, chan);
414 chan->objd = chan_objd;
415 if (channel_type == METADATA_CHANNEL) {
416 session->metadata = chan;
417 lttng_metadata_create_events(chan_objd);
418 }
f4681817
MD
419 /* The channel created holds a reference on the session */
420 objd_ref(session_objd);
421
422 return chan_objd;
423
424chan_error:
1ea11eab
MD
425 {
426 int err;
427
d4419b81 428 err = lttng_ust_objd_unref(chan_objd);
1ea11eab
MD
429 assert(!err);
430 }
f4681817
MD
431objd_error:
432 return ret;
433}
434
435/**
436 * lttng_session_cmd - lttng session object command
437 *
438 * @obj: the object
439 * @cmd: the command
440 * @arg: command arg
441 *
442 * This descriptor implements lttng commands:
443 * LTTNG_UST_CHANNEL
444 * Returns a LTTng channel object descriptor
445 * LTTNG_UST_ENABLE
446 * Enables tracing for a session (weak enable)
447 * LTTNG_UST_DISABLE
448 * Disables tracing for a session (strong disable)
449 * LTTNG_UST_METADATA
450 * Returns a LTTng metadata object descriptor
451 *
452 * The returned channel will be deleted when its file descriptor is closed.
453 */
454static
455long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg)
456{
457 struct ltt_session *session = objd_private(objd);
458
459 switch (cmd) {
460 case LTTNG_UST_CHANNEL:
461 return lttng_abi_create_channel(objd,
462 (struct lttng_ust_channel *) arg,
463 PER_CPU_CHANNEL);
464 case LTTNG_UST_SESSION_START:
465 case LTTNG_UST_ENABLE:
466 return ltt_session_enable(session);
467 case LTTNG_UST_SESSION_STOP:
468 case LTTNG_UST_DISABLE:
469 return ltt_session_disable(session);
470 case LTTNG_UST_METADATA:
471 return lttng_abi_create_channel(objd,
472 (struct lttng_ust_channel *) arg,
473 METADATA_CHANNEL);
474 default:
475 return -EINVAL;
476 }
477}
478
479/*
480 * Called when the last file reference is dropped.
481 *
482 * Big fat note: channels and events are invariant for the whole session after
483 * their creation. So this session destruction also destroys all channel and
484 * event structures specific to this session (they are not destroyed when their
485 * individual file is released).
486 */
487static
1ea11eab 488int lttng_release_session(int objd)
f4681817
MD
489{
490 struct ltt_session *session = objd_private(objd);
491
1ea11eab 492 if (session) {
f4681817 493 ltt_session_destroy(session);
1ea11eab
MD
494 return 0;
495 } else {
496 return -EINVAL;
497 }
f4681817
MD
498}
499
b61ce3b2 500static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 501 .release = lttng_release_session,
f4681817
MD
502 .cmd = lttng_session_cmd,
503};
504
51489cad
MD
505/*
506 * beware: we don't keep the mutex over the send, but we must walk the
507 * whole list each time we are called again. So sending one tracepoint
508 * at a time means this is O(n^2). TODO: do as in the kernel and send
509 * multiple tracepoints for each call to amortize this cost.
510 */
511static
512void ltt_tracepoint_list_get(struct ltt_tracepoint_list *list,
cbef6901 513 struct lttng_ust_tracepoint_iter *tracepoint)
51489cad 514{
40e8d353 515next:
51489cad
MD
516 if (!list->got_first) {
517 tracepoint_iter_start(&list->iter);
518 list->got_first = 1;
519 goto copy;
520 }
521 tracepoint_iter_next(&list->iter);
522copy:
523 if (!list->iter.tracepoint) {
cbef6901 524 tracepoint->name[0] = '\0'; /* end of list */
51489cad 525 } else {
a4ada9b8
MD
526 if (!strcmp((*list->iter.tracepoint)->name,
527 "lttng_ust:metadata"))
40e8d353 528 goto next;
cbef6901 529 memcpy(tracepoint->name, (*list->iter.tracepoint)->name,
51489cad 530 LTTNG_UST_SYM_NAME_LEN);
cbef6901
MD
531#if 0
532 if ((*list->iter.tracepoint)->loglevel) {
533 memcpy(tracepoint->loglevel,
534 (*list->iter.tracepoint)->loglevel->identifier,
535 LTTNG_UST_SYM_NAME_LEN);
536 tracepoint->loglevel_value =
537 (*list->iter.tracepoint)->loglevel->value;
538 } else {
539#endif
540 tracepoint->loglevel[0] = '\0';
541 tracepoint->loglevel_value = 0;
542#if 0
543 }
544#endif
51489cad
MD
545 }
546}
547
548static
549long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg)
550{
551 struct ltt_tracepoint_list *list = objd_private(objd);
cbef6901
MD
552 struct lttng_ust_tracepoint_iter *tp =
553 (struct lttng_ust_tracepoint_iter *) arg;
51489cad
MD
554
555 switch (cmd) {
556 case LTTNG_UST_TRACEPOINT_LIST_GET:
cbef6901
MD
557 ltt_tracepoint_list_get(list, tp);
558 if (tp->name[0] == '\0')
b115631f 559 return -ENOENT;
51489cad
MD
560 return 0;
561 default:
562 return -EINVAL;
563 }
564}
565
566static
567int lttng_abi_tracepoint_list(void)
568{
569 int list_objd, ret;
570 struct ltt_tracepoint_list *list;
571
572 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
573 if (list_objd < 0) {
574 ret = list_objd;
575 goto objd_error;
576 }
577 list = zmalloc(sizeof(*list));
578 if (!list) {
579 ret = -ENOMEM;
580 goto alloc_error;
581 }
582 objd_set_private(list_objd, list);
583
584 return list_objd;
585
586alloc_error:
587 {
588 int err;
589
590 err = lttng_ust_objd_unref(list_objd);
591 assert(!err);
592 }
593objd_error:
594 return ret;
595}
596
597static
598int lttng_release_tracepoint_list(int objd)
599{
600 struct ltt_tracepoint_list *list = objd_private(objd);
601
602 if (list) {
603 tracepoint_iter_stop(&list->iter);
604 free(list);
605 return 0;
606 } else {
607 return -EINVAL;
608 }
609}
610
611static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
612 .release = lttng_release_tracepoint_list,
613 .cmd = lttng_tracepoint_list_cmd,
614};
615
381c0f1e 616struct stream_priv_data {
4cfec15c 617 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
618 struct ltt_channel *ltt_chan;
619};
620
f4681817 621static
381c0f1e 622int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info)
f4681817
MD
623{
624 struct ltt_channel *channel = objd_private(channel_objd);
4cfec15c 625 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e 626 struct stream_priv_data *priv;
f4681817
MD
627 int stream_objd, ret;
628
381c0f1e
MD
629 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
630 &info->shm_fd, &info->wait_fd, &info->memory_map_size);
f4681817
MD
631 if (!buf)
632 return -ENOENT;
633
381c0f1e
MD
634 priv = zmalloc(sizeof(*priv));
635 if (!priv) {
636 ret = -ENOMEM;
637 goto alloc_error;
638 }
639 priv->buf = buf;
640 priv->ltt_chan = channel;
641 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
f4681817
MD
642 if (stream_objd < 0) {
643 ret = stream_objd;
644 goto objd_error;
645 }
381c0f1e
MD
646 /* Hold a reference on the channel object descriptor */
647 objd_ref(channel_objd);
f4681817
MD
648 return stream_objd;
649
650objd_error:
381c0f1e
MD
651 free(priv);
652alloc_error:
653 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
654 return ret;
655}
f4681817
MD
656
657static
658int lttng_abi_create_event(int channel_objd,
659 struct lttng_ust_event *event_param)
660{
661 struct ltt_channel *channel = objd_private(channel_objd);
662 struct ltt_event *event;
663 int event_objd, ret;
664
665 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
666 event_objd = objd_alloc(NULL, &lttng_event_ops);
667 if (event_objd < 0) {
668 ret = event_objd;
669 goto objd_error;
670 }
671 /*
672 * We tolerate no failure path after event creation. It will stay
673 * invariant for the rest of the session.
674 */
576599a0
MD
675 ret = ltt_event_create(channel, event_param, NULL, &event);
676 if (ret < 0) {
f4681817
MD
677 goto event_error;
678 }
679 objd_set_private(event_objd, event);
680 /* The event holds a reference on the channel */
681 objd_ref(channel_objd);
682 return event_objd;
683
684event_error:
1ea11eab
MD
685 {
686 int err;
687
d4419b81 688 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
689 assert(!err);
690 }
f4681817
MD
691objd_error:
692 return ret;
693}
694
695/**
696 * lttng_channel_cmd - lttng control through object descriptors
697 *
698 * @objd: the object descriptor
699 * @cmd: the command
700 * @arg: command arg
701 *
702 * This object descriptor implements lttng commands:
703 * LTTNG_UST_STREAM
704 * Returns an event stream object descriptor or failure.
705 * (typically, one event stream records events from one CPU)
706 * LTTNG_UST_EVENT
707 * Returns an event object descriptor or failure.
708 * LTTNG_UST_CONTEXT
709 * Prepend a context field to each event in the channel
710 * LTTNG_UST_ENABLE
711 * Enable recording for events in this channel (weak enable)
712 * LTTNG_UST_DISABLE
713 * Disable recording for events in this channel (strong disable)
714 *
715 * Channel and event file descriptors also hold a reference on the session.
716 */
717static
718long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg)
719{
720 struct ltt_channel *channel = objd_private(objd);
721
722 switch (cmd) {
723 case LTTNG_UST_STREAM:
381c0f1e
MD
724 {
725 struct lttng_ust_stream *stream;
726
727 stream = (struct lttng_ust_stream *) arg;
728 /* stream used as output */
729 return lttng_abi_open_stream(objd, stream);
730 }
f4681817
MD
731 case LTTNG_UST_EVENT:
732 return lttng_abi_create_event(objd, (struct lttng_ust_event *) arg);
733 case LTTNG_UST_CONTEXT:
734 return lttng_abi_add_context(objd,
735 (struct lttng_ust_context *) arg,
736 &channel->ctx, channel->session);
737 case LTTNG_UST_ENABLE:
738 return ltt_channel_enable(channel);
739 case LTTNG_UST_DISABLE:
740 return ltt_channel_disable(channel);
43d330a4
MD
741 case LTTNG_UST_FLUSH_BUFFER:
742 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
743 default:
744 return -EINVAL;
745 }
746}
747
748/**
749 * lttng_metadata_cmd - lttng control through object descriptors
750 *
751 * @objd: the object descriptor
752 * @cmd: the command
753 * @arg: command arg
754 *
755 * This object descriptor implements lttng commands:
756 * LTTNG_UST_STREAM
757 * Returns an event stream file descriptor or failure.
758 *
759 * Channel and event file descriptors also hold a reference on the session.
760 */
761static
762long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg)
763{
43861eab
MD
764 struct ltt_channel *channel = objd_private(objd);
765
f4681817
MD
766 switch (cmd) {
767 case LTTNG_UST_STREAM:
381c0f1e
MD
768 {
769 struct lttng_ust_stream *stream;
770
771 stream = (struct lttng_ust_stream *) arg;
772 /* stream used as output */
773 return lttng_abi_open_stream(objd, stream);
774 }
43d330a4
MD
775 case LTTNG_UST_FLUSH_BUFFER:
776 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
777 default:
778 return -EINVAL;
779 }
780}
781
782#if 0
783/**
784 * lttng_channel_poll - lttng stream addition/removal monitoring
785 *
786 * @file: the file
787 * @wait: poll table
788 */
789unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
790{
791 struct ltt_channel *channel = file->private_data;
792 unsigned int mask = 0;
793
794 if (file->f_mode & FMODE_READ) {
795 poll_wait_set_exclusive(wait);
796 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
797 wait);
798
799 if (channel->ops->is_disabled(channel->chan))
800 return POLLERR;
801 if (channel->ops->is_finalized(channel->chan))
802 return POLLHUP;
803 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
804 return POLLIN | POLLRDNORM;
805 return 0;
806 }
807 return mask;
808
809}
810#endif //0
811
812static
813int lttng_channel_release(int objd)
814{
815 struct ltt_channel *channel = objd_private(objd);
816
817 if (channel)
d4419b81 818 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
819 return 0;
820}
821
b61ce3b2 822static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817
MD
823 .release = lttng_channel_release,
824 //.poll = lttng_channel_poll,
825 .cmd = lttng_channel_cmd,
826};
827
b61ce3b2 828static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
829 .release = lttng_channel_release,
830 .cmd = lttng_metadata_cmd,
831};
832
381c0f1e
MD
833/**
834 * lttng_rb_cmd - lttng ring buffer control through object descriptors
835 *
836 * @objd: the object descriptor
837 * @cmd: the command
838 * @arg: command arg
839 *
840 * This object descriptor implements lttng commands:
841 * (None for now. Access is done directly though shm.)
381c0f1e
MD
842 */
843static
844long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg)
845{
381c0f1e
MD
846 switch (cmd) {
847 default:
848 return -EINVAL;
849 }
850}
851
852static
853int lttng_rb_release(int objd)
854{
855 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 856 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
857 struct ltt_channel *channel;
858
859 if (priv) {
860 buf = priv->buf;
861 channel = priv->ltt_chan;
862 free(priv);
45e9e699
MD
863 /*
864 * If we are at ABI exit, we don't want to close the
865 * buffer opened for read: it is being shared between
866 * the parent and child (right after fork), and we don't
867 * want the child to close it for the parent. For a real
868 * exit, we don't care about marking it as closed, as
869 * the consumer daemon (if there is one) will do fine
870 * even if we don't mark it as "closed" for reading on
871 * our side.
872 * We only mark it as closed if it is being explicitely
873 * released by the session daemon with an explicit
874 * release command.
875 */
876 if (!lttng_ust_abi_close_in_progress)
877 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 878
d4419b81 879 return lttng_ust_objd_unref(channel->objd);
381c0f1e
MD
880 }
881 return 0;
882}
883
b61ce3b2 884static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
885 .release = lttng_rb_release,
886 .cmd = lttng_rb_cmd,
887};
888
f4681817
MD
889/**
890 * lttng_event_cmd - lttng control through object descriptors
891 *
892 * @objd: the object descriptor
893 * @cmd: the command
894 * @arg: command arg
895 *
896 * This object descriptor implements lttng commands:
897 * LTTNG_UST_CONTEXT
898 * Prepend a context field to each record of this event
899 * LTTNG_UST_ENABLE
900 * Enable recording for this event (weak enable)
901 * LTTNG_UST_DISABLE
902 * Disable recording for this event (strong disable)
903 */
904static
905long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg)
906{
907 struct ltt_event *event = objd_private(objd);
908
909 switch (cmd) {
910 case LTTNG_UST_CONTEXT:
911 return lttng_abi_add_context(objd,
912 (struct lttng_ust_context *) arg,
913 &event->ctx, event->chan->session);
914 case LTTNG_UST_ENABLE:
915 return ltt_event_enable(event);
916 case LTTNG_UST_DISABLE:
917 return ltt_event_disable(event);
918 default:
919 return -EINVAL;
920 }
921}
922
923static
924int lttng_event_release(int objd)
925{
926 struct ltt_event *event = objd_private(objd);
927
928 if (event)
d4419b81 929 return lttng_ust_objd_unref(event->chan->objd);
f4681817
MD
930 return 0;
931}
932
933/* TODO: filter control ioctl */
b61ce3b2 934static const struct lttng_ust_objd_ops lttng_event_ops = {
f4681817
MD
935 .release = lttng_event_release,
936 .cmd = lttng_event_cmd,
937};
938
b35d179d 939void lttng_ust_abi_exit(void)
f4681817 940{
45e9e699 941 lttng_ust_abi_close_in_progress = 1;
f4681817 942 objd_table_destroy();
45e9e699 943 lttng_ust_abi_close_in_progress = 0;
f4681817 944}
This page took 0.064653 seconds and 4 git commands to generate.