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