OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/debug/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
736 #endif | 736 #endif |
737 } | 737 } |
738 | 738 |
739 #if !defined(__UCLIBC__) | 739 #if !defined(__UCLIBC__) |
740 void StackTrace::OutputToStream(std::ostream* os) const { | 740 void StackTrace::OutputToStream(std::ostream* os) const { |
741 StreamBacktraceOutputHandler handler(os); | 741 StreamBacktraceOutputHandler handler(os); |
742 ProcessBacktrace(trace_, count_, &handler); | 742 ProcessBacktrace(trace_, count_, &handler); |
743 } | 743 } |
744 #endif | 744 #endif |
745 | 745 |
746 #if HAVE_TRACE_STACK_FRAME_POINTERS | |
747 | |
748 size_t TraceStackFramePointers(const void** out_trace, | |
749 size_t max_depth, | |
750 size_t skip_count) { | |
751 uintptr_t fp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); | |
752 | |
753 size_t depth = 0; | |
754 while (fp && depth < max_depth) { | |
755 if (skip_count != 0) { | |
756 skip_count--; | |
757 } else { | |
758 out_trace[depth++] = reinterpret_cast<const void**>(fp)[1]; | |
759 } | |
760 | |
761 // Find out next frame pointer | |
762 // (heuristics are from TCMalloc's stacktrace functions) | |
763 { | |
764 uintptr_t next_fp = reinterpret_cast<const uintptr_t*>(fp)[0]; | |
765 | |
766 // With the stack growing downwards, older stack frame must be | |
767 // at a greater address that the current one. | |
768 if (next_fp <= fp) break; | |
769 | |
770 // Assume stack frames larger than 100,000 bytes are bogus. | |
771 if (next_fp - fp > 100000) break; | |
772 | |
773 // Check alignment. | |
774 if (next_fp & (sizeof(void*) - 1)) break; | |
775 | |
776 #ifdef __i386__ | |
777 // On 64-bit machines, the stack pointer can be very close to | |
bcwhite
2016/04/13 12:36:24
You say 64-bit but have 32-bit numbers.
Dmitry Skiba
2016/04/15 07:31:56
This code came from TCMalloc's stacktrace_x86-inl.
| |
778 // 0xffffffff, so we explicitly check for a pointer into the | |
779 // last two pages in the address fpace | |
bcwhite
2016/04/13 12:36:24
What is special about the last two pages? Should
| |
780 if (next_fp >= 0xffffe000) break; | |
781 #endif | |
782 | |
783 fp = next_fp; | |
784 } | |
785 } | |
786 | |
787 return depth; | |
788 } | |
789 | |
790 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | |
791 | |
746 namespace internal { | 792 namespace internal { |
747 | 793 |
748 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 794 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
749 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { | 795 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { |
750 // Make sure we can write at least one NUL byte. | 796 // Make sure we can write at least one NUL byte. |
751 size_t n = 1; | 797 size_t n = 1; |
752 if (n > sz) | 798 if (n > sz) |
753 return NULL; | 799 return NULL; |
754 | 800 |
755 if (base < 2 || base > 16) { | 801 if (base < 2 || base > 16) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
804 *ptr = *start; | 850 *ptr = *start; |
805 *start++ = ch; | 851 *start++ = ch; |
806 } | 852 } |
807 return buf; | 853 return buf; |
808 } | 854 } |
809 | 855 |
810 } // namespace internal | 856 } // namespace internal |
811 | 857 |
812 } // namespace debug | 858 } // namespace debug |
813 } // namespace base | 859 } // namespace base |
OLD | NEW |