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