Move private ABI counter client symbols to dedicated header
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204 1/*
c0c0989a 2 * SPDX-License-Identifier: GPL-2.0-only
57773204 3 *
c0c0989a
MJ
4 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
57773204
MD
6 */
7
fb31eb73 8#include <stdint.h>
57773204 9#include <string.h>
fb31eb73 10#include <sys/mman.h>
4e79769f 11#include <unistd.h>
a834901f
MD
12#include <sys/types.h>
13#include <sys/socket.h>
fb31eb73 14
c62a3816 15#include <lttng/ust-config.h>
4318ae1b
MD
16#include <lttng/ust-ctl.h>
17#include <lttng/ust-abi.h>
3208818b 18#include <lttng/ust-endian.h>
bb7ad29d 19
44c72f10 20#include <usterr-signal-safe.h>
b728d87e 21#include <ust-comm.h>
864a1eda 22#include <ust-helper.h>
cd61d9bf 23#include "ust-compat.h"
57773204
MD
24
25#include "../libringbuffer/backend.h"
26#include "../libringbuffer/frontend.h"
bb7ad29d 27#include "../liblttng-ust/ust-events-internal.h"
c9023c93 28#include "../liblttng-ust/wait.h"
b2f3252a 29#include "../liblttng-ust/lttng-rb-clients.h"
f9364363 30#include "../liblttng-ust/clock.h"
92ce256d 31#include "../liblttng-ust/getenv.h"
23d128de 32#include "../liblttng-ust/lttng-tracer-core.h"
b62f8205 33#include "../liblttng-ust/lttng-counter-client.h"
c9023c93 34
ebabbf58
MD
35#include "../libcounter/shm.h"
36#include "../libcounter/smp.h"
37#include "../libcounter/counter.h"
38
c9023c93
MD
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 {
e7bc0ef6 49 struct lttng_ust_channel_buffer *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 {
74d81a6c
MD
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
ebabbf58
MD
68#define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
69struct ustctl_counter_attr {
70 enum ustctl_counter_arithmetic arithmetic;
71 enum ustctl_counter_bitness bitness;
72 uint32_t nr_dimensions;
73 int64_t global_sum_step;
74 struct ustctl_counter_dimension dimensions[USTCTL_COUNTER_ATTR_DIMENSION_MAX];
81bc4972 75 bool coalesce_hits;
ebabbf58
MD
76};
77
78/*
79 * Counter representation within daemon.
80 */
81struct ustctl_daemon_counter {
82 struct lib_counter *counter;
83 const struct lttng_counter_ops *ops;
84 struct ustctl_counter_attr *attr; /* initial attributes */
85};
86
74d81a6c 87extern void lttng_ring_buffer_client_overwrite_init(void);
08a3170c 88extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74d81a6c 89extern void lttng_ring_buffer_client_discard_init(void);
08a3170c 90extern void lttng_ring_buffer_client_discard_rt_init(void);
74d81a6c
MD
91extern void lttng_ring_buffer_metadata_client_init(void);
92extern void lttng_ring_buffer_client_overwrite_exit(void);
08a3170c 93extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74d81a6c 94extern void lttng_ring_buffer_client_discard_exit(void);
08a3170c 95extern void lttng_ring_buffer_client_discard_rt_exit(void);
74d81a6c 96extern void lttng_ring_buffer_metadata_client_exit(void);
ddabe860 97
2be0e72c
MD
98int ustctl_release_handle(int sock, int handle)
99{
100 struct ustcomm_ust_msg lum;
101 struct ustcomm_ust_reply lur;
2be0e72c 102
74d81a6c
MD
103 if (sock < 0 || handle < 0)
104 return 0;
105 memset(&lum, 0, sizeof(lum));
106 lum.handle = handle;
fd17d7ce 107 lum.cmd = LTTNG_UST_ABI_RELEASE;
74d81a6c 108 return ustcomm_send_app_cmd(sock, &lum, &lur);
2be0e72c 109}
74d81a6c 110
12388166
MD
111/*
112 * If sock is negative, it means we don't have to notify the other side
113 * (e.g. application has already vanished).
114 */
fd17d7ce 115int ustctl_release_object(int sock, struct lttng_ust_abi_object_data *data)
57773204 116{
57773204
MD
117 int ret;
118
9bfc503d
MD
119 if (!data)
120 return -EINVAL;
121
74d81a6c 122 switch (data->type) {
fd17d7ce 123 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
124 if (data->u.channel.wakeup_fd >= 0) {
125 ret = close(data->u.channel.wakeup_fd);
126 if (ret < 0) {
127 ret = -errno;
128 return ret;
129 }
dd6c697c 130 data->u.channel.wakeup_fd = -1;
ff0f5728 131 }
74d81a6c 132 free(data->u.channel.data);
dd6c697c 133 data->u.channel.data = NULL;
74d81a6c 134 break;
fd17d7ce 135 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM:
74d81a6c
MD
136 if (data->u.stream.shm_fd >= 0) {
137 ret = close(data->u.stream.shm_fd);
138 if (ret < 0) {
139 ret = -errno;
140 return ret;
141 }
dd6c697c 142 data->u.stream.shm_fd = -1;
d26228ae 143 }
74d81a6c
MD
144 if (data->u.stream.wakeup_fd >= 0) {
145 ret = close(data->u.stream.wakeup_fd);
146 if (ret < 0) {
147 ret = -errno;
148 return ret;
149 }
dd6c697c 150 data->u.stream.wakeup_fd = -1;
d26228ae 151 }
74d81a6c 152 break;
fd17d7ce
MD
153 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT:
154 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT:
155 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP:
156 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER:
32ce8569 157 break;
fd17d7ce 158 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER:
ebabbf58
MD
159 free(data->u.counter.data);
160 data->u.counter.data = NULL;
161 break;
fd17d7ce 162 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL:
ebabbf58
MD
163 if (data->u.counter_global.shm_fd >= 0) {
164 ret = close(data->u.counter_global.shm_fd);
165 if (ret < 0) {
166 ret = -errno;
167 return ret;
168 }
169 data->u.counter_global.shm_fd = -1;
170 }
171 break;
fd17d7ce 172 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU:
ebabbf58
MD
173 if (data->u.counter_cpu.shm_fd >= 0) {
174 ret = close(data->u.counter_cpu.shm_fd);
175 if (ret < 0) {
176 ret = -errno;
177 return ret;
178 }
179 data->u.counter_cpu.shm_fd = -1;
180 }
181 break;
74d81a6c
MD
182 default:
183 assert(0);
d26228ae 184 }
2be0e72c 185 return ustctl_release_handle(sock, data->handle);
57773204
MD
186}
187
1c5e467e
MD
188/*
189 * Send registration done packet to the application.
190 */
191int ustctl_register_done(int sock)
192{
193 struct ustcomm_ust_msg lum;
194 struct ustcomm_ust_reply lur;
195 int ret;
196
197 DBG("Sending register done command to %d", sock);
198 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
199 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
200 lum.cmd = LTTNG_UST_ABI_REGISTER_DONE;
1c5e467e
MD
201 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
202 if (ret)
203 return ret;
1c5e467e 204 return 0;
1c5e467e
MD
205}
206
57773204
MD
207/*
208 * returns session handle.
209 */
210int ustctl_create_session(int sock)
211{
212 struct ustcomm_ust_msg lum;
213 struct ustcomm_ust_reply lur;
214 int ret, session_handle;
215
216 /* Create session */
217 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
218 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
219 lum.cmd = LTTNG_UST_ABI_SESSION;
57773204
MD
220 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
221 if (ret)
222 return ret;
223 session_handle = lur.ret_val;
224 DBG("received session handle %u", session_handle);
225 return session_handle;
226}
227
fd17d7ce
MD
228int ustctl_create_event(int sock, struct lttng_ust_abi_event *ev,
229 struct lttng_ust_abi_object_data *channel_data,
230 struct lttng_ust_abi_object_data **_event_data)
57773204
MD
231{
232 struct ustcomm_ust_msg lum;
233 struct ustcomm_ust_reply lur;
fd17d7ce 234 struct lttng_ust_abi_object_data *event_data;
57773204
MD
235 int ret;
236
9bfc503d
MD
237 if (!channel_data || !_event_data)
238 return -EINVAL;
239
74d81a6c 240 event_data = zmalloc(sizeof(*event_data));
57773204
MD
241 if (!event_data)
242 return -ENOMEM;
fd17d7ce 243 event_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT;
57773204
MD
244 memset(&lum, 0, sizeof(lum));
245 lum.handle = channel_data->handle;
fd17d7ce 246 lum.cmd = LTTNG_UST_ABI_EVENT;
57773204 247 strncpy(lum.u.event.name, ev->name,
fd17d7ce 248 LTTNG_UST_ABI_SYM_NAME_LEN);
57773204 249 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
250 lum.u.event.loglevel_type = ev->loglevel_type;
251 lum.u.event.loglevel = ev->loglevel;
57773204
MD
252 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
253 if (ret) {
254 free(event_data);
255 return ret;
256 }
257 event_data->handle = lur.ret_val;
258 DBG("received event handle %u", event_data->handle);
259 *_event_data = event_data;
260 return 0;
261}
262
53f0df51 263int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx,
fd17d7ce
MD
264 struct lttng_ust_abi_object_data *obj_data,
265 struct lttng_ust_abi_object_data **_context_data)
57773204
MD
266{
267 struct ustcomm_ust_msg lum;
268 struct ustcomm_ust_reply lur;
fd17d7ce 269 struct lttng_ust_abi_object_data *context_data = NULL;
53f0df51
JG
270 char *buf = NULL;
271 size_t len;
57773204
MD
272 int ret;
273
53f0df51
JG
274 if (!obj_data || !_context_data) {
275 ret = -EINVAL;
276 goto end;
277 }
9bfc503d 278
74d81a6c 279 context_data = zmalloc(sizeof(*context_data));
53f0df51
JG
280 if (!context_data) {
281 ret = -ENOMEM;
282 goto end;
283 }
fd17d7ce 284 context_data->type = LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT;
57773204 285 memset(&lum, 0, sizeof(lum));
3039d8ed 286 lum.handle = obj_data->handle;
fd17d7ce 287 lum.cmd = LTTNG_UST_ABI_CONTEXT;
53f0df51
JG
288
289 lum.u.context.ctx = ctx->ctx;
290 switch (ctx->ctx) {
fd17d7ce 291 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
53f0df51
JG
292 lum.u.context.u.perf_counter = ctx->u.perf_counter;
293 break;
fd17d7ce 294 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
53f0df51
JG
295 {
296 size_t provider_name_len = strlen(
297 ctx->u.app_ctx.provider_name) + 1;
298 size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1;
299
300 lum.u.context.u.app_ctx.provider_name_len = provider_name_len;
301 lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len;
302
303 len = provider_name_len + ctx_name_len;
304 buf = zmalloc(len);
305 if (!buf) {
306 ret = -ENOMEM;
307 goto end;
308 }
309 memcpy(buf, ctx->u.app_ctx.provider_name,
310 provider_name_len);
311 memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name,
312 ctx_name_len);
313 break;
314 }
315 default:
316 break;
317 }
318 ret = ustcomm_send_app_msg(sock, &lum);
319 if (ret)
320 goto end;
321 if (buf) {
322 /* send var len ctx_name */
323 ret = ustcomm_send_unix_sock(sock, buf, len);
324 if (ret < 0) {
325 goto end;
326 }
327 if (ret != len) {
328 ret = -EINVAL;
329 goto end;
330 }
331 }
332 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
333 if (ret < 0) {
334 goto end;
57773204 335 }
32ce8569
MD
336 context_data->handle = -1;
337 DBG("Context created successfully");
57773204 338 *_context_data = context_data;
53f0df51
JG
339 context_data = NULL;
340end:
341 free(context_data);
342 free(buf);
57773204
MD
343 return ret;
344}
345
fd17d7ce
MD
346int ustctl_set_filter(int sock, struct lttng_ust_abi_filter_bytecode *bytecode,
347 struct lttng_ust_abi_object_data *obj_data)
cd54f6d9
MD
348{
349 struct ustcomm_ust_msg lum;
350 struct ustcomm_ust_reply lur;
351 int ret;
352
353 if (!obj_data)
354 return -EINVAL;
355
356 memset(&lum, 0, sizeof(lum));
357 lum.handle = obj_data->handle;
fd17d7ce 358 lum.cmd = LTTNG_UST_ABI_FILTER;
cd54f6d9
MD
359 lum.u.filter.data_size = bytecode->len;
360 lum.u.filter.reloc_offset = bytecode->reloc_offset;
e695af51 361 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
362
363 ret = ustcomm_send_app_msg(sock, &lum);
d37ecb3f
FD
364 if (ret)
365 return ret;
366 /* send var len bytecode */
367 ret = ustcomm_send_unix_sock(sock, bytecode->data,
368 bytecode->len);
369 if (ret < 0) {
370 return ret;
371 }
372 if (ret != bytecode->len)
373 return -EINVAL;
374 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
375}
376
fd17d7ce
MD
377int ustctl_set_capture(int sock, struct lttng_ust_abi_capture_bytecode *bytecode,
378 struct lttng_ust_abi_object_data *obj_data)
d37ecb3f
FD
379{
380 struct ustcomm_ust_msg lum;
381 struct ustcomm_ust_reply lur;
382 int ret;
383
384 if (!obj_data)
385 return -EINVAL;
386
387 memset(&lum, 0, sizeof(lum));
388 lum.handle = obj_data->handle;
fd17d7ce 389 lum.cmd = LTTNG_UST_ABI_CAPTURE;
d37ecb3f
FD
390 lum.u.capture.data_size = bytecode->len;
391 lum.u.capture.reloc_offset = bytecode->reloc_offset;
392 lum.u.capture.seqnum = bytecode->seqnum;
393
394 ret = ustcomm_send_app_msg(sock, &lum);
cd54f6d9
MD
395 if (ret)
396 return ret;
cd54f6d9
MD
397 /* send var len bytecode */
398 ret = ustcomm_send_unix_sock(sock, bytecode->data,
399 bytecode->len);
400 if (ret < 0) {
401 return ret;
402 }
7bc53e94
MD
403 if (ret != bytecode->len)
404 return -EINVAL;
405 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
406}
407
fd17d7ce
MD
408int ustctl_set_exclusion(int sock, struct lttng_ust_abi_event_exclusion *exclusion,
409 struct lttng_ust_abi_object_data *obj_data)
da57c034
JI
410{
411 struct ustcomm_ust_msg lum;
412 struct ustcomm_ust_reply lur;
413 int ret;
414
415 if (!obj_data) {
416 return -EINVAL;
417 }
418
419 memset(&lum, 0, sizeof(lum));
420 lum.handle = obj_data->handle;
fd17d7ce 421 lum.cmd = LTTNG_UST_ABI_EXCLUSION;
da57c034
JI
422 lum.u.exclusion.count = exclusion->count;
423
424 ret = ustcomm_send_app_msg(sock, &lum);
425 if (ret) {
426 return ret;
427 }
428
1628366f 429 /* send var len exclusion names */
da57c034
JI
430 ret = ustcomm_send_unix_sock(sock,
431 exclusion->names,
fd17d7ce 432 exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN);
da57c034
JI
433 if (ret < 0) {
434 return ret;
435 }
fd17d7ce 436 if (ret != exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) {
da57c034
JI
437 return -EINVAL;
438 }
439 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
440}
441
57773204 442/* Enable event, channel and session ioctl */
fd17d7ce 443int ustctl_enable(int sock, struct lttng_ust_abi_object_data *object)
57773204
MD
444{
445 struct ustcomm_ust_msg lum;
446 struct ustcomm_ust_reply lur;
447 int ret;
448
9bfc503d
MD
449 if (!object)
450 return -EINVAL;
451
57773204
MD
452 memset(&lum, 0, sizeof(lum));
453 lum.handle = object->handle;
fd17d7ce 454 lum.cmd = LTTNG_UST_ABI_ENABLE;
57773204
MD
455 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
456 if (ret)
457 return ret;
458 DBG("enabled handle %u", object->handle);
459 return 0;
460}
461
462/* Disable event, channel and session ioctl */
fd17d7ce 463int ustctl_disable(int sock, struct lttng_ust_abi_object_data *object)
57773204
MD
464{
465 struct ustcomm_ust_msg lum;
466 struct ustcomm_ust_reply lur;
467 int ret;
468
9bfc503d
MD
469 if (!object)
470 return -EINVAL;
471
57773204
MD
472 memset(&lum, 0, sizeof(lum));
473 lum.handle = object->handle;
fd17d7ce 474 lum.cmd = LTTNG_UST_ABI_DISABLE;
57773204
MD
475 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
476 if (ret)
477 return ret;
478 DBG("disable handle %u", object->handle);
479 return 0;
480}
481
4a6ca058 482int ustctl_start_session(int sock, int handle)
57773204 483{
fd17d7ce 484 struct lttng_ust_abi_object_data obj;
4a6ca058
MD
485
486 obj.handle = handle;
487 return ustctl_enable(sock, &obj);
57773204
MD
488}
489
4a6ca058 490int ustctl_stop_session(int sock, int handle)
57773204 491{
fd17d7ce 492 struct lttng_ust_abi_object_data obj;
4a6ca058
MD
493
494 obj.handle = handle;
495 return ustctl_disable(sock, &obj);
57773204
MD
496}
497
d8d2416d 498int ustctl_create_event_notifier_group(int sock, int pipe_fd,
fd17d7ce 499 struct lttng_ust_abi_object_data **_event_notifier_group_data)
d8d2416d 500{
fd17d7ce 501 struct lttng_ust_abi_object_data *event_notifier_group_data;
d8d2416d
FD
502 struct ustcomm_ust_msg lum;
503 struct ustcomm_ust_reply lur;
504 ssize_t len;
505 int ret;
506
507 if (!_event_notifier_group_data)
508 return -EINVAL;
509
510 event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data));
511 if (!event_notifier_group_data)
512 return -ENOMEM;
513
fd17d7ce 514 event_notifier_group_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP;
d8d2416d
FD
515
516 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
517 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
518 lum.cmd = LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE;
d8d2416d
FD
519
520 ret = ustcomm_send_app_msg(sock, &lum);
521 if (ret)
522 goto error;
523
524 /* Send event_notifier notification pipe. */
525 len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1);
526 if (len <= 0) {
527 ret = len;
528 goto error;
529 }
530
531 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
532 if (ret)
533 goto error;
534
535 event_notifier_group_data->handle = lur.ret_val;
536 DBG("received event_notifier group handle %d", event_notifier_group_data->handle);
537
538 *_event_notifier_group_data = event_notifier_group_data;
539
540 ret = 0;
541 goto end;
542error:
543 free(event_notifier_group_data);
544
545end:
546 return ret;
547}
548
fd17d7ce
MD
549int ustctl_create_event_notifier(int sock, struct lttng_ust_abi_event_notifier *event_notifier,
550 struct lttng_ust_abi_object_data *event_notifier_group,
551 struct lttng_ust_abi_object_data **_event_notifier_data)
d8d2416d
FD
552{
553 struct ustcomm_ust_msg lum;
554 struct ustcomm_ust_reply lur;
fd17d7ce 555 struct lttng_ust_abi_object_data *event_notifier_data;
8406222c 556 ssize_t len;
d8d2416d
FD
557 int ret;
558
559 if (!event_notifier_group || !_event_notifier_data)
560 return -EINVAL;
561
562 event_notifier_data = zmalloc(sizeof(*event_notifier_data));
563 if (!event_notifier_data)
564 return -ENOMEM;
565
fd17d7ce 566 event_notifier_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER;
d8d2416d
FD
567
568 memset(&lum, 0, sizeof(lum));
569 lum.handle = event_notifier_group->handle;
fd17d7ce 570 lum.cmd = LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE;
8406222c 571 lum.u.event_notifier.len = sizeof(*event_notifier);
d8d2416d 572
41844673 573 ret = ustcomm_send_app_msg(sock, &lum);
d8d2416d
FD
574 if (ret) {
575 free(event_notifier_data);
576 return ret;
577 }
31624f6c 578 /* Send struct lttng_ust_abi_event_notifier */
8406222c
MD
579 len = ustcomm_send_unix_sock(sock, event_notifier, sizeof(*event_notifier));
580 if (len != sizeof(*event_notifier)) {
4c4f4917 581 free(event_notifier_data);
8406222c
MD
582 if (len < 0)
583 return len;
584 else
585 return -EIO;
586 }
41844673
MD
587 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
588 if (ret) {
589 free(event_notifier_data);
590 return ret;
591 }
d8d2416d
FD
592 event_notifier_data->handle = lur.ret_val;
593 DBG("received event_notifier handle %u", event_notifier_data->handle);
594 *_event_notifier_data = event_notifier_data;
595
596 return ret;
597}
598
57773204
MD
599int ustctl_tracepoint_list(int sock)
600{
b115631f
MD
601 struct ustcomm_ust_msg lum;
602 struct ustcomm_ust_reply lur;
603 int ret, tp_list_handle;
604
605 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
606 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
607 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_LIST;
b115631f
MD
608 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
609 if (ret)
610 return ret;
611 tp_list_handle = lur.ret_val;
612 DBG("received tracepoint list handle %u", tp_list_handle);
613 return tp_list_handle;
614}
615
616int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
fd17d7ce 617 struct lttng_ust_abi_tracepoint_iter *iter)
b115631f
MD
618{
619 struct ustcomm_ust_msg lum;
620 struct ustcomm_ust_reply lur;
621 int ret;
622
9bfc503d
MD
623 if (!iter)
624 return -EINVAL;
625
b115631f
MD
626 memset(&lum, 0, sizeof(lum));
627 lum.handle = tp_list_handle;
fd17d7ce 628 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_LIST_GET;
b115631f
MD
629 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
630 if (ret)
631 return ret;
882a56d7 632 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 633 lur.u.tracepoint.name,
882a56d7 634 lur.u.tracepoint.loglevel);
cbef6901 635 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 636 return 0;
57773204
MD
637}
638
40003310
MD
639int ustctl_tracepoint_field_list(int sock)
640{
641 struct ustcomm_ust_msg lum;
642 struct ustcomm_ust_reply lur;
643 int ret, tp_field_list_handle;
644
645 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
646 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
647 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST;
40003310
MD
648 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
649 if (ret)
650 return ret;
651 tp_field_list_handle = lur.ret_val;
652 DBG("received tracepoint field list handle %u", tp_field_list_handle);
653 return tp_field_list_handle;
654}
655
656int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
fd17d7ce 657 struct lttng_ust_abi_field_iter *iter)
40003310
MD
658{
659 struct ustcomm_ust_msg lum;
660 struct ustcomm_ust_reply lur;
661 int ret;
662 ssize_t len;
663
664 if (!iter)
665 return -EINVAL;
666
667 memset(&lum, 0, sizeof(lum));
668 lum.handle = tp_field_list_handle;
fd17d7ce 669 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET;
40003310
MD
670 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
671 if (ret)
672 return ret;
673 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
674 if (len != sizeof(*iter)) {
675 return -EINVAL;
676 }
677 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
678 iter->event_name,
679 iter->loglevel,
680 iter->field_name,
681 iter->type);
682 return 0;
683}
684
fd17d7ce 685int ustctl_tracer_version(int sock, struct lttng_ust_abi_tracer_version *v)
57773204
MD
686{
687 struct ustcomm_ust_msg lum;
688 struct ustcomm_ust_reply lur;
689 int ret;
690
9bfc503d
MD
691 if (!v)
692 return -EINVAL;
693
57773204 694 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
695 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
696 lum.cmd = LTTNG_UST_ABI_TRACER_VERSION;
57773204
MD
697 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
698 if (ret)
699 return ret;
700 memcpy(v, &lur.u.version, sizeof(*v));
701 DBG("received tracer version");
702 return 0;
703}
704
705int ustctl_wait_quiescent(int sock)
706{
707 struct ustcomm_ust_msg lum;
708 struct ustcomm_ust_reply lur;
709 int ret;
710
711 memset(&lum, 0, sizeof(lum));
fd17d7ce
MD
712 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
713 lum.cmd = LTTNG_UST_ABI_WAIT_QUIESCENT;
57773204
MD
714 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
715 if (ret)
716 return ret;
717 DBG("waited for quiescent state");
718 return 0;
719}
720
fd17d7ce 721int ustctl_calibrate(int sock, struct lttng_ust_abi_calibrate *calibrate)
57773204 722{
9bfc503d
MD
723 if (!calibrate)
724 return -EINVAL;
725
57773204
MD
726 return -ENOSYS;
727}
728
fd17d7ce 729int ustctl_sock_flush_buffer(int sock, struct lttng_ust_abi_object_data *object)
f1fffc57
MD
730{
731 struct ustcomm_ust_msg lum;
732 struct ustcomm_ust_reply lur;
733 int ret;
734
9bfc503d
MD
735 if (!object)
736 return -EINVAL;
737
f1fffc57
MD
738 memset(&lum, 0, sizeof(lum));
739 lum.handle = object->handle;
fd17d7ce 740 lum.cmd = LTTNG_UST_ABI_FLUSH_BUFFER;
f1fffc57
MD
741 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
742 if (ret)
743 return ret;
744 DBG("flushed buffer handle %u", object->handle);
745 return 0;
746}
747
74d81a6c
MD
748static
749int ustctl_send_channel(int sock,
fd17d7ce 750 enum lttng_ust_abi_chan_type type,
74d81a6c
MD
751 void *data,
752 uint64_t size,
ff0f5728 753 int wakeup_fd,
74d81a6c
MD
754 int send_fd_only)
755{
756 ssize_t len;
757
758 if (!send_fd_only) {
759 /* Send mmap size */
760 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
761 if (len != sizeof(size)) {
762 if (len < 0)
763 return len;
764 else
765 return -EIO;
766 }
767
768 /* Send channel type */
769 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
770 if (len != sizeof(type)) {
771 if (len < 0)
772 return len;
773 else
774 return -EIO;
775 }
776 }
777
778 /* Send channel data */
779 len = ustcomm_send_unix_sock(sock, data, size);
780 if (len != size) {
781 if (len < 0)
782 return len;
783 else
784 return -EIO;
785 }
57773204 786
ff0f5728
MD
787 /* Send wakeup fd */
788 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
789 if (len <= 0) {
790 if (len < 0)
791 return len;
792 else
793 return -EIO;
794 }
74d81a6c
MD
795 return 0;
796}
797
798static
799int ustctl_send_stream(int sock,
800 uint32_t stream_nr,
801 uint64_t memory_map_size,
802 int shm_fd, int wakeup_fd,
803 int send_fd_only)
57773204 804{
74d81a6c
MD
805 ssize_t len;
806 int fds[2];
807
808 if (!send_fd_only) {
809 if (shm_fd < 0) {
810 /* finish iteration */
811 uint64_t v = -1;
812
813 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
814 if (len != sizeof(v)) {
815 if (len < 0)
816 return len;
817 else
818 return -EIO;
819 }
820 return 0;
821 }
822
823 /* Send mmap size */
824 len = ustcomm_send_unix_sock(sock, &memory_map_size,
825 sizeof(memory_map_size));
826 if (len != sizeof(memory_map_size)) {
827 if (len < 0)
828 return len;
829 else
830 return -EIO;
831 }
832
833 /* Send stream nr */
834 len = ustcomm_send_unix_sock(sock, &stream_nr,
835 sizeof(stream_nr));
836 if (len != sizeof(stream_nr)) {
837 if (len < 0)
838 return len;
839 else
840 return -EIO;
841 }
842 }
843
844 /* Send shm fd and wakeup fd */
845 fds[0] = shm_fd;
846 fds[1] = wakeup_fd;
847 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
848 if (len <= 0) {
849 if (len < 0)
850 return len;
851 else
852 return -EIO;
853 }
854 return 0;
855}
856
857int ustctl_recv_channel_from_consumer(int sock,
fd17d7ce 858 struct lttng_ust_abi_object_data **_channel_data)
74d81a6c 859{
fd17d7ce 860 struct lttng_ust_abi_object_data *channel_data;
74d81a6c 861 ssize_t len;
ff0f5728 862 int wakeup_fd;
7a784989 863 int ret;
57773204 864
74d81a6c
MD
865 channel_data = zmalloc(sizeof(*channel_data));
866 if (!channel_data) {
867 ret = -ENOMEM;
868 goto error_alloc;
869 }
fd17d7ce 870 channel_data->type = LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL;
12f3dabc 871 channel_data->handle = -1;
74d81a6c
MD
872
873 /* recv mmap size */
874 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
875 sizeof(channel_data->size));
876 if (len != sizeof(channel_data->size)) {
877 if (len < 0)
878 ret = len;
879 else
880 ret = -EINVAL;
881 goto error;
882 }
9bfc503d 883
74d81a6c
MD
884 /* recv channel type */
885 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
886 sizeof(channel_data->u.channel.type));
887 if (len != sizeof(channel_data->u.channel.type)) {
888 if (len < 0)
889 ret = len;
890 else
891 ret = -EINVAL;
892 goto error;
893 }
894
895 /* recv channel data */
896 channel_data->u.channel.data = zmalloc(channel_data->size);
897 if (!channel_data->u.channel.data) {
898 ret = -ENOMEM;
899 goto error;
900 }
901 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
902 channel_data->size);
903 if (len != channel_data->size) {
904 if (len < 0)
905 ret = len;
906 else
907 ret = -EINVAL;
908 goto error_recv_data;
909 }
ff0f5728
MD
910 /* recv wakeup fd */
911 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
912 if (len <= 0) {
913 if (len < 0) {
914 ret = len;
915 goto error_recv_data;
916 } else {
917 ret = -EIO;
918 goto error_recv_data;
919 }
920 }
921 channel_data->u.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
922 *_channel_data = channel_data;
923 return 0;
924
925error_recv_data:
926 free(channel_data->u.channel.data);
927error:
928 free(channel_data);
929error_alloc:
930 return ret;
931}
932
933int ustctl_recv_stream_from_consumer(int sock,
fd17d7ce 934 struct lttng_ust_abi_object_data **_stream_data)
74d81a6c 935{
fd17d7ce 936 struct lttng_ust_abi_object_data *stream_data;
74d81a6c
MD
937 ssize_t len;
938 int ret;
939 int fds[2];
940
941 stream_data = zmalloc(sizeof(*stream_data));
942 if (!stream_data) {
943 ret = -ENOMEM;
944 goto error_alloc;
57773204 945 }
74d81a6c 946
fd17d7ce 947 stream_data->type = LTTNG_UST_ABI_OBJECT_TYPE_STREAM;
74d81a6c
MD
948 stream_data->handle = -1;
949
950 /* recv mmap size */
951 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
952 sizeof(stream_data->size));
953 if (len != sizeof(stream_data->size)) {
954 if (len < 0)
955 ret = len;
956 else
957 ret = -EINVAL;
958 goto error;
959 }
960 if (stream_data->size == -1) {
961 ret = -LTTNG_UST_ERR_NOENT;
962 goto error;
963 }
964
965 /* recv stream nr */
966 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
967 sizeof(stream_data->u.stream.stream_nr));
968 if (len != sizeof(stream_data->u.stream.stream_nr)) {
969 if (len < 0)
970 ret = len;
971 else
972 ret = -EINVAL;
973 goto error;
974 }
975
976 /* recv shm fd and wakeup fd */
977 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
978 if (len <= 0) {
979 if (len < 0) {
980 ret = len;
981 goto error;
982 } else {
983 ret = -EIO;
984 goto error;
0bfe09ec 985 }
0bfe09ec 986 }
74d81a6c
MD
987 stream_data->u.stream.shm_fd = fds[0];
988 stream_data->u.stream.wakeup_fd = fds[1];
989 *_stream_data = stream_data;
990 return 0;
0bfe09ec 991
74d81a6c
MD
992error:
993 free(stream_data);
994error_alloc:
995 return ret;
996}
997
998int ustctl_send_channel_to_ust(int sock, int session_handle,
fd17d7ce 999 struct lttng_ust_abi_object_data *channel_data)
74d81a6c
MD
1000{
1001 struct ustcomm_ust_msg lum;
1002 struct ustcomm_ust_reply lur;
1003 int ret;
1004
1005 if (!channel_data)
1006 return -EINVAL;
1007
1008 memset(&lum, 0, sizeof(lum));
1009 lum.handle = session_handle;
fd17d7ce 1010 lum.cmd = LTTNG_UST_ABI_CHANNEL;
74d81a6c
MD
1011 lum.u.channel.len = channel_data->size;
1012 lum.u.channel.type = channel_data->u.channel.type;
1013 ret = ustcomm_send_app_msg(sock, &lum);
1014 if (ret)
1015 return ret;
1016
1017 ret = ustctl_send_channel(sock,
1018 channel_data->u.channel.type,
1019 channel_data->u.channel.data,
1020 channel_data->size,
ff0f5728 1021 channel_data->u.channel.wakeup_fd,
74d81a6c
MD
1022 1);
1023 if (ret)
1024 return ret;
1025 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
1026 if (!ret) {
7f2348b8 1027 channel_data->handle = lur.ret_val;
57773204 1028 }
74d81a6c
MD
1029 return ret;
1030}
1031
1032int ustctl_send_stream_to_ust(int sock,
fd17d7ce
MD
1033 struct lttng_ust_abi_object_data *channel_data,
1034 struct lttng_ust_abi_object_data *stream_data)
74d81a6c
MD
1035{
1036 struct ustcomm_ust_msg lum;
1037 struct ustcomm_ust_reply lur;
1038 int ret;
1039
1040 memset(&lum, 0, sizeof(lum));
1041 lum.handle = channel_data->handle;
fd17d7ce 1042 lum.cmd = LTTNG_UST_ABI_STREAM;
74d81a6c
MD
1043 lum.u.stream.len = stream_data->size;
1044 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
1045 ret = ustcomm_send_app_msg(sock, &lum);
1046 if (ret)
1047 return ret;
1048
1049 assert(stream_data);
fd17d7ce 1050 assert(stream_data->type == LTTNG_UST_ABI_OBJECT_TYPE_STREAM);
74d81a6c
MD
1051
1052 ret = ustctl_send_stream(sock,
1053 stream_data->u.stream.stream_nr,
1054 stream_data->size,
1055 stream_data->u.stream.shm_fd,
1056 stream_data->u.stream.wakeup_fd, 1);
1057 if (ret)
1058 return ret;
1059 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
1060}
1061
fd17d7ce
MD
1062int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data **dest,
1063 struct lttng_ust_abi_object_data *src)
12f3dabc 1064{
fd17d7ce 1065 struct lttng_ust_abi_object_data *obj;
12f3dabc
MD
1066 int ret;
1067
1068 if (src->handle != -1) {
1069 ret = -EINVAL;
1070 goto error;
1071 }
1072
1073 obj = zmalloc(sizeof(*obj));
1074 if (!obj) {
1075 ret = -ENOMEM;
1076 goto error;
1077 }
1078
1079 obj->type = src->type;
1080 obj->handle = src->handle;
1081 obj->size = src->size;
1082
1083 switch (obj->type) {
fd17d7ce 1084 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL:
12f3dabc
MD
1085 {
1086 obj->u.channel.type = src->u.channel.type;
1087 if (src->u.channel.wakeup_fd >= 0) {
1088 obj->u.channel.wakeup_fd =
1089 dup(src->u.channel.wakeup_fd);
1090 if (obj->u.channel.wakeup_fd < 0) {
1091 ret = errno;
1092 goto chan_error_wakeup_fd;
1093 }
1094 } else {
1095 obj->u.channel.wakeup_fd =
1096 src->u.channel.wakeup_fd;
1097 }
1098 obj->u.channel.data = zmalloc(obj->size);
1099 if (!obj->u.channel.data) {
1100 ret = -ENOMEM;
1101 goto chan_error_alloc;
1102 }
1103 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
1104 break;
1105
1106 chan_error_alloc:
1107 if (src->u.channel.wakeup_fd >= 0) {
1108 int closeret;
1109
1110 closeret = close(obj->u.channel.wakeup_fd);
1111 if (closeret) {
1112 PERROR("close");
1113 }
1114 }
1115 chan_error_wakeup_fd:
1116 goto error_type;
1117
1118 }
1119
fd17d7ce 1120 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM:
12f3dabc
MD
1121 {
1122 obj->u.stream.stream_nr = src->u.stream.stream_nr;
1123 if (src->u.stream.wakeup_fd >= 0) {
1124 obj->u.stream.wakeup_fd =
1125 dup(src->u.stream.wakeup_fd);
1126 if (obj->u.stream.wakeup_fd < 0) {
1127 ret = errno;
1128 goto stream_error_wakeup_fd;
1129 }
1130 } else {
1131 obj->u.stream.wakeup_fd =
1132 src->u.stream.wakeup_fd;
1133 }
1134
1135 if (src->u.stream.shm_fd >= 0) {
1136 obj->u.stream.shm_fd =
1137 dup(src->u.stream.shm_fd);
1138 if (obj->u.stream.shm_fd < 0) {
1139 ret = errno;
1140 goto stream_error_shm_fd;
1141 }
1142 } else {
1143 obj->u.stream.shm_fd =
1144 src->u.stream.shm_fd;
1145 }
1146 break;
1147
1148 stream_error_shm_fd:
1149 if (src->u.stream.wakeup_fd >= 0) {
1150 int closeret;
1151
1152 closeret = close(obj->u.stream.wakeup_fd);
1153 if (closeret) {
1154 PERROR("close");
1155 }
1156 }
1157 stream_error_wakeup_fd:
1158 goto error_type;
1159 }
1160
fd17d7ce 1161 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER:
ebabbf58
MD
1162 {
1163 obj->u.counter.data = zmalloc(obj->size);
1164 if (!obj->u.counter.data) {
1165 ret = -ENOMEM;
1166 goto error_type;
1167 }
1168 memcpy(obj->u.counter.data, src->u.counter.data, obj->size);
1169 break;
1170 }
1171
fd17d7ce 1172 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL:
ebabbf58
MD
1173 {
1174 if (src->u.counter_global.shm_fd >= 0) {
1175 obj->u.counter_global.shm_fd =
1176 dup(src->u.counter_global.shm_fd);
1177 if (obj->u.counter_global.shm_fd < 0) {
1178 ret = errno;
1179 goto error_type;
1180 }
1181 }
1182 break;
1183 }
1184
fd17d7ce 1185 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU:
ebabbf58
MD
1186 {
1187 obj->u.counter_cpu.cpu_nr = src->u.counter_cpu.cpu_nr;
1188 if (src->u.counter_cpu.shm_fd >= 0) {
1189 obj->u.counter_cpu.shm_fd =
1190 dup(src->u.counter_cpu.shm_fd);
1191 if (obj->u.counter_cpu.shm_fd < 0) {
1192 ret = errno;
1193 goto error_type;
1194 }
1195 }
1196 break;
1197 }
1198
12f3dabc
MD
1199 default:
1200 ret = -EINVAL;
1201 goto error_type;
1202 }
1203
1204 *dest = obj;
1205 return 0;
1206
1207error_type:
1208 free(obj);
1209error:
1210 return ret;
1211}
1212
74d81a6c
MD
1213
1214/* Buffer operations */
1215
5ea386c3
MD
1216int ustctl_get_nr_stream_per_channel(void)
1217{
1218 return num_possible_cpus();
1219}
1220
74d81a6c 1221struct ustctl_consumer_channel *
5ea386c3
MD
1222 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr,
1223 const int *stream_fds, int nr_stream_fds)
74d81a6c
MD
1224{
1225 struct ustctl_consumer_channel *chan;
1226 const char *transport_name;
1227 struct lttng_transport *transport;
1228
1229 switch (attr->type) {
fd17d7ce
MD
1230 case LTTNG_UST_ABI_CHAN_PER_CPU:
1231 if (attr->output == LTTNG_UST_ABI_MMAP) {
34a91bdb
MD
1232 if (attr->overwrite) {
1233 if (attr->read_timer_interval == 0) {
1234 transport_name = "relay-overwrite-mmap";
1235 } else {
1236 transport_name = "relay-overwrite-rt-mmap";
1237 }
1238 } else {
1239 if (attr->read_timer_interval == 0) {
1240 transport_name = "relay-discard-mmap";
1241 } else {
1242 transport_name = "relay-discard-rt-mmap";
1243 }
1244 }
74d81a6c
MD
1245 } else {
1246 return NULL;
1247 }
c1fca457 1248 break;
fd17d7ce
MD
1249 case LTTNG_UST_ABI_CHAN_METADATA:
1250 if (attr->output == LTTNG_UST_ABI_MMAP)
74d81a6c
MD
1251 transport_name = "relay-metadata-mmap";
1252 else
1253 return NULL;
c1fca457
MD
1254 break;
1255 default:
74d81a6c 1256 transport_name = "<unknown>";
c1fca457
MD
1257 return NULL;
1258 }
74d81a6c 1259
65c48d6a 1260 transport = lttng_ust_transport_find(transport_name);
74d81a6c
MD
1261 if (!transport) {
1262 DBG("LTTng transport %s not found\n",
32ce8569 1263 transport_name);
74d81a6c 1264 return NULL;
7a784989 1265 }
74d81a6c
MD
1266
1267 chan = zmalloc(sizeof(*chan));
1268 if (!chan)
1269 return NULL;
1270
a880bae5 1271 chan->chan = transport->ops.priv->channel_create(transport_name, NULL,
32ce8569 1272 attr->subbuf_size, attr->num_subbuf,
74d81a6c 1273 attr->switch_timer_interval,
32ce8569 1274 attr->read_timer_interval,
a9ff648c 1275 attr->uuid, attr->chan_id,
b2c5f61a
MD
1276 stream_fds, nr_stream_fds,
1277 attr->blocking_timeout);
74d81a6c
MD
1278 if (!chan->chan) {
1279 goto chan_error;
1280 }
1281 chan->chan->ops = &transport->ops;
1282 memcpy(&chan->attr, attr, sizeof(chan->attr));
cb7378b3
MD
1283 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
1284 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
74d81a6c
MD
1285 return chan;
1286
1287chan_error:
1288 free(chan);
1289 return NULL;
57773204
MD
1290}
1291
74d81a6c 1292void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 1293{
b24e4e91
MD
1294 (void) ustctl_channel_close_wait_fd(chan);
1295 (void) ustctl_channel_close_wakeup_fd(chan);
a880bae5 1296 chan->chan->ops->priv->channel_destroy(chan->chan);
74d81a6c
MD
1297 free(chan);
1298}
1299
1300int ustctl_send_channel_to_sessiond(int sock,
1301 struct ustctl_consumer_channel *channel)
1302{
1303 struct shm_object_table *table;
57773204 1304
07539b34 1305 table = channel->chan->priv->rb_chan->handle->table;
74d81a6c 1306 if (table->size <= 0)
9bfc503d 1307 return -EINVAL;
74d81a6c
MD
1308 return ustctl_send_channel(sock,
1309 channel->attr.type,
1310 table->objects[0].memory_map,
1311 table->objects[0].memory_map_size,
ff0f5728 1312 channel->wakeup_fd,
74d81a6c
MD
1313 0);
1314}
9bfc503d 1315
74d81a6c
MD
1316int ustctl_send_stream_to_sessiond(int sock,
1317 struct ustctl_consumer_stream *stream)
1318{
1319 if (!stream)
1320 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1321
1322 return ustctl_send_stream(sock,
1323 stream->cpu,
1324 stream->memory_map_size,
1325 stream->shm_fd, stream->wakeup_fd,
1326 0);
57773204
MD
1327}
1328
c9023c93
MD
1329int ustctl_write_metadata_to_channel(
1330 struct ustctl_consumer_channel *channel,
1331 const char *metadata_str, /* NOT null-terminated */
1332 size_t len) /* metadata length */
1333{
1334 struct lttng_ust_lib_ring_buffer_ctx ctx;
e7bc0ef6 1335 struct lttng_ust_channel_buffer *lttng_chan_buf = channel->chan;
07539b34 1336 struct lttng_ust_lib_ring_buffer_channel *rb_chan = lttng_chan_buf->priv->rb_chan;
c9023c93
MD
1337 const char *str = metadata_str;
1338 int ret = 0, waitret;
1339 size_t reserve_len, pos;
1340
1341 for (pos = 0; pos < len; pos += reserve_len) {
1342 reserve_len = min_t(size_t,
07539b34 1343 lttng_chan_buf->ops->priv->packet_avail_size(lttng_chan_buf),
c9023c93 1344 len - pos);
3b8bedd8 1345 lttng_ust_lib_ring_buffer_ctx_init(&ctx, rb_chan, NULL, reserve_len, sizeof(char));
c9023c93
MD
1346 /*
1347 * We don't care about metadata buffer's records lost
1348 * count, because we always retry here. Report error if
1349 * we need to bail out after timeout or being
1350 * interrupted.
1351 */
1352 waitret = wait_cond_interruptible_timeout(
1353 ({
e7bc0ef6 1354 ret = lttng_chan_buf->ops->event_reserve(&ctx, 0);
c9023c93
MD
1355 ret != -ENOBUFS || !ret;
1356 }),
1357 LTTNG_METADATA_TIMEOUT_MSEC);
1358 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1359 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1360 waitret == -EINTR ? "interrupted" :
1361 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1362 if (waitret == -EINTR)
1363 ret = waitret;
1364 goto end;
1365 }
e7bc0ef6
MD
1366 lttng_chan_buf->ops->event_write(&ctx, &str[pos], reserve_len);
1367 lttng_chan_buf->ops->event_commit(&ctx);
c9023c93
MD
1368 }
1369end:
1370 return ret;
1371}
1372
3ef94b0e
JD
1373/*
1374 * Write at most one packet in the channel.
1375 * Returns the number of bytes written on success, < 0 on error.
1376 */
1377ssize_t ustctl_write_one_packet_to_channel(
1378 struct ustctl_consumer_channel *channel,
1379 const char *metadata_str, /* NOT null-terminated */
1380 size_t len) /* metadata length */
1381{
1382 struct lttng_ust_lib_ring_buffer_ctx ctx;
e7bc0ef6 1383 struct lttng_ust_channel_buffer *lttng_chan_buf = channel->chan;
07539b34 1384 struct lttng_ust_lib_ring_buffer_channel *rb_chan = lttng_chan_buf->priv->rb_chan;
3ef94b0e
JD
1385 const char *str = metadata_str;
1386 ssize_t reserve_len;
1387 int ret;
1388
1389 reserve_len = min_t(ssize_t,
07539b34 1390 lttng_chan_buf->ops->priv->packet_avail_size(lttng_chan_buf),
3ef94b0e 1391 len);
3b8bedd8 1392 lttng_ust_lib_ring_buffer_ctx_init(&ctx, rb_chan, NULL, reserve_len, sizeof(char));
e7bc0ef6 1393 ret = lttng_chan_buf->ops->event_reserve(&ctx, 0);
3ef94b0e
JD
1394 if (ret != 0) {
1395 DBG("LTTng: event reservation failed");
1396 assert(ret < 0);
1397 reserve_len = ret;
1398 goto end;
1399 }
e7bc0ef6
MD
1400 lttng_chan_buf->ops->event_write(&ctx, str, reserve_len);
1401 lttng_chan_buf->ops->event_commit(&ctx);
3ef94b0e
JD
1402
1403end:
1404 return reserve_len;
1405}
1406
ff0f5728
MD
1407int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1408{
5198080d 1409 struct lttng_ust_lib_ring_buffer_channel *chan;
cb7378b3 1410 int ret;
ff0f5728 1411
07539b34 1412 chan = consumer_chan->chan->priv->rb_chan;
cb7378b3 1413 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
ff0f5728 1414 chan, chan->handle);
cb7378b3
MD
1415 if (!ret)
1416 consumer_chan->wait_fd = -1;
1417 return ret;
ff0f5728
MD
1418}
1419
1420int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1421{
5198080d 1422 struct lttng_ust_lib_ring_buffer_channel *chan;
cb7378b3 1423 int ret;
ff0f5728 1424
07539b34 1425 chan = consumer_chan->chan->priv->rb_chan;
cb7378b3 1426 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
ff0f5728 1427 chan, chan->handle);
cb7378b3
MD
1428 if (!ret)
1429 consumer_chan->wakeup_fd = -1;
1430 return ret;
ff0f5728
MD
1431}
1432
74d81a6c 1433int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8 1434{
5198080d 1435 struct lttng_ust_lib_ring_buffer_channel *chan;
5224b5c8 1436
07539b34 1437 chan = stream->chan->chan->priv->rb_chan;
ff0f5728 1438 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
07539b34 1439 chan, chan->handle, stream->cpu);
5224b5c8
MD
1440}
1441
74d81a6c 1442int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 1443{
5198080d 1444 struct lttng_ust_lib_ring_buffer_channel *chan;
74d81a6c 1445
07539b34 1446 chan = stream->chan->chan->priv->rb_chan;
ff0f5728 1447 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
07539b34 1448 chan, chan->handle, stream->cpu);
74d81a6c
MD
1449}
1450
1451struct ustctl_consumer_stream *
1452 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1453 int cpu)
1454{
1455 struct ustctl_consumer_stream *stream;
1456 struct lttng_ust_shm_handle *handle;
07539b34 1457 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
74d81a6c
MD
1458 int shm_fd, wait_fd, wakeup_fd;
1459 uint64_t memory_map_size;
4cfec15c 1460 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
1461 int ret;
1462
74d81a6c
MD
1463 if (!channel)
1464 return NULL;
07539b34
MD
1465 rb_chan = channel->chan->priv->rb_chan;
1466 handle = rb_chan->handle;
9bfc503d
MD
1467 if (!handle)
1468 return NULL;
1469
07539b34
MD
1470 buf = channel_get_ring_buffer(&rb_chan->backend.config,
1471 rb_chan, cpu, handle, &shm_fd, &wait_fd,
74d81a6c 1472 &wakeup_fd, &memory_map_size);
6e922b24
MD
1473 if (!buf)
1474 return NULL;
74d81a6c 1475 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
1476 if (ret)
1477 return NULL;
74d81a6c
MD
1478
1479 stream = zmalloc(sizeof(*stream));
1480 if (!stream)
1481 goto alloc_error;
74d81a6c
MD
1482 stream->buf = buf;
1483 stream->chan = channel;
1484 stream->shm_fd = shm_fd;
1485 stream->wait_fd = wait_fd;
1486 stream->wakeup_fd = wakeup_fd;
1487 stream->memory_map_size = memory_map_size;
1488 stream->cpu = cpu;
1489 return stream;
1490
1491alloc_error:
1492 return NULL;
1493}
1494
1495void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1496{
1497 struct lttng_ust_lib_ring_buffer *buf;
1498 struct ustctl_consumer_channel *consumer_chan;
1499
1500 assert(stream);
1501 buf = stream->buf;
1502 consumer_chan = stream->chan;
b24e4e91
MD
1503 (void) ustctl_stream_close_wait_fd(stream);
1504 (void) ustctl_stream_close_wakeup_fd(stream);
07539b34 1505 lib_ring_buffer_release_read(buf, consumer_chan->chan->priv->rb_chan->handle);
74d81a6c 1506 free(stream);
6e922b24
MD
1507}
1508
ff0f5728
MD
1509int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1510{
1511 if (!chan)
1512 return -EINVAL;
07539b34
MD
1513 return shm_get_wait_fd(chan->chan->priv->rb_chan->handle,
1514 &chan->chan->priv->rb_chan->handle->chan._ref);
ff0f5728
MD
1515}
1516
1517int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1518{
1519 if (!chan)
1520 return -EINVAL;
07539b34
MD
1521 return shm_get_wakeup_fd(chan->chan->priv->rb_chan->handle,
1522 &chan->chan->priv->rb_chan->handle->chan._ref);
ff0f5728
MD
1523}
1524
1525int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1526{
74d81a6c
MD
1527 struct lttng_ust_lib_ring_buffer *buf;
1528 struct ustctl_consumer_channel *consumer_chan;
1529
1530 if (!stream)
1531 return -EINVAL;
1532 buf = stream->buf;
1533 consumer_chan = stream->chan;
07539b34 1534 return shm_get_wait_fd(consumer_chan->chan->priv->rb_chan->handle, &buf->self._ref);
74d81a6c
MD
1535}
1536
ff0f5728 1537int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1538{
1539 struct lttng_ust_lib_ring_buffer *buf;
1540 struct ustctl_consumer_channel *consumer_chan;
1541
1542 if (!stream)
1543 return -EINVAL;
1544 buf = stream->buf;
1545 consumer_chan = stream->chan;
07539b34 1546 return shm_get_wakeup_fd(consumer_chan->chan->priv->rb_chan->handle, &buf->self._ref);
6e922b24
MD
1547}
1548
57773204
MD
1549/* For mmap mode, readable without "get" operation */
1550
74d81a6c 1551void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1552{
74d81a6c
MD
1553 struct lttng_ust_lib_ring_buffer *buf;
1554 struct ustctl_consumer_channel *consumer_chan;
1555
1556 if (!stream)
9bfc503d 1557 return NULL;
74d81a6c
MD
1558 buf = stream->buf;
1559 consumer_chan = stream->chan;
07539b34 1560 return shmp(consumer_chan->chan->priv->rb_chan->handle, buf->backend.memory_map);
9095efe9
MD
1561}
1562
57773204 1563/* returns the length to mmap. */
74d81a6c 1564int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1565 unsigned long *len)
1566{
74d81a6c 1567 struct ustctl_consumer_channel *consumer_chan;
57773204 1568 unsigned long mmap_buf_len;
07539b34 1569 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
57773204 1570
74d81a6c 1571 if (!stream)
9bfc503d 1572 return -EINVAL;
74d81a6c 1573 consumer_chan = stream->chan;
07539b34
MD
1574 rb_chan = consumer_chan->chan->priv->rb_chan;
1575 if (rb_chan->backend.config.output != RING_BUFFER_MMAP)
57773204 1576 return -EINVAL;
07539b34
MD
1577 mmap_buf_len = rb_chan->backend.buf_size;
1578 if (rb_chan->backend.extra_reader_sb)
1579 mmap_buf_len += rb_chan->backend.subbuf_size;
57773204
MD
1580 if (mmap_buf_len > INT_MAX)
1581 return -EFBIG;
1582 *len = mmap_buf_len;
1583 return 0;
1584}
1585
1586/* returns the maximum size for sub-buffers. */
74d81a6c 1587int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1588 unsigned long *len)
1589{
74d81a6c 1590 struct ustctl_consumer_channel *consumer_chan;
07539b34 1591 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
57773204 1592
74d81a6c 1593 if (!stream)
9bfc503d 1594 return -EINVAL;
74d81a6c 1595 consumer_chan = stream->chan;
07539b34
MD
1596 rb_chan = consumer_chan->chan->priv->rb_chan;
1597 *len = rb_chan->backend.subbuf_size;
57773204
MD
1598 return 0;
1599}
1600
1601/*
1602 * For mmap mode, operate on the current packet (between get/put or
1603 * get_next/put_next).
1604 */
1605
1606/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1607int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1608 unsigned long *off)
57773204 1609{
07539b34 1610 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
57773204 1611 unsigned long sb_bindex;
74d81a6c
MD
1612 struct lttng_ust_lib_ring_buffer *buf;
1613 struct ustctl_consumer_channel *consumer_chan;
34daae3e
MD
1614 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx;
1615 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
57773204 1616
74d81a6c 1617 if (!stream)
9bfc503d 1618 return -EINVAL;
74d81a6c
MD
1619 buf = stream->buf;
1620 consumer_chan = stream->chan;
07539b34
MD
1621 rb_chan = consumer_chan->chan->priv->rb_chan;
1622 if (rb_chan->backend.config.output != RING_BUFFER_MMAP)
57773204 1623 return -EINVAL;
07539b34 1624 sb_bindex = subbuffer_id_get_index(&rb_chan->backend.config,
32ce8569 1625 buf->backend.buf_rsb.id);
07539b34 1626 barray_idx = shmp_index(rb_chan->handle, buf->backend.array,
34daae3e
MD
1627 sb_bindex);
1628 if (!barray_idx)
1629 return -EINVAL;
07539b34 1630 pages = shmp(rb_chan->handle, barray_idx->shmp);
34daae3e
MD
1631 if (!pages)
1632 return -EINVAL;
1633 *off = pages->mmap_offset;
57773204
MD
1634 return 0;
1635}
1636
1637/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1638int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1639 unsigned long *len)
57773204 1640{
74d81a6c 1641 struct ustctl_consumer_channel *consumer_chan;
07539b34 1642 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
74d81a6c 1643 struct lttng_ust_lib_ring_buffer *buf;
57773204 1644
74d81a6c 1645 if (!stream)
9bfc503d
MD
1646 return -EINVAL;
1647
74d81a6c
MD
1648 buf = stream->buf;
1649 consumer_chan = stream->chan;
07539b34
MD
1650 rb_chan = consumer_chan->chan->priv->rb_chan;
1651 *len = lib_ring_buffer_get_read_data_size(&rb_chan->backend.config, buf,
1652 rb_chan->handle);
57773204
MD
1653 return 0;
1654}
1655
1656/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1657int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1658 unsigned long *len)
57773204 1659{
74d81a6c 1660 struct ustctl_consumer_channel *consumer_chan;
07539b34 1661 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
74d81a6c 1662 struct lttng_ust_lib_ring_buffer *buf;
57773204 1663
74d81a6c 1664 if (!stream)
9bfc503d 1665 return -EINVAL;
74d81a6c
MD
1666 buf = stream->buf;
1667 consumer_chan = stream->chan;
07539b34
MD
1668 rb_chan = consumer_chan->chan->priv->rb_chan;
1669 *len = lib_ring_buffer_get_read_data_size(&rb_chan->backend.config, buf,
1670 rb_chan->handle);
b72687b8 1671 *len = LTTNG_UST_PAGE_ALIGN(*len);
57773204
MD
1672 return 0;
1673}
1674
1675/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1676int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1677{
74d81a6c
MD
1678 struct lttng_ust_lib_ring_buffer *buf;
1679 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1680
74d81a6c
MD
1681 if (!stream)
1682 return -EINVAL;
1683 buf = stream->buf;
1684 consumer_chan = stream->chan;
1685 return lib_ring_buffer_get_next_subbuf(buf,
07539b34 1686 consumer_chan->chan->priv->rb_chan->handle);
57773204
MD
1687}
1688
1689
1690/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1691int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1692{
74d81a6c
MD
1693 struct lttng_ust_lib_ring_buffer *buf;
1694 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1695
74d81a6c
MD
1696 if (!stream)
1697 return -EINVAL;
1698 buf = stream->buf;
1699 consumer_chan = stream->chan;
07539b34 1700 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->priv->rb_chan->handle);
57773204
MD
1701 return 0;
1702}
1703
1704/* snapshot */
1705
1706/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1707int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1708{
74d81a6c
MD
1709 struct lttng_ust_lib_ring_buffer *buf;
1710 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1711
74d81a6c
MD
1712 if (!stream)
1713 return -EINVAL;
1714 buf = stream->buf;
1715 consumer_chan = stream->chan;
57773204 1716 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
07539b34 1717 &buf->prod_snapshot, consumer_chan->chan->priv->rb_chan->handle);
57773204
MD
1718}
1719
f45930b7
JG
1720/*
1721 * Get a snapshot of the current ring buffer producer and consumer positions
1722 * even if the consumed and produced positions are contained within the same
1723 * subbuffer.
1724 */
1725int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream)
1726{
1727 struct lttng_ust_lib_ring_buffer *buf;
1728 struct ustctl_consumer_channel *consumer_chan;
1729
1730 if (!stream)
1731 return -EINVAL;
1732 buf = stream->buf;
1733 consumer_chan = stream->chan;
1734 return lib_ring_buffer_snapshot_sample_positions(buf,
1735 &buf->cons_snapshot, &buf->prod_snapshot,
07539b34 1736 consumer_chan->chan->priv->rb_chan->handle);
f45930b7
JG
1737}
1738
57773204 1739/* Get the consumer position (iteration start) */
74d81a6c
MD
1740int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1741 unsigned long *pos)
57773204 1742{
74d81a6c 1743 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1744
74d81a6c
MD
1745 if (!stream)
1746 return -EINVAL;
1747 buf = stream->buf;
57773204
MD
1748 *pos = buf->cons_snapshot;
1749 return 0;
1750}
1751
1752/* Get the producer position (iteration end) */
74d81a6c
MD
1753int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1754 unsigned long *pos)
57773204 1755{
74d81a6c 1756 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1757
74d81a6c
MD
1758 if (!stream)
1759 return -EINVAL;
1760 buf = stream->buf;
57773204
MD
1761 *pos = buf->prod_snapshot;
1762 return 0;
1763}
1764
1765/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1766int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1767 unsigned long *pos)
57773204 1768{
74d81a6c
MD
1769 struct lttng_ust_lib_ring_buffer *buf;
1770 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1771
74d81a6c
MD
1772 if (!stream)
1773 return -EINVAL;
1774 buf = stream->buf;
1775 consumer_chan = stream->chan;
1776 return lib_ring_buffer_get_subbuf(buf, *pos,
07539b34 1777 consumer_chan->chan->priv->rb_chan->handle);
57773204
MD
1778}
1779
1780/* Release exclusive sub-buffer access */
74d81a6c 1781int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1782{
74d81a6c
MD
1783 struct lttng_ust_lib_ring_buffer *buf;
1784 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1785
74d81a6c
MD
1786 if (!stream)
1787 return -EINVAL;
1788 buf = stream->buf;
1789 consumer_chan = stream->chan;
07539b34 1790 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->priv->rb_chan->handle);
57773204
MD
1791 return 0;
1792}
1793
74d81a6c 1794void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1795 int producer_active)
57773204 1796{
74d81a6c
MD
1797 struct lttng_ust_lib_ring_buffer *buf;
1798 struct ustctl_consumer_channel *consumer_chan;
1799
1800 assert(stream);
1801 buf = stream->buf;
1802 consumer_chan = stream->chan;
b52190f2
MD
1803 lib_ring_buffer_switch_slow(buf,
1804 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
07539b34 1805 consumer_chan->chan->priv->rb_chan->handle);
74d81a6c
MD
1806}
1807
beca55a1
MD
1808void ustctl_clear_buffer(struct ustctl_consumer_stream *stream)
1809{
1810 struct lttng_ust_lib_ring_buffer *buf;
1811 struct ustctl_consumer_channel *consumer_chan;
1812
1813 assert(stream);
1814 buf = stream->buf;
1815 consumer_chan = stream->chan;
1816 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
07539b34
MD
1817 consumer_chan->chan->priv->rb_chan->handle);
1818 lib_ring_buffer_clear_reader(buf, consumer_chan->chan->priv->rb_chan->handle);
beca55a1
MD
1819}
1820
b2f3252a
JD
1821static
1822struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1823 struct lttng_ust_lib_ring_buffer *buf,
07539b34 1824 struct lttng_ust_lib_ring_buffer_channel *chan)
b2f3252a 1825{
b2f3252a
JD
1826 const struct lttng_ust_lib_ring_buffer_config *config;
1827 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1828
b2f3252a
JD
1829 config = &chan->backend.config;
1830 if (!config->cb_ptr)
1831 return NULL;
1832 client_cb = caa_container_of(config->cb_ptr,
1833 struct lttng_ust_client_lib_ring_buffer_client_cb,
1834 parent);
1835 return client_cb;
1836}
1837
1838int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1839 uint64_t *timestamp_begin)
1840{
1841 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1842 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1843 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1844
1845 if (!stream || !timestamp_begin)
1846 return -EINVAL;
e1919a41 1847 buf = stream->buf;
07539b34
MD
1848 chan = stream->chan->chan->priv->rb_chan;
1849 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1850 if (!client_cb)
1851 return -ENOSYS;
07539b34 1852 return client_cb->timestamp_begin(buf, chan, timestamp_begin);
b2f3252a
JD
1853}
1854
1855int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1856 uint64_t *timestamp_end)
1857{
1858 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1859 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1860 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1861
1862 if (!stream || !timestamp_end)
1863 return -EINVAL;
e1919a41 1864 buf = stream->buf;
07539b34
MD
1865 chan = stream->chan->chan->priv->rb_chan;
1866 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1867 if (!client_cb)
1868 return -ENOSYS;
07539b34 1869 return client_cb->timestamp_end(buf, chan, timestamp_end);
b2f3252a
JD
1870}
1871
1872int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1873 uint64_t *events_discarded)
1874{
1875 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1876 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1877 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1878
1879 if (!stream || !events_discarded)
1880 return -EINVAL;
e1919a41 1881 buf = stream->buf;
07539b34
MD
1882 chan = stream->chan->chan->priv->rb_chan;
1883 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1884 if (!client_cb)
1885 return -ENOSYS;
07539b34 1886 return client_cb->events_discarded(buf, chan, events_discarded);
b2f3252a
JD
1887}
1888
1889int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1890 uint64_t *content_size)
1891{
1892 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1893 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1894 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1895
1896 if (!stream || !content_size)
1897 return -EINVAL;
e1919a41 1898 buf = stream->buf;
07539b34
MD
1899 chan = stream->chan->chan->priv->rb_chan;
1900 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1901 if (!client_cb)
1902 return -ENOSYS;
07539b34 1903 return client_cb->content_size(buf, chan, content_size);
b2f3252a
JD
1904}
1905
1906int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1907 uint64_t *packet_size)
1908{
1909 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1910 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1911 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1912
1913 if (!stream || !packet_size)
1914 return -EINVAL;
e1919a41 1915 buf = stream->buf;
07539b34
MD
1916 chan = stream->chan->chan->priv->rb_chan;
1917 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1918 if (!client_cb)
1919 return -ENOSYS;
07539b34 1920 return client_cb->packet_size(buf, chan, packet_size);
b2f3252a
JD
1921}
1922
1923int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1924 uint64_t *stream_id)
1925{
1926 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1927 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1928 struct lttng_ust_lib_ring_buffer *buf;
b2f3252a
JD
1929
1930 if (!stream || !stream_id)
1931 return -EINVAL;
e1919a41 1932 buf = stream->buf;
07539b34
MD
1933 chan = stream->chan->chan->priv->rb_chan;
1934 client_cb = get_client_cb(buf, chan);
b2f3252a
JD
1935 if (!client_cb)
1936 return -ENOSYS;
07539b34 1937 return client_cb->stream_id(buf, chan, stream_id);
b2f3252a
JD
1938}
1939
fca361e8
JD
1940int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1941 uint64_t *ts)
1942{
1943 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1944 struct lttng_ust_lib_ring_buffer_channel *chan;
e1919a41 1945 struct lttng_ust_lib_ring_buffer *buf;
fca361e8
JD
1946
1947 if (!stream || !ts)
1948 return -EINVAL;
e1919a41 1949 buf = stream->buf;
07539b34
MD
1950 chan = stream->chan->chan->priv->rb_chan;
1951 client_cb = get_client_cb(buf, chan);
fca361e8
JD
1952 if (!client_cb || !client_cb->current_timestamp)
1953 return -ENOSYS;
07539b34 1954 return client_cb->current_timestamp(buf, chan, ts);
fca361e8
JD
1955}
1956
1ff31389
JD
1957int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream,
1958 uint64_t *seq)
1959{
1960 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1961 struct lttng_ust_lib_ring_buffer_channel *chan;
1ff31389 1962 struct lttng_ust_lib_ring_buffer *buf;
1ff31389
JD
1963
1964 if (!stream || !seq)
1965 return -EINVAL;
1966 buf = stream->buf;
07539b34
MD
1967 chan = stream->chan->chan->priv->rb_chan;
1968 client_cb = get_client_cb(buf, chan);
1ff31389
JD
1969 if (!client_cb || !client_cb->sequence_number)
1970 return -ENOSYS;
07539b34 1971 return client_cb->sequence_number(buf, chan, seq);
1ff31389
JD
1972}
1973
45a00b05
JD
1974int ustctl_get_instance_id(struct ustctl_consumer_stream *stream,
1975 uint64_t *id)
1976{
1977 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
07539b34 1978 struct lttng_ust_lib_ring_buffer_channel *chan;
45a00b05 1979 struct lttng_ust_lib_ring_buffer *buf;
45a00b05
JD
1980
1981 if (!stream || !id)
1982 return -EINVAL;
1983 buf = stream->buf;
07539b34
MD
1984 chan = stream->chan->chan->priv->rb_chan;
1985 client_cb = get_client_cb(buf, chan);
45a00b05
JD
1986 if (!client_cb)
1987 return -ENOSYS;
07539b34 1988 return client_cb->instance_id(buf, chan, id);
45a00b05
JD
1989}
1990
eeef0055 1991#ifdef HAVE_LINUX_PERF_EVENT_H
57201bb3
MD
1992
1993int ustctl_has_perf_counters(void)
1994{
1995 return 1;
1996}
1997
1998#else
1999
2000int ustctl_has_perf_counters(void)
2001{
2002 return 0;
2003}
2004
2005#endif
2006
a834901f
MD
2007#ifdef __linux__
2008/*
2009 * Override application pid/uid/gid with unix socket credentials. If
2010 * the application announced a pid matching our view, it means it is
2011 * within the same pid namespace, so expose the ppid provided by the
2012 * application.
2013 */
2014static
2015int get_cred(int sock,
2016 const struct ustctl_reg_msg *reg_msg,
2017 uint32_t *pid,
2018 uint32_t *ppid,
2019 uint32_t *uid,
2020 uint32_t *gid)
2021{
2022 struct ucred ucred;
2023 socklen_t ucred_len = sizeof(struct ucred);
2024 int ret;
2025
2026 ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len);
2027 if (ret) {
2028 return -LTTNG_UST_ERR_PEERCRED;
2029 }
2030 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2031 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2032 ucred.pid, ucred.uid, ucred.gid,
2033 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2034 if (!ucred.pid) {
2035 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2036 return -LTTNG_UST_ERR_PEERCRED_PID;
2037 }
2038 *pid = ucred.pid;
2039 *uid = ucred.uid;
2040 *gid = ucred.gid;
2041 if (ucred.pid == reg_msg->pid) {
2042 *ppid = reg_msg->ppid;
2043 } else {
2044 *ppid = 0;
2045 }
2046 return 0;
2047}
2048#elif defined(__FreeBSD__)
2049#include <sys/ucred.h>
e494a9cd 2050#include <sys/un.h>
a834901f
MD
2051
2052/*
2053 * Override application uid/gid with unix socket credentials. Use the
2054 * first group of the cr_groups.
2055 * Use the pid and ppid provided by the application on registration.
2056 */
2057static
2058int get_cred(int sock,
2059 const struct ustctl_reg_msg *reg_msg,
2060 uint32_t *pid,
2061 uint32_t *ppid,
2062 uint32_t *uid,
2063 uint32_t *gid)
2064{
2065 struct xucred xucred;
2066 socklen_t xucred_len = sizeof(struct xucred);
2067 int ret;
2068
2069 ret = getsockopt(sock, SOL_SOCKET, LOCAL_PEERCRED, &xucred, &xucred_len);
2070 if (ret) {
2071 return -LTTNG_UST_ERR_PEERCRED;
2072 }
2073 if (xucred.cr_version != XUCRED_VERSION || xucred.cr_ngroups < 1) {
2074 return -LTTNG_UST_ERR_PEERCRED;
2075 }
2076 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2077 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
e494a9cd 2078 xucred.cr_uid, xucred.cr_groups[0],
a834901f
MD
2079 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2080 *pid = reg_msg->pid;
2081 *ppid = reg_msg->ppid;
e494a9cd 2082 *uid = xucred.cr_uid;
a834901f
MD
2083 *gid = xucred.cr_groups[0];
2084 return 0;
2085}
2086#else
2087#warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2088static
2089int get_cred(int sock,
2090 const struct ustctl_reg_msg *reg_msg,
2091 uint32_t *pid,
2092 uint32_t *ppid,
2093 uint32_t *uid,
2094 uint32_t *gid)
2095{
2096 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2097 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2098 *pid = reg_msg->pid;
2099 *ppid = reg_msg->ppid;
2100 *uid = reg_msg->uid;
2101 *gid = reg_msg->gid;
2102 return 0;
2103}
2104#endif
2105
32ce8569
MD
2106/*
2107 * Returns 0 on success, negative error value on error.
2108 */
2109int ustctl_recv_reg_msg(int sock,
2110 enum ustctl_socket_type *type,
2111 uint32_t *major,
2112 uint32_t *minor,
2113 uint32_t *pid,
2114 uint32_t *ppid,
2115 uint32_t *uid,
2116 uint32_t *gid,
2117 uint32_t *bits_per_long,
2118 uint32_t *uint8_t_alignment,
2119 uint32_t *uint16_t_alignment,
2120 uint32_t *uint32_t_alignment,
2121 uint32_t *uint64_t_alignment,
2122 uint32_t *long_alignment,
2123 int *byte_order,
2124 char *name)
2125{
2126 ssize_t len;
2127 struct ustctl_reg_msg reg_msg;
2128
2129 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
2130 if (len > 0 && len != sizeof(reg_msg))
2131 return -EIO;
2132 if (len == 0)
2133 return -EPIPE;
2134 if (len < 0)
2135 return len;
2136
fd17d7ce 2137 if (reg_msg.magic == LTTNG_UST_ABI_COMM_MAGIC) {
32ce8569
MD
2138 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
2139 BIG_ENDIAN : LITTLE_ENDIAN;
fd17d7ce 2140 } else if (reg_msg.magic == bswap_32(LTTNG_UST_ABI_COMM_MAGIC)) {
32ce8569
MD
2141 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
2142 LITTLE_ENDIAN : BIG_ENDIAN;
2143 } else {
2144 return -LTTNG_UST_ERR_INVAL_MAGIC;
2145 }
2146 switch (reg_msg.socket_type) {
2147 case 0: *type = USTCTL_SOCKET_CMD;
2148 break;
2149 case 1: *type = USTCTL_SOCKET_NOTIFY;
2150 break;
2151 default:
2152 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
2153 }
2154 *major = reg_msg.major;
2155 *minor = reg_msg.minor;
32ce8569
MD
2156 *bits_per_long = reg_msg.bits_per_long;
2157 *uint8_t_alignment = reg_msg.uint8_t_alignment;
2158 *uint16_t_alignment = reg_msg.uint16_t_alignment;
2159 *uint32_t_alignment = reg_msg.uint32_t_alignment;
2160 *uint64_t_alignment = reg_msg.uint64_t_alignment;
2161 *long_alignment = reg_msg.long_alignment;
2162 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
6a359b8a
MD
2163 if (reg_msg.major < LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE ||
2164 reg_msg.major > LTTNG_UST_ABI_MAJOR_VERSION) {
32ce8569
MD
2165 return -LTTNG_UST_ERR_UNSUP_MAJOR;
2166 }
a834901f 2167 return get_cred(sock, &reg_msg, pid, ppid, uid, gid);
32ce8569
MD
2168}
2169
2170int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
2171{
2172 struct ustcomm_notify_hdr header;
2173 ssize_t len;
2174
2175 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
2176 if (len > 0 && len != sizeof(header))
2177 return -EIO;
2178 if (len == 0)
2179 return -EPIPE;
2180 if (len < 0)
2181 return len;
2182 switch (header.notify_cmd) {
2183 case 0:
2184 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2185 break;
2186 case 1:
2187 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2188 break;
c785c634
MD
2189 case 2:
2190 *notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2191 break;
32ce8569
MD
2192 default:
2193 return -EINVAL;
2194 }
2195 return 0;
2196}
2197
2198/*
2199 * Returns 0 on success, negative error value on error.
2200 */
2201int ustctl_recv_register_event(int sock,
2202 int *session_objd,
2203 int *channel_objd,
2204 char *event_name,
2205 int *loglevel,
2206 char **signature,
2207 size_t *nr_fields,
2208 struct ustctl_field **fields,
2209 char **model_emf_uri)
2210{
2211 ssize_t len;
2212 struct ustcomm_notify_event_msg msg;
2213 size_t signature_len, fields_len, model_emf_uri_len;
2214 char *a_sign = NULL, *a_model_emf_uri = NULL;
2215 struct ustctl_field *a_fields = NULL;
2216
2217 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2218 if (len > 0 && len != sizeof(msg))
2219 return -EIO;
2220 if (len == 0)
2221 return -EPIPE;
2222 if (len < 0)
2223 return len;
2224
2225 *session_objd = msg.session_objd;
2226 *channel_objd = msg.channel_objd;
fd17d7ce
MD
2227 strncpy(event_name, msg.event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
2228 event_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
32ce8569
MD
2229 *loglevel = msg.loglevel;
2230 signature_len = msg.signature_len;
2231 fields_len = msg.fields_len;
2232
2233 if (fields_len % sizeof(*a_fields) != 0) {
2234 return -EINVAL;
2235 }
2236
2237 model_emf_uri_len = msg.model_emf_uri_len;
2238
2239 /* recv signature. contains at least \0. */
2240 a_sign = zmalloc(signature_len);
2241 if (!a_sign)
2242 return -ENOMEM;
2243 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
2244 if (len > 0 && len != signature_len) {
2245 len = -EIO;
2246 goto signature_error;
2247 }
2248 if (len == 0) {
2249 len = -EPIPE;
2250 goto signature_error;
2251 }
2252 if (len < 0) {
2253 goto signature_error;
2254 }
2255 /* Enforce end of string */
111198c2 2256 a_sign[signature_len - 1] = '\0';
32ce8569
MD
2257
2258 /* recv fields */
2259 if (fields_len) {
2260 a_fields = zmalloc(fields_len);
2261 if (!a_fields) {
2262 len = -ENOMEM;
2263 goto signature_error;
2264 }
2265 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2266 if (len > 0 && len != fields_len) {
2267 len = -EIO;
2268 goto fields_error;
2269 }
2270 if (len == 0) {
2271 len = -EPIPE;
2272 goto fields_error;
2273 }
2274 if (len < 0) {
2275 goto fields_error;
2276 }
2277 }
2278
2279 if (model_emf_uri_len) {
2280 /* recv model_emf_uri_len */
2281 a_model_emf_uri = zmalloc(model_emf_uri_len);
2282 if (!a_model_emf_uri) {
2283 len = -ENOMEM;
2284 goto fields_error;
2285 }
2286 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
2287 model_emf_uri_len);
2288 if (len > 0 && len != model_emf_uri_len) {
2289 len = -EIO;
2290 goto model_error;
2291 }
2292 if (len == 0) {
2293 len = -EPIPE;
2294 goto model_error;
2295 }
2296 if (len < 0) {
2297 goto model_error;
2298 }
2299 /* Enforce end of string */
2300 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
2301 }
2302
2303 *signature = a_sign;
2304 *nr_fields = fields_len / sizeof(*a_fields);
2305 *fields = a_fields;
2306 *model_emf_uri = a_model_emf_uri;
2307
2308 return 0;
2309
2310model_error:
2311 free(a_model_emf_uri);
2312fields_error:
2313 free(a_fields);
2314signature_error:
2315 free(a_sign);
2316 return len;
2317}
2318
2319/*
2320 * Returns 0 on success, negative error value on error.
2321 */
2322int ustctl_reply_register_event(int sock,
2323 uint32_t id,
2324 int ret_code)
2325{
2326 ssize_t len;
2327 struct {
2328 struct ustcomm_notify_hdr header;
2329 struct ustcomm_notify_event_reply r;
2330 } reply;
2331
2332 memset(&reply, 0, sizeof(reply));
2333 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2334 reply.r.ret_code = ret_code;
2335 reply.r.event_id = id;
2336 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2337 if (len > 0 && len != sizeof(reply))
2338 return -EIO;
2339 if (len < 0)
2340 return len;
2341 return 0;
2342}
2343
c785c634
MD
2344/*
2345 * Returns 0 on success, negative UST or system error value on error.
2346 */
2347int ustctl_recv_register_enum(int sock,
2348 int *session_objd,
2349 char *enum_name,
2350 struct ustctl_enum_entry **entries,
2351 size_t *nr_entries)
2352{
2353 ssize_t len;
2354 struct ustcomm_notify_enum_msg msg;
2355 size_t entries_len;
2356 struct ustctl_enum_entry *a_entries = NULL;
2357
2358 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2359 if (len > 0 && len != sizeof(msg))
2360 return -EIO;
2361 if (len == 0)
2362 return -EPIPE;
2363 if (len < 0)
2364 return len;
2365
2366 *session_objd = msg.session_objd;
fd17d7ce
MD
2367 strncpy(enum_name, msg.enum_name, LTTNG_UST_ABI_SYM_NAME_LEN);
2368 enum_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
c785c634
MD
2369 entries_len = msg.entries_len;
2370
2371 if (entries_len % sizeof(*a_entries) != 0) {
2372 return -EINVAL;
2373 }
2374
2375 /* recv entries */
2376 if (entries_len) {
2377 a_entries = zmalloc(entries_len);
2378 if (!a_entries)
2379 return -ENOMEM;
2380 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2381 if (len > 0 && len != entries_len) {
2382 len = -EIO;
2383 goto entries_error;
2384 }
2385 if (len == 0) {
2386 len = -EPIPE;
2387 goto entries_error;
2388 }
2389 if (len < 0) {
2390 goto entries_error;
2391 }
2392 }
2393 *nr_entries = entries_len / sizeof(*a_entries);
2394 *entries = a_entries;
2395
2396 return 0;
2397
2398entries_error:
2399 free(a_entries);
2400 return len;
2401}
2402
2403/*
2404 * Returns 0 on success, negative error value on error.
2405 */
2406int ustctl_reply_register_enum(int sock,
2407 uint64_t id,
2408 int ret_code)
2409{
2410 ssize_t len;
2411 struct {
2412 struct ustcomm_notify_hdr header;
2413 struct ustcomm_notify_enum_reply r;
2414 } reply;
2415
2416 memset(&reply, 0, sizeof(reply));
2417 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2418 reply.r.ret_code = ret_code;
2419 reply.r.enum_id = id;
2420 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2421 if (len > 0 && len != sizeof(reply))
2422 return -EIO;
2423 if (len < 0)
2424 return len;
2425 return 0;
2426}
2427
32ce8569
MD
2428/*
2429 * Returns 0 on success, negative UST or system error value on error.
2430 */
2431int ustctl_recv_register_channel(int sock,
2432 int *session_objd, /* session descriptor (output) */
2433 int *channel_objd, /* channel descriptor (output) */
2434 size_t *nr_fields,
2435 struct ustctl_field **fields)
2436{
2437 ssize_t len;
2438 struct ustcomm_notify_channel_msg msg;
2439 size_t fields_len;
2440 struct ustctl_field *a_fields;
2441
2442 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2443 if (len > 0 && len != sizeof(msg))
2444 return -EIO;
2445 if (len == 0)
2446 return -EPIPE;
2447 if (len < 0)
2448 return len;
2449
2450 *session_objd = msg.session_objd;
2451 *channel_objd = msg.channel_objd;
2452 fields_len = msg.ctx_fields_len;
2453
2454 if (fields_len % sizeof(*a_fields) != 0) {
2455 return -EINVAL;
2456 }
2457
2458 /* recv fields */
2459 if (fields_len) {
2460 a_fields = zmalloc(fields_len);
2461 if (!a_fields) {
2462 len = -ENOMEM;
2463 goto alloc_error;
2464 }
2465 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2466 if (len > 0 && len != fields_len) {
2467 len = -EIO;
2468 goto fields_error;
2469 }
2470 if (len == 0) {
2471 len = -EPIPE;
2472 goto fields_error;
2473 }
2474 if (len < 0) {
2475 goto fields_error;
2476 }
2477 *fields = a_fields;
2478 } else {
2479 *fields = NULL;
2480 }
2481 *nr_fields = fields_len / sizeof(*a_fields);
2482 return 0;
2483
2484fields_error:
2485 free(a_fields);
2486alloc_error:
2487 return len;
2488}
2489
2490/*
2491 * Returns 0 on success, negative error value on error.
2492 */
2493int ustctl_reply_register_channel(int sock,
2494 uint32_t chan_id,
2495 enum ustctl_channel_header header_type,
2496 int ret_code)
2497{
2498 ssize_t len;
2499 struct {
2500 struct ustcomm_notify_hdr header;
2501 struct ustcomm_notify_channel_reply r;
2502 } reply;
2503
2504 memset(&reply, 0, sizeof(reply));
2505 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2506 reply.r.ret_code = ret_code;
2507 reply.r.chan_id = chan_id;
2508 switch (header_type) {
2509 case USTCTL_CHANNEL_HEADER_COMPACT:
2510 reply.r.header_type = 1;
2511 break;
2512 case USTCTL_CHANNEL_HEADER_LARGE:
2513 reply.r.header_type = 2;
2514 break;
2515 default:
2516 reply.r.header_type = 0;
2517 break;
2518 }
2519 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2520 if (len > 0 && len != sizeof(reply))
2521 return -EIO;
2522 if (len < 0)
2523 return len;
2524 return 0;
2525}
2526
f53329f3
JD
2527/* Regenerate the statedump. */
2528int ustctl_regenerate_statedump(int sock, int handle)
2529{
2530 struct ustcomm_ust_msg lum;
2531 struct ustcomm_ust_reply lur;
2532 int ret;
2533
2534 memset(&lum, 0, sizeof(lum));
2535 lum.handle = handle;
fd17d7ce 2536 lum.cmd = LTTNG_UST_ABI_SESSION_STATEDUMP;
f53329f3
JD
2537 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2538 if (ret)
2539 return ret;
2540 DBG("Regenerated statedump for handle %u", handle);
2541 return 0;
2542}
2543
ebabbf58
MD
2544/* counter operations */
2545
2546int ustctl_get_nr_cpu_per_counter(void)
2547{
2548 return lttng_counter_num_possible_cpus();
2549}
2550
2551struct ustctl_daemon_counter *
2552 ustctl_create_counter(size_t nr_dimensions,
2553 const struct ustctl_counter_dimension *dimensions,
2554 int64_t global_sum_step,
2555 int global_counter_fd,
2556 int nr_counter_cpu_fds,
2557 const int *counter_cpu_fds,
2558 enum ustctl_counter_bitness bitness,
2559 enum ustctl_counter_arithmetic arithmetic,
81bc4972
MD
2560 uint32_t alloc_flags,
2561 bool coalesce_hits)
ebabbf58
MD
2562{
2563 const char *transport_name;
2564 struct ustctl_daemon_counter *counter;
2565 struct lttng_counter_transport *transport;
2566 struct lttng_counter_dimension ust_dim[LTTNG_COUNTER_DIMENSION_MAX];
2567 size_t i;
2568
2569 if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX)
2570 return NULL;
2571 /* Currently, only per-cpu allocation is supported. */
2572 switch (alloc_flags) {
2573 case USTCTL_COUNTER_ALLOC_PER_CPU:
2574 break;
2575
2576 case USTCTL_COUNTER_ALLOC_PER_CPU | USTCTL_COUNTER_ALLOC_GLOBAL:
2577 case USTCTL_COUNTER_ALLOC_GLOBAL:
2578 default:
2579 return NULL;
2580 }
2581 switch (bitness) {
2582 case USTCTL_COUNTER_BITNESS_32:
2583 switch (arithmetic) {
2584 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2585 transport_name = "counter-per-cpu-32-modular";
2586 break;
2587 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2588 transport_name = "counter-per-cpu-32-saturation";
2589 break;
2590 default:
2591 return NULL;
2592 }
2593 break;
2594 case USTCTL_COUNTER_BITNESS_64:
2595 switch (arithmetic) {
2596 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2597 transport_name = "counter-per-cpu-64-modular";
2598 break;
2599 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2600 transport_name = "counter-per-cpu-64-saturation";
2601 break;
2602 default:
2603 return NULL;
2604 }
2605 break;
2606 default:
2607 return NULL;
2608 }
2609
2610 transport = lttng_counter_transport_find(transport_name);
2611 if (!transport) {
2612 DBG("LTTng transport %s not found\n",
2613 transport_name);
2614 return NULL;
2615 }
2616
2617 counter = zmalloc(sizeof(*counter));
2618 if (!counter)
2619 return NULL;
2620 counter->attr = zmalloc(sizeof(*counter->attr));
2621 if (!counter->attr)
2622 goto free_counter;
2623 counter->attr->bitness = bitness;
2624 counter->attr->arithmetic = arithmetic;
2625 counter->attr->nr_dimensions = nr_dimensions;
2626 counter->attr->global_sum_step = global_sum_step;
81bc4972 2627 counter->attr->coalesce_hits = coalesce_hits;
ebabbf58
MD
2628 for (i = 0; i < nr_dimensions; i++)
2629 counter->attr->dimensions[i] = dimensions[i];
2630
2631 for (i = 0; i < nr_dimensions; i++) {
2632 ust_dim[i].size = dimensions[i].size;
2633 ust_dim[i].underflow_index = dimensions[i].underflow_index;
2634 ust_dim[i].overflow_index = dimensions[i].overflow_index;
2635 ust_dim[i].has_underflow = dimensions[i].has_underflow;
2636 ust_dim[i].has_overflow = dimensions[i].has_overflow;
2637 }
2638 counter->counter = transport->ops.counter_create(nr_dimensions,
2639 ust_dim, global_sum_step, global_counter_fd,
2640 nr_counter_cpu_fds, counter_cpu_fds, true);
2641 if (!counter->counter)
2642 goto free_attr;
2643 counter->ops = &transport->ops;
2644 return counter;
2645
2646free_attr:
2647 free(counter->attr);
2648free_counter:
2649 free(counter);
2650 return NULL;
2651}
2652
2653int ustctl_create_counter_data(struct ustctl_daemon_counter *counter,
fd17d7ce 2654 struct lttng_ust_abi_object_data **_counter_data)
ebabbf58 2655{
fd17d7ce
MD
2656 struct lttng_ust_abi_object_data *counter_data;
2657 struct lttng_ust_abi_counter_conf counter_conf = {0};
ebabbf58
MD
2658 size_t i;
2659 int ret;
2660
2661 switch (counter->attr->arithmetic) {
2662 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
fd17d7ce 2663 counter_conf.arithmetic = LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR;
ebabbf58
MD
2664 break;
2665 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
fd17d7ce 2666 counter_conf.arithmetic = LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION;
ebabbf58
MD
2667 break;
2668 default:
2669 return -EINVAL;
2670 }
2671 switch (counter->attr->bitness) {
2672 case USTCTL_COUNTER_BITNESS_32:
fd17d7ce 2673 counter_conf.bitness = LTTNG_UST_ABI_COUNTER_BITNESS_32;
ebabbf58
MD
2674 break;
2675 case USTCTL_COUNTER_BITNESS_64:
fd17d7ce 2676 counter_conf.bitness = LTTNG_UST_ABI_COUNTER_BITNESS_64;
ebabbf58
MD
2677 break;
2678 default:
2679 return -EINVAL;
2680 }
2681 counter_conf.number_dimensions = counter->attr->nr_dimensions;
2682 counter_conf.global_sum_step = counter->attr->global_sum_step;
81bc4972 2683 counter_conf.coalesce_hits = counter->attr->coalesce_hits;
ebabbf58
MD
2684 for (i = 0; i < counter->attr->nr_dimensions; i++) {
2685 counter_conf.dimensions[i].size = counter->attr->dimensions[i].size;
2686 counter_conf.dimensions[i].underflow_index = counter->attr->dimensions[i].underflow_index;
2687 counter_conf.dimensions[i].overflow_index = counter->attr->dimensions[i].overflow_index;
2688 counter_conf.dimensions[i].has_underflow = counter->attr->dimensions[i].has_underflow;
2689 counter_conf.dimensions[i].has_overflow = counter->attr->dimensions[i].has_overflow;
2690 }
2691
2692 counter_data = zmalloc(sizeof(*counter_data));
2693 if (!counter_data) {
2694 ret = -ENOMEM;
2695 goto error_alloc;
2696 }
fd17d7ce 2697 counter_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER;
ebabbf58
MD
2698 counter_data->handle = -1;
2699
2700 counter_data->size = sizeof(counter_conf);
2701 counter_data->u.counter.data = zmalloc(sizeof(counter_conf));
2702 if (!counter_data->u.counter.data) {
2703 ret = -ENOMEM;
2704 goto error_alloc_data;
2705 }
2706
2707 memcpy(counter_data->u.counter.data, &counter_conf, sizeof(counter_conf));
2708 *_counter_data = counter_data;
2709
2710 return 0;
2711
2712error_alloc_data:
2713 free(counter_data);
2714error_alloc:
2715 return ret;
2716}
2717
2718int ustctl_create_counter_global_data(struct ustctl_daemon_counter *counter,
fd17d7ce 2719 struct lttng_ust_abi_object_data **_counter_global_data)
ebabbf58 2720{
fd17d7ce 2721 struct lttng_ust_abi_object_data *counter_global_data;
ebabbf58
MD
2722 int ret, fd;
2723 size_t len;
2724
2725 if (lttng_counter_get_global_shm(counter->counter, &fd, &len))
2726 return -EINVAL;
2727 counter_global_data = zmalloc(sizeof(*counter_global_data));
2728 if (!counter_global_data) {
2729 ret = -ENOMEM;
2730 goto error_alloc;
2731 }
fd17d7ce 2732 counter_global_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL;
ebabbf58
MD
2733 counter_global_data->handle = -1;
2734 counter_global_data->size = len;
2735 counter_global_data->u.counter_global.shm_fd = fd;
2736 *_counter_global_data = counter_global_data;
2737 return 0;
2738
2739error_alloc:
2740 return ret;
2741}
2742
2743int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter *counter, int cpu,
fd17d7ce 2744 struct lttng_ust_abi_object_data **_counter_cpu_data)
ebabbf58 2745{
fd17d7ce 2746 struct lttng_ust_abi_object_data *counter_cpu_data;
ebabbf58
MD
2747 int ret, fd;
2748 size_t len;
2749
2750 if (lttng_counter_get_cpu_shm(counter->counter, cpu, &fd, &len))
2751 return -EINVAL;
2752 counter_cpu_data = zmalloc(sizeof(*counter_cpu_data));
2753 if (!counter_cpu_data) {
2754 ret = -ENOMEM;
2755 goto error_alloc;
2756 }
fd17d7ce 2757 counter_cpu_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU;
ebabbf58
MD
2758 counter_cpu_data->handle = -1;
2759 counter_cpu_data->size = len;
2760 counter_cpu_data->u.counter_cpu.shm_fd = fd;
2761 counter_cpu_data->u.counter_cpu.cpu_nr = cpu;
2762 *_counter_cpu_data = counter_cpu_data;
2763 return 0;
2764
2765error_alloc:
2766 return ret;
2767}
2768
2769void ustctl_destroy_counter(struct ustctl_daemon_counter *counter)
2770{
2771 counter->ops->counter_destroy(counter->counter);
2772 free(counter->attr);
2773 free(counter);
2774}
2775
2776int ustctl_send_counter_data_to_ust(int sock, int parent_handle,
fd17d7ce 2777 struct lttng_ust_abi_object_data *counter_data)
ebabbf58
MD
2778{
2779 struct ustcomm_ust_msg lum;
2780 struct ustcomm_ust_reply lur;
2781 int ret;
2782 size_t size;
2783 ssize_t len;
2784
2785 if (!counter_data)
2786 return -EINVAL;
2787
2788 size = counter_data->size;
2789 memset(&lum, 0, sizeof(lum));
2790 lum.handle = parent_handle;
fd17d7ce 2791 lum.cmd = LTTNG_UST_ABI_COUNTER;
ebabbf58
MD
2792 lum.u.counter.len = size;
2793 ret = ustcomm_send_app_msg(sock, &lum);
2794 if (ret)
2795 return ret;
2796
2797 /* Send counter data */
2798 len = ustcomm_send_unix_sock(sock, counter_data->u.counter.data, size);
2799 if (len != size) {
2800 if (len < 0)
2801 return len;
2802 else
2803 return -EIO;
2804 }
2805
2806 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2807 if (!ret) {
2808 counter_data->handle = lur.ret_val;
2809 }
2810 return ret;
2811}
2812
2813int ustctl_send_counter_global_data_to_ust(int sock,
fd17d7ce
MD
2814 struct lttng_ust_abi_object_data *counter_data,
2815 struct lttng_ust_abi_object_data *counter_global_data)
ebabbf58
MD
2816{
2817 struct ustcomm_ust_msg lum;
2818 struct ustcomm_ust_reply lur;
2819 int ret, shm_fd[1];
2820 size_t size;
2821 ssize_t len;
2822
2823 if (!counter_data || !counter_global_data)
2824 return -EINVAL;
2825
2826 size = counter_global_data->size;
2827 memset(&lum, 0, sizeof(lum));
2828 lum.handle = counter_data->handle; /* parent handle */
fd17d7ce 2829 lum.cmd = LTTNG_UST_ABI_COUNTER_GLOBAL;
ebabbf58
MD
2830 lum.u.counter_global.len = size;
2831 ret = ustcomm_send_app_msg(sock, &lum);
2832 if (ret)
2833 return ret;
2834
2835 shm_fd[0] = counter_global_data->u.counter_global.shm_fd;
2836 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2837 if (len <= 0) {
2838 if (len < 0)
2839 return len;
2840 else
2841 return -EIO;
2842 }
2843
2844 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2845 if (!ret) {
2846 counter_global_data->handle = lur.ret_val;
2847 }
2848 return ret;
2849}
2850
2851int ustctl_send_counter_cpu_data_to_ust(int sock,
fd17d7ce
MD
2852 struct lttng_ust_abi_object_data *counter_data,
2853 struct lttng_ust_abi_object_data *counter_cpu_data)
ebabbf58
MD
2854{
2855 struct ustcomm_ust_msg lum;
2856 struct ustcomm_ust_reply lur;
2857 int ret, shm_fd[1];
2858 size_t size;
2859 ssize_t len;
2860
2861 if (!counter_data || !counter_cpu_data)
2862 return -EINVAL;
2863
2864 size = counter_cpu_data->size;
2865 memset(&lum, 0, sizeof(lum));
2866 lum.handle = counter_data->handle; /* parent handle */
fd17d7ce 2867 lum.cmd = LTTNG_UST_ABI_COUNTER_CPU;
ebabbf58
MD
2868 lum.u.counter_cpu.len = size;
2869 lum.u.counter_cpu.cpu_nr = counter_cpu_data->u.counter_cpu.cpu_nr;
2870 ret = ustcomm_send_app_msg(sock, &lum);
2871 if (ret)
2872 return ret;
2873
2874 shm_fd[0] = counter_cpu_data->u.counter_global.shm_fd;
2875 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2876 if (len <= 0) {
2877 if (len < 0)
2878 return len;
2879 else
2880 return -EIO;
2881 }
2882
2883 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2884 if (!ret) {
2885 counter_cpu_data->handle = lur.ret_val;
2886 }
2887 return ret;
2888}
2889
2890int ustctl_counter_read(struct ustctl_daemon_counter *counter,
2891 const size_t *dimension_indexes,
2892 int cpu, int64_t *value,
2893 bool *overflow, bool *underflow)
2894{
2895 return counter->ops->counter_read(counter->counter, dimension_indexes, cpu,
2896 value, overflow, underflow);
2897}
2898
2899int ustctl_counter_aggregate(struct ustctl_daemon_counter *counter,
2900 const size_t *dimension_indexes,
2901 int64_t *value,
2902 bool *overflow, bool *underflow)
2903{
2904 return counter->ops->counter_aggregate(counter->counter, dimension_indexes,
2905 value, overflow, underflow);
2906}
2907
2908int ustctl_counter_clear(struct ustctl_daemon_counter *counter,
2909 const size_t *dimension_indexes)
2910{
2911 return counter->ops->counter_clear(counter->counter, dimension_indexes);
2912}
2913
74d81a6c
MD
2914static __attribute__((constructor))
2915void ustctl_init(void)
2916{
2b6f4374
MJ
2917 ust_err_init();
2918 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
f9364363 2919 lttng_ust_clock_init();
74d81a6c
MD
2920 lttng_ring_buffer_metadata_client_init();
2921 lttng_ring_buffer_client_overwrite_init();
34a91bdb 2922 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 2923 lttng_ring_buffer_client_discard_init();
34a91bdb 2924 lttng_ring_buffer_client_discard_rt_init();
ebabbf58
MD
2925 lttng_counter_client_percpu_32_modular_init();
2926 lttng_counter_client_percpu_64_modular_init();
03d2d293 2927 lib_ringbuffer_signal_init();
74d81a6c
MD
2928}
2929
2930static __attribute__((destructor))
2931void ustctl_exit(void)
2932{
34a91bdb 2933 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 2934 lttng_ring_buffer_client_discard_exit();
34a91bdb 2935 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
2936 lttng_ring_buffer_client_overwrite_exit();
2937 lttng_ring_buffer_metadata_client_exit();
ebabbf58
MD
2938 lttng_counter_client_percpu_32_modular_exit();
2939 lttng_counter_client_percpu_64_modular_exit();
57773204 2940}
This page took 0.171895 seconds and 4 git commands to generate.