Commit | Line | Data |
---|---|---|
4847e9bb | 1 | /* |
e92f3e28 | 2 | * lttng-context-procname.c |
4847e9bb MD |
3 | * |
4 | * LTTng UST procname context. | |
5 | * | |
009745db | 6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
e92f3e28 MD |
7 | * |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
4847e9bb MD |
21 | */ |
22 | ||
3fbec7dc | 23 | #define _LGPL_SOURCE |
4318ae1b MD |
24 | #include <lttng/ust-events.h> |
25 | #include <lttng/ust-tracer.h> | |
26 | #include <lttng/ringbuffer-config.h> | |
8c90a710 | 27 | #include <urcu/tls-compat.h> |
4847e9bb | 28 | #include <assert.h> |
08114193 | 29 | #include "compat.h" |
4847e9bb | 30 | |
74011e88 MD |
31 | /* Maximum number of nesting levels for the procname cache. */ |
32 | #define PROCNAME_NESTING_MAX 2 | |
33 | ||
4847e9bb MD |
34 | /* |
35 | * We cache the result to ensure we don't trigger a system call for | |
36 | * each event. | |
37 | * Upon exec, procname changes, but exec takes care of throwing away | |
38 | * this cached version. | |
009745db MD |
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 | |
41 | * thread. | |
4847e9bb | 42 | */ |
74011e88 MD |
43 | typedef char procname_array[PROCNAME_NESTING_MAX][17]; |
44 | ||
16adecf1 | 45 | static DEFINE_URCU_TLS(procname_array, cached_procname); |
4847e9bb | 46 | |
74011e88 MD |
47 | static DEFINE_URCU_TLS(int, procname_nesting); |
48 | ||
4847e9bb MD |
49 | static inline |
50 | char *wrapper_getprocname(void) | |
51 | { | |
2c44512a | 52 | int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting)); |
74011e88 MD |
53 | |
54 | if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX)) | |
55 | return "<unknown>"; | |
56 | if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) { | |
57 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1); | |
2c44512a MD |
58 | /* Increment nesting before updating cache. */ |
59 | cmm_barrier(); | |
74011e88 MD |
60 | lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]); |
61 | URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0'; | |
2c44512a MD |
62 | /* Decrement nesting after updating cache. */ |
63 | cmm_barrier(); | |
74011e88 | 64 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting); |
4847e9bb | 65 | } |
74011e88 | 66 | return URCU_TLS(cached_procname)[nesting]; |
4847e9bb MD |
67 | } |
68 | ||
2c44512a | 69 | /* Reset should not be called from a signal handler. */ |
4847e9bb MD |
70 | void lttng_context_procname_reset(void) |
71 | { | |
2c44512a | 72 | CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0'); |
74011e88 | 73 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1); |
2c44512a | 74 | CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0'); |
74011e88 | 75 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0); |
4847e9bb MD |
76 | } |
77 | ||
78 | static | |
53569322 | 79 | size_t procname_get_size(struct lttng_ctx_field *field, size_t offset) |
4847e9bb | 80 | { |
6e9ac4ae | 81 | return LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
82 | } |
83 | ||
84 | static | |
85 | void procname_record(struct lttng_ctx_field *field, | |
4cfec15c | 86 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 87 | struct lttng_channel *chan) |
4847e9bb MD |
88 | { |
89 | char *procname; | |
90 | ||
91 | procname = wrapper_getprocname(); | |
08114193 | 92 | chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN); |
4847e9bb MD |
93 | } |
94 | ||
77aa5901 MD |
95 | static |
96 | void procname_get_value(struct lttng_ctx_field *field, | |
53569322 | 97 | struct lttng_ctx_value *value) |
77aa5901 | 98 | { |
6e9ac4ae | 99 | value->u.str = wrapper_getprocname(); |
77aa5901 MD |
100 | } |
101 | ||
4847e9bb MD |
102 | int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) |
103 | { | |
104 | struct lttng_ctx_field *field; | |
105 | ||
106 | field = lttng_append_context(ctx); | |
107 | if (!field) | |
108 | return -ENOMEM; | |
109 | if (lttng_find_context(*ctx, "procname")) { | |
110 | lttng_remove_context_field(ctx, field); | |
111 | return -EEXIST; | |
112 | } | |
113 | field->event_field.name = "procname"; | |
114 | field->event_field.type.atype = atype_array; | |
115 | field->event_field.type.u.array.elem_type.atype = atype_integer; | |
116 | field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT; | |
117 | field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT; | |
118 | field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char); | |
119 | field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0; | |
120 | field->event_field.type.u.array.elem_type.u.basic.integer.base = 10; | |
121 | field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8; | |
08114193 | 122 | field->event_field.type.u.array.length = LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
123 | field->get_size = procname_get_size; |
124 | field->record = procname_record; | |
77aa5901 | 125 | field->get_value = procname_get_value; |
b2cc986a | 126 | lttng_context_update(*ctx); |
4847e9bb MD |
127 | return 0; |
128 | } | |
009745db MD |
129 | |
130 | /* | |
131 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
132 | */ | |
133 | void lttng_fixup_procname_tls(void) | |
134 | { | |
8c90a710 | 135 | asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0])); |
009745db | 136 | } |