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
40 struct lttng_ust_obj
{
44 const struct lttng_ust_objd_ops
*ops
;
47 int freelist_next
; /* offset freelist. end is -1. */
51 struct lttng_ust_objd_table
{
52 struct lttng_ust_obj
*array
;
53 unsigned int len
, allocated_len
;
54 int freelist_head
; /* offset freelist head. end is -1 */
57 static struct lttng_ust_objd_table objd_table
= {
62 int objd_alloc(void *private_data
, const struct lttng_ust_objd_ops
*ops
)
64 struct lttng_ust_obj
*obj
;
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 lttng_ust_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 lttng_ust_obj
) * new_allocated_len
);
85 memcpy(new_table
, old_table
,
86 sizeof(struct lttng_ust_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 lttng_ust_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 lttng_ust_obj
*obj
= _objd_get(id
);
116 return obj
->u
.s
.private_data
;
120 void objd_set_private(int id
, void *private_data
)
122 struct lttng_ust_obj
*obj
= _objd_get(id
);
124 obj
->u
.s
.private_data
= private_data
;
127 const struct lttng_ust_objd_ops
*objd_ops(int id
)
129 struct lttng_ust_obj
*obj
= _objd_get(id
);
137 void objd_free(int id
)
139 struct lttng_ust_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 lttng_ust_obj
*obj
= _objd_get(id
);
155 int lttng_ust_objd_unref(int id
)
157 struct lttng_ust_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 lttng_ust_objd_ops
*ops
= objd_ops(id
);
176 void objd_table_destroy(void)
180 for (i
= 0; i
< objd_table
.allocated_len
; i
++)
181 (void) lttng_ust_objd_unref(i
);
182 free(objd_table
.array
);
183 objd_table
.array
= NULL
;
185 objd_table
.allocated_len
= 0;
186 objd_table
.freelist_head
= -1;
190 * This is LTTng's own personal way to create an ABI for sessiond.
191 * We send commands over a socket.
194 static const struct lttng_ust_objd_ops lttng_ops
;
195 static const struct lttng_ust_objd_ops lttng_session_ops
;
196 static const struct lttng_ust_objd_ops lttng_channel_ops
;
197 static const struct lttng_ust_objd_ops lttng_metadata_ops
;
198 static const struct lttng_ust_objd_ops lttng_event_ops
;
199 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
;
206 int lttng_abi_create_root_handle(void)
210 root_handle
= objd_alloc(NULL
, <tng_ops
);
215 int lttng_abi_create_session(void)
217 struct ltt_session
*session
;
218 int session_objd
, ret
;
220 session
= ltt_session_create();
223 session_objd
= objd_alloc(session
, <tng_session_ops
);
224 if (session_objd
< 0) {
228 session
->objd
= session_objd
;
232 ltt_session_destroy(session
);
238 int lttng_abi_tracepoint_list(void)
242 /* TODO: Create list private data */
243 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
257 long lttng_abi_tracer_version(int objd
,
258 struct lttng_ust_tracer_version
*v
)
260 v
->version
= LTTNG_UST_VERSION
;
261 v
->patchlevel
= LTTNG_UST_PATCHLEVEL
;
262 v
->sublevel
= LTTNG_UST_SUBLEVEL
;
267 long lttng_abi_add_context(int objd
,
268 struct lttng_ust_context
*context_param
,
269 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
271 if (session
->been_active
)
274 switch (context_param
->ctx
) {
275 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
276 return lttng_add_pthread_id_to_ctx(ctx
);
277 case LTTNG_UST_CONTEXT_VTID
:
278 return lttng_add_vtid_to_ctx(ctx
);
279 case LTTNG_UST_CONTEXT_VPID
:
280 return lttng_add_vpid_to_ctx(ctx
);
281 case LTTNG_UST_CONTEXT_PROCNAME
:
282 return lttng_add_procname_to_ctx(ctx
);
289 * lttng_cmd - lttng control through socket commands
291 * @objd: the object descriptor
295 * This descriptor implements lttng commands:
297 * Returns a LTTng trace session object descriptor
298 * LTTNG_UST_TRACER_VERSION
299 * Returns the LTTng kernel tracer version
300 * LTTNG_UST_TRACEPOINT_LIST
301 * Returns a file descriptor listing available tracepoints
302 * LTTNG_UST_WAIT_QUIESCENT
303 * Returns after all previously running probes have completed
305 * The returned session will be deleted when its file descriptor is closed.
308 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
311 case LTTNG_UST_SESSION
:
312 return lttng_abi_create_session();
313 case LTTNG_UST_TRACER_VERSION
:
314 return lttng_abi_tracer_version(objd
,
315 (struct lttng_ust_tracer_version
*) arg
);
316 case LTTNG_UST_TRACEPOINT_LIST
:
317 return -ENOSYS
; //TODO
318 //return lttng_abi_tracepoint_list();
319 case LTTNG_UST_WAIT_QUIESCENT
:
327 static const struct lttng_ust_objd_ops lttng_ops
= {
332 * We tolerate no failure in this function (if one happens, we print a dmesg
333 * error, but cannot return any error, because the channel information is
337 void lttng_metadata_create_events(int channel_objd
)
339 struct ltt_channel
*channel
= objd_private(channel_objd
);
340 static struct lttng_ust_event metadata_params
= {
341 .instrumentation
= LTTNG_UST_TRACEPOINT
,
342 .name
= "lttng_metadata",
344 struct ltt_event
*event
;
348 * We tolerate no failure path after event creation. It will stay
349 * invariant for the rest of the session.
351 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
360 return; /* not allowed to return error */
363 int lttng_abi_create_channel(int session_objd
,
364 struct lttng_ust_channel
*chan_param
,
365 enum channel_type channel_type
)
367 struct ltt_session
*session
= objd_private(session_objd
);
368 const struct lttng_ust_objd_ops
*ops
;
369 const char *transport_name
;
370 struct ltt_channel
*chan
;
374 chan_objd
= objd_alloc(NULL
, <tng_channel_ops
);
379 switch (channel_type
) {
380 case PER_CPU_CHANNEL
:
381 if (chan_param
->output
== LTTNG_UST_MMAP
) {
382 transport_name
= chan_param
->overwrite
?
383 "relay-overwrite-mmap" : "relay-discard-mmap";
387 ops
= <tng_channel_ops
;
389 case METADATA_CHANNEL
:
390 if (chan_param
->output
== LTTNG_UST_MMAP
)
391 transport_name
= "relay-metadata-mmap";
394 ops
= <tng_metadata_ops
;
397 transport_name
= "<unknown>";
401 * We tolerate no failure path after channel creation. It will stay
402 * invariant for the rest of the session.
404 chan
= ltt_channel_create(session
, transport_name
, NULL
,
405 chan_param
->subbuf_size
,
406 chan_param
->num_subbuf
,
407 chan_param
->switch_timer_interval
,
408 chan_param
->read_timer_interval
,
410 &chan_param
->wait_fd
,
411 &chan_param
->memory_map_size
);
416 objd_set_private(chan_objd
, chan
);
417 chan
->objd
= chan_objd
;
418 if (channel_type
== METADATA_CHANNEL
) {
419 session
->metadata
= chan
;
420 lttng_metadata_create_events(chan_objd
);
423 /* The channel created holds a reference on the session */
424 objd_ref(session_objd
);
432 err
= lttng_ust_objd_unref(chan_objd
);
440 * lttng_session_cmd - lttng session object command
446 * This descriptor implements lttng commands:
448 * Returns a LTTng channel object descriptor
450 * Enables tracing for a session (weak enable)
452 * Disables tracing for a session (strong disable)
454 * Returns a LTTng metadata object descriptor
456 * The returned channel will be deleted when its file descriptor is closed.
459 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
461 struct ltt_session
*session
= objd_private(objd
);
464 case LTTNG_UST_CHANNEL
:
465 return lttng_abi_create_channel(objd
,
466 (struct lttng_ust_channel
*) arg
,
468 case LTTNG_UST_SESSION_START
:
469 case LTTNG_UST_ENABLE
:
470 return ltt_session_enable(session
);
471 case LTTNG_UST_SESSION_STOP
:
472 case LTTNG_UST_DISABLE
:
473 return ltt_session_disable(session
);
474 case LTTNG_UST_METADATA
:
475 return lttng_abi_create_channel(objd
,
476 (struct lttng_ust_channel
*) arg
,
484 * Called when the last file reference is dropped.
486 * Big fat note: channels and events are invariant for the whole session after
487 * their creation. So this session destruction also destroys all channel and
488 * event structures specific to this session (they are not destroyed when their
489 * individual file is released).
492 int lttng_release_session(int objd
)
494 struct ltt_session
*session
= objd_private(objd
);
497 ltt_session_destroy(session
);
504 static const struct lttng_ust_objd_ops lttng_session_ops
= {
505 .release
= lttng_release_session
,
506 .cmd
= lttng_session_cmd
,
509 struct stream_priv_data
{
510 struct lttng_ust_lib_ring_buffer
*buf
;
511 struct ltt_channel
*ltt_chan
;
515 int lttng_abi_open_stream(int channel_objd
, struct lttng_ust_stream
*info
)
517 struct ltt_channel
*channel
= objd_private(channel_objd
);
518 struct lttng_ust_lib_ring_buffer
*buf
;
519 struct stream_priv_data
*priv
;
520 int stream_objd
, ret
;
522 buf
= channel
->ops
->buffer_read_open(channel
->chan
, channel
->handle
,
523 &info
->shm_fd
, &info
->wait_fd
, &info
->memory_map_size
);
527 priv
= zmalloc(sizeof(*priv
));
533 priv
->ltt_chan
= channel
;
534 stream_objd
= objd_alloc(priv
, &lib_ring_buffer_objd_ops
);
535 if (stream_objd
< 0) {
539 /* Hold a reference on the channel object descriptor */
540 objd_ref(channel_objd
);
546 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
551 int lttng_abi_create_event(int channel_objd
,
552 struct lttng_ust_event
*event_param
)
554 struct ltt_channel
*channel
= objd_private(channel_objd
);
555 struct ltt_event
*event
;
558 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
559 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
560 if (event_objd
< 0) {
565 * We tolerate no failure path after event creation. It will stay
566 * invariant for the rest of the session.
568 event
= ltt_event_create(channel
, event_param
, NULL
);
573 objd_set_private(event_objd
, event
);
574 /* The event holds a reference on the channel */
575 objd_ref(channel_objd
);
582 err
= lttng_ust_objd_unref(event_objd
);
590 * lttng_channel_cmd - lttng control through object descriptors
592 * @objd: the object descriptor
596 * This object descriptor implements lttng commands:
598 * Returns an event stream object descriptor or failure.
599 * (typically, one event stream records events from one CPU)
601 * Returns an event object descriptor or failure.
603 * Prepend a context field to each event in the channel
605 * Enable recording for events in this channel (weak enable)
607 * Disable recording for events in this channel (strong disable)
609 * Channel and event file descriptors also hold a reference on the session.
612 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
614 struct ltt_channel
*channel
= objd_private(objd
);
617 case LTTNG_UST_STREAM
:
619 struct lttng_ust_stream
*stream
;
621 stream
= (struct lttng_ust_stream
*) arg
;
622 /* stream used as output */
623 return lttng_abi_open_stream(objd
, stream
);
625 case LTTNG_UST_EVENT
:
626 return lttng_abi_create_event(objd
, (struct lttng_ust_event
*) arg
);
627 case LTTNG_UST_CONTEXT
:
628 return lttng_abi_add_context(objd
,
629 (struct lttng_ust_context
*) arg
,
630 &channel
->ctx
, channel
->session
);
631 case LTTNG_UST_ENABLE
:
632 return ltt_channel_enable(channel
);
633 case LTTNG_UST_DISABLE
:
634 return ltt_channel_disable(channel
);
635 case LTTNG_UST_FLUSH_BUFFER
:
636 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
643 * lttng_metadata_cmd - lttng control through object descriptors
645 * @objd: the object descriptor
649 * This object descriptor implements lttng commands:
651 * Returns an event stream file descriptor or failure.
653 * Channel and event file descriptors also hold a reference on the session.
656 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
658 struct ltt_channel
*channel
= objd_private(objd
);
661 case LTTNG_UST_STREAM
:
663 struct lttng_ust_stream
*stream
;
665 stream
= (struct lttng_ust_stream
*) arg
;
666 /* stream used as output */
667 return lttng_abi_open_stream(objd
, stream
);
669 case LTTNG_UST_FLUSH_BUFFER
:
670 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
678 * lttng_channel_poll - lttng stream addition/removal monitoring
683 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
685 struct ltt_channel
*channel
= file
->private_data
;
686 unsigned int mask
= 0;
688 if (file
->f_mode
& FMODE_READ
) {
689 poll_wait_set_exclusive(wait
);
690 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
693 if (channel
->ops
->is_disabled(channel
->chan
))
695 if (channel
->ops
->is_finalized(channel
->chan
))
697 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
698 return POLLIN
| POLLRDNORM
;
707 int lttng_channel_release(int objd
)
709 struct ltt_channel
*channel
= objd_private(objd
);
712 return lttng_ust_objd_unref(channel
->session
->objd
);
716 static const struct lttng_ust_objd_ops lttng_channel_ops
= {
717 .release
= lttng_channel_release
,
718 //.poll = lttng_channel_poll,
719 .cmd
= lttng_channel_cmd
,
722 static const struct lttng_ust_objd_ops lttng_metadata_ops
= {
723 .release
= lttng_channel_release
,
724 .cmd
= lttng_metadata_cmd
,
728 * lttng_rb_cmd - lttng ring buffer control through object descriptors
730 * @objd: the object descriptor
734 * This object descriptor implements lttng commands:
735 * (None for now. Access is done directly though shm.)
738 long lttng_rb_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
747 int lttng_rb_release(int objd
)
749 struct stream_priv_data
*priv
= objd_private(objd
);
750 struct lttng_ust_lib_ring_buffer
*buf
;
751 struct ltt_channel
*channel
;
755 channel
= priv
->ltt_chan
;
757 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
759 return lttng_ust_objd_unref(channel
->objd
);
764 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
= {
765 .release
= lttng_rb_release
,
770 * lttng_event_cmd - lttng control through object descriptors
772 * @objd: the object descriptor
776 * This object descriptor implements lttng commands:
778 * Prepend a context field to each record of this event
780 * Enable recording for this event (weak enable)
782 * Disable recording for this event (strong disable)
785 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
787 struct ltt_event
*event
= objd_private(objd
);
790 case LTTNG_UST_CONTEXT
:
791 return lttng_abi_add_context(objd
,
792 (struct lttng_ust_context
*) arg
,
793 &event
->ctx
, event
->chan
->session
);
794 case LTTNG_UST_ENABLE
:
795 return ltt_event_enable(event
);
796 case LTTNG_UST_DISABLE
:
797 return ltt_event_disable(event
);
804 int lttng_event_release(int objd
)
806 struct ltt_event
*event
= objd_private(objd
);
809 return lttng_ust_objd_unref(event
->chan
->objd
);
813 /* TODO: filter control ioctl */
814 static const struct lttng_ust_objd_ops lttng_event_ops
= {
815 .release
= lttng_event_release
,
816 .cmd
= lttng_event_cmd
,
819 void lttng_ust_abi_exit(void)
821 objd_table_destroy();