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