#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/debug/stack_trace.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <tuple>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_IOS) && defined(OFFICIAL_BUILD)
#define HAVE_DLADDR
#include <dlfcn.h>
#endif
#if BUILDFLAG(IS_APPLE) || \
(defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__AIX))
#define HAVE_BACKTRACE
#include <execinfo.h>
#endif
#if !defined(USE_SYMBOLIZE) && defined(HAVE_BACKTRACE) && !defined(HAVE_DLADDR)
#define DEMANGLE_SYMBOLS
#endif
#if defined(DEMANGLE_SYMBOLS)
#include <cxxabi.h>
#endif
#if BUILDFLAG(IS_APPLE)
#include <AvailabilityMacros.h>
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <sys/prctl.h>
#include "base/debug/proc_maps_linux.h"
#endif
#include "base/cfi_buildflags.h"
#include "base/debug/debugger.h"
#include "base/debug/debugging_buildflags.h"
#include "base/debug/stack_trace.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/memory/free_deleter.h"
#include "base/memory/singleton.h"
#include "base/numerics/safe_conversions.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#if defined(USE_SYMBOLIZE)
#include "base/third_party/symbolize/symbolize.h"
#if BUILDFLAG(ENABLE_STACK_TRACE_LINE_NUMBERS)
#include "base/debug/dwarf_line_no.h"
#endif
#endif
namespace base::debug {
namespace {
volatile sig_atomic_t in_signal_handler = …;
#if !BUILDFLAG(IS_NACL)
bool (*try_handle_signal)(int, siginfo_t*, void*) = …;
#endif
#if defined(DEMANGLE_SYMBOLS)
const char kMangledSymbolPrefix[] = "_Z";
const char kSymbolCharacters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
void DemangleSymbols(std::string* text) {
std::string::size_type search_from = 0;
while (search_from < text->size()) {
std::string::size_type mangled_start =
text->find(kMangledSymbolPrefix, search_from);
if (mangled_start == std::string::npos) {
break;
}
std::string::size_type mangled_end =
text->find_first_not_of(kSymbolCharacters, mangled_start);
if (mangled_end == std::string::npos) {
mangled_end = text->size();
}
std::string mangled_symbol =
text->substr(mangled_start, mangled_end - mangled_start);
int status = 0;
std::unique_ptr<char, base::FreeDeleter> demangled_symbol(
abi::__cxa_demangle(mangled_symbol.c_str(), nullptr, 0, &status));
if (status == 0) {
text->erase(mangled_start, mangled_end - mangled_start);
text->insert(mangled_start, demangled_symbol.get());
search_from = mangled_start + strlen(demangled_symbol.get());
} else {
search_from = mangled_start + 2;
}
}
}
#endif
class BacktraceOutputHandler { … };
#if defined(HAVE_BACKTRACE)
void OutputPointer(const void* pointer, BacktraceOutputHandler* handler) { … }
#if defined(HAVE_DLADDR) || defined(USE_SYMBOLIZE)
void OutputValue(size_t value, BacktraceOutputHandler* handler) { … }
#endif
#if defined(USE_SYMBOLIZE)
void OutputFrameId(size_t frame_id, BacktraceOutputHandler* handler) { … }
#endif
void ProcessBacktrace(span<const void* const> traces,
cstring_view prefix_string,
BacktraceOutputHandler* handler) { … }
#endif
void PrintToStderr(const char* output) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
void AlarmSignalHandler(int signal, siginfo_t* info, void* void_context) { … }
#endif
void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { … }
class PrintBacktraceOutputHandler : public BacktraceOutputHandler { … };
class StreamBacktraceOutputHandler : public BacktraceOutputHandler { … };
void WarmUpBacktrace() { … }
#if defined(USE_SYMBOLIZE)
class SandboxSymbolizeHelper { … };
#endif
}
bool EnableInProcessStackDumping() { … }
#if !BUILDFLAG(IS_NACL)
bool SetStackDumpFirstChanceCallback(bool (*handler)(int, siginfo_t*, void*)) { … }
#endif
size_t CollectStackTrace(span<const void*> trace) { … }
void StackTrace::PrintMessageWithPrefix(cstring_view prefix_string,
cstring_view message) { … }
void StackTrace::PrintWithPrefixImpl(cstring_view prefix_string) const { … }
#if defined(HAVE_BACKTRACE)
void StackTrace::OutputToStreamWithPrefixImpl(
std::ostream* os,
cstring_view prefix_string) const { … }
#endif
namespace internal {
char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { … }
}
}