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