misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame^] | 1 | // Copyright 2000 - 2007 Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Author: Sanjay Ghemawat |
| 5 | // |
| 6 | // Portable implementation - just use glibc |
| 7 | // |
| 8 | // Note: The glibc implementation may cause a call to malloc. |
| 9 | // This can cause a deadlock in HeapProfiler. |
| 10 | |
| 11 | #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_ |
| 12 | #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_ |
| 13 | |
| 14 | #include <execinfo.h> |
| 15 | #include <cstring> |
| 16 | |
| 17 | #include "absl/debugging/stacktrace.h" |
| 18 | |
| 19 | template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> |
| 20 | static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, |
| 21 | const void *ucp, int *min_dropped_frames) { |
| 22 | static const int kStackLength = 64; |
| 23 | void * stack[kStackLength]; |
| 24 | int size; |
| 25 | |
| 26 | size = backtrace(stack, kStackLength); |
| 27 | skip_count++; // we want to skip the current frame as well |
| 28 | int result_count = size - skip_count; |
| 29 | if (result_count < 0) |
| 30 | result_count = 0; |
| 31 | if (result_count > max_depth) |
| 32 | result_count = max_depth; |
| 33 | for (int i = 0; i < result_count; i++) |
| 34 | result[i] = stack[i + skip_count]; |
| 35 | |
| 36 | if (IS_STACK_FRAMES) { |
| 37 | // No implementation for finding out the stack frame sizes yet. |
| 38 | memset(sizes, 0, sizeof(*sizes) * result_count); |
| 39 | } |
| 40 | if (min_dropped_frames != nullptr) { |
| 41 | if (size - skip_count - max_depth > 0) { |
| 42 | *min_dropped_frames = size - skip_count - max_depth; |
| 43 | } else { |
| 44 | *min_dropped_frames = 0; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return result_count; |
| 49 | } |
| 50 | |
| 51 | #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_ |