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;
44 * If sock is negative, it means we don't have to notify the other side
45 * (e.g. application has already vanished).
47 void ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
49 struct ustcomm_ust_msg lum
;
50 struct ustcomm_ust_reply lur
;
53 if (data
->shm_fd
>= 0)
55 if (data
->wait_fd
>= 0)
58 memset(&lum
, 0, sizeof(lum
));
59 lum
.handle
= data
->handle
;
60 lum
.cmd
= LTTNG_UST_RELEASE
;
61 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
67 * Send registration done packet to the application.
69 int ustctl_register_done(int sock
)
71 struct ustcomm_ust_msg lum
;
72 struct ustcomm_ust_reply lur
;
75 DBG("Sending register done command to %d", sock
);
76 memset(&lum
, 0, sizeof(lum
));
77 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
78 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
79 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
82 if (lur
.ret_code
!= USTCOMM_OK
) {
83 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
93 * returns session handle.
95 int ustctl_create_session(int sock
)
97 struct ustcomm_ust_msg lum
;
98 struct ustcomm_ust_reply lur
;
99 int ret
, session_handle
;
102 memset(&lum
, 0, sizeof(lum
));
103 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
104 lum
.cmd
= LTTNG_UST_SESSION
;
105 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
108 session_handle
= lur
.ret_val
;
109 DBG("received session handle %u", session_handle
);
110 return session_handle
;
113 /* open the metadata global channel */
114 int ustctl_open_metadata(int sock
, int session_handle
,
115 struct lttng_ust_channel_attr
*chops
,
116 struct lttng_ust_object_data
**_metadata_data
)
118 struct ustcomm_ust_msg lum
;
119 struct ustcomm_ust_reply lur
;
120 struct lttng_ust_object_data
*metadata_data
;
123 metadata_data
= malloc(sizeof(*metadata_data
));
126 init_object(metadata_data
);
127 /* Create metadata channel */
128 memset(&lum
, 0, sizeof(lum
));
129 lum
.handle
= session_handle
;
130 lum
.cmd
= LTTNG_UST_METADATA
;
131 lum
.u
.channel
.overwrite
= chops
->overwrite
;
132 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
133 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
134 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
135 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
136 lum
.u
.channel
.output
= chops
->output
;
137 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
142 if (lur
.ret_code
!= USTCOMM_OK
) {
146 metadata_data
->handle
= lur
.ret_val
;
147 DBG("received metadata handle %u", metadata_data
->handle
);
148 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
150 ret
= ustcomm_recv_fd(sock
);
153 metadata_data
->shm_fd
= ret
;
155 ret
= ustcomm_recv_fd(sock
);
158 metadata_data
->wait_fd
= ret
;
159 *_metadata_data
= metadata_data
;
163 ustctl_release_object(sock
, metadata_data
);
168 int ustctl_create_channel(int sock
, int session_handle
,
169 struct lttng_ust_channel_attr
*chops
,
170 struct lttng_ust_object_data
**_channel_data
)
172 struct ustcomm_ust_msg lum
;
173 struct ustcomm_ust_reply lur
;
174 struct lttng_ust_object_data
*channel_data
;
177 channel_data
= malloc(sizeof(*channel_data
));
180 init_object(channel_data
);
181 /* Create metadata channel */
182 memset(&lum
, 0, sizeof(lum
));
183 lum
.handle
= session_handle
;
184 lum
.cmd
= LTTNG_UST_CHANNEL
;
185 lum
.u
.channel
.overwrite
= chops
->overwrite
;
186 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
187 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
188 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
189 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
190 lum
.u
.channel
.output
= chops
->output
;
191 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
196 if (lur
.ret_code
!= USTCOMM_OK
) {
200 channel_data
->handle
= lur
.ret_val
;
201 DBG("received channel handle %u", channel_data
->handle
);
202 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
204 ret
= ustcomm_recv_fd(sock
);
207 channel_data
->shm_fd
= ret
;
209 ret
= ustcomm_recv_fd(sock
);
212 channel_data
->wait_fd
= ret
;
213 *_channel_data
= channel_data
;
217 ustctl_release_object(sock
, channel_data
);
223 * Return -ENOENT if no more stream is available for creation.
224 * Return 0 on success.
225 * Return negative error value on error.
227 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
228 struct lttng_ust_object_data
**_stream_data
)
230 struct ustcomm_ust_msg lum
;
231 struct ustcomm_ust_reply lur
;
232 struct lttng_ust_object_data
*stream_data
;
235 stream_data
= malloc(sizeof(*stream_data
));
238 init_object(stream_data
);
239 memset(&lum
, 0, sizeof(lum
));
240 lum
.handle
= channel_data
->handle
;
241 lum
.cmd
= LTTNG_UST_STREAM
;
242 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
247 if (lur
.ret_code
!= USTCOMM_OK
) {
252 stream_data
->handle
= lur
.ret_val
;
253 DBG("received stream handle %u", stream_data
->handle
);
254 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
256 fd
= ustcomm_recv_fd(sock
);
259 stream_data
->shm_fd
= fd
;
261 fd
= ustcomm_recv_fd(sock
);
264 stream_data
->wait_fd
= fd
;
265 *_stream_data
= stream_data
;
269 ustctl_release_object(sock
, stream_data
);
274 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
275 struct lttng_ust_object_data
*channel_data
,
276 struct lttng_ust_object_data
**_event_data
)
278 struct ustcomm_ust_msg lum
;
279 struct ustcomm_ust_reply lur
;
280 struct lttng_ust_object_data
*event_data
;
283 event_data
= malloc(sizeof(*event_data
));
286 init_object(event_data
);
287 memset(&lum
, 0, sizeof(lum
));
288 lum
.handle
= channel_data
->handle
;
289 lum
.cmd
= LTTNG_UST_EVENT
;
290 strncpy(lum
.u
.event
.name
, ev
->name
,
291 LTTNG_UST_SYM_NAME_LEN
);
292 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
293 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
298 event_data
->handle
= lur
.ret_val
;
299 DBG("received event handle %u", event_data
->handle
);
300 *_event_data
= event_data
;
304 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
305 struct lttng_ust_object_data
*obj_data
,
306 struct lttng_ust_object_data
**_context_data
)
308 struct ustcomm_ust_msg lum
;
309 struct ustcomm_ust_reply lur
;
310 struct lttng_ust_object_data
*context_data
;
313 context_data
= malloc(sizeof(*context_data
));
316 init_object(context_data
);
317 memset(&lum
, 0, sizeof(lum
));
318 lum
.handle
= obj_data
->handle
;
319 lum
.cmd
= LTTNG_UST_CONTEXT
;
320 lum
.u
.context
.ctx
= ctx
->ctx
;
321 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
326 context_data
->handle
= lur
.ret_val
;
327 DBG("received context handle %u", context_data
->handle
);
328 *_context_data
= context_data
;
332 /* Enable event, channel and session ioctl */
333 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
335 struct ustcomm_ust_msg lum
;
336 struct ustcomm_ust_reply lur
;
339 memset(&lum
, 0, sizeof(lum
));
340 lum
.handle
= object
->handle
;
341 lum
.cmd
= LTTNG_UST_ENABLE
;
342 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
345 DBG("enabled handle %u", object
->handle
);
349 /* Disable event, channel and session ioctl */
350 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
352 struct ustcomm_ust_msg lum
;
353 struct ustcomm_ust_reply lur
;
356 memset(&lum
, 0, sizeof(lum
));
357 lum
.handle
= object
->handle
;
358 lum
.cmd
= LTTNG_UST_DISABLE
;
359 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
362 DBG("disable handle %u", object
->handle
);
366 int ustctl_start_session(int sock
, int handle
)
368 struct lttng_ust_object_data obj
;
371 return ustctl_enable(sock
, &obj
);
374 int ustctl_stop_session(int sock
, int handle
)
376 struct lttng_ust_object_data obj
;
379 return ustctl_disable(sock
, &obj
);
382 int ustctl_tracepoint_list(int sock
)
384 struct ustcomm_ust_msg lum
;
385 struct ustcomm_ust_reply lur
;
386 int ret
, tp_list_handle
;
388 memset(&lum
, 0, sizeof(lum
));
389 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
390 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
391 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
394 tp_list_handle
= lur
.ret_val
;
395 DBG("received tracepoint list handle %u", tp_list_handle
);
396 return tp_list_handle
;
399 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
400 char iter
[LTTNG_UST_SYM_NAME_LEN
])
402 struct ustcomm_ust_msg lum
;
403 struct ustcomm_ust_reply lur
;
406 memset(&lum
, 0, sizeof(lum
));
407 lum
.handle
= tp_list_handle
;
408 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
409 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
412 DBG("received tracepoint list entry %s", lur
.u
.tracepoint_list_entry
);
413 memcpy(iter
, lur
.u
.tracepoint_list_entry
, LTTNG_UST_SYM_NAME_LEN
);
417 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
419 struct ustcomm_ust_msg lum
;
420 struct ustcomm_ust_reply lur
;
423 memset(&lum
, 0, sizeof(lum
));
424 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
425 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
426 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
429 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
430 DBG("received tracer version");
434 int ustctl_wait_quiescent(int sock
)
436 struct ustcomm_ust_msg lum
;
437 struct ustcomm_ust_reply lur
;
440 memset(&lum
, 0, sizeof(lum
));
441 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
442 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
443 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
446 DBG("waited for quiescent state");
450 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
455 /* Buffer operations */
457 /* Map channel shm into process memory */
458 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
460 struct lttng_ust_shm_handle
*handle
;
461 struct channel
*chan
;
463 struct lttng_ust_lib_ring_buffer_config
*config
;
466 handle
= channel_handle_create(chan_data
->shm_fd
,
468 chan_data
->memory_map_size
);
470 ERR("create handle error");
474 * Set to -1 because the lttng_ust_shm_handle destruction will take care
475 * of closing shm_fd and wait_fd.
477 chan_data
->shm_fd
= -1;
478 chan_data
->wait_fd
= -1;
481 * TODO: add consistency checks to be resilient if the
482 * application try to feed us with incoherent channel structure
485 chan
= shmp(handle
, handle
->chan
);
486 /* chan is object 0. This is hardcoded. */
487 chan_size
= handle
->table
->objects
[0].allocated_len
;
488 handle
->shadow_chan
= malloc(chan_size
);
489 if (!handle
->shadow_chan
) {
490 channel_destroy(chan
, handle
, 1);
493 memcpy(handle
->shadow_chan
, chan
, chan_size
);
495 * The callback pointers in the producer are invalid in the
496 * consumer. We need to look them up here.
498 config
= &handle
->shadow_chan
->backend
.config
;
499 switch (config
->client_type
) {
500 case LTTNG_CLIENT_METADATA
:
501 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
504 case LTTNG_CLIENT_DISCARD
:
505 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
508 case LTTNG_CLIENT_OVERWRITE
:
509 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
513 ERR("Unknown client type %d", config
->client_type
);
514 channel_destroy(chan
, handle
, 1);
517 /* Replace the object table pointer. */
518 ret
= munmap(handle
->table
->objects
[0].memory_map
,
519 handle
->table
->objects
[0].memory_map_size
);
524 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
525 handle
->table
->objects
[0].is_shadow
= 1;
529 /* Add stream to channel shm and map its shm into process memory */
530 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
531 struct lttng_ust_object_data
*stream_data
)
535 if (!stream_data
->handle
)
538 ret
= channel_handle_add_stream(handle
,
540 stream_data
->wait_fd
,
541 stream_data
->memory_map_size
);
543 ERR("add stream error\n");
547 * Set to -1 because the lttng_ust_shm_handle destruction will take care
548 * of closing shm_fd and wait_fd.
550 stream_data
->shm_fd
= -1;
551 stream_data
->wait_fd
= -1;
555 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
557 struct channel
*chan
;
559 chan
= shmp(handle
, handle
->chan
);
560 channel_destroy(chan
, handle
, 1);
563 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
566 struct channel
*chan
= handle
->shadow_chan
;
568 uint64_t memory_map_size
;
569 struct lttng_ust_lib_ring_buffer
*buf
;
572 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
573 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
576 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
582 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
583 struct lttng_ust_lib_ring_buffer
*buf
)
585 lib_ring_buffer_release_read(buf
, handle
, 1);
588 /* For mmap mode, readable without "get" operation */
590 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
591 struct lttng_ust_lib_ring_buffer
*buf
)
593 return shmp(handle
, buf
->backend
.memory_map
);
596 /* returns the length to mmap. */
597 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
598 struct lttng_ust_lib_ring_buffer
*buf
,
601 unsigned long mmap_buf_len
;
602 struct channel
*chan
= handle
->shadow_chan
;
604 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
606 mmap_buf_len
= chan
->backend
.buf_size
;
607 if (chan
->backend
.extra_reader_sb
)
608 mmap_buf_len
+= chan
->backend
.subbuf_size
;
609 if (mmap_buf_len
> INT_MAX
)
615 /* returns the maximum size for sub-buffers. */
616 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
617 struct lttng_ust_lib_ring_buffer
*buf
,
620 struct channel
*chan
= handle
->shadow_chan
;
622 *len
= chan
->backend
.subbuf_size
;
627 * For mmap mode, operate on the current packet (between get/put or
628 * get_next/put_next).
631 /* returns the offset of the subbuffer belonging to the mmap reader. */
632 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
633 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
635 struct channel
*chan
= handle
->shadow_chan
;
636 unsigned long sb_bindex
;
638 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
640 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
641 buf
->backend
.buf_rsb
.id
);
642 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
646 /* returns the size of the current sub-buffer, without padding (for mmap). */
647 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
648 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
650 struct channel
*chan
= handle
->shadow_chan
;
652 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
657 /* returns the size of the current sub-buffer, without padding (for mmap). */
658 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
659 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
661 struct channel
*chan
= handle
->shadow_chan
;
663 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
665 *len
= PAGE_ALIGN(*len
);
669 /* Get exclusive read access to the next sub-buffer that can be read. */
670 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
671 struct lttng_ust_lib_ring_buffer
*buf
)
673 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
677 /* Release exclusive sub-buffer access, move consumer forward. */
678 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
679 struct lttng_ust_lib_ring_buffer
*buf
)
681 lib_ring_buffer_put_next_subbuf(buf
, handle
);
687 /* Get a snapshot of the current ring buffer producer and consumer positions */
688 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
689 struct lttng_ust_lib_ring_buffer
*buf
)
691 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
692 &buf
->prod_snapshot
, handle
);
695 /* Get the consumer position (iteration start) */
696 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
697 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
699 *pos
= buf
->cons_snapshot
;
703 /* Get the producer position (iteration end) */
704 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
705 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
707 *pos
= buf
->prod_snapshot
;
711 /* Get exclusive read access to the specified sub-buffer position */
712 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
713 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
715 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
718 /* Release exclusive sub-buffer access */
719 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
720 struct lttng_ust_lib_ring_buffer
*buf
)
722 lib_ring_buffer_put_subbuf(buf
, handle
);
726 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
727 struct lttng_ust_lib_ring_buffer
*buf
,
730 lib_ring_buffer_switch_slow(buf
,
731 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,