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_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
337 struct lttng_ust_object_data
*obj_data
)
339 struct ustcomm_ust_msg lum
;
340 struct ustcomm_ust_reply lur
;
346 memset(&lum
, 0, sizeof(lum
));
347 lum
.handle
= obj_data
->handle
;
348 lum
.cmd
= LTTNG_UST_CAPTURE
;
349 lum
.u
.capture
.data_size
= bytecode
->len
;
350 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
351 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
353 ret
= ustcomm_send_app_msg(sock
, &lum
);
356 /* send var len bytecode */
357 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
362 if (ret
!= bytecode
->len
)
364 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
367 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
368 struct lttng_ust_object_data
*obj_data
)
370 struct ustcomm_ust_msg lum
;
371 struct ustcomm_ust_reply lur
;
378 memset(&lum
, 0, sizeof(lum
));
379 lum
.handle
= obj_data
->handle
;
380 lum
.cmd
= LTTNG_UST_EXCLUSION
;
381 lum
.u
.exclusion
.count
= exclusion
->count
;
383 ret
= ustcomm_send_app_msg(sock
, &lum
);
388 /* send var len exclusion names */
389 ret
= ustcomm_send_unix_sock(sock
,
391 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
395 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
398 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
401 /* Enable event, channel and session ioctl */
402 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
404 struct ustcomm_ust_msg lum
;
405 struct ustcomm_ust_reply lur
;
411 memset(&lum
, 0, sizeof(lum
));
412 lum
.handle
= object
->handle
;
413 lum
.cmd
= LTTNG_UST_ENABLE
;
414 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
417 DBG("enabled handle %u", object
->handle
);
421 /* Disable event, channel and session ioctl */
422 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
424 struct ustcomm_ust_msg lum
;
425 struct ustcomm_ust_reply lur
;
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= object
->handle
;
433 lum
.cmd
= LTTNG_UST_DISABLE
;
434 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
437 DBG("disable handle %u", object
->handle
);
441 int ustctl_start_session(int sock
, int handle
)
443 struct lttng_ust_object_data obj
;
446 return ustctl_enable(sock
, &obj
);
449 int ustctl_stop_session(int sock
, int handle
)
451 struct lttng_ust_object_data obj
;
454 return ustctl_disable(sock
, &obj
);
457 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
458 struct lttng_ust_object_data
**_event_notifier_group_data
)
460 struct lttng_ust_object_data
*event_notifier_group_data
;
461 struct ustcomm_ust_msg lum
;
462 struct ustcomm_ust_reply lur
;
466 if (!_event_notifier_group_data
)
469 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
470 if (!event_notifier_group_data
)
473 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
475 memset(&lum
, 0, sizeof(lum
));
476 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
477 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
479 ret
= ustcomm_send_app_msg(sock
, &lum
);
483 /* Send event_notifier notification pipe. */
484 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
490 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
494 event_notifier_group_data
->handle
= lur
.ret_val
;
495 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
497 *_event_notifier_group_data
= event_notifier_group_data
;
502 free(event_notifier_group_data
);
508 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
509 struct lttng_ust_object_data
*event_notifier_group
,
510 struct lttng_ust_object_data
**_event_notifier_data
)
512 struct ustcomm_ust_msg lum
;
513 struct ustcomm_ust_reply lur
;
514 struct lttng_ust_object_data
*event_notifier_data
;
517 if (!event_notifier_group
|| !_event_notifier_data
)
520 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
521 if (!event_notifier_data
)
524 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
526 memset(&lum
, 0, sizeof(lum
));
527 lum
.handle
= event_notifier_group
->handle
;
528 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
530 strncpy(lum
.u
.event_notifier
.event
.name
, event_notifier
->event
.name
,
531 LTTNG_UST_SYM_NAME_LEN
);
532 lum
.u
.event_notifier
.event
.instrumentation
= event_notifier
->event
.instrumentation
;
533 lum
.u
.event_notifier
.event
.loglevel_type
= event_notifier
->event
.loglevel_type
;
534 lum
.u
.event_notifier
.event
.loglevel
= event_notifier
->event
.loglevel
;
535 lum
.u
.event_notifier
.event
.token
= event_notifier
->event
.token
;
536 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
538 free(event_notifier_data
);
541 event_notifier_data
->handle
= lur
.ret_val
;
542 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
543 *_event_notifier_data
= event_notifier_data
;
548 int ustctl_tracepoint_list(int sock
)
550 struct ustcomm_ust_msg lum
;
551 struct ustcomm_ust_reply lur
;
552 int ret
, tp_list_handle
;
554 memset(&lum
, 0, sizeof(lum
));
555 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
556 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
557 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
560 tp_list_handle
= lur
.ret_val
;
561 DBG("received tracepoint list handle %u", tp_list_handle
);
562 return tp_list_handle
;
565 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
566 struct lttng_ust_tracepoint_iter
*iter
)
568 struct ustcomm_ust_msg lum
;
569 struct ustcomm_ust_reply lur
;
575 memset(&lum
, 0, sizeof(lum
));
576 lum
.handle
= tp_list_handle
;
577 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
578 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
581 DBG("received tracepoint list entry name %s loglevel %d",
582 lur
.u
.tracepoint
.name
,
583 lur
.u
.tracepoint
.loglevel
);
584 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
588 int ustctl_tracepoint_field_list(int sock
)
590 struct ustcomm_ust_msg lum
;
591 struct ustcomm_ust_reply lur
;
592 int ret
, tp_field_list_handle
;
594 memset(&lum
, 0, sizeof(lum
));
595 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
596 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
597 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
600 tp_field_list_handle
= lur
.ret_val
;
601 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
602 return tp_field_list_handle
;
605 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
606 struct lttng_ust_field_iter
*iter
)
608 struct ustcomm_ust_msg lum
;
609 struct ustcomm_ust_reply lur
;
616 memset(&lum
, 0, sizeof(lum
));
617 lum
.handle
= tp_field_list_handle
;
618 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
619 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
622 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
623 if (len
!= sizeof(*iter
)) {
626 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
634 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
636 struct ustcomm_ust_msg lum
;
637 struct ustcomm_ust_reply lur
;
643 memset(&lum
, 0, sizeof(lum
));
644 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
645 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
646 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
649 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
650 DBG("received tracer version");
654 int ustctl_wait_quiescent(int sock
)
656 struct ustcomm_ust_msg lum
;
657 struct ustcomm_ust_reply lur
;
660 memset(&lum
, 0, sizeof(lum
));
661 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
662 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
663 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
666 DBG("waited for quiescent state");
670 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
678 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
680 struct ustcomm_ust_msg lum
;
681 struct ustcomm_ust_reply lur
;
687 memset(&lum
, 0, sizeof(lum
));
688 lum
.handle
= object
->handle
;
689 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
690 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
693 DBG("flushed buffer handle %u", object
->handle
);
698 int ustctl_send_channel(int sock
,
699 enum lttng_ust_chan_type type
,
709 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
710 if (len
!= sizeof(size
)) {
717 /* Send channel type */
718 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
719 if (len
!= sizeof(type
)) {
727 /* Send channel data */
728 len
= ustcomm_send_unix_sock(sock
, data
, size
);
737 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
748 int ustctl_send_stream(int sock
,
750 uint64_t memory_map_size
,
751 int shm_fd
, int wakeup_fd
,
759 /* finish iteration */
762 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
763 if (len
!= sizeof(v
)) {
773 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
774 sizeof(memory_map_size
));
775 if (len
!= sizeof(memory_map_size
)) {
783 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
785 if (len
!= sizeof(stream_nr
)) {
793 /* Send shm fd and wakeup fd */
796 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
806 int ustctl_recv_channel_from_consumer(int sock
,
807 struct lttng_ust_object_data
**_channel_data
)
809 struct lttng_ust_object_data
*channel_data
;
814 channel_data
= zmalloc(sizeof(*channel_data
));
819 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
820 channel_data
->handle
= -1;
823 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
824 sizeof(channel_data
->size
));
825 if (len
!= sizeof(channel_data
->size
)) {
833 /* recv channel type */
834 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
835 sizeof(channel_data
->u
.channel
.type
));
836 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
844 /* recv channel data */
845 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
846 if (!channel_data
->u
.channel
.data
) {
850 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
852 if (len
!= channel_data
->size
) {
857 goto error_recv_data
;
860 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
864 goto error_recv_data
;
867 goto error_recv_data
;
870 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
871 *_channel_data
= channel_data
;
875 free(channel_data
->u
.channel
.data
);
882 int ustctl_recv_stream_from_consumer(int sock
,
883 struct lttng_ust_object_data
**_stream_data
)
885 struct lttng_ust_object_data
*stream_data
;
890 stream_data
= zmalloc(sizeof(*stream_data
));
896 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
897 stream_data
->handle
= -1;
900 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
901 sizeof(stream_data
->size
));
902 if (len
!= sizeof(stream_data
->size
)) {
909 if (stream_data
->size
== -1) {
910 ret
= -LTTNG_UST_ERR_NOENT
;
915 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
916 sizeof(stream_data
->u
.stream
.stream_nr
));
917 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
925 /* recv shm fd and wakeup fd */
926 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
936 stream_data
->u
.stream
.shm_fd
= fds
[0];
937 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
938 *_stream_data
= stream_data
;
947 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
948 struct lttng_ust_object_data
*channel_data
)
950 struct ustcomm_ust_msg lum
;
951 struct ustcomm_ust_reply lur
;
957 memset(&lum
, 0, sizeof(lum
));
958 lum
.handle
= session_handle
;
959 lum
.cmd
= LTTNG_UST_CHANNEL
;
960 lum
.u
.channel
.len
= channel_data
->size
;
961 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
962 ret
= ustcomm_send_app_msg(sock
, &lum
);
966 ret
= ustctl_send_channel(sock
,
967 channel_data
->u
.channel
.type
,
968 channel_data
->u
.channel
.data
,
970 channel_data
->u
.channel
.wakeup_fd
,
974 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
976 channel_data
->handle
= lur
.ret_val
;
981 int ustctl_send_stream_to_ust(int sock
,
982 struct lttng_ust_object_data
*channel_data
,
983 struct lttng_ust_object_data
*stream_data
)
985 struct ustcomm_ust_msg lum
;
986 struct ustcomm_ust_reply lur
;
989 memset(&lum
, 0, sizeof(lum
));
990 lum
.handle
= channel_data
->handle
;
991 lum
.cmd
= LTTNG_UST_STREAM
;
992 lum
.u
.stream
.len
= stream_data
->size
;
993 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
994 ret
= ustcomm_send_app_msg(sock
, &lum
);
999 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1001 ret
= ustctl_send_stream(sock
,
1002 stream_data
->u
.stream
.stream_nr
,
1004 stream_data
->u
.stream
.shm_fd
,
1005 stream_data
->u
.stream
.wakeup_fd
, 1);
1008 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1011 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1012 struct lttng_ust_object_data
*src
)
1014 struct lttng_ust_object_data
*obj
;
1017 if (src
->handle
!= -1) {
1022 obj
= zmalloc(sizeof(*obj
));
1028 obj
->type
= src
->type
;
1029 obj
->handle
= src
->handle
;
1030 obj
->size
= src
->size
;
1032 switch (obj
->type
) {
1033 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1035 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1036 if (src
->u
.channel
.wakeup_fd
>= 0) {
1037 obj
->u
.channel
.wakeup_fd
=
1038 dup(src
->u
.channel
.wakeup_fd
);
1039 if (obj
->u
.channel
.wakeup_fd
< 0) {
1041 goto chan_error_wakeup_fd
;
1044 obj
->u
.channel
.wakeup_fd
=
1045 src
->u
.channel
.wakeup_fd
;
1047 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1048 if (!obj
->u
.channel
.data
) {
1050 goto chan_error_alloc
;
1052 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1056 if (src
->u
.channel
.wakeup_fd
>= 0) {
1059 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1064 chan_error_wakeup_fd
:
1069 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1071 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1072 if (src
->u
.stream
.wakeup_fd
>= 0) {
1073 obj
->u
.stream
.wakeup_fd
=
1074 dup(src
->u
.stream
.wakeup_fd
);
1075 if (obj
->u
.stream
.wakeup_fd
< 0) {
1077 goto stream_error_wakeup_fd
;
1080 obj
->u
.stream
.wakeup_fd
=
1081 src
->u
.stream
.wakeup_fd
;
1084 if (src
->u
.stream
.shm_fd
>= 0) {
1085 obj
->u
.stream
.shm_fd
=
1086 dup(src
->u
.stream
.shm_fd
);
1087 if (obj
->u
.stream
.shm_fd
< 0) {
1089 goto stream_error_shm_fd
;
1092 obj
->u
.stream
.shm_fd
=
1093 src
->u
.stream
.shm_fd
;
1097 stream_error_shm_fd
:
1098 if (src
->u
.stream
.wakeup_fd
>= 0) {
1101 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1106 stream_error_wakeup_fd
:
1125 /* Buffer operations */
1127 int ustctl_get_nr_stream_per_channel(void)
1129 return num_possible_cpus();
1132 struct ustctl_consumer_channel
*
1133 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1134 const int *stream_fds
, int nr_stream_fds
)
1136 struct ustctl_consumer_channel
*chan
;
1137 const char *transport_name
;
1138 struct lttng_transport
*transport
;
1140 switch (attr
->type
) {
1141 case LTTNG_UST_CHAN_PER_CPU
:
1142 if (attr
->output
== LTTNG_UST_MMAP
) {
1143 if (attr
->overwrite
) {
1144 if (attr
->read_timer_interval
== 0) {
1145 transport_name
= "relay-overwrite-mmap";
1147 transport_name
= "relay-overwrite-rt-mmap";
1150 if (attr
->read_timer_interval
== 0) {
1151 transport_name
= "relay-discard-mmap";
1153 transport_name
= "relay-discard-rt-mmap";
1160 case LTTNG_UST_CHAN_METADATA
:
1161 if (attr
->output
== LTTNG_UST_MMAP
)
1162 transport_name
= "relay-metadata-mmap";
1167 transport_name
= "<unknown>";
1171 transport
= lttng_transport_find(transport_name
);
1173 DBG("LTTng transport %s not found\n",
1178 chan
= zmalloc(sizeof(*chan
));
1182 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1183 attr
->subbuf_size
, attr
->num_subbuf
,
1184 attr
->switch_timer_interval
,
1185 attr
->read_timer_interval
,
1186 attr
->uuid
, attr
->chan_id
,
1187 stream_fds
, nr_stream_fds
,
1188 attr
->blocking_timeout
);
1192 chan
->chan
->ops
= &transport
->ops
;
1193 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1194 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1195 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1203 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1205 (void) ustctl_channel_close_wait_fd(chan
);
1206 (void) ustctl_channel_close_wakeup_fd(chan
);
1207 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1211 int ustctl_send_channel_to_sessiond(int sock
,
1212 struct ustctl_consumer_channel
*channel
)
1214 struct shm_object_table
*table
;
1216 table
= channel
->chan
->handle
->table
;
1217 if (table
->size
<= 0)
1219 return ustctl_send_channel(sock
,
1221 table
->objects
[0].memory_map
,
1222 table
->objects
[0].memory_map_size
,
1227 int ustctl_send_stream_to_sessiond(int sock
,
1228 struct ustctl_consumer_stream
*stream
)
1231 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1233 return ustctl_send_stream(sock
,
1235 stream
->memory_map_size
,
1236 stream
->shm_fd
, stream
->wakeup_fd
,
1240 int ustctl_write_metadata_to_channel(
1241 struct ustctl_consumer_channel
*channel
,
1242 const char *metadata_str
, /* NOT null-terminated */
1243 size_t len
) /* metadata length */
1245 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1246 struct lttng_channel
*chan
= channel
->chan
;
1247 const char *str
= metadata_str
;
1248 int ret
= 0, waitret
;
1249 size_t reserve_len
, pos
;
1251 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1252 reserve_len
= min_t(size_t,
1253 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1255 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1256 sizeof(char), -1, chan
->handle
, NULL
);
1258 * We don't care about metadata buffer's records lost
1259 * count, because we always retry here. Report error if
1260 * we need to bail out after timeout or being
1263 waitret
= wait_cond_interruptible_timeout(
1265 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1266 ret
!= -ENOBUFS
|| !ret
;
1268 LTTNG_METADATA_TIMEOUT_MSEC
);
1269 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1270 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1271 waitret
== -EINTR
? "interrupted" :
1272 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1273 if (waitret
== -EINTR
)
1277 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1278 chan
->ops
->event_commit(&ctx
);
1285 * Write at most one packet in the channel.
1286 * Returns the number of bytes written on success, < 0 on error.
1288 ssize_t
ustctl_write_one_packet_to_channel(
1289 struct ustctl_consumer_channel
*channel
,
1290 const char *metadata_str
, /* NOT null-terminated */
1291 size_t len
) /* metadata length */
1293 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1294 struct lttng_channel
*chan
= channel
->chan
;
1295 const char *str
= metadata_str
;
1296 ssize_t reserve_len
;
1299 reserve_len
= min_t(ssize_t
,
1300 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1302 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1303 sizeof(char), -1, chan
->handle
, NULL
);
1304 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1306 DBG("LTTng: event reservation failed");
1311 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1312 chan
->ops
->event_commit(&ctx
);
1318 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1320 struct channel
*chan
;
1323 chan
= consumer_chan
->chan
->chan
;
1324 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1325 chan
, chan
->handle
);
1327 consumer_chan
->wait_fd
= -1;
1331 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1333 struct channel
*chan
;
1336 chan
= consumer_chan
->chan
->chan
;
1337 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1338 chan
, chan
->handle
);
1340 consumer_chan
->wakeup_fd
= -1;
1344 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1346 struct channel
*chan
;
1348 chan
= stream
->chan
->chan
->chan
;
1349 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1350 chan
, stream
->handle
, stream
->cpu
);
1353 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1355 struct channel
*chan
;
1357 chan
= stream
->chan
->chan
->chan
;
1358 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1359 chan
, stream
->handle
, stream
->cpu
);
1362 struct ustctl_consumer_stream
*
1363 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1366 struct ustctl_consumer_stream
*stream
;
1367 struct lttng_ust_shm_handle
*handle
;
1368 struct channel
*chan
;
1369 int shm_fd
, wait_fd
, wakeup_fd
;
1370 uint64_t memory_map_size
;
1371 struct lttng_ust_lib_ring_buffer
*buf
;
1376 handle
= channel
->chan
->handle
;
1380 chan
= channel
->chan
->chan
;
1381 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1382 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1383 &wakeup_fd
, &memory_map_size
);
1386 ret
= lib_ring_buffer_open_read(buf
, handle
);
1390 stream
= zmalloc(sizeof(*stream
));
1393 stream
->handle
= handle
;
1395 stream
->chan
= channel
;
1396 stream
->shm_fd
= shm_fd
;
1397 stream
->wait_fd
= wait_fd
;
1398 stream
->wakeup_fd
= wakeup_fd
;
1399 stream
->memory_map_size
= memory_map_size
;
1407 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1409 struct lttng_ust_lib_ring_buffer
*buf
;
1410 struct ustctl_consumer_channel
*consumer_chan
;
1414 consumer_chan
= stream
->chan
;
1415 (void) ustctl_stream_close_wait_fd(stream
);
1416 (void) ustctl_stream_close_wakeup_fd(stream
);
1417 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1421 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1425 return shm_get_wait_fd(chan
->chan
->handle
,
1426 &chan
->chan
->handle
->chan
._ref
);
1429 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1433 return shm_get_wakeup_fd(chan
->chan
->handle
,
1434 &chan
->chan
->handle
->chan
._ref
);
1437 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1439 struct lttng_ust_lib_ring_buffer
*buf
;
1440 struct ustctl_consumer_channel
*consumer_chan
;
1445 consumer_chan
= stream
->chan
;
1446 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1449 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1451 struct lttng_ust_lib_ring_buffer
*buf
;
1452 struct ustctl_consumer_channel
*consumer_chan
;
1457 consumer_chan
= stream
->chan
;
1458 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1461 /* For mmap mode, readable without "get" operation */
1463 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1465 struct lttng_ust_lib_ring_buffer
*buf
;
1466 struct ustctl_consumer_channel
*consumer_chan
;
1471 consumer_chan
= stream
->chan
;
1472 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1475 /* returns the length to mmap. */
1476 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1479 struct ustctl_consumer_channel
*consumer_chan
;
1480 unsigned long mmap_buf_len
;
1481 struct channel
*chan
;
1485 consumer_chan
= stream
->chan
;
1486 chan
= consumer_chan
->chan
->chan
;
1487 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1489 mmap_buf_len
= chan
->backend
.buf_size
;
1490 if (chan
->backend
.extra_reader_sb
)
1491 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1492 if (mmap_buf_len
> INT_MAX
)
1494 *len
= mmap_buf_len
;
1498 /* returns the maximum size for sub-buffers. */
1499 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1502 struct ustctl_consumer_channel
*consumer_chan
;
1503 struct channel
*chan
;
1507 consumer_chan
= stream
->chan
;
1508 chan
= consumer_chan
->chan
->chan
;
1509 *len
= chan
->backend
.subbuf_size
;
1514 * For mmap mode, operate on the current packet (between get/put or
1515 * get_next/put_next).
1518 /* returns the offset of the subbuffer belonging to the mmap reader. */
1519 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1522 struct channel
*chan
;
1523 unsigned long sb_bindex
;
1524 struct lttng_ust_lib_ring_buffer
*buf
;
1525 struct ustctl_consumer_channel
*consumer_chan
;
1526 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1527 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1532 consumer_chan
= stream
->chan
;
1533 chan
= consumer_chan
->chan
->chan
;
1534 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1536 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1537 buf
->backend
.buf_rsb
.id
);
1538 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1542 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1545 *off
= pages
->mmap_offset
;
1549 /* returns the size of the current sub-buffer, without padding (for mmap). */
1550 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1553 struct ustctl_consumer_channel
*consumer_chan
;
1554 struct channel
*chan
;
1555 struct lttng_ust_lib_ring_buffer
*buf
;
1561 consumer_chan
= stream
->chan
;
1562 chan
= consumer_chan
->chan
->chan
;
1563 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1564 consumer_chan
->chan
->handle
);
1568 /* returns the size of the current sub-buffer, without padding (for mmap). */
1569 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1572 struct ustctl_consumer_channel
*consumer_chan
;
1573 struct channel
*chan
;
1574 struct lttng_ust_lib_ring_buffer
*buf
;
1579 consumer_chan
= stream
->chan
;
1580 chan
= consumer_chan
->chan
->chan
;
1581 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1582 consumer_chan
->chan
->handle
);
1583 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1587 /* Get exclusive read access to the next sub-buffer that can be read. */
1588 int ustctl_get_next_subbuf(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_get_next_subbuf(buf
,
1598 consumer_chan
->chan
->handle
);
1602 /* Release exclusive sub-buffer access, move consumer forward. */
1603 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1605 struct lttng_ust_lib_ring_buffer
*buf
;
1606 struct ustctl_consumer_channel
*consumer_chan
;
1611 consumer_chan
= stream
->chan
;
1612 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1618 /* Get a snapshot of the current ring buffer producer and consumer positions */
1619 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1621 struct lttng_ust_lib_ring_buffer
*buf
;
1622 struct ustctl_consumer_channel
*consumer_chan
;
1627 consumer_chan
= stream
->chan
;
1628 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1629 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1633 * Get a snapshot of the current ring buffer producer and consumer positions
1634 * even if the consumed and produced positions are contained within the same
1637 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1639 struct lttng_ust_lib_ring_buffer
*buf
;
1640 struct ustctl_consumer_channel
*consumer_chan
;
1645 consumer_chan
= stream
->chan
;
1646 return lib_ring_buffer_snapshot_sample_positions(buf
,
1647 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1648 consumer_chan
->chan
->handle
);
1651 /* Get the consumer position (iteration start) */
1652 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1655 struct lttng_ust_lib_ring_buffer
*buf
;
1660 *pos
= buf
->cons_snapshot
;
1664 /* Get the producer position (iteration end) */
1665 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1668 struct lttng_ust_lib_ring_buffer
*buf
;
1673 *pos
= buf
->prod_snapshot
;
1677 /* Get exclusive read access to the specified sub-buffer position */
1678 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1681 struct lttng_ust_lib_ring_buffer
*buf
;
1682 struct ustctl_consumer_channel
*consumer_chan
;
1687 consumer_chan
= stream
->chan
;
1688 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1689 consumer_chan
->chan
->handle
);
1692 /* Release exclusive sub-buffer access */
1693 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1695 struct lttng_ust_lib_ring_buffer
*buf
;
1696 struct ustctl_consumer_channel
*consumer_chan
;
1701 consumer_chan
= stream
->chan
;
1702 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1706 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1707 int producer_active
)
1709 struct lttng_ust_lib_ring_buffer
*buf
;
1710 struct ustctl_consumer_channel
*consumer_chan
;
1714 consumer_chan
= stream
->chan
;
1715 lib_ring_buffer_switch_slow(buf
,
1716 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1717 consumer_chan
->chan
->handle
);
1720 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1722 struct lttng_ust_lib_ring_buffer
*buf
;
1723 struct ustctl_consumer_channel
*consumer_chan
;
1727 consumer_chan
= stream
->chan
;
1728 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1729 consumer_chan
->chan
->handle
);
1730 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1734 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1735 struct lttng_ust_lib_ring_buffer
*buf
,
1736 struct lttng_ust_shm_handle
*handle
)
1738 struct channel
*chan
;
1739 const struct lttng_ust_lib_ring_buffer_config
*config
;
1740 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1742 chan
= shmp(handle
, buf
->backend
.chan
);
1745 config
= &chan
->backend
.config
;
1746 if (!config
->cb_ptr
)
1748 client_cb
= caa_container_of(config
->cb_ptr
,
1749 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1754 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1755 uint64_t *timestamp_begin
)
1757 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1758 struct lttng_ust_lib_ring_buffer
*buf
;
1759 struct lttng_ust_shm_handle
*handle
;
1761 if (!stream
|| !timestamp_begin
)
1764 handle
= stream
->chan
->chan
->handle
;
1765 client_cb
= get_client_cb(buf
, handle
);
1768 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1771 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1772 uint64_t *timestamp_end
)
1774 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1775 struct lttng_ust_lib_ring_buffer
*buf
;
1776 struct lttng_ust_shm_handle
*handle
;
1778 if (!stream
|| !timestamp_end
)
1781 handle
= stream
->chan
->chan
->handle
;
1782 client_cb
= get_client_cb(buf
, handle
);
1785 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1788 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1789 uint64_t *events_discarded
)
1791 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1792 struct lttng_ust_lib_ring_buffer
*buf
;
1793 struct lttng_ust_shm_handle
*handle
;
1795 if (!stream
|| !events_discarded
)
1798 handle
= stream
->chan
->chan
->handle
;
1799 client_cb
= get_client_cb(buf
, handle
);
1802 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1805 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1806 uint64_t *content_size
)
1808 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1809 struct lttng_ust_lib_ring_buffer
*buf
;
1810 struct lttng_ust_shm_handle
*handle
;
1812 if (!stream
|| !content_size
)
1815 handle
= stream
->chan
->chan
->handle
;
1816 client_cb
= get_client_cb(buf
, handle
);
1819 return client_cb
->content_size(buf
, handle
, content_size
);
1822 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1823 uint64_t *packet_size
)
1825 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1826 struct lttng_ust_lib_ring_buffer
*buf
;
1827 struct lttng_ust_shm_handle
*handle
;
1829 if (!stream
|| !packet_size
)
1832 handle
= stream
->chan
->chan
->handle
;
1833 client_cb
= get_client_cb(buf
, handle
);
1836 return client_cb
->packet_size(buf
, handle
, packet_size
);
1839 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1840 uint64_t *stream_id
)
1842 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1843 struct lttng_ust_lib_ring_buffer
*buf
;
1844 struct lttng_ust_shm_handle
*handle
;
1846 if (!stream
|| !stream_id
)
1849 handle
= stream
->chan
->chan
->handle
;
1850 client_cb
= get_client_cb(buf
, handle
);
1853 return client_cb
->stream_id(buf
, handle
, stream_id
);
1856 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1859 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1860 struct lttng_ust_lib_ring_buffer
*buf
;
1861 struct lttng_ust_shm_handle
*handle
;
1866 handle
= stream
->chan
->chan
->handle
;
1867 client_cb
= get_client_cb(buf
, handle
);
1868 if (!client_cb
|| !client_cb
->current_timestamp
)
1870 return client_cb
->current_timestamp(buf
, handle
, ts
);
1873 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1876 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1877 struct lttng_ust_lib_ring_buffer
*buf
;
1878 struct lttng_ust_shm_handle
*handle
;
1880 if (!stream
|| !seq
)
1883 handle
= stream
->chan
->chan
->handle
;
1884 client_cb
= get_client_cb(buf
, handle
);
1885 if (!client_cb
|| !client_cb
->sequence_number
)
1887 return client_cb
->sequence_number(buf
, handle
, seq
);
1890 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1893 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1894 struct lttng_ust_lib_ring_buffer
*buf
;
1895 struct lttng_ust_shm_handle
*handle
;
1900 handle
= stream
->chan
->chan
->handle
;
1901 client_cb
= get_client_cb(buf
, handle
);
1904 return client_cb
->instance_id(buf
, handle
, id
);
1907 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1909 int ustctl_has_perf_counters(void)
1916 int ustctl_has_perf_counters(void)
1924 * Returns 0 on success, negative error value on error.
1926 int ustctl_recv_reg_msg(int sock
,
1927 enum ustctl_socket_type
*type
,
1934 uint32_t *bits_per_long
,
1935 uint32_t *uint8_t_alignment
,
1936 uint32_t *uint16_t_alignment
,
1937 uint32_t *uint32_t_alignment
,
1938 uint32_t *uint64_t_alignment
,
1939 uint32_t *long_alignment
,
1944 struct ustctl_reg_msg reg_msg
;
1946 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
1947 if (len
> 0 && len
!= sizeof(reg_msg
))
1954 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
1955 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1956 BIG_ENDIAN
: LITTLE_ENDIAN
;
1957 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
1958 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
1959 LITTLE_ENDIAN
: BIG_ENDIAN
;
1961 return -LTTNG_UST_ERR_INVAL_MAGIC
;
1963 switch (reg_msg
.socket_type
) {
1964 case 0: *type
= USTCTL_SOCKET_CMD
;
1966 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
1969 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
1971 *major
= reg_msg
.major
;
1972 *minor
= reg_msg
.minor
;
1974 *ppid
= reg_msg
.ppid
;
1977 *bits_per_long
= reg_msg
.bits_per_long
;
1978 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
1979 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
1980 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
1981 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
1982 *long_alignment
= reg_msg
.long_alignment
;
1983 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
1984 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
1985 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
1986 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
1992 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
1994 struct ustcomm_notify_hdr header
;
1997 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
1998 if (len
> 0 && len
!= sizeof(header
))
2004 switch (header
.notify_cmd
) {
2006 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2009 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2012 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2021 * Returns 0 on success, negative error value on error.
2023 int ustctl_recv_register_event(int sock
,
2030 struct ustctl_field
**fields
,
2031 char **model_emf_uri
)
2034 struct ustcomm_notify_event_msg msg
;
2035 size_t signature_len
, fields_len
, model_emf_uri_len
;
2036 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2037 struct ustctl_field
*a_fields
= NULL
;
2039 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2040 if (len
> 0 && len
!= sizeof(msg
))
2047 *session_objd
= msg
.session_objd
;
2048 *channel_objd
= msg
.channel_objd
;
2049 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2050 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2051 *loglevel
= msg
.loglevel
;
2052 signature_len
= msg
.signature_len
;
2053 fields_len
= msg
.fields_len
;
2055 if (fields_len
% sizeof(*a_fields
) != 0) {
2059 model_emf_uri_len
= msg
.model_emf_uri_len
;
2061 /* recv signature. contains at least \0. */
2062 a_sign
= zmalloc(signature_len
);
2065 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2066 if (len
> 0 && len
!= signature_len
) {
2068 goto signature_error
;
2072 goto signature_error
;
2075 goto signature_error
;
2077 /* Enforce end of string */
2078 a_sign
[signature_len
- 1] = '\0';
2082 a_fields
= zmalloc(fields_len
);
2085 goto signature_error
;
2087 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2088 if (len
> 0 && len
!= fields_len
) {
2101 if (model_emf_uri_len
) {
2102 /* recv model_emf_uri_len */
2103 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2104 if (!a_model_emf_uri
) {
2108 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2110 if (len
> 0 && len
!= model_emf_uri_len
) {
2121 /* Enforce end of string */
2122 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2125 *signature
= a_sign
;
2126 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2128 *model_emf_uri
= a_model_emf_uri
;
2133 free(a_model_emf_uri
);
2142 * Returns 0 on success, negative error value on error.
2144 int ustctl_reply_register_event(int sock
,
2150 struct ustcomm_notify_hdr header
;
2151 struct ustcomm_notify_event_reply r
;
2154 memset(&reply
, 0, sizeof(reply
));
2155 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2156 reply
.r
.ret_code
= ret_code
;
2157 reply
.r
.event_id
= id
;
2158 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2159 if (len
> 0 && len
!= sizeof(reply
))
2167 * Returns 0 on success, negative UST or system error value on error.
2169 int ustctl_recv_register_enum(int sock
,
2172 struct ustctl_enum_entry
**entries
,
2176 struct ustcomm_notify_enum_msg msg
;
2178 struct ustctl_enum_entry
*a_entries
= NULL
;
2180 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2181 if (len
> 0 && len
!= sizeof(msg
))
2188 *session_objd
= msg
.session_objd
;
2189 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2190 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2191 entries_len
= msg
.entries_len
;
2193 if (entries_len
% sizeof(*a_entries
) != 0) {
2199 a_entries
= zmalloc(entries_len
);
2202 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2203 if (len
> 0 && len
!= entries_len
) {
2215 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2216 *entries
= a_entries
;
2226 * Returns 0 on success, negative error value on error.
2228 int ustctl_reply_register_enum(int sock
,
2234 struct ustcomm_notify_hdr header
;
2235 struct ustcomm_notify_enum_reply r
;
2238 memset(&reply
, 0, sizeof(reply
));
2239 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2240 reply
.r
.ret_code
= ret_code
;
2241 reply
.r
.enum_id
= id
;
2242 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2243 if (len
> 0 && len
!= sizeof(reply
))
2251 * Returns 0 on success, negative UST or system error value on error.
2253 int ustctl_recv_register_channel(int sock
,
2254 int *session_objd
, /* session descriptor (output) */
2255 int *channel_objd
, /* channel descriptor (output) */
2257 struct ustctl_field
**fields
)
2260 struct ustcomm_notify_channel_msg msg
;
2262 struct ustctl_field
*a_fields
;
2264 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2265 if (len
> 0 && len
!= sizeof(msg
))
2272 *session_objd
= msg
.session_objd
;
2273 *channel_objd
= msg
.channel_objd
;
2274 fields_len
= msg
.ctx_fields_len
;
2276 if (fields_len
% sizeof(*a_fields
) != 0) {
2282 a_fields
= zmalloc(fields_len
);
2287 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2288 if (len
> 0 && len
!= fields_len
) {
2303 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2313 * Returns 0 on success, negative error value on error.
2315 int ustctl_reply_register_channel(int sock
,
2317 enum ustctl_channel_header header_type
,
2322 struct ustcomm_notify_hdr header
;
2323 struct ustcomm_notify_channel_reply r
;
2326 memset(&reply
, 0, sizeof(reply
));
2327 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2328 reply
.r
.ret_code
= ret_code
;
2329 reply
.r
.chan_id
= chan_id
;
2330 switch (header_type
) {
2331 case USTCTL_CHANNEL_HEADER_COMPACT
:
2332 reply
.r
.header_type
= 1;
2334 case USTCTL_CHANNEL_HEADER_LARGE
:
2335 reply
.r
.header_type
= 2;
2338 reply
.r
.header_type
= 0;
2341 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2342 if (len
> 0 && len
!= sizeof(reply
))
2349 /* Regenerate the statedump. */
2350 int ustctl_regenerate_statedump(int sock
, int handle
)
2352 struct ustcomm_ust_msg lum
;
2353 struct ustcomm_ust_reply lur
;
2356 memset(&lum
, 0, sizeof(lum
));
2357 lum
.handle
= handle
;
2358 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2359 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2362 DBG("Regenerated statedump for handle %u", handle
);
2366 static __attribute__((constructor
))
2367 void ustctl_init(void)
2370 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2371 lttng_ust_clock_init();
2372 lttng_ring_buffer_metadata_client_init();
2373 lttng_ring_buffer_client_overwrite_init();
2374 lttng_ring_buffer_client_overwrite_rt_init();
2375 lttng_ring_buffer_client_discard_init();
2376 lttng_ring_buffer_client_discard_rt_init();
2377 lib_ringbuffer_signal_init();
2380 static __attribute__((destructor
))
2381 void ustctl_exit(void)
2383 lttng_ring_buffer_client_discard_rt_exit();
2384 lttng_ring_buffer_client_discard_exit();
2385 lttng_ring_buffer_client_overwrite_rt_exit();
2386 lttng_ring_buffer_client_overwrite_exit();
2387 lttng_ring_buffer_metadata_client_exit();