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