2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <lttng/ust-ctl.h>
22 #include <lttng/ust-abi.h>
23 #include <lttng/ust-events.h>
26 #include <usterr-signal-safe.h>
29 #include "../libringbuffer/backend.h"
30 #include "../libringbuffer/frontend.h"
32 volatile enum ust_loglevel ust_loglevel
;
35 void init_object(struct lttng_ust_object_data
*data
)
40 data
->memory_map_size
= 0;
43 int ustctl_release_handle(int sock
, int handle
)
45 struct ustcomm_ust_msg lum
;
46 struct ustcomm_ust_reply lur
;
50 memset(&lum
, 0, sizeof(lum
));
52 lum
.cmd
= LTTNG_UST_RELEASE
;
53 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
61 * If sock is negative, it means we don't have to notify the other side
62 * (e.g. application has already vanished).
64 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
68 if (data
->shm_fd
>= 0) {
69 ret
= close(data
->shm_fd
);
74 if (data
->wait_fd
>= 0) {
75 ret
= close(data
->wait_fd
);
80 return ustctl_release_handle(sock
, data
->handle
);
84 * Send registration done packet to the application.
86 int ustctl_register_done(int sock
)
88 struct ustcomm_ust_msg lum
;
89 struct ustcomm_ust_reply lur
;
92 DBG("Sending register done command to %d", sock
);
93 memset(&lum
, 0, sizeof(lum
));
94 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
95 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
96 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
99 if (lur
.ret_code
!= USTCOMM_OK
) {
100 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
110 * returns session handle.
112 int ustctl_create_session(int sock
)
114 struct ustcomm_ust_msg lum
;
115 struct ustcomm_ust_reply lur
;
116 int ret
, session_handle
;
119 memset(&lum
, 0, sizeof(lum
));
120 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
121 lum
.cmd
= LTTNG_UST_SESSION
;
122 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
125 session_handle
= lur
.ret_val
;
126 DBG("received session handle %u", session_handle
);
127 return session_handle
;
130 /* open the metadata global channel */
131 int ustctl_open_metadata(int sock
, int session_handle
,
132 struct lttng_ust_channel_attr
*chops
,
133 struct lttng_ust_object_data
**_metadata_data
)
135 struct ustcomm_ust_msg lum
;
136 struct ustcomm_ust_reply lur
;
137 struct lttng_ust_object_data
*metadata_data
;
140 metadata_data
= malloc(sizeof(*metadata_data
));
143 init_object(metadata_data
);
144 /* Create metadata channel */
145 memset(&lum
, 0, sizeof(lum
));
146 lum
.handle
= session_handle
;
147 lum
.cmd
= LTTNG_UST_METADATA
;
148 lum
.u
.channel
.overwrite
= chops
->overwrite
;
149 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
150 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
151 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
152 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
153 lum
.u
.channel
.output
= chops
->output
;
154 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
159 if (lur
.ret_code
!= USTCOMM_OK
) {
163 metadata_data
->handle
= lur
.ret_val
;
164 DBG("received metadata handle %u", metadata_data
->handle
);
165 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
167 ret
= ustcomm_recv_fd(sock
);
171 metadata_data
->shm_fd
= ret
;
173 * We need to get the second FD even if the first fails, because
174 * libust expects us to read the two FDs.
177 ret
= ustcomm_recv_fd(sock
);
181 metadata_data
->wait_fd
= ret
;
184 *_metadata_data
= metadata_data
;
188 (void) ustctl_release_object(sock
, metadata_data
);
193 int ustctl_create_channel(int sock
, int session_handle
,
194 struct lttng_ust_channel_attr
*chops
,
195 struct lttng_ust_object_data
**_channel_data
)
197 struct ustcomm_ust_msg lum
;
198 struct ustcomm_ust_reply lur
;
199 struct lttng_ust_object_data
*channel_data
;
202 channel_data
= malloc(sizeof(*channel_data
));
205 init_object(channel_data
);
206 /* Create metadata channel */
207 memset(&lum
, 0, sizeof(lum
));
208 lum
.handle
= session_handle
;
209 lum
.cmd
= LTTNG_UST_CHANNEL
;
210 lum
.u
.channel
.overwrite
= chops
->overwrite
;
211 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
212 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
213 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
214 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
215 lum
.u
.channel
.output
= chops
->output
;
216 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
221 if (lur
.ret_code
!= USTCOMM_OK
) {
225 channel_data
->handle
= lur
.ret_val
;
226 DBG("received channel handle %u", channel_data
->handle
);
227 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
229 ret
= ustcomm_recv_fd(sock
);
233 channel_data
->shm_fd
= ret
;
235 * We need to get the second FD even if the first fails, because
236 * libust expects us to read the two FDs.
239 ret
= ustcomm_recv_fd(sock
);
243 channel_data
->wait_fd
= ret
;
246 *_channel_data
= channel_data
;
250 (void) ustctl_release_object(sock
, channel_data
);
256 * Return -ENOENT if no more stream is available for creation.
257 * Return 0 on success.
258 * Return negative error value on error.
260 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
261 struct lttng_ust_object_data
**_stream_data
)
263 struct ustcomm_ust_msg lum
;
264 struct ustcomm_ust_reply lur
;
265 struct lttng_ust_object_data
*stream_data
;
266 int ret
, fd
, err
= 0;
268 stream_data
= malloc(sizeof(*stream_data
));
271 init_object(stream_data
);
272 memset(&lum
, 0, sizeof(lum
));
273 lum
.handle
= channel_data
->handle
;
274 lum
.cmd
= LTTNG_UST_STREAM
;
275 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
280 if (lur
.ret_code
!= USTCOMM_OK
) {
285 stream_data
->handle
= lur
.ret_val
;
286 DBG("received stream handle %u", stream_data
->handle
);
287 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
289 fd
= ustcomm_recv_fd(sock
);
293 stream_data
->shm_fd
= fd
;
295 * We need to get the second FD even if the first fails, because
296 * libust expects us to read the two FDs.
299 fd
= ustcomm_recv_fd(sock
);
303 stream_data
->wait_fd
= fd
;
306 *_stream_data
= stream_data
;
310 (void) ustctl_release_object(sock
, stream_data
);
315 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
316 struct lttng_ust_object_data
*channel_data
,
317 struct lttng_ust_object_data
**_event_data
)
319 struct ustcomm_ust_msg lum
;
320 struct ustcomm_ust_reply lur
;
321 struct lttng_ust_object_data
*event_data
;
324 event_data
= malloc(sizeof(*event_data
));
327 init_object(event_data
);
328 memset(&lum
, 0, sizeof(lum
));
329 lum
.handle
= channel_data
->handle
;
330 lum
.cmd
= LTTNG_UST_EVENT
;
331 strncpy(lum
.u
.event
.name
, ev
->name
,
332 LTTNG_UST_SYM_NAME_LEN
);
333 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
334 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
339 event_data
->handle
= lur
.ret_val
;
340 DBG("received event handle %u", event_data
->handle
);
341 *_event_data
= event_data
;
345 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
346 struct lttng_ust_object_data
*obj_data
,
347 struct lttng_ust_object_data
**_context_data
)
349 struct ustcomm_ust_msg lum
;
350 struct ustcomm_ust_reply lur
;
351 struct lttng_ust_object_data
*context_data
;
354 context_data
= malloc(sizeof(*context_data
));
357 init_object(context_data
);
358 memset(&lum
, 0, sizeof(lum
));
359 lum
.handle
= obj_data
->handle
;
360 lum
.cmd
= LTTNG_UST_CONTEXT
;
361 lum
.u
.context
.ctx
= ctx
->ctx
;
362 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
367 context_data
->handle
= lur
.ret_val
;
368 DBG("received context handle %u", context_data
->handle
);
369 *_context_data
= context_data
;
373 /* Enable event, channel and session ioctl */
374 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
376 struct ustcomm_ust_msg lum
;
377 struct ustcomm_ust_reply lur
;
380 memset(&lum
, 0, sizeof(lum
));
381 lum
.handle
= object
->handle
;
382 lum
.cmd
= LTTNG_UST_ENABLE
;
383 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
386 DBG("enabled handle %u", object
->handle
);
390 /* Disable event, channel and session ioctl */
391 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
393 struct ustcomm_ust_msg lum
;
394 struct ustcomm_ust_reply lur
;
397 memset(&lum
, 0, sizeof(lum
));
398 lum
.handle
= object
->handle
;
399 lum
.cmd
= LTTNG_UST_DISABLE
;
400 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
403 DBG("disable handle %u", object
->handle
);
407 int ustctl_start_session(int sock
, int handle
)
409 struct lttng_ust_object_data obj
;
412 return ustctl_enable(sock
, &obj
);
415 int ustctl_stop_session(int sock
, int handle
)
417 struct lttng_ust_object_data obj
;
420 return ustctl_disable(sock
, &obj
);
423 int ustctl_tracepoint_list(int sock
)
425 struct ustcomm_ust_msg lum
;
426 struct ustcomm_ust_reply lur
;
427 int ret
, tp_list_handle
;
429 memset(&lum
, 0, sizeof(lum
));
430 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
431 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
432 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
435 tp_list_handle
= lur
.ret_val
;
436 DBG("received tracepoint list handle %u", tp_list_handle
);
437 return tp_list_handle
;
440 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
441 struct lttng_ust_tracepoint_iter
*iter
)
443 struct ustcomm_ust_msg lum
;
444 struct ustcomm_ust_reply lur
;
447 memset(&lum
, 0, sizeof(lum
));
448 lum
.handle
= tp_list_handle
;
449 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
450 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
453 DBG("received tracepoint list entry name %s loglevel %d",
454 lur
.u
.tracepoint
.name
,
455 lur
.u
.tracepoint
.loglevel
);
456 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
460 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
462 struct ustcomm_ust_msg lum
;
463 struct ustcomm_ust_reply lur
;
466 memset(&lum
, 0, sizeof(lum
));
467 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
468 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
469 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
472 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
473 DBG("received tracer version");
477 int ustctl_wait_quiescent(int sock
)
479 struct ustcomm_ust_msg lum
;
480 struct ustcomm_ust_reply lur
;
483 memset(&lum
, 0, sizeof(lum
));
484 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
485 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
486 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
489 DBG("waited for quiescent state");
493 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
498 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
500 struct ustcomm_ust_msg lum
;
501 struct ustcomm_ust_reply lur
;
504 memset(&lum
, 0, sizeof(lum
));
505 lum
.handle
= object
->handle
;
506 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
507 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
510 DBG("flushed buffer handle %u", object
->handle
);
514 /* Buffer operations */
516 /* Map channel shm into process memory */
517 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
519 struct lttng_ust_shm_handle
*handle
;
520 struct channel
*chan
;
522 struct lttng_ust_lib_ring_buffer_config
*config
;
525 handle
= channel_handle_create(chan_data
->shm_fd
,
527 chan_data
->memory_map_size
);
529 ERR("create handle error");
533 * Set to -1, and then close the shm fd, and set the handle shm
534 * fd to -1 too. We don't need the shm fds after they have been
536 * The wait_fd is set to -1 in chan_data because it is now owned
539 chan_data
->shm_fd
= -1;
540 chan_data
->wait_fd
= -1;
542 /* chan is object 0. This is hardcoded. */
543 if (handle
->table
->objects
[0].shm_fd
>= 0) {
544 ret
= close(handle
->table
->objects
[0].shm_fd
);
546 perror("Error closing shm_fd");
548 handle
->table
->objects
[0].shm_fd
= -1;
552 * TODO: add consistency checks to be resilient if the
553 * application try to feed us with incoherent channel structure
556 chan
= shmp(handle
, handle
->chan
);
557 /* chan is object 0. This is hardcoded. */
558 chan_size
= handle
->table
->objects
[0].allocated_len
;
559 handle
->shadow_chan
= malloc(chan_size
);
560 if (!handle
->shadow_chan
) {
561 channel_destroy(chan
, handle
, 1);
564 memcpy(handle
->shadow_chan
, chan
, chan_size
);
566 * The callback pointers in the producer are invalid in the
567 * consumer. We need to look them up here.
569 config
= &handle
->shadow_chan
->backend
.config
;
570 switch (config
->client_type
) {
571 case LTTNG_CLIENT_METADATA
:
572 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
575 case LTTNG_CLIENT_DISCARD
:
576 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
579 case LTTNG_CLIENT_OVERWRITE
:
580 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
584 ERR("Unknown client type %d", config
->client_type
);
585 channel_destroy(chan
, handle
, 1);
588 /* Replace the object table pointer. */
589 ret
= munmap(handle
->table
->objects
[0].memory_map
,
590 handle
->table
->objects
[0].memory_map_size
);
595 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
596 handle
->table
->objects
[0].is_shadow
= 1;
600 /* Add stream to channel shm and map its shm into process memory */
601 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
602 struct lttng_ust_object_data
*stream_data
)
606 if (!stream_data
->handle
)
609 ret
= channel_handle_add_stream(handle
,
611 stream_data
->wait_fd
,
612 stream_data
->memory_map_size
);
614 ERR("add stream error\n");
618 * Set to -1 because the lttng_ust_shm_handle destruction will take care
619 * of closing shm_fd and wait_fd.
621 stream_data
->shm_fd
= -1;
622 stream_data
->wait_fd
= -1;
626 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
628 struct channel
*chan
;
630 chan
= shmp(handle
, handle
->chan
);
631 channel_destroy(chan
, handle
, 1);
635 * ustctl closes the shm_fd fds after mapping it.
637 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
640 struct channel
*chan
= handle
->shadow_chan
;
641 int *shm_fd
, *wait_fd
;
642 uint64_t *memory_map_size
;
643 struct lttng_ust_lib_ring_buffer
*buf
;
646 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
647 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
650 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
654 * We can close shm_fd early, right after is has been mapped.
657 ret
= close(*shm_fd
);
659 perror("Error closing shm_fd");
666 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
667 struct lttng_ust_lib_ring_buffer
*buf
)
669 lib_ring_buffer_release_read(buf
, handle
, 1);
672 /* For mmap mode, readable without "get" operation */
674 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
675 struct lttng_ust_lib_ring_buffer
*buf
)
677 return shmp(handle
, buf
->backend
.memory_map
);
680 /* returns the length to mmap. */
681 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
682 struct lttng_ust_lib_ring_buffer
*buf
,
685 unsigned long mmap_buf_len
;
686 struct channel
*chan
= handle
->shadow_chan
;
688 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
690 mmap_buf_len
= chan
->backend
.buf_size
;
691 if (chan
->backend
.extra_reader_sb
)
692 mmap_buf_len
+= chan
->backend
.subbuf_size
;
693 if (mmap_buf_len
> INT_MAX
)
699 /* returns the maximum size for sub-buffers. */
700 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
701 struct lttng_ust_lib_ring_buffer
*buf
,
704 struct channel
*chan
= handle
->shadow_chan
;
706 *len
= chan
->backend
.subbuf_size
;
711 * For mmap mode, operate on the current packet (between get/put or
712 * get_next/put_next).
715 /* returns the offset of the subbuffer belonging to the mmap reader. */
716 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
717 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
719 struct channel
*chan
= handle
->shadow_chan
;
720 unsigned long sb_bindex
;
722 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
724 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
725 buf
->backend
.buf_rsb
.id
);
726 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
730 /* returns the size of the current sub-buffer, without padding (for mmap). */
731 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
732 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
734 struct channel
*chan
= handle
->shadow_chan
;
736 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
741 /* returns the size of the current sub-buffer, without padding (for mmap). */
742 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
743 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
745 struct channel
*chan
= handle
->shadow_chan
;
747 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
749 *len
= PAGE_ALIGN(*len
);
753 /* Get exclusive read access to the next sub-buffer that can be read. */
754 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
755 struct lttng_ust_lib_ring_buffer
*buf
)
757 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
761 /* Release exclusive sub-buffer access, move consumer forward. */
762 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
763 struct lttng_ust_lib_ring_buffer
*buf
)
765 lib_ring_buffer_put_next_subbuf(buf
, handle
);
771 /* Get a snapshot of the current ring buffer producer and consumer positions */
772 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
773 struct lttng_ust_lib_ring_buffer
*buf
)
775 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
776 &buf
->prod_snapshot
, handle
);
779 /* Get the consumer position (iteration start) */
780 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
781 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
783 *pos
= buf
->cons_snapshot
;
787 /* Get the producer position (iteration end) */
788 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
789 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
791 *pos
= buf
->prod_snapshot
;
795 /* Get exclusive read access to the specified sub-buffer position */
796 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
797 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
799 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
802 /* Release exclusive sub-buffer access */
803 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
804 struct lttng_ust_lib_ring_buffer
*buf
)
806 lib_ring_buffer_put_subbuf(buf
, handle
);
810 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
811 struct lttng_ust_lib_ring_buffer
*buf
,
814 lib_ring_buffer_switch_slow(buf
,
815 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,