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"
34 #include "tracepoint-internal.h"
36 struct ltt_tracepoint_list
{
37 struct tracepoint_iter iter
;
41 static int lttng_ust_abi_close_in_progress
;
44 int lttng_abi_tracepoint_list(void);
47 * Object descriptor table. Should be protected from concurrent access
51 struct lttng_ust_obj
{
55 const struct lttng_ust_objd_ops
*ops
;
58 int freelist_next
; /* offset freelist. end is -1. */
62 struct lttng_ust_objd_table
{
63 struct lttng_ust_obj
*array
;
64 unsigned int len
, allocated_len
;
65 int freelist_head
; /* offset freelist head. end is -1 */
68 static struct lttng_ust_objd_table objd_table
= {
73 int objd_alloc(void *private_data
, const struct lttng_ust_objd_ops
*ops
)
75 struct lttng_ust_obj
*obj
;
77 if (objd_table
.freelist_head
!= -1) {
78 obj
= &objd_table
.array
[objd_table
.freelist_head
];
79 objd_table
.freelist_head
= obj
->u
.freelist_next
;
83 if (objd_table
.len
>= objd_table
.allocated_len
) {
84 unsigned int new_allocated_len
, old_allocated_len
;
85 struct lttng_ust_obj
*new_table
, *old_table
;
87 old_allocated_len
= objd_table
.allocated_len
;
88 old_table
= objd_table
.array
;
89 if (!old_allocated_len
)
90 new_allocated_len
= 1;
92 new_allocated_len
= old_allocated_len
<< 1;
93 new_table
= zmalloc(sizeof(struct lttng_ust_obj
) * new_allocated_len
);
96 memcpy(new_table
, old_table
,
97 sizeof(struct lttng_ust_obj
) * old_allocated_len
);
99 objd_table
.array
= new_table
;
100 objd_table
.allocated_len
= new_allocated_len
;
102 obj
= &objd_table
.array
[objd_table
.len
];
105 obj
->u
.s
.private_data
= private_data
;
107 obj
->u
.s
.f_count
= 2; /* count == 1 : object is allocated */
108 /* count == 2 : allocated + hold ref */
109 return obj
- objd_table
.array
;
113 struct lttng_ust_obj
*_objd_get(int id
)
115 if (id
>= objd_table
.len
)
117 if (!objd_table
.array
[id
].u
.s
.f_count
)
119 return &objd_table
.array
[id
];
123 void *objd_private(int id
)
125 struct lttng_ust_obj
*obj
= _objd_get(id
);
127 return obj
->u
.s
.private_data
;
131 void objd_set_private(int id
, void *private_data
)
133 struct lttng_ust_obj
*obj
= _objd_get(id
);
135 obj
->u
.s
.private_data
= private_data
;
138 const struct lttng_ust_objd_ops
*objd_ops(int id
)
140 struct lttng_ust_obj
*obj
= _objd_get(id
);
148 void objd_free(int id
)
150 struct lttng_ust_obj
*obj
= _objd_get(id
);
153 obj
->u
.freelist_next
= objd_table
.freelist_head
;
154 objd_table
.freelist_head
= obj
- objd_table
.array
;
155 assert(obj
->u
.s
.f_count
== 1);
156 obj
->u
.s
.f_count
= 0; /* deallocated */
160 void objd_ref(int id
)
162 struct lttng_ust_obj
*obj
= _objd_get(id
);
166 int lttng_ust_objd_unref(int id
)
168 struct lttng_ust_obj
*obj
= _objd_get(id
);
172 if (obj
->u
.s
.f_count
== 1) {
173 ERR("Reference counting error\n");
176 if ((--obj
->u
.s
.f_count
) == 1) {
177 const struct lttng_ust_objd_ops
*ops
= objd_ops(id
);
187 void objd_table_destroy(void)
191 for (i
= 0; i
< objd_table
.allocated_len
; i
++)
192 (void) lttng_ust_objd_unref(i
);
193 free(objd_table
.array
);
194 objd_table
.array
= NULL
;
196 objd_table
.allocated_len
= 0;
197 objd_table
.freelist_head
= -1;
201 * This is LTTng's own personal way to create an ABI for sessiond.
202 * We send commands over a socket.
205 static const struct lttng_ust_objd_ops lttng_ops
;
206 static const struct lttng_ust_objd_ops lttng_session_ops
;
207 static const struct lttng_ust_objd_ops lttng_channel_ops
;
208 static const struct lttng_ust_objd_ops lttng_metadata_ops
;
209 static const struct lttng_ust_objd_ops lttng_event_ops
;
210 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
;
211 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
;
218 int lttng_abi_create_root_handle(void)
222 root_handle
= objd_alloc(NULL
, <tng_ops
);
227 int lttng_abi_create_session(void)
229 struct ltt_session
*session
;
230 int session_objd
, ret
;
232 session
= ltt_session_create();
235 session_objd
= objd_alloc(session
, <tng_session_ops
);
236 if (session_objd
< 0) {
240 session
->objd
= session_objd
;
244 ltt_session_destroy(session
);
249 long lttng_abi_tracer_version(int objd
,
250 struct lttng_ust_tracer_version
*v
)
252 v
->major
= LTTNG_UST_MAJOR
;
253 v
->minor
= LTTNG_UST_MINOR
;
254 v
->patchlevel
= LTTNG_UST_PATCHLEVEL
;
259 long lttng_abi_add_context(int objd
,
260 struct lttng_ust_context
*context_param
,
261 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
263 if (session
->been_active
)
266 switch (context_param
->ctx
) {
267 case LTTNG_UST_CONTEXT_PTHREAD_ID
:
268 return lttng_add_pthread_id_to_ctx(ctx
);
269 case LTTNG_UST_CONTEXT_VTID
:
270 return lttng_add_vtid_to_ctx(ctx
);
271 case LTTNG_UST_CONTEXT_VPID
:
272 return lttng_add_vpid_to_ctx(ctx
);
273 case LTTNG_UST_CONTEXT_PROCNAME
:
274 return lttng_add_procname_to_ctx(ctx
);
281 * lttng_cmd - lttng control through socket commands
283 * @objd: the object descriptor
287 * This descriptor implements lttng commands:
289 * Returns a LTTng trace session object descriptor
290 * LTTNG_UST_TRACER_VERSION
291 * Returns the LTTng kernel tracer version
292 * LTTNG_UST_TRACEPOINT_LIST
293 * Returns a file descriptor listing available tracepoints
294 * LTTNG_UST_WAIT_QUIESCENT
295 * Returns after all previously running probes have completed
297 * The returned session will be deleted when its file descriptor is closed.
300 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
303 case LTTNG_UST_SESSION
:
304 return lttng_abi_create_session();
305 case LTTNG_UST_TRACER_VERSION
:
306 return lttng_abi_tracer_version(objd
,
307 (struct lttng_ust_tracer_version
*) arg
);
308 case LTTNG_UST_TRACEPOINT_LIST
:
309 return lttng_abi_tracepoint_list();
310 case LTTNG_UST_WAIT_QUIESCENT
:
318 static const struct lttng_ust_objd_ops lttng_ops
= {
323 * We tolerate no failure in this function (if one happens, we print a dmesg
324 * error, but cannot return any error, because the channel information is
328 void lttng_metadata_create_events(int channel_objd
)
330 struct ltt_channel
*channel
= objd_private(channel_objd
);
331 static struct lttng_ust_event metadata_params
= {
332 .instrumentation
= LTTNG_UST_TRACEPOINT
,
333 .name
= "lttng_ust:metadata",
335 struct ltt_event
*event
;
338 * We tolerate no failure path after event creation. It will stay
339 * invariant for the rest of the session.
341 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
349 return; /* not allowed to return error */
352 int lttng_abi_create_channel(int session_objd
,
353 struct lttng_ust_channel
*chan_param
,
354 enum channel_type channel_type
)
356 struct ltt_session
*session
= objd_private(session_objd
);
357 const struct lttng_ust_objd_ops
*ops
;
358 const char *transport_name
;
359 struct ltt_channel
*chan
;
362 struct ltt_channel chan_priv_init
;
364 switch (channel_type
) {
365 case PER_CPU_CHANNEL
:
366 if (chan_param
->output
== LTTNG_UST_MMAP
) {
367 transport_name
= chan_param
->overwrite
?
368 "relay-overwrite-mmap" : "relay-discard-mmap";
372 ops
= <tng_channel_ops
;
374 case METADATA_CHANNEL
:
375 if (chan_param
->output
== LTTNG_UST_MMAP
)
376 transport_name
= "relay-metadata-mmap";
379 ops
= <tng_metadata_ops
;
382 transport_name
= "<unknown>";
385 chan_objd
= objd_alloc(NULL
, ops
);
390 memset(&chan_priv_init
, 0, sizeof(chan_priv_init
));
391 /* Copy of session UUID for consumer (availability through shm) */
392 memcpy(chan_priv_init
.uuid
, session
->uuid
, sizeof(session
->uuid
));
395 * We tolerate no failure path after channel creation. It will stay
396 * invariant for the rest of the session.
398 chan
= ltt_channel_create(session
, transport_name
, NULL
,
399 chan_param
->subbuf_size
,
400 chan_param
->num_subbuf
,
401 chan_param
->switch_timer_interval
,
402 chan_param
->read_timer_interval
,
404 &chan_param
->wait_fd
,
405 &chan_param
->memory_map_size
,
411 objd_set_private(chan_objd
, chan
);
412 chan
->objd
= chan_objd
;
413 if (channel_type
== METADATA_CHANNEL
) {
414 session
->metadata
= chan
;
415 lttng_metadata_create_events(chan_objd
);
417 /* The channel created holds a reference on the session */
418 objd_ref(session_objd
);
426 err
= lttng_ust_objd_unref(chan_objd
);
434 * lttng_session_cmd - lttng session object command
440 * This descriptor implements lttng commands:
442 * Returns a LTTng channel object descriptor
444 * Enables tracing for a session (weak enable)
446 * Disables tracing for a session (strong disable)
448 * Returns a LTTng metadata object descriptor
450 * The returned channel will be deleted when its file descriptor is closed.
453 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
455 struct ltt_session
*session
= objd_private(objd
);
458 case LTTNG_UST_CHANNEL
:
459 return lttng_abi_create_channel(objd
,
460 (struct lttng_ust_channel
*) arg
,
462 case LTTNG_UST_SESSION_START
:
463 case LTTNG_UST_ENABLE
:
464 return ltt_session_enable(session
);
465 case LTTNG_UST_SESSION_STOP
:
466 case LTTNG_UST_DISABLE
:
467 return ltt_session_disable(session
);
468 case LTTNG_UST_METADATA
:
469 return lttng_abi_create_channel(objd
,
470 (struct lttng_ust_channel
*) arg
,
478 * Called when the last file reference is dropped.
480 * Big fat note: channels and events are invariant for the whole session after
481 * their creation. So this session destruction also destroys all channel and
482 * event structures specific to this session (they are not destroyed when their
483 * individual file is released).
486 int lttng_release_session(int objd
)
488 struct ltt_session
*session
= objd_private(objd
);
491 ltt_session_destroy(session
);
498 static const struct lttng_ust_objd_ops lttng_session_ops
= {
499 .release
= lttng_release_session
,
500 .cmd
= lttng_session_cmd
,
504 * beware: we don't keep the mutex over the send, but we must walk the
505 * whole list each time we are called again. So sending one tracepoint
506 * at a time means this is O(n^2). TODO: do as in the kernel and send
507 * multiple tracepoints for each call to amortize this cost.
510 void ltt_tracepoint_list_get(struct ltt_tracepoint_list
*list
,
514 if (!list
->got_first
) {
515 tracepoint_iter_start(&list
->iter
);
519 tracepoint_iter_next(&list
->iter
);
521 if (!list
->iter
.tracepoint
) {
522 tp_list_entry
[0] = '\0'; /* end of list */
524 if (!strcmp((*list
->iter
.tracepoint
)->name
,
525 "lttng_ust:metadata"))
527 memcpy(tp_list_entry
, (*list
->iter
.tracepoint
)->name
,
528 LTTNG_UST_SYM_NAME_LEN
);
533 long lttng_tracepoint_list_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
535 struct ltt_tracepoint_list
*list
= objd_private(objd
);
536 char *str
= (char *) arg
;
539 case LTTNG_UST_TRACEPOINT_LIST_GET
:
540 ltt_tracepoint_list_get(list
, str
);
550 int lttng_abi_tracepoint_list(void)
553 struct ltt_tracepoint_list
*list
;
555 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
560 list
= zmalloc(sizeof(*list
));
565 objd_set_private(list_objd
, list
);
573 err
= lttng_ust_objd_unref(list_objd
);
581 int lttng_release_tracepoint_list(int objd
)
583 struct ltt_tracepoint_list
*list
= objd_private(objd
);
586 tracepoint_iter_stop(&list
->iter
);
594 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
= {
595 .release
= lttng_release_tracepoint_list
,
596 .cmd
= lttng_tracepoint_list_cmd
,
599 struct stream_priv_data
{
600 struct lttng_ust_lib_ring_buffer
*buf
;
601 struct ltt_channel
*ltt_chan
;
605 int lttng_abi_open_stream(int channel_objd
, struct lttng_ust_stream
*info
)
607 struct ltt_channel
*channel
= objd_private(channel_objd
);
608 struct lttng_ust_lib_ring_buffer
*buf
;
609 struct stream_priv_data
*priv
;
610 int stream_objd
, ret
;
612 buf
= channel
->ops
->buffer_read_open(channel
->chan
, channel
->handle
,
613 &info
->shm_fd
, &info
->wait_fd
, &info
->memory_map_size
);
617 priv
= zmalloc(sizeof(*priv
));
623 priv
->ltt_chan
= channel
;
624 stream_objd
= objd_alloc(priv
, &lib_ring_buffer_objd_ops
);
625 if (stream_objd
< 0) {
629 /* Hold a reference on the channel object descriptor */
630 objd_ref(channel_objd
);
636 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
641 int lttng_abi_create_event(int channel_objd
,
642 struct lttng_ust_event
*event_param
)
644 struct ltt_channel
*channel
= objd_private(channel_objd
);
645 struct ltt_event
*event
;
648 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
649 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
650 if (event_objd
< 0) {
655 * We tolerate no failure path after event creation. It will stay
656 * invariant for the rest of the session.
658 event
= ltt_event_create(channel
, event_param
, NULL
);
663 objd_set_private(event_objd
, event
);
664 /* The event holds a reference on the channel */
665 objd_ref(channel_objd
);
672 err
= lttng_ust_objd_unref(event_objd
);
680 * lttng_channel_cmd - lttng control through object descriptors
682 * @objd: the object descriptor
686 * This object descriptor implements lttng commands:
688 * Returns an event stream object descriptor or failure.
689 * (typically, one event stream records events from one CPU)
691 * Returns an event object descriptor or failure.
693 * Prepend a context field to each event in the channel
695 * Enable recording for events in this channel (weak enable)
697 * Disable recording for events in this channel (strong disable)
699 * Channel and event file descriptors also hold a reference on the session.
702 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
704 struct ltt_channel
*channel
= objd_private(objd
);
707 case LTTNG_UST_STREAM
:
709 struct lttng_ust_stream
*stream
;
711 stream
= (struct lttng_ust_stream
*) arg
;
712 /* stream used as output */
713 return lttng_abi_open_stream(objd
, stream
);
715 case LTTNG_UST_EVENT
:
716 return lttng_abi_create_event(objd
, (struct lttng_ust_event
*) arg
);
717 case LTTNG_UST_CONTEXT
:
718 return lttng_abi_add_context(objd
,
719 (struct lttng_ust_context
*) arg
,
720 &channel
->ctx
, channel
->session
);
721 case LTTNG_UST_ENABLE
:
722 return ltt_channel_enable(channel
);
723 case LTTNG_UST_DISABLE
:
724 return ltt_channel_disable(channel
);
725 case LTTNG_UST_FLUSH_BUFFER
:
726 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
733 * lttng_metadata_cmd - lttng control through object descriptors
735 * @objd: the object descriptor
739 * This object descriptor implements lttng commands:
741 * Returns an event stream file descriptor or failure.
743 * Channel and event file descriptors also hold a reference on the session.
746 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
748 struct ltt_channel
*channel
= objd_private(objd
);
751 case LTTNG_UST_STREAM
:
753 struct lttng_ust_stream
*stream
;
755 stream
= (struct lttng_ust_stream
*) arg
;
756 /* stream used as output */
757 return lttng_abi_open_stream(objd
, stream
);
759 case LTTNG_UST_FLUSH_BUFFER
:
760 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
768 * lttng_channel_poll - lttng stream addition/removal monitoring
773 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
775 struct ltt_channel
*channel
= file
->private_data
;
776 unsigned int mask
= 0;
778 if (file
->f_mode
& FMODE_READ
) {
779 poll_wait_set_exclusive(wait
);
780 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
783 if (channel
->ops
->is_disabled(channel
->chan
))
785 if (channel
->ops
->is_finalized(channel
->chan
))
787 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
788 return POLLIN
| POLLRDNORM
;
797 int lttng_channel_release(int objd
)
799 struct ltt_channel
*channel
= objd_private(objd
);
802 return lttng_ust_objd_unref(channel
->session
->objd
);
806 static const struct lttng_ust_objd_ops lttng_channel_ops
= {
807 .release
= lttng_channel_release
,
808 //.poll = lttng_channel_poll,
809 .cmd
= lttng_channel_cmd
,
812 static const struct lttng_ust_objd_ops lttng_metadata_ops
= {
813 .release
= lttng_channel_release
,
814 .cmd
= lttng_metadata_cmd
,
818 * lttng_rb_cmd - lttng ring buffer control through object descriptors
820 * @objd: the object descriptor
824 * This object descriptor implements lttng commands:
825 * (None for now. Access is done directly though shm.)
828 long lttng_rb_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
837 int lttng_rb_release(int objd
)
839 struct stream_priv_data
*priv
= objd_private(objd
);
840 struct lttng_ust_lib_ring_buffer
*buf
;
841 struct ltt_channel
*channel
;
845 channel
= priv
->ltt_chan
;
848 * If we are at ABI exit, we don't want to close the
849 * buffer opened for read: it is being shared between
850 * the parent and child (right after fork), and we don't
851 * want the child to close it for the parent. For a real
852 * exit, we don't care about marking it as closed, as
853 * the consumer daemon (if there is one) will do fine
854 * even if we don't mark it as "closed" for reading on
856 * We only mark it as closed if it is being explicitely
857 * released by the session daemon with an explicit
860 if (!lttng_ust_abi_close_in_progress
)
861 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
863 return lttng_ust_objd_unref(channel
->objd
);
868 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops
= {
869 .release
= lttng_rb_release
,
874 * lttng_event_cmd - lttng control through object descriptors
876 * @objd: the object descriptor
880 * This object descriptor implements lttng commands:
882 * Prepend a context field to each record of this event
884 * Enable recording for this event (weak enable)
886 * Disable recording for this event (strong disable)
889 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
891 struct ltt_event
*event
= objd_private(objd
);
894 case LTTNG_UST_CONTEXT
:
895 return lttng_abi_add_context(objd
,
896 (struct lttng_ust_context
*) arg
,
897 &event
->ctx
, event
->chan
->session
);
898 case LTTNG_UST_ENABLE
:
899 return ltt_event_enable(event
);
900 case LTTNG_UST_DISABLE
:
901 return ltt_event_disable(event
);
908 int lttng_event_release(int objd
)
910 struct ltt_event
*event
= objd_private(objd
);
913 return lttng_ust_objd_unref(event
->chan
->objd
);
917 /* TODO: filter control ioctl */
918 static const struct lttng_ust_objd_ops lttng_event_ops
= {
919 .release
= lttng_event_release
,
920 .cmd
= lttng_event_cmd
,
923 void lttng_ust_abi_exit(void)
925 lttng_ust_abi_close_in_progress
= 1;
926 objd_table_destroy();
927 lttng_ust_abi_close_in_progress
= 0;