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