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