| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng UST vtid context. |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <limits.h> |
| 11 | #include <stddef.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | #include <lttng/ust-events.h> |
| 15 | #include <lttng/ust-tracer.h> |
| 16 | #include <lttng/ringbuffer-context.h> |
| 17 | #include "common/compat/tid.h" |
| 18 | #include <urcu/tls-compat.h> |
| 19 | |
| 20 | #include "context-internal.h" |
| 21 | #include "lttng-tracer-core.h" |
| 22 | |
| 23 | /* |
| 24 | * We cache the result to ensure we don't trigger a system call for |
| 25 | * each event. |
| 26 | */ |
| 27 | static DEFINE_URCU_TLS(pid_t, cached_vtid); |
| 28 | |
| 29 | /* |
| 30 | * Upon fork or clone, the TID assigned to our thread is not the same as |
| 31 | * we kept in cache. Luckily, we are the only thread surviving in the |
| 32 | * child process, so we can simply clear our cached version. |
| 33 | */ |
| 34 | void lttng_context_vtid_reset(void) |
| 35 | { |
| 36 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0); |
| 37 | } |
| 38 | |
| 39 | static |
| 40 | size_t vtid_get_size(void *priv __attribute__((unused)), |
| 41 | size_t offset) |
| 42 | { |
| 43 | size_t size = 0; |
| 44 | |
| 45 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t)); |
| 46 | size += sizeof(pid_t); |
| 47 | return size; |
| 48 | } |
| 49 | |
| 50 | static inline |
| 51 | pid_t wrapper_getvtid(void) |
| 52 | { |
| 53 | pid_t vtid; |
| 54 | |
| 55 | vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid)); |
| 56 | if (caa_unlikely(!vtid)) { |
| 57 | vtid = lttng_gettid(); |
| 58 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid); |
| 59 | } |
| 60 | return vtid; |
| 61 | } |
| 62 | |
| 63 | static |
| 64 | void vtid_record(void *priv __attribute__((unused)), |
| 65 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 66 | struct lttng_ust_channel_buffer *chan) |
| 67 | { |
| 68 | pid_t vtid = wrapper_getvtid(); |
| 69 | |
| 70 | chan->ops->event_write(ctx, &vtid, sizeof(vtid), lttng_ust_rb_alignof(vtid)); |
| 71 | } |
| 72 | |
| 73 | static |
| 74 | void vtid_get_value(void *priv __attribute__((unused)), |
| 75 | struct lttng_ust_ctx_value *value) |
| 76 | { |
| 77 | value->u.s64 = wrapper_getvtid(); |
| 78 | } |
| 79 | |
| 80 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
| 81 | lttng_ust_static_event_field("vtid", |
| 82 | lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT, |
| 83 | lttng_ust_rb_alignof(pid_t) * CHAR_BIT, |
| 84 | lttng_ust_is_signed_type(pid_t), |
| 85 | BYTE_ORDER, 10), |
| 86 | false, false), |
| 87 | vtid_get_size, |
| 88 | vtid_record, |
| 89 | vtid_get_value, |
| 90 | NULL, NULL); |
| 91 | |
| 92 | int lttng_add_vtid_to_ctx(struct lttng_ust_ctx **ctx) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
| 97 | ret = -EEXIST; |
| 98 | goto error_find_context; |
| 99 | } |
| 100 | ret = lttng_ust_context_append(ctx, ctx_field); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | return 0; |
| 104 | |
| 105 | error_find_context: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 111 | */ |
| 112 | void lttng_fixup_vtid_tls(void) |
| 113 | { |
| 114 | asm volatile ("" : : "m" (URCU_TLS(cached_vtid))); |
| 115 | } |