4 * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
24 * Dual LGPL v2.1/GPL v2 license.
27 #include <ust/lttng-ust-abi.h>
28 #include <urcu/compiler.h>
29 #include <urcu/list.h>
30 #include <ust/lttng-events.h>
31 #include <ust/usterr-signal-safe.h>
33 #include "ltt-tracer.h"
36 * Object descriptor table. Should be protected from concurrent access
44 const struct objd_ops
*ops
;
47 int freelist_next
; /* offset freelist. end is -1. */
53 unsigned int len
, allocated_len
;
54 int freelist_head
; /* offset freelist head. end is -1 */
57 static struct objd_table objd_table
= {
62 int objd_alloc(void *private_data
, const struct objd_ops
*ops
)
66 if (objd_table
.freelist_head
!= -1) {
67 obj
= &objd_table
.array
[objd_table
.freelist_head
];
68 objd_table
.freelist_head
= obj
->u
.freelist_next
;
72 if (objd_table
.len
>= objd_table
.allocated_len
) {
73 unsigned int new_allocated_len
, old_allocated_len
;
74 struct obj
*new_table
, *old_table
;
76 old_allocated_len
= objd_table
.allocated_len
;
77 old_table
= objd_table
.array
;
78 if (!old_allocated_len
)
79 new_allocated_len
= 1;
81 new_allocated_len
= old_allocated_len
<< 1;
82 new_table
= zmalloc(sizeof(struct obj
) * new_allocated_len
);
85 memcpy(new_table
, old_table
,
86 sizeof(struct obj
) * old_allocated_len
);
88 objd_table
.array
= new_table
;
89 objd_table
.allocated_len
= new_allocated_len
;
91 obj
= &objd_table
.array
[objd_table
.len
];
94 obj
->u
.s
.private_data
= private_data
;
96 obj
->u
.s
.f_count
= 2; /* count == 1 : object is allocated */
97 /* count == 2 : allocated + hold ref */
98 return obj
- objd_table
.array
;
102 struct obj
*_objd_get(int id
)
104 if (id
>= objd_table
.len
)
106 if (!objd_table
.array
[id
].u
.s
.f_count
)
108 return &objd_table
.array
[id
];
112 void *objd_private(int id
)
114 struct obj
*obj
= _objd_get(id
);
116 return obj
->u
.s
.private_data
;
120 void objd_set_private(int id
, void *private_data
)
122 struct obj
*obj
= _objd_get(id
);
124 obj
->u
.s
.private_data
= private_data
;
127 const struct objd_ops
*objd_ops(int id
)
129 struct obj
*obj
= _objd_get(id
);
137 void objd_free(int id
)
139 struct obj
*obj
= _objd_get(id
);
142 obj
->u
.freelist_next
= objd_table
.freelist_head
;
143 objd_table
.freelist_head
= obj
- objd_table
.array
;
144 assert(obj
->u
.s
.f_count
== 1);
145 obj
->u
.s
.f_count
= 0; /* deallocated */
149 void objd_ref(int id
)
151 struct obj
*obj
= _objd_get(id
);
155 int objd_unref(int id
)
157 struct obj
*obj
= _objd_get(id
);
161 if (obj
->u
.s
.f_count
== 1) {
162 ERR("Reference counting error\n");
165 if ((--obj
->u
.s
.f_count
) == 1) {
166 const struct objd_ops
*ops
= objd_ops(id
);
176 void objd_table_destroy(void)
180 for (i
= 0; i
< objd_table
.allocated_len
; i
++) {
181 struct obj
*obj
= _objd_get(i
);
182 const struct objd_ops
*ops
;
190 free(objd_table
.array
);
191 objd_table
.freelist_head
= -1;
195 * This is LTTng's own personal way to create an ABI for sessiond.
196 * We send commands over a socket.
199 static const struct objd_ops lttng_ops
;
200 static const struct objd_ops lttng_session_ops
;
201 static const struct objd_ops lttng_channel_ops
;
202 static const struct objd_ops lttng_metadata_ops
;
203 static const struct objd_ops lttng_event_ops
;
210 int lttng_abi_create_root_handle(void)
214 root_handle
= objd_alloc(NULL
, <tng_ops
);
215 assert(root_handle
== 0);
220 int lttng_abi_create_session(void)
222 struct ltt_session
*session
;
223 int session_objd
, ret
;
225 session
= ltt_session_create();
228 session_objd
= objd_alloc(session
, <tng_session_ops
);
229 if (session_objd
< 0) {
233 session
->objd
= session_objd
;
237 ltt_session_destroy(session
);
243 int lttng_abi_tracepoint_list(void)
247 /* TODO: Create list private data */
248 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
262 long lttng_abi_tracer_version(int objd
,
263 struct lttng_ust_tracer_version
*v
)
265 v
->version
= LTTNG_UST_VERSION
;
266 v
->patchlevel
= LTTNG_UST_PATCHLEVEL
;
267 v
->sublevel
= LTTNG_UST_SUBLEVEL
;
272 long lttng_abi_add_context(int objd
,
273 struct lttng_ust_context
*context_param
,
274 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
276 if (session
->been_active
)
279 switch (context_param
->ctx
) {
280 case LTTNG_UST_CONTEXT_VTID
:
281 //TODO return lttng_add_vtid_to_ctx(ctx);
288 * lttng_cmd - lttng control through socket commands
290 * @objd: the object descriptor
294 * This descriptor implements lttng commands:
296 * Returns a LTTng trace session object descriptor
297 * LTTNG_UST_TRACER_VERSION
298 * Returns the LTTng kernel tracer version
299 * LTTNG_UST_TRACEPOINT_LIST
300 * Returns a file descriptor listing available tracepoints
301 * LTTNG_UST_WAIT_QUIESCENT
302 * Returns after all previously running probes have completed
304 * The returned session will be deleted when its file descriptor is closed.
307 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
310 case LTTNG_UST_SESSION
:
311 return lttng_abi_create_session();
312 case LTTNG_UST_TRACER_VERSION
:
313 return lttng_abi_tracer_version(objd
,
314 (struct lttng_ust_tracer_version
*) arg
);
315 case LTTNG_UST_TRACEPOINT_LIST
:
316 return -ENOSYS
; //TODO
317 //return lttng_abi_tracepoint_list();
318 case LTTNG_UST_WAIT_QUIESCENT
:
326 static const struct objd_ops lttng_ops
= {
331 * We tolerate no failure in this function (if one happens, we print a dmesg
332 * error, but cannot return any error, because the channel information is
336 void lttng_metadata_create_events(int channel_objd
)
338 struct ltt_channel
*channel
= objd_private(channel_objd
);
339 static struct lttng_ust_event metadata_params
= {
340 .instrumentation
= LTTNG_UST_TRACEPOINT
,
341 .name
= "lttng_metadata",
343 struct ltt_event
*event
;
347 * We tolerate no failure path after event creation. It will stay
348 * invariant for the rest of the session.
350 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
359 return; /* not allowed to return error */
362 int lttng_abi_create_channel(int session_objd
,
363 struct lttng_ust_channel
*chan_param
,
364 enum channel_type channel_type
)
366 struct ltt_session
*session
= objd_private(session_objd
);
367 const struct objd_ops
*ops
;
368 const char *transport_name
;
369 struct ltt_channel
*chan
;
373 chan_objd
= objd_alloc(NULL
, <tng_channel_ops
);
378 switch (channel_type
) {
379 case PER_CPU_CHANNEL
:
380 if (chan_param
->output
== LTTNG_UST_MMAP
) {
381 transport_name
= chan_param
->overwrite
?
382 "relay-overwrite-mmap" : "relay-discard-mmap";
386 ops
= <tng_channel_ops
;
388 case METADATA_CHANNEL
:
389 if (chan_param
->output
== LTTNG_UST_MMAP
)
390 transport_name
= "relay-metadata-mmap";
393 ops
= <tng_metadata_ops
;
396 transport_name
= "<unknown>";
400 * We tolerate no failure path after channel creation. It will stay
401 * invariant for the rest of the session.
403 chan
= ltt_channel_create(session
, transport_name
, NULL
,
404 chan_param
->subbuf_size
,
405 chan_param
->num_subbuf
,
406 chan_param
->switch_timer_interval
,
407 chan_param
->read_timer_interval
);
412 objd_set_private(chan_objd
, chan
);
413 chan
->objd
= chan_objd
;
414 if (channel_type
== METADATA_CHANNEL
) {
415 session
->metadata
= chan
;
416 lttng_metadata_create_events(chan_objd
);
419 /* The channel created holds a reference on the session */
420 objd_ref(session_objd
);
428 err
= objd_unref(chan_objd
);
436 * lttng_session_cmd - lttng session object command
442 * This descriptor implements lttng commands:
444 * Returns a LTTng channel object descriptor
446 * Enables tracing for a session (weak enable)
448 * Disables tracing for a session (strong disable)
450 * Returns a LTTng metadata object descriptor
452 * The returned channel will be deleted when its file descriptor is closed.
455 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
457 struct ltt_session
*session
= objd_private(objd
);
460 case LTTNG_UST_CHANNEL
:
461 return lttng_abi_create_channel(objd
,
462 (struct lttng_ust_channel
*) arg
,
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
,
480 * Called when the last file reference is dropped.
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).
488 int lttng_release_session(int objd
)
490 struct ltt_session
*session
= objd_private(objd
);
493 ltt_session_destroy(session
);
500 static const struct objd_ops lttng_session_ops
= {
501 .release
= lttng_release_session
,
502 .cmd
= lttng_session_cmd
,
507 int lttng_abi_open_stream(int channel_objd
)
509 struct ltt_channel
*channel
= objd_private(channel_objd
);
510 struct lib_ring_buffer
*buf
;
511 int stream_objd
, ret
;
513 buf
= channel
->ops
->buffer_read_open(channel
->chan
);
517 stream_objd
= objd_alloc(buf
, &lib_ring_buffer_objd_ops
);
518 if (stream_objd
< 0) {
523 * The stream holds a reference to the channel within the generic ring
524 * buffer library, so no need to hold a refcount on the channel and
525 * session files here.
530 channel
->ops
->buffer_read_close(buf
);
536 int lttng_abi_create_event(int channel_objd
,
537 struct lttng_ust_event
*event_param
)
539 struct ltt_channel
*channel
= objd_private(channel_objd
);
540 struct ltt_event
*event
;
543 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
544 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
545 if (event_objd
< 0) {
550 * We tolerate no failure path after event creation. It will stay
551 * invariant for the rest of the session.
553 event
= ltt_event_create(channel
, event_param
, NULL
);
558 objd_set_private(event_objd
, event
);
559 /* The event holds a reference on the channel */
560 objd_ref(channel_objd
);
567 err
= objd_unref(event_objd
);
575 * lttng_channel_cmd - lttng control through object descriptors
577 * @objd: the object descriptor
581 * This object descriptor implements lttng commands:
583 * Returns an event stream object descriptor or failure.
584 * (typically, one event stream records events from one CPU)
586 * Returns an event object descriptor or failure.
588 * Prepend a context field to each event in the channel
590 * Enable recording for events in this channel (weak enable)
592 * Disable recording for events in this channel (strong disable)
594 * Channel and event file descriptors also hold a reference on the session.
597 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
599 struct ltt_channel
*channel
= objd_private(objd
);
602 case LTTNG_UST_STREAM
:
603 return -ENOSYS
; //TODO
604 //return lttng_abi_open_stream(objd);
605 case LTTNG_UST_EVENT
:
606 return lttng_abi_create_event(objd
, (struct lttng_ust_event
*) arg
);
607 case LTTNG_UST_CONTEXT
:
608 return lttng_abi_add_context(objd
,
609 (struct lttng_ust_context
*) arg
,
610 &channel
->ctx
, channel
->session
);
611 case LTTNG_UST_ENABLE
:
612 return ltt_channel_enable(channel
);
613 case LTTNG_UST_DISABLE
:
614 return ltt_channel_disable(channel
);
621 * lttng_metadata_cmd - lttng control through object descriptors
623 * @objd: the object descriptor
627 * This object descriptor implements lttng commands:
629 * Returns an event stream file descriptor or failure.
631 * Channel and event file descriptors also hold a reference on the session.
634 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
637 case LTTNG_UST_STREAM
:
638 return -ENOSYS
; //TODO
639 //return lttng_abi_open_stream(objd);
647 * lttng_channel_poll - lttng stream addition/removal monitoring
652 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
654 struct ltt_channel
*channel
= file
->private_data
;
655 unsigned int mask
= 0;
657 if (file
->f_mode
& FMODE_READ
) {
658 poll_wait_set_exclusive(wait
);
659 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
662 if (channel
->ops
->is_disabled(channel
->chan
))
664 if (channel
->ops
->is_finalized(channel
->chan
))
666 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
667 return POLLIN
| POLLRDNORM
;
676 int lttng_channel_release(int objd
)
678 struct ltt_channel
*channel
= objd_private(objd
);
681 return objd_unref(channel
->session
->objd
);
685 static const struct objd_ops lttng_channel_ops
= {
686 .release
= lttng_channel_release
,
687 //.poll = lttng_channel_poll,
688 .cmd
= lttng_channel_cmd
,
691 static const struct objd_ops lttng_metadata_ops
= {
692 .release
= lttng_channel_release
,
693 .cmd
= lttng_metadata_cmd
,
697 * lttng_event_cmd - lttng control through object descriptors
699 * @objd: the object descriptor
703 * This object descriptor implements lttng commands:
705 * Prepend a context field to each record of this event
707 * Enable recording for this event (weak enable)
709 * Disable recording for this event (strong disable)
712 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
714 struct ltt_event
*event
= objd_private(objd
);
717 case LTTNG_UST_CONTEXT
:
718 return lttng_abi_add_context(objd
,
719 (struct lttng_ust_context
*) arg
,
720 &event
->ctx
, event
->chan
->session
);
721 case LTTNG_UST_ENABLE
:
722 return ltt_event_enable(event
);
723 case LTTNG_UST_DISABLE
:
724 return ltt_event_disable(event
);
731 int lttng_event_release(int objd
)
733 struct ltt_event
*event
= objd_private(objd
);
736 return objd_unref(event
->chan
->objd
);
740 /* TODO: filter control ioctl */
741 static const struct objd_ops lttng_event_ops
= {
742 .release
= lttng_event_release
,
743 .cmd
= lttng_event_cmd
,
746 void lttng_ust_abi_exit(void)
748 objd_table_destroy();