| 1 | /* |
| 2 | * lttng-context-vtid.c |
| 3 | * |
| 4 | * LTTng UST vtid context. |
| 5 | * |
| 6 | * Copyright (C) 2009-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 | #define _LGPL_SOURCE |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #include <lttng/ust-events.h> |
| 27 | #include <lttng/ust-tracer.h> |
| 28 | #include <lttng/ringbuffer-config.h> |
| 29 | #include <lttng/ust-tid.h> |
| 30 | #include <urcu/tls-compat.h> |
| 31 | #include "lttng-tracer-core.h" |
| 32 | |
| 33 | /* |
| 34 | * We cache the result to ensure we don't trigger a system call for |
| 35 | * each event. |
| 36 | */ |
| 37 | static DEFINE_URCU_TLS(pid_t, cached_vtid); |
| 38 | |
| 39 | /* |
| 40 | * Upon fork or clone, the TID assigned to our thread is not the same as |
| 41 | * we kept in cache. Luckily, we are the only thread surviving in the |
| 42 | * child process, so we can simply clear our cached version. |
| 43 | */ |
| 44 | void lttng_context_vtid_reset(void) |
| 45 | { |
| 46 | URCU_TLS(cached_vtid) = 0; |
| 47 | } |
| 48 | |
| 49 | static |
| 50 | size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset) |
| 51 | { |
| 52 | size_t size = 0; |
| 53 | |
| 54 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 55 | size += sizeof(pid_t); |
| 56 | return size; |
| 57 | } |
| 58 | |
| 59 | static |
| 60 | void vtid_record(struct lttng_ctx_field *field, |
| 61 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 62 | struct lttng_channel *chan) |
| 63 | { |
| 64 | if (caa_unlikely(!URCU_TLS(cached_vtid))) |
| 65 | URCU_TLS(cached_vtid) = gettid(); |
| 66 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(URCU_TLS(cached_vtid))); |
| 67 | chan->ops->event_write(ctx, &URCU_TLS(cached_vtid), |
| 68 | sizeof(URCU_TLS(cached_vtid))); |
| 69 | } |
| 70 | |
| 71 | static |
| 72 | void vtid_get_value(struct lttng_ctx_field *field, |
| 73 | struct lttng_ctx_value *value) |
| 74 | { |
| 75 | if (caa_unlikely(!URCU_TLS(cached_vtid))) |
| 76 | URCU_TLS(cached_vtid) = gettid(); |
| 77 | value->u.s64 = URCU_TLS(cached_vtid); |
| 78 | } |
| 79 | |
| 80 | int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx) |
| 81 | { |
| 82 | struct lttng_ctx_field *field; |
| 83 | |
| 84 | field = lttng_append_context(ctx); |
| 85 | if (!field) |
| 86 | return -ENOMEM; |
| 87 | if (lttng_find_context(*ctx, "vtid")) { |
| 88 | lttng_remove_context_field(ctx, field); |
| 89 | return -EEXIST; |
| 90 | } |
| 91 | field->event_field.name = "vtid"; |
| 92 | field->event_field.type.atype = atype_integer; |
| 93 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 94 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 95 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); |
| 96 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 97 | field->event_field.type.u.basic.integer.base = 10; |
| 98 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 99 | field->get_size = vtid_get_size; |
| 100 | field->record = vtid_record; |
| 101 | field->get_value = vtid_get_value; |
| 102 | lttng_context_update(*ctx); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 108 | */ |
| 109 | void lttng_fixup_vtid_tls(void) |
| 110 | { |
| 111 | asm volatile ("" : : "m" (URCU_TLS(cached_vtid))); |
| 112 | } |