Fix: don't flush-final for offset 0 if reader is on sub-buffer
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - 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#include <helper.h>
29
30#include "../libringbuffer/backend.h"
31#include "../libringbuffer/frontend.h"
32
33/*
34 * Channel representation within consumer.
35 */
36struct ustctl_consumer_channel {
37 struct lttng_channel *chan; /* lttng channel buffers */
38
39 /* initial attributes */
40 struct ustctl_consumer_channel_attr attr;
41};
42
43/*
44 * Stream representation within consumer.
45 */
46struct ustctl_consumer_stream {
47 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
48 struct lttng_ust_lib_ring_buffer *buf;
49 struct ustctl_consumer_channel *chan;
50 int shm_fd, wait_fd, wakeup_fd;
51 int cpu;
52 uint64_t memory_map_size;
53};
54
55extern void lttng_ring_buffer_client_overwrite_init(void);
56extern void lttng_ring_buffer_client_discard_init(void);
57extern void lttng_ring_buffer_metadata_client_init(void);
58extern void lttng_ring_buffer_client_overwrite_exit(void);
59extern void lttng_ring_buffer_client_discard_exit(void);
60extern void lttng_ring_buffer_metadata_client_exit(void);
61
62volatile enum ust_loglevel ust_loglevel;
63
64int ustctl_release_handle(int sock, int handle)
65{
66 struct ustcomm_ust_msg lum;
67 struct ustcomm_ust_reply lur;
68
69 if (sock < 0 || handle < 0)
70 return 0;
71 memset(&lum, 0, sizeof(lum));
72 lum.handle = handle;
73 lum.cmd = LTTNG_UST_RELEASE;
74 return ustcomm_send_app_cmd(sock, &lum, &lur);
75}
76
77/*
78 * If sock is negative, it means we don't have to notify the other side
79 * (e.g. application has already vanished).
80 */
81int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
82{
83 int ret;
84
85 if (!data)
86 return -EINVAL;
87
88 switch (data->type) {
89 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
90 free(data->u.channel.data);
91 break;
92 case LTTNG_UST_OBJECT_TYPE_STREAM:
93 if (data->u.stream.shm_fd >= 0) {
94 ret = close(data->u.stream.shm_fd);
95 if (ret < 0) {
96 ret = -errno;
97 return ret;
98 }
99 }
100 if (data->u.stream.wakeup_fd >= 0) {
101 ret = close(data->u.stream.wakeup_fd);
102 if (ret < 0) {
103 ret = -errno;
104 return ret;
105 }
106 }
107 break;
108 default:
109 assert(0);
110 }
111 return ustctl_release_handle(sock, data->handle);
112}
113
114/*
115 * Send registration done packet to the application.
116 */
117int ustctl_register_done(int sock)
118{
119 struct ustcomm_ust_msg lum;
120 struct ustcomm_ust_reply lur;
121 int ret;
122
123 DBG("Sending register done command to %d", sock);
124 memset(&lum, 0, sizeof(lum));
125 lum.handle = LTTNG_UST_ROOT_HANDLE;
126 lum.cmd = LTTNG_UST_REGISTER_DONE;
127 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
128 if (ret)
129 return ret;
130 return 0;
131}
132
133/*
134 * returns session handle.
135 */
136int ustctl_create_session(int sock)
137{
138 struct ustcomm_ust_msg lum;
139 struct ustcomm_ust_reply lur;
140 int ret, session_handle;
141
142 /* Create session */
143 memset(&lum, 0, sizeof(lum));
144 lum.handle = LTTNG_UST_ROOT_HANDLE;
145 lum.cmd = LTTNG_UST_SESSION;
146 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
147 if (ret)
148 return ret;
149 session_handle = lur.ret_val;
150 DBG("received session handle %u", session_handle);
151 return session_handle;
152}
153
154int ustctl_create_event(int sock, struct lttng_ust_event *ev,
155 struct lttng_ust_object_data *channel_data,
156 struct lttng_ust_object_data **_event_data)
157{
158 struct ustcomm_ust_msg lum;
159 struct ustcomm_ust_reply lur;
160 struct lttng_ust_object_data *event_data;
161 int ret;
162
163 if (!channel_data || !_event_data)
164 return -EINVAL;
165
166 event_data = zmalloc(sizeof(*event_data));
167 if (!event_data)
168 return -ENOMEM;
169 memset(&lum, 0, sizeof(lum));
170 lum.handle = channel_data->handle;
171 lum.cmd = LTTNG_UST_EVENT;
172 strncpy(lum.u.event.name, ev->name,
173 LTTNG_UST_SYM_NAME_LEN);
174 lum.u.event.instrumentation = ev->instrumentation;
175 lum.u.event.loglevel_type = ev->loglevel_type;
176 lum.u.event.loglevel = ev->loglevel;
177 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
178 if (ret) {
179 free(event_data);
180 return ret;
181 }
182 event_data->handle = lur.ret_val;
183 DBG("received event handle %u", event_data->handle);
184 *_event_data = event_data;
185 return 0;
186}
187
188int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
189 struct lttng_ust_object_data *obj_data,
190 struct lttng_ust_object_data **_context_data)
191{
192 struct ustcomm_ust_msg lum;
193 struct ustcomm_ust_reply lur;
194 struct lttng_ust_object_data *context_data;
195 int ret;
196
197 if (!obj_data || !_context_data)
198 return -EINVAL;
199
200 context_data = zmalloc(sizeof(*context_data));
201 if (!context_data)
202 return -ENOMEM;
203 memset(&lum, 0, sizeof(lum));
204 lum.handle = obj_data->handle;
205 lum.cmd = LTTNG_UST_CONTEXT;
206 lum.u.context.ctx = ctx->ctx;
207 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
208 if (ret) {
209 free(context_data);
210 return ret;
211 }
212 context_data->handle = lur.ret_val;
213 DBG("received context handle %u", context_data->handle);
214 *_context_data = context_data;
215 return ret;
216}
217
218int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
219 struct lttng_ust_object_data *obj_data)
220{
221 struct ustcomm_ust_msg lum;
222 struct ustcomm_ust_reply lur;
223 int ret;
224
225 if (!obj_data)
226 return -EINVAL;
227
228 memset(&lum, 0, sizeof(lum));
229 lum.handle = obj_data->handle;
230 lum.cmd = LTTNG_UST_FILTER;
231 lum.u.filter.data_size = bytecode->len;
232 lum.u.filter.reloc_offset = bytecode->reloc_offset;
233 lum.u.filter.seqnum = bytecode->seqnum;
234
235 ret = ustcomm_send_app_msg(sock, &lum);
236 if (ret)
237 return ret;
238 /* send var len bytecode */
239 ret = ustcomm_send_unix_sock(sock, bytecode->data,
240 bytecode->len);
241 if (ret < 0) {
242 if (ret == -ECONNRESET)
243 fprintf(stderr, "remote end closed connection\n");
244 return ret;
245 }
246 if (ret != bytecode->len)
247 return -EINVAL;
248 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
249}
250
251/* Enable event, channel and session ioctl */
252int ustctl_enable(int sock, struct lttng_ust_object_data *object)
253{
254 struct ustcomm_ust_msg lum;
255 struct ustcomm_ust_reply lur;
256 int ret;
257
258 if (!object)
259 return -EINVAL;
260
261 memset(&lum, 0, sizeof(lum));
262 lum.handle = object->handle;
263 lum.cmd = LTTNG_UST_ENABLE;
264 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
265 if (ret)
266 return ret;
267 DBG("enabled handle %u", object->handle);
268 return 0;
269}
270
271/* Disable event, channel and session ioctl */
272int ustctl_disable(int sock, struct lttng_ust_object_data *object)
273{
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
276 int ret;
277
278 if (!object)
279 return -EINVAL;
280
281 memset(&lum, 0, sizeof(lum));
282 lum.handle = object->handle;
283 lum.cmd = LTTNG_UST_DISABLE;
284 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
285 if (ret)
286 return ret;
287 DBG("disable handle %u", object->handle);
288 return 0;
289}
290
291int ustctl_start_session(int sock, int handle)
292{
293 struct lttng_ust_object_data obj;
294
295 obj.handle = handle;
296 return ustctl_enable(sock, &obj);
297}
298
299int ustctl_stop_session(int sock, int handle)
300{
301 struct lttng_ust_object_data obj;
302
303 obj.handle = handle;
304 return ustctl_disable(sock, &obj);
305}
306
307int ustctl_tracepoint_list(int sock)
308{
309 struct ustcomm_ust_msg lum;
310 struct ustcomm_ust_reply lur;
311 int ret, tp_list_handle;
312
313 memset(&lum, 0, sizeof(lum));
314 lum.handle = LTTNG_UST_ROOT_HANDLE;
315 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
316 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
317 if (ret)
318 return ret;
319 tp_list_handle = lur.ret_val;
320 DBG("received tracepoint list handle %u", tp_list_handle);
321 return tp_list_handle;
322}
323
324int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
325 struct lttng_ust_tracepoint_iter *iter)
326{
327 struct ustcomm_ust_msg lum;
328 struct ustcomm_ust_reply lur;
329 int ret;
330
331 if (!iter)
332 return -EINVAL;
333
334 memset(&lum, 0, sizeof(lum));
335 lum.handle = tp_list_handle;
336 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
337 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
338 if (ret)
339 return ret;
340 DBG("received tracepoint list entry name %s loglevel %d",
341 lur.u.tracepoint.name,
342 lur.u.tracepoint.loglevel);
343 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
344 return 0;
345}
346
347int ustctl_tracepoint_field_list(int sock)
348{
349 struct ustcomm_ust_msg lum;
350 struct ustcomm_ust_reply lur;
351 int ret, tp_field_list_handle;
352
353 memset(&lum, 0, sizeof(lum));
354 lum.handle = LTTNG_UST_ROOT_HANDLE;
355 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
356 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
357 if (ret)
358 return ret;
359 tp_field_list_handle = lur.ret_val;
360 DBG("received tracepoint field list handle %u", tp_field_list_handle);
361 return tp_field_list_handle;
362}
363
364int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
365 struct lttng_ust_field_iter *iter)
366{
367 struct ustcomm_ust_msg lum;
368 struct ustcomm_ust_reply lur;
369 int ret;
370 ssize_t len;
371
372 if (!iter)
373 return -EINVAL;
374
375 memset(&lum, 0, sizeof(lum));
376 lum.handle = tp_field_list_handle;
377 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
378 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
379 if (ret)
380 return ret;
381 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
382 if (len != sizeof(*iter)) {
383 return -EINVAL;
384 }
385 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
386 iter->event_name,
387 iter->loglevel,
388 iter->field_name,
389 iter->type);
390 return 0;
391}
392
393int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
394{
395 struct ustcomm_ust_msg lum;
396 struct ustcomm_ust_reply lur;
397 int ret;
398
399 if (!v)
400 return -EINVAL;
401
402 memset(&lum, 0, sizeof(lum));
403 lum.handle = LTTNG_UST_ROOT_HANDLE;
404 lum.cmd = LTTNG_UST_TRACER_VERSION;
405 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
406 if (ret)
407 return ret;
408 memcpy(v, &lur.u.version, sizeof(*v));
409 DBG("received tracer version");
410 return 0;
411}
412
413int ustctl_wait_quiescent(int sock)
414{
415 struct ustcomm_ust_msg lum;
416 struct ustcomm_ust_reply lur;
417 int ret;
418
419 memset(&lum, 0, sizeof(lum));
420 lum.handle = LTTNG_UST_ROOT_HANDLE;
421 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
422 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
423 if (ret)
424 return ret;
425 DBG("waited for quiescent state");
426 return 0;
427}
428
429int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
430{
431 if (!calibrate)
432 return -EINVAL;
433
434 return -ENOSYS;
435}
436
437int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
438{
439 struct ustcomm_ust_msg lum;
440 struct ustcomm_ust_reply lur;
441 int ret;
442
443 if (!object)
444 return -EINVAL;
445
446 memset(&lum, 0, sizeof(lum));
447 lum.handle = object->handle;
448 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
449 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
450 if (ret)
451 return ret;
452 DBG("flushed buffer handle %u", object->handle);
453 return 0;
454}
455
456static
457int ustctl_send_channel(int sock,
458 enum lttng_ust_chan_type type,
459 void *data,
460 uint64_t size,
461 int send_fd_only)
462{
463 ssize_t len;
464
465 if (!send_fd_only) {
466 /* Send mmap size */
467 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
468 if (len != sizeof(size)) {
469 if (len < 0)
470 return len;
471 else
472 return -EIO;
473 }
474
475 /* Send channel type */
476 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
477 if (len != sizeof(type)) {
478 if (len < 0)
479 return len;
480 else
481 return -EIO;
482 }
483 }
484
485 /* Send channel data */
486 len = ustcomm_send_unix_sock(sock, data, size);
487 if (len != size) {
488 if (len < 0)
489 return len;
490 else
491 return -EIO;
492 }
493
494 return 0;
495}
496
497static
498int ustctl_send_stream(int sock,
499 uint32_t stream_nr,
500 uint64_t memory_map_size,
501 int shm_fd, int wakeup_fd,
502 int send_fd_only)
503{
504 ssize_t len;
505 int fds[2];
506
507 if (!send_fd_only) {
508 if (shm_fd < 0) {
509 /* finish iteration */
510 uint64_t v = -1;
511
512 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
513 if (len != sizeof(v)) {
514 if (len < 0)
515 return len;
516 else
517 return -EIO;
518 }
519 return 0;
520 }
521
522 /* Send mmap size */
523 len = ustcomm_send_unix_sock(sock, &memory_map_size,
524 sizeof(memory_map_size));
525 if (len != sizeof(memory_map_size)) {
526 if (len < 0)
527 return len;
528 else
529 return -EIO;
530 }
531
532 /* Send stream nr */
533 len = ustcomm_send_unix_sock(sock, &stream_nr,
534 sizeof(stream_nr));
535 if (len != sizeof(stream_nr)) {
536 if (len < 0)
537 return len;
538 else
539 return -EIO;
540 }
541 }
542
543 /* Send shm fd and wakeup fd */
544 fds[0] = shm_fd;
545 fds[1] = wakeup_fd;
546 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
547 if (len <= 0) {
548 if (len < 0)
549 return len;
550 else
551 return -EIO;
552 }
553 return 0;
554}
555
556int ustctl_recv_channel_from_consumer(int sock,
557 struct lttng_ust_object_data **_channel_data)
558{
559 struct lttng_ust_object_data *channel_data;
560 ssize_t len;
561 int ret;
562
563 channel_data = zmalloc(sizeof(*channel_data));
564 if (!channel_data) {
565 ret = -ENOMEM;
566 goto error_alloc;
567 }
568 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
569
570 /* recv mmap size */
571 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
572 sizeof(channel_data->size));
573 if (len != sizeof(channel_data->size)) {
574 if (len < 0)
575 ret = len;
576 else
577 ret = -EINVAL;
578 goto error;
579 }
580
581 /* recv channel type */
582 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
583 sizeof(channel_data->u.channel.type));
584 if (len != sizeof(channel_data->u.channel.type)) {
585 if (len < 0)
586 ret = len;
587 else
588 ret = -EINVAL;
589 goto error;
590 }
591
592 /* recv channel data */
593 channel_data->u.channel.data = zmalloc(channel_data->size);
594 if (!channel_data->u.channel.data) {
595 ret = -ENOMEM;
596 goto error;
597 }
598 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
599 channel_data->size);
600 if (len != channel_data->size) {
601 if (len < 0)
602 ret = len;
603 else
604 ret = -EINVAL;
605 goto error_recv_data;
606 }
607
608 *_channel_data = channel_data;
609 return 0;
610
611error_recv_data:
612 free(channel_data->u.channel.data);
613error:
614 free(channel_data);
615error_alloc:
616 return ret;
617}
618
619int ustctl_recv_stream_from_consumer(int sock,
620 struct lttng_ust_object_data **_stream_data)
621{
622 struct lttng_ust_object_data *stream_data;
623 ssize_t len;
624 int ret;
625 int fds[2];
626
627 stream_data = zmalloc(sizeof(*stream_data));
628 if (!stream_data) {
629 ret = -ENOMEM;
630 goto error_alloc;
631 }
632
633 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
634 stream_data->handle = -1;
635
636 /* recv mmap size */
637 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
638 sizeof(stream_data->size));
639 if (len != sizeof(stream_data->size)) {
640 if (len < 0)
641 ret = len;
642 else
643 ret = -EINVAL;
644 goto error;
645 }
646 if (stream_data->size == -1) {
647 ret = -LTTNG_UST_ERR_NOENT;
648 goto error;
649 }
650
651 /* recv stream nr */
652 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
653 sizeof(stream_data->u.stream.stream_nr));
654 if (len != sizeof(stream_data->u.stream.stream_nr)) {
655 if (len < 0)
656 ret = len;
657 else
658 ret = -EINVAL;
659 goto error;
660 }
661
662 /* recv shm fd and wakeup fd */
663 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
664 if (len <= 0) {
665 if (len < 0) {
666 ret = len;
667 goto error;
668 } else {
669 ret = -EIO;
670 goto error;
671 }
672 }
673 stream_data->u.stream.shm_fd = fds[0];
674 stream_data->u.stream.wakeup_fd = fds[1];
675 *_stream_data = stream_data;
676 return 0;
677
678error:
679 free(stream_data);
680error_alloc:
681 return ret;
682}
683
684int ustctl_send_channel_to_ust(int sock, int session_handle,
685 struct lttng_ust_object_data *channel_data)
686{
687 struct ustcomm_ust_msg lum;
688 struct ustcomm_ust_reply lur;
689 int ret;
690
691 if (!channel_data)
692 return -EINVAL;
693
694 memset(&lum, 0, sizeof(lum));
695 lum.handle = session_handle;
696 lum.cmd = LTTNG_UST_CHANNEL;
697 lum.u.channel.len = channel_data->size;
698 lum.u.channel.type = channel_data->u.channel.type;
699 ret = ustcomm_send_app_msg(sock, &lum);
700 if (ret)
701 return ret;
702
703 ret = ustctl_send_channel(sock,
704 channel_data->u.channel.type,
705 channel_data->u.channel.data,
706 channel_data->size,
707 1);
708 if (ret)
709 return ret;
710 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
711 if (!ret) {
712 if (lur.ret_val >= 0) {
713 channel_data->handle = lur.ret_val;
714 }
715 }
716 return ret;
717}
718
719int ustctl_send_stream_to_ust(int sock,
720 struct lttng_ust_object_data *channel_data,
721 struct lttng_ust_object_data *stream_data)
722{
723 struct ustcomm_ust_msg lum;
724 struct ustcomm_ust_reply lur;
725 int ret;
726
727 memset(&lum, 0, sizeof(lum));
728 lum.handle = channel_data->handle;
729 lum.cmd = LTTNG_UST_STREAM;
730 lum.u.stream.len = stream_data->size;
731 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
732 ret = ustcomm_send_app_msg(sock, &lum);
733 if (ret)
734 return ret;
735
736 assert(stream_data);
737 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
738
739 ret = ustctl_send_stream(sock,
740 stream_data->u.stream.stream_nr,
741 stream_data->size,
742 stream_data->u.stream.shm_fd,
743 stream_data->u.stream.wakeup_fd, 1);
744 if (ret)
745 return ret;
746 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
747}
748
749
750/* Buffer operations */
751
752struct ustctl_consumer_channel *
753 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr)
754{
755 struct ustctl_consumer_channel *chan;
756 const char *transport_name;
757 struct lttng_transport *transport;
758
759 switch (attr->type) {
760 case LTTNG_UST_CHAN_PER_CPU:
761 if (attr->output == LTTNG_UST_MMAP) {
762 transport_name = attr->overwrite ?
763 "relay-overwrite-mmap" : "relay-discard-mmap";
764 } else {
765 return NULL;
766 }
767 break;
768 case LTTNG_UST_CHAN_METADATA:
769 if (attr->output == LTTNG_UST_MMAP)
770 transport_name = "relay-metadata-mmap";
771 else
772 return NULL;
773 break;
774 default:
775 transport_name = "<unknown>";
776 return NULL;
777 }
778
779 transport = lttng_transport_find(transport_name);
780 if (!transport) {
781 DBG("LTTng transport %s not found\n",
782 transport_name);
783 return NULL;
784 }
785
786 chan = zmalloc(sizeof(*chan));
787 if (!chan)
788 return NULL;
789
790 chan->chan = transport->ops.channel_create(transport_name, NULL,
791 attr->subbuf_size, attr->num_subbuf,
792 attr->switch_timer_interval,
793 attr->read_timer_interval,
794 attr->uuid);
795 if (!chan->chan) {
796 goto chan_error;
797 }
798 chan->chan->ops = &transport->ops;
799 memcpy(&chan->attr, attr, sizeof(chan->attr));
800 return chan;
801
802chan_error:
803 free(chan);
804 return NULL;
805}
806
807void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
808{
809 chan->chan->ops->channel_destroy(chan->chan);
810 free(chan);
811}
812
813int ustctl_send_channel_to_sessiond(int sock,
814 struct ustctl_consumer_channel *channel)
815{
816 struct shm_object_table *table;
817
818 table = channel->chan->handle->table;
819 if (table->size <= 0)
820 return -EINVAL;
821 return ustctl_send_channel(sock,
822 channel->attr.type,
823 table->objects[0].memory_map,
824 table->objects[0].memory_map_size,
825 0);
826}
827
828int ustctl_send_stream_to_sessiond(int sock,
829 struct ustctl_consumer_stream *stream)
830{
831 if (!stream)
832 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
833
834 return ustctl_send_stream(sock,
835 stream->cpu,
836 stream->memory_map_size,
837 stream->shm_fd, stream->wakeup_fd,
838 0);
839}
840
841int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
842{
843 struct channel *chan;
844
845 chan = stream->chan->chan->chan;
846 return ring_buffer_close_wait_fd(&chan->backend.config,
847 chan, stream->handle, stream->cpu);
848}
849
850int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
851{
852 struct channel *chan;
853
854 chan = stream->chan->chan->chan;
855 return ring_buffer_close_wakeup_fd(&chan->backend.config,
856 chan, stream->handle, stream->cpu);
857}
858
859struct ustctl_consumer_stream *
860 ustctl_create_stream(struct ustctl_consumer_channel *channel,
861 int cpu)
862{
863 struct ustctl_consumer_stream *stream;
864 struct lttng_ust_shm_handle *handle;
865 struct channel *chan;
866 int shm_fd, wait_fd, wakeup_fd;
867 uint64_t memory_map_size;
868 struct lttng_ust_lib_ring_buffer *buf;
869 int ret;
870
871 if (!channel)
872 return NULL;
873 handle = channel->chan->handle;
874 if (!handle)
875 return NULL;
876
877 chan = channel->chan->chan;
878 buf = channel_get_ring_buffer(&chan->backend.config,
879 chan, cpu, handle, &shm_fd, &wait_fd,
880 &wakeup_fd, &memory_map_size);
881 if (!buf)
882 return NULL;
883 ret = lib_ring_buffer_open_read(buf, handle);
884 if (ret)
885 return NULL;
886
887 stream = zmalloc(sizeof(*stream));
888 if (!stream)
889 goto alloc_error;
890 stream->handle = handle;
891 stream->buf = buf;
892 stream->chan = channel;
893 stream->shm_fd = shm_fd;
894 stream->wait_fd = wait_fd;
895 stream->wakeup_fd = wakeup_fd;
896 stream->memory_map_size = memory_map_size;
897 stream->cpu = cpu;
898 return stream;
899
900alloc_error:
901 return NULL;
902}
903
904void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
905{
906 struct lttng_ust_lib_ring_buffer *buf;
907 struct ustctl_consumer_channel *consumer_chan;
908
909 assert(stream);
910 buf = stream->buf;
911 consumer_chan = stream->chan;
912 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
913 free(stream);
914}
915
916int ustctl_get_wait_fd(struct ustctl_consumer_stream *stream)
917{
918 struct lttng_ust_lib_ring_buffer *buf;
919 struct ustctl_consumer_channel *consumer_chan;
920
921 if (!stream)
922 return -EINVAL;
923 buf = stream->buf;
924 consumer_chan = stream->chan;
925 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
926}
927
928int ustctl_get_wakeup_fd(struct ustctl_consumer_stream *stream)
929{
930 struct lttng_ust_lib_ring_buffer *buf;
931 struct ustctl_consumer_channel *consumer_chan;
932
933 if (!stream)
934 return -EINVAL;
935 buf = stream->buf;
936 consumer_chan = stream->chan;
937 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
938}
939
940/* For mmap mode, readable without "get" operation */
941
942void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
943{
944 struct lttng_ust_lib_ring_buffer *buf;
945 struct ustctl_consumer_channel *consumer_chan;
946
947 if (!stream)
948 return NULL;
949 buf = stream->buf;
950 consumer_chan = stream->chan;
951 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
952}
953
954/* returns the length to mmap. */
955int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
956 unsigned long *len)
957{
958 struct ustctl_consumer_channel *consumer_chan;
959 unsigned long mmap_buf_len;
960 struct channel *chan;
961
962 if (!stream)
963 return -EINVAL;
964 consumer_chan = stream->chan;
965 chan = consumer_chan->chan->chan;
966 if (chan->backend.config.output != RING_BUFFER_MMAP)
967 return -EINVAL;
968 mmap_buf_len = chan->backend.buf_size;
969 if (chan->backend.extra_reader_sb)
970 mmap_buf_len += chan->backend.subbuf_size;
971 if (mmap_buf_len > INT_MAX)
972 return -EFBIG;
973 *len = mmap_buf_len;
974 return 0;
975}
976
977/* returns the maximum size for sub-buffers. */
978int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
979 unsigned long *len)
980{
981 struct ustctl_consumer_channel *consumer_chan;
982 struct channel *chan;
983
984 if (!stream)
985 return -EINVAL;
986 consumer_chan = stream->chan;
987 chan = consumer_chan->chan->chan;
988 *len = chan->backend.subbuf_size;
989 return 0;
990}
991
992/*
993 * For mmap mode, operate on the current packet (between get/put or
994 * get_next/put_next).
995 */
996
997/* returns the offset of the subbuffer belonging to the mmap reader. */
998int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
999 unsigned long *off)
1000{
1001 struct channel *chan;
1002 unsigned long sb_bindex;
1003 struct lttng_ust_lib_ring_buffer *buf;
1004 struct ustctl_consumer_channel *consumer_chan;
1005
1006 if (!stream)
1007 return -EINVAL;
1008 buf = stream->buf;
1009 consumer_chan = stream->chan;
1010 chan = consumer_chan->chan->chan;
1011 if (chan->backend.config.output != RING_BUFFER_MMAP)
1012 return -EINVAL;
1013 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
1014 buf->backend.buf_rsb.id);
1015 *off = shmp(consumer_chan->chan->handle,
1016 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
1017 return 0;
1018}
1019
1020/* returns the size of the current sub-buffer, without padding (for mmap). */
1021int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1022 unsigned long *len)
1023{
1024 struct ustctl_consumer_channel *consumer_chan;
1025 struct channel *chan;
1026 struct lttng_ust_lib_ring_buffer *buf;
1027
1028 if (!stream)
1029 return -EINVAL;
1030
1031 buf = stream->buf;
1032 consumer_chan = stream->chan;
1033 chan = consumer_chan->chan->chan;
1034 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1035 consumer_chan->chan->handle);
1036 return 0;
1037}
1038
1039/* returns the size of the current sub-buffer, without padding (for mmap). */
1040int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1041 unsigned long *len)
1042{
1043 struct ustctl_consumer_channel *consumer_chan;
1044 struct channel *chan;
1045 struct lttng_ust_lib_ring_buffer *buf;
1046
1047 if (!stream)
1048 return -EINVAL;
1049 buf = stream->buf;
1050 consumer_chan = stream->chan;
1051 chan = consumer_chan->chan->chan;
1052 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1053 consumer_chan->chan->handle);
1054 *len = PAGE_ALIGN(*len);
1055 return 0;
1056}
1057
1058/* Get exclusive read access to the next sub-buffer that can be read. */
1059int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
1060{
1061 struct lttng_ust_lib_ring_buffer *buf;
1062 struct ustctl_consumer_channel *consumer_chan;
1063
1064 if (!stream)
1065 return -EINVAL;
1066 buf = stream->buf;
1067 consumer_chan = stream->chan;
1068 return lib_ring_buffer_get_next_subbuf(buf,
1069 consumer_chan->chan->handle);
1070}
1071
1072
1073/* Release exclusive sub-buffer access, move consumer forward. */
1074int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
1075{
1076 struct lttng_ust_lib_ring_buffer *buf;
1077 struct ustctl_consumer_channel *consumer_chan;
1078
1079 if (!stream)
1080 return -EINVAL;
1081 buf = stream->buf;
1082 consumer_chan = stream->chan;
1083 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
1084 return 0;
1085}
1086
1087/* snapshot */
1088
1089/* Get a snapshot of the current ring buffer producer and consumer positions */
1090int ustctl_snapshot(struct ustctl_consumer_stream *stream)
1091{
1092 struct lttng_ust_lib_ring_buffer *buf;
1093 struct ustctl_consumer_channel *consumer_chan;
1094
1095 if (!stream)
1096 return -EINVAL;
1097 buf = stream->buf;
1098 consumer_chan = stream->chan;
1099 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1100 &buf->prod_snapshot, consumer_chan->chan->handle);
1101}
1102
1103/* Get the consumer position (iteration start) */
1104int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1105 unsigned long *pos)
1106{
1107 struct lttng_ust_lib_ring_buffer *buf;
1108
1109 if (!stream)
1110 return -EINVAL;
1111 buf = stream->buf;
1112 *pos = buf->cons_snapshot;
1113 return 0;
1114}
1115
1116/* Get the producer position (iteration end) */
1117int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1118 unsigned long *pos)
1119{
1120 struct lttng_ust_lib_ring_buffer *buf;
1121
1122 if (!stream)
1123 return -EINVAL;
1124 buf = stream->buf;
1125 *pos = buf->prod_snapshot;
1126 return 0;
1127}
1128
1129/* Get exclusive read access to the specified sub-buffer position */
1130int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1131 unsigned long *pos)
1132{
1133 struct lttng_ust_lib_ring_buffer *buf;
1134 struct ustctl_consumer_channel *consumer_chan;
1135
1136 if (!stream)
1137 return -EINVAL;
1138 buf = stream->buf;
1139 consumer_chan = stream->chan;
1140 return lib_ring_buffer_get_subbuf(buf, *pos,
1141 consumer_chan->chan->handle);
1142}
1143
1144/* Release exclusive sub-buffer access */
1145int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
1146{
1147 struct lttng_ust_lib_ring_buffer *buf;
1148 struct ustctl_consumer_channel *consumer_chan;
1149
1150 if (!stream)
1151 return -EINVAL;
1152 buf = stream->buf;
1153 consumer_chan = stream->chan;
1154 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
1155 return 0;
1156}
1157
1158void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
1159 int producer_active)
1160{
1161 struct lttng_ust_lib_ring_buffer *buf;
1162 struct ustctl_consumer_channel *consumer_chan;
1163
1164 assert(stream);
1165 buf = stream->buf;
1166 consumer_chan = stream->chan;
1167 lib_ring_buffer_switch_slow(buf,
1168 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
1169 consumer_chan->chan->handle);
1170}
1171
1172static __attribute__((constructor))
1173void ustctl_init(void)
1174{
1175 init_usterr();
1176 lttng_ring_buffer_metadata_client_init();
1177 lttng_ring_buffer_client_overwrite_init();
1178 lttng_ring_buffer_client_discard_init();
1179}
1180
1181static __attribute__((destructor))
1182void ustctl_exit(void)
1183{
1184 lttng_ring_buffer_client_discard_exit();
1185 lttng_ring_buffer_client_overwrite_exit();
1186 lttng_ring_buffer_metadata_client_exit();
1187}
This page took 0.027082 seconds and 4 git commands to generate.