blob: 5d7492a47c1bd207bff1940ac25cf308efe0c799 [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) {
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
Abseil Team5a8de8a2018-01-17 09:53:47 -080051namespace absl {
Abseil Team6c7de162018-06-20 06:25:23 -070052inline namespace lts_2018_06_20 {
Abseil Team5a8de8a2018-01-17 09:53:47 -080053namespace debugging_internal {
54bool StackTraceWorksForTest() {
55 return true;
56}
57} // namespace debugging_internal
Abseil Team6c7de162018-06-20 06:25:23 -070058} // inline namespace lts_2018_06_20
Abseil Team5a8de8a2018-01-17 09:53:47 -080059} // namespace absl
60
mistergc2e75482017-09-19 16:54:40 -040061#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_