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>
28 #include "../libringbuffer/backend.h"
29 #include "../libringbuffer/frontend.h"
31 volatile enum ust_loglevel ust_loglevel
;
34 void init_object(struct lttng_ust_object_data
*data
)
39 data
->memory_map_size
= 0;
42 void ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
44 struct ustcomm_ust_msg lum
;
45 struct ustcomm_ust_reply lur
;
48 if (data
->shm_fd
>= 0)
50 if (data
->wait_fd
>= 0)
52 memset(&lum
, 0, sizeof(lum
));
53 lum
.handle
= data
->handle
;
54 lum
.cmd
= LTTNG_UST_RELEASE
;
55 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
61 * Send registration done packet to the application.
63 int ustctl_register_done(int sock
)
65 struct ustcomm_ust_msg lum
;
66 struct ustcomm_ust_reply lur
;
69 DBG("Sending register done command to %d", sock
);
70 memset(&lum
, 0, sizeof(lum
));
71 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
72 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
73 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
76 if (lur
.ret_code
!= USTCOMM_OK
) {
77 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
87 * returns session handle.
89 int ustctl_create_session(int sock
)
91 struct ustcomm_ust_msg lum
;
92 struct ustcomm_ust_reply lur
;
93 int ret
, session_handle
;
96 memset(&lum
, 0, sizeof(lum
));
97 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
98 lum
.cmd
= LTTNG_UST_SESSION
;
99 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
102 session_handle
= lur
.ret_val
;
103 DBG("received session handle %u", session_handle
);
104 return session_handle
;
107 /* open the metadata global channel */
108 int ustctl_open_metadata(int sock
, int session_handle
,
109 struct lttng_ust_channel_attr
*chops
,
110 struct lttng_ust_object_data
**_metadata_data
)
112 struct ustcomm_ust_msg lum
;
113 struct ustcomm_ust_reply lur
;
114 struct lttng_ust_object_data
*metadata_data
;
117 metadata_data
= malloc(sizeof(*metadata_data
));
120 init_object(metadata_data
);
121 /* Create metadata channel */
122 memset(&lum
, 0, sizeof(lum
));
123 lum
.handle
= session_handle
;
124 lum
.cmd
= LTTNG_UST_METADATA
;
125 lum
.u
.channel
.overwrite
= chops
->overwrite
;
126 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
127 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
128 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
129 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
130 lum
.u
.channel
.output
= chops
->output
;
131 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
136 if (lur
.ret_code
!= USTCOMM_OK
) {
140 metadata_data
->handle
= lur
.ret_val
;
141 DBG("received metadata handle %u", metadata_data
->handle
);
142 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
144 ret
= ustcomm_recv_fd(sock
);
147 metadata_data
->shm_fd
= ret
;
149 ret
= ustcomm_recv_fd(sock
);
152 metadata_data
->wait_fd
= ret
;
153 *_metadata_data
= metadata_data
;
157 ustctl_release_object(sock
, metadata_data
);
161 int ustctl_create_channel(int sock
, int session_handle
,
162 struct lttng_ust_channel_attr
*chops
,
163 struct lttng_ust_object_data
**_channel_data
)
165 struct ustcomm_ust_msg lum
;
166 struct ustcomm_ust_reply lur
;
167 struct lttng_ust_object_data
*channel_data
;
170 channel_data
= malloc(sizeof(*channel_data
));
173 init_object(channel_data
);
174 /* Create metadata channel */
175 memset(&lum
, 0, sizeof(lum
));
176 lum
.handle
= session_handle
;
177 lum
.cmd
= LTTNG_UST_CHANNEL
;
178 lum
.u
.channel
.overwrite
= chops
->overwrite
;
179 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
180 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
181 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
182 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
183 lum
.u
.channel
.output
= chops
->output
;
184 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
189 if (lur
.ret_code
!= USTCOMM_OK
) {
193 channel_data
->handle
= lur
.ret_val
;
194 DBG("received channel handle %u", channel_data
->handle
);
195 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
197 ret
= ustcomm_recv_fd(sock
);
200 channel_data
->shm_fd
= ret
;
202 ret
= ustcomm_recv_fd(sock
);
205 channel_data
->wait_fd
= ret
;
206 *_channel_data
= channel_data
;
210 ustctl_release_object(sock
, channel_data
);
215 * Return -ENOENT if no more stream is available for creation.
216 * Return 0 on success.
217 * Return negative error value on error.
219 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
220 struct lttng_ust_object_data
**_stream_data
)
222 struct ustcomm_ust_msg lum
;
223 struct ustcomm_ust_reply lur
;
224 struct lttng_ust_object_data
*stream_data
;
227 stream_data
= malloc(sizeof(*stream_data
));
230 init_object(stream_data
);
231 memset(&lum
, 0, sizeof(lum
));
232 lum
.handle
= channel_data
->handle
;
233 lum
.cmd
= LTTNG_UST_STREAM
;
234 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
239 if (lur
.ret_code
!= USTCOMM_OK
) {
244 stream_data
->handle
= lur
.ret_val
;
245 DBG("received stream handle %u", stream_data
->handle
);
246 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
248 fd
= ustcomm_recv_fd(sock
);
251 stream_data
->shm_fd
= fd
;
253 fd
= ustcomm_recv_fd(sock
);
256 stream_data
->wait_fd
= fd
;
257 *_stream_data
= stream_data
;
261 ustctl_release_object(sock
, stream_data
);
265 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
266 struct lttng_ust_object_data
*channel_data
,
267 struct lttng_ust_object_data
**_event_data
)
269 struct ustcomm_ust_msg lum
;
270 struct ustcomm_ust_reply lur
;
271 struct lttng_ust_object_data
*event_data
;
274 event_data
= malloc(sizeof(*event_data
));
277 init_object(event_data
);
278 memset(&lum
, 0, sizeof(lum
));
279 lum
.handle
= channel_data
->handle
;
280 lum
.cmd
= LTTNG_UST_EVENT
;
281 strncpy(lum
.u
.event
.name
, ev
->name
,
282 LTTNG_UST_SYM_NAME_LEN
);
283 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
284 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
289 event_data
->handle
= lur
.ret_val
;
290 DBG("received event handle %u", event_data
->handle
);
291 *_event_data
= event_data
;
295 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
296 struct lttng_ust_object_data
*obj_data
,
297 struct lttng_ust_object_data
**_context_data
)
299 struct ustcomm_ust_msg lum
;
300 struct ustcomm_ust_reply lur
;
301 struct lttng_ust_object_data
*context_data
;
304 context_data
= malloc(sizeof(*context_data
));
307 init_object(context_data
);
308 memset(&lum
, 0, sizeof(lum
));
309 lum
.handle
= obj_data
->handle
;
310 lum
.cmd
= LTTNG_UST_CONTEXT
;
311 lum
.u
.context
.ctx
= ctx
->ctx
;
312 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
317 context_data
->handle
= lur
.ret_val
;
318 DBG("received context handle %u", context_data
->handle
);
319 *_context_data
= context_data
;
323 /* Enable event, channel and session ioctl */
324 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
326 struct ustcomm_ust_msg lum
;
327 struct ustcomm_ust_reply lur
;
330 memset(&lum
, 0, sizeof(lum
));
331 lum
.handle
= object
->handle
;
332 lum
.cmd
= LTTNG_UST_ENABLE
;
333 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
336 DBG("enabled handle %u", object
->handle
);
340 /* Disable event, channel and session ioctl */
341 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
343 struct ustcomm_ust_msg lum
;
344 struct ustcomm_ust_reply lur
;
347 memset(&lum
, 0, sizeof(lum
));
348 lum
.handle
= object
->handle
;
349 lum
.cmd
= LTTNG_UST_DISABLE
;
350 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
353 DBG("disable handle %u", object
->handle
);
357 int ustctl_start_session(int sock
, int handle
)
359 struct lttng_ust_object_data obj
;
362 return ustctl_enable(sock
, &obj
);
365 int ustctl_stop_session(int sock
, int handle
)
367 struct lttng_ust_object_data obj
;
370 return ustctl_disable(sock
, &obj
);
373 int ustctl_tracepoint_list(int sock
)
375 struct ustcomm_ust_msg lum
;
376 struct ustcomm_ust_reply lur
;
377 int ret
, tp_list_handle
;
379 memset(&lum
, 0, sizeof(lum
));
380 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
381 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
382 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
385 tp_list_handle
= lur
.ret_val
;
386 DBG("received tracepoint list handle %u", tp_list_handle
);
387 return tp_list_handle
;
390 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
391 char iter
[LTTNG_UST_SYM_NAME_LEN
])
393 struct ustcomm_ust_msg lum
;
394 struct ustcomm_ust_reply lur
;
397 memset(&lum
, 0, sizeof(lum
));
398 lum
.handle
= tp_list_handle
;
399 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
400 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
403 DBG("received tracepoint list entry %s", lur
.u
.tracepoint_list_entry
);
404 memcpy(iter
, lur
.u
.tracepoint_list_entry
, LTTNG_UST_SYM_NAME_LEN
);
408 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
410 struct ustcomm_ust_msg lum
;
411 struct ustcomm_ust_reply lur
;
414 memset(&lum
, 0, sizeof(lum
));
415 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
416 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
417 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
420 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
421 DBG("received tracer version");
425 int ustctl_wait_quiescent(int sock
)
427 struct ustcomm_ust_msg lum
;
428 struct ustcomm_ust_reply lur
;
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
433 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
434 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
437 DBG("waited for quiescent state");
441 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
446 /* Buffer operations */
448 /* Map channel shm into process memory */
449 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
451 struct lttng_ust_shm_handle
*handle
;
452 struct channel
*chan
;
454 struct lttng_ust_lib_ring_buffer_config
*config
;
457 handle
= channel_handle_create(chan_data
->shm_fd
,
459 chan_data
->memory_map_size
);
461 ERR("create handle error");
465 * Set to -1 because the lttng_ust_shm_handle destruction will take care
466 * of closing shm_fd and wait_fd.
468 chan_data
->shm_fd
= -1;
469 chan_data
->wait_fd
= -1;
472 * TODO: add consistency checks to be resilient if the
473 * application try to feed us with incoherent channel structure
476 chan
= shmp(handle
, handle
->chan
);
477 /* chan is object 0. This is hardcoded. */
478 chan_size
= handle
->table
->objects
[0].allocated_len
;
479 handle
->shadow_chan
= malloc(chan_size
);
480 if (!handle
->shadow_chan
) {
481 channel_destroy(chan
, handle
, 1);
484 memcpy(handle
->shadow_chan
, chan
, chan_size
);
486 * The callback pointers in the producer are invalid in the
487 * consumer. We need to look them up here.
489 config
= &handle
->shadow_chan
->backend
.config
;
490 switch (config
->client_type
) {
491 case LTTNG_CLIENT_METADATA
:
492 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
495 case LTTNG_CLIENT_DISCARD
:
496 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
499 case LTTNG_CLIENT_OVERWRITE
:
500 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
504 ERR("Unknown client type %d", config
->client_type
);
505 channel_destroy(chan
, handle
, 1);
508 /* Replace the object table pointer. */
509 ret
= munmap(handle
->table
->objects
[0].memory_map
,
510 handle
->table
->objects
[0].memory_map_size
);
515 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
516 handle
->table
->objects
[0].is_shadow
= 1;
520 /* Add stream to channel shm and map its shm into process memory */
521 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
522 struct lttng_ust_object_data
*stream_data
)
526 if (!stream_data
->handle
)
529 ret
= channel_handle_add_stream(handle
,
531 stream_data
->wait_fd
,
532 stream_data
->memory_map_size
);
534 ERR("add stream error\n");
538 * Set to -1 because the lttng_ust_shm_handle destruction will take care
539 * of closing shm_fd and wait_fd.
541 stream_data
->shm_fd
= -1;
542 stream_data
->wait_fd
= -1;
546 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
548 struct channel
*chan
;
550 chan
= shmp(handle
, handle
->chan
);
551 channel_destroy(chan
, handle
, 1);
554 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
557 struct channel
*chan
= handle
->shadow_chan
;
559 uint64_t memory_map_size
;
560 struct lttng_ust_lib_ring_buffer
*buf
;
563 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
564 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
567 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
573 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
574 struct lttng_ust_lib_ring_buffer
*buf
)
576 lib_ring_buffer_release_read(buf
, handle
, 1);
579 /* For mmap mode, readable without "get" operation */
581 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
582 struct lttng_ust_lib_ring_buffer
*buf
)
584 return shmp(handle
, buf
->backend
.memory_map
);
587 /* returns the length to mmap. */
588 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
589 struct lttng_ust_lib_ring_buffer
*buf
,
592 unsigned long mmap_buf_len
;
593 struct channel
*chan
= handle
->shadow_chan
;
595 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
597 mmap_buf_len
= chan
->backend
.buf_size
;
598 if (chan
->backend
.extra_reader_sb
)
599 mmap_buf_len
+= chan
->backend
.subbuf_size
;
600 if (mmap_buf_len
> INT_MAX
)
606 /* returns the maximum size for sub-buffers. */
607 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
608 struct lttng_ust_lib_ring_buffer
*buf
,
611 struct channel
*chan
= handle
->shadow_chan
;
613 *len
= chan
->backend
.subbuf_size
;
618 * For mmap mode, operate on the current packet (between get/put or
619 * get_next/put_next).
622 /* returns the offset of the subbuffer belonging to the mmap reader. */
623 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
624 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
626 struct channel
*chan
= handle
->shadow_chan
;
627 unsigned long sb_bindex
;
629 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
631 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
632 buf
->backend
.buf_rsb
.id
);
633 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
637 /* returns the size of the current sub-buffer, without padding (for mmap). */
638 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
639 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
641 struct channel
*chan
= handle
->shadow_chan
;
643 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
648 /* returns the size of the current sub-buffer, without padding (for mmap). */
649 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
650 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
652 struct channel
*chan
= handle
->shadow_chan
;
654 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
656 *len
= PAGE_ALIGN(*len
);
660 /* Get exclusive read access to the next sub-buffer that can be read. */
661 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
662 struct lttng_ust_lib_ring_buffer
*buf
)
664 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
668 /* Release exclusive sub-buffer access, move consumer forward. */
669 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
670 struct lttng_ust_lib_ring_buffer
*buf
)
672 lib_ring_buffer_put_next_subbuf(buf
, handle
);
678 /* Get a snapshot of the current ring buffer producer and consumer positions */
679 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
680 struct lttng_ust_lib_ring_buffer
*buf
)
682 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
683 &buf
->prod_snapshot
, handle
);
686 /* Get the consumer position (iteration start) */
687 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
688 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
690 *pos
= buf
->cons_snapshot
;
694 /* Get the producer position (iteration end) */
695 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
696 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
698 *pos
= buf
->prod_snapshot
;
702 /* Get exclusive read access to the specified sub-buffer position */
703 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
704 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
706 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
709 /* Release exclusive sub-buffer access */
710 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
711 struct lttng_ust_lib_ring_buffer
*buf
)
713 lib_ring_buffer_put_subbuf(buf
, handle
);
717 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
718 struct lttng_ust_lib_ring_buffer
*buf
,
721 lib_ring_buffer_switch_slow(buf
,
722 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,