bytecode: handle all integer types of dynamic contexts
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
74d81a6c 3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
57773204 4 *
e92f3e28
MD
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
57773204
MD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
e92f3e28
MD
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57773204
MD
17 */
18
fb31eb73 19#include <stdint.h>
57773204 20#include <string.h>
fb31eb73 21#include <sys/mman.h>
4e79769f 22#include <unistd.h>
fb31eb73 23
c62a3816 24#include <lttng/ust-config.h>
4318ae1b
MD
25#include <lttng/ust-ctl.h>
26#include <lttng/ust-abi.h>
c1fca457 27#include <lttng/ust-events.h>
3208818b 28#include <lttng/ust-endian.h>
44c72f10 29#include <usterr-signal-safe.h>
b728d87e 30#include <ust-comm.h>
74d81a6c 31#include <helper.h>
57773204
MD
32
33#include "../libringbuffer/backend.h"
34#include "../libringbuffer/frontend.h"
c9023c93 35#include "../liblttng-ust/wait.h"
b2f3252a 36#include "../liblttng-ust/lttng-rb-clients.h"
f9364363 37#include "../liblttng-ust/clock.h"
92ce256d 38#include "../liblttng-ust/getenv.h"
c9023c93
MD
39
40/*
41 * Number of milliseconds to retry before failing metadata writes on
42 * buffer full condition. (10 seconds)
43 */
44#define LTTNG_METADATA_TIMEOUT_MSEC 10000
57773204 45
74d81a6c
MD
46/*
47 * Channel representation within consumer.
48 */
49struct ustctl_consumer_channel {
50 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 51
74d81a6c
MD
52 /* initial attributes */
53 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
54 int wait_fd; /* monitor close() */
55 int wakeup_fd; /* monitor close() */
74d81a6c
MD
56};
57
58/*
59 * Stream representation within consumer.
60 */
61struct ustctl_consumer_stream {
62 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
63 struct lttng_ust_lib_ring_buffer *buf;
64 struct ustctl_consumer_channel *chan;
65 int shm_fd, wait_fd, wakeup_fd;
66 int cpu;
67 uint64_t memory_map_size;
68};
69
70extern void lttng_ring_buffer_client_overwrite_init(void);
08a3170c 71extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74d81a6c 72extern void lttng_ring_buffer_client_discard_init(void);
08a3170c 73extern void lttng_ring_buffer_client_discard_rt_init(void);
74d81a6c
MD
74extern void lttng_ring_buffer_metadata_client_init(void);
75extern void lttng_ring_buffer_client_overwrite_exit(void);
08a3170c 76extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74d81a6c 77extern void lttng_ring_buffer_client_discard_exit(void);
08a3170c 78extern void lttng_ring_buffer_client_discard_rt_exit(void);
74d81a6c
MD
79extern void lttng_ring_buffer_metadata_client_exit(void);
80
2be0e72c
MD
81int ustctl_release_handle(int sock, int handle)
82{
83 struct ustcomm_ust_msg lum;
84 struct ustcomm_ust_reply lur;
2be0e72c 85
74d81a6c
MD
86 if (sock < 0 || handle < 0)
87 return 0;
88 memset(&lum, 0, sizeof(lum));
89 lum.handle = handle;
90 lum.cmd = LTTNG_UST_RELEASE;
91 return ustcomm_send_app_cmd(sock, &lum, &lur);
2be0e72c 92}
74d81a6c 93
12388166
MD
94/*
95 * If sock is negative, it means we don't have to notify the other side
96 * (e.g. application has already vanished).
97 */
d26228ae 98int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 99{
57773204
MD
100 int ret;
101
9bfc503d
MD
102 if (!data)
103 return -EINVAL;
104
74d81a6c
MD
105 switch (data->type) {
106 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
107 if (data->u.channel.wakeup_fd >= 0) {
108 ret = close(data->u.channel.wakeup_fd);
109 if (ret < 0) {
110 ret = -errno;
111 return ret;
112 }
dd6c697c 113 data->u.channel.wakeup_fd = -1;
ff0f5728 114 }
74d81a6c 115 free(data->u.channel.data);
dd6c697c 116 data->u.channel.data = NULL;
74d81a6c
MD
117 break;
118 case LTTNG_UST_OBJECT_TYPE_STREAM:
119 if (data->u.stream.shm_fd >= 0) {
120 ret = close(data->u.stream.shm_fd);
121 if (ret < 0) {
122 ret = -errno;
123 return ret;
124 }
dd6c697c 125 data->u.stream.shm_fd = -1;
d26228ae 126 }
74d81a6c
MD
127 if (data->u.stream.wakeup_fd >= 0) {
128 ret = close(data->u.stream.wakeup_fd);
129 if (ret < 0) {
130 ret = -errno;
131 return ret;
132 }
dd6c697c 133 data->u.stream.wakeup_fd = -1;
d26228ae 134 }
74d81a6c 135 break;
32ce8569
MD
136 case LTTNG_UST_OBJECT_TYPE_EVENT:
137 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
d8d2416d
FD
138 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP:
139 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER:
32ce8569 140 break;
74d81a6c
MD
141 default:
142 assert(0);
d26228ae 143 }
2be0e72c 144 return ustctl_release_handle(sock, data->handle);
57773204
MD
145}
146
1c5e467e
MD
147/*
148 * Send registration done packet to the application.
149 */
150int ustctl_register_done(int sock)
151{
152 struct ustcomm_ust_msg lum;
153 struct ustcomm_ust_reply lur;
154 int ret;
155
156 DBG("Sending register done command to %d", sock);
157 memset(&lum, 0, sizeof(lum));
158 lum.handle = LTTNG_UST_ROOT_HANDLE;
159 lum.cmd = LTTNG_UST_REGISTER_DONE;
160 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
161 if (ret)
162 return ret;
1c5e467e 163 return 0;
1c5e467e
MD
164}
165
57773204
MD
166/*
167 * returns session handle.
168 */
169int ustctl_create_session(int sock)
170{
171 struct ustcomm_ust_msg lum;
172 struct ustcomm_ust_reply lur;
173 int ret, session_handle;
174
175 /* Create session */
176 memset(&lum, 0, sizeof(lum));
177 lum.handle = LTTNG_UST_ROOT_HANDLE;
178 lum.cmd = LTTNG_UST_SESSION;
179 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
180 if (ret)
181 return ret;
182 session_handle = lur.ret_val;
183 DBG("received session handle %u", session_handle);
184 return session_handle;
185}
186
57773204 187int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
188 struct lttng_ust_object_data *channel_data,
189 struct lttng_ust_object_data **_event_data)
57773204
MD
190{
191 struct ustcomm_ust_msg lum;
192 struct ustcomm_ust_reply lur;
61f02aea 193 struct lttng_ust_object_data *event_data;
57773204
MD
194 int ret;
195
9bfc503d
MD
196 if (!channel_data || !_event_data)
197 return -EINVAL;
198
74d81a6c 199 event_data = zmalloc(sizeof(*event_data));
57773204
MD
200 if (!event_data)
201 return -ENOMEM;
32ce8569 202 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
57773204
MD
203 memset(&lum, 0, sizeof(lum));
204 lum.handle = channel_data->handle;
205 lum.cmd = LTTNG_UST_EVENT;
206 strncpy(lum.u.event.name, ev->name,
207 LTTNG_UST_SYM_NAME_LEN);
208 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
209 lum.u.event.loglevel_type = ev->loglevel_type;
210 lum.u.event.loglevel = ev->loglevel;
57773204
MD
211 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
212 if (ret) {
213 free(event_data);
214 return ret;
215 }
216 event_data->handle = lur.ret_val;
217 DBG("received event handle %u", event_data->handle);
218 *_event_data = event_data;
219 return 0;
220}
221
53f0df51 222int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx,
61f02aea
MD
223 struct lttng_ust_object_data *obj_data,
224 struct lttng_ust_object_data **_context_data)
57773204
MD
225{
226 struct ustcomm_ust_msg lum;
227 struct ustcomm_ust_reply lur;
53f0df51
JG
228 struct lttng_ust_object_data *context_data = NULL;
229 char *buf = NULL;
230 size_t len;
57773204
MD
231 int ret;
232
53f0df51
JG
233 if (!obj_data || !_context_data) {
234 ret = -EINVAL;
235 goto end;
236 }
9bfc503d 237
74d81a6c 238 context_data = zmalloc(sizeof(*context_data));
53f0df51
JG
239 if (!context_data) {
240 ret = -ENOMEM;
241 goto end;
242 }
32ce8569 243 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
57773204 244 memset(&lum, 0, sizeof(lum));
3039d8ed 245 lum.handle = obj_data->handle;
57773204 246 lum.cmd = LTTNG_UST_CONTEXT;
53f0df51
JG
247
248 lum.u.context.ctx = ctx->ctx;
249 switch (ctx->ctx) {
250 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
251 lum.u.context.u.perf_counter = ctx->u.perf_counter;
252 break;
253 case LTTNG_UST_CONTEXT_APP_CONTEXT:
254 {
255 size_t provider_name_len = strlen(
256 ctx->u.app_ctx.provider_name) + 1;
257 size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1;
258
259 lum.u.context.u.app_ctx.provider_name_len = provider_name_len;
260 lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len;
261
262 len = provider_name_len + ctx_name_len;
263 buf = zmalloc(len);
264 if (!buf) {
265 ret = -ENOMEM;
266 goto end;
267 }
268 memcpy(buf, ctx->u.app_ctx.provider_name,
269 provider_name_len);
270 memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name,
271 ctx_name_len);
272 break;
273 }
274 default:
275 break;
276 }
277 ret = ustcomm_send_app_msg(sock, &lum);
278 if (ret)
279 goto end;
280 if (buf) {
281 /* send var len ctx_name */
282 ret = ustcomm_send_unix_sock(sock, buf, len);
283 if (ret < 0) {
284 goto end;
285 }
286 if (ret != len) {
287 ret = -EINVAL;
288 goto end;
289 }
290 }
291 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
292 if (ret < 0) {
293 goto end;
57773204 294 }
32ce8569
MD
295 context_data->handle = -1;
296 DBG("Context created successfully");
57773204 297 *_context_data = context_data;
53f0df51
JG
298 context_data = NULL;
299end:
300 free(context_data);
301 free(buf);
57773204
MD
302 return ret;
303}
304
cd54f6d9
MD
305int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
306 struct lttng_ust_object_data *obj_data)
307{
308 struct ustcomm_ust_msg lum;
309 struct ustcomm_ust_reply lur;
310 int ret;
311
312 if (!obj_data)
313 return -EINVAL;
314
315 memset(&lum, 0, sizeof(lum));
316 lum.handle = obj_data->handle;
317 lum.cmd = LTTNG_UST_FILTER;
318 lum.u.filter.data_size = bytecode->len;
319 lum.u.filter.reloc_offset = bytecode->reloc_offset;
e695af51 320 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
321
322 ret = ustcomm_send_app_msg(sock, &lum);
323 if (ret)
324 return ret;
cd54f6d9
MD
325 /* send var len bytecode */
326 ret = ustcomm_send_unix_sock(sock, bytecode->data,
327 bytecode->len);
328 if (ret < 0) {
329 return ret;
330 }
7bc53e94
MD
331 if (ret != bytecode->len)
332 return -EINVAL;
333 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
334}
335
da57c034
JI
336int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion,
337 struct lttng_ust_object_data *obj_data)
338{
339 struct ustcomm_ust_msg lum;
340 struct ustcomm_ust_reply lur;
341 int ret;
342
343 if (!obj_data) {
344 return -EINVAL;
345 }
346
347 memset(&lum, 0, sizeof(lum));
348 lum.handle = obj_data->handle;
349 lum.cmd = LTTNG_UST_EXCLUSION;
350 lum.u.exclusion.count = exclusion->count;
351
352 ret = ustcomm_send_app_msg(sock, &lum);
353 if (ret) {
354 return ret;
355 }
356
1628366f 357 /* send var len exclusion names */
da57c034
JI
358 ret = ustcomm_send_unix_sock(sock,
359 exclusion->names,
360 exclusion->count * LTTNG_UST_SYM_NAME_LEN);
361 if (ret < 0) {
362 return ret;
363 }
364 if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) {
365 return -EINVAL;
366 }
367 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
368}
369
57773204 370/* Enable event, channel and session ioctl */
61f02aea 371int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
372{
373 struct ustcomm_ust_msg lum;
374 struct ustcomm_ust_reply lur;
375 int ret;
376
9bfc503d
MD
377 if (!object)
378 return -EINVAL;
379
57773204
MD
380 memset(&lum, 0, sizeof(lum));
381 lum.handle = object->handle;
382 lum.cmd = LTTNG_UST_ENABLE;
383 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
384 if (ret)
385 return ret;
386 DBG("enabled handle %u", object->handle);
387 return 0;
388}
389
390/* Disable event, channel and session ioctl */
61f02aea 391int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
392{
393 struct ustcomm_ust_msg lum;
394 struct ustcomm_ust_reply lur;
395 int ret;
396
9bfc503d
MD
397 if (!object)
398 return -EINVAL;
399
57773204
MD
400 memset(&lum, 0, sizeof(lum));
401 lum.handle = object->handle;
402 lum.cmd = LTTNG_UST_DISABLE;
403 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
404 if (ret)
405 return ret;
406 DBG("disable handle %u", object->handle);
407 return 0;
408}
409
4a6ca058 410int ustctl_start_session(int sock, int handle)
57773204 411{
61f02aea 412 struct lttng_ust_object_data obj;
4a6ca058
MD
413
414 obj.handle = handle;
415 return ustctl_enable(sock, &obj);
57773204
MD
416}
417
4a6ca058 418int ustctl_stop_session(int sock, int handle)
57773204 419{
61f02aea 420 struct lttng_ust_object_data obj;
4a6ca058
MD
421
422 obj.handle = handle;
423 return ustctl_disable(sock, &obj);
57773204
MD
424}
425
d8d2416d
FD
426int ustctl_create_event_notifier_group(int sock, int pipe_fd,
427 struct lttng_ust_object_data **_event_notifier_group_data)
428{
429 struct lttng_ust_object_data *event_notifier_group_data;
430 struct ustcomm_ust_msg lum;
431 struct ustcomm_ust_reply lur;
432 ssize_t len;
433 int ret;
434
435 if (!_event_notifier_group_data)
436 return -EINVAL;
437
438 event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data));
439 if (!event_notifier_group_data)
440 return -ENOMEM;
441
442 event_notifier_group_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP;
443
444 memset(&lum, 0, sizeof(lum));
445 lum.handle = LTTNG_UST_ROOT_HANDLE;
446 lum.cmd = LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE;
447
448 ret = ustcomm_send_app_msg(sock, &lum);
449 if (ret)
450 goto error;
451
452 /* Send event_notifier notification pipe. */
453 len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1);
454 if (len <= 0) {
455 ret = len;
456 goto error;
457 }
458
459 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
460 if (ret)
461 goto error;
462
463 event_notifier_group_data->handle = lur.ret_val;
464 DBG("received event_notifier group handle %d", event_notifier_group_data->handle);
465
466 *_event_notifier_group_data = event_notifier_group_data;
467
468 ret = 0;
469 goto end;
470error:
471 free(event_notifier_group_data);
472
473end:
474 return ret;
475}
476
477int ustctl_create_event_notifier(int sock, struct lttng_ust_event_notifier *event_notifier,
478 struct lttng_ust_object_data *event_notifier_group,
479 struct lttng_ust_object_data **_event_notifier_data)
480{
481 struct ustcomm_ust_msg lum;
482 struct ustcomm_ust_reply lur;
483 struct lttng_ust_object_data *event_notifier_data;
484 int ret;
485
486 if (!event_notifier_group || !_event_notifier_data)
487 return -EINVAL;
488
489 event_notifier_data = zmalloc(sizeof(*event_notifier_data));
490 if (!event_notifier_data)
491 return -ENOMEM;
492
493 event_notifier_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER;
494
495 memset(&lum, 0, sizeof(lum));
496 lum.handle = event_notifier_group->handle;
497 lum.cmd = LTTNG_UST_EVENT_NOTIFIER_CREATE;
498
499 strncpy(lum.u.event_notifier.event.name, event_notifier->event.name,
500 LTTNG_UST_SYM_NAME_LEN);
501 lum.u.event_notifier.event.instrumentation = event_notifier->event.instrumentation;
502 lum.u.event_notifier.event.loglevel_type = event_notifier->event.loglevel_type;
503 lum.u.event_notifier.event.loglevel = event_notifier->event.loglevel;
504 lum.u.event_notifier.event.token = event_notifier->event.token;
505 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
506 if (ret) {
507 free(event_notifier_data);
508 return ret;
509 }
510 event_notifier_data->handle = lur.ret_val;
511 DBG("received event_notifier handle %u", event_notifier_data->handle);
512 *_event_notifier_data = event_notifier_data;
513
514 return ret;
515}
516
57773204
MD
517int ustctl_tracepoint_list(int sock)
518{
b115631f
MD
519 struct ustcomm_ust_msg lum;
520 struct ustcomm_ust_reply lur;
521 int ret, tp_list_handle;
522
523 memset(&lum, 0, sizeof(lum));
524 lum.handle = LTTNG_UST_ROOT_HANDLE;
525 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
526 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
527 if (ret)
528 return ret;
529 tp_list_handle = lur.ret_val;
530 DBG("received tracepoint list handle %u", tp_list_handle);
531 return tp_list_handle;
532}
533
534int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
cbef6901 535 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
536{
537 struct ustcomm_ust_msg lum;
538 struct ustcomm_ust_reply lur;
539 int ret;
540
9bfc503d
MD
541 if (!iter)
542 return -EINVAL;
543
b115631f
MD
544 memset(&lum, 0, sizeof(lum));
545 lum.handle = tp_list_handle;
546 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
547 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
548 if (ret)
549 return ret;
882a56d7 550 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 551 lur.u.tracepoint.name,
882a56d7 552 lur.u.tracepoint.loglevel);
cbef6901 553 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 554 return 0;
57773204
MD
555}
556
40003310
MD
557int ustctl_tracepoint_field_list(int sock)
558{
559 struct ustcomm_ust_msg lum;
560 struct ustcomm_ust_reply lur;
561 int ret, tp_field_list_handle;
562
563 memset(&lum, 0, sizeof(lum));
564 lum.handle = LTTNG_UST_ROOT_HANDLE;
565 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
566 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
567 if (ret)
568 return ret;
569 tp_field_list_handle = lur.ret_val;
570 DBG("received tracepoint field list handle %u", tp_field_list_handle);
571 return tp_field_list_handle;
572}
573
574int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
575 struct lttng_ust_field_iter *iter)
576{
577 struct ustcomm_ust_msg lum;
578 struct ustcomm_ust_reply lur;
579 int ret;
580 ssize_t len;
581
582 if (!iter)
583 return -EINVAL;
584
585 memset(&lum, 0, sizeof(lum));
586 lum.handle = tp_field_list_handle;
587 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
588 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
589 if (ret)
590 return ret;
591 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
592 if (len != sizeof(*iter)) {
593 return -EINVAL;
594 }
595 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
596 iter->event_name,
597 iter->loglevel,
598 iter->field_name,
599 iter->type);
600 return 0;
601}
602
57773204
MD
603int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
604{
605 struct ustcomm_ust_msg lum;
606 struct ustcomm_ust_reply lur;
607 int ret;
608
9bfc503d
MD
609 if (!v)
610 return -EINVAL;
611
57773204
MD
612 memset(&lum, 0, sizeof(lum));
613 lum.handle = LTTNG_UST_ROOT_HANDLE;
614 lum.cmd = LTTNG_UST_TRACER_VERSION;
615 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
616 if (ret)
617 return ret;
618 memcpy(v, &lur.u.version, sizeof(*v));
619 DBG("received tracer version");
620 return 0;
621}
622
623int ustctl_wait_quiescent(int sock)
624{
625 struct ustcomm_ust_msg lum;
626 struct ustcomm_ust_reply lur;
627 int ret;
628
629 memset(&lum, 0, sizeof(lum));
630 lum.handle = LTTNG_UST_ROOT_HANDLE;
631 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
632 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
633 if (ret)
634 return ret;
635 DBG("waited for quiescent state");
636 return 0;
637}
638
639int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
640{
9bfc503d
MD
641 if (!calibrate)
642 return -EINVAL;
643
57773204
MD
644 return -ENOSYS;
645}
646
f1fffc57
MD
647int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
648{
649 struct ustcomm_ust_msg lum;
650 struct ustcomm_ust_reply lur;
651 int ret;
652
9bfc503d
MD
653 if (!object)
654 return -EINVAL;
655
f1fffc57
MD
656 memset(&lum, 0, sizeof(lum));
657 lum.handle = object->handle;
658 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
659 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
660 if (ret)
661 return ret;
662 DBG("flushed buffer handle %u", object->handle);
663 return 0;
664}
665
74d81a6c
MD
666static
667int ustctl_send_channel(int sock,
668 enum lttng_ust_chan_type type,
669 void *data,
670 uint64_t size,
ff0f5728 671 int wakeup_fd,
74d81a6c
MD
672 int send_fd_only)
673{
674 ssize_t len;
675
676 if (!send_fd_only) {
677 /* Send mmap size */
678 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
679 if (len != sizeof(size)) {
680 if (len < 0)
681 return len;
682 else
683 return -EIO;
684 }
685
686 /* Send channel type */
687 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
688 if (len != sizeof(type)) {
689 if (len < 0)
690 return len;
691 else
692 return -EIO;
693 }
694 }
695
696 /* Send channel data */
697 len = ustcomm_send_unix_sock(sock, data, size);
698 if (len != size) {
699 if (len < 0)
700 return len;
701 else
702 return -EIO;
703 }
57773204 704
ff0f5728
MD
705 /* Send wakeup fd */
706 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
707 if (len <= 0) {
708 if (len < 0)
709 return len;
710 else
711 return -EIO;
712 }
74d81a6c
MD
713 return 0;
714}
715
716static
717int ustctl_send_stream(int sock,
718 uint32_t stream_nr,
719 uint64_t memory_map_size,
720 int shm_fd, int wakeup_fd,
721 int send_fd_only)
57773204 722{
74d81a6c
MD
723 ssize_t len;
724 int fds[2];
725
726 if (!send_fd_only) {
727 if (shm_fd < 0) {
728 /* finish iteration */
729 uint64_t v = -1;
730
731 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
732 if (len != sizeof(v)) {
733 if (len < 0)
734 return len;
735 else
736 return -EIO;
737 }
738 return 0;
739 }
740
741 /* Send mmap size */
742 len = ustcomm_send_unix_sock(sock, &memory_map_size,
743 sizeof(memory_map_size));
744 if (len != sizeof(memory_map_size)) {
745 if (len < 0)
746 return len;
747 else
748 return -EIO;
749 }
750
751 /* Send stream nr */
752 len = ustcomm_send_unix_sock(sock, &stream_nr,
753 sizeof(stream_nr));
754 if (len != sizeof(stream_nr)) {
755 if (len < 0)
756 return len;
757 else
758 return -EIO;
759 }
760 }
761
762 /* Send shm fd and wakeup fd */
763 fds[0] = shm_fd;
764 fds[1] = wakeup_fd;
765 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
766 if (len <= 0) {
767 if (len < 0)
768 return len;
769 else
770 return -EIO;
771 }
772 return 0;
773}
774
775int ustctl_recv_channel_from_consumer(int sock,
776 struct lttng_ust_object_data **_channel_data)
777{
778 struct lttng_ust_object_data *channel_data;
779 ssize_t len;
ff0f5728 780 int wakeup_fd;
7a784989 781 int ret;
57773204 782
74d81a6c
MD
783 channel_data = zmalloc(sizeof(*channel_data));
784 if (!channel_data) {
785 ret = -ENOMEM;
786 goto error_alloc;
787 }
788 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
12f3dabc 789 channel_data->handle = -1;
74d81a6c
MD
790
791 /* recv mmap size */
792 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
793 sizeof(channel_data->size));
794 if (len != sizeof(channel_data->size)) {
795 if (len < 0)
796 ret = len;
797 else
798 ret = -EINVAL;
799 goto error;
800 }
9bfc503d 801
74d81a6c
MD
802 /* recv channel type */
803 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
804 sizeof(channel_data->u.channel.type));
805 if (len != sizeof(channel_data->u.channel.type)) {
806 if (len < 0)
807 ret = len;
808 else
809 ret = -EINVAL;
810 goto error;
811 }
812
813 /* recv channel data */
814 channel_data->u.channel.data = zmalloc(channel_data->size);
815 if (!channel_data->u.channel.data) {
816 ret = -ENOMEM;
817 goto error;
818 }
819 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
820 channel_data->size);
821 if (len != channel_data->size) {
822 if (len < 0)
823 ret = len;
824 else
825 ret = -EINVAL;
826 goto error_recv_data;
827 }
ff0f5728
MD
828 /* recv wakeup fd */
829 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
830 if (len <= 0) {
831 if (len < 0) {
832 ret = len;
833 goto error_recv_data;
834 } else {
835 ret = -EIO;
836 goto error_recv_data;
837 }
838 }
839 channel_data->u.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
840 *_channel_data = channel_data;
841 return 0;
842
843error_recv_data:
844 free(channel_data->u.channel.data);
845error:
846 free(channel_data);
847error_alloc:
848 return ret;
849}
850
851int ustctl_recv_stream_from_consumer(int sock,
852 struct lttng_ust_object_data **_stream_data)
853{
854 struct lttng_ust_object_data *stream_data;
855 ssize_t len;
856 int ret;
857 int fds[2];
858
859 stream_data = zmalloc(sizeof(*stream_data));
860 if (!stream_data) {
861 ret = -ENOMEM;
862 goto error_alloc;
57773204 863 }
74d81a6c
MD
864
865 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
866 stream_data->handle = -1;
867
868 /* recv mmap size */
869 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
870 sizeof(stream_data->size));
871 if (len != sizeof(stream_data->size)) {
872 if (len < 0)
873 ret = len;
874 else
875 ret = -EINVAL;
876 goto error;
877 }
878 if (stream_data->size == -1) {
879 ret = -LTTNG_UST_ERR_NOENT;
880 goto error;
881 }
882
883 /* recv stream nr */
884 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
885 sizeof(stream_data->u.stream.stream_nr));
886 if (len != sizeof(stream_data->u.stream.stream_nr)) {
887 if (len < 0)
888 ret = len;
889 else
890 ret = -EINVAL;
891 goto error;
892 }
893
894 /* recv shm fd and wakeup fd */
895 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
896 if (len <= 0) {
897 if (len < 0) {
898 ret = len;
899 goto error;
900 } else {
901 ret = -EIO;
902 goto error;
0bfe09ec 903 }
0bfe09ec 904 }
74d81a6c
MD
905 stream_data->u.stream.shm_fd = fds[0];
906 stream_data->u.stream.wakeup_fd = fds[1];
907 *_stream_data = stream_data;
908 return 0;
0bfe09ec 909
74d81a6c
MD
910error:
911 free(stream_data);
912error_alloc:
913 return ret;
914}
915
916int ustctl_send_channel_to_ust(int sock, int session_handle,
917 struct lttng_ust_object_data *channel_data)
918{
919 struct ustcomm_ust_msg lum;
920 struct ustcomm_ust_reply lur;
921 int ret;
922
923 if (!channel_data)
924 return -EINVAL;
925
926 memset(&lum, 0, sizeof(lum));
927 lum.handle = session_handle;
928 lum.cmd = LTTNG_UST_CHANNEL;
929 lum.u.channel.len = channel_data->size;
930 lum.u.channel.type = channel_data->u.channel.type;
931 ret = ustcomm_send_app_msg(sock, &lum);
932 if (ret)
933 return ret;
934
935 ret = ustctl_send_channel(sock,
936 channel_data->u.channel.type,
937 channel_data->u.channel.data,
938 channel_data->size,
ff0f5728 939 channel_data->u.channel.wakeup_fd,
74d81a6c
MD
940 1);
941 if (ret)
942 return ret;
943 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
944 if (!ret) {
7f2348b8 945 channel_data->handle = lur.ret_val;
57773204 946 }
74d81a6c
MD
947 return ret;
948}
949
950int ustctl_send_stream_to_ust(int sock,
951 struct lttng_ust_object_data *channel_data,
952 struct lttng_ust_object_data *stream_data)
953{
954 struct ustcomm_ust_msg lum;
955 struct ustcomm_ust_reply lur;
956 int ret;
957
958 memset(&lum, 0, sizeof(lum));
959 lum.handle = channel_data->handle;
960 lum.cmd = LTTNG_UST_STREAM;
961 lum.u.stream.len = stream_data->size;
962 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
963 ret = ustcomm_send_app_msg(sock, &lum);
964 if (ret)
965 return ret;
966
967 assert(stream_data);
968 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
969
970 ret = ustctl_send_stream(sock,
971 stream_data->u.stream.stream_nr,
972 stream_data->size,
973 stream_data->u.stream.shm_fd,
974 stream_data->u.stream.wakeup_fd, 1);
975 if (ret)
976 return ret;
977 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
978}
979
12f3dabc
MD
980int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest,
981 struct lttng_ust_object_data *src)
982{
983 struct lttng_ust_object_data *obj;
984 int ret;
985
986 if (src->handle != -1) {
987 ret = -EINVAL;
988 goto error;
989 }
990
991 obj = zmalloc(sizeof(*obj));
992 if (!obj) {
993 ret = -ENOMEM;
994 goto error;
995 }
996
997 obj->type = src->type;
998 obj->handle = src->handle;
999 obj->size = src->size;
1000
1001 switch (obj->type) {
1002 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
1003 {
1004 obj->u.channel.type = src->u.channel.type;
1005 if (src->u.channel.wakeup_fd >= 0) {
1006 obj->u.channel.wakeup_fd =
1007 dup(src->u.channel.wakeup_fd);
1008 if (obj->u.channel.wakeup_fd < 0) {
1009 ret = errno;
1010 goto chan_error_wakeup_fd;
1011 }
1012 } else {
1013 obj->u.channel.wakeup_fd =
1014 src->u.channel.wakeup_fd;
1015 }
1016 obj->u.channel.data = zmalloc(obj->size);
1017 if (!obj->u.channel.data) {
1018 ret = -ENOMEM;
1019 goto chan_error_alloc;
1020 }
1021 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
1022 break;
1023
1024 chan_error_alloc:
1025 if (src->u.channel.wakeup_fd >= 0) {
1026 int closeret;
1027
1028 closeret = close(obj->u.channel.wakeup_fd);
1029 if (closeret) {
1030 PERROR("close");
1031 }
1032 }
1033 chan_error_wakeup_fd:
1034 goto error_type;
1035
1036 }
1037
1038 case LTTNG_UST_OBJECT_TYPE_STREAM:
1039 {
1040 obj->u.stream.stream_nr = src->u.stream.stream_nr;
1041 if (src->u.stream.wakeup_fd >= 0) {
1042 obj->u.stream.wakeup_fd =
1043 dup(src->u.stream.wakeup_fd);
1044 if (obj->u.stream.wakeup_fd < 0) {
1045 ret = errno;
1046 goto stream_error_wakeup_fd;
1047 }
1048 } else {
1049 obj->u.stream.wakeup_fd =
1050 src->u.stream.wakeup_fd;
1051 }
1052
1053 if (src->u.stream.shm_fd >= 0) {
1054 obj->u.stream.shm_fd =
1055 dup(src->u.stream.shm_fd);
1056 if (obj->u.stream.shm_fd < 0) {
1057 ret = errno;
1058 goto stream_error_shm_fd;
1059 }
1060 } else {
1061 obj->u.stream.shm_fd =
1062 src->u.stream.shm_fd;
1063 }
1064 break;
1065
1066 stream_error_shm_fd:
1067 if (src->u.stream.wakeup_fd >= 0) {
1068 int closeret;
1069
1070 closeret = close(obj->u.stream.wakeup_fd);
1071 if (closeret) {
1072 PERROR("close");
1073 }
1074 }
1075 stream_error_wakeup_fd:
1076 goto error_type;
1077 }
1078
1079 default:
1080 ret = -EINVAL;
1081 goto error_type;
1082 }
1083
1084 *dest = obj;
1085 return 0;
1086
1087error_type:
1088 free(obj);
1089error:
1090 return ret;
1091}
1092
74d81a6c
MD
1093
1094/* Buffer operations */
1095
5ea386c3
MD
1096int ustctl_get_nr_stream_per_channel(void)
1097{
1098 return num_possible_cpus();
1099}
1100
74d81a6c 1101struct ustctl_consumer_channel *
5ea386c3
MD
1102 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr,
1103 const int *stream_fds, int nr_stream_fds)
74d81a6c
MD
1104{
1105 struct ustctl_consumer_channel *chan;
1106 const char *transport_name;
1107 struct lttng_transport *transport;
1108
1109 switch (attr->type) {
1110 case LTTNG_UST_CHAN_PER_CPU:
1111 if (attr->output == LTTNG_UST_MMAP) {
34a91bdb
MD
1112 if (attr->overwrite) {
1113 if (attr->read_timer_interval == 0) {
1114 transport_name = "relay-overwrite-mmap";
1115 } else {
1116 transport_name = "relay-overwrite-rt-mmap";
1117 }
1118 } else {
1119 if (attr->read_timer_interval == 0) {
1120 transport_name = "relay-discard-mmap";
1121 } else {
1122 transport_name = "relay-discard-rt-mmap";
1123 }
1124 }
74d81a6c
MD
1125 } else {
1126 return NULL;
1127 }
c1fca457 1128 break;
74d81a6c
MD
1129 case LTTNG_UST_CHAN_METADATA:
1130 if (attr->output == LTTNG_UST_MMAP)
1131 transport_name = "relay-metadata-mmap";
1132 else
1133 return NULL;
c1fca457
MD
1134 break;
1135 default:
74d81a6c 1136 transport_name = "<unknown>";
c1fca457
MD
1137 return NULL;
1138 }
74d81a6c
MD
1139
1140 transport = lttng_transport_find(transport_name);
1141 if (!transport) {
1142 DBG("LTTng transport %s not found\n",
32ce8569 1143 transport_name);
74d81a6c 1144 return NULL;
7a784989 1145 }
74d81a6c
MD
1146
1147 chan = zmalloc(sizeof(*chan));
1148 if (!chan)
1149 return NULL;
1150
1151 chan->chan = transport->ops.channel_create(transport_name, NULL,
32ce8569 1152 attr->subbuf_size, attr->num_subbuf,
74d81a6c 1153 attr->switch_timer_interval,
32ce8569 1154 attr->read_timer_interval,
a9ff648c 1155 attr->uuid, attr->chan_id,
b2c5f61a
MD
1156 stream_fds, nr_stream_fds,
1157 attr->blocking_timeout);
74d81a6c
MD
1158 if (!chan->chan) {
1159 goto chan_error;
1160 }
1161 chan->chan->ops = &transport->ops;
1162 memcpy(&chan->attr, attr, sizeof(chan->attr));
cb7378b3
MD
1163 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
1164 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
74d81a6c
MD
1165 return chan;
1166
1167chan_error:
1168 free(chan);
1169 return NULL;
57773204
MD
1170}
1171
74d81a6c 1172void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 1173{
b24e4e91
MD
1174 (void) ustctl_channel_close_wait_fd(chan);
1175 (void) ustctl_channel_close_wakeup_fd(chan);
74d81a6c
MD
1176 chan->chan->ops->channel_destroy(chan->chan);
1177 free(chan);
1178}
1179
1180int ustctl_send_channel_to_sessiond(int sock,
1181 struct ustctl_consumer_channel *channel)
1182{
1183 struct shm_object_table *table;
57773204 1184
74d81a6c
MD
1185 table = channel->chan->handle->table;
1186 if (table->size <= 0)
9bfc503d 1187 return -EINVAL;
74d81a6c
MD
1188 return ustctl_send_channel(sock,
1189 channel->attr.type,
1190 table->objects[0].memory_map,
1191 table->objects[0].memory_map_size,
ff0f5728 1192 channel->wakeup_fd,
74d81a6c
MD
1193 0);
1194}
9bfc503d 1195
74d81a6c
MD
1196int ustctl_send_stream_to_sessiond(int sock,
1197 struct ustctl_consumer_stream *stream)
1198{
1199 if (!stream)
1200 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1201
1202 return ustctl_send_stream(sock,
1203 stream->cpu,
1204 stream->memory_map_size,
1205 stream->shm_fd, stream->wakeup_fd,
1206 0);
57773204
MD
1207}
1208
c9023c93
MD
1209int ustctl_write_metadata_to_channel(
1210 struct ustctl_consumer_channel *channel,
1211 const char *metadata_str, /* NOT null-terminated */
1212 size_t len) /* metadata length */
1213{
1214 struct lttng_ust_lib_ring_buffer_ctx ctx;
1215 struct lttng_channel *chan = channel->chan;
1216 const char *str = metadata_str;
1217 int ret = 0, waitret;
1218 size_t reserve_len, pos;
1219
1220 for (pos = 0; pos < len; pos += reserve_len) {
1221 reserve_len = min_t(size_t,
1222 chan->ops->packet_avail_size(chan->chan, chan->handle),
1223 len - pos);
1224 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
53569322 1225 sizeof(char), -1, chan->handle, NULL);
c9023c93
MD
1226 /*
1227 * We don't care about metadata buffer's records lost
1228 * count, because we always retry here. Report error if
1229 * we need to bail out after timeout or being
1230 * interrupted.
1231 */
1232 waitret = wait_cond_interruptible_timeout(
1233 ({
1234 ret = chan->ops->event_reserve(&ctx, 0);
1235 ret != -ENOBUFS || !ret;
1236 }),
1237 LTTNG_METADATA_TIMEOUT_MSEC);
1238 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1239 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1240 waitret == -EINTR ? "interrupted" :
1241 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1242 if (waitret == -EINTR)
1243 ret = waitret;
1244 goto end;
1245 }
1246 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1247 chan->ops->event_commit(&ctx);
1248 }
1249end:
1250 return ret;
1251}
1252
3ef94b0e
JD
1253/*
1254 * Write at most one packet in the channel.
1255 * Returns the number of bytes written on success, < 0 on error.
1256 */
1257ssize_t ustctl_write_one_packet_to_channel(
1258 struct ustctl_consumer_channel *channel,
1259 const char *metadata_str, /* NOT null-terminated */
1260 size_t len) /* metadata length */
1261{
1262 struct lttng_ust_lib_ring_buffer_ctx ctx;
1263 struct lttng_channel *chan = channel->chan;
1264 const char *str = metadata_str;
1265 ssize_t reserve_len;
1266 int ret;
1267
1268 reserve_len = min_t(ssize_t,
1269 chan->ops->packet_avail_size(chan->chan, chan->handle),
1270 len);
1271 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
53569322 1272 sizeof(char), -1, chan->handle, NULL);
3ef94b0e
JD
1273 ret = chan->ops->event_reserve(&ctx, 0);
1274 if (ret != 0) {
1275 DBG("LTTng: event reservation failed");
1276 assert(ret < 0);
1277 reserve_len = ret;
1278 goto end;
1279 }
1280 chan->ops->event_write(&ctx, str, reserve_len);
1281 chan->ops->event_commit(&ctx);
1282
1283end:
1284 return reserve_len;
1285}
1286
ff0f5728
MD
1287int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1288{
1289 struct channel *chan;
cb7378b3 1290 int ret;
ff0f5728
MD
1291
1292 chan = consumer_chan->chan->chan;
cb7378b3 1293 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
ff0f5728 1294 chan, chan->handle);
cb7378b3
MD
1295 if (!ret)
1296 consumer_chan->wait_fd = -1;
1297 return ret;
ff0f5728
MD
1298}
1299
1300int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1301{
1302 struct channel *chan;
cb7378b3 1303 int ret;
ff0f5728
MD
1304
1305 chan = consumer_chan->chan->chan;
cb7378b3 1306 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
ff0f5728 1307 chan, chan->handle);
cb7378b3
MD
1308 if (!ret)
1309 consumer_chan->wakeup_fd = -1;
1310 return ret;
ff0f5728
MD
1311}
1312
74d81a6c 1313int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
1314{
1315 struct channel *chan;
1316
74d81a6c 1317 chan = stream->chan->chan->chan;
ff0f5728 1318 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
74d81a6c 1319 chan, stream->handle, stream->cpu);
5224b5c8
MD
1320}
1321
74d81a6c 1322int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 1323{
66bdd22a 1324 struct channel *chan;
74d81a6c
MD
1325
1326 chan = stream->chan->chan->chan;
ff0f5728 1327 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
74d81a6c
MD
1328 chan, stream->handle, stream->cpu);
1329}
1330
1331struct ustctl_consumer_stream *
1332 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1333 int cpu)
1334{
1335 struct ustctl_consumer_stream *stream;
1336 struct lttng_ust_shm_handle *handle;
1337 struct channel *chan;
1338 int shm_fd, wait_fd, wakeup_fd;
1339 uint64_t memory_map_size;
4cfec15c 1340 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
1341 int ret;
1342
74d81a6c
MD
1343 if (!channel)
1344 return NULL;
1345 handle = channel->chan->handle;
9bfc503d
MD
1346 if (!handle)
1347 return NULL;
1348
74d81a6c 1349 chan = channel->chan->chan;
6e922b24 1350 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
1351 chan, cpu, handle, &shm_fd, &wait_fd,
1352 &wakeup_fd, &memory_map_size);
6e922b24
MD
1353 if (!buf)
1354 return NULL;
74d81a6c 1355 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
1356 if (ret)
1357 return NULL;
74d81a6c
MD
1358
1359 stream = zmalloc(sizeof(*stream));
1360 if (!stream)
1361 goto alloc_error;
1362 stream->handle = handle;
1363 stream->buf = buf;
1364 stream->chan = channel;
1365 stream->shm_fd = shm_fd;
1366 stream->wait_fd = wait_fd;
1367 stream->wakeup_fd = wakeup_fd;
1368 stream->memory_map_size = memory_map_size;
1369 stream->cpu = cpu;
1370 return stream;
1371
1372alloc_error:
1373 return NULL;
1374}
1375
1376void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1377{
1378 struct lttng_ust_lib_ring_buffer *buf;
1379 struct ustctl_consumer_channel *consumer_chan;
1380
1381 assert(stream);
1382 buf = stream->buf;
1383 consumer_chan = stream->chan;
b24e4e91
MD
1384 (void) ustctl_stream_close_wait_fd(stream);
1385 (void) ustctl_stream_close_wakeup_fd(stream);
74d81a6c
MD
1386 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1387 free(stream);
6e922b24
MD
1388}
1389
ff0f5728
MD
1390int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1391{
1392 if (!chan)
1393 return -EINVAL;
1394 return shm_get_wait_fd(chan->chan->handle,
1395 &chan->chan->handle->chan._ref);
1396}
1397
1398int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1399{
1400 if (!chan)
1401 return -EINVAL;
1402 return shm_get_wakeup_fd(chan->chan->handle,
1403 &chan->chan->handle->chan._ref);
1404}
1405
1406int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1407{
74d81a6c
MD
1408 struct lttng_ust_lib_ring_buffer *buf;
1409 struct ustctl_consumer_channel *consumer_chan;
1410
1411 if (!stream)
1412 return -EINVAL;
1413 buf = stream->buf;
1414 consumer_chan = stream->chan;
1415 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1416}
1417
ff0f5728 1418int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1419{
1420 struct lttng_ust_lib_ring_buffer *buf;
1421 struct ustctl_consumer_channel *consumer_chan;
1422
1423 if (!stream)
1424 return -EINVAL;
1425 buf = stream->buf;
1426 consumer_chan = stream->chan;
1427 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
6e922b24
MD
1428}
1429
57773204
MD
1430/* For mmap mode, readable without "get" operation */
1431
74d81a6c 1432void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1433{
74d81a6c
MD
1434 struct lttng_ust_lib_ring_buffer *buf;
1435 struct ustctl_consumer_channel *consumer_chan;
1436
1437 if (!stream)
9bfc503d 1438 return NULL;
74d81a6c
MD
1439 buf = stream->buf;
1440 consumer_chan = stream->chan;
1441 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
1442}
1443
57773204 1444/* returns the length to mmap. */
74d81a6c 1445int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1446 unsigned long *len)
1447{
74d81a6c 1448 struct ustctl_consumer_channel *consumer_chan;
57773204 1449 unsigned long mmap_buf_len;
66bdd22a 1450 struct channel *chan;
57773204 1451
74d81a6c 1452 if (!stream)
9bfc503d 1453 return -EINVAL;
74d81a6c
MD
1454 consumer_chan = stream->chan;
1455 chan = consumer_chan->chan->chan;
57773204
MD
1456 if (chan->backend.config.output != RING_BUFFER_MMAP)
1457 return -EINVAL;
1458 mmap_buf_len = chan->backend.buf_size;
1459 if (chan->backend.extra_reader_sb)
1460 mmap_buf_len += chan->backend.subbuf_size;
1461 if (mmap_buf_len > INT_MAX)
1462 return -EFBIG;
1463 *len = mmap_buf_len;
1464 return 0;
1465}
1466
1467/* returns the maximum size for sub-buffers. */
74d81a6c 1468int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1469 unsigned long *len)
1470{
74d81a6c 1471 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1472 struct channel *chan;
57773204 1473
74d81a6c 1474 if (!stream)
9bfc503d 1475 return -EINVAL;
74d81a6c
MD
1476 consumer_chan = stream->chan;
1477 chan = consumer_chan->chan->chan;
57773204
MD
1478 *len = chan->backend.subbuf_size;
1479 return 0;
1480}
1481
1482/*
1483 * For mmap mode, operate on the current packet (between get/put or
1484 * get_next/put_next).
1485 */
1486
1487/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1488int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1489 unsigned long *off)
57773204 1490{
66bdd22a 1491 struct channel *chan;
57773204 1492 unsigned long sb_bindex;
74d81a6c
MD
1493 struct lttng_ust_lib_ring_buffer *buf;
1494 struct ustctl_consumer_channel *consumer_chan;
34daae3e
MD
1495 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx;
1496 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
57773204 1497
74d81a6c 1498 if (!stream)
9bfc503d 1499 return -EINVAL;
74d81a6c
MD
1500 buf = stream->buf;
1501 consumer_chan = stream->chan;
1502 chan = consumer_chan->chan->chan;
57773204
MD
1503 if (chan->backend.config.output != RING_BUFFER_MMAP)
1504 return -EINVAL;
1505 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
32ce8569 1506 buf->backend.buf_rsb.id);
34daae3e
MD
1507 barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array,
1508 sb_bindex);
1509 if (!barray_idx)
1510 return -EINVAL;
1511 pages = shmp(consumer_chan->chan->handle, barray_idx->shmp);
1512 if (!pages)
1513 return -EINVAL;
1514 *off = pages->mmap_offset;
57773204
MD
1515 return 0;
1516}
1517
1518/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1519int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1520 unsigned long *len)
57773204 1521{
74d81a6c 1522 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1523 struct channel *chan;
74d81a6c 1524 struct lttng_ust_lib_ring_buffer *buf;
57773204 1525
74d81a6c 1526 if (!stream)
9bfc503d
MD
1527 return -EINVAL;
1528
74d81a6c
MD
1529 buf = stream->buf;
1530 consumer_chan = stream->chan;
1531 chan = consumer_chan->chan->chan;
57773204 1532 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1533 consumer_chan->chan->handle);
57773204
MD
1534 return 0;
1535}
1536
1537/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1538int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1539 unsigned long *len)
57773204 1540{
74d81a6c 1541 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1542 struct channel *chan;
74d81a6c 1543 struct lttng_ust_lib_ring_buffer *buf;
57773204 1544
74d81a6c 1545 if (!stream)
9bfc503d 1546 return -EINVAL;
74d81a6c
MD
1547 buf = stream->buf;
1548 consumer_chan = stream->chan;
1549 chan = consumer_chan->chan->chan;
57773204 1550 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1551 consumer_chan->chan->handle);
b72687b8 1552 *len = LTTNG_UST_PAGE_ALIGN(*len);
57773204
MD
1553 return 0;
1554}
1555
1556/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1557int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1558{
74d81a6c
MD
1559 struct lttng_ust_lib_ring_buffer *buf;
1560 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1561
74d81a6c
MD
1562 if (!stream)
1563 return -EINVAL;
1564 buf = stream->buf;
1565 consumer_chan = stream->chan;
1566 return lib_ring_buffer_get_next_subbuf(buf,
1567 consumer_chan->chan->handle);
57773204
MD
1568}
1569
1570
1571/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1572int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1573{
74d81a6c
MD
1574 struct lttng_ust_lib_ring_buffer *buf;
1575 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1576
74d81a6c
MD
1577 if (!stream)
1578 return -EINVAL;
1579 buf = stream->buf;
1580 consumer_chan = stream->chan;
1581 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1582 return 0;
1583}
1584
1585/* snapshot */
1586
1587/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1588int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1589{
74d81a6c
MD
1590 struct lttng_ust_lib_ring_buffer *buf;
1591 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1592
74d81a6c
MD
1593 if (!stream)
1594 return -EINVAL;
1595 buf = stream->buf;
1596 consumer_chan = stream->chan;
57773204 1597 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1598 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1599}
1600
f45930b7
JG
1601/*
1602 * Get a snapshot of the current ring buffer producer and consumer positions
1603 * even if the consumed and produced positions are contained within the same
1604 * subbuffer.
1605 */
1606int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream)
1607{
1608 struct lttng_ust_lib_ring_buffer *buf;
1609 struct ustctl_consumer_channel *consumer_chan;
1610
1611 if (!stream)
1612 return -EINVAL;
1613 buf = stream->buf;
1614 consumer_chan = stream->chan;
1615 return lib_ring_buffer_snapshot_sample_positions(buf,
1616 &buf->cons_snapshot, &buf->prod_snapshot,
1617 consumer_chan->chan->handle);
1618}
1619
57773204 1620/* Get the consumer position (iteration start) */
74d81a6c
MD
1621int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1622 unsigned long *pos)
57773204 1623{
74d81a6c 1624 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1625
74d81a6c
MD
1626 if (!stream)
1627 return -EINVAL;
1628 buf = stream->buf;
57773204
MD
1629 *pos = buf->cons_snapshot;
1630 return 0;
1631}
1632
1633/* Get the producer position (iteration end) */
74d81a6c
MD
1634int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1635 unsigned long *pos)
57773204 1636{
74d81a6c 1637 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1638
74d81a6c
MD
1639 if (!stream)
1640 return -EINVAL;
1641 buf = stream->buf;
57773204
MD
1642 *pos = buf->prod_snapshot;
1643 return 0;
1644}
1645
1646/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1647int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1648 unsigned long *pos)
57773204 1649{
74d81a6c
MD
1650 struct lttng_ust_lib_ring_buffer *buf;
1651 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1652
74d81a6c
MD
1653 if (!stream)
1654 return -EINVAL;
1655 buf = stream->buf;
1656 consumer_chan = stream->chan;
1657 return lib_ring_buffer_get_subbuf(buf, *pos,
1658 consumer_chan->chan->handle);
57773204
MD
1659}
1660
1661/* Release exclusive sub-buffer access */
74d81a6c 1662int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1663{
74d81a6c
MD
1664 struct lttng_ust_lib_ring_buffer *buf;
1665 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1666
74d81a6c
MD
1667 if (!stream)
1668 return -EINVAL;
1669 buf = stream->buf;
1670 consumer_chan = stream->chan;
1671 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1672 return 0;
1673}
1674
74d81a6c 1675void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1676 int producer_active)
57773204 1677{
74d81a6c
MD
1678 struct lttng_ust_lib_ring_buffer *buf;
1679 struct ustctl_consumer_channel *consumer_chan;
1680
1681 assert(stream);
1682 buf = stream->buf;
1683 consumer_chan = stream->chan;
b52190f2
MD
1684 lib_ring_buffer_switch_slow(buf,
1685 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
1686 consumer_chan->chan->handle);
1687}
1688
beca55a1
MD
1689void ustctl_clear_buffer(struct ustctl_consumer_stream *stream)
1690{
1691 struct lttng_ust_lib_ring_buffer *buf;
1692 struct ustctl_consumer_channel *consumer_chan;
1693
1694 assert(stream);
1695 buf = stream->buf;
1696 consumer_chan = stream->chan;
1697 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
1698 consumer_chan->chan->handle);
1699 lib_ring_buffer_clear_reader(buf, consumer_chan->chan->handle);
1700}
1701
b2f3252a
JD
1702static
1703struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1704 struct lttng_ust_lib_ring_buffer *buf,
1705 struct lttng_ust_shm_handle *handle)
1706{
1707 struct channel *chan;
1708 const struct lttng_ust_lib_ring_buffer_config *config;
1709 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1710
1711 chan = shmp(handle, buf->backend.chan);
34daae3e
MD
1712 if (!chan)
1713 return NULL;
b2f3252a
JD
1714 config = &chan->backend.config;
1715 if (!config->cb_ptr)
1716 return NULL;
1717 client_cb = caa_container_of(config->cb_ptr,
1718 struct lttng_ust_client_lib_ring_buffer_client_cb,
1719 parent);
1720 return client_cb;
1721}
1722
1723int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1724 uint64_t *timestamp_begin)
1725{
1726 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1727 struct lttng_ust_lib_ring_buffer *buf;
1728 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1729
1730 if (!stream || !timestamp_begin)
1731 return -EINVAL;
e1919a41
MD
1732 buf = stream->buf;
1733 handle = stream->chan->chan->handle;
b2f3252a
JD
1734 client_cb = get_client_cb(buf, handle);
1735 if (!client_cb)
1736 return -ENOSYS;
1737 return client_cb->timestamp_begin(buf, handle, timestamp_begin);
1738}
1739
1740int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1741 uint64_t *timestamp_end)
1742{
1743 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1744 struct lttng_ust_lib_ring_buffer *buf;
1745 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1746
1747 if (!stream || !timestamp_end)
1748 return -EINVAL;
e1919a41
MD
1749 buf = stream->buf;
1750 handle = stream->chan->chan->handle;
b2f3252a
JD
1751 client_cb = get_client_cb(buf, handle);
1752 if (!client_cb)
1753 return -ENOSYS;
1754 return client_cb->timestamp_end(buf, handle, timestamp_end);
1755}
1756
1757int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1758 uint64_t *events_discarded)
1759{
1760 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1761 struct lttng_ust_lib_ring_buffer *buf;
1762 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1763
1764 if (!stream || !events_discarded)
1765 return -EINVAL;
e1919a41
MD
1766 buf = stream->buf;
1767 handle = stream->chan->chan->handle;
b2f3252a
JD
1768 client_cb = get_client_cb(buf, handle);
1769 if (!client_cb)
1770 return -ENOSYS;
1771 return client_cb->events_discarded(buf, handle, events_discarded);
1772}
1773
1774int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1775 uint64_t *content_size)
1776{
1777 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1778 struct lttng_ust_lib_ring_buffer *buf;
1779 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1780
1781 if (!stream || !content_size)
1782 return -EINVAL;
e1919a41
MD
1783 buf = stream->buf;
1784 handle = stream->chan->chan->handle;
b2f3252a
JD
1785 client_cb = get_client_cb(buf, handle);
1786 if (!client_cb)
1787 return -ENOSYS;
1788 return client_cb->content_size(buf, handle, content_size);
1789}
1790
1791int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1792 uint64_t *packet_size)
1793{
1794 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1795 struct lttng_ust_lib_ring_buffer *buf;
1796 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1797
1798 if (!stream || !packet_size)
1799 return -EINVAL;
e1919a41
MD
1800 buf = stream->buf;
1801 handle = stream->chan->chan->handle;
b2f3252a
JD
1802 client_cb = get_client_cb(buf, handle);
1803 if (!client_cb)
1804 return -ENOSYS;
1805 return client_cb->packet_size(buf, handle, packet_size);
1806}
1807
1808int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1809 uint64_t *stream_id)
1810{
1811 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1812 struct lttng_ust_lib_ring_buffer *buf;
1813 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1814
1815 if (!stream || !stream_id)
1816 return -EINVAL;
e1919a41
MD
1817 buf = stream->buf;
1818 handle = stream->chan->chan->handle;
b2f3252a
JD
1819 client_cb = get_client_cb(buf, handle);
1820 if (!client_cb)
1821 return -ENOSYS;
1822 return client_cb->stream_id(buf, handle, stream_id);
1823}
1824
fca361e8
JD
1825int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1826 uint64_t *ts)
1827{
1828 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1829 struct lttng_ust_lib_ring_buffer *buf;
1830 struct lttng_ust_shm_handle *handle;
fca361e8
JD
1831
1832 if (!stream || !ts)
1833 return -EINVAL;
e1919a41
MD
1834 buf = stream->buf;
1835 handle = stream->chan->chan->handle;
fca361e8
JD
1836 client_cb = get_client_cb(buf, handle);
1837 if (!client_cb || !client_cb->current_timestamp)
1838 return -ENOSYS;
1839 return client_cb->current_timestamp(buf, handle, ts);
1840}
1841
1ff31389
JD
1842int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream,
1843 uint64_t *seq)
1844{
1845 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1846 struct lttng_ust_lib_ring_buffer *buf;
1847 struct lttng_ust_shm_handle *handle;
1848
1849 if (!stream || !seq)
1850 return -EINVAL;
1851 buf = stream->buf;
1852 handle = stream->chan->chan->handle;
1853 client_cb = get_client_cb(buf, handle);
1854 if (!client_cb || !client_cb->sequence_number)
1855 return -ENOSYS;
1856 return client_cb->sequence_number(buf, handle, seq);
1857}
1858
45a00b05
JD
1859int ustctl_get_instance_id(struct ustctl_consumer_stream *stream,
1860 uint64_t *id)
1861{
1862 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1863 struct lttng_ust_lib_ring_buffer *buf;
1864 struct lttng_ust_shm_handle *handle;
1865
1866 if (!stream || !id)
1867 return -EINVAL;
1868 buf = stream->buf;
1869 handle = stream->chan->chan->handle;
1870 client_cb = get_client_cb(buf, handle);
1871 if (!client_cb)
1872 return -ENOSYS;
1873 return client_cb->instance_id(buf, handle, id);
1874}
1875
c62a3816 1876#ifdef LTTNG_UST_HAVE_PERF_EVENT
57201bb3
MD
1877
1878int ustctl_has_perf_counters(void)
1879{
1880 return 1;
1881}
1882
1883#else
1884
1885int ustctl_has_perf_counters(void)
1886{
1887 return 0;
1888}
1889
1890#endif
1891
32ce8569
MD
1892/*
1893 * Returns 0 on success, negative error value on error.
1894 */
1895int ustctl_recv_reg_msg(int sock,
1896 enum ustctl_socket_type *type,
1897 uint32_t *major,
1898 uint32_t *minor,
1899 uint32_t *pid,
1900 uint32_t *ppid,
1901 uint32_t *uid,
1902 uint32_t *gid,
1903 uint32_t *bits_per_long,
1904 uint32_t *uint8_t_alignment,
1905 uint32_t *uint16_t_alignment,
1906 uint32_t *uint32_t_alignment,
1907 uint32_t *uint64_t_alignment,
1908 uint32_t *long_alignment,
1909 int *byte_order,
1910 char *name)
1911{
1912 ssize_t len;
1913 struct ustctl_reg_msg reg_msg;
1914
1915 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1916 if (len > 0 && len != sizeof(reg_msg))
1917 return -EIO;
1918 if (len == 0)
1919 return -EPIPE;
1920 if (len < 0)
1921 return len;
1922
1923 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1924 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1925 BIG_ENDIAN : LITTLE_ENDIAN;
1926 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1927 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1928 LITTLE_ENDIAN : BIG_ENDIAN;
1929 } else {
1930 return -LTTNG_UST_ERR_INVAL_MAGIC;
1931 }
1932 switch (reg_msg.socket_type) {
1933 case 0: *type = USTCTL_SOCKET_CMD;
1934 break;
1935 case 1: *type = USTCTL_SOCKET_NOTIFY;
1936 break;
1937 default:
1938 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1939 }
1940 *major = reg_msg.major;
1941 *minor = reg_msg.minor;
1942 *pid = reg_msg.pid;
1943 *ppid = reg_msg.ppid;
1944 *uid = reg_msg.uid;
1945 *gid = reg_msg.gid;
1946 *bits_per_long = reg_msg.bits_per_long;
1947 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1948 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1949 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1950 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1951 *long_alignment = reg_msg.long_alignment;
1952 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
6a359b8a
MD
1953 if (reg_msg.major < LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE ||
1954 reg_msg.major > LTTNG_UST_ABI_MAJOR_VERSION) {
32ce8569
MD
1955 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1956 }
1957
1958 return 0;
1959}
1960
1961int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1962{
1963 struct ustcomm_notify_hdr header;
1964 ssize_t len;
1965
1966 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1967 if (len > 0 && len != sizeof(header))
1968 return -EIO;
1969 if (len == 0)
1970 return -EPIPE;
1971 if (len < 0)
1972 return len;
1973 switch (header.notify_cmd) {
1974 case 0:
1975 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1976 break;
1977 case 1:
1978 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1979 break;
c785c634
MD
1980 case 2:
1981 *notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1982 break;
32ce8569
MD
1983 default:
1984 return -EINVAL;
1985 }
1986 return 0;
1987}
1988
1989/*
1990 * Returns 0 on success, negative error value on error.
1991 */
1992int ustctl_recv_register_event(int sock,
1993 int *session_objd,
1994 int *channel_objd,
1995 char *event_name,
1996 int *loglevel,
1997 char **signature,
1998 size_t *nr_fields,
1999 struct ustctl_field **fields,
2000 char **model_emf_uri)
2001{
2002 ssize_t len;
2003 struct ustcomm_notify_event_msg msg;
2004 size_t signature_len, fields_len, model_emf_uri_len;
2005 char *a_sign = NULL, *a_model_emf_uri = NULL;
2006 struct ustctl_field *a_fields = NULL;
2007
2008 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2009 if (len > 0 && len != sizeof(msg))
2010 return -EIO;
2011 if (len == 0)
2012 return -EPIPE;
2013 if (len < 0)
2014 return len;
2015
2016 *session_objd = msg.session_objd;
2017 *channel_objd = msg.channel_objd;
2018 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
2019 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2020 *loglevel = msg.loglevel;
2021 signature_len = msg.signature_len;
2022 fields_len = msg.fields_len;
2023
2024 if (fields_len % sizeof(*a_fields) != 0) {
2025 return -EINVAL;
2026 }
2027
2028 model_emf_uri_len = msg.model_emf_uri_len;
2029
2030 /* recv signature. contains at least \0. */
2031 a_sign = zmalloc(signature_len);
2032 if (!a_sign)
2033 return -ENOMEM;
2034 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
2035 if (len > 0 && len != signature_len) {
2036 len = -EIO;
2037 goto signature_error;
2038 }
2039 if (len == 0) {
2040 len = -EPIPE;
2041 goto signature_error;
2042 }
2043 if (len < 0) {
2044 goto signature_error;
2045 }
2046 /* Enforce end of string */
111198c2 2047 a_sign[signature_len - 1] = '\0';
32ce8569
MD
2048
2049 /* recv fields */
2050 if (fields_len) {
2051 a_fields = zmalloc(fields_len);
2052 if (!a_fields) {
2053 len = -ENOMEM;
2054 goto signature_error;
2055 }
2056 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2057 if (len > 0 && len != fields_len) {
2058 len = -EIO;
2059 goto fields_error;
2060 }
2061 if (len == 0) {
2062 len = -EPIPE;
2063 goto fields_error;
2064 }
2065 if (len < 0) {
2066 goto fields_error;
2067 }
2068 }
2069
2070 if (model_emf_uri_len) {
2071 /* recv model_emf_uri_len */
2072 a_model_emf_uri = zmalloc(model_emf_uri_len);
2073 if (!a_model_emf_uri) {
2074 len = -ENOMEM;
2075 goto fields_error;
2076 }
2077 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
2078 model_emf_uri_len);
2079 if (len > 0 && len != model_emf_uri_len) {
2080 len = -EIO;
2081 goto model_error;
2082 }
2083 if (len == 0) {
2084 len = -EPIPE;
2085 goto model_error;
2086 }
2087 if (len < 0) {
2088 goto model_error;
2089 }
2090 /* Enforce end of string */
2091 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
2092 }
2093
2094 *signature = a_sign;
2095 *nr_fields = fields_len / sizeof(*a_fields);
2096 *fields = a_fields;
2097 *model_emf_uri = a_model_emf_uri;
2098
2099 return 0;
2100
2101model_error:
2102 free(a_model_emf_uri);
2103fields_error:
2104 free(a_fields);
2105signature_error:
2106 free(a_sign);
2107 return len;
2108}
2109
2110/*
2111 * Returns 0 on success, negative error value on error.
2112 */
2113int ustctl_reply_register_event(int sock,
2114 uint32_t id,
2115 int ret_code)
2116{
2117 ssize_t len;
2118 struct {
2119 struct ustcomm_notify_hdr header;
2120 struct ustcomm_notify_event_reply r;
2121 } reply;
2122
2123 memset(&reply, 0, sizeof(reply));
2124 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2125 reply.r.ret_code = ret_code;
2126 reply.r.event_id = id;
2127 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2128 if (len > 0 && len != sizeof(reply))
2129 return -EIO;
2130 if (len < 0)
2131 return len;
2132 return 0;
2133}
2134
c785c634
MD
2135/*
2136 * Returns 0 on success, negative UST or system error value on error.
2137 */
2138int ustctl_recv_register_enum(int sock,
2139 int *session_objd,
2140 char *enum_name,
2141 struct ustctl_enum_entry **entries,
2142 size_t *nr_entries)
2143{
2144 ssize_t len;
2145 struct ustcomm_notify_enum_msg msg;
2146 size_t entries_len;
2147 struct ustctl_enum_entry *a_entries = NULL;
2148
2149 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2150 if (len > 0 && len != sizeof(msg))
2151 return -EIO;
2152 if (len == 0)
2153 return -EPIPE;
2154 if (len < 0)
2155 return len;
2156
2157 *session_objd = msg.session_objd;
2158 strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN);
2159 enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2160 entries_len = msg.entries_len;
2161
2162 if (entries_len % sizeof(*a_entries) != 0) {
2163 return -EINVAL;
2164 }
2165
2166 /* recv entries */
2167 if (entries_len) {
2168 a_entries = zmalloc(entries_len);
2169 if (!a_entries)
2170 return -ENOMEM;
2171 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2172 if (len > 0 && len != entries_len) {
2173 len = -EIO;
2174 goto entries_error;
2175 }
2176 if (len == 0) {
2177 len = -EPIPE;
2178 goto entries_error;
2179 }
2180 if (len < 0) {
2181 goto entries_error;
2182 }
2183 }
2184 *nr_entries = entries_len / sizeof(*a_entries);
2185 *entries = a_entries;
2186
2187 return 0;
2188
2189entries_error:
2190 free(a_entries);
2191 return len;
2192}
2193
2194/*
2195 * Returns 0 on success, negative error value on error.
2196 */
2197int ustctl_reply_register_enum(int sock,
2198 uint64_t id,
2199 int ret_code)
2200{
2201 ssize_t len;
2202 struct {
2203 struct ustcomm_notify_hdr header;
2204 struct ustcomm_notify_enum_reply r;
2205 } reply;
2206
2207 memset(&reply, 0, sizeof(reply));
2208 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2209 reply.r.ret_code = ret_code;
2210 reply.r.enum_id = id;
2211 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2212 if (len > 0 && len != sizeof(reply))
2213 return -EIO;
2214 if (len < 0)
2215 return len;
2216 return 0;
2217}
2218
32ce8569
MD
2219/*
2220 * Returns 0 on success, negative UST or system error value on error.
2221 */
2222int ustctl_recv_register_channel(int sock,
2223 int *session_objd, /* session descriptor (output) */
2224 int *channel_objd, /* channel descriptor (output) */
2225 size_t *nr_fields,
2226 struct ustctl_field **fields)
2227{
2228 ssize_t len;
2229 struct ustcomm_notify_channel_msg msg;
2230 size_t fields_len;
2231 struct ustctl_field *a_fields;
2232
2233 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2234 if (len > 0 && len != sizeof(msg))
2235 return -EIO;
2236 if (len == 0)
2237 return -EPIPE;
2238 if (len < 0)
2239 return len;
2240
2241 *session_objd = msg.session_objd;
2242 *channel_objd = msg.channel_objd;
2243 fields_len = msg.ctx_fields_len;
2244
2245 if (fields_len % sizeof(*a_fields) != 0) {
2246 return -EINVAL;
2247 }
2248
2249 /* recv fields */
2250 if (fields_len) {
2251 a_fields = zmalloc(fields_len);
2252 if (!a_fields) {
2253 len = -ENOMEM;
2254 goto alloc_error;
2255 }
2256 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2257 if (len > 0 && len != fields_len) {
2258 len = -EIO;
2259 goto fields_error;
2260 }
2261 if (len == 0) {
2262 len = -EPIPE;
2263 goto fields_error;
2264 }
2265 if (len < 0) {
2266 goto fields_error;
2267 }
2268 *fields = a_fields;
2269 } else {
2270 *fields = NULL;
2271 }
2272 *nr_fields = fields_len / sizeof(*a_fields);
2273 return 0;
2274
2275fields_error:
2276 free(a_fields);
2277alloc_error:
2278 return len;
2279}
2280
2281/*
2282 * Returns 0 on success, negative error value on error.
2283 */
2284int ustctl_reply_register_channel(int sock,
2285 uint32_t chan_id,
2286 enum ustctl_channel_header header_type,
2287 int ret_code)
2288{
2289 ssize_t len;
2290 struct {
2291 struct ustcomm_notify_hdr header;
2292 struct ustcomm_notify_channel_reply r;
2293 } reply;
2294
2295 memset(&reply, 0, sizeof(reply));
2296 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2297 reply.r.ret_code = ret_code;
2298 reply.r.chan_id = chan_id;
2299 switch (header_type) {
2300 case USTCTL_CHANNEL_HEADER_COMPACT:
2301 reply.r.header_type = 1;
2302 break;
2303 case USTCTL_CHANNEL_HEADER_LARGE:
2304 reply.r.header_type = 2;
2305 break;
2306 default:
2307 reply.r.header_type = 0;
2308 break;
2309 }
2310 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2311 if (len > 0 && len != sizeof(reply))
2312 return -EIO;
2313 if (len < 0)
2314 return len;
2315 return 0;
2316}
2317
f53329f3
JD
2318/* Regenerate the statedump. */
2319int ustctl_regenerate_statedump(int sock, int handle)
2320{
2321 struct ustcomm_ust_msg lum;
2322 struct ustcomm_ust_reply lur;
2323 int ret;
2324
2325 memset(&lum, 0, sizeof(lum));
2326 lum.handle = handle;
2327 lum.cmd = LTTNG_UST_SESSION_STATEDUMP;
2328 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2329 if (ret)
2330 return ret;
2331 DBG("Regenerated statedump for handle %u", handle);
2332 return 0;
2333}
2334
74d81a6c
MD
2335static __attribute__((constructor))
2336void ustctl_init(void)
2337{
2338 init_usterr();
6f626d28 2339 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
f9364363 2340 lttng_ust_clock_init();
74d81a6c
MD
2341 lttng_ring_buffer_metadata_client_init();
2342 lttng_ring_buffer_client_overwrite_init();
34a91bdb 2343 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 2344 lttng_ring_buffer_client_discard_init();
34a91bdb 2345 lttng_ring_buffer_client_discard_rt_init();
03d2d293 2346 lib_ringbuffer_signal_init();
74d81a6c
MD
2347}
2348
2349static __attribute__((destructor))
2350void ustctl_exit(void)
2351{
34a91bdb 2352 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 2353 lttng_ring_buffer_client_discard_exit();
34a91bdb 2354 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
2355 lttng_ring_buffer_client_overwrite_exit();
2356 lttng_ring_buffer_metadata_client_exit();
57773204 2357}
This page took 0.135064 seconds and 4 git commands to generate.