Per-stream ioctl to get the current timestamp
[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
fca361e8
JD
1574int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1575 uint64_t *ts)
1576{
1577 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1578 struct lttng_ust_lib_ring_buffer *buf = stream->buf;
1579 struct lttng_ust_shm_handle *handle = stream->chan->chan->handle;
1580
1581 if (!stream || !ts)
1582 return -EINVAL;
1583 client_cb = get_client_cb(buf, handle);
1584 if (!client_cb || !client_cb->current_timestamp)
1585 return -ENOSYS;
1586 return client_cb->current_timestamp(buf, handle, ts);
1587}
1588
32ce8569
MD
1589/*
1590 * Returns 0 on success, negative error value on error.
1591 */
1592int ustctl_recv_reg_msg(int sock,
1593 enum ustctl_socket_type *type,
1594 uint32_t *major,
1595 uint32_t *minor,
1596 uint32_t *pid,
1597 uint32_t *ppid,
1598 uint32_t *uid,
1599 uint32_t *gid,
1600 uint32_t *bits_per_long,
1601 uint32_t *uint8_t_alignment,
1602 uint32_t *uint16_t_alignment,
1603 uint32_t *uint32_t_alignment,
1604 uint32_t *uint64_t_alignment,
1605 uint32_t *long_alignment,
1606 int *byte_order,
1607 char *name)
1608{
1609 ssize_t len;
1610 struct ustctl_reg_msg reg_msg;
1611
1612 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1613 if (len > 0 && len != sizeof(reg_msg))
1614 return -EIO;
1615 if (len == 0)
1616 return -EPIPE;
1617 if (len < 0)
1618 return len;
1619
1620 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1621 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1622 BIG_ENDIAN : LITTLE_ENDIAN;
1623 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1624 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1625 LITTLE_ENDIAN : BIG_ENDIAN;
1626 } else {
1627 return -LTTNG_UST_ERR_INVAL_MAGIC;
1628 }
1629 switch (reg_msg.socket_type) {
1630 case 0: *type = USTCTL_SOCKET_CMD;
1631 break;
1632 case 1: *type = USTCTL_SOCKET_NOTIFY;
1633 break;
1634 default:
1635 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1636 }
1637 *major = reg_msg.major;
1638 *minor = reg_msg.minor;
1639 *pid = reg_msg.pid;
1640 *ppid = reg_msg.ppid;
1641 *uid = reg_msg.uid;
1642 *gid = reg_msg.gid;
1643 *bits_per_long = reg_msg.bits_per_long;
1644 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1645 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1646 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1647 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1648 *long_alignment = reg_msg.long_alignment;
1649 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1650 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1651 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1652 }
1653
1654 return 0;
1655}
1656
1657int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1658{
1659 struct ustcomm_notify_hdr header;
1660 ssize_t len;
1661
1662 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1663 if (len > 0 && len != sizeof(header))
1664 return -EIO;
1665 if (len == 0)
1666 return -EPIPE;
1667 if (len < 0)
1668 return len;
1669 switch (header.notify_cmd) {
1670 case 0:
1671 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1672 break;
1673 case 1:
1674 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1675 break;
1676 default:
1677 return -EINVAL;
1678 }
1679 return 0;
1680}
1681
1682/*
1683 * Returns 0 on success, negative error value on error.
1684 */
1685int ustctl_recv_register_event(int sock,
1686 int *session_objd,
1687 int *channel_objd,
1688 char *event_name,
1689 int *loglevel,
1690 char **signature,
1691 size_t *nr_fields,
1692 struct ustctl_field **fields,
1693 char **model_emf_uri)
1694{
1695 ssize_t len;
1696 struct ustcomm_notify_event_msg msg;
1697 size_t signature_len, fields_len, model_emf_uri_len;
1698 char *a_sign = NULL, *a_model_emf_uri = NULL;
1699 struct ustctl_field *a_fields = NULL;
1700
1701 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1702 if (len > 0 && len != sizeof(msg))
1703 return -EIO;
1704 if (len == 0)
1705 return -EPIPE;
1706 if (len < 0)
1707 return len;
1708
1709 *session_objd = msg.session_objd;
1710 *channel_objd = msg.channel_objd;
1711 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1712 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1713 *loglevel = msg.loglevel;
1714 signature_len = msg.signature_len;
1715 fields_len = msg.fields_len;
1716
1717 if (fields_len % sizeof(*a_fields) != 0) {
1718 return -EINVAL;
1719 }
1720
1721 model_emf_uri_len = msg.model_emf_uri_len;
1722
1723 /* recv signature. contains at least \0. */
1724 a_sign = zmalloc(signature_len);
1725 if (!a_sign)
1726 return -ENOMEM;
1727 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1728 if (len > 0 && len != signature_len) {
1729 len = -EIO;
1730 goto signature_error;
1731 }
1732 if (len == 0) {
1733 len = -EPIPE;
1734 goto signature_error;
1735 }
1736 if (len < 0) {
1737 goto signature_error;
1738 }
1739 /* Enforce end of string */
111198c2 1740 a_sign[signature_len - 1] = '\0';
32ce8569
MD
1741
1742 /* recv fields */
1743 if (fields_len) {
1744 a_fields = zmalloc(fields_len);
1745 if (!a_fields) {
1746 len = -ENOMEM;
1747 goto signature_error;
1748 }
1749 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1750 if (len > 0 && len != fields_len) {
1751 len = -EIO;
1752 goto fields_error;
1753 }
1754 if (len == 0) {
1755 len = -EPIPE;
1756 goto fields_error;
1757 }
1758 if (len < 0) {
1759 goto fields_error;
1760 }
1761 }
1762
1763 if (model_emf_uri_len) {
1764 /* recv model_emf_uri_len */
1765 a_model_emf_uri = zmalloc(model_emf_uri_len);
1766 if (!a_model_emf_uri) {
1767 len = -ENOMEM;
1768 goto fields_error;
1769 }
1770 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1771 model_emf_uri_len);
1772 if (len > 0 && len != model_emf_uri_len) {
1773 len = -EIO;
1774 goto model_error;
1775 }
1776 if (len == 0) {
1777 len = -EPIPE;
1778 goto model_error;
1779 }
1780 if (len < 0) {
1781 goto model_error;
1782 }
1783 /* Enforce end of string */
1784 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1785 }
1786
1787 *signature = a_sign;
1788 *nr_fields = fields_len / sizeof(*a_fields);
1789 *fields = a_fields;
1790 *model_emf_uri = a_model_emf_uri;
1791
1792 return 0;
1793
1794model_error:
1795 free(a_model_emf_uri);
1796fields_error:
1797 free(a_fields);
1798signature_error:
1799 free(a_sign);
1800 return len;
1801}
1802
1803/*
1804 * Returns 0 on success, negative error value on error.
1805 */
1806int ustctl_reply_register_event(int sock,
1807 uint32_t id,
1808 int ret_code)
1809{
1810 ssize_t len;
1811 struct {
1812 struct ustcomm_notify_hdr header;
1813 struct ustcomm_notify_event_reply r;
1814 } reply;
1815
1816 memset(&reply, 0, sizeof(reply));
1817 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1818 reply.r.ret_code = ret_code;
1819 reply.r.event_id = id;
1820 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1821 if (len > 0 && len != sizeof(reply))
1822 return -EIO;
1823 if (len < 0)
1824 return len;
1825 return 0;
1826}
1827
1828/*
1829 * Returns 0 on success, negative UST or system error value on error.
1830 */
1831int ustctl_recv_register_channel(int sock,
1832 int *session_objd, /* session descriptor (output) */
1833 int *channel_objd, /* channel descriptor (output) */
1834 size_t *nr_fields,
1835 struct ustctl_field **fields)
1836{
1837 ssize_t len;
1838 struct ustcomm_notify_channel_msg msg;
1839 size_t fields_len;
1840 struct ustctl_field *a_fields;
1841
1842 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1843 if (len > 0 && len != sizeof(msg))
1844 return -EIO;
1845 if (len == 0)
1846 return -EPIPE;
1847 if (len < 0)
1848 return len;
1849
1850 *session_objd = msg.session_objd;
1851 *channel_objd = msg.channel_objd;
1852 fields_len = msg.ctx_fields_len;
1853
1854 if (fields_len % sizeof(*a_fields) != 0) {
1855 return -EINVAL;
1856 }
1857
1858 /* recv fields */
1859 if (fields_len) {
1860 a_fields = zmalloc(fields_len);
1861 if (!a_fields) {
1862 len = -ENOMEM;
1863 goto alloc_error;
1864 }
1865 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1866 if (len > 0 && len != fields_len) {
1867 len = -EIO;
1868 goto fields_error;
1869 }
1870 if (len == 0) {
1871 len = -EPIPE;
1872 goto fields_error;
1873 }
1874 if (len < 0) {
1875 goto fields_error;
1876 }
1877 *fields = a_fields;
1878 } else {
1879 *fields = NULL;
1880 }
1881 *nr_fields = fields_len / sizeof(*a_fields);
1882 return 0;
1883
1884fields_error:
1885 free(a_fields);
1886alloc_error:
1887 return len;
1888}
1889
1890/*
1891 * Returns 0 on success, negative error value on error.
1892 */
1893int ustctl_reply_register_channel(int sock,
1894 uint32_t chan_id,
1895 enum ustctl_channel_header header_type,
1896 int ret_code)
1897{
1898 ssize_t len;
1899 struct {
1900 struct ustcomm_notify_hdr header;
1901 struct ustcomm_notify_channel_reply r;
1902 } reply;
1903
1904 memset(&reply, 0, sizeof(reply));
1905 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1906 reply.r.ret_code = ret_code;
1907 reply.r.chan_id = chan_id;
1908 switch (header_type) {
1909 case USTCTL_CHANNEL_HEADER_COMPACT:
1910 reply.r.header_type = 1;
1911 break;
1912 case USTCTL_CHANNEL_HEADER_LARGE:
1913 reply.r.header_type = 2;
1914 break;
1915 default:
1916 reply.r.header_type = 0;
1917 break;
1918 }
1919 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1920 if (len > 0 && len != sizeof(reply))
1921 return -EIO;
1922 if (len < 0)
1923 return len;
1924 return 0;
1925}
1926
74d81a6c
MD
1927static __attribute__((constructor))
1928void ustctl_init(void)
1929{
1930 init_usterr();
1931 lttng_ring_buffer_metadata_client_init();
1932 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1933 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 1934 lttng_ring_buffer_client_discard_init();
34a91bdb 1935 lttng_ring_buffer_client_discard_rt_init();
03d2d293 1936 lib_ringbuffer_signal_init();
74d81a6c
MD
1937}
1938
1939static __attribute__((destructor))
1940void ustctl_exit(void)
1941{
34a91bdb 1942 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 1943 lttng_ring_buffer_client_discard_exit();
34a91bdb 1944 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
1945 lttng_ring_buffer_client_overwrite_exit();
1946 lttng_ring_buffer_metadata_client_exit();
57773204 1947}
This page took 0.110851 seconds and 4 git commands to generate.