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