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