Fix: fixup vtid TLS
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
1 /*
2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * LTTng UST vtid context.
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <lttng/ust-events.h>
13 #include <lttng/ust-tracer.h>
14 #include <lttng/ringbuffer-config.h>
15 #include "ltt-tracer-core.h"
16
17 #ifdef __linux__
18 #include <syscall.h>
19 #endif
20
21 #if defined(_syscall0)
22 _syscall0(pid_t, gettid)
23 #elif defined(__NR_gettid)
24 static inline pid_t gettid(void)
25 {
26 return syscall(__NR_gettid);
27 }
28 #else
29 #warning "use pid as tid"
30 static inline pid_t gettid(void)
31 {
32 return getpid();
33 }
34 #endif
35
36 /*
37 * We cache the result to ensure we don't trigger a system call for
38 * each event.
39 */
40 static __thread pid_t cached_vtid;
41
42 /*
43 * Upon fork or clone, the TID assigned to our thread is not the same as
44 * we kept in cache. Luckily, we are the only thread surviving in the
45 * child process, so we can simply clear our cached version.
46 */
47 void lttng_context_vtid_reset(void)
48 {
49 cached_vtid = 0;
50 }
51
52 static
53 size_t vtid_get_size(size_t offset)
54 {
55 size_t size = 0;
56
57 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
58 size += sizeof(pid_t);
59 return size;
60 }
61
62 static
63 void vtid_record(struct lttng_ctx_field *field,
64 struct lttng_ust_lib_ring_buffer_ctx *ctx,
65 struct ltt_channel *chan)
66 {
67 if (caa_unlikely(!cached_vtid))
68 cached_vtid = gettid();
69 lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid));
70 chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid));
71 }
72
73 int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx)
74 {
75 struct lttng_ctx_field *field;
76
77 field = lttng_append_context(ctx);
78 if (!field)
79 return -ENOMEM;
80 if (lttng_find_context(*ctx, "vtid")) {
81 lttng_remove_context_field(ctx, field);
82 return -EEXIST;
83 }
84 field->event_field.name = "vtid";
85 field->event_field.type.atype = atype_integer;
86 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
87 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
88 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
89 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
90 field->event_field.type.u.basic.integer.base = 10;
91 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
92 field->get_size = vtid_get_size;
93 field->record = vtid_record;
94 return 0;
95 }
96
97 /*
98 * Force a read (imply TLS fixup for dlopen) of TLS variables.
99 */
100 void lttng_fixup_vtid_tls(void)
101 {
102 asm volatile ("" : : "m" (cached_vtid));
103 }
This page took 0.041106 seconds and 5 git commands to generate.