2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng UST vtid context.
7 * Dual LGPL v2.1/GPL v2 license.
10 #include <sys/types.h>
12 #include <lttng/ust-events.h>
13 #include <lttng/ust-tracer.h>
14 #include <lttng/ringbuffer-config.h>
15 #include <lttng/ust-tid.h>
16 #include "ltt-tracer-core.h"
19 * We cache the result to ensure we don't trigger a system call for
22 static __thread pid_t cached_vtid
;
25 * Upon fork or clone, the TID assigned to our thread is not the same as
26 * we kept in cache. Luckily, we are the only thread surviving in the
27 * child process, so we can simply clear our cached version.
29 void lttng_context_vtid_reset(void)
35 size_t vtid_get_size(size_t offset
)
39 size
+= lib_ring_buffer_align(offset
, lttng_alignof(pid_t
));
40 size
+= sizeof(pid_t
);
45 void vtid_record(struct lttng_ctx_field
*field
,
46 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
47 struct ltt_channel
*chan
)
49 if (caa_unlikely(!cached_vtid
))
50 cached_vtid
= gettid();
51 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(cached_vtid
));
52 chan
->ops
->event_write(ctx
, &cached_vtid
, sizeof(cached_vtid
));
55 int lttng_add_vtid_to_ctx(struct lttng_ctx
**ctx
)
57 struct lttng_ctx_field
*field
;
59 field
= lttng_append_context(ctx
);
62 if (lttng_find_context(*ctx
, "vtid")) {
63 lttng_remove_context_field(ctx
, field
);
66 field
->event_field
.name
= "vtid";
67 field
->event_field
.type
.atype
= atype_integer
;
68 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(pid_t
) * CHAR_BIT
;
69 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(pid_t
) * CHAR_BIT
;
70 field
->event_field
.type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(pid_t
);
71 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
72 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
73 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
74 field
->get_size
= vtid_get_size
;
75 field
->record
= vtid_record
;
80 * Force a read (imply TLS fixup for dlopen) of TLS variables.
82 void lttng_fixup_vtid_tls(void)
84 asm volatile ("" : : "m" (cached_vtid
));