2 /*--------------------------------------------------------------------*/
3 /*--- Take snapshots of client stacks. m_stacktrace.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2015 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_threadstate.h"
34 #include "pub_core_debuginfo.h" // XXX: circular dependency
35 #include "pub_core_aspacemgr.h" // For VG_(is_addressable)()
36 #include "pub_core_libcbase.h"
37 #include "pub_core_libcassert.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_machine.h"
40 #include "pub_core_options.h"
41 #include "pub_core_stacks.h" // VG_(stack_limits)
42 #include "pub_core_stacktrace.h"
43 #include "pub_core_syswrap.h" // VG_(is_in_syscall)
44 #include "pub_core_xarray.h"
45 #include "pub_core_clientstate.h" // VG_(client__dl_sysinfo_int80)
46 #include "pub_core_trampoline.h"
49 /*------------------------------------------------------------*/
51 /*--- BEGIN platform-dependent unwinder worker functions ---*/
53 /*------------------------------------------------------------*/
55 /* Take a snapshot of the client's stack, putting up to 'max_n_ips'
56 IPs into 'ips'. In order to be thread-safe, we pass in the
57 thread's IP SP, FP if that's meaningful, and LR if that's
58 meaningful. Returns number of IPs put in 'ips'.
60 If you know what the thread ID for this stack is, send that as the
61 first parameter, else send zero. This helps generate better stack
62 traces on ppc64-linux and has no effect on other platforms.
65 /* Do frame merging in the _i frames in _ips array of recursive cycles
66 of up to _nframes. The merge is done during stack unwinding
67 (i.e. in platform specific unwinders) to collect as many
68 "interesting" stack traces as possible. */
69 #define RECURSIVE_MERGE(_nframes,_ips,_i) if (UNLIKELY(_nframes > 0)) \
72 for (dist = 1; dist <= _nframes && dist < (Int)_i; dist++) { \
73 if (_ips[_i-1] == _ips[_i-1-dist]) { \
80 /* Note about calculation of fp_min : fp_min is the lowest address
81 which can be accessed during unwinding. This is SP - VG_STACK_REDZONE_SZB.
82 On most platforms, this will be equal to SP (as VG_STACK_REDZONE_SZB
83 is 0). However, on some platforms (e.g. amd64), there is an accessible
84 redzone below the SP. Some CFI unwind info are generated, taking this
85 into account. As an example, the following is a CFI unwind info on
86 amd64 found for a 'retq' instruction:
87 [0x400f7e .. 0x400f7e]: let cfa=oldSP+8 in RA=*(cfa+-8) SP=cfa+0 BP=*(cfa+-16)
89 As you can see, the previous BP is found 16 bytes below the cfa, which
90 is the oldSP+8. So, effectively, the BP is found 8 bytes below the SP.
91 The fp_min must take this into account, otherwise, VG_(use_CF_info) will
94 /* ------------------------ x86 ------------------------- */
96 #if defined(VGP_x86_linux) || defined(VGP_x86_darwin) \
97 || defined(VGP_x86_solaris)
99 #define N_FP_CF_VERIF 1021
100 // prime number so that size of fp_CF_verif is just below 4K or 8K
101 // Note that this prime nr differs from the one chosen in
102 // m_debuginfo/debuginfo.c for the cfsi cache : in case we have
103 // a collision here between two IPs, we expect to not (often) have the
104 // same collision in the cfsi cache (and vice-versa).
106 // unwinding with fp chain is ok:
108 // there is no CFI info for this IP:
110 // Unwind with FP is not ok, must use CF unwind:
113 static Addr fp_CF_verif_cache
[N_FP_CF_VERIF
];
115 /* An unwind done by following the fp chain technique can be incorrect
116 as not all frames are respecting the standard bp/sp ABI.
117 The CF information is now generated by default by gcc
118 (as part of the dwarf info). However, unwinding using CF information
119 is significantly slower : a slowdown of 20% has been observed
120 on an helgrind test case.
121 So, by default, the unwinding will be done using the fp chain.
122 But before accepting to unwind an IP with fp_chain, the result
123 of the unwind will be checked with the CF information.
124 This check can give 3 results:
125 FPUNWIND (0): there is CF info, and it gives the same result as fp unwind.
126 => it is assumed that future unwind for this IP can be done
127 with the fast fp chain, without further CF checking
128 NOINFO (1): there is no CF info (so, fp unwind is the only do-able thing)
129 CFUNWIND (2): there is CF info, but unwind result differs.
130 => it is assumed that future unwind for this IP must be done
132 Of course, if each fp unwind implies a check done with a CF unwind,
133 it would just be slower => we cache the check result in an
134 array of checked Addr.
135 The check for an IP will be stored at
136 fp_CF_verif_cache[IP % N_FP_CF_VERIF] as one of:
141 Note: we can re-use the last (ROUNDDOWN (log (N_FP_CF_VERIF))) bits
142 to store the check result, as they are guaranteed to be non significant
143 in the comparison between 2 IPs stored in fp_CF_verif_cache).
144 In other words, if two IPs are only differing on the last 2 bits,
145 then they will not land in the same cache bucket.
148 /* cached result of VG_(FPO_info_present)(). Refreshed each time
149 the fp_CF_verif_generation is different of the current debuginfo
151 static Bool FPO_info_present
= False
;
153 static UInt fp_CF_verif_generation
= 0;
154 // Our cache has to be maintained in sync with the CFI cache.
155 // Each time the debuginfo is changed, its generation will be incremented.
156 // We will clear our cache when our saved generation differs from
157 // the debuginfo generation.
159 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
160 /*OUT*/Addr
* ips
, UInt max_n_ips
,
161 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
162 const UnwindStartRegs
* startRegs
,
165 const Bool do_stats
= False
; // compute and output some stats regularly.
167 UInt nr
; // nr of stacktraces computed
168 UInt nf
; // nr of frames computed
169 UInt Ca
; // unwind for which cache indicates CFUnwind must be used.
170 UInt FF
; // unwind for which cache indicates FPUnwind can be used.
171 UInt Cf
; // unwind at end of stack+store CFUNWIND (xip not end of stack).
172 UInt Fw
; // unwind at end of stack+store FPUNWIND
173 UInt FO
; // unwind + store FPUNWIND
174 UInt CF
; // unwind + store CFUNWIND. Details below.
175 UInt xi
; UInt xs
; UInt xb
; // register(s) which caused a 'store CFUNWIND'.
176 UInt Ck
; // unwind fp invalid+store FPUNWIND
177 UInt MS
; // microsoft unwind
180 const Bool debug
= False
;
181 // = VG_(debugLog_getLevel) () > 3;
183 // = stats.nr >= 123456;
184 const HChar
* unwind_case
; // used when debug is True.
185 // Debugging this function is not straightforward.
186 // Here is the easiest way I have found:
187 // 1. Change the above to True.
188 // 2. Start your program under Valgrind with --tool=none --vgdb-error=0
189 // 3. Use GDB/vgdb to put a breakpoint where you want to debug the stacktrace
190 // 4. Continue till breakpoint is encountered
191 // 5. From GDB, use 'monitor v.info scheduler' and examine the unwind traces.
192 // You might have to do twice 'monitor v.info scheduler' to see
193 // the effect of caching the results of the verification.
194 // You can also modify the debug dynamically using by using
195 // 'monitor v.set debuglog 4.
200 const Int cmrf
= VG_(clo_merge_recursive_frames
);
202 vg_assert(sizeof(Addr
) == sizeof(UWord
));
203 vg_assert(sizeof(Addr
) == sizeof(void*));
205 D3UnwindRegs fpverif_uregs
; // result of CF unwind for a check reason.
206 Addr xip_verified
= 0; // xip for which we have calculated fpverif_uregs
207 // 0 assigned to silence false positive -Wuninitialized warning
208 // This is a false positive as xip_verified is assigned when
209 // xip_verif > CFUNWIND and only used if xip_verif > CFUNWIND.
212 uregs
.xip
= (Addr
)startRegs
->r_pc
;
213 uregs
.xsp
= (Addr
)startRegs
->r_sp
;
214 uregs
.xbp
= startRegs
->misc
.X86
.r_ebp
;
215 Addr fp_min
= uregs
.xsp
- VG_STACK_REDZONE_SZB
;
217 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
218 stopping when the trail goes cold, which we guess to be
219 when FP is not a reasonable stack location. */
221 // JRS 2002-sep-17: hack, to round up fp_max to the end of the
222 // current page, at least. Dunno if it helps.
223 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
224 fp_max
= VG_PGROUNDUP(fp_max_orig
);
225 if (fp_max
>= sizeof(Addr
))
226 fp_max
-= sizeof(Addr
);
229 VG_(printf
)("max_n_ips=%u fp_min=0x%08lx fp_max_orig=0x08%lx, "
230 "fp_max=0x%08lx ip=0x%08lx fp=0x%08lx\n",
231 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
232 uregs
.xip
, uregs
.xbp
);
234 /* Assertion broken before main() is reached in pthreaded programs; the
235 * offending stack traces only have one item. --njn, 2002-aug-16 */
236 /* vg_assert(fp_min <= fp_max);*/
237 // On Darwin, this kicks in for pthread-related stack traces, so they're
238 // only 1 entry long which is wrong.
239 # if defined(VGO_linux)
240 if (fp_min
+ 512 >= fp_max
) {
241 /* If the stack limits look bogus, don't poke around ... but
242 don't bomb out either. */
243 # elif defined(VGO_solaris)
245 /* VG_(get_StackTrace)() can be called by tools very early when
246 various tracing options are enabled. Don't proceed further
247 if the stack limits look bogus.
250 # if defined(VGO_linux) || defined(VGO_solaris)
251 if (sps
) sps
[0] = uregs
.xsp
;
252 if (fps
) fps
[0] = uregs
.xbp
;
258 if (UNLIKELY (fp_CF_verif_generation
!= VG_(debuginfo_generation
)())) {
259 fp_CF_verif_generation
= VG_(debuginfo_generation
)();
260 VG_(memset
)(&fp_CF_verif_cache
, 0, sizeof(fp_CF_verif_cache
));
261 FPO_info_present
= VG_(FPO_info_present
)();
265 /* Loop unwinding the stack. Note that the IP value we get on
266 * each pass (whether from CFI info or a stack frame) is a
267 * return address so is actually after the calling instruction
268 * in the calling function.
270 * Because of this we subtract one from the IP after each pass
271 * of the loop so that we find the right CFI block on the next
272 * pass - otherwise we can find the wrong CFI info if it happens
273 * to change after the calling instruction and that will mean
274 * that we will fail to unwind the next step.
276 * This most frequently happens at the end of a function when
277 * a tail call occurs and we wind up using the CFI info for the
278 * next function which is completely wrong.
280 if (sps
) sps
[0] = uregs
.xsp
;
281 if (fps
) fps
[0] = uregs
.xbp
;
284 if (do_stats
) stats
.nr
++;
291 UWord hash
= uregs
.xip
% N_FP_CF_VERIF
;
292 Addr xip_verif
= uregs
.xip
^ fp_CF_verif_cache
[hash
];
294 VG_(printf
)(" uregs.xip 0x%08lx xip_verif[0x%08lx]"
295 " xbp 0x%08lx xsp 0x%08lx\n",
296 uregs
.xip
, xip_verif
,
297 uregs
.xbp
, uregs
.xsp
);
298 // If xip is in cache, then xip_verif will be <= CFUNWIND.
299 // Otherwise, if not in cache, xip_verif will be > CFUNWIND.
301 /* Try to derive a new (ip,sp,fp) triple from the current set. */
303 /* Do we have to do CFI unwinding ?
304 We do CFI unwinding if one of the following condition holds:
305 a. fp_CF_verif_cache contains xip but indicates CFUNWIND must
306 be done (i.e. fp unwind check failed when we did the first
308 b. fp_CF_verif_cache does not contain xip.
309 We will try CFI unwinding in fpverif_uregs and compare with
310 FP unwind result to insert xip in the cache with the correct
312 if (UNLIKELY(xip_verif
>= CFUNWIND
)) {
313 if (xip_verif
== CFUNWIND
) {
314 /* case a : do "real" cfi unwind */
315 if ( VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
) ) {
316 if (debug
) unwind_case
= "Ca";
317 if (do_stats
) stats
.Ca
++;
320 /* ??? cache indicates we have to do CFI unwind (so, we
321 previously found CFI info, and failed the fp unwind
322 check). Now, we just failed with CFI. So, once we
323 succeed, once we fail. No idea what is going on =>
324 cleanup the cache entry and fallover to fp unwind (this
326 fp_CF_verif_cache
[hash
] = 0;
327 if (debug
) VG_(printf
)(" cache reset as CFI ok then nok\n");
331 /* case b : do "verif" cfi unwind in fpverif_uregs */
332 fpverif_uregs
= uregs
;
333 xip_verified
= uregs
.xip
;
334 if ( !VG_(use_CF_info
)( &fpverif_uregs
, fp_min
, fp_max
) ) {
335 fp_CF_verif_cache
[hash
] = uregs
.xip
^ NOINFO
;
336 if (debug
) VG_(printf
)(" cache NOINFO fpverif_uregs\n");
342 /* On x86, try the old-fashioned method of following the
343 %ebp-chain. This can be done if the fp_CF_verif_cache for xip
344 indicate fp unwind is ok. This must be done if the cache indicates
345 there is no info. This is also done to confirm what to put in the cache
346 if xip was not in the cache. */
347 /* This deals with frames resulting from functions which begin "pushl%
348 ebp ; movl %esp, %ebp" which is the ABI-mandated preamble. */
349 if (fp_min
<= uregs
.xbp
&&
350 uregs
.xbp
<= fp_max
- 1 * sizeof(UWord
)/*see comment below*/ &&
351 VG_IS_4_ALIGNED(uregs
.xbp
))
355 /* fp looks sane, so use it. */
356 uregs
.xip
= (((UWord
*)uregs
.xbp
)[1]);
357 // We stop if we hit a zero (the traditional end-of-stack
358 // marker) or a one -- these correspond to recorded IPs of 0 or -1.
359 // The latter because r8818 (in this file) changes the meaning of
360 // entries [1] and above in a stack trace, by subtracting 1 from
361 // them. Hence stacks that used to end with a zero value now end in
362 // -1 and so we must detect that too.
363 if (0 == uregs
.xip
|| 1 == uregs
.xip
) {
364 if (xip_verif
> CFUNWIND
) {
365 // Check if we obtain the same result with fp unwind.
366 // If same result, then mark xip as fp unwindable
367 if (uregs
.xip
== fpverif_uregs
.xip
) {
368 fp_CF_verif_cache
[hash
] = xip_verified
^ FPUNWIND
;
369 if (debug
) VG_(printf
)(" cache FPUNWIND 0\n");
371 if (do_stats
) stats
.Fw
++;
374 fp_CF_verif_cache
[hash
] = xip_verified
^ CFUNWIND
;
375 uregs
= fpverif_uregs
;
376 if (debug
) VG_(printf
)(" cache CFUNWIND 0\n");
378 if (do_stats
) stats
.Cf
++;
382 // end of stack => out of the loop.
388 uregs
.xsp
= uregs
.xbp
+ sizeof(Addr
) /*saved %ebp*/
389 + sizeof(Addr
) /*ra*/;
390 uregs
.xbp
= (((UWord
*)uregs
.xbp
)[0]);
391 if (xip_verif
> CFUNWIND
) {
392 if (uregs
.xip
== fpverif_uregs
.xip
393 && uregs
.xsp
== fpverif_uregs
.xsp
394 && uregs
.xbp
== fpverif_uregs
.xbp
) {
395 fp_CF_verif_cache
[hash
] = xip_verified
^ FPUNWIND
;
396 if (debug
) VG_(printf
)(" cache FPUNWIND >2\n");
397 if (debug
) unwind_case
= "FO";
398 if (do_stats
) stats
.FO
++;
399 if (old_xsp
>= uregs
.xsp
) {
401 VG_(printf
) (" FO end of stack old_xsp %p >= xsp %p\n",
402 (void*)old_xsp
, (void*)uregs
.xsp
);
406 fp_CF_verif_cache
[hash
] = xip_verified
^ CFUNWIND
;
407 if (debug
) VG_(printf
)(" cache CFUNWIND >2\n");
408 if (do_stats
&& uregs
.xip
!= fpverif_uregs
.xip
) stats
.xi
++;
409 if (do_stats
&& uregs
.xsp
!= fpverif_uregs
.xsp
) stats
.xs
++;
410 if (do_stats
&& uregs
.xbp
!= fpverif_uregs
.xbp
) stats
.xb
++;
411 uregs
= fpverif_uregs
;
412 if (debug
) unwind_case
= "CF";
413 if (do_stats
) stats
.CF
++;
416 if (debug
) unwind_case
= "FF";
417 if (do_stats
) stats
.FF
++;
418 if (old_xsp
>= uregs
.xsp
) {
420 VG_(printf
) (" FF end of stack old_xsp %p >= xsp %p\n",
421 (void*)old_xsp
, (void*)uregs
.xsp
);
427 // fp unwind has failed.
428 // If we were checking the validity of the cfi unwinding,
429 // we mark in the cache that the fp unwind cannot be done, and that
430 // cfi unwind is desired.
431 if (xip_verif
> CFUNWIND
) {
432 // We know that fpverif_uregs contains valid information,
433 // as a failed cf unwind would have put NOINFO in xip_verif.
434 fp_CF_verif_cache
[hash
] = xip_verified
^ CFUNWIND
;
435 if (debug
) VG_(printf
)(" cache CFUNWIND as fp failed\n");
436 uregs
= fpverif_uregs
;
437 if (debug
) unwind_case
= "Ck";
438 if (do_stats
) stats
.Ck
++;
441 // xip_verif is FPUNWIND or NOINFO.
442 // We failed the cfi unwind and/or the fp unwind.
443 // => fallback to FPO info.
446 /* And, similarly, try for MSVC FPO unwind info. */
448 && VG_(use_FPO_info
)( &uregs
.xip
, &uregs
.xsp
, &uregs
.xbp
,
450 if (debug
) unwind_case
= "MS";
451 if (do_stats
) stats
.MS
++;
455 /* No luck. We have to give up. */
459 /* Add a frame in ips/sps/fps */
460 /* fp is %ebp. sp is %esp. ip is %eip. */
461 if (0 == uregs
.xip
|| 1 == uregs
.xip
) break;
462 if (sps
) sps
[i
] = uregs
.xsp
;
463 if (fps
) fps
[i
] = uregs
.xbp
;
464 ips
[i
++] = uregs
.xip
- 1;
465 /* -1: refer to calling insn, not the RA */
467 VG_(printf
)(" ips%s[%d]=0x%08lx\n", unwind_case
, i
-1, ips
[i
-1]);
468 uregs
.xip
= uregs
.xip
- 1;
469 /* as per comment at the head of this loop */
470 RECURSIVE_MERGE(cmrf
,ips
,i
);
473 if (do_stats
) stats
.nf
+= i
;
474 if (do_stats
&& stats
.nr
% 10000 == 0) {
475 VG_(printf
)("nr %u nf %u "
479 "CF %u (xi %u xs %u xb %u) "
485 stats
.CF
, stats
.xi
, stats
.xs
, stats
.xb
,
499 /* ----------------------- amd64 ------------------------ */
501 #if defined(VGP_amd64_linux) || defined(VGP_amd64_darwin) \
502 || defined(VGP_amd64_solaris)
504 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
505 /*OUT*/Addr
* ips
, UInt max_n_ips
,
506 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
507 const UnwindStartRegs
* startRegs
,
510 const Bool debug
= False
;
514 const Int cmrf
= VG_(clo_merge_recursive_frames
);
516 vg_assert(sizeof(Addr
) == sizeof(UWord
));
517 vg_assert(sizeof(Addr
) == sizeof(void*));
520 uregs
.xip
= startRegs
->r_pc
;
521 uregs
.xsp
= startRegs
->r_sp
;
522 uregs
.xbp
= startRegs
->misc
.AMD64
.r_rbp
;
523 Addr fp_min
= uregs
.xsp
- VG_STACK_REDZONE_SZB
;
525 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
526 stopping when the trail goes cold, which we guess to be
527 when FP is not a reasonable stack location. */
529 // JRS 2002-sep-17: hack, to round up fp_max to the end of the
530 // current page, at least. Dunno if it helps.
531 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
532 fp_max
= VG_PGROUNDUP(fp_max_orig
);
533 if (fp_max
>= sizeof(Addr
))
534 fp_max
-= sizeof(Addr
);
537 VG_(printf
)("max_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
538 "fp_max=0x%lx ip=0x%lx fp=0x%lx\n",
539 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
540 uregs
.xip
, uregs
.xbp
);
542 /* Assertion broken before main() is reached in pthreaded programs; the
543 * offending stack traces only have one item. --njn, 2002-aug-16 */
544 /* vg_assert(fp_min <= fp_max);*/
545 // On Darwin, this kicks in for pthread-related stack traces, so they're
546 // only 1 entry long which is wrong.
547 # if defined(VGO_linux)
548 if (fp_min
+ 256 >= fp_max
) {
549 /* If the stack limits look bogus, don't poke around ... but
550 don't bomb out either. */
551 # elif defined(VGO_solaris)
553 /* VG_(get_StackTrace)() can be called by tools very early when
554 various tracing options are enabled. Don't proceed further
555 if the stack limits look bogus.
558 # if defined(VGO_linux) || defined(VGO_solaris)
560 if (sps
) sps
[0] = uregs
.xsp
;
561 if (fps
) fps
[0] = uregs
.xbp
;
567 /* fp is %rbp. sp is %rsp. ip is %rip. */
570 if (sps
) sps
[0] = uregs
.xsp
;
571 if (fps
) fps
[0] = uregs
.xbp
;
574 VG_(printf
)(" ipsS[%d]=%#08lx rbp %#08lx rsp %#08lx\n",
575 i
-1, ips
[i
-1], uregs
.xbp
, uregs
.xsp
);
577 # if defined(VGO_darwin)
578 if (VG_(is_valid_tid
)(tid_if_known
) &&
579 VG_(is_in_syscall
)(tid_if_known
) &&
581 /* On Darwin, all the system call stubs have no function
582 * prolog. So instead of top of the stack being a new
583 * frame comprising a saved BP and a return address, we
584 * just have the return address in the caller's frame.
585 * Adjust for this by recording the return address.
587 ips
[i
] = *(Addr
*)uregs
.xsp
- 1;
588 if (sps
) sps
[i
] = uregs
.xsp
;
589 if (fps
) fps
[i
] = uregs
.xbp
;
594 /* Loop unwinding the stack. Note that the IP value we get on
595 * each pass (whether from CFI info or a stack frame) is a
596 * return address so is actually after the calling instruction
597 * in the calling function.
599 * Because of this we subtract one from the IP after each pass
600 * of the loop so that we find the right CFI block on the next
601 * pass - otherwise we can find the wrong CFI info if it happens
602 * to change after the calling instruction and that will mean
603 * that we will fail to unwind the next step.
605 * This most frequently happens at the end of a function when
606 * a tail call occurs and we wind up using the CFI info for the
607 * next function which is completely wrong.
614 /* Try to derive a new (ip,sp,fp) triple from the current set. */
616 /* First off, see if there is any CFI info to hand which can
618 if ( VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
) ) {
619 if (0 == uregs
.xip
|| 1 == uregs
.xip
) break;
620 if (sps
) sps
[i
] = uregs
.xsp
;
621 if (fps
) fps
[i
] = uregs
.xbp
;
622 ips
[i
++] = uregs
.xip
- 1; /* -1: refer to calling insn, not the RA */
624 VG_(printf
)(" ipsC[%d]=%#08lx rbp %#08lx rsp %#08lx\n",
625 i
-1, ips
[i
-1], uregs
.xbp
, uregs
.xsp
);
626 uregs
.xip
= uregs
.xip
- 1; /* as per comment at the head of this loop */
627 RECURSIVE_MERGE(cmrf
,ips
,i
);
631 /* If VG_(use_CF_info) fails, it won't modify ip/sp/fp, so
632 we can safely try the old-fashioned method. */
633 /* This bit is supposed to deal with frames resulting from
634 functions which begin "pushq %rbp ; movq %rsp, %rbp".
635 Unfortunately, since we can't (easily) look at the insns at
636 the start of the fn, like GDB does, there's no reliable way
637 to tell. Hence the hack of first trying out CFI, and if that
638 fails, then use this as a fallback. */
639 /* Note: re "- 1 * sizeof(UWord)", need to take account of the
640 fact that we are prodding at & ((UWord*)fp)[1] and so need to
641 adjust the limit check accordingly. Omitting this has been
642 observed to cause segfaults on rare occasions. */
643 if (fp_min
<= uregs
.xbp
&& uregs
.xbp
<= fp_max
- 1 * sizeof(UWord
)) {
644 /* fp looks sane, so use it. */
645 uregs
.xip
= (((UWord
*)uregs
.xbp
)[1]);
646 if (0 == uregs
.xip
|| 1 == uregs
.xip
) break;
647 uregs
.xsp
= uregs
.xbp
+ sizeof(Addr
) /*saved %rbp*/
648 + sizeof(Addr
) /*ra*/;
649 uregs
.xbp
= (((UWord
*)uregs
.xbp
)[0]);
650 if (sps
) sps
[i
] = uregs
.xsp
;
651 if (fps
) fps
[i
] = uregs
.xbp
;
652 ips
[i
++] = uregs
.xip
- 1; /* -1: refer to calling insn, not the RA */
654 VG_(printf
)(" ipsF[%d]=%#08lx rbp %#08lx rsp %#08lx\n",
655 i
-1, ips
[i
-1], uregs
.xbp
, uregs
.xsp
);
656 uregs
.xip
= uregs
.xip
- 1; /* as per comment at the head of this loop */
657 RECURSIVE_MERGE(cmrf
,ips
,i
);
661 /* Last-ditch hack (evidently GDB does something similar). We
662 are in the middle of nowhere and we have a nonsense value for
663 the frame pointer. If the stack pointer is still valid,
664 assume that what it points at is a return address. Yes,
665 desperate measures. Could do better here:
666 - check that the supposed return address is in
668 - check that the supposed return address is just after a call insn
669 - given those two checks, don't just consider *sp as the return
670 address; instead scan a likely section of stack (eg sp .. sp+256)
671 and use suitable values found there.
673 if (fp_min
<= uregs
.xsp
&& uregs
.xsp
< fp_max
) {
674 uregs
.xip
= ((UWord
*)uregs
.xsp
)[0];
675 if (0 == uregs
.xip
|| 1 == uregs
.xip
) break;
676 if (sps
) sps
[i
] = uregs
.xsp
;
677 if (fps
) fps
[i
] = uregs
.xbp
;
678 ips
[i
++] = uregs
.xip
== 0
679 ? 0 /* sp[0] == 0 ==> stuck at the bottom of a
682 /* -1: refer to calling insn, not the RA */
684 VG_(printf
)(" ipsH[%d]=%#08lx\n", i
-1, ips
[i
-1]);
685 uregs
.xip
= uregs
.xip
- 1; /* as per comment at the head of this loop */
687 RECURSIVE_MERGE(cmrf
,ips
,i
);
691 /* No luck at all. We have to give up. */
701 /* -----------------------ppc32/64 ---------------------- */
703 #if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
704 || defined(VGP_ppc64le_linux)
706 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
707 /*OUT*/Addr
* ips
, UInt max_n_ips
,
708 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
709 const UnwindStartRegs
* startRegs
,
712 Bool lr_is_first_RA
= False
;
713 # if defined(VG_PLAT_USES_PPCTOC) || defined(VGP_ppc64le_linux)
714 Word redir_stack_size
= 0;
715 Word redirs_used
= 0;
717 const Int cmrf
= VG_(clo_merge_recursive_frames
);
724 vg_assert(sizeof(Addr
) == sizeof(UWord
));
725 vg_assert(sizeof(Addr
) == sizeof(void*));
727 Addr ip
= (Addr
)startRegs
->r_pc
;
728 Addr sp
= (Addr
)startRegs
->r_sp
;
730 # if defined(VGP_ppc32_linux)
731 Addr lr
= startRegs
->misc
.PPC32
.r_lr
;
732 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
733 Addr lr
= startRegs
->misc
.PPC64
.r_lr
;
735 Addr fp_min
= sp
- VG_STACK_REDZONE_SZB
;
737 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
738 stopping when the trail goes cold, which we guess to be
739 when FP is not a reasonable stack location. */
741 // JRS 2002-sep-17: hack, to round up fp_max to the end of the
742 // current page, at least. Dunno if it helps.
743 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
744 fp_max
= VG_PGROUNDUP(fp_max_orig
);
745 if (fp_max
>= sizeof(Addr
))
746 fp_max
-= sizeof(Addr
);
749 VG_(printf
)("max_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
750 "fp_max=0x%lx ip=0x%lx fp=0x%lx\n",
751 max_n_ips
, fp_min
, fp_max_orig
, fp_max
, ip
, fp
);
753 /* Assertion broken before main() is reached in pthreaded programs; the
754 * offending stack traces only have one item. --njn, 2002-aug-16 */
755 /* vg_assert(fp_min <= fp_max);*/
756 if (fp_min
+ 512 >= fp_max
) {
757 /* If the stack limits look bogus, don't poke around ... but
758 don't bomb out either. */
759 if (sps
) sps
[0] = sp
;
760 if (fps
) fps
[0] = fp
;
765 /* fp is %r1. ip is %cia. Note, ppc uses r1 as both the stack and
768 # if defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
769 redir_stack_size
= VEX_GUEST_PPC64_REDIR_STACK_SIZE
;
773 # if defined(VG_PLAT_USES_PPCTOC) || defined (VGP_ppc64le_linux)
774 /* Deal with bogus LR values caused by function
775 interception/wrapping on ppc-TOC platforms; see comment on
776 similar code a few lines further down. */
777 if (lr
== (Addr
)&VG_(ppctoc_magic_redirect_return_stub
)
778 && VG_(is_valid_tid
)(tid_if_known
)) {
779 Word hsp
= VG_(threads
)[tid_if_known
].arch
.vex
.guest_REDIR_SP
;
781 if (hsp
>= 1 && hsp
< redir_stack_size
)
782 lr
= VG_(threads
)[tid_if_known
]
783 .arch
.vex
.guest_REDIR_STACK
[hsp
-1];
787 /* We have to determine whether or not LR currently holds this fn
788 (call it F)'s return address. It might not if F has previously
789 called some other function, hence overwriting LR with a pointer
790 to some part of F. Hence if LR and IP point to the same
791 function then we conclude LR does not hold this function's
792 return address; instead the LR at entry must have been saved in
793 the stack by F's prologue and so we must get it from there
794 instead. Note all this guff only applies to the innermost
796 lr_is_first_RA
= False
;
798 const HChar
*buf_lr
, *buf_ip
;
799 /* The following conditional looks grossly inefficient and
800 surely could be majorly improved, with not much effort. */
801 if (VG_(get_fnname_raw
) (lr
, &buf_lr
)) {
802 HChar buf_lr_copy
[VG_(strlen
)(buf_lr
) + 1];
803 VG_(strcpy
)(buf_lr_copy
, buf_lr
);
804 if (VG_(get_fnname_raw
) (ip
, &buf_ip
))
805 if (VG_(strcmp
)(buf_lr_copy
, buf_ip
))
806 lr_is_first_RA
= True
;
810 if (sps
) sps
[0] = fp
; /* NB. not sp */
811 if (fps
) fps
[0] = fp
;
815 if (fp_min
<= fp
&& fp
< fp_max
-VG_WORDSIZE
+1) {
817 /* initial FP is sane; keep going */
818 fp
= (((UWord
*)fp
)[0]);
822 /* On ppc64-linux (ppc64-elf, really), the lr save
823 slot is 2 words back from sp, whereas on ppc32-elf(?) it's
824 only one word back. */
825 # if defined(VG_PLAT_USES_PPCTOC) || defined(VGP_ppc64le_linux)
826 const Int lr_offset
= 2;
828 const Int lr_offset
= 1;
834 /* Try to derive a new (ip,fp) pair from the current set. */
836 if (fp_min
<= fp
&& fp
<= fp_max
- lr_offset
* sizeof(UWord
)) {
837 /* fp looks sane, so use it. */
839 if (i
== 1 && lr_is_first_RA
)
842 ip
= (((UWord
*)fp
)[lr_offset
]);
844 # if defined(VG_PLAT_USES_PPCTOC) || defined(VGP_ppc64le_linux)
845 /* Nasty hack to do with function replacement/wrapping on
846 ppc64-linux. If LR points to our magic return stub,
847 then we are in a wrapped or intercepted function, in
848 which LR has been messed with. The original LR will
849 have been pushed onto the thread's hidden REDIR stack
850 one down from the top (top element is the saved R2) and
851 so we should restore the value from there instead.
852 Since nested redirections can and do happen, we keep
853 track of the number of nested LRs used by the unwinding
854 so far with 'redirs_used'. */
855 if (ip
== (Addr
)&VG_(ppctoc_magic_redirect_return_stub
)
856 && VG_(is_valid_tid
)(tid_if_known
)) {
857 Word hsp
= VG_(threads
)[tid_if_known
]
858 .arch
.vex
.guest_REDIR_SP
;
859 hsp
-= 2 * redirs_used
;
861 if (hsp
>= 1 && hsp
< redir_stack_size
)
862 ip
= VG_(threads
)[tid_if_known
]
863 .arch
.vex
.guest_REDIR_STACK
[hsp
-1];
867 if (0 == ip
|| 1 == ip
) break;
868 if (sps
) sps
[i
] = fp
; /* NB. not sp */
869 if (fps
) fps
[i
] = fp
;
870 fp
= (((UWord
*)fp
)[0]);
871 ips
[i
++] = ip
- 1; /* -1: refer to calling insn, not the RA */
873 VG_(printf
)(" ipsF[%d]=%#08lx\n", i
-1, ips
[i
-1]);
874 ip
= ip
- 1; /* ip is probably dead at this point, but
875 play safe, a la x86/amd64 above. See
876 extensive comments above. */
877 RECURSIVE_MERGE(cmrf
,ips
,i
);
881 /* No luck there. We have to give up. */
892 /* ------------------------ arm ------------------------- */
894 #if defined(VGP_arm_linux)
896 static Bool
in_same_fn ( Addr a1
, Addr a2
)
898 const HChar
*buf_a1
, *buf_a2
;
899 /* The following conditional looks grossly inefficient and
900 surely could be majorly improved, with not much effort. */
901 if (VG_(get_fnname_raw
) (a1
, &buf_a1
)) {
902 HChar buf_a1_copy
[VG_(strlen
)(buf_a1
) + 1];
903 VG_(strcpy
)(buf_a1_copy
, buf_a1
);
904 if (VG_(get_fnname_raw
) (a2
, &buf_a2
))
905 if (VG_(strcmp
)(buf_a1_copy
, buf_a2
))
911 static Bool
in_same_page ( Addr a1
, Addr a2
) {
912 return (a1
& ~0xFFF) == (a2
& ~0xFFF);
915 static Addr
abs_diff ( Addr a1
, Addr a2
) {
916 return (Addr
)(a1
> a2
? a1
- a2
: a2
- a1
);
919 static Bool
has_XT_perms ( Addr a
)
921 NSegment
const* seg
= VG_(am_find_nsegment
)(a
);
922 return seg
&& seg
->hasX
&& seg
->hasT
;
925 static Bool
looks_like_Thumb_call32 ( UShort w0
, UShort w1
)
928 VG_(printf
)("isT32call %04x %04x\n", (UInt
)w0
, (UInt
)w1
);
930 if ((w0
& 0xF800) == 0xF000 && (w1
& 0xC000) == 0xC000) return True
;
932 if ((w0
& 0xF800) == 0xF000 && (w1
& 0xC000) == 0xC000) return True
;
936 static Bool
looks_like_Thumb_call16 ( UShort w0
)
941 static Bool
looks_like_ARM_call ( UInt a0
)
944 VG_(printf
)("isA32call %08x\n", a0
);
945 // Leading E forces unconditional only -- fix
946 if ((a0
& 0xFF000000) == 0xEB000000) return True
;
950 static Bool
looks_like_RA ( Addr ra
)
952 /* 'ra' is a plausible return address if it points to
953 an instruction after a call insn. */
956 // returning to Thumb code
959 if (has_XT_perms(ra
)) {
960 UShort w0
= *(UShort
*)ra
;
961 UShort w1
= in_same_page(ra
, ra
+2) ? *(UShort
*)(ra
+2) : 0;
962 if (looks_like_Thumb_call16(w1
) || looks_like_Thumb_call32(w0
,w1
))
969 if (has_XT_perms(ra
)) {
970 UInt a0
= *(UInt
*)ra
;
971 if (looks_like_ARM_call(a0
))
978 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
979 /*OUT*/Addr
* ips
, UInt max_n_ips
,
980 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
981 const UnwindStartRegs
* startRegs
,
988 const Int cmrf
= VG_(clo_merge_recursive_frames
);
990 vg_assert(sizeof(Addr
) == sizeof(UWord
));
991 vg_assert(sizeof(Addr
) == sizeof(void*));
994 uregs
.r15
= startRegs
->r_pc
& 0xFFFFFFFE;
995 uregs
.r14
= startRegs
->misc
.ARM
.r14
;
996 uregs
.r13
= startRegs
->r_sp
;
997 uregs
.r12
= startRegs
->misc
.ARM
.r12
;
998 uregs
.r11
= startRegs
->misc
.ARM
.r11
;
999 uregs
.r7
= startRegs
->misc
.ARM
.r7
;
1000 Addr fp_min
= uregs
.r13
- VG_STACK_REDZONE_SZB
;
1002 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
1003 stopping when the trail goes cold, which we guess to be
1004 when FP is not a reasonable stack location. */
1006 // JRS 2002-sep-17: hack, to round up fp_max to the end of the
1007 // current page, at least. Dunno if it helps.
1008 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
1009 fp_max
= VG_PGROUNDUP(fp_max_orig
);
1010 if (fp_max
>= sizeof(Addr
))
1011 fp_max
-= sizeof(Addr
);
1014 VG_(printf
)("\nmax_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
1015 "fp_max=0x%lx r15=0x%lx r13=0x%lx\n",
1016 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
1017 uregs
.r15
, uregs
.r13
);
1019 /* Assertion broken before main() is reached in pthreaded programs; the
1020 * offending stack traces only have one item. --njn, 2002-aug-16 */
1021 /* vg_assert(fp_min <= fp_max);*/
1022 // On Darwin, this kicks in for pthread-related stack traces, so they're
1023 // only 1 entry long which is wrong.
1024 if (fp_min
+ 512 >= fp_max
) {
1025 /* If the stack limits look bogus, don't poke around ... but
1026 don't bomb out either. */
1027 if (sps
) sps
[0] = uregs
.r13
;
1028 if (fps
) fps
[0] = 0;
1035 if (sps
) sps
[0] = uregs
.r13
;
1036 if (fps
) fps
[0] = 0;
1040 /* Loop unwinding the stack. */
1041 Bool do_stack_scan
= False
;
1043 /* First try the Official Way, using Dwarf CFI. */
1046 VG_(printf
)("i: %d, r15: 0x%lx, r13: 0x%lx\n",
1047 i
, uregs
.r15
, uregs
.r13
);
1053 if (VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
)) {
1054 if (sps
) sps
[i
] = uregs
.r13
;
1055 if (fps
) fps
[i
] = 0;
1056 ips
[i
++] = (uregs
.r15
& 0xFFFFFFFE) - 1;
1058 VG_(printf
)("USING CFI: r15: 0x%lx, r13: 0x%lx\n",
1059 uregs
.r15
, uregs
.r13
);
1060 uregs
.r15
= (uregs
.r15
& 0xFFFFFFFE) - 1;
1061 RECURSIVE_MERGE(cmrf
,ips
,i
);
1065 /* No luck. We have to give up. */
1066 do_stack_scan
= True
;
1070 /* Now try Plan B (maybe) -- stack scanning. This often gives
1071 pretty bad results, so this has to be enabled explicitly by the
1074 && i
< max_n_ips
&& i
< (Int
)VG_(clo_unw_stack_scan_thresh
)) {
1075 Int nByStackScan
= 0;
1076 Addr lr
= uregs
.r14
;
1077 Addr sp
= uregs
.r13
& ~3;
1078 Addr pc
= uregs
.r15
;
1079 // First see if LR contains
1080 // something that could be a valid return address.
1081 if (!in_same_fn(lr
, pc
) && looks_like_RA(lr
)) {
1082 // take it only if 'cand' isn't obviously a duplicate
1083 // of the last found IP value
1084 Addr cand
= (lr
& 0xFFFFFFFE) - 1;
1085 if (abs_diff(cand
, ips
[i
-1]) > 1) {
1086 if (sps
) sps
[i
] = 0;
1087 if (fps
) fps
[i
] = 0;
1089 RECURSIVE_MERGE(cmrf
,ips
,i
);
1093 while (in_same_page(sp
, uregs
.r13
)) {
1096 // we're in the same page; fairly safe to keep going
1097 UWord w
= *(UWord
*)(sp
& ~0x3);
1098 if (looks_like_RA(w
)) {
1099 Addr cand
= (w
& 0xFFFFFFFE) - 1;
1100 // take it only if 'cand' isn't obviously a duplicate
1101 // of the last found IP value
1102 if (abs_diff(cand
, ips
[i
-1]) > 1) {
1103 if (sps
) sps
[i
] = 0;
1104 if (fps
) fps
[i
] = 0;
1106 RECURSIVE_MERGE(cmrf
,ips
,i
);
1107 if (++nByStackScan
>= VG_(clo_unw_stack_scan_frames
)) break;
1120 /* ------------------------ arm64 ------------------------- */
1122 #if defined(VGP_arm64_linux)
1124 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
1125 /*OUT*/Addr
* ips
, UInt max_n_ips
,
1126 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
1127 const UnwindStartRegs
* startRegs
,
1134 const Int cmrf
= VG_(clo_merge_recursive_frames
);
1136 vg_assert(sizeof(Addr
) == sizeof(UWord
));
1137 vg_assert(sizeof(Addr
) == sizeof(void*));
1140 uregs
.pc
= startRegs
->r_pc
;
1141 uregs
.sp
= startRegs
->r_sp
;
1142 uregs
.x30
= startRegs
->misc
.ARM64
.x30
;
1143 uregs
.x29
= startRegs
->misc
.ARM64
.x29
;
1144 Addr fp_min
= uregs
.sp
- VG_STACK_REDZONE_SZB
;
1146 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
1147 stopping when the trail goes cold, which we guess to be
1148 when FP is not a reasonable stack location. */
1150 // JRS 2002-sep-17: hack, to round up fp_max to the end of the
1151 // current page, at least. Dunno if it helps.
1152 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
1153 fp_max
= VG_PGROUNDUP(fp_max_orig
);
1154 if (fp_max
>= sizeof(Addr
))
1155 fp_max
-= sizeof(Addr
);
1158 VG_(printf
)("\nmax_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
1159 "fp_max=0x%lx PC=0x%lx SP=0x%lx\n",
1160 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
1161 uregs
.pc
, uregs
.sp
);
1163 /* Assertion broken before main() is reached in pthreaded programs; the
1164 * offending stack traces only have one item. --njn, 2002-aug-16 */
1165 /* vg_assert(fp_min <= fp_max);*/
1166 // On Darwin, this kicks in for pthread-related stack traces, so they're
1167 // only 1 entry long which is wrong.
1168 if (fp_min
+ 512 >= fp_max
) {
1169 /* If the stack limits look bogus, don't poke around ... but
1170 don't bomb out either. */
1171 if (sps
) sps
[0] = uregs
.sp
;
1172 if (fps
) fps
[0] = uregs
.x29
;
1179 if (sps
) sps
[0] = uregs
.sp
;
1180 if (fps
) fps
[0] = uregs
.x29
;
1184 /* Loop unwinding the stack, using CFI. */
1187 VG_(printf
)("i: %d, pc: 0x%lx, sp: 0x%lx\n",
1188 i
, uregs
.pc
, uregs
.sp
);
1194 if (VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
)) {
1195 if (sps
) sps
[i
] = uregs
.sp
;
1196 if (fps
) fps
[i
] = uregs
.x29
;
1197 ips
[i
++] = uregs
.pc
- 1;
1199 VG_(printf
)("USING CFI: pc: 0x%lx, sp: 0x%lx\n",
1200 uregs
.pc
, uregs
.sp
);
1201 uregs
.pc
= uregs
.pc
- 1;
1202 RECURSIVE_MERGE(cmrf
,ips
,i
);
1206 /* No luck. We have to give up. */
1216 /* ------------------------ s390x ------------------------- */
1218 #if defined(VGP_s390x_linux)
1220 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
1221 /*OUT*/Addr
* ips
, UInt max_n_ips
,
1222 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
1223 const UnwindStartRegs
* startRegs
,
1230 const Int cmrf
= VG_(clo_merge_recursive_frames
);
1232 vg_assert(sizeof(Addr
) == sizeof(UWord
));
1233 vg_assert(sizeof(Addr
) == sizeof(void*));
1236 uregs
.ia
= startRegs
->r_pc
;
1237 uregs
.sp
= startRegs
->r_sp
;
1238 Addr fp_min
= uregs
.sp
- VG_STACK_REDZONE_SZB
;
1239 uregs
.fp
= startRegs
->misc
.S390X
.r_fp
;
1240 uregs
.lr
= startRegs
->misc
.S390X
.r_lr
;
1242 fp_max
= VG_PGROUNDUP(fp_max_orig
);
1243 if (fp_max
>= sizeof(Addr
))
1244 fp_max
-= sizeof(Addr
);
1247 VG_(printf
)("max_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
1248 "fp_max=0x%lx IA=0x%lx SP=0x%lx FP=0x%lx\n",
1249 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
1250 uregs
.ia
, uregs
.sp
,uregs
.fp
);
1252 /* The first frame is pretty obvious */
1254 if (sps
) sps
[0] = uregs
.sp
;
1255 if (fps
) fps
[0] = uregs
.fp
;
1258 /* for everything else we have to rely on the eh_frame. gcc defaults to
1259 not create a backchain and all the other tools (like gdb) also have
1265 if (VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
)) {
1266 if (sps
) sps
[i
] = uregs
.sp
;
1267 if (fps
) fps
[i
] = uregs
.fp
;
1268 ips
[i
++] = uregs
.ia
- 1;
1269 uregs
.ia
= uregs
.ia
- 1;
1270 RECURSIVE_MERGE(cmrf
,ips
,i
);
1273 /* A problem on the first frame? Lets assume it was a bad jump.
1274 We will use the link register and the current stack and frame
1275 pointers and see if we can use the CFI in the next round. */
1285 uregs
.ia
= uregs
.lr
- 1;
1286 ips
[i
++] = uregs
.lr
- 1;
1287 RECURSIVE_MERGE(cmrf
,ips
,i
);
1291 /* No luck. We have to give up. */
1301 /* ------------------------ mips 32/64 ------------------------- */
1302 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
1303 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
1304 /*OUT*/Addr
* ips
, UInt max_n_ips
,
1305 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
1306 const UnwindStartRegs
* startRegs
,
1313 const Int cmrf
= VG_(clo_merge_recursive_frames
);
1315 vg_assert(sizeof(Addr
) == sizeof(UWord
));
1316 vg_assert(sizeof(Addr
) == sizeof(void*));
1319 uregs
.pc
= startRegs
->r_pc
;
1320 uregs
.sp
= startRegs
->r_sp
;
1321 Addr fp_min
= uregs
.sp
- VG_STACK_REDZONE_SZB
;
1323 #if defined(VGP_mips32_linux)
1324 uregs
.fp
= startRegs
->misc
.MIPS32
.r30
;
1325 uregs
.ra
= startRegs
->misc
.MIPS32
.r31
;
1326 #elif defined(VGP_mips64_linux)
1327 uregs
.fp
= startRegs
->misc
.MIPS64
.r30
;
1328 uregs
.ra
= startRegs
->misc
.MIPS64
.r31
;
1331 /* Snaffle IPs from the client's stack into ips[0 .. max_n_ips-1],
1332 stopping when the trail goes cold, which we guess to be
1333 when FP is not a reasonable stack location. */
1335 fp_max
= VG_PGROUNDUP(fp_max_orig
);
1336 if (fp_max
>= sizeof(Addr
))
1337 fp_max
-= sizeof(Addr
);
1340 VG_(printf
)("max_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
1341 "fp_max=0x%lx pc=0x%lx sp=0x%lx fp=0x%lx\n",
1342 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
1343 uregs
.pc
, uregs
.sp
, uregs
.fp
);
1345 if (sps
) sps
[0] = uregs
.sp
;
1346 if (fps
) fps
[0] = uregs
.fp
;
1350 /* Loop unwinding the stack. */
1354 VG_(printf
)("i: %d, pc: 0x%lx, sp: 0x%lx, ra: 0x%lx\n",
1355 i
, uregs
.pc
, uregs
.sp
, uregs
.ra
);
1360 D3UnwindRegs uregs_copy
= uregs
;
1361 if (VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
)) {
1363 VG_(printf
)("USING CFI: pc: 0x%lx, sp: 0x%lx, ra: 0x%lx\n",
1364 uregs
.pc
, uregs
.sp
, uregs
.ra
);
1365 if (0 != uregs
.pc
&& 1 != uregs
.pc
) {
1366 if (sps
) sps
[i
] = uregs
.sp
;
1367 if (fps
) fps
[i
] = uregs
.fp
;
1368 ips
[i
++] = uregs
.pc
- 4;
1369 uregs
.pc
= uregs
.pc
- 4;
1370 RECURSIVE_MERGE(cmrf
,ips
,i
);
1376 int seen_sp_adjust
= 0;
1377 long frame_offset
= 0;
1379 if (VG_(get_inst_offset_in_function
)(uregs
.pc
, &offset
)) {
1380 Addr start_pc
= uregs
.pc
- offset
;
1381 Addr limit_pc
= uregs
.pc
;
1383 for (cur_pc
= start_pc
; cur_pc
< limit_pc
; cur_pc
+= 4) {
1384 unsigned long inst
, high_word
, low_word
;
1385 unsigned long * cur_inst
;
1386 /* Fetch the instruction. */
1387 cur_inst
= (unsigned long *)cur_pc
;
1388 inst
= *((UInt
*) cur_inst
);
1390 VG_(printf
)("cur_pc: 0x%lx, inst: 0x%lx\n", cur_pc
, inst
);
1392 /* Save some code by pre-extracting some useful fields. */
1393 high_word
= (inst
>> 16) & 0xffff;
1394 low_word
= inst
& 0xffff;
1396 if (high_word
== 0x27bd /* addiu $sp,$sp,-i */
1397 || high_word
== 0x23bd /* addi $sp,$sp,-i */
1398 || high_word
== 0x67bd) { /* daddiu $sp,$sp,-i */
1399 if (low_word
& 0x8000) /* negative stack adjustment? */
1400 frame_offset
+= 0x10000 - low_word
;
1402 /* Exit loop if a positive stack adjustment is found, which
1403 usually means that the stack cleanup code in the function
1404 epilogue is reached. */
1410 VG_(printf
)("offset: 0x%lx\n", frame_offset
);
1412 if (seen_sp_adjust
) {
1413 if (0 == uregs
.pc
|| 1 == uregs
.pc
) break;
1414 if (uregs
.pc
== uregs
.ra
- 8) break;
1416 sps
[i
] = uregs
.sp
+ frame_offset
;
1418 uregs
.sp
= uregs
.sp
+ frame_offset
;
1424 if (0 == uregs
.ra
|| 1 == uregs
.ra
) break;
1425 uregs
.pc
= uregs
.ra
- 8;
1426 ips
[i
++] = uregs
.ra
- 8;
1427 RECURSIVE_MERGE(cmrf
,ips
,i
);
1440 if (0 == uregs
.ra
|| 1 == uregs
.ra
) break;
1441 uregs
.pc
= uregs
.ra
- 8;
1442 ips
[i
++] = uregs
.ra
- 8;
1443 RECURSIVE_MERGE(cmrf
,ips
,i
);
1446 /* No luck. We have to give up. */
1456 /* ------------------------ tilegx ------------------------- */
1457 #if defined(VGP_tilegx_linux)
1458 UInt
VG_(get_StackTrace_wrk
) ( ThreadId tid_if_known
,
1459 /*OUT*/Addr
* ips
, UInt max_n_ips
,
1460 /*OUT*/Addr
* sps
, /*OUT*/Addr
* fps
,
1461 const UnwindStartRegs
* startRegs
,
1468 const Int cmrf
= VG_(clo_merge_recursive_frames
);
1470 vg_assert(sizeof(Addr
) == sizeof(UWord
));
1471 vg_assert(sizeof(Addr
) == sizeof(void*));
1474 uregs
.pc
= startRegs
->r_pc
;
1475 uregs
.sp
= startRegs
->r_sp
;
1476 Addr fp_min
= uregs
.sp
- VG_STACK_REDZONE_SZB
;
1478 uregs
.fp
= startRegs
->misc
.TILEGX
.r52
;
1479 uregs
.lr
= startRegs
->misc
.TILEGX
.r55
;
1481 fp_max
= VG_PGROUNDUP(fp_max_orig
);
1482 if (fp_max
>= sizeof(Addr
))
1483 fp_max
-= sizeof(Addr
);
1486 VG_(printf
)("max_n_ips=%u fp_min=0x%lx fp_max_orig=0x%lx, "
1487 "fp_max=0x%lx pc=0x%lx sp=0x%lx fp=0x%lx\n",
1488 max_n_ips
, fp_min
, fp_max_orig
, fp_max
,
1489 uregs
.pc
, uregs
.sp
, uregs
.fp
);
1491 if (sps
) sps
[0] = uregs
.sp
;
1492 if (fps
) fps
[0] = uregs
.fp
;
1496 /* Loop unwinding the stack. */
1499 VG_(printf
)("i: %d, pc: 0x%lx, sp: 0x%lx, lr: 0x%lx\n",
1500 i
, uregs
.pc
, uregs
.sp
, uregs
.lr
);
1505 D3UnwindRegs uregs_copy
= uregs
;
1506 if (VG_(use_CF_info
)( &uregs
, fp_min
, fp_max
)) {
1508 VG_(printf
)("USING CFI: pc: 0x%lx, sp: 0x%lx, fp: 0x%lx, lr: 0x%lx\n",
1509 uregs
.pc
, uregs
.sp
, uregs
.fp
, uregs
.lr
);
1510 if (0 != uregs
.pc
&& 1 != uregs
.pc
&&
1511 (uregs
.pc
< fp_min
|| uregs
.pc
> fp_max
)) {
1512 if (sps
) sps
[i
] = uregs
.sp
;
1513 if (fps
) fps
[i
] = uregs
.fp
;
1514 if (uregs
.pc
!= uregs_copy
.pc
&& uregs
.sp
!= uregs_copy
.sp
)
1515 ips
[i
++] = uregs
.pc
- 8;
1516 uregs
.pc
= uregs
.pc
- 8;
1517 RECURSIVE_MERGE(cmrf
,ips
,i
);
1523 Long frame_offset
= 0;
1525 if (VG_(get_inst_offset_in_function
)(uregs
.pc
, &offset
)) {
1526 Addr start_pc
= uregs
.pc
;
1527 Addr limit_pc
= uregs
.pc
- offset
;
1529 /* Try to find any stack adjustment from current instruction
1530 bundles downward. */
1531 for (cur_pc
= start_pc
; cur_pc
> limit_pc
; cur_pc
-= 8) {
1535 /* Fetch the instruction. */
1536 cur_inst
= (ULong
*)cur_pc
;
1539 VG_(printf
)("cur_pc: 0x%lx, inst: 0x%lx\n", cur_pc
, inst
);
1541 if ((inst
& 0xC000000000000000ULL
) == 0) {
1542 /* Bundle is X type. */
1543 if ((inst
& 0xC000000070000fffULL
) ==
1544 (0x0000000010000db6ULL
)) {
1546 off
= (short)(0xFFFF & (inst
>> 12));
1547 } else if ((inst
& 0xF80007ff80000000ULL
) ==
1548 (0x000006db00000000ULL
)) {
1549 /* addli at X1 addli*/
1550 off
= (short)(0xFFFF & (inst
>> 43));
1551 } else if ((inst
& 0xC00000007FF00FFFULL
) ==
1552 (0x0000000040100db6ULL
)) {
1554 off
= (char)(0xFF & (inst
>> 12));
1555 } else if ((inst
& 0xFFF807ff80000000ULL
) ==
1556 (0x180806db00000000ULL
)) {
1558 off
= (char)(0xFF & (inst
>> 43));
1561 /* Bundle is Y type. */
1562 if ((inst
& 0x0000000078000FFFULL
) ==
1563 (0x0000000000000db6ULL
)) {
1565 off
= (char)(0xFF & (inst
>> 12));
1566 } else if ((inst
& 0x3C0007FF80000000ULL
) ==
1567 (0x040006db00000000ULL
)) {
1569 off
= (char)(0xFF & (inst
>> 43));
1574 VG_(printf
)("offset: -0x%lx\n", -off
);
1577 /* frame offset should be modular of 8 */
1578 vg_assert((off
& 7) == 0);
1579 frame_offset
+= off
;
1581 /* Exit loop if a positive stack adjustment is found, which
1582 usually means that the stack cleanup code in the function
1583 epilogue is reached. */
1588 if (frame_offset
< 0) {
1589 if (0 == uregs
.pc
|| 1 == uregs
.pc
) break;
1591 /* Subtract the offset from the current stack. */
1592 uregs
.sp
= uregs
.sp
+ (ULong
)(-frame_offset
);
1595 VG_(printf
)("offset: i: %d, pc: 0x%lx, sp: 0x%lx, lr: 0x%lx\n",
1596 i
, uregs
.pc
, uregs
.sp
, uregs
.lr
);
1598 if (uregs
.pc
== uregs
.lr
- 8 ||
1599 uregs
.lr
- 8 >= fp_min
&& uregs
.lr
- 8 <= fp_max
) {
1601 VG_(printf
)("new lr = 0x%lx\n", *(ULong
*)uregs
.sp
);
1602 uregs
.lr
= *(ULong
*)uregs
.sp
;
1605 uregs
.pc
= uregs
.lr
- 8;
1607 if (uregs
.lr
!= 0) {
1608 /* Avoid the invalid pc = 0xffff...ff8 */
1615 ips
[i
++] = uregs
.pc
;
1617 RECURSIVE_MERGE(cmrf
,ips
,i
);
1622 /* A special case for the 1st frame. Assume it was a bad jump.
1623 Use the link register "lr" and current stack and frame to
1634 if (0 == uregs
.lr
|| 1 == uregs
.lr
)
1637 uregs
.pc
= uregs
.lr
- 8;
1638 ips
[i
++] = uregs
.lr
- 8;
1639 RECURSIVE_MERGE(cmrf
,ips
,i
);
1642 /* No luck. We have to give up. */
1647 /* Display the back trace. */
1649 for ( ii
= 0; ii
< i
; ii
++) {
1651 VG_(printf
)("%d: pc=%lx ", ii
, ips
[ii
]);
1652 VG_(printf
)("sp=%lx\n", sps
[ii
]);
1654 VG_(printf
)("%d: pc=%lx\n", ii
, ips
[ii
]);
1664 /*------------------------------------------------------------*/
1666 /*--- END platform-dependent unwinder worker functions ---*/
1668 /*------------------------------------------------------------*/
1670 /*------------------------------------------------------------*/
1671 /*--- Exported functions. ---*/
1672 /*------------------------------------------------------------*/
1674 UInt
VG_(get_StackTrace
) ( ThreadId tid
,
1675 /*OUT*/StackTrace ips
, UInt max_n_ips
,
1676 /*OUT*/StackTrace sps
,
1677 /*OUT*/StackTrace fps
,
1678 Word first_ip_delta
)
1680 /* Get the register values with which to start the unwind. */
1681 UnwindStartRegs startRegs
;
1682 VG_(memset
)( &startRegs
, 0, sizeof(startRegs
) );
1683 VG_(get_UnwindStartRegs
)( &startRegs
, tid
);
1685 Addr stack_highest_byte
= VG_(threads
)[tid
].client_stack_highest_byte
;
1686 Addr stack_lowest_byte
= 0;
1688 # if defined(VGP_x86_linux)
1689 /* Nasty little hack to deal with syscalls - if libc is using its
1690 _dl_sysinfo_int80 function for syscalls (the TLS version does),
1691 then ip will always appear to be in that function when doing a
1692 syscall, not the actual libc function doing the syscall. This
1693 check sees if IP is within that function, and pops the return
1694 address off the stack so that ip is placed within the library
1695 function calling the syscall. This makes stack backtraces much
1698 The function is assumed to look like this (from glibc-2.3.6 sources):
1702 That is 3 (2+1) bytes long. We could be more thorough and check
1703 the 3 bytes of the function are as expected, but I can't be
1706 if (VG_(client__dl_sysinfo_int80
) != 0 /* we know its address */
1707 && startRegs
.r_pc
>= VG_(client__dl_sysinfo_int80
)
1708 && startRegs
.r_pc
< VG_(client__dl_sysinfo_int80
)+3
1709 && VG_(am_is_valid_for_client
)(startRegs
.r_pc
, sizeof(Addr
),
1711 startRegs
.r_pc
= (ULong
) *(Addr
*)(UWord
)startRegs
.r_sp
;
1712 startRegs
.r_sp
+= (ULong
) sizeof(Addr
);
1716 /* See if we can get a better idea of the stack limits */
1717 VG_(stack_limits
)( (Addr
)startRegs
.r_sp
,
1718 &stack_lowest_byte
, &stack_highest_byte
);
1720 /* Take into account the first_ip_delta. */
1721 startRegs
.r_pc
+= (Long
)(Word
)first_ip_delta
;
1724 VG_(printf
)("tid %u: stack_highest=0x%08lx ip=0x%010llx "
1726 tid
, stack_highest_byte
,
1727 startRegs
.r_pc
, startRegs
.r_sp
);
1729 return VG_(get_StackTrace_wrk
)(tid
, ips
, max_n_ips
,
1732 stack_highest_byte
);
1735 static void printIpDesc(UInt n
, Addr ip
, void* uu_opaque
)
1737 InlIPCursor
*iipc
= VG_(new_IIPC
)(ip
);
1740 const HChar
*buf
= VG_(describe_IP
)(ip
, iipc
);
1742 VG_(printf_xml
)(" %s\n", buf
);
1744 VG_(message
)(Vg_UserMsg
, " %s %s\n",
1745 ( n
== 0 ? "at" : "by" ), buf
);
1748 // Increase n to show "at" for only one level.
1749 } while (VG_(next_IIPC
)(iipc
));
1750 VG_(delete_IIPC
)(iipc
);
1753 /* Print a StackTrace. */
1754 void VG_(pp_StackTrace
) ( StackTrace ips
, UInt n_ips
)
1756 vg_assert( n_ips
> 0 );
1759 VG_(printf_xml
)(" <stack>\n");
1761 VG_(apply_StackTrace
)( printIpDesc
, NULL
, ips
, n_ips
);
1764 VG_(printf_xml
)(" </stack>\n");
1767 /* Get and immediately print a StackTrace. */
1768 void VG_(get_and_pp_StackTrace
) ( ThreadId tid
, UInt max_n_ips
)
1770 Addr ips
[max_n_ips
];
1772 = VG_(get_StackTrace
)(tid
, ips
, max_n_ips
,
1773 NULL
/*array to dump SP values in*/,
1774 NULL
/*array to dump FP values in*/,
1775 0/*first_ip_delta*/);
1776 VG_(pp_StackTrace
)(ips
, n_ips
);
1779 void VG_(apply_StackTrace
)(
1780 void(*action
)(UInt n
, Addr ip
, void* opaque
),
1782 StackTrace ips
, UInt n_ips
1785 Bool main_done
= False
;
1788 vg_assert(n_ips
> 0);
1792 // Stop after the first appearance of "main" or one of the other names
1793 // (the appearance of which is a pretty good sign that we've gone past
1794 // main without seeing it, for whatever reason)
1795 if ( ! VG_(clo_show_below_main
) ) {
1796 Vg_FnNameKind kind
= VG_(get_fnname_kind_from_IP
)(ip
);
1797 if (Vg_FnNameMain
== kind
|| Vg_FnNameBelowMain
== kind
) {
1803 action(i
, ip
, opaque
);
1806 } while (i
< n_ips
&& !main_done
);
1810 /*--------------------------------------------------------------------*/
1812 /*--------------------------------------------------------------------*/