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