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