| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; only version 2 |
| 8 | * of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 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 | |
| 26 | #include <usterr-signal-safe.h> |
| 27 | #include <ust-comm.h> |
| 28 | |
| 29 | #include "../libringbuffer/backend.h" |
| 30 | #include "../libringbuffer/frontend.h" |
| 31 | |
| 32 | volatile enum ust_loglevel ust_loglevel; |
| 33 | |
| 34 | static |
| 35 | void init_object(struct lttng_ust_object_data *data) |
| 36 | { |
| 37 | data->handle = -1; |
| 38 | data->shm_fd = -1; |
| 39 | data->wait_fd = -1; |
| 40 | data->memory_map_size = 0; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * If sock is negative, it means we don't have to notify the other side |
| 45 | * (e.g. application has already vanished). |
| 46 | */ |
| 47 | void ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
| 48 | { |
| 49 | struct ustcomm_ust_msg lum; |
| 50 | struct ustcomm_ust_reply lur; |
| 51 | int ret; |
| 52 | |
| 53 | if (data->shm_fd >= 0) |
| 54 | close(data->shm_fd); |
| 55 | if (data->wait_fd >= 0) |
| 56 | close(data->wait_fd); |
| 57 | if (sock >= 0) { |
| 58 | memset(&lum, 0, sizeof(lum)); |
| 59 | lum.handle = data->handle; |
| 60 | lum.cmd = LTTNG_UST_RELEASE; |
| 61 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 62 | assert(!ret); |
| 63 | } |
| 64 | free(data); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Send registration done packet to the application. |
| 69 | */ |
| 70 | int ustctl_register_done(int sock) |
| 71 | { |
| 72 | struct ustcomm_ust_msg lum; |
| 73 | struct ustcomm_ust_reply lur; |
| 74 | int ret; |
| 75 | |
| 76 | DBG("Sending register done command to %d", sock); |
| 77 | memset(&lum, 0, sizeof(lum)); |
| 78 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 79 | lum.cmd = LTTNG_UST_REGISTER_DONE; |
| 80 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | if (lur.ret_code != USTCOMM_OK) { |
| 84 | DBG("Return code: %s", ustcomm_get_readable_code(lur.ret_code)); |
| 85 | goto error; |
| 86 | } |
| 87 | return 0; |
| 88 | |
| 89 | error: |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * returns session handle. |
| 95 | */ |
| 96 | int ustctl_create_session(int sock) |
| 97 | { |
| 98 | struct ustcomm_ust_msg lum; |
| 99 | struct ustcomm_ust_reply lur; |
| 100 | int ret, session_handle; |
| 101 | |
| 102 | /* Create session */ |
| 103 | memset(&lum, 0, sizeof(lum)); |
| 104 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 105 | lum.cmd = LTTNG_UST_SESSION; |
| 106 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | session_handle = lur.ret_val; |
| 110 | DBG("received session handle %u", session_handle); |
| 111 | return session_handle; |
| 112 | } |
| 113 | |
| 114 | /* open the metadata global channel */ |
| 115 | int ustctl_open_metadata(int sock, int session_handle, |
| 116 | struct lttng_ust_channel_attr *chops, |
| 117 | struct lttng_ust_object_data **_metadata_data) |
| 118 | { |
| 119 | struct ustcomm_ust_msg lum; |
| 120 | struct ustcomm_ust_reply lur; |
| 121 | struct lttng_ust_object_data *metadata_data; |
| 122 | int ret; |
| 123 | |
| 124 | metadata_data = malloc(sizeof(*metadata_data)); |
| 125 | if (!metadata_data) |
| 126 | return -ENOMEM; |
| 127 | init_object(metadata_data); |
| 128 | /* Create metadata channel */ |
| 129 | memset(&lum, 0, sizeof(lum)); |
| 130 | lum.handle = session_handle; |
| 131 | lum.cmd = LTTNG_UST_METADATA; |
| 132 | lum.u.channel.overwrite = chops->overwrite; |
| 133 | lum.u.channel.subbuf_size = chops->subbuf_size; |
| 134 | lum.u.channel.num_subbuf = chops->num_subbuf; |
| 135 | lum.u.channel.switch_timer_interval = chops->switch_timer_interval; |
| 136 | lum.u.channel.read_timer_interval = chops->read_timer_interval; |
| 137 | lum.u.channel.output = chops->output; |
| 138 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 139 | if (ret) { |
| 140 | free(metadata_data); |
| 141 | return ret; |
| 142 | } |
| 143 | if (lur.ret_code != USTCOMM_OK) { |
| 144 | free(metadata_data); |
| 145 | return lur.ret_code; |
| 146 | } |
| 147 | metadata_data->handle = lur.ret_val; |
| 148 | DBG("received metadata handle %u", metadata_data->handle); |
| 149 | metadata_data->memory_map_size = lur.u.channel.memory_map_size; |
| 150 | /* get shm fd */ |
| 151 | ret = ustcomm_recv_fd(sock); |
| 152 | if (ret < 0) |
| 153 | goto error; |
| 154 | metadata_data->shm_fd = ret; |
| 155 | /* get wait fd */ |
| 156 | ret = ustcomm_recv_fd(sock); |
| 157 | if (ret < 0) |
| 158 | goto error; |
| 159 | metadata_data->wait_fd = ret; |
| 160 | *_metadata_data = metadata_data; |
| 161 | return 0; |
| 162 | |
| 163 | error: |
| 164 | ustctl_release_object(sock, metadata_data); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
| 168 | int ustctl_create_channel(int sock, int session_handle, |
| 169 | struct lttng_ust_channel_attr *chops, |
| 170 | struct lttng_ust_object_data **_channel_data) |
| 171 | { |
| 172 | struct ustcomm_ust_msg lum; |
| 173 | struct ustcomm_ust_reply lur; |
| 174 | struct lttng_ust_object_data *channel_data; |
| 175 | int ret; |
| 176 | |
| 177 | channel_data = malloc(sizeof(*channel_data)); |
| 178 | if (!channel_data) |
| 179 | return -ENOMEM; |
| 180 | init_object(channel_data); |
| 181 | /* Create metadata channel */ |
| 182 | memset(&lum, 0, sizeof(lum)); |
| 183 | lum.handle = session_handle; |
| 184 | lum.cmd = LTTNG_UST_CHANNEL; |
| 185 | lum.u.channel.overwrite = chops->overwrite; |
| 186 | lum.u.channel.subbuf_size = chops->subbuf_size; |
| 187 | lum.u.channel.num_subbuf = chops->num_subbuf; |
| 188 | lum.u.channel.switch_timer_interval = chops->switch_timer_interval; |
| 189 | lum.u.channel.read_timer_interval = chops->read_timer_interval; |
| 190 | lum.u.channel.output = chops->output; |
| 191 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 192 | if (ret) { |
| 193 | free(channel_data); |
| 194 | return ret; |
| 195 | } |
| 196 | if (lur.ret_code != USTCOMM_OK) { |
| 197 | free(channel_data); |
| 198 | return lur.ret_code; |
| 199 | } |
| 200 | channel_data->handle = lur.ret_val; |
| 201 | DBG("received channel handle %u", channel_data->handle); |
| 202 | channel_data->memory_map_size = lur.u.channel.memory_map_size; |
| 203 | /* get shm fd */ |
| 204 | ret = ustcomm_recv_fd(sock); |
| 205 | if (ret < 0) |
| 206 | goto error; |
| 207 | channel_data->shm_fd = ret; |
| 208 | /* get wait fd */ |
| 209 | ret = ustcomm_recv_fd(sock); |
| 210 | if (ret < 0) |
| 211 | goto error; |
| 212 | channel_data->wait_fd = ret; |
| 213 | *_channel_data = channel_data; |
| 214 | return 0; |
| 215 | |
| 216 | error: |
| 217 | ustctl_release_object(sock, channel_data); |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Return -ENOENT if no more stream is available for creation. |
| 223 | * Return 0 on success. |
| 224 | * Return negative error value on error. |
| 225 | */ |
| 226 | int ustctl_create_stream(int sock, struct lttng_ust_object_data *channel_data, |
| 227 | struct lttng_ust_object_data **_stream_data) |
| 228 | { |
| 229 | struct ustcomm_ust_msg lum; |
| 230 | struct ustcomm_ust_reply lur; |
| 231 | struct lttng_ust_object_data *stream_data; |
| 232 | int ret, fd; |
| 233 | |
| 234 | stream_data = malloc(sizeof(*stream_data)); |
| 235 | if (!stream_data) |
| 236 | return -ENOMEM; |
| 237 | init_object(stream_data); |
| 238 | memset(&lum, 0, sizeof(lum)); |
| 239 | lum.handle = channel_data->handle; |
| 240 | lum.cmd = LTTNG_UST_STREAM; |
| 241 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 242 | if (ret) { |
| 243 | free(stream_data); |
| 244 | return ret; |
| 245 | } |
| 246 | if (lur.ret_code != USTCOMM_OK) { |
| 247 | free(stream_data); |
| 248 | return lur.ret_code; |
| 249 | } |
| 250 | |
| 251 | stream_data->handle = lur.ret_val; |
| 252 | DBG("received stream handle %u", stream_data->handle); |
| 253 | stream_data->memory_map_size = lur.u.stream.memory_map_size; |
| 254 | /* get shm fd */ |
| 255 | fd = ustcomm_recv_fd(sock); |
| 256 | if (fd < 0) |
| 257 | goto error; |
| 258 | stream_data->shm_fd = fd; |
| 259 | /* get wait fd */ |
| 260 | fd = ustcomm_recv_fd(sock); |
| 261 | if (fd < 0) |
| 262 | goto error; |
| 263 | stream_data->wait_fd = fd; |
| 264 | *_stream_data = stream_data; |
| 265 | return ret; |
| 266 | |
| 267 | error: |
| 268 | ustctl_release_object(sock, stream_data); |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
| 272 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
| 273 | struct lttng_ust_object_data *channel_data, |
| 274 | struct lttng_ust_object_data **_event_data) |
| 275 | { |
| 276 | struct ustcomm_ust_msg lum; |
| 277 | struct ustcomm_ust_reply lur; |
| 278 | struct lttng_ust_object_data *event_data; |
| 279 | int ret; |
| 280 | |
| 281 | event_data = malloc(sizeof(*event_data)); |
| 282 | if (!event_data) |
| 283 | return -ENOMEM; |
| 284 | init_object(event_data); |
| 285 | memset(&lum, 0, sizeof(lum)); |
| 286 | lum.handle = channel_data->handle; |
| 287 | lum.cmd = LTTNG_UST_EVENT; |
| 288 | strncpy(lum.u.event.name, ev->name, |
| 289 | LTTNG_UST_SYM_NAME_LEN); |
| 290 | lum.u.event.instrumentation = ev->instrumentation; |
| 291 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 292 | if (ret) { |
| 293 | free(event_data); |
| 294 | return ret; |
| 295 | } |
| 296 | event_data->handle = lur.ret_val; |
| 297 | DBG("received event handle %u", event_data->handle); |
| 298 | *_event_data = event_data; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | int ustctl_add_context(int sock, struct lttng_ust_context *ctx, |
| 303 | struct lttng_ust_object_data *obj_data, |
| 304 | struct lttng_ust_object_data **_context_data) |
| 305 | { |
| 306 | struct ustcomm_ust_msg lum; |
| 307 | struct ustcomm_ust_reply lur; |
| 308 | struct lttng_ust_object_data *context_data; |
| 309 | int ret; |
| 310 | |
| 311 | context_data = malloc(sizeof(*context_data)); |
| 312 | if (!context_data) |
| 313 | return -ENOMEM; |
| 314 | init_object(context_data); |
| 315 | memset(&lum, 0, sizeof(lum)); |
| 316 | lum.handle = obj_data->handle; |
| 317 | lum.cmd = LTTNG_UST_CONTEXT; |
| 318 | lum.u.context.ctx = ctx->ctx; |
| 319 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 320 | if (ret) { |
| 321 | free(context_data); |
| 322 | return ret; |
| 323 | } |
| 324 | context_data->handle = lur.ret_val; |
| 325 | DBG("received context handle %u", context_data->handle); |
| 326 | *_context_data = context_data; |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | /* Enable event, channel and session ioctl */ |
| 331 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
| 332 | { |
| 333 | struct ustcomm_ust_msg lum; |
| 334 | struct ustcomm_ust_reply lur; |
| 335 | int ret; |
| 336 | |
| 337 | memset(&lum, 0, sizeof(lum)); |
| 338 | lum.handle = object->handle; |
| 339 | lum.cmd = LTTNG_UST_ENABLE; |
| 340 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | DBG("enabled handle %u", object->handle); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* Disable event, channel and session ioctl */ |
| 348 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
| 349 | { |
| 350 | struct ustcomm_ust_msg lum; |
| 351 | struct ustcomm_ust_reply lur; |
| 352 | int ret; |
| 353 | |
| 354 | memset(&lum, 0, sizeof(lum)); |
| 355 | lum.handle = object->handle; |
| 356 | lum.cmd = LTTNG_UST_DISABLE; |
| 357 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 358 | if (ret) |
| 359 | return ret; |
| 360 | DBG("disable handle %u", object->handle); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int ustctl_start_session(int sock, int handle) |
| 365 | { |
| 366 | struct lttng_ust_object_data obj; |
| 367 | |
| 368 | obj.handle = handle; |
| 369 | return ustctl_enable(sock, &obj); |
| 370 | } |
| 371 | |
| 372 | int ustctl_stop_session(int sock, int handle) |
| 373 | { |
| 374 | struct lttng_ust_object_data obj; |
| 375 | |
| 376 | obj.handle = handle; |
| 377 | return ustctl_disable(sock, &obj); |
| 378 | } |
| 379 | |
| 380 | int ustctl_tracepoint_list(int sock) |
| 381 | { |
| 382 | struct ustcomm_ust_msg lum; |
| 383 | struct ustcomm_ust_reply lur; |
| 384 | int ret, tp_list_handle; |
| 385 | |
| 386 | memset(&lum, 0, sizeof(lum)); |
| 387 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 388 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; |
| 389 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | tp_list_handle = lur.ret_val; |
| 393 | DBG("received tracepoint list handle %u", tp_list_handle); |
| 394 | return tp_list_handle; |
| 395 | } |
| 396 | |
| 397 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, |
| 398 | char iter[LTTNG_UST_SYM_NAME_LEN]) |
| 399 | { |
| 400 | struct ustcomm_ust_msg lum; |
| 401 | struct ustcomm_ust_reply lur; |
| 402 | int ret; |
| 403 | |
| 404 | memset(&lum, 0, sizeof(lum)); |
| 405 | lum.handle = tp_list_handle; |
| 406 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; |
| 407 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 408 | if (ret) |
| 409 | return ret; |
| 410 | DBG("received tracepoint list entry %s", lur.u.tracepoint_list_entry); |
| 411 | memcpy(iter, lur.u.tracepoint_list_entry, LTTNG_UST_SYM_NAME_LEN); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
| 416 | { |
| 417 | struct ustcomm_ust_msg lum; |
| 418 | struct ustcomm_ust_reply lur; |
| 419 | int ret; |
| 420 | |
| 421 | memset(&lum, 0, sizeof(lum)); |
| 422 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 423 | lum.cmd = LTTNG_UST_TRACER_VERSION; |
| 424 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 425 | if (ret) |
| 426 | return ret; |
| 427 | memcpy(v, &lur.u.version, sizeof(*v)); |
| 428 | DBG("received tracer version"); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | int ustctl_wait_quiescent(int sock) |
| 433 | { |
| 434 | struct ustcomm_ust_msg lum; |
| 435 | struct ustcomm_ust_reply lur; |
| 436 | int ret; |
| 437 | |
| 438 | memset(&lum, 0, sizeof(lum)); |
| 439 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 440 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; |
| 441 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 442 | if (ret) |
| 443 | return ret; |
| 444 | DBG("waited for quiescent state"); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) |
| 449 | { |
| 450 | return -ENOSYS; |
| 451 | } |
| 452 | |
| 453 | /* Buffer operations */ |
| 454 | |
| 455 | /* Map channel shm into process memory */ |
| 456 | struct lttng_ust_shm_handle *ustctl_map_channel(struct lttng_ust_object_data *chan_data) |
| 457 | { |
| 458 | struct lttng_ust_shm_handle *handle; |
| 459 | struct channel *chan; |
| 460 | size_t chan_size; |
| 461 | struct lttng_ust_lib_ring_buffer_config *config; |
| 462 | int ret; |
| 463 | |
| 464 | handle = channel_handle_create(chan_data->shm_fd, |
| 465 | chan_data->wait_fd, |
| 466 | chan_data->memory_map_size); |
| 467 | if (!handle) { |
| 468 | ERR("create handle error"); |
| 469 | return NULL; |
| 470 | } |
| 471 | /* |
| 472 | * Set to -1 because the lttng_ust_shm_handle destruction will take care |
| 473 | * of closing shm_fd and wait_fd. |
| 474 | */ |
| 475 | chan_data->shm_fd = -1; |
| 476 | chan_data->wait_fd = -1; |
| 477 | |
| 478 | /* |
| 479 | * TODO: add consistency checks to be resilient if the |
| 480 | * application try to feed us with incoherent channel structure |
| 481 | * values. |
| 482 | */ |
| 483 | chan = shmp(handle, handle->chan); |
| 484 | /* chan is object 0. This is hardcoded. */ |
| 485 | chan_size = handle->table->objects[0].allocated_len; |
| 486 | handle->shadow_chan = malloc(chan_size); |
| 487 | if (!handle->shadow_chan) { |
| 488 | channel_destroy(chan, handle, 1); |
| 489 | return NULL; |
| 490 | } |
| 491 | memcpy(handle->shadow_chan, chan, chan_size); |
| 492 | /* |
| 493 | * The callback pointers in the producer are invalid in the |
| 494 | * consumer. We need to look them up here. |
| 495 | */ |
| 496 | config = &handle->shadow_chan->backend.config; |
| 497 | switch (config->client_type) { |
| 498 | case LTTNG_CLIENT_METADATA: |
| 499 | memcpy(&config->cb, lttng_client_callbacks_metadata, |
| 500 | sizeof(config->cb)); |
| 501 | break; |
| 502 | case LTTNG_CLIENT_DISCARD: |
| 503 | memcpy(&config->cb, lttng_client_callbacks_discard, |
| 504 | sizeof(config->cb)); |
| 505 | break; |
| 506 | case LTTNG_CLIENT_OVERWRITE: |
| 507 | memcpy(&config->cb, lttng_client_callbacks_overwrite, |
| 508 | sizeof(config->cb)); |
| 509 | break; |
| 510 | default: |
| 511 | ERR("Unknown client type %d", config->client_type); |
| 512 | channel_destroy(chan, handle, 1); |
| 513 | return NULL; |
| 514 | } |
| 515 | /* Replace the object table pointer. */ |
| 516 | ret = munmap(handle->table->objects[0].memory_map, |
| 517 | handle->table->objects[0].memory_map_size); |
| 518 | if (ret) { |
| 519 | perror("munmap"); |
| 520 | assert(0); |
| 521 | } |
| 522 | handle->table->objects[0].memory_map = (char *) handle->shadow_chan; |
| 523 | handle->table->objects[0].is_shadow = 1; |
| 524 | return handle; |
| 525 | } |
| 526 | |
| 527 | /* Add stream to channel shm and map its shm into process memory */ |
| 528 | int ustctl_add_stream(struct lttng_ust_shm_handle *handle, |
| 529 | struct lttng_ust_object_data *stream_data) |
| 530 | { |
| 531 | int ret; |
| 532 | |
| 533 | if (!stream_data->handle) |
| 534 | return -ENOENT; |
| 535 | /* map stream */ |
| 536 | ret = channel_handle_add_stream(handle, |
| 537 | stream_data->shm_fd, |
| 538 | stream_data->wait_fd, |
| 539 | stream_data->memory_map_size); |
| 540 | if (ret) { |
| 541 | ERR("add stream error\n"); |
| 542 | return ret; |
| 543 | } |
| 544 | /* |
| 545 | * Set to -1 because the lttng_ust_shm_handle destruction will take care |
| 546 | * of closing shm_fd and wait_fd. |
| 547 | */ |
| 548 | stream_data->shm_fd = -1; |
| 549 | stream_data->wait_fd = -1; |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | void ustctl_unmap_channel(struct lttng_ust_shm_handle *handle) |
| 554 | { |
| 555 | struct channel *chan; |
| 556 | |
| 557 | chan = shmp(handle, handle->chan); |
| 558 | channel_destroy(chan, handle, 1); |
| 559 | } |
| 560 | |
| 561 | struct lttng_ust_lib_ring_buffer *ustctl_open_stream_read(struct lttng_ust_shm_handle *handle, |
| 562 | int cpu) |
| 563 | { |
| 564 | struct channel *chan = handle->shadow_chan; |
| 565 | int shm_fd, wait_fd; |
| 566 | uint64_t memory_map_size; |
| 567 | struct lttng_ust_lib_ring_buffer *buf; |
| 568 | int ret; |
| 569 | |
| 570 | buf = channel_get_ring_buffer(&chan->backend.config, |
| 571 | chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size); |
| 572 | if (!buf) |
| 573 | return NULL; |
| 574 | ret = lib_ring_buffer_open_read(buf, handle, 1); |
| 575 | if (ret) |
| 576 | return NULL; |
| 577 | return buf; |
| 578 | } |
| 579 | |
| 580 | void ustctl_close_stream_read(struct lttng_ust_shm_handle *handle, |
| 581 | struct lttng_ust_lib_ring_buffer *buf) |
| 582 | { |
| 583 | lib_ring_buffer_release_read(buf, handle, 1); |
| 584 | } |
| 585 | |
| 586 | /* For mmap mode, readable without "get" operation */ |
| 587 | |
| 588 | void *ustctl_get_mmap_base(struct lttng_ust_shm_handle *handle, |
| 589 | struct lttng_ust_lib_ring_buffer *buf) |
| 590 | { |
| 591 | return shmp(handle, buf->backend.memory_map); |
| 592 | } |
| 593 | |
| 594 | /* returns the length to mmap. */ |
| 595 | int ustctl_get_mmap_len(struct lttng_ust_shm_handle *handle, |
| 596 | struct lttng_ust_lib_ring_buffer *buf, |
| 597 | unsigned long *len) |
| 598 | { |
| 599 | unsigned long mmap_buf_len; |
| 600 | struct channel *chan = handle->shadow_chan; |
| 601 | |
| 602 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 603 | return -EINVAL; |
| 604 | mmap_buf_len = chan->backend.buf_size; |
| 605 | if (chan->backend.extra_reader_sb) |
| 606 | mmap_buf_len += chan->backend.subbuf_size; |
| 607 | if (mmap_buf_len > INT_MAX) |
| 608 | return -EFBIG; |
| 609 | *len = mmap_buf_len; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* returns the maximum size for sub-buffers. */ |
| 614 | int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle *handle, |
| 615 | struct lttng_ust_lib_ring_buffer *buf, |
| 616 | unsigned long *len) |
| 617 | { |
| 618 | struct channel *chan = handle->shadow_chan; |
| 619 | |
| 620 | *len = chan->backend.subbuf_size; |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * For mmap mode, operate on the current packet (between get/put or |
| 626 | * get_next/put_next). |
| 627 | */ |
| 628 | |
| 629 | /* returns the offset of the subbuffer belonging to the mmap reader. */ |
| 630 | int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle, |
| 631 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *off) |
| 632 | { |
| 633 | struct channel *chan = handle->shadow_chan; |
| 634 | unsigned long sb_bindex; |
| 635 | |
| 636 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 637 | return -EINVAL; |
| 638 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, |
| 639 | buf->backend.buf_rsb.id); |
| 640 | *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset; |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 645 | int ustctl_get_subbuf_size(struct lttng_ust_shm_handle *handle, |
| 646 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *len) |
| 647 | { |
| 648 | struct channel *chan = handle->shadow_chan; |
| 649 | |
| 650 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 651 | handle); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 656 | int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle *handle, |
| 657 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *len) |
| 658 | { |
| 659 | struct channel *chan = handle->shadow_chan; |
| 660 | |
| 661 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 662 | handle); |
| 663 | *len = PAGE_ALIGN(*len); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /* Get exclusive read access to the next sub-buffer that can be read. */ |
| 668 | int ustctl_get_next_subbuf(struct lttng_ust_shm_handle *handle, |
| 669 | struct lttng_ust_lib_ring_buffer *buf) |
| 670 | { |
| 671 | return lib_ring_buffer_get_next_subbuf(buf, handle); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | /* Release exclusive sub-buffer access, move consumer forward. */ |
| 676 | int ustctl_put_next_subbuf(struct lttng_ust_shm_handle *handle, |
| 677 | struct lttng_ust_lib_ring_buffer *buf) |
| 678 | { |
| 679 | lib_ring_buffer_put_next_subbuf(buf, handle); |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | /* snapshot */ |
| 684 | |
| 685 | /* Get a snapshot of the current ring buffer producer and consumer positions */ |
| 686 | int ustctl_snapshot(struct lttng_ust_shm_handle *handle, |
| 687 | struct lttng_ust_lib_ring_buffer *buf) |
| 688 | { |
| 689 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 690 | &buf->prod_snapshot, handle); |
| 691 | } |
| 692 | |
| 693 | /* Get the consumer position (iteration start) */ |
| 694 | int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle *handle, |
| 695 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
| 696 | { |
| 697 | *pos = buf->cons_snapshot; |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* Get the producer position (iteration end) */ |
| 702 | int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle *handle, |
| 703 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
| 704 | { |
| 705 | *pos = buf->prod_snapshot; |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /* Get exclusive read access to the specified sub-buffer position */ |
| 710 | int ustctl_get_subbuf(struct lttng_ust_shm_handle *handle, |
| 711 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
| 712 | { |
| 713 | return lib_ring_buffer_get_subbuf(buf, *pos, handle); |
| 714 | } |
| 715 | |
| 716 | /* Release exclusive sub-buffer access */ |
| 717 | int ustctl_put_subbuf(struct lttng_ust_shm_handle *handle, |
| 718 | struct lttng_ust_lib_ring_buffer *buf) |
| 719 | { |
| 720 | lib_ring_buffer_put_subbuf(buf, handle); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | void ustctl_flush_buffer(struct lttng_ust_shm_handle *handle, |
| 725 | struct lttng_ust_lib_ring_buffer *buf, |
| 726 | int producer_active) |
| 727 | { |
| 728 | lib_ring_buffer_switch_slow(buf, |
| 729 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, |
| 730 | handle); |
| 731 | } |