2 * lttng-context-procname.c
4 * LTTng UST procname 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 <lttng/ust-events.h>
24 #include <lttng/ust-tracer.h>
25 #include <lttng/ringbuffer-config.h>
26 #include <urcu/tls-compat.h>
31 * We cache the result to ensure we don't trigger a system call for
33 * Upon exec, procname changes, but exec takes care of throwing away
34 * this cached version.
35 * The procname can also change by calling prctl(). The procname should
36 * be set for a thread before the first event is logged within this
39 typedef char procname_array
[17];
40 static DEFINE_URCU_TLS(procname_array
, cached_procname
);
43 char *wrapper_getprocname(void)
45 if (caa_unlikely(!URCU_TLS(cached_procname
)[0])) {
46 lttng_ust_getprocname(URCU_TLS(cached_procname
));
47 URCU_TLS(cached_procname
)[LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
49 return URCU_TLS(cached_procname
);
52 void lttng_context_procname_reset(void)
54 URCU_TLS(cached_procname
)[0] = '\0';
58 size_t procname_get_size(size_t offset
)
62 size
+= LTTNG_UST_PROCNAME_LEN
;
67 void procname_record(struct lttng_ctx_field
*field
,
68 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
69 struct lttng_channel
*chan
)
73 procname
= wrapper_getprocname();
74 chan
->ops
->event_write(ctx
, procname
, LTTNG_UST_PROCNAME_LEN
);
77 int lttng_add_procname_to_ctx(struct lttng_ctx
**ctx
)
79 struct lttng_ctx_field
*field
;
81 field
= lttng_append_context(ctx
);
84 if (lttng_find_context(*ctx
, "procname")) {
85 lttng_remove_context_field(ctx
, field
);
88 field
->event_field
.name
= "procname";
89 field
->event_field
.type
.atype
= atype_array
;
90 field
->event_field
.type
.u
.array
.elem_type
.atype
= atype_integer
;
91 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.size
= sizeof(char) * CHAR_BIT
;
92 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.alignment
= lttng_alignof(char) * CHAR_BIT
;
93 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(char);
94 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.reverse_byte_order
= 0;
95 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.base
= 10;
96 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.encoding
= lttng_encode_UTF8
;
97 field
->event_field
.type
.u
.array
.length
= LTTNG_UST_PROCNAME_LEN
;
98 field
->get_size
= procname_get_size
;
99 field
->record
= procname_record
;
104 * Force a read (imply TLS fixup for dlopen) of TLS variables.
106 void lttng_fixup_procname_tls(void)
108 asm volatile ("" : : "m" (URCU_TLS(cached_procname
)[0]));