drd/tests/swapcontext: Improve the portability of this test further
[valgrind.git] / callgrind / fn.c
blob7cce1a0c71b7a32e0a9156e4677ac866b2a705d6
1 /*--------------------------------------------------------------------*/
2 /*--- Callgrind ---*/
3 /*--- ct_fn.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Callgrind, a Valgrind tool for call tracing.
9 Copyright (C) 2002-2017, Josef Weidendorfer (Josef.Weidendorfer@gmx.de)
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 The GNU General Public License is contained in the file COPYING.
27 #include "global.h"
29 #define N_INITIAL_FN_ARRAY_SIZE 10071
31 static fn_array current_fn_active;
33 /* x86_64 defines 4 variants. */
34 #define MAX_RESOLVE_ADDRS 4
35 static int runtime_resolve_addrs = 0;
36 static Addr runtime_resolve_addr[MAX_RESOLVE_ADDRS];
37 static int runtime_resolve_length[MAX_RESOLVE_ADDRS];
39 // a code pattern is a list of tuples (start offset, length)
40 struct chunk_t { int start, len; };
41 struct pattern
43 const HChar* name;
44 int len;
45 struct chunk_t chunk[];
48 /* Scan for a pattern in the code of an ELF object.
49 * If found, return true and set runtime_resolve_{addr,length}
51 __attribute__((unused)) // Possibly; depends on the platform.
52 static Bool check_code(obj_node* obj,
53 UChar code[], struct pattern* pat)
55 Bool found;
56 Addr addr, end;
57 int chunk, start, len;
59 /* first chunk of pattern should always start at offset 0 and
60 * have at least 3 bytes */
61 CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2));
63 /* and we cannot be called more than MAX_RESOLVE_ADDRS times */
64 CLG_ASSERT(runtime_resolve_addrs < MAX_RESOLVE_ADDRS);
66 CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n",
67 obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]);
69 end = obj->start + obj->size - pat->len;
70 addr = obj->start;
71 while(addr < end) {
72 found = (VG_(memcmp)( (void*)addr, code, pat->chunk[0].len) == 0);
74 if (found) {
75 chunk = 1;
76 while(1) {
77 start = pat->chunk[chunk].start;
78 len = pat->chunk[chunk].len;
79 if (len == 0) break;
81 CLG_ASSERT(len >2);
82 CLG_DEBUG(1, " found chunk %d at %#lx, checking %d bytes "
83 "of [%x %x %x...]\n",
84 chunk-1, addr - obj->start, len,
85 code[start], code[start+1], code[start+2]);
87 if (VG_(memcmp)( (void*)(addr+start), code+start, len) != 0) {
88 found = False;
89 break;
91 chunk++;
94 if (found) {
95 CLG_DEBUG(1, "found at offset %#lx.\n", addr - obj->start);
96 if (VG_(clo_verbosity) > 1)
97 VG_(message)(Vg_DebugMsg, "Found runtime_resolve (%s): "
98 "%s +%#lx=%#lx, length %d\n",
99 pat->name, obj->name + obj->last_slash_pos,
100 addr - obj->start, addr, pat->len);
102 runtime_resolve_addr[runtime_resolve_addrs] = addr;
103 runtime_resolve_length[runtime_resolve_addrs] = pat->len;
104 runtime_resolve_addrs++;
105 return True;
108 addr++;
110 CLG_DEBUG(1, " found nothing.\n");
111 return False;
115 /* _ld_runtime_resolve, located in ld.so, needs special handling:
116 * The jump at end into the resolved function should not be
117 * represented as a call (as usually done in callgrind with jumps),
118 * but as a return + call. Otherwise, the repeated existence of
119 * _ld_runtime_resolve in call chains will lead to huge cycles,
120 * making the profile almost worthless.
122 * If ld.so is stripped, the symbol will not appear. But as this
123 * function is handcrafted assembler, we search for it.
125 * We stop if the ELF object name does not seem to be the runtime linker
127 static Bool search_runtime_resolve(obj_node* obj)
129 #if defined(VGP_x86_linux)
130 static UChar code[] = {
131 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
132 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
133 /*16*/ 0x5a, 0x59, 0x87, 0x04, 0x24, 0xc2, 0x08, 0x00 };
134 /* Check ranges [0-11] and [16-23] ([12-15] is an absolute address) */
135 static struct pattern pat = {
136 "x86-def", 24, {{ 0,12 }, { 16,8 }, { 24,0}} };
138 /* Pattern for glibc-2.8 on OpenSuse11.0 */
139 static UChar code_28[] = {
140 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
141 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
142 /*16*/ 0x5a, 0x8b, 0x0c, 0x24, 0x89, 0x04, 0x24, 0x8b,
143 /*24*/ 0x44, 0x24, 0x04, 0xc2, 0x0c, 0x00 };
144 static struct pattern pat_28 = {
145 "x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} };
147 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
148 Bool pat_p = check_code(obj, code, &pat);
149 Bool pat_28_p = check_code(obj, code_28, &pat_28);
150 if (pat_p || pat_28_p) return True;
151 return False;
152 #endif
154 #if defined(VGP_ppc32_linux)
155 static UChar code[] = {
156 /* 0*/ 0x94, 0x21, 0xff, 0xc0, 0x90, 0x01, 0x00, 0x0c,
157 /* 8*/ 0x90, 0x61, 0x00, 0x10, 0x90, 0x81, 0x00, 0x14,
158 /*16*/ 0x7d, 0x83, 0x63, 0x78, 0x90, 0xa1, 0x00, 0x18,
159 /*24*/ 0x7d, 0x64, 0x5b, 0x78, 0x90, 0xc1, 0x00, 0x1c,
160 /*32*/ 0x7c, 0x08, 0x02, 0xa6, 0x90, 0xe1, 0x00, 0x20,
161 /*40*/ 0x90, 0x01, 0x00, 0x30, 0x91, 0x01, 0x00, 0x24,
162 /*48*/ 0x7c, 0x00, 0x00, 0x26, 0x91, 0x21, 0x00, 0x28,
163 /*56*/ 0x91, 0x41, 0x00, 0x2c, 0x90, 0x01, 0x00, 0x08,
164 /*64*/ 0x48, 0x00, 0x02, 0x91, 0x7c, 0x69, 0x03, 0xa6, /* at 64: bl aff0 <fixup> */
165 /*72*/ 0x80, 0x01, 0x00, 0x30, 0x81, 0x41, 0x00, 0x2c,
166 /*80*/ 0x81, 0x21, 0x00, 0x28, 0x7c, 0x08, 0x03, 0xa6,
167 /*88*/ 0x81, 0x01, 0x00, 0x24, 0x80, 0x01, 0x00, 0x08,
168 /*96*/ 0x80, 0xe1, 0x00, 0x20, 0x80, 0xc1, 0x00, 0x1c,
169 /*104*/0x7c, 0x0f, 0xf1, 0x20, 0x80, 0xa1, 0x00, 0x18,
170 /*112*/0x80, 0x81, 0x00, 0x14, 0x80, 0x61, 0x00, 0x10,
171 /*120*/0x80, 0x01, 0x00, 0x0c, 0x38, 0x21, 0x00, 0x40,
172 /*128*/0x4e, 0x80, 0x04, 0x20 };
173 static struct pattern pat = {
174 "ppc32-def", 132, {{ 0,65 }, { 68,64 }, { 132,0 }} };
176 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
177 return check_code(obj, code, &pat);
178 #endif
180 #if defined(VGP_amd64_linux)
181 static UChar code[] = {
182 /* 0*/ 0x48, 0x83, 0xec, 0x38, 0x48, 0x89, 0x04, 0x24,
183 /* 8*/ 0x48, 0x89, 0x4c, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x10,
184 /*18*/ 0x48, 0x89, 0x74, 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20,
185 /*28*/ 0x4c, 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24, 0x30,
186 /*38*/ 0x48, 0x8b, 0x74, 0x24, 0x40, 0x49, 0x89, 0xf3,
187 /*46*/ 0x4c, 0x01, 0xde, 0x4c, 0x01, 0xde, 0x48, 0xc1, 0xe6, 0x03,
188 /*56*/ 0x48, 0x8b, 0x7c, 0x24, 0x38, 0xe8, 0xee, 0x01, 0x00, 0x00,
189 /*66*/ 0x49, 0x89, 0xc3, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
190 /*74*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
191 /*84*/ 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54, 0x24, 0x10,
192 /*94*/ 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48, 0x8b, 0x04, 0x24,
193 /*103*/0x48, 0x83, 0xc4, 0x48, 0x41, 0xff, 0xe3 };
194 static struct pattern pat = {
195 "amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} };
197 static UChar code_xsavec[] = {
198 /* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
199 /* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
200 /*15*/ 0x48,
201 /*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
202 /*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
203 /*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
204 /*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
205 /*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
206 /*56*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
207 /*64*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
208 /*72*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
209 /*80*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
210 /*88*/ 0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
211 /*96*/ 0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
212 /*04*/ 0x0f, 0xc7, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
213 /*112*/0x10, 0x48, 0x8b, 0x7b, 0x08,
214 /*117*/0xe8, 0x00, 0x00, 0x00, 0x00, /* callq <_dl_fixup> */
215 /*122*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
216 /*128*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
217 /*136*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
218 /*144*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
219 /*152*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
220 /*160*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
221 /*168*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
222 /*176*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
223 /*184*/0xff, 0xe3 };
224 static struct pattern pat_xsavec = {
225 "amd64-xsavec", 186, {{ 0,11 }, { 15,103 }, {122,64}, { 186,0 }} };
227 static UChar code_xsave[] = {
228 /* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
229 /* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
230 /*15*/ 0x48,
231 /*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
232 /*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
233 /*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
234 /*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
235 /*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
236 /*56*/ 0x48, 0x89, 0x94, 0x24, 0x40, 0x02, 0x00, 0x00,
237 /*64*/ 0x48, 0x89, 0x94, 0x24, 0x48, 0x02, 0x00, 0x00,
238 /*72*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
239 /*80*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
240 /*88*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
241 /*96*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
242 /*104*/0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
243 /*112*/0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
244 /*120*/0x0f, 0xae, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
245 /*128*/0x10, 0x48, 0x8b, 0x7b, 0x08,
246 /*133*/0xe8, 0x00, 0x00, 0x00, 0x00, /* callq <_dl_fixup> */
247 /*138*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
248 /*144*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
249 /*152*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
250 /*160*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
251 /*168*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
252 /*176*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
253 /*184*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
254 /*192*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
255 /*200*/0xff, 0xe3 };
256 static struct pattern pat_xsave = {
257 "amd64-xsave", 202, {{ 0,11 }, { 15,119 }, {138,64}, { 202,0 }} };
259 static UChar code_fxsave[] = {
260 /* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xf0,
261 /* 8*/ 0x48, 0x81, 0xec, 0x40, 0x02, 0x00, 0x00, 0x48,
262 /*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
263 /*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
264 /*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
265 /*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
266 /*48*/ 0x30, 0x0f, 0xae, 0x44, 0x24, 0x40, 0x48, 0x8b,
267 /*56*/ 0x73, 0x10, 0x48, 0x8b, 0x7b, 0x08,
268 /*62*/ 0xe8, 0x00, 0x00, 0x00, 0x00, /* callq <_dl_fixup> */
269 /*67*/ 0x49, 0x89, 0xc3, 0x0f, 0xae,
270 /*72*/ 0x4c, 0x24, 0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
271 /*80*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c,
272 /*88*/ 0x24, 0x20, 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48,
273 /*96*/ 0x8b, 0x54, 0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24,
274 /*104*/0x08, 0x48, 0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc,
275 /*112*/0x48, 0x8b, 0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18,
276 /*120*/0xf2, 0x41, 0xff, 0xe3 };
277 static struct pattern pat_fxsave = {
278 "amd64-fxsave", 124, {{ 0,63 }, { 67,57 }, { 124,0 }} };
280 if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
281 (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0) &&
282 (VG_(strncmp)(obj->name, "/usr/lib/ld", 11) != 0) &&
283 (VG_(strncmp)(obj->name, "/usr/lib64/ld", 13) != 0)) return False;
284 Bool pat_p = check_code(obj, code, &pat);
285 Bool pat_xsavec_p = check_code(obj, code_xsavec, &pat_xsavec);
286 Bool pat_xsave_p = check_code(obj, code_xsave, &pat_xsave);
287 Bool pat_fxsave_p = check_code(obj, code_fxsave, &pat_fxsave);
288 if (pat_p || pat_xsavec_p || pat_xsave_p || pat_fxsave_p) return True;
289 #endif
291 /* For other platforms, no patterns known */
292 return False;
296 /*------------------------------------------------------------*/
297 /*--- Object/File/Function hash entry operations ---*/
298 /*------------------------------------------------------------*/
300 /* Object hash table, fixed */
301 static obj_node* obj_table[N_OBJ_ENTRIES];
303 void CLG_(init_obj_table)()
305 Int i;
306 for (i = 0; i < N_OBJ_ENTRIES; i++)
307 obj_table[i] = 0;
310 #define HASH_CONSTANT 256
312 static UInt str_hash(const HChar *s, UInt table_size)
314 int hash_value = 0;
315 for ( ; *s; s++)
316 hash_value = (HASH_CONSTANT * hash_value + *s) % table_size;
317 return hash_value;
321 static const HChar* anonymous_obj = "???";
323 static __inline__
324 obj_node* new_obj_node(DebugInfo* di, obj_node* next)
326 Int i;
327 obj_node* obj;
329 obj = (obj_node*) CLG_MALLOC("cl.fn.non.1", sizeof(obj_node));
330 obj->name = di ? VG_(strdup)( "cl.fn.non.2",
331 VG_(DebugInfo_get_filename)(di) )
332 : anonymous_obj;
333 for (i = 0; i < N_FILE_ENTRIES; i++) {
334 obj->files[i] = NULL;
336 CLG_(stat).distinct_objs ++;
337 obj->number = CLG_(stat).distinct_objs;
338 /* JRS 2008 Feb 19: maybe rename .start/.size/.offset to
339 .text_avma/.text_size/.test_bias to make it clearer what these
340 fields really mean */
341 obj->start = di ? VG_(DebugInfo_get_text_avma)(di) : 0;
342 obj->size = di ? VG_(DebugInfo_get_text_size)(di) : 0;
343 obj->offset = di ? VG_(DebugInfo_get_text_bias)(di) : 0;
344 obj->next = next;
346 // not only used for debug output (see static.c)
347 obj->last_slash_pos = 0;
348 i = 0;
349 while(obj->name[i]) {
350 if (obj->name[i]=='/') obj->last_slash_pos = i+1;
351 i++;
354 if (runtime_resolve_addrs == 0) search_runtime_resolve(obj);
356 return obj;
359 obj_node* CLG_(get_obj_node)(DebugInfo* di)
361 obj_node* curr_obj_node;
362 UInt objname_hash;
363 const HChar* obj_name;
365 obj_name = di ? VG_(DebugInfo_get_filename)(di) : anonymous_obj;
367 /* lookup in obj hash */
368 objname_hash = str_hash(obj_name, N_OBJ_ENTRIES);
369 curr_obj_node = obj_table[objname_hash];
370 while (NULL != curr_obj_node &&
371 VG_(strcmp)(obj_name, curr_obj_node->name) != 0) {
372 curr_obj_node = curr_obj_node->next;
374 if (NULL == curr_obj_node) {
375 obj_table[objname_hash] = curr_obj_node =
376 new_obj_node(di, obj_table[objname_hash]);
379 return curr_obj_node;
383 static __inline__
384 file_node* new_file_node(const HChar *filename,
385 obj_node* obj, file_node* next)
387 Int i;
388 file_node* file = (file_node*) CLG_MALLOC("cl.fn.nfn.1",
389 sizeof(file_node));
390 file->name = VG_(strdup)("cl.fn.nfn.2", filename);
391 for (i = 0; i < N_FN_ENTRIES; i++) {
392 file->fns[i] = NULL;
394 CLG_(stat).distinct_files++;
395 file->number = CLG_(stat).distinct_files;
396 file->obj = obj;
397 file->next = next;
398 return file;
402 file_node* CLG_(get_file_node)(obj_node* curr_obj_node,
403 const HChar *dir, const HChar *file)
405 file_node* curr_file_node;
406 UInt filename_hash;
408 /* Build up an absolute pathname, if there is a directory available */
409 HChar filename[VG_(strlen)(dir) + 1 + VG_(strlen)(file) + 1];
410 VG_(strcpy)(filename, dir);
411 if (filename[0] != '\0') {
412 VG_(strcat)(filename, "/");
414 VG_(strcat)(filename, file);
416 /* lookup in file hash */
417 filename_hash = str_hash(filename, N_FILE_ENTRIES);
418 curr_file_node = curr_obj_node->files[filename_hash];
419 while (NULL != curr_file_node &&
420 VG_(strcmp)(filename, curr_file_node->name) != 0) {
421 curr_file_node = curr_file_node->next;
423 if (NULL == curr_file_node) {
424 curr_obj_node->files[filename_hash] = curr_file_node =
425 new_file_node(filename, curr_obj_node,
426 curr_obj_node->files[filename_hash]);
429 return curr_file_node;
432 /* forward decl. */
433 static void resize_fn_array(void);
435 static __inline__
436 fn_node* new_fn_node(const HChar *fnname,
437 file_node* file, fn_node* next)
439 fn_node* fn = (fn_node*) CLG_MALLOC("cl.fn.nfnnd.1",
440 sizeof(fn_node));
441 fn->name = VG_(strdup)("cl.fn.nfnnd.2", fnname);
443 CLG_(stat).distinct_fns++;
444 fn->number = CLG_(stat).distinct_fns;
445 fn->last_cxt = 0;
446 fn->pure_cxt = 0;
447 fn->file = file;
448 fn->next = next;
450 fn->dump_before = False;
451 fn->dump_after = False;
452 fn->zero_before = False;
453 fn->toggle_collect = False;
454 fn->skip = False;
455 fn->pop_on_jump = CLG_(clo).pop_on_jump;
456 fn->is_malloc = False;
457 fn->is_realloc = False;
458 fn->is_free = False;
460 fn->group = 0;
461 fn->separate_callers = CLG_(clo).separate_callers;
462 fn->separate_recursions = CLG_(clo).separate_recursions;
464 #if CLG_ENABLE_DEBUG
465 fn->verbosity = -1;
466 #endif
468 if (CLG_(stat).distinct_fns >= current_fn_active.size)
469 resize_fn_array();
471 return fn;
475 /* Get a function node in hash2 with known file node.
476 * hash nodes are created if needed
478 static
479 fn_node* get_fn_node_infile(file_node* curr_file_node,
480 const HChar *fnname)
482 fn_node* curr_fn_node;
483 UInt fnname_hash;
485 CLG_ASSERT(curr_file_node != 0);
487 /* lookup in function hash */
488 fnname_hash = str_hash(fnname, N_FN_ENTRIES);
489 curr_fn_node = curr_file_node->fns[fnname_hash];
490 while (NULL != curr_fn_node &&
491 VG_(strcmp)(fnname, curr_fn_node->name) != 0) {
492 curr_fn_node = curr_fn_node->next;
494 if (NULL == curr_fn_node) {
495 curr_file_node->fns[fnname_hash] = curr_fn_node =
496 new_fn_node(fnname, curr_file_node,
497 curr_file_node->fns[fnname_hash]);
500 return curr_fn_node;
504 /* Get a function node in a Segment.
505 * Hash nodes are created if needed.
507 static __inline__
508 fn_node* get_fn_node_inseg(DebugInfo* di,
509 const HChar *dirname,
510 const HChar *filename,
511 const HChar *fnname)
513 obj_node *obj = CLG_(get_obj_node)(di);
514 file_node *file = CLG_(get_file_node)(obj, dirname, filename);
515 fn_node *fn = get_fn_node_infile(file, fnname);
517 return fn;
521 Bool CLG_(get_debug_info)(Addr instr_addr,
522 const HChar **dir,
523 const HChar **file,
524 const HChar **fn_name, UInt* line_num,
525 DebugInfo** pDebugInfo)
527 Bool found_file_line, found_fn, result = True;
528 UInt line;
530 CLG_DEBUG(6, " + get_debug_info(%#lx)\n", instr_addr);
532 DiEpoch ep = VG_(current_DiEpoch)();
533 if (pDebugInfo) {
534 *pDebugInfo = VG_(find_DebugInfo)(ep, instr_addr);
536 // for generated code in anonymous space, pSegInfo is 0
539 found_file_line = VG_(get_filename_linenum)(ep, instr_addr,
540 file,
541 dir,
542 &line);
543 found_fn = VG_(get_fnname)(ep, instr_addr, fn_name);
545 if (!found_file_line && !found_fn) {
546 CLG_(stat).no_debug_BBs++;
547 *file = "???";
548 *fn_name = "???";
549 if (line_num) *line_num=0;
550 result = False;
552 } else if ( found_file_line && found_fn) {
553 CLG_(stat).full_debug_BBs++;
554 if (line_num) *line_num=line;
556 } else if ( found_file_line && !found_fn) {
557 CLG_(stat).file_line_debug_BBs++;
558 *fn_name = "???";
559 if (line_num) *line_num=line;
561 } else /*(!found_file_line && found_fn)*/ {
562 CLG_(stat).fn_name_debug_BBs++;
563 *file = "???";
564 if (line_num) *line_num=0;
567 CLG_DEBUG(6, " - get_debug_info(%#lx): seg '%s', fn %s\n",
568 instr_addr,
569 !pDebugInfo ? "-" :
570 (*pDebugInfo) ? VG_(DebugInfo_get_filename)(*pDebugInfo) :
571 "(None)",
572 *fn_name);
574 return result;
577 /* for _libc_freeres_wrapper => _exit renaming */
578 static BB* exit_bb = 0;
582 * Attach function struct to a BB from debug info.
584 fn_node* CLG_(get_fn_node)(BB* bb)
586 const HChar *fnname, *filename, *dirname;
587 DebugInfo* di;
588 UInt line_num;
589 fn_node* fn;
590 Int i;
592 /* fn from debug info is idempotent for a BB */
593 if (bb->fn) return bb->fn;
595 CLG_DEBUG(3,"+ get_fn_node(BB %#lx)\n", bb_addr(bb));
597 /* get function/file name, line number and object of
598 * the BB according to debug information
600 CLG_(get_debug_info)(bb_addr(bb),
601 &dirname, &filename, &fnname, &line_num, &di);
603 DiEpoch ep = VG_(current_DiEpoch)();
604 if (0 == VG_(strcmp)(fnname, "???")) {
605 int p;
606 static HChar buf[32]; // for sure large enough
607 /* Use address as found in library */
608 if (sizeof(Addr) == 4)
609 p = VG_(sprintf)(buf, "%#08lx", (UWord)bb->offset);
610 else
611 // 64bit address
612 p = VG_(sprintf)(buf, "%#016lx", (UWord)bb->offset);
614 VG_(sprintf)(buf + p, "%s",
615 (bb->sect_kind == Vg_SectData) ? " [Data]" :
616 (bb->sect_kind == Vg_SectBSS) ? " [BSS]" :
617 (bb->sect_kind == Vg_SectGOT) ? " [GOT]" :
618 (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : "");
619 fnname = buf;
621 else {
622 if (VG_(get_fnname_if_entry)(ep, bb_addr(bb), &fnname))
623 bb->is_entry = 1;
626 /* HACK for correct _exit:
627 * _exit is redirected to VG_(__libc_freeres_wrapper) by valgrind,
628 * so we rename it back again :-)
630 if (0 == VG_(strcmp)(fnname, "vgPlain___libc_freeres_wrapper")
631 && exit_bb) {
632 CLG_(get_debug_info)(bb_addr(exit_bb),
633 &dirname, &filename, &fnname, &line_num, &di);
635 CLG_DEBUG(1, "__libc_freeres_wrapper renamed to _exit\n");
637 if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
638 exit_bb = bb;
640 for (i = 0; i < runtime_resolve_addrs; i++) {
641 if ((bb_addr(bb) >= runtime_resolve_addr[i]) &&
642 (bb_addr(bb) < runtime_resolve_addr[i] + runtime_resolve_length[i])) {
643 /* BB in runtime_resolve found by code check; use this name */
644 fnname = "_dl_runtime_resolve";
645 break;
649 /* get fn_node struct for this function */
650 fn = get_fn_node_inseg( di, dirname, filename, fnname);
652 /* if this is the 1st time the function is seen,
653 * some attributes are set */
654 if (fn->pure_cxt == 0) {
656 /* Every function gets a "pure" context, i.e. a context with stack
657 * depth 1 only with this function. This is for compression of mangled
658 * names
660 fn_node* pure[2];
661 pure[0] = 0;
662 pure[1] = fn;
663 fn->pure_cxt = CLG_(get_cxt)(pure+1);
665 if (bb->sect_kind == Vg_SectPLT)
666 fn->skip = CLG_(clo).skip_plt;
668 if (VG_(strcmp)(fn->name, "_dl_runtime_resolve")==0) {
669 fn->pop_on_jump = True;
671 if (VG_(clo_verbosity) > 1)
672 VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve:"
673 " %s +%#lx=%#lx\n",
674 bb->obj->name + bb->obj->last_slash_pos,
675 (UWord)bb->offset, bb_addr(bb));
678 fn->is_malloc = (VG_(strcmp)(fn->name, "malloc")==0);
679 fn->is_realloc = (VG_(strcmp)(fn->name, "realloc")==0);
680 fn->is_free = (VG_(strcmp)(fn->name, "free")==0);
682 /* apply config options from function name patterns
683 * given on command line */
684 CLG_(update_fn_config)(fn);
688 bb->fn = fn;
689 bb->line = line_num;
691 if (dirname[0]) {
692 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s:%u)\n",
693 bb_addr(bb), fnname, filename, line_num);
694 } else
695 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s/%s:%u)\n",
696 bb_addr(bb), fnname, dirname, filename, line_num);
698 return fn;
702 /*------------------------------------------------------------*/
703 /*--- Active function array operations ---*/
704 /*------------------------------------------------------------*/
706 /* The active function array is a thread-specific array
707 * of UInts, mapping function numbers to the active count of
708 * functions.
709 * The active count is the number of times a function appears
710 * in the current call stack, and is used when costs for recursion
711 * levels should be separated.
714 UInt* CLG_(get_fn_entry)(Int n)
716 CLG_ASSERT(n < current_fn_active.size);
717 return current_fn_active.array + n;
720 void CLG_(init_fn_array)(fn_array* a)
722 Int i;
724 CLG_ASSERT(a != 0);
726 a->size = N_INITIAL_FN_ARRAY_SIZE;
727 if (a->size <= CLG_(stat).distinct_fns)
728 a->size = CLG_(stat).distinct_fns+1;
730 a->array = (UInt*) CLG_MALLOC("cl.fn.gfe.1",
731 a->size * sizeof(UInt));
732 for(i=0;i<a->size;i++)
733 a->array[i] = 0;
736 void CLG_(copy_current_fn_array)(fn_array* dst)
738 CLG_ASSERT(dst != 0);
740 dst->size = current_fn_active.size;
741 dst->array = current_fn_active.array;
744 fn_array* CLG_(get_current_fn_array)()
746 return &current_fn_active;
749 void CLG_(set_current_fn_array)(fn_array* a)
751 CLG_ASSERT(a != 0);
753 current_fn_active.size = a->size;
754 current_fn_active.array = a->array;
755 if (current_fn_active.size <= CLG_(stat).distinct_fns)
756 resize_fn_array();
759 /* ensure that active_array is big enough:
760 * <distinct_fns> is the highest index, so <fn_active_array_size>
761 * has to be bigger than that.
763 static void resize_fn_array(void)
765 UInt* new_array;
766 Int i;
768 UInt newsize = current_fn_active.size;
769 while (newsize <= CLG_(stat).distinct_fns) newsize *=2;
771 CLG_DEBUG(0, "Resize fn_active_array: %u => %u\n",
772 current_fn_active.size, newsize);
774 new_array = (UInt*) CLG_MALLOC("cl.fn.rfa.1", newsize * sizeof(UInt));
775 for(i=0;i<current_fn_active.size;i++)
776 new_array[i] = current_fn_active.array[i];
777 while(i<newsize)
778 new_array[i++] = 0;
780 VG_(free)(current_fn_active.array);
781 current_fn_active.size = newsize;
782 current_fn_active.array = new_array;
783 CLG_(stat).fn_array_resizes++;