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