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;
43 * If sock is negative, it means we don't have to notify the other side
44 * (e.g. application has already vanished).
46 void ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
48 struct ustcomm_ust_msg lum
;
49 struct ustcomm_ust_reply lur
;
52 if (data
->shm_fd
>= 0)
54 if (data
->wait_fd
>= 0)
57 memset(&lum
, 0, sizeof(lum
));
58 lum
.handle
= data
->handle
;
59 lum
.cmd
= LTTNG_UST_RELEASE
;
60 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
);
167 int ustctl_create_channel(int sock
, int session_handle
,
168 struct lttng_ust_channel_attr
*chops
,
169 struct lttng_ust_object_data
**_channel_data
)
171 struct ustcomm_ust_msg lum
;
172 struct ustcomm_ust_reply lur
;
173 struct lttng_ust_object_data
*channel_data
;
176 channel_data
= malloc(sizeof(*channel_data
));
179 init_object(channel_data
);
180 /* Create metadata channel */
181 memset(&lum
, 0, sizeof(lum
));
182 lum
.handle
= session_handle
;
183 lum
.cmd
= LTTNG_UST_CHANNEL
;
184 lum
.u
.channel
.overwrite
= chops
->overwrite
;
185 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
186 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
187 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
188 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
189 lum
.u
.channel
.output
= chops
->output
;
190 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
195 if (lur
.ret_code
!= USTCOMM_OK
) {
199 channel_data
->handle
= lur
.ret_val
;
200 DBG("received channel handle %u", channel_data
->handle
);
201 channel_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
203 ret
= ustcomm_recv_fd(sock
);
206 channel_data
->shm_fd
= ret
;
208 ret
= ustcomm_recv_fd(sock
);
211 channel_data
->wait_fd
= ret
;
212 *_channel_data
= channel_data
;
216 ustctl_release_object(sock
, channel_data
);
221 * Return -ENOENT if no more stream is available for creation.
222 * Return 0 on success.
223 * Return negative error value on error.
225 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
226 struct lttng_ust_object_data
**_stream_data
)
228 struct ustcomm_ust_msg lum
;
229 struct ustcomm_ust_reply lur
;
230 struct lttng_ust_object_data
*stream_data
;
233 stream_data
= malloc(sizeof(*stream_data
));
236 init_object(stream_data
);
237 memset(&lum
, 0, sizeof(lum
));
238 lum
.handle
= channel_data
->handle
;
239 lum
.cmd
= LTTNG_UST_STREAM
;
240 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
245 if (lur
.ret_code
!= USTCOMM_OK
) {
250 stream_data
->handle
= lur
.ret_val
;
251 DBG("received stream handle %u", stream_data
->handle
);
252 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
254 fd
= ustcomm_recv_fd(sock
);
257 stream_data
->shm_fd
= fd
;
259 fd
= ustcomm_recv_fd(sock
);
262 stream_data
->wait_fd
= fd
;
263 *_stream_data
= stream_data
;
267 ustctl_release_object(sock
, stream_data
);
271 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
272 struct lttng_ust_object_data
*channel_data
,
273 struct lttng_ust_object_data
**_event_data
)
275 struct ustcomm_ust_msg lum
;
276 struct ustcomm_ust_reply lur
;
277 struct lttng_ust_object_data
*event_data
;
280 event_data
= malloc(sizeof(*event_data
));
283 init_object(event_data
);
284 memset(&lum
, 0, sizeof(lum
));
285 lum
.handle
= channel_data
->handle
;
286 lum
.cmd
= LTTNG_UST_EVENT
;
287 strncpy(lum
.u
.event
.name
, ev
->name
,
288 LTTNG_UST_SYM_NAME_LEN
);
289 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
290 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
295 event_data
->handle
= lur
.ret_val
;
296 DBG("received event handle %u", event_data
->handle
);
297 *_event_data
= event_data
;
301 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
302 struct lttng_ust_object_data
*obj_data
,
303 struct lttng_ust_object_data
**_context_data
)
305 struct ustcomm_ust_msg lum
;
306 struct ustcomm_ust_reply lur
;
307 struct lttng_ust_object_data
*context_data
;
310 context_data
= malloc(sizeof(*context_data
));
313 init_object(context_data
);
314 memset(&lum
, 0, sizeof(lum
));
315 lum
.handle
= obj_data
->handle
;
316 lum
.cmd
= LTTNG_UST_CONTEXT
;
317 lum
.u
.context
.ctx
= ctx
->ctx
;
318 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
323 context_data
->handle
= lur
.ret_val
;
324 DBG("received context handle %u", context_data
->handle
);
325 *_context_data
= context_data
;
329 /* Enable event, channel and session ioctl */
330 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
332 struct ustcomm_ust_msg lum
;
333 struct ustcomm_ust_reply lur
;
336 memset(&lum
, 0, sizeof(lum
));
337 lum
.handle
= object
->handle
;
338 lum
.cmd
= LTTNG_UST_ENABLE
;
339 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
342 DBG("enabled handle %u", object
->handle
);
346 /* Disable event, channel and session ioctl */
347 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
349 struct ustcomm_ust_msg lum
;
350 struct ustcomm_ust_reply lur
;
353 memset(&lum
, 0, sizeof(lum
));
354 lum
.handle
= object
->handle
;
355 lum
.cmd
= LTTNG_UST_DISABLE
;
356 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
359 DBG("disable handle %u", object
->handle
);
363 int ustctl_start_session(int sock
, int handle
)
365 struct lttng_ust_object_data obj
;
368 return ustctl_enable(sock
, &obj
);
371 int ustctl_stop_session(int sock
, int handle
)
373 struct lttng_ust_object_data obj
;
376 return ustctl_disable(sock
, &obj
);
379 int ustctl_tracepoint_list(int sock
)
381 struct ustcomm_ust_msg lum
;
382 struct ustcomm_ust_reply lur
;
383 int ret
, tp_list_handle
;
385 memset(&lum
, 0, sizeof(lum
));
386 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
387 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
388 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
391 tp_list_handle
= lur
.ret_val
;
392 DBG("received tracepoint list handle %u", tp_list_handle
);
393 return tp_list_handle
;
396 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
397 char iter
[LTTNG_UST_SYM_NAME_LEN
])
399 struct ustcomm_ust_msg lum
;
400 struct ustcomm_ust_reply lur
;
403 memset(&lum
, 0, sizeof(lum
));
404 lum
.handle
= tp_list_handle
;
405 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
406 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
409 DBG("received tracepoint list entry %s", lur
.u
.tracepoint_list_entry
);
410 memcpy(iter
, lur
.u
.tracepoint_list_entry
, LTTNG_UST_SYM_NAME_LEN
);
414 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
416 struct ustcomm_ust_msg lum
;
417 struct ustcomm_ust_reply lur
;
420 memset(&lum
, 0, sizeof(lum
));
421 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
422 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
423 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
426 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
427 DBG("received tracer version");
431 int ustctl_wait_quiescent(int sock
)
433 struct ustcomm_ust_msg lum
;
434 struct ustcomm_ust_reply lur
;
437 memset(&lum
, 0, sizeof(lum
));
438 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
439 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
440 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
443 DBG("waited for quiescent state");
447 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
452 /* Buffer operations */
454 /* Map channel shm into process memory */
455 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
457 struct lttng_ust_shm_handle
*handle
;
458 struct channel
*chan
;
460 struct lttng_ust_lib_ring_buffer_config
*config
;
463 handle
= channel_handle_create(chan_data
->shm_fd
,
465 chan_data
->memory_map_size
);
467 ERR("create handle error");
471 * Set to -1 because the lttng_ust_shm_handle destruction will take care
472 * of closing shm_fd and wait_fd.
474 chan_data
->shm_fd
= -1;
475 chan_data
->wait_fd
= -1;
478 * TODO: add consistency checks to be resilient if the
479 * application try to feed us with incoherent channel structure
482 chan
= shmp(handle
, handle
->chan
);
483 /* chan is object 0. This is hardcoded. */
484 chan_size
= handle
->table
->objects
[0].allocated_len
;
485 handle
->shadow_chan
= malloc(chan_size
);
486 if (!handle
->shadow_chan
) {
487 channel_destroy(chan
, handle
, 1);
490 memcpy(handle
->shadow_chan
, chan
, chan_size
);
492 * The callback pointers in the producer are invalid in the
493 * consumer. We need to look them up here.
495 config
= &handle
->shadow_chan
->backend
.config
;
496 switch (config
->client_type
) {
497 case LTTNG_CLIENT_METADATA
:
498 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
501 case LTTNG_CLIENT_DISCARD
:
502 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
505 case LTTNG_CLIENT_OVERWRITE
:
506 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
510 ERR("Unknown client type %d", config
->client_type
);
511 channel_destroy(chan
, handle
, 1);
514 /* Replace the object table pointer. */
515 ret
= munmap(handle
->table
->objects
[0].memory_map
,
516 handle
->table
->objects
[0].memory_map_size
);
521 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
522 handle
->table
->objects
[0].is_shadow
= 1;
526 /* Add stream to channel shm and map its shm into process memory */
527 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
528 struct lttng_ust_object_data
*stream_data
)
532 if (!stream_data
->handle
)
535 ret
= channel_handle_add_stream(handle
,
537 stream_data
->wait_fd
,
538 stream_data
->memory_map_size
);
540 ERR("add stream error\n");
544 * Set to -1 because the lttng_ust_shm_handle destruction will take care
545 * of closing shm_fd and wait_fd.
547 stream_data
->shm_fd
= -1;
548 stream_data
->wait_fd
= -1;
552 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
554 struct channel
*chan
;
556 chan
= shmp(handle
, handle
->chan
);
557 channel_destroy(chan
, handle
, 1);
560 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
563 struct channel
*chan
= handle
->shadow_chan
;
565 uint64_t memory_map_size
;
566 struct lttng_ust_lib_ring_buffer
*buf
;
569 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
570 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
573 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
579 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
580 struct lttng_ust_lib_ring_buffer
*buf
)
582 lib_ring_buffer_release_read(buf
, handle
, 1);
585 /* For mmap mode, readable without "get" operation */
587 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
588 struct lttng_ust_lib_ring_buffer
*buf
)
590 return shmp(handle
, buf
->backend
.memory_map
);
593 /* returns the length to mmap. */
594 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
595 struct lttng_ust_lib_ring_buffer
*buf
,
598 unsigned long mmap_buf_len
;
599 struct channel
*chan
= handle
->shadow_chan
;
601 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
603 mmap_buf_len
= chan
->backend
.buf_size
;
604 if (chan
->backend
.extra_reader_sb
)
605 mmap_buf_len
+= chan
->backend
.subbuf_size
;
606 if (mmap_buf_len
> INT_MAX
)
612 /* returns the maximum size for sub-buffers. */
613 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
614 struct lttng_ust_lib_ring_buffer
*buf
,
617 struct channel
*chan
= handle
->shadow_chan
;
619 *len
= chan
->backend
.subbuf_size
;
624 * For mmap mode, operate on the current packet (between get/put or
625 * get_next/put_next).
628 /* returns the offset of the subbuffer belonging to the mmap reader. */
629 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
630 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
632 struct channel
*chan
= handle
->shadow_chan
;
633 unsigned long sb_bindex
;
635 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
637 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
638 buf
->backend
.buf_rsb
.id
);
639 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
643 /* returns the size of the current sub-buffer, without padding (for mmap). */
644 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
645 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
647 struct channel
*chan
= handle
->shadow_chan
;
649 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
654 /* returns the size of the current sub-buffer, without padding (for mmap). */
655 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
656 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
658 struct channel
*chan
= handle
->shadow_chan
;
660 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
662 *len
= PAGE_ALIGN(*len
);
666 /* Get exclusive read access to the next sub-buffer that can be read. */
667 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
668 struct lttng_ust_lib_ring_buffer
*buf
)
670 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
674 /* Release exclusive sub-buffer access, move consumer forward. */
675 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
676 struct lttng_ust_lib_ring_buffer
*buf
)
678 lib_ring_buffer_put_next_subbuf(buf
, handle
);
684 /* Get a snapshot of the current ring buffer producer and consumer positions */
685 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
686 struct lttng_ust_lib_ring_buffer
*buf
)
688 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
689 &buf
->prod_snapshot
, handle
);
692 /* Get the consumer position (iteration start) */
693 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
694 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
696 *pos
= buf
->cons_snapshot
;
700 /* Get the producer position (iteration end) */
701 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
702 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
704 *pos
= buf
->prod_snapshot
;
708 /* Get exclusive read access to the specified sub-buffer position */
709 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
710 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
712 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
715 /* Release exclusive sub-buffer access */
716 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
717 struct lttng_ust_lib_ring_buffer
*buf
)
719 lib_ring_buffer_put_subbuf(buf
, handle
);
723 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
724 struct lttng_ust_lib_ring_buffer
*buf
,
727 lib_ring_buffer_switch_slow(buf
,
728 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,