m_stacktrace.c revision db2ac81bd9aa4d57027cf0e416852664e18ec683
1 2/*--------------------------------------------------------------------*/ 3/*--- Take snapshots of client stacks. m_stacktrace.c ---*/ 4/*--------------------------------------------------------------------*/ 5 6/* 7 This file is part of Valgrind, a dynamic binary instrumentation 8 framework. 9 10 Copyright (C) 2000-2005 Julian Seward 11 jseward@acm.org 12 13 This program is free software; you can redistribute it and/or 14 modify it under the terms of the GNU General Public License as 15 published by the Free Software Foundation; either version 2 of the 16 License, or (at your option) any later version. 17 18 This program is distributed in the hope that it will be useful, but 19 WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program; if not, write to the Free Software 25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 26 02111-1307, USA. 27 28 The GNU General Public License is contained in the file COPYING. 29*/ 30 31#include "pub_core_basics.h" 32#include "pub_core_threadstate.h" 33#include "pub_core_debuginfo.h" 34#include "pub_core_aspacemgr.h" // For VG_(is_addressable)() 35#include "pub_core_libcbase.h" 36#include "pub_core_libcassert.h" 37#include "pub_core_libcprint.h" 38#include "pub_core_machine.h" 39#include "pub_core_options.h" 40#include "pub_core_stacktrace.h" 41#include "pub_core_trampoline.h" 42 43/*------------------------------------------------------------*/ 44/*--- Exported functions. ---*/ 45/*------------------------------------------------------------*/ 46 47/* Take a snapshot of the client's stack, putting the up to 'n_ips' 48 IPs into 'ips'. In order to be thread-safe, we pass in the 49 thread's IP SP, FP if that's meaningful, and LR if that's 50 meaningful. Returns number of IPs put in 'ips'. 51*/ 52UInt VG_(get_StackTrace2) ( Addr* ips, UInt n_ips, 53 Addr ip, Addr sp, Addr fp, Addr lr, 54 Addr fp_min, Addr fp_max_orig ) 55{ 56#if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) 57 Bool lr_is_first_RA = False; /* ppc only */ 58#endif 59 Bool debug = False; 60 Int i; 61 Addr fp_max; 62 UInt n_found = 0; 63 64 vg_assert(sizeof(Addr) == sizeof(UWord)); 65 vg_assert(sizeof(Addr) == sizeof(void*)); 66 67 /* Snaffle IPs from the client's stack into ips[0 .. n_ips-1], 68 putting zeroes in when the trail goes cold, which we guess to be 69 when FP is not a reasonable stack location. */ 70 71 for (i = 0; i < n_ips; i++) 72 ips[i] = 0; 73 74 // JRS 2002-sep-17: hack, to round up fp_max to the end of the 75 // current page, at least. Dunno if it helps. 76 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again 77 fp_max = VG_PGROUNDUP(fp_max_orig); 78 fp_max -= sizeof(Addr); 79 80 if (debug) 81 VG_(printf)("n_ips=%d fp_min=%p fp_max_orig=%p, fp_max=%p ip=%p fp=%p\n", 82 n_ips, fp_min, fp_max_orig, fp_max, ip, fp); 83 84 /* Assertion broken before main() is reached in pthreaded programs; the 85 * offending stack traces only have one item. --njn, 2002-aug-16 */ 86 /* vg_assert(fp_min <= fp_max);*/ 87 88 if (fp_min + VG_(clo_max_stackframe) <= fp_max) { 89 /* If the stack is ridiculously big, don't poke around ... but 90 don't bomb out either. Needed to make John Regehr's 91 user-space threads package work. JRS 20021001 */ 92 ips[0] = ip; 93 return 1; 94 } 95 96 /* Otherwise unwind the stack in a platform-specific way. Trying 97 to merge the x86, amd64, ppc32 and ppc64 logic into a single 98 piece of code is just too confusing and difficult to 99 performance-tune. */ 100 101# if defined(VGP_x86_linux) 102 103 /*--------------------- x86 ---------------------*/ 104 105 /* fp is %ebp. sp is %esp. ip is %eip. */ 106 107 ips[0] = ip; 108 i = 1; 109 110 /* Loop unwinding the stack. Note that the IP value we get on 111 * each pass (whether from CFI info or a stack frame) is a 112 * return address so is actually after the calling instruction 113 * in the calling function. 114 * 115 * Because of this we subtract one from the IP after each pass 116 * of the loop so that we find the right CFI block on the next 117 * pass - otherwise we can find the wrong CFI info if it happens 118 * to change after the calling instruction and that will mean 119 * that we will fail to unwind the next step. 120 * 121 * This most frequently happens at the end of a function when 122 * a tail call occurs and we wind up using the CFI info for the 123 * next function which is completely wrong. 124 */ 125 while (True) { 126 127 if (i >= n_ips) 128 break; 129 130 /* Try to derive a new (ip,sp,fp) triple from the current 131 set. */ 132 133 /* On x86, first try the old-fashioned method of following the 134 %ebp-chain. Code which doesn't use this (that is, compiled 135 with -fomit-frame-pointer) is not ABI compliant and so 136 relatively rare. Besides, trying the CFI first almost always 137 fails, and is expensive. */ 138 /* Deal with frames resulting from functions which begin "pushl% 139 ebp ; movl %esp, %ebp" which is the ABI-mandated preamble. */ 140 if (fp_min <= fp && fp <= fp_max) { 141 /* fp looks sane, so use it. */ 142 ip = (((UWord*)fp)[1]); 143 sp = fp + sizeof(Addr) /*saved %ebp*/ 144 + sizeof(Addr) /*ra*/; 145 fp = (((UWord*)fp)[0]); 146 ips[i++] = ip; 147 if (debug) 148 VG_(printf)(" ipsF[%d]=%08p\n", i-1, ips[i-1]); 149 ip = ip - 1; 150 continue; 151 } 152 153 /* That didn't work out, so see if there is any CFI info to hand 154 which can be used. */ 155 if ( VG_(use_CFI_info)( &ip, &sp, &fp, fp_min, fp_max ) ) { 156 ips[i++] = ip; 157 if (debug) 158 VG_(printf)(" ipsC[%d]=%08p\n", i-1, ips[i-1]); 159 ip = ip - 1; 160 continue; 161 } 162 163 /* No luck. We have to give up. */ 164 break; 165 } 166 167# elif defined(VGP_amd64_linux) 168 169 /*--------------------- amd64 ---------------------*/ 170 171 /* fp is %rbp. sp is %rsp. ip is %rip. */ 172 173 ips[0] = ip; 174 i = 1; 175 176 /* Loop unwinding the stack. Note that the IP value we get on 177 * each pass (whether from CFI info or a stack frame) is a 178 * return address so is actually after the calling instruction 179 * in the calling function. 180 * 181 * Because of this we subtract one from the IP after each pass 182 * of the loop so that we find the right CFI block on the next 183 * pass - otherwise we can find the wrong CFI info if it happens 184 * to change after the calling instruction and that will mean 185 * that we will fail to unwind the next step. 186 * 187 * This most frequently happens at the end of a function when 188 * a tail call occurs and we wind up using the CFI info for the 189 * next function which is completely wrong. 190 */ 191 while (True) { 192 193 if (i >= n_ips) 194 break; 195 196 /* Try to derive a new (ip,sp,fp) triple from the current 197 set. */ 198 199 /* First off, see if there is any CFI info to hand which can 200 be used. */ 201 if ( VG_(use_CFI_info)( &ip, &sp, &fp, fp_min, fp_max ) ) { 202 ips[i++] = ip; 203 if (debug) 204 VG_(printf)(" ipsC[%d]=%08p\n", i-1, ips[i-1]); 205 ip = ip - 1; 206 continue; 207 } 208 209 /* If VG_(use_CFI_info) fails, it won't modify ip/sp/fp, so 210 we can safely try the old-fashioned method. */ 211 /* This bit is supposed to deal with frames resulting from 212 functions which begin "pushq %rbp ; movq %rsp, %rbp". 213 Unfortunately, since we can't (easily) look at the insns at 214 the start of the fn, like GDB does, there's no reliable way 215 to tell. Hence the hack of first trying out CFI, and if that 216 fails, then use this as a fallback. */ 217 if (fp_min <= fp && fp <= fp_max) { 218 /* fp looks sane, so use it. */ 219 ip = (((UWord*)fp)[1]); 220 sp = fp + sizeof(Addr) /*saved %rbp*/ 221 + sizeof(Addr) /*ra*/; 222 fp = (((UWord*)fp)[0]); 223 ips[i++] = ip; 224 if (debug) 225 VG_(printf)(" ipsF[%d]=%08p\n", i-1, ips[i-1]); 226 ip = ip - 1; 227 continue; 228 } 229 230 /* No luck there. We have to give up. */ 231 break; 232 } 233 234# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) 235 236 /*--------------------- ppc32 ---------------------*/ 237 238 /* fp is %r1. ip is %cia. Note, ppc uses r1 as both the stack and 239 frame pointers. */ 240 241 lr_is_first_RA = False; 242 { 243# define M_VG_ERRTXT 1000 244 UChar buf_lr[M_VG_ERRTXT], buf_ip[M_VG_ERRTXT]; 245 if (VG_(get_fnname_nodemangle) (lr, buf_lr, M_VG_ERRTXT)) 246 if (VG_(get_fnname_nodemangle) (ip, buf_ip, M_VG_ERRTXT)) 247 if (VG_(strncmp)(buf_lr, buf_ip, M_VG_ERRTXT)) 248 lr_is_first_RA = True; 249# undef M_VG_ERRTXT 250 } 251 252 ips[0] = ip; 253 i = 1; 254 255 if (fp_min <= fp && fp < fp_max-VG_WORDSIZE+1) { 256 257 /* initial FP is sane; keep going */ 258 fp = (((UWord*)fp)[0]); 259 260 while (True) { 261 262 /* on ppc64-linux (ppc64-elf, really), the lr save slot is 2 263 words back from sp, whereas on ppc32-elf(?) it's only one 264 word back. */ 265 const Int lr_offset = VG_WORDSIZE==8 ? 2 : 1; 266 267 if (i >= n_ips) 268 break; 269 270 /* Try to derive a new (ip,fp) pair from the current set. */ 271 272 if (fp_min <= fp && fp <= fp_max) { 273 /* fp looks sane, so use it. */ 274 275 if (i == 1 && lr_is_first_RA) 276 ip = lr; 277 else 278 ip = (((UWord*)fp)[lr_offset]); 279 280 fp = (((UWord*)fp)[0]); 281 ips[i++] = ip; 282 if (debug) 283 VG_(printf)(" ipsF[%d]=%08p\n", i-1, ips[i-1]); 284 continue; 285 } 286 287 /* No luck there. We have to give up. */ 288 break; 289 } 290 } 291 292# else 293# error "Unknown platform" 294# endif 295 296 n_found = i; 297 return n_found; 298} 299 300UInt VG_(get_StackTrace) ( ThreadId tid, StackTrace ips, UInt n_ips ) 301{ 302 /* thread in thread table */ 303 Addr ip = VG_(get_IP)(tid); 304 Addr fp = VG_(get_FP)(tid); 305 Addr sp = VG_(get_SP)(tid); 306 Addr lr = VG_(get_LR)(tid); 307 Addr stack_highest_word = VG_(threads)[tid].client_stack_highest_word; 308 309# if defined(VGP_x86_linux) 310 /* Nasty little hack to deal with sysinfo syscalls - if libc is 311 using the sysinfo page for syscalls (the TLS version does), then 312 ip will always appear to be in that page when doing a syscall, 313 not the actual libc function doing the syscall. This check sees 314 if IP is within the syscall code, and pops the return address 315 off the stack so that ip is placed within the library function 316 calling the syscall. This makes stack backtraces much more 317 useful. */ 318 if (ip >= (Addr)&VG_(trampoline_stuff_start) 319 && ip < (Addr)&VG_(trampoline_stuff_end) 320 && VG_(am_is_valid_for_client)(sp, sizeof(Addr), VKI_PROT_READ)) { 321 ip = *(Addr *)sp; 322 sp += sizeof(Addr); 323 } 324# endif 325 326 if (0) 327 VG_(printf)("tid %d: stack_highest=%p ip=%p sp=%p fp=%p\n", 328 tid, stack_highest_word, ip, sp, fp); 329 330 return VG_(get_StackTrace2)(ips, n_ips, ip, sp, fp, lr, sp, stack_highest_word); 331} 332 333static void printIpDesc(UInt n, Addr ip) 334{ 335 #define BUF_LEN 4096 336 337 static UChar buf[BUF_LEN]; 338 339 VG_(describe_IP)(ip, buf, BUF_LEN); 340 341 if (VG_(clo_xml)) { 342 VG_(message)(Vg_UserMsg, " %s", buf); 343 } else { 344 VG_(message)(Vg_UserMsg, " %s %s", ( n == 0 ? "at" : "by" ), buf); 345 } 346} 347 348/* Print a StackTrace. */ 349void VG_(pp_StackTrace) ( StackTrace ips, UInt n_ips ) 350{ 351 vg_assert( n_ips > 0 ); 352 353 if (VG_(clo_xml)) 354 VG_(message)(Vg_UserMsg, " <stack>"); 355 356 VG_(apply_StackTrace)( printIpDesc, ips, n_ips ); 357 358 if (VG_(clo_xml)) 359 VG_(message)(Vg_UserMsg, " </stack>"); 360} 361 362/* Get and immediately print a StackTrace. */ 363void VG_(get_and_pp_StackTrace) ( ThreadId tid, UInt n_ips ) 364{ 365 Addr ips[n_ips]; 366 VG_(get_StackTrace)(tid, ips, n_ips); 367 VG_(pp_StackTrace) ( ips, n_ips); 368} 369 370 371void VG_(apply_StackTrace)( void(*action)(UInt n, Addr ip), 372 StackTrace ips, UInt n_ips ) 373{ 374 #define MYBUF_LEN 50 // only needs to be long enough for 375 // the names specially tested for 376 377 Bool main_done = False; 378 Char mybuf[MYBUF_LEN]; // ok to stack allocate mybuf[] -- it's tiny 379 Int i = 0; 380 381 vg_assert(n_ips > 0); 382 do { 383 Addr ip = ips[i]; 384 if (i > 0) 385 ip -= VG_MIN_INSTR_SZB; // point to calling line 386 387 // Stop after the first appearance of "main" or one of the other names 388 // (the appearance of which is a pretty good sign that we've gone past 389 // main without seeing it, for whatever reason) 390 if ( ! VG_(clo_show_below_main)) { 391 VG_(get_fnname_nodemangle)( ip, mybuf, MYBUF_LEN ); 392 mybuf[MYBUF_LEN-1] = 0; // paranoia 393 if ( VG_STREQ("main", mybuf) 394# if defined(VGO_linux) 395 || VG_STREQ("__libc_start_main", mybuf) // glibc glibness 396 || VG_STREQ("generic_start_main", mybuf) // Yellow Dog doggedness 397# endif 398 ) 399 main_done = True; 400 } 401 402 // Act on the ip 403 action(i, ip); 404 405 i++; 406 } while (i < n_ips && ips[i] != 0 && !main_done); 407 408 #undef MYBUF_LEN 409} 410 411 412/*--------------------------------------------------------------------*/ 413/*--- end ---*/ 414/*--------------------------------------------------------------------*/ 415