michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // stackwalker_selftest.cc: Tests StackwalkerX86 or StackwalkerPPC using the michael@0: // running process' stack as test data, if running on an x86 or ppc and michael@0: // compiled with gcc. This test is not enabled in the "make check" suite michael@0: // by default, because certain optimizations interfere with its proper michael@0: // operation. To turn it on, configure with --enable-selftest. michael@0: // michael@0: // Optimizations that cause problems: michael@0: // - stack frame reuse. The Recursor function here calls itself with michael@0: // |return Recursor|. When the caller's frame is reused, it will cause michael@0: // CountCallerFrames to correctly return the same number of frames michael@0: // in both the caller and callee. This is considered an unexpected michael@0: // condition in the test, which expects a callee to have one more michael@0: // caller frame in the stack than its caller. michael@0: // - frame pointer omission. Even with a stackwalker that understands michael@0: // this optimization, the code to harness debug information currently michael@0: // only exists to retrieve it from minidumps, not the current process. michael@0: // michael@0: // This test can also serve as a developmental and debugging aid if michael@0: // PRINT_STACKS is defined. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #include "processor/logging.h" michael@0: michael@0: #if defined(__i386) && !defined(__i386__) michael@0: #define __i386__ michael@0: #endif michael@0: #if defined(__sparc) && !defined(__sparc__) michael@0: #define __sparc__ michael@0: #endif michael@0: michael@0: #if (defined(__SUNPRO_CC) || defined(__GNUC__)) && \ michael@0: (defined(__i386__) || defined(__ppc__) || defined(__sparc__)) michael@0: michael@0: michael@0: #include michael@0: michael@0: #include "common/scoped_ptr.h" michael@0: #include "google_breakpad/common/breakpad_types.h" michael@0: #include "google_breakpad/common/minidump_format.h" michael@0: #include "google_breakpad/processor/basic_source_line_resolver.h" michael@0: #include "google_breakpad/processor/call_stack.h" michael@0: #include "google_breakpad/processor/code_module.h" michael@0: #include "google_breakpad/processor/memory_region.h" michael@0: #include "google_breakpad/processor/stack_frame.h" michael@0: #include "google_breakpad/processor/stack_frame_cpu.h" michael@0: michael@0: using google_breakpad::BasicSourceLineResolver; michael@0: using google_breakpad::CallStack; michael@0: using google_breakpad::CodeModule; michael@0: using google_breakpad::MemoryRegion; michael@0: using google_breakpad::scoped_ptr; michael@0: using google_breakpad::StackFrame; michael@0: using google_breakpad::StackFramePPC; michael@0: using google_breakpad::StackFrameX86; michael@0: using google_breakpad::StackFrameSPARC; michael@0: michael@0: #if defined(__i386__) michael@0: #include "processor/stackwalker_x86.h" michael@0: using google_breakpad::StackwalkerX86; michael@0: #elif defined(__ppc__) michael@0: #include "processor/stackwalker_ppc.h" michael@0: using google_breakpad::StackwalkerPPC; michael@0: #elif defined(__sparc__) michael@0: #include "processor/stackwalker_sparc.h" michael@0: using google_breakpad::StackwalkerSPARC; michael@0: #endif // __i386__ || __ppc__ || __sparc__ michael@0: michael@0: #define RECURSION_DEPTH 100 michael@0: michael@0: michael@0: // A simple MemoryRegion subclass that provides direct access to this michael@0: // process' memory space by pointer. michael@0: class SelfMemoryRegion : public MemoryRegion { michael@0: public: michael@0: virtual uint64_t GetBase() { return 0; } michael@0: virtual uint32_t GetSize() { return 0xffffffff; } michael@0: michael@0: bool GetMemoryAtAddress(uint64_t address, uint8_t* value) { michael@0: return GetMemoryAtAddressInternal(address, value); } michael@0: bool GetMemoryAtAddress(uint64_t address, uint16_t* value) { michael@0: return GetMemoryAtAddressInternal(address, value); } michael@0: bool GetMemoryAtAddress(uint64_t address, uint32_t* value) { michael@0: return GetMemoryAtAddressInternal(address, value); } michael@0: bool GetMemoryAtAddress(uint64_t address, uint64_t* value) { michael@0: return GetMemoryAtAddressInternal(address, value); } michael@0: michael@0: private: michael@0: template bool GetMemoryAtAddressInternal(uint64_t address, michael@0: T* value) { michael@0: // Without knowing what addresses are actually mapped, just assume that michael@0: // everything low is not mapped. This helps the stackwalker catch the michael@0: // end of a stack when it tries to dereference a null or low pointer michael@0: // in an attempt to find the caller frame. Other unmapped accesses will michael@0: // cause the program to crash, but that would properly be a test failure. michael@0: if (address < 0x100) michael@0: return false; michael@0: michael@0: uint8_t* memory = 0; michael@0: *value = *reinterpret_cast(&memory[address]); michael@0: return true; michael@0: } michael@0: }; michael@0: michael@0: michael@0: #if defined(__GNUC__) michael@0: michael@0: michael@0: #if defined(__i386__) michael@0: michael@0: // GetEBP returns the current value of the %ebp register. Because it's michael@0: // implemented as a function, %ebp itself contains GetEBP's frame pointer michael@0: // and not the caller's frame pointer. Dereference %ebp to obtain the michael@0: // caller's frame pointer, which the compiler-generated preamble stored michael@0: // on the stack (provided frame pointers are not being omitted.) Because michael@0: // this function depends on the compiler-generated preamble, inlining is michael@0: // disabled. michael@0: static uint32_t GetEBP() __attribute__((noinline)); michael@0: static uint32_t GetEBP() { michael@0: uint32_t ebp; michael@0: __asm__ __volatile__( michael@0: "movl (%%ebp), %0" michael@0: : "=a" (ebp) michael@0: ); michael@0: return ebp; michael@0: } michael@0: michael@0: michael@0: // The caller's %esp is 8 higher than the value of %ebp in this function, michael@0: // assuming that it's not inlined and that the standard prolog is used. michael@0: // The CALL instruction places a 4-byte return address on the stack above michael@0: // the caller's %esp, and this function's prolog will save the caller's %ebp michael@0: // on the stack as well, for another 4 bytes, before storing %esp in %ebp. michael@0: static uint32_t GetESP() __attribute__((noinline)); michael@0: static uint32_t GetESP() { michael@0: uint32_t ebp; michael@0: __asm__ __volatile__( michael@0: "movl %%ebp, %0" michael@0: : "=a" (ebp) michael@0: ); michael@0: return ebp + 8; michael@0: } michael@0: michael@0: michael@0: // GetEIP returns the instruction pointer identifying the next instruction michael@0: // to execute after GetEIP returns. It obtains this information from the michael@0: // stack, where it was placed by the call instruction that called GetEIP. michael@0: // This function depends on frame pointers not being omitted. It is possible michael@0: // to write a pure asm version of this routine that has no compiler-generated michael@0: // preamble and uses %esp instead of %ebp; that would function in the michael@0: // absence of frame pointers. However, the simpler approach is used here michael@0: // because GetEBP and stackwalking necessarily depends on access to frame michael@0: // pointers. Because this function depends on a call instruction and the michael@0: // compiler-generated preamble, inlining is disabled. michael@0: static uint32_t GetEIP() __attribute__((noinline)); michael@0: static uint32_t GetEIP() { michael@0: uint32_t eip; michael@0: __asm__ __volatile__( michael@0: "movl 4(%%ebp), %0" michael@0: : "=a" (eip) michael@0: ); michael@0: return eip; michael@0: } michael@0: michael@0: michael@0: #elif defined(__ppc__) michael@0: michael@0: michael@0: // GetSP returns the current value of the %r1 register, which by convention, michael@0: // is the stack pointer on ppc. Because it's implemented as a function, michael@0: // %r1 itself contains GetSP's own stack pointer and not the caller's stack michael@0: // pointer. Dereference %r1 to obtain the caller's stack pointer, which the michael@0: // compiler-generated prolog stored on the stack. Because this function michael@0: // depends on the compiler-generated prolog, inlining is disabled. michael@0: static uint32_t GetSP() __attribute__((noinline)); michael@0: static uint32_t GetSP() { michael@0: uint32_t sp; michael@0: __asm__ __volatile__( michael@0: "lwz %0, 0(r1)" michael@0: : "=r" (sp) michael@0: ); michael@0: return sp; michael@0: } michael@0: michael@0: michael@0: // GetPC returns the program counter identifying the next instruction to michael@0: // execute after GetPC returns. It obtains this information from the michael@0: // link register, where it was placed by the branch instruction that called michael@0: // GetPC. Because this function depends on the caller's use of a branch michael@0: // instruction, inlining is disabled. michael@0: static uint32_t GetPC() __attribute__((noinline)); michael@0: static uint32_t GetPC() { michael@0: uint32_t lr; michael@0: __asm__ __volatile__( michael@0: "mflr %0" michael@0: : "=r" (lr) michael@0: ); michael@0: return lr; michael@0: } michael@0: michael@0: michael@0: #elif defined(__sparc__) michael@0: michael@0: michael@0: // GetSP returns the current value of the %sp/%o6/%g_r[14] register, which michael@0: // by convention, is the stack pointer on sparc. Because it's implemented michael@0: // as a function, %sp itself contains GetSP's own stack pointer and not michael@0: // the caller's stack pointer. Dereference to obtain the caller's stack michael@0: // pointer, which the compiler-generated prolog stored on the stack. michael@0: // Because this function depends on the compiler-generated prolog, inlining michael@0: // is disabled. michael@0: static uint32_t GetSP() __attribute__((noinline)); michael@0: static uint32_t GetSP() { michael@0: uint32_t sp; michael@0: __asm__ __volatile__( michael@0: "mov %%fp, %0" michael@0: : "=r" (sp) michael@0: ); michael@0: return sp; michael@0: } michael@0: michael@0: // GetFP returns the current value of the %fp register. Because it's michael@0: // implemented as a function, %fp itself contains GetFP's frame pointer michael@0: // and not the caller's frame pointer. Dereference %fp to obtain the michael@0: // caller's frame pointer, which the compiler-generated preamble stored michael@0: // on the stack (provided frame pointers are not being omitted.) Because michael@0: // this function depends on the compiler-generated preamble, inlining is michael@0: // disabled. michael@0: static uint32_t GetFP() __attribute__((noinline)); michael@0: static uint32_t GetFP() { michael@0: uint32_t fp; michael@0: __asm__ __volatile__( michael@0: "ld [%%fp+56], %0" michael@0: : "=r" (fp) michael@0: ); michael@0: return fp; michael@0: } michael@0: michael@0: // GetPC returns the program counter identifying the next instruction to michael@0: // execute after GetPC returns. It obtains this information from the michael@0: // link register, where it was placed by the branch instruction that called michael@0: // GetPC. Because this function depends on the caller's use of a branch michael@0: // instruction, inlining is disabled. michael@0: static uint32_t GetPC() __attribute__((noinline)); michael@0: static uint32_t GetPC() { michael@0: uint32_t pc; michael@0: __asm__ __volatile__( michael@0: "mov %%i7, %0" michael@0: : "=r" (pc) michael@0: ); michael@0: return pc + 8; michael@0: } michael@0: michael@0: #endif // __i386__ || __ppc__ || __sparc__ michael@0: michael@0: #elif defined(__SUNPRO_CC) michael@0: michael@0: #if defined(__i386__) michael@0: extern "C" { michael@0: extern uint32_t GetEIP(); michael@0: extern uint32_t GetEBP(); michael@0: extern uint32_t GetESP(); michael@0: } michael@0: #elif defined(__sparc__) michael@0: extern "C" { michael@0: extern uint32_t GetPC(); michael@0: extern uint32_t GetFP(); michael@0: extern uint32_t GetSP(); michael@0: } michael@0: #endif // __i386__ || __sparc__ michael@0: michael@0: #endif // __GNUC__ || __SUNPRO_CC michael@0: michael@0: // CountCallerFrames returns the number of stack frames beneath the function michael@0: // that called CountCallerFrames. Because this function's return value michael@0: // is dependent on the size of the stack beneath it, inlining is disabled, michael@0: // and any function that calls this should not be inlined either. michael@0: #if defined(__GNUC__) michael@0: static unsigned int CountCallerFrames() __attribute__((noinline)); michael@0: #elif defined(__SUNPRO_CC) michael@0: static unsigned int CountCallerFrames(); michael@0: #endif michael@0: static unsigned int CountCallerFrames() { michael@0: SelfMemoryRegion memory; michael@0: BasicSourceLineResolver resolver; michael@0: michael@0: #if defined(__i386__) michael@0: MDRawContextX86 context = MDRawContextX86(); michael@0: context.eip = GetEIP(); michael@0: context.ebp = GetEBP(); michael@0: context.esp = GetESP(); michael@0: michael@0: StackwalkerX86 stackwalker = StackwalkerX86(NULL, &context, &memory, NULL, michael@0: NULL, &resolver); michael@0: #elif defined(__ppc__) michael@0: MDRawContextPPC context = MDRawContextPPC(); michael@0: context.srr0 = GetPC(); michael@0: context.gpr[1] = GetSP(); michael@0: michael@0: StackwalkerPPC stackwalker = StackwalkerPPC(NULL, &context, &memory, NULL, michael@0: NULL, &resolver); michael@0: #elif defined(__sparc__) michael@0: MDRawContextSPARC context = MDRawContextSPARC(); michael@0: context.pc = GetPC(); michael@0: context.g_r[14] = GetSP(); michael@0: context.g_r[30] = GetFP(); michael@0: michael@0: StackwalkerSPARC stackwalker = StackwalkerSPARC(NULL, &context, &memory, michael@0: NULL, NULL, &resolver); michael@0: #endif // __i386__ || __ppc__ || __sparc__ michael@0: michael@0: CallStack stack; michael@0: vector modules_without_symbols; michael@0: stackwalker.Walk(&stack, &modules_without_symbols); michael@0: michael@0: #ifdef PRINT_STACKS michael@0: printf("\n"); michael@0: for (unsigned int frame_index = 0; michael@0: frame_index < stack.frames()->size(); michael@0: ++frame_index) { michael@0: StackFrame *frame = stack.frames()->at(frame_index); michael@0: printf("frame %-3d instruction = 0x%08" PRIx64, michael@0: frame_index, frame->instruction); michael@0: #if defined(__i386__) michael@0: StackFrameX86 *frame_x86 = reinterpret_cast(frame); michael@0: printf(" esp = 0x%08x ebp = 0x%08x\n", michael@0: frame_x86->context.esp, frame_x86->context.ebp); michael@0: #elif defined(__ppc__) michael@0: StackFramePPC *frame_ppc = reinterpret_cast(frame); michael@0: printf(" gpr[1] = 0x%08x\n", frame_ppc->context.gpr[1]); michael@0: #elif defined(__sparc__) michael@0: StackFrameSPARC *frame_sparc = reinterpret_cast(frame); michael@0: printf(" sp = 0x%08x fp = 0x%08x\n", michael@0: frame_sparc->context.g_r[14], frame_sparc->context.g_r[30]); michael@0: #endif // __i386__ || __ppc__ || __sparc__ michael@0: } michael@0: #endif // PRINT_STACKS michael@0: michael@0: // Subtract 1 because the caller wants the number of frames beneath michael@0: // itself. Because the caller called us, subract two for our frame and its michael@0: // frame, which are included in stack.size(). michael@0: return stack.frames()->size() - 2; michael@0: } michael@0: michael@0: michael@0: // Recursor verifies that the number stack frames beneath itself is one more michael@0: // than the number of stack frames beneath its parent. When depth frames michael@0: // have been reached, Recursor stops checking and returns success. If the michael@0: // frame count check fails at any depth, Recursor will stop and return false. michael@0: // Because this calls CountCallerFrames, inlining is disabled. michael@0: #if defined(__GNUC__) michael@0: static bool Recursor(unsigned int depth, unsigned int parent_callers) michael@0: __attribute__((noinline)); michael@0: #elif defined(__SUNPRO_CC) michael@0: static bool Recursor(unsigned int depth, unsigned int parent_callers); michael@0: #endif michael@0: static bool Recursor(unsigned int depth, unsigned int parent_callers) { michael@0: unsigned int callers = CountCallerFrames(); michael@0: if (callers != parent_callers + 1) michael@0: return false; michael@0: michael@0: if (depth) michael@0: return Recursor(depth - 1, callers); michael@0: michael@0: // depth == 0 michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // Because this calls CountCallerFrames, inlining is disabled - but because michael@0: // it's main (and nobody calls it other than the entry point), it wouldn't michael@0: // be inlined anyway. michael@0: #if defined(__GNUC__) michael@0: int main(int argc, char** argv) __attribute__((noinline)); michael@0: #elif defined(__SUNPRO_CC) michael@0: int main(int argc, char** argv); michael@0: #endif michael@0: int main(int argc, char** argv) { michael@0: BPLOG_INIT(&argc, &argv); michael@0: michael@0: return Recursor(RECURSION_DEPTH, CountCallerFrames()) ? 0 : 1; michael@0: } michael@0: michael@0: michael@0: #else michael@0: // Not i386 or ppc or sparc? We can only test stacks we know how to walk. michael@0: michael@0: michael@0: int main(int argc, char **argv) { michael@0: BPLOG_INIT(&argc, &argv); michael@0: michael@0: // "make check" interprets an exit status of 77 to mean that the test is michael@0: // not supported. michael@0: BPLOG(ERROR) << "Selftest not supported here"; michael@0: return 77; michael@0: } michael@0: michael@0: michael@0: #endif // (__GNUC__ || __SUNPRO_CC) && (__i386__ || __ppc__ || __sparc__)