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