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