tsan_symbolize.cc revision bc12f5d836b161b881de91e29548cfbc014243a3
1bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//===-- tsan_symbolize.cc -------------------------------------------------===//
2bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//
3bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//                     The LLVM Compiler Infrastructure
4bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//
5bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov// This file is distributed under the University of Illinois Open Source
6bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov// License. See LICENSE.TXT for details.
7bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//
8bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//===----------------------------------------------------------------------===//
9bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//
10bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov// This file is a part of ThreadSanitizer (TSan), a race detector.
11bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//
12bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov//===----------------------------------------------------------------------===//
13bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
14bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "tsan_symbolize.h"
15bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
16bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "sanitizer_common/sanitizer_common.h"
17bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "sanitizer_common/sanitizer_placement_new.h"
18bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "sanitizer_common/sanitizer_symbolizer.h"
19bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "tsan_flags.h"
20bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov#include "tsan_report.h"
21bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
22bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonovnamespace __tsan {
23bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
24bc12f5d836b161b881de91e29548cfbc014243a3Alexey SamsonovReportStack *NewReportStackEntry(uptr addr) {
25bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ReportStack *ent = (ReportStack*)internal_alloc(MBlockReportStack,
26bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov                                                  sizeof(ReportStack));
27bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  internal_memset(ent, 0, sizeof(*ent));
28bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ent->pc = addr;
29bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  return ent;
30bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov}
31bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
32bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonovstatic ReportStack *NewReportStackEntry(const AddressInfo &info) {
33bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ReportStack *ent = NewReportStackEntry(info.address);
34bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  if (info.module)
35bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    ent->module = internal_strdup(info.module);
36bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ent->offset = info.module_offset;
37bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  if (info.function) {
38bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    ent->func = internal_strdup(info.function);
39bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  }
40bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  if (info.file)
41bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    ent->file = internal_strdup(info.file);
42bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ent->line = info.line;
43bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  ent->col = info.column;
44bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  return ent;
45bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov}
46bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
47bc12f5d836b161b881de91e29548cfbc014243a3Alexey SamsonovReportStack *SymbolizeCode(uptr addr) {
48bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  if (flags()->use_internal_symbolizer) {
49bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    static const uptr kMaxAddrFrames = 16;
50bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    InternalScopedBuf<AddressInfo> addr_frames(kMaxAddrFrames);
51bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    for (uptr i = 0; i < kMaxAddrFrames; i++)
52bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      new(&addr_frames[i]) AddressInfo();
53bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    uptr addr_frames_num = __sanitizer::SymbolizeCode(addr, addr_frames,
54bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov                                                      kMaxAddrFrames);
55bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    if (addr_frames_num == 0)
56bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      return NewReportStackEntry(addr);
57bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    ReportStack *top = 0;
58bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    ReportStack *bottom = 0;
59bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    for (uptr i = 0; i < addr_frames_num; i++) {
60bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      ReportStack *cur_entry = NewReportStackEntry(addr_frames[i]);
61bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      CHECK(cur_entry);
62bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      addr_frames[i].Clear();
63bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      if (i == 0)
64bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov        top = cur_entry;
65bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      else
66bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov        bottom->next = cur_entry;
67bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov      bottom = cur_entry;
68bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    }
69bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov    return top;
70bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  }
71bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  return SymbolizeCodeAddr2Line(addr);
72bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov}
73bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
74bc12f5d836b161b881de91e29548cfbc014243a3Alexey SamsonovReportStack *SymbolizeData(uptr addr) {
75bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov  return SymbolizeDataAddr2Line(addr);
76bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov}
77bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov
78bc12f5d836b161b881de91e29548cfbc014243a3Alexey Samsonov}  // namespace __tsan
79