Commit | Line | Data |
---|---|---|
57773204 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
74d81a6c | 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
57773204 | 4 | * |
e92f3e28 MD |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2 of the License only. | |
57773204 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
e92f3e28 MD |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
57773204 MD |
17 | */ |
18 | ||
9d335227 | 19 | #define _GNU_SOURCE |
57773204 | 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> |
32ce8569 | 25 | #include <byteswap.h> |
44c72f10 MD |
26 | |
27 | #include <usterr-signal-safe.h> | |
b728d87e | 28 | #include <ust-comm.h> |
74d81a6c | 29 | #include <helper.h> |
57773204 MD |
30 | |
31 | #include "../libringbuffer/backend.h" | |
32 | #include "../libringbuffer/frontend.h" | |
c9023c93 | 33 | #include "../liblttng-ust/wait.h" |
b2f3252a | 34 | #include "../liblttng-ust/lttng-rb-clients.h" |
c9023c93 MD |
35 | |
36 | /* | |
37 | * Number of milliseconds to retry before failing metadata writes on | |
38 | * buffer full condition. (10 seconds) | |
39 | */ | |
40 | #define LTTNG_METADATA_TIMEOUT_MSEC 10000 | |
57773204 | 41 | |
74d81a6c MD |
42 | /* |
43 | * Channel representation within consumer. | |
44 | */ | |
45 | struct ustctl_consumer_channel { | |
46 | struct lttng_channel *chan; /* lttng channel buffers */ | |
6b120308 | 47 | |
74d81a6c MD |
48 | /* initial attributes */ |
49 | struct ustctl_consumer_channel_attr attr; | |
ff0f5728 MD |
50 | int wait_fd; /* monitor close() */ |
51 | int wakeup_fd; /* monitor close() */ | |
74d81a6c MD |
52 | }; |
53 | ||
54 | /* | |
55 | * Stream representation within consumer. | |
56 | */ | |
57 | struct ustctl_consumer_stream { | |
58 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ | |
59 | struct lttng_ust_lib_ring_buffer *buf; | |
60 | struct ustctl_consumer_channel *chan; | |
61 | int shm_fd, wait_fd, wakeup_fd; | |
62 | int cpu; | |
63 | uint64_t memory_map_size; | |
64 | }; | |
65 | ||
66 | extern void lttng_ring_buffer_client_overwrite_init(void); | |
08a3170c | 67 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
74d81a6c | 68 | extern void lttng_ring_buffer_client_discard_init(void); |
08a3170c | 69 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
74d81a6c MD |
70 | extern void lttng_ring_buffer_metadata_client_init(void); |
71 | extern void lttng_ring_buffer_client_overwrite_exit(void); | |
08a3170c | 72 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
74d81a6c | 73 | extern void lttng_ring_buffer_client_discard_exit(void); |
08a3170c | 74 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
74d81a6c MD |
75 | extern void lttng_ring_buffer_metadata_client_exit(void); |
76 | ||
77 | volatile enum ust_loglevel ust_loglevel; | |
57773204 | 78 | |
2be0e72c MD |
79 | int ustctl_release_handle(int sock, int handle) |
80 | { | |
81 | struct ustcomm_ust_msg lum; | |
82 | struct ustcomm_ust_reply lur; | |
2be0e72c | 83 | |
74d81a6c MD |
84 | if (sock < 0 || handle < 0) |
85 | return 0; | |
86 | memset(&lum, 0, sizeof(lum)); | |
87 | lum.handle = handle; | |
88 | lum.cmd = LTTNG_UST_RELEASE; | |
89 | return ustcomm_send_app_cmd(sock, &lum, &lur); | |
2be0e72c | 90 | } |
74d81a6c | 91 | |
12388166 MD |
92 | /* |
93 | * If sock is negative, it means we don't have to notify the other side | |
94 | * (e.g. application has already vanished). | |
95 | */ | |
d26228ae | 96 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
57773204 | 97 | { |
57773204 MD |
98 | int ret; |
99 | ||
9bfc503d MD |
100 | if (!data) |
101 | return -EINVAL; | |
102 | ||
74d81a6c MD |
103 | switch (data->type) { |
104 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: | |
ff0f5728 MD |
105 | if (data->u.channel.wakeup_fd >= 0) { |
106 | ret = close(data->u.channel.wakeup_fd); | |
107 | if (ret < 0) { | |
108 | ret = -errno; | |
109 | return ret; | |
110 | } | |
111 | } | |
74d81a6c MD |
112 | free(data->u.channel.data); |
113 | break; | |
114 | case LTTNG_UST_OBJECT_TYPE_STREAM: | |
115 | if (data->u.stream.shm_fd >= 0) { | |
116 | ret = close(data->u.stream.shm_fd); | |
117 | if (ret < 0) { | |
118 | ret = -errno; | |
119 | return ret; | |
120 | } | |
d26228ae | 121 | } |
74d81a6c MD |
122 | if (data->u.stream.wakeup_fd >= 0) { |
123 | ret = close(data->u.stream.wakeup_fd); | |
124 | if (ret < 0) { | |
125 | ret = -errno; | |
126 | return ret; | |
127 | } | |
d26228ae | 128 | } |
74d81a6c | 129 | break; |
32ce8569 MD |
130 | case LTTNG_UST_OBJECT_TYPE_EVENT: |
131 | case LTTNG_UST_OBJECT_TYPE_CONTEXT: | |
132 | break; | |
74d81a6c MD |
133 | default: |
134 | assert(0); | |
d26228ae | 135 | } |
2be0e72c | 136 | return ustctl_release_handle(sock, data->handle); |
57773204 MD |
137 | } |
138 | ||
1c5e467e MD |
139 | /* |
140 | * Send registration done packet to the application. | |
141 | */ | |
142 | int ustctl_register_done(int sock) | |
143 | { | |
144 | struct ustcomm_ust_msg lum; | |
145 | struct ustcomm_ust_reply lur; | |
146 | int ret; | |
147 | ||
148 | DBG("Sending register done command to %d", sock); | |
149 | memset(&lum, 0, sizeof(lum)); | |
150 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
151 | lum.cmd = LTTNG_UST_REGISTER_DONE; | |
152 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
153 | if (ret) | |
154 | return ret; | |
1c5e467e | 155 | return 0; |
1c5e467e MD |
156 | } |
157 | ||
57773204 MD |
158 | /* |
159 | * returns session handle. | |
160 | */ | |
161 | int ustctl_create_session(int sock) | |
162 | { | |
163 | struct ustcomm_ust_msg lum; | |
164 | struct ustcomm_ust_reply lur; | |
165 | int ret, session_handle; | |
166 | ||
167 | /* Create session */ | |
168 | memset(&lum, 0, sizeof(lum)); | |
169 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
170 | lum.cmd = LTTNG_UST_SESSION; | |
171 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
172 | if (ret) | |
173 | return ret; | |
174 | session_handle = lur.ret_val; | |
175 | DBG("received session handle %u", session_handle); | |
176 | return session_handle; | |
177 | } | |
178 | ||
57773204 | 179 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
61f02aea MD |
180 | struct lttng_ust_object_data *channel_data, |
181 | struct lttng_ust_object_data **_event_data) | |
57773204 MD |
182 | { |
183 | struct ustcomm_ust_msg lum; | |
184 | struct ustcomm_ust_reply lur; | |
61f02aea | 185 | struct lttng_ust_object_data *event_data; |
57773204 MD |
186 | int ret; |
187 | ||
9bfc503d MD |
188 | if (!channel_data || !_event_data) |
189 | return -EINVAL; | |
190 | ||
74d81a6c | 191 | event_data = zmalloc(sizeof(*event_data)); |
57773204 MD |
192 | if (!event_data) |
193 | return -ENOMEM; | |
32ce8569 | 194 | event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT; |
57773204 MD |
195 | memset(&lum, 0, sizeof(lum)); |
196 | lum.handle = channel_data->handle; | |
197 | lum.cmd = LTTNG_UST_EVENT; | |
198 | strncpy(lum.u.event.name, ev->name, | |
199 | LTTNG_UST_SYM_NAME_LEN); | |
200 | lum.u.event.instrumentation = ev->instrumentation; | |
457a6b58 MD |
201 | lum.u.event.loglevel_type = ev->loglevel_type; |
202 | lum.u.event.loglevel = ev->loglevel; | |
57773204 MD |
203 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
204 | if (ret) { | |
205 | free(event_data); | |
206 | return ret; | |
207 | } | |
208 | event_data->handle = lur.ret_val; | |
209 | DBG("received event handle %u", event_data->handle); | |
210 | *_event_data = event_data; | |
211 | return 0; | |
212 | } | |
213 | ||
214 | int ustctl_add_context(int sock, struct lttng_ust_context *ctx, | |
61f02aea MD |
215 | struct lttng_ust_object_data *obj_data, |
216 | struct lttng_ust_object_data **_context_data) | |
57773204 MD |
217 | { |
218 | struct ustcomm_ust_msg lum; | |
219 | struct ustcomm_ust_reply lur; | |
61f02aea | 220 | struct lttng_ust_object_data *context_data; |
57773204 MD |
221 | int ret; |
222 | ||
9bfc503d MD |
223 | if (!obj_data || !_context_data) |
224 | return -EINVAL; | |
225 | ||
74d81a6c | 226 | context_data = zmalloc(sizeof(*context_data)); |
57773204 MD |
227 | if (!context_data) |
228 | return -ENOMEM; | |
32ce8569 | 229 | context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT; |
57773204 | 230 | memset(&lum, 0, sizeof(lum)); |
3039d8ed | 231 | lum.handle = obj_data->handle; |
57773204 | 232 | lum.cmd = LTTNG_UST_CONTEXT; |
d58d1454 | 233 | lum.u.context = *ctx; |
57773204 MD |
234 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
235 | if (ret) { | |
236 | free(context_data); | |
237 | return ret; | |
238 | } | |
32ce8569 MD |
239 | context_data->handle = -1; |
240 | DBG("Context created successfully"); | |
57773204 MD |
241 | *_context_data = context_data; |
242 | return ret; | |
243 | } | |
244 | ||
cd54f6d9 MD |
245 | int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode, |
246 | struct lttng_ust_object_data *obj_data) | |
247 | { | |
248 | struct ustcomm_ust_msg lum; | |
249 | struct ustcomm_ust_reply lur; | |
250 | int ret; | |
251 | ||
252 | if (!obj_data) | |
253 | return -EINVAL; | |
254 | ||
255 | memset(&lum, 0, sizeof(lum)); | |
256 | lum.handle = obj_data->handle; | |
257 | lum.cmd = LTTNG_UST_FILTER; | |
258 | lum.u.filter.data_size = bytecode->len; | |
259 | lum.u.filter.reloc_offset = bytecode->reloc_offset; | |
e695af51 | 260 | lum.u.filter.seqnum = bytecode->seqnum; |
cd54f6d9 MD |
261 | |
262 | ret = ustcomm_send_app_msg(sock, &lum); | |
263 | if (ret) | |
264 | return ret; | |
cd54f6d9 MD |
265 | /* send var len bytecode */ |
266 | ret = ustcomm_send_unix_sock(sock, bytecode->data, | |
267 | bytecode->len); | |
268 | if (ret < 0) { | |
269 | return ret; | |
270 | } | |
7bc53e94 MD |
271 | if (ret != bytecode->len) |
272 | return -EINVAL; | |
273 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
cd54f6d9 MD |
274 | } |
275 | ||
da57c034 JI |
276 | int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion, |
277 | struct lttng_ust_object_data *obj_data) | |
278 | { | |
279 | struct ustcomm_ust_msg lum; | |
280 | struct ustcomm_ust_reply lur; | |
281 | int ret; | |
282 | ||
283 | if (!obj_data) { | |
284 | return -EINVAL; | |
285 | } | |
286 | ||
287 | memset(&lum, 0, sizeof(lum)); | |
288 | lum.handle = obj_data->handle; | |
289 | lum.cmd = LTTNG_UST_EXCLUSION; | |
290 | lum.u.exclusion.count = exclusion->count; | |
291 | ||
292 | ret = ustcomm_send_app_msg(sock, &lum); | |
293 | if (ret) { | |
294 | return ret; | |
295 | } | |
296 | ||
1628366f | 297 | /* send var len exclusion names */ |
da57c034 JI |
298 | ret = ustcomm_send_unix_sock(sock, |
299 | exclusion->names, | |
300 | exclusion->count * LTTNG_UST_SYM_NAME_LEN); | |
301 | if (ret < 0) { | |
302 | return ret; | |
303 | } | |
304 | if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) { | |
305 | return -EINVAL; | |
306 | } | |
307 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
308 | } | |
309 | ||
57773204 | 310 | /* Enable event, channel and session ioctl */ |
61f02aea | 311 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
312 | { |
313 | struct ustcomm_ust_msg lum; | |
314 | struct ustcomm_ust_reply lur; | |
315 | int ret; | |
316 | ||
9bfc503d MD |
317 | if (!object) |
318 | return -EINVAL; | |
319 | ||
57773204 MD |
320 | memset(&lum, 0, sizeof(lum)); |
321 | lum.handle = object->handle; | |
322 | lum.cmd = LTTNG_UST_ENABLE; | |
323 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
324 | if (ret) | |
325 | return ret; | |
326 | DBG("enabled handle %u", object->handle); | |
327 | return 0; | |
328 | } | |
329 | ||
330 | /* Disable event, channel and session ioctl */ | |
61f02aea | 331 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
332 | { |
333 | struct ustcomm_ust_msg lum; | |
334 | struct ustcomm_ust_reply lur; | |
335 | int ret; | |
336 | ||
9bfc503d MD |
337 | if (!object) |
338 | return -EINVAL; | |
339 | ||
57773204 MD |
340 | memset(&lum, 0, sizeof(lum)); |
341 | lum.handle = object->handle; | |
342 | lum.cmd = LTTNG_UST_DISABLE; | |
343 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
344 | if (ret) | |
345 | return ret; | |
346 | DBG("disable handle %u", object->handle); | |
347 | return 0; | |
348 | } | |
349 | ||
4a6ca058 | 350 | int ustctl_start_session(int sock, int handle) |
57773204 | 351 | { |
61f02aea | 352 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
353 | |
354 | obj.handle = handle; | |
355 | return ustctl_enable(sock, &obj); | |
57773204 MD |
356 | } |
357 | ||
4a6ca058 | 358 | int ustctl_stop_session(int sock, int handle) |
57773204 | 359 | { |
61f02aea | 360 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
361 | |
362 | obj.handle = handle; | |
363 | return ustctl_disable(sock, &obj); | |
57773204 MD |
364 | } |
365 | ||
57773204 MD |
366 | int ustctl_tracepoint_list(int sock) |
367 | { | |
b115631f MD |
368 | struct ustcomm_ust_msg lum; |
369 | struct ustcomm_ust_reply lur; | |
370 | int ret, tp_list_handle; | |
371 | ||
372 | memset(&lum, 0, sizeof(lum)); | |
373 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
374 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; | |
375 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
376 | if (ret) | |
377 | return ret; | |
378 | tp_list_handle = lur.ret_val; | |
379 | DBG("received tracepoint list handle %u", tp_list_handle); | |
380 | return tp_list_handle; | |
381 | } | |
382 | ||
383 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, | |
cbef6901 | 384 | struct lttng_ust_tracepoint_iter *iter) |
b115631f MD |
385 | { |
386 | struct ustcomm_ust_msg lum; | |
387 | struct ustcomm_ust_reply lur; | |
388 | int ret; | |
389 | ||
9bfc503d MD |
390 | if (!iter) |
391 | return -EINVAL; | |
392 | ||
b115631f MD |
393 | memset(&lum, 0, sizeof(lum)); |
394 | lum.handle = tp_list_handle; | |
395 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; | |
396 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
397 | if (ret) | |
398 | return ret; | |
882a56d7 | 399 | DBG("received tracepoint list entry name %s loglevel %d", |
cbef6901 | 400 | lur.u.tracepoint.name, |
882a56d7 | 401 | lur.u.tracepoint.loglevel); |
cbef6901 | 402 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
b115631f | 403 | return 0; |
57773204 MD |
404 | } |
405 | ||
40003310 MD |
406 | int ustctl_tracepoint_field_list(int sock) |
407 | { | |
408 | struct ustcomm_ust_msg lum; | |
409 | struct ustcomm_ust_reply lur; | |
410 | int ret, tp_field_list_handle; | |
411 | ||
412 | memset(&lum, 0, sizeof(lum)); | |
413 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
414 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST; | |
415 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
416 | if (ret) | |
417 | return ret; | |
418 | tp_field_list_handle = lur.ret_val; | |
419 | DBG("received tracepoint field list handle %u", tp_field_list_handle); | |
420 | return tp_field_list_handle; | |
421 | } | |
422 | ||
423 | int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle, | |
424 | struct lttng_ust_field_iter *iter) | |
425 | { | |
426 | struct ustcomm_ust_msg lum; | |
427 | struct ustcomm_ust_reply lur; | |
428 | int ret; | |
429 | ssize_t len; | |
430 | ||
431 | if (!iter) | |
432 | return -EINVAL; | |
433 | ||
434 | memset(&lum, 0, sizeof(lum)); | |
435 | lum.handle = tp_field_list_handle; | |
436 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET; | |
437 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
438 | if (ret) | |
439 | return ret; | |
440 | len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter)); | |
441 | if (len != sizeof(*iter)) { | |
442 | return -EINVAL; | |
443 | } | |
444 | DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d", | |
445 | iter->event_name, | |
446 | iter->loglevel, | |
447 | iter->field_name, | |
448 | iter->type); | |
449 | return 0; | |
450 | } | |
451 | ||
57773204 MD |
452 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
453 | { | |
454 | struct ustcomm_ust_msg lum; | |
455 | struct ustcomm_ust_reply lur; | |
456 | int ret; | |
457 | ||
9bfc503d MD |
458 | if (!v) |
459 | return -EINVAL; | |
460 | ||
57773204 MD |
461 | memset(&lum, 0, sizeof(lum)); |
462 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
463 | lum.cmd = LTTNG_UST_TRACER_VERSION; | |
464 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
465 | if (ret) | |
466 | return ret; | |
467 | memcpy(v, &lur.u.version, sizeof(*v)); | |
468 | DBG("received tracer version"); | |
469 | return 0; | |
470 | } | |
471 | ||
472 | int ustctl_wait_quiescent(int sock) | |
473 | { | |
474 | struct ustcomm_ust_msg lum; | |
475 | struct ustcomm_ust_reply lur; | |
476 | int ret; | |
477 | ||
478 | memset(&lum, 0, sizeof(lum)); | |
479 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
480 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; | |
481 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
482 | if (ret) | |
483 | return ret; | |
484 | DBG("waited for quiescent state"); | |
485 | return 0; | |
486 | } | |
487 | ||
488 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) | |
489 | { | |
9bfc503d MD |
490 | if (!calibrate) |
491 | return -EINVAL; | |
492 | ||
57773204 MD |
493 | return -ENOSYS; |
494 | } | |
495 | ||
f1fffc57 MD |
496 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
497 | { | |
498 | struct ustcomm_ust_msg lum; | |
499 | struct ustcomm_ust_reply lur; | |
500 | int ret; | |
501 | ||
9bfc503d MD |
502 | if (!object) |
503 | return -EINVAL; | |
504 | ||
f1fffc57 MD |
505 | memset(&lum, 0, sizeof(lum)); |
506 | lum.handle = object->handle; | |
507 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; | |
508 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
509 | if (ret) | |
510 | return ret; | |
511 | DBG("flushed buffer handle %u", object->handle); | |
512 | return 0; | |
513 | } | |
514 | ||
74d81a6c MD |
515 | static |
516 | int ustctl_send_channel(int sock, | |
517 | enum lttng_ust_chan_type type, | |
518 | void *data, | |
519 | uint64_t size, | |
ff0f5728 | 520 | int wakeup_fd, |
74d81a6c MD |
521 | int send_fd_only) |
522 | { | |
523 | ssize_t len; | |
524 | ||
525 | if (!send_fd_only) { | |
526 | /* Send mmap size */ | |
527 | len = ustcomm_send_unix_sock(sock, &size, sizeof(size)); | |
528 | if (len != sizeof(size)) { | |
529 | if (len < 0) | |
530 | return len; | |
531 | else | |
532 | return -EIO; | |
533 | } | |
534 | ||
535 | /* Send channel type */ | |
536 | len = ustcomm_send_unix_sock(sock, &type, sizeof(type)); | |
537 | if (len != sizeof(type)) { | |
538 | if (len < 0) | |
539 | return len; | |
540 | else | |
541 | return -EIO; | |
542 | } | |
543 | } | |
544 | ||
545 | /* Send channel data */ | |
546 | len = ustcomm_send_unix_sock(sock, data, size); | |
547 | if (len != size) { | |
548 | if (len < 0) | |
549 | return len; | |
550 | else | |
551 | return -EIO; | |
552 | } | |
57773204 | 553 | |
ff0f5728 MD |
554 | /* Send wakeup fd */ |
555 | len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1); | |
556 | if (len <= 0) { | |
557 | if (len < 0) | |
558 | return len; | |
559 | else | |
560 | return -EIO; | |
561 | } | |
74d81a6c MD |
562 | return 0; |
563 | } | |
564 | ||
565 | static | |
566 | int ustctl_send_stream(int sock, | |
567 | uint32_t stream_nr, | |
568 | uint64_t memory_map_size, | |
569 | int shm_fd, int wakeup_fd, | |
570 | int send_fd_only) | |
57773204 | 571 | { |
74d81a6c MD |
572 | ssize_t len; |
573 | int fds[2]; | |
574 | ||
575 | if (!send_fd_only) { | |
576 | if (shm_fd < 0) { | |
577 | /* finish iteration */ | |
578 | uint64_t v = -1; | |
579 | ||
580 | len = ustcomm_send_unix_sock(sock, &v, sizeof(v)); | |
581 | if (len != sizeof(v)) { | |
582 | if (len < 0) | |
583 | return len; | |
584 | else | |
585 | return -EIO; | |
586 | } | |
587 | return 0; | |
588 | } | |
589 | ||
590 | /* Send mmap size */ | |
591 | len = ustcomm_send_unix_sock(sock, &memory_map_size, | |
592 | sizeof(memory_map_size)); | |
593 | if (len != sizeof(memory_map_size)) { | |
594 | if (len < 0) | |
595 | return len; | |
596 | else | |
597 | return -EIO; | |
598 | } | |
599 | ||
600 | /* Send stream nr */ | |
601 | len = ustcomm_send_unix_sock(sock, &stream_nr, | |
602 | sizeof(stream_nr)); | |
603 | if (len != sizeof(stream_nr)) { | |
604 | if (len < 0) | |
605 | return len; | |
606 | else | |
607 | return -EIO; | |
608 | } | |
609 | } | |
610 | ||
611 | /* Send shm fd and wakeup fd */ | |
612 | fds[0] = shm_fd; | |
613 | fds[1] = wakeup_fd; | |
614 | len = ustcomm_send_fds_unix_sock(sock, fds, 2); | |
615 | if (len <= 0) { | |
616 | if (len < 0) | |
617 | return len; | |
618 | else | |
619 | return -EIO; | |
620 | } | |
621 | return 0; | |
622 | } | |
623 | ||
624 | int ustctl_recv_channel_from_consumer(int sock, | |
625 | struct lttng_ust_object_data **_channel_data) | |
626 | { | |
627 | struct lttng_ust_object_data *channel_data; | |
628 | ssize_t len; | |
ff0f5728 | 629 | int wakeup_fd; |
7a784989 | 630 | int ret; |
57773204 | 631 | |
74d81a6c MD |
632 | channel_data = zmalloc(sizeof(*channel_data)); |
633 | if (!channel_data) { | |
634 | ret = -ENOMEM; | |
635 | goto error_alloc; | |
636 | } | |
637 | channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL; | |
12f3dabc | 638 | channel_data->handle = -1; |
74d81a6c MD |
639 | |
640 | /* recv mmap size */ | |
641 | len = ustcomm_recv_unix_sock(sock, &channel_data->size, | |
642 | sizeof(channel_data->size)); | |
643 | if (len != sizeof(channel_data->size)) { | |
644 | if (len < 0) | |
645 | ret = len; | |
646 | else | |
647 | ret = -EINVAL; | |
648 | goto error; | |
649 | } | |
9bfc503d | 650 | |
74d81a6c MD |
651 | /* recv channel type */ |
652 | len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type, | |
653 | sizeof(channel_data->u.channel.type)); | |
654 | if (len != sizeof(channel_data->u.channel.type)) { | |
655 | if (len < 0) | |
656 | ret = len; | |
657 | else | |
658 | ret = -EINVAL; | |
659 | goto error; | |
660 | } | |
661 | ||
662 | /* recv channel data */ | |
663 | channel_data->u.channel.data = zmalloc(channel_data->size); | |
664 | if (!channel_data->u.channel.data) { | |
665 | ret = -ENOMEM; | |
666 | goto error; | |
667 | } | |
668 | len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data, | |
669 | channel_data->size); | |
670 | if (len != channel_data->size) { | |
671 | if (len < 0) | |
672 | ret = len; | |
673 | else | |
674 | ret = -EINVAL; | |
675 | goto error_recv_data; | |
676 | } | |
ff0f5728 MD |
677 | /* recv wakeup fd */ |
678 | len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); | |
679 | if (len <= 0) { | |
680 | if (len < 0) { | |
681 | ret = len; | |
682 | goto error_recv_data; | |
683 | } else { | |
684 | ret = -EIO; | |
685 | goto error_recv_data; | |
686 | } | |
687 | } | |
688 | channel_data->u.channel.wakeup_fd = wakeup_fd; | |
74d81a6c MD |
689 | *_channel_data = channel_data; |
690 | return 0; | |
691 | ||
692 | error_recv_data: | |
693 | free(channel_data->u.channel.data); | |
694 | error: | |
695 | free(channel_data); | |
696 | error_alloc: | |
697 | return ret; | |
698 | } | |
699 | ||
700 | int ustctl_recv_stream_from_consumer(int sock, | |
701 | struct lttng_ust_object_data **_stream_data) | |
702 | { | |
703 | struct lttng_ust_object_data *stream_data; | |
704 | ssize_t len; | |
705 | int ret; | |
706 | int fds[2]; | |
707 | ||
708 | stream_data = zmalloc(sizeof(*stream_data)); | |
709 | if (!stream_data) { | |
710 | ret = -ENOMEM; | |
711 | goto error_alloc; | |
57773204 | 712 | } |
74d81a6c MD |
713 | |
714 | stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM; | |
715 | stream_data->handle = -1; | |
716 | ||
717 | /* recv mmap size */ | |
718 | len = ustcomm_recv_unix_sock(sock, &stream_data->size, | |
719 | sizeof(stream_data->size)); | |
720 | if (len != sizeof(stream_data->size)) { | |
721 | if (len < 0) | |
722 | ret = len; | |
723 | else | |
724 | ret = -EINVAL; | |
725 | goto error; | |
726 | } | |
727 | if (stream_data->size == -1) { | |
728 | ret = -LTTNG_UST_ERR_NOENT; | |
729 | goto error; | |
730 | } | |
731 | ||
732 | /* recv stream nr */ | |
733 | len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr, | |
734 | sizeof(stream_data->u.stream.stream_nr)); | |
735 | if (len != sizeof(stream_data->u.stream.stream_nr)) { | |
736 | if (len < 0) | |
737 | ret = len; | |
738 | else | |
739 | ret = -EINVAL; | |
740 | goto error; | |
741 | } | |
742 | ||
743 | /* recv shm fd and wakeup fd */ | |
744 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); | |
745 | if (len <= 0) { | |
746 | if (len < 0) { | |
747 | ret = len; | |
748 | goto error; | |
749 | } else { | |
750 | ret = -EIO; | |
751 | goto error; | |
0bfe09ec | 752 | } |
0bfe09ec | 753 | } |
74d81a6c MD |
754 | stream_data->u.stream.shm_fd = fds[0]; |
755 | stream_data->u.stream.wakeup_fd = fds[1]; | |
756 | *_stream_data = stream_data; | |
757 | return 0; | |
0bfe09ec | 758 | |
74d81a6c MD |
759 | error: |
760 | free(stream_data); | |
761 | error_alloc: | |
762 | return ret; | |
763 | } | |
764 | ||
765 | int ustctl_send_channel_to_ust(int sock, int session_handle, | |
766 | struct lttng_ust_object_data *channel_data) | |
767 | { | |
768 | struct ustcomm_ust_msg lum; | |
769 | struct ustcomm_ust_reply lur; | |
770 | int ret; | |
771 | ||
772 | if (!channel_data) | |
773 | return -EINVAL; | |
774 | ||
775 | memset(&lum, 0, sizeof(lum)); | |
776 | lum.handle = session_handle; | |
777 | lum.cmd = LTTNG_UST_CHANNEL; | |
778 | lum.u.channel.len = channel_data->size; | |
779 | lum.u.channel.type = channel_data->u.channel.type; | |
780 | ret = ustcomm_send_app_msg(sock, &lum); | |
781 | if (ret) | |
782 | return ret; | |
783 | ||
784 | ret = ustctl_send_channel(sock, | |
785 | channel_data->u.channel.type, | |
786 | channel_data->u.channel.data, | |
787 | channel_data->size, | |
ff0f5728 | 788 | channel_data->u.channel.wakeup_fd, |
74d81a6c MD |
789 | 1); |
790 | if (ret) | |
791 | return ret; | |
792 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
793 | if (!ret) { | |
7f2348b8 | 794 | channel_data->handle = lur.ret_val; |
57773204 | 795 | } |
74d81a6c MD |
796 | return ret; |
797 | } | |
798 | ||
799 | int ustctl_send_stream_to_ust(int sock, | |
800 | struct lttng_ust_object_data *channel_data, | |
801 | struct lttng_ust_object_data *stream_data) | |
802 | { | |
803 | struct ustcomm_ust_msg lum; | |
804 | struct ustcomm_ust_reply lur; | |
805 | int ret; | |
806 | ||
807 | memset(&lum, 0, sizeof(lum)); | |
808 | lum.handle = channel_data->handle; | |
809 | lum.cmd = LTTNG_UST_STREAM; | |
810 | lum.u.stream.len = stream_data->size; | |
811 | lum.u.stream.stream_nr = stream_data->u.stream.stream_nr; | |
812 | ret = ustcomm_send_app_msg(sock, &lum); | |
813 | if (ret) | |
814 | return ret; | |
815 | ||
816 | assert(stream_data); | |
817 | assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM); | |
818 | ||
819 | ret = ustctl_send_stream(sock, | |
820 | stream_data->u.stream.stream_nr, | |
821 | stream_data->size, | |
822 | stream_data->u.stream.shm_fd, | |
823 | stream_data->u.stream.wakeup_fd, 1); | |
824 | if (ret) | |
825 | return ret; | |
826 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
827 | } | |
828 | ||
12f3dabc MD |
829 | int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest, |
830 | struct lttng_ust_object_data *src) | |
831 | { | |
832 | struct lttng_ust_object_data *obj; | |
833 | int ret; | |
834 | ||
835 | if (src->handle != -1) { | |
836 | ret = -EINVAL; | |
837 | goto error; | |
838 | } | |
839 | ||
840 | obj = zmalloc(sizeof(*obj)); | |
841 | if (!obj) { | |
842 | ret = -ENOMEM; | |
843 | goto error; | |
844 | } | |
845 | ||
846 | obj->type = src->type; | |
847 | obj->handle = src->handle; | |
848 | obj->size = src->size; | |
849 | ||
850 | switch (obj->type) { | |
851 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: | |
852 | { | |
853 | obj->u.channel.type = src->u.channel.type; | |
854 | if (src->u.channel.wakeup_fd >= 0) { | |
855 | obj->u.channel.wakeup_fd = | |
856 | dup(src->u.channel.wakeup_fd); | |
857 | if (obj->u.channel.wakeup_fd < 0) { | |
858 | ret = errno; | |
859 | goto chan_error_wakeup_fd; | |
860 | } | |
861 | } else { | |
862 | obj->u.channel.wakeup_fd = | |
863 | src->u.channel.wakeup_fd; | |
864 | } | |
865 | obj->u.channel.data = zmalloc(obj->size); | |
866 | if (!obj->u.channel.data) { | |
867 | ret = -ENOMEM; | |
868 | goto chan_error_alloc; | |
869 | } | |
870 | memcpy(obj->u.channel.data, src->u.channel.data, obj->size); | |
871 | break; | |
872 | ||
873 | chan_error_alloc: | |
874 | if (src->u.channel.wakeup_fd >= 0) { | |
875 | int closeret; | |
876 | ||
877 | closeret = close(obj->u.channel.wakeup_fd); | |
878 | if (closeret) { | |
879 | PERROR("close"); | |
880 | } | |
881 | } | |
882 | chan_error_wakeup_fd: | |
883 | goto error_type; | |
884 | ||
885 | } | |
886 | ||
887 | case LTTNG_UST_OBJECT_TYPE_STREAM: | |
888 | { | |
889 | obj->u.stream.stream_nr = src->u.stream.stream_nr; | |
890 | if (src->u.stream.wakeup_fd >= 0) { | |
891 | obj->u.stream.wakeup_fd = | |
892 | dup(src->u.stream.wakeup_fd); | |
893 | if (obj->u.stream.wakeup_fd < 0) { | |
894 | ret = errno; | |
895 | goto stream_error_wakeup_fd; | |
896 | } | |
897 | } else { | |
898 | obj->u.stream.wakeup_fd = | |
899 | src->u.stream.wakeup_fd; | |
900 | } | |
901 | ||
902 | if (src->u.stream.shm_fd >= 0) { | |
903 | obj->u.stream.shm_fd = | |
904 | dup(src->u.stream.shm_fd); | |
905 | if (obj->u.stream.shm_fd < 0) { | |
906 | ret = errno; | |
907 | goto stream_error_shm_fd; | |
908 | } | |
909 | } else { | |
910 | obj->u.stream.shm_fd = | |
911 | src->u.stream.shm_fd; | |
912 | } | |
913 | break; | |
914 | ||
915 | stream_error_shm_fd: | |
916 | if (src->u.stream.wakeup_fd >= 0) { | |
917 | int closeret; | |
918 | ||
919 | closeret = close(obj->u.stream.wakeup_fd); | |
920 | if (closeret) { | |
921 | PERROR("close"); | |
922 | } | |
923 | } | |
924 | stream_error_wakeup_fd: | |
925 | goto error_type; | |
926 | } | |
927 | ||
928 | default: | |
929 | ret = -EINVAL; | |
930 | goto error_type; | |
931 | } | |
932 | ||
933 | *dest = obj; | |
934 | return 0; | |
935 | ||
936 | error_type: | |
937 | free(obj); | |
938 | error: | |
939 | return ret; | |
940 | } | |
941 | ||
74d81a6c MD |
942 | |
943 | /* Buffer operations */ | |
944 | ||
5ea386c3 MD |
945 | int ustctl_get_nr_stream_per_channel(void) |
946 | { | |
947 | return num_possible_cpus(); | |
948 | } | |
949 | ||
74d81a6c | 950 | struct ustctl_consumer_channel * |
5ea386c3 MD |
951 | ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, |
952 | const int *stream_fds, int nr_stream_fds) | |
74d81a6c MD |
953 | { |
954 | struct ustctl_consumer_channel *chan; | |
955 | const char *transport_name; | |
956 | struct lttng_transport *transport; | |
957 | ||
958 | switch (attr->type) { | |
959 | case LTTNG_UST_CHAN_PER_CPU: | |
960 | if (attr->output == LTTNG_UST_MMAP) { | |
34a91bdb MD |
961 | if (attr->overwrite) { |
962 | if (attr->read_timer_interval == 0) { | |
963 | transport_name = "relay-overwrite-mmap"; | |
964 | } else { | |
965 | transport_name = "relay-overwrite-rt-mmap"; | |
966 | } | |
967 | } else { | |
968 | if (attr->read_timer_interval == 0) { | |
969 | transport_name = "relay-discard-mmap"; | |
970 | } else { | |
971 | transport_name = "relay-discard-rt-mmap"; | |
972 | } | |
973 | } | |
74d81a6c MD |
974 | } else { |
975 | return NULL; | |
976 | } | |
c1fca457 | 977 | break; |
74d81a6c MD |
978 | case LTTNG_UST_CHAN_METADATA: |
979 | if (attr->output == LTTNG_UST_MMAP) | |
980 | transport_name = "relay-metadata-mmap"; | |
981 | else | |
982 | return NULL; | |
c1fca457 MD |
983 | break; |
984 | default: | |
74d81a6c | 985 | transport_name = "<unknown>"; |
c1fca457 MD |
986 | return NULL; |
987 | } | |
74d81a6c MD |
988 | |
989 | transport = lttng_transport_find(transport_name); | |
990 | if (!transport) { | |
991 | DBG("LTTng transport %s not found\n", | |
32ce8569 | 992 | transport_name); |
74d81a6c | 993 | return NULL; |
7a784989 | 994 | } |
74d81a6c MD |
995 | |
996 | chan = zmalloc(sizeof(*chan)); | |
997 | if (!chan) | |
998 | return NULL; | |
999 | ||
1000 | chan->chan = transport->ops.channel_create(transport_name, NULL, | |
32ce8569 | 1001 | attr->subbuf_size, attr->num_subbuf, |
74d81a6c | 1002 | attr->switch_timer_interval, |
32ce8569 | 1003 | attr->read_timer_interval, |
a9ff648c | 1004 | attr->uuid, attr->chan_id, |
5ea386c3 | 1005 | stream_fds, nr_stream_fds); |
74d81a6c MD |
1006 | if (!chan->chan) { |
1007 | goto chan_error; | |
1008 | } | |
1009 | chan->chan->ops = &transport->ops; | |
1010 | memcpy(&chan->attr, attr, sizeof(chan->attr)); | |
cb7378b3 MD |
1011 | chan->wait_fd = ustctl_channel_get_wait_fd(chan); |
1012 | chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan); | |
74d81a6c MD |
1013 | return chan; |
1014 | ||
1015 | chan_error: | |
1016 | free(chan); | |
1017 | return NULL; | |
57773204 MD |
1018 | } |
1019 | ||
74d81a6c | 1020 | void ustctl_destroy_channel(struct ustctl_consumer_channel *chan) |
57773204 | 1021 | { |
b24e4e91 MD |
1022 | (void) ustctl_channel_close_wait_fd(chan); |
1023 | (void) ustctl_channel_close_wakeup_fd(chan); | |
74d81a6c MD |
1024 | chan->chan->ops->channel_destroy(chan->chan); |
1025 | free(chan); | |
1026 | } | |
1027 | ||
1028 | int ustctl_send_channel_to_sessiond(int sock, | |
1029 | struct ustctl_consumer_channel *channel) | |
1030 | { | |
1031 | struct shm_object_table *table; | |
57773204 | 1032 | |
74d81a6c MD |
1033 | table = channel->chan->handle->table; |
1034 | if (table->size <= 0) | |
9bfc503d | 1035 | return -EINVAL; |
74d81a6c MD |
1036 | return ustctl_send_channel(sock, |
1037 | channel->attr.type, | |
1038 | table->objects[0].memory_map, | |
1039 | table->objects[0].memory_map_size, | |
ff0f5728 | 1040 | channel->wakeup_fd, |
74d81a6c MD |
1041 | 0); |
1042 | } | |
9bfc503d | 1043 | |
74d81a6c MD |
1044 | int ustctl_send_stream_to_sessiond(int sock, |
1045 | struct ustctl_consumer_stream *stream) | |
1046 | { | |
1047 | if (!stream) | |
1048 | return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0); | |
1049 | ||
1050 | return ustctl_send_stream(sock, | |
1051 | stream->cpu, | |
1052 | stream->memory_map_size, | |
1053 | stream->shm_fd, stream->wakeup_fd, | |
1054 | 0); | |
57773204 MD |
1055 | } |
1056 | ||
c9023c93 MD |
1057 | int ustctl_write_metadata_to_channel( |
1058 | struct ustctl_consumer_channel *channel, | |
1059 | const char *metadata_str, /* NOT null-terminated */ | |
1060 | size_t len) /* metadata length */ | |
1061 | { | |
1062 | struct lttng_ust_lib_ring_buffer_ctx ctx; | |
1063 | struct lttng_channel *chan = channel->chan; | |
1064 | const char *str = metadata_str; | |
1065 | int ret = 0, waitret; | |
1066 | size_t reserve_len, pos; | |
1067 | ||
1068 | for (pos = 0; pos < len; pos += reserve_len) { | |
1069 | reserve_len = min_t(size_t, | |
1070 | chan->ops->packet_avail_size(chan->chan, chan->handle), | |
1071 | len - pos); | |
1072 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
1073 | sizeof(char), -1, chan->handle); | |
1074 | /* | |
1075 | * We don't care about metadata buffer's records lost | |
1076 | * count, because we always retry here. Report error if | |
1077 | * we need to bail out after timeout or being | |
1078 | * interrupted. | |
1079 | */ | |
1080 | waitret = wait_cond_interruptible_timeout( | |
1081 | ({ | |
1082 | ret = chan->ops->event_reserve(&ctx, 0); | |
1083 | ret != -ENOBUFS || !ret; | |
1084 | }), | |
1085 | LTTNG_METADATA_TIMEOUT_MSEC); | |
1086 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { | |
1087 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", | |
1088 | waitret == -EINTR ? "interrupted" : | |
1089 | (ret == -ENOBUFS ? "timeout" : "I/O error")); | |
1090 | if (waitret == -EINTR) | |
1091 | ret = waitret; | |
1092 | goto end; | |
1093 | } | |
1094 | chan->ops->event_write(&ctx, &str[pos], reserve_len); | |
1095 | chan->ops->event_commit(&ctx); | |
1096 | } | |
1097 | end: | |
1098 | return ret; | |
1099 | } | |
1100 | ||
3ef94b0e JD |
1101 | /* |
1102 | * Write at most one packet in the channel. | |
1103 | * Returns the number of bytes written on success, < 0 on error. | |
1104 | */ | |
1105 | ssize_t ustctl_write_one_packet_to_channel( | |
1106 | struct ustctl_consumer_channel *channel, | |
1107 | const char *metadata_str, /* NOT null-terminated */ | |
1108 | size_t len) /* metadata length */ | |
1109 | { | |
1110 | struct lttng_ust_lib_ring_buffer_ctx ctx; | |
1111 | struct lttng_channel *chan = channel->chan; | |
1112 | const char *str = metadata_str; | |
1113 | ssize_t reserve_len; | |
1114 | int ret; | |
1115 | ||
1116 | reserve_len = min_t(ssize_t, | |
1117 | chan->ops->packet_avail_size(chan->chan, chan->handle), | |
1118 | len); | |
1119 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
1120 | sizeof(char), -1, chan->handle); | |
1121 | ret = chan->ops->event_reserve(&ctx, 0); | |
1122 | if (ret != 0) { | |
1123 | DBG("LTTng: event reservation failed"); | |
1124 | assert(ret < 0); | |
1125 | reserve_len = ret; | |
1126 | goto end; | |
1127 | } | |
1128 | chan->ops->event_write(&ctx, str, reserve_len); | |
1129 | chan->ops->event_commit(&ctx); | |
1130 | ||
1131 | end: | |
1132 | return reserve_len; | |
1133 | } | |
1134 | ||
ff0f5728 MD |
1135 | int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan) |
1136 | { | |
1137 | struct channel *chan; | |
cb7378b3 | 1138 | int ret; |
ff0f5728 MD |
1139 | |
1140 | chan = consumer_chan->chan->chan; | |
cb7378b3 | 1141 | ret = ring_buffer_channel_close_wait_fd(&chan->backend.config, |
ff0f5728 | 1142 | chan, chan->handle); |
cb7378b3 MD |
1143 | if (!ret) |
1144 | consumer_chan->wait_fd = -1; | |
1145 | return ret; | |
ff0f5728 MD |
1146 | } |
1147 | ||
1148 | int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan) | |
1149 | { | |
1150 | struct channel *chan; | |
cb7378b3 | 1151 | int ret; |
ff0f5728 MD |
1152 | |
1153 | chan = consumer_chan->chan->chan; | |
cb7378b3 | 1154 | ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config, |
ff0f5728 | 1155 | chan, chan->handle); |
cb7378b3 MD |
1156 | if (!ret) |
1157 | consumer_chan->wakeup_fd = -1; | |
1158 | return ret; | |
ff0f5728 MD |
1159 | } |
1160 | ||
74d81a6c | 1161 | int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream) |
5224b5c8 MD |
1162 | { |
1163 | struct channel *chan; | |
1164 | ||
74d81a6c | 1165 | chan = stream->chan->chan->chan; |
ff0f5728 | 1166 | return ring_buffer_stream_close_wait_fd(&chan->backend.config, |
74d81a6c | 1167 | chan, stream->handle, stream->cpu); |
5224b5c8 MD |
1168 | } |
1169 | ||
74d81a6c | 1170 | int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream) |
6e922b24 | 1171 | { |
66bdd22a | 1172 | struct channel *chan; |
74d81a6c MD |
1173 | |
1174 | chan = stream->chan->chan->chan; | |
ff0f5728 | 1175 | return ring_buffer_stream_close_wakeup_fd(&chan->backend.config, |
74d81a6c MD |
1176 | chan, stream->handle, stream->cpu); |
1177 | } | |
1178 | ||
1179 | struct ustctl_consumer_stream * | |
1180 | ustctl_create_stream(struct ustctl_consumer_channel *channel, | |
1181 | int cpu) | |
1182 | { | |
1183 | struct ustctl_consumer_stream *stream; | |
1184 | struct lttng_ust_shm_handle *handle; | |
1185 | struct channel *chan; | |
1186 | int shm_fd, wait_fd, wakeup_fd; | |
1187 | uint64_t memory_map_size; | |
4cfec15c | 1188 | struct lttng_ust_lib_ring_buffer *buf; |
6e922b24 MD |
1189 | int ret; |
1190 | ||
74d81a6c MD |
1191 | if (!channel) |
1192 | return NULL; | |
1193 | handle = channel->chan->handle; | |
9bfc503d MD |
1194 | if (!handle) |
1195 | return NULL; | |
1196 | ||
74d81a6c | 1197 | chan = channel->chan->chan; |
6e922b24 | 1198 | buf = channel_get_ring_buffer(&chan->backend.config, |
74d81a6c MD |
1199 | chan, cpu, handle, &shm_fd, &wait_fd, |
1200 | &wakeup_fd, &memory_map_size); | |
6e922b24 MD |
1201 | if (!buf) |
1202 | return NULL; | |
74d81a6c | 1203 | ret = lib_ring_buffer_open_read(buf, handle); |
6e922b24 MD |
1204 | if (ret) |
1205 | return NULL; | |
74d81a6c MD |
1206 | |
1207 | stream = zmalloc(sizeof(*stream)); | |
1208 | if (!stream) | |
1209 | goto alloc_error; | |
1210 | stream->handle = handle; | |
1211 | stream->buf = buf; | |
1212 | stream->chan = channel; | |
1213 | stream->shm_fd = shm_fd; | |
1214 | stream->wait_fd = wait_fd; | |
1215 | stream->wakeup_fd = wakeup_fd; | |
1216 | stream->memory_map_size = memory_map_size; | |
1217 | stream->cpu = cpu; | |
1218 | return stream; | |
1219 | ||
1220 | alloc_error: | |
1221 | return NULL; | |
1222 | } | |
1223 | ||
1224 | void ustctl_destroy_stream(struct ustctl_consumer_stream *stream) | |
1225 | { | |
1226 | struct lttng_ust_lib_ring_buffer *buf; | |
1227 | struct ustctl_consumer_channel *consumer_chan; | |
1228 | ||
1229 | assert(stream); | |
1230 | buf = stream->buf; | |
1231 | consumer_chan = stream->chan; | |
b24e4e91 MD |
1232 | (void) ustctl_stream_close_wait_fd(stream); |
1233 | (void) ustctl_stream_close_wakeup_fd(stream); | |
74d81a6c MD |
1234 | lib_ring_buffer_release_read(buf, consumer_chan->chan->handle); |
1235 | free(stream); | |
6e922b24 MD |
1236 | } |
1237 | ||
ff0f5728 MD |
1238 | int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan) |
1239 | { | |
1240 | if (!chan) | |
1241 | return -EINVAL; | |
1242 | return shm_get_wait_fd(chan->chan->handle, | |
1243 | &chan->chan->handle->chan._ref); | |
1244 | } | |
1245 | ||
1246 | int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan) | |
1247 | { | |
1248 | if (!chan) | |
1249 | return -EINVAL; | |
1250 | return shm_get_wakeup_fd(chan->chan->handle, | |
1251 | &chan->chan->handle->chan._ref); | |
1252 | } | |
1253 | ||
1254 | int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream) | |
6e922b24 | 1255 | { |
74d81a6c MD |
1256 | struct lttng_ust_lib_ring_buffer *buf; |
1257 | struct ustctl_consumer_channel *consumer_chan; | |
1258 | ||
1259 | if (!stream) | |
1260 | return -EINVAL; | |
1261 | buf = stream->buf; | |
1262 | consumer_chan = stream->chan; | |
1263 | return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref); | |
1264 | } | |
1265 | ||
ff0f5728 | 1266 | int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream) |
74d81a6c MD |
1267 | { |
1268 | struct lttng_ust_lib_ring_buffer *buf; | |
1269 | struct ustctl_consumer_channel *consumer_chan; | |
1270 | ||
1271 | if (!stream) | |
1272 | return -EINVAL; | |
1273 | buf = stream->buf; | |
1274 | consumer_chan = stream->chan; | |
1275 | return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref); | |
6e922b24 MD |
1276 | } |
1277 | ||
57773204 MD |
1278 | /* For mmap mode, readable without "get" operation */ |
1279 | ||
74d81a6c | 1280 | void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream) |
9095efe9 | 1281 | { |
74d81a6c MD |
1282 | struct lttng_ust_lib_ring_buffer *buf; |
1283 | struct ustctl_consumer_channel *consumer_chan; | |
1284 | ||
1285 | if (!stream) | |
9bfc503d | 1286 | return NULL; |
74d81a6c MD |
1287 | buf = stream->buf; |
1288 | consumer_chan = stream->chan; | |
1289 | return shmp(consumer_chan->chan->handle, buf->backend.memory_map); | |
9095efe9 MD |
1290 | } |
1291 | ||
57773204 | 1292 | /* returns the length to mmap. */ |
74d81a6c | 1293 | int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream, |
57773204 MD |
1294 | unsigned long *len) |
1295 | { | |
74d81a6c | 1296 | struct ustctl_consumer_channel *consumer_chan; |
57773204 | 1297 | unsigned long mmap_buf_len; |
66bdd22a | 1298 | struct channel *chan; |
57773204 | 1299 | |
74d81a6c | 1300 | if (!stream) |
9bfc503d | 1301 | return -EINVAL; |
74d81a6c MD |
1302 | consumer_chan = stream->chan; |
1303 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1304 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
1305 | return -EINVAL; | |
1306 | mmap_buf_len = chan->backend.buf_size; | |
1307 | if (chan->backend.extra_reader_sb) | |
1308 | mmap_buf_len += chan->backend.subbuf_size; | |
1309 | if (mmap_buf_len > INT_MAX) | |
1310 | return -EFBIG; | |
1311 | *len = mmap_buf_len; | |
1312 | return 0; | |
1313 | } | |
1314 | ||
1315 | /* returns the maximum size for sub-buffers. */ | |
74d81a6c | 1316 | int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream, |
57773204 MD |
1317 | unsigned long *len) |
1318 | { | |
74d81a6c | 1319 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1320 | struct channel *chan; |
57773204 | 1321 | |
74d81a6c | 1322 | if (!stream) |
9bfc503d | 1323 | return -EINVAL; |
74d81a6c MD |
1324 | consumer_chan = stream->chan; |
1325 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1326 | *len = chan->backend.subbuf_size; |
1327 | return 0; | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * For mmap mode, operate on the current packet (between get/put or | |
1332 | * get_next/put_next). | |
1333 | */ | |
1334 | ||
1335 | /* returns the offset of the subbuffer belonging to the mmap reader. */ | |
74d81a6c MD |
1336 | int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream, |
1337 | unsigned long *off) | |
57773204 | 1338 | { |
66bdd22a | 1339 | struct channel *chan; |
57773204 | 1340 | unsigned long sb_bindex; |
74d81a6c MD |
1341 | struct lttng_ust_lib_ring_buffer *buf; |
1342 | struct ustctl_consumer_channel *consumer_chan; | |
57773204 | 1343 | |
74d81a6c | 1344 | if (!stream) |
9bfc503d | 1345 | return -EINVAL; |
74d81a6c MD |
1346 | buf = stream->buf; |
1347 | consumer_chan = stream->chan; | |
1348 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1349 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
1350 | return -EINVAL; | |
1351 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, | |
32ce8569 | 1352 | buf->backend.buf_rsb.id); |
74d81a6c MD |
1353 | *off = shmp(consumer_chan->chan->handle, |
1354 | shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset; | |
57773204 MD |
1355 | return 0; |
1356 | } | |
1357 | ||
1358 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
74d81a6c MD |
1359 | int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream, |
1360 | unsigned long *len) | |
57773204 | 1361 | { |
74d81a6c | 1362 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1363 | struct channel *chan; |
74d81a6c | 1364 | struct lttng_ust_lib_ring_buffer *buf; |
57773204 | 1365 | |
74d81a6c | 1366 | if (!stream) |
9bfc503d MD |
1367 | return -EINVAL; |
1368 | ||
74d81a6c MD |
1369 | buf = stream->buf; |
1370 | consumer_chan = stream->chan; | |
1371 | chan = consumer_chan->chan->chan; | |
57773204 | 1372 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
74d81a6c | 1373 | consumer_chan->chan->handle); |
57773204 MD |
1374 | return 0; |
1375 | } | |
1376 | ||
1377 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
74d81a6c MD |
1378 | int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream, |
1379 | unsigned long *len) | |
57773204 | 1380 | { |
74d81a6c | 1381 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1382 | struct channel *chan; |
74d81a6c | 1383 | struct lttng_ust_lib_ring_buffer *buf; |
57773204 | 1384 | |
74d81a6c | 1385 | if (!stream) |
9bfc503d | 1386 | return -EINVAL; |
74d81a6c MD |
1387 | buf = stream->buf; |
1388 | consumer_chan = stream->chan; | |
1389 | chan = consumer_chan->chan->chan; | |
57773204 | 1390 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
74d81a6c | 1391 | consumer_chan->chan->handle); |
57773204 MD |
1392 | *len = PAGE_ALIGN(*len); |
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | /* Get exclusive read access to the next sub-buffer that can be read. */ | |
74d81a6c | 1397 | int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1398 | { |
74d81a6c MD |
1399 | struct lttng_ust_lib_ring_buffer *buf; |
1400 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1401 | |
74d81a6c MD |
1402 | if (!stream) |
1403 | return -EINVAL; | |
1404 | buf = stream->buf; | |
1405 | consumer_chan = stream->chan; | |
1406 | return lib_ring_buffer_get_next_subbuf(buf, | |
1407 | consumer_chan->chan->handle); | |
57773204 MD |
1408 | } |
1409 | ||
1410 | ||
1411 | /* Release exclusive sub-buffer access, move consumer forward. */ | |
74d81a6c | 1412 | int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1413 | { |
74d81a6c MD |
1414 | struct lttng_ust_lib_ring_buffer *buf; |
1415 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1416 | |
74d81a6c MD |
1417 | if (!stream) |
1418 | return -EINVAL; | |
1419 | buf = stream->buf; | |
1420 | consumer_chan = stream->chan; | |
1421 | lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle); | |
57773204 MD |
1422 | return 0; |
1423 | } | |
1424 | ||
1425 | /* snapshot */ | |
1426 | ||
1427 | /* Get a snapshot of the current ring buffer producer and consumer positions */ | |
74d81a6c | 1428 | int ustctl_snapshot(struct ustctl_consumer_stream *stream) |
57773204 | 1429 | { |
74d81a6c MD |
1430 | struct lttng_ust_lib_ring_buffer *buf; |
1431 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1432 | |
74d81a6c MD |
1433 | if (!stream) |
1434 | return -EINVAL; | |
1435 | buf = stream->buf; | |
1436 | consumer_chan = stream->chan; | |
57773204 | 1437 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
74d81a6c | 1438 | &buf->prod_snapshot, consumer_chan->chan->handle); |
57773204 MD |
1439 | } |
1440 | ||
1441 | /* Get the consumer position (iteration start) */ | |
74d81a6c MD |
1442 | int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream, |
1443 | unsigned long *pos) | |
57773204 | 1444 | { |
74d81a6c | 1445 | struct lttng_ust_lib_ring_buffer *buf; |
9bfc503d | 1446 | |
74d81a6c MD |
1447 | if (!stream) |
1448 | return -EINVAL; | |
1449 | buf = stream->buf; | |
57773204 MD |
1450 | *pos = buf->cons_snapshot; |
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | /* Get the producer position (iteration end) */ | |
74d81a6c MD |
1455 | int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream, |
1456 | unsigned long *pos) | |
57773204 | 1457 | { |
74d81a6c | 1458 | struct lttng_ust_lib_ring_buffer *buf; |
9bfc503d | 1459 | |
74d81a6c MD |
1460 | if (!stream) |
1461 | return -EINVAL; | |
1462 | buf = stream->buf; | |
57773204 MD |
1463 | *pos = buf->prod_snapshot; |
1464 | return 0; | |
1465 | } | |
1466 | ||
1467 | /* Get exclusive read access to the specified sub-buffer position */ | |
74d81a6c MD |
1468 | int ustctl_get_subbuf(struct ustctl_consumer_stream *stream, |
1469 | unsigned long *pos) | |
57773204 | 1470 | { |
74d81a6c MD |
1471 | struct lttng_ust_lib_ring_buffer *buf; |
1472 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1473 | |
74d81a6c MD |
1474 | if (!stream) |
1475 | return -EINVAL; | |
1476 | buf = stream->buf; | |
1477 | consumer_chan = stream->chan; | |
1478 | return lib_ring_buffer_get_subbuf(buf, *pos, | |
1479 | consumer_chan->chan->handle); | |
57773204 MD |
1480 | } |
1481 | ||
1482 | /* Release exclusive sub-buffer access */ | |
74d81a6c | 1483 | int ustctl_put_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1484 | { |
74d81a6c MD |
1485 | struct lttng_ust_lib_ring_buffer *buf; |
1486 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1487 | |
74d81a6c MD |
1488 | if (!stream) |
1489 | return -EINVAL; | |
1490 | buf = stream->buf; | |
1491 | consumer_chan = stream->chan; | |
1492 | lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle); | |
57773204 MD |
1493 | return 0; |
1494 | } | |
1495 | ||
74d81a6c | 1496 | void ustctl_flush_buffer(struct ustctl_consumer_stream *stream, |
b52190f2 | 1497 | int producer_active) |
57773204 | 1498 | { |
74d81a6c MD |
1499 | struct lttng_ust_lib_ring_buffer *buf; |
1500 | struct ustctl_consumer_channel *consumer_chan; | |
1501 | ||
1502 | assert(stream); | |
1503 | buf = stream->buf; | |
1504 | consumer_chan = stream->chan; | |
b52190f2 MD |
1505 | lib_ring_buffer_switch_slow(buf, |
1506 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, | |
74d81a6c MD |
1507 | consumer_chan->chan->handle); |
1508 | } | |
1509 | ||
b2f3252a JD |
1510 | static |
1511 | struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb( | |
1512 | struct lttng_ust_lib_ring_buffer *buf, | |
1513 | struct lttng_ust_shm_handle *handle) | |
1514 | { | |
1515 | struct channel *chan; | |
1516 | const struct lttng_ust_lib_ring_buffer_config *config; | |
1517 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
1518 | ||
1519 | chan = shmp(handle, buf->backend.chan); | |
1520 | config = &chan->backend.config; | |
1521 | if (!config->cb_ptr) | |
1522 | return NULL; | |
1523 | client_cb = caa_container_of(config->cb_ptr, | |
1524 | struct lttng_ust_client_lib_ring_buffer_client_cb, | |
1525 | parent); | |
1526 | return client_cb; | |
1527 | } | |
1528 | ||
1529 | int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream, | |
1530 | uint64_t *timestamp_begin) | |
1531 | { | |
1532 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1533 | struct lttng_ust_lib_ring_buffer *buf; |
1534 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1535 | |
1536 | if (!stream || !timestamp_begin) | |
1537 | return -EINVAL; | |
e1919a41 MD |
1538 | buf = stream->buf; |
1539 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1540 | client_cb = get_client_cb(buf, handle); |
1541 | if (!client_cb) | |
1542 | return -ENOSYS; | |
1543 | return client_cb->timestamp_begin(buf, handle, timestamp_begin); | |
1544 | } | |
1545 | ||
1546 | int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream, | |
1547 | uint64_t *timestamp_end) | |
1548 | { | |
1549 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1550 | struct lttng_ust_lib_ring_buffer *buf; |
1551 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1552 | |
1553 | if (!stream || !timestamp_end) | |
1554 | return -EINVAL; | |
e1919a41 MD |
1555 | buf = stream->buf; |
1556 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1557 | client_cb = get_client_cb(buf, handle); |
1558 | if (!client_cb) | |
1559 | return -ENOSYS; | |
1560 | return client_cb->timestamp_end(buf, handle, timestamp_end); | |
1561 | } | |
1562 | ||
1563 | int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream, | |
1564 | uint64_t *events_discarded) | |
1565 | { | |
1566 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1567 | struct lttng_ust_lib_ring_buffer *buf; |
1568 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1569 | |
1570 | if (!stream || !events_discarded) | |
1571 | return -EINVAL; | |
e1919a41 MD |
1572 | buf = stream->buf; |
1573 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1574 | client_cb = get_client_cb(buf, handle); |
1575 | if (!client_cb) | |
1576 | return -ENOSYS; | |
1577 | return client_cb->events_discarded(buf, handle, events_discarded); | |
1578 | } | |
1579 | ||
1580 | int ustctl_get_content_size(struct ustctl_consumer_stream *stream, | |
1581 | uint64_t *content_size) | |
1582 | { | |
1583 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1584 | struct lttng_ust_lib_ring_buffer *buf; |
1585 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1586 | |
1587 | if (!stream || !content_size) | |
1588 | return -EINVAL; | |
e1919a41 MD |
1589 | buf = stream->buf; |
1590 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1591 | client_cb = get_client_cb(buf, handle); |
1592 | if (!client_cb) | |
1593 | return -ENOSYS; | |
1594 | return client_cb->content_size(buf, handle, content_size); | |
1595 | } | |
1596 | ||
1597 | int ustctl_get_packet_size(struct ustctl_consumer_stream *stream, | |
1598 | uint64_t *packet_size) | |
1599 | { | |
1600 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1601 | struct lttng_ust_lib_ring_buffer *buf; |
1602 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1603 | |
1604 | if (!stream || !packet_size) | |
1605 | return -EINVAL; | |
e1919a41 MD |
1606 | buf = stream->buf; |
1607 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1608 | client_cb = get_client_cb(buf, handle); |
1609 | if (!client_cb) | |
1610 | return -ENOSYS; | |
1611 | return client_cb->packet_size(buf, handle, packet_size); | |
1612 | } | |
1613 | ||
1614 | int ustctl_get_stream_id(struct ustctl_consumer_stream *stream, | |
1615 | uint64_t *stream_id) | |
1616 | { | |
1617 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1618 | struct lttng_ust_lib_ring_buffer *buf; |
1619 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1620 | |
1621 | if (!stream || !stream_id) | |
1622 | return -EINVAL; | |
e1919a41 MD |
1623 | buf = stream->buf; |
1624 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1625 | client_cb = get_client_cb(buf, handle); |
1626 | if (!client_cb) | |
1627 | return -ENOSYS; | |
1628 | return client_cb->stream_id(buf, handle, stream_id); | |
1629 | } | |
1630 | ||
fca361e8 JD |
1631 | int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream, |
1632 | uint64_t *ts) | |
1633 | { | |
1634 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1635 | struct lttng_ust_lib_ring_buffer *buf; |
1636 | struct lttng_ust_shm_handle *handle; | |
fca361e8 JD |
1637 | |
1638 | if (!stream || !ts) | |
1639 | return -EINVAL; | |
e1919a41 MD |
1640 | buf = stream->buf; |
1641 | handle = stream->chan->chan->handle; | |
fca361e8 JD |
1642 | client_cb = get_client_cb(buf, handle); |
1643 | if (!client_cb || !client_cb->current_timestamp) | |
1644 | return -ENOSYS; | |
1645 | return client_cb->current_timestamp(buf, handle, ts); | |
1646 | } | |
1647 | ||
57201bb3 MD |
1648 | #if defined(__x86_64__) || defined(__i386__) |
1649 | ||
1650 | int ustctl_has_perf_counters(void) | |
1651 | { | |
1652 | return 1; | |
1653 | } | |
1654 | ||
1655 | #else | |
1656 | ||
1657 | int ustctl_has_perf_counters(void) | |
1658 | { | |
1659 | return 0; | |
1660 | } | |
1661 | ||
1662 | #endif | |
1663 | ||
32ce8569 MD |
1664 | /* |
1665 | * Returns 0 on success, negative error value on error. | |
1666 | */ | |
1667 | int ustctl_recv_reg_msg(int sock, | |
1668 | enum ustctl_socket_type *type, | |
1669 | uint32_t *major, | |
1670 | uint32_t *minor, | |
1671 | uint32_t *pid, | |
1672 | uint32_t *ppid, | |
1673 | uint32_t *uid, | |
1674 | uint32_t *gid, | |
1675 | uint32_t *bits_per_long, | |
1676 | uint32_t *uint8_t_alignment, | |
1677 | uint32_t *uint16_t_alignment, | |
1678 | uint32_t *uint32_t_alignment, | |
1679 | uint32_t *uint64_t_alignment, | |
1680 | uint32_t *long_alignment, | |
1681 | int *byte_order, | |
1682 | char *name) | |
1683 | { | |
1684 | ssize_t len; | |
1685 | struct ustctl_reg_msg reg_msg; | |
1686 | ||
1687 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); | |
1688 | if (len > 0 && len != sizeof(reg_msg)) | |
1689 | return -EIO; | |
1690 | if (len == 0) | |
1691 | return -EPIPE; | |
1692 | if (len < 0) | |
1693 | return len; | |
1694 | ||
1695 | if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) { | |
1696 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? | |
1697 | BIG_ENDIAN : LITTLE_ENDIAN; | |
1698 | } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) { | |
1699 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? | |
1700 | LITTLE_ENDIAN : BIG_ENDIAN; | |
1701 | } else { | |
1702 | return -LTTNG_UST_ERR_INVAL_MAGIC; | |
1703 | } | |
1704 | switch (reg_msg.socket_type) { | |
1705 | case 0: *type = USTCTL_SOCKET_CMD; | |
1706 | break; | |
1707 | case 1: *type = USTCTL_SOCKET_NOTIFY; | |
1708 | break; | |
1709 | default: | |
1710 | return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE; | |
1711 | } | |
1712 | *major = reg_msg.major; | |
1713 | *minor = reg_msg.minor; | |
1714 | *pid = reg_msg.pid; | |
1715 | *ppid = reg_msg.ppid; | |
1716 | *uid = reg_msg.uid; | |
1717 | *gid = reg_msg.gid; | |
1718 | *bits_per_long = reg_msg.bits_per_long; | |
1719 | *uint8_t_alignment = reg_msg.uint8_t_alignment; | |
1720 | *uint16_t_alignment = reg_msg.uint16_t_alignment; | |
1721 | *uint32_t_alignment = reg_msg.uint32_t_alignment; | |
1722 | *uint64_t_alignment = reg_msg.uint64_t_alignment; | |
1723 | *long_alignment = reg_msg.long_alignment; | |
1724 | memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN); | |
1725 | if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) { | |
1726 | return -LTTNG_UST_ERR_UNSUP_MAJOR; | |
1727 | } | |
1728 | ||
1729 | return 0; | |
1730 | } | |
1731 | ||
1732 | int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd) | |
1733 | { | |
1734 | struct ustcomm_notify_hdr header; | |
1735 | ssize_t len; | |
1736 | ||
1737 | len = ustcomm_recv_unix_sock(sock, &header, sizeof(header)); | |
1738 | if (len > 0 && len != sizeof(header)) | |
1739 | return -EIO; | |
1740 | if (len == 0) | |
1741 | return -EPIPE; | |
1742 | if (len < 0) | |
1743 | return len; | |
1744 | switch (header.notify_cmd) { | |
1745 | case 0: | |
1746 | *notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
1747 | break; | |
1748 | case 1: | |
1749 | *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
1750 | break; | |
1751 | default: | |
1752 | return -EINVAL; | |
1753 | } | |
1754 | return 0; | |
1755 | } | |
1756 | ||
1757 | /* | |
1758 | * Returns 0 on success, negative error value on error. | |
1759 | */ | |
1760 | int ustctl_recv_register_event(int sock, | |
1761 | int *session_objd, | |
1762 | int *channel_objd, | |
1763 | char *event_name, | |
1764 | int *loglevel, | |
1765 | char **signature, | |
1766 | size_t *nr_fields, | |
1767 | struct ustctl_field **fields, | |
1768 | char **model_emf_uri) | |
1769 | { | |
1770 | ssize_t len; | |
1771 | struct ustcomm_notify_event_msg msg; | |
1772 | size_t signature_len, fields_len, model_emf_uri_len; | |
1773 | char *a_sign = NULL, *a_model_emf_uri = NULL; | |
1774 | struct ustctl_field *a_fields = NULL; | |
1775 | ||
1776 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
1777 | if (len > 0 && len != sizeof(msg)) | |
1778 | return -EIO; | |
1779 | if (len == 0) | |
1780 | return -EPIPE; | |
1781 | if (len < 0) | |
1782 | return len; | |
1783 | ||
1784 | *session_objd = msg.session_objd; | |
1785 | *channel_objd = msg.channel_objd; | |
1786 | strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN); | |
1787 | event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1788 | *loglevel = msg.loglevel; | |
1789 | signature_len = msg.signature_len; | |
1790 | fields_len = msg.fields_len; | |
1791 | ||
1792 | if (fields_len % sizeof(*a_fields) != 0) { | |
1793 | return -EINVAL; | |
1794 | } | |
1795 | ||
1796 | model_emf_uri_len = msg.model_emf_uri_len; | |
1797 | ||
1798 | /* recv signature. contains at least \0. */ | |
1799 | a_sign = zmalloc(signature_len); | |
1800 | if (!a_sign) | |
1801 | return -ENOMEM; | |
1802 | len = ustcomm_recv_unix_sock(sock, a_sign, signature_len); | |
1803 | if (len > 0 && len != signature_len) { | |
1804 | len = -EIO; | |
1805 | goto signature_error; | |
1806 | } | |
1807 | if (len == 0) { | |
1808 | len = -EPIPE; | |
1809 | goto signature_error; | |
1810 | } | |
1811 | if (len < 0) { | |
1812 | goto signature_error; | |
1813 | } | |
1814 | /* Enforce end of string */ | |
111198c2 | 1815 | a_sign[signature_len - 1] = '\0'; |
32ce8569 MD |
1816 | |
1817 | /* recv fields */ | |
1818 | if (fields_len) { | |
1819 | a_fields = zmalloc(fields_len); | |
1820 | if (!a_fields) { | |
1821 | len = -ENOMEM; | |
1822 | goto signature_error; | |
1823 | } | |
1824 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); | |
1825 | if (len > 0 && len != fields_len) { | |
1826 | len = -EIO; | |
1827 | goto fields_error; | |
1828 | } | |
1829 | if (len == 0) { | |
1830 | len = -EPIPE; | |
1831 | goto fields_error; | |
1832 | } | |
1833 | if (len < 0) { | |
1834 | goto fields_error; | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | if (model_emf_uri_len) { | |
1839 | /* recv model_emf_uri_len */ | |
1840 | a_model_emf_uri = zmalloc(model_emf_uri_len); | |
1841 | if (!a_model_emf_uri) { | |
1842 | len = -ENOMEM; | |
1843 | goto fields_error; | |
1844 | } | |
1845 | len = ustcomm_recv_unix_sock(sock, a_model_emf_uri, | |
1846 | model_emf_uri_len); | |
1847 | if (len > 0 && len != model_emf_uri_len) { | |
1848 | len = -EIO; | |
1849 | goto model_error; | |
1850 | } | |
1851 | if (len == 0) { | |
1852 | len = -EPIPE; | |
1853 | goto model_error; | |
1854 | } | |
1855 | if (len < 0) { | |
1856 | goto model_error; | |
1857 | } | |
1858 | /* Enforce end of string */ | |
1859 | a_model_emf_uri[model_emf_uri_len - 1] = '\0'; | |
1860 | } | |
1861 | ||
1862 | *signature = a_sign; | |
1863 | *nr_fields = fields_len / sizeof(*a_fields); | |
1864 | *fields = a_fields; | |
1865 | *model_emf_uri = a_model_emf_uri; | |
1866 | ||
1867 | return 0; | |
1868 | ||
1869 | model_error: | |
1870 | free(a_model_emf_uri); | |
1871 | fields_error: | |
1872 | free(a_fields); | |
1873 | signature_error: | |
1874 | free(a_sign); | |
1875 | return len; | |
1876 | } | |
1877 | ||
1878 | /* | |
1879 | * Returns 0 on success, negative error value on error. | |
1880 | */ | |
1881 | int ustctl_reply_register_event(int sock, | |
1882 | uint32_t id, | |
1883 | int ret_code) | |
1884 | { | |
1885 | ssize_t len; | |
1886 | struct { | |
1887 | struct ustcomm_notify_hdr header; | |
1888 | struct ustcomm_notify_event_reply r; | |
1889 | } reply; | |
1890 | ||
1891 | memset(&reply, 0, sizeof(reply)); | |
1892 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
1893 | reply.r.ret_code = ret_code; | |
1894 | reply.r.event_id = id; | |
1895 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
1896 | if (len > 0 && len != sizeof(reply)) | |
1897 | return -EIO; | |
1898 | if (len < 0) | |
1899 | return len; | |
1900 | return 0; | |
1901 | } | |
1902 | ||
1903 | /* | |
1904 | * Returns 0 on success, negative UST or system error value on error. | |
1905 | */ | |
1906 | int ustctl_recv_register_channel(int sock, | |
1907 | int *session_objd, /* session descriptor (output) */ | |
1908 | int *channel_objd, /* channel descriptor (output) */ | |
1909 | size_t *nr_fields, | |
1910 | struct ustctl_field **fields) | |
1911 | { | |
1912 | ssize_t len; | |
1913 | struct ustcomm_notify_channel_msg msg; | |
1914 | size_t fields_len; | |
1915 | struct ustctl_field *a_fields; | |
1916 | ||
1917 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
1918 | if (len > 0 && len != sizeof(msg)) | |
1919 | return -EIO; | |
1920 | if (len == 0) | |
1921 | return -EPIPE; | |
1922 | if (len < 0) | |
1923 | return len; | |
1924 | ||
1925 | *session_objd = msg.session_objd; | |
1926 | *channel_objd = msg.channel_objd; | |
1927 | fields_len = msg.ctx_fields_len; | |
1928 | ||
1929 | if (fields_len % sizeof(*a_fields) != 0) { | |
1930 | return -EINVAL; | |
1931 | } | |
1932 | ||
1933 | /* recv fields */ | |
1934 | if (fields_len) { | |
1935 | a_fields = zmalloc(fields_len); | |
1936 | if (!a_fields) { | |
1937 | len = -ENOMEM; | |
1938 | goto alloc_error; | |
1939 | } | |
1940 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); | |
1941 | if (len > 0 && len != fields_len) { | |
1942 | len = -EIO; | |
1943 | goto fields_error; | |
1944 | } | |
1945 | if (len == 0) { | |
1946 | len = -EPIPE; | |
1947 | goto fields_error; | |
1948 | } | |
1949 | if (len < 0) { | |
1950 | goto fields_error; | |
1951 | } | |
1952 | *fields = a_fields; | |
1953 | } else { | |
1954 | *fields = NULL; | |
1955 | } | |
1956 | *nr_fields = fields_len / sizeof(*a_fields); | |
1957 | return 0; | |
1958 | ||
1959 | fields_error: | |
1960 | free(a_fields); | |
1961 | alloc_error: | |
1962 | return len; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Returns 0 on success, negative error value on error. | |
1967 | */ | |
1968 | int ustctl_reply_register_channel(int sock, | |
1969 | uint32_t chan_id, | |
1970 | enum ustctl_channel_header header_type, | |
1971 | int ret_code) | |
1972 | { | |
1973 | ssize_t len; | |
1974 | struct { | |
1975 | struct ustcomm_notify_hdr header; | |
1976 | struct ustcomm_notify_channel_reply r; | |
1977 | } reply; | |
1978 | ||
1979 | memset(&reply, 0, sizeof(reply)); | |
1980 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
1981 | reply.r.ret_code = ret_code; | |
1982 | reply.r.chan_id = chan_id; | |
1983 | switch (header_type) { | |
1984 | case USTCTL_CHANNEL_HEADER_COMPACT: | |
1985 | reply.r.header_type = 1; | |
1986 | break; | |
1987 | case USTCTL_CHANNEL_HEADER_LARGE: | |
1988 | reply.r.header_type = 2; | |
1989 | break; | |
1990 | default: | |
1991 | reply.r.header_type = 0; | |
1992 | break; | |
1993 | } | |
1994 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
1995 | if (len > 0 && len != sizeof(reply)) | |
1996 | return -EIO; | |
1997 | if (len < 0) | |
1998 | return len; | |
1999 | return 0; | |
2000 | } | |
2001 | ||
74d81a6c MD |
2002 | static __attribute__((constructor)) |
2003 | void ustctl_init(void) | |
2004 | { | |
2005 | init_usterr(); | |
2006 | lttng_ring_buffer_metadata_client_init(); | |
2007 | lttng_ring_buffer_client_overwrite_init(); | |
34a91bdb | 2008 | lttng_ring_buffer_client_overwrite_rt_init(); |
74d81a6c | 2009 | lttng_ring_buffer_client_discard_init(); |
34a91bdb | 2010 | lttng_ring_buffer_client_discard_rt_init(); |
03d2d293 | 2011 | lib_ringbuffer_signal_init(); |
74d81a6c MD |
2012 | } |
2013 | ||
2014 | static __attribute__((destructor)) | |
2015 | void ustctl_exit(void) | |
2016 | { | |
34a91bdb | 2017 | lttng_ring_buffer_client_discard_rt_exit(); |
74d81a6c | 2018 | lttng_ring_buffer_client_discard_exit(); |
34a91bdb | 2019 | lttng_ring_buffer_client_overwrite_rt_exit(); |
74d81a6c MD |
2020 | lttng_ring_buffer_client_overwrite_exit(); |
2021 | lttng_ring_buffer_metadata_client_exit(); | |
57773204 | 2022 | } |