6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Mimic system calls for:
24 * - session creation, returns an object descriptor or failure.
25 * - channel creation, returns an object descriptor or failure.
26 * - Operates on a session object descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns an object descriptor or failure.
29 * - Operates on a channel object descriptor.
30 * - stream notifier get, returns an object descriptor or failure.
31 * - Operates on a channel object descriptor.
32 * - event creation, returns an object descriptor or failure.
33 * - Operates on a channel object descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
40 #include <lttng/ust-abi.h>
41 #include <lttng/ust-error.h>
42 #include <urcu/compiler.h>
43 #include <urcu/list.h>
44 #include <lttng/ust-events.h>
45 #include <lttng/ust-version.h>
46 #include <lttng/tracepoint.h>
47 #include "tracepoint-internal.h"
48 #include <usterr-signal-safe.h>
50 #include "lttng-tracer.h"
51 #include "../libringbuffer/shm.h"
52 #include "../libringbuffer/frontend_types.h"
54 #define OBJ_NAME_LEN 16
56 static int lttng_ust_abi_close_in_progress
;
59 int lttng_abi_tracepoint_list(void *owner
);
61 int lttng_abi_tracepoint_field_list(void *owner
);
64 * Object descriptor table. Should be protected from concurrent access
68 struct lttng_ust_obj
{
72 const struct lttng_ust_objd_ops
*ops
;
75 char name
[OBJ_NAME_LEN
];
77 int freelist_next
; /* offset freelist. end is -1. */
81 struct lttng_ust_objd_table
{
82 struct lttng_ust_obj
*array
;
83 unsigned int len
, allocated_len
;
84 int freelist_head
; /* offset freelist head. end is -1 */
87 static struct lttng_ust_objd_table objd_table
= {
92 int objd_alloc(void *private_data
, const struct lttng_ust_objd_ops
*ops
,
93 void *owner
, const char *name
)
95 struct lttng_ust_obj
*obj
;
97 if (objd_table
.freelist_head
!= -1) {
98 obj
= &objd_table
.array
[objd_table
.freelist_head
];
99 objd_table
.freelist_head
= obj
->u
.freelist_next
;
103 if (objd_table
.len
>= objd_table
.allocated_len
) {
104 unsigned int new_allocated_len
, old_allocated_len
;
105 struct lttng_ust_obj
*new_table
, *old_table
;
107 old_allocated_len
= objd_table
.allocated_len
;
108 old_table
= objd_table
.array
;
109 if (!old_allocated_len
)
110 new_allocated_len
= 1;
112 new_allocated_len
= old_allocated_len
<< 1;
113 new_table
= zmalloc(sizeof(struct lttng_ust_obj
) * new_allocated_len
);
116 memcpy(new_table
, old_table
,
117 sizeof(struct lttng_ust_obj
) * old_allocated_len
);
119 objd_table
.array
= new_table
;
120 objd_table
.allocated_len
= new_allocated_len
;
122 obj
= &objd_table
.array
[objd_table
.len
];
125 obj
->u
.s
.private_data
= private_data
;
127 obj
->u
.s
.f_count
= 2; /* count == 1 : object is allocated */
128 /* count == 2 : allocated + hold ref */
129 obj
->u
.s
.owner
= owner
;
130 strncpy(obj
->u
.s
.name
, name
, OBJ_NAME_LEN
);
131 obj
->u
.s
.name
[OBJ_NAME_LEN
- 1] = '\0';
132 return obj
- objd_table
.array
;
136 struct lttng_ust_obj
*_objd_get(int id
)
138 if (id
>= objd_table
.len
)
140 if (!objd_table
.array
[id
].u
.s
.f_count
)
142 return &objd_table
.array
[id
];
146 void *objd_private(int id
)
148 struct lttng_ust_obj
*obj
= _objd_get(id
);
150 return obj
->u
.s
.private_data
;
154 void objd_set_private(int id
, void *private_data
)
156 struct lttng_ust_obj
*obj
= _objd_get(id
);
158 obj
->u
.s
.private_data
= private_data
;
161 const struct lttng_ust_objd_ops
*objd_ops(int id
)
163 struct lttng_ust_obj
*obj
= _objd_get(id
);
171 void objd_free(int id
)
173 struct lttng_ust_obj
*obj
= _objd_get(id
);
176 obj
->u
.freelist_next
= objd_table
.freelist_head
;
177 objd_table
.freelist_head
= obj
- objd_table
.array
;
178 assert(obj
->u
.s
.f_count
== 1);
179 obj
->u
.s
.f_count
= 0; /* deallocated */
183 void objd_ref(int id
)
185 struct lttng_ust_obj
*obj
= _objd_get(id
);
189 int lttng_ust_objd_unref(int id
)
191 struct lttng_ust_obj
*obj
= _objd_get(id
);
195 if (obj
->u
.s
.f_count
== 1) {
196 ERR("Reference counting error\n");
199 if ((--obj
->u
.s
.f_count
) == 1) {
200 const struct lttng_ust_objd_ops
*ops
= objd_ops(id
);
210 void objd_table_destroy(void)
214 for (i
= 0; i
< objd_table
.allocated_len
; i
++)
215 (void) lttng_ust_objd_unref(i
);
216 free(objd_table
.array
);
217 objd_table
.array
= NULL
;
219 objd_table
.allocated_len
= 0;
220 objd_table
.freelist_head
= -1;
223 const char *lttng_ust_obj_get_name(int id
)
225 struct lttng_ust_obj
*obj
= _objd_get(id
);
229 return obj
->u
.s
.name
;
232 void lttng_ust_objd_table_owner_cleanup(void *owner
)
236 for (i
= 0; i
< objd_table
.allocated_len
; i
++) {
237 struct lttng_ust_obj
*obj
;
243 continue; /* skip root handles */
244 if (obj
->u
.s
.owner
== owner
)
245 (void) lttng_ust_objd_unref(i
);
250 * This is LTTng's own personal way to create an ABI for sessiond.
251 * We send commands over a socket.
254 static const struct lttng_ust_objd_ops lttng_ops
;
255 static const struct lttng_ust_objd_ops lttng_session_ops
;
256 static const struct lttng_ust_objd_ops lttng_channel_ops
;
257 static const struct lttng_ust_objd_ops lttng_enabler_ops
;
258 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
;
259 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops
;
261 int lttng_abi_create_root_handle(void)
265 /* root handles have NULL owners */
266 root_handle
= objd_alloc(NULL
, <tng_ops
, NULL
, "root");
271 int lttng_is_channel_ready(struct lttng_channel
*lttng_chan
)
273 struct channel
*chan
;
274 unsigned int nr_streams
, exp_streams
;
276 chan
= lttng_chan
->chan
;
277 nr_streams
= channel_handle_get_nr_streams(lttng_chan
->handle
);
278 exp_streams
= chan
->nr_streams
;
279 return nr_streams
== exp_streams
;
283 int lttng_abi_create_session(void *owner
)
285 struct lttng_session
*session
;
286 int session_objd
, ret
;
288 session
= lttng_session_create();
291 session_objd
= objd_alloc(session
, <tng_session_ops
, owner
, "session");
292 if (session_objd
< 0) {
296 session
->objd
= session_objd
;
297 session
->owner
= owner
;
301 lttng_session_destroy(session
);
306 long lttng_abi_tracer_version(int objd
,
307 struct lttng_ust_tracer_version
*v
)
309 v
->major
= LTTNG_UST_MAJOR_VERSION
;
310 v
->minor
= LTTNG_UST_MINOR_VERSION
;
311 v
->patchlevel
= LTTNG_UST_PATCHLEVEL_VERSION
;
316 long lttng_abi_add_context(int objd
,
317 struct lttng_ust_context
*context_param
,
318 struct lttng_ctx
**ctx
, struct lttng_session
*session
)
320 return lttng_attach_context(context_param
, ctx
, session
);
324 * lttng_cmd - lttng control through socket commands
326 * @objd: the object descriptor
329 * @uargs: UST arguments (internal)
332 * This descriptor implements lttng commands:
334 * Returns a LTTng trace session object descriptor
335 * LTTNG_UST_TRACER_VERSION
336 * Returns the LTTng kernel tracer version
337 * LTTNG_UST_TRACEPOINT_LIST
338 * Returns a file descriptor listing available tracepoints
339 * LTTNG_UST_TRACEPOINT_FIELD_LIST
340 * Returns a file descriptor listing available tracepoint fields
341 * LTTNG_UST_WAIT_QUIESCENT
342 * Returns after all previously running probes have completed
344 * The returned session will be deleted when its file descriptor is closed.
347 long lttng_cmd(int objd
, unsigned int cmd
, unsigned long arg
,
348 union ust_args
*uargs
, void *owner
)
351 case LTTNG_UST_SESSION
:
352 return lttng_abi_create_session(owner
);
353 case LTTNG_UST_TRACER_VERSION
:
354 return lttng_abi_tracer_version(objd
,
355 (struct lttng_ust_tracer_version
*) arg
);
356 case LTTNG_UST_TRACEPOINT_LIST
:
357 return lttng_abi_tracepoint_list(owner
);
358 case LTTNG_UST_TRACEPOINT_FIELD_LIST
:
359 return lttng_abi_tracepoint_field_list(owner
);
360 case LTTNG_UST_WAIT_QUIESCENT
:
368 static const struct lttng_ust_objd_ops lttng_ops
= {
372 int lttng_abi_map_channel(int session_objd
,
373 struct lttng_ust_channel
*ust_chan
,
374 union ust_args
*uargs
,
377 struct lttng_session
*session
= objd_private(session_objd
);
378 const char *transport_name
;
379 const struct lttng_transport
*transport
;
380 const char *chan_name
;
382 struct lttng_ust_shm_handle
*channel_handle
;
383 struct lttng_channel
*lttng_chan
;
384 struct channel
*chan
;
385 struct lttng_ust_lib_ring_buffer_config
*config
;
389 enum lttng_ust_chan_type type
;
391 chan_data
= uargs
->channel
.chan_data
;
393 type
= ust_chan
->type
;
396 case LTTNG_UST_CHAN_PER_CPU
:
402 if (session
->been_active
) {
404 goto active
; /* Refuse to add channel to active session */
407 channel_handle
= channel_handle_create(chan_data
, len
);
408 if (!channel_handle
) {
413 chan
= shmp(channel_handle
, channel_handle
->chan
);
415 config
= &chan
->backend
.config
;
416 lttng_chan
= channel_get_private(chan
);
422 /* Lookup transport name */
424 case LTTNG_UST_CHAN_PER_CPU
:
425 if (config
->output
== RING_BUFFER_MMAP
) {
426 transport_name
= config
->mode
== RING_BUFFER_OVERWRITE
?
427 "relay-overwrite-mmap" : "relay-discard-mmap";
432 chan_name
= "channel";
435 transport_name
= "<unknown>";
436 chan_name
= "<unknown>";
440 transport
= lttng_transport_find(transport_name
);
442 DBG("LTTng transport %s not found\n",
448 chan_objd
= objd_alloc(NULL
, <tng_channel_ops
, owner
, chan_name
);
454 /* Initialize our lttng chan */
455 lttng_chan
->chan
= chan
;
456 lttng_chan
->enabled
= 1;
457 lttng_chan
->ctx
= NULL
;
458 lttng_chan
->session
= session
;
459 lttng_chan
->ops
= &transport
->ops
;
460 memcpy(<tng_chan
->chan
->backend
.config
,
461 transport
->client_config
,
462 sizeof(lttng_chan
->chan
->backend
.config
));
463 cds_list_add(<tng_chan
->node
, &session
->chan_head
);
464 lttng_chan
->header_type
= 0;
465 lttng_chan
->handle
= channel_handle
;
466 lttng_chan
->type
= type
;
469 * We tolerate no failure path after channel creation. It will stay
470 * invariant for the rest of the session.
472 objd_set_private(chan_objd
, lttng_chan
);
473 lttng_chan
->objd
= chan_objd
;
474 /* The channel created holds a reference on the session */
475 objd_ref(session_objd
);
482 channel_destroy(chan
, channel_handle
, 0);
489 * lttng_session_cmd - lttng session object command
494 * @uargs: UST arguments (internal)
497 * This descriptor implements lttng commands:
499 * Returns a LTTng channel object descriptor
501 * Enables tracing for a session (weak enable)
503 * Disables tracing for a session (strong disable)
505 * The returned channel will be deleted when its file descriptor is closed.
508 long lttng_session_cmd(int objd
, unsigned int cmd
, unsigned long arg
,
509 union ust_args
*uargs
, void *owner
)
511 struct lttng_session
*session
= objd_private(objd
);
514 case LTTNG_UST_CHANNEL
:
515 return lttng_abi_map_channel(objd
,
516 (struct lttng_ust_channel
*) arg
,
518 case LTTNG_UST_SESSION_START
:
519 case LTTNG_UST_ENABLE
:
520 return lttng_session_enable(session
);
521 case LTTNG_UST_SESSION_STOP
:
522 case LTTNG_UST_DISABLE
:
523 return lttng_session_disable(session
);
530 * Called when the last file reference is dropped.
532 * Big fat note: channels and events are invariant for the whole session after
533 * their creation. So this session destruction also destroys all channel and
534 * event structures specific to this session (they are not destroyed when their
535 * individual file is released).
538 int lttng_release_session(int objd
)
540 struct lttng_session
*session
= objd_private(objd
);
543 lttng_session_destroy(session
);
550 static const struct lttng_ust_objd_ops lttng_session_ops
= {
551 .release
= lttng_release_session
,
552 .cmd
= lttng_session_cmd
,
556 long lttng_tracepoint_list_cmd(int objd
, unsigned int cmd
, unsigned long arg
,
557 union ust_args
*uargs
, void *owner
)
559 struct lttng_ust_tracepoint_list
*list
= objd_private(objd
);
560 struct lttng_ust_tracepoint_iter
*tp
=
561 (struct lttng_ust_tracepoint_iter
*) arg
;
562 struct lttng_ust_tracepoint_iter
*iter
;
565 case LTTNG_UST_TRACEPOINT_LIST_GET
:
567 iter
= lttng_ust_tracepoint_list_get_iter_next(list
);
569 return -LTTNG_UST_ERR_NOENT
;
570 memcpy(tp
, iter
, sizeof(*tp
));
579 int lttng_abi_tracepoint_list(void *owner
)
582 struct lttng_ust_tracepoint_list
*list
;
584 list_objd
= objd_alloc(NULL
, <tng_tracepoint_list_ops
, owner
, "tp_list");
589 list
= zmalloc(sizeof(*list
));
594 objd_set_private(list_objd
, list
);
596 /* populate list by walking on all registered probes. */
597 ret
= lttng_probes_get_event_list(list
);
609 err
= lttng_ust_objd_unref(list_objd
);
617 int lttng_release_tracepoint_list(int objd
)
619 struct lttng_ust_tracepoint_list
*list
= objd_private(objd
);
622 lttng_probes_prune_event_list(list
);
630 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops
= {
631 .release
= lttng_release_tracepoint_list
,
632 .cmd
= lttng_tracepoint_list_cmd
,
636 long lttng_tracepoint_field_list_cmd(int objd
, unsigned int cmd
,
637 unsigned long arg
, union ust_args
*uargs
, void *owner
)
639 struct lttng_ust_field_list
*list
= objd_private(objd
);
640 struct lttng_ust_field_iter
*tp
= &uargs
->field_list
.entry
;
641 struct lttng_ust_field_iter
*iter
;
644 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
646 iter
= lttng_ust_field_list_get_iter_next(list
);
648 return -LTTNG_UST_ERR_NOENT
;
649 memcpy(tp
, iter
, sizeof(*tp
));
658 int lttng_abi_tracepoint_field_list(void *owner
)
661 struct lttng_ust_field_list
*list
;
663 list_objd
= objd_alloc(NULL
, <tng_tracepoint_field_list_ops
, owner
,
669 list
= zmalloc(sizeof(*list
));
674 objd_set_private(list_objd
, list
);
676 /* populate list by walking on all registered probes. */
677 ret
= lttng_probes_get_field_list(list
);
689 err
= lttng_ust_objd_unref(list_objd
);
697 int lttng_release_tracepoint_field_list(int objd
)
699 struct lttng_ust_field_list
*list
= objd_private(objd
);
702 lttng_probes_prune_field_list(list
);
710 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops
= {
711 .release
= lttng_release_tracepoint_field_list
,
712 .cmd
= lttng_tracepoint_field_list_cmd
,
716 int lttng_abi_map_stream(int channel_objd
, struct lttng_ust_stream
*info
,
717 union ust_args
*uargs
, void *owner
)
719 struct lttng_channel
*channel
= objd_private(channel_objd
);
722 ret
= channel_handle_add_stream(channel
->handle
,
723 uargs
->stream
.shm_fd
, uargs
->stream
.wakeup_fd
,
724 info
->stream_nr
, info
->len
);
726 goto error_add_stream
;
735 int lttng_abi_create_enabler(int channel_objd
,
736 struct lttng_ust_event
*event_param
,
738 enum lttng_enabler_type type
)
740 struct lttng_channel
*channel
= objd_private(channel_objd
);
741 struct lttng_enabler
*enabler
;
744 event_param
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
745 event_objd
= objd_alloc(NULL
, <tng_enabler_ops
, owner
, "enabler");
746 if (event_objd
< 0) {
751 * We tolerate no failure path after event creation. It will stay
752 * invariant for the rest of the session.
754 enabler
= lttng_enabler_create(type
, event_param
, channel
);
759 objd_set_private(event_objd
, enabler
);
760 /* The event holds a reference on the channel */
761 objd_ref(channel_objd
);
768 err
= lttng_ust_objd_unref(event_objd
);
776 * lttng_channel_cmd - lttng control through object descriptors
778 * @objd: the object descriptor
781 * @uargs: UST arguments (internal)
784 * This object descriptor implements lttng commands:
786 * Returns an event stream object descriptor or failure.
787 * (typically, one event stream records events from one CPU)
789 * Returns an event object descriptor or failure.
791 * Prepend a context field to each event in the channel
793 * Enable recording for events in this channel (weak enable)
795 * Disable recording for events in this channel (strong disable)
797 * Channel and event file descriptors also hold a reference on the session.
800 long lttng_channel_cmd(int objd
, unsigned int cmd
, unsigned long arg
,
801 union ust_args
*uargs
, void *owner
)
803 struct lttng_channel
*channel
= objd_private(objd
);
805 if (cmd
!= LTTNG_UST_STREAM
) {
807 * Check if channel received all streams.
809 if (!lttng_is_channel_ready(channel
))
814 case LTTNG_UST_STREAM
:
816 struct lttng_ust_stream
*stream
;
818 stream
= (struct lttng_ust_stream
*) arg
;
819 /* stream used as output */
820 return lttng_abi_map_stream(objd
, stream
, uargs
, owner
);
822 case LTTNG_UST_EVENT
:
824 struct lttng_ust_event
*event_param
=
825 (struct lttng_ust_event
*) arg
;
826 if (event_param
->name
[strlen(event_param
->name
) - 1] == '*') {
827 /* If ends with wildcard, create wildcard. */
828 return lttng_abi_create_enabler(objd
, event_param
,
829 owner
, LTTNG_ENABLER_WILDCARD
);
831 return lttng_abi_create_enabler(objd
, event_param
,
832 owner
, LTTNG_ENABLER_EVENT
);
835 case LTTNG_UST_CONTEXT
:
836 return lttng_abi_add_context(objd
,
837 (struct lttng_ust_context
*) arg
,
838 &channel
->ctx
, channel
->session
);
839 case LTTNG_UST_ENABLE
:
840 return lttng_channel_enable(channel
);
841 case LTTNG_UST_DISABLE
:
842 return lttng_channel_disable(channel
);
843 case LTTNG_UST_FLUSH_BUFFER
:
844 return channel
->ops
->flush_buffer(channel
->chan
, channel
->handle
);
851 int lttng_channel_release(int objd
)
853 struct lttng_channel
*channel
= objd_private(objd
);
856 return lttng_ust_objd_unref(channel
->session
->objd
);
860 static const struct lttng_ust_objd_ops lttng_channel_ops
= {
861 .release
= lttng_channel_release
,
862 .cmd
= lttng_channel_cmd
,
866 * lttng_enabler_cmd - lttng control through object descriptors
868 * @objd: the object descriptor
871 * @uargs: UST arguments (internal)
874 * This object descriptor implements lttng commands:
876 * Prepend a context field to each record of events of this
879 * Enable recording for this enabler
881 * Disable recording for this enabler
883 * Attach a filter to an enabler.
886 long lttng_enabler_cmd(int objd
, unsigned int cmd
, unsigned long arg
,
887 union ust_args
*uargs
, void *owner
)
889 struct lttng_enabler
*enabler
= objd_private(objd
);
892 case LTTNG_UST_CONTEXT
:
893 return lttng_enabler_attach_context(enabler
,
894 (struct lttng_ust_context
*) arg
);
895 case LTTNG_UST_ENABLE
:
896 return lttng_enabler_enable(enabler
);
897 case LTTNG_UST_DISABLE
:
898 return lttng_enabler_disable(enabler
);
899 case LTTNG_UST_FILTER
:
903 ret
= lttng_enabler_attach_bytecode(enabler
,
904 (struct lttng_ust_filter_bytecode_node
*) arg
);
915 int lttng_enabler_release(int objd
)
917 struct lttng_enabler
*enabler
= objd_private(objd
);
920 return lttng_ust_objd_unref(enabler
->chan
->objd
);
924 static const struct lttng_ust_objd_ops lttng_enabler_ops
= {
925 .release
= lttng_enabler_release
,
926 .cmd
= lttng_enabler_cmd
,
929 void lttng_ust_abi_exit(void)
931 lttng_ust_abi_close_in_progress
= 1;
932 objd_table_destroy();
933 lttng_ust_abi_close_in_progress
= 0;