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.
21 #include <lttng/ust-config.h>
22 #include <lttng/ust-ctl.h>
23 #include <lttng/ust-abi.h>
24 #include <lttng/ust-events.h>
28 #include <usterr-signal-safe.h>
32 #include "../libringbuffer/backend.h"
33 #include "../libringbuffer/frontend.h"
34 #include "../liblttng-ust/wait.h"
35 #include "../liblttng-ust/lttng-rb-clients.h"
36 #include "../liblttng-ust/clock.h"
37 #include "../liblttng-ust/getenv.h"
40 * Number of milliseconds to retry before failing metadata writes on
41 * buffer full condition. (10 seconds)
43 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
46 * Channel representation within consumer.
48 struct ustctl_consumer_channel
{
49 struct lttng_channel
*chan
; /* lttng channel buffers */
51 /* initial attributes */
52 struct ustctl_consumer_channel_attr attr
;
53 int wait_fd
; /* monitor close() */
54 int wakeup_fd
; /* monitor close() */
58 * Stream representation within consumer.
60 struct ustctl_consumer_stream
{
61 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
62 struct lttng_ust_lib_ring_buffer
*buf
;
63 struct ustctl_consumer_channel
*chan
;
64 int shm_fd
, wait_fd
, wakeup_fd
;
66 uint64_t memory_map_size
;
69 extern void lttng_ring_buffer_client_overwrite_init(void);
70 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
71 extern void lttng_ring_buffer_client_discard_init(void);
72 extern void lttng_ring_buffer_client_discard_rt_init(void);
73 extern void lttng_ring_buffer_metadata_client_init(void);
74 extern void lttng_ring_buffer_client_overwrite_exit(void);
75 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
76 extern void lttng_ring_buffer_client_discard_exit(void);
77 extern void lttng_ring_buffer_client_discard_rt_exit(void);
78 extern void lttng_ring_buffer_metadata_client_exit(void);
80 volatile enum ust_loglevel ust_loglevel
;
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
);
115 free(data
->u
.channel
.data
);
117 case LTTNG_UST_OBJECT_TYPE_STREAM
:
118 if (data
->u
.stream
.shm_fd
>= 0) {
119 ret
= close(data
->u
.stream
.shm_fd
);
125 if (data
->u
.stream
.wakeup_fd
>= 0) {
126 ret
= close(data
->u
.stream
.wakeup_fd
);
133 case LTTNG_UST_OBJECT_TYPE_EVENT
:
134 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
139 return ustctl_release_handle(sock
, data
->handle
);
143 * Send registration done packet to the application.
145 int ustctl_register_done(int sock
)
147 struct ustcomm_ust_msg lum
;
148 struct ustcomm_ust_reply lur
;
151 DBG("Sending register done command to %d", sock
);
152 memset(&lum
, 0, sizeof(lum
));
153 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
154 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
155 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
162 * returns session handle.
164 int ustctl_create_session(int sock
)
166 struct ustcomm_ust_msg lum
;
167 struct ustcomm_ust_reply lur
;
168 int ret
, session_handle
;
171 memset(&lum
, 0, sizeof(lum
));
172 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
173 lum
.cmd
= LTTNG_UST_SESSION
;
174 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
177 session_handle
= lur
.ret_val
;
178 DBG("received session handle %u", session_handle
);
179 return session_handle
;
182 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
183 struct lttng_ust_object_data
*channel_data
,
184 struct lttng_ust_object_data
**_event_data
)
186 struct ustcomm_ust_msg lum
;
187 struct ustcomm_ust_reply lur
;
188 struct lttng_ust_object_data
*event_data
;
191 if (!channel_data
|| !_event_data
)
194 event_data
= zmalloc(sizeof(*event_data
));
197 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
198 memset(&lum
, 0, sizeof(lum
));
199 lum
.handle
= channel_data
->handle
;
200 lum
.cmd
= LTTNG_UST_EVENT
;
201 strncpy(lum
.u
.event
.name
, ev
->name
,
202 LTTNG_UST_SYM_NAME_LEN
);
203 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
204 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
205 lum
.u
.event
.loglevel
= ev
->loglevel
;
206 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
211 event_data
->handle
= lur
.ret_val
;
212 DBG("received event handle %u", event_data
->handle
);
213 *_event_data
= event_data
;
217 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
218 struct lttng_ust_object_data
*obj_data
,
219 struct lttng_ust_object_data
**_context_data
)
221 struct ustcomm_ust_msg lum
;
222 struct ustcomm_ust_reply lur
;
223 struct lttng_ust_object_data
*context_data
= NULL
;
228 if (!obj_data
|| !_context_data
) {
233 context_data
= zmalloc(sizeof(*context_data
));
238 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
239 memset(&lum
, 0, sizeof(lum
));
240 lum
.handle
= obj_data
->handle
;
241 lum
.cmd
= LTTNG_UST_CONTEXT
;
243 lum
.u
.context
.ctx
= ctx
->ctx
;
245 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
246 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
248 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
250 size_t provider_name_len
= strlen(
251 ctx
->u
.app_ctx
.provider_name
) + 1;
252 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
254 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
255 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
257 len
= provider_name_len
+ ctx_name_len
;
263 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
265 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
272 ret
= ustcomm_send_app_msg(sock
, &lum
);
276 /* send var len ctx_name */
277 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
286 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
290 context_data
->handle
= -1;
291 DBG("Context created successfully");
292 *_context_data
= context_data
;
300 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
301 struct lttng_ust_object_data
*obj_data
)
303 struct ustcomm_ust_msg lum
;
304 struct ustcomm_ust_reply lur
;
310 memset(&lum
, 0, sizeof(lum
));
311 lum
.handle
= obj_data
->handle
;
312 lum
.cmd
= LTTNG_UST_FILTER
;
313 lum
.u
.filter
.data_size
= bytecode
->len
;
314 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
315 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
317 ret
= ustcomm_send_app_msg(sock
, &lum
);
320 /* send var len bytecode */
321 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
326 if (ret
!= bytecode
->len
)
328 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
331 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
332 struct lttng_ust_object_data
*obj_data
)
334 struct ustcomm_ust_msg lum
;
335 struct ustcomm_ust_reply lur
;
342 memset(&lum
, 0, sizeof(lum
));
343 lum
.handle
= obj_data
->handle
;
344 lum
.cmd
= LTTNG_UST_EXCLUSION
;
345 lum
.u
.exclusion
.count
= exclusion
->count
;
347 ret
= ustcomm_send_app_msg(sock
, &lum
);
352 /* send var len exclusion names */
353 ret
= ustcomm_send_unix_sock(sock
,
355 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
359 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
362 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
365 /* Enable event, channel and session ioctl */
366 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
368 struct ustcomm_ust_msg lum
;
369 struct ustcomm_ust_reply lur
;
375 memset(&lum
, 0, sizeof(lum
));
376 lum
.handle
= object
->handle
;
377 lum
.cmd
= LTTNG_UST_ENABLE
;
378 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
381 DBG("enabled handle %u", object
->handle
);
385 /* Disable event, channel and session ioctl */
386 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
388 struct ustcomm_ust_msg lum
;
389 struct ustcomm_ust_reply lur
;
395 memset(&lum
, 0, sizeof(lum
));
396 lum
.handle
= object
->handle
;
397 lum
.cmd
= LTTNG_UST_DISABLE
;
398 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
401 DBG("disable handle %u", object
->handle
);
405 int ustctl_start_session(int sock
, int handle
)
407 struct lttng_ust_object_data obj
;
410 return ustctl_enable(sock
, &obj
);
413 int ustctl_stop_session(int sock
, int handle
)
415 struct lttng_ust_object_data obj
;
418 return ustctl_disable(sock
, &obj
);
421 int ustctl_tracepoint_list(int sock
)
423 struct ustcomm_ust_msg lum
;
424 struct ustcomm_ust_reply lur
;
425 int ret
, tp_list_handle
;
427 memset(&lum
, 0, sizeof(lum
));
428 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
429 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
430 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
433 tp_list_handle
= lur
.ret_val
;
434 DBG("received tracepoint list handle %u", tp_list_handle
);
435 return tp_list_handle
;
438 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
439 struct lttng_ust_tracepoint_iter
*iter
)
441 struct ustcomm_ust_msg lum
;
442 struct ustcomm_ust_reply lur
;
448 memset(&lum
, 0, sizeof(lum
));
449 lum
.handle
= tp_list_handle
;
450 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
451 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
454 DBG("received tracepoint list entry name %s loglevel %d",
455 lur
.u
.tracepoint
.name
,
456 lur
.u
.tracepoint
.loglevel
);
457 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
461 int ustctl_tracepoint_field_list(int sock
)
463 struct ustcomm_ust_msg lum
;
464 struct ustcomm_ust_reply lur
;
465 int ret
, tp_field_list_handle
;
467 memset(&lum
, 0, sizeof(lum
));
468 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
469 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
470 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
473 tp_field_list_handle
= lur
.ret_val
;
474 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
475 return tp_field_list_handle
;
478 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
479 struct lttng_ust_field_iter
*iter
)
481 struct ustcomm_ust_msg lum
;
482 struct ustcomm_ust_reply lur
;
489 memset(&lum
, 0, sizeof(lum
));
490 lum
.handle
= tp_field_list_handle
;
491 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
492 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
495 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
496 if (len
!= sizeof(*iter
)) {
499 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
507 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
509 struct ustcomm_ust_msg lum
;
510 struct ustcomm_ust_reply lur
;
516 memset(&lum
, 0, sizeof(lum
));
517 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
518 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
519 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
522 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
523 DBG("received tracer version");
527 int ustctl_wait_quiescent(int sock
)
529 struct ustcomm_ust_msg lum
;
530 struct ustcomm_ust_reply lur
;
533 memset(&lum
, 0, sizeof(lum
));
534 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
535 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
536 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
539 DBG("waited for quiescent state");
543 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
551 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
553 struct ustcomm_ust_msg lum
;
554 struct ustcomm_ust_reply lur
;
560 memset(&lum
, 0, sizeof(lum
));
561 lum
.handle
= object
->handle
;
562 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
563 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
566 DBG("flushed buffer handle %u", object
->handle
);
571 int ustctl_send_channel(int sock
,
572 enum lttng_ust_chan_type type
,
582 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
583 if (len
!= sizeof(size
)) {
590 /* Send channel type */
591 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
592 if (len
!= sizeof(type
)) {
600 /* Send channel data */
601 len
= ustcomm_send_unix_sock(sock
, data
, size
);
610 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
621 int ustctl_send_stream(int sock
,
623 uint64_t memory_map_size
,
624 int shm_fd
, int wakeup_fd
,
632 /* finish iteration */
635 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
636 if (len
!= sizeof(v
)) {
646 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
647 sizeof(memory_map_size
));
648 if (len
!= sizeof(memory_map_size
)) {
656 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
658 if (len
!= sizeof(stream_nr
)) {
666 /* Send shm fd and wakeup fd */
669 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
679 int ustctl_recv_channel_from_consumer(int sock
,
680 struct lttng_ust_object_data
**_channel_data
)
682 struct lttng_ust_object_data
*channel_data
;
687 channel_data
= zmalloc(sizeof(*channel_data
));
692 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
693 channel_data
->handle
= -1;
696 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
697 sizeof(channel_data
->size
));
698 if (len
!= sizeof(channel_data
->size
)) {
706 /* recv channel type */
707 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
708 sizeof(channel_data
->u
.channel
.type
));
709 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
717 /* recv channel data */
718 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
719 if (!channel_data
->u
.channel
.data
) {
723 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
725 if (len
!= channel_data
->size
) {
730 goto error_recv_data
;
733 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
737 goto error_recv_data
;
740 goto error_recv_data
;
743 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
744 *_channel_data
= channel_data
;
748 free(channel_data
->u
.channel
.data
);
755 int ustctl_recv_stream_from_consumer(int sock
,
756 struct lttng_ust_object_data
**_stream_data
)
758 struct lttng_ust_object_data
*stream_data
;
763 stream_data
= zmalloc(sizeof(*stream_data
));
769 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
770 stream_data
->handle
= -1;
773 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
774 sizeof(stream_data
->size
));
775 if (len
!= sizeof(stream_data
->size
)) {
782 if (stream_data
->size
== -1) {
783 ret
= -LTTNG_UST_ERR_NOENT
;
788 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
789 sizeof(stream_data
->u
.stream
.stream_nr
));
790 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
798 /* recv shm fd and wakeup fd */
799 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
809 stream_data
->u
.stream
.shm_fd
= fds
[0];
810 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
811 *_stream_data
= stream_data
;
820 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
821 struct lttng_ust_object_data
*channel_data
)
823 struct ustcomm_ust_msg lum
;
824 struct ustcomm_ust_reply lur
;
830 memset(&lum
, 0, sizeof(lum
));
831 lum
.handle
= session_handle
;
832 lum
.cmd
= LTTNG_UST_CHANNEL
;
833 lum
.u
.channel
.len
= channel_data
->size
;
834 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
835 ret
= ustcomm_send_app_msg(sock
, &lum
);
839 ret
= ustctl_send_channel(sock
,
840 channel_data
->u
.channel
.type
,
841 channel_data
->u
.channel
.data
,
843 channel_data
->u
.channel
.wakeup_fd
,
847 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
849 channel_data
->handle
= lur
.ret_val
;
854 int ustctl_send_stream_to_ust(int sock
,
855 struct lttng_ust_object_data
*channel_data
,
856 struct lttng_ust_object_data
*stream_data
)
858 struct ustcomm_ust_msg lum
;
859 struct ustcomm_ust_reply lur
;
862 memset(&lum
, 0, sizeof(lum
));
863 lum
.handle
= channel_data
->handle
;
864 lum
.cmd
= LTTNG_UST_STREAM
;
865 lum
.u
.stream
.len
= stream_data
->size
;
866 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
867 ret
= ustcomm_send_app_msg(sock
, &lum
);
872 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
874 ret
= ustctl_send_stream(sock
,
875 stream_data
->u
.stream
.stream_nr
,
877 stream_data
->u
.stream
.shm_fd
,
878 stream_data
->u
.stream
.wakeup_fd
, 1);
881 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
884 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
885 struct lttng_ust_object_data
*src
)
887 struct lttng_ust_object_data
*obj
;
890 if (src
->handle
!= -1) {
895 obj
= zmalloc(sizeof(*obj
));
901 obj
->type
= src
->type
;
902 obj
->handle
= src
->handle
;
903 obj
->size
= src
->size
;
906 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
908 obj
->u
.channel
.type
= src
->u
.channel
.type
;
909 if (src
->u
.channel
.wakeup_fd
>= 0) {
910 obj
->u
.channel
.wakeup_fd
=
911 dup(src
->u
.channel
.wakeup_fd
);
912 if (obj
->u
.channel
.wakeup_fd
< 0) {
914 goto chan_error_wakeup_fd
;
917 obj
->u
.channel
.wakeup_fd
=
918 src
->u
.channel
.wakeup_fd
;
920 obj
->u
.channel
.data
= zmalloc(obj
->size
);
921 if (!obj
->u
.channel
.data
) {
923 goto chan_error_alloc
;
925 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
929 if (src
->u
.channel
.wakeup_fd
>= 0) {
932 closeret
= close(obj
->u
.channel
.wakeup_fd
);
937 chan_error_wakeup_fd
:
942 case LTTNG_UST_OBJECT_TYPE_STREAM
:
944 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
945 if (src
->u
.stream
.wakeup_fd
>= 0) {
946 obj
->u
.stream
.wakeup_fd
=
947 dup(src
->u
.stream
.wakeup_fd
);
948 if (obj
->u
.stream
.wakeup_fd
< 0) {
950 goto stream_error_wakeup_fd
;
953 obj
->u
.stream
.wakeup_fd
=
954 src
->u
.stream
.wakeup_fd
;
957 if (src
->u
.stream
.shm_fd
>= 0) {
958 obj
->u
.stream
.shm_fd
=
959 dup(src
->u
.stream
.shm_fd
);
960 if (obj
->u
.stream
.shm_fd
< 0) {
962 goto stream_error_shm_fd
;
965 obj
->u
.stream
.shm_fd
=
966 src
->u
.stream
.shm_fd
;
971 if (src
->u
.stream
.wakeup_fd
>= 0) {
974 closeret
= close(obj
->u
.stream
.wakeup_fd
);
979 stream_error_wakeup_fd
:
998 /* Buffer operations */
1000 int ustctl_get_nr_stream_per_channel(void)
1002 return num_possible_cpus();
1005 struct ustctl_consumer_channel
*
1006 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1007 const int *stream_fds
, int nr_stream_fds
)
1009 struct ustctl_consumer_channel
*chan
;
1010 const char *transport_name
;
1011 struct lttng_transport
*transport
;
1013 switch (attr
->type
) {
1014 case LTTNG_UST_CHAN_PER_CPU
:
1015 if (attr
->output
== LTTNG_UST_MMAP
) {
1016 if (attr
->overwrite
) {
1017 if (attr
->read_timer_interval
== 0) {
1018 transport_name
= "relay-overwrite-mmap";
1020 transport_name
= "relay-overwrite-rt-mmap";
1023 if (attr
->read_timer_interval
== 0) {
1024 transport_name
= "relay-discard-mmap";
1026 transport_name
= "relay-discard-rt-mmap";
1033 case LTTNG_UST_CHAN_METADATA
:
1034 if (attr
->output
== LTTNG_UST_MMAP
)
1035 transport_name
= "relay-metadata-mmap";
1040 transport_name
= "<unknown>";
1044 transport
= lttng_transport_find(transport_name
);
1046 DBG("LTTng transport %s not found\n",
1051 chan
= zmalloc(sizeof(*chan
));
1055 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1056 attr
->subbuf_size
, attr
->num_subbuf
,
1057 attr
->switch_timer_interval
,
1058 attr
->read_timer_interval
,
1059 attr
->uuid
, attr
->chan_id
,
1060 stream_fds
, nr_stream_fds
,
1061 attr
->blocking_timeout
);
1065 chan
->chan
->ops
= &transport
->ops
;
1066 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1067 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1068 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1076 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1078 (void) ustctl_channel_close_wait_fd(chan
);
1079 (void) ustctl_channel_close_wakeup_fd(chan
);
1080 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1084 int ustctl_send_channel_to_sessiond(int sock
,
1085 struct ustctl_consumer_channel
*channel
)
1087 struct shm_object_table
*table
;
1089 table
= channel
->chan
->handle
->table
;
1090 if (table
->size
<= 0)
1092 return ustctl_send_channel(sock
,
1094 table
->objects
[0].memory_map
,
1095 table
->objects
[0].memory_map_size
,
1100 int ustctl_send_stream_to_sessiond(int sock
,
1101 struct ustctl_consumer_stream
*stream
)
1104 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1106 return ustctl_send_stream(sock
,
1108 stream
->memory_map_size
,
1109 stream
->shm_fd
, stream
->wakeup_fd
,
1113 int ustctl_write_metadata_to_channel(
1114 struct ustctl_consumer_channel
*channel
,
1115 const char *metadata_str
, /* NOT null-terminated */
1116 size_t len
) /* metadata length */
1118 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1119 struct lttng_channel
*chan
= channel
->chan
;
1120 const char *str
= metadata_str
;
1121 int ret
= 0, waitret
;
1122 size_t reserve_len
, pos
;
1124 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1125 reserve_len
= min_t(size_t,
1126 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1128 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1129 sizeof(char), -1, chan
->handle
, NULL
);
1131 * We don't care about metadata buffer's records lost
1132 * count, because we always retry here. Report error if
1133 * we need to bail out after timeout or being
1136 waitret
= wait_cond_interruptible_timeout(
1138 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1139 ret
!= -ENOBUFS
|| !ret
;
1141 LTTNG_METADATA_TIMEOUT_MSEC
);
1142 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1143 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1144 waitret
== -EINTR
? "interrupted" :
1145 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1146 if (waitret
== -EINTR
)
1150 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1151 chan
->ops
->event_commit(&ctx
);
1158 * Write at most one packet in the channel.
1159 * Returns the number of bytes written on success, < 0 on error.
1161 ssize_t
ustctl_write_one_packet_to_channel(
1162 struct ustctl_consumer_channel
*channel
,
1163 const char *metadata_str
, /* NOT null-terminated */
1164 size_t len
) /* metadata length */
1166 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1167 struct lttng_channel
*chan
= channel
->chan
;
1168 const char *str
= metadata_str
;
1169 ssize_t reserve_len
;
1172 reserve_len
= min_t(ssize_t
,
1173 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1175 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1176 sizeof(char), -1, chan
->handle
, NULL
);
1177 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1179 DBG("LTTng: event reservation failed");
1184 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1185 chan
->ops
->event_commit(&ctx
);
1191 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1193 struct channel
*chan
;
1196 chan
= consumer_chan
->chan
->chan
;
1197 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1198 chan
, chan
->handle
);
1200 consumer_chan
->wait_fd
= -1;
1204 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1206 struct channel
*chan
;
1209 chan
= consumer_chan
->chan
->chan
;
1210 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1211 chan
, chan
->handle
);
1213 consumer_chan
->wakeup_fd
= -1;
1217 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1219 struct channel
*chan
;
1221 chan
= stream
->chan
->chan
->chan
;
1222 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1223 chan
, stream
->handle
, stream
->cpu
);
1226 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1228 struct channel
*chan
;
1230 chan
= stream
->chan
->chan
->chan
;
1231 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1232 chan
, stream
->handle
, stream
->cpu
);
1235 struct ustctl_consumer_stream
*
1236 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1239 struct ustctl_consumer_stream
*stream
;
1240 struct lttng_ust_shm_handle
*handle
;
1241 struct channel
*chan
;
1242 int shm_fd
, wait_fd
, wakeup_fd
;
1243 uint64_t memory_map_size
;
1244 struct lttng_ust_lib_ring_buffer
*buf
;
1249 handle
= channel
->chan
->handle
;
1253 chan
= channel
->chan
->chan
;
1254 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1255 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1256 &wakeup_fd
, &memory_map_size
);
1259 ret
= lib_ring_buffer_open_read(buf
, handle
);
1263 stream
= zmalloc(sizeof(*stream
));
1266 stream
->handle
= handle
;
1268 stream
->chan
= channel
;
1269 stream
->shm_fd
= shm_fd
;
1270 stream
->wait_fd
= wait_fd
;
1271 stream
->wakeup_fd
= wakeup_fd
;
1272 stream
->memory_map_size
= memory_map_size
;
1280 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1282 struct lttng_ust_lib_ring_buffer
*buf
;
1283 struct ustctl_consumer_channel
*consumer_chan
;
1287 consumer_chan
= stream
->chan
;
1288 (void) ustctl_stream_close_wait_fd(stream
);
1289 (void) ustctl_stream_close_wakeup_fd(stream
);
1290 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1294 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1298 return shm_get_wait_fd(chan
->chan
->handle
,
1299 &chan
->chan
->handle
->chan
._ref
);
1302 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1306 return shm_get_wakeup_fd(chan
->chan
->handle
,
1307 &chan
->chan
->handle
->chan
._ref
);
1310 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1312 struct lttng_ust_lib_ring_buffer
*buf
;
1313 struct ustctl_consumer_channel
*consumer_chan
;
1318 consumer_chan
= stream
->chan
;
1319 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1322 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1324 struct lttng_ust_lib_ring_buffer
*buf
;
1325 struct ustctl_consumer_channel
*consumer_chan
;
1330 consumer_chan
= stream
->chan
;
1331 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1334 /* For mmap mode, readable without "get" operation */
1336 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1338 struct lttng_ust_lib_ring_buffer
*buf
;
1339 struct ustctl_consumer_channel
*consumer_chan
;
1344 consumer_chan
= stream
->chan
;
1345 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1348 /* returns the length to mmap. */
1349 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1352 struct ustctl_consumer_channel
*consumer_chan
;
1353 unsigned long mmap_buf_len
;
1354 struct channel
*chan
;
1358 consumer_chan
= stream
->chan
;
1359 chan
= consumer_chan
->chan
->chan
;
1360 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1362 mmap_buf_len
= chan
->backend
.buf_size
;
1363 if (chan
->backend
.extra_reader_sb
)
1364 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1365 if (mmap_buf_len
> INT_MAX
)
1367 *len
= mmap_buf_len
;
1371 /* returns the maximum size for sub-buffers. */
1372 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1375 struct ustctl_consumer_channel
*consumer_chan
;
1376 struct channel
*chan
;
1380 consumer_chan
= stream
->chan
;
1381 chan
= consumer_chan
->chan
->chan
;
1382 *len
= chan
->backend
.subbuf_size
;
1387 * For mmap mode, operate on the current packet (between get/put or
1388 * get_next/put_next).
1391 /* returns the offset of the subbuffer belonging to the mmap reader. */
1392 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1395 struct channel
*chan
;
1396 unsigned long sb_bindex
;
1397 struct lttng_ust_lib_ring_buffer
*buf
;
1398 struct ustctl_consumer_channel
*consumer_chan
;
1399 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1400 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1405 consumer_chan
= stream
->chan
;
1406 chan
= consumer_chan
->chan
->chan
;
1407 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1409 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1410 buf
->backend
.buf_rsb
.id
);
1411 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1415 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1418 *off
= pages
->mmap_offset
;
1422 /* returns the size of the current sub-buffer, without padding (for mmap). */
1423 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1426 struct ustctl_consumer_channel
*consumer_chan
;
1427 struct channel
*chan
;
1428 struct lttng_ust_lib_ring_buffer
*buf
;
1434 consumer_chan
= stream
->chan
;
1435 chan
= consumer_chan
->chan
->chan
;
1436 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1437 consumer_chan
->chan
->handle
);
1441 /* returns the size of the current sub-buffer, without padding (for mmap). */
1442 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1445 struct ustctl_consumer_channel
*consumer_chan
;
1446 struct channel
*chan
;
1447 struct lttng_ust_lib_ring_buffer
*buf
;
1452 consumer_chan
= stream
->chan
;
1453 chan
= consumer_chan
->chan
->chan
;
1454 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1455 consumer_chan
->chan
->handle
);
1456 *len
= PAGE_ALIGN(*len
);
1460 /* Get exclusive read access to the next sub-buffer that can be read. */
1461 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1463 struct lttng_ust_lib_ring_buffer
*buf
;
1464 struct ustctl_consumer_channel
*consumer_chan
;
1469 consumer_chan
= stream
->chan
;
1470 return lib_ring_buffer_get_next_subbuf(buf
,
1471 consumer_chan
->chan
->handle
);
1475 /* Release exclusive sub-buffer access, move consumer forward. */
1476 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1478 struct lttng_ust_lib_ring_buffer
*buf
;
1479 struct ustctl_consumer_channel
*consumer_chan
;
1484 consumer_chan
= stream
->chan
;
1485 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1491 /* Get a snapshot of the current ring buffer producer and consumer positions */
1492 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1494 struct lttng_ust_lib_ring_buffer
*buf
;
1495 struct ustctl_consumer_channel
*consumer_chan
;
1500 consumer_chan
= stream
->chan
;
1501 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1502 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1506 * Get a snapshot of the current ring buffer producer and consumer positions
1507 * even if the consumed and produced positions are contained within the same
1510 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1512 struct lttng_ust_lib_ring_buffer
*buf
;
1513 struct ustctl_consumer_channel
*consumer_chan
;
1518 consumer_chan
= stream
->chan
;
1519 return lib_ring_buffer_snapshot_sample_positions(buf
,
1520 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1521 consumer_chan
->chan
->handle
);
1524 /* Get the consumer position (iteration start) */
1525 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1528 struct lttng_ust_lib_ring_buffer
*buf
;
1533 *pos
= buf
->cons_snapshot
;
1537 /* Get the producer position (iteration end) */
1538 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1541 struct lttng_ust_lib_ring_buffer
*buf
;
1546 *pos
= buf
->prod_snapshot
;
1550 /* Get exclusive read access to the specified sub-buffer position */
1551 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1554 struct lttng_ust_lib_ring_buffer
*buf
;
1555 struct ustctl_consumer_channel
*consumer_chan
;
1560 consumer_chan
= stream
->chan
;
1561 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1562 consumer_chan
->chan
->handle
);
1565 /* Release exclusive sub-buffer access */
1566 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1568 struct lttng_ust_lib_ring_buffer
*buf
;
1569 struct ustctl_consumer_channel
*consumer_chan
;
1574 consumer_chan
= stream
->chan
;
1575 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1579 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1580 int producer_active
)
1582 struct lttng_ust_lib_ring_buffer
*buf
;
1583 struct ustctl_consumer_channel
*consumer_chan
;
1587 consumer_chan
= stream
->chan
;
1588 lib_ring_buffer_switch_slow(buf
,
1589 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1590 consumer_chan
->chan
->handle
);
1594 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1595 struct lttng_ust_lib_ring_buffer
*buf
,
1596 struct lttng_ust_shm_handle
*handle
)
1598 struct channel
*chan
;
1599 const struct lttng_ust_lib_ring_buffer_config
*config
;
1600 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1602 chan
= shmp(handle
, buf
->backend
.chan
);
1605 config
= &chan
->backend
.config
;
1606 if (!config
->cb_ptr
)
1608 client_cb
= caa_container_of(config
->cb_ptr
,
1609 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1614 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1615 uint64_t *timestamp_begin
)
1617 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1618 struct lttng_ust_lib_ring_buffer
*buf
;
1619 struct lttng_ust_shm_handle
*handle
;
1621 if (!stream
|| !timestamp_begin
)
1624 handle
= stream
->chan
->chan
->handle
;
1625 client_cb
= get_client_cb(buf
, handle
);
1628 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1631 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1632 uint64_t *timestamp_end
)
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_end
)
1641 handle
= stream
->chan
->chan
->handle
;
1642 client_cb
= get_client_cb(buf
, handle
);
1645 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1648 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1649 uint64_t *events_discarded
)
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
|| !events_discarded
)
1658 handle
= stream
->chan
->chan
->handle
;
1659 client_cb
= get_client_cb(buf
, handle
);
1662 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1665 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1666 uint64_t *content_size
)
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
|| !content_size
)
1675 handle
= stream
->chan
->chan
->handle
;
1676 client_cb
= get_client_cb(buf
, handle
);
1679 return client_cb
->content_size(buf
, handle
, content_size
);
1682 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1683 uint64_t *packet_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
|| !packet_size
)
1692 handle
= stream
->chan
->chan
->handle
;
1693 client_cb
= get_client_cb(buf
, handle
);
1696 return client_cb
->packet_size(buf
, handle
, packet_size
);
1699 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1700 uint64_t *stream_id
)
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
|| !stream_id
)
1709 handle
= stream
->chan
->chan
->handle
;
1710 client_cb
= get_client_cb(buf
, handle
);
1713 return client_cb
->stream_id(buf
, handle
, stream_id
);
1716 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
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
;
1726 handle
= stream
->chan
->chan
->handle
;
1727 client_cb
= get_client_cb(buf
, handle
);
1728 if (!client_cb
|| !client_cb
->current_timestamp
)
1730 return client_cb
->current_timestamp(buf
, handle
, ts
);
1733 int ustctl_get_sequence_number(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
;
1740 if (!stream
|| !seq
)
1743 handle
= stream
->chan
->chan
->handle
;
1744 client_cb
= get_client_cb(buf
, handle
);
1745 if (!client_cb
|| !client_cb
->sequence_number
)
1747 return client_cb
->sequence_number(buf
, handle
, seq
);
1750 int ustctl_get_instance_id(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
;
1760 handle
= stream
->chan
->chan
->handle
;
1761 client_cb
= get_client_cb(buf
, handle
);
1764 return client_cb
->instance_id(buf
, handle
, id
);
1767 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1769 int ustctl_has_perf_counters(void)
1776 int ustctl_has_perf_counters(void)
1784 * Returns 0 on success, negative error value on error.
1786 int ustctl_recv_reg_msg(int sock
,
1787 enum ustctl_socket_type
*type
,
1794 uint32_t *bits_per_long
,
1795 uint32_t *uint8_t_alignment
,
1796 uint32_t *uint16_t_alignment
,
1797 uint32_t *uint32_t_alignment
,
1798 uint32_t *uint64_t_alignment
,
1799 uint32_t *long_alignment
,
1804 struct ustctl_reg_msg reg_msg
;
1806 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1807 if (len
> 0 && len
!= sizeof(reg_msg
))
1814 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1815 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1816 BIG_ENDIAN
: LITTLE_ENDIAN
;
1817 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1818 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1819 LITTLE_ENDIAN
: BIG_ENDIAN
;
1821 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1823 switch (reg_msg
.socket_type
) {
1824 case 0: *type
= USTCTL_SOCKET_CMD
;
1826 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1829 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1831 *major
= reg_msg
.major
;
1832 *minor
= reg_msg
.minor
;
1834 *ppid
= reg_msg
.ppid
;
1837 *bits_per_long
= reg_msg
.bits_per_long
;
1838 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1839 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1840 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1841 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1842 *long_alignment
= reg_msg
.long_alignment
;
1843 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1844 if (reg_msg
.major
!= LTTNG_UST_ABI_MAJOR_VERSION
) {
1845 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1851 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1853 struct ustcomm_notify_hdr header
;
1856 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1857 if (len
> 0 && len
!= sizeof(header
))
1863 switch (header
.notify_cmd
) {
1865 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1868 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1871 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1880 * Returns 0 on success, negative error value on error.
1882 int ustctl_recv_register_event(int sock
,
1889 struct ustctl_field
**fields
,
1890 char **model_emf_uri
)
1893 struct ustcomm_notify_event_msg msg
;
1894 size_t signature_len
, fields_len
, model_emf_uri_len
;
1895 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
1896 struct ustctl_field
*a_fields
= NULL
;
1898 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1899 if (len
> 0 && len
!= sizeof(msg
))
1906 *session_objd
= msg
.session_objd
;
1907 *channel_objd
= msg
.channel_objd
;
1908 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1909 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1910 *loglevel
= msg
.loglevel
;
1911 signature_len
= msg
.signature_len
;
1912 fields_len
= msg
.fields_len
;
1914 if (fields_len
% sizeof(*a_fields
) != 0) {
1918 model_emf_uri_len
= msg
.model_emf_uri_len
;
1920 /* recv signature. contains at least \0. */
1921 a_sign
= zmalloc(signature_len
);
1924 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
1925 if (len
> 0 && len
!= signature_len
) {
1927 goto signature_error
;
1931 goto signature_error
;
1934 goto signature_error
;
1936 /* Enforce end of string */
1937 a_sign
[signature_len
- 1] = '\0';
1941 a_fields
= zmalloc(fields_len
);
1944 goto signature_error
;
1946 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
1947 if (len
> 0 && len
!= fields_len
) {
1960 if (model_emf_uri_len
) {
1961 /* recv model_emf_uri_len */
1962 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
1963 if (!a_model_emf_uri
) {
1967 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
1969 if (len
> 0 && len
!= model_emf_uri_len
) {
1980 /* Enforce end of string */
1981 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
1984 *signature
= a_sign
;
1985 *nr_fields
= fields_len
/ sizeof(*a_fields
);
1987 *model_emf_uri
= a_model_emf_uri
;
1992 free(a_model_emf_uri
);
2001 * Returns 0 on success, negative error value on error.
2003 int ustctl_reply_register_event(int sock
,
2009 struct ustcomm_notify_hdr header
;
2010 struct ustcomm_notify_event_reply r
;
2013 memset(&reply
, 0, sizeof(reply
));
2014 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2015 reply
.r
.ret_code
= ret_code
;
2016 reply
.r
.event_id
= id
;
2017 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2018 if (len
> 0 && len
!= sizeof(reply
))
2026 * Returns 0 on success, negative UST or system error value on error.
2028 int ustctl_recv_register_enum(int sock
,
2031 struct ustctl_enum_entry
**entries
,
2035 struct ustcomm_notify_enum_msg msg
;
2037 struct ustctl_enum_entry
*a_entries
= NULL
;
2039 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2040 if (len
> 0 && len
!= sizeof(msg
))
2047 *session_objd
= msg
.session_objd
;
2048 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2049 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2050 entries_len
= msg
.entries_len
;
2052 if (entries_len
% sizeof(*a_entries
) != 0) {
2058 a_entries
= zmalloc(entries_len
);
2061 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2062 if (len
> 0 && len
!= entries_len
) {
2074 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2075 *entries
= a_entries
;
2085 * Returns 0 on success, negative error value on error.
2087 int ustctl_reply_register_enum(int sock
,
2093 struct ustcomm_notify_hdr header
;
2094 struct ustcomm_notify_enum_reply r
;
2097 memset(&reply
, 0, sizeof(reply
));
2098 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2099 reply
.r
.ret_code
= ret_code
;
2100 reply
.r
.enum_id
= id
;
2101 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2102 if (len
> 0 && len
!= sizeof(reply
))
2110 * Returns 0 on success, negative UST or system error value on error.
2112 int ustctl_recv_register_channel(int sock
,
2113 int *session_objd
, /* session descriptor (output) */
2114 int *channel_objd
, /* channel descriptor (output) */
2116 struct ustctl_field
**fields
)
2119 struct ustcomm_notify_channel_msg msg
;
2121 struct ustctl_field
*a_fields
;
2123 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2124 if (len
> 0 && len
!= sizeof(msg
))
2131 *session_objd
= msg
.session_objd
;
2132 *channel_objd
= msg
.channel_objd
;
2133 fields_len
= msg
.ctx_fields_len
;
2135 if (fields_len
% sizeof(*a_fields
) != 0) {
2141 a_fields
= zmalloc(fields_len
);
2146 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2147 if (len
> 0 && len
!= fields_len
) {
2162 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2172 * Returns 0 on success, negative error value on error.
2174 int ustctl_reply_register_channel(int sock
,
2176 enum ustctl_channel_header header_type
,
2181 struct ustcomm_notify_hdr header
;
2182 struct ustcomm_notify_channel_reply r
;
2185 memset(&reply
, 0, sizeof(reply
));
2186 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2187 reply
.r
.ret_code
= ret_code
;
2188 reply
.r
.chan_id
= chan_id
;
2189 switch (header_type
) {
2190 case USTCTL_CHANNEL_HEADER_COMPACT
:
2191 reply
.r
.header_type
= 1;
2193 case USTCTL_CHANNEL_HEADER_LARGE
:
2194 reply
.r
.header_type
= 2;
2197 reply
.r
.header_type
= 0;
2200 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2201 if (len
> 0 && len
!= sizeof(reply
))
2208 /* Regenerate the statedump. */
2209 int ustctl_regenerate_statedump(int sock
, int handle
)
2211 struct ustcomm_ust_msg lum
;
2212 struct ustcomm_ust_reply lur
;
2215 memset(&lum
, 0, sizeof(lum
));
2216 lum
.handle
= handle
;
2217 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2218 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2221 DBG("Regenerated statedump for handle %u", handle
);
2225 static __attribute__((constructor
))
2226 void ustctl_init(void)
2229 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2230 lttng_ust_clock_init();
2231 lttng_ring_buffer_metadata_client_init();
2232 lttng_ring_buffer_client_overwrite_init();
2233 lttng_ring_buffer_client_overwrite_rt_init();
2234 lttng_ring_buffer_client_discard_init();
2235 lttng_ring_buffer_client_discard_rt_init();
2236 lib_ringbuffer_signal_init();
2239 static __attribute__((destructor
))
2240 void ustctl_exit(void)
2242 lttng_ring_buffer_client_discard_rt_exit();
2243 lttng_ring_buffer_client_discard_exit();
2244 lttng_ring_buffer_client_overwrite_rt_exit();
2245 lttng_ring_buffer_client_overwrite_exit();
2246 lttng_ring_buffer_metadata_client_exit();