2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <lttng/ust-config.h>
27 #include <lttng/ust-ctl.h>
28 #include <lttng/ust-abi.h>
29 #include <lttng/ust-events.h>
30 #include <usterr-signal-safe.h>
34 #include "../libringbuffer/backend.h"
35 #include "../libringbuffer/frontend.h"
36 #include "../liblttng-ust/wait.h"
37 #include "../liblttng-ust/lttng-rb-clients.h"
38 #include "../liblttng-ust/clock.h"
39 #include "../liblttng-ust/getenv.h"
42 * Number of milliseconds to retry before failing metadata writes on
43 * buffer full condition. (10 seconds)
45 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
48 * Channel representation within consumer.
50 struct ustctl_consumer_channel
{
51 struct lttng_channel
*chan
; /* lttng channel buffers */
53 /* initial attributes */
54 struct ustctl_consumer_channel_attr attr
;
55 int wait_fd
; /* monitor close() */
56 int wakeup_fd
; /* monitor close() */
60 * Stream representation within consumer.
62 struct ustctl_consumer_stream
{
63 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
64 struct lttng_ust_lib_ring_buffer
*buf
;
65 struct ustctl_consumer_channel
*chan
;
66 int shm_fd
, wait_fd
, wakeup_fd
;
68 uint64_t memory_map_size
;
71 extern void lttng_ring_buffer_client_overwrite_init(void);
72 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
73 extern void lttng_ring_buffer_client_discard_init(void);
74 extern void lttng_ring_buffer_client_discard_rt_init(void);
75 extern void lttng_ring_buffer_metadata_client_init(void);
76 extern void lttng_ring_buffer_client_overwrite_exit(void);
77 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
78 extern void lttng_ring_buffer_client_discard_exit(void);
79 extern void lttng_ring_buffer_client_discard_rt_exit(void);
80 extern void lttng_ring_buffer_metadata_client_exit(void);
82 int ustctl_release_handle(int sock
, int handle
)
84 struct ustcomm_ust_msg lum
;
85 struct ustcomm_ust_reply lur
;
87 if (sock
< 0 || handle
< 0)
89 memset(&lum
, 0, sizeof(lum
));
91 lum
.cmd
= LTTNG_UST_RELEASE
;
92 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
96 * If sock is negative, it means we don't have to notify the other side
97 * (e.g. application has already vanished).
99 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
106 switch (data
->type
) {
107 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
108 if (data
->u
.channel
.wakeup_fd
>= 0) {
109 ret
= close(data
->u
.channel
.wakeup_fd
);
114 data
->u
.channel
.wakeup_fd
= -1;
116 free(data
->u
.channel
.data
);
117 data
->u
.channel
.data
= NULL
;
119 case LTTNG_UST_OBJECT_TYPE_STREAM
:
120 if (data
->u
.stream
.shm_fd
>= 0) {
121 ret
= close(data
->u
.stream
.shm_fd
);
126 data
->u
.stream
.shm_fd
= -1;
128 if (data
->u
.stream
.wakeup_fd
>= 0) {
129 ret
= close(data
->u
.stream
.wakeup_fd
);
134 data
->u
.stream
.wakeup_fd
= -1;
137 case LTTNG_UST_OBJECT_TYPE_EVENT
:
138 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
143 return ustctl_release_handle(sock
, data
->handle
);
147 * Send registration done packet to the application.
149 int ustctl_register_done(int sock
)
151 struct ustcomm_ust_msg lum
;
152 struct ustcomm_ust_reply lur
;
155 DBG("Sending register done command to %d", sock
);
156 memset(&lum
, 0, sizeof(lum
));
157 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
158 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
159 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
166 * returns session handle.
168 int ustctl_create_session(int sock
)
170 struct ustcomm_ust_msg lum
;
171 struct ustcomm_ust_reply lur
;
172 int ret
, session_handle
;
175 memset(&lum
, 0, sizeof(lum
));
176 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
177 lum
.cmd
= LTTNG_UST_SESSION
;
178 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
181 session_handle
= lur
.ret_val
;
182 DBG("received session handle %u", session_handle
);
183 return session_handle
;
186 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
187 struct lttng_ust_object_data
*channel_data
,
188 struct lttng_ust_object_data
**_event_data
)
190 struct ustcomm_ust_msg lum
;
191 struct ustcomm_ust_reply lur
;
192 struct lttng_ust_object_data
*event_data
;
195 if (!channel_data
|| !_event_data
)
198 event_data
= zmalloc(sizeof(*event_data
));
201 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
202 memset(&lum
, 0, sizeof(lum
));
203 lum
.handle
= channel_data
->handle
;
204 lum
.cmd
= LTTNG_UST_EVENT
;
205 strncpy(lum
.u
.event
.name
, ev
->name
,
206 LTTNG_UST_SYM_NAME_LEN
);
207 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
208 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
209 lum
.u
.event
.loglevel
= ev
->loglevel
;
210 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
215 event_data
->handle
= lur
.ret_val
;
216 DBG("received event handle %u", event_data
->handle
);
217 *_event_data
= event_data
;
221 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
222 struct lttng_ust_object_data
*obj_data
,
223 struct lttng_ust_object_data
**_context_data
)
225 struct ustcomm_ust_msg lum
;
226 struct ustcomm_ust_reply lur
;
227 struct lttng_ust_object_data
*context_data
= NULL
;
232 if (!obj_data
|| !_context_data
) {
237 context_data
= zmalloc(sizeof(*context_data
));
242 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
243 memset(&lum
, 0, sizeof(lum
));
244 lum
.handle
= obj_data
->handle
;
245 lum
.cmd
= LTTNG_UST_CONTEXT
;
247 lum
.u
.context
.ctx
= ctx
->ctx
;
249 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
250 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
252 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
254 size_t provider_name_len
= strlen(
255 ctx
->u
.app_ctx
.provider_name
) + 1;
256 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
258 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
259 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
261 len
= provider_name_len
+ ctx_name_len
;
267 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
269 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
276 ret
= ustcomm_send_app_msg(sock
, &lum
);
280 /* send var len ctx_name */
281 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
290 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
294 context_data
->handle
= -1;
295 DBG("Context created successfully");
296 *_context_data
= context_data
;
304 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
305 struct lttng_ust_object_data
*obj_data
)
307 struct ustcomm_ust_msg lum
;
308 struct ustcomm_ust_reply lur
;
314 memset(&lum
, 0, sizeof(lum
));
315 lum
.handle
= obj_data
->handle
;
316 lum
.cmd
= LTTNG_UST_FILTER
;
317 lum
.u
.filter
.data_size
= bytecode
->len
;
318 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
319 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
321 ret
= ustcomm_send_app_msg(sock
, &lum
);
324 /* send var len bytecode */
325 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
330 if (ret
!= bytecode
->len
)
332 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
335 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
336 struct lttng_ust_object_data
*obj_data
)
338 struct ustcomm_ust_msg lum
;
339 struct ustcomm_ust_reply lur
;
346 memset(&lum
, 0, sizeof(lum
));
347 lum
.handle
= obj_data
->handle
;
348 lum
.cmd
= LTTNG_UST_EXCLUSION
;
349 lum
.u
.exclusion
.count
= exclusion
->count
;
351 ret
= ustcomm_send_app_msg(sock
, &lum
);
356 /* send var len exclusion names */
357 ret
= ustcomm_send_unix_sock(sock
,
359 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
363 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
366 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
369 /* Enable event, channel and session ioctl */
370 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
372 struct ustcomm_ust_msg lum
;
373 struct ustcomm_ust_reply lur
;
379 memset(&lum
, 0, sizeof(lum
));
380 lum
.handle
= object
->handle
;
381 lum
.cmd
= LTTNG_UST_ENABLE
;
382 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
385 DBG("enabled handle %u", object
->handle
);
389 /* Disable event, channel and session ioctl */
390 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
392 struct ustcomm_ust_msg lum
;
393 struct ustcomm_ust_reply lur
;
399 memset(&lum
, 0, sizeof(lum
));
400 lum
.handle
= object
->handle
;
401 lum
.cmd
= LTTNG_UST_DISABLE
;
402 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
405 DBG("disable handle %u", object
->handle
);
409 int ustctl_start_session(int sock
, int handle
)
411 struct lttng_ust_object_data obj
;
414 return ustctl_enable(sock
, &obj
);
417 int ustctl_stop_session(int sock
, int handle
)
419 struct lttng_ust_object_data obj
;
422 return ustctl_disable(sock
, &obj
);
425 int ustctl_tracepoint_list(int sock
)
427 struct ustcomm_ust_msg lum
;
428 struct ustcomm_ust_reply lur
;
429 int ret
, tp_list_handle
;
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
433 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
434 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
437 tp_list_handle
= lur
.ret_val
;
438 DBG("received tracepoint list handle %u", tp_list_handle
);
439 return tp_list_handle
;
442 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
443 struct lttng_ust_tracepoint_iter
*iter
)
445 struct ustcomm_ust_msg lum
;
446 struct ustcomm_ust_reply lur
;
452 memset(&lum
, 0, sizeof(lum
));
453 lum
.handle
= tp_list_handle
;
454 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
455 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
458 DBG("received tracepoint list entry name %s loglevel %d",
459 lur
.u
.tracepoint
.name
,
460 lur
.u
.tracepoint
.loglevel
);
461 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
465 int ustctl_tracepoint_field_list(int sock
)
467 struct ustcomm_ust_msg lum
;
468 struct ustcomm_ust_reply lur
;
469 int ret
, tp_field_list_handle
;
471 memset(&lum
, 0, sizeof(lum
));
472 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
473 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
474 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
477 tp_field_list_handle
= lur
.ret_val
;
478 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
479 return tp_field_list_handle
;
482 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
483 struct lttng_ust_field_iter
*iter
)
485 struct ustcomm_ust_msg lum
;
486 struct ustcomm_ust_reply lur
;
493 memset(&lum
, 0, sizeof(lum
));
494 lum
.handle
= tp_field_list_handle
;
495 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
496 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
499 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
500 if (len
!= sizeof(*iter
)) {
503 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
511 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
513 struct ustcomm_ust_msg lum
;
514 struct ustcomm_ust_reply lur
;
520 memset(&lum
, 0, sizeof(lum
));
521 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
522 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
523 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
526 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
527 DBG("received tracer version");
531 int ustctl_wait_quiescent(int sock
)
533 struct ustcomm_ust_msg lum
;
534 struct ustcomm_ust_reply lur
;
537 memset(&lum
, 0, sizeof(lum
));
538 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
539 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
540 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
543 DBG("waited for quiescent state");
547 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
555 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
557 struct ustcomm_ust_msg lum
;
558 struct ustcomm_ust_reply lur
;
564 memset(&lum
, 0, sizeof(lum
));
565 lum
.handle
= object
->handle
;
566 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
567 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
570 DBG("flushed buffer handle %u", object
->handle
);
575 int ustctl_send_channel(int sock
,
576 enum lttng_ust_chan_type type
,
586 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
587 if (len
!= sizeof(size
)) {
594 /* Send channel type */
595 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
596 if (len
!= sizeof(type
)) {
604 /* Send channel data */
605 len
= ustcomm_send_unix_sock(sock
, data
, size
);
614 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
625 int ustctl_send_stream(int sock
,
627 uint64_t memory_map_size
,
628 int shm_fd
, int wakeup_fd
,
636 /* finish iteration */
639 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
640 if (len
!= sizeof(v
)) {
650 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
651 sizeof(memory_map_size
));
652 if (len
!= sizeof(memory_map_size
)) {
660 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
662 if (len
!= sizeof(stream_nr
)) {
670 /* Send shm fd and wakeup fd */
673 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
683 int ustctl_recv_channel_from_consumer(int sock
,
684 struct lttng_ust_object_data
**_channel_data
)
686 struct lttng_ust_object_data
*channel_data
;
691 channel_data
= zmalloc(sizeof(*channel_data
));
696 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
697 channel_data
->handle
= -1;
700 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
701 sizeof(channel_data
->size
));
702 if (len
!= sizeof(channel_data
->size
)) {
710 /* recv channel type */
711 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
712 sizeof(channel_data
->u
.channel
.type
));
713 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
721 /* recv channel data */
722 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
723 if (!channel_data
->u
.channel
.data
) {
727 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
729 if (len
!= channel_data
->size
) {
734 goto error_recv_data
;
737 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
741 goto error_recv_data
;
744 goto error_recv_data
;
747 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
748 *_channel_data
= channel_data
;
752 free(channel_data
->u
.channel
.data
);
759 int ustctl_recv_stream_from_consumer(int sock
,
760 struct lttng_ust_object_data
**_stream_data
)
762 struct lttng_ust_object_data
*stream_data
;
767 stream_data
= zmalloc(sizeof(*stream_data
));
773 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
774 stream_data
->handle
= -1;
777 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
778 sizeof(stream_data
->size
));
779 if (len
!= sizeof(stream_data
->size
)) {
786 if (stream_data
->size
== -1) {
787 ret
= -LTTNG_UST_ERR_NOENT
;
792 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
793 sizeof(stream_data
->u
.stream
.stream_nr
));
794 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
802 /* recv shm fd and wakeup fd */
803 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
813 stream_data
->u
.stream
.shm_fd
= fds
[0];
814 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
815 *_stream_data
= stream_data
;
824 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
825 struct lttng_ust_object_data
*channel_data
)
827 struct ustcomm_ust_msg lum
;
828 struct ustcomm_ust_reply lur
;
834 memset(&lum
, 0, sizeof(lum
));
835 lum
.handle
= session_handle
;
836 lum
.cmd
= LTTNG_UST_CHANNEL
;
837 lum
.u
.channel
.len
= channel_data
->size
;
838 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
839 ret
= ustcomm_send_app_msg(sock
, &lum
);
843 ret
= ustctl_send_channel(sock
,
844 channel_data
->u
.channel
.type
,
845 channel_data
->u
.channel
.data
,
847 channel_data
->u
.channel
.wakeup_fd
,
851 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
853 channel_data
->handle
= lur
.ret_val
;
858 int ustctl_send_stream_to_ust(int sock
,
859 struct lttng_ust_object_data
*channel_data
,
860 struct lttng_ust_object_data
*stream_data
)
862 struct ustcomm_ust_msg lum
;
863 struct ustcomm_ust_reply lur
;
866 memset(&lum
, 0, sizeof(lum
));
867 lum
.handle
= channel_data
->handle
;
868 lum
.cmd
= LTTNG_UST_STREAM
;
869 lum
.u
.stream
.len
= stream_data
->size
;
870 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
871 ret
= ustcomm_send_app_msg(sock
, &lum
);
876 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
878 ret
= ustctl_send_stream(sock
,
879 stream_data
->u
.stream
.stream_nr
,
881 stream_data
->u
.stream
.shm_fd
,
882 stream_data
->u
.stream
.wakeup_fd
, 1);
885 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
888 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
889 struct lttng_ust_object_data
*src
)
891 struct lttng_ust_object_data
*obj
;
894 if (src
->handle
!= -1) {
899 obj
= zmalloc(sizeof(*obj
));
905 obj
->type
= src
->type
;
906 obj
->handle
= src
->handle
;
907 obj
->size
= src
->size
;
910 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
912 obj
->u
.channel
.type
= src
->u
.channel
.type
;
913 if (src
->u
.channel
.wakeup_fd
>= 0) {
914 obj
->u
.channel
.wakeup_fd
=
915 dup(src
->u
.channel
.wakeup_fd
);
916 if (obj
->u
.channel
.wakeup_fd
< 0) {
918 goto chan_error_wakeup_fd
;
921 obj
->u
.channel
.wakeup_fd
=
922 src
->u
.channel
.wakeup_fd
;
924 obj
->u
.channel
.data
= zmalloc(obj
->size
);
925 if (!obj
->u
.channel
.data
) {
927 goto chan_error_alloc
;
929 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
933 if (src
->u
.channel
.wakeup_fd
>= 0) {
936 closeret
= close(obj
->u
.channel
.wakeup_fd
);
941 chan_error_wakeup_fd
:
946 case LTTNG_UST_OBJECT_TYPE_STREAM
:
948 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
949 if (src
->u
.stream
.wakeup_fd
>= 0) {
950 obj
->u
.stream
.wakeup_fd
=
951 dup(src
->u
.stream
.wakeup_fd
);
952 if (obj
->u
.stream
.wakeup_fd
< 0) {
954 goto stream_error_wakeup_fd
;
957 obj
->u
.stream
.wakeup_fd
=
958 src
->u
.stream
.wakeup_fd
;
961 if (src
->u
.stream
.shm_fd
>= 0) {
962 obj
->u
.stream
.shm_fd
=
963 dup(src
->u
.stream
.shm_fd
);
964 if (obj
->u
.stream
.shm_fd
< 0) {
966 goto stream_error_shm_fd
;
969 obj
->u
.stream
.shm_fd
=
970 src
->u
.stream
.shm_fd
;
975 if (src
->u
.stream
.wakeup_fd
>= 0) {
978 closeret
= close(obj
->u
.stream
.wakeup_fd
);
983 stream_error_wakeup_fd
:
1002 /* Buffer operations */
1004 int ustctl_get_nr_stream_per_channel(void)
1006 return num_possible_cpus();
1009 struct ustctl_consumer_channel
*
1010 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1011 const int *stream_fds
, int nr_stream_fds
)
1013 struct ustctl_consumer_channel
*chan
;
1014 const char *transport_name
;
1015 struct lttng_transport
*transport
;
1017 switch (attr
->type
) {
1018 case LTTNG_UST_CHAN_PER_CPU
:
1019 if (attr
->output
== LTTNG_UST_MMAP
) {
1020 if (attr
->overwrite
) {
1021 if (attr
->read_timer_interval
== 0) {
1022 transport_name
= "relay-overwrite-mmap";
1024 transport_name
= "relay-overwrite-rt-mmap";
1027 if (attr
->read_timer_interval
== 0) {
1028 transport_name
= "relay-discard-mmap";
1030 transport_name
= "relay-discard-rt-mmap";
1037 case LTTNG_UST_CHAN_METADATA
:
1038 if (attr
->output
== LTTNG_UST_MMAP
)
1039 transport_name
= "relay-metadata-mmap";
1044 transport_name
= "<unknown>";
1048 transport
= lttng_transport_find(transport_name
);
1050 DBG("LTTng transport %s not found\n",
1055 chan
= zmalloc(sizeof(*chan
));
1059 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1060 attr
->subbuf_size
, attr
->num_subbuf
,
1061 attr
->switch_timer_interval
,
1062 attr
->read_timer_interval
,
1063 attr
->uuid
, attr
->chan_id
,
1064 stream_fds
, nr_stream_fds
,
1065 attr
->blocking_timeout
);
1069 chan
->chan
->ops
= &transport
->ops
;
1070 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1071 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1072 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1080 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1082 (void) ustctl_channel_close_wait_fd(chan
);
1083 (void) ustctl_channel_close_wakeup_fd(chan
);
1084 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1088 int ustctl_send_channel_to_sessiond(int sock
,
1089 struct ustctl_consumer_channel
*channel
)
1091 struct shm_object_table
*table
;
1093 table
= channel
->chan
->handle
->table
;
1094 if (table
->size
<= 0)
1096 return ustctl_send_channel(sock
,
1098 table
->objects
[0].memory_map
,
1099 table
->objects
[0].memory_map_size
,
1104 int ustctl_send_stream_to_sessiond(int sock
,
1105 struct ustctl_consumer_stream
*stream
)
1108 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1110 return ustctl_send_stream(sock
,
1112 stream
->memory_map_size
,
1113 stream
->shm_fd
, stream
->wakeup_fd
,
1117 int ustctl_write_metadata_to_channel(
1118 struct ustctl_consumer_channel
*channel
,
1119 const char *metadata_str
, /* NOT null-terminated */
1120 size_t len
) /* metadata length */
1122 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1123 struct lttng_channel
*chan
= channel
->chan
;
1124 const char *str
= metadata_str
;
1125 int ret
= 0, waitret
;
1126 size_t reserve_len
, pos
;
1128 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1129 reserve_len
= min_t(size_t,
1130 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1132 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1133 sizeof(char), -1, chan
->handle
, NULL
);
1135 * We don't care about metadata buffer's records lost
1136 * count, because we always retry here. Report error if
1137 * we need to bail out after timeout or being
1140 waitret
= wait_cond_interruptible_timeout(
1142 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1143 ret
!= -ENOBUFS
|| !ret
;
1145 LTTNG_METADATA_TIMEOUT_MSEC
);
1146 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1147 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1148 waitret
== -EINTR
? "interrupted" :
1149 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1150 if (waitret
== -EINTR
)
1154 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1155 chan
->ops
->event_commit(&ctx
);
1162 * Write at most one packet in the channel.
1163 * Returns the number of bytes written on success, < 0 on error.
1165 ssize_t
ustctl_write_one_packet_to_channel(
1166 struct ustctl_consumer_channel
*channel
,
1167 const char *metadata_str
, /* NOT null-terminated */
1168 size_t len
) /* metadata length */
1170 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1171 struct lttng_channel
*chan
= channel
->chan
;
1172 const char *str
= metadata_str
;
1173 ssize_t reserve_len
;
1176 reserve_len
= min_t(ssize_t
,
1177 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1179 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1180 sizeof(char), -1, chan
->handle
, NULL
);
1181 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1183 DBG("LTTng: event reservation failed");
1188 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1189 chan
->ops
->event_commit(&ctx
);
1195 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1197 struct channel
*chan
;
1200 chan
= consumer_chan
->chan
->chan
;
1201 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1202 chan
, chan
->handle
);
1204 consumer_chan
->wait_fd
= -1;
1208 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1210 struct channel
*chan
;
1213 chan
= consumer_chan
->chan
->chan
;
1214 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1215 chan
, chan
->handle
);
1217 consumer_chan
->wakeup_fd
= -1;
1221 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1223 struct channel
*chan
;
1225 chan
= stream
->chan
->chan
->chan
;
1226 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1227 chan
, stream
->handle
, stream
->cpu
);
1230 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1232 struct channel
*chan
;
1234 chan
= stream
->chan
->chan
->chan
;
1235 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1236 chan
, stream
->handle
, stream
->cpu
);
1239 struct ustctl_consumer_stream
*
1240 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1243 struct ustctl_consumer_stream
*stream
;
1244 struct lttng_ust_shm_handle
*handle
;
1245 struct channel
*chan
;
1246 int shm_fd
, wait_fd
, wakeup_fd
;
1247 uint64_t memory_map_size
;
1248 struct lttng_ust_lib_ring_buffer
*buf
;
1253 handle
= channel
->chan
->handle
;
1257 chan
= channel
->chan
->chan
;
1258 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1259 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1260 &wakeup_fd
, &memory_map_size
);
1263 ret
= lib_ring_buffer_open_read(buf
, handle
);
1267 stream
= zmalloc(sizeof(*stream
));
1270 stream
->handle
= handle
;
1272 stream
->chan
= channel
;
1273 stream
->shm_fd
= shm_fd
;
1274 stream
->wait_fd
= wait_fd
;
1275 stream
->wakeup_fd
= wakeup_fd
;
1276 stream
->memory_map_size
= memory_map_size
;
1284 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1286 struct lttng_ust_lib_ring_buffer
*buf
;
1287 struct ustctl_consumer_channel
*consumer_chan
;
1291 consumer_chan
= stream
->chan
;
1292 (void) ustctl_stream_close_wait_fd(stream
);
1293 (void) ustctl_stream_close_wakeup_fd(stream
);
1294 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1298 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1302 return shm_get_wait_fd(chan
->chan
->handle
,
1303 &chan
->chan
->handle
->chan
._ref
);
1306 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1310 return shm_get_wakeup_fd(chan
->chan
->handle
,
1311 &chan
->chan
->handle
->chan
._ref
);
1314 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1316 struct lttng_ust_lib_ring_buffer
*buf
;
1317 struct ustctl_consumer_channel
*consumer_chan
;
1322 consumer_chan
= stream
->chan
;
1323 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1326 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1328 struct lttng_ust_lib_ring_buffer
*buf
;
1329 struct ustctl_consumer_channel
*consumer_chan
;
1334 consumer_chan
= stream
->chan
;
1335 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1338 /* For mmap mode, readable without "get" operation */
1340 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1342 struct lttng_ust_lib_ring_buffer
*buf
;
1343 struct ustctl_consumer_channel
*consumer_chan
;
1348 consumer_chan
= stream
->chan
;
1349 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1352 /* returns the length to mmap. */
1353 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1356 struct ustctl_consumer_channel
*consumer_chan
;
1357 unsigned long mmap_buf_len
;
1358 struct channel
*chan
;
1362 consumer_chan
= stream
->chan
;
1363 chan
= consumer_chan
->chan
->chan
;
1364 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1366 mmap_buf_len
= chan
->backend
.buf_size
;
1367 if (chan
->backend
.extra_reader_sb
)
1368 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1369 if (mmap_buf_len
> INT_MAX
)
1371 *len
= mmap_buf_len
;
1375 /* returns the maximum size for sub-buffers. */
1376 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1379 struct ustctl_consumer_channel
*consumer_chan
;
1380 struct channel
*chan
;
1384 consumer_chan
= stream
->chan
;
1385 chan
= consumer_chan
->chan
->chan
;
1386 *len
= chan
->backend
.subbuf_size
;
1391 * For mmap mode, operate on the current packet (between get/put or
1392 * get_next/put_next).
1395 /* returns the offset of the subbuffer belonging to the mmap reader. */
1396 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1399 struct channel
*chan
;
1400 unsigned long sb_bindex
;
1401 struct lttng_ust_lib_ring_buffer
*buf
;
1402 struct ustctl_consumer_channel
*consumer_chan
;
1403 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1404 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1409 consumer_chan
= stream
->chan
;
1410 chan
= consumer_chan
->chan
->chan
;
1411 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1413 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1414 buf
->backend
.buf_rsb
.id
);
1415 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1419 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1422 *off
= pages
->mmap_offset
;
1426 /* returns the size of the current sub-buffer, without padding (for mmap). */
1427 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1430 struct ustctl_consumer_channel
*consumer_chan
;
1431 struct channel
*chan
;
1432 struct lttng_ust_lib_ring_buffer
*buf
;
1438 consumer_chan
= stream
->chan
;
1439 chan
= consumer_chan
->chan
->chan
;
1440 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1441 consumer_chan
->chan
->handle
);
1445 /* returns the size of the current sub-buffer, without padding (for mmap). */
1446 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1449 struct ustctl_consumer_channel
*consumer_chan
;
1450 struct channel
*chan
;
1451 struct lttng_ust_lib_ring_buffer
*buf
;
1456 consumer_chan
= stream
->chan
;
1457 chan
= consumer_chan
->chan
->chan
;
1458 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1459 consumer_chan
->chan
->handle
);
1460 *len
= PAGE_ALIGN(*len
);
1464 /* Get exclusive read access to the next sub-buffer that can be read. */
1465 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1467 struct lttng_ust_lib_ring_buffer
*buf
;
1468 struct ustctl_consumer_channel
*consumer_chan
;
1473 consumer_chan
= stream
->chan
;
1474 return lib_ring_buffer_get_next_subbuf(buf
,
1475 consumer_chan
->chan
->handle
);
1479 /* Release exclusive sub-buffer access, move consumer forward. */
1480 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1482 struct lttng_ust_lib_ring_buffer
*buf
;
1483 struct ustctl_consumer_channel
*consumer_chan
;
1488 consumer_chan
= stream
->chan
;
1489 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1495 /* Get a snapshot of the current ring buffer producer and consumer positions */
1496 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1498 struct lttng_ust_lib_ring_buffer
*buf
;
1499 struct ustctl_consumer_channel
*consumer_chan
;
1504 consumer_chan
= stream
->chan
;
1505 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1506 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1510 * Get a snapshot of the current ring buffer producer and consumer positions
1511 * even if the consumed and produced positions are contained within the same
1514 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1516 struct lttng_ust_lib_ring_buffer
*buf
;
1517 struct ustctl_consumer_channel
*consumer_chan
;
1522 consumer_chan
= stream
->chan
;
1523 return lib_ring_buffer_snapshot_sample_positions(buf
,
1524 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1525 consumer_chan
->chan
->handle
);
1528 /* Get the consumer position (iteration start) */
1529 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1532 struct lttng_ust_lib_ring_buffer
*buf
;
1537 *pos
= buf
->cons_snapshot
;
1541 /* Get the producer position (iteration end) */
1542 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1545 struct lttng_ust_lib_ring_buffer
*buf
;
1550 *pos
= buf
->prod_snapshot
;
1554 /* Get exclusive read access to the specified sub-buffer position */
1555 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1558 struct lttng_ust_lib_ring_buffer
*buf
;
1559 struct ustctl_consumer_channel
*consumer_chan
;
1564 consumer_chan
= stream
->chan
;
1565 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1566 consumer_chan
->chan
->handle
);
1569 /* Release exclusive sub-buffer access */
1570 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1572 struct lttng_ust_lib_ring_buffer
*buf
;
1573 struct ustctl_consumer_channel
*consumer_chan
;
1578 consumer_chan
= stream
->chan
;
1579 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1583 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1584 int producer_active
)
1586 struct lttng_ust_lib_ring_buffer
*buf
;
1587 struct ustctl_consumer_channel
*consumer_chan
;
1591 consumer_chan
= stream
->chan
;
1592 lib_ring_buffer_switch_slow(buf
,
1593 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1594 consumer_chan
->chan
->handle
);
1597 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1599 struct lttng_ust_lib_ring_buffer
*buf
;
1600 struct ustctl_consumer_channel
*consumer_chan
;
1604 consumer_chan
= stream
->chan
;
1605 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1606 consumer_chan
->chan
->handle
);
1607 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1611 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1612 struct lttng_ust_lib_ring_buffer
*buf
,
1613 struct lttng_ust_shm_handle
*handle
)
1615 struct channel
*chan
;
1616 const struct lttng_ust_lib_ring_buffer_config
*config
;
1617 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1619 chan
= shmp(handle
, buf
->backend
.chan
);
1622 config
= &chan
->backend
.config
;
1623 if (!config
->cb_ptr
)
1625 client_cb
= caa_container_of(config
->cb_ptr
,
1626 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1631 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1632 uint64_t *timestamp_begin
)
1634 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1635 struct lttng_ust_lib_ring_buffer
*buf
;
1636 struct lttng_ust_shm_handle
*handle
;
1638 if (!stream
|| !timestamp_begin
)
1641 handle
= stream
->chan
->chan
->handle
;
1642 client_cb
= get_client_cb(buf
, handle
);
1645 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1648 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1649 uint64_t *timestamp_end
)
1651 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1652 struct lttng_ust_lib_ring_buffer
*buf
;
1653 struct lttng_ust_shm_handle
*handle
;
1655 if (!stream
|| !timestamp_end
)
1658 handle
= stream
->chan
->chan
->handle
;
1659 client_cb
= get_client_cb(buf
, handle
);
1662 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1665 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1666 uint64_t *events_discarded
)
1668 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1669 struct lttng_ust_lib_ring_buffer
*buf
;
1670 struct lttng_ust_shm_handle
*handle
;
1672 if (!stream
|| !events_discarded
)
1675 handle
= stream
->chan
->chan
->handle
;
1676 client_cb
= get_client_cb(buf
, handle
);
1679 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1682 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1683 uint64_t *content_size
)
1685 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1686 struct lttng_ust_lib_ring_buffer
*buf
;
1687 struct lttng_ust_shm_handle
*handle
;
1689 if (!stream
|| !content_size
)
1692 handle
= stream
->chan
->chan
->handle
;
1693 client_cb
= get_client_cb(buf
, handle
);
1696 return client_cb
->content_size(buf
, handle
, content_size
);
1699 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1700 uint64_t *packet_size
)
1702 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1703 struct lttng_ust_lib_ring_buffer
*buf
;
1704 struct lttng_ust_shm_handle
*handle
;
1706 if (!stream
|| !packet_size
)
1709 handle
= stream
->chan
->chan
->handle
;
1710 client_cb
= get_client_cb(buf
, handle
);
1713 return client_cb
->packet_size(buf
, handle
, packet_size
);
1716 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1717 uint64_t *stream_id
)
1719 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1720 struct lttng_ust_lib_ring_buffer
*buf
;
1721 struct lttng_ust_shm_handle
*handle
;
1723 if (!stream
|| !stream_id
)
1726 handle
= stream
->chan
->chan
->handle
;
1727 client_cb
= get_client_cb(buf
, handle
);
1730 return client_cb
->stream_id(buf
, handle
, stream_id
);
1733 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1736 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1737 struct lttng_ust_lib_ring_buffer
*buf
;
1738 struct lttng_ust_shm_handle
*handle
;
1743 handle
= stream
->chan
->chan
->handle
;
1744 client_cb
= get_client_cb(buf
, handle
);
1745 if (!client_cb
|| !client_cb
->current_timestamp
)
1747 return client_cb
->current_timestamp(buf
, handle
, ts
);
1750 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1753 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1754 struct lttng_ust_lib_ring_buffer
*buf
;
1755 struct lttng_ust_shm_handle
*handle
;
1757 if (!stream
|| !seq
)
1760 handle
= stream
->chan
->chan
->handle
;
1761 client_cb
= get_client_cb(buf
, handle
);
1762 if (!client_cb
|| !client_cb
->sequence_number
)
1764 return client_cb
->sequence_number(buf
, handle
, seq
);
1767 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1770 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1771 struct lttng_ust_lib_ring_buffer
*buf
;
1772 struct lttng_ust_shm_handle
*handle
;
1777 handle
= stream
->chan
->chan
->handle
;
1778 client_cb
= get_client_cb(buf
, handle
);
1781 return client_cb
->instance_id(buf
, handle
, id
);
1784 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1786 int ustctl_has_perf_counters(void)
1793 int ustctl_has_perf_counters(void)
1801 * Returns 0 on success, negative error value on error.
1803 int ustctl_recv_reg_msg(int sock
,
1804 enum ustctl_socket_type
*type
,
1811 uint32_t *bits_per_long
,
1812 uint32_t *uint8_t_alignment
,
1813 uint32_t *uint16_t_alignment
,
1814 uint32_t *uint32_t_alignment
,
1815 uint32_t *uint64_t_alignment
,
1816 uint32_t *long_alignment
,
1821 struct ustctl_reg_msg reg_msg
;
1823 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1824 if (len
> 0 && len
!= sizeof(reg_msg
))
1831 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1832 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1833 BIG_ENDIAN
: LITTLE_ENDIAN
;
1834 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1835 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1836 LITTLE_ENDIAN
: BIG_ENDIAN
;
1838 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1840 switch (reg_msg
.socket_type
) {
1841 case 0: *type
= USTCTL_SOCKET_CMD
;
1843 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1846 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1848 *major
= reg_msg
.major
;
1849 *minor
= reg_msg
.minor
;
1851 *ppid
= reg_msg
.ppid
;
1854 *bits_per_long
= reg_msg
.bits_per_long
;
1855 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1856 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1857 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1858 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1859 *long_alignment
= reg_msg
.long_alignment
;
1860 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1861 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
1862 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
1863 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1869 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1871 struct ustcomm_notify_hdr header
;
1874 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1875 if (len
> 0 && len
!= sizeof(header
))
1881 switch (header
.notify_cmd
) {
1883 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1886 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1889 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1898 * Returns 0 on success, negative error value on error.
1900 int ustctl_recv_register_event(int sock
,
1907 struct ustctl_field
**fields
,
1908 char **model_emf_uri
)
1911 struct ustcomm_notify_event_msg msg
;
1912 size_t signature_len
, fields_len
, model_emf_uri_len
;
1913 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1914 struct ustctl_field
*a_fields
= NULL
;
1916 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1917 if (len
> 0 && len
!= sizeof(msg
))
1924 *session_objd
= msg
.session_objd
;
1925 *channel_objd
= msg
.channel_objd
;
1926 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1927 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1928 *loglevel
= msg
.loglevel
;
1929 signature_len
= msg
.signature_len
;
1930 fields_len
= msg
.fields_len
;
1932 if (fields_len
% sizeof(*a_fields
) != 0) {
1936 model_emf_uri_len
= msg
.model_emf_uri_len
;
1938 /* recv signature. contains at least \0. */
1939 a_sign
= zmalloc(signature_len
);
1942 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1943 if (len
> 0 && len
!= signature_len
) {
1945 goto signature_error
;
1949 goto signature_error
;
1952 goto signature_error
;
1954 /* Enforce end of string */
1955 a_sign
[signature_len
- 1] = '\0';
1959 a_fields
= zmalloc(fields_len
);
1962 goto signature_error
;
1964 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1965 if (len
> 0 && len
!= fields_len
) {
1978 if (model_emf_uri_len
) {
1979 /* recv model_emf_uri_len */
1980 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1981 if (!a_model_emf_uri
) {
1985 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1987 if (len
> 0 && len
!= model_emf_uri_len
) {
1998 /* Enforce end of string */
1999 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2002 *signature
= a_sign
;
2003 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2005 *model_emf_uri
= a_model_emf_uri
;
2010 free(a_model_emf_uri
);
2019 * Returns 0 on success, negative error value on error.
2021 int ustctl_reply_register_event(int sock
,
2027 struct ustcomm_notify_hdr header
;
2028 struct ustcomm_notify_event_reply r
;
2031 memset(&reply
, 0, sizeof(reply
));
2032 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2033 reply
.r
.ret_code
= ret_code
;
2034 reply
.r
.event_id
= id
;
2035 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2036 if (len
> 0 && len
!= sizeof(reply
))
2044 * Returns 0 on success, negative UST or system error value on error.
2046 int ustctl_recv_register_enum(int sock
,
2049 struct ustctl_enum_entry
**entries
,
2053 struct ustcomm_notify_enum_msg msg
;
2055 struct ustctl_enum_entry
*a_entries
= NULL
;
2057 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2058 if (len
> 0 && len
!= sizeof(msg
))
2065 *session_objd
= msg
.session_objd
;
2066 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2067 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2068 entries_len
= msg
.entries_len
;
2070 if (entries_len
% sizeof(*a_entries
) != 0) {
2076 a_entries
= zmalloc(entries_len
);
2079 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2080 if (len
> 0 && len
!= entries_len
) {
2092 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2093 *entries
= a_entries
;
2103 * Returns 0 on success, negative error value on error.
2105 int ustctl_reply_register_enum(int sock
,
2111 struct ustcomm_notify_hdr header
;
2112 struct ustcomm_notify_enum_reply r
;
2115 memset(&reply
, 0, sizeof(reply
));
2116 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2117 reply
.r
.ret_code
= ret_code
;
2118 reply
.r
.enum_id
= id
;
2119 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2120 if (len
> 0 && len
!= sizeof(reply
))
2128 * Returns 0 on success, negative UST or system error value on error.
2130 int ustctl_recv_register_channel(int sock
,
2131 int *session_objd
, /* session descriptor (output) */
2132 int *channel_objd
, /* channel descriptor (output) */
2134 struct ustctl_field
**fields
)
2137 struct ustcomm_notify_channel_msg msg
;
2139 struct ustctl_field
*a_fields
;
2141 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2142 if (len
> 0 && len
!= sizeof(msg
))
2149 *session_objd
= msg
.session_objd
;
2150 *channel_objd
= msg
.channel_objd
;
2151 fields_len
= msg
.ctx_fields_len
;
2153 if (fields_len
% sizeof(*a_fields
) != 0) {
2159 a_fields
= zmalloc(fields_len
);
2164 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2165 if (len
> 0 && len
!= fields_len
) {
2180 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2190 * Returns 0 on success, negative error value on error.
2192 int ustctl_reply_register_channel(int sock
,
2194 enum ustctl_channel_header header_type
,
2199 struct ustcomm_notify_hdr header
;
2200 struct ustcomm_notify_channel_reply r
;
2203 memset(&reply
, 0, sizeof(reply
));
2204 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2205 reply
.r
.ret_code
= ret_code
;
2206 reply
.r
.chan_id
= chan_id
;
2207 switch (header_type
) {
2208 case USTCTL_CHANNEL_HEADER_COMPACT
:
2209 reply
.r
.header_type
= 1;
2211 case USTCTL_CHANNEL_HEADER_LARGE
:
2212 reply
.r
.header_type
= 2;
2215 reply
.r
.header_type
= 0;
2218 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2219 if (len
> 0 && len
!= sizeof(reply
))
2226 /* Regenerate the statedump. */
2227 int ustctl_regenerate_statedump(int sock
, int handle
)
2229 struct ustcomm_ust_msg lum
;
2230 struct ustcomm_ust_reply lur
;
2233 memset(&lum
, 0, sizeof(lum
));
2234 lum
.handle
= handle
;
2235 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2236 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2239 DBG("Regenerated statedump for handle %u", handle
);
2243 static __attribute__((constructor
))
2244 void ustctl_init(void)
2247 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2248 lttng_ust_clock_init();
2249 lttng_ring_buffer_metadata_client_init();
2250 lttng_ring_buffer_client_overwrite_init();
2251 lttng_ring_buffer_client_overwrite_rt_init();
2252 lttng_ring_buffer_client_discard_init();
2253 lttng_ring_buffer_client_discard_rt_init();
2254 lib_ringbuffer_signal_init();
2257 static __attribute__((destructor
))
2258 void ustctl_exit(void)
2260 lttng_ring_buffer_client_discard_rt_exit();
2261 lttng_ring_buffer_client_discard_exit();
2262 lttng_ring_buffer_client_overwrite_rt_exit();
2263 lttng_ring_buffer_client_overwrite_exit();
2264 lttng_ring_buffer_metadata_client_exit();