License text standardization, add missing licenses
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
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 #include <sys/types.h>
24 #include <unistd.h>
25 #include <lttng/ust-events.h>
26 #include <lttng/ust-tracer.h>
27 #include <lttng/ringbuffer-config.h>
28 #include "ltt-tracer-core.h"
29
30 #ifdef __linux__
31 #include <syscall.h>
32 #endif
33
34 #if defined(_syscall0)
35 _syscall0(pid_t, gettid)
36 #elif defined(__NR_gettid)
37 static inline pid_t gettid(void)
38 {
39 return syscall(__NR_gettid);
40 }
41 #else
42 #warning "use pid as tid"
43 static inline pid_t gettid(void)
44 {
45 return getpid();
46 }
47 #endif
48
49 /*
50 * We cache the result to ensure we don't trigger a system call for
51 * each event.
52 */
53 static __thread pid_t cached_vtid;
54
55 /*
56 * Upon fork or clone, the TID assigned to our thread is not the same as
57 * we kept in cache. Luckily, we are the only thread surviving in the
58 * child process, so we can simply clear our cached version.
59 */
60 void lttng_context_vtid_reset(void)
61 {
62 cached_vtid = 0;
63 }
64
65 static
66 size_t vtid_get_size(size_t offset)
67 {
68 size_t size = 0;
69
70 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
71 size += sizeof(pid_t);
72 return size;
73 }
74
75 static
76 void vtid_record(struct lttng_ctx_field *field,
77 struct lttng_ust_lib_ring_buffer_ctx *ctx,
78 struct ltt_channel *chan)
79 {
80 if (caa_unlikely(!cached_vtid))
81 cached_vtid = gettid();
82 lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid));
83 chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid));
84 }
85
86 int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx)
87 {
88 struct lttng_ctx_field *field;
89
90 field = lttng_append_context(ctx);
91 if (!field)
92 return -ENOMEM;
93 if (lttng_find_context(*ctx, "vtid")) {
94 lttng_remove_context_field(ctx, field);
95 return -EEXIST;
96 }
97 field->event_field.name = "vtid";
98 field->event_field.type.atype = atype_integer;
99 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
100 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
101 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
102 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
103 field->event_field.type.u.basic.integer.base = 10;
104 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
105 field->get_size = vtid_get_size;
106 field->record = vtid_record;
107 return 0;
108 }
109
110 /*
111 * Force a read (imply TLS fixup for dlopen) of TLS variables.
112 */
113 void lttng_fixup_vtid_tls(void)
114 {
115 asm volatile ("" : : "m" (cached_vtid));
116 }
This page took 0.033147 seconds and 5 git commands to generate.