Cleanup: remove duplicate check for 0 num_subbuf and subbuf_size
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
9d335227 20#define _GNU_SOURCE
57773204 21#include <string.h>
4318ae1b
MD
22#include <lttng/ust-ctl.h>
23#include <lttng/ust-abi.h>
c1fca457 24#include <lttng/ust-events.h>
7a784989 25#include <sys/mman.h>
44c72f10
MD
26
27#include <usterr-signal-safe.h>
b728d87e 28#include <ust-comm.h>
57773204
MD
29
30#include "../libringbuffer/backend.h"
31#include "../libringbuffer/frontend.h"
32
6b120308
MD
33volatile enum ust_loglevel ust_loglevel;
34
57773204 35static
61f02aea 36void init_object(struct lttng_ust_object_data *data)
57773204
MD
37{
38 data->handle = -1;
39 data->shm_fd = -1;
40 data->wait_fd = -1;
41 data->memory_map_size = 0;
42}
43
2be0e72c
MD
44int ustctl_release_handle(int sock, int handle)
45{
46 struct ustcomm_ust_msg lum;
47 struct ustcomm_ust_reply lur;
48 int ret;
49
50 if (sock >= 0) {
51 memset(&lum, 0, sizeof(lum));
52 lum.handle = handle;
53 lum.cmd = LTTNG_UST_RELEASE;
54 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
55 if (ret < 0) {
56 return ret;
57 }
58 }
59 return 0;
60}
12388166
MD
61/*
62 * If sock is negative, it means we don't have to notify the other side
63 * (e.g. application has already vanished).
64 */
d26228ae 65int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 66{
57773204
MD
67 int ret;
68
9bfc503d
MD
69 if (!data)
70 return -EINVAL;
71
d26228ae
MD
72 if (data->shm_fd >= 0) {
73 ret = close(data->shm_fd);
74 if (ret < 0) {
75 return ret;
76 }
77 }
78 if (data->wait_fd >= 0) {
79 ret = close(data->wait_fd);
80 if (ret < 0) {
81 return ret;
82 }
83 }
2be0e72c 84 return ustctl_release_handle(sock, data->handle);
57773204
MD
85}
86
1c5e467e
MD
87/*
88 * Send registration done packet to the application.
89 */
90int ustctl_register_done(int sock)
91{
92 struct ustcomm_ust_msg lum;
93 struct ustcomm_ust_reply lur;
94 int ret;
95
96 DBG("Sending register done command to %d", sock);
97 memset(&lum, 0, sizeof(lum));
98 lum.handle = LTTNG_UST_ROOT_HANDLE;
99 lum.cmd = LTTNG_UST_REGISTER_DONE;
100 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
101 if (ret)
102 return ret;
103 if (lur.ret_code != USTCOMM_OK) {
104 DBG("Return code: %s", ustcomm_get_readable_code(lur.ret_code));
105 goto error;
106 }
107 return 0;
108
109error:
110 return -1;
111}
112
57773204
MD
113/*
114 * returns session handle.
115 */
116int ustctl_create_session(int sock)
117{
118 struct ustcomm_ust_msg lum;
119 struct ustcomm_ust_reply lur;
120 int ret, session_handle;
121
122 /* Create session */
123 memset(&lum, 0, sizeof(lum));
124 lum.handle = LTTNG_UST_ROOT_HANDLE;
125 lum.cmd = LTTNG_UST_SESSION;
126 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
127 if (ret)
128 return ret;
129 session_handle = lur.ret_val;
130 DBG("received session handle %u", session_handle);
131 return session_handle;
132}
133
134/* open the metadata global channel */
135int ustctl_open_metadata(int sock, int session_handle,
136 struct lttng_ust_channel_attr *chops,
61f02aea 137 struct lttng_ust_object_data **_metadata_data)
57773204
MD
138{
139 struct ustcomm_ust_msg lum;
140 struct ustcomm_ust_reply lur;
61f02aea 141 struct lttng_ust_object_data *metadata_data;
eeee05f3 142 int ret, err = 0;
57773204 143
9bfc503d
MD
144 if (!chops || !_metadata_data)
145 return -EINVAL;
146
57773204
MD
147 metadata_data = malloc(sizeof(*metadata_data));
148 if (!metadata_data)
149 return -ENOMEM;
150 init_object(metadata_data);
151 /* Create metadata channel */
152 memset(&lum, 0, sizeof(lum));
153 lum.handle = session_handle;
154 lum.cmd = LTTNG_UST_METADATA;
155 lum.u.channel.overwrite = chops->overwrite;
156 lum.u.channel.subbuf_size = chops->subbuf_size;
157 lum.u.channel.num_subbuf = chops->num_subbuf;
158 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
159 lum.u.channel.read_timer_interval = chops->read_timer_interval;
160 lum.u.channel.output = chops->output;
161 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
162 if (ret) {
163 free(metadata_data);
164 return ret;
165 }
166 if (lur.ret_code != USTCOMM_OK) {
167 free(metadata_data);
168 return lur.ret_code;
169 }
170 metadata_data->handle = lur.ret_val;
171 DBG("received metadata handle %u", metadata_data->handle);
172 metadata_data->memory_map_size = lur.u.channel.memory_map_size;
173 /* get shm fd */
174 ret = ustcomm_recv_fd(sock);
175 if (ret < 0)
eeee05f3
MD
176 err = 1;
177 else
178 metadata_data->shm_fd = ret;
179 /*
180 * We need to get the second FD even if the first fails, because
181 * libust expects us to read the two FDs.
182 */
57773204
MD
183 /* get wait fd */
184 ret = ustcomm_recv_fd(sock);
185 if (ret < 0)
eeee05f3
MD
186 err = 1;
187 else
188 metadata_data->wait_fd = ret;
189 if (err)
57773204 190 goto error;
57773204
MD
191 *_metadata_data = metadata_data;
192 return 0;
193
194error:
d26228ae 195 (void) ustctl_release_object(sock, metadata_data);
38970582 196 free(metadata_data);
57773204
MD
197 return -EINVAL;
198}
199
200int ustctl_create_channel(int sock, int session_handle,
201 struct lttng_ust_channel_attr *chops,
61f02aea 202 struct lttng_ust_object_data **_channel_data)
57773204
MD
203{
204 struct ustcomm_ust_msg lum;
205 struct ustcomm_ust_reply lur;
61f02aea 206 struct lttng_ust_object_data *channel_data;
eeee05f3 207 int ret, err = 0;
57773204 208
9bfc503d
MD
209 if (!chops || !_channel_data)
210 return -EINVAL;
211
57773204
MD
212 channel_data = malloc(sizeof(*channel_data));
213 if (!channel_data)
214 return -ENOMEM;
215 init_object(channel_data);
216 /* Create metadata channel */
217 memset(&lum, 0, sizeof(lum));
218 lum.handle = session_handle;
219 lum.cmd = LTTNG_UST_CHANNEL;
220 lum.u.channel.overwrite = chops->overwrite;
221 lum.u.channel.subbuf_size = chops->subbuf_size;
222 lum.u.channel.num_subbuf = chops->num_subbuf;
223 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
224 lum.u.channel.read_timer_interval = chops->read_timer_interval;
225 lum.u.channel.output = chops->output;
226 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
227 if (ret) {
228 free(channel_data);
229 return ret;
230 }
231 if (lur.ret_code != USTCOMM_OK) {
232 free(channel_data);
233 return lur.ret_code;
234 }
235 channel_data->handle = lur.ret_val;
236 DBG("received channel handle %u", channel_data->handle);
237 channel_data->memory_map_size = lur.u.channel.memory_map_size;
238 /* get shm fd */
239 ret = ustcomm_recv_fd(sock);
240 if (ret < 0)
eeee05f3
MD
241 err = 1;
242 else
243 channel_data->shm_fd = ret;
244 /*
245 * We need to get the second FD even if the first fails, because
246 * libust expects us to read the two FDs.
247 */
57773204
MD
248 /* get wait fd */
249 ret = ustcomm_recv_fd(sock);
250 if (ret < 0)
eeee05f3
MD
251 err = 1;
252 else
253 channel_data->wait_fd = ret;
254 if (err)
57773204 255 goto error;
57773204
MD
256 *_channel_data = channel_data;
257 return 0;
258
259error:
d26228ae 260 (void) ustctl_release_object(sock, channel_data);
38970582 261 free(channel_data);
57773204
MD
262 return -EINVAL;
263}
264
265/*
266 * Return -ENOENT if no more stream is available for creation.
267 * Return 0 on success.
268 * Return negative error value on error.
269 */
61f02aea
MD
270int ustctl_create_stream(int sock, struct lttng_ust_object_data *channel_data,
271 struct lttng_ust_object_data **_stream_data)
57773204
MD
272{
273 struct ustcomm_ust_msg lum;
274 struct ustcomm_ust_reply lur;
61f02aea 275 struct lttng_ust_object_data *stream_data;
eeee05f3 276 int ret, fd, err = 0;
57773204 277
9bfc503d
MD
278 if (!channel_data || !_stream_data)
279 return -EINVAL;
280
57773204
MD
281 stream_data = malloc(sizeof(*stream_data));
282 if (!stream_data)
283 return -ENOMEM;
284 init_object(stream_data);
285 memset(&lum, 0, sizeof(lum));
286 lum.handle = channel_data->handle;
287 lum.cmd = LTTNG_UST_STREAM;
288 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
289 if (ret) {
290 free(stream_data);
291 return ret;
292 }
293 if (lur.ret_code != USTCOMM_OK) {
294 free(stream_data);
295 return lur.ret_code;
296 }
297
298 stream_data->handle = lur.ret_val;
299 DBG("received stream handle %u", stream_data->handle);
300 stream_data->memory_map_size = lur.u.stream.memory_map_size;
301 /* get shm fd */
302 fd = ustcomm_recv_fd(sock);
303 if (fd < 0)
eeee05f3
MD
304 err = 1;
305 else
306 stream_data->shm_fd = fd;
307 /*
308 * We need to get the second FD even if the first fails, because
309 * libust expects us to read the two FDs.
310 */
57773204
MD
311 /* get wait fd */
312 fd = ustcomm_recv_fd(sock);
313 if (fd < 0)
eeee05f3
MD
314 err = 1;
315 else
316 stream_data->wait_fd = fd;
317 if (err)
57773204 318 goto error;
57773204
MD
319 *_stream_data = stream_data;
320 return ret;
321
322error:
d26228ae 323 (void) ustctl_release_object(sock, stream_data);
38970582 324 free(stream_data);
57773204
MD
325 return -EINVAL;
326}
327
328int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
329 struct lttng_ust_object_data *channel_data,
330 struct lttng_ust_object_data **_event_data)
57773204
MD
331{
332 struct ustcomm_ust_msg lum;
333 struct ustcomm_ust_reply lur;
61f02aea 334 struct lttng_ust_object_data *event_data;
57773204
MD
335 int ret;
336
9bfc503d
MD
337 if (!channel_data || !_event_data)
338 return -EINVAL;
339
57773204
MD
340 event_data = malloc(sizeof(*event_data));
341 if (!event_data)
342 return -ENOMEM;
343 init_object(event_data);
344 memset(&lum, 0, sizeof(lum));
345 lum.handle = channel_data->handle;
346 lum.cmd = LTTNG_UST_EVENT;
347 strncpy(lum.u.event.name, ev->name,
348 LTTNG_UST_SYM_NAME_LEN);
349 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
350 lum.u.event.loglevel_type = ev->loglevel_type;
351 lum.u.event.loglevel = ev->loglevel;
57773204
MD
352 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
353 if (ret) {
354 free(event_data);
355 return ret;
356 }
357 event_data->handle = lur.ret_val;
358 DBG("received event handle %u", event_data->handle);
359 *_event_data = event_data;
360 return 0;
361}
362
363int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
61f02aea
MD
364 struct lttng_ust_object_data *obj_data,
365 struct lttng_ust_object_data **_context_data)
57773204
MD
366{
367 struct ustcomm_ust_msg lum;
368 struct ustcomm_ust_reply lur;
61f02aea 369 struct lttng_ust_object_data *context_data;
57773204
MD
370 int ret;
371
9bfc503d
MD
372 if (!obj_data || !_context_data)
373 return -EINVAL;
374
57773204
MD
375 context_data = malloc(sizeof(*context_data));
376 if (!context_data)
377 return -ENOMEM;
378 init_object(context_data);
379 memset(&lum, 0, sizeof(lum));
3039d8ed 380 lum.handle = obj_data->handle;
57773204
MD
381 lum.cmd = LTTNG_UST_CONTEXT;
382 lum.u.context.ctx = ctx->ctx;
383 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
384 if (ret) {
385 free(context_data);
386 return ret;
387 }
388 context_data->handle = lur.ret_val;
389 DBG("received context handle %u", context_data->handle);
390 *_context_data = context_data;
391 return ret;
392}
393
394/* Enable event, channel and session ioctl */
61f02aea 395int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
396{
397 struct ustcomm_ust_msg lum;
398 struct ustcomm_ust_reply lur;
399 int ret;
400
9bfc503d
MD
401 if (!object)
402 return -EINVAL;
403
57773204
MD
404 memset(&lum, 0, sizeof(lum));
405 lum.handle = object->handle;
406 lum.cmd = LTTNG_UST_ENABLE;
407 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
408 if (ret)
409 return ret;
410 DBG("enabled handle %u", object->handle);
411 return 0;
412}
413
414/* Disable event, channel and session ioctl */
61f02aea 415int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
416{
417 struct ustcomm_ust_msg lum;
418 struct ustcomm_ust_reply lur;
419 int ret;
420
9bfc503d
MD
421 if (!object)
422 return -EINVAL;
423
57773204
MD
424 memset(&lum, 0, sizeof(lum));
425 lum.handle = object->handle;
426 lum.cmd = LTTNG_UST_DISABLE;
427 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
428 if (ret)
429 return ret;
430 DBG("disable handle %u", object->handle);
431 return 0;
432}
433
4a6ca058 434int ustctl_start_session(int sock, int handle)
57773204 435{
61f02aea 436 struct lttng_ust_object_data obj;
4a6ca058
MD
437
438 obj.handle = handle;
439 return ustctl_enable(sock, &obj);
57773204
MD
440}
441
4a6ca058 442int ustctl_stop_session(int sock, int handle)
57773204 443{
61f02aea 444 struct lttng_ust_object_data obj;
4a6ca058
MD
445
446 obj.handle = handle;
447 return ustctl_disable(sock, &obj);
57773204
MD
448}
449
57773204
MD
450int ustctl_tracepoint_list(int sock)
451{
b115631f
MD
452 struct ustcomm_ust_msg lum;
453 struct ustcomm_ust_reply lur;
454 int ret, tp_list_handle;
455
456 memset(&lum, 0, sizeof(lum));
457 lum.handle = LTTNG_UST_ROOT_HANDLE;
458 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
459 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
460 if (ret)
461 return ret;
462 tp_list_handle = lur.ret_val;
463 DBG("received tracepoint list handle %u", tp_list_handle);
464 return tp_list_handle;
465}
466
467int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
cbef6901 468 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
469{
470 struct ustcomm_ust_msg lum;
471 struct ustcomm_ust_reply lur;
472 int ret;
473
9bfc503d
MD
474 if (!iter)
475 return -EINVAL;
476
b115631f
MD
477 memset(&lum, 0, sizeof(lum));
478 lum.handle = tp_list_handle;
479 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
480 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
481 if (ret)
482 return ret;
882a56d7 483 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 484 lur.u.tracepoint.name,
882a56d7 485 lur.u.tracepoint.loglevel);
cbef6901 486 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 487 return 0;
57773204
MD
488}
489
490int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
491{
492 struct ustcomm_ust_msg lum;
493 struct ustcomm_ust_reply lur;
494 int ret;
495
9bfc503d
MD
496 if (!v)
497 return -EINVAL;
498
57773204
MD
499 memset(&lum, 0, sizeof(lum));
500 lum.handle = LTTNG_UST_ROOT_HANDLE;
501 lum.cmd = LTTNG_UST_TRACER_VERSION;
502 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
503 if (ret)
504 return ret;
505 memcpy(v, &lur.u.version, sizeof(*v));
506 DBG("received tracer version");
507 return 0;
508}
509
510int ustctl_wait_quiescent(int sock)
511{
512 struct ustcomm_ust_msg lum;
513 struct ustcomm_ust_reply lur;
514 int ret;
515
516 memset(&lum, 0, sizeof(lum));
517 lum.handle = LTTNG_UST_ROOT_HANDLE;
518 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
519 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
520 if (ret)
521 return ret;
522 DBG("waited for quiescent state");
523 return 0;
524}
525
526int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
527{
9bfc503d
MD
528 if (!calibrate)
529 return -EINVAL;
530
57773204
MD
531 return -ENOSYS;
532}
533
f1fffc57
MD
534int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
535{
536 struct ustcomm_ust_msg lum;
537 struct ustcomm_ust_reply lur;
538 int ret;
539
9bfc503d
MD
540 if (!object)
541 return -EINVAL;
542
f1fffc57
MD
543 memset(&lum, 0, sizeof(lum));
544 lum.handle = object->handle;
545 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
546 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
547 if (ret)
548 return ret;
549 DBG("flushed buffer handle %u", object->handle);
550 return 0;
551}
552
57773204
MD
553/* Buffer operations */
554
555/* Map channel shm into process memory */
38fae1d3 556struct lttng_ust_shm_handle *ustctl_map_channel(struct lttng_ust_object_data *chan_data)
57773204 557{
38fae1d3 558 struct lttng_ust_shm_handle *handle;
57773204
MD
559 struct channel *chan;
560 size_t chan_size;
c1fca457 561 struct lttng_ust_lib_ring_buffer_config *config;
7a784989 562 int ret;
57773204 563
9bfc503d
MD
564 if (!chan_data)
565 return NULL;
566
57773204
MD
567 handle = channel_handle_create(chan_data->shm_fd,
568 chan_data->wait_fd,
569 chan_data->memory_map_size);
570 if (!handle) {
571 ERR("create handle error");
572 return NULL;
573 }
574 /*
0bfe09ec
MD
575 * Set to -1, and then close the shm fd, and set the handle shm
576 * fd to -1 too. We don't need the shm fds after they have been
577 * mapped.
578 * The wait_fd is set to -1 in chan_data because it is now owned
579 * by the handle.
57773204
MD
580 */
581 chan_data->shm_fd = -1;
582 chan_data->wait_fd = -1;
583
0bfe09ec
MD
584 /* chan is object 0. This is hardcoded. */
585 if (handle->table->objects[0].shm_fd >= 0) {
586 ret = close(handle->table->objects[0].shm_fd);
587 if (ret) {
588 perror("Error closing shm_fd");
589 }
590 handle->table->objects[0].shm_fd = -1;
591 }
592
57773204
MD
593 /*
594 * TODO: add consistency checks to be resilient if the
595 * application try to feed us with incoherent channel structure
596 * values.
597 */
598 chan = shmp(handle, handle->chan);
599 /* chan is object 0. This is hardcoded. */
600 chan_size = handle->table->objects[0].allocated_len;
601 handle->shadow_chan = malloc(chan_size);
602 if (!handle->shadow_chan) {
603 channel_destroy(chan, handle, 1);
604 return NULL;
605 }
606 memcpy(handle->shadow_chan, chan, chan_size);
bbc70d1b
MD
607 /*
608 * The callback pointers in the producer are invalid in the
c1fca457 609 * consumer. We need to look them up here.
bbc70d1b 610 */
c1fca457
MD
611 config = &handle->shadow_chan->backend.config;
612 switch (config->client_type) {
613 case LTTNG_CLIENT_METADATA:
614 memcpy(&config->cb, lttng_client_callbacks_metadata,
615 sizeof(config->cb));
616 break;
617 case LTTNG_CLIENT_DISCARD:
618 memcpy(&config->cb, lttng_client_callbacks_discard,
619 sizeof(config->cb));
620 break;
621 case LTTNG_CLIENT_OVERWRITE:
622 memcpy(&config->cb, lttng_client_callbacks_overwrite,
623 sizeof(config->cb));
624 break;
625 default:
626 ERR("Unknown client type %d", config->client_type);
627 channel_destroy(chan, handle, 1);
628 return NULL;
629 }
7a784989
MD
630 /* Replace the object table pointer. */
631 ret = munmap(handle->table->objects[0].memory_map,
632 handle->table->objects[0].memory_map_size);
633 if (ret) {
634 perror("munmap");
635 assert(0);
636 }
637 handle->table->objects[0].memory_map = (char *) handle->shadow_chan;
638 handle->table->objects[0].is_shadow = 1;
57773204
MD
639 return handle;
640}
641
642/* Add stream to channel shm and map its shm into process memory */
38fae1d3 643int ustctl_add_stream(struct lttng_ust_shm_handle *handle,
61f02aea 644 struct lttng_ust_object_data *stream_data)
57773204
MD
645{
646 int ret;
647
9bfc503d
MD
648 if (!handle || !stream_data)
649 return -EINVAL;
650
57773204
MD
651 if (!stream_data->handle)
652 return -ENOENT;
653 /* map stream */
654 ret = channel_handle_add_stream(handle,
655 stream_data->shm_fd,
656 stream_data->wait_fd,
657 stream_data->memory_map_size);
658 if (ret) {
659 ERR("add stream error\n");
660 return ret;
661 }
662 /*
38fae1d3 663 * Set to -1 because the lttng_ust_shm_handle destruction will take care
57773204
MD
664 * of closing shm_fd and wait_fd.
665 */
666 stream_data->shm_fd = -1;
667 stream_data->wait_fd = -1;
668 return 0;
669}
670
38fae1d3 671void ustctl_unmap_channel(struct lttng_ust_shm_handle *handle)
5224b5c8
MD
672{
673 struct channel *chan;
674
9bfc503d 675 assert(handle);
5224b5c8
MD
676 chan = shmp(handle, handle->chan);
677 channel_destroy(chan, handle, 1);
678}
679
0bfe09ec
MD
680/*
681 * ustctl closes the shm_fd fds after mapping it.
682 */
4cfec15c 683struct lttng_ust_lib_ring_buffer *ustctl_open_stream_read(struct lttng_ust_shm_handle *handle,
6e922b24
MD
684 int cpu)
685{
686 struct channel *chan = handle->shadow_chan;
ef9ff354
MD
687 int *shm_fd, *wait_fd;
688 uint64_t *memory_map_size;
4cfec15c 689 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
690 int ret;
691
9bfc503d
MD
692 if (!handle)
693 return NULL;
694
6e922b24
MD
695 buf = channel_get_ring_buffer(&chan->backend.config,
696 chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size);
697 if (!buf)
698 return NULL;
699 ret = lib_ring_buffer_open_read(buf, handle, 1);
700 if (ret)
701 return NULL;
0bfe09ec
MD
702 /*
703 * We can close shm_fd early, right after is has been mapped.
704 */
705 if (*shm_fd >= 0) {
706 ret = close(*shm_fd);
707 if (ret) {
708 perror("Error closing shm_fd");
709 }
710 *shm_fd = -1;
711 }
6e922b24
MD
712 return buf;
713}
714
38fae1d3 715void ustctl_close_stream_read(struct lttng_ust_shm_handle *handle,
4cfec15c 716 struct lttng_ust_lib_ring_buffer *buf)
6e922b24 717{
9bfc503d 718 assert(handle && buf);
6e922b24
MD
719 lib_ring_buffer_release_read(buf, handle, 1);
720}
721
57773204
MD
722/* For mmap mode, readable without "get" operation */
723
38fae1d3 724void *ustctl_get_mmap_base(struct lttng_ust_shm_handle *handle,
4cfec15c 725 struct lttng_ust_lib_ring_buffer *buf)
9095efe9 726{
9bfc503d
MD
727 if (!handle || !buf)
728 return NULL;
9095efe9
MD
729 return shmp(handle, buf->backend.memory_map);
730}
731
57773204 732/* returns the length to mmap. */
38fae1d3 733int ustctl_get_mmap_len(struct lttng_ust_shm_handle *handle,
4cfec15c 734 struct lttng_ust_lib_ring_buffer *buf,
57773204
MD
735 unsigned long *len)
736{
737 unsigned long mmap_buf_len;
738 struct channel *chan = handle->shadow_chan;
739
9bfc503d
MD
740 if (!handle || !buf || !len)
741 return -EINVAL;
742
57773204
MD
743 if (chan->backend.config.output != RING_BUFFER_MMAP)
744 return -EINVAL;
745 mmap_buf_len = chan->backend.buf_size;
746 if (chan->backend.extra_reader_sb)
747 mmap_buf_len += chan->backend.subbuf_size;
748 if (mmap_buf_len > INT_MAX)
749 return -EFBIG;
750 *len = mmap_buf_len;
751 return 0;
752}
753
754/* returns the maximum size for sub-buffers. */
38fae1d3 755int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle *handle,
4cfec15c 756 struct lttng_ust_lib_ring_buffer *buf,
57773204
MD
757 unsigned long *len)
758{
759 struct channel *chan = handle->shadow_chan;
760
9bfc503d
MD
761 if (!handle || !buf || !len)
762 return -EINVAL;
763
57773204
MD
764 *len = chan->backend.subbuf_size;
765 return 0;
766}
767
768/*
769 * For mmap mode, operate on the current packet (between get/put or
770 * get_next/put_next).
771 */
772
773/* returns the offset of the subbuffer belonging to the mmap reader. */
38fae1d3 774int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
4cfec15c 775 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
57773204
MD
776{
777 struct channel *chan = handle->shadow_chan;
778 unsigned long sb_bindex;
779
9bfc503d
MD
780 if (!handle || !buf || !off)
781 return -EINVAL;
782
57773204
MD
783 if (chan->backend.config.output != RING_BUFFER_MMAP)
784 return -EINVAL;
785 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
786 buf->backend.buf_rsb.id);
787 *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
788 return 0;
789}
790
791/* returns the size of the current sub-buffer, without padding (for mmap). */
38fae1d3 792int ustctl_get_subbuf_size(struct lttng_ust_shm_handle *handle,
4cfec15c 793 struct lttng_ust_lib_ring_buffer *buf, unsigned long *len)
57773204
MD
794{
795 struct channel *chan = handle->shadow_chan;
796
9bfc503d
MD
797 if (!handle || !buf || !len)
798 return -EINVAL;
799
57773204
MD
800 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
801 handle);
802 return 0;
803}
804
805/* returns the size of the current sub-buffer, without padding (for mmap). */
38fae1d3 806int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle *handle,
4cfec15c 807 struct lttng_ust_lib_ring_buffer *buf, unsigned long *len)
57773204
MD
808{
809 struct channel *chan = handle->shadow_chan;
810
9bfc503d
MD
811 if (!handle || !buf || !len)
812 return -EINVAL;
813
57773204
MD
814 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
815 handle);
816 *len = PAGE_ALIGN(*len);
817 return 0;
818}
819
820/* Get exclusive read access to the next sub-buffer that can be read. */
38fae1d3 821int ustctl_get_next_subbuf(struct lttng_ust_shm_handle *handle,
4cfec15c 822 struct lttng_ust_lib_ring_buffer *buf)
57773204 823{
9bfc503d
MD
824 if (!handle || !buf)
825 return -EINVAL;
826
57773204
MD
827 return lib_ring_buffer_get_next_subbuf(buf, handle);
828}
829
830
831/* Release exclusive sub-buffer access, move consumer forward. */
38fae1d3 832int ustctl_put_next_subbuf(struct lttng_ust_shm_handle *handle,
4cfec15c 833 struct lttng_ust_lib_ring_buffer *buf)
57773204 834{
9bfc503d
MD
835 if (!handle || !buf)
836 return -EINVAL;
837
57773204
MD
838 lib_ring_buffer_put_next_subbuf(buf, handle);
839 return 0;
840}
841
842/* snapshot */
843
844/* Get a snapshot of the current ring buffer producer and consumer positions */
38fae1d3 845int ustctl_snapshot(struct lttng_ust_shm_handle *handle,
4cfec15c 846 struct lttng_ust_lib_ring_buffer *buf)
57773204 847{
9bfc503d
MD
848 if (!handle || !buf)
849 return -EINVAL;
850
57773204
MD
851 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
852 &buf->prod_snapshot, handle);
853}
854
855/* Get the consumer position (iteration start) */
38fae1d3 856int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle *handle,
4cfec15c 857 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
57773204 858{
9bfc503d
MD
859 if (!handle || !buf || !pos)
860 return -EINVAL;
861
57773204
MD
862 *pos = buf->cons_snapshot;
863 return 0;
864}
865
866/* Get the producer position (iteration end) */
38fae1d3 867int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle *handle,
4cfec15c 868 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
57773204 869{
9bfc503d
MD
870 if (!handle || !buf || !pos)
871 return -EINVAL;
872
57773204
MD
873 *pos = buf->prod_snapshot;
874 return 0;
875}
876
877/* Get exclusive read access to the specified sub-buffer position */
38fae1d3 878int ustctl_get_subbuf(struct lttng_ust_shm_handle *handle,
4cfec15c 879 struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos)
57773204 880{
9bfc503d
MD
881 if (!handle || !buf || !pos)
882 return -EINVAL;
883
57773204
MD
884 return lib_ring_buffer_get_subbuf(buf, *pos, handle);
885}
886
887/* Release exclusive sub-buffer access */
38fae1d3 888int ustctl_put_subbuf(struct lttng_ust_shm_handle *handle,
4cfec15c 889 struct lttng_ust_lib_ring_buffer *buf)
57773204 890{
9bfc503d
MD
891 if (!handle || !buf)
892 return -EINVAL;
893
57773204
MD
894 lib_ring_buffer_put_subbuf(buf, handle);
895 return 0;
896}
897
02a15cfb 898void ustctl_flush_buffer(struct lttng_ust_shm_handle *handle,
b52190f2
MD
899 struct lttng_ust_lib_ring_buffer *buf,
900 int producer_active)
57773204 901{
9bfc503d 902 assert(handle && buf);
b52190f2
MD
903 lib_ring_buffer_switch_slow(buf,
904 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
905 handle);
57773204 906}
This page took 0.062144 seconds and 4 git commands to generate.