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