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