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