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