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