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
)
66 struct ustcomm_ust_msg lum
;
67 struct ustcomm_ust_reply lur
;
70 if (data
->shm_fd
>= 0) {
71 ret
= close(data
->shm_fd
);
76 if (data
->wait_fd
>= 0) {
77 ret
= close(data
->wait_fd
);
82 return ustctl_release_handle(sock
, data
->handle
);
86 * Send registration done packet to the application.
88 int ustctl_register_done(int sock
)
90 struct ustcomm_ust_msg lum
;
91 struct ustcomm_ust_reply lur
;
94 DBG("Sending register done command to %d", sock
);
95 memset(&lum
, 0, sizeof(lum
));
96 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
97 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
98 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
101 if (lur
.ret_code
!= USTCOMM_OK
) {
102 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
112 * returns session handle.
114 int ustctl_create_session(int sock
)
116 struct ustcomm_ust_msg lum
;
117 struct ustcomm_ust_reply lur
;
118 int ret
, session_handle
;
121 memset(&lum
, 0, sizeof(lum
));
122 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
123 lum
.cmd
= LTTNG_UST_SESSION
;
124 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
127 session_handle
= lur
.ret_val
;
128 DBG("received session handle %u", session_handle
);
129 return session_handle
;
132 /* open the metadata global channel */
133 int ustctl_open_metadata(int sock
, int session_handle
,
134 struct lttng_ust_channel_attr
*chops
,
135 struct lttng_ust_object_data
**_metadata_data
)
137 struct ustcomm_ust_msg lum
;
138 struct ustcomm_ust_reply lur
;
139 struct lttng_ust_object_data
*metadata_data
;
142 metadata_data
= malloc(sizeof(*metadata_data
));
145 init_object(metadata_data
);
146 /* Create metadata channel */
147 memset(&lum
, 0, sizeof(lum
));
148 lum
.handle
= session_handle
;
149 lum
.cmd
= LTTNG_UST_METADATA
;
150 lum
.u
.channel
.overwrite
= chops
->overwrite
;
151 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
152 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
153 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
154 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
155 lum
.u
.channel
.output
= chops
->output
;
156 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
161 if (lur
.ret_code
!= USTCOMM_OK
) {
165 metadata_data
->handle
= lur
.ret_val
;
166 DBG("received metadata handle %u", metadata_data
->handle
);
167 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
169 ret
= ustcomm_recv_fd(sock
);
172 metadata_data
->shm_fd
= ret
;
174 ret
= ustcomm_recv_fd(sock
);
177 metadata_data
->wait_fd
= ret
;
178 *_metadata_data
= metadata_data
;
182 (void) ustctl_release_object(sock
, metadata_data
);
187 int ustctl_create_channel(int sock
, int session_handle
,
188 struct lttng_ust_channel_attr
*chops
,
189 struct lttng_ust_object_data
**_channel_data
)
191 struct ustcomm_ust_msg lum
;
192 struct ustcomm_ust_reply lur
;
193 struct lttng_ust_object_data
*channel_data
;
196 channel_data
= malloc(sizeof(*channel_data
));
199 init_object(channel_data
);
200 /* Create metadata channel */
201 memset(&lum
, 0, sizeof(lum
));
202 lum
.handle
= session_handle
;
203 lum
.cmd
= LTTNG_UST_CHANNEL
;
204 lum
.u
.channel
.overwrite
= chops
->overwrite
;
205 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
206 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
207 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
208 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
209 lum
.u
.channel
.output
= chops
->output
;
210 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
215 if (lur
.ret_code
!= USTCOMM_OK
) {
219 channel_data
->handle
= lur
.ret_val
;
220 DBG("received channel handle %u", channel_data
->handle
);
221 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
223 ret
= ustcomm_recv_fd(sock
);
226 channel_data
->shm_fd
= ret
;
228 ret
= ustcomm_recv_fd(sock
);
231 channel_data
->wait_fd
= ret
;
232 *_channel_data
= channel_data
;
236 (void) ustctl_release_object(sock
, channel_data
);
242 * Return -ENOENT if no more stream is available for creation.
243 * Return 0 on success.
244 * Return negative error value on error.
246 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
247 struct lttng_ust_object_data
**_stream_data
)
249 struct ustcomm_ust_msg lum
;
250 struct ustcomm_ust_reply lur
;
251 struct lttng_ust_object_data
*stream_data
;
254 stream_data
= malloc(sizeof(*stream_data
));
257 init_object(stream_data
);
258 memset(&lum
, 0, sizeof(lum
));
259 lum
.handle
= channel_data
->handle
;
260 lum
.cmd
= LTTNG_UST_STREAM
;
261 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
266 if (lur
.ret_code
!= USTCOMM_OK
) {
271 stream_data
->handle
= lur
.ret_val
;
272 DBG("received stream handle %u", stream_data
->handle
);
273 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
275 fd
= ustcomm_recv_fd(sock
);
278 stream_data
->shm_fd
= fd
;
280 fd
= ustcomm_recv_fd(sock
);
283 stream_data
->wait_fd
= fd
;
284 *_stream_data
= stream_data
;
288 (void) ustctl_release_object(sock
, stream_data
);
293 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
294 struct lttng_ust_object_data
*channel_data
,
295 struct lttng_ust_object_data
**_event_data
)
297 struct ustcomm_ust_msg lum
;
298 struct ustcomm_ust_reply lur
;
299 struct lttng_ust_object_data
*event_data
;
302 event_data
= malloc(sizeof(*event_data
));
305 init_object(event_data
);
306 memset(&lum
, 0, sizeof(lum
));
307 lum
.handle
= channel_data
->handle
;
308 lum
.cmd
= LTTNG_UST_EVENT
;
309 strncpy(lum
.u
.event
.name
, ev
->name
,
310 LTTNG_UST_SYM_NAME_LEN
);
311 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
312 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
317 event_data
->handle
= lur
.ret_val
;
318 DBG("received event handle %u", event_data
->handle
);
319 *_event_data
= event_data
;
323 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
324 struct lttng_ust_object_data
*obj_data
,
325 struct lttng_ust_object_data
**_context_data
)
327 struct ustcomm_ust_msg lum
;
328 struct ustcomm_ust_reply lur
;
329 struct lttng_ust_object_data
*context_data
;
332 context_data
= malloc(sizeof(*context_data
));
335 init_object(context_data
);
336 memset(&lum
, 0, sizeof(lum
));
337 lum
.handle
= obj_data
->handle
;
338 lum
.cmd
= LTTNG_UST_CONTEXT
;
339 lum
.u
.context
.ctx
= ctx
->ctx
;
340 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
345 context_data
->handle
= lur
.ret_val
;
346 DBG("received context handle %u", context_data
->handle
);
347 *_context_data
= context_data
;
351 /* Enable event, channel and session ioctl */
352 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
354 struct ustcomm_ust_msg lum
;
355 struct ustcomm_ust_reply lur
;
358 memset(&lum
, 0, sizeof(lum
));
359 lum
.handle
= object
->handle
;
360 lum
.cmd
= LTTNG_UST_ENABLE
;
361 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
364 DBG("enabled handle %u", object
->handle
);
368 /* Disable event, channel and session ioctl */
369 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
371 struct ustcomm_ust_msg lum
;
372 struct ustcomm_ust_reply lur
;
375 memset(&lum
, 0, sizeof(lum
));
376 lum
.handle
= object
->handle
;
377 lum
.cmd
= LTTNG_UST_DISABLE
;
378 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
381 DBG("disable handle %u", object
->handle
);
385 int ustctl_start_session(int sock
, int handle
)
387 struct lttng_ust_object_data obj
;
390 return ustctl_enable(sock
, &obj
);
393 int ustctl_stop_session(int sock
, int handle
)
395 struct lttng_ust_object_data obj
;
398 return ustctl_disable(sock
, &obj
);
401 int ustctl_tracepoint_list(int sock
)
403 struct ustcomm_ust_msg lum
;
404 struct ustcomm_ust_reply lur
;
405 int ret
, tp_list_handle
;
407 memset(&lum
, 0, sizeof(lum
));
408 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
409 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
410 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
413 tp_list_handle
= lur
.ret_val
;
414 DBG("received tracepoint list handle %u", tp_list_handle
);
415 return tp_list_handle
;
418 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
419 struct lttng_ust_tracepoint_iter
*iter
)
421 struct ustcomm_ust_msg lum
;
422 struct ustcomm_ust_reply lur
;
425 memset(&lum
, 0, sizeof(lum
));
426 lum
.handle
= tp_list_handle
;
427 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
428 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
431 DBG("received tracepoint list entry name %s loglevel %s loglevel_value %lld",
432 lur
.u
.tracepoint
.name
,
433 lur
.u
.tracepoint
.loglevel
,
434 (unsigned long long) lur
.u
.tracepoint
.loglevel_value
);
435 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
439 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
441 struct ustcomm_ust_msg lum
;
442 struct ustcomm_ust_reply lur
;
445 memset(&lum
, 0, sizeof(lum
));
446 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
447 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
448 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
451 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
452 DBG("received tracer version");
456 int ustctl_wait_quiescent(int sock
)
458 struct ustcomm_ust_msg lum
;
459 struct ustcomm_ust_reply lur
;
462 memset(&lum
, 0, sizeof(lum
));
463 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
464 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
465 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
468 DBG("waited for quiescent state");
472 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
477 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
479 struct ustcomm_ust_msg lum
;
480 struct ustcomm_ust_reply lur
;
483 memset(&lum
, 0, sizeof(lum
));
484 lum
.handle
= object
->handle
;
485 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
486 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
489 DBG("flushed buffer handle %u", object
->handle
);
493 /* Buffer operations */
495 /* Map channel shm into process memory */
496 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
498 struct lttng_ust_shm_handle
*handle
;
499 struct channel
*chan
;
501 struct lttng_ust_lib_ring_buffer_config
*config
;
504 handle
= channel_handle_create(chan_data
->shm_fd
,
506 chan_data
->memory_map_size
);
508 ERR("create handle error");
512 * Set to -1 because the lttng_ust_shm_handle destruction will take care
513 * of closing shm_fd and wait_fd.
515 chan_data
->shm_fd
= -1;
516 chan_data
->wait_fd
= -1;
519 * TODO: add consistency checks to be resilient if the
520 * application try to feed us with incoherent channel structure
523 chan
= shmp(handle
, handle
->chan
);
524 /* chan is object 0. This is hardcoded. */
525 chan_size
= handle
->table
->objects
[0].allocated_len
;
526 handle
->shadow_chan
= malloc(chan_size
);
527 if (!handle
->shadow_chan
) {
528 channel_destroy(chan
, handle
, 1);
531 memcpy(handle
->shadow_chan
, chan
, chan_size
);
533 * The callback pointers in the producer are invalid in the
534 * consumer. We need to look them up here.
536 config
= &handle
->shadow_chan
->backend
.config
;
537 switch (config
->client_type
) {
538 case LTTNG_CLIENT_METADATA
:
539 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
542 case LTTNG_CLIENT_DISCARD
:
543 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
546 case LTTNG_CLIENT_OVERWRITE
:
547 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
551 ERR("Unknown client type %d", config
->client_type
);
552 channel_destroy(chan
, handle
, 1);
555 /* Replace the object table pointer. */
556 ret
= munmap(handle
->table
->objects
[0].memory_map
,
557 handle
->table
->objects
[0].memory_map_size
);
562 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
563 handle
->table
->objects
[0].is_shadow
= 1;
567 /* Add stream to channel shm and map its shm into process memory */
568 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
569 struct lttng_ust_object_data
*stream_data
)
573 if (!stream_data
->handle
)
576 ret
= channel_handle_add_stream(handle
,
578 stream_data
->wait_fd
,
579 stream_data
->memory_map_size
);
581 ERR("add stream error\n");
585 * Set to -1 because the lttng_ust_shm_handle destruction will take care
586 * of closing shm_fd and wait_fd.
588 stream_data
->shm_fd
= -1;
589 stream_data
->wait_fd
= -1;
593 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
595 struct channel
*chan
;
597 chan
= shmp(handle
, handle
->chan
);
598 channel_destroy(chan
, handle
, 1);
601 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
604 struct channel
*chan
= handle
->shadow_chan
;
606 uint64_t memory_map_size
;
607 struct lttng_ust_lib_ring_buffer
*buf
;
610 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
611 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
614 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
620 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
621 struct lttng_ust_lib_ring_buffer
*buf
)
623 lib_ring_buffer_release_read(buf
, handle
, 1);
626 /* For mmap mode, readable without "get" operation */
628 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
629 struct lttng_ust_lib_ring_buffer
*buf
)
631 return shmp(handle
, buf
->backend
.memory_map
);
634 /* returns the length to mmap. */
635 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
636 struct lttng_ust_lib_ring_buffer
*buf
,
639 unsigned long mmap_buf_len
;
640 struct channel
*chan
= handle
->shadow_chan
;
642 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
644 mmap_buf_len
= chan
->backend
.buf_size
;
645 if (chan
->backend
.extra_reader_sb
)
646 mmap_buf_len
+= chan
->backend
.subbuf_size
;
647 if (mmap_buf_len
> INT_MAX
)
653 /* returns the maximum size for sub-buffers. */
654 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
655 struct lttng_ust_lib_ring_buffer
*buf
,
658 struct channel
*chan
= handle
->shadow_chan
;
660 *len
= chan
->backend
.subbuf_size
;
665 * For mmap mode, operate on the current packet (between get/put or
666 * get_next/put_next).
669 /* returns the offset of the subbuffer belonging to the mmap reader. */
670 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
671 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
673 struct channel
*chan
= handle
->shadow_chan
;
674 unsigned long sb_bindex
;
676 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
678 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
679 buf
->backend
.buf_rsb
.id
);
680 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
684 /* returns the size of the current sub-buffer, without padding (for mmap). */
685 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
686 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
688 struct channel
*chan
= handle
->shadow_chan
;
690 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
695 /* returns the size of the current sub-buffer, without padding (for mmap). */
696 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
697 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
699 struct channel
*chan
= handle
->shadow_chan
;
701 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
703 *len
= PAGE_ALIGN(*len
);
707 /* Get exclusive read access to the next sub-buffer that can be read. */
708 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
709 struct lttng_ust_lib_ring_buffer
*buf
)
711 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
715 /* Release exclusive sub-buffer access, move consumer forward. */
716 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
717 struct lttng_ust_lib_ring_buffer
*buf
)
719 lib_ring_buffer_put_next_subbuf(buf
, handle
);
725 /* Get a snapshot of the current ring buffer producer and consumer positions */
726 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
727 struct lttng_ust_lib_ring_buffer
*buf
)
729 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
730 &buf
->prod_snapshot
, handle
);
733 /* Get the consumer position (iteration start) */
734 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
735 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
737 *pos
= buf
->cons_snapshot
;
741 /* Get the producer position (iteration end) */
742 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
743 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
745 *pos
= buf
->prod_snapshot
;
749 /* Get exclusive read access to the specified sub-buffer position */
750 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
751 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
753 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
756 /* Release exclusive sub-buffer access */
757 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
758 struct lttng_ust_lib_ring_buffer
*buf
)
760 lib_ring_buffer_put_subbuf(buf
, handle
);
764 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
765 struct lttng_ust_lib_ring_buffer
*buf
,
768 lib_ring_buffer_switch_slow(buf
,
769 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,