SoW-2020-0002: Trace Hit Counters
[deliverable/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>
c1fca457 18#include <lttng/ust-events.h>
3208818b 19#include <lttng/ust-endian.h>
44c72f10 20#include <usterr-signal-safe.h>
b728d87e 21#include <ust-comm.h>
74d81a6c 22#include <helper.h>
57773204
MD
23
24#include "../libringbuffer/backend.h"
25#include "../libringbuffer/frontend.h"
c9023c93 26#include "../liblttng-ust/wait.h"
b2f3252a 27#include "../liblttng-ust/lttng-rb-clients.h"
f9364363 28#include "../liblttng-ust/clock.h"
92ce256d 29#include "../liblttng-ust/getenv.h"
23d128de 30#include "../liblttng-ust/lttng-tracer-core.h"
c9023c93 31
ebabbf58
MD
32#include "../libcounter/shm.h"
33#include "../libcounter/smp.h"
34#include "../libcounter/counter.h"
35
c9023c93
MD
36/*
37 * Number of milliseconds to retry before failing metadata writes on
38 * buffer full condition. (10 seconds)
39 */
40#define LTTNG_METADATA_TIMEOUT_MSEC 10000
57773204 41
74d81a6c
MD
42/*
43 * Channel representation within consumer.
44 */
45struct ustctl_consumer_channel {
46 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 47
74d81a6c
MD
48 /* initial attributes */
49 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
50 int wait_fd; /* monitor close() */
51 int wakeup_fd; /* monitor close() */
74d81a6c
MD
52};
53
54/*
55 * Stream representation within consumer.
56 */
57struct ustctl_consumer_stream {
58 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
59 struct lttng_ust_lib_ring_buffer *buf;
60 struct ustctl_consumer_channel *chan;
61 int shm_fd, wait_fd, wakeup_fd;
62 int cpu;
63 uint64_t memory_map_size;
64};
65
ebabbf58
MD
66#define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
67struct ustctl_counter_attr {
68 enum ustctl_counter_arithmetic arithmetic;
69 enum ustctl_counter_bitness bitness;
70 uint32_t nr_dimensions;
71 int64_t global_sum_step;
72 struct ustctl_counter_dimension dimensions[USTCTL_COUNTER_ATTR_DIMENSION_MAX];
bb84a1ec 73 bool coalesce_hits;
ebabbf58
MD
74};
75
76/*
77 * Counter representation within daemon.
78 */
79struct ustctl_daemon_counter {
80 struct lib_counter *counter;
81 const struct lttng_counter_ops *ops;
82 struct ustctl_counter_attr *attr; /* initial attributes */
83};
84
74d81a6c 85extern void lttng_ring_buffer_client_overwrite_init(void);
08a3170c 86extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74d81a6c 87extern void lttng_ring_buffer_client_discard_init(void);
08a3170c 88extern void lttng_ring_buffer_client_discard_rt_init(void);
74d81a6c
MD
89extern void lttng_ring_buffer_metadata_client_init(void);
90extern void lttng_ring_buffer_client_overwrite_exit(void);
08a3170c 91extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74d81a6c 92extern void lttng_ring_buffer_client_discard_exit(void);
08a3170c 93extern void lttng_ring_buffer_client_discard_rt_exit(void);
74d81a6c 94extern void lttng_ring_buffer_metadata_client_exit(void);
31677417 95LTTNG_HIDDEN
ebabbf58 96extern void lttng_counter_client_percpu_32_modular_init(void);
31677417 97LTTNG_HIDDEN
ebabbf58 98extern void lttng_counter_client_percpu_32_modular_exit(void);
31677417 99LTTNG_HIDDEN
ebabbf58 100extern void lttng_counter_client_percpu_64_modular_init(void);
31677417 101LTTNG_HIDDEN
ebabbf58 102extern void lttng_counter_client_percpu_64_modular_exit(void);
74d81a6c 103
2be0e72c
MD
104int ustctl_release_handle(int sock, int handle)
105{
106 struct ustcomm_ust_msg lum;
107 struct ustcomm_ust_reply lur;
2be0e72c 108
74d81a6c
MD
109 if (sock < 0 || handle < 0)
110 return 0;
111 memset(&lum, 0, sizeof(lum));
112 lum.handle = handle;
113 lum.cmd = LTTNG_UST_RELEASE;
114 return ustcomm_send_app_cmd(sock, &lum, &lur);
2be0e72c 115}
74d81a6c 116
12388166
MD
117/*
118 * If sock is negative, it means we don't have to notify the other side
119 * (e.g. application has already vanished).
120 */
d26228ae 121int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 122{
57773204
MD
123 int ret;
124
9bfc503d
MD
125 if (!data)
126 return -EINVAL;
127
74d81a6c
MD
128 switch (data->type) {
129 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
130 if (data->u.channel.wakeup_fd >= 0) {
131 ret = close(data->u.channel.wakeup_fd);
132 if (ret < 0) {
133 ret = -errno;
134 return ret;
135 }
dd6c697c 136 data->u.channel.wakeup_fd = -1;
ff0f5728 137 }
74d81a6c 138 free(data->u.channel.data);
dd6c697c 139 data->u.channel.data = NULL;
74d81a6c
MD
140 break;
141 case LTTNG_UST_OBJECT_TYPE_STREAM:
142 if (data->u.stream.shm_fd >= 0) {
143 ret = close(data->u.stream.shm_fd);
144 if (ret < 0) {
145 ret = -errno;
146 return ret;
147 }
dd6c697c 148 data->u.stream.shm_fd = -1;
d26228ae 149 }
74d81a6c
MD
150 if (data->u.stream.wakeup_fd >= 0) {
151 ret = close(data->u.stream.wakeup_fd);
152 if (ret < 0) {
153 ret = -errno;
154 return ret;
155 }
dd6c697c 156 data->u.stream.wakeup_fd = -1;
d26228ae 157 }
74d81a6c 158 break;
32ce8569
MD
159 case LTTNG_UST_OBJECT_TYPE_EVENT:
160 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
d8d2416d
FD
161 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP:
162 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER:
bb84a1ec 163 case LTTNG_UST_OBJECT_TYPE_COUNTER_EVENT:
32ce8569 164 break;
ebabbf58
MD
165 case LTTNG_UST_OBJECT_TYPE_COUNTER:
166 free(data->u.counter.data);
167 data->u.counter.data = NULL;
168 break;
169 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL:
170 if (data->u.counter_global.shm_fd >= 0) {
171 ret = close(data->u.counter_global.shm_fd);
172 if (ret < 0) {
173 ret = -errno;
174 return ret;
175 }
176 data->u.counter_global.shm_fd = -1;
177 }
178 break;
179 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU:
180 if (data->u.counter_cpu.shm_fd >= 0) {
181 ret = close(data->u.counter_cpu.shm_fd);
182 if (ret < 0) {
183 ret = -errno;
184 return ret;
185 }
186 data->u.counter_cpu.shm_fd = -1;
187 }
188 break;
74d81a6c
MD
189 default:
190 assert(0);
d26228ae 191 }
2be0e72c 192 return ustctl_release_handle(sock, data->handle);
57773204
MD
193}
194
1c5e467e
MD
195/*
196 * Send registration done packet to the application.
197 */
198int ustctl_register_done(int sock)
199{
200 struct ustcomm_ust_msg lum;
201 struct ustcomm_ust_reply lur;
202 int ret;
203
204 DBG("Sending register done command to %d", sock);
205 memset(&lum, 0, sizeof(lum));
206 lum.handle = LTTNG_UST_ROOT_HANDLE;
207 lum.cmd = LTTNG_UST_REGISTER_DONE;
208 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
209 if (ret)
210 return ret;
1c5e467e 211 return 0;
1c5e467e
MD
212}
213
57773204
MD
214/*
215 * returns session handle.
216 */
217int ustctl_create_session(int sock)
218{
219 struct ustcomm_ust_msg lum;
220 struct ustcomm_ust_reply lur;
221 int ret, session_handle;
222
223 /* Create session */
224 memset(&lum, 0, sizeof(lum));
225 lum.handle = LTTNG_UST_ROOT_HANDLE;
226 lum.cmd = LTTNG_UST_SESSION;
227 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
228 if (ret)
229 return ret;
230 session_handle = lur.ret_val;
231 DBG("received session handle %u", session_handle);
232 return session_handle;
233}
234
57773204 235int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
236 struct lttng_ust_object_data *channel_data,
237 struct lttng_ust_object_data **_event_data)
57773204
MD
238{
239 struct ustcomm_ust_msg lum;
240 struct ustcomm_ust_reply lur;
61f02aea 241 struct lttng_ust_object_data *event_data;
57773204
MD
242 int ret;
243
9bfc503d
MD
244 if (!channel_data || !_event_data)
245 return -EINVAL;
246
74d81a6c 247 event_data = zmalloc(sizeof(*event_data));
57773204
MD
248 if (!event_data)
249 return -ENOMEM;
32ce8569 250 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
57773204
MD
251 memset(&lum, 0, sizeof(lum));
252 lum.handle = channel_data->handle;
253 lum.cmd = LTTNG_UST_EVENT;
254 strncpy(lum.u.event.name, ev->name,
255 LTTNG_UST_SYM_NAME_LEN);
256 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
257 lum.u.event.loglevel_type = ev->loglevel_type;
258 lum.u.event.loglevel = ev->loglevel;
57773204
MD
259 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
260 if (ret) {
261 free(event_data);
262 return ret;
263 }
264 event_data->handle = lur.ret_val;
265 DBG("received event handle %u", event_data->handle);
266 *_event_data = event_data;
267 return 0;
268}
269
53f0df51 270int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx,
61f02aea
MD
271 struct lttng_ust_object_data *obj_data,
272 struct lttng_ust_object_data **_context_data)
57773204
MD
273{
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
53f0df51
JG
276 struct lttng_ust_object_data *context_data = NULL;
277 char *buf = NULL;
278 size_t len;
57773204
MD
279 int ret;
280
53f0df51
JG
281 if (!obj_data || !_context_data) {
282 ret = -EINVAL;
283 goto end;
284 }
9bfc503d 285
74d81a6c 286 context_data = zmalloc(sizeof(*context_data));
53f0df51
JG
287 if (!context_data) {
288 ret = -ENOMEM;
289 goto end;
290 }
32ce8569 291 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
57773204 292 memset(&lum, 0, sizeof(lum));
3039d8ed 293 lum.handle = obj_data->handle;
57773204 294 lum.cmd = LTTNG_UST_CONTEXT;
53f0df51
JG
295
296 lum.u.context.ctx = ctx->ctx;
297 switch (ctx->ctx) {
298 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
299 lum.u.context.u.perf_counter = ctx->u.perf_counter;
300 break;
301 case LTTNG_UST_CONTEXT_APP_CONTEXT:
302 {
303 size_t provider_name_len = strlen(
304 ctx->u.app_ctx.provider_name) + 1;
305 size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1;
306
307 lum.u.context.u.app_ctx.provider_name_len = provider_name_len;
308 lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len;
309
310 len = provider_name_len + ctx_name_len;
311 buf = zmalloc(len);
312 if (!buf) {
313 ret = -ENOMEM;
314 goto end;
315 }
316 memcpy(buf, ctx->u.app_ctx.provider_name,
317 provider_name_len);
318 memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name,
319 ctx_name_len);
320 break;
321 }
322 default:
323 break;
324 }
325 ret = ustcomm_send_app_msg(sock, &lum);
326 if (ret)
327 goto end;
328 if (buf) {
329 /* send var len ctx_name */
330 ret = ustcomm_send_unix_sock(sock, buf, len);
331 if (ret < 0) {
332 goto end;
333 }
334 if (ret != len) {
335 ret = -EINVAL;
336 goto end;
337 }
338 }
339 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
340 if (ret < 0) {
341 goto end;
57773204 342 }
32ce8569
MD
343 context_data->handle = -1;
344 DBG("Context created successfully");
57773204 345 *_context_data = context_data;
53f0df51
JG
346 context_data = NULL;
347end:
348 free(context_data);
349 free(buf);
57773204
MD
350 return ret;
351}
352
cd54f6d9
MD
353int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
354 struct lttng_ust_object_data *obj_data)
355{
356 struct ustcomm_ust_msg lum;
357 struct ustcomm_ust_reply lur;
358 int ret;
359
360 if (!obj_data)
361 return -EINVAL;
362
363 memset(&lum, 0, sizeof(lum));
364 lum.handle = obj_data->handle;
365 lum.cmd = LTTNG_UST_FILTER;
366 lum.u.filter.data_size = bytecode->len;
367 lum.u.filter.reloc_offset = bytecode->reloc_offset;
e695af51 368 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
369
370 ret = ustcomm_send_app_msg(sock, &lum);
d37ecb3f
FD
371 if (ret)
372 return ret;
373 /* send var len bytecode */
374 ret = ustcomm_send_unix_sock(sock, bytecode->data,
375 bytecode->len);
376 if (ret < 0) {
377 return ret;
378 }
379 if (ret != bytecode->len)
380 return -EINVAL;
381 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
382}
383
384int ustctl_set_capture(int sock, struct lttng_ust_capture_bytecode *bytecode,
385 struct lttng_ust_object_data *obj_data)
386{
387 struct ustcomm_ust_msg lum;
388 struct ustcomm_ust_reply lur;
389 int ret;
390
391 if (!obj_data)
392 return -EINVAL;
393
394 memset(&lum, 0, sizeof(lum));
395 lum.handle = obj_data->handle;
396 lum.cmd = LTTNG_UST_CAPTURE;
397 lum.u.capture.data_size = bytecode->len;
398 lum.u.capture.reloc_offset = bytecode->reloc_offset;
399 lum.u.capture.seqnum = bytecode->seqnum;
400
401 ret = ustcomm_send_app_msg(sock, &lum);
cd54f6d9
MD
402 if (ret)
403 return ret;
cd54f6d9
MD
404 /* send var len bytecode */
405 ret = ustcomm_send_unix_sock(sock, bytecode->data,
406 bytecode->len);
407 if (ret < 0) {
408 return ret;
409 }
7bc53e94
MD
410 if (ret != bytecode->len)
411 return -EINVAL;
412 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
413}
414
da57c034
JI
415int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion,
416 struct lttng_ust_object_data *obj_data)
417{
418 struct ustcomm_ust_msg lum;
419 struct ustcomm_ust_reply lur;
420 int ret;
421
422 if (!obj_data) {
423 return -EINVAL;
424 }
425
426 memset(&lum, 0, sizeof(lum));
427 lum.handle = obj_data->handle;
428 lum.cmd = LTTNG_UST_EXCLUSION;
429 lum.u.exclusion.count = exclusion->count;
430
431 ret = ustcomm_send_app_msg(sock, &lum);
432 if (ret) {
433 return ret;
434 }
435
1628366f 436 /* send var len exclusion names */
da57c034
JI
437 ret = ustcomm_send_unix_sock(sock,
438 exclusion->names,
439 exclusion->count * LTTNG_UST_SYM_NAME_LEN);
440 if (ret < 0) {
441 return ret;
442 }
443 if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) {
444 return -EINVAL;
445 }
446 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
447}
448
57773204 449/* Enable event, channel and session ioctl */
61f02aea 450int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
451{
452 struct ustcomm_ust_msg lum;
453 struct ustcomm_ust_reply lur;
454 int ret;
455
9bfc503d
MD
456 if (!object)
457 return -EINVAL;
458
57773204
MD
459 memset(&lum, 0, sizeof(lum));
460 lum.handle = object->handle;
461 lum.cmd = LTTNG_UST_ENABLE;
462 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
463 if (ret)
464 return ret;
465 DBG("enabled handle %u", object->handle);
466 return 0;
467}
468
469/* Disable event, channel and session ioctl */
61f02aea 470int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
471{
472 struct ustcomm_ust_msg lum;
473 struct ustcomm_ust_reply lur;
474 int ret;
475
9bfc503d
MD
476 if (!object)
477 return -EINVAL;
478
57773204
MD
479 memset(&lum, 0, sizeof(lum));
480 lum.handle = object->handle;
481 lum.cmd = LTTNG_UST_DISABLE;
482 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
483 if (ret)
484 return ret;
485 DBG("disable handle %u", object->handle);
486 return 0;
487}
488
4a6ca058 489int ustctl_start_session(int sock, int handle)
57773204 490{
61f02aea 491 struct lttng_ust_object_data obj;
4a6ca058
MD
492
493 obj.handle = handle;
494 return ustctl_enable(sock, &obj);
57773204
MD
495}
496
4a6ca058 497int ustctl_stop_session(int sock, int handle)
57773204 498{
61f02aea 499 struct lttng_ust_object_data obj;
4a6ca058
MD
500
501 obj.handle = handle;
502 return ustctl_disable(sock, &obj);
57773204
MD
503}
504
d8d2416d
FD
505int ustctl_create_event_notifier_group(int sock, int pipe_fd,
506 struct lttng_ust_object_data **_event_notifier_group_data)
507{
508 struct lttng_ust_object_data *event_notifier_group_data;
509 struct ustcomm_ust_msg lum;
510 struct ustcomm_ust_reply lur;
511 ssize_t len;
512 int ret;
513
514 if (!_event_notifier_group_data)
515 return -EINVAL;
516
517 event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data));
518 if (!event_notifier_group_data)
519 return -ENOMEM;
520
521 event_notifier_group_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP;
522
523 memset(&lum, 0, sizeof(lum));
524 lum.handle = LTTNG_UST_ROOT_HANDLE;
525 lum.cmd = LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE;
526
527 ret = ustcomm_send_app_msg(sock, &lum);
528 if (ret)
529 goto error;
530
531 /* Send event_notifier notification pipe. */
532 len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1);
533 if (len <= 0) {
534 ret = len;
535 goto error;
536 }
537
538 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
539 if (ret)
540 goto error;
541
542 event_notifier_group_data->handle = lur.ret_val;
543 DBG("received event_notifier group handle %d", event_notifier_group_data->handle);
544
545 *_event_notifier_group_data = event_notifier_group_data;
546
547 ret = 0;
548 goto end;
549error:
550 free(event_notifier_group_data);
551
552end:
553 return ret;
554}
555
556int ustctl_create_event_notifier(int sock, struct lttng_ust_event_notifier *event_notifier,
557 struct lttng_ust_object_data *event_notifier_group,
558 struct lttng_ust_object_data **_event_notifier_data)
559{
560 struct ustcomm_ust_msg lum;
561 struct ustcomm_ust_reply lur;
562 struct lttng_ust_object_data *event_notifier_data;
8406222c 563 ssize_t len;
d8d2416d
FD
564 int ret;
565
566 if (!event_notifier_group || !_event_notifier_data)
567 return -EINVAL;
568
569 event_notifier_data = zmalloc(sizeof(*event_notifier_data));
570 if (!event_notifier_data)
571 return -ENOMEM;
572
573 event_notifier_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER;
574
575 memset(&lum, 0, sizeof(lum));
576 lum.handle = event_notifier_group->handle;
577 lum.cmd = LTTNG_UST_EVENT_NOTIFIER_CREATE;
8406222c 578 lum.u.event_notifier.len = sizeof(*event_notifier);
d8d2416d 579
41844673 580 ret = ustcomm_send_app_msg(sock, &lum);
d8d2416d
FD
581 if (ret) {
582 free(event_notifier_data);
583 return ret;
584 }
8406222c
MD
585 /* Send struct lttng_ust_event_notifier */
586 len = ustcomm_send_unix_sock(sock, event_notifier, sizeof(*event_notifier));
587 if (len != sizeof(*event_notifier)) {
bb84a1ec 588 free(event_notifier_data);
8406222c
MD
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
c62a3816 2003#ifdef LTTNG_UST_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,
bb84a1ec
MD
2221 char **model_emf_uri,
2222 uint64_t *user_token)
32ce8569
MD
2223{
2224 ssize_t len;
2225 struct ustcomm_notify_event_msg msg;
2226 size_t signature_len, fields_len, model_emf_uri_len;
2227 char *a_sign = NULL, *a_model_emf_uri = NULL;
2228 struct ustctl_field *a_fields = NULL;
2229
2230 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2231 if (len > 0 && len != sizeof(msg))
2232 return -EIO;
2233 if (len == 0)
2234 return -EPIPE;
2235 if (len < 0)
2236 return len;
2237
2238 *session_objd = msg.session_objd;
2239 *channel_objd = msg.channel_objd;
2240 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
2241 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2242 *loglevel = msg.loglevel;
2243 signature_len = msg.signature_len;
2244 fields_len = msg.fields_len;
bb84a1ec 2245 *user_token = msg.user_token;
32ce8569
MD
2246
2247 if (fields_len % sizeof(*a_fields) != 0) {
2248 return -EINVAL;
2249 }
2250
2251 model_emf_uri_len = msg.model_emf_uri_len;
2252
2253 /* recv signature. contains at least \0. */
2254 a_sign = zmalloc(signature_len);
2255 if (!a_sign)
2256 return -ENOMEM;
2257 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
2258 if (len > 0 && len != signature_len) {
2259 len = -EIO;
2260 goto signature_error;
2261 }
2262 if (len == 0) {
2263 len = -EPIPE;
2264 goto signature_error;
2265 }
2266 if (len < 0) {
2267 goto signature_error;
2268 }
2269 /* Enforce end of string */
111198c2 2270 a_sign[signature_len - 1] = '\0';
32ce8569
MD
2271
2272 /* recv fields */
2273 if (fields_len) {
2274 a_fields = zmalloc(fields_len);
2275 if (!a_fields) {
2276 len = -ENOMEM;
2277 goto signature_error;
2278 }
2279 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2280 if (len > 0 && len != fields_len) {
2281 len = -EIO;
2282 goto fields_error;
2283 }
2284 if (len == 0) {
2285 len = -EPIPE;
2286 goto fields_error;
2287 }
2288 if (len < 0) {
2289 goto fields_error;
2290 }
2291 }
2292
2293 if (model_emf_uri_len) {
2294 /* recv model_emf_uri_len */
2295 a_model_emf_uri = zmalloc(model_emf_uri_len);
2296 if (!a_model_emf_uri) {
2297 len = -ENOMEM;
2298 goto fields_error;
2299 }
2300 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
2301 model_emf_uri_len);
2302 if (len > 0 && len != model_emf_uri_len) {
2303 len = -EIO;
2304 goto model_error;
2305 }
2306 if (len == 0) {
2307 len = -EPIPE;
2308 goto model_error;
2309 }
2310 if (len < 0) {
2311 goto model_error;
2312 }
2313 /* Enforce end of string */
2314 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
2315 }
2316
2317 *signature = a_sign;
2318 *nr_fields = fields_len / sizeof(*a_fields);
2319 *fields = a_fields;
2320 *model_emf_uri = a_model_emf_uri;
2321
2322 return 0;
2323
2324model_error:
2325 free(a_model_emf_uri);
2326fields_error:
2327 free(a_fields);
2328signature_error:
2329 free(a_sign);
2330 return len;
2331}
2332
2333/*
2334 * Returns 0 on success, negative error value on error.
2335 */
2336int ustctl_reply_register_event(int sock,
bb84a1ec
MD
2337 uint32_t event_id,
2338 uint64_t counter_index,
32ce8569
MD
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;
bb84a1ec
MD
2350 reply.r.event_id = event_id;
2351 reply.r.counter_index = counter_index;
32ce8569
MD
2352 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2353 if (len > 0 && len != sizeof(reply))
2354 return -EIO;
2355 if (len < 0)
2356 return len;
2357 return 0;
2358}
2359
c785c634
MD
2360/*
2361 * Returns 0 on success, negative UST or system error value on error.
2362 */
2363int ustctl_recv_register_enum(int sock,
2364 int *session_objd,
2365 char *enum_name,
2366 struct ustctl_enum_entry **entries,
2367 size_t *nr_entries)
2368{
2369 ssize_t len;
2370 struct ustcomm_notify_enum_msg msg;
2371 size_t entries_len;
2372 struct ustctl_enum_entry *a_entries = NULL;
2373
2374 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2375 if (len > 0 && len != sizeof(msg))
2376 return -EIO;
2377 if (len == 0)
2378 return -EPIPE;
2379 if (len < 0)
2380 return len;
2381
2382 *session_objd = msg.session_objd;
2383 strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN);
2384 enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2385 entries_len = msg.entries_len;
2386
2387 if (entries_len % sizeof(*a_entries) != 0) {
2388 return -EINVAL;
2389 }
2390
2391 /* recv entries */
2392 if (entries_len) {
2393 a_entries = zmalloc(entries_len);
2394 if (!a_entries)
2395 return -ENOMEM;
2396 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2397 if (len > 0 && len != entries_len) {
2398 len = -EIO;
2399 goto entries_error;
2400 }
2401 if (len == 0) {
2402 len = -EPIPE;
2403 goto entries_error;
2404 }
2405 if (len < 0) {
2406 goto entries_error;
2407 }
2408 }
2409 *nr_entries = entries_len / sizeof(*a_entries);
2410 *entries = a_entries;
2411
2412 return 0;
2413
2414entries_error:
2415 free(a_entries);
2416 return len;
2417}
2418
2419/*
2420 * Returns 0 on success, negative error value on error.
2421 */
2422int ustctl_reply_register_enum(int sock,
2423 uint64_t id,
2424 int ret_code)
2425{
2426 ssize_t len;
2427 struct {
2428 struct ustcomm_notify_hdr header;
2429 struct ustcomm_notify_enum_reply r;
2430 } reply;
2431
2432 memset(&reply, 0, sizeof(reply));
2433 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2434 reply.r.ret_code = ret_code;
2435 reply.r.enum_id = id;
2436 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2437 if (len > 0 && len != sizeof(reply))
2438 return -EIO;
2439 if (len < 0)
2440 return len;
2441 return 0;
2442}
2443
32ce8569
MD
2444/*
2445 * Returns 0 on success, negative UST or system error value on error.
2446 */
2447int ustctl_recv_register_channel(int sock,
2448 int *session_objd, /* session descriptor (output) */
2449 int *channel_objd, /* channel descriptor (output) */
2450 size_t *nr_fields,
2451 struct ustctl_field **fields)
2452{
2453 ssize_t len;
2454 struct ustcomm_notify_channel_msg msg;
2455 size_t fields_len;
2456 struct ustctl_field *a_fields;
2457
2458 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2459 if (len > 0 && len != sizeof(msg))
2460 return -EIO;
2461 if (len == 0)
2462 return -EPIPE;
2463 if (len < 0)
2464 return len;
2465
2466 *session_objd = msg.session_objd;
2467 *channel_objd = msg.channel_objd;
2468 fields_len = msg.ctx_fields_len;
2469
2470 if (fields_len % sizeof(*a_fields) != 0) {
2471 return -EINVAL;
2472 }
2473
2474 /* recv fields */
2475 if (fields_len) {
2476 a_fields = zmalloc(fields_len);
2477 if (!a_fields) {
2478 len = -ENOMEM;
2479 goto alloc_error;
2480 }
2481 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2482 if (len > 0 && len != fields_len) {
2483 len = -EIO;
2484 goto fields_error;
2485 }
2486 if (len == 0) {
2487 len = -EPIPE;
2488 goto fields_error;
2489 }
2490 if (len < 0) {
2491 goto fields_error;
2492 }
2493 *fields = a_fields;
2494 } else {
2495 *fields = NULL;
2496 }
2497 *nr_fields = fields_len / sizeof(*a_fields);
2498 return 0;
2499
2500fields_error:
2501 free(a_fields);
2502alloc_error:
2503 return len;
2504}
2505
2506/*
2507 * Returns 0 on success, negative error value on error.
2508 */
2509int ustctl_reply_register_channel(int sock,
2510 uint32_t chan_id,
2511 enum ustctl_channel_header header_type,
2512 int ret_code)
2513{
2514 ssize_t len;
2515 struct {
2516 struct ustcomm_notify_hdr header;
2517 struct ustcomm_notify_channel_reply r;
2518 } reply;
2519
2520 memset(&reply, 0, sizeof(reply));
2521 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2522 reply.r.ret_code = ret_code;
2523 reply.r.chan_id = chan_id;
2524 switch (header_type) {
2525 case USTCTL_CHANNEL_HEADER_COMPACT:
2526 reply.r.header_type = 1;
2527 break;
2528 case USTCTL_CHANNEL_HEADER_LARGE:
2529 reply.r.header_type = 2;
2530 break;
2531 default:
2532 reply.r.header_type = 0;
2533 break;
2534 }
2535 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2536 if (len > 0 && len != sizeof(reply))
2537 return -EIO;
2538 if (len < 0)
2539 return len;
2540 return 0;
2541}
2542
f53329f3
JD
2543/* Regenerate the statedump. */
2544int ustctl_regenerate_statedump(int sock, int handle)
2545{
2546 struct ustcomm_ust_msg lum;
2547 struct ustcomm_ust_reply lur;
2548 int ret;
2549
2550 memset(&lum, 0, sizeof(lum));
2551 lum.handle = handle;
2552 lum.cmd = LTTNG_UST_SESSION_STATEDUMP;
2553 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2554 if (ret)
2555 return ret;
2556 DBG("Regenerated statedump for handle %u", handle);
2557 return 0;
2558}
2559
ebabbf58
MD
2560/* counter operations */
2561
2562int ustctl_get_nr_cpu_per_counter(void)
2563{
2564 return lttng_counter_num_possible_cpus();
2565}
2566
2567struct ustctl_daemon_counter *
2568 ustctl_create_counter(size_t nr_dimensions,
2569 const struct ustctl_counter_dimension *dimensions,
2570 int64_t global_sum_step,
2571 int global_counter_fd,
2572 int nr_counter_cpu_fds,
2573 const int *counter_cpu_fds,
2574 enum ustctl_counter_bitness bitness,
2575 enum ustctl_counter_arithmetic arithmetic,
bb84a1ec
MD
2576 uint32_t alloc_flags,
2577 bool coalesce_hits)
ebabbf58
MD
2578{
2579 const char *transport_name;
2580 struct ustctl_daemon_counter *counter;
2581 struct lttng_counter_transport *transport;
bb84a1ec
MD
2582 struct lttng_ust_counter_dimension ust_dim[LTTNG_COUNTER_DIMENSION_MAX];
2583 size_t counter_len[LTTNG_COUNTER_DIMENSION_MAX], i;
ebabbf58
MD
2584
2585 if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX)
2586 return NULL;
2587 /* Currently, only per-cpu allocation is supported. */
2588 switch (alloc_flags) {
2589 case USTCTL_COUNTER_ALLOC_PER_CPU:
2590 break;
2591
2592 case USTCTL_COUNTER_ALLOC_PER_CPU | USTCTL_COUNTER_ALLOC_GLOBAL:
2593 case USTCTL_COUNTER_ALLOC_GLOBAL:
2594 default:
2595 return NULL;
2596 }
2597 switch (bitness) {
2598 case USTCTL_COUNTER_BITNESS_32:
2599 switch (arithmetic) {
2600 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2601 transport_name = "counter-per-cpu-32-modular";
2602 break;
2603 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2604 transport_name = "counter-per-cpu-32-saturation";
2605 break;
2606 default:
2607 return NULL;
2608 }
2609 break;
2610 case USTCTL_COUNTER_BITNESS_64:
2611 switch (arithmetic) {
2612 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2613 transport_name = "counter-per-cpu-64-modular";
2614 break;
2615 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2616 transport_name = "counter-per-cpu-64-saturation";
2617 break;
2618 default:
2619 return NULL;
2620 }
2621 break;
2622 default:
2623 return NULL;
2624 }
2625
2626 transport = lttng_counter_transport_find(transport_name);
2627 if (!transport) {
2628 DBG("LTTng transport %s not found\n",
2629 transport_name);
2630 return NULL;
2631 }
2632
bb84a1ec
MD
2633 for (i = 0; i < nr_dimensions; i++) {
2634 if (dimensions[i].has_underflow)
2635 return NULL;
2636 if (dimensions[i].has_overflow)
2637 return NULL;
2638 counter_len[i] = ust_dim[i].size = dimensions[i].size;
2639 }
2640
ebabbf58
MD
2641 counter = zmalloc(sizeof(*counter));
2642 if (!counter)
2643 return NULL;
2644 counter->attr = zmalloc(sizeof(*counter->attr));
2645 if (!counter->attr)
2646 goto free_counter;
2647 counter->attr->bitness = bitness;
2648 counter->attr->arithmetic = arithmetic;
2649 counter->attr->nr_dimensions = nr_dimensions;
2650 counter->attr->global_sum_step = global_sum_step;
bb84a1ec
MD
2651 counter->attr->coalesce_hits = coalesce_hits;
2652
ebabbf58
MD
2653 for (i = 0; i < nr_dimensions; i++)
2654 counter->attr->dimensions[i] = dimensions[i];
2655
ebabbf58 2656 counter->counter = transport->ops.counter_create(nr_dimensions,
bb84a1ec 2657 counter_len, global_sum_step, global_counter_fd,
ebabbf58
MD
2658 nr_counter_cpu_fds, counter_cpu_fds, true);
2659 if (!counter->counter)
2660 goto free_attr;
2661 counter->ops = &transport->ops;
2662 return counter;
2663
2664free_attr:
2665 free(counter->attr);
2666free_counter:
2667 free(counter);
2668 return NULL;
2669}
2670
2671int ustctl_create_counter_data(struct ustctl_daemon_counter *counter,
2672 struct lttng_ust_object_data **_counter_data)
2673{
2674 struct lttng_ust_object_data *counter_data;
dca92811 2675 struct lttng_ust_counter_conf counter_conf = {0};
ebabbf58
MD
2676 size_t i;
2677 int ret;
2678
2679 switch (counter->attr->arithmetic) {
2680 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2681 counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_MODULAR;
2682 break;
2683 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2684 counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_SATURATION;
2685 break;
2686 default:
2687 return -EINVAL;
2688 }
2689 switch (counter->attr->bitness) {
2690 case USTCTL_COUNTER_BITNESS_32:
38980ed1 2691 counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_32;
ebabbf58
MD
2692 break;
2693 case USTCTL_COUNTER_BITNESS_64:
38980ed1 2694 counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_64;
ebabbf58
MD
2695 break;
2696 default:
2697 return -EINVAL;
2698 }
2699 counter_conf.number_dimensions = counter->attr->nr_dimensions;
2700 counter_conf.global_sum_step = counter->attr->global_sum_step;
bb84a1ec 2701 counter_conf.coalesce_hits = counter->attr->coalesce_hits;
ebabbf58
MD
2702 for (i = 0; i < counter->attr->nr_dimensions; i++) {
2703 counter_conf.dimensions[i].size = counter->attr->dimensions[i].size;
2704 counter_conf.dimensions[i].underflow_index = counter->attr->dimensions[i].underflow_index;
2705 counter_conf.dimensions[i].overflow_index = counter->attr->dimensions[i].overflow_index;
2706 counter_conf.dimensions[i].has_underflow = counter->attr->dimensions[i].has_underflow;
2707 counter_conf.dimensions[i].has_overflow = counter->attr->dimensions[i].has_overflow;
2708 }
2709
2710 counter_data = zmalloc(sizeof(*counter_data));
2711 if (!counter_data) {
2712 ret = -ENOMEM;
2713 goto error_alloc;
2714 }
2715 counter_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER;
2716 counter_data->handle = -1;
2717
2718 counter_data->size = sizeof(counter_conf);
2719 counter_data->u.counter.data = zmalloc(sizeof(counter_conf));
2720 if (!counter_data->u.counter.data) {
2721 ret = -ENOMEM;
2722 goto error_alloc_data;
2723 }
2724
2725 memcpy(counter_data->u.counter.data, &counter_conf, sizeof(counter_conf));
2726 *_counter_data = counter_data;
2727
2728 return 0;
2729
2730error_alloc_data:
2731 free(counter_data);
2732error_alloc:
2733 return ret;
2734}
2735
2736int ustctl_create_counter_global_data(struct ustctl_daemon_counter *counter,
2737 struct lttng_ust_object_data **_counter_global_data)
2738{
2739 struct lttng_ust_object_data *counter_global_data;
2740 int ret, fd;
2741 size_t len;
2742
2743 if (lttng_counter_get_global_shm(counter->counter, &fd, &len))
2744 return -EINVAL;
2745 counter_global_data = zmalloc(sizeof(*counter_global_data));
2746 if (!counter_global_data) {
2747 ret = -ENOMEM;
2748 goto error_alloc;
2749 }
2750 counter_global_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL;
2751 counter_global_data->handle = -1;
2752 counter_global_data->size = len;
2753 counter_global_data->u.counter_global.shm_fd = fd;
2754 *_counter_global_data = counter_global_data;
2755 return 0;
2756
2757error_alloc:
2758 return ret;
2759}
2760
2761int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter *counter, int cpu,
2762 struct lttng_ust_object_data **_counter_cpu_data)
2763{
2764 struct lttng_ust_object_data *counter_cpu_data;
2765 int ret, fd;
2766 size_t len;
2767
2768 if (lttng_counter_get_cpu_shm(counter->counter, cpu, &fd, &len))
2769 return -EINVAL;
2770 counter_cpu_data = zmalloc(sizeof(*counter_cpu_data));
2771 if (!counter_cpu_data) {
2772 ret = -ENOMEM;
2773 goto error_alloc;
2774 }
2775 counter_cpu_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_CPU;
2776 counter_cpu_data->handle = -1;
2777 counter_cpu_data->size = len;
2778 counter_cpu_data->u.counter_cpu.shm_fd = fd;
2779 counter_cpu_data->u.counter_cpu.cpu_nr = cpu;
2780 *_counter_cpu_data = counter_cpu_data;
2781 return 0;
2782
2783error_alloc:
2784 return ret;
2785}
2786
2787void ustctl_destroy_counter(struct ustctl_daemon_counter *counter)
2788{
2789 counter->ops->counter_destroy(counter->counter);
2790 free(counter->attr);
2791 free(counter);
2792}
2793
2794int ustctl_send_counter_data_to_ust(int sock, int parent_handle,
2795 struct lttng_ust_object_data *counter_data)
2796{
2797 struct ustcomm_ust_msg lum;
2798 struct ustcomm_ust_reply lur;
2799 int ret;
2800 size_t size;
2801 ssize_t len;
2802
2803 if (!counter_data)
2804 return -EINVAL;
2805
2806 size = counter_data->size;
2807 memset(&lum, 0, sizeof(lum));
2808 lum.handle = parent_handle;
2809 lum.cmd = LTTNG_UST_COUNTER;
2810 lum.u.counter.len = size;
2811 ret = ustcomm_send_app_msg(sock, &lum);
2812 if (ret)
2813 return ret;
2814
2815 /* Send counter data */
2816 len = ustcomm_send_unix_sock(sock, counter_data->u.counter.data, size);
2817 if (len != size) {
2818 if (len < 0)
2819 return len;
2820 else
2821 return -EIO;
2822 }
2823
2824 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2825 if (!ret) {
2826 counter_data->handle = lur.ret_val;
2827 }
2828 return ret;
2829}
2830
2831int ustctl_send_counter_global_data_to_ust(int sock,
2832 struct lttng_ust_object_data *counter_data,
2833 struct lttng_ust_object_data *counter_global_data)
2834{
2835 struct ustcomm_ust_msg lum;
2836 struct ustcomm_ust_reply lur;
2837 int ret, shm_fd[1];
2838 size_t size;
2839 ssize_t len;
2840
2841 if (!counter_data || !counter_global_data)
2842 return -EINVAL;
2843
2844 size = counter_global_data->size;
2845 memset(&lum, 0, sizeof(lum));
2846 lum.handle = counter_data->handle; /* parent handle */
2847 lum.cmd = LTTNG_UST_COUNTER_GLOBAL;
2848 lum.u.counter_global.len = size;
2849 ret = ustcomm_send_app_msg(sock, &lum);
2850 if (ret)
2851 return ret;
2852
2853 shm_fd[0] = counter_global_data->u.counter_global.shm_fd;
2854 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2855 if (len <= 0) {
2856 if (len < 0)
2857 return len;
2858 else
2859 return -EIO;
2860 }
2861
2862 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2863 if (!ret) {
2864 counter_global_data->handle = lur.ret_val;
2865 }
2866 return ret;
2867}
2868
2869int ustctl_send_counter_cpu_data_to_ust(int sock,
2870 struct lttng_ust_object_data *counter_data,
2871 struct lttng_ust_object_data *counter_cpu_data)
2872{
2873 struct ustcomm_ust_msg lum;
2874 struct ustcomm_ust_reply lur;
2875 int ret, shm_fd[1];
2876 size_t size;
2877 ssize_t len;
2878
2879 if (!counter_data || !counter_cpu_data)
2880 return -EINVAL;
2881
2882 size = counter_cpu_data->size;
2883 memset(&lum, 0, sizeof(lum));
2884 lum.handle = counter_data->handle; /* parent handle */
2885 lum.cmd = LTTNG_UST_COUNTER_CPU;
2886 lum.u.counter_cpu.len = size;
2887 lum.u.counter_cpu.cpu_nr = counter_cpu_data->u.counter_cpu.cpu_nr;
2888 ret = ustcomm_send_app_msg(sock, &lum);
2889 if (ret)
2890 return ret;
2891
2892 shm_fd[0] = counter_cpu_data->u.counter_global.shm_fd;
2893 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2894 if (len <= 0) {
2895 if (len < 0)
2896 return len;
2897 else
2898 return -EIO;
2899 }
2900
2901 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2902 if (!ret) {
2903 counter_cpu_data->handle = lur.ret_val;
2904 }
2905 return ret;
2906}
2907
bb84a1ec
MD
2908int ustctl_counter_create_event(int sock,
2909 struct lttng_ust_counter_event *counter_event,
2910 struct lttng_ust_object_data *counter_data,
2911 struct lttng_ust_object_data **_counter_event_data)
2912{
2913 struct ustcomm_ust_msg lum;
2914 struct ustcomm_ust_reply lur;
2915 struct lttng_ust_object_data *counter_event_data;
2916 ssize_t len;
2917 int ret;
2918
2919 if (!counter_data || !_counter_event_data)
2920 return -EINVAL;
2921
2922 counter_event_data = zmalloc(sizeof(*counter_event_data));
2923 if (!counter_event_data)
2924 return -ENOMEM;
2925 counter_event_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_EVENT;
2926 memset(&lum, 0, sizeof(lum));
2927 lum.handle = counter_data->handle;
2928 lum.cmd = LTTNG_UST_COUNTER_EVENT;
2929 lum.u.counter_event.len = sizeof(*counter_event);
2930 ret = ustcomm_send_app_msg(sock, &lum);
2931 if (ret) {
2932 free(counter_event_data);
2933 return ret;
2934 }
2935 /* Send struct lttng_ust_counter_event */
2936 len = ustcomm_send_unix_sock(sock, counter_event, sizeof(*counter_event));
2937 if (len != sizeof(*counter_event)) {
2938 free(counter_event_data);
2939 if (len < 0)
2940 return len;
2941 else
2942 return -EIO;
2943 }
2944 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2945 if (ret) {
2946 free(counter_event_data);
2947 return ret;
2948 }
2949 counter_event_data->handle = lur.ret_val;
2950 DBG("received counter event handle %u", counter_event_data->handle);
2951 *_counter_event_data = counter_event_data;
2952 return 0;
2953}
2954
ebabbf58
MD
2955int ustctl_counter_read(struct ustctl_daemon_counter *counter,
2956 const size_t *dimension_indexes,
2957 int cpu, int64_t *value,
2958 bool *overflow, bool *underflow)
2959{
2960 return counter->ops->counter_read(counter->counter, dimension_indexes, cpu,
2961 value, overflow, underflow);
2962}
2963
2964int ustctl_counter_aggregate(struct ustctl_daemon_counter *counter,
2965 const size_t *dimension_indexes,
2966 int64_t *value,
2967 bool *overflow, bool *underflow)
2968{
2969 return counter->ops->counter_aggregate(counter->counter, dimension_indexes,
2970 value, overflow, underflow);
2971}
2972
2973int ustctl_counter_clear(struct ustctl_daemon_counter *counter,
2974 const size_t *dimension_indexes)
2975{
2976 return counter->ops->counter_clear(counter->counter, dimension_indexes);
2977}
2978
74d81a6c
MD
2979static __attribute__((constructor))
2980void ustctl_init(void)
2981{
2982 init_usterr();
6f626d28 2983 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
f9364363 2984 lttng_ust_clock_init();
74d81a6c
MD
2985 lttng_ring_buffer_metadata_client_init();
2986 lttng_ring_buffer_client_overwrite_init();
34a91bdb 2987 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 2988 lttng_ring_buffer_client_discard_init();
34a91bdb 2989 lttng_ring_buffer_client_discard_rt_init();
ebabbf58
MD
2990 lttng_counter_client_percpu_32_modular_init();
2991 lttng_counter_client_percpu_64_modular_init();
03d2d293 2992 lib_ringbuffer_signal_init();
74d81a6c
MD
2993}
2994
2995static __attribute__((destructor))
2996void ustctl_exit(void)
2997{
34a91bdb 2998 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 2999 lttng_ring_buffer_client_discard_exit();
34a91bdb 3000 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
3001 lttng_ring_buffer_client_overwrite_exit();
3002 lttng_ring_buffer_metadata_client_exit();
ebabbf58
MD
3003 lttng_counter_client_percpu_32_modular_exit();
3004 lttng_counter_client_percpu_64_modular_exit();
57773204 3005}
This page took 0.178358 seconds and 5 git commands to generate.