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