blob: 823942af53556385cf75f5bf624c4ff505b0a5ec [file] [log] [blame]
mistergc2e75482017-09-19 16:54:40 -04001// 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
19template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
20static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
21 const void *ucp, int *min_dropped_frames) {
Abseil Teamfcb10452018-12-04 11:01:12 -080022 static_cast<void>(ucp); // Unused.
mistergc2e75482017-09-19 16:54:40 -040023 static const int kStackLength = 64;
24 void * stack[kStackLength];
25 int size;
26
27 size = backtrace(stack, kStackLength);
28 skip_count++; // we want to skip the current frame as well
29 int result_count = size - skip_count;
30 if (result_count < 0)
31 result_count = 0;
32 if (result_count > max_depth)
33 result_count = max_depth;
34 for (int i = 0; i < result_count; i++)
35 result[i] = stack[i + skip_count];
36
37 if (IS_STACK_FRAMES) {
38 // No implementation for finding out the stack frame sizes yet.
39 memset(sizes, 0, sizeof(*sizes) * result_count);
40 }
41 if (min_dropped_frames != nullptr) {
42 if (size - skip_count - max_depth > 0) {
43 *min_dropped_frames = size - skip_count - max_depth;
44 } else {
45 *min_dropped_frames = 0;
46 }
47 }
48
49 return result_count;
50}
51
Abseil Team5a8de8a2018-01-17 09:53:47 -080052namespace absl {
Abseil Teamfcb10452018-12-04 11:01:12 -080053inline namespace lts_2018_12_18 {
Abseil Team5a8de8a2018-01-17 09:53:47 -080054namespace debugging_internal {
55bool StackTraceWorksForTest() {
56 return true;
57}
58} // namespace debugging_internal
Abseil Teamfcb10452018-12-04 11:01:12 -080059} // inline namespace lts_2018_12_18
Abseil Team5a8de8a2018-01-17 09:53:47 -080060} // namespace absl
61
mistergc2e75482017-09-19 16:54:40 -040062#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_