Commit | Line | Data |
---|---|---|
57773204 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: GPL-2.0-only |
57773204 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca> |
5 | * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
57773204 MD |
6 | */ |
7 | ||
fb31eb73 | 8 | #include <stdint.h> |
57773204 | 9 | #include <string.h> |
fb31eb73 | 10 | #include <sys/mman.h> |
4e79769f | 11 | #include <unistd.h> |
a834901f MD |
12 | #include <sys/types.h> |
13 | #include <sys/socket.h> | |
fb31eb73 | 14 | |
c62a3816 | 15 | #include <lttng/ust-config.h> |
4318ae1b MD |
16 | #include <lttng/ust-ctl.h> |
17 | #include <lttng/ust-abi.h> | |
c1fca457 | 18 | #include <lttng/ust-events.h> |
3208818b | 19 | #include <lttng/ust-endian.h> |
44c72f10 | 20 | #include <usterr-signal-safe.h> |
b728d87e | 21 | #include <ust-comm.h> |
864a1eda | 22 | #include <ust-helper.h> |
cd61d9bf | 23 | #include "ust-compat.h" |
57773204 MD |
24 | |
25 | #include "../libringbuffer/backend.h" | |
26 | #include "../libringbuffer/frontend.h" | |
c9023c93 | 27 | #include "../liblttng-ust/wait.h" |
b2f3252a | 28 | #include "../liblttng-ust/lttng-rb-clients.h" |
f9364363 | 29 | #include "../liblttng-ust/clock.h" |
92ce256d | 30 | #include "../liblttng-ust/getenv.h" |
23d128de | 31 | #include "../liblttng-ust/lttng-tracer-core.h" |
c9023c93 | 32 | |
ebabbf58 MD |
33 | #include "../libcounter/shm.h" |
34 | #include "../libcounter/smp.h" | |
35 | #include "../libcounter/counter.h" | |
36 | ||
c9023c93 MD |
37 | /* |
38 | * Number of milliseconds to retry before failing metadata writes on | |
39 | * buffer full condition. (10 seconds) | |
40 | */ | |
41 | #define LTTNG_METADATA_TIMEOUT_MSEC 10000 | |
57773204 | 42 | |
74d81a6c MD |
43 | /* |
44 | * Channel representation within consumer. | |
45 | */ | |
46 | struct ustctl_consumer_channel { | |
47 | struct lttng_channel *chan; /* lttng channel buffers */ | |
6b120308 | 48 | |
74d81a6c MD |
49 | /* initial attributes */ |
50 | struct ustctl_consumer_channel_attr attr; | |
ff0f5728 MD |
51 | int wait_fd; /* monitor close() */ |
52 | int wakeup_fd; /* monitor close() */ | |
74d81a6c MD |
53 | }; |
54 | ||
55 | /* | |
56 | * Stream representation within consumer. | |
57 | */ | |
58 | struct ustctl_consumer_stream { | |
59 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ | |
60 | struct lttng_ust_lib_ring_buffer *buf; | |
61 | struct ustctl_consumer_channel *chan; | |
62 | int shm_fd, wait_fd, wakeup_fd; | |
63 | int cpu; | |
64 | uint64_t memory_map_size; | |
65 | }; | |
66 | ||
ebabbf58 MD |
67 | #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8 |
68 | struct ustctl_counter_attr { | |
69 | enum ustctl_counter_arithmetic arithmetic; | |
70 | enum ustctl_counter_bitness bitness; | |
71 | uint32_t nr_dimensions; | |
72 | int64_t global_sum_step; | |
73 | struct ustctl_counter_dimension dimensions[USTCTL_COUNTER_ATTR_DIMENSION_MAX]; | |
74 | }; | |
75 | ||
76 | /* | |
77 | * Counter representation within daemon. | |
78 | */ | |
79 | struct ustctl_daemon_counter { | |
80 | struct lib_counter *counter; | |
81 | const struct lttng_counter_ops *ops; | |
82 | struct ustctl_counter_attr *attr; /* initial attributes */ | |
83 | }; | |
84 | ||
74d81a6c | 85 | extern void lttng_ring_buffer_client_overwrite_init(void); |
08a3170c | 86 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
74d81a6c | 87 | extern void lttng_ring_buffer_client_discard_init(void); |
08a3170c | 88 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
74d81a6c MD |
89 | extern void lttng_ring_buffer_metadata_client_init(void); |
90 | extern void lttng_ring_buffer_client_overwrite_exit(void); | |
08a3170c | 91 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
74d81a6c | 92 | extern void lttng_ring_buffer_client_discard_exit(void); |
08a3170c | 93 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
74d81a6c | 94 | extern void lttng_ring_buffer_metadata_client_exit(void); |
31677417 | 95 | LTTNG_HIDDEN |
ebabbf58 | 96 | extern void lttng_counter_client_percpu_32_modular_init(void); |
31677417 | 97 | LTTNG_HIDDEN |
ebabbf58 | 98 | extern void lttng_counter_client_percpu_32_modular_exit(void); |
31677417 | 99 | LTTNG_HIDDEN |
ebabbf58 | 100 | extern void lttng_counter_client_percpu_64_modular_init(void); |
31677417 | 101 | LTTNG_HIDDEN |
ebabbf58 | 102 | extern void lttng_counter_client_percpu_64_modular_exit(void); |
74d81a6c | 103 | |
2be0e72c MD |
104 | int ustctl_release_handle(int sock, int handle) |
105 | { | |
106 | struct ustcomm_ust_msg lum; | |
107 | struct ustcomm_ust_reply lur; | |
2be0e72c | 108 | |
74d81a6c MD |
109 | if (sock < 0 || handle < 0) |
110 | return 0; | |
111 | memset(&lum, 0, sizeof(lum)); | |
112 | lum.handle = handle; | |
113 | lum.cmd = LTTNG_UST_RELEASE; | |
114 | return ustcomm_send_app_cmd(sock, &lum, &lur); | |
2be0e72c | 115 | } |
74d81a6c | 116 | |
12388166 MD |
117 | /* |
118 | * If sock is negative, it means we don't have to notify the other side | |
119 | * (e.g. application has already vanished). | |
120 | */ | |
d26228ae | 121 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
57773204 | 122 | { |
57773204 MD |
123 | int ret; |
124 | ||
9bfc503d MD |
125 | if (!data) |
126 | return -EINVAL; | |
127 | ||
74d81a6c MD |
128 | switch (data->type) { |
129 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: | |
ff0f5728 MD |
130 | if (data->u.channel.wakeup_fd >= 0) { |
131 | ret = close(data->u.channel.wakeup_fd); | |
132 | if (ret < 0) { | |
133 | ret = -errno; | |
134 | return ret; | |
135 | } | |
dd6c697c | 136 | data->u.channel.wakeup_fd = -1; |
ff0f5728 | 137 | } |
74d81a6c | 138 | free(data->u.channel.data); |
dd6c697c | 139 | data->u.channel.data = NULL; |
74d81a6c MD |
140 | break; |
141 | case LTTNG_UST_OBJECT_TYPE_STREAM: | |
142 | if (data->u.stream.shm_fd >= 0) { | |
143 | ret = close(data->u.stream.shm_fd); | |
144 | if (ret < 0) { | |
145 | ret = -errno; | |
146 | return ret; | |
147 | } | |
dd6c697c | 148 | data->u.stream.shm_fd = -1; |
d26228ae | 149 | } |
74d81a6c MD |
150 | if (data->u.stream.wakeup_fd >= 0) { |
151 | ret = close(data->u.stream.wakeup_fd); | |
152 | if (ret < 0) { | |
153 | ret = -errno; | |
154 | return ret; | |
155 | } | |
dd6c697c | 156 | data->u.stream.wakeup_fd = -1; |
d26228ae | 157 | } |
74d81a6c | 158 | break; |
32ce8569 MD |
159 | case LTTNG_UST_OBJECT_TYPE_EVENT: |
160 | case LTTNG_UST_OBJECT_TYPE_CONTEXT: | |
d8d2416d FD |
161 | case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP: |
162 | case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER: | |
32ce8569 | 163 | break; |
ebabbf58 MD |
164 | case LTTNG_UST_OBJECT_TYPE_COUNTER: |
165 | free(data->u.counter.data); | |
166 | data->u.counter.data = NULL; | |
167 | break; | |
168 | case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL: | |
169 | if (data->u.counter_global.shm_fd >= 0) { | |
170 | ret = close(data->u.counter_global.shm_fd); | |
171 | if (ret < 0) { | |
172 | ret = -errno; | |
173 | return ret; | |
174 | } | |
175 | data->u.counter_global.shm_fd = -1; | |
176 | } | |
177 | break; | |
178 | case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU: | |
179 | if (data->u.counter_cpu.shm_fd >= 0) { | |
180 | ret = close(data->u.counter_cpu.shm_fd); | |
181 | if (ret < 0) { | |
182 | ret = -errno; | |
183 | return ret; | |
184 | } | |
185 | data->u.counter_cpu.shm_fd = -1; | |
186 | } | |
187 | break; | |
74d81a6c MD |
188 | default: |
189 | assert(0); | |
d26228ae | 190 | } |
2be0e72c | 191 | return ustctl_release_handle(sock, data->handle); |
57773204 MD |
192 | } |
193 | ||
1c5e467e MD |
194 | /* |
195 | * Send registration done packet to the application. | |
196 | */ | |
197 | int ustctl_register_done(int sock) | |
198 | { | |
199 | struct ustcomm_ust_msg lum; | |
200 | struct ustcomm_ust_reply lur; | |
201 | int ret; | |
202 | ||
203 | DBG("Sending register done command to %d", sock); | |
204 | memset(&lum, 0, sizeof(lum)); | |
205 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
206 | lum.cmd = LTTNG_UST_REGISTER_DONE; | |
207 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
208 | if (ret) | |
209 | return ret; | |
1c5e467e | 210 | return 0; |
1c5e467e MD |
211 | } |
212 | ||
57773204 MD |
213 | /* |
214 | * returns session handle. | |
215 | */ | |
216 | int ustctl_create_session(int sock) | |
217 | { | |
218 | struct ustcomm_ust_msg lum; | |
219 | struct ustcomm_ust_reply lur; | |
220 | int ret, session_handle; | |
221 | ||
222 | /* Create session */ | |
223 | memset(&lum, 0, sizeof(lum)); | |
224 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
225 | lum.cmd = LTTNG_UST_SESSION; | |
226 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
227 | if (ret) | |
228 | return ret; | |
229 | session_handle = lur.ret_val; | |
230 | DBG("received session handle %u", session_handle); | |
231 | return session_handle; | |
232 | } | |
233 | ||
57773204 | 234 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
61f02aea MD |
235 | struct lttng_ust_object_data *channel_data, |
236 | struct lttng_ust_object_data **_event_data) | |
57773204 MD |
237 | { |
238 | struct ustcomm_ust_msg lum; | |
239 | struct ustcomm_ust_reply lur; | |
61f02aea | 240 | struct lttng_ust_object_data *event_data; |
57773204 MD |
241 | int ret; |
242 | ||
9bfc503d MD |
243 | if (!channel_data || !_event_data) |
244 | return -EINVAL; | |
245 | ||
74d81a6c | 246 | event_data = zmalloc(sizeof(*event_data)); |
57773204 MD |
247 | if (!event_data) |
248 | return -ENOMEM; | |
32ce8569 | 249 | event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT; |
57773204 MD |
250 | memset(&lum, 0, sizeof(lum)); |
251 | lum.handle = channel_data->handle; | |
252 | lum.cmd = LTTNG_UST_EVENT; | |
253 | strncpy(lum.u.event.name, ev->name, | |
254 | LTTNG_UST_SYM_NAME_LEN); | |
255 | lum.u.event.instrumentation = ev->instrumentation; | |
457a6b58 MD |
256 | lum.u.event.loglevel_type = ev->loglevel_type; |
257 | lum.u.event.loglevel = ev->loglevel; | |
57773204 MD |
258 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
259 | if (ret) { | |
260 | free(event_data); | |
261 | return ret; | |
262 | } | |
263 | event_data->handle = lur.ret_val; | |
264 | DBG("received event handle %u", event_data->handle); | |
265 | *_event_data = event_data; | |
266 | return 0; | |
267 | } | |
268 | ||
53f0df51 | 269 | int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx, |
61f02aea MD |
270 | struct lttng_ust_object_data *obj_data, |
271 | struct lttng_ust_object_data **_context_data) | |
57773204 MD |
272 | { |
273 | struct ustcomm_ust_msg lum; | |
274 | struct ustcomm_ust_reply lur; | |
53f0df51 JG |
275 | struct lttng_ust_object_data *context_data = NULL; |
276 | char *buf = NULL; | |
277 | size_t len; | |
57773204 MD |
278 | int ret; |
279 | ||
53f0df51 JG |
280 | if (!obj_data || !_context_data) { |
281 | ret = -EINVAL; | |
282 | goto end; | |
283 | } | |
9bfc503d | 284 | |
74d81a6c | 285 | context_data = zmalloc(sizeof(*context_data)); |
53f0df51 JG |
286 | if (!context_data) { |
287 | ret = -ENOMEM; | |
288 | goto end; | |
289 | } | |
32ce8569 | 290 | context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT; |
57773204 | 291 | memset(&lum, 0, sizeof(lum)); |
3039d8ed | 292 | lum.handle = obj_data->handle; |
57773204 | 293 | lum.cmd = LTTNG_UST_CONTEXT; |
53f0df51 JG |
294 | |
295 | lum.u.context.ctx = ctx->ctx; | |
296 | switch (ctx->ctx) { | |
297 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: | |
298 | lum.u.context.u.perf_counter = ctx->u.perf_counter; | |
299 | break; | |
300 | case LTTNG_UST_CONTEXT_APP_CONTEXT: | |
301 | { | |
302 | size_t provider_name_len = strlen( | |
303 | ctx->u.app_ctx.provider_name) + 1; | |
304 | size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1; | |
305 | ||
306 | lum.u.context.u.app_ctx.provider_name_len = provider_name_len; | |
307 | lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len; | |
308 | ||
309 | len = provider_name_len + ctx_name_len; | |
310 | buf = zmalloc(len); | |
311 | if (!buf) { | |
312 | ret = -ENOMEM; | |
313 | goto end; | |
314 | } | |
315 | memcpy(buf, ctx->u.app_ctx.provider_name, | |
316 | provider_name_len); | |
317 | memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name, | |
318 | ctx_name_len); | |
319 | break; | |
320 | } | |
321 | default: | |
322 | break; | |
323 | } | |
324 | ret = ustcomm_send_app_msg(sock, &lum); | |
325 | if (ret) | |
326 | goto end; | |
327 | if (buf) { | |
328 | /* send var len ctx_name */ | |
329 | ret = ustcomm_send_unix_sock(sock, buf, len); | |
330 | if (ret < 0) { | |
331 | goto end; | |
332 | } | |
333 | if (ret != len) { | |
334 | ret = -EINVAL; | |
335 | goto end; | |
336 | } | |
337 | } | |
338 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
339 | if (ret < 0) { | |
340 | goto end; | |
57773204 | 341 | } |
32ce8569 MD |
342 | context_data->handle = -1; |
343 | DBG("Context created successfully"); | |
57773204 | 344 | *_context_data = context_data; |
53f0df51 JG |
345 | context_data = NULL; |
346 | end: | |
347 | free(context_data); | |
348 | free(buf); | |
57773204 MD |
349 | return ret; |
350 | } | |
351 | ||
cd54f6d9 MD |
352 | int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode, |
353 | struct lttng_ust_object_data *obj_data) | |
354 | { | |
355 | struct ustcomm_ust_msg lum; | |
356 | struct ustcomm_ust_reply lur; | |
357 | int ret; | |
358 | ||
359 | if (!obj_data) | |
360 | return -EINVAL; | |
361 | ||
362 | memset(&lum, 0, sizeof(lum)); | |
363 | lum.handle = obj_data->handle; | |
364 | lum.cmd = LTTNG_UST_FILTER; | |
365 | lum.u.filter.data_size = bytecode->len; | |
366 | lum.u.filter.reloc_offset = bytecode->reloc_offset; | |
e695af51 | 367 | lum.u.filter.seqnum = bytecode->seqnum; |
cd54f6d9 MD |
368 | |
369 | ret = ustcomm_send_app_msg(sock, &lum); | |
d37ecb3f FD |
370 | if (ret) |
371 | return ret; | |
372 | /* send var len bytecode */ | |
373 | ret = ustcomm_send_unix_sock(sock, bytecode->data, | |
374 | bytecode->len); | |
375 | if (ret < 0) { | |
376 | return ret; | |
377 | } | |
378 | if (ret != bytecode->len) | |
379 | return -EINVAL; | |
380 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
381 | } | |
382 | ||
383 | int ustctl_set_capture(int sock, struct lttng_ust_capture_bytecode *bytecode, | |
384 | struct lttng_ust_object_data *obj_data) | |
385 | { | |
386 | struct ustcomm_ust_msg lum; | |
387 | struct ustcomm_ust_reply lur; | |
388 | int ret; | |
389 | ||
390 | if (!obj_data) | |
391 | return -EINVAL; | |
392 | ||
393 | memset(&lum, 0, sizeof(lum)); | |
394 | lum.handle = obj_data->handle; | |
395 | lum.cmd = LTTNG_UST_CAPTURE; | |
396 | lum.u.capture.data_size = bytecode->len; | |
397 | lum.u.capture.reloc_offset = bytecode->reloc_offset; | |
398 | lum.u.capture.seqnum = bytecode->seqnum; | |
399 | ||
400 | ret = ustcomm_send_app_msg(sock, &lum); | |
cd54f6d9 MD |
401 | if (ret) |
402 | return ret; | |
cd54f6d9 MD |
403 | /* send var len bytecode */ |
404 | ret = ustcomm_send_unix_sock(sock, bytecode->data, | |
405 | bytecode->len); | |
406 | if (ret < 0) { | |
407 | return ret; | |
408 | } | |
7bc53e94 MD |
409 | if (ret != bytecode->len) |
410 | return -EINVAL; | |
411 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
cd54f6d9 MD |
412 | } |
413 | ||
da57c034 JI |
414 | int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion, |
415 | struct lttng_ust_object_data *obj_data) | |
416 | { | |
417 | struct ustcomm_ust_msg lum; | |
418 | struct ustcomm_ust_reply lur; | |
419 | int ret; | |
420 | ||
421 | if (!obj_data) { | |
422 | return -EINVAL; | |
423 | } | |
424 | ||
425 | memset(&lum, 0, sizeof(lum)); | |
426 | lum.handle = obj_data->handle; | |
427 | lum.cmd = LTTNG_UST_EXCLUSION; | |
428 | lum.u.exclusion.count = exclusion->count; | |
429 | ||
430 | ret = ustcomm_send_app_msg(sock, &lum); | |
431 | if (ret) { | |
432 | return ret; | |
433 | } | |
434 | ||
1628366f | 435 | /* send var len exclusion names */ |
da57c034 JI |
436 | ret = ustcomm_send_unix_sock(sock, |
437 | exclusion->names, | |
438 | exclusion->count * LTTNG_UST_SYM_NAME_LEN); | |
439 | if (ret < 0) { | |
440 | return ret; | |
441 | } | |
442 | if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) { | |
443 | return -EINVAL; | |
444 | } | |
445 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
446 | } | |
447 | ||
57773204 | 448 | /* Enable event, channel and session ioctl */ |
61f02aea | 449 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
450 | { |
451 | struct ustcomm_ust_msg lum; | |
452 | struct ustcomm_ust_reply lur; | |
453 | int ret; | |
454 | ||
9bfc503d MD |
455 | if (!object) |
456 | return -EINVAL; | |
457 | ||
57773204 MD |
458 | memset(&lum, 0, sizeof(lum)); |
459 | lum.handle = object->handle; | |
460 | lum.cmd = LTTNG_UST_ENABLE; | |
461 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
462 | if (ret) | |
463 | return ret; | |
464 | DBG("enabled handle %u", object->handle); | |
465 | return 0; | |
466 | } | |
467 | ||
468 | /* Disable event, channel and session ioctl */ | |
61f02aea | 469 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
470 | { |
471 | struct ustcomm_ust_msg lum; | |
472 | struct ustcomm_ust_reply lur; | |
473 | int ret; | |
474 | ||
9bfc503d MD |
475 | if (!object) |
476 | return -EINVAL; | |
477 | ||
57773204 MD |
478 | memset(&lum, 0, sizeof(lum)); |
479 | lum.handle = object->handle; | |
480 | lum.cmd = LTTNG_UST_DISABLE; | |
481 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
482 | if (ret) | |
483 | return ret; | |
484 | DBG("disable handle %u", object->handle); | |
485 | return 0; | |
486 | } | |
487 | ||
4a6ca058 | 488 | int ustctl_start_session(int sock, int handle) |
57773204 | 489 | { |
61f02aea | 490 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
491 | |
492 | obj.handle = handle; | |
493 | return ustctl_enable(sock, &obj); | |
57773204 MD |
494 | } |
495 | ||
4a6ca058 | 496 | int ustctl_stop_session(int sock, int handle) |
57773204 | 497 | { |
61f02aea | 498 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
499 | |
500 | obj.handle = handle; | |
501 | return ustctl_disable(sock, &obj); | |
57773204 MD |
502 | } |
503 | ||
d8d2416d FD |
504 | int ustctl_create_event_notifier_group(int sock, int pipe_fd, |
505 | struct lttng_ust_object_data **_event_notifier_group_data) | |
506 | { | |
507 | struct lttng_ust_object_data *event_notifier_group_data; | |
508 | struct ustcomm_ust_msg lum; | |
509 | struct ustcomm_ust_reply lur; | |
510 | ssize_t len; | |
511 | int ret; | |
512 | ||
513 | if (!_event_notifier_group_data) | |
514 | return -EINVAL; | |
515 | ||
516 | event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data)); | |
517 | if (!event_notifier_group_data) | |
518 | return -ENOMEM; | |
519 | ||
520 | event_notifier_group_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP; | |
521 | ||
522 | memset(&lum, 0, sizeof(lum)); | |
523 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
524 | lum.cmd = LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE; | |
525 | ||
526 | ret = ustcomm_send_app_msg(sock, &lum); | |
527 | if (ret) | |
528 | goto error; | |
529 | ||
530 | /* Send event_notifier notification pipe. */ | |
531 | len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1); | |
532 | if (len <= 0) { | |
533 | ret = len; | |
534 | goto error; | |
535 | } | |
536 | ||
537 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
538 | if (ret) | |
539 | goto error; | |
540 | ||
541 | event_notifier_group_data->handle = lur.ret_val; | |
542 | DBG("received event_notifier group handle %d", event_notifier_group_data->handle); | |
543 | ||
544 | *_event_notifier_group_data = event_notifier_group_data; | |
545 | ||
546 | ret = 0; | |
547 | goto end; | |
548 | error: | |
549 | free(event_notifier_group_data); | |
550 | ||
551 | end: | |
552 | return ret; | |
553 | } | |
554 | ||
555 | int ustctl_create_event_notifier(int sock, struct lttng_ust_event_notifier *event_notifier, | |
556 | struct lttng_ust_object_data *event_notifier_group, | |
557 | struct lttng_ust_object_data **_event_notifier_data) | |
558 | { | |
559 | struct ustcomm_ust_msg lum; | |
560 | struct ustcomm_ust_reply lur; | |
561 | struct lttng_ust_object_data *event_notifier_data; | |
8406222c | 562 | ssize_t len; |
d8d2416d FD |
563 | int ret; |
564 | ||
565 | if (!event_notifier_group || !_event_notifier_data) | |
566 | return -EINVAL; | |
567 | ||
568 | event_notifier_data = zmalloc(sizeof(*event_notifier_data)); | |
569 | if (!event_notifier_data) | |
570 | return -ENOMEM; | |
571 | ||
572 | event_notifier_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER; | |
573 | ||
574 | memset(&lum, 0, sizeof(lum)); | |
575 | lum.handle = event_notifier_group->handle; | |
576 | lum.cmd = LTTNG_UST_EVENT_NOTIFIER_CREATE; | |
8406222c | 577 | lum.u.event_notifier.len = sizeof(*event_notifier); |
d8d2416d | 578 | |
41844673 | 579 | ret = ustcomm_send_app_msg(sock, &lum); |
d8d2416d FD |
580 | if (ret) { |
581 | free(event_notifier_data); | |
582 | return ret; | |
583 | } | |
8406222c MD |
584 | /* Send struct lttng_ust_event_notifier */ |
585 | len = ustcomm_send_unix_sock(sock, event_notifier, sizeof(*event_notifier)); | |
586 | if (len != sizeof(*event_notifier)) { | |
587 | if (len < 0) | |
588 | return len; | |
589 | else | |
590 | return -EIO; | |
591 | } | |
41844673 MD |
592 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
593 | if (ret) { | |
594 | free(event_notifier_data); | |
595 | return ret; | |
596 | } | |
d8d2416d FD |
597 | event_notifier_data->handle = lur.ret_val; |
598 | DBG("received event_notifier handle %u", event_notifier_data->handle); | |
599 | *_event_notifier_data = event_notifier_data; | |
600 | ||
601 | return ret; | |
602 | } | |
603 | ||
57773204 MD |
604 | int ustctl_tracepoint_list(int sock) |
605 | { | |
b115631f MD |
606 | struct ustcomm_ust_msg lum; |
607 | struct ustcomm_ust_reply lur; | |
608 | int ret, tp_list_handle; | |
609 | ||
610 | memset(&lum, 0, sizeof(lum)); | |
611 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
612 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; | |
613 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
614 | if (ret) | |
615 | return ret; | |
616 | tp_list_handle = lur.ret_val; | |
617 | DBG("received tracepoint list handle %u", tp_list_handle); | |
618 | return tp_list_handle; | |
619 | } | |
620 | ||
621 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, | |
cbef6901 | 622 | struct lttng_ust_tracepoint_iter *iter) |
b115631f MD |
623 | { |
624 | struct ustcomm_ust_msg lum; | |
625 | struct ustcomm_ust_reply lur; | |
626 | int ret; | |
627 | ||
9bfc503d MD |
628 | if (!iter) |
629 | return -EINVAL; | |
630 | ||
b115631f MD |
631 | memset(&lum, 0, sizeof(lum)); |
632 | lum.handle = tp_list_handle; | |
633 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; | |
634 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
635 | if (ret) | |
636 | return ret; | |
882a56d7 | 637 | DBG("received tracepoint list entry name %s loglevel %d", |
cbef6901 | 638 | lur.u.tracepoint.name, |
882a56d7 | 639 | lur.u.tracepoint.loglevel); |
cbef6901 | 640 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
b115631f | 641 | return 0; |
57773204 MD |
642 | } |
643 | ||
40003310 MD |
644 | int ustctl_tracepoint_field_list(int sock) |
645 | { | |
646 | struct ustcomm_ust_msg lum; | |
647 | struct ustcomm_ust_reply lur; | |
648 | int ret, tp_field_list_handle; | |
649 | ||
650 | memset(&lum, 0, sizeof(lum)); | |
651 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
652 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST; | |
653 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
654 | if (ret) | |
655 | return ret; | |
656 | tp_field_list_handle = lur.ret_val; | |
657 | DBG("received tracepoint field list handle %u", tp_field_list_handle); | |
658 | return tp_field_list_handle; | |
659 | } | |
660 | ||
661 | int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle, | |
662 | struct lttng_ust_field_iter *iter) | |
663 | { | |
664 | struct ustcomm_ust_msg lum; | |
665 | struct ustcomm_ust_reply lur; | |
666 | int ret; | |
667 | ssize_t len; | |
668 | ||
669 | if (!iter) | |
670 | return -EINVAL; | |
671 | ||
672 | memset(&lum, 0, sizeof(lum)); | |
673 | lum.handle = tp_field_list_handle; | |
674 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET; | |
675 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
676 | if (ret) | |
677 | return ret; | |
678 | len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter)); | |
679 | if (len != sizeof(*iter)) { | |
680 | return -EINVAL; | |
681 | } | |
682 | DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d", | |
683 | iter->event_name, | |
684 | iter->loglevel, | |
685 | iter->field_name, | |
686 | iter->type); | |
687 | return 0; | |
688 | } | |
689 | ||
57773204 MD |
690 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
691 | { | |
692 | struct ustcomm_ust_msg lum; | |
693 | struct ustcomm_ust_reply lur; | |
694 | int ret; | |
695 | ||
9bfc503d MD |
696 | if (!v) |
697 | return -EINVAL; | |
698 | ||
57773204 MD |
699 | memset(&lum, 0, sizeof(lum)); |
700 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
701 | lum.cmd = LTTNG_UST_TRACER_VERSION; | |
702 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
703 | if (ret) | |
704 | return ret; | |
705 | memcpy(v, &lur.u.version, sizeof(*v)); | |
706 | DBG("received tracer version"); | |
707 | return 0; | |
708 | } | |
709 | ||
710 | int ustctl_wait_quiescent(int sock) | |
711 | { | |
712 | struct ustcomm_ust_msg lum; | |
713 | struct ustcomm_ust_reply lur; | |
714 | int ret; | |
715 | ||
716 | memset(&lum, 0, sizeof(lum)); | |
717 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
718 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; | |
719 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
720 | if (ret) | |
721 | return ret; | |
722 | DBG("waited for quiescent state"); | |
723 | return 0; | |
724 | } | |
725 | ||
726 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) | |
727 | { | |
9bfc503d MD |
728 | if (!calibrate) |
729 | return -EINVAL; | |
730 | ||
57773204 MD |
731 | return -ENOSYS; |
732 | } | |
733 | ||
f1fffc57 MD |
734 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
735 | { | |
736 | struct ustcomm_ust_msg lum; | |
737 | struct ustcomm_ust_reply lur; | |
738 | int ret; | |
739 | ||
9bfc503d MD |
740 | if (!object) |
741 | return -EINVAL; | |
742 | ||
f1fffc57 MD |
743 | memset(&lum, 0, sizeof(lum)); |
744 | lum.handle = object->handle; | |
745 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; | |
746 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
747 | if (ret) | |
748 | return ret; | |
749 | DBG("flushed buffer handle %u", object->handle); | |
750 | return 0; | |
751 | } | |
752 | ||
74d81a6c MD |
753 | static |
754 | int ustctl_send_channel(int sock, | |
755 | enum lttng_ust_chan_type type, | |
756 | void *data, | |
757 | uint64_t size, | |
ff0f5728 | 758 | int wakeup_fd, |
74d81a6c MD |
759 | int send_fd_only) |
760 | { | |
761 | ssize_t len; | |
762 | ||
763 | if (!send_fd_only) { | |
764 | /* Send mmap size */ | |
765 | len = ustcomm_send_unix_sock(sock, &size, sizeof(size)); | |
766 | if (len != sizeof(size)) { | |
767 | if (len < 0) | |
768 | return len; | |
769 | else | |
770 | return -EIO; | |
771 | } | |
772 | ||
773 | /* Send channel type */ | |
774 | len = ustcomm_send_unix_sock(sock, &type, sizeof(type)); | |
775 | if (len != sizeof(type)) { | |
776 | if (len < 0) | |
777 | return len; | |
778 | else | |
779 | return -EIO; | |
780 | } | |
781 | } | |
782 | ||
783 | /* Send channel data */ | |
784 | len = ustcomm_send_unix_sock(sock, data, size); | |
785 | if (len != size) { | |
786 | if (len < 0) | |
787 | return len; | |
788 | else | |
789 | return -EIO; | |
790 | } | |
57773204 | 791 | |
ff0f5728 MD |
792 | /* Send wakeup fd */ |
793 | len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1); | |
794 | if (len <= 0) { | |
795 | if (len < 0) | |
796 | return len; | |
797 | else | |
798 | return -EIO; | |
799 | } | |
74d81a6c MD |
800 | return 0; |
801 | } | |
802 | ||
803 | static | |
804 | int ustctl_send_stream(int sock, | |
805 | uint32_t stream_nr, | |
806 | uint64_t memory_map_size, | |
807 | int shm_fd, int wakeup_fd, | |
808 | int send_fd_only) | |
57773204 | 809 | { |
74d81a6c MD |
810 | ssize_t len; |
811 | int fds[2]; | |
812 | ||
813 | if (!send_fd_only) { | |
814 | if (shm_fd < 0) { | |
815 | /* finish iteration */ | |
816 | uint64_t v = -1; | |
817 | ||
818 | len = ustcomm_send_unix_sock(sock, &v, sizeof(v)); | |
819 | if (len != sizeof(v)) { | |
820 | if (len < 0) | |
821 | return len; | |
822 | else | |
823 | return -EIO; | |
824 | } | |
825 | return 0; | |
826 | } | |
827 | ||
828 | /* Send mmap size */ | |
829 | len = ustcomm_send_unix_sock(sock, &memory_map_size, | |
830 | sizeof(memory_map_size)); | |
831 | if (len != sizeof(memory_map_size)) { | |
832 | if (len < 0) | |
833 | return len; | |
834 | else | |
835 | return -EIO; | |
836 | } | |
837 | ||
838 | /* Send stream nr */ | |
839 | len = ustcomm_send_unix_sock(sock, &stream_nr, | |
840 | sizeof(stream_nr)); | |
841 | if (len != sizeof(stream_nr)) { | |
842 | if (len < 0) | |
843 | return len; | |
844 | else | |
845 | return -EIO; | |
846 | } | |
847 | } | |
848 | ||
849 | /* Send shm fd and wakeup fd */ | |
850 | fds[0] = shm_fd; | |
851 | fds[1] = wakeup_fd; | |
852 | len = ustcomm_send_fds_unix_sock(sock, fds, 2); | |
853 | if (len <= 0) { | |
854 | if (len < 0) | |
855 | return len; | |
856 | else | |
857 | return -EIO; | |
858 | } | |
859 | return 0; | |
860 | } | |
861 | ||
862 | int ustctl_recv_channel_from_consumer(int sock, | |
863 | struct lttng_ust_object_data **_channel_data) | |
864 | { | |
865 | struct lttng_ust_object_data *channel_data; | |
866 | ssize_t len; | |
ff0f5728 | 867 | int wakeup_fd; |
7a784989 | 868 | int ret; |
57773204 | 869 | |
74d81a6c MD |
870 | channel_data = zmalloc(sizeof(*channel_data)); |
871 | if (!channel_data) { | |
872 | ret = -ENOMEM; | |
873 | goto error_alloc; | |
874 | } | |
875 | channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL; | |
12f3dabc | 876 | channel_data->handle = -1; |
74d81a6c MD |
877 | |
878 | /* recv mmap size */ | |
879 | len = ustcomm_recv_unix_sock(sock, &channel_data->size, | |
880 | sizeof(channel_data->size)); | |
881 | if (len != sizeof(channel_data->size)) { | |
882 | if (len < 0) | |
883 | ret = len; | |
884 | else | |
885 | ret = -EINVAL; | |
886 | goto error; | |
887 | } | |
9bfc503d | 888 | |
74d81a6c MD |
889 | /* recv channel type */ |
890 | len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type, | |
891 | sizeof(channel_data->u.channel.type)); | |
892 | if (len != sizeof(channel_data->u.channel.type)) { | |
893 | if (len < 0) | |
894 | ret = len; | |
895 | else | |
896 | ret = -EINVAL; | |
897 | goto error; | |
898 | } | |
899 | ||
900 | /* recv channel data */ | |
901 | channel_data->u.channel.data = zmalloc(channel_data->size); | |
902 | if (!channel_data->u.channel.data) { | |
903 | ret = -ENOMEM; | |
904 | goto error; | |
905 | } | |
906 | len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data, | |
907 | channel_data->size); | |
908 | if (len != channel_data->size) { | |
909 | if (len < 0) | |
910 | ret = len; | |
911 | else | |
912 | ret = -EINVAL; | |
913 | goto error_recv_data; | |
914 | } | |
ff0f5728 MD |
915 | /* recv wakeup fd */ |
916 | len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); | |
917 | if (len <= 0) { | |
918 | if (len < 0) { | |
919 | ret = len; | |
920 | goto error_recv_data; | |
921 | } else { | |
922 | ret = -EIO; | |
923 | goto error_recv_data; | |
924 | } | |
925 | } | |
926 | channel_data->u.channel.wakeup_fd = wakeup_fd; | |
74d81a6c MD |
927 | *_channel_data = channel_data; |
928 | return 0; | |
929 | ||
930 | error_recv_data: | |
931 | free(channel_data->u.channel.data); | |
932 | error: | |
933 | free(channel_data); | |
934 | error_alloc: | |
935 | return ret; | |
936 | } | |
937 | ||
938 | int ustctl_recv_stream_from_consumer(int sock, | |
939 | struct lttng_ust_object_data **_stream_data) | |
940 | { | |
941 | struct lttng_ust_object_data *stream_data; | |
942 | ssize_t len; | |
943 | int ret; | |
944 | int fds[2]; | |
945 | ||
946 | stream_data = zmalloc(sizeof(*stream_data)); | |
947 | if (!stream_data) { | |
948 | ret = -ENOMEM; | |
949 | goto error_alloc; | |
57773204 | 950 | } |
74d81a6c MD |
951 | |
952 | stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM; | |
953 | stream_data->handle = -1; | |
954 | ||
955 | /* recv mmap size */ | |
956 | len = ustcomm_recv_unix_sock(sock, &stream_data->size, | |
957 | sizeof(stream_data->size)); | |
958 | if (len != sizeof(stream_data->size)) { | |
959 | if (len < 0) | |
960 | ret = len; | |
961 | else | |
962 | ret = -EINVAL; | |
963 | goto error; | |
964 | } | |
965 | if (stream_data->size == -1) { | |
966 | ret = -LTTNG_UST_ERR_NOENT; | |
967 | goto error; | |
968 | } | |
969 | ||
970 | /* recv stream nr */ | |
971 | len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr, | |
972 | sizeof(stream_data->u.stream.stream_nr)); | |
973 | if (len != sizeof(stream_data->u.stream.stream_nr)) { | |
974 | if (len < 0) | |
975 | ret = len; | |
976 | else | |
977 | ret = -EINVAL; | |
978 | goto error; | |
979 | } | |
980 | ||
981 | /* recv shm fd and wakeup fd */ | |
982 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); | |
983 | if (len <= 0) { | |
984 | if (len < 0) { | |
985 | ret = len; | |
986 | goto error; | |
987 | } else { | |
988 | ret = -EIO; | |
989 | goto error; | |
0bfe09ec | 990 | } |
0bfe09ec | 991 | } |
74d81a6c MD |
992 | stream_data->u.stream.shm_fd = fds[0]; |
993 | stream_data->u.stream.wakeup_fd = fds[1]; | |
994 | *_stream_data = stream_data; | |
995 | return 0; | |
0bfe09ec | 996 | |
74d81a6c MD |
997 | error: |
998 | free(stream_data); | |
999 | error_alloc: | |
1000 | return ret; | |
1001 | } | |
1002 | ||
1003 | int ustctl_send_channel_to_ust(int sock, int session_handle, | |
1004 | struct lttng_ust_object_data *channel_data) | |
1005 | { | |
1006 | struct ustcomm_ust_msg lum; | |
1007 | struct ustcomm_ust_reply lur; | |
1008 | int ret; | |
1009 | ||
1010 | if (!channel_data) | |
1011 | return -EINVAL; | |
1012 | ||
1013 | memset(&lum, 0, sizeof(lum)); | |
1014 | lum.handle = session_handle; | |
1015 | lum.cmd = LTTNG_UST_CHANNEL; | |
1016 | lum.u.channel.len = channel_data->size; | |
1017 | lum.u.channel.type = channel_data->u.channel.type; | |
1018 | ret = ustcomm_send_app_msg(sock, &lum); | |
1019 | if (ret) | |
1020 | return ret; | |
1021 | ||
1022 | ret = ustctl_send_channel(sock, | |
1023 | channel_data->u.channel.type, | |
1024 | channel_data->u.channel.data, | |
1025 | channel_data->size, | |
ff0f5728 | 1026 | channel_data->u.channel.wakeup_fd, |
74d81a6c MD |
1027 | 1); |
1028 | if (ret) | |
1029 | return ret; | |
1030 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
1031 | if (!ret) { | |
7f2348b8 | 1032 | channel_data->handle = lur.ret_val; |
57773204 | 1033 | } |
74d81a6c MD |
1034 | return ret; |
1035 | } | |
1036 | ||
1037 | int ustctl_send_stream_to_ust(int sock, | |
1038 | struct lttng_ust_object_data *channel_data, | |
1039 | struct lttng_ust_object_data *stream_data) | |
1040 | { | |
1041 | struct ustcomm_ust_msg lum; | |
1042 | struct ustcomm_ust_reply lur; | |
1043 | int ret; | |
1044 | ||
1045 | memset(&lum, 0, sizeof(lum)); | |
1046 | lum.handle = channel_data->handle; | |
1047 | lum.cmd = LTTNG_UST_STREAM; | |
1048 | lum.u.stream.len = stream_data->size; | |
1049 | lum.u.stream.stream_nr = stream_data->u.stream.stream_nr; | |
1050 | ret = ustcomm_send_app_msg(sock, &lum); | |
1051 | if (ret) | |
1052 | return ret; | |
1053 | ||
1054 | assert(stream_data); | |
1055 | assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM); | |
1056 | ||
1057 | ret = ustctl_send_stream(sock, | |
1058 | stream_data->u.stream.stream_nr, | |
1059 | stream_data->size, | |
1060 | stream_data->u.stream.shm_fd, | |
1061 | stream_data->u.stream.wakeup_fd, 1); | |
1062 | if (ret) | |
1063 | return ret; | |
1064 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
1065 | } | |
1066 | ||
12f3dabc MD |
1067 | int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest, |
1068 | struct lttng_ust_object_data *src) | |
1069 | { | |
1070 | struct lttng_ust_object_data *obj; | |
1071 | int ret; | |
1072 | ||
1073 | if (src->handle != -1) { | |
1074 | ret = -EINVAL; | |
1075 | goto error; | |
1076 | } | |
1077 | ||
1078 | obj = zmalloc(sizeof(*obj)); | |
1079 | if (!obj) { | |
1080 | ret = -ENOMEM; | |
1081 | goto error; | |
1082 | } | |
1083 | ||
1084 | obj->type = src->type; | |
1085 | obj->handle = src->handle; | |
1086 | obj->size = src->size; | |
1087 | ||
1088 | switch (obj->type) { | |
1089 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: | |
1090 | { | |
1091 | obj->u.channel.type = src->u.channel.type; | |
1092 | if (src->u.channel.wakeup_fd >= 0) { | |
1093 | obj->u.channel.wakeup_fd = | |
1094 | dup(src->u.channel.wakeup_fd); | |
1095 | if (obj->u.channel.wakeup_fd < 0) { | |
1096 | ret = errno; | |
1097 | goto chan_error_wakeup_fd; | |
1098 | } | |
1099 | } else { | |
1100 | obj->u.channel.wakeup_fd = | |
1101 | src->u.channel.wakeup_fd; | |
1102 | } | |
1103 | obj->u.channel.data = zmalloc(obj->size); | |
1104 | if (!obj->u.channel.data) { | |
1105 | ret = -ENOMEM; | |
1106 | goto chan_error_alloc; | |
1107 | } | |
1108 | memcpy(obj->u.channel.data, src->u.channel.data, obj->size); | |
1109 | break; | |
1110 | ||
1111 | chan_error_alloc: | |
1112 | if (src->u.channel.wakeup_fd >= 0) { | |
1113 | int closeret; | |
1114 | ||
1115 | closeret = close(obj->u.channel.wakeup_fd); | |
1116 | if (closeret) { | |
1117 | PERROR("close"); | |
1118 | } | |
1119 | } | |
1120 | chan_error_wakeup_fd: | |
1121 | goto error_type; | |
1122 | ||
1123 | } | |
1124 | ||
1125 | case LTTNG_UST_OBJECT_TYPE_STREAM: | |
1126 | { | |
1127 | obj->u.stream.stream_nr = src->u.stream.stream_nr; | |
1128 | if (src->u.stream.wakeup_fd >= 0) { | |
1129 | obj->u.stream.wakeup_fd = | |
1130 | dup(src->u.stream.wakeup_fd); | |
1131 | if (obj->u.stream.wakeup_fd < 0) { | |
1132 | ret = errno; | |
1133 | goto stream_error_wakeup_fd; | |
1134 | } | |
1135 | } else { | |
1136 | obj->u.stream.wakeup_fd = | |
1137 | src->u.stream.wakeup_fd; | |
1138 | } | |
1139 | ||
1140 | if (src->u.stream.shm_fd >= 0) { | |
1141 | obj->u.stream.shm_fd = | |
1142 | dup(src->u.stream.shm_fd); | |
1143 | if (obj->u.stream.shm_fd < 0) { | |
1144 | ret = errno; | |
1145 | goto stream_error_shm_fd; | |
1146 | } | |
1147 | } else { | |
1148 | obj->u.stream.shm_fd = | |
1149 | src->u.stream.shm_fd; | |
1150 | } | |
1151 | break; | |
1152 | ||
1153 | stream_error_shm_fd: | |
1154 | if (src->u.stream.wakeup_fd >= 0) { | |
1155 | int closeret; | |
1156 | ||
1157 | closeret = close(obj->u.stream.wakeup_fd); | |
1158 | if (closeret) { | |
1159 | PERROR("close"); | |
1160 | } | |
1161 | } | |
1162 | stream_error_wakeup_fd: | |
1163 | goto error_type; | |
1164 | } | |
1165 | ||
ebabbf58 MD |
1166 | case LTTNG_UST_OBJECT_TYPE_COUNTER: |
1167 | { | |
1168 | obj->u.counter.data = zmalloc(obj->size); | |
1169 | if (!obj->u.counter.data) { | |
1170 | ret = -ENOMEM; | |
1171 | goto error_type; | |
1172 | } | |
1173 | memcpy(obj->u.counter.data, src->u.counter.data, obj->size); | |
1174 | break; | |
1175 | } | |
1176 | ||
1177 | case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL: | |
1178 | { | |
1179 | if (src->u.counter_global.shm_fd >= 0) { | |
1180 | obj->u.counter_global.shm_fd = | |
1181 | dup(src->u.counter_global.shm_fd); | |
1182 | if (obj->u.counter_global.shm_fd < 0) { | |
1183 | ret = errno; | |
1184 | goto error_type; | |
1185 | } | |
1186 | } | |
1187 | break; | |
1188 | } | |
1189 | ||
1190 | case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU: | |
1191 | { | |
1192 | obj->u.counter_cpu.cpu_nr = src->u.counter_cpu.cpu_nr; | |
1193 | if (src->u.counter_cpu.shm_fd >= 0) { | |
1194 | obj->u.counter_cpu.shm_fd = | |
1195 | dup(src->u.counter_cpu.shm_fd); | |
1196 | if (obj->u.counter_cpu.shm_fd < 0) { | |
1197 | ret = errno; | |
1198 | goto error_type; | |
1199 | } | |
1200 | } | |
1201 | break; | |
1202 | } | |
1203 | ||
12f3dabc MD |
1204 | default: |
1205 | ret = -EINVAL; | |
1206 | goto error_type; | |
1207 | } | |
1208 | ||
1209 | *dest = obj; | |
1210 | return 0; | |
1211 | ||
1212 | error_type: | |
1213 | free(obj); | |
1214 | error: | |
1215 | return ret; | |
1216 | } | |
1217 | ||
74d81a6c MD |
1218 | |
1219 | /* Buffer operations */ | |
1220 | ||
5ea386c3 MD |
1221 | int ustctl_get_nr_stream_per_channel(void) |
1222 | { | |
1223 | return num_possible_cpus(); | |
1224 | } | |
1225 | ||
74d81a6c | 1226 | struct ustctl_consumer_channel * |
5ea386c3 MD |
1227 | ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, |
1228 | const int *stream_fds, int nr_stream_fds) | |
74d81a6c MD |
1229 | { |
1230 | struct ustctl_consumer_channel *chan; | |
1231 | const char *transport_name; | |
1232 | struct lttng_transport *transport; | |
1233 | ||
1234 | switch (attr->type) { | |
1235 | case LTTNG_UST_CHAN_PER_CPU: | |
1236 | if (attr->output == LTTNG_UST_MMAP) { | |
34a91bdb MD |
1237 | if (attr->overwrite) { |
1238 | if (attr->read_timer_interval == 0) { | |
1239 | transport_name = "relay-overwrite-mmap"; | |
1240 | } else { | |
1241 | transport_name = "relay-overwrite-rt-mmap"; | |
1242 | } | |
1243 | } else { | |
1244 | if (attr->read_timer_interval == 0) { | |
1245 | transport_name = "relay-discard-mmap"; | |
1246 | } else { | |
1247 | transport_name = "relay-discard-rt-mmap"; | |
1248 | } | |
1249 | } | |
74d81a6c MD |
1250 | } else { |
1251 | return NULL; | |
1252 | } | |
c1fca457 | 1253 | break; |
74d81a6c MD |
1254 | case LTTNG_UST_CHAN_METADATA: |
1255 | if (attr->output == LTTNG_UST_MMAP) | |
1256 | transport_name = "relay-metadata-mmap"; | |
1257 | else | |
1258 | return NULL; | |
c1fca457 MD |
1259 | break; |
1260 | default: | |
74d81a6c | 1261 | transport_name = "<unknown>"; |
c1fca457 MD |
1262 | return NULL; |
1263 | } | |
74d81a6c MD |
1264 | |
1265 | transport = lttng_transport_find(transport_name); | |
1266 | if (!transport) { | |
1267 | DBG("LTTng transport %s not found\n", | |
32ce8569 | 1268 | transport_name); |
74d81a6c | 1269 | return NULL; |
7a784989 | 1270 | } |
74d81a6c MD |
1271 | |
1272 | chan = zmalloc(sizeof(*chan)); | |
1273 | if (!chan) | |
1274 | return NULL; | |
1275 | ||
1276 | chan->chan = transport->ops.channel_create(transport_name, NULL, | |
32ce8569 | 1277 | attr->subbuf_size, attr->num_subbuf, |
74d81a6c | 1278 | attr->switch_timer_interval, |
32ce8569 | 1279 | attr->read_timer_interval, |
a9ff648c | 1280 | attr->uuid, attr->chan_id, |
b2c5f61a MD |
1281 | stream_fds, nr_stream_fds, |
1282 | attr->blocking_timeout); | |
74d81a6c MD |
1283 | if (!chan->chan) { |
1284 | goto chan_error; | |
1285 | } | |
1286 | chan->chan->ops = &transport->ops; | |
1287 | memcpy(&chan->attr, attr, sizeof(chan->attr)); | |
cb7378b3 MD |
1288 | chan->wait_fd = ustctl_channel_get_wait_fd(chan); |
1289 | chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan); | |
74d81a6c MD |
1290 | return chan; |
1291 | ||
1292 | chan_error: | |
1293 | free(chan); | |
1294 | return NULL; | |
57773204 MD |
1295 | } |
1296 | ||
74d81a6c | 1297 | void ustctl_destroy_channel(struct ustctl_consumer_channel *chan) |
57773204 | 1298 | { |
b24e4e91 MD |
1299 | (void) ustctl_channel_close_wait_fd(chan); |
1300 | (void) ustctl_channel_close_wakeup_fd(chan); | |
74d81a6c MD |
1301 | chan->chan->ops->channel_destroy(chan->chan); |
1302 | free(chan); | |
1303 | } | |
1304 | ||
1305 | int ustctl_send_channel_to_sessiond(int sock, | |
1306 | struct ustctl_consumer_channel *channel) | |
1307 | { | |
1308 | struct shm_object_table *table; | |
57773204 | 1309 | |
74d81a6c MD |
1310 | table = channel->chan->handle->table; |
1311 | if (table->size <= 0) | |
9bfc503d | 1312 | return -EINVAL; |
74d81a6c MD |
1313 | return ustctl_send_channel(sock, |
1314 | channel->attr.type, | |
1315 | table->objects[0].memory_map, | |
1316 | table->objects[0].memory_map_size, | |
ff0f5728 | 1317 | channel->wakeup_fd, |
74d81a6c MD |
1318 | 0); |
1319 | } | |
9bfc503d | 1320 | |
74d81a6c MD |
1321 | int ustctl_send_stream_to_sessiond(int sock, |
1322 | struct ustctl_consumer_stream *stream) | |
1323 | { | |
1324 | if (!stream) | |
1325 | return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0); | |
1326 | ||
1327 | return ustctl_send_stream(sock, | |
1328 | stream->cpu, | |
1329 | stream->memory_map_size, | |
1330 | stream->shm_fd, stream->wakeup_fd, | |
1331 | 0); | |
57773204 MD |
1332 | } |
1333 | ||
c9023c93 MD |
1334 | int ustctl_write_metadata_to_channel( |
1335 | struct ustctl_consumer_channel *channel, | |
1336 | const char *metadata_str, /* NOT null-terminated */ | |
1337 | size_t len) /* metadata length */ | |
1338 | { | |
1339 | struct lttng_ust_lib_ring_buffer_ctx ctx; | |
1340 | struct lttng_channel *chan = channel->chan; | |
1341 | const char *str = metadata_str; | |
1342 | int ret = 0, waitret; | |
1343 | size_t reserve_len, pos; | |
1344 | ||
1345 | for (pos = 0; pos < len; pos += reserve_len) { | |
1346 | reserve_len = min_t(size_t, | |
1347 | chan->ops->packet_avail_size(chan->chan, chan->handle), | |
1348 | len - pos); | |
1349 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
53569322 | 1350 | sizeof(char), -1, chan->handle, NULL); |
c9023c93 MD |
1351 | /* |
1352 | * We don't care about metadata buffer's records lost | |
1353 | * count, because we always retry here. Report error if | |
1354 | * we need to bail out after timeout or being | |
1355 | * interrupted. | |
1356 | */ | |
1357 | waitret = wait_cond_interruptible_timeout( | |
1358 | ({ | |
1359 | ret = chan->ops->event_reserve(&ctx, 0); | |
1360 | ret != -ENOBUFS || !ret; | |
1361 | }), | |
1362 | LTTNG_METADATA_TIMEOUT_MSEC); | |
1363 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { | |
1364 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", | |
1365 | waitret == -EINTR ? "interrupted" : | |
1366 | (ret == -ENOBUFS ? "timeout" : "I/O error")); | |
1367 | if (waitret == -EINTR) | |
1368 | ret = waitret; | |
1369 | goto end; | |
1370 | } | |
1371 | chan->ops->event_write(&ctx, &str[pos], reserve_len); | |
1372 | chan->ops->event_commit(&ctx); | |
1373 | } | |
1374 | end: | |
1375 | return ret; | |
1376 | } | |
1377 | ||
3ef94b0e JD |
1378 | /* |
1379 | * Write at most one packet in the channel. | |
1380 | * Returns the number of bytes written on success, < 0 on error. | |
1381 | */ | |
1382 | ssize_t ustctl_write_one_packet_to_channel( | |
1383 | struct ustctl_consumer_channel *channel, | |
1384 | const char *metadata_str, /* NOT null-terminated */ | |
1385 | size_t len) /* metadata length */ | |
1386 | { | |
1387 | struct lttng_ust_lib_ring_buffer_ctx ctx; | |
1388 | struct lttng_channel *chan = channel->chan; | |
1389 | const char *str = metadata_str; | |
1390 | ssize_t reserve_len; | |
1391 | int ret; | |
1392 | ||
1393 | reserve_len = min_t(ssize_t, | |
1394 | chan->ops->packet_avail_size(chan->chan, chan->handle), | |
1395 | len); | |
1396 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
53569322 | 1397 | sizeof(char), -1, chan->handle, NULL); |
3ef94b0e JD |
1398 | ret = chan->ops->event_reserve(&ctx, 0); |
1399 | if (ret != 0) { | |
1400 | DBG("LTTng: event reservation failed"); | |
1401 | assert(ret < 0); | |
1402 | reserve_len = ret; | |
1403 | goto end; | |
1404 | } | |
1405 | chan->ops->event_write(&ctx, str, reserve_len); | |
1406 | chan->ops->event_commit(&ctx); | |
1407 | ||
1408 | end: | |
1409 | return reserve_len; | |
1410 | } | |
1411 | ||
ff0f5728 MD |
1412 | int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan) |
1413 | { | |
1414 | struct channel *chan; | |
cb7378b3 | 1415 | int ret; |
ff0f5728 MD |
1416 | |
1417 | chan = consumer_chan->chan->chan; | |
cb7378b3 | 1418 | ret = ring_buffer_channel_close_wait_fd(&chan->backend.config, |
ff0f5728 | 1419 | chan, chan->handle); |
cb7378b3 MD |
1420 | if (!ret) |
1421 | consumer_chan->wait_fd = -1; | |
1422 | return ret; | |
ff0f5728 MD |
1423 | } |
1424 | ||
1425 | int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan) | |
1426 | { | |
1427 | struct channel *chan; | |
cb7378b3 | 1428 | int ret; |
ff0f5728 MD |
1429 | |
1430 | chan = consumer_chan->chan->chan; | |
cb7378b3 | 1431 | ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config, |
ff0f5728 | 1432 | chan, chan->handle); |
cb7378b3 MD |
1433 | if (!ret) |
1434 | consumer_chan->wakeup_fd = -1; | |
1435 | return ret; | |
ff0f5728 MD |
1436 | } |
1437 | ||
74d81a6c | 1438 | int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream) |
5224b5c8 MD |
1439 | { |
1440 | struct channel *chan; | |
1441 | ||
74d81a6c | 1442 | chan = stream->chan->chan->chan; |
ff0f5728 | 1443 | return ring_buffer_stream_close_wait_fd(&chan->backend.config, |
74d81a6c | 1444 | chan, stream->handle, stream->cpu); |
5224b5c8 MD |
1445 | } |
1446 | ||
74d81a6c | 1447 | int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream) |
6e922b24 | 1448 | { |
66bdd22a | 1449 | struct channel *chan; |
74d81a6c MD |
1450 | |
1451 | chan = stream->chan->chan->chan; | |
ff0f5728 | 1452 | return ring_buffer_stream_close_wakeup_fd(&chan->backend.config, |
74d81a6c MD |
1453 | chan, stream->handle, stream->cpu); |
1454 | } | |
1455 | ||
1456 | struct ustctl_consumer_stream * | |
1457 | ustctl_create_stream(struct ustctl_consumer_channel *channel, | |
1458 | int cpu) | |
1459 | { | |
1460 | struct ustctl_consumer_stream *stream; | |
1461 | struct lttng_ust_shm_handle *handle; | |
1462 | struct channel *chan; | |
1463 | int shm_fd, wait_fd, wakeup_fd; | |
1464 | uint64_t memory_map_size; | |
4cfec15c | 1465 | struct lttng_ust_lib_ring_buffer *buf; |
6e922b24 MD |
1466 | int ret; |
1467 | ||
74d81a6c MD |
1468 | if (!channel) |
1469 | return NULL; | |
1470 | handle = channel->chan->handle; | |
9bfc503d MD |
1471 | if (!handle) |
1472 | return NULL; | |
1473 | ||
74d81a6c | 1474 | chan = channel->chan->chan; |
6e922b24 | 1475 | buf = channel_get_ring_buffer(&chan->backend.config, |
74d81a6c MD |
1476 | chan, cpu, handle, &shm_fd, &wait_fd, |
1477 | &wakeup_fd, &memory_map_size); | |
6e922b24 MD |
1478 | if (!buf) |
1479 | return NULL; | |
74d81a6c | 1480 | ret = lib_ring_buffer_open_read(buf, handle); |
6e922b24 MD |
1481 | if (ret) |
1482 | return NULL; | |
74d81a6c MD |
1483 | |
1484 | stream = zmalloc(sizeof(*stream)); | |
1485 | if (!stream) | |
1486 | goto alloc_error; | |
1487 | stream->handle = handle; | |
1488 | stream->buf = buf; | |
1489 | stream->chan = channel; | |
1490 | stream->shm_fd = shm_fd; | |
1491 | stream->wait_fd = wait_fd; | |
1492 | stream->wakeup_fd = wakeup_fd; | |
1493 | stream->memory_map_size = memory_map_size; | |
1494 | stream->cpu = cpu; | |
1495 | return stream; | |
1496 | ||
1497 | alloc_error: | |
1498 | return NULL; | |
1499 | } | |
1500 | ||
1501 | void ustctl_destroy_stream(struct ustctl_consumer_stream *stream) | |
1502 | { | |
1503 | struct lttng_ust_lib_ring_buffer *buf; | |
1504 | struct ustctl_consumer_channel *consumer_chan; | |
1505 | ||
1506 | assert(stream); | |
1507 | buf = stream->buf; | |
1508 | consumer_chan = stream->chan; | |
b24e4e91 MD |
1509 | (void) ustctl_stream_close_wait_fd(stream); |
1510 | (void) ustctl_stream_close_wakeup_fd(stream); | |
74d81a6c MD |
1511 | lib_ring_buffer_release_read(buf, consumer_chan->chan->handle); |
1512 | free(stream); | |
6e922b24 MD |
1513 | } |
1514 | ||
ff0f5728 MD |
1515 | int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan) |
1516 | { | |
1517 | if (!chan) | |
1518 | return -EINVAL; | |
1519 | return shm_get_wait_fd(chan->chan->handle, | |
1520 | &chan->chan->handle->chan._ref); | |
1521 | } | |
1522 | ||
1523 | int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan) | |
1524 | { | |
1525 | if (!chan) | |
1526 | return -EINVAL; | |
1527 | return shm_get_wakeup_fd(chan->chan->handle, | |
1528 | &chan->chan->handle->chan._ref); | |
1529 | } | |
1530 | ||
1531 | int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream) | |
6e922b24 | 1532 | { |
74d81a6c MD |
1533 | struct lttng_ust_lib_ring_buffer *buf; |
1534 | struct ustctl_consumer_channel *consumer_chan; | |
1535 | ||
1536 | if (!stream) | |
1537 | return -EINVAL; | |
1538 | buf = stream->buf; | |
1539 | consumer_chan = stream->chan; | |
1540 | return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref); | |
1541 | } | |
1542 | ||
ff0f5728 | 1543 | int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream) |
74d81a6c MD |
1544 | { |
1545 | struct lttng_ust_lib_ring_buffer *buf; | |
1546 | struct ustctl_consumer_channel *consumer_chan; | |
1547 | ||
1548 | if (!stream) | |
1549 | return -EINVAL; | |
1550 | buf = stream->buf; | |
1551 | consumer_chan = stream->chan; | |
1552 | return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref); | |
6e922b24 MD |
1553 | } |
1554 | ||
57773204 MD |
1555 | /* For mmap mode, readable without "get" operation */ |
1556 | ||
74d81a6c | 1557 | void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream) |
9095efe9 | 1558 | { |
74d81a6c MD |
1559 | struct lttng_ust_lib_ring_buffer *buf; |
1560 | struct ustctl_consumer_channel *consumer_chan; | |
1561 | ||
1562 | if (!stream) | |
9bfc503d | 1563 | return NULL; |
74d81a6c MD |
1564 | buf = stream->buf; |
1565 | consumer_chan = stream->chan; | |
1566 | return shmp(consumer_chan->chan->handle, buf->backend.memory_map); | |
9095efe9 MD |
1567 | } |
1568 | ||
57773204 | 1569 | /* returns the length to mmap. */ |
74d81a6c | 1570 | int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream, |
57773204 MD |
1571 | unsigned long *len) |
1572 | { | |
74d81a6c | 1573 | struct ustctl_consumer_channel *consumer_chan; |
57773204 | 1574 | unsigned long mmap_buf_len; |
66bdd22a | 1575 | struct channel *chan; |
57773204 | 1576 | |
74d81a6c | 1577 | if (!stream) |
9bfc503d | 1578 | return -EINVAL; |
74d81a6c MD |
1579 | consumer_chan = stream->chan; |
1580 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1581 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
1582 | return -EINVAL; | |
1583 | mmap_buf_len = chan->backend.buf_size; | |
1584 | if (chan->backend.extra_reader_sb) | |
1585 | mmap_buf_len += chan->backend.subbuf_size; | |
1586 | if (mmap_buf_len > INT_MAX) | |
1587 | return -EFBIG; | |
1588 | *len = mmap_buf_len; | |
1589 | return 0; | |
1590 | } | |
1591 | ||
1592 | /* returns the maximum size for sub-buffers. */ | |
74d81a6c | 1593 | int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream, |
57773204 MD |
1594 | unsigned long *len) |
1595 | { | |
74d81a6c | 1596 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1597 | struct channel *chan; |
57773204 | 1598 | |
74d81a6c | 1599 | if (!stream) |
9bfc503d | 1600 | return -EINVAL; |
74d81a6c MD |
1601 | consumer_chan = stream->chan; |
1602 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1603 | *len = chan->backend.subbuf_size; |
1604 | return 0; | |
1605 | } | |
1606 | ||
1607 | /* | |
1608 | * For mmap mode, operate on the current packet (between get/put or | |
1609 | * get_next/put_next). | |
1610 | */ | |
1611 | ||
1612 | /* returns the offset of the subbuffer belonging to the mmap reader. */ | |
74d81a6c MD |
1613 | int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream, |
1614 | unsigned long *off) | |
57773204 | 1615 | { |
66bdd22a | 1616 | struct channel *chan; |
57773204 | 1617 | unsigned long sb_bindex; |
74d81a6c MD |
1618 | struct lttng_ust_lib_ring_buffer *buf; |
1619 | struct ustctl_consumer_channel *consumer_chan; | |
34daae3e MD |
1620 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx; |
1621 | struct lttng_ust_lib_ring_buffer_backend_pages *pages; | |
57773204 | 1622 | |
74d81a6c | 1623 | if (!stream) |
9bfc503d | 1624 | return -EINVAL; |
74d81a6c MD |
1625 | buf = stream->buf; |
1626 | consumer_chan = stream->chan; | |
1627 | chan = consumer_chan->chan->chan; | |
57773204 MD |
1628 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
1629 | return -EINVAL; | |
1630 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, | |
32ce8569 | 1631 | buf->backend.buf_rsb.id); |
34daae3e MD |
1632 | barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array, |
1633 | sb_bindex); | |
1634 | if (!barray_idx) | |
1635 | return -EINVAL; | |
1636 | pages = shmp(consumer_chan->chan->handle, barray_idx->shmp); | |
1637 | if (!pages) | |
1638 | return -EINVAL; | |
1639 | *off = pages->mmap_offset; | |
57773204 MD |
1640 | return 0; |
1641 | } | |
1642 | ||
1643 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
74d81a6c MD |
1644 | int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream, |
1645 | unsigned long *len) | |
57773204 | 1646 | { |
74d81a6c | 1647 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1648 | struct channel *chan; |
74d81a6c | 1649 | struct lttng_ust_lib_ring_buffer *buf; |
57773204 | 1650 | |
74d81a6c | 1651 | if (!stream) |
9bfc503d MD |
1652 | return -EINVAL; |
1653 | ||
74d81a6c MD |
1654 | buf = stream->buf; |
1655 | consumer_chan = stream->chan; | |
1656 | chan = consumer_chan->chan->chan; | |
57773204 | 1657 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
74d81a6c | 1658 | consumer_chan->chan->handle); |
57773204 MD |
1659 | return 0; |
1660 | } | |
1661 | ||
1662 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
74d81a6c MD |
1663 | int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream, |
1664 | unsigned long *len) | |
57773204 | 1665 | { |
74d81a6c | 1666 | struct ustctl_consumer_channel *consumer_chan; |
66bdd22a | 1667 | struct channel *chan; |
74d81a6c | 1668 | struct lttng_ust_lib_ring_buffer *buf; |
57773204 | 1669 | |
74d81a6c | 1670 | if (!stream) |
9bfc503d | 1671 | return -EINVAL; |
74d81a6c MD |
1672 | buf = stream->buf; |
1673 | consumer_chan = stream->chan; | |
1674 | chan = consumer_chan->chan->chan; | |
57773204 | 1675 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
74d81a6c | 1676 | consumer_chan->chan->handle); |
b72687b8 | 1677 | *len = LTTNG_UST_PAGE_ALIGN(*len); |
57773204 MD |
1678 | return 0; |
1679 | } | |
1680 | ||
1681 | /* Get exclusive read access to the next sub-buffer that can be read. */ | |
74d81a6c | 1682 | int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1683 | { |
74d81a6c MD |
1684 | struct lttng_ust_lib_ring_buffer *buf; |
1685 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1686 | |
74d81a6c MD |
1687 | if (!stream) |
1688 | return -EINVAL; | |
1689 | buf = stream->buf; | |
1690 | consumer_chan = stream->chan; | |
1691 | return lib_ring_buffer_get_next_subbuf(buf, | |
1692 | consumer_chan->chan->handle); | |
57773204 MD |
1693 | } |
1694 | ||
1695 | ||
1696 | /* Release exclusive sub-buffer access, move consumer forward. */ | |
74d81a6c | 1697 | int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1698 | { |
74d81a6c MD |
1699 | struct lttng_ust_lib_ring_buffer *buf; |
1700 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1701 | |
74d81a6c MD |
1702 | if (!stream) |
1703 | return -EINVAL; | |
1704 | buf = stream->buf; | |
1705 | consumer_chan = stream->chan; | |
1706 | lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle); | |
57773204 MD |
1707 | return 0; |
1708 | } | |
1709 | ||
1710 | /* snapshot */ | |
1711 | ||
1712 | /* Get a snapshot of the current ring buffer producer and consumer positions */ | |
74d81a6c | 1713 | int ustctl_snapshot(struct ustctl_consumer_stream *stream) |
57773204 | 1714 | { |
74d81a6c MD |
1715 | struct lttng_ust_lib_ring_buffer *buf; |
1716 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1717 | |
74d81a6c MD |
1718 | if (!stream) |
1719 | return -EINVAL; | |
1720 | buf = stream->buf; | |
1721 | consumer_chan = stream->chan; | |
57773204 | 1722 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
74d81a6c | 1723 | &buf->prod_snapshot, consumer_chan->chan->handle); |
57773204 MD |
1724 | } |
1725 | ||
f45930b7 JG |
1726 | /* |
1727 | * Get a snapshot of the current ring buffer producer and consumer positions | |
1728 | * even if the consumed and produced positions are contained within the same | |
1729 | * subbuffer. | |
1730 | */ | |
1731 | int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream) | |
1732 | { | |
1733 | struct lttng_ust_lib_ring_buffer *buf; | |
1734 | struct ustctl_consumer_channel *consumer_chan; | |
1735 | ||
1736 | if (!stream) | |
1737 | return -EINVAL; | |
1738 | buf = stream->buf; | |
1739 | consumer_chan = stream->chan; | |
1740 | return lib_ring_buffer_snapshot_sample_positions(buf, | |
1741 | &buf->cons_snapshot, &buf->prod_snapshot, | |
1742 | consumer_chan->chan->handle); | |
1743 | } | |
1744 | ||
57773204 | 1745 | /* Get the consumer position (iteration start) */ |
74d81a6c MD |
1746 | int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream, |
1747 | unsigned long *pos) | |
57773204 | 1748 | { |
74d81a6c | 1749 | struct lttng_ust_lib_ring_buffer *buf; |
9bfc503d | 1750 | |
74d81a6c MD |
1751 | if (!stream) |
1752 | return -EINVAL; | |
1753 | buf = stream->buf; | |
57773204 MD |
1754 | *pos = buf->cons_snapshot; |
1755 | return 0; | |
1756 | } | |
1757 | ||
1758 | /* Get the producer position (iteration end) */ | |
74d81a6c MD |
1759 | int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream, |
1760 | unsigned long *pos) | |
57773204 | 1761 | { |
74d81a6c | 1762 | struct lttng_ust_lib_ring_buffer *buf; |
9bfc503d | 1763 | |
74d81a6c MD |
1764 | if (!stream) |
1765 | return -EINVAL; | |
1766 | buf = stream->buf; | |
57773204 MD |
1767 | *pos = buf->prod_snapshot; |
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | /* Get exclusive read access to the specified sub-buffer position */ | |
74d81a6c MD |
1772 | int ustctl_get_subbuf(struct ustctl_consumer_stream *stream, |
1773 | unsigned long *pos) | |
57773204 | 1774 | { |
74d81a6c MD |
1775 | struct lttng_ust_lib_ring_buffer *buf; |
1776 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1777 | |
74d81a6c MD |
1778 | if (!stream) |
1779 | return -EINVAL; | |
1780 | buf = stream->buf; | |
1781 | consumer_chan = stream->chan; | |
1782 | return lib_ring_buffer_get_subbuf(buf, *pos, | |
1783 | consumer_chan->chan->handle); | |
57773204 MD |
1784 | } |
1785 | ||
1786 | /* Release exclusive sub-buffer access */ | |
74d81a6c | 1787 | int ustctl_put_subbuf(struct ustctl_consumer_stream *stream) |
57773204 | 1788 | { |
74d81a6c MD |
1789 | struct lttng_ust_lib_ring_buffer *buf; |
1790 | struct ustctl_consumer_channel *consumer_chan; | |
9bfc503d | 1791 | |
74d81a6c MD |
1792 | if (!stream) |
1793 | return -EINVAL; | |
1794 | buf = stream->buf; | |
1795 | consumer_chan = stream->chan; | |
1796 | lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle); | |
57773204 MD |
1797 | return 0; |
1798 | } | |
1799 | ||
74d81a6c | 1800 | void ustctl_flush_buffer(struct ustctl_consumer_stream *stream, |
b52190f2 | 1801 | int producer_active) |
57773204 | 1802 | { |
74d81a6c MD |
1803 | struct lttng_ust_lib_ring_buffer *buf; |
1804 | struct ustctl_consumer_channel *consumer_chan; | |
1805 | ||
1806 | assert(stream); | |
1807 | buf = stream->buf; | |
1808 | consumer_chan = stream->chan; | |
b52190f2 MD |
1809 | lib_ring_buffer_switch_slow(buf, |
1810 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, | |
74d81a6c MD |
1811 | consumer_chan->chan->handle); |
1812 | } | |
1813 | ||
beca55a1 MD |
1814 | void ustctl_clear_buffer(struct ustctl_consumer_stream *stream) |
1815 | { | |
1816 | struct lttng_ust_lib_ring_buffer *buf; | |
1817 | struct ustctl_consumer_channel *consumer_chan; | |
1818 | ||
1819 | assert(stream); | |
1820 | buf = stream->buf; | |
1821 | consumer_chan = stream->chan; | |
1822 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, | |
1823 | consumer_chan->chan->handle); | |
1824 | lib_ring_buffer_clear_reader(buf, consumer_chan->chan->handle); | |
1825 | } | |
1826 | ||
b2f3252a JD |
1827 | static |
1828 | struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb( | |
1829 | struct lttng_ust_lib_ring_buffer *buf, | |
1830 | struct lttng_ust_shm_handle *handle) | |
1831 | { | |
1832 | struct channel *chan; | |
1833 | const struct lttng_ust_lib_ring_buffer_config *config; | |
1834 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
1835 | ||
1836 | chan = shmp(handle, buf->backend.chan); | |
34daae3e MD |
1837 | if (!chan) |
1838 | return NULL; | |
b2f3252a JD |
1839 | config = &chan->backend.config; |
1840 | if (!config->cb_ptr) | |
1841 | return NULL; | |
1842 | client_cb = caa_container_of(config->cb_ptr, | |
1843 | struct lttng_ust_client_lib_ring_buffer_client_cb, | |
1844 | parent); | |
1845 | return client_cb; | |
1846 | } | |
1847 | ||
1848 | int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream, | |
1849 | uint64_t *timestamp_begin) | |
1850 | { | |
1851 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1852 | struct lttng_ust_lib_ring_buffer *buf; |
1853 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1854 | |
1855 | if (!stream || !timestamp_begin) | |
1856 | return -EINVAL; | |
e1919a41 MD |
1857 | buf = stream->buf; |
1858 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1859 | client_cb = get_client_cb(buf, handle); |
1860 | if (!client_cb) | |
1861 | return -ENOSYS; | |
1862 | return client_cb->timestamp_begin(buf, handle, timestamp_begin); | |
1863 | } | |
1864 | ||
1865 | int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream, | |
1866 | uint64_t *timestamp_end) | |
1867 | { | |
1868 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1869 | struct lttng_ust_lib_ring_buffer *buf; |
1870 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1871 | |
1872 | if (!stream || !timestamp_end) | |
1873 | return -EINVAL; | |
e1919a41 MD |
1874 | buf = stream->buf; |
1875 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1876 | client_cb = get_client_cb(buf, handle); |
1877 | if (!client_cb) | |
1878 | return -ENOSYS; | |
1879 | return client_cb->timestamp_end(buf, handle, timestamp_end); | |
1880 | } | |
1881 | ||
1882 | int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream, | |
1883 | uint64_t *events_discarded) | |
1884 | { | |
1885 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1886 | struct lttng_ust_lib_ring_buffer *buf; |
1887 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1888 | |
1889 | if (!stream || !events_discarded) | |
1890 | return -EINVAL; | |
e1919a41 MD |
1891 | buf = stream->buf; |
1892 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1893 | client_cb = get_client_cb(buf, handle); |
1894 | if (!client_cb) | |
1895 | return -ENOSYS; | |
1896 | return client_cb->events_discarded(buf, handle, events_discarded); | |
1897 | } | |
1898 | ||
1899 | int ustctl_get_content_size(struct ustctl_consumer_stream *stream, | |
1900 | uint64_t *content_size) | |
1901 | { | |
1902 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1903 | struct lttng_ust_lib_ring_buffer *buf; |
1904 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1905 | |
1906 | if (!stream || !content_size) | |
1907 | return -EINVAL; | |
e1919a41 MD |
1908 | buf = stream->buf; |
1909 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1910 | client_cb = get_client_cb(buf, handle); |
1911 | if (!client_cb) | |
1912 | return -ENOSYS; | |
1913 | return client_cb->content_size(buf, handle, content_size); | |
1914 | } | |
1915 | ||
1916 | int ustctl_get_packet_size(struct ustctl_consumer_stream *stream, | |
1917 | uint64_t *packet_size) | |
1918 | { | |
1919 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1920 | struct lttng_ust_lib_ring_buffer *buf; |
1921 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1922 | |
1923 | if (!stream || !packet_size) | |
1924 | return -EINVAL; | |
e1919a41 MD |
1925 | buf = stream->buf; |
1926 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1927 | client_cb = get_client_cb(buf, handle); |
1928 | if (!client_cb) | |
1929 | return -ENOSYS; | |
1930 | return client_cb->packet_size(buf, handle, packet_size); | |
1931 | } | |
1932 | ||
1933 | int ustctl_get_stream_id(struct ustctl_consumer_stream *stream, | |
1934 | uint64_t *stream_id) | |
1935 | { | |
1936 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1937 | struct lttng_ust_lib_ring_buffer *buf; |
1938 | struct lttng_ust_shm_handle *handle; | |
b2f3252a JD |
1939 | |
1940 | if (!stream || !stream_id) | |
1941 | return -EINVAL; | |
e1919a41 MD |
1942 | buf = stream->buf; |
1943 | handle = stream->chan->chan->handle; | |
b2f3252a JD |
1944 | client_cb = get_client_cb(buf, handle); |
1945 | if (!client_cb) | |
1946 | return -ENOSYS; | |
1947 | return client_cb->stream_id(buf, handle, stream_id); | |
1948 | } | |
1949 | ||
fca361e8 JD |
1950 | int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream, |
1951 | uint64_t *ts) | |
1952 | { | |
1953 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
e1919a41 MD |
1954 | struct lttng_ust_lib_ring_buffer *buf; |
1955 | struct lttng_ust_shm_handle *handle; | |
fca361e8 JD |
1956 | |
1957 | if (!stream || !ts) | |
1958 | return -EINVAL; | |
e1919a41 MD |
1959 | buf = stream->buf; |
1960 | handle = stream->chan->chan->handle; | |
fca361e8 JD |
1961 | client_cb = get_client_cb(buf, handle); |
1962 | if (!client_cb || !client_cb->current_timestamp) | |
1963 | return -ENOSYS; | |
1964 | return client_cb->current_timestamp(buf, handle, ts); | |
1965 | } | |
1966 | ||
1ff31389 JD |
1967 | int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream, |
1968 | uint64_t *seq) | |
1969 | { | |
1970 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
1971 | struct lttng_ust_lib_ring_buffer *buf; | |
1972 | struct lttng_ust_shm_handle *handle; | |
1973 | ||
1974 | if (!stream || !seq) | |
1975 | return -EINVAL; | |
1976 | buf = stream->buf; | |
1977 | handle = stream->chan->chan->handle; | |
1978 | client_cb = get_client_cb(buf, handle); | |
1979 | if (!client_cb || !client_cb->sequence_number) | |
1980 | return -ENOSYS; | |
1981 | return client_cb->sequence_number(buf, handle, seq); | |
1982 | } | |
1983 | ||
45a00b05 JD |
1984 | int ustctl_get_instance_id(struct ustctl_consumer_stream *stream, |
1985 | uint64_t *id) | |
1986 | { | |
1987 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; | |
1988 | struct lttng_ust_lib_ring_buffer *buf; | |
1989 | struct lttng_ust_shm_handle *handle; | |
1990 | ||
1991 | if (!stream || !id) | |
1992 | return -EINVAL; | |
1993 | buf = stream->buf; | |
1994 | handle = stream->chan->chan->handle; | |
1995 | client_cb = get_client_cb(buf, handle); | |
1996 | if (!client_cb) | |
1997 | return -ENOSYS; | |
1998 | return client_cb->instance_id(buf, handle, id); | |
1999 | } | |
2000 | ||
c62a3816 | 2001 | #ifdef LTTNG_UST_HAVE_PERF_EVENT |
57201bb3 MD |
2002 | |
2003 | int ustctl_has_perf_counters(void) | |
2004 | { | |
2005 | return 1; | |
2006 | } | |
2007 | ||
2008 | #else | |
2009 | ||
2010 | int ustctl_has_perf_counters(void) | |
2011 | { | |
2012 | return 0; | |
2013 | } | |
2014 | ||
2015 | #endif | |
2016 | ||
a834901f MD |
2017 | #ifdef __linux__ |
2018 | /* | |
2019 | * Override application pid/uid/gid with unix socket credentials. If | |
2020 | * the application announced a pid matching our view, it means it is | |
2021 | * within the same pid namespace, so expose the ppid provided by the | |
2022 | * application. | |
2023 | */ | |
2024 | static | |
2025 | int get_cred(int sock, | |
2026 | const struct ustctl_reg_msg *reg_msg, | |
2027 | uint32_t *pid, | |
2028 | uint32_t *ppid, | |
2029 | uint32_t *uid, | |
2030 | uint32_t *gid) | |
2031 | { | |
2032 | struct ucred ucred; | |
2033 | socklen_t ucred_len = sizeof(struct ucred); | |
2034 | int ret; | |
2035 | ||
2036 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len); | |
2037 | if (ret) { | |
2038 | return -LTTNG_UST_ERR_PEERCRED; | |
2039 | } | |
2040 | DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], " | |
2041 | "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]", | |
2042 | ucred.pid, ucred.uid, ucred.gid, | |
2043 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); | |
2044 | if (!ucred.pid) { | |
2045 | ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace."); | |
2046 | return -LTTNG_UST_ERR_PEERCRED_PID; | |
2047 | } | |
2048 | *pid = ucred.pid; | |
2049 | *uid = ucred.uid; | |
2050 | *gid = ucred.gid; | |
2051 | if (ucred.pid == reg_msg->pid) { | |
2052 | *ppid = reg_msg->ppid; | |
2053 | } else { | |
2054 | *ppid = 0; | |
2055 | } | |
2056 | return 0; | |
2057 | } | |
2058 | #elif defined(__FreeBSD__) | |
2059 | #include <sys/ucred.h> | |
e494a9cd | 2060 | #include <sys/un.h> |
a834901f MD |
2061 | |
2062 | /* | |
2063 | * Override application uid/gid with unix socket credentials. Use the | |
2064 | * first group of the cr_groups. | |
2065 | * Use the pid and ppid provided by the application on registration. | |
2066 | */ | |
2067 | static | |
2068 | int get_cred(int sock, | |
2069 | const struct ustctl_reg_msg *reg_msg, | |
2070 | uint32_t *pid, | |
2071 | uint32_t *ppid, | |
2072 | uint32_t *uid, | |
2073 | uint32_t *gid) | |
2074 | { | |
2075 | struct xucred xucred; | |
2076 | socklen_t xucred_len = sizeof(struct xucred); | |
2077 | int ret; | |
2078 | ||
2079 | ret = getsockopt(sock, SOL_SOCKET, LOCAL_PEERCRED, &xucred, &xucred_len); | |
2080 | if (ret) { | |
2081 | return -LTTNG_UST_ERR_PEERCRED; | |
2082 | } | |
2083 | if (xucred.cr_version != XUCRED_VERSION || xucred.cr_ngroups < 1) { | |
2084 | return -LTTNG_UST_ERR_PEERCRED; | |
2085 | } | |
2086 | DBG("Unix socket peercred [ uid: %u, gid: %u ], " | |
2087 | "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]", | |
e494a9cd | 2088 | xucred.cr_uid, xucred.cr_groups[0], |
a834901f MD |
2089 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); |
2090 | *pid = reg_msg->pid; | |
2091 | *ppid = reg_msg->ppid; | |
e494a9cd | 2092 | *uid = xucred.cr_uid; |
a834901f MD |
2093 | *gid = xucred.cr_groups[0]; |
2094 | return 0; | |
2095 | } | |
2096 | #else | |
2097 | #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform." | |
2098 | static | |
2099 | int get_cred(int sock, | |
2100 | const struct ustctl_reg_msg *reg_msg, | |
2101 | uint32_t *pid, | |
2102 | uint32_t *ppid, | |
2103 | uint32_t *uid, | |
2104 | uint32_t *gid) | |
2105 | { | |
2106 | DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]", | |
2107 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); | |
2108 | *pid = reg_msg->pid; | |
2109 | *ppid = reg_msg->ppid; | |
2110 | *uid = reg_msg->uid; | |
2111 | *gid = reg_msg->gid; | |
2112 | return 0; | |
2113 | } | |
2114 | #endif | |
2115 | ||
32ce8569 MD |
2116 | /* |
2117 | * Returns 0 on success, negative error value on error. | |
2118 | */ | |
2119 | int ustctl_recv_reg_msg(int sock, | |
2120 | enum ustctl_socket_type *type, | |
2121 | uint32_t *major, | |
2122 | uint32_t *minor, | |
2123 | uint32_t *pid, | |
2124 | uint32_t *ppid, | |
2125 | uint32_t *uid, | |
2126 | uint32_t *gid, | |
2127 | uint32_t *bits_per_long, | |
2128 | uint32_t *uint8_t_alignment, | |
2129 | uint32_t *uint16_t_alignment, | |
2130 | uint32_t *uint32_t_alignment, | |
2131 | uint32_t *uint64_t_alignment, | |
2132 | uint32_t *long_alignment, | |
2133 | int *byte_order, | |
2134 | char *name) | |
2135 | { | |
2136 | ssize_t len; | |
2137 | struct ustctl_reg_msg reg_msg; | |
2138 | ||
2139 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); | |
2140 | if (len > 0 && len != sizeof(reg_msg)) | |
2141 | return -EIO; | |
2142 | if (len == 0) | |
2143 | return -EPIPE; | |
2144 | if (len < 0) | |
2145 | return len; | |
2146 | ||
2147 | if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) { | |
2148 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? | |
2149 | BIG_ENDIAN : LITTLE_ENDIAN; | |
2150 | } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) { | |
2151 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? | |
2152 | LITTLE_ENDIAN : BIG_ENDIAN; | |
2153 | } else { | |
2154 | return -LTTNG_UST_ERR_INVAL_MAGIC; | |
2155 | } | |
2156 | switch (reg_msg.socket_type) { | |
2157 | case 0: *type = USTCTL_SOCKET_CMD; | |
2158 | break; | |
2159 | case 1: *type = USTCTL_SOCKET_NOTIFY; | |
2160 | break; | |
2161 | default: | |
2162 | return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE; | |
2163 | } | |
2164 | *major = reg_msg.major; | |
2165 | *minor = reg_msg.minor; | |
32ce8569 MD |
2166 | *bits_per_long = reg_msg.bits_per_long; |
2167 | *uint8_t_alignment = reg_msg.uint8_t_alignment; | |
2168 | *uint16_t_alignment = reg_msg.uint16_t_alignment; | |
2169 | *uint32_t_alignment = reg_msg.uint32_t_alignment; | |
2170 | *uint64_t_alignment = reg_msg.uint64_t_alignment; | |
2171 | *long_alignment = reg_msg.long_alignment; | |
2172 | memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN); | |
6a359b8a MD |
2173 | if (reg_msg.major < LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE || |
2174 | reg_msg.major > LTTNG_UST_ABI_MAJOR_VERSION) { | |
32ce8569 MD |
2175 | return -LTTNG_UST_ERR_UNSUP_MAJOR; |
2176 | } | |
a834901f | 2177 | return get_cred(sock, ®_msg, pid, ppid, uid, gid); |
32ce8569 MD |
2178 | } |
2179 | ||
2180 | int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd) | |
2181 | { | |
2182 | struct ustcomm_notify_hdr header; | |
2183 | ssize_t len; | |
2184 | ||
2185 | len = ustcomm_recv_unix_sock(sock, &header, sizeof(header)); | |
2186 | if (len > 0 && len != sizeof(header)) | |
2187 | return -EIO; | |
2188 | if (len == 0) | |
2189 | return -EPIPE; | |
2190 | if (len < 0) | |
2191 | return len; | |
2192 | switch (header.notify_cmd) { | |
2193 | case 0: | |
2194 | *notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
2195 | break; | |
2196 | case 1: | |
2197 | *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
2198 | break; | |
c785c634 MD |
2199 | case 2: |
2200 | *notify_cmd = USTCTL_NOTIFY_CMD_ENUM; | |
2201 | break; | |
32ce8569 MD |
2202 | default: |
2203 | return -EINVAL; | |
2204 | } | |
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | /* | |
2209 | * Returns 0 on success, negative error value on error. | |
2210 | */ | |
2211 | int ustctl_recv_register_event(int sock, | |
2212 | int *session_objd, | |
2213 | int *channel_objd, | |
2214 | char *event_name, | |
2215 | int *loglevel, | |
2216 | char **signature, | |
2217 | size_t *nr_fields, | |
2218 | struct ustctl_field **fields, | |
2219 | char **model_emf_uri) | |
2220 | { | |
2221 | ssize_t len; | |
2222 | struct ustcomm_notify_event_msg msg; | |
2223 | size_t signature_len, fields_len, model_emf_uri_len; | |
2224 | char *a_sign = NULL, *a_model_emf_uri = NULL; | |
2225 | struct ustctl_field *a_fields = NULL; | |
2226 | ||
2227 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
2228 | if (len > 0 && len != sizeof(msg)) | |
2229 | return -EIO; | |
2230 | if (len == 0) | |
2231 | return -EPIPE; | |
2232 | if (len < 0) | |
2233 | return len; | |
2234 | ||
2235 | *session_objd = msg.session_objd; | |
2236 | *channel_objd = msg.channel_objd; | |
2237 | strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN); | |
2238 | event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
2239 | *loglevel = msg.loglevel; | |
2240 | signature_len = msg.signature_len; | |
2241 | fields_len = msg.fields_len; | |
2242 | ||
2243 | if (fields_len % sizeof(*a_fields) != 0) { | |
2244 | return -EINVAL; | |
2245 | } | |
2246 | ||
2247 | model_emf_uri_len = msg.model_emf_uri_len; | |
2248 | ||
2249 | /* recv signature. contains at least \0. */ | |
2250 | a_sign = zmalloc(signature_len); | |
2251 | if (!a_sign) | |
2252 | return -ENOMEM; | |
2253 | len = ustcomm_recv_unix_sock(sock, a_sign, signature_len); | |
2254 | if (len > 0 && len != signature_len) { | |
2255 | len = -EIO; | |
2256 | goto signature_error; | |
2257 | } | |
2258 | if (len == 0) { | |
2259 | len = -EPIPE; | |
2260 | goto signature_error; | |
2261 | } | |
2262 | if (len < 0) { | |
2263 | goto signature_error; | |
2264 | } | |
2265 | /* Enforce end of string */ | |
111198c2 | 2266 | a_sign[signature_len - 1] = '\0'; |
32ce8569 MD |
2267 | |
2268 | /* recv fields */ | |
2269 | if (fields_len) { | |
2270 | a_fields = zmalloc(fields_len); | |
2271 | if (!a_fields) { | |
2272 | len = -ENOMEM; | |
2273 | goto signature_error; | |
2274 | } | |
2275 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); | |
2276 | if (len > 0 && len != fields_len) { | |
2277 | len = -EIO; | |
2278 | goto fields_error; | |
2279 | } | |
2280 | if (len == 0) { | |
2281 | len = -EPIPE; | |
2282 | goto fields_error; | |
2283 | } | |
2284 | if (len < 0) { | |
2285 | goto fields_error; | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | if (model_emf_uri_len) { | |
2290 | /* recv model_emf_uri_len */ | |
2291 | a_model_emf_uri = zmalloc(model_emf_uri_len); | |
2292 | if (!a_model_emf_uri) { | |
2293 | len = -ENOMEM; | |
2294 | goto fields_error; | |
2295 | } | |
2296 | len = ustcomm_recv_unix_sock(sock, a_model_emf_uri, | |
2297 | model_emf_uri_len); | |
2298 | if (len > 0 && len != model_emf_uri_len) { | |
2299 | len = -EIO; | |
2300 | goto model_error; | |
2301 | } | |
2302 | if (len == 0) { | |
2303 | len = -EPIPE; | |
2304 | goto model_error; | |
2305 | } | |
2306 | if (len < 0) { | |
2307 | goto model_error; | |
2308 | } | |
2309 | /* Enforce end of string */ | |
2310 | a_model_emf_uri[model_emf_uri_len - 1] = '\0'; | |
2311 | } | |
2312 | ||
2313 | *signature = a_sign; | |
2314 | *nr_fields = fields_len / sizeof(*a_fields); | |
2315 | *fields = a_fields; | |
2316 | *model_emf_uri = a_model_emf_uri; | |
2317 | ||
2318 | return 0; | |
2319 | ||
2320 | model_error: | |
2321 | free(a_model_emf_uri); | |
2322 | fields_error: | |
2323 | free(a_fields); | |
2324 | signature_error: | |
2325 | free(a_sign); | |
2326 | return len; | |
2327 | } | |
2328 | ||
2329 | /* | |
2330 | * Returns 0 on success, negative error value on error. | |
2331 | */ | |
2332 | int ustctl_reply_register_event(int sock, | |
2333 | uint32_t id, | |
2334 | int ret_code) | |
2335 | { | |
2336 | ssize_t len; | |
2337 | struct { | |
2338 | struct ustcomm_notify_hdr header; | |
2339 | struct ustcomm_notify_event_reply r; | |
2340 | } reply; | |
2341 | ||
2342 | memset(&reply, 0, sizeof(reply)); | |
2343 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
2344 | reply.r.ret_code = ret_code; | |
2345 | reply.r.event_id = id; | |
2346 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
2347 | if (len > 0 && len != sizeof(reply)) | |
2348 | return -EIO; | |
2349 | if (len < 0) | |
2350 | return len; | |
2351 | return 0; | |
2352 | } | |
2353 | ||
c785c634 MD |
2354 | /* |
2355 | * Returns 0 on success, negative UST or system error value on error. | |
2356 | */ | |
2357 | int ustctl_recv_register_enum(int sock, | |
2358 | int *session_objd, | |
2359 | char *enum_name, | |
2360 | struct ustctl_enum_entry **entries, | |
2361 | size_t *nr_entries) | |
2362 | { | |
2363 | ssize_t len; | |
2364 | struct ustcomm_notify_enum_msg msg; | |
2365 | size_t entries_len; | |
2366 | struct ustctl_enum_entry *a_entries = NULL; | |
2367 | ||
2368 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
2369 | if (len > 0 && len != sizeof(msg)) | |
2370 | return -EIO; | |
2371 | if (len == 0) | |
2372 | return -EPIPE; | |
2373 | if (len < 0) | |
2374 | return len; | |
2375 | ||
2376 | *session_objd = msg.session_objd; | |
2377 | strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN); | |
2378 | enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
2379 | entries_len = msg.entries_len; | |
2380 | ||
2381 | if (entries_len % sizeof(*a_entries) != 0) { | |
2382 | return -EINVAL; | |
2383 | } | |
2384 | ||
2385 | /* recv entries */ | |
2386 | if (entries_len) { | |
2387 | a_entries = zmalloc(entries_len); | |
2388 | if (!a_entries) | |
2389 | return -ENOMEM; | |
2390 | len = ustcomm_recv_unix_sock(sock, a_entries, entries_len); | |
2391 | if (len > 0 && len != entries_len) { | |
2392 | len = -EIO; | |
2393 | goto entries_error; | |
2394 | } | |
2395 | if (len == 0) { | |
2396 | len = -EPIPE; | |
2397 | goto entries_error; | |
2398 | } | |
2399 | if (len < 0) { | |
2400 | goto entries_error; | |
2401 | } | |
2402 | } | |
2403 | *nr_entries = entries_len / sizeof(*a_entries); | |
2404 | *entries = a_entries; | |
2405 | ||
2406 | return 0; | |
2407 | ||
2408 | entries_error: | |
2409 | free(a_entries); | |
2410 | return len; | |
2411 | } | |
2412 | ||
2413 | /* | |
2414 | * Returns 0 on success, negative error value on error. | |
2415 | */ | |
2416 | int ustctl_reply_register_enum(int sock, | |
2417 | uint64_t id, | |
2418 | int ret_code) | |
2419 | { | |
2420 | ssize_t len; | |
2421 | struct { | |
2422 | struct ustcomm_notify_hdr header; | |
2423 | struct ustcomm_notify_enum_reply r; | |
2424 | } reply; | |
2425 | ||
2426 | memset(&reply, 0, sizeof(reply)); | |
2427 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM; | |
2428 | reply.r.ret_code = ret_code; | |
2429 | reply.r.enum_id = id; | |
2430 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
2431 | if (len > 0 && len != sizeof(reply)) | |
2432 | return -EIO; | |
2433 | if (len < 0) | |
2434 | return len; | |
2435 | return 0; | |
2436 | } | |
2437 | ||
32ce8569 MD |
2438 | /* |
2439 | * Returns 0 on success, negative UST or system error value on error. | |
2440 | */ | |
2441 | int ustctl_recv_register_channel(int sock, | |
2442 | int *session_objd, /* session descriptor (output) */ | |
2443 | int *channel_objd, /* channel descriptor (output) */ | |
2444 | size_t *nr_fields, | |
2445 | struct ustctl_field **fields) | |
2446 | { | |
2447 | ssize_t len; | |
2448 | struct ustcomm_notify_channel_msg msg; | |
2449 | size_t fields_len; | |
2450 | struct ustctl_field *a_fields; | |
2451 | ||
2452 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
2453 | if (len > 0 && len != sizeof(msg)) | |
2454 | return -EIO; | |
2455 | if (len == 0) | |
2456 | return -EPIPE; | |
2457 | if (len < 0) | |
2458 | return len; | |
2459 | ||
2460 | *session_objd = msg.session_objd; | |
2461 | *channel_objd = msg.channel_objd; | |
2462 | fields_len = msg.ctx_fields_len; | |
2463 | ||
2464 | if (fields_len % sizeof(*a_fields) != 0) { | |
2465 | return -EINVAL; | |
2466 | } | |
2467 | ||
2468 | /* recv fields */ | |
2469 | if (fields_len) { | |
2470 | a_fields = zmalloc(fields_len); | |
2471 | if (!a_fields) { | |
2472 | len = -ENOMEM; | |
2473 | goto alloc_error; | |
2474 | } | |
2475 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); | |
2476 | if (len > 0 && len != fields_len) { | |
2477 | len = -EIO; | |
2478 | goto fields_error; | |
2479 | } | |
2480 | if (len == 0) { | |
2481 | len = -EPIPE; | |
2482 | goto fields_error; | |
2483 | } | |
2484 | if (len < 0) { | |
2485 | goto fields_error; | |
2486 | } | |
2487 | *fields = a_fields; | |
2488 | } else { | |
2489 | *fields = NULL; | |
2490 | } | |
2491 | *nr_fields = fields_len / sizeof(*a_fields); | |
2492 | return 0; | |
2493 | ||
2494 | fields_error: | |
2495 | free(a_fields); | |
2496 | alloc_error: | |
2497 | return len; | |
2498 | } | |
2499 | ||
2500 | /* | |
2501 | * Returns 0 on success, negative error value on error. | |
2502 | */ | |
2503 | int ustctl_reply_register_channel(int sock, | |
2504 | uint32_t chan_id, | |
2505 | enum ustctl_channel_header header_type, | |
2506 | int ret_code) | |
2507 | { | |
2508 | ssize_t len; | |
2509 | struct { | |
2510 | struct ustcomm_notify_hdr header; | |
2511 | struct ustcomm_notify_channel_reply r; | |
2512 | } reply; | |
2513 | ||
2514 | memset(&reply, 0, sizeof(reply)); | |
2515 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
2516 | reply.r.ret_code = ret_code; | |
2517 | reply.r.chan_id = chan_id; | |
2518 | switch (header_type) { | |
2519 | case USTCTL_CHANNEL_HEADER_COMPACT: | |
2520 | reply.r.header_type = 1; | |
2521 | break; | |
2522 | case USTCTL_CHANNEL_HEADER_LARGE: | |
2523 | reply.r.header_type = 2; | |
2524 | break; | |
2525 | default: | |
2526 | reply.r.header_type = 0; | |
2527 | break; | |
2528 | } | |
2529 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
2530 | if (len > 0 && len != sizeof(reply)) | |
2531 | return -EIO; | |
2532 | if (len < 0) | |
2533 | return len; | |
2534 | return 0; | |
2535 | } | |
2536 | ||
f53329f3 JD |
2537 | /* Regenerate the statedump. */ |
2538 | int ustctl_regenerate_statedump(int sock, int handle) | |
2539 | { | |
2540 | struct ustcomm_ust_msg lum; | |
2541 | struct ustcomm_ust_reply lur; | |
2542 | int ret; | |
2543 | ||
2544 | memset(&lum, 0, sizeof(lum)); | |
2545 | lum.handle = handle; | |
2546 | lum.cmd = LTTNG_UST_SESSION_STATEDUMP; | |
2547 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
2548 | if (ret) | |
2549 | return ret; | |
2550 | DBG("Regenerated statedump for handle %u", handle); | |
2551 | return 0; | |
2552 | } | |
2553 | ||
ebabbf58 MD |
2554 | /* counter operations */ |
2555 | ||
2556 | int ustctl_get_nr_cpu_per_counter(void) | |
2557 | { | |
2558 | return lttng_counter_num_possible_cpus(); | |
2559 | } | |
2560 | ||
2561 | struct ustctl_daemon_counter * | |
2562 | ustctl_create_counter(size_t nr_dimensions, | |
2563 | const struct ustctl_counter_dimension *dimensions, | |
2564 | int64_t global_sum_step, | |
2565 | int global_counter_fd, | |
2566 | int nr_counter_cpu_fds, | |
2567 | const int *counter_cpu_fds, | |
2568 | enum ustctl_counter_bitness bitness, | |
2569 | enum ustctl_counter_arithmetic arithmetic, | |
2570 | uint32_t alloc_flags) | |
2571 | { | |
2572 | const char *transport_name; | |
2573 | struct ustctl_daemon_counter *counter; | |
2574 | struct lttng_counter_transport *transport; | |
2575 | struct lttng_counter_dimension ust_dim[LTTNG_COUNTER_DIMENSION_MAX]; | |
2576 | size_t i; | |
2577 | ||
2578 | if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX) | |
2579 | return NULL; | |
2580 | /* Currently, only per-cpu allocation is supported. */ | |
2581 | switch (alloc_flags) { | |
2582 | case USTCTL_COUNTER_ALLOC_PER_CPU: | |
2583 | break; | |
2584 | ||
2585 | case USTCTL_COUNTER_ALLOC_PER_CPU | USTCTL_COUNTER_ALLOC_GLOBAL: | |
2586 | case USTCTL_COUNTER_ALLOC_GLOBAL: | |
2587 | default: | |
2588 | return NULL; | |
2589 | } | |
2590 | switch (bitness) { | |
2591 | case USTCTL_COUNTER_BITNESS_32: | |
2592 | switch (arithmetic) { | |
2593 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: | |
2594 | transport_name = "counter-per-cpu-32-modular"; | |
2595 | break; | |
2596 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: | |
2597 | transport_name = "counter-per-cpu-32-saturation"; | |
2598 | break; | |
2599 | default: | |
2600 | return NULL; | |
2601 | } | |
2602 | break; | |
2603 | case USTCTL_COUNTER_BITNESS_64: | |
2604 | switch (arithmetic) { | |
2605 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: | |
2606 | transport_name = "counter-per-cpu-64-modular"; | |
2607 | break; | |
2608 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: | |
2609 | transport_name = "counter-per-cpu-64-saturation"; | |
2610 | break; | |
2611 | default: | |
2612 | return NULL; | |
2613 | } | |
2614 | break; | |
2615 | default: | |
2616 | return NULL; | |
2617 | } | |
2618 | ||
2619 | transport = lttng_counter_transport_find(transport_name); | |
2620 | if (!transport) { | |
2621 | DBG("LTTng transport %s not found\n", | |
2622 | transport_name); | |
2623 | return NULL; | |
2624 | } | |
2625 | ||
2626 | counter = zmalloc(sizeof(*counter)); | |
2627 | if (!counter) | |
2628 | return NULL; | |
2629 | counter->attr = zmalloc(sizeof(*counter->attr)); | |
2630 | if (!counter->attr) | |
2631 | goto free_counter; | |
2632 | counter->attr->bitness = bitness; | |
2633 | counter->attr->arithmetic = arithmetic; | |
2634 | counter->attr->nr_dimensions = nr_dimensions; | |
2635 | counter->attr->global_sum_step = global_sum_step; | |
2636 | for (i = 0; i < nr_dimensions; i++) | |
2637 | counter->attr->dimensions[i] = dimensions[i]; | |
2638 | ||
2639 | for (i = 0; i < nr_dimensions; i++) { | |
2640 | ust_dim[i].size = dimensions[i].size; | |
2641 | ust_dim[i].underflow_index = dimensions[i].underflow_index; | |
2642 | ust_dim[i].overflow_index = dimensions[i].overflow_index; | |
2643 | ust_dim[i].has_underflow = dimensions[i].has_underflow; | |
2644 | ust_dim[i].has_overflow = dimensions[i].has_overflow; | |
2645 | } | |
2646 | counter->counter = transport->ops.counter_create(nr_dimensions, | |
2647 | ust_dim, global_sum_step, global_counter_fd, | |
2648 | nr_counter_cpu_fds, counter_cpu_fds, true); | |
2649 | if (!counter->counter) | |
2650 | goto free_attr; | |
2651 | counter->ops = &transport->ops; | |
2652 | return counter; | |
2653 | ||
2654 | free_attr: | |
2655 | free(counter->attr); | |
2656 | free_counter: | |
2657 | free(counter); | |
2658 | return NULL; | |
2659 | } | |
2660 | ||
2661 | int ustctl_create_counter_data(struct ustctl_daemon_counter *counter, | |
2662 | struct lttng_ust_object_data **_counter_data) | |
2663 | { | |
2664 | struct lttng_ust_object_data *counter_data; | |
dca92811 | 2665 | struct lttng_ust_counter_conf counter_conf = {0}; |
ebabbf58 MD |
2666 | size_t i; |
2667 | int ret; | |
2668 | ||
2669 | switch (counter->attr->arithmetic) { | |
2670 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: | |
2671 | counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_MODULAR; | |
2672 | break; | |
2673 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: | |
2674 | counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_SATURATION; | |
2675 | break; | |
2676 | default: | |
2677 | return -EINVAL; | |
2678 | } | |
2679 | switch (counter->attr->bitness) { | |
2680 | case USTCTL_COUNTER_BITNESS_32: | |
38980ed1 | 2681 | counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_32; |
ebabbf58 MD |
2682 | break; |
2683 | case USTCTL_COUNTER_BITNESS_64: | |
38980ed1 | 2684 | counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_64; |
ebabbf58 MD |
2685 | break; |
2686 | default: | |
2687 | return -EINVAL; | |
2688 | } | |
2689 | counter_conf.number_dimensions = counter->attr->nr_dimensions; | |
2690 | counter_conf.global_sum_step = counter->attr->global_sum_step; | |
2691 | for (i = 0; i < counter->attr->nr_dimensions; i++) { | |
2692 | counter_conf.dimensions[i].size = counter->attr->dimensions[i].size; | |
2693 | counter_conf.dimensions[i].underflow_index = counter->attr->dimensions[i].underflow_index; | |
2694 | counter_conf.dimensions[i].overflow_index = counter->attr->dimensions[i].overflow_index; | |
2695 | counter_conf.dimensions[i].has_underflow = counter->attr->dimensions[i].has_underflow; | |
2696 | counter_conf.dimensions[i].has_overflow = counter->attr->dimensions[i].has_overflow; | |
2697 | } | |
2698 | ||
2699 | counter_data = zmalloc(sizeof(*counter_data)); | |
2700 | if (!counter_data) { | |
2701 | ret = -ENOMEM; | |
2702 | goto error_alloc; | |
2703 | } | |
2704 | counter_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER; | |
2705 | counter_data->handle = -1; | |
2706 | ||
2707 | counter_data->size = sizeof(counter_conf); | |
2708 | counter_data->u.counter.data = zmalloc(sizeof(counter_conf)); | |
2709 | if (!counter_data->u.counter.data) { | |
2710 | ret = -ENOMEM; | |
2711 | goto error_alloc_data; | |
2712 | } | |
2713 | ||
2714 | memcpy(counter_data->u.counter.data, &counter_conf, sizeof(counter_conf)); | |
2715 | *_counter_data = counter_data; | |
2716 | ||
2717 | return 0; | |
2718 | ||
2719 | error_alloc_data: | |
2720 | free(counter_data); | |
2721 | error_alloc: | |
2722 | return ret; | |
2723 | } | |
2724 | ||
2725 | int ustctl_create_counter_global_data(struct ustctl_daemon_counter *counter, | |
2726 | struct lttng_ust_object_data **_counter_global_data) | |
2727 | { | |
2728 | struct lttng_ust_object_data *counter_global_data; | |
2729 | int ret, fd; | |
2730 | size_t len; | |
2731 | ||
2732 | if (lttng_counter_get_global_shm(counter->counter, &fd, &len)) | |
2733 | return -EINVAL; | |
2734 | counter_global_data = zmalloc(sizeof(*counter_global_data)); | |
2735 | if (!counter_global_data) { | |
2736 | ret = -ENOMEM; | |
2737 | goto error_alloc; | |
2738 | } | |
2739 | counter_global_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL; | |
2740 | counter_global_data->handle = -1; | |
2741 | counter_global_data->size = len; | |
2742 | counter_global_data->u.counter_global.shm_fd = fd; | |
2743 | *_counter_global_data = counter_global_data; | |
2744 | return 0; | |
2745 | ||
2746 | error_alloc: | |
2747 | return ret; | |
2748 | } | |
2749 | ||
2750 | int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter *counter, int cpu, | |
2751 | struct lttng_ust_object_data **_counter_cpu_data) | |
2752 | { | |
2753 | struct lttng_ust_object_data *counter_cpu_data; | |
2754 | int ret, fd; | |
2755 | size_t len; | |
2756 | ||
2757 | if (lttng_counter_get_cpu_shm(counter->counter, cpu, &fd, &len)) | |
2758 | return -EINVAL; | |
2759 | counter_cpu_data = zmalloc(sizeof(*counter_cpu_data)); | |
2760 | if (!counter_cpu_data) { | |
2761 | ret = -ENOMEM; | |
2762 | goto error_alloc; | |
2763 | } | |
2764 | counter_cpu_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_CPU; | |
2765 | counter_cpu_data->handle = -1; | |
2766 | counter_cpu_data->size = len; | |
2767 | counter_cpu_data->u.counter_cpu.shm_fd = fd; | |
2768 | counter_cpu_data->u.counter_cpu.cpu_nr = cpu; | |
2769 | *_counter_cpu_data = counter_cpu_data; | |
2770 | return 0; | |
2771 | ||
2772 | error_alloc: | |
2773 | return ret; | |
2774 | } | |
2775 | ||
2776 | void ustctl_destroy_counter(struct ustctl_daemon_counter *counter) | |
2777 | { | |
2778 | counter->ops->counter_destroy(counter->counter); | |
2779 | free(counter->attr); | |
2780 | free(counter); | |
2781 | } | |
2782 | ||
2783 | int ustctl_send_counter_data_to_ust(int sock, int parent_handle, | |
2784 | struct lttng_ust_object_data *counter_data) | |
2785 | { | |
2786 | struct ustcomm_ust_msg lum; | |
2787 | struct ustcomm_ust_reply lur; | |
2788 | int ret; | |
2789 | size_t size; | |
2790 | ssize_t len; | |
2791 | ||
2792 | if (!counter_data) | |
2793 | return -EINVAL; | |
2794 | ||
2795 | size = counter_data->size; | |
2796 | memset(&lum, 0, sizeof(lum)); | |
2797 | lum.handle = parent_handle; | |
2798 | lum.cmd = LTTNG_UST_COUNTER; | |
2799 | lum.u.counter.len = size; | |
2800 | ret = ustcomm_send_app_msg(sock, &lum); | |
2801 | if (ret) | |
2802 | return ret; | |
2803 | ||
2804 | /* Send counter data */ | |
2805 | len = ustcomm_send_unix_sock(sock, counter_data->u.counter.data, size); | |
2806 | if (len != size) { | |
2807 | if (len < 0) | |
2808 | return len; | |
2809 | else | |
2810 | return -EIO; | |
2811 | } | |
2812 | ||
2813 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
2814 | if (!ret) { | |
2815 | counter_data->handle = lur.ret_val; | |
2816 | } | |
2817 | return ret; | |
2818 | } | |
2819 | ||
2820 | int ustctl_send_counter_global_data_to_ust(int sock, | |
2821 | struct lttng_ust_object_data *counter_data, | |
2822 | struct lttng_ust_object_data *counter_global_data) | |
2823 | { | |
2824 | struct ustcomm_ust_msg lum; | |
2825 | struct ustcomm_ust_reply lur; | |
2826 | int ret, shm_fd[1]; | |
2827 | size_t size; | |
2828 | ssize_t len; | |
2829 | ||
2830 | if (!counter_data || !counter_global_data) | |
2831 | return -EINVAL; | |
2832 | ||
2833 | size = counter_global_data->size; | |
2834 | memset(&lum, 0, sizeof(lum)); | |
2835 | lum.handle = counter_data->handle; /* parent handle */ | |
2836 | lum.cmd = LTTNG_UST_COUNTER_GLOBAL; | |
2837 | lum.u.counter_global.len = size; | |
2838 | ret = ustcomm_send_app_msg(sock, &lum); | |
2839 | if (ret) | |
2840 | return ret; | |
2841 | ||
2842 | shm_fd[0] = counter_global_data->u.counter_global.shm_fd; | |
2843 | len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1); | |
2844 | if (len <= 0) { | |
2845 | if (len < 0) | |
2846 | return len; | |
2847 | else | |
2848 | return -EIO; | |
2849 | } | |
2850 | ||
2851 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
2852 | if (!ret) { | |
2853 | counter_global_data->handle = lur.ret_val; | |
2854 | } | |
2855 | return ret; | |
2856 | } | |
2857 | ||
2858 | int ustctl_send_counter_cpu_data_to_ust(int sock, | |
2859 | struct lttng_ust_object_data *counter_data, | |
2860 | struct lttng_ust_object_data *counter_cpu_data) | |
2861 | { | |
2862 | struct ustcomm_ust_msg lum; | |
2863 | struct ustcomm_ust_reply lur; | |
2864 | int ret, shm_fd[1]; | |
2865 | size_t size; | |
2866 | ssize_t len; | |
2867 | ||
2868 | if (!counter_data || !counter_cpu_data) | |
2869 | return -EINVAL; | |
2870 | ||
2871 | size = counter_cpu_data->size; | |
2872 | memset(&lum, 0, sizeof(lum)); | |
2873 | lum.handle = counter_data->handle; /* parent handle */ | |
2874 | lum.cmd = LTTNG_UST_COUNTER_CPU; | |
2875 | lum.u.counter_cpu.len = size; | |
2876 | lum.u.counter_cpu.cpu_nr = counter_cpu_data->u.counter_cpu.cpu_nr; | |
2877 | ret = ustcomm_send_app_msg(sock, &lum); | |
2878 | if (ret) | |
2879 | return ret; | |
2880 | ||
2881 | shm_fd[0] = counter_cpu_data->u.counter_global.shm_fd; | |
2882 | len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1); | |
2883 | if (len <= 0) { | |
2884 | if (len < 0) | |
2885 | return len; | |
2886 | else | |
2887 | return -EIO; | |
2888 | } | |
2889 | ||
2890 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); | |
2891 | if (!ret) { | |
2892 | counter_cpu_data->handle = lur.ret_val; | |
2893 | } | |
2894 | return ret; | |
2895 | } | |
2896 | ||
2897 | int ustctl_counter_read(struct ustctl_daemon_counter *counter, | |
2898 | const size_t *dimension_indexes, | |
2899 | int cpu, int64_t *value, | |
2900 | bool *overflow, bool *underflow) | |
2901 | { | |
2902 | return counter->ops->counter_read(counter->counter, dimension_indexes, cpu, | |
2903 | value, overflow, underflow); | |
2904 | } | |
2905 | ||
2906 | int ustctl_counter_aggregate(struct ustctl_daemon_counter *counter, | |
2907 | const size_t *dimension_indexes, | |
2908 | int64_t *value, | |
2909 | bool *overflow, bool *underflow) | |
2910 | { | |
2911 | return counter->ops->counter_aggregate(counter->counter, dimension_indexes, | |
2912 | value, overflow, underflow); | |
2913 | } | |
2914 | ||
2915 | int ustctl_counter_clear(struct ustctl_daemon_counter *counter, | |
2916 | const size_t *dimension_indexes) | |
2917 | { | |
2918 | return counter->ops->counter_clear(counter->counter, dimension_indexes); | |
2919 | } | |
2920 | ||
74d81a6c MD |
2921 | static __attribute__((constructor)) |
2922 | void ustctl_init(void) | |
2923 | { | |
2b6f4374 MJ |
2924 | ust_err_init(); |
2925 | lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */ | |
f9364363 | 2926 | lttng_ust_clock_init(); |
74d81a6c MD |
2927 | lttng_ring_buffer_metadata_client_init(); |
2928 | lttng_ring_buffer_client_overwrite_init(); | |
34a91bdb | 2929 | lttng_ring_buffer_client_overwrite_rt_init(); |
74d81a6c | 2930 | lttng_ring_buffer_client_discard_init(); |
34a91bdb | 2931 | lttng_ring_buffer_client_discard_rt_init(); |
ebabbf58 MD |
2932 | lttng_counter_client_percpu_32_modular_init(); |
2933 | lttng_counter_client_percpu_64_modular_init(); | |
03d2d293 | 2934 | lib_ringbuffer_signal_init(); |
74d81a6c MD |
2935 | } |
2936 | ||
2937 | static __attribute__((destructor)) | |
2938 | void ustctl_exit(void) | |
2939 | { | |
34a91bdb | 2940 | lttng_ring_buffer_client_discard_rt_exit(); |
74d81a6c | 2941 | lttng_ring_buffer_client_discard_exit(); |
34a91bdb | 2942 | lttng_ring_buffer_client_overwrite_rt_exit(); |
74d81a6c MD |
2943 | lttng_ring_buffer_client_overwrite_exit(); |
2944 | lttng_ring_buffer_metadata_client_exit(); | |
ebabbf58 MD |
2945 | lttng_counter_client_percpu_32_modular_exit(); |
2946 | lttng_counter_client_percpu_64_modular_exit(); | |
57773204 | 2947 | } |