2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST procname context.
11 #include <lttng/ust-events.h>
12 #include <lttng/ust-tracer.h>
13 #include <lttng/ringbuffer-context.h>
14 #include <urcu/tls-compat.h>
17 #include "lttng-tracer-core.h"
19 #include "context-internal.h"
21 /* Maximum number of nesting levels for the procname cache. */
22 #define PROCNAME_NESTING_MAX 2
25 * We cache the result to ensure we don't trigger a system call for
27 * Upon exec, procname changes, but exec takes care of throwing away
28 * this cached version.
29 * The procname can also change by calling prctl(). The procname should
30 * be set for a thread before the first event is logged within this
33 typedef char procname_array
[PROCNAME_NESTING_MAX
][17];
35 static DEFINE_URCU_TLS(procname_array
, cached_procname
);
37 static DEFINE_URCU_TLS(int, procname_nesting
);
40 const char *wrapper_getprocname(void)
42 int nesting
= CMM_LOAD_SHARED(URCU_TLS(procname_nesting
));
44 if (caa_unlikely(nesting
>= PROCNAME_NESTING_MAX
))
46 if (caa_unlikely(!URCU_TLS(cached_procname
)[nesting
][0])) {
47 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
+ 1);
48 /* Increment nesting before updating cache. */
50 lttng_pthread_getname_np(URCU_TLS(cached_procname
)[nesting
], LTTNG_UST_ABI_PROCNAME_LEN
);
51 URCU_TLS(cached_procname
)[nesting
][LTTNG_UST_ABI_PROCNAME_LEN
- 1] = '\0';
52 /* Decrement nesting after updating cache. */
54 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
);
56 return URCU_TLS(cached_procname
)[nesting
];
59 /* Reset should not be called from a signal handler. */
60 void lttng_ust_context_procname_reset(void)
62 CMM_STORE_SHARED(URCU_TLS(cached_procname
)[1][0], '\0');
63 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 1);
64 CMM_STORE_SHARED(URCU_TLS(cached_procname
)[0][0], '\0');
65 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 0);
69 size_t procname_get_size(struct lttng_ust_ctx_field
*field
__attribute__((unused
)),
70 size_t offset
__attribute__((unused
)))
72 return LTTNG_UST_ABI_PROCNAME_LEN
;
76 void procname_record(struct lttng_ust_ctx_field
*field
__attribute__((unused
)),
77 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
78 struct lttng_ust_channel_buffer
*chan
)
82 procname
= wrapper_getprocname();
83 chan
->ops
->event_write(ctx
, procname
, LTTNG_UST_ABI_PROCNAME_LEN
, 1);
87 void procname_get_value(struct lttng_ust_ctx_field
*field
__attribute__((unused
)),
88 struct lttng_ust_ctx_value
*value
)
90 value
->u
.str
= wrapper_getprocname();
93 int lttng_add_procname_to_ctx(struct lttng_ust_ctx
**ctx
)
95 struct lttng_ust_ctx_field
*field
;
96 struct lttng_ust_type_common
*type
;
99 type
= lttng_ust_create_type_array_text(LTTNG_UST_ABI_PROCNAME_LEN
);
102 field
= lttng_append_context(ctx
);
107 if (lttng_find_context(*ctx
, "procname")) {
109 goto error_find_context
;
111 field
->event_field
->name
= strdup("procname");
112 if (!field
->event_field
->name
) {
116 field
->event_field
->type
= type
;
117 field
->get_size
= procname_get_size
;
118 field
->record
= procname_record
;
119 field
->get_value
= procname_get_value
;
120 lttng_context_update(*ctx
);
125 lttng_remove_context_field(ctx
, field
);
127 lttng_ust_destroy_type(type
);
132 * Force a read (imply TLS fixup for dlopen) of TLS variables.
134 void lttng_fixup_procname_tls(void)
136 asm volatile ("" : : "m" (URCU_TLS(cached_procname
)[0]));