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 (void) 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 objd_ops lttng_ops
;
195 static const struct objd_ops lttng_session_ops
;
196 static const struct objd_ops lttng_channel_ops
;
197 static const struct objd_ops lttng_metadata_ops
;
198 static const struct objd_ops lttng_event_ops
;
199 static const struct objd_ops lib_ring_buffer_objd_ops
;
206 int lttng_abi_create_root_handle(void)
210 root_handle
= objd_alloc(NULL
, <tng_ops
);
211 assert(root_handle
== 0);
216 int lttng_abi_create_session(void)
218 struct ltt_session
*session
;
219 int session_objd
, ret
;
221 session
= ltt_session_create();
224 session_objd
= objd_alloc(session
, <tng_session_ops
);
225 if (session_objd
< 0) {
229 session
->objd
= session_objd
;
233 ltt_session_destroy(session
);
239 int lttng_abi_tracepoint_list(void)
243 /* TODO: Create list private data */
244 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
);
258 long lttng_abi_tracer_version(int objd
,
259 struct lttng_ust_tracer_version
*v
)
261 v
->version
= LTTNG_UST_VERSION
;
262 v
->patchlevel
= LTTNG_UST_PATCHLEVEL
;
263 v
->sublevel
= LTTNG_UST_SUBLEVEL
;
268 long lttng_abi_add_context(int objd
,
269 struct lttng_ust_context
*context_param
,
270 struct lttng_ctx
**ctx
, struct ltt_session
*session
)
272 if (session
->been_active
)
275 switch (context_param
->ctx
) {
276 case LTTNG_UST_CONTEXT_VTID
:
277 //TODO return lttng_add_vtid_to_ctx(ctx);
284 * lttng_cmd - lttng control through socket commands
286 * @objd: the object descriptor
290 * This descriptor implements lttng commands:
292 * Returns a LTTng trace session object descriptor
293 * LTTNG_UST_TRACER_VERSION
294 * Returns the LTTng kernel tracer version
295 * LTTNG_UST_TRACEPOINT_LIST
296 * Returns a file descriptor listing available tracepoints
297 * LTTNG_UST_WAIT_QUIESCENT
298 * Returns after all previously running probes have completed
300 * The returned session will be deleted when its file descriptor is closed.
303 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
306 case LTTNG_UST_SESSION
:
307 return lttng_abi_create_session();
308 case LTTNG_UST_TRACER_VERSION
:
309 return lttng_abi_tracer_version(objd
,
310 (struct lttng_ust_tracer_version
*) arg
);
311 case LTTNG_UST_TRACEPOINT_LIST
:
312 return -ENOSYS
; //TODO
313 //return lttng_abi_tracepoint_list();
314 case LTTNG_UST_WAIT_QUIESCENT
:
322 static const struct objd_ops lttng_ops
= {
327 * We tolerate no failure in this function (if one happens, we print a dmesg
328 * error, but cannot return any error, because the channel information is
332 void lttng_metadata_create_events(int channel_objd
)
334 struct ltt_channel
*channel
= objd_private(channel_objd
);
335 static struct lttng_ust_event metadata_params
= {
336 .instrumentation
= LTTNG_UST_TRACEPOINT
,
337 .name
= "lttng_metadata",
339 struct ltt_event
*event
;
343 * We tolerate no failure path after event creation. It will stay
344 * invariant for the rest of the session.
346 event
= ltt_event_create(channel
, &metadata_params
, NULL
);
355 return; /* not allowed to return error */
358 int lttng_abi_create_channel(int session_objd
,
359 struct lttng_ust_channel
*chan_param
,
360 enum channel_type channel_type
)
362 struct ltt_session
*session
= objd_private(session_objd
);
363 const struct objd_ops
*ops
;
364 const char *transport_name
;
365 struct ltt_channel
*chan
;
369 chan_objd
= objd_alloc(NULL
, <tng_channel_ops
);
374 switch (channel_type
) {
375 case PER_CPU_CHANNEL
:
376 if (chan_param
->output
== LTTNG_UST_MMAP
) {
377 transport_name
= chan_param
->overwrite
?
378 "relay-overwrite-mmap" : "relay-discard-mmap";
382 ops
= <tng_channel_ops
;
384 case METADATA_CHANNEL
:
385 if (chan_param
->output
== LTTNG_UST_MMAP
)
386 transport_name
= "relay-metadata-mmap";
389 ops
= <tng_metadata_ops
;
392 transport_name
= "<unknown>";
396 * We tolerate no failure path after channel creation. It will stay
397 * invariant for the rest of the session.
399 chan
= ltt_channel_create(session
, transport_name
, NULL
,
400 chan_param
->subbuf_size
,
401 chan_param
->num_subbuf
,
402 chan_param
->switch_timer_interval
,
403 chan_param
->read_timer_interval
,
405 &chan_param
->wait_fd
,
406 &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
);
418 /* The channel created holds a reference on the session */
419 objd_ref(session_objd
);
427 err
= objd_unref(chan_objd
);
435 * lttng_session_cmd - lttng session object command
441 * This descriptor implements lttng commands:
443 * Returns a LTTng channel object descriptor
445 * Enables tracing for a session (weak enable)
447 * Disables tracing for a session (strong disable)
449 * Returns a LTTng metadata object descriptor
451 * The returned channel will be deleted when its file descriptor is closed.
454 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
456 struct ltt_session
*session
= objd_private(objd
);
459 case LTTNG_UST_CHANNEL
:
460 return lttng_abi_create_channel(objd
,
461 (struct lttng_ust_channel
*) arg
,
463 case LTTNG_UST_SESSION_START
:
464 case LTTNG_UST_ENABLE
:
465 return ltt_session_enable(session
);
466 case LTTNG_UST_SESSION_STOP
:
467 case LTTNG_UST_DISABLE
:
468 return ltt_session_disable(session
);
469 case LTTNG_UST_METADATA
:
470 return lttng_abi_create_channel(objd
,
471 (struct lttng_ust_channel
*) arg
,
479 * Called when the last file reference is dropped.
481 * Big fat note: channels and events are invariant for the whole session after
482 * their creation. So this session destruction also destroys all channel and
483 * event structures specific to this session (they are not destroyed when their
484 * individual file is released).
487 int lttng_release_session(int objd
)
489 struct ltt_session
*session
= objd_private(objd
);
492 ltt_session_destroy(session
);
499 static const struct objd_ops lttng_session_ops
= {
500 .release
= lttng_release_session
,
501 .cmd
= lttng_session_cmd
,
504 struct stream_priv_data
{
505 struct lib_ring_buffer
*buf
;
506 struct ltt_channel
*ltt_chan
;
510 int lttng_abi_open_stream(int channel_objd
, struct lttng_ust_stream
*info
)
512 struct ltt_channel
*channel
= objd_private(channel_objd
);
513 struct lib_ring_buffer
*buf
;
514 struct stream_priv_data
*priv
;
515 int stream_objd
, ret
;
517 buf
= channel
->ops
->buffer_read_open(channel
->chan
, channel
->handle
,
518 &info
->shm_fd
, &info
->wait_fd
, &info
->memory_map_size
);
522 priv
= zmalloc(sizeof(*priv
));
528 priv
->ltt_chan
= channel
;
529 stream_objd
= objd_alloc(priv
, &lib_ring_buffer_objd_ops
);
530 if (stream_objd
< 0) {
534 /* Hold a reference on the channel object descriptor */
535 objd_ref(channel_objd
);
541 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
546 int lttng_abi_create_event(int channel_objd
,
547 struct lttng_ust_event
*event_param
)
549 struct ltt_channel
*channel
= objd_private(channel_objd
);
550 struct ltt_event
*event
;
553 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
554 event_objd
= objd_alloc(NULL
, <tng_event_ops
);
555 if (event_objd
< 0) {
560 * We tolerate no failure path after event creation. It will stay
561 * invariant for the rest of the session.
563 event
= ltt_event_create(channel
, event_param
, NULL
);
568 objd_set_private(event_objd
, event
);
569 /* The event holds a reference on the channel */
570 objd_ref(channel_objd
);
577 err
= objd_unref(event_objd
);
585 * lttng_channel_cmd - lttng control through object descriptors
587 * @objd: the object descriptor
591 * This object descriptor implements lttng commands:
593 * Returns an event stream object descriptor or failure.
594 * (typically, one event stream records events from one CPU)
596 * Returns an event object descriptor or failure.
598 * Prepend a context field to each event in the channel
600 * Enable recording for events in this channel (weak enable)
602 * Disable recording for events in this channel (strong disable)
604 * Channel and event file descriptors also hold a reference on the session.
607 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
609 struct ltt_channel
*channel
= objd_private(objd
);
612 case LTTNG_UST_STREAM
:
614 struct lttng_ust_stream
*stream
;
616 stream
= (struct lttng_ust_stream
*) arg
;
617 /* stream used as output */
618 return lttng_abi_open_stream(objd
, stream
);
620 case LTTNG_UST_EVENT
:
621 return lttng_abi_create_event(objd
, (struct lttng_ust_event
*) arg
);
622 case LTTNG_UST_CONTEXT
:
623 return lttng_abi_add_context(objd
,
624 (struct lttng_ust_context
*) arg
,
625 &channel
->ctx
, channel
->session
);
626 case LTTNG_UST_ENABLE
:
627 return ltt_channel_enable(channel
);
628 case LTTNG_UST_DISABLE
:
629 return ltt_channel_disable(channel
);
636 * lttng_metadata_cmd - lttng control through object descriptors
638 * @objd: the object descriptor
642 * This object descriptor implements lttng commands:
644 * Returns an event stream file descriptor or failure.
646 * Channel and event file descriptors also hold a reference on the session.
649 long lttng_metadata_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
652 case LTTNG_UST_STREAM
:
654 struct lttng_ust_stream
*stream
;
656 stream
= (struct lttng_ust_stream
*) arg
;
657 /* stream used as output */
658 return lttng_abi_open_stream(objd
, stream
);
667 * lttng_channel_poll - lttng stream addition/removal monitoring
672 unsigned int lttng_channel_poll(struct file
*file
, poll_table
*wait
)
674 struct ltt_channel
*channel
= file
->private_data
;
675 unsigned int mask
= 0;
677 if (file
->f_mode
& FMODE_READ
) {
678 poll_wait_set_exclusive(wait
);
679 poll_wait(file
, channel
->ops
->get_hp_wait_queue(channel
->chan
),
682 if (channel
->ops
->is_disabled(channel
->chan
))
684 if (channel
->ops
->is_finalized(channel
->chan
))
686 if (channel
->ops
->buffer_has_read_closed_stream(channel
->chan
))
687 return POLLIN
| POLLRDNORM
;
696 int lttng_channel_release(int objd
)
698 struct ltt_channel
*channel
= objd_private(objd
);
701 return objd_unref(channel
->session
->objd
);
705 static const struct objd_ops lttng_channel_ops
= {
706 .release
= lttng_channel_release
,
707 //.poll = lttng_channel_poll,
708 .cmd
= lttng_channel_cmd
,
711 static const struct objd_ops lttng_metadata_ops
= {
712 .release
= lttng_channel_release
,
713 .cmd
= lttng_metadata_cmd
,
717 * lttng_rb_cmd - lttng ring buffer control through object descriptors
719 * @objd: the object descriptor
723 * This object descriptor implements lttng commands:
724 * (None for now. Access is done directly though shm.)
725 * TODO: Add buffer flush.
728 long lttng_rb_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
730 //struct stream_priv_data *priv = objd_private(objd);
739 int lttng_rb_release(int objd
)
741 struct stream_priv_data
*priv
= objd_private(objd
);
742 struct lib_ring_buffer
*buf
;
743 struct ltt_channel
*channel
;
747 channel
= priv
->ltt_chan
;
749 channel
->ops
->buffer_read_close(buf
, channel
->handle
);
751 return objd_unref(channel
->objd
);
756 static const struct objd_ops lib_ring_buffer_objd_ops
= {
757 .release
= lttng_rb_release
,
762 * lttng_event_cmd - lttng control through object descriptors
764 * @objd: the object descriptor
768 * This object descriptor implements lttng commands:
770 * Prepend a context field to each record of this event
772 * Enable recording for this event (weak enable)
774 * Disable recording for this event (strong disable)
777 long lttng_event_cmd(int objd
, unsigned int cmd
, unsigned long arg
)
779 struct ltt_event
*event
= objd_private(objd
);
782 case LTTNG_UST_CONTEXT
:
783 return lttng_abi_add_context(objd
,
784 (struct lttng_ust_context
*) arg
,
785 &event
->ctx
, event
->chan
->session
);
786 case LTTNG_UST_ENABLE
:
787 return ltt_event_enable(event
);
788 case LTTNG_UST_DISABLE
:
789 return ltt_event_disable(event
);
796 int lttng_event_release(int objd
)
798 struct ltt_event
*event
= objd_private(objd
);
801 return objd_unref(event
->chan
->objd
);
805 /* TODO: filter control ioctl */
806 static const struct objd_ops lttng_event_ops
= {
807 .release
= lttng_event_release
,
808 .cmd
= lttng_event_cmd
,
811 void lttng_ust_abi_exit(void)
813 objd_table_destroy();