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