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"
29 volatile enum ust_loglevel ust_loglevel
;
32 void init_object(struct object_data
*data
)
37 data
->memory_map_size
= 0;
40 void release_object(int sock
, struct 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 * returns session handle.
61 int ustctl_create_session(int sock
)
63 struct ustcomm_ust_msg lum
;
64 struct ustcomm_ust_reply lur
;
65 int ret
, session_handle
;
68 memset(&lum
, 0, sizeof(lum
));
69 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
70 lum
.cmd
= LTTNG_UST_SESSION
;
71 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
74 session_handle
= lur
.ret_val
;
75 DBG("received session handle %u", session_handle
);
76 return session_handle
;
79 /* open the metadata global channel */
80 int ustctl_open_metadata(int sock
, int session_handle
,
81 struct lttng_ust_channel_attr
*chops
,
82 struct object_data
**_metadata_data
)
84 struct ustcomm_ust_msg lum
;
85 struct ustcomm_ust_reply lur
;
86 struct object_data
*metadata_data
;
89 metadata_data
= malloc(sizeof(*metadata_data
));
92 init_object(metadata_data
);
93 /* Create metadata channel */
94 memset(&lum
, 0, sizeof(lum
));
95 lum
.handle
= session_handle
;
96 lum
.cmd
= LTTNG_UST_METADATA
;
97 lum
.u
.channel
.overwrite
= chops
->overwrite
;
98 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
99 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
100 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
101 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
102 lum
.u
.channel
.output
= chops
->output
;
103 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
108 if (lur
.ret_code
!= USTCOMM_OK
) {
112 metadata_data
->handle
= lur
.ret_val
;
113 DBG("received metadata handle %u", metadata_data
->handle
);
114 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
116 ret
= ustcomm_recv_fd(sock
);
119 metadata_data
->shm_fd
= ret
;
121 ret
= ustcomm_recv_fd(sock
);
124 metadata_data
->wait_fd
= ret
;
125 *_metadata_data
= metadata_data
;
129 release_object(sock
, metadata_data
);
133 int ustctl_create_channel(int sock
, int session_handle
,
134 struct lttng_ust_channel_attr
*chops
,
135 struct object_data
**_channel_data
)
137 struct ustcomm_ust_msg lum
;
138 struct ustcomm_ust_reply lur
;
139 struct object_data
*channel_data
;
142 channel_data
= malloc(sizeof(*channel_data
));
145 init_object(channel_data
);
146 /* Create metadata channel */
147 memset(&lum
, 0, sizeof(lum
));
148 lum
.handle
= session_handle
;
149 lum
.cmd
= LTTNG_UST_CHANNEL
;
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 channel_data
->handle
= lur
.ret_val
;
166 DBG("received channel handle %u", channel_data
->handle
);
167 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
169 ret
= ustcomm_recv_fd(sock
);
172 channel_data
->shm_fd
= ret
;
174 ret
= ustcomm_recv_fd(sock
);
177 channel_data
->wait_fd
= ret
;
178 *_channel_data
= channel_data
;
182 release_object(sock
, channel_data
);
187 * Return -ENOENT if no more stream is available for creation.
188 * Return 0 on success.
189 * Return negative error value on error.
191 int ustctl_create_stream(int sock
, struct object_data
*channel_data
,
192 struct object_data
**_stream_data
)
194 struct ustcomm_ust_msg lum
;
195 struct ustcomm_ust_reply lur
;
196 struct object_data
*stream_data
;
199 stream_data
= malloc(sizeof(*stream_data
));
202 init_object(stream_data
);
203 memset(&lum
, 0, sizeof(lum
));
204 lum
.handle
= channel_data
->handle
;
205 lum
.cmd
= LTTNG_UST_STREAM
;
206 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
211 if (lur
.ret_code
!= USTCOMM_OK
) {
216 stream_data
->handle
= lur
.ret_val
;
217 DBG("received stream handle %u", stream_data
->handle
);
218 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
220 fd
= ustcomm_recv_fd(sock
);
223 stream_data
->shm_fd
= fd
;
225 fd
= ustcomm_recv_fd(sock
);
228 stream_data
->wait_fd
= fd
;
229 *_stream_data
= stream_data
;
233 release_object(sock
, stream_data
);
237 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
238 struct object_data
*channel_data
,
239 struct object_data
**_event_data
)
241 struct ustcomm_ust_msg lum
;
242 struct ustcomm_ust_reply lur
;
243 struct object_data
*event_data
;
246 event_data
= malloc(sizeof(*event_data
));
249 init_object(event_data
);
250 memset(&lum
, 0, sizeof(lum
));
251 lum
.handle
= channel_data
->handle
;
252 lum
.cmd
= LTTNG_UST_EVENT
;
253 strncpy(lum
.u
.event
.name
, ev
->name
,
254 LTTNG_UST_SYM_NAME_LEN
);
255 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
256 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
261 event_data
->handle
= lur
.ret_val
;
262 DBG("received event handle %u", event_data
->handle
);
263 *_event_data
= event_data
;
267 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
268 struct object_data
*channel_data
,
269 struct object_data
**_context_data
)
271 struct ustcomm_ust_msg lum
;
272 struct ustcomm_ust_reply lur
;
273 struct object_data
*context_data
;
276 context_data
= malloc(sizeof(*context_data
));
279 init_object(context_data
);
280 memset(&lum
, 0, sizeof(lum
));
281 lum
.handle
= channel_data
->handle
;
282 lum
.cmd
= LTTNG_UST_CONTEXT
;
283 lum
.u
.context
.ctx
= ctx
->ctx
;
284 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
289 context_data
->handle
= lur
.ret_val
;
290 DBG("received context handle %u", context_data
->handle
);
291 *_context_data
= context_data
;
295 /* Enable event, channel and session ioctl */
296 int ustctl_enable(int sock
, struct object_data
*object
)
298 struct ustcomm_ust_msg lum
;
299 struct ustcomm_ust_reply lur
;
302 memset(&lum
, 0, sizeof(lum
));
303 lum
.handle
= object
->handle
;
304 lum
.cmd
= LTTNG_UST_ENABLE
;
305 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
308 DBG("enabled handle %u", object
->handle
);
312 /* Disable event, channel and session ioctl */
313 int ustctl_disable(int sock
, struct object_data
*object
)
315 struct ustcomm_ust_msg lum
;
316 struct ustcomm_ust_reply lur
;
319 memset(&lum
, 0, sizeof(lum
));
320 lum
.handle
= object
->handle
;
321 lum
.cmd
= LTTNG_UST_DISABLE
;
322 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
325 DBG("disable handle %u", object
->handle
);
329 int ustctl_start_session(int sock
, struct object_data
*object
)
331 return ustctl_enable(sock
, object
);
334 int ustctl_stop_session(int sock
, struct object_data
*object
)
336 return ustctl_disable(sock
, object
);
340 int ustctl_tracepoint_list(int sock
)
342 return -ENOSYS
; /* not implemented */
345 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
347 struct ustcomm_ust_msg lum
;
348 struct ustcomm_ust_reply lur
;
351 memset(&lum
, 0, sizeof(lum
));
352 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
353 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
354 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
357 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
358 DBG("received tracer version");
362 int ustctl_wait_quiescent(int sock
)
364 struct ustcomm_ust_msg lum
;
365 struct ustcomm_ust_reply lur
;
368 memset(&lum
, 0, sizeof(lum
));
369 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
370 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
371 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
374 DBG("waited for quiescent state");
378 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
383 /* Buffer operations */
385 /* Map channel shm into process memory */
386 struct shm_handle
*ustctl_map_channel(struct object_data
*chan_data
)
388 struct shm_handle
*handle
;
389 struct channel
*chan
;
392 handle
= channel_handle_create(chan_data
->shm_fd
,
394 chan_data
->memory_map_size
);
396 ERR("create handle error");
400 * Set to -1 because the shm_handle destruction will take care
401 * of closing shm_fd and wait_fd.
403 chan_data
->shm_fd
= -1;
404 chan_data
->wait_fd
= -1;
407 * TODO: add consistency checks to be resilient if the
408 * application try to feed us with incoherent channel structure
411 chan
= shmp(handle
, handle
->chan
);
412 /* chan is object 0. This is hardcoded. */
413 chan_size
= handle
->table
->objects
[0].allocated_len
;
414 handle
->shadow_chan
= malloc(chan_size
);
415 if (!handle
->shadow_chan
) {
416 channel_destroy(chan
, handle
, 1);
419 memcpy(handle
->shadow_chan
, chan
, chan_size
);
423 /* Add stream to channel shm and map its shm into process memory */
424 int ustctl_add_stream(struct shm_handle
*handle
,
425 struct object_data
*stream_data
)
429 if (!stream_data
->handle
)
432 ret
= channel_handle_add_stream(handle
,
434 stream_data
->wait_fd
,
435 stream_data
->memory_map_size
);
437 ERR("add stream error\n");
441 * Set to -1 because the shm_handle destruction will take care
442 * of closing shm_fd and wait_fd.
444 stream_data
->shm_fd
= -1;
445 stream_data
->wait_fd
= -1;
449 void ustctl_unmap_channel(struct shm_handle
*handle
)
451 struct channel
*chan
;
453 chan
= shmp(handle
, handle
->chan
);
454 channel_destroy(chan
, handle
, 1);
457 struct lib_ring_buffer
*ustctl_open_stream_read(struct shm_handle
*handle
,
460 struct channel
*chan
= handle
->shadow_chan
;
462 uint64_t memory_map_size
;
463 struct lib_ring_buffer
*buf
;
466 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
467 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
470 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
476 void ustctl_close_stream_read(struct shm_handle
*handle
,
477 struct lib_ring_buffer
*buf
)
479 lib_ring_buffer_release_read(buf
, handle
, 1);
482 /* For mmap mode, readable without "get" operation */
484 void *ustctl_get_mmap_base(struct shm_handle
*handle
,
485 struct lib_ring_buffer
*buf
)
487 return shmp(handle
, buf
->backend
.memory_map
);
490 /* returns the length to mmap. */
491 int ustctl_get_mmap_len(struct shm_handle
*handle
,
492 struct lib_ring_buffer
*buf
,
495 unsigned long mmap_buf_len
;
496 struct channel
*chan
= handle
->shadow_chan
;
498 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
500 mmap_buf_len
= chan
->backend
.buf_size
;
501 if (chan
->backend
.extra_reader_sb
)
502 mmap_buf_len
+= chan
->backend
.subbuf_size
;
503 if (mmap_buf_len
> INT_MAX
)
509 /* returns the maximum size for sub-buffers. */
510 int ustctl_get_max_subbuf_size(struct shm_handle
*handle
,
511 struct lib_ring_buffer
*buf
,
514 struct channel
*chan
= handle
->shadow_chan
;
516 *len
= chan
->backend
.subbuf_size
;
521 * For mmap mode, operate on the current packet (between get/put or
522 * get_next/put_next).
525 /* returns the offset of the subbuffer belonging to the mmap reader. */
526 int ustctl_get_mmap_read_offset(struct shm_handle
*handle
,
527 struct lib_ring_buffer
*buf
, unsigned long *off
)
529 struct channel
*chan
= handle
->shadow_chan
;
530 unsigned long sb_bindex
;
532 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
534 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
535 buf
->backend
.buf_rsb
.id
);
536 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
540 /* returns the size of the current sub-buffer, without padding (for mmap). */
541 int ustctl_get_subbuf_size(struct shm_handle
*handle
,
542 struct lib_ring_buffer
*buf
, unsigned long *len
)
544 struct channel
*chan
= handle
->shadow_chan
;
546 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
551 /* returns the size of the current sub-buffer, without padding (for mmap). */
552 int ustctl_get_padded_subbuf_size(struct shm_handle
*handle
,
553 struct lib_ring_buffer
*buf
, unsigned long *len
)
555 struct channel
*chan
= handle
->shadow_chan
;
557 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
559 *len
= PAGE_ALIGN(*len
);
563 /* Get exclusive read access to the next sub-buffer that can be read. */
564 int ustctl_get_next_subbuf(struct shm_handle
*handle
,
565 struct lib_ring_buffer
*buf
)
567 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
571 /* Release exclusive sub-buffer access, move consumer forward. */
572 int ustctl_put_next_subbuf(struct shm_handle
*handle
,
573 struct lib_ring_buffer
*buf
)
575 lib_ring_buffer_put_next_subbuf(buf
, handle
);
581 /* Get a snapshot of the current ring buffer producer and consumer positions */
582 int ustctl_snapshot(struct shm_handle
*handle
,
583 struct lib_ring_buffer
*buf
)
585 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
586 &buf
->prod_snapshot
, handle
);
589 /* Get the consumer position (iteration start) */
590 int ustctl_snapshot_get_consumed(struct shm_handle
*handle
,
591 struct lib_ring_buffer
*buf
, unsigned long *pos
)
593 *pos
= buf
->cons_snapshot
;
597 /* Get the producer position (iteration end) */
598 int ustctl_snapshot_get_produced(struct shm_handle
*handle
,
599 struct lib_ring_buffer
*buf
, unsigned long *pos
)
601 *pos
= buf
->prod_snapshot
;
605 /* Get exclusive read access to the specified sub-buffer position */
606 int ustctl_get_subbuf(struct shm_handle
*handle
,
607 struct lib_ring_buffer
*buf
, unsigned long *pos
)
609 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
612 /* Release exclusive sub-buffer access */
613 int ustctl_put_subbuf(struct shm_handle
*handle
,
614 struct lib_ring_buffer
*buf
)
616 lib_ring_buffer_put_subbuf(buf
, handle
);
620 int ustctl_buffer_flush(struct shm_handle
*handle
,
621 struct lib_ring_buffer
*buf
)
623 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);