sewardj | 267100d | 2005-04-24 12:33:12 +0000 | [diff] [blame] | 1 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
sewardj | 267100d | 2005-04-24 12:33:12 +0000 | [diff] [blame] | 3 | /*--- Take snapshots of client stacks. m_stacktrace.c ---*/ |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2009 Julian Seward |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_threadstate.h" |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 34 | #include "pub_core_debuginfo.h" // XXX: circular dependency |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 35 | #include "pub_core_aspacemgr.h" // For VG_(is_addressable)() |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_core_libcprint.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 39 | #include "pub_core_machine.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 40 | #include "pub_core_options.h" |
sewardj | 9084de7 | 2008-02-11 11:23:12 +0000 | [diff] [blame] | 41 | #include "pub_core_stacks.h" // VG_(stack_limits) |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 42 | #include "pub_core_stacktrace.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 43 | #include "pub_core_xarray.h" |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 44 | #include "pub_core_clientstate.h" // VG_(client__dl_sysinfo_int80) |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 45 | #include "pub_core_trampoline.h" |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 46 | |
| 47 | /*------------------------------------------------------------*/ |
| 48 | /*--- Exported functions. ---*/ |
| 49 | /*------------------------------------------------------------*/ |
| 50 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 51 | /* Take a snapshot of the client's stack, putting up to 'max_n_ips' |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 52 | IPs into 'ips'. In order to be thread-safe, we pass in the |
| 53 | thread's IP SP, FP if that's meaningful, and LR if that's |
| 54 | meaningful. Returns number of IPs put in 'ips'. |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 55 | |
| 56 | If you know what the thread ID for this stack is, send that as the |
| 57 | first parameter, else send zero. This helps generate better stack |
| 58 | traces on ppc64-linux and has no effect on other platforms. |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 59 | */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 60 | UInt VG_(get_StackTrace_wrk) ( ThreadId tid_if_known, |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 61 | /*OUT*/Addr* ips, UInt max_n_ips, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 62 | /*OUT*/Addr* sps, /*OUT*/Addr* fps, |
| 63 | Addr ip, Addr sp, Addr fp, Addr lr, |
| 64 | Addr fp_min, Addr fp_max_orig ) |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 65 | { |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 66 | # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) \ |
| 67 | || defined(VGP_ppc32_aix5) \ |
| 68 | || defined(VGP_ppc64_aix5) |
| 69 | Bool lr_is_first_RA = False; |
| 70 | # endif |
| 71 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) \ |
| 72 | || defined(VGP_ppc32_aix5) |
| 73 | Word redir_stack_size = 0; |
| 74 | Word redirs_used = 0; |
| 75 | # endif |
| 76 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 77 | Bool debug = False; |
| 78 | Int i; |
| 79 | Addr fp_max; |
| 80 | UInt n_found = 0; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 81 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 82 | vg_assert(sizeof(Addr) == sizeof(UWord)); |
| 83 | vg_assert(sizeof(Addr) == sizeof(void*)); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 84 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 85 | /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1], |
njn | c0ec8e9 | 2005-12-25 06:34:04 +0000 | [diff] [blame] | 86 | stopping when the trail goes cold, which we guess to be |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 87 | when FP is not a reasonable stack location. */ |
| 88 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 89 | // JRS 2002-sep-17: hack, to round up fp_max to the end of the |
| 90 | // current page, at least. Dunno if it helps. |
| 91 | // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 92 | fp_max = VG_PGROUNDUP(fp_max_orig); |
sewardj | d0c0ea6 | 2008-03-03 22:20:51 +0000 | [diff] [blame] | 93 | if (fp_max >= sizeof(Addr)) |
| 94 | fp_max -= sizeof(Addr); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 95 | |
| 96 | if (debug) |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 97 | VG_(printf)("max_n_ips=%d fp_min=0x%lx fp_max_orig=0x%lx, " |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 98 | "fp_max=0x%lx ip=0x%lx fp=0x%lx\n", |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 99 | max_n_ips, fp_min, fp_max_orig, fp_max, ip, fp); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 100 | |
| 101 | /* Assertion broken before main() is reached in pthreaded programs; the |
| 102 | * offending stack traces only have one item. --njn, 2002-aug-16 */ |
| 103 | /* vg_assert(fp_min <= fp_max);*/ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 104 | // On Darwin, this kicks in for pthread-related stack traces, so they're |
| 105 | // only 1 entry long which is wrong. |
| 106 | #if !defined(VGO_darwin) |
sewardj | 5bdfbd2 | 2007-12-15 22:13:05 +0000 | [diff] [blame] | 107 | if (fp_min + 512 >= fp_max) { |
| 108 | /* If the stack limits look bogus, don't poke around ... but |
| 109 | don't bomb out either. */ |
sewardj | 5fa8780 | 2008-05-02 22:27:07 +0000 | [diff] [blame] | 110 | if (sps) sps[0] = sp; |
| 111 | if (fps) fps[0] = fp; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 112 | ips[0] = ip; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 113 | return 1; |
| 114 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 115 | #endif |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 116 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 117 | /* Otherwise unwind the stack in a platform-specific way. Trying |
sewardj | db2ac81 | 2005-12-23 23:33:51 +0000 | [diff] [blame] | 118 | to merge the x86, amd64, ppc32 and ppc64 logic into a single |
| 119 | piece of code is just too confusing and difficult to |
| 120 | performance-tune. */ |
njn | 88b5a98 | 2005-05-16 00:16:56 +0000 | [diff] [blame] | 121 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 122 | # if defined(VGP_x86_linux) || defined(VGP_x86_darwin) |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 123 | |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 124 | /*--------------------- x86 ---------------------*/ |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 125 | |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 126 | /* fp is %ebp. sp is %esp. ip is %eip. */ |
| 127 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 128 | if (sps) sps[0] = sp; |
| 129 | if (fps) fps[0] = fp; |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 130 | ips[0] = ip; |
| 131 | i = 1; |
| 132 | |
| 133 | /* Loop unwinding the stack. Note that the IP value we get on |
| 134 | * each pass (whether from CFI info or a stack frame) is a |
| 135 | * return address so is actually after the calling instruction |
| 136 | * in the calling function. |
| 137 | * |
| 138 | * Because of this we subtract one from the IP after each pass |
| 139 | * of the loop so that we find the right CFI block on the next |
| 140 | * pass - otherwise we can find the wrong CFI info if it happens |
| 141 | * to change after the calling instruction and that will mean |
| 142 | * that we will fail to unwind the next step. |
| 143 | * |
| 144 | * This most frequently happens at the end of a function when |
| 145 | * a tail call occurs and we wind up using the CFI info for the |
| 146 | * next function which is completely wrong. |
| 147 | */ |
| 148 | while (True) { |
| 149 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 150 | if (i >= max_n_ips) |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 151 | break; |
| 152 | |
| 153 | /* Try to derive a new (ip,sp,fp) triple from the current |
| 154 | set. */ |
| 155 | |
| 156 | /* On x86, first try the old-fashioned method of following the |
| 157 | %ebp-chain. Code which doesn't use this (that is, compiled |
| 158 | with -fomit-frame-pointer) is not ABI compliant and so |
| 159 | relatively rare. Besides, trying the CFI first almost always |
| 160 | fails, and is expensive. */ |
| 161 | /* Deal with frames resulting from functions which begin "pushl% |
| 162 | ebp ; movl %esp, %ebp" which is the ABI-mandated preamble. */ |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 163 | if (fp_min <= fp && |
| 164 | fp <= fp_max - 1 * sizeof(UWord)/*see comment below*/) |
| 165 | { |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 166 | /* fp looks sane, so use it. */ |
| 167 | ip = (((UWord*)fp)[1]); |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 168 | // We stop if we hit a zero (the traditional end-of-stack |
| 169 | // marker) or a one -- these correspond to recorded IPs of 0 or -1. |
| 170 | // The latter because r8818 (in this file) changes the meaning of |
| 171 | // entries [1] and above in a stack trace, by subtracting 1 from |
| 172 | // them. Hence stacks that used to end with a zero value now end in |
| 173 | // -1 and so we must detect that too. |
| 174 | if (0 == ip || 1 == ip) break; |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 175 | sp = fp + sizeof(Addr) /*saved %ebp*/ |
| 176 | + sizeof(Addr) /*ra*/; |
| 177 | fp = (((UWord*)fp)[0]); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 178 | if (sps) sps[i] = sp; |
| 179 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 180 | ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 181 | if (debug) |
dirk | e406723 | 2007-10-04 21:36:40 +0000 | [diff] [blame] | 182 | VG_(printf)(" ipsF[%d]=0x%08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 183 | ip = ip - 1; /* as per comment at the head of this loop */ |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 184 | continue; |
| 185 | } |
| 186 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 187 | /* That didn't work out, so see if there is any CF info to hand |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 188 | which can be used. */ |
sewardj | eadcd86 | 2006-04-04 15:12:44 +0000 | [diff] [blame] | 189 | if ( VG_(use_CF_info)( &ip, &sp, &fp, fp_min, fp_max ) ) { |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 190 | if (0 == ip || 1 == ip) break; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 191 | if (sps) sps[i] = sp; |
| 192 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 193 | ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 194 | if (debug) |
dirk | e406723 | 2007-10-04 21:36:40 +0000 | [diff] [blame] | 195 | VG_(printf)(" ipsC[%d]=0x%08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 196 | ip = ip - 1; /* as per comment at the head of this loop */ |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 197 | continue; |
| 198 | } |
| 199 | |
sewardj | c8259b8 | 2009-04-22 22:42:10 +0000 | [diff] [blame] | 200 | /* And, similarly, try for MSVC FPO unwind info. */ |
| 201 | if ( VG_(use_FPO_info)( &ip, &sp, &fp, fp_min, fp_max ) ) { |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 202 | if (0 == ip || 1 == ip) break; |
sewardj | c8259b8 | 2009-04-22 22:42:10 +0000 | [diff] [blame] | 203 | if (sps) sps[i] = sp; |
| 204 | if (fps) fps[i] = fp; |
| 205 | ips[i++] = ip; |
| 206 | if (debug) |
| 207 | VG_(printf)(" ipsC[%d]=0x%08lx\n", i-1, ips[i-1]); |
| 208 | ip = ip - 1; |
| 209 | continue; |
| 210 | } |
| 211 | |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 212 | /* No luck. We have to give up. */ |
| 213 | break; |
| 214 | } |
| 215 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 216 | # elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin) |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 217 | |
| 218 | /*--------------------- amd64 ---------------------*/ |
| 219 | |
| 220 | /* fp is %rbp. sp is %rsp. ip is %rip. */ |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 221 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 222 | ips[0] = ip; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 223 | if (sps) sps[0] = sp; |
| 224 | if (fps) fps[0] = fp; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 225 | i = 1; |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 226 | |
tom | ac35f10 | 2005-11-05 00:17:21 +0000 | [diff] [blame] | 227 | /* Loop unwinding the stack. Note that the IP value we get on |
| 228 | * each pass (whether from CFI info or a stack frame) is a |
| 229 | * return address so is actually after the calling instruction |
| 230 | * in the calling function. |
| 231 | * |
| 232 | * Because of this we subtract one from the IP after each pass |
| 233 | * of the loop so that we find the right CFI block on the next |
| 234 | * pass - otherwise we can find the wrong CFI info if it happens |
| 235 | * to change after the calling instruction and that will mean |
| 236 | * that we will fail to unwind the next step. |
| 237 | * |
| 238 | * This most frequently happens at the end of a function when |
| 239 | * a tail call occurs and we wind up using the CFI info for the |
| 240 | * next function which is completely wrong. |
| 241 | */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 242 | while (True) { |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 243 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 244 | if (i >= max_n_ips) |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 245 | break; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 246 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 247 | /* Try to derive a new (ip,sp,fp) triple from the current set. */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 248 | |
| 249 | /* First off, see if there is any CFI info to hand which can |
| 250 | be used. */ |
sewardj | eadcd86 | 2006-04-04 15:12:44 +0000 | [diff] [blame] | 251 | if ( VG_(use_CF_info)( &ip, &sp, &fp, fp_min, fp_max ) ) { |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 252 | if (0 == ip || 1 == ip) break; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 253 | if (sps) sps[i] = sp; |
| 254 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 255 | ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 256 | if (debug) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 257 | VG_(printf)(" ipsC[%d]=%#08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 258 | ip = ip - 1; /* as per comment at the head of this loop */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 259 | continue; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 260 | } |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 261 | |
sewardj | eadcd86 | 2006-04-04 15:12:44 +0000 | [diff] [blame] | 262 | /* If VG_(use_CF_info) fails, it won't modify ip/sp/fp, so |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 263 | we can safely try the old-fashioned method. */ |
| 264 | /* This bit is supposed to deal with frames resulting from |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 265 | functions which begin "pushq %rbp ; movq %rsp, %rbp". |
| 266 | Unfortunately, since we can't (easily) look at the insns at |
| 267 | the start of the fn, like GDB does, there's no reliable way |
| 268 | to tell. Hence the hack of first trying out CFI, and if that |
| 269 | fails, then use this as a fallback. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 270 | /* Note: re "- 1 * sizeof(UWord)", need to take account of the |
| 271 | fact that we are prodding at & ((UWord*)fp)[1] and so need to |
| 272 | adjust the limit check accordingly. Omitting this has been |
| 273 | observed to cause segfaults on rare occasions. */ |
| 274 | if (fp_min <= fp && fp <= fp_max - 1 * sizeof(UWord)) { |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 275 | /* fp looks sane, so use it. */ |
| 276 | ip = (((UWord*)fp)[1]); |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 277 | if (0 == ip || 1 == ip) break; |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 278 | sp = fp + sizeof(Addr) /*saved %rbp*/ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 279 | + sizeof(Addr) /*ra*/; |
| 280 | fp = (((UWord*)fp)[0]); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 281 | if (sps) sps[i] = sp; |
| 282 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 283 | ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 284 | if (debug) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 285 | VG_(printf)(" ipsF[%d]=%#08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 286 | ip = ip - 1; /* as per comment at the head of this loop */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 290 | /* Last-ditch hack (evidently GDB does something similar). We |
| 291 | are in the middle of nowhere and we have a nonsense value for |
| 292 | the frame pointer. If the stack pointer is still valid, |
| 293 | assume that what it points at is a return address. Yes, |
| 294 | desperate measures. Could do better here: |
| 295 | - check that the supposed return address is in |
| 296 | an executable page |
| 297 | - check that the supposed return address is just after a call insn |
| 298 | - given those two checks, don't just consider *sp as the return |
| 299 | address; instead scan a likely section of stack (eg sp .. sp+256) |
| 300 | and use suitable values found there. |
| 301 | */ |
| 302 | if (fp_min <= sp && sp < fp_max) { |
| 303 | ip = ((UWord*)sp)[0]; |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 304 | if (0 == ip || 1 == ip) break; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 305 | if (sps) sps[i] = sp; |
| 306 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 307 | ips[i++] = ip == 0 |
| 308 | ? 0 /* sp[0] == 0 ==> stuck at the bottom of a |
| 309 | thread stack */ |
| 310 | : ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 311 | if (debug) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 312 | VG_(printf)(" ipsH[%d]=%#08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 313 | ip = ip - 1; /* as per comment at the head of this loop */ |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 314 | sp += 8; |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | /* No luck at all. We have to give up. */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 319 | break; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 320 | } |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 321 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 322 | # elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) \ |
| 323 | || defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 324 | |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 325 | /*--------------------- ppc32/64 ---------------------*/ |
sewardj | 75ea798 | 2005-11-14 15:18:25 +0000 | [diff] [blame] | 326 | |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 327 | /* fp is %r1. ip is %cia. Note, ppc uses r1 as both the stack and |
| 328 | frame pointers. */ |
| 329 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 330 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) |
| 331 | redir_stack_size = VEX_GUEST_PPC64_REDIR_STACK_SIZE; |
| 332 | redirs_used = 0; |
| 333 | # elif defined(VGP_ppc32_aix5) |
| 334 | redir_stack_size = VEX_GUEST_PPC32_REDIR_STACK_SIZE; |
| 335 | redirs_used = 0; |
| 336 | # endif |
| 337 | |
| 338 | # if defined(VG_PLAT_USES_PPCTOC) |
sewardj | cfb5e2b | 2006-01-19 03:47:30 +0000 | [diff] [blame] | 339 | /* Deal with bogus LR values caused by function |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 340 | interception/wrapping on ppc-TOC platforms; see comment on |
| 341 | similar code a few lines further down. */ |
| 342 | if (ULong_to_Ptr(lr) == (void*)&VG_(ppctoc_magic_redirect_return_stub) |
sewardj | cfb5e2b | 2006-01-19 03:47:30 +0000 | [diff] [blame] | 343 | && VG_(is_valid_tid)(tid_if_known)) { |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 344 | Word hsp = VG_(threads)[tid_if_known].arch.vex.guest_REDIR_SP; |
| 345 | redirs_used++; |
| 346 | if (hsp >= 1 && hsp < redir_stack_size) |
sewardj | cfb5e2b | 2006-01-19 03:47:30 +0000 | [diff] [blame] | 347 | lr = VG_(threads)[tid_if_known] |
| 348 | .arch.vex.guest_REDIR_STACK[hsp-1]; |
| 349 | } |
| 350 | # endif |
| 351 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 352 | /* We have to determine whether or not LR currently holds this fn |
| 353 | (call it F)'s return address. It might not if F has previously |
| 354 | called some other function, hence overwriting LR with a pointer |
| 355 | to some part of F. Hence if LR and IP point to the same |
| 356 | function then we conclude LR does not hold this function's |
| 357 | return address; instead the LR at entry must have been saved in |
| 358 | the stack by F's prologue and so we must get it from there |
| 359 | instead. Note all this guff only applies to the innermost |
| 360 | frame. */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 361 | lr_is_first_RA = False; |
| 362 | { |
| 363 | # define M_VG_ERRTXT 1000 |
| 364 | UChar buf_lr[M_VG_ERRTXT], buf_ip[M_VG_ERRTXT]; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 365 | /* The following conditional looks grossly inefficient and |
| 366 | surely could be majorly improved, with not much effort. */ |
njn | 6b7611b | 2009-02-11 06:06:10 +0000 | [diff] [blame] | 367 | if (VG_(get_fnname_raw) (lr, buf_lr, M_VG_ERRTXT)) |
| 368 | if (VG_(get_fnname_raw) (ip, buf_ip, M_VG_ERRTXT)) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 369 | if (VG_(strncmp)(buf_lr, buf_ip, M_VG_ERRTXT)) |
| 370 | lr_is_first_RA = True; |
| 371 | # undef M_VG_ERRTXT |
| 372 | } |
| 373 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 374 | if (sps) sps[0] = fp; /* NB. not sp */ |
| 375 | if (fps) fps[0] = fp; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 376 | ips[0] = ip; |
| 377 | i = 1; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 378 | |
sewardj | db2ac81 | 2005-12-23 23:33:51 +0000 | [diff] [blame] | 379 | if (fp_min <= fp && fp < fp_max-VG_WORDSIZE+1) { |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 380 | |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 381 | /* initial FP is sane; keep going */ |
| 382 | fp = (((UWord*)fp)[0]); |
| 383 | |
| 384 | while (True) { |
| 385 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 386 | /* On ppc64-linux (ppc64-elf, really), and on AIX, the lr save |
| 387 | slot is 2 words back from sp, whereas on ppc32-elf(?) it's |
| 388 | only one word back. */ |
| 389 | # if defined(VGP_ppc64_linux) \ |
| 390 | || defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 391 | const Int lr_offset = 2; |
| 392 | # else |
| 393 | const Int lr_offset = 1; |
| 394 | # endif |
sewardj | db2ac81 | 2005-12-23 23:33:51 +0000 | [diff] [blame] | 395 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 396 | if (i >= max_n_ips) |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 397 | break; |
| 398 | |
| 399 | /* Try to derive a new (ip,fp) pair from the current set. */ |
| 400 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 401 | if (fp_min <= fp && fp <= fp_max - lr_offset * sizeof(UWord)) { |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 402 | /* fp looks sane, so use it. */ |
| 403 | |
| 404 | if (i == 1 && lr_is_first_RA) |
| 405 | ip = lr; |
| 406 | else |
sewardj | db2ac81 | 2005-12-23 23:33:51 +0000 | [diff] [blame] | 407 | ip = (((UWord*)fp)[lr_offset]); |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 408 | |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 409 | # if defined(VG_PLAT_USES_PPCTOC) |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 410 | /* Nasty hack to do with function replacement/wrapping on |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 411 | ppc64-linux/ppc64-aix/ppc32-aix. If LR points to our |
| 412 | magic return stub, then we are in a wrapped or |
| 413 | intercepted function, in which LR has been messed with. |
| 414 | The original LR will have been pushed onto the thread's |
| 415 | hidden REDIR stack one down from the top (top element |
| 416 | is the saved R2) and so we should restore the value |
| 417 | from there instead. Since nested redirections can and |
| 418 | do happen, we keep track of the number of nested LRs |
| 419 | used by the unwinding so far with 'redirs_used'. */ |
| 420 | if (ip == (Addr)&VG_(ppctoc_magic_redirect_return_stub) |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 421 | && VG_(is_valid_tid)(tid_if_known)) { |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 422 | Word hsp = VG_(threads)[tid_if_known] |
| 423 | .arch.vex.guest_REDIR_SP; |
sewardj | 15338c5 | 2006-10-17 01:31:58 +0000 | [diff] [blame] | 424 | hsp -= 2 * redirs_used; |
| 425 | redirs_used ++; |
| 426 | if (hsp >= 1 && hsp < redir_stack_size) |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 427 | ip = VG_(threads)[tid_if_known] |
| 428 | .arch.vex.guest_REDIR_STACK[hsp-1]; |
| 429 | } |
| 430 | # endif |
| 431 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 432 | if (0 == ip || 1 == ip) break; |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 433 | fp = (((UWord*)fp)[0]); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 434 | if (sps) sps[i] = fp; /* NB. not sp */ |
| 435 | if (fps) fps[i] = fp; |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 436 | ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */ |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 437 | if (debug) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 438 | VG_(printf)(" ipsF[%d]=%#08lx\n", i-1, ips[i-1]); |
sewardj | b1ae15d | 2008-12-12 13:23:03 +0000 | [diff] [blame] | 439 | ip = ip - 1; /* ip is probably dead at this point, but |
| 440 | play safe, a la x86/amd64 above. See |
| 441 | extensive comments above. */ |
sewardj | 525e232 | 2005-11-13 02:41:35 +0000 | [diff] [blame] | 442 | continue; |
| 443 | } |
| 444 | |
| 445 | /* No luck there. We have to give up. */ |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 446 | break; |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 447 | } |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | # else |
| 451 | # error "Unknown platform" |
| 452 | # endif |
| 453 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 454 | n_found = i; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 455 | return n_found; |
| 456 | } |
| 457 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 458 | UInt VG_(get_StackTrace) ( ThreadId tid, |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 459 | /*OUT*/StackTrace ips, UInt max_n_ips, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 460 | /*OUT*/StackTrace sps, |
| 461 | /*OUT*/StackTrace fps, |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 462 | Word first_ip_delta ) |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 463 | { |
| 464 | /* thread in thread table */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 465 | Addr ip = VG_(get_IP)(tid); |
| 466 | Addr fp = VG_(get_FP)(tid); |
| 467 | Addr sp = VG_(get_SP)(tid); |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 468 | Addr lr = VG_(get_LR)(tid); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 469 | Addr stack_highest_word = VG_(threads)[tid].client_stack_highest_word; |
tom | 690c3c8 | 2008-02-08 15:17:07 +0000 | [diff] [blame] | 470 | Addr stack_lowest_word = 0; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 471 | |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 472 | # if defined(VGP_x86_linux) |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 473 | /* Nasty little hack to deal with syscalls - if libc is using its |
| 474 | _dl_sysinfo_int80 function for syscalls (the TLS version does), |
| 475 | then ip will always appear to be in that function when doing a |
| 476 | syscall, not the actual libc function doing the syscall. This |
| 477 | check sees if IP is within that function, and pops the return |
| 478 | address off the stack so that ip is placed within the library |
| 479 | function calling the syscall. This makes stack backtraces much |
| 480 | more useful. |
| 481 | |
| 482 | The function is assumed to look like this (from glibc-2.3.6 sources): |
| 483 | _dl_sysinfo_int80: |
| 484 | int $0x80 |
| 485 | ret |
| 486 | That is 3 (2+1) bytes long. We could be more thorough and check |
| 487 | the 3 bytes of the function are as expected, but I can't be |
| 488 | bothered. |
| 489 | */ |
| 490 | if (VG_(client__dl_sysinfo_int80) != 0 /* we know its address */ |
| 491 | && ip >= VG_(client__dl_sysinfo_int80) |
| 492 | && ip < VG_(client__dl_sysinfo_int80)+3 |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 493 | && VG_(am_is_valid_for_client)(sp, sizeof(Addr), VKI_PROT_READ)) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 494 | ip = *(Addr *)sp; |
| 495 | sp += sizeof(Addr); |
| 496 | } |
sewardj | b9bce63 | 2005-06-21 01:41:34 +0000 | [diff] [blame] | 497 | # endif |
| 498 | |
tom | 690c3c8 | 2008-02-08 15:17:07 +0000 | [diff] [blame] | 499 | /* See if we can get a better idea of the stack limits */ |
| 500 | VG_(stack_limits)(sp, &stack_lowest_word, &stack_highest_word); |
| 501 | |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 502 | /* Take into account the first_ip_delta. */ |
| 503 | vg_assert( sizeof(Addr) == sizeof(Word) ); |
| 504 | ip += first_ip_delta; |
| 505 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 506 | if (0) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 507 | VG_(printf)("tid %d: stack_highest=0x%08lx ip=0x%08lx " |
| 508 | "sp=0x%08lx fp=0x%08lx\n", |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 509 | tid, stack_highest_word, ip, sp, fp); |
| 510 | |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 511 | return VG_(get_StackTrace_wrk)(tid, ips, max_n_ips, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 512 | sps, fps, |
| 513 | ip, sp, fp, lr, sp, |
| 514 | stack_highest_word); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void printIpDesc(UInt n, Addr ip) |
| 518 | { |
njn | 83f9e79 | 2005-06-11 05:04:09 +0000 | [diff] [blame] | 519 | #define BUF_LEN 4096 |
| 520 | |
| 521 | static UChar buf[BUF_LEN]; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 522 | |
njn | 83f9e79 | 2005-06-11 05:04:09 +0000 | [diff] [blame] | 523 | VG_(describe_IP)(ip, buf, BUF_LEN); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 524 | |
| 525 | if (VG_(clo_xml)) { |
| 526 | VG_(message)(Vg_UserMsg, " %s", buf); |
| 527 | } else { |
| 528 | VG_(message)(Vg_UserMsg, " %s %s", ( n == 0 ? "at" : "by" ), buf); |
| 529 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* Print a StackTrace. */ |
| 533 | void VG_(pp_StackTrace) ( StackTrace ips, UInt n_ips ) |
| 534 | { |
| 535 | vg_assert( n_ips > 0 ); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 536 | |
| 537 | if (VG_(clo_xml)) |
| 538 | VG_(message)(Vg_UserMsg, " <stack>"); |
| 539 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 540 | VG_(apply_StackTrace)( printIpDesc, ips, n_ips ); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 541 | |
| 542 | if (VG_(clo_xml)) |
| 543 | VG_(message)(Vg_UserMsg, " </stack>"); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* Get and immediately print a StackTrace. */ |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 547 | void VG_(get_and_pp_StackTrace) ( ThreadId tid, UInt max_n_ips ) |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 548 | { |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 549 | Addr ips[max_n_ips]; |
| 550 | UInt n_ips |
| 551 | = VG_(get_StackTrace)(tid, ips, max_n_ips, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 552 | NULL/*array to dump SP values in*/, |
| 553 | NULL/*array to dump FP values in*/, |
| 554 | 0/*first_ip_delta*/); |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 555 | VG_(pp_StackTrace)(ips, n_ips); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 556 | } |
| 557 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 558 | void VG_(apply_StackTrace)( void(*action)(UInt n, Addr ip), |
| 559 | StackTrace ips, UInt n_ips ) |
| 560 | { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 561 | Bool main_done = False; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 562 | Int i = 0; |
| 563 | |
| 564 | vg_assert(n_ips > 0); |
| 565 | do { |
| 566 | Addr ip = ips[i]; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 567 | |
sewardj | 73cf4c6 | 2005-11-17 15:12:34 +0000 | [diff] [blame] | 568 | // Stop after the first appearance of "main" or one of the other names |
| 569 | // (the appearance of which is a pretty good sign that we've gone past |
| 570 | // main without seeing it, for whatever reason) |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 571 | if ( ! VG_(clo_show_below_main) ) { |
| 572 | Vg_FnNameKind kind = VG_(get_fnname_kind_from_IP)(ip); |
| 573 | if (Vg_FnNameMain == kind || Vg_FnNameBelowMain == kind) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 574 | main_done = True; |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 575 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // Act on the ip |
| 579 | action(i, ip); |
| 580 | |
| 581 | i++; |
njn | 3a4b58f | 2009-05-07 23:08:10 +0000 | [diff] [blame] | 582 | } while (i < n_ips && !main_done); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 583 | |
| 584 | #undef MYBUF_LEN |
| 585 | } |
| 586 | |
| 587 | |
| 588 | /*--------------------------------------------------------------------*/ |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 589 | /*--- end ---*/ |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 590 | /*--------------------------------------------------------------------*/ |