From f278d734c1c02901631e04d1ac9099a4897ab75b Mon Sep 17 00:00:00 2001 From: Shinichiro Hamaji Date: Tue, 9 May 2017 17:11:51 +0900 Subject: [PATCH] x86 stacktrace: Use __builtin_frame_address if possible --- src/stacktrace_x86-inl.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/stacktrace_x86-inl.h b/src/stacktrace_x86-inl.h index cfd31f7..3b8d5a8 100644 --- a/src/stacktrace_x86-inl.h +++ b/src/stacktrace_x86-inl.h @@ -93,16 +93,23 @@ static void **NextStackFrame(void **old_sp) { // If you change this function, also change GetStackFrames below. int GetStackTrace(void** result, int max_depth, int skip_count) { void **sp; -#ifdef __i386__ + +#ifdef __GNUC__ +#if __GNUC__ * 100 + __GNUC_MINOR__ >= 402 +#define USE_BUILTIN_FRAME_ADDRESS +#endif +#endif + +#ifdef USE_BUILTIN_FRAME_ADDRESS + sp = reinterpret_cast(__builtin_frame_address(0)); +#elif defined(__i386__) // Stack frame format: // sp[0] pointer to previous frame // sp[1] caller address // sp[2] first argument // ... sp = (void **)&result - 2; -#endif - -#ifdef __x86_64__ +#elif defined(__x86_64__) // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8 unsigned long rbp; // Move the value of the register %rbp into the local variable rbp. -- 2.7.4