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