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
);
68 * Send registration done packet to the application.
70 int ustctl_register_done(int sock
)
72 struct ustcomm_ust_msg lum
;
73 struct ustcomm_ust_reply lur
;
76 DBG("Sending register done command to %d", sock
);
77 memset(&lum
, 0, sizeof(lum
));
78 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
79 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
80 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
83 if (lur
.ret_code
!= USTCOMM_OK
) {
84 DBG("Return code: %s", ustcomm_get_readable_code(lur
.ret_code
));
94 * returns session handle.
96 int ustctl_create_session(int sock
)
98 struct ustcomm_ust_msg lum
;
99 struct ustcomm_ust_reply lur
;
100 int ret
, session_handle
;
103 memset(&lum
, 0, sizeof(lum
));
104 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
105 lum
.cmd
= LTTNG_UST_SESSION
;
106 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
109 session_handle
= lur
.ret_val
;
110 DBG("received session handle %u", session_handle
);
111 return session_handle
;
114 /* open the metadata global channel */
115 int ustctl_open_metadata(int sock
, int session_handle
,
116 struct lttng_ust_channel_attr
*chops
,
117 struct lttng_ust_object_data
**_metadata_data
)
119 struct ustcomm_ust_msg lum
;
120 struct ustcomm_ust_reply lur
;
121 struct lttng_ust_object_data
*metadata_data
;
124 metadata_data
= malloc(sizeof(*metadata_data
));
127 init_object(metadata_data
);
128 /* Create metadata channel */
129 memset(&lum
, 0, sizeof(lum
));
130 lum
.handle
= session_handle
;
131 lum
.cmd
= LTTNG_UST_METADATA
;
132 lum
.u
.channel
.overwrite
= chops
->overwrite
;
133 lum
.u
.channel
.subbuf_size
= chops
->subbuf_size
;
134 lum
.u
.channel
.num_subbuf
= chops
->num_subbuf
;
135 lum
.u
.channel
.switch_timer_interval
= chops
->switch_timer_interval
;
136 lum
.u
.channel
.read_timer_interval
= chops
->read_timer_interval
;
137 lum
.u
.channel
.output
= chops
->output
;
138 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
143 if (lur
.ret_code
!= USTCOMM_OK
) {
147 metadata_data
->handle
= lur
.ret_val
;
148 DBG("received metadata handle %u", metadata_data
->handle
);
149 metadata_data
->memory_map_size
= lur
.u
.channel
.memory_map_size
;
151 ret
= ustcomm_recv_fd(sock
);
154 metadata_data
->shm_fd
= ret
;
156 ret
= ustcomm_recv_fd(sock
);
159 metadata_data
->wait_fd
= ret
;
160 *_metadata_data
= metadata_data
;
164 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
);
222 * Return -ENOENT if no more stream is available for creation.
223 * Return 0 on success.
224 * Return negative error value on error.
226 int ustctl_create_stream(int sock
, struct lttng_ust_object_data
*channel_data
,
227 struct lttng_ust_object_data
**_stream_data
)
229 struct ustcomm_ust_msg lum
;
230 struct ustcomm_ust_reply lur
;
231 struct lttng_ust_object_data
*stream_data
;
234 stream_data
= malloc(sizeof(*stream_data
));
237 init_object(stream_data
);
238 memset(&lum
, 0, sizeof(lum
));
239 lum
.handle
= channel_data
->handle
;
240 lum
.cmd
= LTTNG_UST_STREAM
;
241 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
246 if (lur
.ret_code
!= USTCOMM_OK
) {
251 stream_data
->handle
= lur
.ret_val
;
252 DBG("received stream handle %u", stream_data
->handle
);
253 stream_data
->memory_map_size
= lur
.u
.stream
.memory_map_size
;
255 fd
= ustcomm_recv_fd(sock
);
258 stream_data
->shm_fd
= fd
;
260 fd
= ustcomm_recv_fd(sock
);
263 stream_data
->wait_fd
= fd
;
264 *_stream_data
= stream_data
;
268 ustctl_release_object(sock
, stream_data
);
272 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
273 struct lttng_ust_object_data
*channel_data
,
274 struct lttng_ust_object_data
**_event_data
)
276 struct ustcomm_ust_msg lum
;
277 struct ustcomm_ust_reply lur
;
278 struct lttng_ust_object_data
*event_data
;
281 event_data
= malloc(sizeof(*event_data
));
284 init_object(event_data
);
285 memset(&lum
, 0, sizeof(lum
));
286 lum
.handle
= channel_data
->handle
;
287 lum
.cmd
= LTTNG_UST_EVENT
;
288 strncpy(lum
.u
.event
.name
, ev
->name
,
289 LTTNG_UST_SYM_NAME_LEN
);
290 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
291 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
296 event_data
->handle
= lur
.ret_val
;
297 DBG("received event handle %u", event_data
->handle
);
298 *_event_data
= event_data
;
302 int ustctl_add_context(int sock
, struct lttng_ust_context
*ctx
,
303 struct lttng_ust_object_data
*obj_data
,
304 struct lttng_ust_object_data
**_context_data
)
306 struct ustcomm_ust_msg lum
;
307 struct ustcomm_ust_reply lur
;
308 struct lttng_ust_object_data
*context_data
;
311 context_data
= malloc(sizeof(*context_data
));
314 init_object(context_data
);
315 memset(&lum
, 0, sizeof(lum
));
316 lum
.handle
= obj_data
->handle
;
317 lum
.cmd
= LTTNG_UST_CONTEXT
;
318 lum
.u
.context
.ctx
= ctx
->ctx
;
319 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
324 context_data
->handle
= lur
.ret_val
;
325 DBG("received context handle %u", context_data
->handle
);
326 *_context_data
= context_data
;
330 /* Enable event, channel and session ioctl */
331 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
333 struct ustcomm_ust_msg lum
;
334 struct ustcomm_ust_reply lur
;
337 memset(&lum
, 0, sizeof(lum
));
338 lum
.handle
= object
->handle
;
339 lum
.cmd
= LTTNG_UST_ENABLE
;
340 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
343 DBG("enabled handle %u", object
->handle
);
347 /* Disable event, channel and session ioctl */
348 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
350 struct ustcomm_ust_msg lum
;
351 struct ustcomm_ust_reply lur
;
354 memset(&lum
, 0, sizeof(lum
));
355 lum
.handle
= object
->handle
;
356 lum
.cmd
= LTTNG_UST_DISABLE
;
357 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
360 DBG("disable handle %u", object
->handle
);
364 int ustctl_start_session(int sock
, int handle
)
366 struct lttng_ust_object_data obj
;
369 return ustctl_enable(sock
, &obj
);
372 int ustctl_stop_session(int sock
, int handle
)
374 struct lttng_ust_object_data obj
;
377 return ustctl_disable(sock
, &obj
);
380 int ustctl_tracepoint_list(int sock
)
382 struct ustcomm_ust_msg lum
;
383 struct ustcomm_ust_reply lur
;
384 int ret
, tp_list_handle
;
386 memset(&lum
, 0, sizeof(lum
));
387 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
388 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
389 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
392 tp_list_handle
= lur
.ret_val
;
393 DBG("received tracepoint list handle %u", tp_list_handle
);
394 return tp_list_handle
;
397 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
398 char iter
[LTTNG_UST_SYM_NAME_LEN
])
400 struct ustcomm_ust_msg lum
;
401 struct ustcomm_ust_reply lur
;
404 memset(&lum
, 0, sizeof(lum
));
405 lum
.handle
= tp_list_handle
;
406 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
407 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
410 DBG("received tracepoint list entry %s", lur
.u
.tracepoint_list_entry
);
411 memcpy(iter
, lur
.u
.tracepoint_list_entry
, LTTNG_UST_SYM_NAME_LEN
);
415 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
417 struct ustcomm_ust_msg lum
;
418 struct ustcomm_ust_reply lur
;
421 memset(&lum
, 0, sizeof(lum
));
422 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
423 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
424 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
427 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
428 DBG("received tracer version");
432 int ustctl_wait_quiescent(int sock
)
434 struct ustcomm_ust_msg lum
;
435 struct ustcomm_ust_reply lur
;
438 memset(&lum
, 0, sizeof(lum
));
439 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
440 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
441 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
444 DBG("waited for quiescent state");
448 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
453 /* Buffer operations */
455 /* Map channel shm into process memory */
456 struct lttng_ust_shm_handle
*ustctl_map_channel(struct lttng_ust_object_data
*chan_data
)
458 struct lttng_ust_shm_handle
*handle
;
459 struct channel
*chan
;
461 struct lttng_ust_lib_ring_buffer_config
*config
;
464 handle
= channel_handle_create(chan_data
->shm_fd
,
466 chan_data
->memory_map_size
);
468 ERR("create handle error");
472 * Set to -1 because the lttng_ust_shm_handle destruction will take care
473 * of closing shm_fd and wait_fd.
475 chan_data
->shm_fd
= -1;
476 chan_data
->wait_fd
= -1;
479 * TODO: add consistency checks to be resilient if the
480 * application try to feed us with incoherent channel structure
483 chan
= shmp(handle
, handle
->chan
);
484 /* chan is object 0. This is hardcoded. */
485 chan_size
= handle
->table
->objects
[0].allocated_len
;
486 handle
->shadow_chan
= malloc(chan_size
);
487 if (!handle
->shadow_chan
) {
488 channel_destroy(chan
, handle
, 1);
491 memcpy(handle
->shadow_chan
, chan
, chan_size
);
493 * The callback pointers in the producer are invalid in the
494 * consumer. We need to look them up here.
496 config
= &handle
->shadow_chan
->backend
.config
;
497 switch (config
->client_type
) {
498 case LTTNG_CLIENT_METADATA
:
499 memcpy(&config
->cb
, lttng_client_callbacks_metadata
,
502 case LTTNG_CLIENT_DISCARD
:
503 memcpy(&config
->cb
, lttng_client_callbacks_discard
,
506 case LTTNG_CLIENT_OVERWRITE
:
507 memcpy(&config
->cb
, lttng_client_callbacks_overwrite
,
511 ERR("Unknown client type %d", config
->client_type
);
512 channel_destroy(chan
, handle
, 1);
515 /* Replace the object table pointer. */
516 ret
= munmap(handle
->table
->objects
[0].memory_map
,
517 handle
->table
->objects
[0].memory_map_size
);
522 handle
->table
->objects
[0].memory_map
= (char *) handle
->shadow_chan
;
523 handle
->table
->objects
[0].is_shadow
= 1;
527 /* Add stream to channel shm and map its shm into process memory */
528 int ustctl_add_stream(struct lttng_ust_shm_handle
*handle
,
529 struct lttng_ust_object_data
*stream_data
)
533 if (!stream_data
->handle
)
536 ret
= channel_handle_add_stream(handle
,
538 stream_data
->wait_fd
,
539 stream_data
->memory_map_size
);
541 ERR("add stream error\n");
545 * Set to -1 because the lttng_ust_shm_handle destruction will take care
546 * of closing shm_fd and wait_fd.
548 stream_data
->shm_fd
= -1;
549 stream_data
->wait_fd
= -1;
553 void ustctl_unmap_channel(struct lttng_ust_shm_handle
*handle
)
555 struct channel
*chan
;
557 chan
= shmp(handle
, handle
->chan
);
558 channel_destroy(chan
, handle
, 1);
561 struct lttng_ust_lib_ring_buffer
*ustctl_open_stream_read(struct lttng_ust_shm_handle
*handle
,
564 struct channel
*chan
= handle
->shadow_chan
;
566 uint64_t memory_map_size
;
567 struct lttng_ust_lib_ring_buffer
*buf
;
570 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
571 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
574 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
580 void ustctl_close_stream_read(struct lttng_ust_shm_handle
*handle
,
581 struct lttng_ust_lib_ring_buffer
*buf
)
583 lib_ring_buffer_release_read(buf
, handle
, 1);
586 /* For mmap mode, readable without "get" operation */
588 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle
*handle
,
589 struct lttng_ust_lib_ring_buffer
*buf
)
591 return shmp(handle
, buf
->backend
.memory_map
);
594 /* returns the length to mmap. */
595 int ustctl_get_mmap_len(struct lttng_ust_shm_handle
*handle
,
596 struct lttng_ust_lib_ring_buffer
*buf
,
599 unsigned long mmap_buf_len
;
600 struct channel
*chan
= handle
->shadow_chan
;
602 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
604 mmap_buf_len
= chan
->backend
.buf_size
;
605 if (chan
->backend
.extra_reader_sb
)
606 mmap_buf_len
+= chan
->backend
.subbuf_size
;
607 if (mmap_buf_len
> INT_MAX
)
613 /* returns the maximum size for sub-buffers. */
614 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle
*handle
,
615 struct lttng_ust_lib_ring_buffer
*buf
,
618 struct channel
*chan
= handle
->shadow_chan
;
620 *len
= chan
->backend
.subbuf_size
;
625 * For mmap mode, operate on the current packet (between get/put or
626 * get_next/put_next).
629 /* returns the offset of the subbuffer belonging to the mmap reader. */
630 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
631 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
633 struct channel
*chan
= handle
->shadow_chan
;
634 unsigned long sb_bindex
;
636 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
638 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
639 buf
->backend
.buf_rsb
.id
);
640 *off
= shmp(handle
, shmp_index(handle
, buf
->backend
.array
, sb_bindex
)->shmp
)->mmap_offset
;
644 /* returns the size of the current sub-buffer, without padding (for mmap). */
645 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle
*handle
,
646 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
648 struct channel
*chan
= handle
->shadow_chan
;
650 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
655 /* returns the size of the current sub-buffer, without padding (for mmap). */
656 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle
*handle
,
657 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *len
)
659 struct channel
*chan
= handle
->shadow_chan
;
661 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
663 *len
= PAGE_ALIGN(*len
);
667 /* Get exclusive read access to the next sub-buffer that can be read. */
668 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle
*handle
,
669 struct lttng_ust_lib_ring_buffer
*buf
)
671 return lib_ring_buffer_get_next_subbuf(buf
, handle
);
675 /* Release exclusive sub-buffer access, move consumer forward. */
676 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle
*handle
,
677 struct lttng_ust_lib_ring_buffer
*buf
)
679 lib_ring_buffer_put_next_subbuf(buf
, handle
);
685 /* Get a snapshot of the current ring buffer producer and consumer positions */
686 int ustctl_snapshot(struct lttng_ust_shm_handle
*handle
,
687 struct lttng_ust_lib_ring_buffer
*buf
)
689 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
690 &buf
->prod_snapshot
, handle
);
693 /* Get the consumer position (iteration start) */
694 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle
*handle
,
695 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
697 *pos
= buf
->cons_snapshot
;
701 /* Get the producer position (iteration end) */
702 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle
*handle
,
703 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
705 *pos
= buf
->prod_snapshot
;
709 /* Get exclusive read access to the specified sub-buffer position */
710 int ustctl_get_subbuf(struct lttng_ust_shm_handle
*handle
,
711 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *pos
)
713 return lib_ring_buffer_get_subbuf(buf
, *pos
, handle
);
716 /* Release exclusive sub-buffer access */
717 int ustctl_put_subbuf(struct lttng_ust_shm_handle
*handle
,
718 struct lttng_ust_lib_ring_buffer
*buf
)
720 lib_ring_buffer_put_subbuf(buf
, handle
);
724 void ustctl_flush_buffer(struct lttng_ust_shm_handle
*handle
,
725 struct lttng_ust_lib_ring_buffer
*buf
,
728 lib_ring_buffer_switch_slow(buf
,
729 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,