2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <lttng/ust-config.h>
27 #include <lttng/ust-ctl.h>
28 #include <lttng/ust-abi.h>
29 #include <lttng/ust-events.h>
30 #include <usterr-signal-safe.h>
34 #include "../libringbuffer/backend.h"
35 #include "../libringbuffer/frontend.h"
36 #include "../liblttng-ust/wait.h"
37 #include "../liblttng-ust/lttng-rb-clients.h"
38 #include "../liblttng-ust/clock.h"
39 #include "../liblttng-ust/getenv.h"
42 * Number of milliseconds to retry before failing metadata writes on
43 * buffer full condition. (10 seconds)
45 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
48 * Channel representation within consumer.
50 struct ustctl_consumer_channel
{
51 struct lttng_channel
*chan
; /* lttng channel buffers */
53 /* initial attributes */
54 struct ustctl_consumer_channel_attr attr
;
55 int wait_fd
; /* monitor close() */
56 int wakeup_fd
; /* monitor close() */
60 * Stream representation within consumer.
62 struct ustctl_consumer_stream
{
63 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
64 struct lttng_ust_lib_ring_buffer
*buf
;
65 struct ustctl_consumer_channel
*chan
;
66 int shm_fd
, wait_fd
, wakeup_fd
;
68 uint64_t memory_map_size
;
71 extern void lttng_ring_buffer_client_overwrite_init(void);
72 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
73 extern void lttng_ring_buffer_client_discard_init(void);
74 extern void lttng_ring_buffer_client_discard_rt_init(void);
75 extern void lttng_ring_buffer_metadata_client_init(void);
76 extern void lttng_ring_buffer_client_overwrite_exit(void);
77 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
78 extern void lttng_ring_buffer_client_discard_exit(void);
79 extern void lttng_ring_buffer_client_discard_rt_exit(void);
80 extern void lttng_ring_buffer_metadata_client_exit(void);
82 int ustctl_release_handle(int sock
, int handle
)
84 struct ustcomm_ust_msg lum
;
85 struct ustcomm_ust_reply lur
;
87 if (sock
< 0 || handle
< 0)
89 memset(&lum
, 0, sizeof(lum
));
91 lum
.cmd
= LTTNG_UST_RELEASE
;
92 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
96 * If sock is negative, it means we don't have to notify the other side
97 * (e.g. application has already vanished).
99 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
106 switch (data
->type
) {
107 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
108 if (data
->u
.channel
.wakeup_fd
>= 0) {
109 ret
= close(data
->u
.channel
.wakeup_fd
);
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
:
135 case LTTNG_UST_OBJECT_TYPE_TRIGGER_GROUP
:
136 case LTTNG_UST_OBJECT_TYPE_TRIGGER
:
141 return ustctl_release_handle(sock
, data
->handle
);
145 * Send registration done packet to the application.
147 int ustctl_register_done(int sock
)
149 struct ustcomm_ust_msg lum
;
150 struct ustcomm_ust_reply lur
;
153 DBG("Sending register done command to %d", sock
);
154 memset(&lum
, 0, sizeof(lum
));
155 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
156 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
157 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
164 * returns session handle.
166 int ustctl_create_session(int sock
)
168 struct ustcomm_ust_msg lum
;
169 struct ustcomm_ust_reply lur
;
170 int ret
, session_handle
;
173 memset(&lum
, 0, sizeof(lum
));
174 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
175 lum
.cmd
= LTTNG_UST_SESSION
;
176 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
179 session_handle
= lur
.ret_val
;
180 DBG("received session handle %u", session_handle
);
181 return session_handle
;
184 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
185 struct lttng_ust_object_data
*channel_data
,
186 struct lttng_ust_object_data
**_event_data
)
188 struct ustcomm_ust_msg lum
;
189 struct ustcomm_ust_reply lur
;
190 struct lttng_ust_object_data
*event_data
;
193 if (!channel_data
|| !_event_data
)
196 event_data
= zmalloc(sizeof(*event_data
));
199 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
200 memset(&lum
, 0, sizeof(lum
));
201 lum
.handle
= channel_data
->handle
;
202 lum
.cmd
= LTTNG_UST_EVENT
;
203 strncpy(lum
.u
.event
.name
, ev
->name
,
204 LTTNG_UST_SYM_NAME_LEN
);
205 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
206 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
207 lum
.u
.event
.loglevel
= ev
->loglevel
;
208 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
213 event_data
->handle
= lur
.ret_val
;
214 DBG("received event handle %u", event_data
->handle
);
215 *_event_data
= event_data
;
219 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
220 struct lttng_ust_object_data
*obj_data
,
221 struct lttng_ust_object_data
**_context_data
)
223 struct ustcomm_ust_msg lum
;
224 struct ustcomm_ust_reply lur
;
225 struct lttng_ust_object_data
*context_data
= NULL
;
230 if (!obj_data
|| !_context_data
) {
235 context_data
= zmalloc(sizeof(*context_data
));
240 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
241 memset(&lum
, 0, sizeof(lum
));
242 lum
.handle
= obj_data
->handle
;
243 lum
.cmd
= LTTNG_UST_CONTEXT
;
245 lum
.u
.context
.ctx
= ctx
->ctx
;
247 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
248 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
250 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
252 size_t provider_name_len
= strlen(
253 ctx
->u
.app_ctx
.provider_name
) + 1;
254 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
256 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
257 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
259 len
= provider_name_len
+ ctx_name_len
;
265 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
267 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
274 ret
= ustcomm_send_app_msg(sock
, &lum
);
278 /* send var len ctx_name */
279 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
288 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
292 context_data
->handle
= -1;
293 DBG("Context created successfully");
294 *_context_data
= context_data
;
302 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
303 struct lttng_ust_object_data
*obj_data
)
305 struct ustcomm_ust_msg lum
;
306 struct ustcomm_ust_reply lur
;
312 memset(&lum
, 0, sizeof(lum
));
313 lum
.handle
= obj_data
->handle
;
314 lum
.cmd
= LTTNG_UST_FILTER
;
315 lum
.u
.filter
.data_size
= bytecode
->len
;
316 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
317 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
319 ret
= ustcomm_send_app_msg(sock
, &lum
);
322 /* send var len bytecode */
323 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
328 if (ret
!= bytecode
->len
)
330 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
333 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
334 struct lttng_ust_object_data
*obj_data
)
336 struct ustcomm_ust_msg lum
;
337 struct ustcomm_ust_reply lur
;
343 memset(&lum
, 0, sizeof(lum
));
344 lum
.handle
= obj_data
->handle
;
345 lum
.cmd
= LTTNG_UST_CAPTURE
;
346 lum
.u
.capture
.data_size
= bytecode
->len
;
347 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
348 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
350 ret
= ustcomm_send_app_msg(sock
, &lum
);
353 /* send var len bytecode */
354 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
359 if (ret
!= bytecode
->len
)
361 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
364 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
365 struct lttng_ust_object_data
*obj_data
)
367 struct ustcomm_ust_msg lum
;
368 struct ustcomm_ust_reply lur
;
375 memset(&lum
, 0, sizeof(lum
));
376 lum
.handle
= obj_data
->handle
;
377 lum
.cmd
= LTTNG_UST_EXCLUSION
;
378 lum
.u
.exclusion
.count
= exclusion
->count
;
380 ret
= ustcomm_send_app_msg(sock
, &lum
);
385 /* send var len exclusion names */
386 ret
= ustcomm_send_unix_sock(sock
,
388 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
392 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
395 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
398 /* Enable event, channel and session ioctl */
399 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
401 struct ustcomm_ust_msg lum
;
402 struct ustcomm_ust_reply lur
;
408 memset(&lum
, 0, sizeof(lum
));
409 lum
.handle
= object
->handle
;
410 lum
.cmd
= LTTNG_UST_ENABLE
;
411 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
414 DBG("enabled handle %u", object
->handle
);
418 /* Disable event, channel and session ioctl */
419 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
421 struct ustcomm_ust_msg lum
;
422 struct ustcomm_ust_reply lur
;
428 memset(&lum
, 0, sizeof(lum
));
429 lum
.handle
= object
->handle
;
430 lum
.cmd
= LTTNG_UST_DISABLE
;
431 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
434 DBG("disable handle %u", object
->handle
);
438 int ustctl_start_session(int sock
, int handle
)
440 struct lttng_ust_object_data obj
;
443 return ustctl_enable(sock
, &obj
);
446 int ustctl_stop_session(int sock
, int handle
)
448 struct lttng_ust_object_data obj
;
451 return ustctl_disable(sock
, &obj
);
454 int ustctl_create_trigger_group(int sock
, int pipe_fd
,
455 struct lttng_ust_object_data
**_trigger_group_data
)
457 struct lttng_ust_object_data
*trigger_group_data
;
458 struct ustcomm_ust_msg lum
;
459 struct ustcomm_ust_reply lur
;
463 if (!_trigger_group_data
)
466 trigger_group_data
= zmalloc(sizeof(*trigger_group_data
));
467 if (!trigger_group_data
)
470 trigger_group_data
->type
= LTTNG_UST_OBJECT_TYPE_TRIGGER_GROUP
;
472 memset(&lum
, 0, sizeof(lum
));
473 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
474 lum
.cmd
= LTTNG_UST_TRIGGER_GROUP_CREATE
;
476 ret
= ustcomm_send_app_msg(sock
, &lum
);
480 /* Send trigger notification pipe. */
481 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
487 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
491 trigger_group_data
->handle
= lur
.ret_val
;
492 DBG("received trigger group handle %d", trigger_group_data
->handle
);
494 *_trigger_group_data
= trigger_group_data
;
499 free(trigger_group_data
);
505 int ustctl_create_trigger(int sock
, struct lttng_ust_trigger
*trigger
,
506 struct lttng_ust_object_data
*trigger_group
,
507 struct lttng_ust_object_data
**_trigger_data
)
509 struct ustcomm_ust_msg lum
;
510 struct ustcomm_ust_reply lur
;
511 struct lttng_ust_object_data
*trigger_data
;
514 if (!trigger_group
|| !_trigger_data
)
517 trigger_data
= zmalloc(sizeof(*trigger_data
));
521 trigger_data
->type
= LTTNG_UST_OBJECT_TYPE_TRIGGER
;
523 memset(&lum
, 0, sizeof(lum
));
524 lum
.handle
= trigger_group
->handle
;
525 lum
.cmd
= LTTNG_UST_TRIGGER_CREATE
;
527 strncpy(lum
.u
.trigger
.name
, trigger
->name
,
528 LTTNG_UST_SYM_NAME_LEN
);
529 lum
.u
.trigger
.instrumentation
= trigger
->instrumentation
;
530 lum
.u
.trigger
.loglevel_type
= trigger
->loglevel_type
;
531 lum
.u
.trigger
.loglevel
= trigger
->loglevel
;
532 lum
.u
.trigger
.id
= trigger
->id
;
533 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
538 trigger_data
->handle
= lur
.ret_val
;
539 DBG("received event handle %u", trigger_data
->handle
);
540 *_trigger_data
= trigger_data
;
545 int ustctl_tracepoint_list(int sock
)
547 struct ustcomm_ust_msg lum
;
548 struct ustcomm_ust_reply lur
;
549 int ret
, tp_list_handle
;
551 memset(&lum
, 0, sizeof(lum
));
552 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
553 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
554 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
557 tp_list_handle
= lur
.ret_val
;
558 DBG("received tracepoint list handle %u", tp_list_handle
);
559 return tp_list_handle
;
562 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
563 struct lttng_ust_tracepoint_iter
*iter
)
565 struct ustcomm_ust_msg lum
;
566 struct ustcomm_ust_reply lur
;
572 memset(&lum
, 0, sizeof(lum
));
573 lum
.handle
= tp_list_handle
;
574 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
575 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
578 DBG("received tracepoint list entry name %s loglevel %d",
579 lur
.u
.tracepoint
.name
,
580 lur
.u
.tracepoint
.loglevel
);
581 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
585 int ustctl_tracepoint_field_list(int sock
)
587 struct ustcomm_ust_msg lum
;
588 struct ustcomm_ust_reply lur
;
589 int ret
, tp_field_list_handle
;
591 memset(&lum
, 0, sizeof(lum
));
592 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
593 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
594 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
597 tp_field_list_handle
= lur
.ret_val
;
598 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
599 return tp_field_list_handle
;
602 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
603 struct lttng_ust_field_iter
*iter
)
605 struct ustcomm_ust_msg lum
;
606 struct ustcomm_ust_reply lur
;
613 memset(&lum
, 0, sizeof(lum
));
614 lum
.handle
= tp_field_list_handle
;
615 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
616 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
619 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
620 if (len
!= sizeof(*iter
)) {
623 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
631 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
633 struct ustcomm_ust_msg lum
;
634 struct ustcomm_ust_reply lur
;
640 memset(&lum
, 0, sizeof(lum
));
641 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
642 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
643 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
646 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
647 DBG("received tracer version");
651 int ustctl_wait_quiescent(int sock
)
653 struct ustcomm_ust_msg lum
;
654 struct ustcomm_ust_reply lur
;
657 memset(&lum
, 0, sizeof(lum
));
658 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
659 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
660 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
663 DBG("waited for quiescent state");
667 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
675 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
677 struct ustcomm_ust_msg lum
;
678 struct ustcomm_ust_reply lur
;
684 memset(&lum
, 0, sizeof(lum
));
685 lum
.handle
= object
->handle
;
686 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
687 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
690 DBG("flushed buffer handle %u", object
->handle
);
695 int ustctl_send_channel(int sock
,
696 enum lttng_ust_chan_type type
,
706 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
707 if (len
!= sizeof(size
)) {
714 /* Send channel type */
715 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
716 if (len
!= sizeof(type
)) {
724 /* Send channel data */
725 len
= ustcomm_send_unix_sock(sock
, data
, size
);
734 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
745 int ustctl_send_stream(int sock
,
747 uint64_t memory_map_size
,
748 int shm_fd
, int wakeup_fd
,
756 /* finish iteration */
759 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
760 if (len
!= sizeof(v
)) {
770 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
771 sizeof(memory_map_size
));
772 if (len
!= sizeof(memory_map_size
)) {
780 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
782 if (len
!= sizeof(stream_nr
)) {
790 /* Send shm fd and wakeup fd */
793 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
803 int ustctl_recv_channel_from_consumer(int sock
,
804 struct lttng_ust_object_data
**_channel_data
)
806 struct lttng_ust_object_data
*channel_data
;
811 channel_data
= zmalloc(sizeof(*channel_data
));
816 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
817 channel_data
->handle
= -1;
820 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
821 sizeof(channel_data
->size
));
822 if (len
!= sizeof(channel_data
->size
)) {
830 /* recv channel type */
831 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
832 sizeof(channel_data
->u
.channel
.type
));
833 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
841 /* recv channel data */
842 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
843 if (!channel_data
->u
.channel
.data
) {
847 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
849 if (len
!= channel_data
->size
) {
854 goto error_recv_data
;
857 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
861 goto error_recv_data
;
864 goto error_recv_data
;
867 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
868 *_channel_data
= channel_data
;
872 free(channel_data
->u
.channel
.data
);
879 int ustctl_recv_stream_from_consumer(int sock
,
880 struct lttng_ust_object_data
**_stream_data
)
882 struct lttng_ust_object_data
*stream_data
;
887 stream_data
= zmalloc(sizeof(*stream_data
));
893 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
894 stream_data
->handle
= -1;
897 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
898 sizeof(stream_data
->size
));
899 if (len
!= sizeof(stream_data
->size
)) {
906 if (stream_data
->size
== -1) {
907 ret
= -LTTNG_UST_ERR_NOENT
;
912 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
913 sizeof(stream_data
->u
.stream
.stream_nr
));
914 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
922 /* recv shm fd and wakeup fd */
923 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
933 stream_data
->u
.stream
.shm_fd
= fds
[0];
934 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
935 *_stream_data
= stream_data
;
944 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
945 struct lttng_ust_object_data
*channel_data
)
947 struct ustcomm_ust_msg lum
;
948 struct ustcomm_ust_reply lur
;
954 memset(&lum
, 0, sizeof(lum
));
955 lum
.handle
= session_handle
;
956 lum
.cmd
= LTTNG_UST_CHANNEL
;
957 lum
.u
.channel
.len
= channel_data
->size
;
958 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
959 ret
= ustcomm_send_app_msg(sock
, &lum
);
963 ret
= ustctl_send_channel(sock
,
964 channel_data
->u
.channel
.type
,
965 channel_data
->u
.channel
.data
,
967 channel_data
->u
.channel
.wakeup_fd
,
971 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
973 channel_data
->handle
= lur
.ret_val
;
978 int ustctl_send_stream_to_ust(int sock
,
979 struct lttng_ust_object_data
*channel_data
,
980 struct lttng_ust_object_data
*stream_data
)
982 struct ustcomm_ust_msg lum
;
983 struct ustcomm_ust_reply lur
;
986 memset(&lum
, 0, sizeof(lum
));
987 lum
.handle
= channel_data
->handle
;
988 lum
.cmd
= LTTNG_UST_STREAM
;
989 lum
.u
.stream
.len
= stream_data
->size
;
990 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
991 ret
= ustcomm_send_app_msg(sock
, &lum
);
996 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
998 ret
= ustctl_send_stream(sock
,
999 stream_data
->u
.stream
.stream_nr
,
1001 stream_data
->u
.stream
.shm_fd
,
1002 stream_data
->u
.stream
.wakeup_fd
, 1);
1005 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1008 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1009 struct lttng_ust_object_data
*src
)
1011 struct lttng_ust_object_data
*obj
;
1014 if (src
->handle
!= -1) {
1019 obj
= zmalloc(sizeof(*obj
));
1025 obj
->type
= src
->type
;
1026 obj
->handle
= src
->handle
;
1027 obj
->size
= src
->size
;
1029 switch (obj
->type
) {
1030 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1032 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1033 if (src
->u
.channel
.wakeup_fd
>= 0) {
1034 obj
->u
.channel
.wakeup_fd
=
1035 dup(src
->u
.channel
.wakeup_fd
);
1036 if (obj
->u
.channel
.wakeup_fd
< 0) {
1038 goto chan_error_wakeup_fd
;
1041 obj
->u
.channel
.wakeup_fd
=
1042 src
->u
.channel
.wakeup_fd
;
1044 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1045 if (!obj
->u
.channel
.data
) {
1047 goto chan_error_alloc
;
1049 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1053 if (src
->u
.channel
.wakeup_fd
>= 0) {
1056 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1061 chan_error_wakeup_fd
:
1066 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1068 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1069 if (src
->u
.stream
.wakeup_fd
>= 0) {
1070 obj
->u
.stream
.wakeup_fd
=
1071 dup(src
->u
.stream
.wakeup_fd
);
1072 if (obj
->u
.stream
.wakeup_fd
< 0) {
1074 goto stream_error_wakeup_fd
;
1077 obj
->u
.stream
.wakeup_fd
=
1078 src
->u
.stream
.wakeup_fd
;
1081 if (src
->u
.stream
.shm_fd
>= 0) {
1082 obj
->u
.stream
.shm_fd
=
1083 dup(src
->u
.stream
.shm_fd
);
1084 if (obj
->u
.stream
.shm_fd
< 0) {
1086 goto stream_error_shm_fd
;
1089 obj
->u
.stream
.shm_fd
=
1090 src
->u
.stream
.shm_fd
;
1094 stream_error_shm_fd
:
1095 if (src
->u
.stream
.wakeup_fd
>= 0) {
1098 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1103 stream_error_wakeup_fd
:
1122 /* Buffer operations */
1124 int ustctl_get_nr_stream_per_channel(void)
1126 return num_possible_cpus();
1129 struct ustctl_consumer_channel
*
1130 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1131 const int *stream_fds
, int nr_stream_fds
)
1133 struct ustctl_consumer_channel
*chan
;
1134 const char *transport_name
;
1135 struct lttng_transport
*transport
;
1137 switch (attr
->type
) {
1138 case LTTNG_UST_CHAN_PER_CPU
:
1139 if (attr
->output
== LTTNG_UST_MMAP
) {
1140 if (attr
->overwrite
) {
1141 if (attr
->read_timer_interval
== 0) {
1142 transport_name
= "relay-overwrite-mmap";
1144 transport_name
= "relay-overwrite-rt-mmap";
1147 if (attr
->read_timer_interval
== 0) {
1148 transport_name
= "relay-discard-mmap";
1150 transport_name
= "relay-discard-rt-mmap";
1157 case LTTNG_UST_CHAN_METADATA
:
1158 if (attr
->output
== LTTNG_UST_MMAP
)
1159 transport_name
= "relay-metadata-mmap";
1164 transport_name
= "<unknown>";
1168 transport
= lttng_transport_find(transport_name
);
1170 DBG("LTTng transport %s not found\n",
1175 chan
= zmalloc(sizeof(*chan
));
1179 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1180 attr
->subbuf_size
, attr
->num_subbuf
,
1181 attr
->switch_timer_interval
,
1182 attr
->read_timer_interval
,
1183 attr
->uuid
, attr
->chan_id
,
1184 stream_fds
, nr_stream_fds
,
1185 attr
->blocking_timeout
);
1189 chan
->chan
->ops
= &transport
->ops
;
1190 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1191 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1192 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1200 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1202 (void) ustctl_channel_close_wait_fd(chan
);
1203 (void) ustctl_channel_close_wakeup_fd(chan
);
1204 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1208 int ustctl_send_channel_to_sessiond(int sock
,
1209 struct ustctl_consumer_channel
*channel
)
1211 struct shm_object_table
*table
;
1213 table
= channel
->chan
->handle
->table
;
1214 if (table
->size
<= 0)
1216 return ustctl_send_channel(sock
,
1218 table
->objects
[0].memory_map
,
1219 table
->objects
[0].memory_map_size
,
1224 int ustctl_send_stream_to_sessiond(int sock
,
1225 struct ustctl_consumer_stream
*stream
)
1228 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1230 return ustctl_send_stream(sock
,
1232 stream
->memory_map_size
,
1233 stream
->shm_fd
, stream
->wakeup_fd
,
1237 int ustctl_write_metadata_to_channel(
1238 struct ustctl_consumer_channel
*channel
,
1239 const char *metadata_str
, /* NOT null-terminated */
1240 size_t len
) /* metadata length */
1242 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1243 struct lttng_channel
*chan
= channel
->chan
;
1244 const char *str
= metadata_str
;
1245 int ret
= 0, waitret
;
1246 size_t reserve_len
, pos
;
1248 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1249 reserve_len
= min_t(size_t,
1250 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1252 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1253 sizeof(char), -1, chan
->handle
, NULL
);
1255 * We don't care about metadata buffer's records lost
1256 * count, because we always retry here. Report error if
1257 * we need to bail out after timeout or being
1260 waitret
= wait_cond_interruptible_timeout(
1262 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1263 ret
!= -ENOBUFS
|| !ret
;
1265 LTTNG_METADATA_TIMEOUT_MSEC
);
1266 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1267 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1268 waitret
== -EINTR
? "interrupted" :
1269 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1270 if (waitret
== -EINTR
)
1274 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1275 chan
->ops
->event_commit(&ctx
);
1282 * Write at most one packet in the channel.
1283 * Returns the number of bytes written on success, < 0 on error.
1285 ssize_t
ustctl_write_one_packet_to_channel(
1286 struct ustctl_consumer_channel
*channel
,
1287 const char *metadata_str
, /* NOT null-terminated */
1288 size_t len
) /* metadata length */
1290 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1291 struct lttng_channel
*chan
= channel
->chan
;
1292 const char *str
= metadata_str
;
1293 ssize_t reserve_len
;
1296 reserve_len
= min_t(ssize_t
,
1297 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1299 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1300 sizeof(char), -1, chan
->handle
, NULL
);
1301 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1303 DBG("LTTng: event reservation failed");
1308 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1309 chan
->ops
->event_commit(&ctx
);
1315 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1317 struct channel
*chan
;
1320 chan
= consumer_chan
->chan
->chan
;
1321 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1322 chan
, chan
->handle
);
1324 consumer_chan
->wait_fd
= -1;
1328 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1330 struct channel
*chan
;
1333 chan
= consumer_chan
->chan
->chan
;
1334 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1335 chan
, chan
->handle
);
1337 consumer_chan
->wakeup_fd
= -1;
1341 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1343 struct channel
*chan
;
1345 chan
= stream
->chan
->chan
->chan
;
1346 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1347 chan
, stream
->handle
, stream
->cpu
);
1350 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1352 struct channel
*chan
;
1354 chan
= stream
->chan
->chan
->chan
;
1355 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1356 chan
, stream
->handle
, stream
->cpu
);
1359 struct ustctl_consumer_stream
*
1360 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1363 struct ustctl_consumer_stream
*stream
;
1364 struct lttng_ust_shm_handle
*handle
;
1365 struct channel
*chan
;
1366 int shm_fd
, wait_fd
, wakeup_fd
;
1367 uint64_t memory_map_size
;
1368 struct lttng_ust_lib_ring_buffer
*buf
;
1373 handle
= channel
->chan
->handle
;
1377 chan
= channel
->chan
->chan
;
1378 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1379 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1380 &wakeup_fd
, &memory_map_size
);
1383 ret
= lib_ring_buffer_open_read(buf
, handle
);
1387 stream
= zmalloc(sizeof(*stream
));
1390 stream
->handle
= handle
;
1392 stream
->chan
= channel
;
1393 stream
->shm_fd
= shm_fd
;
1394 stream
->wait_fd
= wait_fd
;
1395 stream
->wakeup_fd
= wakeup_fd
;
1396 stream
->memory_map_size
= memory_map_size
;
1404 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1406 struct lttng_ust_lib_ring_buffer
*buf
;
1407 struct ustctl_consumer_channel
*consumer_chan
;
1411 consumer_chan
= stream
->chan
;
1412 (void) ustctl_stream_close_wait_fd(stream
);
1413 (void) ustctl_stream_close_wakeup_fd(stream
);
1414 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1418 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1422 return shm_get_wait_fd(chan
->chan
->handle
,
1423 &chan
->chan
->handle
->chan
._ref
);
1426 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1430 return shm_get_wakeup_fd(chan
->chan
->handle
,
1431 &chan
->chan
->handle
->chan
._ref
);
1434 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1436 struct lttng_ust_lib_ring_buffer
*buf
;
1437 struct ustctl_consumer_channel
*consumer_chan
;
1442 consumer_chan
= stream
->chan
;
1443 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1446 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1448 struct lttng_ust_lib_ring_buffer
*buf
;
1449 struct ustctl_consumer_channel
*consumer_chan
;
1454 consumer_chan
= stream
->chan
;
1455 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1458 /* For mmap mode, readable without "get" operation */
1460 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1462 struct lttng_ust_lib_ring_buffer
*buf
;
1463 struct ustctl_consumer_channel
*consumer_chan
;
1468 consumer_chan
= stream
->chan
;
1469 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1472 /* returns the length to mmap. */
1473 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1476 struct ustctl_consumer_channel
*consumer_chan
;
1477 unsigned long mmap_buf_len
;
1478 struct channel
*chan
;
1482 consumer_chan
= stream
->chan
;
1483 chan
= consumer_chan
->chan
->chan
;
1484 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1486 mmap_buf_len
= chan
->backend
.buf_size
;
1487 if (chan
->backend
.extra_reader_sb
)
1488 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1489 if (mmap_buf_len
> INT_MAX
)
1491 *len
= mmap_buf_len
;
1495 /* returns the maximum size for sub-buffers. */
1496 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1499 struct ustctl_consumer_channel
*consumer_chan
;
1500 struct channel
*chan
;
1504 consumer_chan
= stream
->chan
;
1505 chan
= consumer_chan
->chan
->chan
;
1506 *len
= chan
->backend
.subbuf_size
;
1511 * For mmap mode, operate on the current packet (between get/put or
1512 * get_next/put_next).
1515 /* returns the offset of the subbuffer belonging to the mmap reader. */
1516 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1519 struct channel
*chan
;
1520 unsigned long sb_bindex
;
1521 struct lttng_ust_lib_ring_buffer
*buf
;
1522 struct ustctl_consumer_channel
*consumer_chan
;
1523 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1524 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1529 consumer_chan
= stream
->chan
;
1530 chan
= consumer_chan
->chan
->chan
;
1531 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1533 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1534 buf
->backend
.buf_rsb
.id
);
1535 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1539 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1542 *off
= pages
->mmap_offset
;
1546 /* returns the size of the current sub-buffer, without padding (for mmap). */
1547 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1550 struct ustctl_consumer_channel
*consumer_chan
;
1551 struct channel
*chan
;
1552 struct lttng_ust_lib_ring_buffer
*buf
;
1558 consumer_chan
= stream
->chan
;
1559 chan
= consumer_chan
->chan
->chan
;
1560 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1561 consumer_chan
->chan
->handle
);
1565 /* returns the size of the current sub-buffer, without padding (for mmap). */
1566 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1569 struct ustctl_consumer_channel
*consumer_chan
;
1570 struct channel
*chan
;
1571 struct lttng_ust_lib_ring_buffer
*buf
;
1576 consumer_chan
= stream
->chan
;
1577 chan
= consumer_chan
->chan
->chan
;
1578 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1579 consumer_chan
->chan
->handle
);
1580 *len
= PAGE_ALIGN(*len
);
1584 /* Get exclusive read access to the next sub-buffer that can be read. */
1585 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1587 struct lttng_ust_lib_ring_buffer
*buf
;
1588 struct ustctl_consumer_channel
*consumer_chan
;
1593 consumer_chan
= stream
->chan
;
1594 return lib_ring_buffer_get_next_subbuf(buf
,
1595 consumer_chan
->chan
->handle
);
1599 /* Release exclusive sub-buffer access, move consumer forward. */
1600 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1602 struct lttng_ust_lib_ring_buffer
*buf
;
1603 struct ustctl_consumer_channel
*consumer_chan
;
1608 consumer_chan
= stream
->chan
;
1609 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1615 /* Get a snapshot of the current ring buffer producer and consumer positions */
1616 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1618 struct lttng_ust_lib_ring_buffer
*buf
;
1619 struct ustctl_consumer_channel
*consumer_chan
;
1624 consumer_chan
= stream
->chan
;
1625 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1626 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1630 * Get a snapshot of the current ring buffer producer and consumer positions
1631 * even if the consumed and produced positions are contained within the same
1634 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1636 struct lttng_ust_lib_ring_buffer
*buf
;
1637 struct ustctl_consumer_channel
*consumer_chan
;
1642 consumer_chan
= stream
->chan
;
1643 return lib_ring_buffer_snapshot_sample_positions(buf
,
1644 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1645 consumer_chan
->chan
->handle
);
1648 /* Get the consumer position (iteration start) */
1649 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1652 struct lttng_ust_lib_ring_buffer
*buf
;
1657 *pos
= buf
->cons_snapshot
;
1661 /* Get the producer position (iteration end) */
1662 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1665 struct lttng_ust_lib_ring_buffer
*buf
;
1670 *pos
= buf
->prod_snapshot
;
1674 /* Get exclusive read access to the specified sub-buffer position */
1675 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1678 struct lttng_ust_lib_ring_buffer
*buf
;
1679 struct ustctl_consumer_channel
*consumer_chan
;
1684 consumer_chan
= stream
->chan
;
1685 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1686 consumer_chan
->chan
->handle
);
1689 /* Release exclusive sub-buffer access */
1690 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1692 struct lttng_ust_lib_ring_buffer
*buf
;
1693 struct ustctl_consumer_channel
*consumer_chan
;
1698 consumer_chan
= stream
->chan
;
1699 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1703 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1704 int producer_active
)
1706 struct lttng_ust_lib_ring_buffer
*buf
;
1707 struct ustctl_consumer_channel
*consumer_chan
;
1711 consumer_chan
= stream
->chan
;
1712 lib_ring_buffer_switch_slow(buf
,
1713 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1714 consumer_chan
->chan
->handle
);
1717 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1719 struct lttng_ust_lib_ring_buffer
*buf
;
1720 struct ustctl_consumer_channel
*consumer_chan
;
1724 consumer_chan
= stream
->chan
;
1725 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1726 consumer_chan
->chan
->handle
);
1727 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1731 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1732 struct lttng_ust_lib_ring_buffer
*buf
,
1733 struct lttng_ust_shm_handle
*handle
)
1735 struct channel
*chan
;
1736 const struct lttng_ust_lib_ring_buffer_config
*config
;
1737 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1739 chan
= shmp(handle
, buf
->backend
.chan
);
1742 config
= &chan
->backend
.config
;
1743 if (!config
->cb_ptr
)
1745 client_cb
= caa_container_of(config
->cb_ptr
,
1746 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1751 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1752 uint64_t *timestamp_begin
)
1754 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1755 struct lttng_ust_lib_ring_buffer
*buf
;
1756 struct lttng_ust_shm_handle
*handle
;
1758 if (!stream
|| !timestamp_begin
)
1761 handle
= stream
->chan
->chan
->handle
;
1762 client_cb
= get_client_cb(buf
, handle
);
1765 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1768 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1769 uint64_t *timestamp_end
)
1771 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1772 struct lttng_ust_lib_ring_buffer
*buf
;
1773 struct lttng_ust_shm_handle
*handle
;
1775 if (!stream
|| !timestamp_end
)
1778 handle
= stream
->chan
->chan
->handle
;
1779 client_cb
= get_client_cb(buf
, handle
);
1782 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1785 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1786 uint64_t *events_discarded
)
1788 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1789 struct lttng_ust_lib_ring_buffer
*buf
;
1790 struct lttng_ust_shm_handle
*handle
;
1792 if (!stream
|| !events_discarded
)
1795 handle
= stream
->chan
->chan
->handle
;
1796 client_cb
= get_client_cb(buf
, handle
);
1799 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1802 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1803 uint64_t *content_size
)
1805 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1806 struct lttng_ust_lib_ring_buffer
*buf
;
1807 struct lttng_ust_shm_handle
*handle
;
1809 if (!stream
|| !content_size
)
1812 handle
= stream
->chan
->chan
->handle
;
1813 client_cb
= get_client_cb(buf
, handle
);
1816 return client_cb
->content_size(buf
, handle
, content_size
);
1819 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1820 uint64_t *packet_size
)
1822 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1823 struct lttng_ust_lib_ring_buffer
*buf
;
1824 struct lttng_ust_shm_handle
*handle
;
1826 if (!stream
|| !packet_size
)
1829 handle
= stream
->chan
->chan
->handle
;
1830 client_cb
= get_client_cb(buf
, handle
);
1833 return client_cb
->packet_size(buf
, handle
, packet_size
);
1836 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1837 uint64_t *stream_id
)
1839 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1840 struct lttng_ust_lib_ring_buffer
*buf
;
1841 struct lttng_ust_shm_handle
*handle
;
1843 if (!stream
|| !stream_id
)
1846 handle
= stream
->chan
->chan
->handle
;
1847 client_cb
= get_client_cb(buf
, handle
);
1850 return client_cb
->stream_id(buf
, handle
, stream_id
);
1853 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1856 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1857 struct lttng_ust_lib_ring_buffer
*buf
;
1858 struct lttng_ust_shm_handle
*handle
;
1863 handle
= stream
->chan
->chan
->handle
;
1864 client_cb
= get_client_cb(buf
, handle
);
1865 if (!client_cb
|| !client_cb
->current_timestamp
)
1867 return client_cb
->current_timestamp(buf
, handle
, ts
);
1870 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1873 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1874 struct lttng_ust_lib_ring_buffer
*buf
;
1875 struct lttng_ust_shm_handle
*handle
;
1877 if (!stream
|| !seq
)
1880 handle
= stream
->chan
->chan
->handle
;
1881 client_cb
= get_client_cb(buf
, handle
);
1882 if (!client_cb
|| !client_cb
->sequence_number
)
1884 return client_cb
->sequence_number(buf
, handle
, seq
);
1887 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1890 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1891 struct lttng_ust_lib_ring_buffer
*buf
;
1892 struct lttng_ust_shm_handle
*handle
;
1897 handle
= stream
->chan
->chan
->handle
;
1898 client_cb
= get_client_cb(buf
, handle
);
1901 return client_cb
->instance_id(buf
, handle
, id
);
1904 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1906 int ustctl_has_perf_counters(void)
1913 int ustctl_has_perf_counters(void)
1921 * Returns 0 on success, negative error value on error.
1923 int ustctl_recv_reg_msg(int sock
,
1924 enum ustctl_socket_type
*type
,
1931 uint32_t *bits_per_long
,
1932 uint32_t *uint8_t_alignment
,
1933 uint32_t *uint16_t_alignment
,
1934 uint32_t *uint32_t_alignment
,
1935 uint32_t *uint64_t_alignment
,
1936 uint32_t *long_alignment
,
1941 struct ustctl_reg_msg reg_msg
;
1943 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1944 if (len
> 0 && len
!= sizeof(reg_msg
))
1951 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1952 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1953 BIG_ENDIAN
: LITTLE_ENDIAN
;
1954 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1955 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1956 LITTLE_ENDIAN
: BIG_ENDIAN
;
1958 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1960 switch (reg_msg
.socket_type
) {
1961 case 0: *type
= USTCTL_SOCKET_CMD
;
1963 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1966 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1968 *major
= reg_msg
.major
;
1969 *minor
= reg_msg
.minor
;
1971 *ppid
= reg_msg
.ppid
;
1974 *bits_per_long
= reg_msg
.bits_per_long
;
1975 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1976 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1977 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1978 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1979 *long_alignment
= reg_msg
.long_alignment
;
1980 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1981 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
1982 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
1983 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1989 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1991 struct ustcomm_notify_hdr header
;
1994 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1995 if (len
> 0 && len
!= sizeof(header
))
2001 switch (header
.notify_cmd
) {
2003 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2006 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2009 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2018 * Returns 0 on success, negative error value on error.
2020 int ustctl_recv_register_event(int sock
,
2027 struct ustctl_field
**fields
,
2028 char **model_emf_uri
)
2031 struct ustcomm_notify_event_msg msg
;
2032 size_t signature_len
, fields_len
, model_emf_uri_len
;
2033 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2034 struct ustctl_field
*a_fields
= NULL
;
2036 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2037 if (len
> 0 && len
!= sizeof(msg
))
2044 *session_objd
= msg
.session_objd
;
2045 *channel_objd
= msg
.channel_objd
;
2046 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2047 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2048 *loglevel
= msg
.loglevel
;
2049 signature_len
= msg
.signature_len
;
2050 fields_len
= msg
.fields_len
;
2052 if (fields_len
% sizeof(*a_fields
) != 0) {
2056 model_emf_uri_len
= msg
.model_emf_uri_len
;
2058 /* recv signature. contains at least \0. */
2059 a_sign
= zmalloc(signature_len
);
2062 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2063 if (len
> 0 && len
!= signature_len
) {
2065 goto signature_error
;
2069 goto signature_error
;
2072 goto signature_error
;
2074 /* Enforce end of string */
2075 a_sign
[signature_len
- 1] = '\0';
2079 a_fields
= zmalloc(fields_len
);
2082 goto signature_error
;
2084 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2085 if (len
> 0 && len
!= fields_len
) {
2098 if (model_emf_uri_len
) {
2099 /* recv model_emf_uri_len */
2100 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2101 if (!a_model_emf_uri
) {
2105 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2107 if (len
> 0 && len
!= model_emf_uri_len
) {
2118 /* Enforce end of string */
2119 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2122 *signature
= a_sign
;
2123 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2125 *model_emf_uri
= a_model_emf_uri
;
2130 free(a_model_emf_uri
);
2139 * Returns 0 on success, negative error value on error.
2141 int ustctl_reply_register_event(int sock
,
2147 struct ustcomm_notify_hdr header
;
2148 struct ustcomm_notify_event_reply r
;
2151 memset(&reply
, 0, sizeof(reply
));
2152 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2153 reply
.r
.ret_code
= ret_code
;
2154 reply
.r
.event_id
= id
;
2155 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2156 if (len
> 0 && len
!= sizeof(reply
))
2164 * Returns 0 on success, negative UST or system error value on error.
2166 int ustctl_recv_register_enum(int sock
,
2169 struct ustctl_enum_entry
**entries
,
2173 struct ustcomm_notify_enum_msg msg
;
2175 struct ustctl_enum_entry
*a_entries
= NULL
;
2177 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2178 if (len
> 0 && len
!= sizeof(msg
))
2185 *session_objd
= msg
.session_objd
;
2186 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2187 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2188 entries_len
= msg
.entries_len
;
2190 if (entries_len
% sizeof(*a_entries
) != 0) {
2196 a_entries
= zmalloc(entries_len
);
2199 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2200 if (len
> 0 && len
!= entries_len
) {
2212 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2213 *entries
= a_entries
;
2223 * Returns 0 on success, negative error value on error.
2225 int ustctl_reply_register_enum(int sock
,
2231 struct ustcomm_notify_hdr header
;
2232 struct ustcomm_notify_enum_reply r
;
2235 memset(&reply
, 0, sizeof(reply
));
2236 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2237 reply
.r
.ret_code
= ret_code
;
2238 reply
.r
.enum_id
= id
;
2239 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2240 if (len
> 0 && len
!= sizeof(reply
))
2248 * Returns 0 on success, negative UST or system error value on error.
2250 int ustctl_recv_register_channel(int sock
,
2251 int *session_objd
, /* session descriptor (output) */
2252 int *channel_objd
, /* channel descriptor (output) */
2254 struct ustctl_field
**fields
)
2257 struct ustcomm_notify_channel_msg msg
;
2259 struct ustctl_field
*a_fields
;
2261 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2262 if (len
> 0 && len
!= sizeof(msg
))
2269 *session_objd
= msg
.session_objd
;
2270 *channel_objd
= msg
.channel_objd
;
2271 fields_len
= msg
.ctx_fields_len
;
2273 if (fields_len
% sizeof(*a_fields
) != 0) {
2279 a_fields
= zmalloc(fields_len
);
2284 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2285 if (len
> 0 && len
!= fields_len
) {
2300 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2310 * Returns 0 on success, negative error value on error.
2312 int ustctl_reply_register_channel(int sock
,
2314 enum ustctl_channel_header header_type
,
2319 struct ustcomm_notify_hdr header
;
2320 struct ustcomm_notify_channel_reply r
;
2323 memset(&reply
, 0, sizeof(reply
));
2324 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2325 reply
.r
.ret_code
= ret_code
;
2326 reply
.r
.chan_id
= chan_id
;
2327 switch (header_type
) {
2328 case USTCTL_CHANNEL_HEADER_COMPACT
:
2329 reply
.r
.header_type
= 1;
2331 case USTCTL_CHANNEL_HEADER_LARGE
:
2332 reply
.r
.header_type
= 2;
2335 reply
.r
.header_type
= 0;
2338 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2339 if (len
> 0 && len
!= sizeof(reply
))
2346 /* Regenerate the statedump. */
2347 int ustctl_regenerate_statedump(int sock
, int handle
)
2349 struct ustcomm_ust_msg lum
;
2350 struct ustcomm_ust_reply lur
;
2353 memset(&lum
, 0, sizeof(lum
));
2354 lum
.handle
= handle
;
2355 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2356 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2359 DBG("Regenerated statedump for handle %u", handle
);
2363 static __attribute__((constructor
))
2364 void ustctl_init(void)
2367 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2368 lttng_ust_clock_init();
2369 lttng_ring_buffer_metadata_client_init();
2370 lttng_ring_buffer_client_overwrite_init();
2371 lttng_ring_buffer_client_overwrite_rt_init();
2372 lttng_ring_buffer_client_discard_init();
2373 lttng_ring_buffer_client_discard_rt_init();
2374 lib_ringbuffer_signal_init();
2377 static __attribute__((destructor
))
2378 void ustctl_exit(void)
2380 lttng_ring_buffer_client_discard_rt_exit();
2381 lttng_ring_buffer_client_discard_exit();
2382 lttng_ring_buffer_client_overwrite_rt_exit();
2383 lttng_ring_buffer_client_overwrite_exit();
2384 lttng_ring_buffer_metadata_client_exit();