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