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