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