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