| 1 | /* |
| 2 | * lttng-ust-abi.c |
| 3 | * |
| 4 | * LTTng UST ABI |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
| 22 | * |
| 23 | * Mimic system calls for: |
| 24 | * - session creation, returns an object descriptor or failure. |
| 25 | * - channel creation, returns an object descriptor or failure. |
| 26 | * - Operates on a session object descriptor |
| 27 | * - Takes all channel options as parameters. |
| 28 | * - stream get, returns an object descriptor or failure. |
| 29 | * - Operates on a channel object descriptor. |
| 30 | * - stream notifier get, returns an object descriptor or failure. |
| 31 | * - Operates on a channel object descriptor. |
| 32 | * - event creation, returns an object descriptor or failure. |
| 33 | * - Operates on a channel object descriptor |
| 34 | * - Takes an event name as parameter |
| 35 | * - Takes an instrumentation source as parameter |
| 36 | * - e.g. tracepoints, dynamic_probes... |
| 37 | * - Takes instrumentation source specific arguments. |
| 38 | */ |
| 39 | |
| 40 | #include <lttng/ust-abi.h> |
| 41 | #include <lttng/ust-error.h> |
| 42 | #include <urcu/compiler.h> |
| 43 | #include <urcu/list.h> |
| 44 | #include <lttng/ust-events.h> |
| 45 | #include <lttng/ust-version.h> |
| 46 | #include <lttng/tracepoint.h> |
| 47 | #include "tracepoint-internal.h" |
| 48 | #include <usterr-signal-safe.h> |
| 49 | #include <helper.h> |
| 50 | #include "lttng-tracer.h" |
| 51 | #include "../libringbuffer/shm.h" |
| 52 | #include "../libringbuffer/frontend_types.h" |
| 53 | |
| 54 | #define OBJ_NAME_LEN 16 |
| 55 | |
| 56 | static int lttng_ust_abi_close_in_progress; |
| 57 | |
| 58 | static |
| 59 | int lttng_abi_tracepoint_list(void *owner); |
| 60 | static |
| 61 | int lttng_abi_tracepoint_field_list(void *owner); |
| 62 | |
| 63 | /* |
| 64 | * Object descriptor table. Should be protected from concurrent access |
| 65 | * by the caller. |
| 66 | */ |
| 67 | |
| 68 | struct lttng_ust_obj { |
| 69 | union { |
| 70 | struct { |
| 71 | void *private_data; |
| 72 | const struct lttng_ust_objd_ops *ops; |
| 73 | int f_count; |
| 74 | void *owner; |
| 75 | char name[OBJ_NAME_LEN]; |
| 76 | } s; |
| 77 | int freelist_next; /* offset freelist. end is -1. */ |
| 78 | } u; |
| 79 | }; |
| 80 | |
| 81 | struct lttng_ust_objd_table { |
| 82 | struct lttng_ust_obj *array; |
| 83 | unsigned int len, allocated_len; |
| 84 | int freelist_head; /* offset freelist head. end is -1 */ |
| 85 | }; |
| 86 | |
| 87 | static struct lttng_ust_objd_table objd_table = { |
| 88 | .freelist_head = -1, |
| 89 | }; |
| 90 | |
| 91 | static |
| 92 | int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops, |
| 93 | void *owner, const char *name) |
| 94 | { |
| 95 | struct lttng_ust_obj *obj; |
| 96 | |
| 97 | if (objd_table.freelist_head != -1) { |
| 98 | obj = &objd_table.array[objd_table.freelist_head]; |
| 99 | objd_table.freelist_head = obj->u.freelist_next; |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | if (objd_table.len >= objd_table.allocated_len) { |
| 104 | unsigned int new_allocated_len, old_allocated_len; |
| 105 | struct lttng_ust_obj *new_table, *old_table; |
| 106 | |
| 107 | old_allocated_len = objd_table.allocated_len; |
| 108 | old_table = objd_table.array; |
| 109 | if (!old_allocated_len) |
| 110 | new_allocated_len = 1; |
| 111 | else |
| 112 | new_allocated_len = old_allocated_len << 1; |
| 113 | new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len); |
| 114 | if (!new_table) |
| 115 | return -ENOMEM; |
| 116 | memcpy(new_table, old_table, |
| 117 | sizeof(struct lttng_ust_obj) * old_allocated_len); |
| 118 | free(old_table); |
| 119 | objd_table.array = new_table; |
| 120 | objd_table.allocated_len = new_allocated_len; |
| 121 | } |
| 122 | obj = &objd_table.array[objd_table.len]; |
| 123 | objd_table.len++; |
| 124 | end: |
| 125 | obj->u.s.private_data = private_data; |
| 126 | obj->u.s.ops = ops; |
| 127 | obj->u.s.f_count = 2; /* count == 1 : object is allocated */ |
| 128 | /* count == 2 : allocated + hold ref */ |
| 129 | obj->u.s.owner = owner; |
| 130 | strncpy(obj->u.s.name, name, OBJ_NAME_LEN); |
| 131 | obj->u.s.name[OBJ_NAME_LEN - 1] = '\0'; |
| 132 | return obj - objd_table.array; |
| 133 | } |
| 134 | |
| 135 | static |
| 136 | struct lttng_ust_obj *_objd_get(int id) |
| 137 | { |
| 138 | if (id >= objd_table.len) |
| 139 | return NULL; |
| 140 | if (!objd_table.array[id].u.s.f_count) |
| 141 | return NULL; |
| 142 | return &objd_table.array[id]; |
| 143 | } |
| 144 | |
| 145 | static |
| 146 | void *objd_private(int id) |
| 147 | { |
| 148 | struct lttng_ust_obj *obj = _objd_get(id); |
| 149 | assert(obj); |
| 150 | return obj->u.s.private_data; |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | void objd_set_private(int id, void *private_data) |
| 155 | { |
| 156 | struct lttng_ust_obj *obj = _objd_get(id); |
| 157 | assert(obj); |
| 158 | obj->u.s.private_data = private_data; |
| 159 | } |
| 160 | |
| 161 | const struct lttng_ust_objd_ops *objd_ops(int id) |
| 162 | { |
| 163 | struct lttng_ust_obj *obj = _objd_get(id); |
| 164 | |
| 165 | if (!obj) |
| 166 | return NULL; |
| 167 | return obj->u.s.ops; |
| 168 | } |
| 169 | |
| 170 | static |
| 171 | void objd_free(int id) |
| 172 | { |
| 173 | struct lttng_ust_obj *obj = _objd_get(id); |
| 174 | |
| 175 | assert(obj); |
| 176 | obj->u.freelist_next = objd_table.freelist_head; |
| 177 | objd_table.freelist_head = obj - objd_table.array; |
| 178 | assert(obj->u.s.f_count == 1); |
| 179 | obj->u.s.f_count = 0; /* deallocated */ |
| 180 | } |
| 181 | |
| 182 | static |
| 183 | void objd_ref(int id) |
| 184 | { |
| 185 | struct lttng_ust_obj *obj = _objd_get(id); |
| 186 | obj->u.s.f_count++; |
| 187 | } |
| 188 | |
| 189 | int lttng_ust_objd_unref(int id) |
| 190 | { |
| 191 | struct lttng_ust_obj *obj = _objd_get(id); |
| 192 | |
| 193 | if (!obj) |
| 194 | return -EINVAL; |
| 195 | if (obj->u.s.f_count == 1) { |
| 196 | ERR("Reference counting error\n"); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | if ((--obj->u.s.f_count) == 1) { |
| 200 | const struct lttng_ust_objd_ops *ops = objd_ops(id); |
| 201 | |
| 202 | if (ops->release) |
| 203 | ops->release(id); |
| 204 | objd_free(id); |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static |
| 210 | void objd_table_destroy(void) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | for (i = 0; i < objd_table.allocated_len; i++) |
| 215 | (void) lttng_ust_objd_unref(i); |
| 216 | free(objd_table.array); |
| 217 | objd_table.array = NULL; |
| 218 | objd_table.len = 0; |
| 219 | objd_table.allocated_len = 0; |
| 220 | objd_table.freelist_head = -1; |
| 221 | } |
| 222 | |
| 223 | const char *lttng_ust_obj_get_name(int id) |
| 224 | { |
| 225 | struct lttng_ust_obj *obj = _objd_get(id); |
| 226 | |
| 227 | if (!obj) |
| 228 | return NULL; |
| 229 | return obj->u.s.name; |
| 230 | } |
| 231 | |
| 232 | void lttng_ust_objd_table_owner_cleanup(void *owner) |
| 233 | { |
| 234 | int i; |
| 235 | |
| 236 | for (i = 0; i < objd_table.allocated_len; i++) { |
| 237 | struct lttng_ust_obj *obj; |
| 238 | |
| 239 | obj = _objd_get(i); |
| 240 | if (!obj) |
| 241 | continue; |
| 242 | if (!obj->u.s.owner) |
| 243 | continue; /* skip root handles */ |
| 244 | if (obj->u.s.owner == owner) |
| 245 | (void) lttng_ust_objd_unref(i); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * This is LTTng's own personal way to create an ABI for sessiond. |
| 251 | * We send commands over a socket. |
| 252 | */ |
| 253 | |
| 254 | static const struct lttng_ust_objd_ops lttng_ops; |
| 255 | static const struct lttng_ust_objd_ops lttng_session_ops; |
| 256 | static const struct lttng_ust_objd_ops lttng_channel_ops; |
| 257 | static const struct lttng_ust_objd_ops lttng_enabler_ops; |
| 258 | static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops; |
| 259 | static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops; |
| 260 | |
| 261 | int lttng_abi_create_root_handle(void) |
| 262 | { |
| 263 | int root_handle; |
| 264 | |
| 265 | /* root handles have NULL owners */ |
| 266 | root_handle = objd_alloc(NULL, <tng_ops, NULL, "root"); |
| 267 | return root_handle; |
| 268 | } |
| 269 | |
| 270 | static |
| 271 | int lttng_is_channel_ready(struct lttng_channel *lttng_chan) |
| 272 | { |
| 273 | struct channel *chan; |
| 274 | unsigned int nr_streams, exp_streams; |
| 275 | |
| 276 | chan = lttng_chan->chan; |
| 277 | nr_streams = channel_handle_get_nr_streams(lttng_chan->handle); |
| 278 | exp_streams = chan->nr_streams; |
| 279 | return nr_streams == exp_streams; |
| 280 | } |
| 281 | |
| 282 | static |
| 283 | int lttng_abi_create_session(void *owner) |
| 284 | { |
| 285 | struct lttng_session *session; |
| 286 | int session_objd, ret; |
| 287 | |
| 288 | session = lttng_session_create(); |
| 289 | if (!session) |
| 290 | return -ENOMEM; |
| 291 | session_objd = objd_alloc(session, <tng_session_ops, owner, "session"); |
| 292 | if (session_objd < 0) { |
| 293 | ret = session_objd; |
| 294 | goto objd_error; |
| 295 | } |
| 296 | session->objd = session_objd; |
| 297 | session->owner = owner; |
| 298 | return session_objd; |
| 299 | |
| 300 | objd_error: |
| 301 | lttng_session_destroy(session); |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static |
| 306 | long lttng_abi_tracer_version(int objd, |
| 307 | struct lttng_ust_tracer_version *v) |
| 308 | { |
| 309 | v->major = LTTNG_UST_MAJOR_VERSION; |
| 310 | v->minor = LTTNG_UST_MINOR_VERSION; |
| 311 | v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION; |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static |
| 316 | long lttng_abi_add_context(int objd, |
| 317 | struct lttng_ust_context *context_param, |
| 318 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 319 | { |
| 320 | return lttng_attach_context(context_param, ctx, session); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * lttng_cmd - lttng control through socket commands |
| 325 | * |
| 326 | * @objd: the object descriptor |
| 327 | * @cmd: the command |
| 328 | * @arg: command arg |
| 329 | * @uargs: UST arguments (internal) |
| 330 | * @owner: objd owner |
| 331 | * |
| 332 | * This descriptor implements lttng commands: |
| 333 | * LTTNG_UST_SESSION |
| 334 | * Returns a LTTng trace session object descriptor |
| 335 | * LTTNG_UST_TRACER_VERSION |
| 336 | * Returns the LTTng kernel tracer version |
| 337 | * LTTNG_UST_TRACEPOINT_LIST |
| 338 | * Returns a file descriptor listing available tracepoints |
| 339 | * LTTNG_UST_TRACEPOINT_FIELD_LIST |
| 340 | * Returns a file descriptor listing available tracepoint fields |
| 341 | * LTTNG_UST_WAIT_QUIESCENT |
| 342 | * Returns after all previously running probes have completed |
| 343 | * |
| 344 | * The returned session will be deleted when its file descriptor is closed. |
| 345 | */ |
| 346 | static |
| 347 | long lttng_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 348 | union ust_args *uargs, void *owner) |
| 349 | { |
| 350 | switch (cmd) { |
| 351 | case LTTNG_UST_SESSION: |
| 352 | return lttng_abi_create_session(owner); |
| 353 | case LTTNG_UST_TRACER_VERSION: |
| 354 | return lttng_abi_tracer_version(objd, |
| 355 | (struct lttng_ust_tracer_version *) arg); |
| 356 | case LTTNG_UST_TRACEPOINT_LIST: |
| 357 | return lttng_abi_tracepoint_list(owner); |
| 358 | case LTTNG_UST_TRACEPOINT_FIELD_LIST: |
| 359 | return lttng_abi_tracepoint_field_list(owner); |
| 360 | case LTTNG_UST_WAIT_QUIESCENT: |
| 361 | synchronize_trace(); |
| 362 | return 0; |
| 363 | default: |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static const struct lttng_ust_objd_ops lttng_ops = { |
| 369 | .cmd = lttng_cmd, |
| 370 | }; |
| 371 | |
| 372 | int lttng_abi_map_channel(int session_objd, |
| 373 | struct lttng_ust_channel *ust_chan, |
| 374 | union ust_args *uargs, |
| 375 | void *owner) |
| 376 | { |
| 377 | struct lttng_session *session = objd_private(session_objd); |
| 378 | const char *transport_name; |
| 379 | const struct lttng_transport *transport; |
| 380 | const char *chan_name; |
| 381 | int chan_objd; |
| 382 | struct lttng_ust_shm_handle *channel_handle; |
| 383 | struct lttng_channel *lttng_chan; |
| 384 | struct channel *chan; |
| 385 | struct lttng_ust_lib_ring_buffer_config *config; |
| 386 | void *chan_data; |
| 387 | uint64_t len; |
| 388 | int ret; |
| 389 | enum lttng_ust_chan_type type; |
| 390 | |
| 391 | chan_data = uargs->channel.chan_data; |
| 392 | len = ust_chan->len; |
| 393 | type = ust_chan->type; |
| 394 | |
| 395 | switch (type) { |
| 396 | case LTTNG_UST_CHAN_PER_CPU: |
| 397 | break; |
| 398 | default: |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | if (session->been_active) { |
| 403 | ret = -EBUSY; |
| 404 | goto active; /* Refuse to add channel to active session */ |
| 405 | } |
| 406 | |
| 407 | channel_handle = channel_handle_create(chan_data, len); |
| 408 | if (!channel_handle) { |
| 409 | ret = -EINVAL; |
| 410 | goto handle_error; |
| 411 | } |
| 412 | |
| 413 | chan = shmp(channel_handle, channel_handle->chan); |
| 414 | assert(chan); |
| 415 | chan->handle = channel_handle; |
| 416 | config = &chan->backend.config; |
| 417 | lttng_chan = channel_get_private(chan); |
| 418 | if (!lttng_chan) { |
| 419 | ret = -EINVAL; |
| 420 | goto alloc_error; |
| 421 | } |
| 422 | |
| 423 | /* Lookup transport name */ |
| 424 | switch (type) { |
| 425 | case LTTNG_UST_CHAN_PER_CPU: |
| 426 | if (config->output == RING_BUFFER_MMAP) { |
| 427 | transport_name = config->mode == RING_BUFFER_OVERWRITE ? |
| 428 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 429 | } else { |
| 430 | ret = -EINVAL; |
| 431 | goto notransport; |
| 432 | } |
| 433 | chan_name = "channel"; |
| 434 | break; |
| 435 | default: |
| 436 | transport_name = "<unknown>"; |
| 437 | chan_name = "<unknown>"; |
| 438 | ret = -EINVAL; |
| 439 | goto notransport; |
| 440 | } |
| 441 | transport = lttng_transport_find(transport_name); |
| 442 | if (!transport) { |
| 443 | DBG("LTTng transport %s not found\n", |
| 444 | transport_name); |
| 445 | ret = -EINVAL; |
| 446 | goto notransport; |
| 447 | } |
| 448 | |
| 449 | chan_objd = objd_alloc(NULL, <tng_channel_ops, owner, chan_name); |
| 450 | if (chan_objd < 0) { |
| 451 | ret = chan_objd; |
| 452 | goto objd_error; |
| 453 | } |
| 454 | |
| 455 | /* Initialize our lttng chan */ |
| 456 | lttng_chan->chan = chan; |
| 457 | lttng_chan->tstate = 1; |
| 458 | lttng_chan->enabled = 1; |
| 459 | lttng_chan->ctx = NULL; |
| 460 | lttng_chan->session = session; |
| 461 | lttng_chan->ops = &transport->ops; |
| 462 | memcpy(<tng_chan->chan->backend.config, |
| 463 | transport->client_config, |
| 464 | sizeof(lttng_chan->chan->backend.config)); |
| 465 | cds_list_add(<tng_chan->node, &session->chan_head); |
| 466 | lttng_chan->header_type = 0; |
| 467 | lttng_chan->handle = channel_handle; |
| 468 | lttng_chan->type = type; |
| 469 | |
| 470 | /* |
| 471 | * We tolerate no failure path after channel creation. It will stay |
| 472 | * invariant for the rest of the session. |
| 473 | */ |
| 474 | objd_set_private(chan_objd, lttng_chan); |
| 475 | lttng_chan->objd = chan_objd; |
| 476 | /* The channel created holds a reference on the session */ |
| 477 | objd_ref(session_objd); |
| 478 | return chan_objd; |
| 479 | |
| 480 | objd_error: |
| 481 | notransport: |
| 482 | free(lttng_chan); |
| 483 | alloc_error: |
| 484 | channel_destroy(chan, channel_handle, 0); |
| 485 | handle_error: |
| 486 | active: |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * lttng_session_cmd - lttng session object command |
| 492 | * |
| 493 | * @obj: the object |
| 494 | * @cmd: the command |
| 495 | * @arg: command arg |
| 496 | * @uargs: UST arguments (internal) |
| 497 | * @owner: objd owner |
| 498 | * |
| 499 | * This descriptor implements lttng commands: |
| 500 | * LTTNG_UST_CHANNEL |
| 501 | * Returns a LTTng channel object descriptor |
| 502 | * LTTNG_UST_ENABLE |
| 503 | * Enables tracing for a session (weak enable) |
| 504 | * LTTNG_UST_DISABLE |
| 505 | * Disables tracing for a session (strong disable) |
| 506 | * |
| 507 | * The returned channel will be deleted when its file descriptor is closed. |
| 508 | */ |
| 509 | static |
| 510 | long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 511 | union ust_args *uargs, void *owner) |
| 512 | { |
| 513 | struct lttng_session *session = objd_private(objd); |
| 514 | |
| 515 | switch (cmd) { |
| 516 | case LTTNG_UST_CHANNEL: |
| 517 | return lttng_abi_map_channel(objd, |
| 518 | (struct lttng_ust_channel *) arg, |
| 519 | uargs, owner); |
| 520 | case LTTNG_UST_SESSION_START: |
| 521 | case LTTNG_UST_ENABLE: |
| 522 | return lttng_session_enable(session); |
| 523 | case LTTNG_UST_SESSION_STOP: |
| 524 | case LTTNG_UST_DISABLE: |
| 525 | return lttng_session_disable(session); |
| 526 | default: |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Called when the last file reference is dropped. |
| 533 | * |
| 534 | * Big fat note: channels and events are invariant for the whole session after |
| 535 | * their creation. So this session destruction also destroys all channel and |
| 536 | * event structures specific to this session (they are not destroyed when their |
| 537 | * individual file is released). |
| 538 | */ |
| 539 | static |
| 540 | int lttng_release_session(int objd) |
| 541 | { |
| 542 | struct lttng_session *session = objd_private(objd); |
| 543 | |
| 544 | if (session) { |
| 545 | lttng_session_destroy(session); |
| 546 | return 0; |
| 547 | } else { |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static const struct lttng_ust_objd_ops lttng_session_ops = { |
| 553 | .release = lttng_release_session, |
| 554 | .cmd = lttng_session_cmd, |
| 555 | }; |
| 556 | |
| 557 | static |
| 558 | long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 559 | union ust_args *uargs, void *owner) |
| 560 | { |
| 561 | struct lttng_ust_tracepoint_list *list = objd_private(objd); |
| 562 | struct lttng_ust_tracepoint_iter *tp = |
| 563 | (struct lttng_ust_tracepoint_iter *) arg; |
| 564 | struct lttng_ust_tracepoint_iter *iter; |
| 565 | |
| 566 | switch (cmd) { |
| 567 | case LTTNG_UST_TRACEPOINT_LIST_GET: |
| 568 | { |
| 569 | iter = lttng_ust_tracepoint_list_get_iter_next(list); |
| 570 | if (!iter) |
| 571 | return -LTTNG_UST_ERR_NOENT; |
| 572 | memcpy(tp, iter, sizeof(*tp)); |
| 573 | return 0; |
| 574 | } |
| 575 | default: |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static |
| 581 | int lttng_abi_tracepoint_list(void *owner) |
| 582 | { |
| 583 | int list_objd, ret; |
| 584 | struct lttng_ust_tracepoint_list *list; |
| 585 | |
| 586 | list_objd = objd_alloc(NULL, <tng_tracepoint_list_ops, owner, "tp_list"); |
| 587 | if (list_objd < 0) { |
| 588 | ret = list_objd; |
| 589 | goto objd_error; |
| 590 | } |
| 591 | list = zmalloc(sizeof(*list)); |
| 592 | if (!list) { |
| 593 | ret = -ENOMEM; |
| 594 | goto alloc_error; |
| 595 | } |
| 596 | objd_set_private(list_objd, list); |
| 597 | |
| 598 | /* populate list by walking on all registered probes. */ |
| 599 | ret = lttng_probes_get_event_list(list); |
| 600 | if (ret) { |
| 601 | goto list_error; |
| 602 | } |
| 603 | return list_objd; |
| 604 | |
| 605 | list_error: |
| 606 | free(list); |
| 607 | alloc_error: |
| 608 | { |
| 609 | int err; |
| 610 | |
| 611 | err = lttng_ust_objd_unref(list_objd); |
| 612 | assert(!err); |
| 613 | } |
| 614 | objd_error: |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static |
| 619 | int lttng_release_tracepoint_list(int objd) |
| 620 | { |
| 621 | struct lttng_ust_tracepoint_list *list = objd_private(objd); |
| 622 | |
| 623 | if (list) { |
| 624 | lttng_probes_prune_event_list(list); |
| 625 | free(list); |
| 626 | return 0; |
| 627 | } else { |
| 628 | return -EINVAL; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = { |
| 633 | .release = lttng_release_tracepoint_list, |
| 634 | .cmd = lttng_tracepoint_list_cmd, |
| 635 | }; |
| 636 | |
| 637 | static |
| 638 | long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd, |
| 639 | unsigned long arg, union ust_args *uargs, void *owner) |
| 640 | { |
| 641 | struct lttng_ust_field_list *list = objd_private(objd); |
| 642 | struct lttng_ust_field_iter *tp = &uargs->field_list.entry; |
| 643 | struct lttng_ust_field_iter *iter; |
| 644 | |
| 645 | switch (cmd) { |
| 646 | case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: |
| 647 | { |
| 648 | iter = lttng_ust_field_list_get_iter_next(list); |
| 649 | if (!iter) |
| 650 | return -LTTNG_UST_ERR_NOENT; |
| 651 | memcpy(tp, iter, sizeof(*tp)); |
| 652 | return 0; |
| 653 | } |
| 654 | default: |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | static |
| 660 | int lttng_abi_tracepoint_field_list(void *owner) |
| 661 | { |
| 662 | int list_objd, ret; |
| 663 | struct lttng_ust_field_list *list; |
| 664 | |
| 665 | list_objd = objd_alloc(NULL, <tng_tracepoint_field_list_ops, owner, |
| 666 | "tp_field_list"); |
| 667 | if (list_objd < 0) { |
| 668 | ret = list_objd; |
| 669 | goto objd_error; |
| 670 | } |
| 671 | list = zmalloc(sizeof(*list)); |
| 672 | if (!list) { |
| 673 | ret = -ENOMEM; |
| 674 | goto alloc_error; |
| 675 | } |
| 676 | objd_set_private(list_objd, list); |
| 677 | |
| 678 | /* populate list by walking on all registered probes. */ |
| 679 | ret = lttng_probes_get_field_list(list); |
| 680 | if (ret) { |
| 681 | goto list_error; |
| 682 | } |
| 683 | return list_objd; |
| 684 | |
| 685 | list_error: |
| 686 | free(list); |
| 687 | alloc_error: |
| 688 | { |
| 689 | int err; |
| 690 | |
| 691 | err = lttng_ust_objd_unref(list_objd); |
| 692 | assert(!err); |
| 693 | } |
| 694 | objd_error: |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | static |
| 699 | int lttng_release_tracepoint_field_list(int objd) |
| 700 | { |
| 701 | struct lttng_ust_field_list *list = objd_private(objd); |
| 702 | |
| 703 | if (list) { |
| 704 | lttng_probes_prune_field_list(list); |
| 705 | free(list); |
| 706 | return 0; |
| 707 | } else { |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = { |
| 713 | .release = lttng_release_tracepoint_field_list, |
| 714 | .cmd = lttng_tracepoint_field_list_cmd, |
| 715 | }; |
| 716 | |
| 717 | static |
| 718 | int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info, |
| 719 | union ust_args *uargs, void *owner) |
| 720 | { |
| 721 | struct lttng_channel *channel = objd_private(channel_objd); |
| 722 | int ret; |
| 723 | |
| 724 | ret = channel_handle_add_stream(channel->handle, |
| 725 | uargs->stream.shm_fd, uargs->stream.wakeup_fd, |
| 726 | info->stream_nr, info->len); |
| 727 | if (ret) |
| 728 | goto error_add_stream; |
| 729 | |
| 730 | return 0; |
| 731 | |
| 732 | error_add_stream: |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | static |
| 737 | int lttng_abi_create_enabler(int channel_objd, |
| 738 | struct lttng_ust_event *event_param, |
| 739 | void *owner, |
| 740 | enum lttng_enabler_type type) |
| 741 | { |
| 742 | struct lttng_channel *channel = objd_private(channel_objd); |
| 743 | struct lttng_enabler *enabler; |
| 744 | int event_objd, ret; |
| 745 | |
| 746 | event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 747 | event_objd = objd_alloc(NULL, <tng_enabler_ops, owner, "enabler"); |
| 748 | if (event_objd < 0) { |
| 749 | ret = event_objd; |
| 750 | goto objd_error; |
| 751 | } |
| 752 | /* |
| 753 | * We tolerate no failure path after event creation. It will stay |
| 754 | * invariant for the rest of the session. |
| 755 | */ |
| 756 | enabler = lttng_enabler_create(type, event_param, channel); |
| 757 | if (!enabler) { |
| 758 | ret = -ENOMEM; |
| 759 | goto event_error; |
| 760 | } |
| 761 | objd_set_private(event_objd, enabler); |
| 762 | /* The event holds a reference on the channel */ |
| 763 | objd_ref(channel_objd); |
| 764 | return event_objd; |
| 765 | |
| 766 | event_error: |
| 767 | { |
| 768 | int err; |
| 769 | |
| 770 | err = lttng_ust_objd_unref(event_objd); |
| 771 | assert(!err); |
| 772 | } |
| 773 | objd_error: |
| 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * lttng_channel_cmd - lttng control through object descriptors |
| 779 | * |
| 780 | * @objd: the object descriptor |
| 781 | * @cmd: the command |
| 782 | * @arg: command arg |
| 783 | * @uargs: UST arguments (internal) |
| 784 | * @owner: objd owner |
| 785 | * |
| 786 | * This object descriptor implements lttng commands: |
| 787 | * LTTNG_UST_STREAM |
| 788 | * Returns an event stream object descriptor or failure. |
| 789 | * (typically, one event stream records events from one CPU) |
| 790 | * LTTNG_UST_EVENT |
| 791 | * Returns an event object descriptor or failure. |
| 792 | * LTTNG_UST_CONTEXT |
| 793 | * Prepend a context field to each event in the channel |
| 794 | * LTTNG_UST_ENABLE |
| 795 | * Enable recording for events in this channel (weak enable) |
| 796 | * LTTNG_UST_DISABLE |
| 797 | * Disable recording for events in this channel (strong disable) |
| 798 | * |
| 799 | * Channel and event file descriptors also hold a reference on the session. |
| 800 | */ |
| 801 | static |
| 802 | long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 803 | union ust_args *uargs, void *owner) |
| 804 | { |
| 805 | struct lttng_channel *channel = objd_private(objd); |
| 806 | |
| 807 | if (cmd != LTTNG_UST_STREAM) { |
| 808 | /* |
| 809 | * Check if channel received all streams. |
| 810 | */ |
| 811 | if (!lttng_is_channel_ready(channel)) |
| 812 | return -EPERM; |
| 813 | } |
| 814 | |
| 815 | switch (cmd) { |
| 816 | case LTTNG_UST_STREAM: |
| 817 | { |
| 818 | struct lttng_ust_stream *stream; |
| 819 | |
| 820 | stream = (struct lttng_ust_stream *) arg; |
| 821 | /* stream used as output */ |
| 822 | return lttng_abi_map_stream(objd, stream, uargs, owner); |
| 823 | } |
| 824 | case LTTNG_UST_EVENT: |
| 825 | { |
| 826 | struct lttng_ust_event *event_param = |
| 827 | (struct lttng_ust_event *) arg; |
| 828 | if (event_param->name[strlen(event_param->name) - 1] == '*') { |
| 829 | /* If ends with wildcard, create wildcard. */ |
| 830 | return lttng_abi_create_enabler(objd, event_param, |
| 831 | owner, LTTNG_ENABLER_WILDCARD); |
| 832 | } else { |
| 833 | return lttng_abi_create_enabler(objd, event_param, |
| 834 | owner, LTTNG_ENABLER_EVENT); |
| 835 | } |
| 836 | } |
| 837 | case LTTNG_UST_CONTEXT: |
| 838 | return lttng_abi_add_context(objd, |
| 839 | (struct lttng_ust_context *) arg, |
| 840 | &channel->ctx, channel->session); |
| 841 | case LTTNG_UST_ENABLE: |
| 842 | return lttng_channel_enable(channel); |
| 843 | case LTTNG_UST_DISABLE: |
| 844 | return lttng_channel_disable(channel); |
| 845 | case LTTNG_UST_FLUSH_BUFFER: |
| 846 | return channel->ops->flush_buffer(channel->chan, channel->handle); |
| 847 | default: |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | static |
| 853 | int lttng_channel_release(int objd) |
| 854 | { |
| 855 | struct lttng_channel *channel = objd_private(objd); |
| 856 | |
| 857 | if (channel) |
| 858 | return lttng_ust_objd_unref(channel->session->objd); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static const struct lttng_ust_objd_ops lttng_channel_ops = { |
| 863 | .release = lttng_channel_release, |
| 864 | .cmd = lttng_channel_cmd, |
| 865 | }; |
| 866 | |
| 867 | /** |
| 868 | * lttng_enabler_cmd - lttng control through object descriptors |
| 869 | * |
| 870 | * @objd: the object descriptor |
| 871 | * @cmd: the command |
| 872 | * @arg: command arg |
| 873 | * @uargs: UST arguments (internal) |
| 874 | * @owner: objd owner |
| 875 | * |
| 876 | * This object descriptor implements lttng commands: |
| 877 | * LTTNG_UST_CONTEXT |
| 878 | * Prepend a context field to each record of events of this |
| 879 | * enabler. |
| 880 | * LTTNG_UST_ENABLE |
| 881 | * Enable recording for this enabler |
| 882 | * LTTNG_UST_DISABLE |
| 883 | * Disable recording for this enabler |
| 884 | * LTTNG_UST_FILTER |
| 885 | * Attach a filter to an enabler. |
| 886 | */ |
| 887 | static |
| 888 | long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 889 | union ust_args *uargs, void *owner) |
| 890 | { |
| 891 | struct lttng_enabler *enabler = objd_private(objd); |
| 892 | |
| 893 | switch (cmd) { |
| 894 | case LTTNG_UST_CONTEXT: |
| 895 | return lttng_enabler_attach_context(enabler, |
| 896 | (struct lttng_ust_context *) arg); |
| 897 | case LTTNG_UST_ENABLE: |
| 898 | return lttng_enabler_enable(enabler); |
| 899 | case LTTNG_UST_DISABLE: |
| 900 | return lttng_enabler_disable(enabler); |
| 901 | case LTTNG_UST_FILTER: |
| 902 | { |
| 903 | int ret; |
| 904 | |
| 905 | ret = lttng_enabler_attach_bytecode(enabler, |
| 906 | (struct lttng_ust_filter_bytecode_node *) arg); |
| 907 | if (ret) |
| 908 | return ret; |
| 909 | return 0; |
| 910 | } |
| 911 | default: |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | static |
| 917 | int lttng_enabler_release(int objd) |
| 918 | { |
| 919 | struct lttng_enabler *enabler = objd_private(objd); |
| 920 | |
| 921 | if (enabler) |
| 922 | return lttng_ust_objd_unref(enabler->chan->objd); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | static const struct lttng_ust_objd_ops lttng_enabler_ops = { |
| 927 | .release = lttng_enabler_release, |
| 928 | .cmd = lttng_enabler_cmd, |
| 929 | }; |
| 930 | |
| 931 | void lttng_ust_abi_exit(void) |
| 932 | { |
| 933 | lttng_ust_abi_close_in_progress = 1; |
| 934 | objd_table_destroy(); |
| 935 | lttng_ust_abi_close_in_progress = 0; |
| 936 | } |