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