backtrace_sanitizer_common.cpp revision 353358
1//===-- backtrace_sanitizer_common.cpp --------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <assert.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <string.h>
13
14#include "gwp_asan/optional/backtrace.h"
15#include "gwp_asan/options.h"
16#include "sanitizer_common/sanitizer_stacktrace.h"
17
18void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
19                                                 void *context,
20                                                 bool request_fast,
21                                                 u32 max_depth) {
22  if (!StackTrace::WillUseFastUnwind(request_fast)) {
23    return Unwind(max_depth, pc, bp, context, 0, 0, request_fast);
24  }
25  Unwind(max_depth, pc, 0, context, 0, 0, false);
26}
27
28namespace {
29void Backtrace(uintptr_t *TraceBuffer, size_t Size) {
30  __sanitizer::BufferedStackTrace Trace;
31  Trace.Reset();
32  if (Size > __sanitizer::kStackTraceMax)
33    Size = __sanitizer::kStackTraceMax;
34
35  Trace.Unwind((__sanitizer::uptr)__builtin_return_address(0),
36               (__sanitizer::uptr)__builtin_frame_address(0),
37               /* ucontext */ nullptr,
38               /* fast unwind */ true, Size - 1);
39
40  memcpy(TraceBuffer, Trace.trace, Trace.size * sizeof(uintptr_t));
41  TraceBuffer[Trace.size] = 0;
42}
43
44static void PrintBacktrace(uintptr_t *Trace,
45                           gwp_asan::options::Printf_t Printf) {
46  __sanitizer::StackTrace StackTrace;
47  StackTrace.trace = reinterpret_cast<__sanitizer::uptr *>(Trace);
48
49  for (StackTrace.size = 0; StackTrace.size < __sanitizer::kStackTraceMax;
50       ++StackTrace.size) {
51    if (Trace[StackTrace.size] == 0)
52      break;
53  }
54
55  if (StackTrace.size == 0) {
56    Printf("  <unknown (does your allocator support backtracing?)>\n\n");
57    return;
58  }
59
60  StackTrace.Print();
61}
62} // anonymous namespace
63
64namespace gwp_asan {
65namespace options {
66Backtrace_t getBacktraceFunction() { return Backtrace; }
67PrintBacktrace_t getPrintBacktraceFunction() { return PrintBacktrace; }
68} // namespace options
69} // namespace gwp_asan
70