weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Callgrind ---*/ |
| 3 | /*--- ct_fn.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Callgrind, a Valgrind tool for call tracing. |
| 8 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 9 | Copyright (C) 2002-2008, Josef Weidendorfer (Josef.Weidendorfer@gmx.de) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 10 | |
| 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 | |
| 33 | static fn_array current_fn_active; |
| 34 | |
| 35 | static Addr runtime_resolve_addr = 0; |
| 36 | static int runtime_resolve_length = 0; |
| 37 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 38 | // a code pattern is a list of tuples (start offset, length) |
| 39 | struct chunk_t { int start, len; }; |
| 40 | struct pattern |
| 41 | { |
| 42 | const char* name; |
| 43 | 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 | */ |
| 50 | static Bool check_code(obj_node* obj, |
| 51 | unsigned char code[], struct pattern* pat) |
| 52 | { |
| 53 | Bool found; |
| 54 | Addr addr, end; |
| 55 | int chunk, start, len; |
| 56 | |
| 57 | /* first chunk of pattern should always start at offset 0 and |
| 58 | * have at least 3 bytes */ |
| 59 | CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2)); |
| 60 | |
| 61 | CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n", |
| 62 | obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]); |
| 63 | |
| 64 | end = obj->start + obj->size - pat->len; |
| 65 | addr = obj->start; |
| 66 | while(addr < end) { |
| 67 | found = (VG_(memcmp)( (void*)addr, code, pat->chunk[0].len) == 0); |
| 68 | |
| 69 | if (found) { |
| 70 | chunk = 1; |
| 71 | while(1) { |
| 72 | start = pat->chunk[chunk].start; |
| 73 | len = pat->chunk[chunk].len; |
| 74 | if (len == 0) break; |
| 75 | |
| 76 | CLG_ASSERT(len >2); |
| 77 | CLG_DEBUG(1, " found chunk %d at %#lx, checking %d bytes of [%x %x %x...]\n", |
| 78 | chunk-1, addr - obj->start, len, |
| 79 | code[start], code[start+1], code[start+2]); |
| 80 | |
| 81 | if (VG_(memcmp)( (void*)(addr+start), code+start, len) != 0) { |
| 82 | found = False; |
| 83 | break; |
| 84 | } |
| 85 | chunk++; |
| 86 | } |
| 87 | |
| 88 | if (found) { |
| 89 | CLG_DEBUG(1, "found at offset %#lx.\n", addr - obj->start); |
| 90 | if (VG_(clo_verbosity) > 1) |
| 91 | VG_(message)(Vg_DebugMsg, "Found runtime_resolve (%s): %s +%#lx=%#lx, length %d", |
| 92 | pat->name, obj->name + obj->last_slash_pos, |
| 93 | addr - obj->start, addr, pat->len); |
| 94 | |
| 95 | runtime_resolve_addr = addr; |
| 96 | runtime_resolve_length = pat->len; |
| 97 | return True; |
| 98 | } |
| 99 | } |
| 100 | addr++; |
| 101 | } |
| 102 | CLG_DEBUG(1, " found nothing.\n"); |
| 103 | return False; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* _ld_runtime_resolve, located in ld.so, needs special handling: |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 108 | * The jump at end into the resolved function should not be |
| 109 | * represented as a call (as usually done in callgrind with jumps), |
| 110 | * but as a return + call. Otherwise, the repeated existance of |
| 111 | * _ld_runtime_resolve in call chains will lead to huge cycles, |
| 112 | * making the profile almost worthless. |
| 113 | * |
| 114 | * If ld.so is stripped, the symbol will not appear. But as this |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 115 | * function is handcrafted assembler, we search for it. |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 116 | * |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 117 | * We stop if the ELF object name does not seem to be the runtime linker |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 118 | */ |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 119 | static Bool search_runtime_resolve(obj_node* obj) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 120 | { |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 121 | #if defined(VGP_x86_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 122 | static unsigned char code[] = { |
| 123 | /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b, |
| 124 | /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00, |
| 125 | /*16*/ 0x5a, 0x59, 0x87, 0x04, 0x24, 0xc2, 0x08, 0x00 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 126 | /* Check ranges [0-11] and [16-23] ([12-15] is an absolute address) */ |
| 127 | static struct pattern pat = { |
| 128 | "x86-def", 24, {{ 0,12 }, { 16,8 }, { 24,0}} }; |
| 129 | |
| 130 | /* Pattern for glibc-2.8 on OpenSuse11.0 */ |
| 131 | static unsigned char code_28[] = { |
| 132 | /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b, |
| 133 | /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00, |
| 134 | /*16*/ 0x5a, 0x8b, 0x0c, 0x24, 0x89, 0x04, 0x24, 0x8b, |
| 135 | /*24*/ 0x44, 0x24, 0x04, 0xc2, 0x0c, 0x00 }; |
| 136 | static struct pattern pat_28 = { |
| 137 | "x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} }; |
| 138 | |
| 139 | if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False; |
| 140 | if (check_code(obj, code, &pat)) return True; |
| 141 | if (check_code(obj, code_28, &pat_28)) return True; |
| 142 | return False; |
| 143 | #endif |
| 144 | |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 145 | #if defined(VGP_ppc32_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 146 | static unsigned char code[] = { |
| 147 | /* 0*/ 0x94, 0x21, 0xff, 0xc0, 0x90, 0x01, 0x00, 0x0c, |
| 148 | /* 8*/ 0x90, 0x61, 0x00, 0x10, 0x90, 0x81, 0x00, 0x14, |
| 149 | /*16*/ 0x7d, 0x83, 0x63, 0x78, 0x90, 0xa1, 0x00, 0x18, |
| 150 | /*24*/ 0x7d, 0x64, 0x5b, 0x78, 0x90, 0xc1, 0x00, 0x1c, |
| 151 | /*32*/ 0x7c, 0x08, 0x02, 0xa6, 0x90, 0xe1, 0x00, 0x20, |
| 152 | /*40*/ 0x90, 0x01, 0x00, 0x30, 0x91, 0x01, 0x00, 0x24, |
| 153 | /*48*/ 0x7c, 0x00, 0x00, 0x26, 0x91, 0x21, 0x00, 0x28, |
| 154 | /*56*/ 0x91, 0x41, 0x00, 0x2c, 0x90, 0x01, 0x00, 0x08, |
| 155 | /*64*/ 0x48, 0x00, 0x02, 0x91, 0x7c, 0x69, 0x03, 0xa6, /* at 64: bl aff0 <fixup> */ |
| 156 | /*72*/ 0x80, 0x01, 0x00, 0x30, 0x81, 0x41, 0x00, 0x2c, |
| 157 | /*80*/ 0x81, 0x21, 0x00, 0x28, 0x7c, 0x08, 0x03, 0xa6, |
| 158 | /*88*/ 0x81, 0x01, 0x00, 0x24, 0x80, 0x01, 0x00, 0x08, |
| 159 | /*96*/ 0x80, 0xe1, 0x00, 0x20, 0x80, 0xc1, 0x00, 0x1c, |
| 160 | /*104*/0x7c, 0x0f, 0xf1, 0x20, 0x80, 0xa1, 0x00, 0x18, |
| 161 | /*112*/0x80, 0x81, 0x00, 0x14, 0x80, 0x61, 0x00, 0x10, |
| 162 | /*120*/0x80, 0x01, 0x00, 0x0c, 0x38, 0x21, 0x00, 0x40, |
| 163 | /*128*/0x4e, 0x80, 0x04, 0x20 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 164 | static struct pattern pat = { |
| 165 | "ppc32-def", 132, {{ 0,65 }, { 68,64 }, { 132,0 }} }; |
| 166 | |
| 167 | if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False; |
| 168 | return check_code(obj, code, &pat); |
| 169 | #endif |
| 170 | |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 171 | #if defined(VGP_amd64_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 172 | static unsigned char code[] = { |
| 173 | /* 0*/ 0x48, 0x83, 0xec, 0x38, 0x48, 0x89, 0x04, 0x24, |
| 174 | /* 8*/ 0x48, 0x89, 0x4c, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x10, |
| 175 | /*18*/ 0x48, 0x89, 0x74, 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, |
| 176 | /*28*/ 0x4c, 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24, 0x30, |
| 177 | /*38*/ 0x48, 0x8b, 0x74, 0x24, 0x40, 0x49, 0x89, 0xf3, |
| 178 | /*46*/ 0x4c, 0x01, 0xde, 0x4c, 0x01, 0xde, 0x48, 0xc1, 0xe6, 0x03, |
| 179 | /*56*/ 0x48, 0x8b, 0x7c, 0x24, 0x38, 0xe8, 0xee, 0x01, 0x00, 0x00, |
| 180 | /*66*/ 0x49, 0x89, 0xc3, 0x4c, 0x8b, 0x4c, 0x24, 0x30, |
| 181 | /*74*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20, |
| 182 | /*84*/ 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54, 0x24, 0x10, |
| 183 | /*94*/ 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48, 0x8b, 0x04, 0x24, |
| 184 | /*103*/0x48, 0x83, 0xc4, 0x48, 0x41, 0xff, 0xe3 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 185 | static struct pattern pat = { |
| 186 | "amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} }; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 187 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 188 | if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) && |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 189 | (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False; |
| 190 | return check_code(obj, code, &pat); |
| 191 | #endif |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 192 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 193 | /* For other platforms, no patterns known */ |
| 194 | return False; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 195 | } |
| 196 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 197 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 198 | /*------------------------------------------------------------*/ |
| 199 | /*--- Object/File/Function hash entry operations ---*/ |
| 200 | /*------------------------------------------------------------*/ |
| 201 | |
| 202 | /* Object hash table, fixed */ |
| 203 | static obj_node* obj_table[N_OBJ_ENTRIES]; |
| 204 | |
| 205 | void CLG_(init_obj_table)() |
| 206 | { |
| 207 | Int i; |
| 208 | for (i = 0; i < N_OBJ_ENTRIES; i++) |
| 209 | obj_table[i] = 0; |
| 210 | } |
| 211 | |
| 212 | #define HASH_CONSTANT 256 |
| 213 | |
| 214 | static UInt str_hash(const Char *s, UInt table_size) |
| 215 | { |
| 216 | int hash_value = 0; |
| 217 | for ( ; *s; s++) |
| 218 | hash_value = (HASH_CONSTANT * hash_value + *s) % table_size; |
| 219 | return hash_value; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | static Char* anonymous_obj = "???"; |
| 224 | |
| 225 | static __inline__ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 226 | obj_node* new_obj_node(DebugInfo* di, obj_node* next) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 227 | { |
| 228 | Int i; |
| 229 | obj_node* new; |
| 230 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 231 | new = (obj_node*) CLG_MALLOC("cl.fn.non.1", sizeof(obj_node)); |
| 232 | new->name = di ? VG_(strdup)( "cl.fn.non.2",VG_(seginfo_filename)(di) ) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 233 | : anonymous_obj; |
| 234 | for (i = 0; i < N_FILE_ENTRIES; i++) { |
| 235 | new->files[i] = NULL; |
| 236 | } |
| 237 | CLG_(stat).distinct_objs ++; |
| 238 | new->number = CLG_(stat).distinct_objs; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 239 | /* JRS 2008 Feb 19: maybe rename .start/.size/.offset to |
| 240 | .text_avma/.text_size/.test_bias to make it clearer what these |
| 241 | fields really mean */ |
| 242 | new->start = di ? VG_(seginfo_get_text_avma)(di) : 0; |
| 243 | new->size = di ? VG_(seginfo_get_text_size)(di) : 0; |
| 244 | new->offset = di ? VG_(seginfo_get_text_bias)(di) : 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 245 | new->next = next; |
| 246 | |
| 247 | // not only used for debug output (see static.c) |
| 248 | new->last_slash_pos = 0; |
| 249 | i = 0; |
| 250 | while(new->name[i]) { |
| 251 | if (new->name[i]=='/') new->last_slash_pos = i+1; |
| 252 | i++; |
| 253 | } |
| 254 | |
| 255 | if (runtime_resolve_addr == 0) search_runtime_resolve(new); |
| 256 | |
| 257 | return new; |
| 258 | } |
| 259 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 260 | obj_node* CLG_(get_obj_node)(DebugInfo* di) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 261 | { |
| 262 | obj_node* curr_obj_node; |
| 263 | UInt objname_hash; |
| 264 | const UChar* obj_name; |
| 265 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 266 | obj_name = di ? (Char*) VG_(seginfo_filename)(di) : anonymous_obj; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 267 | |
| 268 | /* lookup in obj hash */ |
| 269 | objname_hash = str_hash(obj_name, N_OBJ_ENTRIES); |
| 270 | curr_obj_node = obj_table[objname_hash]; |
| 271 | while (NULL != curr_obj_node && |
| 272 | VG_(strcmp)(obj_name, curr_obj_node->name) != 0) { |
| 273 | curr_obj_node = curr_obj_node->next; |
| 274 | } |
| 275 | if (NULL == curr_obj_node) { |
| 276 | obj_table[objname_hash] = curr_obj_node = |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 277 | new_obj_node(di, obj_table[objname_hash]); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return curr_obj_node; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static __inline__ |
| 285 | file_node* new_file_node(Char filename[FILENAME_LEN], |
| 286 | obj_node* obj, file_node* next) |
| 287 | { |
| 288 | Int i; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 289 | file_node* new = (file_node*) CLG_MALLOC("cl.fn.nfn.1", |
| 290 | sizeof(file_node)); |
| 291 | new->name = VG_(strdup)("cl.fn.nfn.2", filename); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 292 | for (i = 0; i < N_FN_ENTRIES; i++) { |
| 293 | new->fns[i] = NULL; |
| 294 | } |
| 295 | CLG_(stat).distinct_files++; |
| 296 | new->number = CLG_(stat).distinct_files; |
| 297 | new->obj = obj; |
| 298 | new->next = next; |
| 299 | return new; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | file_node* CLG_(get_file_node)(obj_node* curr_obj_node, |
| 304 | Char filename[FILENAME_LEN]) |
| 305 | { |
| 306 | file_node* curr_file_node; |
| 307 | UInt filename_hash; |
| 308 | |
| 309 | /* lookup in file hash */ |
| 310 | filename_hash = str_hash(filename, N_FILE_ENTRIES); |
| 311 | curr_file_node = curr_obj_node->files[filename_hash]; |
| 312 | while (NULL != curr_file_node && |
| 313 | VG_(strcmp)(filename, curr_file_node->name) != 0) { |
| 314 | curr_file_node = curr_file_node->next; |
| 315 | } |
| 316 | if (NULL == curr_file_node) { |
| 317 | curr_obj_node->files[filename_hash] = curr_file_node = |
| 318 | new_file_node(filename, curr_obj_node, |
| 319 | curr_obj_node->files[filename_hash]); |
| 320 | } |
| 321 | |
| 322 | return curr_file_node; |
| 323 | } |
| 324 | |
| 325 | /* forward decl. */ |
| 326 | static void resize_fn_array(void); |
| 327 | |
| 328 | static __inline__ |
| 329 | fn_node* new_fn_node(Char fnname[FILENAME_LEN], |
| 330 | file_node* file, fn_node* next) |
| 331 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 332 | fn_node* new = (fn_node*) CLG_MALLOC("cl.fn.nfnnd.1", |
| 333 | sizeof(fn_node)); |
| 334 | new->name = VG_(strdup)("cl.fn.nfnnd.2", fnname); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 335 | |
| 336 | CLG_(stat).distinct_fns++; |
| 337 | new->number = CLG_(stat).distinct_fns; |
| 338 | new->last_cxt = 0; |
| 339 | new->pure_cxt = 0; |
| 340 | new->file = file; |
| 341 | new->next = next; |
| 342 | |
| 343 | new->dump_before = False; |
| 344 | new->dump_after = False; |
| 345 | new->zero_before = False; |
| 346 | new->toggle_collect = False; |
| 347 | new->skip = False; |
weidendo | a762b0f | 2006-05-01 00:55:54 +0000 | [diff] [blame] | 348 | new->pop_on_jump = CLG_(clo).pop_on_jump; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 349 | new->is_malloc = False; |
| 350 | new->is_realloc = False; |
| 351 | new->is_free = False; |
| 352 | |
| 353 | new->group = 0; |
| 354 | new->separate_callers = CLG_(clo).separate_callers; |
| 355 | new->separate_recursions = CLG_(clo).separate_recursions; |
| 356 | |
| 357 | #if CLG_ENABLE_DEBUG |
| 358 | new->verbosity = -1; |
| 359 | #endif |
| 360 | |
| 361 | if (CLG_(stat).distinct_fns >= current_fn_active.size) |
| 362 | resize_fn_array(); |
| 363 | |
| 364 | return new; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* Get a function node in hash2 with known file node. |
| 369 | * hash nodes are created if needed |
| 370 | */ |
| 371 | static |
| 372 | fn_node* get_fn_node_infile(file_node* curr_file_node, |
| 373 | Char fnname[FN_NAME_LEN]) |
| 374 | { |
| 375 | fn_node* curr_fn_node; |
| 376 | UInt fnname_hash; |
| 377 | |
| 378 | CLG_ASSERT(curr_file_node != 0); |
| 379 | |
| 380 | /* lookup in function hash */ |
| 381 | fnname_hash = str_hash(fnname, N_FN_ENTRIES); |
| 382 | curr_fn_node = curr_file_node->fns[fnname_hash]; |
| 383 | while (NULL != curr_fn_node && |
| 384 | VG_(strcmp)(fnname, curr_fn_node->name) != 0) { |
| 385 | curr_fn_node = curr_fn_node->next; |
| 386 | } |
| 387 | if (NULL == curr_fn_node) { |
| 388 | curr_file_node->fns[fnname_hash] = curr_fn_node = |
| 389 | new_fn_node(fnname, curr_file_node, |
| 390 | curr_file_node->fns[fnname_hash]); |
| 391 | } |
| 392 | |
| 393 | return curr_fn_node; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* Get a function node in a Segment. |
| 398 | * Hash nodes are created if needed. |
| 399 | */ |
| 400 | static __inline__ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 401 | fn_node* get_fn_node_inseg(DebugInfo* di, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 402 | Char filename[FILENAME_LEN], |
| 403 | Char fnname[FN_NAME_LEN]) |
| 404 | { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 405 | obj_node *obj = CLG_(get_obj_node)(di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 406 | file_node *file = CLG_(get_file_node)(obj, filename); |
| 407 | fn_node *fn = get_fn_node_infile(file, fnname); |
| 408 | |
| 409 | return fn; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | Bool CLG_(get_debug_info)(Addr instr_addr, |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 414 | Char file[FILENAME_LEN], |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 415 | Char fn_name[FN_NAME_LEN], UInt* line_num, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 416 | DebugInfo** pDebugInfo) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 417 | { |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 418 | Bool found_file_line, found_fn, found_dirname, result = True; |
| 419 | Char dir[FILENAME_LEN]; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 420 | UInt line; |
| 421 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 422 | CLG_DEBUG(6, " + get_debug_info(%#lx)\n", instr_addr); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 423 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 424 | if (pDebugInfo) { |
| 425 | *pDebugInfo = VG_(find_seginfo)(instr_addr); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 426 | |
| 427 | // for generated code in anonymous space, pSegInfo is 0 |
| 428 | } |
| 429 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 430 | found_file_line = VG_(get_filename_linenum)(instr_addr, |
| 431 | file, FILENAME_LEN, |
| 432 | dir, FILENAME_LEN, |
| 433 | &found_dirname, |
| 434 | &line); |
| 435 | found_fn = VG_(get_fnname)(instr_addr, |
| 436 | fn_name, FN_NAME_LEN); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 437 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 438 | if (found_dirname) { |
| 439 | // +1 for the '/'. |
| 440 | CLG_ASSERT(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILENAME_LEN); |
| 441 | VG_(strcat)(dir, "/"); // Append '/' |
| 442 | VG_(strcat)(dir, file); // Append file to dir |
| 443 | VG_(strcpy)(file, dir); // Move dir+file to file |
| 444 | } |
| 445 | |
| 446 | if (!found_file_line && !found_fn) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 447 | CLG_(stat).no_debug_BBs++; |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 448 | VG_(strcpy)(file, "???"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 449 | VG_(strcpy)(fn_name, "???"); |
| 450 | if (line_num) *line_num=0; |
| 451 | result = False; |
| 452 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 453 | } else if ( found_file_line && found_fn) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 454 | CLG_(stat).full_debug_BBs++; |
| 455 | if (line_num) *line_num=line; |
| 456 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 457 | } else if ( found_file_line && !found_fn) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 458 | CLG_(stat).file_line_debug_BBs++; |
| 459 | VG_(strcpy)(fn_name, "???"); |
| 460 | if (line_num) *line_num=line; |
| 461 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 462 | } else /*(!found_file_line && found_fn)*/ { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 463 | CLG_(stat).fn_name_debug_BBs++; |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 464 | VG_(strcpy)(file, "???"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 465 | if (line_num) *line_num=0; |
| 466 | } |
| 467 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 468 | CLG_DEBUG(6, " - get_debug_info(%#lx): seg '%s', fn %s\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 469 | instr_addr, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 470 | !pDebugInfo ? (const UChar*)"-" : |
| 471 | (*pDebugInfo) ? VG_(seginfo_filename)(*pDebugInfo) : |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 472 | (const UChar*)"(None)", |
| 473 | fn_name); |
| 474 | |
| 475 | return result; |
| 476 | } |
| 477 | |
| 478 | /* for _libc_freeres_wrapper => _exit renaming */ |
| 479 | static BB* exit_bb = 0; |
| 480 | |
| 481 | |
| 482 | /* |
| 483 | * Attach function struct to a BB from debug info. |
| 484 | */ |
| 485 | fn_node* CLG_(get_fn_node)(BB* bb) |
| 486 | { |
| 487 | Char filename[FILENAME_LEN], fnname[FN_NAME_LEN]; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 488 | DebugInfo* di; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 489 | UInt line_num; |
| 490 | fn_node* fn; |
| 491 | |
| 492 | /* fn from debug info is idempotent for a BB */ |
| 493 | if (bb->fn) return bb->fn; |
| 494 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 495 | CLG_DEBUG(3,"+ get_fn_node(BB %#lx)\n", bb_addr(bb)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 496 | |
| 497 | /* get function/file name, line number and object of |
| 498 | * the BB according to debug information |
| 499 | */ |
| 500 | CLG_(get_debug_info)(bb_addr(bb), |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 501 | filename, fnname, &line_num, &di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 502 | |
| 503 | if (0 == VG_(strcmp)(fnname, "???")) { |
| 504 | int p; |
| 505 | |
| 506 | /* Use address as found in library */ |
| 507 | if (sizeof(Addr) == 4) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 508 | p = VG_(sprintf)(fnname, "%#08lx", bb->offset); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 509 | else |
| 510 | // 64bit address |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 511 | p = VG_(sprintf)(fnname, "%#016lx", bb->offset); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 512 | |
| 513 | VG_(sprintf)(fnname+p, "%s", |
| 514 | (bb->sect_kind == Vg_SectData) ? " [Data]" : |
| 515 | (bb->sect_kind == Vg_SectBSS) ? " [BSS]" : |
| 516 | (bb->sect_kind == Vg_SectGOT) ? " [GOT]" : |
| 517 | (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : ""); |
| 518 | } |
| 519 | else { |
| 520 | if (VG_(get_fnname_if_entry)(bb_addr(bb), fnname, FN_NAME_LEN)) |
| 521 | bb->is_entry = 1; |
| 522 | } |
| 523 | |
| 524 | /* HACK for correct _exit: |
| 525 | * _exit is redirected to VG_(__libc_freeres_wrapper) by valgrind, |
| 526 | * so we rename it back again :-) |
| 527 | */ |
| 528 | if (0 == VG_(strcmp)(fnname, "vgPlain___libc_freeres_wrapper") |
| 529 | && exit_bb) { |
| 530 | CLG_(get_debug_info)(bb_addr(exit_bb), |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 531 | filename, fnname, &line_num, &di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 532 | |
| 533 | CLG_DEBUG(1, "__libc_freeres_wrapper renamed to _exit\n"); |
| 534 | } |
| 535 | if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb) |
| 536 | exit_bb = bb; |
| 537 | |
| 538 | if (runtime_resolve_addr && |
| 539 | (bb_addr(bb) >= runtime_resolve_addr) && |
| 540 | (bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) { |
| 541 | /* BB in runtime_resolve found by code check; use this name */ |
| 542 | VG_(sprintf)(fnname, "_dl_runtime_resolve"); |
| 543 | } |
| 544 | |
| 545 | /* get fn_node struct for this function */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 546 | fn = get_fn_node_inseg( di, filename, fnname); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 547 | |
| 548 | /* if this is the 1st time the function is seen, |
| 549 | * some attributes are set */ |
| 550 | if (fn->pure_cxt == 0) { |
| 551 | |
| 552 | /* Every function gets a "pure" context, i.e. a context with stack |
| 553 | * depth 1 only with this function. This is for compression of mangled |
| 554 | * names |
| 555 | */ |
| 556 | fn_node* pure[2]; |
| 557 | pure[0] = 0; |
| 558 | pure[1] = fn; |
| 559 | fn->pure_cxt = CLG_(get_cxt)(pure+1); |
| 560 | |
| 561 | if (bb->sect_kind == Vg_SectPLT) |
| 562 | fn->skip = CLG_(clo).skip_plt; |
| 563 | |
| 564 | if (VG_(strcmp)(fn->name, "_dl_runtime_resolve")==0) { |
| 565 | fn->pop_on_jump = True; |
| 566 | |
| 567 | if (VG_(clo_verbosity) > 1) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 568 | VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve: %s +%#lx=%#lx", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 569 | bb->obj->name + bb->obj->last_slash_pos, |
| 570 | bb->offset, bb_addr(bb)); |
| 571 | } |
| 572 | |
| 573 | fn->is_malloc = (VG_(strcmp)(fn->name, "malloc")==0); |
| 574 | fn->is_realloc = (VG_(strcmp)(fn->name, "realloc")==0); |
| 575 | fn->is_free = (VG_(strcmp)(fn->name, "free")==0); |
| 576 | |
| 577 | /* apply config options from function name patterns |
| 578 | * given on command line */ |
| 579 | CLG_(update_fn_config)(fn); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | bb->fn = fn; |
| 584 | bb->line = line_num; |
| 585 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 586 | CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s:%u)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 587 | bb_addr(bb), fnname, filename, line_num); |
| 588 | |
| 589 | return fn; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /*------------------------------------------------------------*/ |
| 594 | /*--- Active function array operations ---*/ |
| 595 | /*------------------------------------------------------------*/ |
| 596 | |
| 597 | /* The active function array is a thread-specific array |
| 598 | * of UInts, mapping function numbers to the active count of |
| 599 | * functions. |
| 600 | * The active count is the number of times a function appears |
| 601 | * in the current call stack, and is used when costs for recursion |
| 602 | * levels should be separated. |
| 603 | */ |
| 604 | |
| 605 | UInt* CLG_(get_fn_entry)(Int n) |
| 606 | { |
| 607 | CLG_ASSERT(n < current_fn_active.size); |
| 608 | return current_fn_active.array + n; |
| 609 | } |
| 610 | |
| 611 | void CLG_(init_fn_array)(fn_array* a) |
| 612 | { |
| 613 | Int i; |
| 614 | |
| 615 | CLG_ASSERT(a != 0); |
| 616 | |
| 617 | a->size = N_INITIAL_FN_ARRAY_SIZE; |
| 618 | if (a->size <= CLG_(stat).distinct_fns) |
| 619 | a->size = CLG_(stat).distinct_fns+1; |
| 620 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 621 | a->array = (UInt*) CLG_MALLOC("cl.fn.gfe.1", |
| 622 | a->size * sizeof(UInt)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 623 | for(i=0;i<a->size;i++) |
| 624 | a->array[i] = 0; |
| 625 | } |
| 626 | |
| 627 | void CLG_(copy_current_fn_array)(fn_array* dst) |
| 628 | { |
| 629 | CLG_ASSERT(dst != 0); |
| 630 | |
| 631 | dst->size = current_fn_active.size; |
| 632 | dst->array = current_fn_active.array; |
| 633 | } |
| 634 | |
| 635 | fn_array* CLG_(get_current_fn_array)() |
| 636 | { |
| 637 | return ¤t_fn_active; |
| 638 | } |
| 639 | |
| 640 | void CLG_(set_current_fn_array)(fn_array* a) |
| 641 | { |
| 642 | CLG_ASSERT(a != 0); |
| 643 | |
| 644 | current_fn_active.size = a->size; |
| 645 | current_fn_active.array = a->array; |
| 646 | if (current_fn_active.size <= CLG_(stat).distinct_fns) |
| 647 | resize_fn_array(); |
| 648 | } |
| 649 | |
| 650 | /* ensure that active_array is big enough: |
| 651 | * <distinct_fns> is the highest index, so <fn_active_array_size> |
| 652 | * has to be bigger than that. |
| 653 | */ |
| 654 | static void resize_fn_array(void) |
| 655 | { |
| 656 | UInt* new; |
| 657 | Int i, newsize; |
| 658 | |
| 659 | newsize = current_fn_active.size; |
| 660 | while (newsize <= CLG_(stat).distinct_fns) newsize *=2; |
| 661 | |
| 662 | CLG_DEBUG(0, "Resize fn_active_array: %d => %d\n", |
| 663 | current_fn_active.size, newsize); |
| 664 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 665 | new = (UInt*) CLG_MALLOC("cl.fn.rfa.1", newsize * sizeof(UInt)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 666 | for(i=0;i<current_fn_active.size;i++) |
| 667 | new[i] = current_fn_active.array[i]; |
| 668 | while(i<newsize) |
| 669 | new[i++] = 0; |
| 670 | |
| 671 | VG_(free)(current_fn_active.array); |
| 672 | current_fn_active.size = newsize; |
| 673 | current_fn_active.array = new; |
| 674 | CLG_(stat).fn_array_resizes++; |
| 675 | } |
| 676 | |
| 677 | |