3d886d3305c569f7df4092ae90ec076a932e0695
[ust.git] / libustctl / 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 <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
29 static
30 void 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
38 void 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 */
59 int 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 */
78 int 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
126 error:
127 release_object(sock, metadata_data);
128 return -EINVAL;
129 }
130
131 int 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
179 error:
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 */
189 int 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
230 error:
231 release_object(sock, stream_data);
232 return -EINVAL;
233 }
234
235 int 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
265 int 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 */
294 int 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 */
311 int 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
327 int ustctl_start_session(int sock, struct object_data *object)
328 {
329 return ustctl_enable(sock, object);
330 }
331
332 int ustctl_stop_session(int sock, struct object_data *object)
333 {
334 return ustctl_disable(sock, object);
335 }
336
337
338 int ustctl_tracepoint_list(int sock)
339 {
340 return -ENOSYS; /* not implemented */
341 }
342
343 int 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
360 int 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
376 int 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 */
384 struct 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 */
422 int 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
447 /* For mmap mode, readable without "get" operation */
448
449 /* returns the length to mmap. */
450 int ustctl_get_mmap_len(struct shm_handle *handle,
451 struct lib_ring_buffer *buf,
452 unsigned long *len)
453 {
454 unsigned long mmap_buf_len;
455 struct channel *chan = handle->shadow_chan;
456
457 if (chan->backend.config.output != RING_BUFFER_MMAP)
458 return -EINVAL;
459 mmap_buf_len = chan->backend.buf_size;
460 if (chan->backend.extra_reader_sb)
461 mmap_buf_len += chan->backend.subbuf_size;
462 if (mmap_buf_len > INT_MAX)
463 return -EFBIG;
464 *len = mmap_buf_len;
465 return 0;
466 }
467
468 /* returns the maximum size for sub-buffers. */
469 int ustctl_get_max_subbuf_size(struct shm_handle *handle,
470 struct lib_ring_buffer *buf,
471 unsigned long *len)
472 {
473 struct channel *chan = handle->shadow_chan;
474
475 *len = chan->backend.subbuf_size;
476 return 0;
477 }
478
479 /*
480 * For mmap mode, operate on the current packet (between get/put or
481 * get_next/put_next).
482 */
483
484 /* returns the offset of the subbuffer belonging to the mmap reader. */
485 int ustctl_get_mmap_read_offset(struct shm_handle *handle,
486 struct lib_ring_buffer *buf, unsigned long *off)
487 {
488 struct channel *chan = handle->shadow_chan;
489 unsigned long sb_bindex;
490
491 if (chan->backend.config.output != RING_BUFFER_MMAP)
492 return -EINVAL;
493 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
494 buf->backend.buf_rsb.id);
495 *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
496 return 0;
497 }
498
499 /* returns the size of the current sub-buffer, without padding (for mmap). */
500 int ustctl_get_subbuf_size(struct shm_handle *handle,
501 struct lib_ring_buffer *buf, unsigned long *len)
502 {
503 struct channel *chan = handle->shadow_chan;
504
505 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
506 handle);
507 return 0;
508 }
509
510 /* returns the size of the current sub-buffer, without padding (for mmap). */
511 int ustctl_get_padded_subbuf_size(struct shm_handle *handle,
512 struct lib_ring_buffer *buf, unsigned long *len)
513 {
514 struct channel *chan = handle->shadow_chan;
515
516 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
517 handle);
518 *len = PAGE_ALIGN(*len);
519 return 0;
520 }
521
522 /* Get exclusive read access to the next sub-buffer that can be read. */
523 int ustctl_get_next_subbuf(struct shm_handle *handle,
524 struct lib_ring_buffer *buf)
525 {
526 return lib_ring_buffer_get_next_subbuf(buf, handle);
527 }
528
529
530 /* Release exclusive sub-buffer access, move consumer forward. */
531 int ustctl_put_next_subbuf(struct shm_handle *handle,
532 struct lib_ring_buffer *buf)
533 {
534 lib_ring_buffer_put_next_subbuf(buf, handle);
535 return 0;
536 }
537
538 /* snapshot */
539
540 /* Get a snapshot of the current ring buffer producer and consumer positions */
541 int ustctl_snapshot(struct shm_handle *handle,
542 struct lib_ring_buffer *buf)
543 {
544 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
545 &buf->prod_snapshot, handle);
546 }
547
548 /* Get the consumer position (iteration start) */
549 int ustctl_snapshot_get_consumed(struct shm_handle *handle,
550 struct lib_ring_buffer *buf, unsigned long *pos)
551 {
552 *pos = buf->cons_snapshot;
553 return 0;
554 }
555
556 /* Get the producer position (iteration end) */
557 int ustctl_snapshot_get_produced(struct shm_handle *handle,
558 struct lib_ring_buffer *buf, unsigned long *pos)
559 {
560 *pos = buf->prod_snapshot;
561 return 0;
562 }
563
564 /* Get exclusive read access to the specified sub-buffer position */
565 int ustctl_get_subbuf(struct shm_handle *handle,
566 struct lib_ring_buffer *buf, unsigned long *pos)
567 {
568 return lib_ring_buffer_get_subbuf(buf, *pos, handle);
569 }
570
571 /* Release exclusive sub-buffer access */
572 int ustctl_put_subbuf(struct shm_handle *handle,
573 struct lib_ring_buffer *buf)
574 {
575 lib_ring_buffer_put_subbuf(buf, handle);
576 return 0;
577 }
578
579 int ustctl_buffer_flush(struct shm_handle *handle,
580 struct lib_ring_buffer *buf)
581 {
582 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle);
583 return 0;
584 }
This page took 0.042466 seconds and 4 git commands to generate.