Add flush buffers command
[lttng-ust.git] / libustctl / ustctl.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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
8 * of the License.
9 *
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.
14 *
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.
18 */
19
20#include <string.h>
21#include <ust/lttng-ust-ctl.h>
22#include <ust/lttng-ust-abi.h>
23#include <ust/usterr-signal-safe.h>
24#include <ust/lttng-ust-comm.h>
25
26#include "../libringbuffer/backend.h"
27#include "../libringbuffer/frontend.h"
28
29volatile enum ust_loglevel ust_loglevel;
30
31static
32void init_object(struct object_data *data)
33{
34 data->handle = -1;
35 data->shm_fd = -1;
36 data->wait_fd = -1;
37 data->memory_map_size = 0;
38}
39
40void release_object(int sock, struct object_data *data)
41{
42 struct ustcomm_ust_msg lum;
43 struct ustcomm_ust_reply lur;
44 int ret;
45
46 if (data->shm_fd >= 0)
47 close(data->shm_fd);
48 if (data->wait_fd >= 0)
49 close(data->wait_fd);
50 memset(&lum, 0, sizeof(lum));
51 lum.handle = data->handle;
52 lum.cmd = LTTNG_UST_RELEASE;
53 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
54 assert(!ret);
55 free(data);
56}
57
58/*
59 * Send registration done packet to the application.
60 */
61int ustctl_register_done(int sock)
62{
63 struct ustcomm_ust_msg lum;
64 struct ustcomm_ust_reply lur;
65 int ret;
66
67 DBG("Sending register done command to %d", sock);
68 memset(&lum, 0, sizeof(lum));
69 lum.handle = LTTNG_UST_ROOT_HANDLE;
70 lum.cmd = LTTNG_UST_REGISTER_DONE;
71 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
72 if (ret)
73 return ret;
74 if (lur.ret_code != USTCOMM_OK) {
75 DBG("Return code: %s", ustcomm_get_readable_code(lur.ret_code));
76 goto error;
77 }
78 return 0;
79
80error:
81 return -1;
82}
83
84/*
85 * returns session handle.
86 */
87int ustctl_create_session(int sock)
88{
89 struct ustcomm_ust_msg lum;
90 struct ustcomm_ust_reply lur;
91 int ret, session_handle;
92
93 /* Create session */
94 memset(&lum, 0, sizeof(lum));
95 lum.handle = LTTNG_UST_ROOT_HANDLE;
96 lum.cmd = LTTNG_UST_SESSION;
97 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
98 if (ret)
99 return ret;
100 session_handle = lur.ret_val;
101 DBG("received session handle %u", session_handle);
102 return session_handle;
103}
104
105/* open the metadata global channel */
106int ustctl_open_metadata(int sock, int session_handle,
107 struct lttng_ust_channel_attr *chops,
108 struct object_data **_metadata_data)
109{
110 struct ustcomm_ust_msg lum;
111 struct ustcomm_ust_reply lur;
112 struct object_data *metadata_data;
113 int ret;
114
115 metadata_data = malloc(sizeof(*metadata_data));
116 if (!metadata_data)
117 return -ENOMEM;
118 init_object(metadata_data);
119 /* Create metadata channel */
120 memset(&lum, 0, sizeof(lum));
121 lum.handle = session_handle;
122 lum.cmd = LTTNG_UST_METADATA;
123 lum.u.channel.overwrite = chops->overwrite;
124 lum.u.channel.subbuf_size = chops->subbuf_size;
125 lum.u.channel.num_subbuf = chops->num_subbuf;
126 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
127 lum.u.channel.read_timer_interval = chops->read_timer_interval;
128 lum.u.channel.output = chops->output;
129 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
130 if (ret) {
131 free(metadata_data);
132 return ret;
133 }
134 if (lur.ret_code != USTCOMM_OK) {
135 free(metadata_data);
136 return lur.ret_code;
137 }
138 metadata_data->handle = lur.ret_val;
139 DBG("received metadata handle %u", metadata_data->handle);
140 metadata_data->memory_map_size = lur.u.channel.memory_map_size;
141 /* get shm fd */
142 ret = ustcomm_recv_fd(sock);
143 if (ret < 0)
144 goto error;
145 metadata_data->shm_fd = ret;
146 /* get wait fd */
147 ret = ustcomm_recv_fd(sock);
148 if (ret < 0)
149 goto error;
150 metadata_data->wait_fd = ret;
151 *_metadata_data = metadata_data;
152 return 0;
153
154error:
155 release_object(sock, metadata_data);
156 return -EINVAL;
157}
158
159int ustctl_create_channel(int sock, int session_handle,
160 struct lttng_ust_channel_attr *chops,
161 struct object_data **_channel_data)
162{
163 struct ustcomm_ust_msg lum;
164 struct ustcomm_ust_reply lur;
165 struct object_data *channel_data;
166 int ret;
167
168 channel_data = malloc(sizeof(*channel_data));
169 if (!channel_data)
170 return -ENOMEM;
171 init_object(channel_data);
172 /* Create metadata channel */
173 memset(&lum, 0, sizeof(lum));
174 lum.handle = session_handle;
175 lum.cmd = LTTNG_UST_CHANNEL;
176 lum.u.channel.overwrite = chops->overwrite;
177 lum.u.channel.subbuf_size = chops->subbuf_size;
178 lum.u.channel.num_subbuf = chops->num_subbuf;
179 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
180 lum.u.channel.read_timer_interval = chops->read_timer_interval;
181 lum.u.channel.output = chops->output;
182 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
183 if (ret) {
184 free(channel_data);
185 return ret;
186 }
187 if (lur.ret_code != USTCOMM_OK) {
188 free(channel_data);
189 return lur.ret_code;
190 }
191 channel_data->handle = lur.ret_val;
192 DBG("received channel handle %u", channel_data->handle);
193 channel_data->memory_map_size = lur.u.channel.memory_map_size;
194 /* get shm fd */
195 ret = ustcomm_recv_fd(sock);
196 if (ret < 0)
197 goto error;
198 channel_data->shm_fd = ret;
199 /* get wait fd */
200 ret = ustcomm_recv_fd(sock);
201 if (ret < 0)
202 goto error;
203 channel_data->wait_fd = ret;
204 *_channel_data = channel_data;
205 return 0;
206
207error:
208 release_object(sock, channel_data);
209 return -EINVAL;
210}
211
212/*
213 * Return -ENOENT if no more stream is available for creation.
214 * Return 0 on success.
215 * Return negative error value on error.
216 */
217int ustctl_create_stream(int sock, struct object_data *channel_data,
218 struct object_data **_stream_data)
219{
220 struct ustcomm_ust_msg lum;
221 struct ustcomm_ust_reply lur;
222 struct object_data *stream_data;
223 int ret, fd;
224
225 stream_data = malloc(sizeof(*stream_data));
226 if (!stream_data)
227 return -ENOMEM;
228 init_object(stream_data);
229 memset(&lum, 0, sizeof(lum));
230 lum.handle = channel_data->handle;
231 lum.cmd = LTTNG_UST_STREAM;
232 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
233 if (ret) {
234 free(stream_data);
235 return ret;
236 }
237 if (lur.ret_code != USTCOMM_OK) {
238 free(stream_data);
239 return lur.ret_code;
240 }
241
242 stream_data->handle = lur.ret_val;
243 DBG("received stream handle %u", stream_data->handle);
244 stream_data->memory_map_size = lur.u.stream.memory_map_size;
245 /* get shm fd */
246 fd = ustcomm_recv_fd(sock);
247 if (fd < 0)
248 goto error;
249 stream_data->shm_fd = fd;
250 /* get wait fd */
251 fd = ustcomm_recv_fd(sock);
252 if (fd < 0)
253 goto error;
254 stream_data->wait_fd = fd;
255 *_stream_data = stream_data;
256 return ret;
257
258error:
259 release_object(sock, stream_data);
260 return -EINVAL;
261}
262
263int ustctl_create_event(int sock, struct lttng_ust_event *ev,
264 struct object_data *channel_data,
265 struct object_data **_event_data)
266{
267 struct ustcomm_ust_msg lum;
268 struct ustcomm_ust_reply lur;
269 struct object_data *event_data;
270 int ret;
271
272 event_data = malloc(sizeof(*event_data));
273 if (!event_data)
274 return -ENOMEM;
275 init_object(event_data);
276 memset(&lum, 0, sizeof(lum));
277 lum.handle = channel_data->handle;
278 lum.cmd = LTTNG_UST_EVENT;
279 strncpy(lum.u.event.name, ev->name,
280 LTTNG_UST_SYM_NAME_LEN);
281 lum.u.event.instrumentation = ev->instrumentation;
282 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
283 if (ret) {
284 free(event_data);
285 return ret;
286 }
287 event_data->handle = lur.ret_val;
288 DBG("received event handle %u", event_data->handle);
289 *_event_data = event_data;
290 return 0;
291}
292
293int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
294 struct object_data *channel_data,
295 struct object_data **_context_data)
296{
297 struct ustcomm_ust_msg lum;
298 struct ustcomm_ust_reply lur;
299 struct object_data *context_data;
300 int ret;
301
302 context_data = malloc(sizeof(*context_data));
303 if (!context_data)
304 return -ENOMEM;
305 init_object(context_data);
306 memset(&lum, 0, sizeof(lum));
307 lum.handle = channel_data->handle;
308 lum.cmd = LTTNG_UST_CONTEXT;
309 lum.u.context.ctx = ctx->ctx;
310 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
311 if (ret) {
312 free(context_data);
313 return ret;
314 }
315 context_data->handle = lur.ret_val;
316 DBG("received context handle %u", context_data->handle);
317 *_context_data = context_data;
318 return ret;
319}
320
321/* Enable event, channel and session ioctl */
322int ustctl_enable(int sock, struct object_data *object)
323{
324 struct ustcomm_ust_msg lum;
325 struct ustcomm_ust_reply lur;
326 int ret;
327
328 memset(&lum, 0, sizeof(lum));
329 lum.handle = object->handle;
330 lum.cmd = LTTNG_UST_ENABLE;
331 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
332 if (ret)
333 return ret;
334 DBG("enabled handle %u", object->handle);
335 return 0;
336}
337
338/* Disable event, channel and session ioctl */
339int ustctl_disable(int sock, struct object_data *object)
340{
341 struct ustcomm_ust_msg lum;
342 struct ustcomm_ust_reply lur;
343 int ret;
344
345 memset(&lum, 0, sizeof(lum));
346 lum.handle = object->handle;
347 lum.cmd = LTTNG_UST_DISABLE;
348 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
349 if (ret)
350 return ret;
351 DBG("disable handle %u", object->handle);
352 return 0;
353}
354
355int ustctl_start_session(int sock, struct object_data *object)
356{
357 return ustctl_enable(sock, object);
358}
359
360int ustctl_stop_session(int sock, struct object_data *object)
361{
362 return ustctl_disable(sock, object);
363}
364
365
366int ustctl_tracepoint_list(int sock)
367{
368 return -ENOSYS; /* not implemented */
369}
370
371int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
372{
373 struct ustcomm_ust_msg lum;
374 struct ustcomm_ust_reply lur;
375 int ret;
376
377 memset(&lum, 0, sizeof(lum));
378 lum.handle = LTTNG_UST_ROOT_HANDLE;
379 lum.cmd = LTTNG_UST_TRACER_VERSION;
380 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
381 if (ret)
382 return ret;
383 memcpy(v, &lur.u.version, sizeof(*v));
384 DBG("received tracer version");
385 return 0;
386}
387
388int ustctl_wait_quiescent(int sock)
389{
390 struct ustcomm_ust_msg lum;
391 struct ustcomm_ust_reply lur;
392 int ret;
393
394 memset(&lum, 0, sizeof(lum));
395 lum.handle = LTTNG_UST_ROOT_HANDLE;
396 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
397 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
398 if (ret)
399 return ret;
400 DBG("waited for quiescent state");
401 return 0;
402}
403
404int ustctl_flush_buffers(int sock, struct object_data *channel_data)
405{
406 struct ustcomm_ust_msg lum;
407 struct ustcomm_ust_reply lur;
408
409 memset(&lum, 0, sizeof(lum));
410 lum.handle = channel_data->handle;
411 lum.cmd = LTTNG_UST_FLUSH_BUFFERS;
412 return ustcomm_send_app_cmd(sock, &lum, &lur);
413}
414
415int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
416{
417 return -ENOSYS;
418}
419
420/* Buffer operations */
421
422/* Map channel shm into process memory */
423struct shm_handle *ustctl_map_channel(struct object_data *chan_data)
424{
425 struct shm_handle *handle;
426 struct channel *chan;
427 size_t chan_size;
428
429 handle = channel_handle_create(chan_data->shm_fd,
430 chan_data->wait_fd,
431 chan_data->memory_map_size);
432 if (!handle) {
433 ERR("create handle error");
434 return NULL;
435 }
436 /*
437 * Set to -1 because the shm_handle destruction will take care
438 * of closing shm_fd and wait_fd.
439 */
440 chan_data->shm_fd = -1;
441 chan_data->wait_fd = -1;
442
443 /*
444 * TODO: add consistency checks to be resilient if the
445 * application try to feed us with incoherent channel structure
446 * values.
447 */
448 chan = shmp(handle, handle->chan);
449 /* chan is object 0. This is hardcoded. */
450 chan_size = handle->table->objects[0].allocated_len;
451 handle->shadow_chan = malloc(chan_size);
452 if (!handle->shadow_chan) {
453 channel_destroy(chan, handle, 1);
454 return NULL;
455 }
456 memcpy(handle->shadow_chan, chan, chan_size);
457 return handle;
458}
459
460/* Add stream to channel shm and map its shm into process memory */
461int ustctl_add_stream(struct shm_handle *handle,
462 struct object_data *stream_data)
463{
464 int ret;
465
466 if (!stream_data->handle)
467 return -ENOENT;
468 /* map stream */
469 ret = channel_handle_add_stream(handle,
470 stream_data->shm_fd,
471 stream_data->wait_fd,
472 stream_data->memory_map_size);
473 if (ret) {
474 ERR("add stream error\n");
475 return ret;
476 }
477 /*
478 * Set to -1 because the shm_handle destruction will take care
479 * of closing shm_fd and wait_fd.
480 */
481 stream_data->shm_fd = -1;
482 stream_data->wait_fd = -1;
483 return 0;
484}
485
486void ustctl_unmap_channel(struct shm_handle *handle)
487{
488 struct channel *chan;
489
490 chan = shmp(handle, handle->chan);
491 channel_destroy(chan, handle, 1);
492}
493
494struct lib_ring_buffer *ustctl_open_stream_read(struct shm_handle *handle,
495 int cpu)
496{
497 struct channel *chan = handle->shadow_chan;
498 int shm_fd, wait_fd;
499 uint64_t memory_map_size;
500 struct lib_ring_buffer *buf;
501 int ret;
502
503 buf = channel_get_ring_buffer(&chan->backend.config,
504 chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size);
505 if (!buf)
506 return NULL;
507 ret = lib_ring_buffer_open_read(buf, handle, 1);
508 if (ret)
509 return NULL;
510 return buf;
511}
512
513void ustctl_close_stream_read(struct shm_handle *handle,
514 struct lib_ring_buffer *buf)
515{
516 lib_ring_buffer_release_read(buf, handle, 1);
517}
518
519/* For mmap mode, readable without "get" operation */
520
521void *ustctl_get_mmap_base(struct shm_handle *handle,
522 struct lib_ring_buffer *buf)
523{
524 return shmp(handle, buf->backend.memory_map);
525}
526
527/* returns the length to mmap. */
528int ustctl_get_mmap_len(struct shm_handle *handle,
529 struct lib_ring_buffer *buf,
530 unsigned long *len)
531{
532 unsigned long mmap_buf_len;
533 struct channel *chan = handle->shadow_chan;
534
535 if (chan->backend.config.output != RING_BUFFER_MMAP)
536 return -EINVAL;
537 mmap_buf_len = chan->backend.buf_size;
538 if (chan->backend.extra_reader_sb)
539 mmap_buf_len += chan->backend.subbuf_size;
540 if (mmap_buf_len > INT_MAX)
541 return -EFBIG;
542 *len = mmap_buf_len;
543 return 0;
544}
545
546/* returns the maximum size for sub-buffers. */
547int ustctl_get_max_subbuf_size(struct shm_handle *handle,
548 struct lib_ring_buffer *buf,
549 unsigned long *len)
550{
551 struct channel *chan = handle->shadow_chan;
552
553 *len = chan->backend.subbuf_size;
554 return 0;
555}
556
557/*
558 * For mmap mode, operate on the current packet (between get/put or
559 * get_next/put_next).
560 */
561
562/* returns the offset of the subbuffer belonging to the mmap reader. */
563int ustctl_get_mmap_read_offset(struct shm_handle *handle,
564 struct lib_ring_buffer *buf, unsigned long *off)
565{
566 struct channel *chan = handle->shadow_chan;
567 unsigned long sb_bindex;
568
569 if (chan->backend.config.output != RING_BUFFER_MMAP)
570 return -EINVAL;
571 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
572 buf->backend.buf_rsb.id);
573 *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
574 return 0;
575}
576
577/* returns the size of the current sub-buffer, without padding (for mmap). */
578int ustctl_get_subbuf_size(struct shm_handle *handle,
579 struct lib_ring_buffer *buf, unsigned long *len)
580{
581 struct channel *chan = handle->shadow_chan;
582
583 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
584 handle);
585 return 0;
586}
587
588/* returns the size of the current sub-buffer, without padding (for mmap). */
589int ustctl_get_padded_subbuf_size(struct shm_handle *handle,
590 struct lib_ring_buffer *buf, unsigned long *len)
591{
592 struct channel *chan = handle->shadow_chan;
593
594 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
595 handle);
596 *len = PAGE_ALIGN(*len);
597 return 0;
598}
599
600/* Get exclusive read access to the next sub-buffer that can be read. */
601int ustctl_get_next_subbuf(struct shm_handle *handle,
602 struct lib_ring_buffer *buf)
603{
604 return lib_ring_buffer_get_next_subbuf(buf, handle);
605}
606
607
608/* Release exclusive sub-buffer access, move consumer forward. */
609int ustctl_put_next_subbuf(struct shm_handle *handle,
610 struct lib_ring_buffer *buf)
611{
612 lib_ring_buffer_put_next_subbuf(buf, handle);
613 return 0;
614}
615
616/* snapshot */
617
618/* Get a snapshot of the current ring buffer producer and consumer positions */
619int ustctl_snapshot(struct shm_handle *handle,
620 struct lib_ring_buffer *buf)
621{
622 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
623 &buf->prod_snapshot, handle);
624}
625
626/* Get the consumer position (iteration start) */
627int ustctl_snapshot_get_consumed(struct shm_handle *handle,
628 struct lib_ring_buffer *buf, unsigned long *pos)
629{
630 *pos = buf->cons_snapshot;
631 return 0;
632}
633
634/* Get the producer position (iteration end) */
635int ustctl_snapshot_get_produced(struct shm_handle *handle,
636 struct lib_ring_buffer *buf, unsigned long *pos)
637{
638 *pos = buf->prod_snapshot;
639 return 0;
640}
641
642/* Get exclusive read access to the specified sub-buffer position */
643int ustctl_get_subbuf(struct shm_handle *handle,
644 struct lib_ring_buffer *buf, unsigned long *pos)
645{
646 return lib_ring_buffer_get_subbuf(buf, *pos, handle);
647}
648
649/* Release exclusive sub-buffer access */
650int ustctl_put_subbuf(struct shm_handle *handle,
651 struct lib_ring_buffer *buf)
652{
653 lib_ring_buffer_put_subbuf(buf, handle);
654 return 0;
655}
656
657int ustctl_buffer_flush(struct shm_handle *handle,
658 struct lib_ring_buffer *buf)
659{
660 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle);
661 return 0;
662}
This page took 0.027402 seconds and 4 git commands to generate.