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.
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ust-endian.h>
29 #include <usterr-signal-safe.h>
33 #include "../libringbuffer/backend.h"
34 #include "../libringbuffer/frontend.h"
35 #include "../liblttng-ust/wait.h"
36 #include "../liblttng-ust/lttng-rb-clients.h"
37 #include "../liblttng-ust/clock.h"
38 #include "../liblttng-ust/getenv.h"
41 * Number of milliseconds to retry before failing metadata writes on
42 * buffer full condition. (10 seconds)
44 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
47 * Channel representation within consumer.
49 struct ustctl_consumer_channel
{
50 struct lttng_channel
*chan
; /* lttng channel buffers */
52 /* initial attributes */
53 struct ustctl_consumer_channel_attr attr
;
54 int wait_fd
; /* monitor close() */
55 int wakeup_fd
; /* monitor close() */
59 * Stream representation within consumer.
61 struct ustctl_consumer_stream
{
62 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
63 struct lttng_ust_lib_ring_buffer
*buf
;
64 struct ustctl_consumer_channel
*chan
;
65 int shm_fd
, wait_fd
, wakeup_fd
;
67 uint64_t memory_map_size
;
70 extern void lttng_ring_buffer_client_overwrite_init(void);
71 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
72 extern void lttng_ring_buffer_client_discard_init(void);
73 extern void lttng_ring_buffer_client_discard_rt_init(void);
74 extern void lttng_ring_buffer_metadata_client_init(void);
75 extern void lttng_ring_buffer_client_overwrite_exit(void);
76 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
77 extern void lttng_ring_buffer_client_discard_exit(void);
78 extern void lttng_ring_buffer_client_discard_rt_exit(void);
79 extern void lttng_ring_buffer_metadata_client_exit(void);
81 int ustctl_release_handle(int sock
, int handle
)
83 struct ustcomm_ust_msg lum
;
84 struct ustcomm_ust_reply lur
;
86 if (sock
< 0 || handle
< 0)
88 memset(&lum
, 0, sizeof(lum
));
90 lum
.cmd
= LTTNG_UST_RELEASE
;
91 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
95 * If sock is negative, it means we don't have to notify the other side
96 * (e.g. application has already vanished).
98 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
105 switch (data
->type
) {
106 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
107 if (data
->u
.channel
.wakeup_fd
>= 0) {
108 ret
= close(data
->u
.channel
.wakeup_fd
);
113 data
->u
.channel
.wakeup_fd
= -1;
115 free(data
->u
.channel
.data
);
116 data
->u
.channel
.data
= NULL
;
118 case LTTNG_UST_OBJECT_TYPE_STREAM
:
119 if (data
->u
.stream
.shm_fd
>= 0) {
120 ret
= close(data
->u
.stream
.shm_fd
);
125 data
->u
.stream
.shm_fd
= -1;
127 if (data
->u
.stream
.wakeup_fd
>= 0) {
128 ret
= close(data
->u
.stream
.wakeup_fd
);
133 data
->u
.stream
.wakeup_fd
= -1;
136 case LTTNG_UST_OBJECT_TYPE_EVENT
:
137 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
138 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
139 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
144 return ustctl_release_handle(sock
, data
->handle
);
148 * Send registration done packet to the application.
150 int ustctl_register_done(int sock
)
152 struct ustcomm_ust_msg lum
;
153 struct ustcomm_ust_reply lur
;
156 DBG("Sending register done command to %d", sock
);
157 memset(&lum
, 0, sizeof(lum
));
158 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
159 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
160 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
167 * returns session handle.
169 int ustctl_create_session(int sock
)
171 struct ustcomm_ust_msg lum
;
172 struct ustcomm_ust_reply lur
;
173 int ret
, session_handle
;
176 memset(&lum
, 0, sizeof(lum
));
177 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
178 lum
.cmd
= LTTNG_UST_SESSION
;
179 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
182 session_handle
= lur
.ret_val
;
183 DBG("received session handle %u", session_handle
);
184 return session_handle
;
187 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
188 struct lttng_ust_object_data
*channel_data
,
189 struct lttng_ust_object_data
**_event_data
)
191 struct ustcomm_ust_msg lum
;
192 struct ustcomm_ust_reply lur
;
193 struct lttng_ust_object_data
*event_data
;
196 if (!channel_data
|| !_event_data
)
199 event_data
= zmalloc(sizeof(*event_data
));
202 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
203 memset(&lum
, 0, sizeof(lum
));
204 lum
.handle
= channel_data
->handle
;
205 lum
.cmd
= LTTNG_UST_EVENT
;
206 strncpy(lum
.u
.event
.name
, ev
->name
,
207 LTTNG_UST_SYM_NAME_LEN
);
208 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
209 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
210 lum
.u
.event
.loglevel
= ev
->loglevel
;
211 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
216 event_data
->handle
= lur
.ret_val
;
217 DBG("received event handle %u", event_data
->handle
);
218 *_event_data
= event_data
;
222 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
223 struct lttng_ust_object_data
*obj_data
,
224 struct lttng_ust_object_data
**_context_data
)
226 struct ustcomm_ust_msg lum
;
227 struct ustcomm_ust_reply lur
;
228 struct lttng_ust_object_data
*context_data
= NULL
;
233 if (!obj_data
|| !_context_data
) {
238 context_data
= zmalloc(sizeof(*context_data
));
243 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
244 memset(&lum
, 0, sizeof(lum
));
245 lum
.handle
= obj_data
->handle
;
246 lum
.cmd
= LTTNG_UST_CONTEXT
;
248 lum
.u
.context
.ctx
= ctx
->ctx
;
250 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
251 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
253 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
255 size_t provider_name_len
= strlen(
256 ctx
->u
.app_ctx
.provider_name
) + 1;
257 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
259 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
260 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
262 len
= provider_name_len
+ ctx_name_len
;
268 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
270 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
277 ret
= ustcomm_send_app_msg(sock
, &lum
);
281 /* send var len ctx_name */
282 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
291 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
295 context_data
->handle
= -1;
296 DBG("Context created successfully");
297 *_context_data
= context_data
;
305 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
306 struct lttng_ust_object_data
*obj_data
)
308 struct ustcomm_ust_msg lum
;
309 struct ustcomm_ust_reply lur
;
315 memset(&lum
, 0, sizeof(lum
));
316 lum
.handle
= obj_data
->handle
;
317 lum
.cmd
= LTTNG_UST_FILTER
;
318 lum
.u
.filter
.data_size
= bytecode
->len
;
319 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
320 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
322 ret
= ustcomm_send_app_msg(sock
, &lum
);
325 /* send var len bytecode */
326 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
331 if (ret
!= bytecode
->len
)
333 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
336 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
337 struct lttng_ust_object_data
*obj_data
)
339 struct ustcomm_ust_msg lum
;
340 struct ustcomm_ust_reply lur
;
347 memset(&lum
, 0, sizeof(lum
));
348 lum
.handle
= obj_data
->handle
;
349 lum
.cmd
= LTTNG_UST_EXCLUSION
;
350 lum
.u
.exclusion
.count
= exclusion
->count
;
352 ret
= ustcomm_send_app_msg(sock
, &lum
);
357 /* send var len exclusion names */
358 ret
= ustcomm_send_unix_sock(sock
,
360 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
364 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
367 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
370 /* Enable event, channel and session ioctl */
371 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
373 struct ustcomm_ust_msg lum
;
374 struct ustcomm_ust_reply lur
;
380 memset(&lum
, 0, sizeof(lum
));
381 lum
.handle
= object
->handle
;
382 lum
.cmd
= LTTNG_UST_ENABLE
;
383 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
386 DBG("enabled handle %u", object
->handle
);
390 /* Disable event, channel and session ioctl */
391 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
393 struct ustcomm_ust_msg lum
;
394 struct ustcomm_ust_reply lur
;
400 memset(&lum
, 0, sizeof(lum
));
401 lum
.handle
= object
->handle
;
402 lum
.cmd
= LTTNG_UST_DISABLE
;
403 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
406 DBG("disable handle %u", object
->handle
);
410 int ustctl_start_session(int sock
, int handle
)
412 struct lttng_ust_object_data obj
;
415 return ustctl_enable(sock
, &obj
);
418 int ustctl_stop_session(int sock
, int handle
)
420 struct lttng_ust_object_data obj
;
423 return ustctl_disable(sock
, &obj
);
426 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
427 struct lttng_ust_object_data
**_event_notifier_group_data
)
429 struct lttng_ust_object_data
*event_notifier_group_data
;
430 struct ustcomm_ust_msg lum
;
431 struct ustcomm_ust_reply lur
;
435 if (!_event_notifier_group_data
)
438 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
439 if (!event_notifier_group_data
)
442 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
444 memset(&lum
, 0, sizeof(lum
));
445 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
446 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
448 ret
= ustcomm_send_app_msg(sock
, &lum
);
452 /* Send event_notifier notification pipe. */
453 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
459 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
463 event_notifier_group_data
->handle
= lur
.ret_val
;
464 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
466 *_event_notifier_group_data
= event_notifier_group_data
;
471 free(event_notifier_group_data
);
477 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
478 struct lttng_ust_object_data
*event_notifier_group
,
479 struct lttng_ust_object_data
**_event_notifier_data
)
481 struct ustcomm_ust_msg lum
;
482 struct ustcomm_ust_reply lur
;
483 struct lttng_ust_object_data
*event_notifier_data
;
486 if (!event_notifier_group
|| !_event_notifier_data
)
489 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
490 if (!event_notifier_data
)
493 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
495 memset(&lum
, 0, sizeof(lum
));
496 lum
.handle
= event_notifier_group
->handle
;
497 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
499 strncpy(lum
.u
.event_notifier
.event
.name
, event_notifier
->event
.name
,
500 LTTNG_UST_SYM_NAME_LEN
);
501 lum
.u
.event_notifier
.event
.instrumentation
= event_notifier
->event
.instrumentation
;
502 lum
.u
.event_notifier
.event
.loglevel_type
= event_notifier
->event
.loglevel_type
;
503 lum
.u
.event_notifier
.event
.loglevel
= event_notifier
->event
.loglevel
;
504 lum
.u
.event_notifier
.event
.token
= event_notifier
->event
.token
;
505 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
507 free(event_notifier_data
);
510 event_notifier_data
->handle
= lur
.ret_val
;
511 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
512 *_event_notifier_data
= event_notifier_data
;
517 int ustctl_tracepoint_list(int sock
)
519 struct ustcomm_ust_msg lum
;
520 struct ustcomm_ust_reply lur
;
521 int ret
, tp_list_handle
;
523 memset(&lum
, 0, sizeof(lum
));
524 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
525 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
526 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
529 tp_list_handle
= lur
.ret_val
;
530 DBG("received tracepoint list handle %u", tp_list_handle
);
531 return tp_list_handle
;
534 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
535 struct lttng_ust_tracepoint_iter
*iter
)
537 struct ustcomm_ust_msg lum
;
538 struct ustcomm_ust_reply lur
;
544 memset(&lum
, 0, sizeof(lum
));
545 lum
.handle
= tp_list_handle
;
546 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
547 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
550 DBG("received tracepoint list entry name %s loglevel %d",
551 lur
.u
.tracepoint
.name
,
552 lur
.u
.tracepoint
.loglevel
);
553 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
557 int ustctl_tracepoint_field_list(int sock
)
559 struct ustcomm_ust_msg lum
;
560 struct ustcomm_ust_reply lur
;
561 int ret
, tp_field_list_handle
;
563 memset(&lum
, 0, sizeof(lum
));
564 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
565 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
566 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
569 tp_field_list_handle
= lur
.ret_val
;
570 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
571 return tp_field_list_handle
;
574 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
575 struct lttng_ust_field_iter
*iter
)
577 struct ustcomm_ust_msg lum
;
578 struct ustcomm_ust_reply lur
;
585 memset(&lum
, 0, sizeof(lum
));
586 lum
.handle
= tp_field_list_handle
;
587 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
588 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
591 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
592 if (len
!= sizeof(*iter
)) {
595 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
603 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
605 struct ustcomm_ust_msg lum
;
606 struct ustcomm_ust_reply lur
;
612 memset(&lum
, 0, sizeof(lum
));
613 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
614 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
615 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
618 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
619 DBG("received tracer version");
623 int ustctl_wait_quiescent(int sock
)
625 struct ustcomm_ust_msg lum
;
626 struct ustcomm_ust_reply lur
;
629 memset(&lum
, 0, sizeof(lum
));
630 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
631 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
632 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
635 DBG("waited for quiescent state");
639 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
647 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
649 struct ustcomm_ust_msg lum
;
650 struct ustcomm_ust_reply lur
;
656 memset(&lum
, 0, sizeof(lum
));
657 lum
.handle
= object
->handle
;
658 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
659 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
662 DBG("flushed buffer handle %u", object
->handle
);
667 int ustctl_send_channel(int sock
,
668 enum lttng_ust_chan_type type
,
678 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
679 if (len
!= sizeof(size
)) {
686 /* Send channel type */
687 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
688 if (len
!= sizeof(type
)) {
696 /* Send channel data */
697 len
= ustcomm_send_unix_sock(sock
, data
, size
);
706 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
717 int ustctl_send_stream(int sock
,
719 uint64_t memory_map_size
,
720 int shm_fd
, int wakeup_fd
,
728 /* finish iteration */
731 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
732 if (len
!= sizeof(v
)) {
742 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
743 sizeof(memory_map_size
));
744 if (len
!= sizeof(memory_map_size
)) {
752 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
754 if (len
!= sizeof(stream_nr
)) {
762 /* Send shm fd and wakeup fd */
765 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
775 int ustctl_recv_channel_from_consumer(int sock
,
776 struct lttng_ust_object_data
**_channel_data
)
778 struct lttng_ust_object_data
*channel_data
;
783 channel_data
= zmalloc(sizeof(*channel_data
));
788 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
789 channel_data
->handle
= -1;
792 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
793 sizeof(channel_data
->size
));
794 if (len
!= sizeof(channel_data
->size
)) {
802 /* recv channel type */
803 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
804 sizeof(channel_data
->u
.channel
.type
));
805 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
813 /* recv channel data */
814 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
815 if (!channel_data
->u
.channel
.data
) {
819 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
821 if (len
!= channel_data
->size
) {
826 goto error_recv_data
;
829 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
833 goto error_recv_data
;
836 goto error_recv_data
;
839 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
840 *_channel_data
= channel_data
;
844 free(channel_data
->u
.channel
.data
);
851 int ustctl_recv_stream_from_consumer(int sock
,
852 struct lttng_ust_object_data
**_stream_data
)
854 struct lttng_ust_object_data
*stream_data
;
859 stream_data
= zmalloc(sizeof(*stream_data
));
865 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
866 stream_data
->handle
= -1;
869 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
870 sizeof(stream_data
->size
));
871 if (len
!= sizeof(stream_data
->size
)) {
878 if (stream_data
->size
== -1) {
879 ret
= -LTTNG_UST_ERR_NOENT
;
884 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
885 sizeof(stream_data
->u
.stream
.stream_nr
));
886 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
894 /* recv shm fd and wakeup fd */
895 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
905 stream_data
->u
.stream
.shm_fd
= fds
[0];
906 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
907 *_stream_data
= stream_data
;
916 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
917 struct lttng_ust_object_data
*channel_data
)
919 struct ustcomm_ust_msg lum
;
920 struct ustcomm_ust_reply lur
;
926 memset(&lum
, 0, sizeof(lum
));
927 lum
.handle
= session_handle
;
928 lum
.cmd
= LTTNG_UST_CHANNEL
;
929 lum
.u
.channel
.len
= channel_data
->size
;
930 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
931 ret
= ustcomm_send_app_msg(sock
, &lum
);
935 ret
= ustctl_send_channel(sock
,
936 channel_data
->u
.channel
.type
,
937 channel_data
->u
.channel
.data
,
939 channel_data
->u
.channel
.wakeup_fd
,
943 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
945 channel_data
->handle
= lur
.ret_val
;
950 int ustctl_send_stream_to_ust(int sock
,
951 struct lttng_ust_object_data
*channel_data
,
952 struct lttng_ust_object_data
*stream_data
)
954 struct ustcomm_ust_msg lum
;
955 struct ustcomm_ust_reply lur
;
958 memset(&lum
, 0, sizeof(lum
));
959 lum
.handle
= channel_data
->handle
;
960 lum
.cmd
= LTTNG_UST_STREAM
;
961 lum
.u
.stream
.len
= stream_data
->size
;
962 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
963 ret
= ustcomm_send_app_msg(sock
, &lum
);
968 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
970 ret
= ustctl_send_stream(sock
,
971 stream_data
->u
.stream
.stream_nr
,
973 stream_data
->u
.stream
.shm_fd
,
974 stream_data
->u
.stream
.wakeup_fd
, 1);
977 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
980 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
981 struct lttng_ust_object_data
*src
)
983 struct lttng_ust_object_data
*obj
;
986 if (src
->handle
!= -1) {
991 obj
= zmalloc(sizeof(*obj
));
997 obj
->type
= src
->type
;
998 obj
->handle
= src
->handle
;
999 obj
->size
= src
->size
;
1001 switch (obj
->type
) {
1002 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1004 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1005 if (src
->u
.channel
.wakeup_fd
>= 0) {
1006 obj
->u
.channel
.wakeup_fd
=
1007 dup(src
->u
.channel
.wakeup_fd
);
1008 if (obj
->u
.channel
.wakeup_fd
< 0) {
1010 goto chan_error_wakeup_fd
;
1013 obj
->u
.channel
.wakeup_fd
=
1014 src
->u
.channel
.wakeup_fd
;
1016 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1017 if (!obj
->u
.channel
.data
) {
1019 goto chan_error_alloc
;
1021 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1025 if (src
->u
.channel
.wakeup_fd
>= 0) {
1028 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1033 chan_error_wakeup_fd
:
1038 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1040 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1041 if (src
->u
.stream
.wakeup_fd
>= 0) {
1042 obj
->u
.stream
.wakeup_fd
=
1043 dup(src
->u
.stream
.wakeup_fd
);
1044 if (obj
->u
.stream
.wakeup_fd
< 0) {
1046 goto stream_error_wakeup_fd
;
1049 obj
->u
.stream
.wakeup_fd
=
1050 src
->u
.stream
.wakeup_fd
;
1053 if (src
->u
.stream
.shm_fd
>= 0) {
1054 obj
->u
.stream
.shm_fd
=
1055 dup(src
->u
.stream
.shm_fd
);
1056 if (obj
->u
.stream
.shm_fd
< 0) {
1058 goto stream_error_shm_fd
;
1061 obj
->u
.stream
.shm_fd
=
1062 src
->u
.stream
.shm_fd
;
1066 stream_error_shm_fd
:
1067 if (src
->u
.stream
.wakeup_fd
>= 0) {
1070 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1075 stream_error_wakeup_fd
:
1094 /* Buffer operations */
1096 int ustctl_get_nr_stream_per_channel(void)
1098 return num_possible_cpus();
1101 struct ustctl_consumer_channel
*
1102 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1103 const int *stream_fds
, int nr_stream_fds
)
1105 struct ustctl_consumer_channel
*chan
;
1106 const char *transport_name
;
1107 struct lttng_transport
*transport
;
1109 switch (attr
->type
) {
1110 case LTTNG_UST_CHAN_PER_CPU
:
1111 if (attr
->output
== LTTNG_UST_MMAP
) {
1112 if (attr
->overwrite
) {
1113 if (attr
->read_timer_interval
== 0) {
1114 transport_name
= "relay-overwrite-mmap";
1116 transport_name
= "relay-overwrite-rt-mmap";
1119 if (attr
->read_timer_interval
== 0) {
1120 transport_name
= "relay-discard-mmap";
1122 transport_name
= "relay-discard-rt-mmap";
1129 case LTTNG_UST_CHAN_METADATA
:
1130 if (attr
->output
== LTTNG_UST_MMAP
)
1131 transport_name
= "relay-metadata-mmap";
1136 transport_name
= "<unknown>";
1140 transport
= lttng_transport_find(transport_name
);
1142 DBG("LTTng transport %s not found\n",
1147 chan
= zmalloc(sizeof(*chan
));
1151 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1152 attr
->subbuf_size
, attr
->num_subbuf
,
1153 attr
->switch_timer_interval
,
1154 attr
->read_timer_interval
,
1155 attr
->uuid
, attr
->chan_id
,
1156 stream_fds
, nr_stream_fds
,
1157 attr
->blocking_timeout
);
1161 chan
->chan
->ops
= &transport
->ops
;
1162 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1163 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1164 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1172 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1174 (void) ustctl_channel_close_wait_fd(chan
);
1175 (void) ustctl_channel_close_wakeup_fd(chan
);
1176 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1180 int ustctl_send_channel_to_sessiond(int sock
,
1181 struct ustctl_consumer_channel
*channel
)
1183 struct shm_object_table
*table
;
1185 table
= channel
->chan
->handle
->table
;
1186 if (table
->size
<= 0)
1188 return ustctl_send_channel(sock
,
1190 table
->objects
[0].memory_map
,
1191 table
->objects
[0].memory_map_size
,
1196 int ustctl_send_stream_to_sessiond(int sock
,
1197 struct ustctl_consumer_stream
*stream
)
1200 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1202 return ustctl_send_stream(sock
,
1204 stream
->memory_map_size
,
1205 stream
->shm_fd
, stream
->wakeup_fd
,
1209 int ustctl_write_metadata_to_channel(
1210 struct ustctl_consumer_channel
*channel
,
1211 const char *metadata_str
, /* NOT null-terminated */
1212 size_t len
) /* metadata length */
1214 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1215 struct lttng_channel
*chan
= channel
->chan
;
1216 const char *str
= metadata_str
;
1217 int ret
= 0, waitret
;
1218 size_t reserve_len
, pos
;
1220 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1221 reserve_len
= min_t(size_t,
1222 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1224 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1225 sizeof(char), -1, chan
->handle
, NULL
);
1227 * We don't care about metadata buffer's records lost
1228 * count, because we always retry here. Report error if
1229 * we need to bail out after timeout or being
1232 waitret
= wait_cond_interruptible_timeout(
1234 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1235 ret
!= -ENOBUFS
|| !ret
;
1237 LTTNG_METADATA_TIMEOUT_MSEC
);
1238 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1239 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1240 waitret
== -EINTR
? "interrupted" :
1241 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1242 if (waitret
== -EINTR
)
1246 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1247 chan
->ops
->event_commit(&ctx
);
1254 * Write at most one packet in the channel.
1255 * Returns the number of bytes written on success, < 0 on error.
1257 ssize_t
ustctl_write_one_packet_to_channel(
1258 struct ustctl_consumer_channel
*channel
,
1259 const char *metadata_str
, /* NOT null-terminated */
1260 size_t len
) /* metadata length */
1262 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1263 struct lttng_channel
*chan
= channel
->chan
;
1264 const char *str
= metadata_str
;
1265 ssize_t reserve_len
;
1268 reserve_len
= min_t(ssize_t
,
1269 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1271 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1272 sizeof(char), -1, chan
->handle
, NULL
);
1273 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1275 DBG("LTTng: event reservation failed");
1280 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1281 chan
->ops
->event_commit(&ctx
);
1287 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1289 struct channel
*chan
;
1292 chan
= consumer_chan
->chan
->chan
;
1293 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1294 chan
, chan
->handle
);
1296 consumer_chan
->wait_fd
= -1;
1300 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1302 struct channel
*chan
;
1305 chan
= consumer_chan
->chan
->chan
;
1306 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1307 chan
, chan
->handle
);
1309 consumer_chan
->wakeup_fd
= -1;
1313 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1315 struct channel
*chan
;
1317 chan
= stream
->chan
->chan
->chan
;
1318 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1319 chan
, stream
->handle
, stream
->cpu
);
1322 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1324 struct channel
*chan
;
1326 chan
= stream
->chan
->chan
->chan
;
1327 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1328 chan
, stream
->handle
, stream
->cpu
);
1331 struct ustctl_consumer_stream
*
1332 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1335 struct ustctl_consumer_stream
*stream
;
1336 struct lttng_ust_shm_handle
*handle
;
1337 struct channel
*chan
;
1338 int shm_fd
, wait_fd
, wakeup_fd
;
1339 uint64_t memory_map_size
;
1340 struct lttng_ust_lib_ring_buffer
*buf
;
1345 handle
= channel
->chan
->handle
;
1349 chan
= channel
->chan
->chan
;
1350 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1351 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1352 &wakeup_fd
, &memory_map_size
);
1355 ret
= lib_ring_buffer_open_read(buf
, handle
);
1359 stream
= zmalloc(sizeof(*stream
));
1362 stream
->handle
= handle
;
1364 stream
->chan
= channel
;
1365 stream
->shm_fd
= shm_fd
;
1366 stream
->wait_fd
= wait_fd
;
1367 stream
->wakeup_fd
= wakeup_fd
;
1368 stream
->memory_map_size
= memory_map_size
;
1376 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1378 struct lttng_ust_lib_ring_buffer
*buf
;
1379 struct ustctl_consumer_channel
*consumer_chan
;
1383 consumer_chan
= stream
->chan
;
1384 (void) ustctl_stream_close_wait_fd(stream
);
1385 (void) ustctl_stream_close_wakeup_fd(stream
);
1386 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1390 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1394 return shm_get_wait_fd(chan
->chan
->handle
,
1395 &chan
->chan
->handle
->chan
._ref
);
1398 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1402 return shm_get_wakeup_fd(chan
->chan
->handle
,
1403 &chan
->chan
->handle
->chan
._ref
);
1406 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1408 struct lttng_ust_lib_ring_buffer
*buf
;
1409 struct ustctl_consumer_channel
*consumer_chan
;
1414 consumer_chan
= stream
->chan
;
1415 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1418 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1420 struct lttng_ust_lib_ring_buffer
*buf
;
1421 struct ustctl_consumer_channel
*consumer_chan
;
1426 consumer_chan
= stream
->chan
;
1427 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1430 /* For mmap mode, readable without "get" operation */
1432 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1434 struct lttng_ust_lib_ring_buffer
*buf
;
1435 struct ustctl_consumer_channel
*consumer_chan
;
1440 consumer_chan
= stream
->chan
;
1441 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1444 /* returns the length to mmap. */
1445 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1448 struct ustctl_consumer_channel
*consumer_chan
;
1449 unsigned long mmap_buf_len
;
1450 struct channel
*chan
;
1454 consumer_chan
= stream
->chan
;
1455 chan
= consumer_chan
->chan
->chan
;
1456 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1458 mmap_buf_len
= chan
->backend
.buf_size
;
1459 if (chan
->backend
.extra_reader_sb
)
1460 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1461 if (mmap_buf_len
> INT_MAX
)
1463 *len
= mmap_buf_len
;
1467 /* returns the maximum size for sub-buffers. */
1468 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1471 struct ustctl_consumer_channel
*consumer_chan
;
1472 struct channel
*chan
;
1476 consumer_chan
= stream
->chan
;
1477 chan
= consumer_chan
->chan
->chan
;
1478 *len
= chan
->backend
.subbuf_size
;
1483 * For mmap mode, operate on the current packet (between get/put or
1484 * get_next/put_next).
1487 /* returns the offset of the subbuffer belonging to the mmap reader. */
1488 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1491 struct channel
*chan
;
1492 unsigned long sb_bindex
;
1493 struct lttng_ust_lib_ring_buffer
*buf
;
1494 struct ustctl_consumer_channel
*consumer_chan
;
1495 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1496 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1501 consumer_chan
= stream
->chan
;
1502 chan
= consumer_chan
->chan
->chan
;
1503 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1505 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1506 buf
->backend
.buf_rsb
.id
);
1507 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1511 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1514 *off
= pages
->mmap_offset
;
1518 /* returns the size of the current sub-buffer, without padding (for mmap). */
1519 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1522 struct ustctl_consumer_channel
*consumer_chan
;
1523 struct channel
*chan
;
1524 struct lttng_ust_lib_ring_buffer
*buf
;
1530 consumer_chan
= stream
->chan
;
1531 chan
= consumer_chan
->chan
->chan
;
1532 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1533 consumer_chan
->chan
->handle
);
1537 /* returns the size of the current sub-buffer, without padding (for mmap). */
1538 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1541 struct ustctl_consumer_channel
*consumer_chan
;
1542 struct channel
*chan
;
1543 struct lttng_ust_lib_ring_buffer
*buf
;
1548 consumer_chan
= stream
->chan
;
1549 chan
= consumer_chan
->chan
->chan
;
1550 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1551 consumer_chan
->chan
->handle
);
1552 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1556 /* Get exclusive read access to the next sub-buffer that can be read. */
1557 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1559 struct lttng_ust_lib_ring_buffer
*buf
;
1560 struct ustctl_consumer_channel
*consumer_chan
;
1565 consumer_chan
= stream
->chan
;
1566 return lib_ring_buffer_get_next_subbuf(buf
,
1567 consumer_chan
->chan
->handle
);
1571 /* Release exclusive sub-buffer access, move consumer forward. */
1572 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1574 struct lttng_ust_lib_ring_buffer
*buf
;
1575 struct ustctl_consumer_channel
*consumer_chan
;
1580 consumer_chan
= stream
->chan
;
1581 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1587 /* Get a snapshot of the current ring buffer producer and consumer positions */
1588 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1590 struct lttng_ust_lib_ring_buffer
*buf
;
1591 struct ustctl_consumer_channel
*consumer_chan
;
1596 consumer_chan
= stream
->chan
;
1597 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1598 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1602 * Get a snapshot of the current ring buffer producer and consumer positions
1603 * even if the consumed and produced positions are contained within the same
1606 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1608 struct lttng_ust_lib_ring_buffer
*buf
;
1609 struct ustctl_consumer_channel
*consumer_chan
;
1614 consumer_chan
= stream
->chan
;
1615 return lib_ring_buffer_snapshot_sample_positions(buf
,
1616 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1617 consumer_chan
->chan
->handle
);
1620 /* Get the consumer position (iteration start) */
1621 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1624 struct lttng_ust_lib_ring_buffer
*buf
;
1629 *pos
= buf
->cons_snapshot
;
1633 /* Get the producer position (iteration end) */
1634 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1637 struct lttng_ust_lib_ring_buffer
*buf
;
1642 *pos
= buf
->prod_snapshot
;
1646 /* Get exclusive read access to the specified sub-buffer position */
1647 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1650 struct lttng_ust_lib_ring_buffer
*buf
;
1651 struct ustctl_consumer_channel
*consumer_chan
;
1656 consumer_chan
= stream
->chan
;
1657 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1658 consumer_chan
->chan
->handle
);
1661 /* Release exclusive sub-buffer access */
1662 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1664 struct lttng_ust_lib_ring_buffer
*buf
;
1665 struct ustctl_consumer_channel
*consumer_chan
;
1670 consumer_chan
= stream
->chan
;
1671 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1675 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1676 int producer_active
)
1678 struct lttng_ust_lib_ring_buffer
*buf
;
1679 struct ustctl_consumer_channel
*consumer_chan
;
1683 consumer_chan
= stream
->chan
;
1684 lib_ring_buffer_switch_slow(buf
,
1685 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1686 consumer_chan
->chan
->handle
);
1689 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1691 struct lttng_ust_lib_ring_buffer
*buf
;
1692 struct ustctl_consumer_channel
*consumer_chan
;
1696 consumer_chan
= stream
->chan
;
1697 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1698 consumer_chan
->chan
->handle
);
1699 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1703 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1704 struct lttng_ust_lib_ring_buffer
*buf
,
1705 struct lttng_ust_shm_handle
*handle
)
1707 struct channel
*chan
;
1708 const struct lttng_ust_lib_ring_buffer_config
*config
;
1709 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1711 chan
= shmp(handle
, buf
->backend
.chan
);
1714 config
= &chan
->backend
.config
;
1715 if (!config
->cb_ptr
)
1717 client_cb
= caa_container_of(config
->cb_ptr
,
1718 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1723 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1724 uint64_t *timestamp_begin
)
1726 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1727 struct lttng_ust_lib_ring_buffer
*buf
;
1728 struct lttng_ust_shm_handle
*handle
;
1730 if (!stream
|| !timestamp_begin
)
1733 handle
= stream
->chan
->chan
->handle
;
1734 client_cb
= get_client_cb(buf
, handle
);
1737 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1740 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1741 uint64_t *timestamp_end
)
1743 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1744 struct lttng_ust_lib_ring_buffer
*buf
;
1745 struct lttng_ust_shm_handle
*handle
;
1747 if (!stream
|| !timestamp_end
)
1750 handle
= stream
->chan
->chan
->handle
;
1751 client_cb
= get_client_cb(buf
, handle
);
1754 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1757 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1758 uint64_t *events_discarded
)
1760 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1761 struct lttng_ust_lib_ring_buffer
*buf
;
1762 struct lttng_ust_shm_handle
*handle
;
1764 if (!stream
|| !events_discarded
)
1767 handle
= stream
->chan
->chan
->handle
;
1768 client_cb
= get_client_cb(buf
, handle
);
1771 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1774 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1775 uint64_t *content_size
)
1777 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1778 struct lttng_ust_lib_ring_buffer
*buf
;
1779 struct lttng_ust_shm_handle
*handle
;
1781 if (!stream
|| !content_size
)
1784 handle
= stream
->chan
->chan
->handle
;
1785 client_cb
= get_client_cb(buf
, handle
);
1788 return client_cb
->content_size(buf
, handle
, content_size
);
1791 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1792 uint64_t *packet_size
)
1794 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1795 struct lttng_ust_lib_ring_buffer
*buf
;
1796 struct lttng_ust_shm_handle
*handle
;
1798 if (!stream
|| !packet_size
)
1801 handle
= stream
->chan
->chan
->handle
;
1802 client_cb
= get_client_cb(buf
, handle
);
1805 return client_cb
->packet_size(buf
, handle
, packet_size
);
1808 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1809 uint64_t *stream_id
)
1811 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1812 struct lttng_ust_lib_ring_buffer
*buf
;
1813 struct lttng_ust_shm_handle
*handle
;
1815 if (!stream
|| !stream_id
)
1818 handle
= stream
->chan
->chan
->handle
;
1819 client_cb
= get_client_cb(buf
, handle
);
1822 return client_cb
->stream_id(buf
, handle
, stream_id
);
1825 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1828 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1829 struct lttng_ust_lib_ring_buffer
*buf
;
1830 struct lttng_ust_shm_handle
*handle
;
1835 handle
= stream
->chan
->chan
->handle
;
1836 client_cb
= get_client_cb(buf
, handle
);
1837 if (!client_cb
|| !client_cb
->current_timestamp
)
1839 return client_cb
->current_timestamp(buf
, handle
, ts
);
1842 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1845 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1846 struct lttng_ust_lib_ring_buffer
*buf
;
1847 struct lttng_ust_shm_handle
*handle
;
1849 if (!stream
|| !seq
)
1852 handle
= stream
->chan
->chan
->handle
;
1853 client_cb
= get_client_cb(buf
, handle
);
1854 if (!client_cb
|| !client_cb
->sequence_number
)
1856 return client_cb
->sequence_number(buf
, handle
, seq
);
1859 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1862 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1863 struct lttng_ust_lib_ring_buffer
*buf
;
1864 struct lttng_ust_shm_handle
*handle
;
1869 handle
= stream
->chan
->chan
->handle
;
1870 client_cb
= get_client_cb(buf
, handle
);
1873 return client_cb
->instance_id(buf
, handle
, id
);
1876 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1878 int ustctl_has_perf_counters(void)
1885 int ustctl_has_perf_counters(void)
1893 * Returns 0 on success, negative error value on error.
1895 int ustctl_recv_reg_msg(int sock
,
1896 enum ustctl_socket_type
*type
,
1903 uint32_t *bits_per_long
,
1904 uint32_t *uint8_t_alignment
,
1905 uint32_t *uint16_t_alignment
,
1906 uint32_t *uint32_t_alignment
,
1907 uint32_t *uint64_t_alignment
,
1908 uint32_t *long_alignment
,
1913 struct ustctl_reg_msg reg_msg
;
1915 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1916 if (len
> 0 && len
!= sizeof(reg_msg
))
1923 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1924 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1925 BIG_ENDIAN
: LITTLE_ENDIAN
;
1926 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1927 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1928 LITTLE_ENDIAN
: BIG_ENDIAN
;
1930 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1932 switch (reg_msg
.socket_type
) {
1933 case 0: *type
= USTCTL_SOCKET_CMD
;
1935 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1938 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1940 *major
= reg_msg
.major
;
1941 *minor
= reg_msg
.minor
;
1943 *ppid
= reg_msg
.ppid
;
1946 *bits_per_long
= reg_msg
.bits_per_long
;
1947 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1948 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1949 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1950 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1951 *long_alignment
= reg_msg
.long_alignment
;
1952 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1953 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
1954 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
1955 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1961 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1963 struct ustcomm_notify_hdr header
;
1966 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1967 if (len
> 0 && len
!= sizeof(header
))
1973 switch (header
.notify_cmd
) {
1975 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1978 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1981 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1990 * Returns 0 on success, negative error value on error.
1992 int ustctl_recv_register_event(int sock
,
1999 struct ustctl_field
**fields
,
2000 char **model_emf_uri
)
2003 struct ustcomm_notify_event_msg msg
;
2004 size_t signature_len
, fields_len
, model_emf_uri_len
;
2005 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2006 struct ustctl_field
*a_fields
= NULL
;
2008 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2009 if (len
> 0 && len
!= sizeof(msg
))
2016 *session_objd
= msg
.session_objd
;
2017 *channel_objd
= msg
.channel_objd
;
2018 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2019 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2020 *loglevel
= msg
.loglevel
;
2021 signature_len
= msg
.signature_len
;
2022 fields_len
= msg
.fields_len
;
2024 if (fields_len
% sizeof(*a_fields
) != 0) {
2028 model_emf_uri_len
= msg
.model_emf_uri_len
;
2030 /* recv signature. contains at least \0. */
2031 a_sign
= zmalloc(signature_len
);
2034 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2035 if (len
> 0 && len
!= signature_len
) {
2037 goto signature_error
;
2041 goto signature_error
;
2044 goto signature_error
;
2046 /* Enforce end of string */
2047 a_sign
[signature_len
- 1] = '\0';
2051 a_fields
= zmalloc(fields_len
);
2054 goto signature_error
;
2056 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2057 if (len
> 0 && len
!= fields_len
) {
2070 if (model_emf_uri_len
) {
2071 /* recv model_emf_uri_len */
2072 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2073 if (!a_model_emf_uri
) {
2077 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2079 if (len
> 0 && len
!= model_emf_uri_len
) {
2090 /* Enforce end of string */
2091 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2094 *signature
= a_sign
;
2095 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2097 *model_emf_uri
= a_model_emf_uri
;
2102 free(a_model_emf_uri
);
2111 * Returns 0 on success, negative error value on error.
2113 int ustctl_reply_register_event(int sock
,
2119 struct ustcomm_notify_hdr header
;
2120 struct ustcomm_notify_event_reply r
;
2123 memset(&reply
, 0, sizeof(reply
));
2124 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2125 reply
.r
.ret_code
= ret_code
;
2126 reply
.r
.event_id
= id
;
2127 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2128 if (len
> 0 && len
!= sizeof(reply
))
2136 * Returns 0 on success, negative UST or system error value on error.
2138 int ustctl_recv_register_enum(int sock
,
2141 struct ustctl_enum_entry
**entries
,
2145 struct ustcomm_notify_enum_msg msg
;
2147 struct ustctl_enum_entry
*a_entries
= NULL
;
2149 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2150 if (len
> 0 && len
!= sizeof(msg
))
2157 *session_objd
= msg
.session_objd
;
2158 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2159 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2160 entries_len
= msg
.entries_len
;
2162 if (entries_len
% sizeof(*a_entries
) != 0) {
2168 a_entries
= zmalloc(entries_len
);
2171 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2172 if (len
> 0 && len
!= entries_len
) {
2184 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2185 *entries
= a_entries
;
2195 * Returns 0 on success, negative error value on error.
2197 int ustctl_reply_register_enum(int sock
,
2203 struct ustcomm_notify_hdr header
;
2204 struct ustcomm_notify_enum_reply r
;
2207 memset(&reply
, 0, sizeof(reply
));
2208 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2209 reply
.r
.ret_code
= ret_code
;
2210 reply
.r
.enum_id
= id
;
2211 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2212 if (len
> 0 && len
!= sizeof(reply
))
2220 * Returns 0 on success, negative UST or system error value on error.
2222 int ustctl_recv_register_channel(int sock
,
2223 int *session_objd
, /* session descriptor (output) */
2224 int *channel_objd
, /* channel descriptor (output) */
2226 struct ustctl_field
**fields
)
2229 struct ustcomm_notify_channel_msg msg
;
2231 struct ustctl_field
*a_fields
;
2233 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2234 if (len
> 0 && len
!= sizeof(msg
))
2241 *session_objd
= msg
.session_objd
;
2242 *channel_objd
= msg
.channel_objd
;
2243 fields_len
= msg
.ctx_fields_len
;
2245 if (fields_len
% sizeof(*a_fields
) != 0) {
2251 a_fields
= zmalloc(fields_len
);
2256 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2257 if (len
> 0 && len
!= fields_len
) {
2272 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2282 * Returns 0 on success, negative error value on error.
2284 int ustctl_reply_register_channel(int sock
,
2286 enum ustctl_channel_header header_type
,
2291 struct ustcomm_notify_hdr header
;
2292 struct ustcomm_notify_channel_reply r
;
2295 memset(&reply
, 0, sizeof(reply
));
2296 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2297 reply
.r
.ret_code
= ret_code
;
2298 reply
.r
.chan_id
= chan_id
;
2299 switch (header_type
) {
2300 case USTCTL_CHANNEL_HEADER_COMPACT
:
2301 reply
.r
.header_type
= 1;
2303 case USTCTL_CHANNEL_HEADER_LARGE
:
2304 reply
.r
.header_type
= 2;
2307 reply
.r
.header_type
= 0;
2310 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2311 if (len
> 0 && len
!= sizeof(reply
))
2318 /* Regenerate the statedump. */
2319 int ustctl_regenerate_statedump(int sock
, int handle
)
2321 struct ustcomm_ust_msg lum
;
2322 struct ustcomm_ust_reply lur
;
2325 memset(&lum
, 0, sizeof(lum
));
2326 lum
.handle
= handle
;
2327 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2328 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2331 DBG("Regenerated statedump for handle %u", handle
);
2335 static __attribute__((constructor
))
2336 void ustctl_init(void)
2339 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2340 lttng_ust_clock_init();
2341 lttng_ring_buffer_metadata_client_init();
2342 lttng_ring_buffer_client_overwrite_init();
2343 lttng_ring_buffer_client_overwrite_rt_init();
2344 lttng_ring_buffer_client_discard_init();
2345 lttng_ring_buffer_client_discard_rt_init();
2346 lib_ringbuffer_signal_init();
2349 static __attribute__((destructor
))
2350 void ustctl_exit(void)
2352 lttng_ring_buffer_client_discard_rt_exit();
2353 lttng_ring_buffer_client_discard_exit();
2354 lttng_ring_buffer_client_overwrite_rt_exit();
2355 lttng_ring_buffer_client_overwrite_exit();
2356 lttng_ring_buffer_metadata_client_exit();