stacktrace_x86-inl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. //
  33. // Produce stack trace
  34. #ifndef BASE_STACKTRACE_X86_INL_H_
  35. #define BASE_STACKTRACE_X86_INL_H_
  36. // Note: this file is included into stacktrace.cc more than once.
  37. // Anything that should only be defined once should be here:
  38. #include "config.h"
  39. #include <stdlib.h> // for NULL
  40. #include <assert.h>
  41. #if defined(HAVE_SYS_UCONTEXT_H)
  42. #include <sys/ucontext.h>
  43. #elif defined(HAVE_UCONTEXT_H)
  44. #include <ucontext.h> // for ucontext_t
  45. #elif defined(HAVE_CYGWIN_SIGNAL_H)
  46. // cygwin/signal.h has a buglet where it uses pthread_attr_t without
  47. // #including <pthread.h> itself. So we have to do it.
  48. # ifdef HAVE_PTHREAD
  49. # include <pthread.h>
  50. # endif
  51. #include <cygwin/signal.h>
  52. typedef ucontext ucontext_t;
  53. #endif
  54. #ifdef HAVE_STDINT_H
  55. #include <stdint.h> // for uintptr_t
  56. #endif
  57. #ifdef HAVE_UNISTD_H
  58. #include <unistd.h>
  59. #endif
  60. #ifdef HAVE_MMAP
  61. #include <sys/mman.h> // for msync
  62. #include "base/vdso_support.h"
  63. #endif
  64. #include "gperftools/stacktrace.h"
  65. #if defined(__linux__) && defined(__i386__) && defined(__ELF__) && defined(HAVE_MMAP)
  66. // Count "push %reg" instructions in VDSO __kernel_vsyscall(),
  67. // preceding "syscall" or "sysenter".
  68. // If __kernel_vsyscall uses frame pointer, answer 0.
  69. //
  70. // kMaxBytes tells how many instruction bytes of __kernel_vsyscall
  71. // to analyze before giving up. Up to kMaxBytes+1 bytes of
  72. // instructions could be accessed.
  73. //
  74. // Here are known __kernel_vsyscall instruction sequences:
  75. //
  76. // SYSENTER (linux-2.6.26/arch/x86/vdso/vdso32/sysenter.S).
  77. // Used on Intel.
  78. // 0xffffe400 <__kernel_vsyscall+0>: push %ecx
  79. // 0xffffe401 <__kernel_vsyscall+1>: push %edx
  80. // 0xffffe402 <__kernel_vsyscall+2>: push %ebp
  81. // 0xffffe403 <__kernel_vsyscall+3>: mov %esp,%ebp
  82. // 0xffffe405 <__kernel_vsyscall+5>: sysenter
  83. //
  84. // SYSCALL (see linux-2.6.26/arch/x86/vdso/vdso32/syscall.S).
  85. // Used on AMD.
  86. // 0xffffe400 <__kernel_vsyscall+0>: push %ebp
  87. // 0xffffe401 <__kernel_vsyscall+1>: mov %ecx,%ebp
  88. // 0xffffe403 <__kernel_vsyscall+3>: syscall
  89. //
  90. // i386 (see linux-2.6.26/arch/x86/vdso/vdso32/int80.S)
  91. // 0xffffe400 <__kernel_vsyscall+0>: int $0x80
  92. // 0xffffe401 <__kernel_vsyscall+1>: ret
  93. //
  94. static const int kMaxBytes = 10;
  95. // We use assert()s instead of DCHECK()s -- this is too low level
  96. // for DCHECK().
  97. static int CountPushInstructions(const unsigned char *const addr) {
  98. int result = 0;
  99. for (int i = 0; i < kMaxBytes; ++i) {
  100. if (addr[i] == 0x89) {
  101. // "mov reg,reg"
  102. if (addr[i + 1] == 0xE5) {
  103. // Found "mov %esp,%ebp".
  104. return 0;
  105. }
  106. ++i; // Skip register encoding byte.
  107. } else if (addr[i] == 0x0F &&
  108. (addr[i + 1] == 0x34 || addr[i + 1] == 0x05)) {
  109. // Found "sysenter" or "syscall".
  110. return result;
  111. } else if ((addr[i] & 0xF0) == 0x50) {
  112. // Found "push %reg".
  113. ++result;
  114. } else if (addr[i] == 0xCD && addr[i + 1] == 0x80) {
  115. // Found "int $0x80"
  116. assert(result == 0);
  117. return 0;
  118. } else {
  119. // Unexpected instruction.
  120. assert(0 == "unexpected instruction in __kernel_vsyscall");
  121. return 0;
  122. }
  123. }
  124. // Unexpected: didn't find SYSENTER or SYSCALL in
  125. // [__kernel_vsyscall, __kernel_vsyscall + kMaxBytes) interval.
  126. assert(0 == "did not find SYSENTER or SYSCALL in __kernel_vsyscall");
  127. return 0;
  128. }
  129. #endif
  130. // Given a pointer to a stack frame, locate and return the calling
  131. // stackframe, or return NULL if no stackframe can be found. Perform sanity
  132. // checks (the strictness of which is controlled by the boolean parameter
  133. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  134. template<bool STRICT_UNWINDING, bool WITH_CONTEXT>
  135. static void **NextStackFrame(void **old_sp, const void *uc) {
  136. void **new_sp = (void **) *old_sp;
  137. #if defined(__linux__) && defined(__i386__) && defined(HAVE_VDSO_SUPPORT)
  138. if (WITH_CONTEXT && uc != NULL) {
  139. // How many "push %reg" instructions are there at __kernel_vsyscall?
  140. // This is constant for a given kernel and processor, so compute
  141. // it only once.
  142. static int num_push_instructions = -1; // Sentinel: not computed yet.
  143. // Initialize with sentinel value: __kernel_rt_sigreturn can not possibly
  144. // be there.
  145. static const unsigned char *kernel_rt_sigreturn_address = NULL;
  146. static const unsigned char *kernel_vsyscall_address = NULL;
  147. if (num_push_instructions == -1) {
  148. base::VDSOSupport vdso;
  149. if (vdso.IsPresent()) {
  150. base::VDSOSupport::SymbolInfo rt_sigreturn_symbol_info;
  151. base::VDSOSupport::SymbolInfo vsyscall_symbol_info;
  152. if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.5",
  153. STT_FUNC, &rt_sigreturn_symbol_info) ||
  154. !vdso.LookupSymbol("__kernel_vsyscall", "LINUX_2.5",
  155. STT_FUNC, &vsyscall_symbol_info) ||
  156. rt_sigreturn_symbol_info.address == NULL ||
  157. vsyscall_symbol_info.address == NULL) {
  158. // Unexpected: 32-bit VDSO is present, yet one of the expected
  159. // symbols is missing or NULL.
  160. assert(0 == "VDSO is present, but doesn't have expected symbols");
  161. num_push_instructions = 0;
  162. } else {
  163. kernel_rt_sigreturn_address =
  164. reinterpret_cast<const unsigned char *>(
  165. rt_sigreturn_symbol_info.address);
  166. kernel_vsyscall_address =
  167. reinterpret_cast<const unsigned char *>(
  168. vsyscall_symbol_info.address);
  169. num_push_instructions =
  170. CountPushInstructions(kernel_vsyscall_address);
  171. }
  172. } else {
  173. num_push_instructions = 0;
  174. }
  175. }
  176. if (num_push_instructions != 0 && kernel_rt_sigreturn_address != NULL &&
  177. old_sp[1] == kernel_rt_sigreturn_address) {
  178. const ucontext_t *ucv = static_cast<const ucontext_t *>(uc);
  179. // This kernel does not use frame pointer in its VDSO code,
  180. // and so %ebp is not suitable for unwinding.
  181. void **const reg_ebp =
  182. reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_EBP]);
  183. const unsigned char *const reg_eip =
  184. reinterpret_cast<unsigned char *>(ucv->uc_mcontext.gregs[REG_EIP]);
  185. if (new_sp == reg_ebp &&
  186. kernel_vsyscall_address <= reg_eip &&
  187. reg_eip - kernel_vsyscall_address < kMaxBytes) {
  188. // We "stepped up" to __kernel_vsyscall, but %ebp is not usable.
  189. // Restore from 'ucv' instead.
  190. void **const reg_esp =
  191. reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_ESP]);
  192. // Check that alleged %esp is not NULL and is reasonably aligned.
  193. if (reg_esp &&
  194. ((uintptr_t)reg_esp & (sizeof(reg_esp) - 1)) == 0) {
  195. // Check that alleged %esp is actually readable. This is to prevent
  196. // "double fault" in case we hit the first fault due to e.g. stack
  197. // corruption.
  198. //
  199. // page_size is linker-initalized to avoid async-unsafe locking
  200. // that GCC would otherwise insert (__cxa_guard_acquire etc).
  201. static int page_size;
  202. if (page_size == 0) {
  203. // First time through.
  204. page_size = getpagesize();
  205. }
  206. void *const reg_esp_aligned =
  207. reinterpret_cast<void *>(
  208. (uintptr_t)(reg_esp + num_push_instructions - 1) &
  209. ~(page_size - 1));
  210. if (msync(reg_esp_aligned, page_size, MS_ASYNC) == 0) {
  211. // Alleged %esp is readable, use it for further unwinding.
  212. new_sp = reinterpret_cast<void **>(
  213. reg_esp[num_push_instructions - 1]);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. #endif
  220. // Check that the transition from frame pointer old_sp to frame
  221. // pointer new_sp isn't clearly bogus
  222. if (STRICT_UNWINDING) {
  223. // With the stack growing downwards, older stack frame must be
  224. // at a greater address that the current one.
  225. if (new_sp <= old_sp) return NULL;
  226. // Assume stack frames larger than 100,000 bytes are bogus.
  227. if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
  228. } else {
  229. // In the non-strict mode, allow discontiguous stack frames.
  230. // (alternate-signal-stacks for example).
  231. if (new_sp == old_sp) return NULL;
  232. if (new_sp > old_sp) {
  233. // And allow frames upto about 1MB.
  234. const uintptr_t delta = (uintptr_t)new_sp - (uintptr_t)old_sp;
  235. const uintptr_t acceptable_delta = 1000000;
  236. if (delta > acceptable_delta) {
  237. return NULL;
  238. }
  239. }
  240. }
  241. if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
  242. #ifdef __i386__
  243. // On 64-bit machines, the stack pointer can be very close to
  244. // 0xffffffff, so we explicitly check for a pointer into the
  245. // last two pages in the address space
  246. if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
  247. #endif
  248. #ifdef HAVE_MMAP
  249. if (!STRICT_UNWINDING) {
  250. // Lax sanity checks cause a crash on AMD-based machines with
  251. // VDSO-enabled kernels.
  252. // Make an extra sanity check to insure new_sp is readable.
  253. // Note: NextStackFrame<false>() is only called while the program
  254. // is already on its last leg, so it's ok to be slow here.
  255. static int page_size = getpagesize();
  256. void *new_sp_aligned = (void *)((uintptr_t)new_sp & ~(page_size - 1));
  257. if (msync(new_sp_aligned, page_size, MS_ASYNC) == -1)
  258. return NULL;
  259. }
  260. #endif
  261. return new_sp;
  262. }
  263. #endif // BASE_STACKTRACE_X86_INL_H_
  264. // Note: this part of the file is included several times.
  265. // Do not put globals below.
  266. // The following 4 functions are generated from the code below:
  267. // GetStack{Trace,Frames}()
  268. // GetStack{Trace,Frames}WithContext()
  269. //
  270. // These functions take the following args:
  271. // void** result: the stack-trace, as an array
  272. // int* sizes: the size of each stack frame, as an array
  273. // (GetStackFrames* only)
  274. // int max_depth: the size of the result (and sizes) array(s)
  275. // int skip_count: how many stack pointers to skip before storing in result
  276. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  277. static int GET_STACK_TRACE_OR_FRAMES {
  278. void **sp;
  279. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2) || __llvm__
  280. // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8.
  281. // It's always correct on llvm, and the techniques below aren't (in
  282. // particular, llvm-gcc will make a copy of pcs, so it's not in sp[2]),
  283. // so we also prefer __builtin_frame_address when running under llvm.
  284. sp = reinterpret_cast<void**>(__builtin_frame_address(0));
  285. #elif defined(__i386__)
  286. // Stack frame format:
  287. // sp[0] pointer to previous frame
  288. // sp[1] caller address
  289. // sp[2] first argument
  290. // ...
  291. // NOTE: This will break under llvm, since result is a copy and not in sp[2]
  292. sp = (void **)&result - 2;
  293. #elif defined(__x86_64__)
  294. unsigned long rbp;
  295. // Move the value of the register %rbp into the local variable rbp.
  296. // We need 'volatile' to prevent this instruction from getting moved
  297. // around during optimization to before function prologue is done.
  298. // An alternative way to achieve this
  299. // would be (before this __asm__ instruction) to call Noop() defined as
  300. // static void Noop() __attribute__ ((noinline)); // prevent inlining
  301. // static void Noop() { asm(""); } // prevent optimizing-away
  302. __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
  303. // Arguments are passed in registers on x86-64, so we can't just
  304. // offset from &result
  305. sp = (void **) rbp;
  306. #else
  307. # error Using stacktrace_x86-inl.h on a non x86 architecture!
  308. #endif
  309. skip_count++; // skip parent's frame due to indirection in stacktrace.cc
  310. int n = 0;
  311. while (sp && n < max_depth) {
  312. if (*(sp+1) == reinterpret_cast<void *>(0)) {
  313. // In 64-bit code, we often see a frame that
  314. // points to itself and has a return address of 0.
  315. break;
  316. }
  317. #if !IS_WITH_CONTEXT
  318. const void *const ucp = NULL;
  319. #endif
  320. void **next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
  321. if (skip_count > 0) {
  322. skip_count--;
  323. } else {
  324. result[n] = *(sp+1);
  325. #if IS_STACK_FRAMES
  326. if (next_sp > sp) {
  327. sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
  328. } else {
  329. // A frame-size of 0 is used to indicate unknown frame size.
  330. sizes[n] = 0;
  331. }
  332. #endif
  333. n++;
  334. }
  335. sp = next_sp;
  336. }
  337. return n;
  338. }