4 * LTTng UST vtid context.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <sys/types.h>
25 #include <lttng/ust-events.h>
26 #include <lttng/ust-tracer.h>
27 #include <lttng/ringbuffer-config.h>
28 #include <lttng/ust-tid.h>
29 #include "lttng-tracer-core.h"
32 * We cache the result to ensure we don't trigger a system call for
35 static __thread pid_t cached_vtid
;
38 * Upon fork or clone, the TID assigned to our thread is not the same as
39 * we kept in cache. Luckily, we are the only thread surviving in the
40 * child process, so we can simply clear our cached version.
42 void lttng_context_vtid_reset(void)
48 size_t vtid_get_size(size_t offset
)
52 size
+= lib_ring_buffer_align(offset
, lttng_alignof(pid_t
));
53 size
+= sizeof(pid_t
);
58 void vtid_record(struct lttng_ctx_field
*field
,
59 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
60 struct lttng_channel
*chan
)
62 if (caa_unlikely(!cached_vtid
))
63 cached_vtid
= gettid();
64 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(cached_vtid
));
65 chan
->ops
->event_write(ctx
, &cached_vtid
, sizeof(cached_vtid
));
68 int lttng_add_vtid_to_ctx(struct lttng_ctx
**ctx
)
70 struct lttng_ctx_field
*field
;
72 field
= lttng_append_context(ctx
);
75 if (lttng_find_context(*ctx
, "vtid")) {
76 lttng_remove_context_field(ctx
, field
);
79 field
->event_field
.name
= "vtid";
80 field
->event_field
.type
.atype
= atype_integer
;
81 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(pid_t
) * CHAR_BIT
;
82 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(pid_t
) * CHAR_BIT
;
83 field
->event_field
.type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(pid_t
);
84 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
85 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
86 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
87 field
->get_size
= vtid_get_size
;
88 field
->record
= vtid_record
;
93 * Force a read (imply TLS fixup for dlopen) of TLS variables.
95 void lttng_fixup_vtid_tls(void)
97 asm volatile ("" : : "m" (cached_vtid
));