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/usterr-signal-safe.h>
32 #include "lttng/core.h"
33 #include "ltt-tracer.h"
36 int lttng_abi_tracepoint_list(void);
39 * Object descriptor table. Should be protected from concurrent access
43 struct lttng_ust_obj
{
47 const struct lttng_ust_objd_ops
*ops
;
50 int freelist_next
; /* offset freelist. end is -1. */
54 struct lttng_ust_objd_table
{
55 struct lttng_ust_obj
*array
;
56 unsigned int len
, allocated_len
;
57 int freelist_head
; /* offset freelist head. end is -1 */
60 static struct lttng_ust_objd_table objd_table
= {
65 int objd_alloc(void *private_data
, const struct lttng_ust_objd_ops
*ops
)
67 struct lttng_ust_obj
*obj
;
69 if (objd_table
.freelist_head
!= -1) {
70 obj
= &objd_table
.array
[objd_table
.freelist_head
];
71 objd_table
.freelist_head
= obj
->u
.freelist_next
;
75 if (objd_table
.len
>= objd_table
.allocated_len
) {
76 unsigned int new_allocated_len
, old_allocated_len
;
77 struct lttng_ust_obj
*new_table
, *old_table
;
79 old_allocated_len
= objd_table
.allocated_len
;
80 old_table
= objd_table
.array
;
81 if (!old_allocated_len
)
82 new_allocated_len
= 1;
84 new_allocated_len
= old_allocated_len
<< 1;
85 new_table
= zmalloc(sizeof(struct lttng_ust_obj
) * new_allocated_len
);
88 memcpy(new_table
, old_table
,
89 sizeof(struct lttng_ust_obj
) * old_allocated_len
);
91 objd_table
.array
= new_table
;
92 objd_table
.allocated_len
= new_allocated_len
;
94 obj
= &objd_table
.array
[objd_table
.len
];
97 obj
->u
.s
.private_data
= private_data
;
99 obj
->u
.s
.f_count
= 2; /* count == 1 : object is allocated */
100 /* count == 2 : allocated + hold ref */
101 return obj
- objd_table
.array
;
105 struct lttng_ust_obj
*_objd_get(int id
)
107 if (id
>= objd_table
.len
)
109 if (!objd_table
.array
[id
].u
.s
.f_count
)
111 return &objd_table
.array
[id
];
115 void *objd_private(int id
)
117 struct lttng_ust_obj
*obj
= _objd_get(id
);
119 return obj
->u
.s
.private_data
;
123 void objd_set_private(int id
, void *private_data
)
125 struct lttng_ust_obj
*obj
= _objd_get(id
);
127 obj
->u
.s
.private_data
= private_data
;
130 const struct lttng_ust_objd_ops
*objd_ops(int id
)
132 struct lttng_ust_obj
*obj
= _objd_get(id
);
140 void objd_free(int id
)
142 struct lttng_ust_obj
*obj
= _objd_get(id
);
145 obj
->u
.freelist_next
= objd_table
.freelist_head
;
146 objd_table
.freelist_head
= obj
- objd_table
.array
;
147 assert(obj
->u
.s
.f_count
== 1);
148 obj
->u
.s
.f_count
= 0; /* deallocated */
152 void objd_ref(int id
)
154 struct lttng_ust_obj
*obj
= _objd_get(id
);
158 int lttng_ust_objd_unref(int id
)
160 struct lttng_ust_obj
*obj
= _objd_get(id
);
164 if (obj
->u
.s
.f_count
== 1) {
165 ERR("Reference counting error\n");
168 if ((--obj
->u
.s
.f_count
) == 1) {
169 const struct lttng_ust_objd_ops
*ops
= objd_ops(id
);
179 void objd_table_destroy(void)
183 for (i
= 0; i
< objd_table
.allocated_len
; i
++)
184 (void) lttng_ust_objd_unref(i
);
185 free(objd_table
.array
);
186 objd_table
.array
= NULL
;
188 objd_table
.allocated_len
= 0;
189 objd_table
.freelist_head
= -1;
193 * This is LTTng's own personal way to create an ABI for sessiond.
194 * We send commands over a socket.
197 static const struct lttng_ust_objd_ops lttng_ops
;
198 static const struct lttng_ust_objd_ops lttng_session_ops
;
199 static const struct lttng_ust_objd_ops lttng_channel_ops
;
200 static const struct lttng_ust_objd_ops lttng_metadata_ops
;
201 static const struct lttng_ust_objd_ops lttng_event_ops
;
202 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
;
203 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
;
210 int lttng_abi_create_root_handle(void)
214 root_handle
= objd_alloc(NULL
, <tng_ops
);
219 int lttng_abi_create_session(void)
221 struct ltt_session
*session
;
222 int session_objd
, ret
;
224 session
= ltt_session_create();
227 session_objd
= objd_alloc(session
, <tng_session_ops
);
228 if (session_objd
< 0) {
232 session
->objd
= session_objd
;
236 ltt_session_destroy(session
);
241 long lttng_abi_tracer_version(int objd
,
242 struct lttng_ust_tracer_version
*v
)
244 v
->version
= LTTNG_UST_VERSION
;
245 v
->patchlevel
= LTTNG_UST_PATCHLEVEL
;
246 v
->sublevel
= LTTNG_UST_SUBLEVEL
;
251 long lttng_abi_add_context(int objd
,
252 struct lttng_ust_context
*context_param
,
253 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
255 if (session
->been_active
)
258 switch (context_param
->ctx
) {
259 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
260 return lttng_add_pthread_id_to_ctx(ctx
);
261 case LTTNG_UST_CONTEXT_VTID
:
262 return lttng_add_vtid_to_ctx(ctx
);
263 case LTTNG_UST_CONTEXT_VPID
:
264 return lttng_add_vpid_to_ctx(ctx
);
265 case LTTNG_UST_CONTEXT_PROCNAME
:
266 return lttng_add_procname_to_ctx(ctx
);
273 * lttng_cmd - lttng control through socket commands
275 * @objd: the object descriptor
279 * This descriptor implements lttng commands:
281 * Returns a LTTng trace session object descriptor
282 * LTTNG_UST_TRACER_VERSION
283 * Returns the LTTng kernel tracer version
284 * LTTNG_UST_TRACEPOINT_LIST
285 * Returns a file descriptor listing available tracepoints
286 * LTTNG_UST_WAIT_QUIESCENT
287 * Returns after all previously running probes have completed
289 * The returned session will be deleted when its file descriptor is closed.
292 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
295 case LTTNG_UST_SESSION
:
296 return lttng_abi_create_session();
297 case LTTNG_UST_TRACER_VERSION
:
298 return lttng_abi_tracer_version(objd
,
299 (struct lttng_ust_tracer_version
*) arg
);
300 case LTTNG_UST_TRACEPOINT_LIST
:
301 return lttng_abi_tracepoint_list();
302 case LTTNG_UST_WAIT_QUIESCENT
:
310 static const struct lttng_ust_objd_ops lttng_ops
= {
315 * We tolerate no failure in this function (if one happens, we print a dmesg
316 * error, but cannot return any error, because the channel information is
320 void lttng_metadata_create_events(int channel_objd
)
322 struct ltt_channel
*channel
= objd_private(channel_objd
);
323 static struct lttng_ust_event metadata_params
= {
324 .instrumentation
= LTTNG_UST_TRACEPOINT
,
325 .name
= "lttng_metadata",
327 struct ltt_event
*event
;
330 * We tolerate no failure path after event creation. It will stay
331 * invariant for the rest of the session.
333 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
341 return; /* not allowed to return error */
344 int lttng_abi_create_channel(int session_objd
,
345 struct lttng_ust_channel
*chan_param
,
346 enum channel_type channel_type
)
348 struct ltt_session
*session
= objd_private(session_objd
);
349 const struct lttng_ust_objd_ops
*ops
;
350 const char *transport_name
;
351 struct ltt_channel
*chan
;
354 struct ltt_channel chan_priv_init
;
356 switch (channel_type
) {
357 case PER_CPU_CHANNEL
:
358 if (chan_param
->output
== LTTNG_UST_MMAP
) {
359 transport_name
= chan_param
->overwrite
?
360 "relay-overwrite-mmap" : "relay-discard-mmap";
364 ops
= <tng_channel_ops
;
366 case METADATA_CHANNEL
:
367 if (chan_param
->output
== LTTNG_UST_MMAP
)
368 transport_name
= "relay-metadata-mmap";
371 ops
= <tng_metadata_ops
;
374 transport_name
= "<unknown>";
377 chan_objd
= objd_alloc(NULL
, ops
);
382 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
383 /* Copy of session UUID for consumer (availability through shm) */
384 memcpy(chan_priv_init
.uuid
, session
->uuid
, sizeof(session
->uuid
));
387 * We tolerate no failure path after channel creation. It will stay
388 * invariant for the rest of the session.
390 chan
= ltt_channel_create(session
, transport_name
, NULL
,
391 chan_param
->subbuf_size
,
392 chan_param
->num_subbuf
,
393 chan_param
->switch_timer_interval
,
394 chan_param
->read_timer_interval
,
396 &chan_param
->wait_fd
,
397 &chan_param
->memory_map_size
,
403 objd_set_private(chan_objd
, chan
);
404 chan
->objd
= chan_objd
;
405 if (channel_type
== METADATA_CHANNEL
) {
406 session
->metadata
= chan
;
407 lttng_metadata_create_events(chan_objd
);
409 /* The channel created holds a reference on the session */
410 objd_ref(session_objd
);
418 err
= lttng_ust_objd_unref(chan_objd
);
426 * lttng_session_cmd - lttng session object command
432 * This descriptor implements lttng commands:
434 * Returns a LTTng channel object descriptor
436 * Enables tracing for a session (weak enable)
438 * Disables tracing for a session (strong disable)
440 * Returns a LTTng metadata object descriptor
442 * The returned channel will be deleted when its file descriptor is closed.
445 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
447 struct ltt_session
*session
= objd_private(objd
);
450 case LTTNG_UST_CHANNEL
:
451 return lttng_abi_create_channel(objd
,
452 (struct lttng_ust_channel
*) arg
,
454 case LTTNG_UST_SESSION_START
:
455 case LTTNG_UST_ENABLE
:
456 return ltt_session_enable(session
);
457 case LTTNG_UST_SESSION_STOP
:
458 case LTTNG_UST_DISABLE
:
459 return ltt_session_disable(session
);
460 case LTTNG_UST_METADATA
:
461 return lttng_abi_create_channel(objd
,
462 (struct lttng_ust_channel
*) arg
,
470 * Called when the last file reference is dropped.
472 * Big fat note: channels and events are invariant for the whole session after
473 * their creation. So this session destruction also destroys all channel and
474 * event structures specific to this session (they are not destroyed when their
475 * individual file is released).
478 int lttng_release_session(int objd
)
480 struct ltt_session
*session
= objd_private(objd
);
483 ltt_session_destroy(session
);
490 static const struct lttng_ust_objd_ops lttng_session_ops
= {
491 .release
= lttng_release_session
,
492 .cmd
= lttng_session_cmd
,
496 * beware: we don't keep the mutex over the send, but we must walk the
497 * whole list each time we are called again. So sending one tracepoint
498 * at a time means this is O(n^2). TODO: do as in the kernel and send
499 * multiple tracepoints for each call to amortize this cost.
502 void ltt_tracepoint_list_get(struct ltt_tracepoint_list
*list
,
505 if (!list
->got_first
) {
506 tracepoint_iter_start(&list
->iter
);
510 tracepoint_iter_next(&list
->iter
);
512 if (!list
->iter
.tracepoint
) {
513 tp_list_entry
[0] = '\0'; /* end of list */
515 memcpy(tp_list_entry
, (*list
->iter
.tracepoint
)->name
,
516 LTTNG_UST_SYM_NAME_LEN
);
521 long lttng_tracepoint_list_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
523 struct ltt_tracepoint_list
*list
= objd_private(objd
);
524 char *str
= (char *) arg
;
527 case LTTNG_UST_TRACEPOINT_LIST_GET
:
528 ltt_tracepoint_list_get(list
, str
);
538 int lttng_abi_tracepoint_list(void)
541 struct ltt_tracepoint_list
*list
;
543 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
548 list
= zmalloc(sizeof(*list
));
553 objd_set_private(list_objd
, list
);
561 err
= lttng_ust_objd_unref(list_objd
);
569 int lttng_release_tracepoint_list(int objd
)
571 struct ltt_tracepoint_list
*list
= objd_private(objd
);
574 tracepoint_iter_stop(&list
->iter
);
582 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
= {
583 .release
= lttng_release_tracepoint_list
,
584 .cmd
= lttng_tracepoint_list_cmd
,
587 struct stream_priv_data
{
588 struct lttng_ust_lib_ring_buffer
*buf
;
589 struct ltt_channel
*ltt_chan
;
593 int lttng_abi_open_stream(int channel_objd
, struct lttng_ust_stream
*info
)
595 struct ltt_channel
*channel
= objd_private(channel_objd
);
596 struct lttng_ust_lib_ring_buffer
*buf
;
597 struct stream_priv_data
*priv
;
598 int stream_objd
, ret
;
600 buf
= channel
->ops
->buffer_read_open(channel
->chan
, channel
->handle
,
601 &info
->shm_fd
, &info
->wait_fd
, &info
->memory_map_size
);
605 priv
= zmalloc(sizeof(*priv
));
611 priv
->ltt_chan
= channel
;
612 stream_objd
= objd_alloc(priv
, &lib_ring_buffer_objd_ops
);
613 if (stream_objd
< 0) {
617 /* Hold a reference on the channel object descriptor */
618 objd_ref(channel_objd
);
624 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
629 int lttng_abi_create_event(int channel_objd
,
630 struct lttng_ust_event
*event_param
)
632 struct ltt_channel
*channel
= objd_private(channel_objd
);
633 struct ltt_event
*event
;
636 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
637 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
638 if (event_objd
< 0) {
643 * We tolerate no failure path after event creation. It will stay
644 * invariant for the rest of the session.
646 event
= ltt_event_create(channel
, event_param
, NULL
);
651 objd_set_private(event_objd
, event
);
652 /* The event holds a reference on the channel */
653 objd_ref(channel_objd
);
660 err
= lttng_ust_objd_unref(event_objd
);
668 * lttng_channel_cmd - lttng control through object descriptors
670 * @objd: the object descriptor
674 * This object descriptor implements lttng commands:
676 * Returns an event stream object descriptor or failure.
677 * (typically, one event stream records events from one CPU)
679 * Returns an event object descriptor or failure.
681 * Prepend a context field to each event in the channel
683 * Enable recording for events in this channel (weak enable)
685 * Disable recording for events in this channel (strong disable)
687 * Channel and event file descriptors also hold a reference on the session.
690 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
692 struct ltt_channel
*channel
= objd_private(objd
);
695 case LTTNG_UST_STREAM
:
697 struct lttng_ust_stream
*stream
;
699 stream
= (struct lttng_ust_stream
*) arg
;
700 /* stream used as output */
701 return lttng_abi_open_stream(objd
, stream
);
703 case LTTNG_UST_EVENT
:
704 return lttng_abi_create_event(objd
, (struct lttng_ust_event
*) arg
);
705 case LTTNG_UST_CONTEXT
:
706 return lttng_abi_add_context(objd
,
707 (struct lttng_ust_context
*) arg
,
708 &channel
->ctx
, channel
->session
);
709 case LTTNG_UST_ENABLE
:
710 return ltt_channel_enable(channel
);
711 case LTTNG_UST_DISABLE
:
712 return ltt_channel_disable(channel
);
713 case LTTNG_UST_FLUSH_BUFFER
:
714 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
721 * lttng_metadata_cmd - lttng control through object descriptors
723 * @objd: the object descriptor
727 * This object descriptor implements lttng commands:
729 * Returns an event stream file descriptor or failure.
731 * Channel and event file descriptors also hold a reference on the session.
734 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
736 struct ltt_channel
*channel
= objd_private(objd
);
739 case LTTNG_UST_STREAM
:
741 struct lttng_ust_stream
*stream
;
743 stream
= (struct lttng_ust_stream
*) arg
;
744 /* stream used as output */
745 return lttng_abi_open_stream(objd
, stream
);
747 case LTTNG_UST_FLUSH_BUFFER
:
748 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
756 * lttng_channel_poll - lttng stream addition/removal monitoring
761 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
763 struct ltt_channel
*channel
= file
->private_data
;
764 unsigned int mask
= 0;
766 if (file
->f_mode
& FMODE_READ
) {
767 poll_wait_set_exclusive(wait
);
768 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
771 if (channel
->ops
->is_disabled(channel
->chan
))
773 if (channel
->ops
->is_finalized(channel
->chan
))
775 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
776 return POLLIN
| POLLRDNORM
;
785 int lttng_channel_release(int objd
)
787 struct ltt_channel
*channel
= objd_private(objd
);
790 return lttng_ust_objd_unref(channel
->session
->objd
);
794 static const struct lttng_ust_objd_ops lttng_channel_ops
= {
795 .release
= lttng_channel_release
,
796 //.poll = lttng_channel_poll,
797 .cmd
= lttng_channel_cmd
,
800 static const struct lttng_ust_objd_ops lttng_metadata_ops
= {
801 .release
= lttng_channel_release
,
802 .cmd
= lttng_metadata_cmd
,
806 * lttng_rb_cmd - lttng ring buffer control through object descriptors
808 * @objd: the object descriptor
812 * This object descriptor implements lttng commands:
813 * (None for now. Access is done directly though shm.)
816 long lttng_rb_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
825 int lttng_rb_release(int objd
)
827 struct stream_priv_data
*priv
= objd_private(objd
);
828 struct lttng_ust_lib_ring_buffer
*buf
;
829 struct ltt_channel
*channel
;
833 channel
= priv
->ltt_chan
;
835 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
837 return lttng_ust_objd_unref(channel
->objd
);
842 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
= {
843 .release
= lttng_rb_release
,
848 * lttng_event_cmd - lttng control through object descriptors
850 * @objd: the object descriptor
854 * This object descriptor implements lttng commands:
856 * Prepend a context field to each record of this event
858 * Enable recording for this event (weak enable)
860 * Disable recording for this event (strong disable)
863 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
865 struct ltt_event
*event
= objd_private(objd
);
868 case LTTNG_UST_CONTEXT
:
869 return lttng_abi_add_context(objd
,
870 (struct lttng_ust_context
*) arg
,
871 &event
->ctx
, event
->chan
->session
);
872 case LTTNG_UST_ENABLE
:
873 return ltt_event_enable(event
);
874 case LTTNG_UST_DISABLE
:
875 return ltt_event_disable(event
);
882 int lttng_event_release(int objd
)
884 struct ltt_event
*event
= objd_private(objd
);
887 return lttng_ust_objd_unref(event
->chan
->objd
);
891 /* TODO: filter control ioctl */
892 static const struct lttng_ust_objd_ops lttng_event_ops
= {
893 .release
= lttng_event_release
,
894 .cmd
= lttng_event_cmd
,
897 void lttng_ust_abi_exit(void)
899 objd_table_destroy();