Add ustctl_open_stream_read and ustctl_close_stream_read
[lttng-ust.git] / libustctl / 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
20#include <string.h>
21#include <ust/lttng-ust-ctl.h>
22#include <ust/lttng-ust-abi.h>
23#include <ust/usterr-signal-safe.h>
24#include "lttng-ust-comm.h"
25
26#include "../libringbuffer/backend.h"
27#include "../libringbuffer/frontend.h"
28
29static
30void init_object(struct object_data *data)
31{
32 data->handle = -1;
33 data->shm_fd = -1;
34 data->wait_fd = -1;
35 data->memory_map_size = 0;
36}
37
38void release_object(int sock, struct object_data *data)
39{
40 struct ustcomm_ust_msg lum;
41 struct ustcomm_ust_reply lur;
42 int ret;
43
44 if (data->shm_fd >= 0)
45 close(data->shm_fd);
46 if (data->wait_fd >= 0)
47 close(data->wait_fd);
48 memset(&lum, 0, sizeof(lum));
49 lum.handle = data->handle;
50 lum.cmd = LTTNG_UST_RELEASE;
51 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
52 assert(!ret);
53 free(data);
54}
55
56/*
57 * returns session handle.
58 */
59int ustctl_create_session(int sock)
60{
61 struct ustcomm_ust_msg lum;
62 struct ustcomm_ust_reply lur;
63 int ret, session_handle;
64
65 /* Create session */
66 memset(&lum, 0, sizeof(lum));
67 lum.handle = LTTNG_UST_ROOT_HANDLE;
68 lum.cmd = LTTNG_UST_SESSION;
69 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
70 if (ret)
71 return ret;
72 session_handle = lur.ret_val;
73 DBG("received session handle %u", session_handle);
74 return session_handle;
75}
76
77/* open the metadata global channel */
78int ustctl_open_metadata(int sock, int session_handle,
79 struct lttng_ust_channel_attr *chops,
80 struct object_data **_metadata_data)
81{
82 struct ustcomm_ust_msg lum;
83 struct ustcomm_ust_reply lur;
84 struct object_data *metadata_data;
85 int ret;
86
87 metadata_data = malloc(sizeof(*metadata_data));
88 if (!metadata_data)
89 return -ENOMEM;
90 init_object(metadata_data);
91 /* Create metadata channel */
92 memset(&lum, 0, sizeof(lum));
93 lum.handle = session_handle;
94 lum.cmd = LTTNG_UST_METADATA;
95 lum.u.channel.overwrite = chops->overwrite;
96 lum.u.channel.subbuf_size = chops->subbuf_size;
97 lum.u.channel.num_subbuf = chops->num_subbuf;
98 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
99 lum.u.channel.read_timer_interval = chops->read_timer_interval;
100 lum.u.channel.output = chops->output;
101 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
102 if (ret) {
103 free(metadata_data);
104 return ret;
105 }
106 if (lur.ret_code != USTCOMM_OK) {
107 free(metadata_data);
108 return lur.ret_code;
109 }
110 metadata_data->handle = lur.ret_val;
111 DBG("received metadata handle %u", metadata_data->handle);
112 metadata_data->memory_map_size = lur.u.channel.memory_map_size;
113 /* get shm fd */
114 ret = ustcomm_recv_fd(sock);
115 if (ret < 0)
116 goto error;
117 metadata_data->shm_fd = ret;
118 /* get wait fd */
119 ret = ustcomm_recv_fd(sock);
120 if (ret < 0)
121 goto error;
122 metadata_data->wait_fd = ret;
123 *_metadata_data = metadata_data;
124 return 0;
125
126error:
127 release_object(sock, metadata_data);
128 return -EINVAL;
129}
130
131int ustctl_create_channel(int sock, int session_handle,
132 struct lttng_ust_channel_attr *chops,
133 struct object_data **_channel_data)
134{
135 struct ustcomm_ust_msg lum;
136 struct ustcomm_ust_reply lur;
137 struct object_data *channel_data;
138 int ret;
139
140 channel_data = malloc(sizeof(*channel_data));
141 if (!channel_data)
142 return -ENOMEM;
143 init_object(channel_data);
144 /* Create metadata channel */
145 memset(&lum, 0, sizeof(lum));
146 lum.handle = session_handle;
147 lum.cmd = LTTNG_UST_CHANNEL;
148 lum.u.channel.overwrite = chops->overwrite;
149 lum.u.channel.subbuf_size = chops->subbuf_size;
150 lum.u.channel.num_subbuf = chops->num_subbuf;
151 lum.u.channel.switch_timer_interval = chops->switch_timer_interval;
152 lum.u.channel.read_timer_interval = chops->read_timer_interval;
153 lum.u.channel.output = chops->output;
154 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
155 if (ret) {
156 free(channel_data);
157 return ret;
158 }
159 if (lur.ret_code != USTCOMM_OK) {
160 free(channel_data);
161 return lur.ret_code;
162 }
163 channel_data->handle = lur.ret_val;
164 DBG("received channel handle %u", channel_data->handle);
165 channel_data->memory_map_size = lur.u.channel.memory_map_size;
166 /* get shm fd */
167 ret = ustcomm_recv_fd(sock);
168 if (ret < 0)
169 goto error;
170 channel_data->shm_fd = ret;
171 /* get wait fd */
172 ret = ustcomm_recv_fd(sock);
173 if (ret < 0)
174 goto error;
175 channel_data->wait_fd = ret;
176 *_channel_data = channel_data;
177 return 0;
178
179error:
180 release_object(sock, channel_data);
181 return -EINVAL;
182}
183
184/*
185 * Return -ENOENT if no more stream is available for creation.
186 * Return 0 on success.
187 * Return negative error value on error.
188 */
189int ustctl_create_stream(int sock, struct object_data *channel_data,
190 struct object_data **_stream_data)
191{
192 struct ustcomm_ust_msg lum;
193 struct ustcomm_ust_reply lur;
194 struct object_data *stream_data;
195 int ret, fd;
196
197 stream_data = malloc(sizeof(*stream_data));
198 if (!stream_data)
199 return -ENOMEM;
200 init_object(stream_data);
201 memset(&lum, 0, sizeof(lum));
202 lum.handle = channel_data->handle;
203 lum.cmd = LTTNG_UST_STREAM;
204 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
205 if (ret) {
206 free(stream_data);
207 return ret;
208 }
209 if (lur.ret_code != USTCOMM_OK) {
210 free(stream_data);
211 return lur.ret_code;
212 }
213
214 stream_data->handle = lur.ret_val;
215 DBG("received stream handle %u", stream_data->handle);
216 stream_data->memory_map_size = lur.u.stream.memory_map_size;
217 /* get shm fd */
218 fd = ustcomm_recv_fd(sock);
219 if (fd < 0)
220 goto error;
221 stream_data->shm_fd = fd;
222 /* get wait fd */
223 fd = ustcomm_recv_fd(sock);
224 if (fd < 0)
225 goto error;
226 stream_data->wait_fd = fd;
227 *_stream_data = stream_data;
228 return ret;
229
230error:
231 release_object(sock, stream_data);
232 return -EINVAL;
233}
234
235int ustctl_create_event(int sock, struct lttng_ust_event *ev,
236 struct object_data *channel_data,
237 struct object_data **_event_data)
238{
239 struct ustcomm_ust_msg lum;
240 struct ustcomm_ust_reply lur;
241 struct object_data *event_data;
242 int ret;
243
244 event_data = malloc(sizeof(*event_data));
245 if (!event_data)
246 return -ENOMEM;
247 init_object(event_data);
248 memset(&lum, 0, sizeof(lum));
249 lum.handle = channel_data->handle;
250 lum.cmd = LTTNG_UST_EVENT;
251 strncpy(lum.u.event.name, ev->name,
252 LTTNG_UST_SYM_NAME_LEN);
253 lum.u.event.instrumentation = ev->instrumentation;
254 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
255 if (ret) {
256 free(event_data);
257 return ret;
258 }
259 event_data->handle = lur.ret_val;
260 DBG("received event handle %u", event_data->handle);
261 *_event_data = event_data;
262 return 0;
263}
264
265int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
266 struct object_data *channel_data,
267 struct object_data **_context_data)
268{
269 struct ustcomm_ust_msg lum;
270 struct ustcomm_ust_reply lur;
271 struct object_data *context_data;
272 int ret;
273
274 context_data = malloc(sizeof(*context_data));
275 if (!context_data)
276 return -ENOMEM;
277 init_object(context_data);
278 memset(&lum, 0, sizeof(lum));
279 lum.handle = channel_data->handle;
280 lum.cmd = LTTNG_UST_CONTEXT;
281 lum.u.context.ctx = ctx->ctx;
282 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
283 if (ret) {
284 free(context_data);
285 return ret;
286 }
287 context_data->handle = lur.ret_val;
288 DBG("received context handle %u", context_data->handle);
289 *_context_data = context_data;
290 return ret;
291}
292
293/* Enable event, channel and session ioctl */
294int ustctl_enable(int sock, struct object_data *object)
295{
296 struct ustcomm_ust_msg lum;
297 struct ustcomm_ust_reply lur;
298 int ret;
299
300 memset(&lum, 0, sizeof(lum));
301 lum.handle = object->handle;
302 lum.cmd = LTTNG_UST_ENABLE;
303 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
304 if (ret)
305 return ret;
306 DBG("enabled handle %u", object->handle);
307 return 0;
308}
309
310/* Disable event, channel and session ioctl */
311int ustctl_disable(int sock, struct object_data *object)
312{
313 struct ustcomm_ust_msg lum;
314 struct ustcomm_ust_reply lur;
315 int ret;
316
317 memset(&lum, 0, sizeof(lum));
318 lum.handle = object->handle;
319 lum.cmd = LTTNG_UST_DISABLE;
320 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
321 if (ret)
322 return ret;
323 DBG("disable handle %u", object->handle);
324 return 0;
325}
326
327int ustctl_start_session(int sock, struct object_data *object)
328{
329 return ustctl_enable(sock, object);
330}
331
332int ustctl_stop_session(int sock, struct object_data *object)
333{
334 return ustctl_disable(sock, object);
335}
336
337
338int ustctl_tracepoint_list(int sock)
339{
340 return -ENOSYS; /* not implemented */
341}
342
343int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
344{
345 struct ustcomm_ust_msg lum;
346 struct ustcomm_ust_reply lur;
347 int ret;
348
349 memset(&lum, 0, sizeof(lum));
350 lum.handle = LTTNG_UST_ROOT_HANDLE;
351 lum.cmd = LTTNG_UST_TRACER_VERSION;
352 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
353 if (ret)
354 return ret;
355 memcpy(v, &lur.u.version, sizeof(*v));
356 DBG("received tracer version");
357 return 0;
358}
359
360int ustctl_wait_quiescent(int sock)
361{
362 struct ustcomm_ust_msg lum;
363 struct ustcomm_ust_reply lur;
364 int ret;
365
366 memset(&lum, 0, sizeof(lum));
367 lum.handle = LTTNG_UST_ROOT_HANDLE;
368 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
369 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
370 if (ret)
371 return ret;
372 DBG("waited for quiescent state");
373 return 0;
374}
375
376int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
377{
378 return -ENOSYS;
379}
380
381/* Buffer operations */
382
383/* Map channel shm into process memory */
384struct shm_handle *ustctl_map_channel(struct object_data *chan_data)
385{
386 struct shm_handle *handle;
387 struct channel *chan;
388 size_t chan_size;
389
390 handle = channel_handle_create(chan_data->shm_fd,
391 chan_data->wait_fd,
392 chan_data->memory_map_size);
393 if (!handle) {
394 ERR("create handle error");
395 return NULL;
396 }
397 /*
398 * Set to -1 because the shm_handle destruction will take care
399 * of closing shm_fd and wait_fd.
400 */
401 chan_data->shm_fd = -1;
402 chan_data->wait_fd = -1;
403
404 /*
405 * TODO: add consistency checks to be resilient if the
406 * application try to feed us with incoherent channel structure
407 * values.
408 */
409 chan = shmp(handle, handle->chan);
410 /* chan is object 0. This is hardcoded. */
411 chan_size = handle->table->objects[0].allocated_len;
412 handle->shadow_chan = malloc(chan_size);
413 if (!handle->shadow_chan) {
414 channel_destroy(chan, handle, 1);
415 return NULL;
416 }
417 memcpy(handle->shadow_chan, chan, chan_size);
418 return handle;
419}
420
421/* Add stream to channel shm and map its shm into process memory */
422int ustctl_add_stream(struct shm_handle *handle,
423 struct object_data *stream_data)
424{
425 int ret;
426
427 if (!stream_data->handle)
428 return -ENOENT;
429 /* map stream */
430 ret = channel_handle_add_stream(handle,
431 stream_data->shm_fd,
432 stream_data->wait_fd,
433 stream_data->memory_map_size);
434 if (ret) {
435 ERR("add stream error\n");
436 return ret;
437 }
438 /*
439 * Set to -1 because the shm_handle destruction will take care
440 * of closing shm_fd and wait_fd.
441 */
442 stream_data->shm_fd = -1;
443 stream_data->wait_fd = -1;
444 return 0;
445}
446
5224b5c8
MD
447void ustctl_unmap_channel(struct shm_handle *handle)
448{
449 struct channel *chan;
450
451 chan = shmp(handle, handle->chan);
452 channel_destroy(chan, handle, 1);
453}
454
6e922b24
MD
455struct lib_ring_buffer *ustctl_open_stream_read(struct shm_handle *handle,
456 int cpu)
457{
458 struct channel *chan = handle->shadow_chan;
459 int shm_fd, wait_fd;
460 uint64_t memory_map_size;
461 struct lib_ring_buffer *buf;
462 int ret;
463
464 buf = channel_get_ring_buffer(&chan->backend.config,
465 chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size);
466 if (!buf)
467 return NULL;
468 ret = lib_ring_buffer_open_read(buf, handle, 1);
469 if (ret)
470 return NULL;
471 return buf;
472}
473
474void ustctl_close_stream_read(struct shm_handle *handle,
475 struct lib_ring_buffer *buf)
476{
477 lib_ring_buffer_release_read(buf, handle, 1);
478}
479
57773204
MD
480/* For mmap mode, readable without "get" operation */
481
482/* returns the length to mmap. */
483int ustctl_get_mmap_len(struct shm_handle *handle,
484 struct lib_ring_buffer *buf,
485 unsigned long *len)
486{
487 unsigned long mmap_buf_len;
488 struct channel *chan = handle->shadow_chan;
489
490 if (chan->backend.config.output != RING_BUFFER_MMAP)
491 return -EINVAL;
492 mmap_buf_len = chan->backend.buf_size;
493 if (chan->backend.extra_reader_sb)
494 mmap_buf_len += chan->backend.subbuf_size;
495 if (mmap_buf_len > INT_MAX)
496 return -EFBIG;
497 *len = mmap_buf_len;
498 return 0;
499}
500
501/* returns the maximum size for sub-buffers. */
502int ustctl_get_max_subbuf_size(struct shm_handle *handle,
503 struct lib_ring_buffer *buf,
504 unsigned long *len)
505{
506 struct channel *chan = handle->shadow_chan;
507
508 *len = chan->backend.subbuf_size;
509 return 0;
510}
511
512/*
513 * For mmap mode, operate on the current packet (between get/put or
514 * get_next/put_next).
515 */
516
517/* returns the offset of the subbuffer belonging to the mmap reader. */
518int ustctl_get_mmap_read_offset(struct shm_handle *handle,
519 struct lib_ring_buffer *buf, unsigned long *off)
520{
521 struct channel *chan = handle->shadow_chan;
522 unsigned long sb_bindex;
523
524 if (chan->backend.config.output != RING_BUFFER_MMAP)
525 return -EINVAL;
526 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
527 buf->backend.buf_rsb.id);
528 *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
529 return 0;
530}
531
532/* returns the size of the current sub-buffer, without padding (for mmap). */
533int ustctl_get_subbuf_size(struct shm_handle *handle,
534 struct lib_ring_buffer *buf, unsigned long *len)
535{
536 struct channel *chan = handle->shadow_chan;
537
538 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
539 handle);
540 return 0;
541}
542
543/* returns the size of the current sub-buffer, without padding (for mmap). */
544int ustctl_get_padded_subbuf_size(struct shm_handle *handle,
545 struct lib_ring_buffer *buf, unsigned long *len)
546{
547 struct channel *chan = handle->shadow_chan;
548
549 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
550 handle);
551 *len = PAGE_ALIGN(*len);
552 return 0;
553}
554
555/* Get exclusive read access to the next sub-buffer that can be read. */
556int ustctl_get_next_subbuf(struct shm_handle *handle,
557 struct lib_ring_buffer *buf)
558{
559 return lib_ring_buffer_get_next_subbuf(buf, handle);
560}
561
562
563/* Release exclusive sub-buffer access, move consumer forward. */
564int ustctl_put_next_subbuf(struct shm_handle *handle,
565 struct lib_ring_buffer *buf)
566{
567 lib_ring_buffer_put_next_subbuf(buf, handle);
568 return 0;
569}
570
571/* snapshot */
572
573/* Get a snapshot of the current ring buffer producer and consumer positions */
574int ustctl_snapshot(struct shm_handle *handle,
575 struct lib_ring_buffer *buf)
576{
577 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
578 &buf->prod_snapshot, handle);
579}
580
581/* Get the consumer position (iteration start) */
582int ustctl_snapshot_get_consumed(struct shm_handle *handle,
583 struct lib_ring_buffer *buf, unsigned long *pos)
584{
585 *pos = buf->cons_snapshot;
586 return 0;
587}
588
589/* Get the producer position (iteration end) */
590int ustctl_snapshot_get_produced(struct shm_handle *handle,
591 struct lib_ring_buffer *buf, unsigned long *pos)
592{
593 *pos = buf->prod_snapshot;
594 return 0;
595}
596
597/* Get exclusive read access to the specified sub-buffer position */
598int ustctl_get_subbuf(struct shm_handle *handle,
599 struct lib_ring_buffer *buf, unsigned long *pos)
600{
601 return lib_ring_buffer_get_subbuf(buf, *pos, handle);
602}
603
604/* Release exclusive sub-buffer access */
605int ustctl_put_subbuf(struct shm_handle *handle,
606 struct lib_ring_buffer *buf)
607{
608 lib_ring_buffer_put_subbuf(buf, handle);
609 return 0;
610}
611
612int ustctl_buffer_flush(struct shm_handle *handle,
613 struct lib_ring_buffer *buf)
614{
615 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle);
616 return 0;
617}
This page took 0.044556 seconds and 4 git commands to generate.