]> git.ipfire.org Git - thirdparty/gcc.git/blob - libjava/sysdep/i386/backtrace.h
63b3274613789c035739d7d896e945ffb7860a7d
[thirdparty/gcc.git] / libjava / sysdep / i386 / backtrace.h
1 // backtrace.h - Fallback backtrace implementation. i386 implementation.
2
3 /* Copyright (C) 2005, 2006 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #ifndef __SYSDEP_BACKTRACE_H__
12 #define __SYSDEP_BACKTRACE_H__
13
14 #include <java-stack.h>
15
16 extern int main (int, char **);
17
18 /* The context used to keep track of our position while unwinding through
19 the call stack. */
20 struct _Unwind_Context
21 {
22 /* The starting address of the method. */
23 _Jv_uintptr_t meth_addr;
24
25 /* The return address in the method. */
26 _Jv_uintptr_t ret_addr;
27 };
28
29 #ifdef SJLJ_EXCEPTIONS
30
31 #undef _Unwind_GetIPInfo
32 #define _Unwind_GetIPInfo(ctx,ip_before_insn) \
33 (*(ip_before_insn) = 1, (ctx)->ret_addr)
34
35 #undef _Unwind_GetRegionStart
36 #define _Unwind_GetRegionStart(ctx) \
37 ((ctx)->meth_addr)
38
39 #undef _Unwind_Backtrace
40 #define _Unwind_Backtrace(trace_fn,state_ptr) \
41 (fallback_backtrace (trace_fn, state_ptr))
42
43 #endif /* SJLJ_EXCEPTIONS */
44
45 /* Unwind through the call stack calling TRACE_FN with STATE for each stack
46 frame. Returns the reason why the unwinding was stopped. */
47 _Unwind_Reason_Code
48 fallback_backtrace (_Unwind_Trace_Fn trace_fn, _Jv_UnwindState *state)
49 {
50 register _Jv_uintptr_t *_ebp __asm__ ("ebp");
51 register _Jv_uintptr_t _esp __asm__ ("esp");
52 _Jv_uintptr_t rfp;
53 _Unwind_Context ctx;
54
55 for (rfp = *_ebp; rfp; rfp = *(_Jv_uintptr_t *)rfp)
56 {
57 /* Sanity checks to eliminate dubious-looking frame pointer chains.
58 The frame pointer should be a 32-bit word-aligned stack address.
59 Since the stack grows downwards on x86, the frame pointer must have
60 a value greater than the current value of the stack pointer, it
61 should not be below the supposed next frame pointer and it should
62 not be too far off from the supposed next frame pointer. */
63 int diff = *(_Jv_uintptr_t *)rfp - rfp;
64 if ((rfp & 0x00000003) != 0 || rfp < _esp
65 || diff > 4 * 1024 || diff < 0)
66 break;
67
68 /* Get the return address in the calling function. This is stored on
69 the stack just before the value of the old frame pointer. */
70 ctx.ret_addr = *(_Jv_uintptr_t *)(rfp + sizeof (_Jv_uintptr_t));
71
72 /* Try to locate a "pushl %ebp; movl %esp, %ebp" function prologue
73 by scanning backwards at even addresses below the return address.
74 This instruction sequence is encoded either as 0x55 0x89 0xE5 or as
75 0x55 0x8B 0xEC. We give up if we do not find this sequence even
76 after scanning 1024K of memory.
77 FIXME: This is not robust and will probably give us false positives,
78 but this is about the best we can do if we do not have DWARF-2 unwind
79 information based exception handling. */
80 ctx.meth_addr = (_Jv_uintptr_t)NULL;
81 _Jv_uintptr_t scan_addr = (ctx.ret_addr & 0xFFFFFFFE) - 2;
82 _Jv_uintptr_t limit_addr
83 = (scan_addr > 1024 * 1024) ? (scan_addr - 1024 * 1024) : 2;
84 for ( ; scan_addr >= limit_addr; scan_addr -= 2)
85 {
86 unsigned char *scan_bytes = (unsigned char *)scan_addr;
87 if (scan_bytes[0] == 0x55
88 && ((scan_bytes[1] == 0x89 && scan_bytes[2] == 0xE5)
89 || (scan_bytes[1] == 0x8B && scan_bytes[2] == 0xEC)))
90 {
91 ctx.meth_addr = scan_addr;
92 break;
93 }
94 }
95
96 /* Now call the unwinder callback function. */
97 if (trace_fn != NULL)
98 (*trace_fn) (&ctx, state);
99
100 /* No need to unwind beyond _Jv_RunMain(), _Jv_ThreadStart or
101 main(). */
102 void *jv_runmain
103 = (void *)(void (*)(JvVMInitArgs *, jclass, const char *, int,
104 const char **, bool))_Jv_RunMain;
105 if (ctx.meth_addr == (_Jv_uintptr_t)jv_runmain
106 || ctx.meth_addr == (_Jv_uintptr_t)_Jv_ThreadStart
107 || (ctx.meth_addr - (_Jv_uintptr_t)main) < 16)
108 break;
109 }
110
111 return _URC_NO_REASON;
112 }
113 #endif