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