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