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/usterr-signal-safe.h>
24 #include <lttng/ust-comm.h>
26 #include "../libringbuffer/backend.h"
27 #include "../libringbuffer/frontend.h"
29 volatile enum ust_loglevel ust_loglevel
;
32 void init_object(struct lttng_ust_object_data
*data
)
37 data
->memory_map_size
= 0;
40 void ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
42 struct ustcomm_ust_msg lum
;
43 struct ustcomm_ust_reply lur
;
46 if (data
->shm_fd
>= 0)
48 if (data
->wait_fd
>= 0)
50 memset(&lum
, 0, sizeof(lum
));
51 lum
.handle
= data
->handle
;
52 lum
.cmd
= LTTNG_UST_RELEASE
;
53 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
59 * Send registration done packet to the application.
61 int ustctl_register_done(int sock
)
63 struct ustcomm_ust_msg lum
;
64 struct ustcomm_ust_reply lur
;
67 DBG("Sending register done command to %d", sock
);
68 memset(&lum
, 0, sizeof(lum
));
69 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
70 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
71 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
74 if (lur
.ret_code
!= USTCOMM_OK
) {
75 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
85 * returns session handle.
87 int ustctl_create_session(int sock
)
89 struct ustcomm_ust_msg lum
;
90 struct ustcomm_ust_reply lur
;
91 int ret
, session_handle
;
94 memset(&lum
, 0, sizeof(lum
));
95 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
96 lum
.cmd
= LTTNG_UST_SESSION
;
97 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
100 session_handle
= lur
.ret_val
;
101 DBG("received session handle %u", session_handle
);
102 return session_handle
;
105 /* open the metadata global channel */
106 int ustctl_open_metadata(int sock
, int session_handle
,
107 struct lttng_ust_channel_attr
*chops
,
108 struct lttng_ust_object_data
**_metadata_data
)
110 struct ustcomm_ust_msg lum
;
111 struct ustcomm_ust_reply lur
;
112 struct lttng_ust_object_data
*metadata_data
;
115 metadata_data
= malloc(sizeof(*metadata_data
));
118 init_object(metadata_data
);
119 /* Create metadata channel */
120 memset(&lum
, 0, sizeof(lum
));
121 lum
.handle
= session_handle
;
122 lum
.cmd
= LTTNG_UST_METADATA
;
123 lum
.u
.channel
.overwrite
= chops
->overwrite
;
124 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
125 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
126 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
127 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
128 lum
.u
.channel
.output
= chops
->output
;
129 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
134 if (lur
.ret_code
!= USTCOMM_OK
) {
138 metadata_data
->handle
= lur
.ret_val
;
139 DBG("received metadata handle %u", metadata_data
->handle
);
140 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
142 ret
= ustcomm_recv_fd(sock
);
145 metadata_data
->shm_fd
= ret
;
147 ret
= ustcomm_recv_fd(sock
);
150 metadata_data
->wait_fd
= ret
;
151 *_metadata_data
= metadata_data
;
155 ustctl_release_object(sock
, metadata_data
);
159 int ustctl_create_channel(int sock
, int session_handle
,
160 struct lttng_ust_channel_attr
*chops
,
161 struct lttng_ust_object_data
**_channel_data
)
163 struct ustcomm_ust_msg lum
;
164 struct ustcomm_ust_reply lur
;
165 struct lttng_ust_object_data
*channel_data
;
168 channel_data
= malloc(sizeof(*channel_data
));
171 init_object(channel_data
);
172 /* Create metadata channel */
173 memset(&lum
, 0, sizeof(lum
));
174 lum
.handle
= session_handle
;
175 lum
.cmd
= LTTNG_UST_CHANNEL
;
176 lum
.u
.channel
.overwrite
= chops
->overwrite
;
177 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
178 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
179 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
180 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
181 lum
.u
.channel
.output
= chops
->output
;
182 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
187 if (lur
.ret_code
!= USTCOMM_OK
) {
191 channel_data
->handle
= lur
.ret_val
;
192 DBG("received channel handle %u", channel_data
->handle
);
193 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
195 ret
= ustcomm_recv_fd(sock
);
198 channel_data
->shm_fd
= ret
;
200 ret
= ustcomm_recv_fd(sock
);
203 channel_data
->wait_fd
= ret
;
204 *_channel_data
= channel_data
;
208 ustctl_release_object(sock
, channel_data
);
213 * Return -ENOENT if no more stream is available for creation.
214 * Return 0 on success.
215 * Return negative error value on error.
217 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
218 struct lttng_ust_object_data
**_stream_data
)
220 struct ustcomm_ust_msg lum
;
221 struct ustcomm_ust_reply lur
;
222 struct lttng_ust_object_data
*stream_data
;
225 stream_data
= malloc(sizeof(*stream_data
));
228 init_object(stream_data
);
229 memset(&lum
, 0, sizeof(lum
));
230 lum
.handle
= channel_data
->handle
;
231 lum
.cmd
= LTTNG_UST_STREAM
;
232 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
237 if (lur
.ret_code
!= USTCOMM_OK
) {
242 stream_data
->handle
= lur
.ret_val
;
243 DBG("received stream handle %u", stream_data
->handle
);
244 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
246 fd
= ustcomm_recv_fd(sock
);
249 stream_data
->shm_fd
= fd
;
251 fd
= ustcomm_recv_fd(sock
);
254 stream_data
->wait_fd
= fd
;
255 *_stream_data
= stream_data
;
259 ustctl_release_object(sock
, stream_data
);
263 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
264 struct lttng_ust_object_data
*channel_data
,
265 struct lttng_ust_object_data
**_event_data
)
267 struct ustcomm_ust_msg lum
;
268 struct ustcomm_ust_reply lur
;
269 struct lttng_ust_object_data
*event_data
;
272 event_data
= malloc(sizeof(*event_data
));
275 init_object(event_data
);
276 memset(&lum
, 0, sizeof(lum
));
277 lum
.handle
= channel_data
->handle
;
278 lum
.cmd
= LTTNG_UST_EVENT
;
279 strncpy(lum
.u
.event
.name
, ev
->name
,
280 LTTNG_UST_SYM_NAME_LEN
);
281 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
282 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
287 event_data
->handle
= lur
.ret_val
;
288 DBG("received event handle %u", event_data
->handle
);
289 *_event_data
= event_data
;
293 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
294 struct lttng_ust_object_data
*obj_data
,
295 struct lttng_ust_object_data
**_context_data
)
297 struct ustcomm_ust_msg lum
;
298 struct ustcomm_ust_reply lur
;
299 struct lttng_ust_object_data
*context_data
;
302 context_data
= malloc(sizeof(*context_data
));
305 init_object(context_data
);
306 memset(&lum
, 0, sizeof(lum
));
307 lum
.handle
= obj_data
->handle
;
308 lum
.cmd
= LTTNG_UST_CONTEXT
;
309 lum
.u
.context
.ctx
= ctx
->ctx
;
310 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
315 context_data
->handle
= lur
.ret_val
;
316 DBG("received context handle %u", context_data
->handle
);
317 *_context_data
= context_data
;
321 /* Enable event, channel and session ioctl */
322 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
324 struct ustcomm_ust_msg lum
;
325 struct ustcomm_ust_reply lur
;
328 memset(&lum
, 0, sizeof(lum
));
329 lum
.handle
= object
->handle
;
330 lum
.cmd
= LTTNG_UST_ENABLE
;
331 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
334 DBG("enabled handle %u", object
->handle
);
338 /* Disable event, channel and session ioctl */
339 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
341 struct ustcomm_ust_msg lum
;
342 struct ustcomm_ust_reply lur
;
345 memset(&lum
, 0, sizeof(lum
));
346 lum
.handle
= object
->handle
;
347 lum
.cmd
= LTTNG_UST_DISABLE
;
348 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
351 DBG("disable handle %u", object
->handle
);
355 int ustctl_start_session(int sock
, int handle
)
357 struct lttng_ust_object_data obj
;
360 return ustctl_enable(sock
, &obj
);
363 int ustctl_stop_session(int sock
, int handle
)
365 struct lttng_ust_object_data obj
;
368 return ustctl_disable(sock
, &obj
);
372 int ustctl_tracepoint_list(int sock
)
374 return -ENOSYS
; /* not implemented */
377 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
379 struct ustcomm_ust_msg lum
;
380 struct ustcomm_ust_reply lur
;
383 memset(&lum
, 0, sizeof(lum
));
384 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
385 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
386 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
389 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
390 DBG("received tracer version");
394 int ustctl_wait_quiescent(int sock
)
396 struct ustcomm_ust_msg lum
;
397 struct ustcomm_ust_reply lur
;
400 memset(&lum
, 0, sizeof(lum
));
401 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
402 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
403 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
406 DBG("waited for quiescent state");
410 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
415 /* Buffer operations */
417 /* Map channel shm into process memory */
418 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
420 struct lttng_ust_shm_handle
*handle
;
421 struct channel
*chan
;
424 handle
= channel_handle_create(chan_data
->shm_fd
,
426 chan_data
->memory_map_size
);
428 ERR("create handle error");
432 * Set to -1 because the lttng_ust_shm_handle destruction will take care
433 * of closing shm_fd and wait_fd.
435 chan_data
->shm_fd
= -1;
436 chan_data
->wait_fd
= -1;
439 * TODO: add consistency checks to be resilient if the
440 * application try to feed us with incoherent channel structure
443 chan
= shmp(handle
, handle
->chan
);
444 /* chan is object 0. This is hardcoded. */
445 chan_size
= handle
->table
->objects
[0].allocated_len
;
446 handle
->shadow_chan
= malloc(chan_size
);
447 if (!handle
->shadow_chan
) {
448 channel_destroy(chan
, handle
, 1);
451 memcpy(handle
->shadow_chan
, chan
, chan_size
);
453 * The callback pointers in the producer are invalid in the
454 * consumer. Zero them out.
456 memset(&handle
->shadow_chan
->backend
.config
.cb
, 0,
457 sizeof(handle
->shadow_chan
->backend
.config
.cb
));
461 /* Add stream to channel shm and map its shm into process memory */
462 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
463 struct lttng_ust_object_data
*stream_data
)
467 if (!stream_data
->handle
)
470 ret
= channel_handle_add_stream(handle
,
472 stream_data
->wait_fd
,
473 stream_data
->memory_map_size
);
475 ERR("add stream error\n");
479 * Set to -1 because the lttng_ust_shm_handle destruction will take care
480 * of closing shm_fd and wait_fd.
482 stream_data
->shm_fd
= -1;
483 stream_data
->wait_fd
= -1;
487 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
489 struct channel
*chan
;
491 chan
= shmp(handle
, handle
->chan
);
492 channel_destroy(chan
, handle
, 1);
495 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
498 struct channel
*chan
= handle
->shadow_chan
;
500 uint64_t memory_map_size
;
501 struct lttng_ust_lib_ring_buffer
*buf
;
504 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
505 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
508 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
514 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
515 struct lttng_ust_lib_ring_buffer
*buf
)
517 lib_ring_buffer_release_read(buf
, handle
, 1);
520 /* For mmap mode, readable without "get" operation */
522 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
523 struct lttng_ust_lib_ring_buffer
*buf
)
525 return shmp(handle
, buf
->backend
.memory_map
);
528 /* returns the length to mmap. */
529 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
530 struct lttng_ust_lib_ring_buffer
*buf
,
533 unsigned long mmap_buf_len
;
534 struct channel
*chan
= handle
->shadow_chan
;
536 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
538 mmap_buf_len
= chan
->backend
.buf_size
;
539 if (chan
->backend
.extra_reader_sb
)
540 mmap_buf_len
+= chan
->backend
.subbuf_size
;
541 if (mmap_buf_len
> INT_MAX
)
547 /* returns the maximum size for sub-buffers. */
548 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
549 struct lttng_ust_lib_ring_buffer
*buf
,
552 struct channel
*chan
= handle
->shadow_chan
;
554 *len
= chan
->backend
.subbuf_size
;
559 * For mmap mode, operate on the current packet (between get/put or
560 * get_next/put_next).
563 /* returns the offset of the subbuffer belonging to the mmap reader. */
564 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
565 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
567 struct channel
*chan
= handle
->shadow_chan
;
568 unsigned long sb_bindex
;
570 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
572 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
573 buf
->backend
.buf_rsb
.id
);
574 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
578 /* returns the size of the current sub-buffer, without padding (for mmap). */
579 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
580 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
582 struct channel
*chan
= handle
->shadow_chan
;
584 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
589 /* returns the size of the current sub-buffer, without padding (for mmap). */
590 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
591 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
593 struct channel
*chan
= handle
->shadow_chan
;
595 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
597 *len
= PAGE_ALIGN(*len
);
601 /* Get exclusive read access to the next sub-buffer that can be read. */
602 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
603 struct lttng_ust_lib_ring_buffer
*buf
)
605 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
609 /* Release exclusive sub-buffer access, move consumer forward. */
610 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
611 struct lttng_ust_lib_ring_buffer
*buf
)
613 lib_ring_buffer_put_next_subbuf(buf
, handle
);
619 /* Get a snapshot of the current ring buffer producer and consumer positions */
620 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
621 struct lttng_ust_lib_ring_buffer
*buf
)
623 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
624 &buf
->prod_snapshot
, handle
);
627 /* Get the consumer position (iteration start) */
628 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
629 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
631 *pos
= buf
->cons_snapshot
;
635 /* Get the producer position (iteration end) */
636 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
637 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
639 *pos
= buf
->prod_snapshot
;
643 /* Get exclusive read access to the specified sub-buffer position */
644 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
645 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
647 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
650 /* Release exclusive sub-buffer access */
651 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
652 struct lttng_ust_lib_ring_buffer
*buf
)
654 lib_ring_buffer_put_subbuf(buf
, handle
);
658 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
659 struct lttng_ust_lib_ring_buffer
*buf
,
662 lib_ring_buffer_switch_slow(buf
,
663 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,