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 <lttng/ust-abi.h>
28 #include <urcu/compiler.h>
29 #include <urcu/list.h>
30 #include <lttng/ust-events.h>
31 #include <lttng/ust-version.h>
32 #include <usterr-signal-safe.h>
34 #include "ltt-tracer.h"
35 #include "tracepoint-internal.h"
37 struct ltt_tracepoint_list
{
38 struct tracepoint_iter iter
;
42 static int lttng_ust_abi_close_in_progress
;
45 int lttng_abi_tracepoint_list(void);
48 * Object descriptor table. Should be protected from concurrent access
52 struct lttng_ust_obj
{
56 const struct lttng_ust_objd_ops
*ops
;
59 int freelist_next
; /* offset freelist. end is -1. */
63 struct lttng_ust_objd_table
{
64 struct lttng_ust_obj
*array
;
65 unsigned int len
, allocated_len
;
66 int freelist_head
; /* offset freelist head. end is -1 */
69 static struct lttng_ust_objd_table objd_table
= {
74 int objd_alloc(void *private_data
, const struct lttng_ust_objd_ops
*ops
)
76 struct lttng_ust_obj
*obj
;
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
;
84 if (objd_table
.len
>= objd_table
.allocated_len
) {
85 unsigned int new_allocated_len
, old_allocated_len
;
86 struct lttng_ust_obj
*new_table
, *old_table
;
88 old_allocated_len
= objd_table
.allocated_len
;
89 old_table
= objd_table
.array
;
90 if (!old_allocated_len
)
91 new_allocated_len
= 1;
93 new_allocated_len
= old_allocated_len
<< 1;
94 new_table
= zmalloc(sizeof(struct lttng_ust_obj
) * new_allocated_len
);
97 memcpy(new_table
, old_table
,
98 sizeof(struct lttng_ust_obj
) * old_allocated_len
);
100 objd_table
.array
= new_table
;
101 objd_table
.allocated_len
= new_allocated_len
;
103 obj
= &objd_table
.array
[objd_table
.len
];
106 obj
->u
.s
.private_data
= private_data
;
108 obj
->u
.s
.f_count
= 2; /* count == 1 : object is allocated */
109 /* count == 2 : allocated + hold ref */
110 return obj
- objd_table
.array
;
114 struct lttng_ust_obj
*_objd_get(int id
)
116 if (id
>= objd_table
.len
)
118 if (!objd_table
.array
[id
].u
.s
.f_count
)
120 return &objd_table
.array
[id
];
124 void *objd_private(int id
)
126 struct lttng_ust_obj
*obj
= _objd_get(id
);
128 return obj
->u
.s
.private_data
;
132 void objd_set_private(int id
, void *private_data
)
134 struct lttng_ust_obj
*obj
= _objd_get(id
);
136 obj
->u
.s
.private_data
= private_data
;
139 const struct lttng_ust_objd_ops
*objd_ops(int id
)
141 struct lttng_ust_obj
*obj
= _objd_get(id
);
149 void objd_free(int id
)
151 struct lttng_ust_obj
*obj
= _objd_get(id
);
154 obj
->u
.freelist_next
= objd_table
.freelist_head
;
155 objd_table
.freelist_head
= obj
- objd_table
.array
;
156 assert(obj
->u
.s
.f_count
== 1);
157 obj
->u
.s
.f_count
= 0; /* deallocated */
161 void objd_ref(int id
)
163 struct lttng_ust_obj
*obj
= _objd_get(id
);
167 int lttng_ust_objd_unref(int id
)
169 struct lttng_ust_obj
*obj
= _objd_get(id
);
173 if (obj
->u
.s
.f_count
== 1) {
174 ERR("Reference counting error\n");
177 if ((--obj
->u
.s
.f_count
) == 1) {
178 const struct lttng_ust_objd_ops
*ops
= objd_ops(id
);
188 void objd_table_destroy(void)
192 for (i
= 0; i
< objd_table
.allocated_len
; i
++)
193 (void) lttng_ust_objd_unref(i
);
194 free(objd_table
.array
);
195 objd_table
.array
= NULL
;
197 objd_table
.allocated_len
= 0;
198 objd_table
.freelist_head
= -1;
202 * This is LTTng's own personal way to create an ABI for sessiond.
203 * We send commands over a socket.
206 static const struct lttng_ust_objd_ops lttng_ops
;
207 static const struct lttng_ust_objd_ops lttng_session_ops
;
208 static const struct lttng_ust_objd_ops lttng_channel_ops
;
209 static const struct lttng_ust_objd_ops lttng_metadata_ops
;
210 static const struct lttng_ust_objd_ops lttng_event_ops
;
211 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
;
212 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
;
219 int lttng_abi_create_root_handle(void)
223 root_handle
= objd_alloc(NULL
, <tng_ops
);
228 int lttng_abi_create_session(void)
230 struct ltt_session
*session
;
231 int session_objd
, ret
;
233 session
= ltt_session_create();
236 session_objd
= objd_alloc(session
, <tng_session_ops
);
237 if (session_objd
< 0) {
241 session
->objd
= session_objd
;
245 ltt_session_destroy(session
);
250 long lttng_abi_tracer_version(int objd
,
251 struct lttng_ust_tracer_version
*v
)
253 v
->major
= LTTNG_UST_MAJOR_VERSION
;
254 v
->minor
= LTTNG_UST_MINOR_VERSION
;
255 v
->patchlevel
= LTTNG_UST_PATCHLEVEL_VERSION
;
260 long lttng_abi_add_context(int objd
,
261 struct lttng_ust_context
*context_param
,
262 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
264 if (session
->been_active
)
267 switch (context_param
->ctx
) {
268 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
269 return lttng_add_pthread_id_to_ctx(ctx
);
270 case LTTNG_UST_CONTEXT_VTID
:
271 return lttng_add_vtid_to_ctx(ctx
);
272 case LTTNG_UST_CONTEXT_VPID
:
273 return lttng_add_vpid_to_ctx(ctx
);
274 case LTTNG_UST_CONTEXT_PROCNAME
:
275 return lttng_add_procname_to_ctx(ctx
);
282 * lttng_cmd - lttng control through socket commands
284 * @objd: the object descriptor
288 * This descriptor implements lttng commands:
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
298 * The returned session will be deleted when its file descriptor is closed.
301 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
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
:
310 return lttng_abi_tracepoint_list();
311 case LTTNG_UST_WAIT_QUIESCENT
:
319 static const struct lttng_ust_objd_ops lttng_ops
= {
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
329 void lttng_metadata_create_events(int channel_objd
)
331 struct ltt_channel
*channel
= objd_private(channel_objd
);
332 static struct lttng_ust_event metadata_params
= {
333 .instrumentation
= LTTNG_UST_TRACEPOINT
,
334 .name
= "lttng_ust:metadata",
336 struct ltt_event
*event
;
340 * We tolerate no failure path after event creation. It will stay
341 * invariant for the rest of the session.
343 ret
= ltt_event_create(channel
, &metadata_params
, NULL
, &event
);
351 return; /* not allowed to return error */
354 int lttng_abi_create_channel(int session_objd
,
355 struct lttng_ust_channel
*chan_param
,
356 enum channel_type channel_type
)
358 struct ltt_session
*session
= objd_private(session_objd
);
359 const struct lttng_ust_objd_ops
*ops
;
360 const char *transport_name
;
361 struct ltt_channel
*chan
;
364 struct ltt_channel chan_priv_init
;
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";
374 ops
= <tng_channel_ops
;
376 case METADATA_CHANNEL
:
377 if (chan_param
->output
== LTTNG_UST_MMAP
)
378 transport_name
= "relay-metadata-mmap";
381 ops
= <tng_metadata_ops
;
384 transport_name
= "<unknown>";
387 chan_objd
= objd_alloc(NULL
, ops
);
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
));
397 * We tolerate no failure path after channel creation. It will stay
398 * invariant for the rest of the session.
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
,
404 chan_param
->read_timer_interval
,
406 &chan_param
->wait_fd
,
407 &chan_param
->memory_map_size
,
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
);
419 /* The channel created holds a reference on the session */
420 objd_ref(session_objd
);
428 err
= lttng_ust_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 lttng_ust_objd_ops lttng_session_ops
= {
501 .release
= lttng_release_session
,
502 .cmd
= lttng_session_cmd
,
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.
512 void ltt_tracepoint_list_get(struct ltt_tracepoint_list
*list
,
513 struct lttng_ust_tracepoint_iter
*tracepoint
)
516 if (!list
->got_first
) {
517 tracepoint_iter_start(&list
->iter
);
521 tracepoint_iter_next(&list
->iter
);
523 if (!list
->iter
.tracepoint
) {
524 tracepoint
->name
[0] = '\0'; /* end of list */
526 if (!strcmp((*list
->iter
.tracepoint
)->name
,
527 "lttng_ust:metadata"))
529 memcpy(tracepoint
->name
, (*list
->iter
.tracepoint
)->name
,
530 LTTNG_UST_SYM_NAME_LEN
);
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
;
540 tracepoint
->loglevel
[0] = '\0';
541 tracepoint
->loglevel_value
= 0;
549 long lttng_tracepoint_list_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
551 struct ltt_tracepoint_list
*list
= objd_private(objd
);
552 struct lttng_ust_tracepoint_iter
*tp
=
553 (struct lttng_ust_tracepoint_iter
*) arg
;
556 case LTTNG_UST_TRACEPOINT_LIST_GET
:
557 ltt_tracepoint_list_get(list
, tp
);
558 if (tp
->name
[0] == '\0')
567 int lttng_abi_tracepoint_list(void)
570 struct ltt_tracepoint_list
*list
;
572 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
577 list
= zmalloc(sizeof(*list
));
582 objd_set_private(list_objd
, list
);
590 err
= lttng_ust_objd_unref(list_objd
);
598 int lttng_release_tracepoint_list(int objd
)
600 struct ltt_tracepoint_list
*list
= objd_private(objd
);
603 tracepoint_iter_stop(&list
->iter
);
611 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
= {
612 .release
= lttng_release_tracepoint_list
,
613 .cmd
= lttng_tracepoint_list_cmd
,
616 struct stream_priv_data
{
617 struct lttng_ust_lib_ring_buffer
*buf
;
618 struct ltt_channel
*ltt_chan
;
622 int lttng_abi_open_stream(int channel_objd
, struct lttng_ust_stream
*info
)
624 struct ltt_channel
*channel
= objd_private(channel_objd
);
625 struct lttng_ust_lib_ring_buffer
*buf
;
626 struct stream_priv_data
*priv
;
627 int stream_objd
, ret
;
629 buf
= channel
->ops
->buffer_read_open(channel
->chan
, channel
->handle
,
630 &info
->shm_fd
, &info
->wait_fd
, &info
->memory_map_size
);
634 priv
= zmalloc(sizeof(*priv
));
640 priv
->ltt_chan
= channel
;
641 stream_objd
= objd_alloc(priv
, &lib_ring_buffer_objd_ops
);
642 if (stream_objd
< 0) {
646 /* Hold a reference on the channel object descriptor */
647 objd_ref(channel_objd
);
653 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
658 int lttng_abi_create_event(int channel_objd
,
659 struct lttng_ust_event
*event_param
)
661 struct ltt_channel
*channel
= objd_private(channel_objd
);
662 struct ltt_event
*event
;
665 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
666 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
667 if (event_objd
< 0) {
672 * We tolerate no failure path after event creation. It will stay
673 * invariant for the rest of the session.
675 ret
= ltt_event_create(channel
, event_param
, NULL
, &event
);
679 objd_set_private(event_objd
, event
);
680 /* The event holds a reference on the channel */
681 objd_ref(channel_objd
);
688 err
= lttng_ust_objd_unref(event_objd
);
696 * lttng_channel_cmd - lttng control through object descriptors
698 * @objd: the object descriptor
702 * This object descriptor implements lttng commands:
704 * Returns an event stream object descriptor or failure.
705 * (typically, one event stream records events from one CPU)
707 * Returns an event object descriptor or failure.
709 * Prepend a context field to each event in the channel
711 * Enable recording for events in this channel (weak enable)
713 * Disable recording for events in this channel (strong disable)
715 * Channel and event file descriptors also hold a reference on the session.
718 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
720 struct ltt_channel
*channel
= objd_private(objd
);
723 case LTTNG_UST_STREAM
:
725 struct lttng_ust_stream
*stream
;
727 stream
= (struct lttng_ust_stream
*) arg
;
728 /* stream used as output */
729 return lttng_abi_open_stream(objd
, stream
);
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
);
741 case LTTNG_UST_FLUSH_BUFFER
:
742 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
749 * lttng_metadata_cmd - lttng control through object descriptors
751 * @objd: the object descriptor
755 * This object descriptor implements lttng commands:
757 * Returns an event stream file descriptor or failure.
759 * Channel and event file descriptors also hold a reference on the session.
762 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
764 struct ltt_channel
*channel
= objd_private(objd
);
767 case LTTNG_UST_STREAM
:
769 struct lttng_ust_stream
*stream
;
771 stream
= (struct lttng_ust_stream
*) arg
;
772 /* stream used as output */
773 return lttng_abi_open_stream(objd
, stream
);
775 case LTTNG_UST_FLUSH_BUFFER
:
776 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
784 * lttng_channel_poll - lttng stream addition/removal monitoring
789 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
791 struct ltt_channel
*channel
= file
->private_data
;
792 unsigned int mask
= 0;
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
),
799 if (channel
->ops
->is_disabled(channel
->chan
))
801 if (channel
->ops
->is_finalized(channel
->chan
))
803 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
804 return POLLIN
| POLLRDNORM
;
813 int lttng_channel_release(int objd
)
815 struct ltt_channel
*channel
= objd_private(objd
);
818 return lttng_ust_objd_unref(channel
->session
->objd
);
822 static const struct lttng_ust_objd_ops lttng_channel_ops
= {
823 .release
= lttng_channel_release
,
824 //.poll = lttng_channel_poll,
825 .cmd
= lttng_channel_cmd
,
828 static const struct lttng_ust_objd_ops lttng_metadata_ops
= {
829 .release
= lttng_channel_release
,
830 .cmd
= lttng_metadata_cmd
,
834 * lttng_rb_cmd - lttng ring buffer control through object descriptors
836 * @objd: the object descriptor
840 * This object descriptor implements lttng commands:
841 * (None for now. Access is done directly though shm.)
844 long lttng_rb_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
853 int lttng_rb_release(int objd
)
855 struct stream_priv_data
*priv
= objd_private(objd
);
856 struct lttng_ust_lib_ring_buffer
*buf
;
857 struct ltt_channel
*channel
;
861 channel
= priv
->ltt_chan
;
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
872 * We only mark it as closed if it is being explicitely
873 * released by the session daemon with an explicit
876 if (!lttng_ust_abi_close_in_progress
)
877 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
879 return lttng_ust_objd_unref(channel
->objd
);
884 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
= {
885 .release
= lttng_rb_release
,
890 * lttng_event_cmd - lttng control through object descriptors
892 * @objd: the object descriptor
896 * This object descriptor implements lttng commands:
898 * Prepend a context field to each record of this event
900 * Enable recording for this event (weak enable)
902 * Disable recording for this event (strong disable)
905 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
907 struct ltt_event
*event
= objd_private(objd
);
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
);
924 int lttng_event_release(int objd
)
926 struct ltt_event
*event
= objd_private(objd
);
929 return lttng_ust_objd_unref(event
->chan
->objd
);
933 /* TODO: filter control ioctl */
934 static const struct lttng_ust_objd_ops lttng_event_ops
= {
935 .release
= lttng_event_release
,
936 .cmd
= lttng_event_cmd
,
939 void lttng_ust_abi_exit(void)
941 lttng_ust_abi_close_in_progress
= 1;
942 objd_table_destroy();
943 lttng_ust_abi_close_in_progress
= 0;