blob: 6e3c8d07e4f07644ed6fa8081aec5c9a527c519a [file] [log] [blame]
sewardj267100d2005-04-24 12:33:12 +00001
njnd01fef72005-03-25 23:35:48 +00002/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00003/*--- Take snapshots of client stacks. m_stacktrace.c ---*/
njnd01fef72005-03-25 23:35:48 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
njn9f207462009-03-10 22:02:09 +000010 Copyright (C) 2000-2009 Julian Seward
njnd01fef72005-03-25 23:35:48 +000011 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
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000032#include "pub_core_vki.h"
njnc7561b92005-06-19 01:24:32 +000033#include "pub_core_threadstate.h"
njn68824432009-02-10 06:48:00 +000034#include "pub_core_debuginfo.h" // XXX: circular dependency
njn24a6efb2005-06-20 03:36:51 +000035#include "pub_core_aspacemgr.h" // For VG_(is_addressable)()
njn97405b22005-06-02 03:39:33 +000036#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000037#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_core_libcprint.h"
njnf536bbb2005-06-13 04:21:38 +000039#include "pub_core_machine.h"
njn20242342005-05-16 23:31:24 +000040#include "pub_core_options.h"
sewardj9084de72008-02-11 11:23:12 +000041#include "pub_core_stacks.h" // VG_(stack_limits)
njnd01fef72005-03-25 23:35:48 +000042#include "pub_core_stacktrace.h"
sewardj14c7cc52007-02-25 15:08:24 +000043#include "pub_core_xarray.h"
sewardja672ea32006-04-29 18:03:14 +000044#include "pub_core_clientstate.h" // VG_(client__dl_sysinfo_int80)
njna7598f62005-06-18 03:27:58 +000045#include "pub_core_trampoline.h"
njnd01fef72005-03-25 23:35:48 +000046
47/*------------------------------------------------------------*/
48/*--- Exported functions. ---*/
49/*------------------------------------------------------------*/
50
njn3a4b58f2009-05-07 23:08:10 +000051/* Take a snapshot of the client's stack, putting up to 'max_n_ips'
sewardjacaec5f2005-08-19 16:02:59 +000052 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'.
sewardjdfbaa222006-01-18 04:25:20 +000055
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.
sewardjacaec5f2005-08-19 16:02:59 +000059*/
sewardjb8b79ad2008-03-03 01:35:41 +000060UInt VG_(get_StackTrace_wrk) ( ThreadId tid_if_known,
njn3a4b58f2009-05-07 23:08:10 +000061 /*OUT*/Addr* ips, UInt max_n_ips,
sewardjb8b79ad2008-03-03 01:35:41 +000062 /*OUT*/Addr* sps, /*OUT*/Addr* fps,
63 Addr ip, Addr sp, Addr fp, Addr lr,
64 Addr fp_min, Addr fp_max_orig )
njnd01fef72005-03-25 23:35:48 +000065{
sewardj15338c52006-10-17 01:31:58 +000066# 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
sewardjacaec5f2005-08-19 16:02:59 +000077 Bool debug = False;
78 Int i;
79 Addr fp_max;
80 UInt n_found = 0;
njnd01fef72005-03-25 23:35:48 +000081
sewardjacaec5f2005-08-19 16:02:59 +000082 vg_assert(sizeof(Addr) == sizeof(UWord));
83 vg_assert(sizeof(Addr) == sizeof(void*));
njnd01fef72005-03-25 23:35:48 +000084
njn3a4b58f2009-05-07 23:08:10 +000085 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
njnc0ec8e92005-12-25 06:34:04 +000086 stopping when the trail goes cold, which we guess to be
sewardjacaec5f2005-08-19 16:02:59 +000087 when FP is not a reasonable stack location. */
88
njnd01fef72005-03-25 23:35:48 +000089 // 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
sewardj45f4e7c2005-09-27 19:20:21 +000092 fp_max = VG_PGROUNDUP(fp_max_orig);
sewardjd0c0ea62008-03-03 22:20:51 +000093 if (fp_max >= sizeof(Addr))
94 fp_max -= sizeof(Addr);
njnd01fef72005-03-25 23:35:48 +000095
96 if (debug)
njn3a4b58f2009-05-07 23:08:10 +000097 VG_(printf)("max_n_ips=%d fp_min=0x%lx fp_max_orig=0x%lx, "
barta0b6b2c2008-07-07 06:49:24 +000098 "fp_max=0x%lx ip=0x%lx fp=0x%lx\n",
njn3a4b58f2009-05-07 23:08:10 +000099 max_n_ips, fp_min, fp_max_orig, fp_max, ip, fp);
njnd01fef72005-03-25 23:35:48 +0000100
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);*/
njnf76d27a2009-05-28 01:53:07 +0000104 // 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)
sewardj5bdfbd22007-12-15 22:13:05 +0000107 if (fp_min + 512 >= fp_max) {
108 /* If the stack limits look bogus, don't poke around ... but
109 don't bomb out either. */
sewardj5fa87802008-05-02 22:27:07 +0000110 if (sps) sps[0] = sp;
111 if (fps) fps[0] = fp;
sewardjacaec5f2005-08-19 16:02:59 +0000112 ips[0] = ip;
sewardjacaec5f2005-08-19 16:02:59 +0000113 return 1;
114 }
njnf76d27a2009-05-28 01:53:07 +0000115#endif
sewardj35165532005-04-30 18:47:48 +0000116
sewardjacaec5f2005-08-19 16:02:59 +0000117 /* Otherwise unwind the stack in a platform-specific way. Trying
sewardjdb2ac812005-12-23 23:33:51 +0000118 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. */
njn88b5a982005-05-16 00:16:56 +0000121
njnf76d27a2009-05-28 01:53:07 +0000122# if defined(VGP_x86_linux) || defined(VGP_x86_darwin)
sewardj35165532005-04-30 18:47:48 +0000123
sewardj75ea7982005-11-14 15:18:25 +0000124 /*--------------------- x86 ---------------------*/
sewardj35165532005-04-30 18:47:48 +0000125
sewardj75ea7982005-11-14 15:18:25 +0000126 /* fp is %ebp. sp is %esp. ip is %eip. */
127
sewardjb8b79ad2008-03-03 01:35:41 +0000128 if (sps) sps[0] = sp;
129 if (fps) fps[0] = fp;
sewardj75ea7982005-11-14 15:18:25 +0000130 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
njn3a4b58f2009-05-07 23:08:10 +0000150 if (i >= max_n_ips)
sewardj75ea7982005-11-14 15:18:25 +0000151 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. */
njn3a4b58f2009-05-07 23:08:10 +0000163 if (fp_min <= fp &&
164 fp <= fp_max - 1 * sizeof(UWord)/*see comment below*/)
165 {
sewardj75ea7982005-11-14 15:18:25 +0000166 /* fp looks sane, so use it. */
167 ip = (((UWord*)fp)[1]);
njn3a4b58f2009-05-07 23:08:10 +0000168 // 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;
sewardj75ea7982005-11-14 15:18:25 +0000175 sp = fp + sizeof(Addr) /*saved %ebp*/
176 + sizeof(Addr) /*ra*/;
177 fp = (((UWord*)fp)[0]);
sewardjb8b79ad2008-03-03 01:35:41 +0000178 if (sps) sps[i] = sp;
179 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000180 ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */
sewardj75ea7982005-11-14 15:18:25 +0000181 if (debug)
dirke4067232007-10-04 21:36:40 +0000182 VG_(printf)(" ipsF[%d]=0x%08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000183 ip = ip - 1; /* as per comment at the head of this loop */
sewardj75ea7982005-11-14 15:18:25 +0000184 continue;
185 }
186
sewardj15338c52006-10-17 01:31:58 +0000187 /* That didn't work out, so see if there is any CF info to hand
sewardj75ea7982005-11-14 15:18:25 +0000188 which can be used. */
sewardjeadcd862006-04-04 15:12:44 +0000189 if ( VG_(use_CF_info)( &ip, &sp, &fp, fp_min, fp_max ) ) {
njn3a4b58f2009-05-07 23:08:10 +0000190 if (0 == ip || 1 == ip) break;
sewardjb8b79ad2008-03-03 01:35:41 +0000191 if (sps) sps[i] = sp;
192 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000193 ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */
sewardj75ea7982005-11-14 15:18:25 +0000194 if (debug)
dirke4067232007-10-04 21:36:40 +0000195 VG_(printf)(" ipsC[%d]=0x%08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000196 ip = ip - 1; /* as per comment at the head of this loop */
sewardj75ea7982005-11-14 15:18:25 +0000197 continue;
198 }
199
sewardjc8259b82009-04-22 22:42:10 +0000200 /* And, similarly, try for MSVC FPO unwind info. */
201 if ( VG_(use_FPO_info)( &ip, &sp, &fp, fp_min, fp_max ) ) {
njn3a4b58f2009-05-07 23:08:10 +0000202 if (0 == ip || 1 == ip) break;
sewardjc8259b82009-04-22 22:42:10 +0000203 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
sewardj75ea7982005-11-14 15:18:25 +0000212 /* No luck. We have to give up. */
213 break;
214 }
215
njnf76d27a2009-05-28 01:53:07 +0000216# elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin)
sewardj75ea7982005-11-14 15:18:25 +0000217
218 /*--------------------- amd64 ---------------------*/
219
220 /* fp is %rbp. sp is %rsp. ip is %rip. */
sewardj35165532005-04-30 18:47:48 +0000221
sewardjacaec5f2005-08-19 16:02:59 +0000222 ips[0] = ip;
sewardjb8b79ad2008-03-03 01:35:41 +0000223 if (sps) sps[0] = sp;
224 if (fps) fps[0] = fp;
sewardjacaec5f2005-08-19 16:02:59 +0000225 i = 1;
sewardj35165532005-04-30 18:47:48 +0000226
tomac35f102005-11-05 00:17:21 +0000227 /* 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 */
sewardjacaec5f2005-08-19 16:02:59 +0000242 while (True) {
sewardj35165532005-04-30 18:47:48 +0000243
njn3a4b58f2009-05-07 23:08:10 +0000244 if (i >= max_n_ips)
sewardj35165532005-04-30 18:47:48 +0000245 break;
sewardjacaec5f2005-08-19 16:02:59 +0000246
njn3a4b58f2009-05-07 23:08:10 +0000247 /* Try to derive a new (ip,sp,fp) triple from the current set. */
sewardjacaec5f2005-08-19 16:02:59 +0000248
249 /* First off, see if there is any CFI info to hand which can
250 be used. */
sewardjeadcd862006-04-04 15:12:44 +0000251 if ( VG_(use_CF_info)( &ip, &sp, &fp, fp_min, fp_max ) ) {
njn3a4b58f2009-05-07 23:08:10 +0000252 if (0 == ip || 1 == ip) break;
sewardjb8b79ad2008-03-03 01:35:41 +0000253 if (sps) sps[i] = sp;
254 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000255 ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */
sewardjacaec5f2005-08-19 16:02:59 +0000256 if (debug)
barta0b6b2c2008-07-07 06:49:24 +0000257 VG_(printf)(" ipsC[%d]=%#08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000258 ip = ip - 1; /* as per comment at the head of this loop */
sewardjacaec5f2005-08-19 16:02:59 +0000259 continue;
njnd01fef72005-03-25 23:35:48 +0000260 }
sewardj35165532005-04-30 18:47:48 +0000261
sewardjeadcd862006-04-04 15:12:44 +0000262 /* If VG_(use_CF_info) fails, it won't modify ip/sp/fp, so
sewardjacaec5f2005-08-19 16:02:59 +0000263 we can safely try the old-fashioned method. */
264 /* This bit is supposed to deal with frames resulting from
sewardj75ea7982005-11-14 15:18:25 +0000265 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. */
sewardjf98e1c02008-10-25 16:22:41 +0000270 /* 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)) {
sewardjacaec5f2005-08-19 16:02:59 +0000275 /* fp looks sane, so use it. */
276 ip = (((UWord*)fp)[1]);
njn3a4b58f2009-05-07 23:08:10 +0000277 if (0 == ip || 1 == ip) break;
sewardj75ea7982005-11-14 15:18:25 +0000278 sp = fp + sizeof(Addr) /*saved %rbp*/
sewardjacaec5f2005-08-19 16:02:59 +0000279 + sizeof(Addr) /*ra*/;
280 fp = (((UWord*)fp)[0]);
sewardjb8b79ad2008-03-03 01:35:41 +0000281 if (sps) sps[i] = sp;
282 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000283 ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */
sewardjacaec5f2005-08-19 16:02:59 +0000284 if (debug)
barta0b6b2c2008-07-07 06:49:24 +0000285 VG_(printf)(" ipsF[%d]=%#08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000286 ip = ip - 1; /* as per comment at the head of this loop */
sewardjacaec5f2005-08-19 16:02:59 +0000287 continue;
288 }
289
sewardj39f34232007-11-09 23:02:28 +0000290 /* 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];
njn3a4b58f2009-05-07 23:08:10 +0000304 if (0 == ip || 1 == ip) break;
sewardjb8b79ad2008-03-03 01:35:41 +0000305 if (sps) sps[i] = sp;
306 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000307 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 */
sewardj39f34232007-11-09 23:02:28 +0000311 if (debug)
barta0b6b2c2008-07-07 06:49:24 +0000312 VG_(printf)(" ipsH[%d]=%#08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000313 ip = ip - 1; /* as per comment at the head of this loop */
sewardj39f34232007-11-09 23:02:28 +0000314 sp += 8;
315 continue;
316 }
317
318 /* No luck at all. We have to give up. */
sewardjacaec5f2005-08-19 16:02:59 +0000319 break;
njnd01fef72005-03-25 23:35:48 +0000320 }
sewardjacaec5f2005-08-19 16:02:59 +0000321
sewardj15338c52006-10-17 01:31:58 +0000322# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) \
323 || defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5)
sewardjacaec5f2005-08-19 16:02:59 +0000324
sewardjdfbaa222006-01-18 04:25:20 +0000325 /*--------------------- ppc32/64 ---------------------*/
sewardj75ea7982005-11-14 15:18:25 +0000326
sewardjacaec5f2005-08-19 16:02:59 +0000327 /* fp is %r1. ip is %cia. Note, ppc uses r1 as both the stack and
328 frame pointers. */
329
sewardj15338c52006-10-17 01:31:58 +0000330# 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)
sewardjcfb5e2b2006-01-19 03:47:30 +0000339 /* Deal with bogus LR values caused by function
sewardj15338c52006-10-17 01:31:58 +0000340 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)
sewardjcfb5e2b2006-01-19 03:47:30 +0000343 && VG_(is_valid_tid)(tid_if_known)) {
sewardj15338c52006-10-17 01:31:58 +0000344 Word hsp = VG_(threads)[tid_if_known].arch.vex.guest_REDIR_SP;
345 redirs_used++;
346 if (hsp >= 1 && hsp < redir_stack_size)
sewardjcfb5e2b2006-01-19 03:47:30 +0000347 lr = VG_(threads)[tid_if_known]
348 .arch.vex.guest_REDIR_STACK[hsp-1];
349 }
350# endif
351
sewardj15338c52006-10-17 01:31:58 +0000352 /* 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. */
sewardjacaec5f2005-08-19 16:02:59 +0000361 lr_is_first_RA = False;
362 {
363# define M_VG_ERRTXT 1000
364 UChar buf_lr[M_VG_ERRTXT], buf_ip[M_VG_ERRTXT];
sewardjb1ae15d2008-12-12 13:23:03 +0000365 /* The following conditional looks grossly inefficient and
366 surely could be majorly improved, with not much effort. */
njn6b7611b2009-02-11 06:06:10 +0000367 if (VG_(get_fnname_raw) (lr, buf_lr, M_VG_ERRTXT))
368 if (VG_(get_fnname_raw) (ip, buf_ip, M_VG_ERRTXT))
sewardjacaec5f2005-08-19 16:02:59 +0000369 if (VG_(strncmp)(buf_lr, buf_ip, M_VG_ERRTXT))
370 lr_is_first_RA = True;
371# undef M_VG_ERRTXT
372 }
373
sewardjb8b79ad2008-03-03 01:35:41 +0000374 if (sps) sps[0] = fp; /* NB. not sp */
375 if (fps) fps[0] = fp;
sewardjacaec5f2005-08-19 16:02:59 +0000376 ips[0] = ip;
377 i = 1;
sewardjacaec5f2005-08-19 16:02:59 +0000378
sewardjdb2ac812005-12-23 23:33:51 +0000379 if (fp_min <= fp && fp < fp_max-VG_WORDSIZE+1) {
sewardjacaec5f2005-08-19 16:02:59 +0000380
sewardj525e2322005-11-13 02:41:35 +0000381 /* initial FP is sane; keep going */
382 fp = (((UWord*)fp)[0]);
383
384 while (True) {
385
sewardj15338c52006-10-17 01:31:58 +0000386 /* 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)
sewardjdfbaa222006-01-18 04:25:20 +0000391 const Int lr_offset = 2;
392# else
393 const Int lr_offset = 1;
394# endif
sewardjdb2ac812005-12-23 23:33:51 +0000395
njn3a4b58f2009-05-07 23:08:10 +0000396 if (i >= max_n_ips)
sewardj525e2322005-11-13 02:41:35 +0000397 break;
398
399 /* Try to derive a new (ip,fp) pair from the current set. */
400
sewardjf98e1c02008-10-25 16:22:41 +0000401 if (fp_min <= fp && fp <= fp_max - lr_offset * sizeof(UWord)) {
sewardj525e2322005-11-13 02:41:35 +0000402 /* fp looks sane, so use it. */
403
404 if (i == 1 && lr_is_first_RA)
405 ip = lr;
406 else
sewardjdb2ac812005-12-23 23:33:51 +0000407 ip = (((UWord*)fp)[lr_offset]);
sewardj525e2322005-11-13 02:41:35 +0000408
sewardj15338c52006-10-17 01:31:58 +0000409# if defined(VG_PLAT_USES_PPCTOC)
sewardjdfbaa222006-01-18 04:25:20 +0000410 /* Nasty hack to do with function replacement/wrapping on
sewardj15338c52006-10-17 01:31:58 +0000411 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)
sewardjdfbaa222006-01-18 04:25:20 +0000421 && VG_(is_valid_tid)(tid_if_known)) {
sewardjb8b79ad2008-03-03 01:35:41 +0000422 Word hsp = VG_(threads)[tid_if_known]
423 .arch.vex.guest_REDIR_SP;
sewardj15338c52006-10-17 01:31:58 +0000424 hsp -= 2 * redirs_used;
425 redirs_used ++;
426 if (hsp >= 1 && hsp < redir_stack_size)
sewardjdfbaa222006-01-18 04:25:20 +0000427 ip = VG_(threads)[tid_if_known]
428 .arch.vex.guest_REDIR_STACK[hsp-1];
429 }
430# endif
431
njn3a4b58f2009-05-07 23:08:10 +0000432 if (0 == ip || 1 == ip) break;
sewardj525e2322005-11-13 02:41:35 +0000433 fp = (((UWord*)fp)[0]);
sewardjb8b79ad2008-03-03 01:35:41 +0000434 if (sps) sps[i] = fp; /* NB. not sp */
435 if (fps) fps[i] = fp;
sewardjb1ae15d2008-12-12 13:23:03 +0000436 ips[i++] = ip - 1; /* -1: refer to calling insn, not the RA */
sewardj525e2322005-11-13 02:41:35 +0000437 if (debug)
barta0b6b2c2008-07-07 06:49:24 +0000438 VG_(printf)(" ipsF[%d]=%#08lx\n", i-1, ips[i-1]);
sewardjb1ae15d2008-12-12 13:23:03 +0000439 ip = ip - 1; /* ip is probably dead at this point, but
440 play safe, a la x86/amd64 above. See
441 extensive comments above. */
sewardj525e2322005-11-13 02:41:35 +0000442 continue;
443 }
444
445 /* No luck there. We have to give up. */
sewardjacaec5f2005-08-19 16:02:59 +0000446 break;
sewardjacaec5f2005-08-19 16:02:59 +0000447 }
sewardjacaec5f2005-08-19 16:02:59 +0000448 }
449
450# else
451# error "Unknown platform"
452# endif
453
njnd01fef72005-03-25 23:35:48 +0000454 n_found = i;
njnd01fef72005-03-25 23:35:48 +0000455 return n_found;
456}
457
sewardjb8b79ad2008-03-03 01:35:41 +0000458UInt VG_(get_StackTrace) ( ThreadId tid,
njn3a4b58f2009-05-07 23:08:10 +0000459 /*OUT*/StackTrace ips, UInt max_n_ips,
sewardjb8b79ad2008-03-03 01:35:41 +0000460 /*OUT*/StackTrace sps,
461 /*OUT*/StackTrace fps,
sewardj39f34232007-11-09 23:02:28 +0000462 Word first_ip_delta )
njnd01fef72005-03-25 23:35:48 +0000463{
464 /* thread in thread table */
njnf536bbb2005-06-13 04:21:38 +0000465 Addr ip = VG_(get_IP)(tid);
466 Addr fp = VG_(get_FP)(tid);
467 Addr sp = VG_(get_SP)(tid);
sewardjacaec5f2005-08-19 16:02:59 +0000468 Addr lr = VG_(get_LR)(tid);
njnf536bbb2005-06-13 04:21:38 +0000469 Addr stack_highest_word = VG_(threads)[tid].client_stack_highest_word;
tom690c3c82008-02-08 15:17:07 +0000470 Addr stack_lowest_word = 0;
njnd01fef72005-03-25 23:35:48 +0000471
sewardjb9bce632005-06-21 01:41:34 +0000472# if defined(VGP_x86_linux)
sewardja672ea32006-04-29 18:03:14 +0000473 /* 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
sewardj45f4e7c2005-09-27 19:20:21 +0000493 && VG_(am_is_valid_for_client)(sp, sizeof(Addr), VKI_PROT_READ)) {
njnd01fef72005-03-25 23:35:48 +0000494 ip = *(Addr *)sp;
495 sp += sizeof(Addr);
496 }
sewardjb9bce632005-06-21 01:41:34 +0000497# endif
498
tom690c3c82008-02-08 15:17:07 +0000499 /* See if we can get a better idea of the stack limits */
500 VG_(stack_limits)(sp, &stack_lowest_word, &stack_highest_word);
501
sewardj39f34232007-11-09 23:02:28 +0000502 /* Take into account the first_ip_delta. */
503 vg_assert( sizeof(Addr) == sizeof(Word) );
504 ip += first_ip_delta;
505
njnd01fef72005-03-25 23:35:48 +0000506 if (0)
sewardjb8b79ad2008-03-03 01:35:41 +0000507 VG_(printf)("tid %d: stack_highest=0x%08lx ip=0x%08lx "
508 "sp=0x%08lx fp=0x%08lx\n",
njnd01fef72005-03-25 23:35:48 +0000509 tid, stack_highest_word, ip, sp, fp);
510
njn3a4b58f2009-05-07 23:08:10 +0000511 return VG_(get_StackTrace_wrk)(tid, ips, max_n_ips,
sewardjb8b79ad2008-03-03 01:35:41 +0000512 sps, fps,
513 ip, sp, fp, lr, sp,
514 stack_highest_word);
njnd01fef72005-03-25 23:35:48 +0000515}
516
517static void printIpDesc(UInt n, Addr ip)
518{
njn83f9e792005-06-11 05:04:09 +0000519 #define BUF_LEN 4096
520
521 static UChar buf[BUF_LEN];
njnd01fef72005-03-25 23:35:48 +0000522
njn83f9e792005-06-11 05:04:09 +0000523 VG_(describe_IP)(ip, buf, BUF_LEN);
sewardj71bc3cb2005-05-19 00:25:45 +0000524
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 }
njnd01fef72005-03-25 23:35:48 +0000530}
531
532/* Print a StackTrace. */
533void VG_(pp_StackTrace) ( StackTrace ips, UInt n_ips )
534{
535 vg_assert( n_ips > 0 );
sewardj71bc3cb2005-05-19 00:25:45 +0000536
537 if (VG_(clo_xml))
538 VG_(message)(Vg_UserMsg, " <stack>");
539
njnd01fef72005-03-25 23:35:48 +0000540 VG_(apply_StackTrace)( printIpDesc, ips, n_ips );
sewardj71bc3cb2005-05-19 00:25:45 +0000541
542 if (VG_(clo_xml))
543 VG_(message)(Vg_UserMsg, " </stack>");
njnd01fef72005-03-25 23:35:48 +0000544}
545
546/* Get and immediately print a StackTrace. */
njn3a4b58f2009-05-07 23:08:10 +0000547void VG_(get_and_pp_StackTrace) ( ThreadId tid, UInt max_n_ips )
njnd01fef72005-03-25 23:35:48 +0000548{
njn3a4b58f2009-05-07 23:08:10 +0000549 Addr ips[max_n_ips];
550 UInt n_ips
551 = VG_(get_StackTrace)(tid, ips, max_n_ips,
sewardjb8b79ad2008-03-03 01:35:41 +0000552 NULL/*array to dump SP values in*/,
553 NULL/*array to dump FP values in*/,
554 0/*first_ip_delta*/);
njn3a4b58f2009-05-07 23:08:10 +0000555 VG_(pp_StackTrace)(ips, n_ips);
njnd01fef72005-03-25 23:35:48 +0000556}
557
njnd01fef72005-03-25 23:35:48 +0000558void VG_(apply_StackTrace)( void(*action)(UInt n, Addr ip),
559 StackTrace ips, UInt n_ips )
560{
njnd01fef72005-03-25 23:35:48 +0000561 Bool main_done = False;
njnd01fef72005-03-25 23:35:48 +0000562 Int i = 0;
563
564 vg_assert(n_ips > 0);
565 do {
566 Addr ip = ips[i];
njnd01fef72005-03-25 23:35:48 +0000567
sewardj73cf4c62005-11-17 15:12:34 +0000568 // 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)
njn68824432009-02-10 06:48:00 +0000571 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) {
njnd01fef72005-03-25 23:35:48 +0000574 main_done = True;
njn68824432009-02-10 06:48:00 +0000575 }
njnd01fef72005-03-25 23:35:48 +0000576 }
577
578 // Act on the ip
579 action(i, ip);
580
581 i++;
njn3a4b58f2009-05-07 23:08:10 +0000582 } while (i < n_ips && !main_done);
njnd01fef72005-03-25 23:35:48 +0000583
584 #undef MYBUF_LEN
585}
586
587
588/*--------------------------------------------------------------------*/
njn24a6efb2005-06-20 03:36:51 +0000589/*--- end ---*/
njnd01fef72005-03-25 23:35:48 +0000590/*--------------------------------------------------------------------*/