3d886d3305c569f7df4092ae90ec076a932e0695
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 <ust/lttng-ust-ctl.h>
22 #include <ust/lttng-ust-abi.h>
23 #include <ust/usterr-signal-safe.h>
24 #include "lttng-ust-comm.h"
26 #include "../libringbuffer/backend.h"
27 #include "../libringbuffer/frontend.h"
30 void init_object(struct object_data
*data
)
35 data
->memory_map_size
= 0;
38 void release_object(int sock
, struct object_data
*data
)
40 struct ustcomm_ust_msg lum
;
41 struct ustcomm_ust_reply lur
;
44 if (data
->shm_fd
>= 0)
46 if (data
->wait_fd
>= 0)
48 memset(&lum
, 0, sizeof(lum
));
49 lum
.handle
= data
->handle
;
50 lum
.cmd
= LTTNG_UST_RELEASE
;
51 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
57 * returns session handle.
59 int ustctl_create_session(int sock
)
61 struct ustcomm_ust_msg lum
;
62 struct ustcomm_ust_reply lur
;
63 int ret
, session_handle
;
66 memset(&lum
, 0, sizeof(lum
));
67 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
68 lum
.cmd
= LTTNG_UST_SESSION
;
69 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
72 session_handle
= lur
.ret_val
;
73 DBG("received session handle %u", session_handle
);
74 return session_handle
;
77 /* open the metadata global channel */
78 int ustctl_open_metadata(int sock
, int session_handle
,
79 struct lttng_ust_channel_attr
*chops
,
80 struct object_data
**_metadata_data
)
82 struct ustcomm_ust_msg lum
;
83 struct ustcomm_ust_reply lur
;
84 struct object_data
*metadata_data
;
87 metadata_data
= malloc(sizeof(*metadata_data
));
90 init_object(metadata_data
);
91 /* Create metadata channel */
92 memset(&lum
, 0, sizeof(lum
));
93 lum
.handle
= session_handle
;
94 lum
.cmd
= LTTNG_UST_METADATA
;
95 lum
.u
.channel
.overwrite
= chops
->overwrite
;
96 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
97 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
98 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
99 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
100 lum
.u
.channel
.output
= chops
->output
;
101 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
106 if (lur
.ret_code
!= USTCOMM_OK
) {
110 metadata_data
->handle
= lur
.ret_val
;
111 DBG("received metadata handle %u", metadata_data
->handle
);
112 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
114 ret
= ustcomm_recv_fd(sock
);
117 metadata_data
->shm_fd
= ret
;
119 ret
= ustcomm_recv_fd(sock
);
122 metadata_data
->wait_fd
= ret
;
123 *_metadata_data
= metadata_data
;
127 release_object(sock
, metadata_data
);
131 int ustctl_create_channel(int sock
, int session_handle
,
132 struct lttng_ust_channel_attr
*chops
,
133 struct object_data
**_channel_data
)
135 struct ustcomm_ust_msg lum
;
136 struct ustcomm_ust_reply lur
;
137 struct object_data
*channel_data
;
140 channel_data
= malloc(sizeof(*channel_data
));
143 init_object(channel_data
);
144 /* Create metadata channel */
145 memset(&lum
, 0, sizeof(lum
));
146 lum
.handle
= session_handle
;
147 lum
.cmd
= LTTNG_UST_CHANNEL
;
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 channel_data
->handle
= lur
.ret_val
;
164 DBG("received channel handle %u", channel_data
->handle
);
165 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
167 ret
= ustcomm_recv_fd(sock
);
170 channel_data
->shm_fd
= ret
;
172 ret
= ustcomm_recv_fd(sock
);
175 channel_data
->wait_fd
= ret
;
176 *_channel_data
= channel_data
;
180 release_object(sock
, channel_data
);
185 * Return -ENOENT if no more stream is available for creation.
186 * Return 0 on success.
187 * Return negative error value on error.
189 int ustctl_create_stream(int sock
, struct object_data
*channel_data
,
190 struct object_data
**_stream_data
)
192 struct ustcomm_ust_msg lum
;
193 struct ustcomm_ust_reply lur
;
194 struct object_data
*stream_data
;
197 stream_data
= malloc(sizeof(*stream_data
));
200 init_object(stream_data
);
201 memset(&lum
, 0, sizeof(lum
));
202 lum
.handle
= channel_data
->handle
;
203 lum
.cmd
= LTTNG_UST_STREAM
;
204 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
209 if (lur
.ret_code
!= USTCOMM_OK
) {
214 stream_data
->handle
= lur
.ret_val
;
215 DBG("received stream handle %u", stream_data
->handle
);
216 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
218 fd
= ustcomm_recv_fd(sock
);
221 stream_data
->shm_fd
= fd
;
223 fd
= ustcomm_recv_fd(sock
);
226 stream_data
->wait_fd
= fd
;
227 *_stream_data
= stream_data
;
231 release_object(sock
, stream_data
);
235 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
236 struct object_data
*channel_data
,
237 struct object_data
**_event_data
)
239 struct ustcomm_ust_msg lum
;
240 struct ustcomm_ust_reply lur
;
241 struct object_data
*event_data
;
244 event_data
= malloc(sizeof(*event_data
));
247 init_object(event_data
);
248 memset(&lum
, 0, sizeof(lum
));
249 lum
.handle
= channel_data
->handle
;
250 lum
.cmd
= LTTNG_UST_EVENT
;
251 strncpy(lum
.u
.event
.name
, ev
->name
,
252 LTTNG_UST_SYM_NAME_LEN
);
253 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
254 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
259 event_data
->handle
= lur
.ret_val
;
260 DBG("received event handle %u", event_data
->handle
);
261 *_event_data
= event_data
;
265 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
266 struct object_data
*channel_data
,
267 struct object_data
**_context_data
)
269 struct ustcomm_ust_msg lum
;
270 struct ustcomm_ust_reply lur
;
271 struct object_data
*context_data
;
274 context_data
= malloc(sizeof(*context_data
));
277 init_object(context_data
);
278 memset(&lum
, 0, sizeof(lum
));
279 lum
.handle
= channel_data
->handle
;
280 lum
.cmd
= LTTNG_UST_CONTEXT
;
281 lum
.u
.context
.ctx
= ctx
->ctx
;
282 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
287 context_data
->handle
= lur
.ret_val
;
288 DBG("received context handle %u", context_data
->handle
);
289 *_context_data
= context_data
;
293 /* Enable event, channel and session ioctl */
294 int ustctl_enable(int sock
, struct object_data
*object
)
296 struct ustcomm_ust_msg lum
;
297 struct ustcomm_ust_reply lur
;
300 memset(&lum
, 0, sizeof(lum
));
301 lum
.handle
= object
->handle
;
302 lum
.cmd
= LTTNG_UST_ENABLE
;
303 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
306 DBG("enabled handle %u", object
->handle
);
310 /* Disable event, channel and session ioctl */
311 int ustctl_disable(int sock
, struct object_data
*object
)
313 struct ustcomm_ust_msg lum
;
314 struct ustcomm_ust_reply lur
;
317 memset(&lum
, 0, sizeof(lum
));
318 lum
.handle
= object
->handle
;
319 lum
.cmd
= LTTNG_UST_DISABLE
;
320 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
323 DBG("disable handle %u", object
->handle
);
327 int ustctl_start_session(int sock
, struct object_data
*object
)
329 return ustctl_enable(sock
, object
);
332 int ustctl_stop_session(int sock
, struct object_data
*object
)
334 return ustctl_disable(sock
, object
);
338 int ustctl_tracepoint_list(int sock
)
340 return -ENOSYS
; /* not implemented */
343 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
345 struct ustcomm_ust_msg lum
;
346 struct ustcomm_ust_reply lur
;
349 memset(&lum
, 0, sizeof(lum
));
350 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
351 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
352 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
355 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
356 DBG("received tracer version");
360 int ustctl_wait_quiescent(int sock
)
362 struct ustcomm_ust_msg lum
;
363 struct ustcomm_ust_reply lur
;
366 memset(&lum
, 0, sizeof(lum
));
367 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
368 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
369 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
372 DBG("waited for quiescent state");
376 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
381 /* Buffer operations */
383 /* Map channel shm into process memory */
384 struct shm_handle
*ustctl_map_channel(struct object_data
*chan_data
)
386 struct shm_handle
*handle
;
387 struct channel
*chan
;
390 handle
= channel_handle_create(chan_data
->shm_fd
,
392 chan_data
->memory_map_size
);
394 ERR("create handle error");
398 * Set to -1 because the shm_handle destruction will take care
399 * of closing shm_fd and wait_fd.
401 chan_data
->shm_fd
= -1;
402 chan_data
->wait_fd
= -1;
405 * TODO: add consistency checks to be resilient if the
406 * application try to feed us with incoherent channel structure
409 chan
= shmp(handle
, handle
->chan
);
410 /* chan is object 0. This is hardcoded. */
411 chan_size
= handle
->table
->objects
[0].allocated_len
;
412 handle
->shadow_chan
= malloc(chan_size
);
413 if (!handle
->shadow_chan
) {
414 channel_destroy(chan
, handle
, 1);
417 memcpy(handle
->shadow_chan
, chan
, chan_size
);
421 /* Add stream to channel shm and map its shm into process memory */
422 int ustctl_add_stream(struct shm_handle
*handle
,
423 struct object_data
*stream_data
)
427 if (!stream_data
->handle
)
430 ret
= channel_handle_add_stream(handle
,
432 stream_data
->wait_fd
,
433 stream_data
->memory_map_size
);
435 ERR("add stream error\n");
439 * Set to -1 because the shm_handle destruction will take care
440 * of closing shm_fd and wait_fd.
442 stream_data
->shm_fd
= -1;
443 stream_data
->wait_fd
= -1;
447 /* For mmap mode, readable without "get" operation */
449 /* returns the length to mmap. */
450 int ustctl_get_mmap_len(struct shm_handle
*handle
,
451 struct lib_ring_buffer
*buf
,
454 unsigned long mmap_buf_len
;
455 struct channel
*chan
= handle
->shadow_chan
;
457 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
459 mmap_buf_len
= chan
->backend
.buf_size
;
460 if (chan
->backend
.extra_reader_sb
)
461 mmap_buf_len
+= chan
->backend
.subbuf_size
;
462 if (mmap_buf_len
> INT_MAX
)
468 /* returns the maximum size for sub-buffers. */
469 int ustctl_get_max_subbuf_size(struct shm_handle
*handle
,
470 struct lib_ring_buffer
*buf
,
473 struct channel
*chan
= handle
->shadow_chan
;
475 *len
= chan
->backend
.subbuf_size
;
480 * For mmap mode, operate on the current packet (between get/put or
481 * get_next/put_next).
484 /* returns the offset of the subbuffer belonging to the mmap reader. */
485 int ustctl_get_mmap_read_offset(struct shm_handle
*handle
,
486 struct lib_ring_buffer
*buf
, unsigned long *off
)
488 struct channel
*chan
= handle
->shadow_chan
;
489 unsigned long sb_bindex
;
491 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
493 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
494 buf
->backend
.buf_rsb
.id
);
495 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
499 /* returns the size of the current sub-buffer, without padding (for mmap). */
500 int ustctl_get_subbuf_size(struct shm_handle
*handle
,
501 struct lib_ring_buffer
*buf
, unsigned long *len
)
503 struct channel
*chan
= handle
->shadow_chan
;
505 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
510 /* returns the size of the current sub-buffer, without padding (for mmap). */
511 int ustctl_get_padded_subbuf_size(struct shm_handle
*handle
,
512 struct lib_ring_buffer
*buf
, unsigned long *len
)
514 struct channel
*chan
= handle
->shadow_chan
;
516 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
518 *len
= PAGE_ALIGN(*len
);
522 /* Get exclusive read access to the next sub-buffer that can be read. */
523 int ustctl_get_next_subbuf(struct shm_handle
*handle
,
524 struct lib_ring_buffer
*buf
)
526 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
530 /* Release exclusive sub-buffer access, move consumer forward. */
531 int ustctl_put_next_subbuf(struct shm_handle
*handle
,
532 struct lib_ring_buffer
*buf
)
534 lib_ring_buffer_put_next_subbuf(buf
, handle
);
540 /* Get a snapshot of the current ring buffer producer and consumer positions */
541 int ustctl_snapshot(struct shm_handle
*handle
,
542 struct lib_ring_buffer
*buf
)
544 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
545 &buf
->prod_snapshot
, handle
);
548 /* Get the consumer position (iteration start) */
549 int ustctl_snapshot_get_consumed(struct shm_handle
*handle
,
550 struct lib_ring_buffer
*buf
, unsigned long *pos
)
552 *pos
= buf
->cons_snapshot
;
556 /* Get the producer position (iteration end) */
557 int ustctl_snapshot_get_produced(struct shm_handle
*handle
,
558 struct lib_ring_buffer
*buf
, unsigned long *pos
)
560 *pos
= buf
->prod_snapshot
;
564 /* Get exclusive read access to the specified sub-buffer position */
565 int ustctl_get_subbuf(struct shm_handle
*handle
,
566 struct lib_ring_buffer
*buf
, unsigned long *pos
)
568 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
571 /* Release exclusive sub-buffer access */
572 int ustctl_put_subbuf(struct shm_handle
*handle
,
573 struct lib_ring_buffer
*buf
)
575 lib_ring_buffer_put_subbuf(buf
, handle
);
579 int ustctl_buffer_flush(struct shm_handle
*handle
,
580 struct lib_ring_buffer
*buf
)
582 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
This page took 0.042466 seconds and 4 git commands to generate.