blob: 7fd14f3beaee26ca330808c24fdffd2f56ac9866 [file] [log] [blame]
weidendoa17f2a32006-03-20 10:27:30 +00001/*--------------------------------------------------------------------*/
2/*--- Callgrind ---*/
3/*--- ct_fn.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Callgrind, a Valgrind tool for call tracing.
8
sewardj4d474d02008-02-11 11:34:59 +00009 Copyright (C) 2002-2008, Josef Weidendorfer (Josef.Weidendorfer@gmx.de)
weidendoa17f2a32006-03-20 10:27:30 +000010
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.
15
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.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 02111-1307, USA.
25
26 The GNU General Public License is contained in the file COPYING.
27*/
28
29#include "global.h"
30
31#define N_INITIAL_FN_ARRAY_SIZE 10071
32
33static fn_array current_fn_active;
34
35static Addr runtime_resolve_addr = 0;
36static int runtime_resolve_length = 0;
37
38/* _ld_runtime_resolve, located in needs special handling:
39 * The jump at end into the resolved function should not be
40 * represented as a call (as usually done in callgrind with jumps),
41 * but as a return + call. Otherwise, the repeated existance of
42 * _ld_runtime_resolve in call chains will lead to huge cycles,
43 * making the profile almost worthless.
44 *
45 * If ld.so is stripped, the symbol will not appear. But as this
46 * function is handcrafted assembler, we search for it...
47 *
48 * Returns 0 if code not found, otherwise start address
49 */
50static void search_runtime_resolve(obj_node* obj)
51{
52 /* We do not check target address of <fixup>, therefore we have >1 ranges.
53 * We use a tuple sequence (offset,length) into the code array for this
54 */
55
sewardj8c2a6ca2006-05-22 00:09:51 +000056#if defined(VGP_x86_linux)
weidendoa17f2a32006-03-20 10:27:30 +000057 /* Check ranges [0-11], [16-23] */
58 static int code_offsets[] = { 0, 12, 16, 8, 24, 0 };
59 static unsigned char code[] = {
60 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
61 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
62 /*16*/ 0x5a, 0x59, 0x87, 0x04, 0x24, 0xc2, 0x08, 0x00 };
63#else
sewardj8c2a6ca2006-05-22 00:09:51 +000064#if defined(VGP_ppc32_linux)
weidendoa17f2a32006-03-20 10:27:30 +000065 static int code_offsets[] = {0, 65, 68, 64, 132, 0 };
66 static unsigned char code[] = {
67 /* 0*/ 0x94, 0x21, 0xff, 0xc0, 0x90, 0x01, 0x00, 0x0c,
68 /* 8*/ 0x90, 0x61, 0x00, 0x10, 0x90, 0x81, 0x00, 0x14,
69 /*16*/ 0x7d, 0x83, 0x63, 0x78, 0x90, 0xa1, 0x00, 0x18,
70 /*24*/ 0x7d, 0x64, 0x5b, 0x78, 0x90, 0xc1, 0x00, 0x1c,
71 /*32*/ 0x7c, 0x08, 0x02, 0xa6, 0x90, 0xe1, 0x00, 0x20,
72 /*40*/ 0x90, 0x01, 0x00, 0x30, 0x91, 0x01, 0x00, 0x24,
73 /*48*/ 0x7c, 0x00, 0x00, 0x26, 0x91, 0x21, 0x00, 0x28,
74 /*56*/ 0x91, 0x41, 0x00, 0x2c, 0x90, 0x01, 0x00, 0x08,
75 /*64*/ 0x48, 0x00, 0x02, 0x91, 0x7c, 0x69, 0x03, 0xa6, /* at 64: bl aff0 <fixup> */
76 /*72*/ 0x80, 0x01, 0x00, 0x30, 0x81, 0x41, 0x00, 0x2c,
77 /*80*/ 0x81, 0x21, 0x00, 0x28, 0x7c, 0x08, 0x03, 0xa6,
78 /*88*/ 0x81, 0x01, 0x00, 0x24, 0x80, 0x01, 0x00, 0x08,
79 /*96*/ 0x80, 0xe1, 0x00, 0x20, 0x80, 0xc1, 0x00, 0x1c,
80 /*104*/0x7c, 0x0f, 0xf1, 0x20, 0x80, 0xa1, 0x00, 0x18,
81 /*112*/0x80, 0x81, 0x00, 0x14, 0x80, 0x61, 0x00, 0x10,
82 /*120*/0x80, 0x01, 0x00, 0x0c, 0x38, 0x21, 0x00, 0x40,
83 /*128*/0x4e, 0x80, 0x04, 0x20 };
84#else
sewardj8c2a6ca2006-05-22 00:09:51 +000085#if defined(VGP_amd64_linux)
weidendoa17f2a32006-03-20 10:27:30 +000086 /* x86_64 */
87 static int code_offsets[] = {0, 62, 66, 44, 110, 0 };
88 static unsigned char code[] = {
89 /* 0*/ 0x48, 0x83, 0xec, 0x38, 0x48, 0x89, 0x04, 0x24,
90 /* 8*/ 0x48, 0x89, 0x4c, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x10,
91 /*18*/ 0x48, 0x89, 0x74, 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20,
92 /*28*/ 0x4c, 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24, 0x30,
93 /*38*/ 0x48, 0x8b, 0x74, 0x24, 0x40, 0x49, 0x89, 0xf3,
94 /*46*/ 0x4c, 0x01, 0xde, 0x4c, 0x01, 0xde, 0x48, 0xc1, 0xe6, 0x03,
95 /*56*/ 0x48, 0x8b, 0x7c, 0x24, 0x38, 0xe8, 0xee, 0x01, 0x00, 0x00,
96 /*66*/ 0x49, 0x89, 0xc3, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
97 /*74*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
98 /*84*/ 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54, 0x24, 0x10,
99 /*94*/ 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48, 0x8b, 0x04, 0x24,
100 /*103*/0x48, 0x83, 0xc4, 0x48, 0x41, 0xff, 0xe3 };
101#else
sewardj8c2a6ca2006-05-22 00:09:51 +0000102 /* Unknown platform, no check is done */
weidendoa17f2a32006-03-20 10:27:30 +0000103 static int code_offsets[] = {0, 0 };
104 static unsigned char code[] = { 0 };
105#endif
106#endif
107#endif
108
109 int *range = &(code_offsets[0]), *r = 0;
110 Bool found = False;
111 Addr addr, end;
112
113 /* Only search in libraries with a given name pattern */
114 if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
115 (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return;
116
117 CLG_DEBUG(1, "search_rs: Checking %d bytes of [%x %x %x...]\n",
118 range[1], code[0], code[1], code[2]);
119
120 end = obj->start + obj->size - range[1];
121 addr = obj->start;
sewardja59aeef2006-05-22 00:05:11 +0000122
123 if (range[1] == 0) return;
124
weidendoa17f2a32006-03-20 10:27:30 +0000125 while(addr < end) {
126 if (VG_(memcmp)( (void*)addr, code, range[1]) == 0) {
127
128 r = range + 2;
129 found = True;
130 while(r[1]) {
131 CLG_DEBUG(1, " [%p] Found! Checking %d bytes of [%x %x %x...]\n",
132 addr, r[1], code[r[0]], code[r[0]+1], code[r[0]+2]);
133
134 if (VG_(memcmp)( (void*)(addr+r[0]), code+r[0], r[1]) != 0) {
135 found = False;
136 break;
137 }
138 r += 2;
139 }
140 if (found) break;
141 }
142 addr++;
143 }
144
145 if (!found || (r==0)) return;
146
147 if (VG_(clo_verbosity) > 1)
148 VG_(message)(Vg_DebugMsg, "Code check found runtime_resolve: %s +%p=%p, length %d",
149 obj->name + obj->last_slash_pos,
150 addr - obj->start, addr, r[0]);
151
152 runtime_resolve_addr = addr;
153 runtime_resolve_length = r[0];
154}
155
156/*------------------------------------------------------------*/
157/*--- Object/File/Function hash entry operations ---*/
158/*------------------------------------------------------------*/
159
160/* Object hash table, fixed */
161static obj_node* obj_table[N_OBJ_ENTRIES];
162
163void CLG_(init_obj_table)()
164{
165 Int i;
166 for (i = 0; i < N_OBJ_ENTRIES; i++)
167 obj_table[i] = 0;
168}
169
170#define HASH_CONSTANT 256
171
172static UInt str_hash(const Char *s, UInt table_size)
173{
174 int hash_value = 0;
175 for ( ; *s; s++)
176 hash_value = (HASH_CONSTANT * hash_value + *s) % table_size;
177 return hash_value;
178}
179
180
181static Char* anonymous_obj = "???";
182
183static __inline__
sewardjb8b79ad2008-03-03 01:35:41 +0000184obj_node* new_obj_node(DebugInfo* di, obj_node* next)
weidendoa17f2a32006-03-20 10:27:30 +0000185{
186 Int i;
187 obj_node* new;
188
189 new = (obj_node*) CLG_MALLOC(sizeof(obj_node));
sewardjb8b79ad2008-03-03 01:35:41 +0000190 new->name = di ? VG_(strdup)( VG_(seginfo_filename)(di) )
weidendoa17f2a32006-03-20 10:27:30 +0000191 : anonymous_obj;
192 for (i = 0; i < N_FILE_ENTRIES; i++) {
193 new->files[i] = NULL;
194 }
195 CLG_(stat).distinct_objs ++;
196 new->number = CLG_(stat).distinct_objs;
sewardjb8b79ad2008-03-03 01:35:41 +0000197 /* JRS 2008 Feb 19: maybe rename .start/.size/.offset to
198 .text_avma/.text_size/.test_bias to make it clearer what these
199 fields really mean */
200 new->start = di ? VG_(seginfo_get_text_avma)(di) : 0;
201 new->size = di ? VG_(seginfo_get_text_size)(di) : 0;
202 new->offset = di ? VG_(seginfo_get_text_bias)(di) : 0;
weidendoa17f2a32006-03-20 10:27:30 +0000203 new->next = next;
204
205 // not only used for debug output (see static.c)
206 new->last_slash_pos = 0;
207 i = 0;
208 while(new->name[i]) {
209 if (new->name[i]=='/') new->last_slash_pos = i+1;
210 i++;
211 }
212
213 if (runtime_resolve_addr == 0) search_runtime_resolve(new);
214
215 return new;
216}
217
sewardjb8b79ad2008-03-03 01:35:41 +0000218obj_node* CLG_(get_obj_node)(DebugInfo* di)
weidendoa17f2a32006-03-20 10:27:30 +0000219{
220 obj_node* curr_obj_node;
221 UInt objname_hash;
222 const UChar* obj_name;
223
sewardjb8b79ad2008-03-03 01:35:41 +0000224 obj_name = di ? (Char*) VG_(seginfo_filename)(di) : anonymous_obj;
weidendoa17f2a32006-03-20 10:27:30 +0000225
226 /* lookup in obj hash */
227 objname_hash = str_hash(obj_name, N_OBJ_ENTRIES);
228 curr_obj_node = obj_table[objname_hash];
229 while (NULL != curr_obj_node &&
230 VG_(strcmp)(obj_name, curr_obj_node->name) != 0) {
231 curr_obj_node = curr_obj_node->next;
232 }
233 if (NULL == curr_obj_node) {
234 obj_table[objname_hash] = curr_obj_node =
sewardjb8b79ad2008-03-03 01:35:41 +0000235 new_obj_node(di, obj_table[objname_hash]);
weidendoa17f2a32006-03-20 10:27:30 +0000236 }
237
238 return curr_obj_node;
239}
240
241
242static __inline__
243file_node* new_file_node(Char filename[FILENAME_LEN],
244 obj_node* obj, file_node* next)
245{
246 Int i;
247 file_node* new = (file_node*) CLG_MALLOC(sizeof(file_node));
248 new->name = VG_(strdup)(filename);
249 for (i = 0; i < N_FN_ENTRIES; i++) {
250 new->fns[i] = NULL;
251 }
252 CLG_(stat).distinct_files++;
253 new->number = CLG_(stat).distinct_files;
254 new->obj = obj;
255 new->next = next;
256 return new;
257}
258
259
260file_node* CLG_(get_file_node)(obj_node* curr_obj_node,
261 Char filename[FILENAME_LEN])
262{
263 file_node* curr_file_node;
264 UInt filename_hash;
265
266 /* lookup in file hash */
267 filename_hash = str_hash(filename, N_FILE_ENTRIES);
268 curr_file_node = curr_obj_node->files[filename_hash];
269 while (NULL != curr_file_node &&
270 VG_(strcmp)(filename, curr_file_node->name) != 0) {
271 curr_file_node = curr_file_node->next;
272 }
273 if (NULL == curr_file_node) {
274 curr_obj_node->files[filename_hash] = curr_file_node =
275 new_file_node(filename, curr_obj_node,
276 curr_obj_node->files[filename_hash]);
277 }
278
279 return curr_file_node;
280}
281
282/* forward decl. */
283static void resize_fn_array(void);
284
285static __inline__
286fn_node* new_fn_node(Char fnname[FILENAME_LEN],
287 file_node* file, fn_node* next)
288{
289 fn_node* new = (fn_node*) CLG_MALLOC(sizeof(fn_node));
290 new->name = VG_(strdup)(fnname);
291
292 CLG_(stat).distinct_fns++;
293 new->number = CLG_(stat).distinct_fns;
294 new->last_cxt = 0;
295 new->pure_cxt = 0;
296 new->file = file;
297 new->next = next;
298
299 new->dump_before = False;
300 new->dump_after = False;
301 new->zero_before = False;
302 new->toggle_collect = False;
303 new->skip = False;
weidendoa762b0f2006-05-01 00:55:54 +0000304 new->pop_on_jump = CLG_(clo).pop_on_jump;
weidendoa17f2a32006-03-20 10:27:30 +0000305 new->is_malloc = False;
306 new->is_realloc = False;
307 new->is_free = False;
308
309 new->group = 0;
310 new->separate_callers = CLG_(clo).separate_callers;
311 new->separate_recursions = CLG_(clo).separate_recursions;
312
313#if CLG_ENABLE_DEBUG
314 new->verbosity = -1;
315#endif
316
317 if (CLG_(stat).distinct_fns >= current_fn_active.size)
318 resize_fn_array();
319
320 return new;
321}
322
323
324/* Get a function node in hash2 with known file node.
325 * hash nodes are created if needed
326 */
327static
328fn_node* get_fn_node_infile(file_node* curr_file_node,
329 Char fnname[FN_NAME_LEN])
330{
331 fn_node* curr_fn_node;
332 UInt fnname_hash;
333
334 CLG_ASSERT(curr_file_node != 0);
335
336 /* lookup in function hash */
337 fnname_hash = str_hash(fnname, N_FN_ENTRIES);
338 curr_fn_node = curr_file_node->fns[fnname_hash];
339 while (NULL != curr_fn_node &&
340 VG_(strcmp)(fnname, curr_fn_node->name) != 0) {
341 curr_fn_node = curr_fn_node->next;
342 }
343 if (NULL == curr_fn_node) {
344 curr_file_node->fns[fnname_hash] = curr_fn_node =
345 new_fn_node(fnname, curr_file_node,
346 curr_file_node->fns[fnname_hash]);
347 }
348
349 return curr_fn_node;
350}
351
352
353/* Get a function node in a Segment.
354 * Hash nodes are created if needed.
355 */
356static __inline__
sewardjb8b79ad2008-03-03 01:35:41 +0000357fn_node* get_fn_node_inseg(DebugInfo* di,
weidendoa17f2a32006-03-20 10:27:30 +0000358 Char filename[FILENAME_LEN],
359 Char fnname[FN_NAME_LEN])
360{
sewardjb8b79ad2008-03-03 01:35:41 +0000361 obj_node *obj = CLG_(get_obj_node)(di);
weidendoa17f2a32006-03-20 10:27:30 +0000362 file_node *file = CLG_(get_file_node)(obj, filename);
363 fn_node *fn = get_fn_node_infile(file, fnname);
364
365 return fn;
366}
367
368
369Bool CLG_(get_debug_info)(Addr instr_addr,
weidendo3db43222007-09-17 12:52:10 +0000370 Char file[FILENAME_LEN],
weidendoa17f2a32006-03-20 10:27:30 +0000371 Char fn_name[FN_NAME_LEN], UInt* line_num,
sewardjb8b79ad2008-03-03 01:35:41 +0000372 DebugInfo** pDebugInfo)
weidendoa17f2a32006-03-20 10:27:30 +0000373{
weidendo3db43222007-09-17 12:52:10 +0000374 Bool found_file_line, found_fn, found_dirname, result = True;
375 Char dir[FILENAME_LEN];
weidendoa17f2a32006-03-20 10:27:30 +0000376 UInt line;
377
378 CLG_DEBUG(6, " + get_debug_info(%p)\n", instr_addr);
379
sewardjb8b79ad2008-03-03 01:35:41 +0000380 if (pDebugInfo) {
381 *pDebugInfo = VG_(find_seginfo)(instr_addr);
weidendoa17f2a32006-03-20 10:27:30 +0000382
383 // for generated code in anonymous space, pSegInfo is 0
384 }
385
weidendo3db43222007-09-17 12:52:10 +0000386 found_file_line = VG_(get_filename_linenum)(instr_addr,
387 file, FILENAME_LEN,
388 dir, FILENAME_LEN,
389 &found_dirname,
390 &line);
391 found_fn = VG_(get_fnname)(instr_addr,
392 fn_name, FN_NAME_LEN);
weidendoa17f2a32006-03-20 10:27:30 +0000393
weidendo3db43222007-09-17 12:52:10 +0000394 if (found_dirname) {
395 // +1 for the '/'.
396 CLG_ASSERT(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILENAME_LEN);
397 VG_(strcat)(dir, "/"); // Append '/'
398 VG_(strcat)(dir, file); // Append file to dir
399 VG_(strcpy)(file, dir); // Move dir+file to file
400 }
401
402 if (!found_file_line && !found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000403 CLG_(stat).no_debug_BBs++;
weidendo3db43222007-09-17 12:52:10 +0000404 VG_(strcpy)(file, "???");
weidendoa17f2a32006-03-20 10:27:30 +0000405 VG_(strcpy)(fn_name, "???");
406 if (line_num) *line_num=0;
407 result = False;
408
weidendo3db43222007-09-17 12:52:10 +0000409 } else if ( found_file_line && found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000410 CLG_(stat).full_debug_BBs++;
411 if (line_num) *line_num=line;
412
weidendo3db43222007-09-17 12:52:10 +0000413 } else if ( found_file_line && !found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000414 CLG_(stat).file_line_debug_BBs++;
415 VG_(strcpy)(fn_name, "???");
416 if (line_num) *line_num=line;
417
weidendo3db43222007-09-17 12:52:10 +0000418 } else /*(!found_file_line && found_fn)*/ {
weidendoa17f2a32006-03-20 10:27:30 +0000419 CLG_(stat).fn_name_debug_BBs++;
weidendo3db43222007-09-17 12:52:10 +0000420 VG_(strcpy)(file, "???");
weidendoa17f2a32006-03-20 10:27:30 +0000421 if (line_num) *line_num=0;
422 }
423
424 CLG_DEBUG(6, " - get_debug_info(%p): seg '%s', fn %s\n",
425 instr_addr,
sewardjb8b79ad2008-03-03 01:35:41 +0000426 !pDebugInfo ? (const UChar*)"-" :
427 (*pDebugInfo) ? VG_(seginfo_filename)(*pDebugInfo) :
weidendoa17f2a32006-03-20 10:27:30 +0000428 (const UChar*)"(None)",
429 fn_name);
430
431 return result;
432}
433
434/* for _libc_freeres_wrapper => _exit renaming */
435static BB* exit_bb = 0;
436
437
438/*
439 * Attach function struct to a BB from debug info.
440 */
441fn_node* CLG_(get_fn_node)(BB* bb)
442{
443 Char filename[FILENAME_LEN], fnname[FN_NAME_LEN];
sewardjb8b79ad2008-03-03 01:35:41 +0000444 DebugInfo* di;
weidendoa17f2a32006-03-20 10:27:30 +0000445 UInt line_num;
446 fn_node* fn;
447
448 /* fn from debug info is idempotent for a BB */
449 if (bb->fn) return bb->fn;
450
451 CLG_DEBUG(3,"+ get_fn_node(BB %p)\n", bb_addr(bb));
452
453 /* get function/file name, line number and object of
454 * the BB according to debug information
455 */
456 CLG_(get_debug_info)(bb_addr(bb),
sewardjb8b79ad2008-03-03 01:35:41 +0000457 filename, fnname, &line_num, &di);
weidendoa17f2a32006-03-20 10:27:30 +0000458
459 if (0 == VG_(strcmp)(fnname, "???")) {
460 int p;
461
462 /* Use address as found in library */
463 if (sizeof(Addr) == 4)
464 p = VG_(sprintf)(fnname, "%08p", bb->offset);
465 else
466 // 64bit address
467 p = VG_(sprintf)(fnname, "%016p", bb->offset);
468
469 VG_(sprintf)(fnname+p, "%s",
470 (bb->sect_kind == Vg_SectData) ? " [Data]" :
471 (bb->sect_kind == Vg_SectBSS) ? " [BSS]" :
472 (bb->sect_kind == Vg_SectGOT) ? " [GOT]" :
473 (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : "");
474 }
475 else {
476 if (VG_(get_fnname_if_entry)(bb_addr(bb), fnname, FN_NAME_LEN))
477 bb->is_entry = 1;
478 }
479
480 /* HACK for correct _exit:
481 * _exit is redirected to VG_(__libc_freeres_wrapper) by valgrind,
482 * so we rename it back again :-)
483 */
484 if (0 == VG_(strcmp)(fnname, "vgPlain___libc_freeres_wrapper")
485 && exit_bb) {
486 CLG_(get_debug_info)(bb_addr(exit_bb),
sewardjb8b79ad2008-03-03 01:35:41 +0000487 filename, fnname, &line_num, &di);
weidendoa17f2a32006-03-20 10:27:30 +0000488
489 CLG_DEBUG(1, "__libc_freeres_wrapper renamed to _exit\n");
490 }
491 if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
492 exit_bb = bb;
493
494 if (runtime_resolve_addr &&
495 (bb_addr(bb) >= runtime_resolve_addr) &&
496 (bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) {
497 /* BB in runtime_resolve found by code check; use this name */
498 VG_(sprintf)(fnname, "_dl_runtime_resolve");
499 }
500
501 /* get fn_node struct for this function */
sewardjb8b79ad2008-03-03 01:35:41 +0000502 fn = get_fn_node_inseg( di, filename, fnname);
weidendoa17f2a32006-03-20 10:27:30 +0000503
504 /* if this is the 1st time the function is seen,
505 * some attributes are set */
506 if (fn->pure_cxt == 0) {
507
508 /* Every function gets a "pure" context, i.e. a context with stack
509 * depth 1 only with this function. This is for compression of mangled
510 * names
511 */
512 fn_node* pure[2];
513 pure[0] = 0;
514 pure[1] = fn;
515 fn->pure_cxt = CLG_(get_cxt)(pure+1);
516
517 if (bb->sect_kind == Vg_SectPLT)
518 fn->skip = CLG_(clo).skip_plt;
519
520 if (VG_(strcmp)(fn->name, "_dl_runtime_resolve")==0) {
521 fn->pop_on_jump = True;
522
523 if (VG_(clo_verbosity) > 1)
524 VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve: %s +%p=%p",
525 bb->obj->name + bb->obj->last_slash_pos,
526 bb->offset, bb_addr(bb));
527 }
528
529 fn->is_malloc = (VG_(strcmp)(fn->name, "malloc")==0);
530 fn->is_realloc = (VG_(strcmp)(fn->name, "realloc")==0);
531 fn->is_free = (VG_(strcmp)(fn->name, "free")==0);
532
533 /* apply config options from function name patterns
534 * given on command line */
535 CLG_(update_fn_config)(fn);
536 }
537
538
539 bb->fn = fn;
540 bb->line = line_num;
541
542 CLG_DEBUG(3,"- get_fn_node(BB %p): %s (in %s:%u)\n",
543 bb_addr(bb), fnname, filename, line_num);
544
545 return fn;
546}
547
548
549/*------------------------------------------------------------*/
550/*--- Active function array operations ---*/
551/*------------------------------------------------------------*/
552
553/* The active function array is a thread-specific array
554 * of UInts, mapping function numbers to the active count of
555 * functions.
556 * The active count is the number of times a function appears
557 * in the current call stack, and is used when costs for recursion
558 * levels should be separated.
559 */
560
561UInt* CLG_(get_fn_entry)(Int n)
562{
563 CLG_ASSERT(n < current_fn_active.size);
564 return current_fn_active.array + n;
565}
566
567void CLG_(init_fn_array)(fn_array* a)
568{
569 Int i;
570
571 CLG_ASSERT(a != 0);
572
573 a->size = N_INITIAL_FN_ARRAY_SIZE;
574 if (a->size <= CLG_(stat).distinct_fns)
575 a->size = CLG_(stat).distinct_fns+1;
576
577 a->array = (UInt*) CLG_MALLOC(a->size * sizeof(UInt));
578 for(i=0;i<a->size;i++)
579 a->array[i] = 0;
580}
581
582void CLG_(copy_current_fn_array)(fn_array* dst)
583{
584 CLG_ASSERT(dst != 0);
585
586 dst->size = current_fn_active.size;
587 dst->array = current_fn_active.array;
588}
589
590fn_array* CLG_(get_current_fn_array)()
591{
592 return &current_fn_active;
593}
594
595void CLG_(set_current_fn_array)(fn_array* a)
596{
597 CLG_ASSERT(a != 0);
598
599 current_fn_active.size = a->size;
600 current_fn_active.array = a->array;
601 if (current_fn_active.size <= CLG_(stat).distinct_fns)
602 resize_fn_array();
603}
604
605/* ensure that active_array is big enough:
606 * <distinct_fns> is the highest index, so <fn_active_array_size>
607 * has to be bigger than that.
608 */
609static void resize_fn_array(void)
610{
611 UInt* new;
612 Int i, newsize;
613
614 newsize = current_fn_active.size;
615 while (newsize <= CLG_(stat).distinct_fns) newsize *=2;
616
617 CLG_DEBUG(0, "Resize fn_active_array: %d => %d\n",
618 current_fn_active.size, newsize);
619
620 new = (UInt*) CLG_MALLOC(newsize * sizeof(UInt));
621 for(i=0;i<current_fn_active.size;i++)
622 new[i] = current_fn_active.array[i];
623 while(i<newsize)
624 new[i++] = 0;
625
626 VG_(free)(current_fn_active.array);
627 current_fn_active.size = newsize;
628 current_fn_active.array = new;
629 CLG_(stat).fn_array_resizes++;
630}
631
632