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>
25 #include <lttng/ust-events.h>
27 #include "../libringbuffer/backend.h"
28 #include "../libringbuffer/frontend.h"
30 volatile enum ust_loglevel ust_loglevel
;
33 void init_object(struct lttng_ust_object_data
*data
)
38 data
->memory_map_size
= 0;
41 void ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
43 struct ustcomm_ust_msg lum
;
44 struct ustcomm_ust_reply lur
;
47 if (data
->shm_fd
>= 0)
49 if (data
->wait_fd
>= 0)
51 memset(&lum
, 0, sizeof(lum
));
52 lum
.handle
= data
->handle
;
53 lum
.cmd
= LTTNG_UST_RELEASE
;
54 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
60 * Send registration done packet to the application.
62 int ustctl_register_done(int sock
)
64 struct ustcomm_ust_msg lum
;
65 struct ustcomm_ust_reply lur
;
68 DBG("Sending register done command to %d", sock
);
69 memset(&lum
, 0, sizeof(lum
));
70 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
71 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
72 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
75 if (lur
.ret_code
!= USTCOMM_OK
) {
76 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
86 * returns session handle.
88 int ustctl_create_session(int sock
)
90 struct ustcomm_ust_msg lum
;
91 struct ustcomm_ust_reply lur
;
92 int ret
, session_handle
;
95 memset(&lum
, 0, sizeof(lum
));
96 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
97 lum
.cmd
= LTTNG_UST_SESSION
;
98 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
101 session_handle
= lur
.ret_val
;
102 DBG("received session handle %u", session_handle
);
103 return session_handle
;
106 /* open the metadata global channel */
107 int ustctl_open_metadata(int sock
, int session_handle
,
108 struct lttng_ust_channel_attr
*chops
,
109 struct lttng_ust_object_data
**_metadata_data
)
111 struct ustcomm_ust_msg lum
;
112 struct ustcomm_ust_reply lur
;
113 struct lttng_ust_object_data
*metadata_data
;
116 metadata_data
= malloc(sizeof(*metadata_data
));
119 init_object(metadata_data
);
120 /* Create metadata channel */
121 memset(&lum
, 0, sizeof(lum
));
122 lum
.handle
= session_handle
;
123 lum
.cmd
= LTTNG_UST_METADATA
;
124 lum
.u
.channel
.overwrite
= chops
->overwrite
;
125 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
126 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
127 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
128 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
129 lum
.u
.channel
.output
= chops
->output
;
130 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
135 if (lur
.ret_code
!= USTCOMM_OK
) {
139 metadata_data
->handle
= lur
.ret_val
;
140 DBG("received metadata handle %u", metadata_data
->handle
);
141 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
143 ret
= ustcomm_recv_fd(sock
);
146 metadata_data
->shm_fd
= ret
;
148 ret
= ustcomm_recv_fd(sock
);
151 metadata_data
->wait_fd
= ret
;
152 *_metadata_data
= metadata_data
;
156 ustctl_release_object(sock
, metadata_data
);
160 int ustctl_create_channel(int sock
, int session_handle
,
161 struct lttng_ust_channel_attr
*chops
,
162 struct lttng_ust_object_data
**_channel_data
)
164 struct ustcomm_ust_msg lum
;
165 struct ustcomm_ust_reply lur
;
166 struct lttng_ust_object_data
*channel_data
;
169 channel_data
= malloc(sizeof(*channel_data
));
172 init_object(channel_data
);
173 /* Create metadata channel */
174 memset(&lum
, 0, sizeof(lum
));
175 lum
.handle
= session_handle
;
176 lum
.cmd
= LTTNG_UST_CHANNEL
;
177 lum
.u
.channel
.overwrite
= chops
->overwrite
;
178 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
179 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
180 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
181 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
182 lum
.u
.channel
.output
= chops
->output
;
183 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
188 if (lur
.ret_code
!= USTCOMM_OK
) {
192 channel_data
->handle
= lur
.ret_val
;
193 DBG("received channel handle %u", channel_data
->handle
);
194 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
196 ret
= ustcomm_recv_fd(sock
);
199 channel_data
->shm_fd
= ret
;
201 ret
= ustcomm_recv_fd(sock
);
204 channel_data
->wait_fd
= ret
;
205 *_channel_data
= channel_data
;
209 ustctl_release_object(sock
, channel_data
);
214 * Return -ENOENT if no more stream is available for creation.
215 * Return 0 on success.
216 * Return negative error value on error.
218 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
219 struct lttng_ust_object_data
**_stream_data
)
221 struct ustcomm_ust_msg lum
;
222 struct ustcomm_ust_reply lur
;
223 struct lttng_ust_object_data
*stream_data
;
226 stream_data
= malloc(sizeof(*stream_data
));
229 init_object(stream_data
);
230 memset(&lum
, 0, sizeof(lum
));
231 lum
.handle
= channel_data
->handle
;
232 lum
.cmd
= LTTNG_UST_STREAM
;
233 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
238 if (lur
.ret_code
!= USTCOMM_OK
) {
243 stream_data
->handle
= lur
.ret_val
;
244 DBG("received stream handle %u", stream_data
->handle
);
245 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
247 fd
= ustcomm_recv_fd(sock
);
250 stream_data
->shm_fd
= fd
;
252 fd
= ustcomm_recv_fd(sock
);
255 stream_data
->wait_fd
= fd
;
256 *_stream_data
= stream_data
;
260 ustctl_release_object(sock
, stream_data
);
264 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
265 struct lttng_ust_object_data
*channel_data
,
266 struct lttng_ust_object_data
**_event_data
)
268 struct ustcomm_ust_msg lum
;
269 struct ustcomm_ust_reply lur
;
270 struct lttng_ust_object_data
*event_data
;
273 event_data
= malloc(sizeof(*event_data
));
276 init_object(event_data
);
277 memset(&lum
, 0, sizeof(lum
));
278 lum
.handle
= channel_data
->handle
;
279 lum
.cmd
= LTTNG_UST_EVENT
;
280 strncpy(lum
.u
.event
.name
, ev
->name
,
281 LTTNG_UST_SYM_NAME_LEN
);
282 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
283 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
288 event_data
->handle
= lur
.ret_val
;
289 DBG("received event handle %u", event_data
->handle
);
290 *_event_data
= event_data
;
294 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
295 struct lttng_ust_object_data
*obj_data
,
296 struct lttng_ust_object_data
**_context_data
)
298 struct ustcomm_ust_msg lum
;
299 struct ustcomm_ust_reply lur
;
300 struct lttng_ust_object_data
*context_data
;
303 context_data
= malloc(sizeof(*context_data
));
306 init_object(context_data
);
307 memset(&lum
, 0, sizeof(lum
));
308 lum
.handle
= obj_data
->handle
;
309 lum
.cmd
= LTTNG_UST_CONTEXT
;
310 lum
.u
.context
.ctx
= ctx
->ctx
;
311 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
316 context_data
->handle
= lur
.ret_val
;
317 DBG("received context handle %u", context_data
->handle
);
318 *_context_data
= context_data
;
322 /* Enable event, channel and session ioctl */
323 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
325 struct ustcomm_ust_msg lum
;
326 struct ustcomm_ust_reply lur
;
329 memset(&lum
, 0, sizeof(lum
));
330 lum
.handle
= object
->handle
;
331 lum
.cmd
= LTTNG_UST_ENABLE
;
332 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
335 DBG("enabled handle %u", object
->handle
);
339 /* Disable event, channel and session ioctl */
340 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
342 struct ustcomm_ust_msg lum
;
343 struct ustcomm_ust_reply lur
;
346 memset(&lum
, 0, sizeof(lum
));
347 lum
.handle
= object
->handle
;
348 lum
.cmd
= LTTNG_UST_DISABLE
;
349 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
352 DBG("disable handle %u", object
->handle
);
356 int ustctl_start_session(int sock
, int handle
)
358 struct lttng_ust_object_data obj
;
361 return ustctl_enable(sock
, &obj
);
364 int ustctl_stop_session(int sock
, int handle
)
366 struct lttng_ust_object_data obj
;
369 return ustctl_disable(sock
, &obj
);
373 int ustctl_tracepoint_list(int sock
)
375 return -ENOSYS
; /* not implemented */
378 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
380 struct ustcomm_ust_msg lum
;
381 struct ustcomm_ust_reply lur
;
384 memset(&lum
, 0, sizeof(lum
));
385 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
386 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
387 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
390 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
391 DBG("received tracer version");
395 int ustctl_wait_quiescent(int sock
)
397 struct ustcomm_ust_msg lum
;
398 struct ustcomm_ust_reply lur
;
401 memset(&lum
, 0, sizeof(lum
));
402 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
403 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
404 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
407 DBG("waited for quiescent state");
411 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
416 /* Buffer operations */
418 /* Map channel shm into process memory */
419 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
421 struct lttng_ust_shm_handle
*handle
;
422 struct channel
*chan
;
424 struct lttng_ust_lib_ring_buffer_config
*config
;
426 handle
= channel_handle_create(chan_data
->shm_fd
,
428 chan_data
->memory_map_size
);
430 ERR("create handle error");
434 * Set to -1 because the lttng_ust_shm_handle destruction will take care
435 * of closing shm_fd and wait_fd.
437 chan_data
->shm_fd
= -1;
438 chan_data
->wait_fd
= -1;
441 * TODO: add consistency checks to be resilient if the
442 * application try to feed us with incoherent channel structure
445 chan
= shmp(handle
, handle
->chan
);
446 /* chan is object 0. This is hardcoded. */
447 chan_size
= handle
->table
->objects
[0].allocated_len
;
448 handle
->shadow_chan
= malloc(chan_size
);
449 if (!handle
->shadow_chan
) {
450 channel_destroy(chan
, handle
, 1);
453 memcpy(handle
->shadow_chan
, chan
, chan_size
);
455 * The callback pointers in the producer are invalid in the
456 * consumer. We need to look them up here.
458 config
= &handle
->shadow_chan
->backend
.config
;
459 switch (config
->client_type
) {
460 case LTTNG_CLIENT_METADATA
:
461 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
464 case LTTNG_CLIENT_DISCARD
:
465 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
468 case LTTNG_CLIENT_OVERWRITE
:
469 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
473 ERR("Unknown client type %d", config
->client_type
);
474 channel_destroy(chan
, handle
, 1);
480 /* Add stream to channel shm and map its shm into process memory */
481 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
482 struct lttng_ust_object_data
*stream_data
)
486 if (!stream_data
->handle
)
489 ret
= channel_handle_add_stream(handle
,
491 stream_data
->wait_fd
,
492 stream_data
->memory_map_size
);
494 ERR("add stream error\n");
498 * Set to -1 because the lttng_ust_shm_handle destruction will take care
499 * of closing shm_fd and wait_fd.
501 stream_data
->shm_fd
= -1;
502 stream_data
->wait_fd
= -1;
506 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
508 struct channel
*chan
;
510 chan
= shmp(handle
, handle
->chan
);
511 channel_destroy(chan
, handle
, 1);
514 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
517 struct channel
*chan
= handle
->shadow_chan
;
519 uint64_t memory_map_size
;
520 struct lttng_ust_lib_ring_buffer
*buf
;
523 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
524 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
527 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
533 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
534 struct lttng_ust_lib_ring_buffer
*buf
)
536 lib_ring_buffer_release_read(buf
, handle
, 1);
539 /* For mmap mode, readable without "get" operation */
541 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
542 struct lttng_ust_lib_ring_buffer
*buf
)
544 return shmp(handle
, buf
->backend
.memory_map
);
547 /* returns the length to mmap. */
548 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
549 struct lttng_ust_lib_ring_buffer
*buf
,
552 unsigned long mmap_buf_len
;
553 struct channel
*chan
= handle
->shadow_chan
;
555 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
557 mmap_buf_len
= chan
->backend
.buf_size
;
558 if (chan
->backend
.extra_reader_sb
)
559 mmap_buf_len
+= chan
->backend
.subbuf_size
;
560 if (mmap_buf_len
> INT_MAX
)
566 /* returns the maximum size for sub-buffers. */
567 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
568 struct lttng_ust_lib_ring_buffer
*buf
,
571 struct channel
*chan
= handle
->shadow_chan
;
573 *len
= chan
->backend
.subbuf_size
;
578 * For mmap mode, operate on the current packet (between get/put or
579 * get_next/put_next).
582 /* returns the offset of the subbuffer belonging to the mmap reader. */
583 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
584 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
586 struct channel
*chan
= handle
->shadow_chan
;
587 unsigned long sb_bindex
;
589 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
591 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
592 buf
->backend
.buf_rsb
.id
);
593 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
597 /* returns the size of the current sub-buffer, without padding (for mmap). */
598 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
599 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
601 struct channel
*chan
= handle
->shadow_chan
;
603 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
608 /* returns the size of the current sub-buffer, without padding (for mmap). */
609 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
610 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
612 struct channel
*chan
= handle
->shadow_chan
;
614 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
616 *len
= PAGE_ALIGN(*len
);
620 /* Get exclusive read access to the next sub-buffer that can be read. */
621 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
622 struct lttng_ust_lib_ring_buffer
*buf
)
624 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
628 /* Release exclusive sub-buffer access, move consumer forward. */
629 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
630 struct lttng_ust_lib_ring_buffer
*buf
)
632 lib_ring_buffer_put_next_subbuf(buf
, handle
);
638 /* Get a snapshot of the current ring buffer producer and consumer positions */
639 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
640 struct lttng_ust_lib_ring_buffer
*buf
)
642 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
643 &buf
->prod_snapshot
, handle
);
646 /* Get the consumer position (iteration start) */
647 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
648 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
650 *pos
= buf
->cons_snapshot
;
654 /* Get the producer position (iteration end) */
655 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
656 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
658 *pos
= buf
->prod_snapshot
;
662 /* Get exclusive read access to the specified sub-buffer position */
663 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
664 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
666 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
669 /* Release exclusive sub-buffer access */
670 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
671 struct lttng_ust_lib_ring_buffer
*buf
)
673 lib_ring_buffer_put_subbuf(buf
, handle
);
677 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
678 struct lttng_ust_lib_ring_buffer
*buf
,
681 lib_ring_buffer_switch_slow(buf
,
682 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,