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