Fix: don't flush-final for offset 0 if reader is on sub-buffer
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
74d81a6c 3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
57773204 4 *
e92f3e28
MD
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.
57773204
MD
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 *
e92f3e28
MD
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.
57773204
MD
17 */
18
9d335227 19#define _GNU_SOURCE
57773204 20#include <string.h>
4318ae1b
MD
21#include <lttng/ust-ctl.h>
22#include <lttng/ust-abi.h>
c1fca457 23#include <lttng/ust-events.h>
7a784989 24#include <sys/mman.h>
44c72f10
MD
25
26#include <usterr-signal-safe.h>
b728d87e 27#include <ust-comm.h>
74d81a6c 28#include <helper.h>
57773204
MD
29
30#include "../libringbuffer/backend.h"
31#include "../libringbuffer/frontend.h"
32
74d81a6c
MD
33/*
34 * Channel representation within consumer.
35 */
36struct ustctl_consumer_channel {
37 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 38
74d81a6c
MD
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;
57773204 63
2be0e72c
MD
64int ustctl_release_handle(int sock, int handle)
65{
66 struct ustcomm_ust_msg lum;
67 struct ustcomm_ust_reply lur;
2be0e72c 68
74d81a6c
MD
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);
2be0e72c 75}
74d81a6c 76
12388166
MD
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 */
d26228ae 81int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 82{
57773204
MD
83 int ret;
84
9bfc503d
MD
85 if (!data)
86 return -EINVAL;
87
74d81a6c
MD
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 }
d26228ae 99 }
74d81a6c
MD
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 }
d26228ae 106 }
74d81a6c
MD
107 break;
108 default:
109 assert(0);
d26228ae 110 }
2be0e72c 111 return ustctl_release_handle(sock, data->handle);
57773204
MD
112}
113
1c5e467e
MD
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;
1c5e467e 130 return 0;
1c5e467e
MD
131}
132
57773204
MD
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
57773204 154int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
155 struct lttng_ust_object_data *channel_data,
156 struct lttng_ust_object_data **_event_data)
57773204
MD
157{
158 struct ustcomm_ust_msg lum;
159 struct ustcomm_ust_reply lur;
61f02aea 160 struct lttng_ust_object_data *event_data;
57773204
MD
161 int ret;
162
9bfc503d
MD
163 if (!channel_data || !_event_data)
164 return -EINVAL;
165
74d81a6c 166 event_data = zmalloc(sizeof(*event_data));
57773204
MD
167 if (!event_data)
168 return -ENOMEM;
57773204
MD
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;
457a6b58
MD
175 lum.u.event.loglevel_type = ev->loglevel_type;
176 lum.u.event.loglevel = ev->loglevel;
57773204
MD
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,
61f02aea
MD
189 struct lttng_ust_object_data *obj_data,
190 struct lttng_ust_object_data **_context_data)
57773204
MD
191{
192 struct ustcomm_ust_msg lum;
193 struct ustcomm_ust_reply lur;
61f02aea 194 struct lttng_ust_object_data *context_data;
57773204
MD
195 int ret;
196
9bfc503d
MD
197 if (!obj_data || !_context_data)
198 return -EINVAL;
199
74d81a6c 200 context_data = zmalloc(sizeof(*context_data));
57773204
MD
201 if (!context_data)
202 return -ENOMEM;
57773204 203 memset(&lum, 0, sizeof(lum));
3039d8ed 204 lum.handle = obj_data->handle;
57773204
MD
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
cd54f6d9
MD
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;
e695af51 233 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
234
235 ret = ustcomm_send_app_msg(sock, &lum);
236 if (ret)
237 return ret;
cd54f6d9
MD
238 /* send var len bytecode */
239 ret = ustcomm_send_unix_sock(sock, bytecode->data,
240 bytecode->len);
241 if (ret < 0) {
7bc53e94
MD
242 if (ret == -ECONNRESET)
243 fprintf(stderr, "remote end closed connection\n");
cd54f6d9
MD
244 return ret;
245 }
7bc53e94
MD
246 if (ret != bytecode->len)
247 return -EINVAL;
248 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
249}
250
57773204 251/* Enable event, channel and session ioctl */
61f02aea 252int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
253{
254 struct ustcomm_ust_msg lum;
255 struct ustcomm_ust_reply lur;
256 int ret;
257
9bfc503d
MD
258 if (!object)
259 return -EINVAL;
260
57773204
MD
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 */
61f02aea 272int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
273{
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
276 int ret;
277
9bfc503d
MD
278 if (!object)
279 return -EINVAL;
280
57773204
MD
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
4a6ca058 291int ustctl_start_session(int sock, int handle)
57773204 292{
61f02aea 293 struct lttng_ust_object_data obj;
4a6ca058
MD
294
295 obj.handle = handle;
296 return ustctl_enable(sock, &obj);
57773204
MD
297}
298
4a6ca058 299int ustctl_stop_session(int sock, int handle)
57773204 300{
61f02aea 301 struct lttng_ust_object_data obj;
4a6ca058
MD
302
303 obj.handle = handle;
304 return ustctl_disable(sock, &obj);
57773204
MD
305}
306
57773204
MD
307int ustctl_tracepoint_list(int sock)
308{
b115631f
MD
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,
cbef6901 325 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
326{
327 struct ustcomm_ust_msg lum;
328 struct ustcomm_ust_reply lur;
329 int ret;
330
9bfc503d
MD
331 if (!iter)
332 return -EINVAL;
333
b115631f
MD
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;
882a56d7 340 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 341 lur.u.tracepoint.name,
882a56d7 342 lur.u.tracepoint.loglevel);
cbef6901 343 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 344 return 0;
57773204
MD
345}
346
40003310
MD
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
57773204
MD
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
9bfc503d
MD
399 if (!v)
400 return -EINVAL;
401
57773204
MD
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{
9bfc503d
MD
431 if (!calibrate)
432 return -EINVAL;
433
57773204
MD
434 return -ENOSYS;
435}
436
f1fffc57
MD
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
9bfc503d
MD
443 if (!object)
444 return -EINVAL;
445
f1fffc57
MD
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
74d81a6c
MD
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 }
57773204 493
74d81a6c
MD
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)
57773204 503{
74d81a6c
MD
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;
7a784989 561 int ret;
57773204 562
74d81a6c
MD
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 }
9bfc503d 580
74d81a6c
MD
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;
57773204 631 }
74d81a6c
MD
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;
0bfe09ec 671 }
0bfe09ec 672 }
74d81a6c
MD
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;
0bfe09ec 677
74d81a6c
MD
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 }
57773204 715 }
74d81a6c
MD
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 }
c1fca457 767 break;
74d81a6c
MD
768 case LTTNG_UST_CHAN_METADATA:
769 if (attr->output == LTTNG_UST_MMAP)
770 transport_name = "relay-metadata-mmap";
771 else
772 return NULL;
c1fca457
MD
773 break;
774 default:
74d81a6c 775 transport_name = "<unknown>";
c1fca457
MD
776 return NULL;
777 }
74d81a6c
MD
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;
7a784989 784 }
74d81a6c
MD
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;
57773204
MD
805}
806
74d81a6c 807void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 808{
74d81a6c
MD
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;
57773204 817
74d81a6c
MD
818 table = channel->chan->handle->table;
819 if (table->size <= 0)
9bfc503d 820 return -EINVAL;
74d81a6c
MD
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}
9bfc503d 827
74d81a6c
MD
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);
57773204
MD
839}
840
74d81a6c 841int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
842{
843 struct channel *chan;
844
74d81a6c
MD
845 chan = stream->chan->chan->chan;
846 return ring_buffer_close_wait_fd(&chan->backend.config,
847 chan, stream->handle, stream->cpu);
5224b5c8
MD
848}
849
74d81a6c 850int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 851{
66bdd22a 852 struct channel *chan;
74d81a6c
MD
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;
4cfec15c 868 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
869 int ret;
870
74d81a6c
MD
871 if (!channel)
872 return NULL;
873 handle = channel->chan->handle;
9bfc503d
MD
874 if (!handle)
875 return NULL;
876
74d81a6c 877 chan = channel->chan->chan;
6e922b24 878 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
879 chan, cpu, handle, &shm_fd, &wait_fd,
880 &wakeup_fd, &memory_map_size);
6e922b24
MD
881 if (!buf)
882 return NULL;
74d81a6c 883 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
884 if (ret)
885 return NULL;
74d81a6c
MD
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);
6e922b24
MD
914}
915
74d81a6c 916int ustctl_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 917{
74d81a6c
MD
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);
6e922b24
MD
938}
939
57773204
MD
940/* For mmap mode, readable without "get" operation */
941
74d81a6c 942void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 943{
74d81a6c
MD
944 struct lttng_ust_lib_ring_buffer *buf;
945 struct ustctl_consumer_channel *consumer_chan;
946
947 if (!stream)
9bfc503d 948 return NULL;
74d81a6c
MD
949 buf = stream->buf;
950 consumer_chan = stream->chan;
951 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
952}
953
57773204 954/* returns the length to mmap. */
74d81a6c 955int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
956 unsigned long *len)
957{
74d81a6c 958 struct ustctl_consumer_channel *consumer_chan;
57773204 959 unsigned long mmap_buf_len;
66bdd22a 960 struct channel *chan;
57773204 961
74d81a6c 962 if (!stream)
9bfc503d 963 return -EINVAL;
74d81a6c
MD
964 consumer_chan = stream->chan;
965 chan = consumer_chan->chan->chan;
57773204
MD
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. */
74d81a6c 978int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
979 unsigned long *len)
980{
74d81a6c 981 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 982 struct channel *chan;
57773204 983
74d81a6c 984 if (!stream)
9bfc503d 985 return -EINVAL;
74d81a6c
MD
986 consumer_chan = stream->chan;
987 chan = consumer_chan->chan->chan;
57773204
MD
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. */
74d81a6c
MD
998int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
999 unsigned long *off)
57773204 1000{
66bdd22a 1001 struct channel *chan;
57773204 1002 unsigned long sb_bindex;
74d81a6c
MD
1003 struct lttng_ust_lib_ring_buffer *buf;
1004 struct ustctl_consumer_channel *consumer_chan;
57773204 1005
74d81a6c 1006 if (!stream)
9bfc503d 1007 return -EINVAL;
74d81a6c
MD
1008 buf = stream->buf;
1009 consumer_chan = stream->chan;
1010 chan = consumer_chan->chan->chan;
57773204
MD
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);
74d81a6c
MD
1015 *off = shmp(consumer_chan->chan->handle,
1016 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
57773204
MD
1017 return 0;
1018}
1019
1020/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1021int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1022 unsigned long *len)
57773204 1023{
74d81a6c 1024 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1025 struct channel *chan;
74d81a6c 1026 struct lttng_ust_lib_ring_buffer *buf;
57773204 1027
74d81a6c 1028 if (!stream)
9bfc503d
MD
1029 return -EINVAL;
1030
74d81a6c
MD
1031 buf = stream->buf;
1032 consumer_chan = stream->chan;
1033 chan = consumer_chan->chan->chan;
57773204 1034 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1035 consumer_chan->chan->handle);
57773204
MD
1036 return 0;
1037}
1038
1039/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1040int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1041 unsigned long *len)
57773204 1042{
74d81a6c 1043 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1044 struct channel *chan;
74d81a6c 1045 struct lttng_ust_lib_ring_buffer *buf;
57773204 1046
74d81a6c 1047 if (!stream)
9bfc503d 1048 return -EINVAL;
74d81a6c
MD
1049 buf = stream->buf;
1050 consumer_chan = stream->chan;
1051 chan = consumer_chan->chan->chan;
57773204 1052 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1053 consumer_chan->chan->handle);
57773204
MD
1054 *len = PAGE_ALIGN(*len);
1055 return 0;
1056}
1057
1058/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1059int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1060{
74d81a6c
MD
1061 struct lttng_ust_lib_ring_buffer *buf;
1062 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1063
74d81a6c
MD
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);
57773204
MD
1070}
1071
1072
1073/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1074int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1075{
74d81a6c
MD
1076 struct lttng_ust_lib_ring_buffer *buf;
1077 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1078
74d81a6c
MD
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);
57773204
MD
1084 return 0;
1085}
1086
1087/* snapshot */
1088
1089/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1090int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1091{
74d81a6c
MD
1092 struct lttng_ust_lib_ring_buffer *buf;
1093 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1094
74d81a6c
MD
1095 if (!stream)
1096 return -EINVAL;
1097 buf = stream->buf;
1098 consumer_chan = stream->chan;
57773204 1099 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1100 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1101}
1102
1103/* Get the consumer position (iteration start) */
74d81a6c
MD
1104int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1105 unsigned long *pos)
57773204 1106{
74d81a6c 1107 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1108
74d81a6c
MD
1109 if (!stream)
1110 return -EINVAL;
1111 buf = stream->buf;
57773204
MD
1112 *pos = buf->cons_snapshot;
1113 return 0;
1114}
1115
1116/* Get the producer position (iteration end) */
74d81a6c
MD
1117int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1118 unsigned long *pos)
57773204 1119{
74d81a6c 1120 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1121
74d81a6c
MD
1122 if (!stream)
1123 return -EINVAL;
1124 buf = stream->buf;
57773204
MD
1125 *pos = buf->prod_snapshot;
1126 return 0;
1127}
1128
1129/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1130int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1131 unsigned long *pos)
57773204 1132{
74d81a6c
MD
1133 struct lttng_ust_lib_ring_buffer *buf;
1134 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1135
74d81a6c
MD
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);
57773204
MD
1142}
1143
1144/* Release exclusive sub-buffer access */
74d81a6c 1145int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1146{
74d81a6c
MD
1147 struct lttng_ust_lib_ring_buffer *buf;
1148 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1149
74d81a6c
MD
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);
57773204
MD
1155 return 0;
1156}
1157
74d81a6c 1158void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1159 int producer_active)
57773204 1160{
74d81a6c
MD
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;
b52190f2
MD
1167 lib_ring_buffer_switch_slow(buf,
1168 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
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();
57773204 1187}
This page took 0.079034 seconds and 4 git commands to generate.