• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- backtrace_linux_libc.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 <execinfo.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "gwp_asan/optional/backtrace.h"
17 #include "gwp_asan/options.h"
18 
19 namespace {
Backtrace(uintptr_t * TraceBuffer,size_t Size)20 size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
21   static_assert(sizeof(uintptr_t) == sizeof(void *), "uintptr_t is not void*");
22 
23   return backtrace(reinterpret_cast<void **>(TraceBuffer), Size);
24 }
25 
26 // We don't need any custom handling for the Segv backtrace - the libc unwinder
27 // has no problems with unwinding through a signal handler. Force inlining here
28 // to avoid the additional frame.
SegvBacktrace(uintptr_t * TraceBuffer,size_t Size,void *)29 GWP_ASAN_ALWAYS_INLINE size_t SegvBacktrace(uintptr_t *TraceBuffer, size_t Size,
30                                             void * /*Context*/) {
31   return Backtrace(TraceBuffer, Size);
32 }
33 
PrintBacktrace(uintptr_t * Trace,size_t TraceLength,gwp_asan::crash_handler::Printf_t Printf)34 static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
35                            gwp_asan::crash_handler::Printf_t Printf) {
36   if (TraceLength == 0) {
37     Printf("  <not found (does your allocator support backtracing?)>\n\n");
38     return;
39   }
40 
41   char **BacktraceSymbols =
42       backtrace_symbols(reinterpret_cast<void **>(Trace), TraceLength);
43 
44   for (size_t i = 0; i < TraceLength; ++i) {
45     if (!BacktraceSymbols)
46       Printf("  #%zu %p\n", i, Trace[i]);
47     else
48       Printf("  #%zu %s\n", i, BacktraceSymbols[i]);
49   }
50 
51   Printf("\n");
52   if (BacktraceSymbols)
53     free(BacktraceSymbols);
54 }
55 } // anonymous namespace
56 
57 namespace gwp_asan {
58 namespace options {
getBacktraceFunction()59 Backtrace_t getBacktraceFunction() { return Backtrace; }
getPrintBacktraceFunction()60 crash_handler::PrintBacktrace_t getPrintBacktraceFunction() {
61   return PrintBacktrace;
62 }
63 } // namespace options
64 
65 namespace crash_handler {
getSegvBacktraceFunction()66 SegvBacktrace_t getSegvBacktraceFunction() { return SegvBacktrace; }
67 } // namespace crash_handler
68 } // namespace gwp_asan
69