OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/base/debug/stack_trace.h" |
| 6 |
| 7 #include <signal.h> |
| 8 #include <stddef.h> |
| 9 #include <string.h> |
| 10 #include <unwind.h> |
| 11 |
| 12 #include <src/base/platform/platform.h> |
| 13 |
| 14 #include <iomanip> |
| 15 #include <ostream> |
| 16 |
| 17 namespace { |
| 18 |
| 19 struct StackCrawlState { |
| 20 StackCrawlState(uintptr_t* frames, size_t max_depth) |
| 21 : frames(frames), |
| 22 frame_count(0), |
| 23 max_depth(max_depth), |
| 24 have_skipped_self(false) {} |
| 25 |
| 26 uintptr_t* frames; |
| 27 size_t frame_count; |
| 28 size_t max_depth; |
| 29 bool have_skipped_self; |
| 30 }; |
| 31 |
| 32 _Unwind_Reason_Code TraceStackFrame(_Unwind_Context* context, void* arg) { |
| 33 StackCrawlState* state = static_cast<StackCrawlState*>(arg); |
| 34 uintptr_t ip = _Unwind_GetIP(context); |
| 35 |
| 36 // The first stack frame is this function itself. Skip it. |
| 37 if (ip != 0 && !state->have_skipped_self) { |
| 38 state->have_skipped_self = true; |
| 39 return _URC_NO_REASON; |
| 40 } |
| 41 |
| 42 state->frames[state->frame_count++] = ip; |
| 43 if (state->frame_count >= state->max_depth) |
| 44 return _URC_END_OF_STACK; |
| 45 return _URC_NO_REASON; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 namespace v8 { |
| 51 namespace base { |
| 52 namespace debug { |
| 53 |
| 54 bool EnableInProcessStackDumping() { |
| 55 // When running in an application, our code typically expects SIGPIPE |
| 56 // to be ignored. Therefore, when testing that same code, it should run |
| 57 // with SIGPIPE ignored as well. |
| 58 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. |
| 59 struct sigaction action; |
| 60 memset(&action, 0, sizeof(action)); |
| 61 action.sa_handler = SIG_IGN; |
| 62 sigemptyset(&action.sa_mask); |
| 63 return (sigaction(SIGPIPE, &action, NULL) == 0); |
| 64 } |
| 65 |
| 66 void DisableSignalStackDump() { |
| 67 } |
| 68 |
| 69 StackTrace::StackTrace() { |
| 70 StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
| 71 _Unwind_Backtrace(&TraceStackFrame, &state); |
| 72 count_ = state.frame_count; |
| 73 } |
| 74 |
| 75 void StackTrace::Print() const { |
| 76 std::string backtrace = ToString(); |
| 77 OS::Print("%s\n", backtrace.c_str()); |
| 78 } |
| 79 |
| 80 void StackTrace::OutputToStream(std::ostream* os) const { |
| 81 for (size_t i = 0; i < count_; ++i) { |
| 82 *os << "#" << std::setw(2) << i << trace_[i] << "\n"; |
| 83 } |
| 84 } |
| 85 |
| 86 } // namespace debug |
| 87 } // namespace base |
| 88 } // namespace v8 |
OLD | NEW |