Debian Codegraph @

runtime/dyninst/common_session_state.h

raw
1 // Will be included once by runtime/common_session_state.h, which is included
2 // once by translate.cxx c_unparser::emit_common_header ().
3
4
5 // Forward declarations for things in runtime/dyninst/shm.c
6 static void *_stp_shm_base;
7 static void *_stp_shm_alloc(size_t size);
8
9 #include "session_attributes.h"
10 #include "transport.h"
11
12 // Global state shared throughout the module
13 struct stp_runtime_session {
14 atomic_t _session_state;
15
16 atomic_t _error_count;
17 atomic_t _skipped_count;
18 atomic_t _skipped_count_lowstack;
19 atomic_t _skipped_count_reentrant;
20 atomic_t _skipped_count_uprobe_reg;
21 atomic_t _skipped_count_uprobe_unreg;
22
23 unsigned long _hash_seed;
24
25 struct _stp_session_attributes _session_attributes;
26
27 #ifdef STP_ALIBI
28 atomic_t _probe_alibi[STP_PROBE_COUNT];
29 #endif
30
31 #ifdef STP_TIMING
32 offptr_t _probe_timing[STP_PROBE_COUNT]; // offptr<Stat>
33 #endif
34
35 struct stp_globals _global;
36
37 struct _stp_transport_session_data _transport_data;
38
39 // NB: the context includes a number of pointers, which wouldn't be
40 // kosher for shared memory, but it's ok as long as they're only set
41 // and dereferenced within each separate handler invocation.
42 struct context _context[]; // variably-sized to cpu count
43 };
44
45 static inline struct stp_runtime_session* _stp_session(void)
46 {
47 // Since the session is always the first thing allocated, it lives
48 // directly at the start of shared memory.
49 // NB: We always reference the main shm pointer, rather than our own
50 // local copy, because reallocations may cause it to move dynamically.
51 return _stp_shm_base;
52 }
53
54
55 #define GET_SESSION_ATOMIC(name) \
56 static inline atomic_t *name(void) { \
57 if (unlikely(_stp_session() == NULL)) \
58 return NULL; \
59 return &_stp_session()->_##name; \
60 }
61
62 GET_SESSION_ATOMIC(session_state);
63
64 GET_SESSION_ATOMIC(error_count);
65 GET_SESSION_ATOMIC(skipped_count);
66 GET_SESSION_ATOMIC(skipped_count_lowstack);
67 GET_SESSION_ATOMIC(skipped_count_reentrant);
68 GET_SESSION_ATOMIC(skipped_count_uprobe_reg);
69 GET_SESSION_ATOMIC(skipped_count_uprobe_unreg);
70
71 #undef GET_SESSION_ATOMIC
72
73
74 static inline unsigned long _stap_hash_seed()
75 {
76 if (likely(_stp_session()))
77 return _stp_session()->_hash_seed;
78 return 0;
79 }
80
81
82 static inline struct _stp_session_attributes *stp_session_attributes(void)
83 {
84 if (likely(_stp_session()))
85 return &_stp_session()->_session_attributes;
86 return NULL;
87 }
88
89
90 #ifdef STP_ALIBI
91 static inline atomic_t *probe_alibi(size_t index)
92 {
93 // Do some simple bounds-checking. Translator-generated code
94 // should never get this wrong, but better to be safe.
95 index = clamp_t(size_t, index, 0, STP_PROBE_COUNT - 1);
96 if (likely(_stp_session()))
97 return &_stp_session()->_probe_alibi[index];
98 return NULL;
99 }
100 #endif
101
102 #ifdef STP_TIMING
103 static inline Stat probe_timing(size_t index)
104 {
105 // Do some simple bounds-checking. Translator-generated code
106 // should never get this wrong, but better to be safe.
107 index = clamp_t(size_t, index, 0, STP_PROBE_COUNT - 1);
108 if (likely(_stp_session()))
109 return offptr_get(&_stp_session()->_probe_timing[index]);
110 return NULL;
111 }
112 #endif
113
114
115 static inline struct context* stp_session_context(size_t index)
116 {
117 // Do some simple bounds-checking. Translator-generated code
118 // should never get this wrong, but better to be safe.
119 index = clamp_t(size_t, index, 0, _stp_runtime_num_contexts - 1);
120 if (likely(_stp_session()))
121 return &_stp_session()->_context[index];
122 return NULL;
123 }
124
125
126 static inline struct _stp_transport_session_data *stp_transport_data(void)
127 {
128 if (_stp_session())
129 return &_stp_session()->_transport_data;
130 return NULL;
131 }
132
133 #define _global_raw(name) (_stp_session()->_global.name)
134 #define _global_type(name) typeof(_global_raw(name))
135 #define _global_cast_type(name) _global_type(name ## _typed)
136 #define _global_offptr_p(name) \
137 __builtin_types_compatible_p(_global_type(name), offptr_t)
138 #define _global_compat_p(name, value) \
139 __builtin_types_compatible_p(_global_cast_type(name), typeof(value))
140
141 // NB: The (offptr_t*) cast is unfortunately necessary for the first expression
142 // to pass syntax on long/string types, but __builtin_choose_expr will still
143 // direct them to the second expression due to the _global_offptr_p check.
144 #define global(name) \
145 __builtin_choose_expr(_global_offptr_p(name), \
146 ((_global_cast_type(name)) \
147 offptr_get((offptr_t*)&_global_raw(name))), \
148 _global_raw(name))
149
150 // NB: This is only used for pointer->offptr assignments, for stats and maps.
151 // Unlike global() which just returns string/long types by value, here we'd
152 // have to encode their separate assignment schemes, in a way that stays
153 // syntax-compatible with all others. The translator can do that simpler.
154 // NB: Evaluate the value *before* the offptr destination is computed, in
155 // case the value's allocation causes shm to move.
156 #define global_set(name, value) ({ \
157 typeof(value) __value = (value); \
158 offptr_set(&_global_raw(name), \
159 __builtin_choose_expr(_global_compat_p(name, __value), \
160 __value, (void)0)); \
161 __value; })
162
163 #define global_lock(name) (&_global_raw(name ## _lock))
164 #define global_lock_init(name) \
165 stp_pthread_rwlock_init_shared(global_lock(name))
166
167 #ifdef STP_TIMING
168 #define global_skipped(name) (&_global_raw(name ## _lock_skip_count))
169 #define global_contended(name) (&_global_raw(name ## _lock_contention_count))
170 #endif
171
172
173 static int stp_session_init(void)
174 {
175 size_t i;
176
177 // Reserve space for the session at the beginning of shared memory.
178 size_t session_size = sizeof(struct stp_runtime_session)
179 + sizeof(struct context) * _stp_runtime_num_contexts;
180 void *session = _stp_shm_zalloc(session_size);
181 if (!session)
182 return -ENOMEM;
183
184 // If we weren't the very first thing allocated, then something is wrong!
185 if (session != _stp_shm_base)
186 return -EINVAL;
187
188 atomic_set(session_state(), STAP_SESSION_UNINITIALIZED);
189
190 atomic_set(error_count(), 0);
191 atomic_set(skipped_count(), 0);
192 atomic_set(skipped_count_lowstack(), 0);
193 atomic_set(skipped_count_reentrant(), 0);
194 atomic_set(skipped_count_uprobe_reg(), 0);
195 atomic_set(skipped_count_uprobe_unreg(), 0);
196
197 _stp_session()->_hash_seed = _stp_random_u ((unsigned long)-1);
198
199 stp_session_attributes_init();
200
201 #ifdef STP_ALIBI
202 // Initialize all the alibi counters
203 for (i = 0; i < STP_PROBE_COUNT; ++i)
204 atomic_set(probe_alibi(i), 0);
205 #endif
206
207 #ifdef STP_TIMING
208 // Initialize each Stat used for timing information
209 for (i = 0; i < STP_PROBE_COUNT; ++i) {
210 // NB: we don't check for null return here, but instead at
211 // passage to probe handlers and at final printing.
212 Stat st = _stp_stat_init(STAT_OP_MIN, STAT_OP_MAX, STAT_OP_AVG, NULL);
213
214 // NB: allocate first, then dereference the session after, in case
215 // allocation-resizing causes the whole thing to move around.
216 offptr_set(&_stp_session()->_probe_timing[i], st);
217 }
218 #endif
219
220 return _stp_dyninst_transport_session_init();
221 }
222
223 static int stp_session_init_finished(void)
224 {
225 return stp_dyninst_session_init_finished();
226 }