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