blob: d4c7b24c187fd34278065fdb02a4dbdcef077e90 [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
Elliott Hughesed398002017-06-21 14:41:24 -07009 Copyright (C) 2002-2017, 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
weidendo234780c2008-12-18 19:48:35 +000038// a code pattern is a list of tuples (start offset, length)
39struct chunk_t { int start, len; };
40struct pattern
41{
florian19f91bb2012-11-10 22:29:54 +000042 const HChar* name;
weidendo234780c2008-12-18 19:48:35 +000043 int len;
44 struct chunk_t chunk[];
45};
46
47/* Scan for a pattern in the code of an ELF object.
48 * If found, return true and set runtime_resolve_{addr,length}
49 */
njn85f79da2009-05-18 05:10:00 +000050__attribute__((unused)) // Possibly; depends on the platform.
weidendo234780c2008-12-18 19:48:35 +000051static Bool check_code(obj_node* obj,
florian19f91bb2012-11-10 22:29:54 +000052 UChar code[], struct pattern* pat)
weidendo234780c2008-12-18 19:48:35 +000053{
54 Bool found;
55 Addr addr, end;
56 int chunk, start, len;
57
58 /* first chunk of pattern should always start at offset 0 and
59 * have at least 3 bytes */
60 CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2));
61
62 CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n",
63 obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]);
64
65 end = obj->start + obj->size - pat->len;
66 addr = obj->start;
67 while(addr < end) {
68 found = (VG_(memcmp)( (void*)addr, code, pat->chunk[0].len) == 0);
69
70 if (found) {
71 chunk = 1;
72 while(1) {
73 start = pat->chunk[chunk].start;
74 len = pat->chunk[chunk].len;
75 if (len == 0) break;
76
77 CLG_ASSERT(len >2);
sewardj0f33adf2009-07-15 14:51:03 +000078 CLG_DEBUG(1, " found chunk %d at %#lx, checking %d bytes "
79 "of [%x %x %x...]\n",
weidendo234780c2008-12-18 19:48:35 +000080 chunk-1, addr - obj->start, len,
81 code[start], code[start+1], code[start+2]);
82
83 if (VG_(memcmp)( (void*)(addr+start), code+start, len) != 0) {
84 found = False;
85 break;
86 }
87 chunk++;
88 }
89
90 if (found) {
91 CLG_DEBUG(1, "found at offset %#lx.\n", addr - obj->start);
92 if (VG_(clo_verbosity) > 1)
sewardj0f33adf2009-07-15 14:51:03 +000093 VG_(message)(Vg_DebugMsg, "Found runtime_resolve (%s): "
94 "%s +%#lx=%#lx, length %d\n",
weidendo234780c2008-12-18 19:48:35 +000095 pat->name, obj->name + obj->last_slash_pos,
96 addr - obj->start, addr, pat->len);
97
98 runtime_resolve_addr = addr;
99 runtime_resolve_length = pat->len;
100 return True;
101 }
102 }
103 addr++;
104 }
105 CLG_DEBUG(1, " found nothing.\n");
106 return False;
107}
njnae54ba42009-05-18 05:10:56 +0000108
weidendo234780c2008-12-18 19:48:35 +0000109
110/* _ld_runtime_resolve, located in ld.so, needs special handling:
weidendoa17f2a32006-03-20 10:27:30 +0000111 * The jump at end into the resolved function should not be
112 * represented as a call (as usually done in callgrind with jumps),
Elliott Hughesed398002017-06-21 14:41:24 -0700113 * but as a return + call. Otherwise, the repeated existence of
weidendoa17f2a32006-03-20 10:27:30 +0000114 * _ld_runtime_resolve in call chains will lead to huge cycles,
115 * making the profile almost worthless.
116 *
117 * If ld.so is stripped, the symbol will not appear. But as this
weidendo234780c2008-12-18 19:48:35 +0000118 * function is handcrafted assembler, we search for it.
weidendoa17f2a32006-03-20 10:27:30 +0000119 *
weidendo234780c2008-12-18 19:48:35 +0000120 * We stop if the ELF object name does not seem to be the runtime linker
weidendoa17f2a32006-03-20 10:27:30 +0000121 */
weidendo234780c2008-12-18 19:48:35 +0000122static Bool search_runtime_resolve(obj_node* obj)
weidendoa17f2a32006-03-20 10:27:30 +0000123{
sewardj8c2a6ca2006-05-22 00:09:51 +0000124#if defined(VGP_x86_linux)
florian19f91bb2012-11-10 22:29:54 +0000125 static UChar code[] = {
weidendoa17f2a32006-03-20 10:27:30 +0000126 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
127 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
128 /*16*/ 0x5a, 0x59, 0x87, 0x04, 0x24, 0xc2, 0x08, 0x00 };
weidendo234780c2008-12-18 19:48:35 +0000129 /* Check ranges [0-11] and [16-23] ([12-15] is an absolute address) */
130 static struct pattern pat = {
131 "x86-def", 24, {{ 0,12 }, { 16,8 }, { 24,0}} };
132
133 /* Pattern for glibc-2.8 on OpenSuse11.0 */
florian19f91bb2012-11-10 22:29:54 +0000134 static UChar code_28[] = {
weidendo234780c2008-12-18 19:48:35 +0000135 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
136 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
137 /*16*/ 0x5a, 0x8b, 0x0c, 0x24, 0x89, 0x04, 0x24, 0x8b,
138 /*24*/ 0x44, 0x24, 0x04, 0xc2, 0x0c, 0x00 };
139 static struct pattern pat_28 = {
140 "x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} };
141
142 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
143 if (check_code(obj, code, &pat)) return True;
144 if (check_code(obj, code_28, &pat_28)) return True;
145 return False;
146#endif
147
sewardj8c2a6ca2006-05-22 00:09:51 +0000148#if defined(VGP_ppc32_linux)
florian19f91bb2012-11-10 22:29:54 +0000149 static UChar code[] = {
weidendoa17f2a32006-03-20 10:27:30 +0000150 /* 0*/ 0x94, 0x21, 0xff, 0xc0, 0x90, 0x01, 0x00, 0x0c,
151 /* 8*/ 0x90, 0x61, 0x00, 0x10, 0x90, 0x81, 0x00, 0x14,
152 /*16*/ 0x7d, 0x83, 0x63, 0x78, 0x90, 0xa1, 0x00, 0x18,
153 /*24*/ 0x7d, 0x64, 0x5b, 0x78, 0x90, 0xc1, 0x00, 0x1c,
154 /*32*/ 0x7c, 0x08, 0x02, 0xa6, 0x90, 0xe1, 0x00, 0x20,
155 /*40*/ 0x90, 0x01, 0x00, 0x30, 0x91, 0x01, 0x00, 0x24,
156 /*48*/ 0x7c, 0x00, 0x00, 0x26, 0x91, 0x21, 0x00, 0x28,
157 /*56*/ 0x91, 0x41, 0x00, 0x2c, 0x90, 0x01, 0x00, 0x08,
158 /*64*/ 0x48, 0x00, 0x02, 0x91, 0x7c, 0x69, 0x03, 0xa6, /* at 64: bl aff0 <fixup> */
159 /*72*/ 0x80, 0x01, 0x00, 0x30, 0x81, 0x41, 0x00, 0x2c,
160 /*80*/ 0x81, 0x21, 0x00, 0x28, 0x7c, 0x08, 0x03, 0xa6,
161 /*88*/ 0x81, 0x01, 0x00, 0x24, 0x80, 0x01, 0x00, 0x08,
162 /*96*/ 0x80, 0xe1, 0x00, 0x20, 0x80, 0xc1, 0x00, 0x1c,
163 /*104*/0x7c, 0x0f, 0xf1, 0x20, 0x80, 0xa1, 0x00, 0x18,
164 /*112*/0x80, 0x81, 0x00, 0x14, 0x80, 0x61, 0x00, 0x10,
165 /*120*/0x80, 0x01, 0x00, 0x0c, 0x38, 0x21, 0x00, 0x40,
166 /*128*/0x4e, 0x80, 0x04, 0x20 };
weidendo234780c2008-12-18 19:48:35 +0000167 static struct pattern pat = {
168 "ppc32-def", 132, {{ 0,65 }, { 68,64 }, { 132,0 }} };
169
170 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
171 return check_code(obj, code, &pat);
172#endif
173
sewardj8c2a6ca2006-05-22 00:09:51 +0000174#if defined(VGP_amd64_linux)
florian19f91bb2012-11-10 22:29:54 +0000175 static UChar code[] = {
weidendoa17f2a32006-03-20 10:27:30 +0000176 /* 0*/ 0x48, 0x83, 0xec, 0x38, 0x48, 0x89, 0x04, 0x24,
177 /* 8*/ 0x48, 0x89, 0x4c, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x10,
178 /*18*/ 0x48, 0x89, 0x74, 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20,
179 /*28*/ 0x4c, 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24, 0x30,
180 /*38*/ 0x48, 0x8b, 0x74, 0x24, 0x40, 0x49, 0x89, 0xf3,
181 /*46*/ 0x4c, 0x01, 0xde, 0x4c, 0x01, 0xde, 0x48, 0xc1, 0xe6, 0x03,
182 /*56*/ 0x48, 0x8b, 0x7c, 0x24, 0x38, 0xe8, 0xee, 0x01, 0x00, 0x00,
183 /*66*/ 0x49, 0x89, 0xc3, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
184 /*74*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
185 /*84*/ 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54, 0x24, 0x10,
186 /*94*/ 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48, 0x8b, 0x04, 0x24,
187 /*103*/0x48, 0x83, 0xc4, 0x48, 0x41, 0xff, 0xe3 };
weidendo234780c2008-12-18 19:48:35 +0000188 static struct pattern pat = {
189 "amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} };
weidendoa17f2a32006-03-20 10:27:30 +0000190
weidendoa17f2a32006-03-20 10:27:30 +0000191 if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
weidendo234780c2008-12-18 19:48:35 +0000192 (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False;
193 return check_code(obj, code, &pat);
194#endif
weidendoa17f2a32006-03-20 10:27:30 +0000195
weidendo234780c2008-12-18 19:48:35 +0000196 /* For other platforms, no patterns known */
197 return False;
weidendoa17f2a32006-03-20 10:27:30 +0000198}
199
weidendo234780c2008-12-18 19:48:35 +0000200
weidendoa17f2a32006-03-20 10:27:30 +0000201/*------------------------------------------------------------*/
202/*--- Object/File/Function hash entry operations ---*/
203/*------------------------------------------------------------*/
204
205/* Object hash table, fixed */
206static obj_node* obj_table[N_OBJ_ENTRIES];
207
208void CLG_(init_obj_table)()
209{
210 Int i;
211 for (i = 0; i < N_OBJ_ENTRIES; i++)
212 obj_table[i] = 0;
213}
214
215#define HASH_CONSTANT 256
216
florian19f91bb2012-11-10 22:29:54 +0000217static UInt str_hash(const HChar *s, UInt table_size)
weidendoa17f2a32006-03-20 10:27:30 +0000218{
219 int hash_value = 0;
220 for ( ; *s; s++)
221 hash_value = (HASH_CONSTANT * hash_value + *s) % table_size;
222 return hash_value;
223}
224
225
florian25f6c572012-10-21 02:55:56 +0000226static const HChar* anonymous_obj = "???";
weidendoa17f2a32006-03-20 10:27:30 +0000227
228static __inline__
sewardjb8b79ad2008-03-03 01:35:41 +0000229obj_node* new_obj_node(DebugInfo* di, obj_node* next)
weidendoa17f2a32006-03-20 10:27:30 +0000230{
231 Int i;
weidendo0b23d6e2009-06-15 00:16:32 +0000232 obj_node* obj;
weidendoa17f2a32006-03-20 10:27:30 +0000233
weidendo0b23d6e2009-06-15 00:16:32 +0000234 obj = (obj_node*) CLG_MALLOC("cl.fn.non.1", sizeof(obj_node));
florian19f91bb2012-11-10 22:29:54 +0000235 obj->name = di ? VG_(strdup)( "cl.fn.non.2",
sewardje3f1e592009-07-31 09:41:29 +0000236 VG_(DebugInfo_get_filename)(di) )
237 : anonymous_obj;
weidendoa17f2a32006-03-20 10:27:30 +0000238 for (i = 0; i < N_FILE_ENTRIES; i++) {
weidendo0b23d6e2009-06-15 00:16:32 +0000239 obj->files[i] = NULL;
weidendoa17f2a32006-03-20 10:27:30 +0000240 }
241 CLG_(stat).distinct_objs ++;
weidendo0b23d6e2009-06-15 00:16:32 +0000242 obj->number = CLG_(stat).distinct_objs;
sewardjb8b79ad2008-03-03 01:35:41 +0000243 /* JRS 2008 Feb 19: maybe rename .start/.size/.offset to
244 .text_avma/.text_size/.test_bias to make it clearer what these
245 fields really mean */
sewardje3f1e592009-07-31 09:41:29 +0000246 obj->start = di ? VG_(DebugInfo_get_text_avma)(di) : 0;
247 obj->size = di ? VG_(DebugInfo_get_text_size)(di) : 0;
248 obj->offset = di ? VG_(DebugInfo_get_text_bias)(di) : 0;
weidendo0b23d6e2009-06-15 00:16:32 +0000249 obj->next = next;
weidendoa17f2a32006-03-20 10:27:30 +0000250
251 // not only used for debug output (see static.c)
weidendo0b23d6e2009-06-15 00:16:32 +0000252 obj->last_slash_pos = 0;
weidendoa17f2a32006-03-20 10:27:30 +0000253 i = 0;
weidendo0b23d6e2009-06-15 00:16:32 +0000254 while(obj->name[i]) {
255 if (obj->name[i]=='/') obj->last_slash_pos = i+1;
weidendoa17f2a32006-03-20 10:27:30 +0000256 i++;
257 }
258
weidendo0b23d6e2009-06-15 00:16:32 +0000259 if (runtime_resolve_addr == 0) search_runtime_resolve(obj);
260
261 return obj;
weidendoa17f2a32006-03-20 10:27:30 +0000262}
263
sewardjb8b79ad2008-03-03 01:35:41 +0000264obj_node* CLG_(get_obj_node)(DebugInfo* di)
weidendoa17f2a32006-03-20 10:27:30 +0000265{
266 obj_node* curr_obj_node;
267 UInt objname_hash;
florian25f6c572012-10-21 02:55:56 +0000268 const HChar* obj_name;
weidendoa17f2a32006-03-20 10:27:30 +0000269
florian3e798632012-11-24 19:41:54 +0000270 obj_name = di ? VG_(DebugInfo_get_filename)(di) : anonymous_obj;
weidendoa17f2a32006-03-20 10:27:30 +0000271
272 /* lookup in obj hash */
273 objname_hash = str_hash(obj_name, N_OBJ_ENTRIES);
274 curr_obj_node = obj_table[objname_hash];
275 while (NULL != curr_obj_node &&
276 VG_(strcmp)(obj_name, curr_obj_node->name) != 0) {
277 curr_obj_node = curr_obj_node->next;
278 }
279 if (NULL == curr_obj_node) {
280 obj_table[objname_hash] = curr_obj_node =
sewardjb8b79ad2008-03-03 01:35:41 +0000281 new_obj_node(di, obj_table[objname_hash]);
weidendoa17f2a32006-03-20 10:27:30 +0000282 }
283
284 return curr_obj_node;
285}
286
287
288static __inline__
florian536e3d72014-10-26 19:16:14 +0000289file_node* new_file_node(const HChar *filename,
weidendoa17f2a32006-03-20 10:27:30 +0000290 obj_node* obj, file_node* next)
291{
292 Int i;
weidendo0b23d6e2009-06-15 00:16:32 +0000293 file_node* file = (file_node*) CLG_MALLOC("cl.fn.nfn.1",
sewardj9c606bd2008-09-18 18:12:50 +0000294 sizeof(file_node));
weidendo0b23d6e2009-06-15 00:16:32 +0000295 file->name = VG_(strdup)("cl.fn.nfn.2", filename);
weidendoa17f2a32006-03-20 10:27:30 +0000296 for (i = 0; i < N_FN_ENTRIES; i++) {
weidendo0b23d6e2009-06-15 00:16:32 +0000297 file->fns[i] = NULL;
weidendoa17f2a32006-03-20 10:27:30 +0000298 }
299 CLG_(stat).distinct_files++;
weidendo0b23d6e2009-06-15 00:16:32 +0000300 file->number = CLG_(stat).distinct_files;
301 file->obj = obj;
302 file->next = next;
303 return file;
weidendoa17f2a32006-03-20 10:27:30 +0000304}
305
306
307file_node* CLG_(get_file_node)(obj_node* curr_obj_node,
florian536e3d72014-10-26 19:16:14 +0000308 const HChar *dir, const HChar *file)
weidendoa17f2a32006-03-20 10:27:30 +0000309{
310 file_node* curr_file_node;
311 UInt filename_hash;
312
florian536e3d72014-10-26 19:16:14 +0000313 /* Build up an absolute pathname, if there is a directory available */
314 HChar filename[VG_(strlen)(dir) + 1 + VG_(strlen)(file) + 1];
315 VG_(strcpy)(filename, dir);
316 if (filename[0] != '\0') {
317 VG_(strcat)(filename, "/");
318 }
319 VG_(strcat)(filename, file);
320
weidendoa17f2a32006-03-20 10:27:30 +0000321 /* lookup in file hash */
322 filename_hash = str_hash(filename, N_FILE_ENTRIES);
323 curr_file_node = curr_obj_node->files[filename_hash];
324 while (NULL != curr_file_node &&
325 VG_(strcmp)(filename, curr_file_node->name) != 0) {
326 curr_file_node = curr_file_node->next;
327 }
328 if (NULL == curr_file_node) {
329 curr_obj_node->files[filename_hash] = curr_file_node =
330 new_file_node(filename, curr_obj_node,
331 curr_obj_node->files[filename_hash]);
332 }
333
334 return curr_file_node;
335}
336
337/* forward decl. */
338static void resize_fn_array(void);
339
340static __inline__
florian46cc0452014-10-25 19:20:38 +0000341fn_node* new_fn_node(const HChar *fnname,
weidendoa17f2a32006-03-20 10:27:30 +0000342 file_node* file, fn_node* next)
343{
weidendo0b23d6e2009-06-15 00:16:32 +0000344 fn_node* fn = (fn_node*) CLG_MALLOC("cl.fn.nfnnd.1",
sewardj9c606bd2008-09-18 18:12:50 +0000345 sizeof(fn_node));
weidendo0b23d6e2009-06-15 00:16:32 +0000346 fn->name = VG_(strdup)("cl.fn.nfnnd.2", fnname);
weidendoa17f2a32006-03-20 10:27:30 +0000347
348 CLG_(stat).distinct_fns++;
weidendo0b23d6e2009-06-15 00:16:32 +0000349 fn->number = CLG_(stat).distinct_fns;
350 fn->last_cxt = 0;
351 fn->pure_cxt = 0;
352 fn->file = file;
353 fn->next = next;
weidendoa17f2a32006-03-20 10:27:30 +0000354
weidendo0b23d6e2009-06-15 00:16:32 +0000355 fn->dump_before = False;
356 fn->dump_after = False;
357 fn->zero_before = False;
358 fn->toggle_collect = False;
359 fn->skip = False;
360 fn->pop_on_jump = CLG_(clo).pop_on_jump;
361 fn->is_malloc = False;
362 fn->is_realloc = False;
363 fn->is_free = False;
weidendoa17f2a32006-03-20 10:27:30 +0000364
weidendo0b23d6e2009-06-15 00:16:32 +0000365 fn->group = 0;
366 fn->separate_callers = CLG_(clo).separate_callers;
367 fn->separate_recursions = CLG_(clo).separate_recursions;
weidendoa17f2a32006-03-20 10:27:30 +0000368
369#if CLG_ENABLE_DEBUG
weidendo0b23d6e2009-06-15 00:16:32 +0000370 fn->verbosity = -1;
weidendoa17f2a32006-03-20 10:27:30 +0000371#endif
372
373 if (CLG_(stat).distinct_fns >= current_fn_active.size)
374 resize_fn_array();
375
weidendo0b23d6e2009-06-15 00:16:32 +0000376 return fn;
weidendoa17f2a32006-03-20 10:27:30 +0000377}
378
379
380/* Get a function node in hash2 with known file node.
381 * hash nodes are created if needed
382 */
383static
384fn_node* get_fn_node_infile(file_node* curr_file_node,
florian46cc0452014-10-25 19:20:38 +0000385 const HChar *fnname)
weidendoa17f2a32006-03-20 10:27:30 +0000386{
387 fn_node* curr_fn_node;
388 UInt fnname_hash;
389
390 CLG_ASSERT(curr_file_node != 0);
391
392 /* lookup in function hash */
393 fnname_hash = str_hash(fnname, N_FN_ENTRIES);
394 curr_fn_node = curr_file_node->fns[fnname_hash];
395 while (NULL != curr_fn_node &&
396 VG_(strcmp)(fnname, curr_fn_node->name) != 0) {
397 curr_fn_node = curr_fn_node->next;
398 }
399 if (NULL == curr_fn_node) {
400 curr_file_node->fns[fnname_hash] = curr_fn_node =
401 new_fn_node(fnname, curr_file_node,
402 curr_file_node->fns[fnname_hash]);
403 }
404
405 return curr_fn_node;
406}
407
408
409/* Get a function node in a Segment.
410 * Hash nodes are created if needed.
411 */
412static __inline__
sewardjb8b79ad2008-03-03 01:35:41 +0000413fn_node* get_fn_node_inseg(DebugInfo* di,
florian536e3d72014-10-26 19:16:14 +0000414 const HChar *dirname,
415 const HChar *filename,
florian46cc0452014-10-25 19:20:38 +0000416 const HChar *fnname)
weidendoa17f2a32006-03-20 10:27:30 +0000417{
sewardjb8b79ad2008-03-03 01:35:41 +0000418 obj_node *obj = CLG_(get_obj_node)(di);
florian536e3d72014-10-26 19:16:14 +0000419 file_node *file = CLG_(get_file_node)(obj, dirname, filename);
weidendoa17f2a32006-03-20 10:27:30 +0000420 fn_node *fn = get_fn_node_infile(file, fnname);
421
422 return fn;
423}
424
425
426Bool CLG_(get_debug_info)(Addr instr_addr,
florian10ef7252014-10-27 12:06:35 +0000427 const HChar **dir,
428 const HChar **file,
florian536e3d72014-10-26 19:16:14 +0000429 const HChar **fn_name, UInt* line_num,
430 DebugInfo** pDebugInfo)
weidendoa17f2a32006-03-20 10:27:30 +0000431{
florianf4384f42014-12-16 20:55:58 +0000432 Bool found_file_line, found_fn, result = True;
weidendoa17f2a32006-03-20 10:27:30 +0000433 UInt line;
434
barta0b6b2c2008-07-07 06:49:24 +0000435 CLG_DEBUG(6, " + get_debug_info(%#lx)\n", instr_addr);
weidendoa17f2a32006-03-20 10:27:30 +0000436
sewardjb8b79ad2008-03-03 01:35:41 +0000437 if (pDebugInfo) {
sewardje3f1e592009-07-31 09:41:29 +0000438 *pDebugInfo = VG_(find_DebugInfo)(instr_addr);
weidendoa17f2a32006-03-20 10:27:30 +0000439
440 // for generated code in anonymous space, pSegInfo is 0
441 }
442
weidendo3db43222007-09-17 12:52:10 +0000443 found_file_line = VG_(get_filename_linenum)(instr_addr,
florian10ef7252014-10-27 12:06:35 +0000444 file,
445 dir,
weidendo3db43222007-09-17 12:52:10 +0000446 &line);
florian46cc0452014-10-25 19:20:38 +0000447 found_fn = VG_(get_fnname)(instr_addr, fn_name);
weidendoa17f2a32006-03-20 10:27:30 +0000448
weidendo3db43222007-09-17 12:52:10 +0000449 if (!found_file_line && !found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000450 CLG_(stat).no_debug_BBs++;
florian10ef7252014-10-27 12:06:35 +0000451 *file = "???";
florian46cc0452014-10-25 19:20:38 +0000452 *fn_name = "???";
weidendoa17f2a32006-03-20 10:27:30 +0000453 if (line_num) *line_num=0;
454 result = False;
455
weidendo3db43222007-09-17 12:52:10 +0000456 } else if ( found_file_line && found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000457 CLG_(stat).full_debug_BBs++;
458 if (line_num) *line_num=line;
459
weidendo3db43222007-09-17 12:52:10 +0000460 } else if ( found_file_line && !found_fn) {
weidendoa17f2a32006-03-20 10:27:30 +0000461 CLG_(stat).file_line_debug_BBs++;
florian46cc0452014-10-25 19:20:38 +0000462 *fn_name = "???";
weidendoa17f2a32006-03-20 10:27:30 +0000463 if (line_num) *line_num=line;
464
weidendo3db43222007-09-17 12:52:10 +0000465 } else /*(!found_file_line && found_fn)*/ {
weidendoa17f2a32006-03-20 10:27:30 +0000466 CLG_(stat).fn_name_debug_BBs++;
florian10ef7252014-10-27 12:06:35 +0000467 *file = "???";
weidendoa17f2a32006-03-20 10:27:30 +0000468 if (line_num) *line_num=0;
469 }
470
barta0b6b2c2008-07-07 06:49:24 +0000471 CLG_DEBUG(6, " - get_debug_info(%#lx): seg '%s', fn %s\n",
weidendoa17f2a32006-03-20 10:27:30 +0000472 instr_addr,
florian19f91bb2012-11-10 22:29:54 +0000473 !pDebugInfo ? "-" :
sewardje3f1e592009-07-31 09:41:29 +0000474 (*pDebugInfo) ? VG_(DebugInfo_get_filename)(*pDebugInfo) :
florian19f91bb2012-11-10 22:29:54 +0000475 "(None)",
florian46cc0452014-10-25 19:20:38 +0000476 *fn_name);
weidendoa17f2a32006-03-20 10:27:30 +0000477
478 return result;
479}
480
481/* for _libc_freeres_wrapper => _exit renaming */
482static BB* exit_bb = 0;
483
484
485/*
486 * Attach function struct to a BB from debug info.
487 */
488fn_node* CLG_(get_fn_node)(BB* bb)
489{
florian10ef7252014-10-27 12:06:35 +0000490 const HChar *fnname, *filename, *dirname;
sewardjb8b79ad2008-03-03 01:35:41 +0000491 DebugInfo* di;
weidendoa17f2a32006-03-20 10:27:30 +0000492 UInt line_num;
493 fn_node* fn;
494
495 /* fn from debug info is idempotent for a BB */
496 if (bb->fn) return bb->fn;
497
barta0b6b2c2008-07-07 06:49:24 +0000498 CLG_DEBUG(3,"+ get_fn_node(BB %#lx)\n", bb_addr(bb));
weidendoa17f2a32006-03-20 10:27:30 +0000499
500 /* get function/file name, line number and object of
501 * the BB according to debug information
502 */
503 CLG_(get_debug_info)(bb_addr(bb),
florian10ef7252014-10-27 12:06:35 +0000504 &dirname, &filename, &fnname, &line_num, &di);
weidendoa17f2a32006-03-20 10:27:30 +0000505
506 if (0 == VG_(strcmp)(fnname, "???")) {
507 int p;
florian46cc0452014-10-25 19:20:38 +0000508 static HChar buf[32]; // for sure large enough
weidendoa17f2a32006-03-20 10:27:30 +0000509 /* Use address as found in library */
510 if (sizeof(Addr) == 4)
florianb7876db2015-08-05 19:04:51 +0000511 p = VG_(sprintf)(buf, "%#08lx", (UWord)bb->offset);
weidendoa17f2a32006-03-20 10:27:30 +0000512 else
513 // 64bit address
florianb7876db2015-08-05 19:04:51 +0000514 p = VG_(sprintf)(buf, "%#016lx", (UWord)bb->offset);
weidendoa17f2a32006-03-20 10:27:30 +0000515
florian46cc0452014-10-25 19:20:38 +0000516 VG_(sprintf)(buf + p, "%s",
weidendoa17f2a32006-03-20 10:27:30 +0000517 (bb->sect_kind == Vg_SectData) ? " [Data]" :
518 (bb->sect_kind == Vg_SectBSS) ? " [BSS]" :
519 (bb->sect_kind == Vg_SectGOT) ? " [GOT]" :
520 (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : "");
florian46cc0452014-10-25 19:20:38 +0000521 fnname = buf;
weidendoa17f2a32006-03-20 10:27:30 +0000522 }
523 else {
florian46cc0452014-10-25 19:20:38 +0000524 if (VG_(get_fnname_if_entry)(bb_addr(bb), &fnname))
weidendoa17f2a32006-03-20 10:27:30 +0000525 bb->is_entry = 1;
526 }
527
528 /* HACK for correct _exit:
529 * _exit is redirected to VG_(__libc_freeres_wrapper) by valgrind,
530 * so we rename it back again :-)
531 */
532 if (0 == VG_(strcmp)(fnname, "vgPlain___libc_freeres_wrapper")
533 && exit_bb) {
534 CLG_(get_debug_info)(bb_addr(exit_bb),
florian10ef7252014-10-27 12:06:35 +0000535 &dirname, &filename, &fnname, &line_num, &di);
weidendoa17f2a32006-03-20 10:27:30 +0000536
537 CLG_DEBUG(1, "__libc_freeres_wrapper renamed to _exit\n");
538 }
539 if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
540 exit_bb = bb;
541
542 if (runtime_resolve_addr &&
543 (bb_addr(bb) >= runtime_resolve_addr) &&
544 (bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) {
545 /* BB in runtime_resolve found by code check; use this name */
florian46cc0452014-10-25 19:20:38 +0000546 fnname = "_dl_runtime_resolve";
weidendoa17f2a32006-03-20 10:27:30 +0000547 }
548
549 /* get fn_node struct for this function */
florian536e3d72014-10-26 19:16:14 +0000550 fn = get_fn_node_inseg( di, dirname, filename, fnname);
weidendoa17f2a32006-03-20 10:27:30 +0000551
552 /* if this is the 1st time the function is seen,
553 * some attributes are set */
554 if (fn->pure_cxt == 0) {
555
556 /* Every function gets a "pure" context, i.e. a context with stack
557 * depth 1 only with this function. This is for compression of mangled
558 * names
559 */
560 fn_node* pure[2];
561 pure[0] = 0;
562 pure[1] = fn;
563 fn->pure_cxt = CLG_(get_cxt)(pure+1);
564
565 if (bb->sect_kind == Vg_SectPLT)
566 fn->skip = CLG_(clo).skip_plt;
567
568 if (VG_(strcmp)(fn->name, "_dl_runtime_resolve")==0) {
569 fn->pop_on_jump = True;
570
571 if (VG_(clo_verbosity) > 1)
sewardj0f33adf2009-07-15 14:51:03 +0000572 VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve:"
573 " %s +%#lx=%#lx\n",
weidendoa17f2a32006-03-20 10:27:30 +0000574 bb->obj->name + bb->obj->last_slash_pos,
florianb7876db2015-08-05 19:04:51 +0000575 (UWord)bb->offset, bb_addr(bb));
weidendoa17f2a32006-03-20 10:27:30 +0000576 }
577
578 fn->is_malloc = (VG_(strcmp)(fn->name, "malloc")==0);
579 fn->is_realloc = (VG_(strcmp)(fn->name, "realloc")==0);
580 fn->is_free = (VG_(strcmp)(fn->name, "free")==0);
581
582 /* apply config options from function name patterns
583 * given on command line */
584 CLG_(update_fn_config)(fn);
585 }
586
587
588 bb->fn = fn;
589 bb->line = line_num;
590
florian536e3d72014-10-26 19:16:14 +0000591 if (dirname[0]) {
592 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s:%u)\n",
593 bb_addr(bb), fnname, filename, line_num);
594 } else
595 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s/%s:%u)\n",
596 bb_addr(bb), fnname, dirname, filename, line_num);
weidendoa17f2a32006-03-20 10:27:30 +0000597
598 return fn;
599}
600
601
602/*------------------------------------------------------------*/
603/*--- Active function array operations ---*/
604/*------------------------------------------------------------*/
605
606/* The active function array is a thread-specific array
607 * of UInts, mapping function numbers to the active count of
608 * functions.
609 * The active count is the number of times a function appears
610 * in the current call stack, and is used when costs for recursion
611 * levels should be separated.
612 */
613
614UInt* CLG_(get_fn_entry)(Int n)
615{
616 CLG_ASSERT(n < current_fn_active.size);
617 return current_fn_active.array + n;
618}
619
620void CLG_(init_fn_array)(fn_array* a)
621{
622 Int i;
623
624 CLG_ASSERT(a != 0);
625
626 a->size = N_INITIAL_FN_ARRAY_SIZE;
627 if (a->size <= CLG_(stat).distinct_fns)
628 a->size = CLG_(stat).distinct_fns+1;
629
sewardj9c606bd2008-09-18 18:12:50 +0000630 a->array = (UInt*) CLG_MALLOC("cl.fn.gfe.1",
631 a->size * sizeof(UInt));
weidendoa17f2a32006-03-20 10:27:30 +0000632 for(i=0;i<a->size;i++)
633 a->array[i] = 0;
634}
635
636void CLG_(copy_current_fn_array)(fn_array* dst)
637{
638 CLG_ASSERT(dst != 0);
639
640 dst->size = current_fn_active.size;
641 dst->array = current_fn_active.array;
642}
643
644fn_array* CLG_(get_current_fn_array)()
645{
646 return &current_fn_active;
647}
648
649void CLG_(set_current_fn_array)(fn_array* a)
650{
651 CLG_ASSERT(a != 0);
652
653 current_fn_active.size = a->size;
654 current_fn_active.array = a->array;
655 if (current_fn_active.size <= CLG_(stat).distinct_fns)
656 resize_fn_array();
657}
658
659/* ensure that active_array is big enough:
660 * <distinct_fns> is the highest index, so <fn_active_array_size>
661 * has to be bigger than that.
662 */
663static void resize_fn_array(void)
664{
weidendo0b23d6e2009-06-15 00:16:32 +0000665 UInt* new_array;
florianb7876db2015-08-05 19:04:51 +0000666 Int i;
weidendoa17f2a32006-03-20 10:27:30 +0000667
florianb7876db2015-08-05 19:04:51 +0000668 UInt newsize = current_fn_active.size;
weidendoa17f2a32006-03-20 10:27:30 +0000669 while (newsize <= CLG_(stat).distinct_fns) newsize *=2;
670
florianb7876db2015-08-05 19:04:51 +0000671 CLG_DEBUG(0, "Resize fn_active_array: %u => %u\n",
weidendoa17f2a32006-03-20 10:27:30 +0000672 current_fn_active.size, newsize);
673
weidendo0b23d6e2009-06-15 00:16:32 +0000674 new_array = (UInt*) CLG_MALLOC("cl.fn.rfa.1", newsize * sizeof(UInt));
weidendoa17f2a32006-03-20 10:27:30 +0000675 for(i=0;i<current_fn_active.size;i++)
weidendo0b23d6e2009-06-15 00:16:32 +0000676 new_array[i] = current_fn_active.array[i];
weidendoa17f2a32006-03-20 10:27:30 +0000677 while(i<newsize)
weidendo0b23d6e2009-06-15 00:16:32 +0000678 new_array[i++] = 0;
weidendoa17f2a32006-03-20 10:27:30 +0000679
680 VG_(free)(current_fn_active.array);
681 current_fn_active.size = newsize;
weidendo0b23d6e2009-06-15 00:16:32 +0000682 current_fn_active.array = new_array;
weidendoa17f2a32006-03-20 10:27:30 +0000683 CLG_(stat).fn_array_resizes++;
684}
685
686