cf7cf87a60d56f00d39bff9a0f8c6ceeb8eba629
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
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 modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <string.h>
21 #include <lttng/ust-ctl.h>
22 #include <lttng/ust-abi.h>
23 #include <lttng/ust-events.h>
24 #include <sys/mman.h>
25
26 #include <usterr-signal-safe.h>
27 #include <ust-comm.h>
28
29 #include "../libringbuffer/backend.h"
30 #include "../libringbuffer/frontend.h"
31
32 volatile enum ust_loglevel ust_loglevel;
33
34 static
35 void init_object(struct lttng_ust_object_data *data)
36 {
37 data->handle = -1;
38 data->shm_fd = -1;
39 data->wait_fd = -1;
40 data->memory_map_size = 0;
41 }
42
43 int ustctl_release_handle(int sock, int handle)
44 {
45 struct ustcomm_ust_msg lum;
46 struct ustcomm_ust_reply lur;
47 int ret;
48
49 if (sock >= 0) {
50 memset(&lum, 0, sizeof(lum));
51 lum.handle = handle;
52 lum.cmd = LTTNG_UST_RELEASE;
53 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
54 if (ret < 0) {
55 return ret;
56 }
57 }
58 return 0;
59 }
60 /*
61 * If sock is negative, it means we don't have to notify the other side
62 * (e.g. application has already vanished).
63 */
64 int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
65 {
66 int ret;
67
68 if (!data)
69 return -EINVAL;
70
71 if (data->shm_fd >= 0) {
72 ret = close(data->shm_fd);
73 if (ret < 0) {
74 return ret;
75 }
76 }
77 if (data->wait_fd >= 0) {
78 ret = close(data->wait_fd);
79 if (ret < 0) {
80 return ret;
81 }
82 }
83 return ustctl_release_handle(sock, data->handle);
84 }
85
86 /*
87 * Send registration done packet to the application.
88 */
89 int ustctl_register_done(int sock)
90 {
91 struct ustcomm_ust_msg lum;
92 struct ustcomm_ust_reply lur;
93 int ret;
94
95 DBG("Sending register done command to %d", sock);
96 memset(&lum, 0, sizeof(lum));
97 lum.handle = LTTNG_UST_ROOT_HANDLE;
98 lum.cmd = LTTNG_UST_REGISTER_DONE;
99 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
100 if (ret)
101 return ret;
102 if (lur.ret_code != USTCOMM_OK) {
103 DBG("Return code: %s", ustcomm_get_readable_code(lur.ret_code));
104 goto error;
105 }
106 return 0;
107
108 error:
109 return -1;
110 }
111
112 /*
113 * returns session handle.
114 */
115 int ustctl_create_session(int sock)
116 {
117 struct ustcomm_ust_msg lum;
118 struct ustcomm_ust_reply lur;
119 int ret, session_handle;
120
121 /* Create session */
122 memset(&lum, 0, sizeof(lum));
123 lum.handle = LTTNG_UST_ROOT_HANDLE;
124 lum.cmd = LTTNG_UST_SESSION;
125 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
126 if (ret)
127 return ret;
128 session_handle = lur.ret_val;
129 DBG("received session handle %u", session_handle);
130 return session_handle;
131 }
132
133 /* open the metadata global channel */
134 int ustctl_open_metadata(int sock, int session_handle,
135 struct lttng_ust_channel_attr *chops,
136 struct lttng_ust_object_data **_metadata_data)
137 {
138 struct ustcomm_ust_msg lum;
139 struct ustcomm_ust_reply lur;
140 struct lttng_ust_object_data *metadata_data;
141 int ret, err = 0;
142
143 if (!chops || !_metadata_data)
144 return -EINVAL;
145
146 metadata_data = malloc(sizeof(*metadata_data));
147 if (!metadata_data)
148 return -ENOMEM;
149 init_object(metadata_data);
150 /* Create metadata channel */
151 memset(&lum, 0, sizeof(lum));
152 lum.handle = session_handle;
153 lum.cmd = LTTNG_UST_METADATA;
154 lum.u.channel.overwrite = chops->overwrite;
155 lum.u.channel.subbuf_size = chops->subbuf_size;
156 lum.u.channel.num_subbuf = chops->num_subbuf;
157 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
158 lum.u.channel.read_timer_interval = chops->read_timer_interval;
159 lum.u.channel.output = chops->output;
160 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
161 if (ret) {
162 free(metadata_data);
163 return ret;
164 }
165 if (lur.ret_code != USTCOMM_OK) {
166 free(metadata_data);
167 return lur.ret_code;
168 }
169 metadata_data->handle = lur.ret_val;
170 DBG("received metadata handle %u", metadata_data->handle);
171 metadata_data->memory_map_size = lur.u.channel.memory_map_size;
172 /* get shm fd */
173 ret = ustcomm_recv_fd(sock);
174 if (ret < 0)
175 err = 1;
176 else
177 metadata_data->shm_fd = ret;
178 /*
179 * We need to get the second FD even if the first fails, because
180 * libust expects us to read the two FDs.
181 */
182 /* get wait fd */
183 ret = ustcomm_recv_fd(sock);
184 if (ret < 0)
185 err = 1;
186 else
187 metadata_data->wait_fd = ret;
188 if (err)
189 goto error;
190 *_metadata_data = metadata_data;
191 return 0;
192
193 error:
194 (void) ustctl_release_object(sock, metadata_data);
195 free(metadata_data);
196 return -EINVAL;
197 }
198
199 int ustctl_create_channel(int sock, int session_handle,
200 struct lttng_ust_channel_attr *chops,
201 struct lttng_ust_object_data **_channel_data)
202 {
203 struct ustcomm_ust_msg lum;
204 struct ustcomm_ust_reply lur;
205 struct lttng_ust_object_data *channel_data;
206 int ret, err = 0;
207
208 if (!chops || !_channel_data)
209 return -EINVAL;
210
211 channel_data = malloc(sizeof(*channel_data));
212 if (!channel_data)
213 return -ENOMEM;
214 init_object(channel_data);
215 /* Create metadata channel */
216 memset(&lum, 0, sizeof(lum));
217 lum.handle = session_handle;
218 lum.cmd = LTTNG_UST_CHANNEL;
219 lum.u.channel.overwrite = chops->overwrite;
220 lum.u.channel.subbuf_size = chops->subbuf_size;
221 lum.u.channel.num_subbuf = chops->num_subbuf;
222 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
223 lum.u.channel.read_timer_interval = chops->read_timer_interval;
224 lum.u.channel.output = chops->output;
225 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
226 if (ret) {
227 free(channel_data);
228 return ret;
229 }
230 if (lur.ret_code != USTCOMM_OK) {
231 free(channel_data);
232 return lur.ret_code;
233 }
234 channel_data->handle = lur.ret_val;
235 DBG("received channel handle %u", channel_data->handle);
236 channel_data->memory_map_size = lur.u.channel.memory_map_size;
237 /* get shm fd */
238 ret = ustcomm_recv_fd(sock);
239 if (ret < 0)
240 err = 1;
241 else
242 channel_data->shm_fd = ret;
243 /*
244 * We need to get the second FD even if the first fails, because
245 * libust expects us to read the two FDs.
246 */
247 /* get wait fd */
248 ret = ustcomm_recv_fd(sock);
249 if (ret < 0)
250 err = 1;
251 else
252 channel_data->wait_fd = ret;
253 if (err)
254 goto error;
255 *_channel_data = channel_data;
256 return 0;
257
258 error:
259 (void) ustctl_release_object(sock, channel_data);
260 free(channel_data);
261 return -EINVAL;
262 }
263
264 /*
265 * Return -ENOENT if no more stream is available for creation.
266 * Return 0 on success.
267 * Return negative error value on error.
268 */
269 int ustctl_create_stream(int sock, struct lttng_ust_object_data *channel_data,
270 struct lttng_ust_object_data **_stream_data)
271 {
272 struct ustcomm_ust_msg lum;
273 struct ustcomm_ust_reply lur;
274 struct lttng_ust_object_data *stream_data;
275 int ret, fd, err = 0;
276
277 if (!channel_data || !_stream_data)
278 return -EINVAL;
279
280 stream_data = malloc(sizeof(*stream_data));
281 if (!stream_data)
282 return -ENOMEM;
283 init_object(stream_data);
284 memset(&lum, 0, sizeof(lum));
285 lum.handle = channel_data->handle;
286 lum.cmd = LTTNG_UST_STREAM;
287 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
288 if (ret) {
289 free(stream_data);
290 return ret;
291 }
292 if (lur.ret_code != USTCOMM_OK) {
293 free(stream_data);
294 return lur.ret_code;
295 }
296
297 stream_data->handle = lur.ret_val;
298 DBG("received stream handle %u", stream_data->handle);
299 stream_data->memory_map_size = lur.u.stream.memory_map_size;
300 /* get shm fd */
301 fd = ustcomm_recv_fd(sock);
302 if (fd < 0)
303 err = 1;
304 else
305 stream_data->shm_fd = fd;
306 /*
307 * We need to get the second FD even if the first fails, because
308 * libust expects us to read the two FDs.
309 */
310 /* get wait fd */
311 fd = ustcomm_recv_fd(sock);
312 if (fd < 0)
313 err = 1;
314 else
315 stream_data->wait_fd = fd;
316 if (err)
317 goto error;
318 *_stream_data = stream_data;
319 return ret;
320
321 error:
322 (void) ustctl_release_object(sock, stream_data);
323 free(stream_data);
324 return -EINVAL;
325 }
326
327 int ustctl_create_event(int sock, struct lttng_ust_event *ev,
328 struct lttng_ust_object_data *channel_data,
329 struct lttng_ust_object_data **_event_data)
330 {
331 struct ustcomm_ust_msg lum;
332 struct ustcomm_ust_reply lur;
333 struct lttng_ust_object_data *event_data;
334 int ret;
335
336 if (!channel_data || !_event_data)
337 return -EINVAL;
338
339 event_data = malloc(sizeof(*event_data));
340 if (!event_data)
341 return -ENOMEM;
342 init_object(event_data);
343 memset(&lum, 0, sizeof(lum));
344 lum.handle = channel_data->handle;
345 lum.cmd = LTTNG_UST_EVENT;
346 strncpy(lum.u.event.name, ev->name,
347 LTTNG_UST_SYM_NAME_LEN);
348 lum.u.event.instrumentation = ev->instrumentation;
349 lum.u.event.loglevel_type = ev->loglevel_type;
350 lum.u.event.loglevel = ev->loglevel;
351 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
352 if (ret) {
353 free(event_data);
354 return ret;
355 }
356 event_data->handle = lur.ret_val;
357 DBG("received event handle %u", event_data->handle);
358 *_event_data = event_data;
359 return 0;
360 }
361
362 int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
363 struct lttng_ust_object_data *obj_data,
364 struct lttng_ust_object_data **_context_data)
365 {
366 struct ustcomm_ust_msg lum;
367 struct ustcomm_ust_reply lur;
368 struct lttng_ust_object_data *context_data;
369 int ret;
370
371 if (!obj_data || !_context_data)
372 return -EINVAL;
373
374 context_data = malloc(sizeof(*context_data));
375 if (!context_data)
376 return -ENOMEM;
377 init_object(context_data);
378 memset(&lum, 0, sizeof(lum));
379 lum.handle = obj_data->handle;
380 lum.cmd = LTTNG_UST_CONTEXT;
381 lum.u.context.ctx = ctx->ctx;
382 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
383 if (ret) {
384 free(context_data);
385 return ret;
386 }
387 context_data->handle = lur.ret_val;
388 DBG("received context handle %u", context_data->handle);
389 *_context_data = context_data;
390 return ret;
391 }
392
393 /* Enable event, channel and session ioctl */
394 int ustctl_enable(int sock, struct lttng_ust_object_data *object)
395 {
396 struct ustcomm_ust_msg lum;
397 struct ustcomm_ust_reply lur;
398 int ret;
399
400 if (!object)
401 return -EINVAL;
402
403 memset(&lum, 0, sizeof(lum));
404 lum.handle = object->handle;
405 lum.cmd = LTTNG_UST_ENABLE;
406 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
407 if (ret)
408 return ret;
409 DBG("enabled handle %u", object->handle);
410 return 0;
411 }
412
413 /* Disable event, channel and session ioctl */
414 int ustctl_disable(int sock, struct lttng_ust_object_data *object)
415 {
416 struct ustcomm_ust_msg lum;
417 struct ustcomm_ust_reply lur;
418 int ret;
419
420 if (!object)
421 return -EINVAL;
422
423 memset(&lum, 0, sizeof(lum));
424 lum.handle = object->handle;
425 lum.cmd = LTTNG_UST_DISABLE;
426 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
427 if (ret)
428 return ret;
429 DBG("disable handle %u", object->handle);
430 return 0;
431 }
432
433 int ustctl_start_session(int sock, int handle)
434 {
435 struct lttng_ust_object_data obj;
436
437 obj.handle = handle;
438 return ustctl_enable(sock, &obj);
439 }
440
441 int ustctl_stop_session(int sock, int handle)
442 {
443 struct lttng_ust_object_data obj;
444
445 obj.handle = handle;
446 return ustctl_disable(sock, &obj);
447 }
448
449 int ustctl_tracepoint_list(int sock)
450 {
451 struct ustcomm_ust_msg lum;
452 struct ustcomm_ust_reply lur;
453 int ret, tp_list_handle;
454
455 memset(&lum, 0, sizeof(lum));
456 lum.handle = LTTNG_UST_ROOT_HANDLE;
457 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
458 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
459 if (ret)
460 return ret;
461 tp_list_handle = lur.ret_val;
462 DBG("received tracepoint list handle %u", tp_list_handle);
463 return tp_list_handle;
464 }
465
466 int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
467 struct lttng_ust_tracepoint_iter *iter)
468 {
469 struct ustcomm_ust_msg lum;
470 struct ustcomm_ust_reply lur;
471 int ret;
472
473 if (!iter)
474 return -EINVAL;
475
476 memset(&lum, 0, sizeof(lum));
477 lum.handle = tp_list_handle;
478 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
479 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
480 if (ret)
481 return ret;
482 DBG("received tracepoint list entry name %s loglevel %d",
483 lur.u.tracepoint.name,
484 lur.u.tracepoint.loglevel);
485 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
486 return 0;
487 }
488
489 int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
490 {
491 struct ustcomm_ust_msg lum;
492 struct ustcomm_ust_reply lur;
493 int ret;
494
495 if (!v)
496 return -EINVAL;
497
498 memset(&lum, 0, sizeof(lum));
499 lum.handle = LTTNG_UST_ROOT_HANDLE;
500 lum.cmd = LTTNG_UST_TRACER_VERSION;
501 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
502 if (ret)
503 return ret;
504 memcpy(v, &lur.u.version, sizeof(*v));
505 DBG("received tracer version");
506 return 0;
507 }
508
509 int ustctl_wait_quiescent(int sock)
510 {
511 struct ustcomm_ust_msg lum;
512 struct ustcomm_ust_reply lur;
513 int ret;
514
515 memset(&lum, 0, sizeof(lum));
516 lum.handle = LTTNG_UST_ROOT_HANDLE;
517 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
518 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
519 if (ret)
520 return ret;
521 DBG("waited for quiescent state");
522 return 0;
523 }
524
525 int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
526 {
527 if (!calibrate)
528 return -EINVAL;
529
530 return -ENOSYS;
531 }
532
533 int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
534 {
535 struct ustcomm_ust_msg lum;
536 struct ustcomm_ust_reply lur;
537 int ret;
538
539 if (!object)
540 return -EINVAL;
541
542 memset(&lum, 0, sizeof(lum));
543 lum.handle = object->handle;
544 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
545 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
546 if (ret)
547 return ret;
548 DBG("flushed buffer handle %u", object->handle);
549 return 0;
550 }
551
552 /* Buffer operations */
553
554 /* Map channel shm into process memory */
555 struct lttng_ust_shm_handle *ustctl_map_channel(struct lttng_ust_object_data *chan_data)
556 {
557 struct lttng_ust_shm_handle *handle;
558 struct channel *chan;
559 size_t chan_size;
560 struct lttng_ust_lib_ring_buffer_config *config;
561 int ret;
562
563 if (!chan_data)
564 return NULL;
565
566 handle = channel_handle_create(chan_data->shm_fd,
567 chan_data->wait_fd,
568 chan_data->memory_map_size);
569 if (!handle) {
570 ERR("create handle error");
571 return NULL;
572 }
573 /*
574 * Set to -1, and then close the shm fd, and set the handle shm
575 * fd to -1 too. We don't need the shm fds after they have been
576 * mapped.
577 * The wait_fd is set to -1 in chan_data because it is now owned
578 * by the handle.
579 */
580 chan_data->shm_fd = -1;
581 chan_data->wait_fd = -1;
582
583 /* chan is object 0. This is hardcoded. */
584 if (handle->table->objects[0].shm_fd >= 0) {
585 ret = close(handle->table->objects[0].shm_fd);
586 if (ret) {
587 perror("Error closing shm_fd");
588 }
589 handle->table->objects[0].shm_fd = -1;
590 }
591
592 /*
593 * TODO: add consistency checks to be resilient if the
594 * application try to feed us with incoherent channel structure
595 * values.
596 */
597 chan = shmp(handle, handle->chan);
598 /* chan is object 0. This is hardcoded. */
599 chan_size = handle->table->objects[0].allocated_len;
600 handle->shadow_chan = malloc(chan_size);
601 if (!handle->shadow_chan) {
602 channel_destroy(chan, handle, 1);
603 return NULL;
604 }
605 memcpy(handle->shadow_chan, chan, chan_size);
606 /*
607 * The callback pointers in the producer are invalid in the
608 * consumer. We need to look them up here.
609 */
610 config = &handle->shadow_chan->backend.config;
611 switch (config->client_type) {
612 case LTTNG_CLIENT_METADATA:
613 memcpy(&config->cb, lttng_client_callbacks_metadata,
614 sizeof(config->cb));
615 break;
616 case LTTNG_CLIENT_DISCARD:
617 memcpy(&config->cb, lttng_client_callbacks_discard,
618 sizeof(config->cb));
619 break;
620 case LTTNG_CLIENT_OVERWRITE:
621 memcpy(&config->cb, lttng_client_callbacks_overwrite,
622 sizeof(config->cb));
623 break;
624 default:
625 ERR("Unknown client type %d", config->client_type);
626 channel_destroy(chan, handle, 1);
627 return NULL;
628 }
629 /* Replace the object table pointer. */
630 ret = munmap(handle->table->objects[0].memory_map,
631 handle->table->objects[0].memory_map_size);
632 if (ret) {
633 perror("munmap");
634 assert(0);
635 }
636 handle->table->objects[0].memory_map = (char *) handle->shadow_chan;
637 handle->table->objects[0].is_shadow = 1;
638 return handle;
639 }
640
641 /* Add stream to channel shm and map its shm into process memory */
642 int ustctl_add_stream(struct lttng_ust_shm_handle *handle,
643 struct lttng_ust_object_data *stream_data)
644 {
645 int ret;
646
647 if (!handle || !stream_data)
648 return -EINVAL;
649
650 if (!stream_data->handle)
651 return -ENOENT;
652 /* map stream */
653 ret = channel_handle_add_stream(handle,
654 stream_data->shm_fd,
655 stream_data->wait_fd,
656 stream_data->memory_map_size);
657 if (ret) {
658 ERR("add stream error\n");
659 return ret;
660 }
661 /*
662 * Set to -1 because the lttng_ust_shm_handle destruction will take care
663 * of closing shm_fd and wait_fd.
664 */
665 stream_data->shm_fd = -1;
666 stream_data->wait_fd = -1;
667 return 0;
668 }
669
670 void ustctl_unmap_channel(struct lttng_ust_shm_handle *handle)
671 {
672 struct channel *chan;
673
674 assert(handle);
675 chan = shmp(handle, handle->chan);
676 channel_destroy(chan, handle, 1);
677 }
678
679 /*
680 * ustctl closes the shm_fd fds after mapping it.
681 */
682 struct lttng_ust_lib_ring_buffer *ustctl_open_stream_read(struct lttng_ust_shm_handle *handle,
683 int cpu)
684 {
685 struct channel *chan;
686 int *shm_fd, *wait_fd;
687 uint64_t *memory_map_size;
688 struct lttng_ust_lib_ring_buffer *buf;
689 int ret;
690
691 if (!handle)
692 return NULL;
693
694 chan = handle->shadow_chan;
695 buf = channel_get_ring_buffer(&chan->backend.config,
696 chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size);
697 if (!buf)
698 return NULL;
699 ret = lib_ring_buffer_open_read(buf, handle, 1);
700 if (ret)
701 return NULL;
702 /*
703 * We can close shm_fd early, right after is has been mapped.
704 */
705 if (*shm_fd >= 0) {
706 ret = close(*shm_fd);
707 if (ret) {
708 perror("Error closing shm_fd");
709 }
710 *shm_fd = -1;
711 }
712 return buf;
713 }
714
715 void ustctl_close_stream_read(struct lttng_ust_shm_handle *handle,
716 struct lttng_ust_lib_ring_buffer *buf)
717 {
718 assert(handle && buf);
719 lib_ring_buffer_release_read(buf, handle, 1);
720 }
721
722 /* For mmap mode, readable without "get" operation */
723
724 void *ustctl_get_mmap_base(struct lttng_ust_shm_handle *handle,
725 struct lttng_ust_lib_ring_buffer *buf)
726 {
727 if (!handle || !buf)
728 return NULL;
729 return shmp(handle, buf->backend.memory_map);
730 }
731
732 /* returns the length to mmap. */
733 int ustctl_get_mmap_len(struct lttng_ust_shm_handle *handle,
734 struct lttng_ust_lib_ring_buffer *buf,
735 unsigned long *len)
736 {
737 unsigned long mmap_buf_len;
738 struct channel *chan;
739
740 if (!handle || !buf || !len)
741 return -EINVAL;
742
743 chan = handle->shadow_chan;
744 if (chan->backend.config.output != RING_BUFFER_MMAP)
745 return -EINVAL;
746 mmap_buf_len = chan->backend.buf_size;
747 if (chan->backend.extra_reader_sb)
748 mmap_buf_len += chan->backend.subbuf_size;
749 if (mmap_buf_len > INT_MAX)
750 return -EFBIG;
751 *len = mmap_buf_len;
752 return 0;
753 }
754
755 /* returns the maximum size for sub-buffers. */
756 int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle *handle,
757 struct lttng_ust_lib_ring_buffer *buf,
758 unsigned long *len)
759 {
760 struct channel *chan;
761
762 if (!handle || !buf || !len)
763 return -EINVAL;
764
765 chan = handle->shadow_chan;
766 *len = chan->backend.subbuf_size;
767 return 0;
768 }
769
770 /*
771 * For mmap mode, operate on the current packet (between get/put or
772 * get_next/put_next).
773 */
774
775 /* returns the offset of the subbuffer belonging to the mmap reader. */
776 int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
777 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
778 {
779 struct channel *chan;
780 unsigned long sb_bindex;
781
782 if (!handle || !buf || !off)
783 return -EINVAL;
784
785 chan = handle->shadow_chan;
786 if (chan->backend.config.output != RING_BUFFER_MMAP)
787 return -EINVAL;
788 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
789 buf->backend.buf_rsb.id);
790 *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
791 return 0;
792 }
793
794 /* returns the size of the current sub-buffer, without padding (for mmap). */
795 int ustctl_get_subbuf_size(struct lttng_ust_shm_handle *handle,
796 struct lttng_ust_lib_ring_buffer *buf, unsigned long *len)
797 {
798 struct channel *chan;
799
800 if (!handle || !buf || !len)
801 return -EINVAL;
802
803 chan = handle->shadow_chan;
804 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
805 handle);
806 return 0;
807 }
808
809 /* returns the size of the current sub-buffer, without padding (for mmap). */
810 int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle *handle,
811 struct lttng_ust_lib_ring_buffer *buf, unsigned long *len)
812 {
813 struct channel *chan;
814
815 if (!handle || !buf || !len)
816 return -EINVAL;
817
818 chan = handle->shadow_chan;
819 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
820 handle);
821 *len = PAGE_ALIGN(*len);
822 return 0;
823 }
824
825 /* Get exclusive read access to the next sub-buffer that can be read. */
826 int ustctl_get_next_subbuf(struct lttng_ust_shm_handle *handle,
827 struct lttng_ust_lib_ring_buffer *buf)
828 {
829 if (!handle || !buf)
830 return -EINVAL;
831
832 return lib_ring_buffer_get_next_subbuf(buf, handle);
833 }
834
835
836 /* Release exclusive sub-buffer access, move consumer forward. */
837 int ustctl_put_next_subbuf(struct lttng_ust_shm_handle *handle,
838 struct lttng_ust_lib_ring_buffer *buf)
839 {
840 if (!handle || !buf)
841 return -EINVAL;
842
843 lib_ring_buffer_put_next_subbuf(buf, handle);
844 return 0;
845 }
846
847 /* snapshot */
848
849 /* Get a snapshot of the current ring buffer producer and consumer positions */
850 int ustctl_snapshot(struct lttng_ust_shm_handle *handle,
851 struct lttng_ust_lib_ring_buffer *buf)
852 {
853 if (!handle || !buf)
854 return -EINVAL;
855
856 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
857 &buf->prod_snapshot, handle);
858 }
859
860 /* Get the consumer position (iteration start) */
861 int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle *handle,
862 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
863 {
864 if (!handle || !buf || !pos)
865 return -EINVAL;
866
867 *pos = buf->cons_snapshot;
868 return 0;
869 }
870
871 /* Get the producer position (iteration end) */
872 int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle *handle,
873 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
874 {
875 if (!handle || !buf || !pos)
876 return -EINVAL;
877
878 *pos = buf->prod_snapshot;
879 return 0;
880 }
881
882 /* Get exclusive read access to the specified sub-buffer position */
883 int ustctl_get_subbuf(struct lttng_ust_shm_handle *handle,
884 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
885 {
886 if (!handle || !buf || !pos)
887 return -EINVAL;
888
889 return lib_ring_buffer_get_subbuf(buf, *pos, handle);
890 }
891
892 /* Release exclusive sub-buffer access */
893 int ustctl_put_subbuf(struct lttng_ust_shm_handle *handle,
894 struct lttng_ust_lib_ring_buffer *buf)
895 {
896 if (!handle || !buf)
897 return -EINVAL;
898
899 lib_ring_buffer_put_subbuf(buf, handle);
900 return 0;
901 }
902
903 void ustctl_flush_buffer(struct lttng_ust_shm_handle *handle,
904 struct lttng_ust_lib_ring_buffer *buf,
905 int producer_active)
906 {
907 assert(handle && buf);
908 lib_ring_buffer_switch_slow(buf,
909 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
910 handle);
911 }
This page took 0.045921 seconds and 3 git commands to generate.