| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2010-2012 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 | |
| 25 | #define _LGPL_SOURCE |
| 26 | #include <fcntl.h> |
| 27 | #include <stdint.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <urcu/compiler.h> |
| 31 | #include <urcu/list.h> |
| 32 | |
| 33 | #include <lttng/tracepoint.h> |
| 34 | #include <lttng/ust-abi.h> |
| 35 | #include <lttng/ust-error.h> |
| 36 | #include <lttng/ust-events.h> |
| 37 | #include <lttng/ust-version.h> |
| 38 | #include <ust-fd.h> |
| 39 | #include <usterr-signal-safe.h> |
| 40 | |
| 41 | #include "../libringbuffer/frontend_types.h" |
| 42 | #include "../libringbuffer/shm.h" |
| 43 | #include "../libcounter/counter.h" |
| 44 | #include "tracepoint-internal.h" |
| 45 | #include "lttng-tracer.h" |
| 46 | #include "string-utils.h" |
| 47 | #include "ust-events-internal.h" |
| 48 | #include "context-internal.h" |
| 49 | #include "ust-helper.h" |
| 50 | |
| 51 | #define OBJ_NAME_LEN 16 |
| 52 | |
| 53 | static int lttng_ust_abi_close_in_progress; |
| 54 | |
| 55 | static |
| 56 | int lttng_abi_tracepoint_list(void *owner); |
| 57 | static |
| 58 | int lttng_abi_tracepoint_field_list(void *owner); |
| 59 | |
| 60 | /* |
| 61 | * Object descriptor table. Should be protected from concurrent access |
| 62 | * by the caller. |
| 63 | */ |
| 64 | |
| 65 | struct lttng_ust_abi_obj { |
| 66 | union { |
| 67 | struct { |
| 68 | void *private_data; |
| 69 | const struct lttng_ust_abi_objd_ops *ops; |
| 70 | int f_count; |
| 71 | int owner_ref; /* has ref from owner */ |
| 72 | void *owner; |
| 73 | char name[OBJ_NAME_LEN]; |
| 74 | } s; |
| 75 | int freelist_next; /* offset freelist. end is -1. */ |
| 76 | } u; |
| 77 | }; |
| 78 | |
| 79 | struct lttng_ust_abi_objd_table { |
| 80 | struct lttng_ust_abi_obj *array; |
| 81 | unsigned int len, allocated_len; |
| 82 | int freelist_head; /* offset freelist head. end is -1 */ |
| 83 | }; |
| 84 | |
| 85 | static struct lttng_ust_abi_objd_table objd_table = { |
| 86 | .freelist_head = -1, |
| 87 | }; |
| 88 | |
| 89 | static |
| 90 | int objd_alloc(void *private_data, const struct lttng_ust_abi_objd_ops *ops, |
| 91 | void *owner, const char *name) |
| 92 | { |
| 93 | struct lttng_ust_abi_obj *obj; |
| 94 | |
| 95 | if (objd_table.freelist_head != -1) { |
| 96 | obj = &objd_table.array[objd_table.freelist_head]; |
| 97 | objd_table.freelist_head = obj->u.freelist_next; |
| 98 | goto end; |
| 99 | } |
| 100 | |
| 101 | if (objd_table.len >= objd_table.allocated_len) { |
| 102 | unsigned int new_allocated_len, old_allocated_len; |
| 103 | struct lttng_ust_abi_obj *new_table, *old_table; |
| 104 | |
| 105 | old_allocated_len = objd_table.allocated_len; |
| 106 | old_table = objd_table.array; |
| 107 | if (!old_allocated_len) |
| 108 | new_allocated_len = 1; |
| 109 | else |
| 110 | new_allocated_len = old_allocated_len << 1; |
| 111 | new_table = zmalloc(sizeof(struct lttng_ust_abi_obj) * new_allocated_len); |
| 112 | if (!new_table) |
| 113 | return -ENOMEM; |
| 114 | memcpy(new_table, old_table, |
| 115 | sizeof(struct lttng_ust_abi_obj) * old_allocated_len); |
| 116 | free(old_table); |
| 117 | objd_table.array = new_table; |
| 118 | objd_table.allocated_len = new_allocated_len; |
| 119 | } |
| 120 | obj = &objd_table.array[objd_table.len]; |
| 121 | objd_table.len++; |
| 122 | end: |
| 123 | obj->u.s.private_data = private_data; |
| 124 | obj->u.s.ops = ops; |
| 125 | obj->u.s.f_count = 2; /* count == 1 : object is allocated */ |
| 126 | /* count == 2 : allocated + hold ref */ |
| 127 | obj->u.s.owner_ref = 1; /* One owner reference */ |
| 128 | obj->u.s.owner = owner; |
| 129 | strncpy(obj->u.s.name, name, OBJ_NAME_LEN); |
| 130 | obj->u.s.name[OBJ_NAME_LEN - 1] = '\0'; |
| 131 | return obj - objd_table.array; |
| 132 | } |
| 133 | |
| 134 | static |
| 135 | struct lttng_ust_abi_obj *_objd_get(int id) |
| 136 | { |
| 137 | if (id >= objd_table.len) |
| 138 | return NULL; |
| 139 | if (!objd_table.array[id].u.s.f_count) |
| 140 | return NULL; |
| 141 | return &objd_table.array[id]; |
| 142 | } |
| 143 | |
| 144 | static |
| 145 | void *objd_private(int id) |
| 146 | { |
| 147 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 148 | assert(obj); |
| 149 | return obj->u.s.private_data; |
| 150 | } |
| 151 | |
| 152 | static |
| 153 | void objd_set_private(int id, void *private_data) |
| 154 | { |
| 155 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 156 | assert(obj); |
| 157 | obj->u.s.private_data = private_data; |
| 158 | } |
| 159 | |
| 160 | const struct lttng_ust_abi_objd_ops *lttng_ust_abi_objd_ops(int id) |
| 161 | { |
| 162 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 163 | |
| 164 | if (!obj) |
| 165 | return NULL; |
| 166 | return obj->u.s.ops; |
| 167 | } |
| 168 | |
| 169 | static |
| 170 | void objd_free(int id) |
| 171 | { |
| 172 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 173 | |
| 174 | assert(obj); |
| 175 | obj->u.freelist_next = objd_table.freelist_head; |
| 176 | objd_table.freelist_head = obj - objd_table.array; |
| 177 | assert(obj->u.s.f_count == 1); |
| 178 | obj->u.s.f_count = 0; /* deallocated */ |
| 179 | } |
| 180 | |
| 181 | static |
| 182 | void objd_ref(int id) |
| 183 | { |
| 184 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 185 | assert(obj != NULL); |
| 186 | obj->u.s.f_count++; |
| 187 | } |
| 188 | |
| 189 | int lttng_ust_abi_objd_unref(int id, int is_owner) |
| 190 | { |
| 191 | struct lttng_ust_abi_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 (is_owner) { |
| 200 | if (!obj->u.s.owner_ref) { |
| 201 | ERR("Error decrementing owner reference"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | obj->u.s.owner_ref--; |
| 205 | } |
| 206 | if ((--obj->u.s.f_count) == 1) { |
| 207 | const struct lttng_ust_abi_objd_ops *ops = lttng_ust_abi_objd_ops(id); |
| 208 | |
| 209 | if (ops->release) |
| 210 | ops->release(id); |
| 211 | objd_free(id); |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static |
| 217 | void objd_table_destroy(void) |
| 218 | { |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < objd_table.allocated_len; i++) { |
| 222 | struct lttng_ust_abi_obj *obj; |
| 223 | |
| 224 | obj = _objd_get(i); |
| 225 | if (!obj) |
| 226 | continue; |
| 227 | if (!obj->u.s.owner_ref) |
| 228 | continue; /* only unref owner ref. */ |
| 229 | (void) lttng_ust_abi_objd_unref(i, 1); |
| 230 | } |
| 231 | free(objd_table.array); |
| 232 | objd_table.array = NULL; |
| 233 | objd_table.len = 0; |
| 234 | objd_table.allocated_len = 0; |
| 235 | objd_table.freelist_head = -1; |
| 236 | } |
| 237 | |
| 238 | const char *lttng_ust_obj_get_name(int id) |
| 239 | { |
| 240 | struct lttng_ust_abi_obj *obj = _objd_get(id); |
| 241 | |
| 242 | if (!obj) |
| 243 | return NULL; |
| 244 | return obj->u.s.name; |
| 245 | } |
| 246 | |
| 247 | void lttng_ust_abi_objd_table_owner_cleanup(void *owner) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | for (i = 0; i < objd_table.allocated_len; i++) { |
| 252 | struct lttng_ust_abi_obj *obj; |
| 253 | |
| 254 | obj = _objd_get(i); |
| 255 | if (!obj) |
| 256 | continue; |
| 257 | if (!obj->u.s.owner) |
| 258 | continue; /* skip root handles */ |
| 259 | if (!obj->u.s.owner_ref) |
| 260 | continue; /* only unref owner ref. */ |
| 261 | if (obj->u.s.owner == owner) |
| 262 | (void) lttng_ust_abi_objd_unref(i, 1); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * This is LTTng's own personal way to create an ABI for sessiond. |
| 268 | * We send commands over a socket. |
| 269 | */ |
| 270 | |
| 271 | static const struct lttng_ust_abi_objd_ops lttng_ops; |
| 272 | static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_ops; |
| 273 | static const struct lttng_ust_abi_objd_ops lttng_session_ops; |
| 274 | static const struct lttng_ust_abi_objd_ops lttng_channel_ops; |
| 275 | static const struct lttng_ust_abi_objd_ops lttng_event_enabler_ops; |
| 276 | static const struct lttng_ust_abi_objd_ops lttng_event_notifier_enabler_ops; |
| 277 | static const struct lttng_ust_abi_objd_ops lttng_tracepoint_list_ops; |
| 278 | static const struct lttng_ust_abi_objd_ops lttng_tracepoint_field_list_ops; |
| 279 | |
| 280 | int lttng_abi_create_root_handle(void) |
| 281 | { |
| 282 | int root_handle; |
| 283 | |
| 284 | /* root handles have NULL owners */ |
| 285 | root_handle = objd_alloc(NULL, <tng_ops, NULL, "root"); |
| 286 | return root_handle; |
| 287 | } |
| 288 | |
| 289 | static |
| 290 | int lttng_is_channel_ready(struct lttng_ust_channel_buffer *lttng_chan) |
| 291 | { |
| 292 | struct lttng_ust_lib_ring_buffer_channel *chan; |
| 293 | unsigned int nr_streams, exp_streams; |
| 294 | |
| 295 | chan = lttng_chan->priv->rb_chan; |
| 296 | nr_streams = channel_handle_get_nr_streams(lttng_chan->priv->rb_chan->handle); |
| 297 | exp_streams = chan->nr_streams; |
| 298 | return nr_streams == exp_streams; |
| 299 | } |
| 300 | |
| 301 | static |
| 302 | int lttng_abi_create_session(void *owner) |
| 303 | { |
| 304 | struct lttng_ust_session *session; |
| 305 | int session_objd, ret; |
| 306 | |
| 307 | session = lttng_session_create(); |
| 308 | if (!session) |
| 309 | return -ENOMEM; |
| 310 | session_objd = objd_alloc(session, <tng_session_ops, owner, "session"); |
| 311 | if (session_objd < 0) { |
| 312 | ret = session_objd; |
| 313 | goto objd_error; |
| 314 | } |
| 315 | session->priv->objd = session_objd; |
| 316 | session->priv->owner = owner; |
| 317 | return session_objd; |
| 318 | |
| 319 | objd_error: |
| 320 | lttng_session_destroy(session); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static |
| 325 | long lttng_abi_tracer_version(int objd, |
| 326 | struct lttng_ust_abi_tracer_version *v) |
| 327 | { |
| 328 | v->major = LTTNG_UST_MAJOR_VERSION; |
| 329 | v->minor = LTTNG_UST_MINOR_VERSION; |
| 330 | v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static |
| 335 | int lttng_abi_event_notifier_send_fd(void *owner, int *event_notifier_notif_fd) |
| 336 | { |
| 337 | struct lttng_event_notifier_group *event_notifier_group; |
| 338 | int event_notifier_group_objd, ret, fd_flag; |
| 339 | |
| 340 | event_notifier_group = lttng_event_notifier_group_create(); |
| 341 | if (!event_notifier_group) |
| 342 | return -ENOMEM; |
| 343 | |
| 344 | /* |
| 345 | * Set this file descriptor as NON-BLOCKING. |
| 346 | */ |
| 347 | fd_flag = fcntl(*event_notifier_notif_fd, F_GETFL); |
| 348 | |
| 349 | fd_flag |= O_NONBLOCK; |
| 350 | |
| 351 | ret = fcntl(*event_notifier_notif_fd, F_SETFL, fd_flag); |
| 352 | if (ret) { |
| 353 | ret = -errno; |
| 354 | goto fd_error; |
| 355 | } |
| 356 | |
| 357 | event_notifier_group_objd = objd_alloc(event_notifier_group, |
| 358 | <tng_event_notifier_group_ops, owner, "event_notifier_group"); |
| 359 | if (event_notifier_group_objd < 0) { |
| 360 | ret = event_notifier_group_objd; |
| 361 | goto objd_error; |
| 362 | } |
| 363 | |
| 364 | event_notifier_group->objd = event_notifier_group_objd; |
| 365 | event_notifier_group->owner = owner; |
| 366 | event_notifier_group->notification_fd = *event_notifier_notif_fd; |
| 367 | /* Object descriptor takes ownership of notification fd. */ |
| 368 | *event_notifier_notif_fd = -1; |
| 369 | |
| 370 | return event_notifier_group_objd; |
| 371 | |
| 372 | objd_error: |
| 373 | lttng_event_notifier_group_destroy(event_notifier_group); |
| 374 | fd_error: |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static |
| 379 | long lttng_abi_add_context(int objd, |
| 380 | struct lttng_ust_abi_context *context_param, |
| 381 | union lttng_ust_abi_args *uargs, |
| 382 | struct lttng_ust_ctx **ctx, struct lttng_ust_session *session) |
| 383 | { |
| 384 | return lttng_attach_context(context_param, uargs, ctx, session); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * lttng_cmd - lttng control through socket commands |
| 389 | * |
| 390 | * @objd: the object descriptor |
| 391 | * @cmd: the command |
| 392 | * @arg: command arg |
| 393 | * @uargs: UST arguments (internal) |
| 394 | * @owner: objd owner |
| 395 | * |
| 396 | * This descriptor implements lttng commands: |
| 397 | * LTTNG_UST_ABI_SESSION |
| 398 | * Returns a LTTng trace session object descriptor |
| 399 | * LTTNG_UST_ABI_TRACER_VERSION |
| 400 | * Returns the LTTng kernel tracer version |
| 401 | * LTTNG_UST_ABI_TRACEPOINT_LIST |
| 402 | * Returns a file descriptor listing available tracepoints |
| 403 | * LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST |
| 404 | * Returns a file descriptor listing available tracepoint fields |
| 405 | * LTTNG_UST_ABI_WAIT_QUIESCENT |
| 406 | * Returns after all previously running probes have completed |
| 407 | * |
| 408 | * The returned session will be deleted when its file descriptor is closed. |
| 409 | */ |
| 410 | static |
| 411 | long lttng_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 412 | union lttng_ust_abi_args *uargs, void *owner) |
| 413 | { |
| 414 | switch (cmd) { |
| 415 | case LTTNG_UST_ABI_SESSION: |
| 416 | return lttng_abi_create_session(owner); |
| 417 | case LTTNG_UST_ABI_TRACER_VERSION: |
| 418 | return lttng_abi_tracer_version(objd, |
| 419 | (struct lttng_ust_abi_tracer_version *) arg); |
| 420 | case LTTNG_UST_ABI_TRACEPOINT_LIST: |
| 421 | return lttng_abi_tracepoint_list(owner); |
| 422 | case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST: |
| 423 | return lttng_abi_tracepoint_field_list(owner); |
| 424 | case LTTNG_UST_ABI_WAIT_QUIESCENT: |
| 425 | lttng_ust_urcu_synchronize_rcu(); |
| 426 | return 0; |
| 427 | case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE: |
| 428 | return lttng_abi_event_notifier_send_fd(owner, |
| 429 | &uargs->event_notifier_handle.event_notifier_notif_fd); |
| 430 | default: |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static const struct lttng_ust_abi_objd_ops lttng_ops = { |
| 436 | .cmd = lttng_cmd, |
| 437 | }; |
| 438 | |
| 439 | static |
| 440 | int lttng_abi_map_channel(int session_objd, |
| 441 | struct lttng_ust_abi_channel *ust_chan, |
| 442 | union lttng_ust_abi_args *uargs, |
| 443 | void *owner) |
| 444 | { |
| 445 | struct lttng_ust_session *session = objd_private(session_objd); |
| 446 | const char *transport_name; |
| 447 | struct lttng_transport *transport; |
| 448 | const char *chan_name; |
| 449 | int chan_objd; |
| 450 | struct lttng_ust_shm_handle *channel_handle; |
| 451 | struct lttng_ust_abi_channel_config *lttng_chan_config; |
| 452 | struct lttng_ust_channel_buffer *lttng_chan_buf; |
| 453 | struct lttng_ust_lib_ring_buffer_channel *chan; |
| 454 | struct lttng_ust_lib_ring_buffer_config *config; |
| 455 | void *chan_data; |
| 456 | int wakeup_fd; |
| 457 | uint64_t len; |
| 458 | int ret; |
| 459 | enum lttng_ust_abi_chan_type type; |
| 460 | |
| 461 | chan_data = uargs->channel.chan_data; |
| 462 | wakeup_fd = uargs->channel.wakeup_fd; |
| 463 | len = ust_chan->len; |
| 464 | type = ust_chan->type; |
| 465 | |
| 466 | switch (type) { |
| 467 | case LTTNG_UST_ABI_CHAN_PER_CPU: |
| 468 | break; |
| 469 | default: |
| 470 | ret = -EINVAL; |
| 471 | goto invalid; |
| 472 | } |
| 473 | |
| 474 | if (session->priv->been_active) { |
| 475 | ret = -EBUSY; |
| 476 | goto active; /* Refuse to add channel to active session */ |
| 477 | } |
| 478 | |
| 479 | lttng_chan_buf = lttng_ust_alloc_channel_buffer(); |
| 480 | if (!lttng_chan_buf) { |
| 481 | ret = -ENOMEM; |
| 482 | goto lttng_chan_buf_error; |
| 483 | } |
| 484 | |
| 485 | channel_handle = channel_handle_create(chan_data, len, wakeup_fd); |
| 486 | if (!channel_handle) { |
| 487 | ret = -EINVAL; |
| 488 | goto handle_error; |
| 489 | } |
| 490 | |
| 491 | /* Ownership of chan_data and wakeup_fd taken by channel handle. */ |
| 492 | uargs->channel.chan_data = NULL; |
| 493 | uargs->channel.wakeup_fd = -1; |
| 494 | |
| 495 | chan = shmp(channel_handle, channel_handle->chan); |
| 496 | assert(chan); |
| 497 | chan->handle = channel_handle; |
| 498 | config = &chan->backend.config; |
| 499 | lttng_chan_config = channel_get_private_config(chan); |
| 500 | if (!lttng_chan_config) { |
| 501 | ret = -EINVAL; |
| 502 | goto alloc_error; |
| 503 | } |
| 504 | |
| 505 | if (lttng_ust_session_uuid_validate(session, lttng_chan_config->uuid)) { |
| 506 | ret = -EINVAL; |
| 507 | goto uuid_error; |
| 508 | } |
| 509 | |
| 510 | /* Lookup transport name */ |
| 511 | switch (type) { |
| 512 | case LTTNG_UST_ABI_CHAN_PER_CPU: |
| 513 | if (config->output == RING_BUFFER_MMAP) { |
| 514 | if (config->mode == RING_BUFFER_OVERWRITE) { |
| 515 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) { |
| 516 | transport_name = "relay-overwrite-mmap"; |
| 517 | } else { |
| 518 | transport_name = "relay-overwrite-rt-mmap"; |
| 519 | } |
| 520 | } else { |
| 521 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) { |
| 522 | transport_name = "relay-discard-mmap"; |
| 523 | } else { |
| 524 | transport_name = "relay-discard-rt-mmap"; |
| 525 | } |
| 526 | } |
| 527 | } else { |
| 528 | ret = -EINVAL; |
| 529 | goto notransport; |
| 530 | } |
| 531 | chan_name = "channel"; |
| 532 | break; |
| 533 | default: |
| 534 | ret = -EINVAL; |
| 535 | goto notransport; |
| 536 | } |
| 537 | transport = lttng_ust_transport_find(transport_name); |
| 538 | if (!transport) { |
| 539 | DBG("LTTng transport %s not found\n", |
| 540 | transport_name); |
| 541 | ret = -EINVAL; |
| 542 | goto notransport; |
| 543 | } |
| 544 | |
| 545 | chan_objd = objd_alloc(NULL, <tng_channel_ops, owner, chan_name); |
| 546 | if (chan_objd < 0) { |
| 547 | ret = chan_objd; |
| 548 | goto objd_error; |
| 549 | } |
| 550 | |
| 551 | /* Initialize our lttng chan */ |
| 552 | lttng_chan_buf->parent->enabled = 1; |
| 553 | lttng_chan_buf->parent->session = session; |
| 554 | |
| 555 | lttng_chan_buf->priv->parent.tstate = 1; |
| 556 | lttng_chan_buf->priv->ctx = NULL; |
| 557 | lttng_chan_buf->priv->rb_chan = chan; |
| 558 | |
| 559 | lttng_chan_buf->ops = &transport->ops; |
| 560 | |
| 561 | memcpy(&chan->backend.config, |
| 562 | transport->client_config, |
| 563 | sizeof(chan->backend.config)); |
| 564 | cds_list_add(<tng_chan_buf->priv->node, &session->priv->chan_head); |
| 565 | lttng_chan_buf->priv->header_type = 0; |
| 566 | lttng_chan_buf->priv->type = type; |
| 567 | /* Copy fields from lttng ust chan config. */ |
| 568 | lttng_chan_buf->priv->id = lttng_chan_config->id; |
| 569 | memcpy(lttng_chan_buf->priv->uuid, lttng_chan_config->uuid, LTTNG_UST_UUID_LEN); |
| 570 | channel_set_private(chan, lttng_chan_buf); |
| 571 | |
| 572 | /* |
| 573 | * We tolerate no failure path after channel creation. It will stay |
| 574 | * invariant for the rest of the session. |
| 575 | */ |
| 576 | objd_set_private(chan_objd, lttng_chan_buf); |
| 577 | lttng_chan_buf->priv->parent.objd = chan_objd; |
| 578 | /* The channel created holds a reference on the session */ |
| 579 | objd_ref(session_objd); |
| 580 | return chan_objd; |
| 581 | |
| 582 | /* error path after channel was created */ |
| 583 | objd_error: |
| 584 | notransport: |
| 585 | uuid_error: |
| 586 | alloc_error: |
| 587 | channel_destroy(chan, channel_handle, 0); |
| 588 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
| 589 | return ret; |
| 590 | |
| 591 | handle_error: |
| 592 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
| 593 | lttng_chan_buf_error: |
| 594 | active: |
| 595 | invalid: |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * lttng_session_cmd - lttng session object command |
| 601 | * |
| 602 | * @obj: the object |
| 603 | * @cmd: the command |
| 604 | * @arg: command arg |
| 605 | * @uargs: UST arguments (internal) |
| 606 | * @owner: objd owner |
| 607 | * |
| 608 | * This descriptor implements lttng commands: |
| 609 | * LTTNG_UST_ABI_CHANNEL |
| 610 | * Returns a LTTng channel object descriptor |
| 611 | * LTTNG_UST_ABI_ENABLE |
| 612 | * Enables tracing for a session (weak enable) |
| 613 | * LTTNG_UST_ABI_DISABLE |
| 614 | * Disables tracing for a session (strong disable) |
| 615 | * |
| 616 | * The returned channel will be deleted when its file descriptor is closed. |
| 617 | */ |
| 618 | static |
| 619 | long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 620 | union lttng_ust_abi_args *uargs, void *owner) |
| 621 | { |
| 622 | struct lttng_ust_session *session = objd_private(objd); |
| 623 | |
| 624 | switch (cmd) { |
| 625 | case LTTNG_UST_ABI_CHANNEL: |
| 626 | return lttng_abi_map_channel(objd, |
| 627 | (struct lttng_ust_abi_channel *) arg, |
| 628 | uargs, owner); |
| 629 | case LTTNG_UST_ABI_SESSION_START: |
| 630 | case LTTNG_UST_ABI_ENABLE: |
| 631 | return lttng_session_enable(session); |
| 632 | case LTTNG_UST_ABI_SESSION_STOP: |
| 633 | case LTTNG_UST_ABI_DISABLE: |
| 634 | return lttng_session_disable(session); |
| 635 | case LTTNG_UST_ABI_SESSION_STATEDUMP: |
| 636 | return lttng_session_statedump(session); |
| 637 | case LTTNG_UST_ABI_COUNTER: |
| 638 | case LTTNG_UST_ABI_COUNTER_GLOBAL: |
| 639 | case LTTNG_UST_ABI_COUNTER_CPU: |
| 640 | /* Not implemented yet. */ |
| 641 | return -EINVAL; |
| 642 | default: |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Called when the last file reference is dropped. |
| 649 | * |
| 650 | * Big fat note: channels and events are invariant for the whole session after |
| 651 | * their creation. So this session destruction also destroys all channel and |
| 652 | * event structures specific to this session (they are not destroyed when their |
| 653 | * individual file is released). |
| 654 | */ |
| 655 | static |
| 656 | int lttng_release_session(int objd) |
| 657 | { |
| 658 | struct lttng_ust_session *session = objd_private(objd); |
| 659 | |
| 660 | if (session) { |
| 661 | lttng_session_destroy(session); |
| 662 | return 0; |
| 663 | } else { |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static const struct lttng_ust_abi_objd_ops lttng_session_ops = { |
| 669 | .release = lttng_release_session, |
| 670 | .cmd = lttng_session_cmd, |
| 671 | }; |
| 672 | |
| 673 | static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj, |
| 674 | void *owner, struct lttng_ust_abi_event_notifier *event_notifier_param, |
| 675 | enum lttng_enabler_format_type type) |
| 676 | { |
| 677 | struct lttng_event_notifier_group *event_notifier_group = |
| 678 | objd_private(event_notifier_group_obj); |
| 679 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 680 | int event_notifier_objd, ret; |
| 681 | |
| 682 | event_notifier_param->event.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 683 | event_notifier_objd = objd_alloc(NULL, <tng_event_notifier_enabler_ops, owner, |
| 684 | "event_notifier enabler"); |
| 685 | if (event_notifier_objd < 0) { |
| 686 | ret = event_notifier_objd; |
| 687 | goto objd_error; |
| 688 | } |
| 689 | |
| 690 | event_notifier_enabler = lttng_event_notifier_enabler_create( |
| 691 | event_notifier_group, type, event_notifier_param); |
| 692 | if (!event_notifier_enabler) { |
| 693 | ret = -ENOMEM; |
| 694 | goto event_notifier_error; |
| 695 | } |
| 696 | |
| 697 | objd_set_private(event_notifier_objd, event_notifier_enabler); |
| 698 | /* The event_notifier holds a reference on the event_notifier group. */ |
| 699 | objd_ref(event_notifier_enabler->group->objd); |
| 700 | |
| 701 | return event_notifier_objd; |
| 702 | |
| 703 | event_notifier_error: |
| 704 | { |
| 705 | int err; |
| 706 | |
| 707 | err = lttng_ust_abi_objd_unref(event_notifier_objd, 1); |
| 708 | assert(!err); |
| 709 | } |
| 710 | objd_error: |
| 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | static |
| 715 | long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 716 | union lttng_ust_abi_args *uargs, void *owner) |
| 717 | { |
| 718 | struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd); |
| 719 | switch (cmd) { |
| 720 | case LTTNG_UST_ABI_FILTER: |
| 721 | return lttng_event_notifier_enabler_attach_filter_bytecode( |
| 722 | event_notifier_enabler, |
| 723 | (struct lttng_ust_bytecode_node **) arg); |
| 724 | case LTTNG_UST_ABI_EXCLUSION: |
| 725 | return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler, |
| 726 | (struct lttng_ust_excluder_node **) arg); |
| 727 | case LTTNG_UST_ABI_CAPTURE: |
| 728 | return lttng_event_notifier_enabler_attach_capture_bytecode( |
| 729 | event_notifier_enabler, |
| 730 | (struct lttng_ust_bytecode_node **) arg); |
| 731 | case LTTNG_UST_ABI_ENABLE: |
| 732 | return lttng_event_notifier_enabler_enable(event_notifier_enabler); |
| 733 | case LTTNG_UST_ABI_DISABLE: |
| 734 | return lttng_event_notifier_enabler_disable(event_notifier_enabler); |
| 735 | default: |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * lttng_event_notifier_group_error_counter_cmd - lttng event_notifier group error counter object command |
| 742 | * |
| 743 | * @obj: the object |
| 744 | * @cmd: the command |
| 745 | * @arg: command arg |
| 746 | * @uargs: UST arguments (internal) |
| 747 | * @owner: objd owner |
| 748 | * |
| 749 | * This descriptor implements lttng commands: |
| 750 | * LTTNG_UST_ABI_COUNTER_GLOBAL |
| 751 | * Return negative error code on error, 0 on success. |
| 752 | * LTTNG_UST_ABI_COUNTER_CPU |
| 753 | * Return negative error code on error, 0 on success. |
| 754 | */ |
| 755 | static |
| 756 | long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 757 | union lttng_ust_abi_args *uargs, void *owner) |
| 758 | { |
| 759 | int ret; |
| 760 | struct lttng_counter *counter = objd_private(objd); |
| 761 | |
| 762 | switch (cmd) { |
| 763 | case LTTNG_UST_ABI_COUNTER_GLOBAL: |
| 764 | ret = -EINVAL; /* Unimplemented. */ |
| 765 | break; |
| 766 | case LTTNG_UST_ABI_COUNTER_CPU: |
| 767 | { |
| 768 | struct lttng_ust_abi_counter_cpu *counter_cpu = |
| 769 | (struct lttng_ust_abi_counter_cpu *)arg; |
| 770 | |
| 771 | ret = lttng_counter_set_cpu_shm(counter->counter, |
| 772 | counter_cpu->cpu_nr, uargs->counter_shm.shm_fd); |
| 773 | if (!ret) { |
| 774 | /* Take ownership of the shm_fd. */ |
| 775 | uargs->counter_shm.shm_fd = -1; |
| 776 | } |
| 777 | break; |
| 778 | } |
| 779 | default: |
| 780 | ret = -EINVAL; |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | __attribute__((visibility("hidden"))) |
| 788 | int lttng_release_event_notifier_group_error_counter(int objd) |
| 789 | { |
| 790 | struct lttng_counter *counter = objd_private(objd); |
| 791 | |
| 792 | if (counter) { |
| 793 | return lttng_ust_abi_objd_unref(counter->event_notifier_group->objd, 0); |
| 794 | } else { |
| 795 | return -EINVAL; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_error_counter_ops = { |
| 800 | .release = lttng_release_event_notifier_group_error_counter, |
| 801 | .cmd = lttng_event_notifier_group_error_counter_cmd, |
| 802 | }; |
| 803 | |
| 804 | static |
| 805 | int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner, |
| 806 | struct lttng_ust_abi_counter_conf *error_counter_conf) |
| 807 | { |
| 808 | const char *counter_transport_name; |
| 809 | struct lttng_event_notifier_group *event_notifier_group = |
| 810 | objd_private(event_notifier_group_objd); |
| 811 | struct lttng_counter *counter; |
| 812 | int counter_objd, ret; |
| 813 | struct lttng_counter_dimension dimensions[1]; |
| 814 | size_t counter_len; |
| 815 | |
| 816 | if (event_notifier_group->error_counter) |
| 817 | return -EBUSY; |
| 818 | |
| 819 | if (error_counter_conf->arithmetic != LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR) |
| 820 | return -EINVAL; |
| 821 | |
| 822 | if (error_counter_conf->number_dimensions != 1) |
| 823 | return -EINVAL; |
| 824 | |
| 825 | switch (error_counter_conf->bitness) { |
| 826 | case LTTNG_UST_ABI_COUNTER_BITNESS_64: |
| 827 | counter_transport_name = "counter-per-cpu-64-modular"; |
| 828 | break; |
| 829 | case LTTNG_UST_ABI_COUNTER_BITNESS_32: |
| 830 | counter_transport_name = "counter-per-cpu-32-modular"; |
| 831 | break; |
| 832 | default: |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | |
| 836 | counter_objd = objd_alloc(NULL, <tng_event_notifier_group_error_counter_ops, owner, |
| 837 | "event_notifier group error counter"); |
| 838 | if (counter_objd < 0) { |
| 839 | ret = counter_objd; |
| 840 | goto objd_error; |
| 841 | } |
| 842 | |
| 843 | counter_len = error_counter_conf->dimensions[0].size; |
| 844 | dimensions[0].size = counter_len; |
| 845 | dimensions[0].underflow_index = 0; |
| 846 | dimensions[0].overflow_index = 0; |
| 847 | dimensions[0].has_underflow = 0; |
| 848 | dimensions[0].has_overflow = 0; |
| 849 | |
| 850 | counter = lttng_ust_counter_create(counter_transport_name, 1, dimensions); |
| 851 | if (!counter) { |
| 852 | ret = -EINVAL; |
| 853 | goto create_error; |
| 854 | } |
| 855 | |
| 856 | event_notifier_group->error_counter_len = counter_len; |
| 857 | /* |
| 858 | * store-release to publish error counter matches load-acquire |
| 859 | * in record_error. Ensures the counter is created and the |
| 860 | * error_counter_len is set before they are used. |
| 861 | * Currently a full memory barrier is used, which could be |
| 862 | * turned into acquire-release barriers. |
| 863 | */ |
| 864 | cmm_smp_mb(); |
| 865 | CMM_STORE_SHARED(event_notifier_group->error_counter, counter); |
| 866 | |
| 867 | counter->objd = counter_objd; |
| 868 | counter->event_notifier_group = event_notifier_group; /* owner */ |
| 869 | |
| 870 | objd_set_private(counter_objd, counter); |
| 871 | /* The error counter holds a reference on the event_notifier group. */ |
| 872 | objd_ref(event_notifier_group->objd); |
| 873 | |
| 874 | return counter_objd; |
| 875 | |
| 876 | create_error: |
| 877 | { |
| 878 | int err; |
| 879 | |
| 880 | err = lttng_ust_abi_objd_unref(counter_objd, 1); |
| 881 | assert(!err); |
| 882 | } |
| 883 | objd_error: |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | static |
| 888 | long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 889 | union lttng_ust_abi_args *uargs, void *owner) |
| 890 | { |
| 891 | switch (cmd) { |
| 892 | case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE: |
| 893 | { |
| 894 | struct lttng_ust_abi_event_notifier *event_notifier_param = |
| 895 | (struct lttng_ust_abi_event_notifier *) arg; |
| 896 | if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) { |
| 897 | /* |
| 898 | * If the event name is a star globbing pattern, |
| 899 | * we create the special star globbing enabler. |
| 900 | */ |
| 901 | return lttng_ust_event_notifier_enabler_create(objd, |
| 902 | owner, event_notifier_param, |
| 903 | LTTNG_ENABLER_FORMAT_STAR_GLOB); |
| 904 | } else { |
| 905 | return lttng_ust_event_notifier_enabler_create(objd, |
| 906 | owner, event_notifier_param, |
| 907 | LTTNG_ENABLER_FORMAT_EVENT); |
| 908 | } |
| 909 | } |
| 910 | case LTTNG_UST_ABI_COUNTER: |
| 911 | { |
| 912 | struct lttng_ust_abi_counter_conf *counter_conf = |
| 913 | (struct lttng_ust_abi_counter_conf *) uargs->counter.counter_data; |
| 914 | return lttng_ust_event_notifier_group_create_error_counter( |
| 915 | objd, owner, counter_conf); |
| 916 | } |
| 917 | default: |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static |
| 923 | int lttng_event_notifier_enabler_release(int objd) |
| 924 | { |
| 925 | struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd); |
| 926 | |
| 927 | if (event_notifier_enabler) |
| 928 | return lttng_ust_abi_objd_unref(event_notifier_enabler->group->objd, 0); |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static const struct lttng_ust_abi_objd_ops lttng_event_notifier_enabler_ops = { |
| 933 | .release = lttng_event_notifier_enabler_release, |
| 934 | .cmd = lttng_event_notifier_enabler_cmd, |
| 935 | }; |
| 936 | |
| 937 | static |
| 938 | int lttng_release_event_notifier_group(int objd) |
| 939 | { |
| 940 | struct lttng_event_notifier_group *event_notifier_group = objd_private(objd); |
| 941 | |
| 942 | if (event_notifier_group) { |
| 943 | lttng_event_notifier_group_destroy(event_notifier_group); |
| 944 | return 0; |
| 945 | } else { |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | static const struct lttng_ust_abi_objd_ops lttng_event_notifier_group_ops = { |
| 951 | .release = lttng_release_event_notifier_group, |
| 952 | .cmd = lttng_event_notifier_group_cmd, |
| 953 | }; |
| 954 | |
| 955 | static |
| 956 | long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 957 | union lttng_ust_abi_args *uargs, void *owner) |
| 958 | { |
| 959 | struct lttng_ust_tracepoint_list *list = objd_private(objd); |
| 960 | struct lttng_ust_abi_tracepoint_iter *tp = |
| 961 | (struct lttng_ust_abi_tracepoint_iter *) arg; |
| 962 | struct lttng_ust_abi_tracepoint_iter *iter; |
| 963 | |
| 964 | switch (cmd) { |
| 965 | case LTTNG_UST_ABI_TRACEPOINT_LIST_GET: |
| 966 | { |
| 967 | iter = lttng_ust_tracepoint_list_get_iter_next(list); |
| 968 | if (!iter) |
| 969 | return -LTTNG_UST_ERR_NOENT; |
| 970 | memcpy(tp, iter, sizeof(*tp)); |
| 971 | return 0; |
| 972 | } |
| 973 | default: |
| 974 | return -EINVAL; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | static |
| 979 | int lttng_abi_tracepoint_list(void *owner) |
| 980 | { |
| 981 | int list_objd, ret; |
| 982 | struct lttng_ust_tracepoint_list *list; |
| 983 | |
| 984 | list_objd = objd_alloc(NULL, <tng_tracepoint_list_ops, owner, "tp_list"); |
| 985 | if (list_objd < 0) { |
| 986 | ret = list_objd; |
| 987 | goto objd_error; |
| 988 | } |
| 989 | list = zmalloc(sizeof(*list)); |
| 990 | if (!list) { |
| 991 | ret = -ENOMEM; |
| 992 | goto alloc_error; |
| 993 | } |
| 994 | objd_set_private(list_objd, list); |
| 995 | |
| 996 | /* populate list by walking on all registered probes. */ |
| 997 | ret = lttng_probes_get_event_list(list); |
| 998 | if (ret) { |
| 999 | goto list_error; |
| 1000 | } |
| 1001 | return list_objd; |
| 1002 | |
| 1003 | list_error: |
| 1004 | free(list); |
| 1005 | alloc_error: |
| 1006 | { |
| 1007 | int err; |
| 1008 | |
| 1009 | err = lttng_ust_abi_objd_unref(list_objd, 1); |
| 1010 | assert(!err); |
| 1011 | } |
| 1012 | objd_error: |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | static |
| 1017 | int lttng_release_tracepoint_list(int objd) |
| 1018 | { |
| 1019 | struct lttng_ust_tracepoint_list *list = objd_private(objd); |
| 1020 | |
| 1021 | if (list) { |
| 1022 | lttng_probes_prune_event_list(list); |
| 1023 | free(list); |
| 1024 | return 0; |
| 1025 | } else { |
| 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static const struct lttng_ust_abi_objd_ops lttng_tracepoint_list_ops = { |
| 1031 | .release = lttng_release_tracepoint_list, |
| 1032 | .cmd = lttng_tracepoint_list_cmd, |
| 1033 | }; |
| 1034 | |
| 1035 | static |
| 1036 | long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd, |
| 1037 | unsigned long arg, union lttng_ust_abi_args *uargs, void *owner) |
| 1038 | { |
| 1039 | struct lttng_ust_field_list *list = objd_private(objd); |
| 1040 | struct lttng_ust_abi_field_iter *tp = &uargs->field_list.entry; |
| 1041 | struct lttng_ust_abi_field_iter *iter; |
| 1042 | |
| 1043 | switch (cmd) { |
| 1044 | case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET: |
| 1045 | { |
| 1046 | iter = lttng_ust_field_list_get_iter_next(list); |
| 1047 | if (!iter) |
| 1048 | return -LTTNG_UST_ERR_NOENT; |
| 1049 | memcpy(tp, iter, sizeof(*tp)); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | default: |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static |
| 1058 | int lttng_abi_tracepoint_field_list(void *owner) |
| 1059 | { |
| 1060 | int list_objd, ret; |
| 1061 | struct lttng_ust_field_list *list; |
| 1062 | |
| 1063 | list_objd = objd_alloc(NULL, <tng_tracepoint_field_list_ops, owner, |
| 1064 | "tp_field_list"); |
| 1065 | if (list_objd < 0) { |
| 1066 | ret = list_objd; |
| 1067 | goto objd_error; |
| 1068 | } |
| 1069 | list = zmalloc(sizeof(*list)); |
| 1070 | if (!list) { |
| 1071 | ret = -ENOMEM; |
| 1072 | goto alloc_error; |
| 1073 | } |
| 1074 | objd_set_private(list_objd, list); |
| 1075 | |
| 1076 | /* populate list by walking on all registered probes. */ |
| 1077 | ret = lttng_probes_get_field_list(list); |
| 1078 | if (ret) { |
| 1079 | goto list_error; |
| 1080 | } |
| 1081 | return list_objd; |
| 1082 | |
| 1083 | list_error: |
| 1084 | free(list); |
| 1085 | alloc_error: |
| 1086 | { |
| 1087 | int err; |
| 1088 | |
| 1089 | err = lttng_ust_abi_objd_unref(list_objd, 1); |
| 1090 | assert(!err); |
| 1091 | } |
| 1092 | objd_error: |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | static |
| 1097 | int lttng_release_tracepoint_field_list(int objd) |
| 1098 | { |
| 1099 | struct lttng_ust_field_list *list = objd_private(objd); |
| 1100 | |
| 1101 | if (list) { |
| 1102 | lttng_probes_prune_field_list(list); |
| 1103 | free(list); |
| 1104 | return 0; |
| 1105 | } else { |
| 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | static const struct lttng_ust_abi_objd_ops lttng_tracepoint_field_list_ops = { |
| 1111 | .release = lttng_release_tracepoint_field_list, |
| 1112 | .cmd = lttng_tracepoint_field_list_cmd, |
| 1113 | }; |
| 1114 | |
| 1115 | static |
| 1116 | int lttng_abi_map_stream(int channel_objd, struct lttng_ust_abi_stream *info, |
| 1117 | union lttng_ust_abi_args *uargs, void *owner) |
| 1118 | { |
| 1119 | struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(channel_objd); |
| 1120 | int ret; |
| 1121 | |
| 1122 | ret = channel_handle_add_stream(lttng_chan_buf->priv->rb_chan->handle, |
| 1123 | uargs->stream.shm_fd, uargs->stream.wakeup_fd, |
| 1124 | info->stream_nr, info->len); |
| 1125 | if (ret) |
| 1126 | goto error_add_stream; |
| 1127 | /* Take ownership of shm_fd and wakeup_fd. */ |
| 1128 | uargs->stream.shm_fd = -1; |
| 1129 | uargs->stream.wakeup_fd = -1; |
| 1130 | |
| 1131 | return 0; |
| 1132 | |
| 1133 | error_add_stream: |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | static |
| 1138 | int lttng_abi_create_event_enabler(int channel_objd, |
| 1139 | struct lttng_ust_abi_event *event_param, |
| 1140 | void *owner, |
| 1141 | enum lttng_enabler_format_type format_type) |
| 1142 | { |
| 1143 | struct lttng_ust_channel_buffer *channel = objd_private(channel_objd); |
| 1144 | struct lttng_event_enabler *enabler; |
| 1145 | int event_objd, ret; |
| 1146 | |
| 1147 | event_param->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 1148 | event_objd = objd_alloc(NULL, <tng_event_enabler_ops, owner, |
| 1149 | "event enabler"); |
| 1150 | if (event_objd < 0) { |
| 1151 | ret = event_objd; |
| 1152 | goto objd_error; |
| 1153 | } |
| 1154 | /* |
| 1155 | * We tolerate no failure path after event creation. It will stay |
| 1156 | * invariant for the rest of the session. |
| 1157 | */ |
| 1158 | enabler = lttng_event_enabler_create(format_type, event_param, channel); |
| 1159 | if (!enabler) { |
| 1160 | ret = -ENOMEM; |
| 1161 | goto event_error; |
| 1162 | } |
| 1163 | objd_set_private(event_objd, enabler); |
| 1164 | /* The event holds a reference on the channel */ |
| 1165 | objd_ref(channel_objd); |
| 1166 | return event_objd; |
| 1167 | |
| 1168 | event_error: |
| 1169 | { |
| 1170 | int err; |
| 1171 | |
| 1172 | err = lttng_ust_abi_objd_unref(event_objd, 1); |
| 1173 | assert(!err); |
| 1174 | } |
| 1175 | objd_error: |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | /** |
| 1180 | * lttng_channel_cmd - lttng control through object descriptors |
| 1181 | * |
| 1182 | * @objd: the object descriptor |
| 1183 | * @cmd: the command |
| 1184 | * @arg: command arg |
| 1185 | * @uargs: UST arguments (internal) |
| 1186 | * @owner: objd owner |
| 1187 | * |
| 1188 | * This object descriptor implements lttng commands: |
| 1189 | * LTTNG_UST_ABI_STREAM |
| 1190 | * Returns an event stream object descriptor or failure. |
| 1191 | * (typically, one event stream records events from one CPU) |
| 1192 | * LTTNG_UST_ABI_EVENT |
| 1193 | * Returns an event object descriptor or failure. |
| 1194 | * LTTNG_UST_ABI_CONTEXT |
| 1195 | * Prepend a context field to each event in the channel |
| 1196 | * LTTNG_UST_ABI_ENABLE |
| 1197 | * Enable recording for events in this channel (weak enable) |
| 1198 | * LTTNG_UST_ABI_DISABLE |
| 1199 | * Disable recording for events in this channel (strong disable) |
| 1200 | * |
| 1201 | * Channel and event file descriptors also hold a reference on the session. |
| 1202 | */ |
| 1203 | static |
| 1204 | long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 1205 | union lttng_ust_abi_args *uargs, void *owner) |
| 1206 | { |
| 1207 | struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(objd); |
| 1208 | |
| 1209 | if (cmd != LTTNG_UST_ABI_STREAM) { |
| 1210 | /* |
| 1211 | * Check if channel received all streams. |
| 1212 | */ |
| 1213 | if (!lttng_is_channel_ready(lttng_chan_buf)) |
| 1214 | return -EPERM; |
| 1215 | } |
| 1216 | |
| 1217 | switch (cmd) { |
| 1218 | case LTTNG_UST_ABI_STREAM: |
| 1219 | { |
| 1220 | struct lttng_ust_abi_stream *stream; |
| 1221 | |
| 1222 | stream = (struct lttng_ust_abi_stream *) arg; |
| 1223 | /* stream used as output */ |
| 1224 | return lttng_abi_map_stream(objd, stream, uargs, owner); |
| 1225 | } |
| 1226 | case LTTNG_UST_ABI_EVENT: |
| 1227 | { |
| 1228 | struct lttng_ust_abi_event *event_param = |
| 1229 | (struct lttng_ust_abi_event *) arg; |
| 1230 | |
| 1231 | if (strutils_is_star_glob_pattern(event_param->name)) { |
| 1232 | /* |
| 1233 | * If the event name is a star globbing pattern, |
| 1234 | * we create the special star globbing enabler. |
| 1235 | */ |
| 1236 | return lttng_abi_create_event_enabler(objd, event_param, |
| 1237 | owner, LTTNG_ENABLER_FORMAT_STAR_GLOB); |
| 1238 | } else { |
| 1239 | return lttng_abi_create_event_enabler(objd, event_param, |
| 1240 | owner, LTTNG_ENABLER_FORMAT_EVENT); |
| 1241 | } |
| 1242 | } |
| 1243 | case LTTNG_UST_ABI_CONTEXT: |
| 1244 | return lttng_abi_add_context(objd, |
| 1245 | (struct lttng_ust_abi_context *) arg, uargs, |
| 1246 | <tng_chan_buf->priv->ctx, |
| 1247 | lttng_chan_buf->parent->session); |
| 1248 | case LTTNG_UST_ABI_ENABLE: |
| 1249 | return lttng_channel_enable(lttng_chan_buf->parent); |
| 1250 | case LTTNG_UST_ABI_DISABLE: |
| 1251 | return lttng_channel_disable(lttng_chan_buf->parent); |
| 1252 | case LTTNG_UST_ABI_FLUSH_BUFFER: |
| 1253 | return lttng_chan_buf->ops->priv->flush_buffer(lttng_chan_buf); |
| 1254 | default: |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | static |
| 1260 | int lttng_channel_release(int objd) |
| 1261 | { |
| 1262 | struct lttng_ust_channel_buffer *lttng_chan_buf = objd_private(objd); |
| 1263 | |
| 1264 | if (lttng_chan_buf) |
| 1265 | return lttng_ust_abi_objd_unref(lttng_chan_buf->parent->session->priv->objd, 0); |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static const struct lttng_ust_abi_objd_ops lttng_channel_ops = { |
| 1270 | .release = lttng_channel_release, |
| 1271 | .cmd = lttng_channel_cmd, |
| 1272 | }; |
| 1273 | |
| 1274 | /** |
| 1275 | * lttng_enabler_cmd - lttng control through object descriptors |
| 1276 | * |
| 1277 | * @objd: the object descriptor |
| 1278 | * @cmd: the command |
| 1279 | * @arg: command arg |
| 1280 | * @uargs: UST arguments (internal) |
| 1281 | * @owner: objd owner |
| 1282 | * |
| 1283 | * This object descriptor implements lttng commands: |
| 1284 | * LTTNG_UST_ABI_CONTEXT |
| 1285 | * Prepend a context field to each record of events of this |
| 1286 | * enabler. |
| 1287 | * LTTNG_UST_ABI_ENABLE |
| 1288 | * Enable recording for this enabler |
| 1289 | * LTTNG_UST_ABI_DISABLE |
| 1290 | * Disable recording for this enabler |
| 1291 | * LTTNG_UST_ABI_FILTER |
| 1292 | * Attach a filter to an enabler. |
| 1293 | * LTTNG_UST_ABI_EXCLUSION |
| 1294 | * Attach exclusions to an enabler. |
| 1295 | */ |
| 1296 | static |
| 1297 | long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg, |
| 1298 | union lttng_ust_abi_args *uargs, void *owner) |
| 1299 | { |
| 1300 | struct lttng_event_enabler *enabler = objd_private(objd); |
| 1301 | |
| 1302 | switch (cmd) { |
| 1303 | case LTTNG_UST_ABI_CONTEXT: |
| 1304 | return lttng_event_enabler_attach_context(enabler, |
| 1305 | (struct lttng_ust_abi_context *) arg); |
| 1306 | case LTTNG_UST_ABI_ENABLE: |
| 1307 | return lttng_event_enabler_enable(enabler); |
| 1308 | case LTTNG_UST_ABI_DISABLE: |
| 1309 | return lttng_event_enabler_disable(enabler); |
| 1310 | case LTTNG_UST_ABI_FILTER: |
| 1311 | { |
| 1312 | int ret; |
| 1313 | |
| 1314 | ret = lttng_event_enabler_attach_filter_bytecode(enabler, |
| 1315 | (struct lttng_ust_bytecode_node **) arg); |
| 1316 | if (ret) |
| 1317 | return ret; |
| 1318 | return 0; |
| 1319 | } |
| 1320 | case LTTNG_UST_ABI_EXCLUSION: |
| 1321 | { |
| 1322 | return lttng_event_enabler_attach_exclusion(enabler, |
| 1323 | (struct lttng_ust_excluder_node **) arg); |
| 1324 | } |
| 1325 | default: |
| 1326 | return -EINVAL; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | static |
| 1331 | int lttng_event_enabler_release(int objd) |
| 1332 | { |
| 1333 | struct lttng_event_enabler *event_enabler = objd_private(objd); |
| 1334 | |
| 1335 | if (event_enabler) |
| 1336 | return lttng_ust_abi_objd_unref(event_enabler->chan->priv->parent.objd, 0); |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | static const struct lttng_ust_abi_objd_ops lttng_event_enabler_ops = { |
| 1342 | .release = lttng_event_enabler_release, |
| 1343 | .cmd = lttng_event_enabler_cmd, |
| 1344 | }; |
| 1345 | |
| 1346 | void lttng_ust_abi_exit(void) |
| 1347 | { |
| 1348 | lttng_ust_abi_close_in_progress = 1; |
| 1349 | ust_lock_nocheck(); |
| 1350 | objd_table_destroy(); |
| 1351 | ust_unlock(); |
| 1352 | lttng_ust_abi_close_in_progress = 0; |
| 1353 | } |