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
24 #include <lttng/ust-events.h>
25 #include <lttng/ust-tracer.h>
26 #include <lttng/ringbuffer-config.h>
27 #include <urcu/tls-compat.h>
31 /* Maximum number of nesting levels for the procname cache. */
32 #define PROCNAME_NESTING_MAX 2
35 * We cache the result to ensure we don't trigger a system call for
37 * Upon exec, procname changes, but exec takes care of throwing away
38 * this cached version.
39 * The procname can also change by calling prctl(). The procname should
40 * be set for a thread before the first event is logged within this
43 typedef char procname_array
[PROCNAME_NESTING_MAX
][17];
45 static DEFINE_URCU_TLS(procname_array
, cached_procname
);
47 static DEFINE_URCU_TLS(int, procname_nesting
);
50 char *wrapper_getprocname(void)
52 int nesting
= URCU_TLS(procname_nesting
);
54 if (caa_unlikely(nesting
>= PROCNAME_NESTING_MAX
))
56 if (caa_unlikely(!URCU_TLS(cached_procname
)[nesting
][0])) {
57 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
+ 1);
58 lttng_ust_getprocname(URCU_TLS(cached_procname
)[nesting
]);
59 URCU_TLS(cached_procname
)[nesting
][LTTNG_UST_PROCNAME_LEN
- 1] = '\0';
60 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), nesting
);
62 return URCU_TLS(cached_procname
)[nesting
];
65 void lttng_context_procname_reset(void)
67 URCU_TLS(cached_procname
)[1][0] = '\0';
68 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 1);
69 URCU_TLS(cached_procname
)[0][0] = '\0';
70 CMM_STORE_SHARED(URCU_TLS(procname_nesting
), 0);
74 size_t procname_get_size(struct lttng_ctx_field
*field
, size_t offset
)
78 size
+= LTTNG_UST_PROCNAME_LEN
;
83 void procname_record(struct lttng_ctx_field
*field
,
84 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
85 struct lttng_channel
*chan
)
89 procname
= wrapper_getprocname();
90 chan
->ops
->event_write(ctx
, procname
, LTTNG_UST_PROCNAME_LEN
);
94 void procname_get_value(struct lttng_ctx_field
*field
,
95 struct lttng_ctx_value
*value
)
99 procname
= wrapper_getprocname();
100 value
->u
.str
= procname
;
103 int lttng_add_procname_to_ctx(struct lttng_ctx
**ctx
)
105 struct lttng_ctx_field
*field
;
107 field
= lttng_append_context(ctx
);
110 if (lttng_find_context(*ctx
, "procname")) {
111 lttng_remove_context_field(ctx
, field
);
114 field
->event_field
.name
= "procname";
115 field
->event_field
.type
.atype
= atype_array
;
116 field
->event_field
.type
.u
.array
.elem_type
.atype
= atype_integer
;
117 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.size
= sizeof(char) * CHAR_BIT
;
118 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.alignment
= lttng_alignof(char) * CHAR_BIT
;
119 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(char);
120 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.reverse_byte_order
= 0;
121 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.base
= 10;
122 field
->event_field
.type
.u
.array
.elem_type
.u
.basic
.integer
.encoding
= lttng_encode_UTF8
;
123 field
->event_field
.type
.u
.array
.length
= LTTNG_UST_PROCNAME_LEN
;
124 field
->get_size
= procname_get_size
;
125 field
->record
= procname_record
;
126 field
->get_value
= procname_get_value
;
127 lttng_context_update(*ctx
);
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]));