1 //===-- tsan_symbolize.cc -------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "tsan_symbolize.h"
13
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "sanitizer_common/sanitizer_placement_new.h"
16 #include "sanitizer_common/sanitizer_symbolizer.h"
17 #include "tsan_flags.h"
18 #include "tsan_report.h"
19 #include "tsan_rtl.h"
20
21 namespace __tsan {
22
EnterSymbolizer()23 void EnterSymbolizer() {
24 ThreadState *thr = cur_thread();
25 CHECK(!thr->in_symbolizer);
26 thr->in_symbolizer = true;
27 thr->ignore_interceptors++;
28 }
29
ExitSymbolizer()30 void ExitSymbolizer() {
31 ThreadState *thr = cur_thread();
32 CHECK(thr->in_symbolizer);
33 thr->in_symbolizer = false;
34 thr->ignore_interceptors--;
35 }
36
37 // Legacy API.
38 // May be overriden by JIT/JAVA/etc,
39 // whatever produces PCs marked with kExternalPCBit.
40 SANITIZER_WEAK_DEFAULT_IMPL
__tsan_symbolize_external(uptr pc,char * func_buf,uptr func_siz,char * file_buf,uptr file_siz,int * line,int * col)41 bool __tsan_symbolize_external(uptr pc, char *func_buf, uptr func_siz,
42 char *file_buf, uptr file_siz, int *line,
43 int *col) {
44 return false;
45 }
46
47 // New API: call __tsan_symbolize_external_ex only when it exists.
48 // Once old clients are gone, provide dummy implementation.
49 SANITIZER_WEAK_DEFAULT_IMPL
__tsan_symbolize_external_ex(uptr pc,void (* add_frame)(void *,const char *,const char *,int,int),void * ctx)50 void __tsan_symbolize_external_ex(uptr pc,
51 void (*add_frame)(void *, const char *,
52 const char *, int, int),
53 void *ctx) {}
54
55 struct SymbolizedStackBuilder {
56 SymbolizedStack *head;
57 SymbolizedStack *tail;
58 uptr addr;
59 };
60
AddFrame(void * ctx,const char * function_name,const char * file,int line,int column)61 static void AddFrame(void *ctx, const char *function_name, const char *file,
62 int line, int column) {
63 SymbolizedStackBuilder *ssb = (struct SymbolizedStackBuilder *)ctx;
64 if (ssb->tail) {
65 ssb->tail->next = SymbolizedStack::New(ssb->addr);
66 ssb->tail = ssb->tail->next;
67 } else {
68 ssb->head = ssb->tail = SymbolizedStack::New(ssb->addr);
69 }
70 AddressInfo *info = &ssb->tail->info;
71 if (function_name) {
72 info->function = internal_strdup(function_name);
73 }
74 if (file) {
75 info->file = internal_strdup(file);
76 }
77 info->line = line;
78 info->column = column;
79 }
80
SymbolizeCode(uptr addr)81 SymbolizedStack *SymbolizeCode(uptr addr) {
82 // Check if PC comes from non-native land.
83 if (addr & kExternalPCBit) {
84 SymbolizedStackBuilder ssb = {nullptr, nullptr, addr};
85 __tsan_symbolize_external_ex(addr, AddFrame, &ssb);
86 if (ssb.head)
87 return ssb.head;
88 // Legacy code: remove along with the declaration above
89 // once all clients using this API are gone.
90 // Declare static to not consume too much stack space.
91 // We symbolize reports in a single thread, so this is fine.
92 static char func_buf[1024];
93 static char file_buf[1024];
94 int line, col;
95 SymbolizedStack *frame = SymbolizedStack::New(addr);
96 if (__tsan_symbolize_external(addr, func_buf, sizeof(func_buf), file_buf,
97 sizeof(file_buf), &line, &col)) {
98 frame->info.function = internal_strdup(func_buf);
99 frame->info.file = internal_strdup(file_buf);
100 frame->info.line = line;
101 frame->info.column = col;
102 }
103 return frame;
104 }
105 return Symbolizer::GetOrInit()->SymbolizePC(addr);
106 }
107
SymbolizeData(uptr addr)108 ReportLocation *SymbolizeData(uptr addr) {
109 DataInfo info;
110 if (!Symbolizer::GetOrInit()->SymbolizeData(addr, &info))
111 return 0;
112 ReportLocation *ent = ReportLocation::New(ReportLocationGlobal);
113 internal_memcpy(&ent->global, &info, sizeof(info));
114 return ent;
115 }
116
SymbolizeFlush()117 void SymbolizeFlush() {
118 Symbolizer::GetOrInit()->Flush();
119 }
120
121 } // namespace __tsan
122