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