Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 1 | //===-- sanitizer_stacktrace_libcdep.cc -----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is shared between AddressSanitizer and ThreadSanitizer |
| 11 | // run-time libraries. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 14 | #include "sanitizer_common.h" |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 15 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 16 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 17 | #include "sanitizer_stacktrace_printer.h" |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 18 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __sanitizer { |
| 21 | |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 22 | void StackTrace::Print() const { |
| 23 | if (trace == nullptr || size == 0) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 24 | Printf(" <empty stack>\n\n"); |
| 25 | return; |
| 26 | } |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 27 | const int kMaxAddrFrames = 64; |
| 28 | InternalScopedBuffer<AddressInfo> addr_frames(kMaxAddrFrames); |
| 29 | for (uptr i = 0; i < kMaxAddrFrames; i++) |
| 30 | new(&addr_frames[i]) AddressInfo(); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 31 | InternalScopedString frame_desc(GetPageSizeCached() * 2); |
| 32 | uptr frame_num = 0; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 33 | for (uptr i = 0; i < size && trace[i]; i++) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 34 | // PCs in stack traces are actually the return addresses, that is, |
| 35 | // addresses of the next instructions after the call. |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 36 | uptr pc = GetPreviousInstructionPc(trace[i]); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 37 | uptr addr_frames_num = Symbolizer::GetOrInit()->SymbolizePC( |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 38 | pc, addr_frames.data(), kMaxAddrFrames); |
Sergey Matveev | 2be4a28 | 2014-05-23 16:04:41 +0000 | [diff] [blame] | 39 | if (addr_frames_num == 0) { |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 40 | addr_frames[0].address = pc; |
| 41 | addr_frames_num = 1; |
Sergey Matveev | 2be4a28 | 2014-05-23 16:04:41 +0000 | [diff] [blame] | 42 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 43 | for (uptr j = 0; j < addr_frames_num; j++) { |
| 44 | AddressInfo &info = addr_frames[j]; |
| 45 | frame_desc.clear(); |
Alexey Samsonov | fbaaed6 | 2014-11-06 18:43:45 +0000 | [diff] [blame^] | 46 | RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++, |
| 47 | info, common_flags()->strip_path_prefix); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 48 | Printf("%s\n", frame_desc.data()); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 49 | info.Clear(); |
| 50 | } |
| 51 | } |
| 52 | // Always print a trailing empty line after stack trace. |
| 53 | Printf("\n"); |
| 54 | } |
| 55 | |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 56 | void BufferedStackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context, |
| 57 | uptr stack_top, uptr stack_bottom, |
| 58 | bool request_fast_unwind) { |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 59 | top_frame_bp = (max_depth > 0) ? bp : 0; |
| 60 | // Avoid doing any work for small max_depth. |
| 61 | if (max_depth == 0) { |
| 62 | size = 0; |
| 63 | return; |
| 64 | } |
| 65 | if (max_depth == 1) { |
| 66 | size = 1; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 67 | trace_buffer[0] = pc; |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 70 | if (!WillUseFastUnwind(request_fast_unwind)) { |
| 71 | if (context) |
| 72 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 73 | else |
| 74 | SlowUnwindStack(pc, max_depth); |
Evgeniy Stepanov | 2629e57 | 2014-02-11 13:45:01 +0000 | [diff] [blame] | 75 | } else { |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 76 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 77 | } |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace __sanitizer |