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