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 | ec062e8 | 2011-10-23 07:32:08 +0000 | [diff] [blame] | 9 | Copyright (C) 2002-2011, 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 | */ |
njn | 85f79da | 2009-05-18 05:10:00 +0000 | [diff] [blame] | 50 | __attribute__((unused)) // Possibly; depends on the platform. |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 51 | static Bool check_code(obj_node* obj, |
| 52 | unsigned char code[], struct pattern* pat) |
| 53 | { |
| 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); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 78 | CLG_DEBUG(1, " found chunk %d at %#lx, checking %d bytes " |
| 79 | "of [%x %x %x...]\n", |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 80 | 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) |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 93 | VG_(message)(Vg_DebugMsg, "Found runtime_resolve (%s): " |
| 94 | "%s +%#lx=%#lx, length %d\n", |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 95 | 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 | } |
njn | ae54ba4 | 2009-05-18 05:10:56 +0000 | [diff] [blame] | 108 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 109 | |
| 110 | /* _ld_runtime_resolve, located in ld.so, needs special handling: |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 111 | * The jump at end into the resolved function should not be |
| 112 | * represented as a call (as usually done in callgrind with jumps), |
| 113 | * but as a return + call. Otherwise, the repeated existance of |
| 114 | * _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 |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 118 | * function is handcrafted assembler, we search for it. |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 119 | * |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 120 | * 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] | 121 | */ |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 122 | static Bool search_runtime_resolve(obj_node* obj) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 123 | { |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 124 | #if defined(VGP_x86_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 125 | static unsigned char code[] = { |
| 126 | /* 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 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 129 | /* 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 */ |
| 134 | static unsigned char code_28[] = { |
| 135 | /* 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 | |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 148 | #if defined(VGP_ppc32_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 149 | static unsigned char code[] = { |
| 150 | /* 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 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 167 | 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 | |
sewardj | 8c2a6ca | 2006-05-22 00:09:51 +0000 | [diff] [blame] | 174 | #if defined(VGP_amd64_linux) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 175 | static unsigned char code[] = { |
| 176 | /* 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 }; |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 188 | static struct pattern pat = { |
| 189 | "amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} }; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 190 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 191 | if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) && |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 192 | (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False; |
| 193 | return check_code(obj, code, &pat); |
| 194 | #endif |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 195 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 196 | /* For other platforms, no patterns known */ |
| 197 | return False; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 198 | } |
| 199 | |
weidendo | 234780c | 2008-12-18 19:48:35 +0000 | [diff] [blame] | 200 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 201 | /*------------------------------------------------------------*/ |
| 202 | /*--- Object/File/Function hash entry operations ---*/ |
| 203 | /*------------------------------------------------------------*/ |
| 204 | |
| 205 | /* Object hash table, fixed */ |
| 206 | static obj_node* obj_table[N_OBJ_ENTRIES]; |
| 207 | |
| 208 | void 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 | |
| 217 | static UInt str_hash(const Char *s, UInt table_size) |
| 218 | { |
| 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 | |
| 226 | static Char* anonymous_obj = "???"; |
| 227 | |
| 228 | static __inline__ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 229 | obj_node* new_obj_node(DebugInfo* di, obj_node* next) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 230 | { |
| 231 | Int i; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 232 | obj_node* obj; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 233 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 234 | obj = (obj_node*) CLG_MALLOC("cl.fn.non.1", sizeof(obj_node)); |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 235 | obj->name = di ? VG_(strdup)( "cl.fn.non.2", |
| 236 | VG_(DebugInfo_get_filename)(di) ) |
| 237 | : anonymous_obj; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 238 | for (i = 0; i < N_FILE_ENTRIES; i++) { |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 239 | obj->files[i] = NULL; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 240 | } |
| 241 | CLG_(stat).distinct_objs ++; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 242 | obj->number = CLG_(stat).distinct_objs; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 243 | /* 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 */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 246 | 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; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 249 | obj->next = next; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 250 | |
| 251 | // not only used for debug output (see static.c) |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 252 | obj->last_slash_pos = 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 253 | i = 0; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 254 | while(obj->name[i]) { |
| 255 | if (obj->name[i]=='/') obj->last_slash_pos = i+1; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 256 | i++; |
| 257 | } |
| 258 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 259 | if (runtime_resolve_addr == 0) search_runtime_resolve(obj); |
| 260 | |
| 261 | return obj; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 262 | } |
| 263 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 264 | obj_node* CLG_(get_obj_node)(DebugInfo* di) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 265 | { |
| 266 | obj_node* curr_obj_node; |
| 267 | UInt objname_hash; |
| 268 | const UChar* obj_name; |
| 269 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 270 | obj_name = di ? (Char*) VG_(DebugInfo_get_filename)(di) : anonymous_obj; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 271 | |
| 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 = |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 281 | new_obj_node(di, obj_table[objname_hash]); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return curr_obj_node; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | static __inline__ |
| 289 | file_node* new_file_node(Char filename[FILENAME_LEN], |
| 290 | obj_node* obj, file_node* next) |
| 291 | { |
| 292 | Int i; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 293 | file_node* file = (file_node*) CLG_MALLOC("cl.fn.nfn.1", |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 294 | sizeof(file_node)); |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 295 | file->name = VG_(strdup)("cl.fn.nfn.2", filename); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 296 | for (i = 0; i < N_FN_ENTRIES; i++) { |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 297 | file->fns[i] = NULL; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 298 | } |
| 299 | CLG_(stat).distinct_files++; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 300 | file->number = CLG_(stat).distinct_files; |
| 301 | file->obj = obj; |
| 302 | file->next = next; |
| 303 | return file; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | file_node* CLG_(get_file_node)(obj_node* curr_obj_node, |
| 308 | Char filename[FILENAME_LEN]) |
| 309 | { |
| 310 | file_node* curr_file_node; |
| 311 | UInt filename_hash; |
| 312 | |
| 313 | /* lookup in file hash */ |
| 314 | filename_hash = str_hash(filename, N_FILE_ENTRIES); |
| 315 | curr_file_node = curr_obj_node->files[filename_hash]; |
| 316 | while (NULL != curr_file_node && |
| 317 | VG_(strcmp)(filename, curr_file_node->name) != 0) { |
| 318 | curr_file_node = curr_file_node->next; |
| 319 | } |
| 320 | if (NULL == curr_file_node) { |
| 321 | curr_obj_node->files[filename_hash] = curr_file_node = |
| 322 | new_file_node(filename, curr_obj_node, |
| 323 | curr_obj_node->files[filename_hash]); |
| 324 | } |
| 325 | |
| 326 | return curr_file_node; |
| 327 | } |
| 328 | |
| 329 | /* forward decl. */ |
| 330 | static void resize_fn_array(void); |
| 331 | |
| 332 | static __inline__ |
| 333 | fn_node* new_fn_node(Char fnname[FILENAME_LEN], |
| 334 | file_node* file, fn_node* next) |
| 335 | { |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 336 | fn_node* fn = (fn_node*) CLG_MALLOC("cl.fn.nfnnd.1", |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 337 | sizeof(fn_node)); |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 338 | fn->name = VG_(strdup)("cl.fn.nfnnd.2", fnname); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 339 | |
| 340 | CLG_(stat).distinct_fns++; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 341 | fn->number = CLG_(stat).distinct_fns; |
| 342 | fn->last_cxt = 0; |
| 343 | fn->pure_cxt = 0; |
| 344 | fn->file = file; |
| 345 | fn->next = next; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 346 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 347 | fn->dump_before = False; |
| 348 | fn->dump_after = False; |
| 349 | fn->zero_before = False; |
| 350 | fn->toggle_collect = False; |
| 351 | fn->skip = False; |
| 352 | fn->pop_on_jump = CLG_(clo).pop_on_jump; |
| 353 | fn->is_malloc = False; |
| 354 | fn->is_realloc = False; |
| 355 | fn->is_free = False; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 356 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 357 | fn->group = 0; |
| 358 | fn->separate_callers = CLG_(clo).separate_callers; |
| 359 | fn->separate_recursions = CLG_(clo).separate_recursions; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 360 | |
| 361 | #if CLG_ENABLE_DEBUG |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 362 | fn->verbosity = -1; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 363 | #endif |
| 364 | |
| 365 | if (CLG_(stat).distinct_fns >= current_fn_active.size) |
| 366 | resize_fn_array(); |
| 367 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 368 | return fn; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | /* Get a function node in hash2 with known file node. |
| 373 | * hash nodes are created if needed |
| 374 | */ |
| 375 | static |
| 376 | fn_node* get_fn_node_infile(file_node* curr_file_node, |
| 377 | Char fnname[FN_NAME_LEN]) |
| 378 | { |
| 379 | fn_node* curr_fn_node; |
| 380 | UInt fnname_hash; |
| 381 | |
| 382 | CLG_ASSERT(curr_file_node != 0); |
| 383 | |
| 384 | /* lookup in function hash */ |
| 385 | fnname_hash = str_hash(fnname, N_FN_ENTRIES); |
| 386 | curr_fn_node = curr_file_node->fns[fnname_hash]; |
| 387 | while (NULL != curr_fn_node && |
| 388 | VG_(strcmp)(fnname, curr_fn_node->name) != 0) { |
| 389 | curr_fn_node = curr_fn_node->next; |
| 390 | } |
| 391 | if (NULL == curr_fn_node) { |
| 392 | curr_file_node->fns[fnname_hash] = curr_fn_node = |
| 393 | new_fn_node(fnname, curr_file_node, |
| 394 | curr_file_node->fns[fnname_hash]); |
| 395 | } |
| 396 | |
| 397 | return curr_fn_node; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /* Get a function node in a Segment. |
| 402 | * Hash nodes are created if needed. |
| 403 | */ |
| 404 | static __inline__ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 405 | fn_node* get_fn_node_inseg(DebugInfo* di, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 406 | Char filename[FILENAME_LEN], |
| 407 | Char fnname[FN_NAME_LEN]) |
| 408 | { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 409 | obj_node *obj = CLG_(get_obj_node)(di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 410 | file_node *file = CLG_(get_file_node)(obj, filename); |
| 411 | fn_node *fn = get_fn_node_infile(file, fnname); |
| 412 | |
| 413 | return fn; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | Bool CLG_(get_debug_info)(Addr instr_addr, |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 418 | Char file[FILENAME_LEN], |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 419 | Char fn_name[FN_NAME_LEN], UInt* line_num, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 420 | DebugInfo** pDebugInfo) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 421 | { |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 422 | Bool found_file_line, found_fn, found_dirname, result = True; |
| 423 | Char dir[FILENAME_LEN]; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 424 | UInt line; |
| 425 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 426 | CLG_DEBUG(6, " + get_debug_info(%#lx)\n", instr_addr); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 427 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 428 | if (pDebugInfo) { |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 429 | *pDebugInfo = VG_(find_DebugInfo)(instr_addr); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 430 | |
| 431 | // for generated code in anonymous space, pSegInfo is 0 |
| 432 | } |
| 433 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 434 | found_file_line = VG_(get_filename_linenum)(instr_addr, |
| 435 | file, FILENAME_LEN, |
| 436 | dir, FILENAME_LEN, |
| 437 | &found_dirname, |
| 438 | &line); |
| 439 | found_fn = VG_(get_fnname)(instr_addr, |
| 440 | fn_name, FN_NAME_LEN); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 441 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 442 | if (found_dirname) { |
| 443 | // +1 for the '/'. |
| 444 | CLG_ASSERT(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILENAME_LEN); |
| 445 | VG_(strcat)(dir, "/"); // Append '/' |
| 446 | VG_(strcat)(dir, file); // Append file to dir |
| 447 | VG_(strcpy)(file, dir); // Move dir+file to file |
| 448 | } |
| 449 | |
| 450 | if (!found_file_line && !found_fn) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 451 | CLG_(stat).no_debug_BBs++; |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 452 | VG_(strcpy)(file, "???"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 453 | VG_(strcpy)(fn_name, "???"); |
| 454 | if (line_num) *line_num=0; |
| 455 | result = False; |
| 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).full_debug_BBs++; |
| 459 | if (line_num) *line_num=line; |
| 460 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 461 | } else if ( found_file_line && !found_fn) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 462 | CLG_(stat).file_line_debug_BBs++; |
| 463 | VG_(strcpy)(fn_name, "???"); |
| 464 | if (line_num) *line_num=line; |
| 465 | |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 466 | } else /*(!found_file_line && found_fn)*/ { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 467 | CLG_(stat).fn_name_debug_BBs++; |
weidendo | 3db4322 | 2007-09-17 12:52:10 +0000 | [diff] [blame] | 468 | VG_(strcpy)(file, "???"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 469 | if (line_num) *line_num=0; |
| 470 | } |
| 471 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 472 | CLG_DEBUG(6, " - get_debug_info(%#lx): seg '%s', fn %s\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 473 | instr_addr, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 474 | !pDebugInfo ? (const UChar*)"-" : |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 475 | (*pDebugInfo) ? VG_(DebugInfo_get_filename)(*pDebugInfo) : |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 476 | (const UChar*)"(None)", |
| 477 | fn_name); |
| 478 | |
| 479 | return result; |
| 480 | } |
| 481 | |
| 482 | /* for _libc_freeres_wrapper => _exit renaming */ |
| 483 | static BB* exit_bb = 0; |
| 484 | |
| 485 | |
| 486 | /* |
| 487 | * Attach function struct to a BB from debug info. |
| 488 | */ |
| 489 | fn_node* CLG_(get_fn_node)(BB* bb) |
| 490 | { |
| 491 | Char filename[FILENAME_LEN], fnname[FN_NAME_LEN]; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 492 | DebugInfo* di; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 493 | UInt line_num; |
| 494 | fn_node* fn; |
| 495 | |
| 496 | /* fn from debug info is idempotent for a BB */ |
| 497 | if (bb->fn) return bb->fn; |
| 498 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 499 | CLG_DEBUG(3,"+ get_fn_node(BB %#lx)\n", bb_addr(bb)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 500 | |
| 501 | /* get function/file name, line number and object of |
| 502 | * the BB according to debug information |
| 503 | */ |
| 504 | CLG_(get_debug_info)(bb_addr(bb), |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 505 | filename, fnname, &line_num, &di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 506 | |
| 507 | if (0 == VG_(strcmp)(fnname, "???")) { |
| 508 | int p; |
| 509 | |
| 510 | /* Use address as found in library */ |
| 511 | if (sizeof(Addr) == 4) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 512 | p = VG_(sprintf)(fnname, "%#08lx", bb->offset); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 513 | else |
| 514 | // 64bit address |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 515 | p = VG_(sprintf)(fnname, "%#016lx", bb->offset); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 516 | |
| 517 | VG_(sprintf)(fnname+p, "%s", |
| 518 | (bb->sect_kind == Vg_SectData) ? " [Data]" : |
| 519 | (bb->sect_kind == Vg_SectBSS) ? " [BSS]" : |
| 520 | (bb->sect_kind == Vg_SectGOT) ? " [GOT]" : |
| 521 | (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : ""); |
| 522 | } |
| 523 | else { |
| 524 | if (VG_(get_fnname_if_entry)(bb_addr(bb), fnname, FN_NAME_LEN)) |
| 525 | 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), |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 535 | filename, fnname, &line_num, &di); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 536 | |
| 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 */ |
| 546 | VG_(sprintf)(fnname, "_dl_runtime_resolve"); |
| 547 | } |
| 548 | |
| 549 | /* get fn_node struct for this function */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 550 | fn = get_fn_node_inseg( di, filename, fnname); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 551 | |
| 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) |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 572 | VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve:" |
| 573 | " %s +%#lx=%#lx\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 574 | bb->obj->name + bb->obj->last_slash_pos, |
| 575 | bb->offset, bb_addr(bb)); |
| 576 | } |
| 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 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 591 | CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s:%u)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 592 | bb_addr(bb), fnname, filename, line_num); |
| 593 | |
| 594 | return fn; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /*------------------------------------------------------------*/ |
| 599 | /*--- Active function array operations ---*/ |
| 600 | /*------------------------------------------------------------*/ |
| 601 | |
| 602 | /* The active function array is a thread-specific array |
| 603 | * of UInts, mapping function numbers to the active count of |
| 604 | * functions. |
| 605 | * The active count is the number of times a function appears |
| 606 | * in the current call stack, and is used when costs for recursion |
| 607 | * levels should be separated. |
| 608 | */ |
| 609 | |
| 610 | UInt* CLG_(get_fn_entry)(Int n) |
| 611 | { |
| 612 | CLG_ASSERT(n < current_fn_active.size); |
| 613 | return current_fn_active.array + n; |
| 614 | } |
| 615 | |
| 616 | void CLG_(init_fn_array)(fn_array* a) |
| 617 | { |
| 618 | Int i; |
| 619 | |
| 620 | CLG_ASSERT(a != 0); |
| 621 | |
| 622 | a->size = N_INITIAL_FN_ARRAY_SIZE; |
| 623 | if (a->size <= CLG_(stat).distinct_fns) |
| 624 | a->size = CLG_(stat).distinct_fns+1; |
| 625 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 626 | a->array = (UInt*) CLG_MALLOC("cl.fn.gfe.1", |
| 627 | a->size * sizeof(UInt)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 628 | for(i=0;i<a->size;i++) |
| 629 | a->array[i] = 0; |
| 630 | } |
| 631 | |
| 632 | void CLG_(copy_current_fn_array)(fn_array* dst) |
| 633 | { |
| 634 | CLG_ASSERT(dst != 0); |
| 635 | |
| 636 | dst->size = current_fn_active.size; |
| 637 | dst->array = current_fn_active.array; |
| 638 | } |
| 639 | |
| 640 | fn_array* CLG_(get_current_fn_array)() |
| 641 | { |
| 642 | return ¤t_fn_active; |
| 643 | } |
| 644 | |
| 645 | void CLG_(set_current_fn_array)(fn_array* a) |
| 646 | { |
| 647 | CLG_ASSERT(a != 0); |
| 648 | |
| 649 | current_fn_active.size = a->size; |
| 650 | current_fn_active.array = a->array; |
| 651 | if (current_fn_active.size <= CLG_(stat).distinct_fns) |
| 652 | resize_fn_array(); |
| 653 | } |
| 654 | |
| 655 | /* ensure that active_array is big enough: |
| 656 | * <distinct_fns> is the highest index, so <fn_active_array_size> |
| 657 | * has to be bigger than that. |
| 658 | */ |
| 659 | static void resize_fn_array(void) |
| 660 | { |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 661 | UInt* new_array; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 662 | Int i, newsize; |
| 663 | |
| 664 | newsize = current_fn_active.size; |
| 665 | while (newsize <= CLG_(stat).distinct_fns) newsize *=2; |
| 666 | |
| 667 | CLG_DEBUG(0, "Resize fn_active_array: %d => %d\n", |
| 668 | current_fn_active.size, newsize); |
| 669 | |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 670 | new_array = (UInt*) CLG_MALLOC("cl.fn.rfa.1", newsize * sizeof(UInt)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 671 | for(i=0;i<current_fn_active.size;i++) |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 672 | new_array[i] = current_fn_active.array[i]; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 673 | while(i<newsize) |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 674 | new_array[i++] = 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 675 | |
| 676 | VG_(free)(current_fn_active.array); |
| 677 | current_fn_active.size = newsize; |
weidendo | 0b23d6e | 2009-06-15 00:16:32 +0000 | [diff] [blame] | 678 | current_fn_active.array = new_array; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 679 | CLG_(stat).fn_array_resizes++; |
| 680 | } |
| 681 | |
| 682 | |