8 #include "src/base/debug/stack_trace.h" 17 #include <sys/param.h> 19 #include <sys/types.h> 28 #if V8_LIBC_GLIBC || V8_LIBC_BSD || V8_LIBC_UCLIBC || V8_OS_SOLARIS 29 #define HAVE_EXECINFO_H 1 37 #include <AvailabilityMacros.h> 40 #include "src/base/build_config.h" 41 #include "src/base/free_deleter.h" 42 #include "src/base/logging.h" 43 #include "src/base/macros.h" 57 char* itoa_r(intptr_t
i,
char* buf,
size_t sz,
int base,
size_t padding);
63 volatile sig_atomic_t in_signal_handler = 0;
64 bool dump_stack_in_signal_handler =
true;
68 const char kMangledSymbolPrefix[] =
"_Z";
72 const char kSymbolCharacters[] =
73 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
81 void DemangleSymbols(std::string* text) {
86 std::string::size_type search_from = 0;
87 while (search_from < text->size()) {
89 std::string::size_type mangled_start =
90 text->find(kMangledSymbolPrefix, search_from);
91 if (mangled_start == std::string::npos) {
96 std::string::size_type mangled_end =
97 text->find_first_not_of(kSymbolCharacters, mangled_start);
98 if (mangled_end == std::string::npos) {
99 mangled_end = text->size();
101 std::string mangled_symbol =
102 text->substr(mangled_start, mangled_end - mangled_start);
106 std::unique_ptr<char, FreeDeleter> demangled_symbol(
107 abi::__cxa_demangle(mangled_symbol.c_str(),
nullptr,
nullptr, &status));
110 text->erase(mangled_start, mangled_end - mangled_start);
112 text->insert(mangled_start, demangled_symbol.get());
114 search_from = mangled_start + strlen(demangled_symbol.get());
117 search_from = mangled_start + 2;
121 #endif // HAVE_EXECINFO_H 123 class BacktraceOutputHandler {
125 virtual void HandleOutput(
const char* output) = 0;
128 virtual ~BacktraceOutputHandler() =
default;
132 void OutputPointer(
void* pointer, BacktraceOutputHandler* handler) {
135 char buf[17] = {
'\0'};
136 handler->HandleOutput(
"0x");
137 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf,
sizeof(buf), 16,
139 handler->HandleOutput(buf);
142 void ProcessBacktrace(
void*
const* trace,
size_t size,
143 BacktraceOutputHandler* handler) {
146 handler->HandleOutput(
"\n");
147 handler->HandleOutput(
"==== C stack trace ===============================\n");
148 handler->HandleOutput(
"\n");
150 bool printed =
false;
154 if (in_signal_handler == 0) {
155 std::unique_ptr<char*, FreeDeleter> trace_symbols(
156 backtrace_symbols(trace, static_cast<int>(size)));
157 if (trace_symbols.get()) {
158 for (
size_t i = 0;
i < size; ++
i) {
159 std::string trace_symbol = trace_symbols.get()[
i];
160 DemangleSymbols(&trace_symbol);
161 handler->HandleOutput(
" ");
162 handler->HandleOutput(trace_symbol.c_str());
163 handler->HandleOutput(
"\n");
171 for (
size_t i = 0;
i < size; ++
i) {
172 handler->HandleOutput(
" [");
173 OutputPointer(trace[
i], handler);
174 handler->HandleOutput(
"]\n");
178 #endif // HAVE_EXECINFO_H 180 void PrintToStderr(
const char* output) {
183 ssize_t return_val = write(STDERR_FILENO, output, strlen(output));
187 void StackDumpSignalHandler(
int signal, siginfo_t* info,
void* void_context) {
193 in_signal_handler = 1;
195 PrintToStderr(
"Received signal ");
196 char buf[1024] = {0};
197 internal::itoa_r(signal, buf,
sizeof(buf), 10, 0);
199 if (signal == SIGBUS) {
200 if (info->si_code == BUS_ADRALN)
201 PrintToStderr(
" BUS_ADRALN ");
202 else if (info->si_code == BUS_ADRERR)
203 PrintToStderr(
" BUS_ADRERR ");
204 else if (info->si_code == BUS_OBJERR)
205 PrintToStderr(
" BUS_OBJERR ");
207 PrintToStderr(
" <unknown> ");
208 }
else if (signal == SIGFPE) {
209 if (info->si_code == FPE_FLTDIV)
210 PrintToStderr(
" FPE_FLTDIV ");
211 else if (info->si_code == FPE_FLTINV)
212 PrintToStderr(
" FPE_FLTINV ");
213 else if (info->si_code == FPE_FLTOVF)
214 PrintToStderr(
" FPE_FLTOVF ");
215 else if (info->si_code == FPE_FLTRES)
216 PrintToStderr(
" FPE_FLTRES ");
217 else if (info->si_code == FPE_FLTSUB)
218 PrintToStderr(
" FPE_FLTSUB ");
219 else if (info->si_code == FPE_FLTUND)
220 PrintToStderr(
" FPE_FLTUND ");
221 else if (info->si_code == FPE_INTDIV)
222 PrintToStderr(
" FPE_INTDIV ");
223 else if (info->si_code == FPE_INTOVF)
224 PrintToStderr(
" FPE_INTOVF ");
226 PrintToStderr(
" <unknown> ");
227 }
else if (signal == SIGILL) {
228 if (info->si_code == ILL_BADSTK)
229 PrintToStderr(
" ILL_BADSTK ");
230 else if (info->si_code == ILL_COPROC)
231 PrintToStderr(
" ILL_COPROC ");
232 else if (info->si_code == ILL_ILLOPN)
233 PrintToStderr(
" ILL_ILLOPN ");
234 else if (info->si_code == ILL_ILLADR)
235 PrintToStderr(
" ILL_ILLADR ");
236 else if (info->si_code == ILL_ILLTRP)
237 PrintToStderr(
" ILL_ILLTRP ");
238 else if (info->si_code == ILL_PRVOPC)
239 PrintToStderr(
" ILL_PRVOPC ");
240 else if (info->si_code == ILL_PRVREG)
241 PrintToStderr(
" ILL_PRVREG ");
243 PrintToStderr(
" <unknown> ");
244 }
else if (signal == SIGSEGV) {
245 if (info->si_code == SEGV_MAPERR)
246 PrintToStderr(
" SEGV_MAPERR ");
247 else if (info->si_code == SEGV_ACCERR)
248 PrintToStderr(
" SEGV_ACCERR ");
250 PrintToStderr(
" <unknown> ");
252 if (signal == SIGBUS || signal == SIGFPE || signal == SIGILL ||
254 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), buf,
255 sizeof(buf), 16, 12);
259 if (dump_stack_in_signal_handler) {
260 debug::StackTrace().Print();
261 PrintToStderr(
"[end of stack trace]\n");
264 if (::signal(signal, SIG_DFL) == SIG_ERR) _exit(1);
267 class PrintBacktraceOutputHandler :
public BacktraceOutputHandler {
269 PrintBacktraceOutputHandler() =
default;
271 void HandleOutput(
const char* output)
override {
274 PrintToStderr(output);
278 DISALLOW_COPY_AND_ASSIGN(PrintBacktraceOutputHandler);
281 class StreamBacktraceOutputHandler :
public BacktraceOutputHandler {
283 explicit StreamBacktraceOutputHandler(std::ostream* os) : os_(os) {}
285 void HandleOutput(
const char* output)
override { (*os_) << output; }
290 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler);
293 void WarmUpBacktrace() {
323 StackTrace stack_trace;
328 bool EnableInProcessStackDumping() {
332 struct sigaction sigpipe_action;
333 memset(&sigpipe_action, 0,
sizeof(sigpipe_action));
334 sigpipe_action.sa_handler = SIG_IGN;
335 sigemptyset(&sigpipe_action.sa_mask);
336 bool success = (sigaction(SIGPIPE, &sigpipe_action,
nullptr) == 0);
341 struct sigaction action;
342 memset(&action, 0,
sizeof(action));
343 action.sa_flags = SA_RESETHAND | SA_SIGINFO;
344 action.sa_sigaction = &StackDumpSignalHandler;
345 sigemptyset(&action.sa_mask);
347 success &= (sigaction(SIGILL, &action,
nullptr) == 0);
348 success &= (sigaction(SIGABRT, &action,
nullptr) == 0);
349 success &= (sigaction(SIGFPE, &action,
nullptr) == 0);
350 success &= (sigaction(SIGBUS, &action,
nullptr) == 0);
351 success &= (sigaction(SIGSEGV, &action,
nullptr) == 0);
352 success &= (sigaction(SIGSYS, &action,
nullptr) == 0);
354 dump_stack_in_signal_handler =
true;
359 void DisableSignalStackDump() {
360 dump_stack_in_signal_handler =
false;
363 StackTrace::StackTrace() {
370 count_ =
static_cast<size_t>(backtrace(trace_, arraysize(trace_)));
376 void StackTrace::Print()
const {
381 PrintBacktraceOutputHandler handler;
382 ProcessBacktrace(trace_, count_, &handler);
386 void StackTrace::OutputToStream(std::ostream* os)
const {
388 StreamBacktraceOutputHandler handler(os);
389 ProcessBacktrace(trace_, count_, &handler);
396 char* itoa_r(intptr_t
i,
char* buf,
size_t sz,
int base,
size_t padding) {
399 if (n > sz)
return nullptr;
401 if (base < 2 || base > 16) {
411 if (
i < 0 && base == 10) {
434 *ptr++ =
"0123456789abcdef"[j % base];
437 if (padding > 0) padding--;
438 }
while (j > 0 || padding > 0);
447 while (--ptr > start) {