| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License only. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <string.h> |
| 21 | #include <lttng/ust-ctl.h> |
| 22 | #include <lttng/ust-abi.h> |
| 23 | #include <lttng/ust-events.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <byteswap.h> |
| 26 | |
| 27 | #include <usterr-signal-safe.h> |
| 28 | #include <ust-comm.h> |
| 29 | #include <helper.h> |
| 30 | |
| 31 | #include "../libringbuffer/backend.h" |
| 32 | #include "../libringbuffer/frontend.h" |
| 33 | #include "../liblttng-ust/wait.h" |
| 34 | |
| 35 | /* |
| 36 | * Number of milliseconds to retry before failing metadata writes on |
| 37 | * buffer full condition. (10 seconds) |
| 38 | */ |
| 39 | #define LTTNG_METADATA_TIMEOUT_MSEC 10000 |
| 40 | |
| 41 | /* |
| 42 | * Channel representation within consumer. |
| 43 | */ |
| 44 | struct ustctl_consumer_channel { |
| 45 | struct lttng_channel *chan; /* lttng channel buffers */ |
| 46 | |
| 47 | /* initial attributes */ |
| 48 | struct ustctl_consumer_channel_attr attr; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Stream representation within consumer. |
| 53 | */ |
| 54 | struct ustctl_consumer_stream { |
| 55 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
| 56 | struct lttng_ust_lib_ring_buffer *buf; |
| 57 | struct ustctl_consumer_channel *chan; |
| 58 | int shm_fd, wait_fd, wakeup_fd; |
| 59 | int cpu; |
| 60 | uint64_t memory_map_size; |
| 61 | }; |
| 62 | |
| 63 | extern void lttng_ring_buffer_client_overwrite_init(void); |
| 64 | extern void lttng_ring_buffer_client_discard_init(void); |
| 65 | extern void lttng_ring_buffer_metadata_client_init(void); |
| 66 | extern void lttng_ring_buffer_client_overwrite_exit(void); |
| 67 | extern void lttng_ring_buffer_client_discard_exit(void); |
| 68 | extern void lttng_ring_buffer_metadata_client_exit(void); |
| 69 | |
| 70 | volatile enum ust_loglevel ust_loglevel; |
| 71 | |
| 72 | int ustctl_release_handle(int sock, int handle) |
| 73 | { |
| 74 | struct ustcomm_ust_msg lum; |
| 75 | struct ustcomm_ust_reply lur; |
| 76 | |
| 77 | if (sock < 0 || handle < 0) |
| 78 | return 0; |
| 79 | memset(&lum, 0, sizeof(lum)); |
| 80 | lum.handle = handle; |
| 81 | lum.cmd = LTTNG_UST_RELEASE; |
| 82 | return ustcomm_send_app_cmd(sock, &lum, &lur); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * If sock is negative, it means we don't have to notify the other side |
| 87 | * (e.g. application has already vanished). |
| 88 | */ |
| 89 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
| 90 | { |
| 91 | int ret; |
| 92 | |
| 93 | if (!data) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | switch (data->type) { |
| 97 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: |
| 98 | free(data->u.channel.data); |
| 99 | break; |
| 100 | case LTTNG_UST_OBJECT_TYPE_STREAM: |
| 101 | if (data->u.stream.shm_fd >= 0) { |
| 102 | ret = close(data->u.stream.shm_fd); |
| 103 | if (ret < 0) { |
| 104 | ret = -errno; |
| 105 | return ret; |
| 106 | } |
| 107 | } |
| 108 | if (data->u.stream.wakeup_fd >= 0) { |
| 109 | ret = close(data->u.stream.wakeup_fd); |
| 110 | if (ret < 0) { |
| 111 | ret = -errno; |
| 112 | return ret; |
| 113 | } |
| 114 | } |
| 115 | break; |
| 116 | case LTTNG_UST_OBJECT_TYPE_EVENT: |
| 117 | case LTTNG_UST_OBJECT_TYPE_CONTEXT: |
| 118 | break; |
| 119 | default: |
| 120 | assert(0); |
| 121 | } |
| 122 | return ustctl_release_handle(sock, data->handle); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Send registration done packet to the application. |
| 127 | */ |
| 128 | int ustctl_register_done(int sock) |
| 129 | { |
| 130 | struct ustcomm_ust_msg lum; |
| 131 | struct ustcomm_ust_reply lur; |
| 132 | int ret; |
| 133 | |
| 134 | DBG("Sending register done command to %d", sock); |
| 135 | memset(&lum, 0, sizeof(lum)); |
| 136 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 137 | lum.cmd = LTTNG_UST_REGISTER_DONE; |
| 138 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * returns session handle. |
| 146 | */ |
| 147 | int ustctl_create_session(int sock) |
| 148 | { |
| 149 | struct ustcomm_ust_msg lum; |
| 150 | struct ustcomm_ust_reply lur; |
| 151 | int ret, session_handle; |
| 152 | |
| 153 | /* Create session */ |
| 154 | memset(&lum, 0, sizeof(lum)); |
| 155 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 156 | lum.cmd = LTTNG_UST_SESSION; |
| 157 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | session_handle = lur.ret_val; |
| 161 | DBG("received session handle %u", session_handle); |
| 162 | return session_handle; |
| 163 | } |
| 164 | |
| 165 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
| 166 | struct lttng_ust_object_data *channel_data, |
| 167 | struct lttng_ust_object_data **_event_data) |
| 168 | { |
| 169 | struct ustcomm_ust_msg lum; |
| 170 | struct ustcomm_ust_reply lur; |
| 171 | struct lttng_ust_object_data *event_data; |
| 172 | int ret; |
| 173 | |
| 174 | if (!channel_data || !_event_data) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | event_data = zmalloc(sizeof(*event_data)); |
| 178 | if (!event_data) |
| 179 | return -ENOMEM; |
| 180 | event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT; |
| 181 | memset(&lum, 0, sizeof(lum)); |
| 182 | lum.handle = channel_data->handle; |
| 183 | lum.cmd = LTTNG_UST_EVENT; |
| 184 | strncpy(lum.u.event.name, ev->name, |
| 185 | LTTNG_UST_SYM_NAME_LEN); |
| 186 | lum.u.event.instrumentation = ev->instrumentation; |
| 187 | lum.u.event.loglevel_type = ev->loglevel_type; |
| 188 | lum.u.event.loglevel = ev->loglevel; |
| 189 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 190 | if (ret) { |
| 191 | free(event_data); |
| 192 | return ret; |
| 193 | } |
| 194 | event_data->handle = lur.ret_val; |
| 195 | DBG("received event handle %u", event_data->handle); |
| 196 | *_event_data = event_data; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int ustctl_add_context(int sock, struct lttng_ust_context *ctx, |
| 201 | struct lttng_ust_object_data *obj_data, |
| 202 | struct lttng_ust_object_data **_context_data) |
| 203 | { |
| 204 | struct ustcomm_ust_msg lum; |
| 205 | struct ustcomm_ust_reply lur; |
| 206 | struct lttng_ust_object_data *context_data; |
| 207 | int ret; |
| 208 | |
| 209 | if (!obj_data || !_context_data) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | context_data = zmalloc(sizeof(*context_data)); |
| 213 | if (!context_data) |
| 214 | return -ENOMEM; |
| 215 | context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT; |
| 216 | memset(&lum, 0, sizeof(lum)); |
| 217 | lum.handle = obj_data->handle; |
| 218 | lum.cmd = LTTNG_UST_CONTEXT; |
| 219 | lum.u.context.ctx = ctx->ctx; |
| 220 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 221 | if (ret) { |
| 222 | free(context_data); |
| 223 | return ret; |
| 224 | } |
| 225 | context_data->handle = -1; |
| 226 | DBG("Context created successfully"); |
| 227 | *_context_data = context_data; |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode, |
| 232 | struct lttng_ust_object_data *obj_data) |
| 233 | { |
| 234 | struct ustcomm_ust_msg lum; |
| 235 | struct ustcomm_ust_reply lur; |
| 236 | int ret; |
| 237 | |
| 238 | if (!obj_data) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | memset(&lum, 0, sizeof(lum)); |
| 242 | lum.handle = obj_data->handle; |
| 243 | lum.cmd = LTTNG_UST_FILTER; |
| 244 | lum.u.filter.data_size = bytecode->len; |
| 245 | lum.u.filter.reloc_offset = bytecode->reloc_offset; |
| 246 | lum.u.filter.seqnum = bytecode->seqnum; |
| 247 | |
| 248 | ret = ustcomm_send_app_msg(sock, &lum); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | /* send var len bytecode */ |
| 252 | ret = ustcomm_send_unix_sock(sock, bytecode->data, |
| 253 | bytecode->len); |
| 254 | if (ret < 0) { |
| 255 | return ret; |
| 256 | } |
| 257 | if (ret != bytecode->len) |
| 258 | return -EINVAL; |
| 259 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 260 | } |
| 261 | |
| 262 | /* Enable event, channel and session ioctl */ |
| 263 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
| 264 | { |
| 265 | struct ustcomm_ust_msg lum; |
| 266 | struct ustcomm_ust_reply lur; |
| 267 | int ret; |
| 268 | |
| 269 | if (!object) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | memset(&lum, 0, sizeof(lum)); |
| 273 | lum.handle = object->handle; |
| 274 | lum.cmd = LTTNG_UST_ENABLE; |
| 275 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | DBG("enabled handle %u", object->handle); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* Disable event, channel and session ioctl */ |
| 283 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
| 284 | { |
| 285 | struct ustcomm_ust_msg lum; |
| 286 | struct ustcomm_ust_reply lur; |
| 287 | int ret; |
| 288 | |
| 289 | if (!object) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | memset(&lum, 0, sizeof(lum)); |
| 293 | lum.handle = object->handle; |
| 294 | lum.cmd = LTTNG_UST_DISABLE; |
| 295 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | DBG("disable handle %u", object->handle); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | int ustctl_start_session(int sock, int handle) |
| 303 | { |
| 304 | struct lttng_ust_object_data obj; |
| 305 | |
| 306 | obj.handle = handle; |
| 307 | return ustctl_enable(sock, &obj); |
| 308 | } |
| 309 | |
| 310 | int ustctl_stop_session(int sock, int handle) |
| 311 | { |
| 312 | struct lttng_ust_object_data obj; |
| 313 | |
| 314 | obj.handle = handle; |
| 315 | return ustctl_disable(sock, &obj); |
| 316 | } |
| 317 | |
| 318 | int ustctl_tracepoint_list(int sock) |
| 319 | { |
| 320 | struct ustcomm_ust_msg lum; |
| 321 | struct ustcomm_ust_reply lur; |
| 322 | int ret, tp_list_handle; |
| 323 | |
| 324 | memset(&lum, 0, sizeof(lum)); |
| 325 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 326 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; |
| 327 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | tp_list_handle = lur.ret_val; |
| 331 | DBG("received tracepoint list handle %u", tp_list_handle); |
| 332 | return tp_list_handle; |
| 333 | } |
| 334 | |
| 335 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, |
| 336 | struct lttng_ust_tracepoint_iter *iter) |
| 337 | { |
| 338 | struct ustcomm_ust_msg lum; |
| 339 | struct ustcomm_ust_reply lur; |
| 340 | int ret; |
| 341 | |
| 342 | if (!iter) |
| 343 | return -EINVAL; |
| 344 | |
| 345 | memset(&lum, 0, sizeof(lum)); |
| 346 | lum.handle = tp_list_handle; |
| 347 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; |
| 348 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | DBG("received tracepoint list entry name %s loglevel %d", |
| 352 | lur.u.tracepoint.name, |
| 353 | lur.u.tracepoint.loglevel); |
| 354 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | int ustctl_tracepoint_field_list(int sock) |
| 359 | { |
| 360 | struct ustcomm_ust_msg lum; |
| 361 | struct ustcomm_ust_reply lur; |
| 362 | int ret, tp_field_list_handle; |
| 363 | |
| 364 | memset(&lum, 0, sizeof(lum)); |
| 365 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 366 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST; |
| 367 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | tp_field_list_handle = lur.ret_val; |
| 371 | DBG("received tracepoint field list handle %u", tp_field_list_handle); |
| 372 | return tp_field_list_handle; |
| 373 | } |
| 374 | |
| 375 | int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle, |
| 376 | struct lttng_ust_field_iter *iter) |
| 377 | { |
| 378 | struct ustcomm_ust_msg lum; |
| 379 | struct ustcomm_ust_reply lur; |
| 380 | int ret; |
| 381 | ssize_t len; |
| 382 | |
| 383 | if (!iter) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | memset(&lum, 0, sizeof(lum)); |
| 387 | lum.handle = tp_field_list_handle; |
| 388 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET; |
| 389 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter)); |
| 393 | if (len != sizeof(*iter)) { |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d", |
| 397 | iter->event_name, |
| 398 | iter->loglevel, |
| 399 | iter->field_name, |
| 400 | iter->type); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
| 405 | { |
| 406 | struct ustcomm_ust_msg lum; |
| 407 | struct ustcomm_ust_reply lur; |
| 408 | int ret; |
| 409 | |
| 410 | if (!v) |
| 411 | return -EINVAL; |
| 412 | |
| 413 | memset(&lum, 0, sizeof(lum)); |
| 414 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 415 | lum.cmd = LTTNG_UST_TRACER_VERSION; |
| 416 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 417 | if (ret) |
| 418 | return ret; |
| 419 | memcpy(v, &lur.u.version, sizeof(*v)); |
| 420 | DBG("received tracer version"); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | int ustctl_wait_quiescent(int sock) |
| 425 | { |
| 426 | struct ustcomm_ust_msg lum; |
| 427 | struct ustcomm_ust_reply lur; |
| 428 | int ret; |
| 429 | |
| 430 | memset(&lum, 0, sizeof(lum)); |
| 431 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 432 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; |
| 433 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | DBG("waited for quiescent state"); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) |
| 441 | { |
| 442 | if (!calibrate) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | return -ENOSYS; |
| 446 | } |
| 447 | |
| 448 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
| 449 | { |
| 450 | struct ustcomm_ust_msg lum; |
| 451 | struct ustcomm_ust_reply lur; |
| 452 | int ret; |
| 453 | |
| 454 | if (!object) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | memset(&lum, 0, sizeof(lum)); |
| 458 | lum.handle = object->handle; |
| 459 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; |
| 460 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 461 | if (ret) |
| 462 | return ret; |
| 463 | DBG("flushed buffer handle %u", object->handle); |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static |
| 468 | int ustctl_send_channel(int sock, |
| 469 | enum lttng_ust_chan_type type, |
| 470 | void *data, |
| 471 | uint64_t size, |
| 472 | int send_fd_only) |
| 473 | { |
| 474 | ssize_t len; |
| 475 | |
| 476 | if (!send_fd_only) { |
| 477 | /* Send mmap size */ |
| 478 | len = ustcomm_send_unix_sock(sock, &size, sizeof(size)); |
| 479 | if (len != sizeof(size)) { |
| 480 | if (len < 0) |
| 481 | return len; |
| 482 | else |
| 483 | return -EIO; |
| 484 | } |
| 485 | |
| 486 | /* Send channel type */ |
| 487 | len = ustcomm_send_unix_sock(sock, &type, sizeof(type)); |
| 488 | if (len != sizeof(type)) { |
| 489 | if (len < 0) |
| 490 | return len; |
| 491 | else |
| 492 | return -EIO; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* Send channel data */ |
| 497 | len = ustcomm_send_unix_sock(sock, data, size); |
| 498 | if (len != size) { |
| 499 | if (len < 0) |
| 500 | return len; |
| 501 | else |
| 502 | return -EIO; |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static |
| 509 | int ustctl_send_stream(int sock, |
| 510 | uint32_t stream_nr, |
| 511 | uint64_t memory_map_size, |
| 512 | int shm_fd, int wakeup_fd, |
| 513 | int send_fd_only) |
| 514 | { |
| 515 | ssize_t len; |
| 516 | int fds[2]; |
| 517 | |
| 518 | if (!send_fd_only) { |
| 519 | if (shm_fd < 0) { |
| 520 | /* finish iteration */ |
| 521 | uint64_t v = -1; |
| 522 | |
| 523 | len = ustcomm_send_unix_sock(sock, &v, sizeof(v)); |
| 524 | if (len != sizeof(v)) { |
| 525 | if (len < 0) |
| 526 | return len; |
| 527 | else |
| 528 | return -EIO; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* Send mmap size */ |
| 534 | len = ustcomm_send_unix_sock(sock, &memory_map_size, |
| 535 | sizeof(memory_map_size)); |
| 536 | if (len != sizeof(memory_map_size)) { |
| 537 | if (len < 0) |
| 538 | return len; |
| 539 | else |
| 540 | return -EIO; |
| 541 | } |
| 542 | |
| 543 | /* Send stream nr */ |
| 544 | len = ustcomm_send_unix_sock(sock, &stream_nr, |
| 545 | sizeof(stream_nr)); |
| 546 | if (len != sizeof(stream_nr)) { |
| 547 | if (len < 0) |
| 548 | return len; |
| 549 | else |
| 550 | return -EIO; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* Send shm fd and wakeup fd */ |
| 555 | fds[0] = shm_fd; |
| 556 | fds[1] = wakeup_fd; |
| 557 | len = ustcomm_send_fds_unix_sock(sock, fds, 2); |
| 558 | if (len <= 0) { |
| 559 | if (len < 0) |
| 560 | return len; |
| 561 | else |
| 562 | return -EIO; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | int ustctl_recv_channel_from_consumer(int sock, |
| 568 | struct lttng_ust_object_data **_channel_data) |
| 569 | { |
| 570 | struct lttng_ust_object_data *channel_data; |
| 571 | ssize_t len; |
| 572 | int ret; |
| 573 | |
| 574 | channel_data = zmalloc(sizeof(*channel_data)); |
| 575 | if (!channel_data) { |
| 576 | ret = -ENOMEM; |
| 577 | goto error_alloc; |
| 578 | } |
| 579 | channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL; |
| 580 | |
| 581 | /* recv mmap size */ |
| 582 | len = ustcomm_recv_unix_sock(sock, &channel_data->size, |
| 583 | sizeof(channel_data->size)); |
| 584 | if (len != sizeof(channel_data->size)) { |
| 585 | if (len < 0) |
| 586 | ret = len; |
| 587 | else |
| 588 | ret = -EINVAL; |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | /* recv channel type */ |
| 593 | len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type, |
| 594 | sizeof(channel_data->u.channel.type)); |
| 595 | if (len != sizeof(channel_data->u.channel.type)) { |
| 596 | if (len < 0) |
| 597 | ret = len; |
| 598 | else |
| 599 | ret = -EINVAL; |
| 600 | goto error; |
| 601 | } |
| 602 | |
| 603 | /* recv channel data */ |
| 604 | channel_data->u.channel.data = zmalloc(channel_data->size); |
| 605 | if (!channel_data->u.channel.data) { |
| 606 | ret = -ENOMEM; |
| 607 | goto error; |
| 608 | } |
| 609 | len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data, |
| 610 | channel_data->size); |
| 611 | if (len != channel_data->size) { |
| 612 | if (len < 0) |
| 613 | ret = len; |
| 614 | else |
| 615 | ret = -EINVAL; |
| 616 | goto error_recv_data; |
| 617 | } |
| 618 | |
| 619 | *_channel_data = channel_data; |
| 620 | return 0; |
| 621 | |
| 622 | error_recv_data: |
| 623 | free(channel_data->u.channel.data); |
| 624 | error: |
| 625 | free(channel_data); |
| 626 | error_alloc: |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | int ustctl_recv_stream_from_consumer(int sock, |
| 631 | struct lttng_ust_object_data **_stream_data) |
| 632 | { |
| 633 | struct lttng_ust_object_data *stream_data; |
| 634 | ssize_t len; |
| 635 | int ret; |
| 636 | int fds[2]; |
| 637 | |
| 638 | stream_data = zmalloc(sizeof(*stream_data)); |
| 639 | if (!stream_data) { |
| 640 | ret = -ENOMEM; |
| 641 | goto error_alloc; |
| 642 | } |
| 643 | |
| 644 | stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM; |
| 645 | stream_data->handle = -1; |
| 646 | |
| 647 | /* recv mmap size */ |
| 648 | len = ustcomm_recv_unix_sock(sock, &stream_data->size, |
| 649 | sizeof(stream_data->size)); |
| 650 | if (len != sizeof(stream_data->size)) { |
| 651 | if (len < 0) |
| 652 | ret = len; |
| 653 | else |
| 654 | ret = -EINVAL; |
| 655 | goto error; |
| 656 | } |
| 657 | if (stream_data->size == -1) { |
| 658 | ret = -LTTNG_UST_ERR_NOENT; |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | /* recv stream nr */ |
| 663 | len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr, |
| 664 | sizeof(stream_data->u.stream.stream_nr)); |
| 665 | if (len != sizeof(stream_data->u.stream.stream_nr)) { |
| 666 | if (len < 0) |
| 667 | ret = len; |
| 668 | else |
| 669 | ret = -EINVAL; |
| 670 | goto error; |
| 671 | } |
| 672 | |
| 673 | /* recv shm fd and wakeup fd */ |
| 674 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
| 675 | if (len <= 0) { |
| 676 | if (len < 0) { |
| 677 | ret = len; |
| 678 | goto error; |
| 679 | } else { |
| 680 | ret = -EIO; |
| 681 | goto error; |
| 682 | } |
| 683 | } |
| 684 | stream_data->u.stream.shm_fd = fds[0]; |
| 685 | stream_data->u.stream.wakeup_fd = fds[1]; |
| 686 | *_stream_data = stream_data; |
| 687 | return 0; |
| 688 | |
| 689 | error: |
| 690 | free(stream_data); |
| 691 | error_alloc: |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | int ustctl_send_channel_to_ust(int sock, int session_handle, |
| 696 | struct lttng_ust_object_data *channel_data) |
| 697 | { |
| 698 | struct ustcomm_ust_msg lum; |
| 699 | struct ustcomm_ust_reply lur; |
| 700 | int ret; |
| 701 | |
| 702 | if (!channel_data) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | memset(&lum, 0, sizeof(lum)); |
| 706 | lum.handle = session_handle; |
| 707 | lum.cmd = LTTNG_UST_CHANNEL; |
| 708 | lum.u.channel.len = channel_data->size; |
| 709 | lum.u.channel.type = channel_data->u.channel.type; |
| 710 | ret = ustcomm_send_app_msg(sock, &lum); |
| 711 | if (ret) |
| 712 | return ret; |
| 713 | |
| 714 | ret = ustctl_send_channel(sock, |
| 715 | channel_data->u.channel.type, |
| 716 | channel_data->u.channel.data, |
| 717 | channel_data->size, |
| 718 | 1); |
| 719 | if (ret) |
| 720 | return ret; |
| 721 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 722 | if (!ret) { |
| 723 | if (lur.ret_val >= 0) { |
| 724 | channel_data->handle = lur.ret_val; |
| 725 | } |
| 726 | } |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | int ustctl_send_stream_to_ust(int sock, |
| 731 | struct lttng_ust_object_data *channel_data, |
| 732 | struct lttng_ust_object_data *stream_data) |
| 733 | { |
| 734 | struct ustcomm_ust_msg lum; |
| 735 | struct ustcomm_ust_reply lur; |
| 736 | int ret; |
| 737 | |
| 738 | memset(&lum, 0, sizeof(lum)); |
| 739 | lum.handle = channel_data->handle; |
| 740 | lum.cmd = LTTNG_UST_STREAM; |
| 741 | lum.u.stream.len = stream_data->size; |
| 742 | lum.u.stream.stream_nr = stream_data->u.stream.stream_nr; |
| 743 | ret = ustcomm_send_app_msg(sock, &lum); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | |
| 747 | assert(stream_data); |
| 748 | assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM); |
| 749 | |
| 750 | ret = ustctl_send_stream(sock, |
| 751 | stream_data->u.stream.stream_nr, |
| 752 | stream_data->size, |
| 753 | stream_data->u.stream.shm_fd, |
| 754 | stream_data->u.stream.wakeup_fd, 1); |
| 755 | if (ret) |
| 756 | return ret; |
| 757 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /* Buffer operations */ |
| 762 | |
| 763 | struct ustctl_consumer_channel * |
| 764 | ustctl_create_channel(struct ustctl_consumer_channel_attr *attr) |
| 765 | { |
| 766 | struct ustctl_consumer_channel *chan; |
| 767 | const char *transport_name; |
| 768 | struct lttng_transport *transport; |
| 769 | |
| 770 | switch (attr->type) { |
| 771 | case LTTNG_UST_CHAN_PER_CPU: |
| 772 | if (attr->output == LTTNG_UST_MMAP) { |
| 773 | transport_name = attr->overwrite ? |
| 774 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 775 | } else { |
| 776 | return NULL; |
| 777 | } |
| 778 | break; |
| 779 | case LTTNG_UST_CHAN_METADATA: |
| 780 | if (attr->output == LTTNG_UST_MMAP) |
| 781 | transport_name = "relay-metadata-mmap"; |
| 782 | else |
| 783 | return NULL; |
| 784 | break; |
| 785 | default: |
| 786 | transport_name = "<unknown>"; |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | transport = lttng_transport_find(transport_name); |
| 791 | if (!transport) { |
| 792 | DBG("LTTng transport %s not found\n", |
| 793 | transport_name); |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | chan = zmalloc(sizeof(*chan)); |
| 798 | if (!chan) |
| 799 | return NULL; |
| 800 | |
| 801 | chan->chan = transport->ops.channel_create(transport_name, NULL, |
| 802 | attr->subbuf_size, attr->num_subbuf, |
| 803 | attr->switch_timer_interval, |
| 804 | attr->read_timer_interval, |
| 805 | attr->uuid); |
| 806 | if (!chan->chan) { |
| 807 | goto chan_error; |
| 808 | } |
| 809 | chan->chan->ops = &transport->ops; |
| 810 | memcpy(&chan->attr, attr, sizeof(chan->attr)); |
| 811 | return chan; |
| 812 | |
| 813 | chan_error: |
| 814 | free(chan); |
| 815 | return NULL; |
| 816 | } |
| 817 | |
| 818 | void ustctl_destroy_channel(struct ustctl_consumer_channel *chan) |
| 819 | { |
| 820 | chan->chan->ops->channel_destroy(chan->chan); |
| 821 | free(chan); |
| 822 | } |
| 823 | |
| 824 | int ustctl_send_channel_to_sessiond(int sock, |
| 825 | struct ustctl_consumer_channel *channel) |
| 826 | { |
| 827 | struct shm_object_table *table; |
| 828 | |
| 829 | table = channel->chan->handle->table; |
| 830 | if (table->size <= 0) |
| 831 | return -EINVAL; |
| 832 | return ustctl_send_channel(sock, |
| 833 | channel->attr.type, |
| 834 | table->objects[0].memory_map, |
| 835 | table->objects[0].memory_map_size, |
| 836 | 0); |
| 837 | } |
| 838 | |
| 839 | int ustctl_send_stream_to_sessiond(int sock, |
| 840 | struct ustctl_consumer_stream *stream) |
| 841 | { |
| 842 | if (!stream) |
| 843 | return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0); |
| 844 | |
| 845 | return ustctl_send_stream(sock, |
| 846 | stream->cpu, |
| 847 | stream->memory_map_size, |
| 848 | stream->shm_fd, stream->wakeup_fd, |
| 849 | 0); |
| 850 | } |
| 851 | |
| 852 | int ustctl_write_metadata_to_channel( |
| 853 | struct ustctl_consumer_channel *channel, |
| 854 | const char *metadata_str, /* NOT null-terminated */ |
| 855 | size_t len) /* metadata length */ |
| 856 | { |
| 857 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 858 | struct lttng_channel *chan = channel->chan; |
| 859 | const char *str = metadata_str; |
| 860 | int ret = 0, waitret; |
| 861 | size_t reserve_len, pos; |
| 862 | |
| 863 | for (pos = 0; pos < len; pos += reserve_len) { |
| 864 | reserve_len = min_t(size_t, |
| 865 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 866 | len - pos); |
| 867 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 868 | sizeof(char), -1, chan->handle); |
| 869 | /* |
| 870 | * We don't care about metadata buffer's records lost |
| 871 | * count, because we always retry here. Report error if |
| 872 | * we need to bail out after timeout or being |
| 873 | * interrupted. |
| 874 | */ |
| 875 | waitret = wait_cond_interruptible_timeout( |
| 876 | ({ |
| 877 | ret = chan->ops->event_reserve(&ctx, 0); |
| 878 | ret != -ENOBUFS || !ret; |
| 879 | }), |
| 880 | LTTNG_METADATA_TIMEOUT_MSEC); |
| 881 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
| 882 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
| 883 | waitret == -EINTR ? "interrupted" : |
| 884 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 885 | if (waitret == -EINTR) |
| 886 | ret = waitret; |
| 887 | goto end; |
| 888 | } |
| 889 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
| 890 | chan->ops->event_commit(&ctx); |
| 891 | } |
| 892 | end: |
| 893 | return ret; |
| 894 | } |
| 895 | |
| 896 | int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream) |
| 897 | { |
| 898 | struct channel *chan; |
| 899 | |
| 900 | chan = stream->chan->chan->chan; |
| 901 | return ring_buffer_close_wait_fd(&chan->backend.config, |
| 902 | chan, stream->handle, stream->cpu); |
| 903 | } |
| 904 | |
| 905 | int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 906 | { |
| 907 | struct channel *chan; |
| 908 | |
| 909 | chan = stream->chan->chan->chan; |
| 910 | return ring_buffer_close_wakeup_fd(&chan->backend.config, |
| 911 | chan, stream->handle, stream->cpu); |
| 912 | } |
| 913 | |
| 914 | struct ustctl_consumer_stream * |
| 915 | ustctl_create_stream(struct ustctl_consumer_channel *channel, |
| 916 | int cpu) |
| 917 | { |
| 918 | struct ustctl_consumer_stream *stream; |
| 919 | struct lttng_ust_shm_handle *handle; |
| 920 | struct channel *chan; |
| 921 | int shm_fd, wait_fd, wakeup_fd; |
| 922 | uint64_t memory_map_size; |
| 923 | struct lttng_ust_lib_ring_buffer *buf; |
| 924 | int ret; |
| 925 | |
| 926 | if (!channel) |
| 927 | return NULL; |
| 928 | handle = channel->chan->handle; |
| 929 | if (!handle) |
| 930 | return NULL; |
| 931 | |
| 932 | chan = channel->chan->chan; |
| 933 | buf = channel_get_ring_buffer(&chan->backend.config, |
| 934 | chan, cpu, handle, &shm_fd, &wait_fd, |
| 935 | &wakeup_fd, &memory_map_size); |
| 936 | if (!buf) |
| 937 | return NULL; |
| 938 | ret = lib_ring_buffer_open_read(buf, handle); |
| 939 | if (ret) |
| 940 | return NULL; |
| 941 | |
| 942 | stream = zmalloc(sizeof(*stream)); |
| 943 | if (!stream) |
| 944 | goto alloc_error; |
| 945 | stream->handle = handle; |
| 946 | stream->buf = buf; |
| 947 | stream->chan = channel; |
| 948 | stream->shm_fd = shm_fd; |
| 949 | stream->wait_fd = wait_fd; |
| 950 | stream->wakeup_fd = wakeup_fd; |
| 951 | stream->memory_map_size = memory_map_size; |
| 952 | stream->cpu = cpu; |
| 953 | return stream; |
| 954 | |
| 955 | alloc_error: |
| 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | void ustctl_destroy_stream(struct ustctl_consumer_stream *stream) |
| 960 | { |
| 961 | struct lttng_ust_lib_ring_buffer *buf; |
| 962 | struct ustctl_consumer_channel *consumer_chan; |
| 963 | |
| 964 | assert(stream); |
| 965 | buf = stream->buf; |
| 966 | consumer_chan = stream->chan; |
| 967 | lib_ring_buffer_release_read(buf, consumer_chan->chan->handle); |
| 968 | free(stream); |
| 969 | } |
| 970 | |
| 971 | int ustctl_get_wait_fd(struct ustctl_consumer_stream *stream) |
| 972 | { |
| 973 | struct lttng_ust_lib_ring_buffer *buf; |
| 974 | struct ustctl_consumer_channel *consumer_chan; |
| 975 | |
| 976 | if (!stream) |
| 977 | return -EINVAL; |
| 978 | buf = stream->buf; |
| 979 | consumer_chan = stream->chan; |
| 980 | return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 981 | } |
| 982 | |
| 983 | int ustctl_get_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 984 | { |
| 985 | struct lttng_ust_lib_ring_buffer *buf; |
| 986 | struct ustctl_consumer_channel *consumer_chan; |
| 987 | |
| 988 | if (!stream) |
| 989 | return -EINVAL; |
| 990 | buf = stream->buf; |
| 991 | consumer_chan = stream->chan; |
| 992 | return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 993 | } |
| 994 | |
| 995 | /* For mmap mode, readable without "get" operation */ |
| 996 | |
| 997 | void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream) |
| 998 | { |
| 999 | struct lttng_ust_lib_ring_buffer *buf; |
| 1000 | struct ustctl_consumer_channel *consumer_chan; |
| 1001 | |
| 1002 | if (!stream) |
| 1003 | return NULL; |
| 1004 | buf = stream->buf; |
| 1005 | consumer_chan = stream->chan; |
| 1006 | return shmp(consumer_chan->chan->handle, buf->backend.memory_map); |
| 1007 | } |
| 1008 | |
| 1009 | /* returns the length to mmap. */ |
| 1010 | int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream, |
| 1011 | unsigned long *len) |
| 1012 | { |
| 1013 | struct ustctl_consumer_channel *consumer_chan; |
| 1014 | unsigned long mmap_buf_len; |
| 1015 | struct channel *chan; |
| 1016 | |
| 1017 | if (!stream) |
| 1018 | return -EINVAL; |
| 1019 | consumer_chan = stream->chan; |
| 1020 | chan = consumer_chan->chan->chan; |
| 1021 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1022 | return -EINVAL; |
| 1023 | mmap_buf_len = chan->backend.buf_size; |
| 1024 | if (chan->backend.extra_reader_sb) |
| 1025 | mmap_buf_len += chan->backend.subbuf_size; |
| 1026 | if (mmap_buf_len > INT_MAX) |
| 1027 | return -EFBIG; |
| 1028 | *len = mmap_buf_len; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | /* returns the maximum size for sub-buffers. */ |
| 1033 | int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1034 | unsigned long *len) |
| 1035 | { |
| 1036 | struct ustctl_consumer_channel *consumer_chan; |
| 1037 | struct channel *chan; |
| 1038 | |
| 1039 | if (!stream) |
| 1040 | return -EINVAL; |
| 1041 | consumer_chan = stream->chan; |
| 1042 | chan = consumer_chan->chan->chan; |
| 1043 | *len = chan->backend.subbuf_size; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * For mmap mode, operate on the current packet (between get/put or |
| 1049 | * get_next/put_next). |
| 1050 | */ |
| 1051 | |
| 1052 | /* returns the offset of the subbuffer belonging to the mmap reader. */ |
| 1053 | int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream, |
| 1054 | unsigned long *off) |
| 1055 | { |
| 1056 | struct channel *chan; |
| 1057 | unsigned long sb_bindex; |
| 1058 | struct lttng_ust_lib_ring_buffer *buf; |
| 1059 | struct ustctl_consumer_channel *consumer_chan; |
| 1060 | |
| 1061 | if (!stream) |
| 1062 | return -EINVAL; |
| 1063 | buf = stream->buf; |
| 1064 | consumer_chan = stream->chan; |
| 1065 | chan = consumer_chan->chan->chan; |
| 1066 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1067 | return -EINVAL; |
| 1068 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, |
| 1069 | buf->backend.buf_rsb.id); |
| 1070 | *off = shmp(consumer_chan->chan->handle, |
| 1071 | shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset; |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1076 | int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1077 | unsigned long *len) |
| 1078 | { |
| 1079 | struct ustctl_consumer_channel *consumer_chan; |
| 1080 | struct channel *chan; |
| 1081 | struct lttng_ust_lib_ring_buffer *buf; |
| 1082 | |
| 1083 | if (!stream) |
| 1084 | return -EINVAL; |
| 1085 | |
| 1086 | buf = stream->buf; |
| 1087 | consumer_chan = stream->chan; |
| 1088 | chan = consumer_chan->chan->chan; |
| 1089 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1090 | consumer_chan->chan->handle); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1095 | int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1096 | unsigned long *len) |
| 1097 | { |
| 1098 | struct ustctl_consumer_channel *consumer_chan; |
| 1099 | struct channel *chan; |
| 1100 | struct lttng_ust_lib_ring_buffer *buf; |
| 1101 | |
| 1102 | if (!stream) |
| 1103 | return -EINVAL; |
| 1104 | buf = stream->buf; |
| 1105 | consumer_chan = stream->chan; |
| 1106 | chan = consumer_chan->chan->chan; |
| 1107 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1108 | consumer_chan->chan->handle); |
| 1109 | *len = PAGE_ALIGN(*len); |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | /* Get exclusive read access to the next sub-buffer that can be read. */ |
| 1114 | int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1115 | { |
| 1116 | struct lttng_ust_lib_ring_buffer *buf; |
| 1117 | struct ustctl_consumer_channel *consumer_chan; |
| 1118 | |
| 1119 | if (!stream) |
| 1120 | return -EINVAL; |
| 1121 | buf = stream->buf; |
| 1122 | consumer_chan = stream->chan; |
| 1123 | return lib_ring_buffer_get_next_subbuf(buf, |
| 1124 | consumer_chan->chan->handle); |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | /* Release exclusive sub-buffer access, move consumer forward. */ |
| 1129 | int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1130 | { |
| 1131 | struct lttng_ust_lib_ring_buffer *buf; |
| 1132 | struct ustctl_consumer_channel *consumer_chan; |
| 1133 | |
| 1134 | if (!stream) |
| 1135 | return -EINVAL; |
| 1136 | buf = stream->buf; |
| 1137 | consumer_chan = stream->chan; |
| 1138 | lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle); |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | /* snapshot */ |
| 1143 | |
| 1144 | /* Get a snapshot of the current ring buffer producer and consumer positions */ |
| 1145 | int ustctl_snapshot(struct ustctl_consumer_stream *stream) |
| 1146 | { |
| 1147 | struct lttng_ust_lib_ring_buffer *buf; |
| 1148 | struct ustctl_consumer_channel *consumer_chan; |
| 1149 | |
| 1150 | if (!stream) |
| 1151 | return -EINVAL; |
| 1152 | buf = stream->buf; |
| 1153 | consumer_chan = stream->chan; |
| 1154 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 1155 | &buf->prod_snapshot, consumer_chan->chan->handle); |
| 1156 | } |
| 1157 | |
| 1158 | /* Get the consumer position (iteration start) */ |
| 1159 | int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream, |
| 1160 | unsigned long *pos) |
| 1161 | { |
| 1162 | struct lttng_ust_lib_ring_buffer *buf; |
| 1163 | |
| 1164 | if (!stream) |
| 1165 | return -EINVAL; |
| 1166 | buf = stream->buf; |
| 1167 | *pos = buf->cons_snapshot; |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | /* Get the producer position (iteration end) */ |
| 1172 | int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream, |
| 1173 | unsigned long *pos) |
| 1174 | { |
| 1175 | struct lttng_ust_lib_ring_buffer *buf; |
| 1176 | |
| 1177 | if (!stream) |
| 1178 | return -EINVAL; |
| 1179 | buf = stream->buf; |
| 1180 | *pos = buf->prod_snapshot; |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | /* Get exclusive read access to the specified sub-buffer position */ |
| 1185 | int ustctl_get_subbuf(struct ustctl_consumer_stream *stream, |
| 1186 | unsigned long *pos) |
| 1187 | { |
| 1188 | struct lttng_ust_lib_ring_buffer *buf; |
| 1189 | struct ustctl_consumer_channel *consumer_chan; |
| 1190 | |
| 1191 | if (!stream) |
| 1192 | return -EINVAL; |
| 1193 | buf = stream->buf; |
| 1194 | consumer_chan = stream->chan; |
| 1195 | return lib_ring_buffer_get_subbuf(buf, *pos, |
| 1196 | consumer_chan->chan->handle); |
| 1197 | } |
| 1198 | |
| 1199 | /* Release exclusive sub-buffer access */ |
| 1200 | int ustctl_put_subbuf(struct ustctl_consumer_stream *stream) |
| 1201 | { |
| 1202 | struct lttng_ust_lib_ring_buffer *buf; |
| 1203 | struct ustctl_consumer_channel *consumer_chan; |
| 1204 | |
| 1205 | if (!stream) |
| 1206 | return -EINVAL; |
| 1207 | buf = stream->buf; |
| 1208 | consumer_chan = stream->chan; |
| 1209 | lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | void ustctl_flush_buffer(struct ustctl_consumer_stream *stream, |
| 1214 | int producer_active) |
| 1215 | { |
| 1216 | struct lttng_ust_lib_ring_buffer *buf; |
| 1217 | struct ustctl_consumer_channel *consumer_chan; |
| 1218 | |
| 1219 | assert(stream); |
| 1220 | buf = stream->buf; |
| 1221 | consumer_chan = stream->chan; |
| 1222 | lib_ring_buffer_switch_slow(buf, |
| 1223 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, |
| 1224 | consumer_chan->chan->handle); |
| 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | * Returns 0 on success, negative error value on error. |
| 1229 | */ |
| 1230 | int ustctl_recv_reg_msg(int sock, |
| 1231 | enum ustctl_socket_type *type, |
| 1232 | uint32_t *major, |
| 1233 | uint32_t *minor, |
| 1234 | uint32_t *pid, |
| 1235 | uint32_t *ppid, |
| 1236 | uint32_t *uid, |
| 1237 | uint32_t *gid, |
| 1238 | uint32_t *bits_per_long, |
| 1239 | uint32_t *uint8_t_alignment, |
| 1240 | uint32_t *uint16_t_alignment, |
| 1241 | uint32_t *uint32_t_alignment, |
| 1242 | uint32_t *uint64_t_alignment, |
| 1243 | uint32_t *long_alignment, |
| 1244 | int *byte_order, |
| 1245 | char *name) |
| 1246 | { |
| 1247 | ssize_t len; |
| 1248 | struct ustctl_reg_msg reg_msg; |
| 1249 | |
| 1250 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
| 1251 | if (len > 0 && len != sizeof(reg_msg)) |
| 1252 | return -EIO; |
| 1253 | if (len == 0) |
| 1254 | return -EPIPE; |
| 1255 | if (len < 0) |
| 1256 | return len; |
| 1257 | |
| 1258 | if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) { |
| 1259 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 1260 | BIG_ENDIAN : LITTLE_ENDIAN; |
| 1261 | } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) { |
| 1262 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 1263 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 1264 | } else { |
| 1265 | return -LTTNG_UST_ERR_INVAL_MAGIC; |
| 1266 | } |
| 1267 | switch (reg_msg.socket_type) { |
| 1268 | case 0: *type = USTCTL_SOCKET_CMD; |
| 1269 | break; |
| 1270 | case 1: *type = USTCTL_SOCKET_NOTIFY; |
| 1271 | break; |
| 1272 | default: |
| 1273 | return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE; |
| 1274 | } |
| 1275 | *major = reg_msg.major; |
| 1276 | *minor = reg_msg.minor; |
| 1277 | *pid = reg_msg.pid; |
| 1278 | *ppid = reg_msg.ppid; |
| 1279 | *uid = reg_msg.uid; |
| 1280 | *gid = reg_msg.gid; |
| 1281 | *bits_per_long = reg_msg.bits_per_long; |
| 1282 | *uint8_t_alignment = reg_msg.uint8_t_alignment; |
| 1283 | *uint16_t_alignment = reg_msg.uint16_t_alignment; |
| 1284 | *uint32_t_alignment = reg_msg.uint32_t_alignment; |
| 1285 | *uint64_t_alignment = reg_msg.uint64_t_alignment; |
| 1286 | *long_alignment = reg_msg.long_alignment; |
| 1287 | memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN); |
| 1288 | if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) { |
| 1289 | return -LTTNG_UST_ERR_UNSUP_MAJOR; |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd) |
| 1296 | { |
| 1297 | struct ustcomm_notify_hdr header; |
| 1298 | ssize_t len; |
| 1299 | |
| 1300 | len = ustcomm_recv_unix_sock(sock, &header, sizeof(header)); |
| 1301 | if (len > 0 && len != sizeof(header)) |
| 1302 | return -EIO; |
| 1303 | if (len == 0) |
| 1304 | return -EPIPE; |
| 1305 | if (len < 0) |
| 1306 | return len; |
| 1307 | switch (header.notify_cmd) { |
| 1308 | case 0: |
| 1309 | *notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 1310 | break; |
| 1311 | case 1: |
| 1312 | *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1313 | break; |
| 1314 | default: |
| 1315 | return -EINVAL; |
| 1316 | } |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * Returns 0 on success, negative error value on error. |
| 1322 | */ |
| 1323 | int ustctl_recv_register_event(int sock, |
| 1324 | int *session_objd, |
| 1325 | int *channel_objd, |
| 1326 | char *event_name, |
| 1327 | int *loglevel, |
| 1328 | char **signature, |
| 1329 | size_t *nr_fields, |
| 1330 | struct ustctl_field **fields, |
| 1331 | char **model_emf_uri) |
| 1332 | { |
| 1333 | ssize_t len; |
| 1334 | struct ustcomm_notify_event_msg msg; |
| 1335 | size_t signature_len, fields_len, model_emf_uri_len; |
| 1336 | char *a_sign = NULL, *a_model_emf_uri = NULL; |
| 1337 | struct ustctl_field *a_fields = NULL; |
| 1338 | |
| 1339 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 1340 | if (len > 0 && len != sizeof(msg)) |
| 1341 | return -EIO; |
| 1342 | if (len == 0) |
| 1343 | return -EPIPE; |
| 1344 | if (len < 0) |
| 1345 | return len; |
| 1346 | |
| 1347 | *session_objd = msg.session_objd; |
| 1348 | *channel_objd = msg.channel_objd; |
| 1349 | strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 1350 | event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1351 | *loglevel = msg.loglevel; |
| 1352 | signature_len = msg.signature_len; |
| 1353 | fields_len = msg.fields_len; |
| 1354 | |
| 1355 | if (fields_len % sizeof(*a_fields) != 0) { |
| 1356 | return -EINVAL; |
| 1357 | } |
| 1358 | |
| 1359 | model_emf_uri_len = msg.model_emf_uri_len; |
| 1360 | |
| 1361 | /* recv signature. contains at least \0. */ |
| 1362 | a_sign = zmalloc(signature_len); |
| 1363 | if (!a_sign) |
| 1364 | return -ENOMEM; |
| 1365 | len = ustcomm_recv_unix_sock(sock, a_sign, signature_len); |
| 1366 | if (len > 0 && len != signature_len) { |
| 1367 | len = -EIO; |
| 1368 | goto signature_error; |
| 1369 | } |
| 1370 | if (len == 0) { |
| 1371 | len = -EPIPE; |
| 1372 | goto signature_error; |
| 1373 | } |
| 1374 | if (len < 0) { |
| 1375 | goto signature_error; |
| 1376 | } |
| 1377 | /* Enforce end of string */ |
| 1378 | signature[signature_len - 1] = '\0'; |
| 1379 | |
| 1380 | /* recv fields */ |
| 1381 | if (fields_len) { |
| 1382 | a_fields = zmalloc(fields_len); |
| 1383 | if (!a_fields) { |
| 1384 | len = -ENOMEM; |
| 1385 | goto signature_error; |
| 1386 | } |
| 1387 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 1388 | if (len > 0 && len != fields_len) { |
| 1389 | len = -EIO; |
| 1390 | goto fields_error; |
| 1391 | } |
| 1392 | if (len == 0) { |
| 1393 | len = -EPIPE; |
| 1394 | goto fields_error; |
| 1395 | } |
| 1396 | if (len < 0) { |
| 1397 | goto fields_error; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | if (model_emf_uri_len) { |
| 1402 | /* recv model_emf_uri_len */ |
| 1403 | a_model_emf_uri = zmalloc(model_emf_uri_len); |
| 1404 | if (!a_model_emf_uri) { |
| 1405 | len = -ENOMEM; |
| 1406 | goto fields_error; |
| 1407 | } |
| 1408 | len = ustcomm_recv_unix_sock(sock, a_model_emf_uri, |
| 1409 | model_emf_uri_len); |
| 1410 | if (len > 0 && len != model_emf_uri_len) { |
| 1411 | len = -EIO; |
| 1412 | goto model_error; |
| 1413 | } |
| 1414 | if (len == 0) { |
| 1415 | len = -EPIPE; |
| 1416 | goto model_error; |
| 1417 | } |
| 1418 | if (len < 0) { |
| 1419 | goto model_error; |
| 1420 | } |
| 1421 | /* Enforce end of string */ |
| 1422 | a_model_emf_uri[model_emf_uri_len - 1] = '\0'; |
| 1423 | } |
| 1424 | |
| 1425 | *signature = a_sign; |
| 1426 | *nr_fields = fields_len / sizeof(*a_fields); |
| 1427 | *fields = a_fields; |
| 1428 | *model_emf_uri = a_model_emf_uri; |
| 1429 | |
| 1430 | return 0; |
| 1431 | |
| 1432 | model_error: |
| 1433 | free(a_model_emf_uri); |
| 1434 | fields_error: |
| 1435 | free(a_fields); |
| 1436 | signature_error: |
| 1437 | free(a_sign); |
| 1438 | return len; |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * Returns 0 on success, negative error value on error. |
| 1443 | */ |
| 1444 | int ustctl_reply_register_event(int sock, |
| 1445 | uint32_t id, |
| 1446 | int ret_code) |
| 1447 | { |
| 1448 | ssize_t len; |
| 1449 | struct { |
| 1450 | struct ustcomm_notify_hdr header; |
| 1451 | struct ustcomm_notify_event_reply r; |
| 1452 | } reply; |
| 1453 | |
| 1454 | memset(&reply, 0, sizeof(reply)); |
| 1455 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 1456 | reply.r.ret_code = ret_code; |
| 1457 | reply.r.event_id = id; |
| 1458 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 1459 | if (len > 0 && len != sizeof(reply)) |
| 1460 | return -EIO; |
| 1461 | if (len < 0) |
| 1462 | return len; |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * Returns 0 on success, negative UST or system error value on error. |
| 1468 | */ |
| 1469 | int ustctl_recv_register_channel(int sock, |
| 1470 | int *session_objd, /* session descriptor (output) */ |
| 1471 | int *channel_objd, /* channel descriptor (output) */ |
| 1472 | size_t *nr_fields, |
| 1473 | struct ustctl_field **fields) |
| 1474 | { |
| 1475 | ssize_t len; |
| 1476 | struct ustcomm_notify_channel_msg msg; |
| 1477 | size_t fields_len; |
| 1478 | struct ustctl_field *a_fields; |
| 1479 | |
| 1480 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 1481 | if (len > 0 && len != sizeof(msg)) |
| 1482 | return -EIO; |
| 1483 | if (len == 0) |
| 1484 | return -EPIPE; |
| 1485 | if (len < 0) |
| 1486 | return len; |
| 1487 | |
| 1488 | *session_objd = msg.session_objd; |
| 1489 | *channel_objd = msg.channel_objd; |
| 1490 | fields_len = msg.ctx_fields_len; |
| 1491 | |
| 1492 | if (fields_len % sizeof(*a_fields) != 0) { |
| 1493 | return -EINVAL; |
| 1494 | } |
| 1495 | |
| 1496 | /* recv fields */ |
| 1497 | if (fields_len) { |
| 1498 | a_fields = zmalloc(fields_len); |
| 1499 | if (!a_fields) { |
| 1500 | len = -ENOMEM; |
| 1501 | goto alloc_error; |
| 1502 | } |
| 1503 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 1504 | if (len > 0 && len != fields_len) { |
| 1505 | len = -EIO; |
| 1506 | goto fields_error; |
| 1507 | } |
| 1508 | if (len == 0) { |
| 1509 | len = -EPIPE; |
| 1510 | goto fields_error; |
| 1511 | } |
| 1512 | if (len < 0) { |
| 1513 | goto fields_error; |
| 1514 | } |
| 1515 | *fields = a_fields; |
| 1516 | } else { |
| 1517 | *fields = NULL; |
| 1518 | } |
| 1519 | *nr_fields = fields_len / sizeof(*a_fields); |
| 1520 | return 0; |
| 1521 | |
| 1522 | fields_error: |
| 1523 | free(a_fields); |
| 1524 | alloc_error: |
| 1525 | return len; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Returns 0 on success, negative error value on error. |
| 1530 | */ |
| 1531 | int ustctl_reply_register_channel(int sock, |
| 1532 | uint32_t chan_id, |
| 1533 | enum ustctl_channel_header header_type, |
| 1534 | int ret_code) |
| 1535 | { |
| 1536 | ssize_t len; |
| 1537 | struct { |
| 1538 | struct ustcomm_notify_hdr header; |
| 1539 | struct ustcomm_notify_channel_reply r; |
| 1540 | } reply; |
| 1541 | |
| 1542 | memset(&reply, 0, sizeof(reply)); |
| 1543 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1544 | reply.r.ret_code = ret_code; |
| 1545 | reply.r.chan_id = chan_id; |
| 1546 | switch (header_type) { |
| 1547 | case USTCTL_CHANNEL_HEADER_COMPACT: |
| 1548 | reply.r.header_type = 1; |
| 1549 | break; |
| 1550 | case USTCTL_CHANNEL_HEADER_LARGE: |
| 1551 | reply.r.header_type = 2; |
| 1552 | break; |
| 1553 | default: |
| 1554 | reply.r.header_type = 0; |
| 1555 | break; |
| 1556 | } |
| 1557 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 1558 | if (len > 0 && len != sizeof(reply)) |
| 1559 | return -EIO; |
| 1560 | if (len < 0) |
| 1561 | return len; |
| 1562 | return 0; |
| 1563 | } |
| 1564 | |
| 1565 | static __attribute__((constructor)) |
| 1566 | void ustctl_init(void) |
| 1567 | { |
| 1568 | init_usterr(); |
| 1569 | lttng_ring_buffer_metadata_client_init(); |
| 1570 | lttng_ring_buffer_client_overwrite_init(); |
| 1571 | lttng_ring_buffer_client_discard_init(); |
| 1572 | } |
| 1573 | |
| 1574 | static __attribute__((destructor)) |
| 1575 | void ustctl_exit(void) |
| 1576 | { |
| 1577 | lttng_ring_buffer_client_discard_exit(); |
| 1578 | lttng_ring_buffer_client_overwrite_exit(); |
| 1579 | lttng_ring_buffer_metadata_client_exit(); |
| 1580 | } |