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