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