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
25 #include <lttng/ust-events.h>
26 #include <lttng/ust-tracer.h>
27 #include <lttng/ringbuffer-config.h>
28 #include <urcu/tls-compat.h>
32 /* Maximum number of nesting levels for the procname cache. */
33 #define PROCNAME_NESTING_MAX 2
36 * We cache the result to ensure we don't trigger a system call for
38 * Upon exec, procname changes, but exec takes care of throwing away
39 * this cached version.
40 * The procname can also change by calling prctl(). The procname should
41 * be set for a thread before the first event is logged within this
44 typedef char procname_array
[PROCNAME_NESTING_MAX
][17];
46 static DEFINE_URCU_TLS(procname_array
, cached_procname
);
48 static DEFINE_URCU_TLS(int, procname_nesting
);
51 char *wrapper_getprocname(void)
53 int nesting
= CMM_LOAD_SHARED(URCU_TLS(procname_nesting
));
55 if (caa_unlikely(nesting
>= PROCNAME_NESTING_MAX
))
57 if (caa_unlikely(!URCU_TLS(cached_procname
)[nesting
][0])) {
58 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
+ 1);
59 /* Increment nesting before updating cache. */
61 lttng_pthread_getname_np(URCU_TLS(cached_procname
)[nesting
], LTTNG_UST_ABI_PROCNAME_LEN
);
62 URCU_TLS(cached_procname
)[nesting
][LTTNG_UST_ABI_PROCNAME_LEN
- 1] = '\0';
63 /* Decrement nesting after updating cache. */
65 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
);
67 return URCU_TLS(cached_procname
)[nesting
];
70 /* Reset should not be called from a signal handler. */
71 void lttng_context_procname_reset(void)
73 CMM_STORE_SHARED(URCU_TLS(cached_procname
)[1][0], '\0');
74 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 1);
75 CMM_STORE_SHARED(URCU_TLS(cached_procname
)[0][0], '\0');
76 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 0);
80 size_t procname_get_size(struct lttng_ctx_field
*field
, size_t offset
)
82 return LTTNG_UST_ABI_PROCNAME_LEN
;
86 void procname_record(struct lttng_ctx_field
*field
,
87 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
88 struct lttng_channel
*chan
)
92 procname
= wrapper_getprocname();
93 chan
->ops
->event_write(ctx
, procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
97 void procname_get_value(struct lttng_ctx_field
*field
,
98 struct lttng_ctx_value
*value
)
100 value
->u
.str
= wrapper_getprocname();
103 static const struct lttng_type procname_array_elem_type
=
104 __type_integer(char, BYTE_ORDER
, 10, UTF8
);
106 int lttng_add_procname_to_ctx(struct lttng_ctx
**ctx
)
108 struct lttng_ctx_field
*field
;
110 field
= lttng_append_context(ctx
);
113 if (lttng_find_context(*ctx
, "procname")) {
114 lttng_remove_context_field(ctx
, field
);
117 field
->event_field
.name
= "procname";
118 field
->event_field
.type
.atype
= atype_array_nestable
;
119 field
->event_field
.type
.u
.array_nestable
.elem_type
=
120 &procname_array_elem_type
;
121 field
->event_field
.type
.u
.array_nestable
.length
= LTTNG_UST_ABI_PROCNAME_LEN
;
122 field
->get_size
= procname_get_size
;
123 field
->record
= procname_record
;
124 field
->get_value
= procname_get_value
;
125 lttng_context_update(*ctx
);
130 * Force a read (imply TLS fixup for dlopen) of TLS variables.
132 void lttng_fixup_procname_tls(void)
134 asm volatile ("" : : "m" (URCU_TLS(cached_procname
)[0]));