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
);
374 int ustctl_tracepoint_list(int sock
)
376 return -ENOSYS
; /* not implemented */
379 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
381 struct ustcomm_ust_msg lum
;
382 struct ustcomm_ust_reply lur
;
385 memset(&lum
, 0, sizeof(lum
));
386 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
387 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
388 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
391 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
392 DBG("received tracer version");
396 int ustctl_wait_quiescent(int sock
)
398 struct ustcomm_ust_msg lum
;
399 struct ustcomm_ust_reply lur
;
402 memset(&lum
, 0, sizeof(lum
));
403 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
404 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
405 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
408 DBG("waited for quiescent state");
412 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
417 /* Buffer operations */
419 /* Map channel shm into process memory */
420 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
422 struct lttng_ust_shm_handle
*handle
;
423 struct channel
*chan
;
425 struct lttng_ust_lib_ring_buffer_config
*config
;
428 handle
= channel_handle_create(chan_data
->shm_fd
,
430 chan_data
->memory_map_size
);
432 ERR("create handle error");
436 * Set to -1 because the lttng_ust_shm_handle destruction will take care
437 * of closing shm_fd and wait_fd.
439 chan_data
->shm_fd
= -1;
440 chan_data
->wait_fd
= -1;
443 * TODO: add consistency checks to be resilient if the
444 * application try to feed us with incoherent channel structure
447 chan
= shmp(handle
, handle
->chan
);
448 /* chan is object 0. This is hardcoded. */
449 chan_size
= handle
->table
->objects
[0].allocated_len
;
450 handle
->shadow_chan
= malloc(chan_size
);
451 if (!handle
->shadow_chan
) {
452 channel_destroy(chan
, handle
, 1);
455 memcpy(handle
->shadow_chan
, chan
, chan_size
);
457 * The callback pointers in the producer are invalid in the
458 * consumer. We need to look them up here.
460 config
= &handle
->shadow_chan
->backend
.config
;
461 switch (config
->client_type
) {
462 case LTTNG_CLIENT_METADATA
:
463 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
466 case LTTNG_CLIENT_DISCARD
:
467 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
470 case LTTNG_CLIENT_OVERWRITE
:
471 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
475 ERR("Unknown client type %d", config
->client_type
);
476 channel_destroy(chan
, handle
, 1);
479 /* Replace the object table pointer. */
480 ret
= munmap(handle
->table
->objects
[0].memory_map
,
481 handle
->table
->objects
[0].memory_map_size
);
486 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
487 handle
->table
->objects
[0].is_shadow
= 1;
491 /* Add stream to channel shm and map its shm into process memory */
492 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
493 struct lttng_ust_object_data
*stream_data
)
497 if (!stream_data
->handle
)
500 ret
= channel_handle_add_stream(handle
,
502 stream_data
->wait_fd
,
503 stream_data
->memory_map_size
);
505 ERR("add stream error\n");
509 * Set to -1 because the lttng_ust_shm_handle destruction will take care
510 * of closing shm_fd and wait_fd.
512 stream_data
->shm_fd
= -1;
513 stream_data
->wait_fd
= -1;
517 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
519 struct channel
*chan
;
521 chan
= shmp(handle
, handle
->chan
);
522 channel_destroy(chan
, handle
, 1);
525 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
528 struct channel
*chan
= handle
->shadow_chan
;
530 uint64_t memory_map_size
;
531 struct lttng_ust_lib_ring_buffer
*buf
;
534 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
535 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
538 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
544 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
545 struct lttng_ust_lib_ring_buffer
*buf
)
547 lib_ring_buffer_release_read(buf
, handle
, 1);
550 /* For mmap mode, readable without "get" operation */
552 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
553 struct lttng_ust_lib_ring_buffer
*buf
)
555 return shmp(handle
, buf
->backend
.memory_map
);
558 /* returns the length to mmap. */
559 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
560 struct lttng_ust_lib_ring_buffer
*buf
,
563 unsigned long mmap_buf_len
;
564 struct channel
*chan
= handle
->shadow_chan
;
566 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
568 mmap_buf_len
= chan
->backend
.buf_size
;
569 if (chan
->backend
.extra_reader_sb
)
570 mmap_buf_len
+= chan
->backend
.subbuf_size
;
571 if (mmap_buf_len
> INT_MAX
)
577 /* returns the maximum size for sub-buffers. */
578 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
579 struct lttng_ust_lib_ring_buffer
*buf
,
582 struct channel
*chan
= handle
->shadow_chan
;
584 *len
= chan
->backend
.subbuf_size
;
589 * For mmap mode, operate on the current packet (between get/put or
590 * get_next/put_next).
593 /* returns the offset of the subbuffer belonging to the mmap reader. */
594 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
595 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
597 struct channel
*chan
= handle
->shadow_chan
;
598 unsigned long sb_bindex
;
600 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
602 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
603 buf
->backend
.buf_rsb
.id
);
604 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
608 /* returns the size of the current sub-buffer, without padding (for mmap). */
609 int ustctl_get_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
,
619 /* returns the size of the current sub-buffer, without padding (for mmap). */
620 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
621 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
623 struct channel
*chan
= handle
->shadow_chan
;
625 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
627 *len
= PAGE_ALIGN(*len
);
631 /* Get exclusive read access to the next sub-buffer that can be read. */
632 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
633 struct lttng_ust_lib_ring_buffer
*buf
)
635 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
639 /* Release exclusive sub-buffer access, move consumer forward. */
640 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
641 struct lttng_ust_lib_ring_buffer
*buf
)
643 lib_ring_buffer_put_next_subbuf(buf
, handle
);
649 /* Get a snapshot of the current ring buffer producer and consumer positions */
650 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
651 struct lttng_ust_lib_ring_buffer
*buf
)
653 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
654 &buf
->prod_snapshot
, handle
);
657 /* Get the consumer position (iteration start) */
658 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
659 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
661 *pos
= buf
->cons_snapshot
;
665 /* Get the producer position (iteration end) */
666 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
667 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
669 *pos
= buf
->prod_snapshot
;
673 /* Get exclusive read access to the specified sub-buffer position */
674 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
675 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
677 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
680 /* Release exclusive sub-buffer access */
681 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
682 struct lttng_ust_lib_ring_buffer
*buf
)
684 lib_ring_buffer_put_subbuf(buf
, handle
);
688 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
689 struct lttng_ust_lib_ring_buffer
*buf
,
692 lib_ring_buffer_switch_slow(buf
,
693 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,