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