blob: 05198a78703931d1c591b5b8b2a1d1ef306a095b [file] [log] [blame]
Kuba Brecka58f44dc2014-07-15 17:33:23 +00001//===-- asan_debugging.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 a part of AddressSanitizer, an address sanity checker.
11//
12// This file contains various functions that are generally useful to call when
13// using a debugger (LLDB, GDB).
14//===----------------------------------------------------------------------===//
15
16#include "asan_allocator.h"
17#include "asan_flags.h"
18#include "asan_internal.h"
19#include "asan_mapping.h"
20#include "asan_thread.h"
21
22namespace __asan {
23
24uptr AsanGetStack(uptr addr, uptr *trace, uptr size, u32 *thread_id,
25 bool alloc_stack) {
26 AsanChunkView chunk = FindHeapChunkByAddress(addr);
27 if (!chunk.IsValid()) return 0;
28
29 StackTrace stack;
30 if (alloc_stack) {
31 if (chunk.AllocTid() == kInvalidTid) return 0;
32 chunk.GetAllocStack(&stack);
33 if (thread_id) *thread_id = chunk.AllocTid();
34 } else {
35 if (chunk.FreeTid() == kInvalidTid) return 0;
36 chunk.GetFreeStack(&stack);
37 if (thread_id) *thread_id = chunk.FreeTid();
38 }
39
40 if (trace && size) {
41 if (size > kStackTraceMax)
42 size = kStackTraceMax;
43 if (size > stack.size)
44 size = stack.size;
45 for (uptr i = 0; i < size; i++)
46 trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);
47
48 return size;
49 }
50
51 return 0;
52}
53
54} // namespace __asan
55
56using namespace __asan;
57
58SANITIZER_INTERFACE_ATTRIBUTE
59uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
60 return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true);
61}
62
63SANITIZER_INTERFACE_ATTRIBUTE
64uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
65 return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false);
66}
67
68SANITIZER_INTERFACE_ATTRIBUTE
69void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {
70 if (shadow_scale)
71 *shadow_scale = SHADOW_SCALE;
72 if (shadow_offset)
73 *shadow_offset = SHADOW_OFFSET;
74}