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