| 1 | /* |
| 2 | * lttng-ust-abi.c |
| 3 | * |
| 4 | * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng UST ABI |
| 7 | * |
| 8 | * Mimic system calls for: |
| 9 | * - session creation, returns an object descriptor or failure. |
| 10 | * - channel creation, returns an object descriptor or failure. |
| 11 | * - Operates on a session object descriptor |
| 12 | * - Takes all channel options as parameters. |
| 13 | * - stream get, returns an object descriptor or failure. |
| 14 | * - Operates on a channel object descriptor. |
| 15 | * - stream notifier get, returns an object descriptor or failure. |
| 16 | * - Operates on a channel object descriptor. |
| 17 | * - event creation, returns an object descriptor or failure. |
| 18 | * - Operates on a channel object descriptor |
| 19 | * - Takes an event name as parameter |
| 20 | * - Takes an instrumentation source as parameter |
| 21 | * - e.g. tracepoints, dynamic_probes... |
| 22 | * - Takes instrumentation source specific arguments. |
| 23 | * |
| 24 | * Dual LGPL v2.1/GPL v2 license. |
| 25 | */ |
| 26 | |
| 27 | #include <lttng/ust-abi.h> |
| 28 | #include <urcu/compiler.h> |
| 29 | #include <urcu/list.h> |
| 30 | #include <lttng/ust-events.h> |
| 31 | #include <lttng/usterr-signal-safe.h> |
| 32 | #include "lttng/core.h" |
| 33 | #include "ltt-tracer.h" |
| 34 | #include "tracepoint-internal.h" |
| 35 | |
| 36 | struct ltt_tracepoint_list { |
| 37 | struct tracepoint_iter iter; |
| 38 | int got_first; |
| 39 | }; |
| 40 | |
| 41 | static int lttng_ust_abi_close_in_progress; |
| 42 | |
| 43 | static |
| 44 | int lttng_abi_tracepoint_list(void); |
| 45 | |
| 46 | /* |
| 47 | * Object descriptor table. Should be protected from concurrent access |
| 48 | * by the caller. |
| 49 | */ |
| 50 | |
| 51 | struct lttng_ust_obj { |
| 52 | union { |
| 53 | struct { |
| 54 | void *private_data; |
| 55 | const struct lttng_ust_objd_ops *ops; |
| 56 | int f_count; |
| 57 | } s; |
| 58 | int freelist_next; /* offset freelist. end is -1. */ |
| 59 | } u; |
| 60 | }; |
| 61 | |
| 62 | struct lttng_ust_objd_table { |
| 63 | struct lttng_ust_obj *array; |
| 64 | unsigned int len, allocated_len; |
| 65 | int freelist_head; /* offset freelist head. end is -1 */ |
| 66 | }; |
| 67 | |
| 68 | static struct lttng_ust_objd_table objd_table = { |
| 69 | .freelist_head = -1, |
| 70 | }; |
| 71 | |
| 72 | static |
| 73 | int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops) |
| 74 | { |
| 75 | struct lttng_ust_obj *obj; |
| 76 | |
| 77 | if (objd_table.freelist_head != -1) { |
| 78 | obj = &objd_table.array[objd_table.freelist_head]; |
| 79 | objd_table.freelist_head = obj->u.freelist_next; |
| 80 | goto end; |
| 81 | } |
| 82 | |
| 83 | if (objd_table.len >= objd_table.allocated_len) { |
| 84 | unsigned int new_allocated_len, old_allocated_len; |
| 85 | struct lttng_ust_obj *new_table, *old_table; |
| 86 | |
| 87 | old_allocated_len = objd_table.allocated_len; |
| 88 | old_table = objd_table.array; |
| 89 | if (!old_allocated_len) |
| 90 | new_allocated_len = 1; |
| 91 | else |
| 92 | new_allocated_len = old_allocated_len << 1; |
| 93 | new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len); |
| 94 | if (!new_table) |
| 95 | return -ENOMEM; |
| 96 | memcpy(new_table, old_table, |
| 97 | sizeof(struct lttng_ust_obj) * old_allocated_len); |
| 98 | free(old_table); |
| 99 | objd_table.array = new_table; |
| 100 | objd_table.allocated_len = new_allocated_len; |
| 101 | } |
| 102 | obj = &objd_table.array[objd_table.len]; |
| 103 | objd_table.len++; |
| 104 | end: |
| 105 | obj->u.s.private_data = private_data; |
| 106 | obj->u.s.ops = ops; |
| 107 | obj->u.s.f_count = 2; /* count == 1 : object is allocated */ |
| 108 | /* count == 2 : allocated + hold ref */ |
| 109 | return obj - objd_table.array; |
| 110 | } |
| 111 | |
| 112 | static |
| 113 | struct lttng_ust_obj *_objd_get(int id) |
| 114 | { |
| 115 | if (id >= objd_table.len) |
| 116 | return NULL; |
| 117 | if (!objd_table.array[id].u.s.f_count) |
| 118 | return NULL; |
| 119 | return &objd_table.array[id]; |
| 120 | } |
| 121 | |
| 122 | static |
| 123 | void *objd_private(int id) |
| 124 | { |
| 125 | struct lttng_ust_obj *obj = _objd_get(id); |
| 126 | assert(obj); |
| 127 | return obj->u.s.private_data; |
| 128 | } |
| 129 | |
| 130 | static |
| 131 | void objd_set_private(int id, void *private_data) |
| 132 | { |
| 133 | struct lttng_ust_obj *obj = _objd_get(id); |
| 134 | assert(obj); |
| 135 | obj->u.s.private_data = private_data; |
| 136 | } |
| 137 | |
| 138 | const struct lttng_ust_objd_ops *objd_ops(int id) |
| 139 | { |
| 140 | struct lttng_ust_obj *obj = _objd_get(id); |
| 141 | |
| 142 | if (!obj) |
| 143 | return NULL; |
| 144 | return obj->u.s.ops; |
| 145 | } |
| 146 | |
| 147 | static |
| 148 | void objd_free(int id) |
| 149 | { |
| 150 | struct lttng_ust_obj *obj = _objd_get(id); |
| 151 | |
| 152 | assert(obj); |
| 153 | obj->u.freelist_next = objd_table.freelist_head; |
| 154 | objd_table.freelist_head = obj - objd_table.array; |
| 155 | assert(obj->u.s.f_count == 1); |
| 156 | obj->u.s.f_count = 0; /* deallocated */ |
| 157 | } |
| 158 | |
| 159 | static |
| 160 | void objd_ref(int id) |
| 161 | { |
| 162 | struct lttng_ust_obj *obj = _objd_get(id); |
| 163 | obj->u.s.f_count++; |
| 164 | } |
| 165 | |
| 166 | int lttng_ust_objd_unref(int id) |
| 167 | { |
| 168 | struct lttng_ust_obj *obj = _objd_get(id); |
| 169 | |
| 170 | if (!obj) |
| 171 | return -EINVAL; |
| 172 | if (obj->u.s.f_count == 1) { |
| 173 | ERR("Reference counting error\n"); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | if ((--obj->u.s.f_count) == 1) { |
| 177 | const struct lttng_ust_objd_ops *ops = objd_ops(id); |
| 178 | |
| 179 | if (ops->release) |
| 180 | ops->release(id); |
| 181 | objd_free(id); |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static |
| 187 | void objd_table_destroy(void) |
| 188 | { |
| 189 | int i; |
| 190 | |
| 191 | for (i = 0; i < objd_table.allocated_len; i++) |
| 192 | (void) lttng_ust_objd_unref(i); |
| 193 | free(objd_table.array); |
| 194 | objd_table.array = NULL; |
| 195 | objd_table.len = 0; |
| 196 | objd_table.allocated_len = 0; |
| 197 | objd_table.freelist_head = -1; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * This is LTTng's own personal way to create an ABI for sessiond. |
| 202 | * We send commands over a socket. |
| 203 | */ |
| 204 | |
| 205 | static const struct lttng_ust_objd_ops lttng_ops; |
| 206 | static const struct lttng_ust_objd_ops lttng_session_ops; |
| 207 | static const struct lttng_ust_objd_ops lttng_channel_ops; |
| 208 | static const struct lttng_ust_objd_ops lttng_metadata_ops; |
| 209 | static const struct lttng_ust_objd_ops lttng_event_ops; |
| 210 | static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops; |
| 211 | static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops; |
| 212 | |
| 213 | enum channel_type { |
| 214 | PER_CPU_CHANNEL, |
| 215 | METADATA_CHANNEL, |
| 216 | }; |
| 217 | |
| 218 | int lttng_abi_create_root_handle(void) |
| 219 | { |
| 220 | int root_handle; |
| 221 | |
| 222 | root_handle = objd_alloc(NULL, <tng_ops); |
| 223 | return root_handle; |
| 224 | } |
| 225 | |
| 226 | static |
| 227 | int lttng_abi_create_session(void) |
| 228 | { |
| 229 | struct ltt_session *session; |
| 230 | int session_objd, ret; |
| 231 | |
| 232 | session = ltt_session_create(); |
| 233 | if (!session) |
| 234 | return -ENOMEM; |
| 235 | session_objd = objd_alloc(session, <tng_session_ops); |
| 236 | if (session_objd < 0) { |
| 237 | ret = session_objd; |
| 238 | goto objd_error; |
| 239 | } |
| 240 | session->objd = session_objd; |
| 241 | return session_objd; |
| 242 | |
| 243 | objd_error: |
| 244 | ltt_session_destroy(session); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static |
| 249 | long lttng_abi_tracer_version(int objd, |
| 250 | struct lttng_ust_tracer_version *v) |
| 251 | { |
| 252 | v->major = LTTNG_UST_MAJOR; |
| 253 | v->minor = LTTNG_UST_MINOR; |
| 254 | v->patchlevel = LTTNG_UST_PATCHLEVEL; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static |
| 259 | long lttng_abi_add_context(int objd, |
| 260 | struct lttng_ust_context *context_param, |
| 261 | struct lttng_ctx **ctx, struct ltt_session *session) |
| 262 | { |
| 263 | if (session->been_active) |
| 264 | return -EPERM; |
| 265 | |
| 266 | switch (context_param->ctx) { |
| 267 | case LTTNG_UST_CONTEXT_PTHREAD_ID: |
| 268 | return lttng_add_pthread_id_to_ctx(ctx); |
| 269 | case LTTNG_UST_CONTEXT_VTID: |
| 270 | return lttng_add_vtid_to_ctx(ctx); |
| 271 | case LTTNG_UST_CONTEXT_VPID: |
| 272 | return lttng_add_vpid_to_ctx(ctx); |
| 273 | case LTTNG_UST_CONTEXT_PROCNAME: |
| 274 | return lttng_add_procname_to_ctx(ctx); |
| 275 | default: |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * lttng_cmd - lttng control through socket commands |
| 282 | * |
| 283 | * @objd: the object descriptor |
| 284 | * @cmd: the command |
| 285 | * @arg: command arg |
| 286 | * |
| 287 | * This descriptor implements lttng commands: |
| 288 | * LTTNG_UST_SESSION |
| 289 | * Returns a LTTng trace session object descriptor |
| 290 | * LTTNG_UST_TRACER_VERSION |
| 291 | * Returns the LTTng kernel tracer version |
| 292 | * LTTNG_UST_TRACEPOINT_LIST |
| 293 | * Returns a file descriptor listing available tracepoints |
| 294 | * LTTNG_UST_WAIT_QUIESCENT |
| 295 | * Returns after all previously running probes have completed |
| 296 | * |
| 297 | * The returned session will be deleted when its file descriptor is closed. |
| 298 | */ |
| 299 | static |
| 300 | long lttng_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 301 | { |
| 302 | switch (cmd) { |
| 303 | case LTTNG_UST_SESSION: |
| 304 | return lttng_abi_create_session(); |
| 305 | case LTTNG_UST_TRACER_VERSION: |
| 306 | return lttng_abi_tracer_version(objd, |
| 307 | (struct lttng_ust_tracer_version *) arg); |
| 308 | case LTTNG_UST_TRACEPOINT_LIST: |
| 309 | return lttng_abi_tracepoint_list(); |
| 310 | case LTTNG_UST_WAIT_QUIESCENT: |
| 311 | synchronize_trace(); |
| 312 | return 0; |
| 313 | default: |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static const struct lttng_ust_objd_ops lttng_ops = { |
| 319 | .cmd = lttng_cmd, |
| 320 | }; |
| 321 | |
| 322 | /* |
| 323 | * We tolerate no failure in this function (if one happens, we print a dmesg |
| 324 | * error, but cannot return any error, because the channel information is |
| 325 | * invariant. |
| 326 | */ |
| 327 | static |
| 328 | void lttng_metadata_create_events(int channel_objd) |
| 329 | { |
| 330 | struct ltt_channel *channel = objd_private(channel_objd); |
| 331 | static struct lttng_ust_event metadata_params = { |
| 332 | .instrumentation = LTTNG_UST_TRACEPOINT, |
| 333 | .name = "lttng_ust:metadata", |
| 334 | }; |
| 335 | struct ltt_event *event; |
| 336 | |
| 337 | /* |
| 338 | * We tolerate no failure path after event creation. It will stay |
| 339 | * invariant for the rest of the session. |
| 340 | */ |
| 341 | event = ltt_event_create(channel, &metadata_params, NULL); |
| 342 | if (!event) { |
| 343 | goto create_error; |
| 344 | } |
| 345 | return; |
| 346 | |
| 347 | create_error: |
| 348 | WARN_ON(1); |
| 349 | return; /* not allowed to return error */ |
| 350 | } |
| 351 | |
| 352 | int lttng_abi_create_channel(int session_objd, |
| 353 | struct lttng_ust_channel *chan_param, |
| 354 | enum channel_type channel_type) |
| 355 | { |
| 356 | struct ltt_session *session = objd_private(session_objd); |
| 357 | const struct lttng_ust_objd_ops *ops; |
| 358 | const char *transport_name; |
| 359 | struct ltt_channel *chan; |
| 360 | int chan_objd; |
| 361 | int ret = 0; |
| 362 | struct ltt_channel chan_priv_init; |
| 363 | |
| 364 | switch (channel_type) { |
| 365 | case PER_CPU_CHANNEL: |
| 366 | if (chan_param->output == LTTNG_UST_MMAP) { |
| 367 | transport_name = chan_param->overwrite ? |
| 368 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 369 | } else { |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | ops = <tng_channel_ops; |
| 373 | break; |
| 374 | case METADATA_CHANNEL: |
| 375 | if (chan_param->output == LTTNG_UST_MMAP) |
| 376 | transport_name = "relay-metadata-mmap"; |
| 377 | else |
| 378 | return -EINVAL; |
| 379 | ops = <tng_metadata_ops; |
| 380 | break; |
| 381 | default: |
| 382 | transport_name = "<unknown>"; |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | chan_objd = objd_alloc(NULL, ops); |
| 386 | if (chan_objd < 0) { |
| 387 | ret = chan_objd; |
| 388 | goto objd_error; |
| 389 | } |
| 390 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
| 391 | /* Copy of session UUID for consumer (availability through shm) */ |
| 392 | memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid)); |
| 393 | |
| 394 | /* |
| 395 | * We tolerate no failure path after channel creation. It will stay |
| 396 | * invariant for the rest of the session. |
| 397 | */ |
| 398 | chan = ltt_channel_create(session, transport_name, NULL, |
| 399 | chan_param->subbuf_size, |
| 400 | chan_param->num_subbuf, |
| 401 | chan_param->switch_timer_interval, |
| 402 | chan_param->read_timer_interval, |
| 403 | &chan_param->shm_fd, |
| 404 | &chan_param->wait_fd, |
| 405 | &chan_param->memory_map_size, |
| 406 | &chan_priv_init); |
| 407 | if (!chan) { |
| 408 | ret = -EINVAL; |
| 409 | goto chan_error; |
| 410 | } |
| 411 | objd_set_private(chan_objd, chan); |
| 412 | chan->objd = chan_objd; |
| 413 | if (channel_type == METADATA_CHANNEL) { |
| 414 | session->metadata = chan; |
| 415 | lttng_metadata_create_events(chan_objd); |
| 416 | } |
| 417 | /* The channel created holds a reference on the session */ |
| 418 | objd_ref(session_objd); |
| 419 | |
| 420 | return chan_objd; |
| 421 | |
| 422 | chan_error: |
| 423 | { |
| 424 | int err; |
| 425 | |
| 426 | err = lttng_ust_objd_unref(chan_objd); |
| 427 | assert(!err); |
| 428 | } |
| 429 | objd_error: |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * lttng_session_cmd - lttng session object command |
| 435 | * |
| 436 | * @obj: the object |
| 437 | * @cmd: the command |
| 438 | * @arg: command arg |
| 439 | * |
| 440 | * This descriptor implements lttng commands: |
| 441 | * LTTNG_UST_CHANNEL |
| 442 | * Returns a LTTng channel object descriptor |
| 443 | * LTTNG_UST_ENABLE |
| 444 | * Enables tracing for a session (weak enable) |
| 445 | * LTTNG_UST_DISABLE |
| 446 | * Disables tracing for a session (strong disable) |
| 447 | * LTTNG_UST_METADATA |
| 448 | * Returns a LTTng metadata object descriptor |
| 449 | * |
| 450 | * The returned channel will be deleted when its file descriptor is closed. |
| 451 | */ |
| 452 | static |
| 453 | long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 454 | { |
| 455 | struct ltt_session *session = objd_private(objd); |
| 456 | |
| 457 | switch (cmd) { |
| 458 | case LTTNG_UST_CHANNEL: |
| 459 | return lttng_abi_create_channel(objd, |
| 460 | (struct lttng_ust_channel *) arg, |
| 461 | PER_CPU_CHANNEL); |
| 462 | case LTTNG_UST_SESSION_START: |
| 463 | case LTTNG_UST_ENABLE: |
| 464 | return ltt_session_enable(session); |
| 465 | case LTTNG_UST_SESSION_STOP: |
| 466 | case LTTNG_UST_DISABLE: |
| 467 | return ltt_session_disable(session); |
| 468 | case LTTNG_UST_METADATA: |
| 469 | return lttng_abi_create_channel(objd, |
| 470 | (struct lttng_ust_channel *) arg, |
| 471 | METADATA_CHANNEL); |
| 472 | default: |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Called when the last file reference is dropped. |
| 479 | * |
| 480 | * Big fat note: channels and events are invariant for the whole session after |
| 481 | * their creation. So this session destruction also destroys all channel and |
| 482 | * event structures specific to this session (they are not destroyed when their |
| 483 | * individual file is released). |
| 484 | */ |
| 485 | static |
| 486 | int lttng_release_session(int objd) |
| 487 | { |
| 488 | struct ltt_session *session = objd_private(objd); |
| 489 | |
| 490 | if (session) { |
| 491 | ltt_session_destroy(session); |
| 492 | return 0; |
| 493 | } else { |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static const struct lttng_ust_objd_ops lttng_session_ops = { |
| 499 | .release = lttng_release_session, |
| 500 | .cmd = lttng_session_cmd, |
| 501 | }; |
| 502 | |
| 503 | /* |
| 504 | * beware: we don't keep the mutex over the send, but we must walk the |
| 505 | * whole list each time we are called again. So sending one tracepoint |
| 506 | * at a time means this is O(n^2). TODO: do as in the kernel and send |
| 507 | * multiple tracepoints for each call to amortize this cost. |
| 508 | */ |
| 509 | static |
| 510 | void ltt_tracepoint_list_get(struct ltt_tracepoint_list *list, |
| 511 | char *tp_list_entry) |
| 512 | { |
| 513 | next: |
| 514 | if (!list->got_first) { |
| 515 | tracepoint_iter_start(&list->iter); |
| 516 | list->got_first = 1; |
| 517 | goto copy; |
| 518 | } |
| 519 | tracepoint_iter_next(&list->iter); |
| 520 | copy: |
| 521 | if (!list->iter.tracepoint) { |
| 522 | tp_list_entry[0] = '\0'; /* end of list */ |
| 523 | } else { |
| 524 | if (!strcmp((*list->iter.tracepoint)->name, |
| 525 | "lttng_ust:metadata")) |
| 526 | goto next; |
| 527 | memcpy(tp_list_entry, (*list->iter.tracepoint)->name, |
| 528 | LTTNG_UST_SYM_NAME_LEN); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static |
| 533 | long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 534 | { |
| 535 | struct ltt_tracepoint_list *list = objd_private(objd); |
| 536 | char *str = (char *) arg; |
| 537 | |
| 538 | switch (cmd) { |
| 539 | case LTTNG_UST_TRACEPOINT_LIST_GET: |
| 540 | ltt_tracepoint_list_get(list, str); |
| 541 | if (str[0] == '\0') |
| 542 | return -ENOENT; |
| 543 | return 0; |
| 544 | default: |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static |
| 550 | int lttng_abi_tracepoint_list(void) |
| 551 | { |
| 552 | int list_objd, ret; |
| 553 | struct ltt_tracepoint_list *list; |
| 554 | |
| 555 | list_objd = objd_alloc(NULL, <tng_tracepoint_list_ops); |
| 556 | if (list_objd < 0) { |
| 557 | ret = list_objd; |
| 558 | goto objd_error; |
| 559 | } |
| 560 | list = zmalloc(sizeof(*list)); |
| 561 | if (!list) { |
| 562 | ret = -ENOMEM; |
| 563 | goto alloc_error; |
| 564 | } |
| 565 | objd_set_private(list_objd, list); |
| 566 | |
| 567 | return list_objd; |
| 568 | |
| 569 | alloc_error: |
| 570 | { |
| 571 | int err; |
| 572 | |
| 573 | err = lttng_ust_objd_unref(list_objd); |
| 574 | assert(!err); |
| 575 | } |
| 576 | objd_error: |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static |
| 581 | int lttng_release_tracepoint_list(int objd) |
| 582 | { |
| 583 | struct ltt_tracepoint_list *list = objd_private(objd); |
| 584 | |
| 585 | if (list) { |
| 586 | tracepoint_iter_stop(&list->iter); |
| 587 | free(list); |
| 588 | return 0; |
| 589 | } else { |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = { |
| 595 | .release = lttng_release_tracepoint_list, |
| 596 | .cmd = lttng_tracepoint_list_cmd, |
| 597 | }; |
| 598 | |
| 599 | struct stream_priv_data { |
| 600 | struct lttng_ust_lib_ring_buffer *buf; |
| 601 | struct ltt_channel *ltt_chan; |
| 602 | }; |
| 603 | |
| 604 | static |
| 605 | int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info) |
| 606 | { |
| 607 | struct ltt_channel *channel = objd_private(channel_objd); |
| 608 | struct lttng_ust_lib_ring_buffer *buf; |
| 609 | struct stream_priv_data *priv; |
| 610 | int stream_objd, ret; |
| 611 | |
| 612 | buf = channel->ops->buffer_read_open(channel->chan, channel->handle, |
| 613 | &info->shm_fd, &info->wait_fd, &info->memory_map_size); |
| 614 | if (!buf) |
| 615 | return -ENOENT; |
| 616 | |
| 617 | priv = zmalloc(sizeof(*priv)); |
| 618 | if (!priv) { |
| 619 | ret = -ENOMEM; |
| 620 | goto alloc_error; |
| 621 | } |
| 622 | priv->buf = buf; |
| 623 | priv->ltt_chan = channel; |
| 624 | stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops); |
| 625 | if (stream_objd < 0) { |
| 626 | ret = stream_objd; |
| 627 | goto objd_error; |
| 628 | } |
| 629 | /* Hold a reference on the channel object descriptor */ |
| 630 | objd_ref(channel_objd); |
| 631 | return stream_objd; |
| 632 | |
| 633 | objd_error: |
| 634 | free(priv); |
| 635 | alloc_error: |
| 636 | channel->ops->buffer_read_close(buf, channel->handle); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | static |
| 641 | int lttng_abi_create_event(int channel_objd, |
| 642 | struct lttng_ust_event *event_param) |
| 643 | { |
| 644 | struct ltt_channel *channel = objd_private(channel_objd); |
| 645 | struct ltt_event *event; |
| 646 | int event_objd, ret; |
| 647 | |
| 648 | event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 649 | event_objd = objd_alloc(NULL, <tng_event_ops); |
| 650 | if (event_objd < 0) { |
| 651 | ret = event_objd; |
| 652 | goto objd_error; |
| 653 | } |
| 654 | /* |
| 655 | * We tolerate no failure path after event creation. It will stay |
| 656 | * invariant for the rest of the session. |
| 657 | */ |
| 658 | event = ltt_event_create(channel, event_param, NULL); |
| 659 | if (!event) { |
| 660 | ret = -EINVAL; |
| 661 | goto event_error; |
| 662 | } |
| 663 | objd_set_private(event_objd, event); |
| 664 | /* The event holds a reference on the channel */ |
| 665 | objd_ref(channel_objd); |
| 666 | return event_objd; |
| 667 | |
| 668 | event_error: |
| 669 | { |
| 670 | int err; |
| 671 | |
| 672 | err = lttng_ust_objd_unref(event_objd); |
| 673 | assert(!err); |
| 674 | } |
| 675 | objd_error: |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * lttng_channel_cmd - lttng control through object descriptors |
| 681 | * |
| 682 | * @objd: the object descriptor |
| 683 | * @cmd: the command |
| 684 | * @arg: command arg |
| 685 | * |
| 686 | * This object descriptor implements lttng commands: |
| 687 | * LTTNG_UST_STREAM |
| 688 | * Returns an event stream object descriptor or failure. |
| 689 | * (typically, one event stream records events from one CPU) |
| 690 | * LTTNG_UST_EVENT |
| 691 | * Returns an event object descriptor or failure. |
| 692 | * LTTNG_UST_CONTEXT |
| 693 | * Prepend a context field to each event in the channel |
| 694 | * LTTNG_UST_ENABLE |
| 695 | * Enable recording for events in this channel (weak enable) |
| 696 | * LTTNG_UST_DISABLE |
| 697 | * Disable recording for events in this channel (strong disable) |
| 698 | * |
| 699 | * Channel and event file descriptors also hold a reference on the session. |
| 700 | */ |
| 701 | static |
| 702 | long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 703 | { |
| 704 | struct ltt_channel *channel = objd_private(objd); |
| 705 | |
| 706 | switch (cmd) { |
| 707 | case LTTNG_UST_STREAM: |
| 708 | { |
| 709 | struct lttng_ust_stream *stream; |
| 710 | |
| 711 | stream = (struct lttng_ust_stream *) arg; |
| 712 | /* stream used as output */ |
| 713 | return lttng_abi_open_stream(objd, stream); |
| 714 | } |
| 715 | case LTTNG_UST_EVENT: |
| 716 | return lttng_abi_create_event(objd, (struct lttng_ust_event *) arg); |
| 717 | case LTTNG_UST_CONTEXT: |
| 718 | return lttng_abi_add_context(objd, |
| 719 | (struct lttng_ust_context *) arg, |
| 720 | &channel->ctx, channel->session); |
| 721 | case LTTNG_UST_ENABLE: |
| 722 | return ltt_channel_enable(channel); |
| 723 | case LTTNG_UST_DISABLE: |
| 724 | return ltt_channel_disable(channel); |
| 725 | case LTTNG_UST_FLUSH_BUFFER: |
| 726 | return channel->ops->flush_buffer(channel->chan, channel->handle); |
| 727 | default: |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * lttng_metadata_cmd - lttng control through object descriptors |
| 734 | * |
| 735 | * @objd: the object descriptor |
| 736 | * @cmd: the command |
| 737 | * @arg: command arg |
| 738 | * |
| 739 | * This object descriptor implements lttng commands: |
| 740 | * LTTNG_UST_STREAM |
| 741 | * Returns an event stream file descriptor or failure. |
| 742 | * |
| 743 | * Channel and event file descriptors also hold a reference on the session. |
| 744 | */ |
| 745 | static |
| 746 | long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 747 | { |
| 748 | struct ltt_channel *channel = objd_private(objd); |
| 749 | |
| 750 | switch (cmd) { |
| 751 | case LTTNG_UST_STREAM: |
| 752 | { |
| 753 | struct lttng_ust_stream *stream; |
| 754 | |
| 755 | stream = (struct lttng_ust_stream *) arg; |
| 756 | /* stream used as output */ |
| 757 | return lttng_abi_open_stream(objd, stream); |
| 758 | } |
| 759 | case LTTNG_UST_FLUSH_BUFFER: |
| 760 | return channel->ops->flush_buffer(channel->chan, channel->handle); |
| 761 | default: |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | #if 0 |
| 767 | /** |
| 768 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 769 | * |
| 770 | * @file: the file |
| 771 | * @wait: poll table |
| 772 | */ |
| 773 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 774 | { |
| 775 | struct ltt_channel *channel = file->private_data; |
| 776 | unsigned int mask = 0; |
| 777 | |
| 778 | if (file->f_mode & FMODE_READ) { |
| 779 | poll_wait_set_exclusive(wait); |
| 780 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 781 | wait); |
| 782 | |
| 783 | if (channel->ops->is_disabled(channel->chan)) |
| 784 | return POLLERR; |
| 785 | if (channel->ops->is_finalized(channel->chan)) |
| 786 | return POLLHUP; |
| 787 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 788 | return POLLIN | POLLRDNORM; |
| 789 | return 0; |
| 790 | } |
| 791 | return mask; |
| 792 | |
| 793 | } |
| 794 | #endif //0 |
| 795 | |
| 796 | static |
| 797 | int lttng_channel_release(int objd) |
| 798 | { |
| 799 | struct ltt_channel *channel = objd_private(objd); |
| 800 | |
| 801 | if (channel) |
| 802 | return lttng_ust_objd_unref(channel->session->objd); |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static const struct lttng_ust_objd_ops lttng_channel_ops = { |
| 807 | .release = lttng_channel_release, |
| 808 | //.poll = lttng_channel_poll, |
| 809 | .cmd = lttng_channel_cmd, |
| 810 | }; |
| 811 | |
| 812 | static const struct lttng_ust_objd_ops lttng_metadata_ops = { |
| 813 | .release = lttng_channel_release, |
| 814 | .cmd = lttng_metadata_cmd, |
| 815 | }; |
| 816 | |
| 817 | /** |
| 818 | * lttng_rb_cmd - lttng ring buffer control through object descriptors |
| 819 | * |
| 820 | * @objd: the object descriptor |
| 821 | * @cmd: the command |
| 822 | * @arg: command arg |
| 823 | * |
| 824 | * This object descriptor implements lttng commands: |
| 825 | * (None for now. Access is done directly though shm.) |
| 826 | */ |
| 827 | static |
| 828 | long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 829 | { |
| 830 | switch (cmd) { |
| 831 | default: |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static |
| 837 | int lttng_rb_release(int objd) |
| 838 | { |
| 839 | struct stream_priv_data *priv = objd_private(objd); |
| 840 | struct lttng_ust_lib_ring_buffer *buf; |
| 841 | struct ltt_channel *channel; |
| 842 | |
| 843 | if (priv) { |
| 844 | buf = priv->buf; |
| 845 | channel = priv->ltt_chan; |
| 846 | free(priv); |
| 847 | /* |
| 848 | * If we are at ABI exit, we don't want to close the |
| 849 | * buffer opened for read: it is being shared between |
| 850 | * the parent and child (right after fork), and we don't |
| 851 | * want the child to close it for the parent. For a real |
| 852 | * exit, we don't care about marking it as closed, as |
| 853 | * the consumer daemon (if there is one) will do fine |
| 854 | * even if we don't mark it as "closed" for reading on |
| 855 | * our side. |
| 856 | * We only mark it as closed if it is being explicitely |
| 857 | * released by the session daemon with an explicit |
| 858 | * release command. |
| 859 | */ |
| 860 | if (!lttng_ust_abi_close_in_progress) |
| 861 | channel->ops->buffer_read_close(buf, channel->handle); |
| 862 | |
| 863 | return lttng_ust_objd_unref(channel->objd); |
| 864 | } |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = { |
| 869 | .release = lttng_rb_release, |
| 870 | .cmd = lttng_rb_cmd, |
| 871 | }; |
| 872 | |
| 873 | /** |
| 874 | * lttng_event_cmd - lttng control through object descriptors |
| 875 | * |
| 876 | * @objd: the object descriptor |
| 877 | * @cmd: the command |
| 878 | * @arg: command arg |
| 879 | * |
| 880 | * This object descriptor implements lttng commands: |
| 881 | * LTTNG_UST_CONTEXT |
| 882 | * Prepend a context field to each record of this event |
| 883 | * LTTNG_UST_ENABLE |
| 884 | * Enable recording for this event (weak enable) |
| 885 | * LTTNG_UST_DISABLE |
| 886 | * Disable recording for this event (strong disable) |
| 887 | */ |
| 888 | static |
| 889 | long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg) |
| 890 | { |
| 891 | struct ltt_event *event = objd_private(objd); |
| 892 | |
| 893 | switch (cmd) { |
| 894 | case LTTNG_UST_CONTEXT: |
| 895 | return lttng_abi_add_context(objd, |
| 896 | (struct lttng_ust_context *) arg, |
| 897 | &event->ctx, event->chan->session); |
| 898 | case LTTNG_UST_ENABLE: |
| 899 | return ltt_event_enable(event); |
| 900 | case LTTNG_UST_DISABLE: |
| 901 | return ltt_event_disable(event); |
| 902 | default: |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | static |
| 908 | int lttng_event_release(int objd) |
| 909 | { |
| 910 | struct ltt_event *event = objd_private(objd); |
| 911 | |
| 912 | if (event) |
| 913 | return lttng_ust_objd_unref(event->chan->objd); |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | /* TODO: filter control ioctl */ |
| 918 | static const struct lttng_ust_objd_ops lttng_event_ops = { |
| 919 | .release = lttng_event_release, |
| 920 | .cmd = lttng_event_cmd, |
| 921 | }; |
| 922 | |
| 923 | void lttng_ust_abi_exit(void) |
| 924 | { |
| 925 | lttng_ust_abi_close_in_progress = 1; |
| 926 | objd_table_destroy(); |
| 927 | lttng_ust_abi_close_in_progress = 0; |
| 928 | } |