LTTng ringbuffer ABI calls for index generation
[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>
32ce8569 25#include <byteswap.h>
44c72f10
MD
26
27#include <usterr-signal-safe.h>
b728d87e 28#include <ust-comm.h>
74d81a6c 29#include <helper.h>
57773204
MD
30
31#include "../libringbuffer/backend.h"
32#include "../libringbuffer/frontend.h"
c9023c93 33#include "../liblttng-ust/wait.h"
b2f3252a 34#include "../liblttng-ust/lttng-rb-clients.h"
c9023c93
MD
35
36/*
37 * Number of milliseconds to retry before failing metadata writes on
38 * buffer full condition. (10 seconds)
39 */
40#define LTTNG_METADATA_TIMEOUT_MSEC 10000
57773204 41
74d81a6c
MD
42/*
43 * Channel representation within consumer.
44 */
45struct ustctl_consumer_channel {
46 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 47
74d81a6c
MD
48 /* initial attributes */
49 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
50 int wait_fd; /* monitor close() */
51 int wakeup_fd; /* monitor close() */
74d81a6c
MD
52};
53
54/*
55 * Stream representation within consumer.
56 */
57struct ustctl_consumer_stream {
58 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
59 struct lttng_ust_lib_ring_buffer *buf;
60 struct ustctl_consumer_channel *chan;
61 int shm_fd, wait_fd, wakeup_fd;
62 int cpu;
63 uint64_t memory_map_size;
64};
65
66extern void lttng_ring_buffer_client_overwrite_init(void);
08a3170c 67extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74d81a6c 68extern void lttng_ring_buffer_client_discard_init(void);
08a3170c 69extern void lttng_ring_buffer_client_discard_rt_init(void);
74d81a6c
MD
70extern void lttng_ring_buffer_metadata_client_init(void);
71extern void lttng_ring_buffer_client_overwrite_exit(void);
08a3170c 72extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74d81a6c 73extern void lttng_ring_buffer_client_discard_exit(void);
08a3170c 74extern void lttng_ring_buffer_client_discard_rt_exit(void);
74d81a6c
MD
75extern void lttng_ring_buffer_metadata_client_exit(void);
76
77volatile enum ust_loglevel ust_loglevel;
57773204 78
2be0e72c
MD
79int ustctl_release_handle(int sock, int handle)
80{
81 struct ustcomm_ust_msg lum;
82 struct ustcomm_ust_reply lur;
2be0e72c 83
74d81a6c
MD
84 if (sock < 0 || handle < 0)
85 return 0;
86 memset(&lum, 0, sizeof(lum));
87 lum.handle = handle;
88 lum.cmd = LTTNG_UST_RELEASE;
89 return ustcomm_send_app_cmd(sock, &lum, &lur);
2be0e72c 90}
74d81a6c 91
12388166
MD
92/*
93 * If sock is negative, it means we don't have to notify the other side
94 * (e.g. application has already vanished).
95 */
d26228ae 96int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 97{
57773204
MD
98 int ret;
99
9bfc503d
MD
100 if (!data)
101 return -EINVAL;
102
74d81a6c
MD
103 switch (data->type) {
104 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
105 if (data->u.channel.wakeup_fd >= 0) {
106 ret = close(data->u.channel.wakeup_fd);
107 if (ret < 0) {
108 ret = -errno;
109 return ret;
110 }
111 }
74d81a6c
MD
112 free(data->u.channel.data);
113 break;
114 case LTTNG_UST_OBJECT_TYPE_STREAM:
115 if (data->u.stream.shm_fd >= 0) {
116 ret = close(data->u.stream.shm_fd);
117 if (ret < 0) {
118 ret = -errno;
119 return ret;
120 }
d26228ae 121 }
74d81a6c
MD
122 if (data->u.stream.wakeup_fd >= 0) {
123 ret = close(data->u.stream.wakeup_fd);
124 if (ret < 0) {
125 ret = -errno;
126 return ret;
127 }
d26228ae 128 }
74d81a6c 129 break;
32ce8569
MD
130 case LTTNG_UST_OBJECT_TYPE_EVENT:
131 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
132 break;
74d81a6c
MD
133 default:
134 assert(0);
d26228ae 135 }
2be0e72c 136 return ustctl_release_handle(sock, data->handle);
57773204
MD
137}
138
1c5e467e
MD
139/*
140 * Send registration done packet to the application.
141 */
142int ustctl_register_done(int sock)
143{
144 struct ustcomm_ust_msg lum;
145 struct ustcomm_ust_reply lur;
146 int ret;
147
148 DBG("Sending register done command to %d", sock);
149 memset(&lum, 0, sizeof(lum));
150 lum.handle = LTTNG_UST_ROOT_HANDLE;
151 lum.cmd = LTTNG_UST_REGISTER_DONE;
152 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
153 if (ret)
154 return ret;
1c5e467e 155 return 0;
1c5e467e
MD
156}
157
57773204
MD
158/*
159 * returns session handle.
160 */
161int ustctl_create_session(int sock)
162{
163 struct ustcomm_ust_msg lum;
164 struct ustcomm_ust_reply lur;
165 int ret, session_handle;
166
167 /* Create session */
168 memset(&lum, 0, sizeof(lum));
169 lum.handle = LTTNG_UST_ROOT_HANDLE;
170 lum.cmd = LTTNG_UST_SESSION;
171 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
172 if (ret)
173 return ret;
174 session_handle = lur.ret_val;
175 DBG("received session handle %u", session_handle);
176 return session_handle;
177}
178
57773204 179int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
180 struct lttng_ust_object_data *channel_data,
181 struct lttng_ust_object_data **_event_data)
57773204
MD
182{
183 struct ustcomm_ust_msg lum;
184 struct ustcomm_ust_reply lur;
61f02aea 185 struct lttng_ust_object_data *event_data;
57773204
MD
186 int ret;
187
9bfc503d
MD
188 if (!channel_data || !_event_data)
189 return -EINVAL;
190
74d81a6c 191 event_data = zmalloc(sizeof(*event_data));
57773204
MD
192 if (!event_data)
193 return -ENOMEM;
32ce8569 194 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
57773204
MD
195 memset(&lum, 0, sizeof(lum));
196 lum.handle = channel_data->handle;
197 lum.cmd = LTTNG_UST_EVENT;
198 strncpy(lum.u.event.name, ev->name,
199 LTTNG_UST_SYM_NAME_LEN);
200 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
201 lum.u.event.loglevel_type = ev->loglevel_type;
202 lum.u.event.loglevel = ev->loglevel;
57773204
MD
203 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
204 if (ret) {
205 free(event_data);
206 return ret;
207 }
208 event_data->handle = lur.ret_val;
209 DBG("received event handle %u", event_data->handle);
210 *_event_data = event_data;
211 return 0;
212}
213
214int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
61f02aea
MD
215 struct lttng_ust_object_data *obj_data,
216 struct lttng_ust_object_data **_context_data)
57773204
MD
217{
218 struct ustcomm_ust_msg lum;
219 struct ustcomm_ust_reply lur;
61f02aea 220 struct lttng_ust_object_data *context_data;
57773204
MD
221 int ret;
222
9bfc503d
MD
223 if (!obj_data || !_context_data)
224 return -EINVAL;
225
74d81a6c 226 context_data = zmalloc(sizeof(*context_data));
57773204
MD
227 if (!context_data)
228 return -ENOMEM;
32ce8569 229 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
57773204 230 memset(&lum, 0, sizeof(lum));
3039d8ed 231 lum.handle = obj_data->handle;
57773204
MD
232 lum.cmd = LTTNG_UST_CONTEXT;
233 lum.u.context.ctx = ctx->ctx;
234 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
235 if (ret) {
236 free(context_data);
237 return ret;
238 }
32ce8569
MD
239 context_data->handle = -1;
240 DBG("Context created successfully");
57773204
MD
241 *_context_data = context_data;
242 return ret;
243}
244
cd54f6d9
MD
245int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
246 struct lttng_ust_object_data *obj_data)
247{
248 struct ustcomm_ust_msg lum;
249 struct ustcomm_ust_reply lur;
250 int ret;
251
252 if (!obj_data)
253 return -EINVAL;
254
255 memset(&lum, 0, sizeof(lum));
256 lum.handle = obj_data->handle;
257 lum.cmd = LTTNG_UST_FILTER;
258 lum.u.filter.data_size = bytecode->len;
259 lum.u.filter.reloc_offset = bytecode->reloc_offset;
e695af51 260 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
261
262 ret = ustcomm_send_app_msg(sock, &lum);
263 if (ret)
264 return ret;
cd54f6d9
MD
265 /* send var len bytecode */
266 ret = ustcomm_send_unix_sock(sock, bytecode->data,
267 bytecode->len);
268 if (ret < 0) {
269 return ret;
270 }
7bc53e94
MD
271 if (ret != bytecode->len)
272 return -EINVAL;
273 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
274}
275
57773204 276/* Enable event, channel and session ioctl */
61f02aea 277int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
278{
279 struct ustcomm_ust_msg lum;
280 struct ustcomm_ust_reply lur;
281 int ret;
282
9bfc503d
MD
283 if (!object)
284 return -EINVAL;
285
57773204
MD
286 memset(&lum, 0, sizeof(lum));
287 lum.handle = object->handle;
288 lum.cmd = LTTNG_UST_ENABLE;
289 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
290 if (ret)
291 return ret;
292 DBG("enabled handle %u", object->handle);
293 return 0;
294}
295
296/* Disable event, channel and session ioctl */
61f02aea 297int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
298{
299 struct ustcomm_ust_msg lum;
300 struct ustcomm_ust_reply lur;
301 int ret;
302
9bfc503d
MD
303 if (!object)
304 return -EINVAL;
305
57773204
MD
306 memset(&lum, 0, sizeof(lum));
307 lum.handle = object->handle;
308 lum.cmd = LTTNG_UST_DISABLE;
309 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
310 if (ret)
311 return ret;
312 DBG("disable handle %u", object->handle);
313 return 0;
314}
315
4a6ca058 316int ustctl_start_session(int sock, int handle)
57773204 317{
61f02aea 318 struct lttng_ust_object_data obj;
4a6ca058
MD
319
320 obj.handle = handle;
321 return ustctl_enable(sock, &obj);
57773204
MD
322}
323
4a6ca058 324int ustctl_stop_session(int sock, int handle)
57773204 325{
61f02aea 326 struct lttng_ust_object_data obj;
4a6ca058
MD
327
328 obj.handle = handle;
329 return ustctl_disable(sock, &obj);
57773204
MD
330}
331
57773204
MD
332int ustctl_tracepoint_list(int sock)
333{
b115631f
MD
334 struct ustcomm_ust_msg lum;
335 struct ustcomm_ust_reply lur;
336 int ret, tp_list_handle;
337
338 memset(&lum, 0, sizeof(lum));
339 lum.handle = LTTNG_UST_ROOT_HANDLE;
340 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
341 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
342 if (ret)
343 return ret;
344 tp_list_handle = lur.ret_val;
345 DBG("received tracepoint list handle %u", tp_list_handle);
346 return tp_list_handle;
347}
348
349int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
cbef6901 350 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
351{
352 struct ustcomm_ust_msg lum;
353 struct ustcomm_ust_reply lur;
354 int ret;
355
9bfc503d
MD
356 if (!iter)
357 return -EINVAL;
358
b115631f
MD
359 memset(&lum, 0, sizeof(lum));
360 lum.handle = tp_list_handle;
361 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
362 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
363 if (ret)
364 return ret;
882a56d7 365 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 366 lur.u.tracepoint.name,
882a56d7 367 lur.u.tracepoint.loglevel);
cbef6901 368 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 369 return 0;
57773204
MD
370}
371
40003310
MD
372int ustctl_tracepoint_field_list(int sock)
373{
374 struct ustcomm_ust_msg lum;
375 struct ustcomm_ust_reply lur;
376 int ret, tp_field_list_handle;
377
378 memset(&lum, 0, sizeof(lum));
379 lum.handle = LTTNG_UST_ROOT_HANDLE;
380 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
381 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
382 if (ret)
383 return ret;
384 tp_field_list_handle = lur.ret_val;
385 DBG("received tracepoint field list handle %u", tp_field_list_handle);
386 return tp_field_list_handle;
387}
388
389int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
390 struct lttng_ust_field_iter *iter)
391{
392 struct ustcomm_ust_msg lum;
393 struct ustcomm_ust_reply lur;
394 int ret;
395 ssize_t len;
396
397 if (!iter)
398 return -EINVAL;
399
400 memset(&lum, 0, sizeof(lum));
401 lum.handle = tp_field_list_handle;
402 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
403 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
404 if (ret)
405 return ret;
406 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
407 if (len != sizeof(*iter)) {
408 return -EINVAL;
409 }
410 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
411 iter->event_name,
412 iter->loglevel,
413 iter->field_name,
414 iter->type);
415 return 0;
416}
417
57773204
MD
418int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
419{
420 struct ustcomm_ust_msg lum;
421 struct ustcomm_ust_reply lur;
422 int ret;
423
9bfc503d
MD
424 if (!v)
425 return -EINVAL;
426
57773204
MD
427 memset(&lum, 0, sizeof(lum));
428 lum.handle = LTTNG_UST_ROOT_HANDLE;
429 lum.cmd = LTTNG_UST_TRACER_VERSION;
430 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
431 if (ret)
432 return ret;
433 memcpy(v, &lur.u.version, sizeof(*v));
434 DBG("received tracer version");
435 return 0;
436}
437
438int ustctl_wait_quiescent(int sock)
439{
440 struct ustcomm_ust_msg lum;
441 struct ustcomm_ust_reply lur;
442 int ret;
443
444 memset(&lum, 0, sizeof(lum));
445 lum.handle = LTTNG_UST_ROOT_HANDLE;
446 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
447 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
448 if (ret)
449 return ret;
450 DBG("waited for quiescent state");
451 return 0;
452}
453
454int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
455{
9bfc503d
MD
456 if (!calibrate)
457 return -EINVAL;
458
57773204
MD
459 return -ENOSYS;
460}
461
f1fffc57
MD
462int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
463{
464 struct ustcomm_ust_msg lum;
465 struct ustcomm_ust_reply lur;
466 int ret;
467
9bfc503d
MD
468 if (!object)
469 return -EINVAL;
470
f1fffc57
MD
471 memset(&lum, 0, sizeof(lum));
472 lum.handle = object->handle;
473 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
474 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
475 if (ret)
476 return ret;
477 DBG("flushed buffer handle %u", object->handle);
478 return 0;
479}
480
74d81a6c
MD
481static
482int ustctl_send_channel(int sock,
483 enum lttng_ust_chan_type type,
484 void *data,
485 uint64_t size,
ff0f5728 486 int wakeup_fd,
74d81a6c
MD
487 int send_fd_only)
488{
489 ssize_t len;
490
491 if (!send_fd_only) {
492 /* Send mmap size */
493 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
494 if (len != sizeof(size)) {
495 if (len < 0)
496 return len;
497 else
498 return -EIO;
499 }
500
501 /* Send channel type */
502 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
503 if (len != sizeof(type)) {
504 if (len < 0)
505 return len;
506 else
507 return -EIO;
508 }
509 }
510
511 /* Send channel data */
512 len = ustcomm_send_unix_sock(sock, data, size);
513 if (len != size) {
514 if (len < 0)
515 return len;
516 else
517 return -EIO;
518 }
57773204 519
ff0f5728
MD
520 /* Send wakeup fd */
521 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
522 if (len <= 0) {
523 if (len < 0)
524 return len;
525 else
526 return -EIO;
527 }
74d81a6c
MD
528 return 0;
529}
530
531static
532int ustctl_send_stream(int sock,
533 uint32_t stream_nr,
534 uint64_t memory_map_size,
535 int shm_fd, int wakeup_fd,
536 int send_fd_only)
57773204 537{
74d81a6c
MD
538 ssize_t len;
539 int fds[2];
540
541 if (!send_fd_only) {
542 if (shm_fd < 0) {
543 /* finish iteration */
544 uint64_t v = -1;
545
546 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
547 if (len != sizeof(v)) {
548 if (len < 0)
549 return len;
550 else
551 return -EIO;
552 }
553 return 0;
554 }
555
556 /* Send mmap size */
557 len = ustcomm_send_unix_sock(sock, &memory_map_size,
558 sizeof(memory_map_size));
559 if (len != sizeof(memory_map_size)) {
560 if (len < 0)
561 return len;
562 else
563 return -EIO;
564 }
565
566 /* Send stream nr */
567 len = ustcomm_send_unix_sock(sock, &stream_nr,
568 sizeof(stream_nr));
569 if (len != sizeof(stream_nr)) {
570 if (len < 0)
571 return len;
572 else
573 return -EIO;
574 }
575 }
576
577 /* Send shm fd and wakeup fd */
578 fds[0] = shm_fd;
579 fds[1] = wakeup_fd;
580 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
581 if (len <= 0) {
582 if (len < 0)
583 return len;
584 else
585 return -EIO;
586 }
587 return 0;
588}
589
590int ustctl_recv_channel_from_consumer(int sock,
591 struct lttng_ust_object_data **_channel_data)
592{
593 struct lttng_ust_object_data *channel_data;
594 ssize_t len;
ff0f5728 595 int wakeup_fd;
7a784989 596 int ret;
57773204 597
74d81a6c
MD
598 channel_data = zmalloc(sizeof(*channel_data));
599 if (!channel_data) {
600 ret = -ENOMEM;
601 goto error_alloc;
602 }
603 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
12f3dabc 604 channel_data->handle = -1;
74d81a6c
MD
605
606 /* recv mmap size */
607 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
608 sizeof(channel_data->size));
609 if (len != sizeof(channel_data->size)) {
610 if (len < 0)
611 ret = len;
612 else
613 ret = -EINVAL;
614 goto error;
615 }
9bfc503d 616
74d81a6c
MD
617 /* recv channel type */
618 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
619 sizeof(channel_data->u.channel.type));
620 if (len != sizeof(channel_data->u.channel.type)) {
621 if (len < 0)
622 ret = len;
623 else
624 ret = -EINVAL;
625 goto error;
626 }
627
628 /* recv channel data */
629 channel_data->u.channel.data = zmalloc(channel_data->size);
630 if (!channel_data->u.channel.data) {
631 ret = -ENOMEM;
632 goto error;
633 }
634 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
635 channel_data->size);
636 if (len != channel_data->size) {
637 if (len < 0)
638 ret = len;
639 else
640 ret = -EINVAL;
641 goto error_recv_data;
642 }
ff0f5728
MD
643 /* recv wakeup fd */
644 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
645 if (len <= 0) {
646 if (len < 0) {
647 ret = len;
648 goto error_recv_data;
649 } else {
650 ret = -EIO;
651 goto error_recv_data;
652 }
653 }
654 channel_data->u.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
655 *_channel_data = channel_data;
656 return 0;
657
658error_recv_data:
659 free(channel_data->u.channel.data);
660error:
661 free(channel_data);
662error_alloc:
663 return ret;
664}
665
666int ustctl_recv_stream_from_consumer(int sock,
667 struct lttng_ust_object_data **_stream_data)
668{
669 struct lttng_ust_object_data *stream_data;
670 ssize_t len;
671 int ret;
672 int fds[2];
673
674 stream_data = zmalloc(sizeof(*stream_data));
675 if (!stream_data) {
676 ret = -ENOMEM;
677 goto error_alloc;
57773204 678 }
74d81a6c
MD
679
680 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
681 stream_data->handle = -1;
682
683 /* recv mmap size */
684 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
685 sizeof(stream_data->size));
686 if (len != sizeof(stream_data->size)) {
687 if (len < 0)
688 ret = len;
689 else
690 ret = -EINVAL;
691 goto error;
692 }
693 if (stream_data->size == -1) {
694 ret = -LTTNG_UST_ERR_NOENT;
695 goto error;
696 }
697
698 /* recv stream nr */
699 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
700 sizeof(stream_data->u.stream.stream_nr));
701 if (len != sizeof(stream_data->u.stream.stream_nr)) {
702 if (len < 0)
703 ret = len;
704 else
705 ret = -EINVAL;
706 goto error;
707 }
708
709 /* recv shm fd and wakeup fd */
710 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
711 if (len <= 0) {
712 if (len < 0) {
713 ret = len;
714 goto error;
715 } else {
716 ret = -EIO;
717 goto error;
0bfe09ec 718 }
0bfe09ec 719 }
74d81a6c
MD
720 stream_data->u.stream.shm_fd = fds[0];
721 stream_data->u.stream.wakeup_fd = fds[1];
722 *_stream_data = stream_data;
723 return 0;
0bfe09ec 724
74d81a6c
MD
725error:
726 free(stream_data);
727error_alloc:
728 return ret;
729}
730
731int ustctl_send_channel_to_ust(int sock, int session_handle,
732 struct lttng_ust_object_data *channel_data)
733{
734 struct ustcomm_ust_msg lum;
735 struct ustcomm_ust_reply lur;
736 int ret;
737
738 if (!channel_data)
739 return -EINVAL;
740
741 memset(&lum, 0, sizeof(lum));
742 lum.handle = session_handle;
743 lum.cmd = LTTNG_UST_CHANNEL;
744 lum.u.channel.len = channel_data->size;
745 lum.u.channel.type = channel_data->u.channel.type;
746 ret = ustcomm_send_app_msg(sock, &lum);
747 if (ret)
748 return ret;
749
750 ret = ustctl_send_channel(sock,
751 channel_data->u.channel.type,
752 channel_data->u.channel.data,
753 channel_data->size,
ff0f5728 754 channel_data->u.channel.wakeup_fd,
74d81a6c
MD
755 1);
756 if (ret)
757 return ret;
758 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
759 if (!ret) {
7f2348b8 760 channel_data->handle = lur.ret_val;
57773204 761 }
74d81a6c
MD
762 return ret;
763}
764
765int ustctl_send_stream_to_ust(int sock,
766 struct lttng_ust_object_data *channel_data,
767 struct lttng_ust_object_data *stream_data)
768{
769 struct ustcomm_ust_msg lum;
770 struct ustcomm_ust_reply lur;
771 int ret;
772
773 memset(&lum, 0, sizeof(lum));
774 lum.handle = channel_data->handle;
775 lum.cmd = LTTNG_UST_STREAM;
776 lum.u.stream.len = stream_data->size;
777 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
778 ret = ustcomm_send_app_msg(sock, &lum);
779 if (ret)
780 return ret;
781
782 assert(stream_data);
783 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
784
785 ret = ustctl_send_stream(sock,
786 stream_data->u.stream.stream_nr,
787 stream_data->size,
788 stream_data->u.stream.shm_fd,
789 stream_data->u.stream.wakeup_fd, 1);
790 if (ret)
791 return ret;
792 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
793}
794
12f3dabc
MD
795int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest,
796 struct lttng_ust_object_data *src)
797{
798 struct lttng_ust_object_data *obj;
799 int ret;
800
801 if (src->handle != -1) {
802 ret = -EINVAL;
803 goto error;
804 }
805
806 obj = zmalloc(sizeof(*obj));
807 if (!obj) {
808 ret = -ENOMEM;
809 goto error;
810 }
811
812 obj->type = src->type;
813 obj->handle = src->handle;
814 obj->size = src->size;
815
816 switch (obj->type) {
817 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
818 {
819 obj->u.channel.type = src->u.channel.type;
820 if (src->u.channel.wakeup_fd >= 0) {
821 obj->u.channel.wakeup_fd =
822 dup(src->u.channel.wakeup_fd);
823 if (obj->u.channel.wakeup_fd < 0) {
824 ret = errno;
825 goto chan_error_wakeup_fd;
826 }
827 } else {
828 obj->u.channel.wakeup_fd =
829 src->u.channel.wakeup_fd;
830 }
831 obj->u.channel.data = zmalloc(obj->size);
832 if (!obj->u.channel.data) {
833 ret = -ENOMEM;
834 goto chan_error_alloc;
835 }
836 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
837 break;
838
839 chan_error_alloc:
840 if (src->u.channel.wakeup_fd >= 0) {
841 int closeret;
842
843 closeret = close(obj->u.channel.wakeup_fd);
844 if (closeret) {
845 PERROR("close");
846 }
847 }
848 chan_error_wakeup_fd:
849 goto error_type;
850
851 }
852
853 case LTTNG_UST_OBJECT_TYPE_STREAM:
854 {
855 obj->u.stream.stream_nr = src->u.stream.stream_nr;
856 if (src->u.stream.wakeup_fd >= 0) {
857 obj->u.stream.wakeup_fd =
858 dup(src->u.stream.wakeup_fd);
859 if (obj->u.stream.wakeup_fd < 0) {
860 ret = errno;
861 goto stream_error_wakeup_fd;
862 }
863 } else {
864 obj->u.stream.wakeup_fd =
865 src->u.stream.wakeup_fd;
866 }
867
868 if (src->u.stream.shm_fd >= 0) {
869 obj->u.stream.shm_fd =
870 dup(src->u.stream.shm_fd);
871 if (obj->u.stream.shm_fd < 0) {
872 ret = errno;
873 goto stream_error_shm_fd;
874 }
875 } else {
876 obj->u.stream.shm_fd =
877 src->u.stream.shm_fd;
878 }
879 break;
880
881 stream_error_shm_fd:
882 if (src->u.stream.wakeup_fd >= 0) {
883 int closeret;
884
885 closeret = close(obj->u.stream.wakeup_fd);
886 if (closeret) {
887 PERROR("close");
888 }
889 }
890 stream_error_wakeup_fd:
891 goto error_type;
892 }
893
894 default:
895 ret = -EINVAL;
896 goto error_type;
897 }
898
899 *dest = obj;
900 return 0;
901
902error_type:
903 free(obj);
904error:
905 return ret;
906}
907
74d81a6c
MD
908
909/* Buffer operations */
910
911struct ustctl_consumer_channel *
912 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr)
913{
914 struct ustctl_consumer_channel *chan;
915 const char *transport_name;
916 struct lttng_transport *transport;
917
918 switch (attr->type) {
919 case LTTNG_UST_CHAN_PER_CPU:
920 if (attr->output == LTTNG_UST_MMAP) {
34a91bdb
MD
921 if (attr->overwrite) {
922 if (attr->read_timer_interval == 0) {
923 transport_name = "relay-overwrite-mmap";
924 } else {
925 transport_name = "relay-overwrite-rt-mmap";
926 }
927 } else {
928 if (attr->read_timer_interval == 0) {
929 transport_name = "relay-discard-mmap";
930 } else {
931 transport_name = "relay-discard-rt-mmap";
932 }
933 }
74d81a6c
MD
934 } else {
935 return NULL;
936 }
c1fca457 937 break;
74d81a6c
MD
938 case LTTNG_UST_CHAN_METADATA:
939 if (attr->output == LTTNG_UST_MMAP)
940 transport_name = "relay-metadata-mmap";
941 else
942 return NULL;
c1fca457
MD
943 break;
944 default:
74d81a6c 945 transport_name = "<unknown>";
c1fca457
MD
946 return NULL;
947 }
74d81a6c
MD
948
949 transport = lttng_transport_find(transport_name);
950 if (!transport) {
951 DBG("LTTng transport %s not found\n",
32ce8569 952 transport_name);
74d81a6c 953 return NULL;
7a784989 954 }
74d81a6c
MD
955
956 chan = zmalloc(sizeof(*chan));
957 if (!chan)
958 return NULL;
959
960 chan->chan = transport->ops.channel_create(transport_name, NULL,
32ce8569 961 attr->subbuf_size, attr->num_subbuf,
74d81a6c 962 attr->switch_timer_interval,
32ce8569 963 attr->read_timer_interval,
6ca18e66 964 attr->uuid, attr->chan_id);
74d81a6c
MD
965 if (!chan->chan) {
966 goto chan_error;
967 }
968 chan->chan->ops = &transport->ops;
969 memcpy(&chan->attr, attr, sizeof(chan->attr));
cb7378b3
MD
970 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
971 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
74d81a6c
MD
972 return chan;
973
974chan_error:
975 free(chan);
976 return NULL;
57773204
MD
977}
978
74d81a6c 979void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 980{
74d81a6c
MD
981 chan->chan->ops->channel_destroy(chan->chan);
982 free(chan);
983}
984
985int ustctl_send_channel_to_sessiond(int sock,
986 struct ustctl_consumer_channel *channel)
987{
988 struct shm_object_table *table;
57773204 989
74d81a6c
MD
990 table = channel->chan->handle->table;
991 if (table->size <= 0)
9bfc503d 992 return -EINVAL;
74d81a6c
MD
993 return ustctl_send_channel(sock,
994 channel->attr.type,
995 table->objects[0].memory_map,
996 table->objects[0].memory_map_size,
ff0f5728 997 channel->wakeup_fd,
74d81a6c
MD
998 0);
999}
9bfc503d 1000
74d81a6c
MD
1001int ustctl_send_stream_to_sessiond(int sock,
1002 struct ustctl_consumer_stream *stream)
1003{
1004 if (!stream)
1005 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1006
1007 return ustctl_send_stream(sock,
1008 stream->cpu,
1009 stream->memory_map_size,
1010 stream->shm_fd, stream->wakeup_fd,
1011 0);
57773204
MD
1012}
1013
c9023c93
MD
1014int ustctl_write_metadata_to_channel(
1015 struct ustctl_consumer_channel *channel,
1016 const char *metadata_str, /* NOT null-terminated */
1017 size_t len) /* metadata length */
1018{
1019 struct lttng_ust_lib_ring_buffer_ctx ctx;
1020 struct lttng_channel *chan = channel->chan;
1021 const char *str = metadata_str;
1022 int ret = 0, waitret;
1023 size_t reserve_len, pos;
1024
1025 for (pos = 0; pos < len; pos += reserve_len) {
1026 reserve_len = min_t(size_t,
1027 chan->ops->packet_avail_size(chan->chan, chan->handle),
1028 len - pos);
1029 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1030 sizeof(char), -1, chan->handle);
1031 /*
1032 * We don't care about metadata buffer's records lost
1033 * count, because we always retry here. Report error if
1034 * we need to bail out after timeout or being
1035 * interrupted.
1036 */
1037 waitret = wait_cond_interruptible_timeout(
1038 ({
1039 ret = chan->ops->event_reserve(&ctx, 0);
1040 ret != -ENOBUFS || !ret;
1041 }),
1042 LTTNG_METADATA_TIMEOUT_MSEC);
1043 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1044 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1045 waitret == -EINTR ? "interrupted" :
1046 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1047 if (waitret == -EINTR)
1048 ret = waitret;
1049 goto end;
1050 }
1051 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1052 chan->ops->event_commit(&ctx);
1053 }
1054end:
1055 return ret;
1056}
1057
3ef94b0e
JD
1058/*
1059 * Write at most one packet in the channel.
1060 * Returns the number of bytes written on success, < 0 on error.
1061 */
1062ssize_t ustctl_write_one_packet_to_channel(
1063 struct ustctl_consumer_channel *channel,
1064 const char *metadata_str, /* NOT null-terminated */
1065 size_t len) /* metadata length */
1066{
1067 struct lttng_ust_lib_ring_buffer_ctx ctx;
1068 struct lttng_channel *chan = channel->chan;
1069 const char *str = metadata_str;
1070 ssize_t reserve_len;
1071 int ret;
1072
1073 reserve_len = min_t(ssize_t,
1074 chan->ops->packet_avail_size(chan->chan, chan->handle),
1075 len);
1076 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1077 sizeof(char), -1, chan->handle);
1078 ret = chan->ops->event_reserve(&ctx, 0);
1079 if (ret != 0) {
1080 DBG("LTTng: event reservation failed");
1081 assert(ret < 0);
1082 reserve_len = ret;
1083 goto end;
1084 }
1085 chan->ops->event_write(&ctx, str, reserve_len);
1086 chan->ops->event_commit(&ctx);
1087
1088end:
1089 return reserve_len;
1090}
1091
ff0f5728
MD
1092int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1093{
1094 struct channel *chan;
cb7378b3 1095 int ret;
ff0f5728
MD
1096
1097 chan = consumer_chan->chan->chan;
cb7378b3 1098 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
ff0f5728 1099 chan, chan->handle);
cb7378b3
MD
1100 if (!ret)
1101 consumer_chan->wait_fd = -1;
1102 return ret;
ff0f5728
MD
1103}
1104
1105int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1106{
1107 struct channel *chan;
cb7378b3 1108 int ret;
ff0f5728
MD
1109
1110 chan = consumer_chan->chan->chan;
cb7378b3 1111 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
ff0f5728 1112 chan, chan->handle);
cb7378b3
MD
1113 if (!ret)
1114 consumer_chan->wakeup_fd = -1;
1115 return ret;
ff0f5728
MD
1116}
1117
74d81a6c 1118int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
1119{
1120 struct channel *chan;
1121
74d81a6c 1122 chan = stream->chan->chan->chan;
ff0f5728 1123 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
74d81a6c 1124 chan, stream->handle, stream->cpu);
5224b5c8
MD
1125}
1126
74d81a6c 1127int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 1128{
66bdd22a 1129 struct channel *chan;
74d81a6c
MD
1130
1131 chan = stream->chan->chan->chan;
ff0f5728 1132 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
74d81a6c
MD
1133 chan, stream->handle, stream->cpu);
1134}
1135
1136struct ustctl_consumer_stream *
1137 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1138 int cpu)
1139{
1140 struct ustctl_consumer_stream *stream;
1141 struct lttng_ust_shm_handle *handle;
1142 struct channel *chan;
1143 int shm_fd, wait_fd, wakeup_fd;
1144 uint64_t memory_map_size;
4cfec15c 1145 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
1146 int ret;
1147
74d81a6c
MD
1148 if (!channel)
1149 return NULL;
1150 handle = channel->chan->handle;
9bfc503d
MD
1151 if (!handle)
1152 return NULL;
1153
74d81a6c 1154 chan = channel->chan->chan;
6e922b24 1155 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
1156 chan, cpu, handle, &shm_fd, &wait_fd,
1157 &wakeup_fd, &memory_map_size);
6e922b24
MD
1158 if (!buf)
1159 return NULL;
74d81a6c 1160 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
1161 if (ret)
1162 return NULL;
74d81a6c
MD
1163
1164 stream = zmalloc(sizeof(*stream));
1165 if (!stream)
1166 goto alloc_error;
1167 stream->handle = handle;
1168 stream->buf = buf;
1169 stream->chan = channel;
1170 stream->shm_fd = shm_fd;
1171 stream->wait_fd = wait_fd;
1172 stream->wakeup_fd = wakeup_fd;
1173 stream->memory_map_size = memory_map_size;
1174 stream->cpu = cpu;
1175 return stream;
1176
1177alloc_error:
1178 return NULL;
1179}
1180
1181void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1182{
1183 struct lttng_ust_lib_ring_buffer *buf;
1184 struct ustctl_consumer_channel *consumer_chan;
1185
1186 assert(stream);
1187 buf = stream->buf;
1188 consumer_chan = stream->chan;
1189 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1190 free(stream);
6e922b24
MD
1191}
1192
ff0f5728
MD
1193int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1194{
1195 if (!chan)
1196 return -EINVAL;
1197 return shm_get_wait_fd(chan->chan->handle,
1198 &chan->chan->handle->chan._ref);
1199}
1200
1201int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1202{
1203 if (!chan)
1204 return -EINVAL;
1205 return shm_get_wakeup_fd(chan->chan->handle,
1206 &chan->chan->handle->chan._ref);
1207}
1208
1209int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1210{
74d81a6c
MD
1211 struct lttng_ust_lib_ring_buffer *buf;
1212 struct ustctl_consumer_channel *consumer_chan;
1213
1214 if (!stream)
1215 return -EINVAL;
1216 buf = stream->buf;
1217 consumer_chan = stream->chan;
1218 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1219}
1220
ff0f5728 1221int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1222{
1223 struct lttng_ust_lib_ring_buffer *buf;
1224 struct ustctl_consumer_channel *consumer_chan;
1225
1226 if (!stream)
1227 return -EINVAL;
1228 buf = stream->buf;
1229 consumer_chan = stream->chan;
1230 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
6e922b24
MD
1231}
1232
57773204
MD
1233/* For mmap mode, readable without "get" operation */
1234
74d81a6c 1235void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1236{
74d81a6c
MD
1237 struct lttng_ust_lib_ring_buffer *buf;
1238 struct ustctl_consumer_channel *consumer_chan;
1239
1240 if (!stream)
9bfc503d 1241 return NULL;
74d81a6c
MD
1242 buf = stream->buf;
1243 consumer_chan = stream->chan;
1244 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
1245}
1246
57773204 1247/* returns the length to mmap. */
74d81a6c 1248int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1249 unsigned long *len)
1250{
74d81a6c 1251 struct ustctl_consumer_channel *consumer_chan;
57773204 1252 unsigned long mmap_buf_len;
66bdd22a 1253 struct channel *chan;
57773204 1254
74d81a6c 1255 if (!stream)
9bfc503d 1256 return -EINVAL;
74d81a6c
MD
1257 consumer_chan = stream->chan;
1258 chan = consumer_chan->chan->chan;
57773204
MD
1259 if (chan->backend.config.output != RING_BUFFER_MMAP)
1260 return -EINVAL;
1261 mmap_buf_len = chan->backend.buf_size;
1262 if (chan->backend.extra_reader_sb)
1263 mmap_buf_len += chan->backend.subbuf_size;
1264 if (mmap_buf_len > INT_MAX)
1265 return -EFBIG;
1266 *len = mmap_buf_len;
1267 return 0;
1268}
1269
1270/* returns the maximum size for sub-buffers. */
74d81a6c 1271int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1272 unsigned long *len)
1273{
74d81a6c 1274 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1275 struct channel *chan;
57773204 1276
74d81a6c 1277 if (!stream)
9bfc503d 1278 return -EINVAL;
74d81a6c
MD
1279 consumer_chan = stream->chan;
1280 chan = consumer_chan->chan->chan;
57773204
MD
1281 *len = chan->backend.subbuf_size;
1282 return 0;
1283}
1284
1285/*
1286 * For mmap mode, operate on the current packet (between get/put or
1287 * get_next/put_next).
1288 */
1289
1290/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1291int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1292 unsigned long *off)
57773204 1293{
66bdd22a 1294 struct channel *chan;
57773204 1295 unsigned long sb_bindex;
74d81a6c
MD
1296 struct lttng_ust_lib_ring_buffer *buf;
1297 struct ustctl_consumer_channel *consumer_chan;
57773204 1298
74d81a6c 1299 if (!stream)
9bfc503d 1300 return -EINVAL;
74d81a6c
MD
1301 buf = stream->buf;
1302 consumer_chan = stream->chan;
1303 chan = consumer_chan->chan->chan;
57773204
MD
1304 if (chan->backend.config.output != RING_BUFFER_MMAP)
1305 return -EINVAL;
1306 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
32ce8569 1307 buf->backend.buf_rsb.id);
74d81a6c
MD
1308 *off = shmp(consumer_chan->chan->handle,
1309 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
57773204
MD
1310 return 0;
1311}
1312
1313/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1314int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1315 unsigned long *len)
57773204 1316{
74d81a6c 1317 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1318 struct channel *chan;
74d81a6c 1319 struct lttng_ust_lib_ring_buffer *buf;
57773204 1320
74d81a6c 1321 if (!stream)
9bfc503d
MD
1322 return -EINVAL;
1323
74d81a6c
MD
1324 buf = stream->buf;
1325 consumer_chan = stream->chan;
1326 chan = consumer_chan->chan->chan;
57773204 1327 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1328 consumer_chan->chan->handle);
57773204
MD
1329 return 0;
1330}
1331
1332/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1333int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1334 unsigned long *len)
57773204 1335{
74d81a6c 1336 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1337 struct channel *chan;
74d81a6c 1338 struct lttng_ust_lib_ring_buffer *buf;
57773204 1339
74d81a6c 1340 if (!stream)
9bfc503d 1341 return -EINVAL;
74d81a6c
MD
1342 buf = stream->buf;
1343 consumer_chan = stream->chan;
1344 chan = consumer_chan->chan->chan;
57773204 1345 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1346 consumer_chan->chan->handle);
57773204
MD
1347 *len = PAGE_ALIGN(*len);
1348 return 0;
1349}
1350
1351/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1352int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1353{
74d81a6c
MD
1354 struct lttng_ust_lib_ring_buffer *buf;
1355 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1356
74d81a6c
MD
1357 if (!stream)
1358 return -EINVAL;
1359 buf = stream->buf;
1360 consumer_chan = stream->chan;
1361 return lib_ring_buffer_get_next_subbuf(buf,
1362 consumer_chan->chan->handle);
57773204
MD
1363}
1364
1365
1366/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1367int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1368{
74d81a6c
MD
1369 struct lttng_ust_lib_ring_buffer *buf;
1370 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1371
74d81a6c
MD
1372 if (!stream)
1373 return -EINVAL;
1374 buf = stream->buf;
1375 consumer_chan = stream->chan;
1376 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1377 return 0;
1378}
1379
1380/* snapshot */
1381
1382/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1383int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1384{
74d81a6c
MD
1385 struct lttng_ust_lib_ring_buffer *buf;
1386 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1387
74d81a6c
MD
1388 if (!stream)
1389 return -EINVAL;
1390 buf = stream->buf;
1391 consumer_chan = stream->chan;
57773204 1392 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1393 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1394}
1395
1396/* Get the consumer position (iteration start) */
74d81a6c
MD
1397int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1398 unsigned long *pos)
57773204 1399{
74d81a6c 1400 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1401
74d81a6c
MD
1402 if (!stream)
1403 return -EINVAL;
1404 buf = stream->buf;
57773204
MD
1405 *pos = buf->cons_snapshot;
1406 return 0;
1407}
1408
1409/* Get the producer position (iteration end) */
74d81a6c
MD
1410int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1411 unsigned long *pos)
57773204 1412{
74d81a6c 1413 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1414
74d81a6c
MD
1415 if (!stream)
1416 return -EINVAL;
1417 buf = stream->buf;
57773204
MD
1418 *pos = buf->prod_snapshot;
1419 return 0;
1420}
1421
1422/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1423int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1424 unsigned long *pos)
57773204 1425{
74d81a6c
MD
1426 struct lttng_ust_lib_ring_buffer *buf;
1427 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1428
74d81a6c
MD
1429 if (!stream)
1430 return -EINVAL;
1431 buf = stream->buf;
1432 consumer_chan = stream->chan;
1433 return lib_ring_buffer_get_subbuf(buf, *pos,
1434 consumer_chan->chan->handle);
57773204
MD
1435}
1436
1437/* Release exclusive sub-buffer access */
74d81a6c 1438int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1439{
74d81a6c
MD
1440 struct lttng_ust_lib_ring_buffer *buf;
1441 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1442
74d81a6c
MD
1443 if (!stream)
1444 return -EINVAL;
1445 buf = stream->buf;
1446 consumer_chan = stream->chan;
1447 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1448 return 0;
1449}
1450
74d81a6c 1451void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1452 int producer_active)
57773204 1453{
74d81a6c
MD
1454 struct lttng_ust_lib_ring_buffer *buf;
1455 struct ustctl_consumer_channel *consumer_chan;
1456
1457 assert(stream);
1458 buf = stream->buf;
1459 consumer_chan = stream->chan;
b52190f2
MD
1460 lib_ring_buffer_switch_slow(buf,
1461 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
1462 consumer_chan->chan->handle);
1463}
1464
b2f3252a
JD
1465static
1466struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1467 struct lttng_ust_lib_ring_buffer *buf,
1468 struct lttng_ust_shm_handle *handle)
1469{
1470 struct channel *chan;
1471 const struct lttng_ust_lib_ring_buffer_config *config;
1472 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1473
1474 chan = shmp(handle, buf->backend.chan);
1475 config = &chan->backend.config;
1476 if (!config->cb_ptr)
1477 return NULL;
1478 client_cb = caa_container_of(config->cb_ptr,
1479 struct lttng_ust_client_lib_ring_buffer_client_cb,
1480 parent);
1481 return client_cb;
1482}
1483
1484int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1485 uint64_t *timestamp_begin)
1486{
1487 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1488 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1489 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1490
1491 if (!stream || !timestamp_begin)
1492 return -EINVAL;
1493 client_cb = get_client_cb(buf, handle);
1494 if (!client_cb)
1495 return -ENOSYS;
1496 return client_cb->timestamp_begin(buf, handle, timestamp_begin);
1497}
1498
1499int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1500 uint64_t *timestamp_end)
1501{
1502 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1503 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1504 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1505
1506 if (!stream || !timestamp_end)
1507 return -EINVAL;
1508 client_cb = get_client_cb(buf, handle);
1509 if (!client_cb)
1510 return -ENOSYS;
1511 return client_cb->timestamp_end(buf, handle, timestamp_end);
1512}
1513
1514int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1515 uint64_t *events_discarded)
1516{
1517 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1518 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1519 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1520
1521 if (!stream || !events_discarded)
1522 return -EINVAL;
1523 client_cb = get_client_cb(buf, handle);
1524 if (!client_cb)
1525 return -ENOSYS;
1526 return client_cb->events_discarded(buf, handle, events_discarded);
1527}
1528
1529int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1530 uint64_t *content_size)
1531{
1532 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1533 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1534 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1535
1536 if (!stream || !content_size)
1537 return -EINVAL;
1538 client_cb = get_client_cb(buf, handle);
1539 if (!client_cb)
1540 return -ENOSYS;
1541 return client_cb->content_size(buf, handle, content_size);
1542}
1543
1544int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1545 uint64_t *packet_size)
1546{
1547 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1548 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1549 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1550
1551 if (!stream || !packet_size)
1552 return -EINVAL;
1553 client_cb = get_client_cb(buf, handle);
1554 if (!client_cb)
1555 return -ENOSYS;
1556 return client_cb->packet_size(buf, handle, packet_size);
1557}
1558
1559int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1560 uint64_t *stream_id)
1561{
1562 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1563 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1564 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1565
1566 if (!stream || !stream_id)
1567 return -EINVAL;
1568 client_cb = get_client_cb(buf, handle);
1569 if (!client_cb)
1570 return -ENOSYS;
1571 return client_cb->stream_id(buf, handle, stream_id);
1572}
1573
32ce8569
MD
1574/*
1575 * Returns 0 on success, negative error value on error.
1576 */
1577int ustctl_recv_reg_msg(int sock,
1578 enum ustctl_socket_type *type,
1579 uint32_t *major,
1580 uint32_t *minor,
1581 uint32_t *pid,
1582 uint32_t *ppid,
1583 uint32_t *uid,
1584 uint32_t *gid,
1585 uint32_t *bits_per_long,
1586 uint32_t *uint8_t_alignment,
1587 uint32_t *uint16_t_alignment,
1588 uint32_t *uint32_t_alignment,
1589 uint32_t *uint64_t_alignment,
1590 uint32_t *long_alignment,
1591 int *byte_order,
1592 char *name)
1593{
1594 ssize_t len;
1595 struct ustctl_reg_msg reg_msg;
1596
1597 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1598 if (len > 0 && len != sizeof(reg_msg))
1599 return -EIO;
1600 if (len == 0)
1601 return -EPIPE;
1602 if (len < 0)
1603 return len;
1604
1605 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1606 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1607 BIG_ENDIAN : LITTLE_ENDIAN;
1608 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1609 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1610 LITTLE_ENDIAN : BIG_ENDIAN;
1611 } else {
1612 return -LTTNG_UST_ERR_INVAL_MAGIC;
1613 }
1614 switch (reg_msg.socket_type) {
1615 case 0: *type = USTCTL_SOCKET_CMD;
1616 break;
1617 case 1: *type = USTCTL_SOCKET_NOTIFY;
1618 break;
1619 default:
1620 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1621 }
1622 *major = reg_msg.major;
1623 *minor = reg_msg.minor;
1624 *pid = reg_msg.pid;
1625 *ppid = reg_msg.ppid;
1626 *uid = reg_msg.uid;
1627 *gid = reg_msg.gid;
1628 *bits_per_long = reg_msg.bits_per_long;
1629 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1630 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1631 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1632 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1633 *long_alignment = reg_msg.long_alignment;
1634 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1635 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1636 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1637 }
1638
1639 return 0;
1640}
1641
1642int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1643{
1644 struct ustcomm_notify_hdr header;
1645 ssize_t len;
1646
1647 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1648 if (len > 0 && len != sizeof(header))
1649 return -EIO;
1650 if (len == 0)
1651 return -EPIPE;
1652 if (len < 0)
1653 return len;
1654 switch (header.notify_cmd) {
1655 case 0:
1656 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1657 break;
1658 case 1:
1659 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1660 break;
1661 default:
1662 return -EINVAL;
1663 }
1664 return 0;
1665}
1666
1667/*
1668 * Returns 0 on success, negative error value on error.
1669 */
1670int ustctl_recv_register_event(int sock,
1671 int *session_objd,
1672 int *channel_objd,
1673 char *event_name,
1674 int *loglevel,
1675 char **signature,
1676 size_t *nr_fields,
1677 struct ustctl_field **fields,
1678 char **model_emf_uri)
1679{
1680 ssize_t len;
1681 struct ustcomm_notify_event_msg msg;
1682 size_t signature_len, fields_len, model_emf_uri_len;
1683 char *a_sign = NULL, *a_model_emf_uri = NULL;
1684 struct ustctl_field *a_fields = NULL;
1685
1686 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1687 if (len > 0 && len != sizeof(msg))
1688 return -EIO;
1689 if (len == 0)
1690 return -EPIPE;
1691 if (len < 0)
1692 return len;
1693
1694 *session_objd = msg.session_objd;
1695 *channel_objd = msg.channel_objd;
1696 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1697 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1698 *loglevel = msg.loglevel;
1699 signature_len = msg.signature_len;
1700 fields_len = msg.fields_len;
1701
1702 if (fields_len % sizeof(*a_fields) != 0) {
1703 return -EINVAL;
1704 }
1705
1706 model_emf_uri_len = msg.model_emf_uri_len;
1707
1708 /* recv signature. contains at least \0. */
1709 a_sign = zmalloc(signature_len);
1710 if (!a_sign)
1711 return -ENOMEM;
1712 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1713 if (len > 0 && len != signature_len) {
1714 len = -EIO;
1715 goto signature_error;
1716 }
1717 if (len == 0) {
1718 len = -EPIPE;
1719 goto signature_error;
1720 }
1721 if (len < 0) {
1722 goto signature_error;
1723 }
1724 /* Enforce end of string */
111198c2 1725 a_sign[signature_len - 1] = '\0';
32ce8569
MD
1726
1727 /* recv fields */
1728 if (fields_len) {
1729 a_fields = zmalloc(fields_len);
1730 if (!a_fields) {
1731 len = -ENOMEM;
1732 goto signature_error;
1733 }
1734 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1735 if (len > 0 && len != fields_len) {
1736 len = -EIO;
1737 goto fields_error;
1738 }
1739 if (len == 0) {
1740 len = -EPIPE;
1741 goto fields_error;
1742 }
1743 if (len < 0) {
1744 goto fields_error;
1745 }
1746 }
1747
1748 if (model_emf_uri_len) {
1749 /* recv model_emf_uri_len */
1750 a_model_emf_uri = zmalloc(model_emf_uri_len);
1751 if (!a_model_emf_uri) {
1752 len = -ENOMEM;
1753 goto fields_error;
1754 }
1755 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1756 model_emf_uri_len);
1757 if (len > 0 && len != model_emf_uri_len) {
1758 len = -EIO;
1759 goto model_error;
1760 }
1761 if (len == 0) {
1762 len = -EPIPE;
1763 goto model_error;
1764 }
1765 if (len < 0) {
1766 goto model_error;
1767 }
1768 /* Enforce end of string */
1769 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1770 }
1771
1772 *signature = a_sign;
1773 *nr_fields = fields_len / sizeof(*a_fields);
1774 *fields = a_fields;
1775 *model_emf_uri = a_model_emf_uri;
1776
1777 return 0;
1778
1779model_error:
1780 free(a_model_emf_uri);
1781fields_error:
1782 free(a_fields);
1783signature_error:
1784 free(a_sign);
1785 return len;
1786}
1787
1788/*
1789 * Returns 0 on success, negative error value on error.
1790 */
1791int ustctl_reply_register_event(int sock,
1792 uint32_t id,
1793 int ret_code)
1794{
1795 ssize_t len;
1796 struct {
1797 struct ustcomm_notify_hdr header;
1798 struct ustcomm_notify_event_reply r;
1799 } reply;
1800
1801 memset(&reply, 0, sizeof(reply));
1802 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1803 reply.r.ret_code = ret_code;
1804 reply.r.event_id = id;
1805 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1806 if (len > 0 && len != sizeof(reply))
1807 return -EIO;
1808 if (len < 0)
1809 return len;
1810 return 0;
1811}
1812
1813/*
1814 * Returns 0 on success, negative UST or system error value on error.
1815 */
1816int ustctl_recv_register_channel(int sock,
1817 int *session_objd, /* session descriptor (output) */
1818 int *channel_objd, /* channel descriptor (output) */
1819 size_t *nr_fields,
1820 struct ustctl_field **fields)
1821{
1822 ssize_t len;
1823 struct ustcomm_notify_channel_msg msg;
1824 size_t fields_len;
1825 struct ustctl_field *a_fields;
1826
1827 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1828 if (len > 0 && len != sizeof(msg))
1829 return -EIO;
1830 if (len == 0)
1831 return -EPIPE;
1832 if (len < 0)
1833 return len;
1834
1835 *session_objd = msg.session_objd;
1836 *channel_objd = msg.channel_objd;
1837 fields_len = msg.ctx_fields_len;
1838
1839 if (fields_len % sizeof(*a_fields) != 0) {
1840 return -EINVAL;
1841 }
1842
1843 /* recv fields */
1844 if (fields_len) {
1845 a_fields = zmalloc(fields_len);
1846 if (!a_fields) {
1847 len = -ENOMEM;
1848 goto alloc_error;
1849 }
1850 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1851 if (len > 0 && len != fields_len) {
1852 len = -EIO;
1853 goto fields_error;
1854 }
1855 if (len == 0) {
1856 len = -EPIPE;
1857 goto fields_error;
1858 }
1859 if (len < 0) {
1860 goto fields_error;
1861 }
1862 *fields = a_fields;
1863 } else {
1864 *fields = NULL;
1865 }
1866 *nr_fields = fields_len / sizeof(*a_fields);
1867 return 0;
1868
1869fields_error:
1870 free(a_fields);
1871alloc_error:
1872 return len;
1873}
1874
1875/*
1876 * Returns 0 on success, negative error value on error.
1877 */
1878int ustctl_reply_register_channel(int sock,
1879 uint32_t chan_id,
1880 enum ustctl_channel_header header_type,
1881 int ret_code)
1882{
1883 ssize_t len;
1884 struct {
1885 struct ustcomm_notify_hdr header;
1886 struct ustcomm_notify_channel_reply r;
1887 } reply;
1888
1889 memset(&reply, 0, sizeof(reply));
1890 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1891 reply.r.ret_code = ret_code;
1892 reply.r.chan_id = chan_id;
1893 switch (header_type) {
1894 case USTCTL_CHANNEL_HEADER_COMPACT:
1895 reply.r.header_type = 1;
1896 break;
1897 case USTCTL_CHANNEL_HEADER_LARGE:
1898 reply.r.header_type = 2;
1899 break;
1900 default:
1901 reply.r.header_type = 0;
1902 break;
1903 }
1904 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1905 if (len > 0 && len != sizeof(reply))
1906 return -EIO;
1907 if (len < 0)
1908 return len;
1909 return 0;
1910}
1911
74d81a6c
MD
1912static __attribute__((constructor))
1913void ustctl_init(void)
1914{
1915 init_usterr();
1916 lttng_ring_buffer_metadata_client_init();
1917 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1918 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 1919 lttng_ring_buffer_client_discard_init();
34a91bdb 1920 lttng_ring_buffer_client_discard_rt_init();
03d2d293 1921 lib_ringbuffer_signal_init();
74d81a6c
MD
1922}
1923
1924static __attribute__((destructor))
1925void ustctl_exit(void)
1926{
34a91bdb 1927 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 1928 lttng_ring_buffer_client_discard_exit();
34a91bdb 1929 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
1930 lttng_ring_buffer_client_overwrite_exit();
1931 lttng_ring_buffer_metadata_client_exit();
57773204 1932}
This page took 0.108124 seconds and 4 git commands to generate.