summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2012-06-15 14:00:25 +0000
committerAlexey Samsonov <samsonov@google.com>2012-06-15 14:00:25 +0000
commit0d76e8983f820d37496cc61eb2f2e8986274e3c9 (patch)
tree7c3d5c6c002ee39007c4e4207e47b010e62a26e9
parentf500be025afd6eb1d2ca98937d710b2d5105c9fa (diff)
downloadbcm5719-llvm-0d76e8983f820d37496cc61eb2f2e8986274e3c9.tar.gz
bcm5719-llvm-0d76e8983f820d37496cc61eb2f2e8986274e3c9.zip
[Sanitizer] Use ProcessMaps in symbolizer to get module name and offset for instruction address
llvm-svn: 158522
-rw-r--r--compiler-rt/lib/asan/asan_stack.cc27
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc115
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h21
3 files changed, 120 insertions, 43 deletions
diff --git a/compiler-rt/lib/asan/asan_stack.cc b/compiler-rt/lib/asan/asan_stack.cc
index e7246bd00e6..1c60565e85a 100644
--- a/compiler-rt/lib/asan/asan_stack.cc
+++ b/compiler-rt/lib/asan/asan_stack.cc
@@ -42,22 +42,27 @@ void AsanStackTrace::PrintStack(uptr *addr, uptr size) {
ProcessMaps proc_maps;
uptr frame_num = 0;
for (uptr i = 0; i < size && addr[i]; i++) {
- proc_maps.Reset();
uptr pc = addr[i];
- uptr offset;
- char filename[4096];
+ AddressInfo addr_frames[64];
+ uptr addr_frames_num = 0;
if (FLAG_symbolize) {
- AddressInfoList *address_info_list = SymbolizeCode(pc);
- for (AddressInfoList *entry = address_info_list; entry;
- entry = entry->next) {
- AddressInfo info = entry->info;
- AsanPrintf(" #%zu 0x%zx %s:%d:%d\n", frame_num, pc,
- (info.file) ? info.file : "",
- info.line, info.column);
+ addr_frames_num = SymbolizeCode(pc, addr_frames,
+ ASAN_ARRAY_SIZE(addr_frames));
+ }
+ if (addr_frames_num > 0) {
+ for (uptr j = 0; j < addr_frames_num; j++) {
+ AddressInfo &info = addr_frames[j];
+ AsanPrintf(" #%zu 0x%zx", frame_num, pc);
+ if (info.module) {
+ AsanPrintf(" (%s+0x%zx)", info.module, info.module_offset);
+ }
+ AsanPrintf("\n");
+ info.Clear();
frame_num++;
}
- address_info_list->Clear();
} else {
+ uptr offset;
+ char filename[4096];
if (proc_maps.GetObjectNameAndOffset(pc, &offset,
filename, sizeof(filename))) {
AsanPrintf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, filename,
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc
index b0845fa3ccb..d7bb65294ef 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc
@@ -13,6 +13,8 @@
//===----------------------------------------------------------------------===//
#include "sanitizer_common.h"
+#include "sanitizer_placement_new.h"
+#include "sanitizer_procmaps.h"
#include "sanitizer_symbolizer.h"
namespace __sanitizer {
@@ -21,30 +23,103 @@ void AddressInfo::Clear() {
InternalFree(module);
InternalFree(function);
InternalFree(file);
+ internal_memset(this, 0, sizeof(AddressInfo));
}
-void AddressInfoList::Clear() {
- AddressInfoList *cur = this;
- while (cur) {
- cur->info.Clear();
- AddressInfoList *nxt = cur->next;
- InternalFree(cur);
- cur = nxt;
+static const int kMaxModuleNameLength = 4096;
+
+struct ModuleDesc {
+ ModuleDesc *next;
+ uptr start;
+ uptr end;
+ uptr offset;
+ char *full_name;
+ char *name;
+
+ ModuleDesc(uptr _start, uptr _end, uptr _offset, const char *module_name) {
+ next = 0;
+ start = _start;
+ end = _end;
+ offset = _offset;
+ full_name = internal_strdup(module_name);
+ name = internal_strrchr(module_name, '/');
+ if (name == 0) {
+ name = full_name;
+ } else {
+ name++;
+ }
}
-}
+};
+
+class Symbolizer {
+ public:
+ void GetModuleDescriptions() {
+ ProcessMaps proc_maps;
+ uptr start, end, offset;
+ char *module_name = (char*)InternalAlloc(kMaxModuleNameLength);
+ ModuleDesc *prev_module = 0;
+ while (proc_maps.Next(&start, &end, &offset, module_name,
+ kMaxModuleNameLength)) {
+ void *mem = InternalAlloc(sizeof(ModuleDesc));
+ ModuleDesc *cur_module = new(mem) ModuleDesc(start, end, offset,
+ module_name);
+ if (!prev_module) {
+ modules_ = cur_module;
+ } else {
+ prev_module->next = cur_module;
+ }
+ prev_module = cur_module;
+ }
+ InternalFree(module_name);
+ }
+
+ uptr SymbolizeCode(uptr addr, AddressInfo *frames, uptr max_frames) {
+ if (max_frames == 0)
+ return 0;
+ AddressInfo *info = &frames[0];
+ info->Clear();
+ info->address = addr;
+ if (modules_ == 0) {
+ GetModuleDescriptions();
+ }
+ bool first = true;
+ for (ModuleDesc *module = modules_; module; module = module->next) {
+ if (addr >= module->start && addr < module->end) {
+ info->module = internal_strdup(module->full_name);
+ // Don't subtract 'start' for the first entry:
+ // * If a binary is compiled w/o -pie, then the first entry in
+ // process maps is likely the binary itself (all dynamic libs
+ // are mapped higher in address space). For such a binary,
+ // instruction offset in binary coincides with the actual
+ // instruction address in virtual memory (as code section
+ // is mapped to a fixed memory range).
+ // * If a binary is compiled with -pie, all the modules are
+ // mapped high at address space (in particular, higher than
+ // shadow memory of the tool), so the module can't be the
+ // first entry.
+ info->module_offset = (addr - (first ? 0 : module->start)) +
+ module->offset;
+ // FIXME: Fill other fields here as well: create debug
+ // context for a given module and fetch file/line info from it.
+ info->function = 0;
+ info->file = 0;
+ info->line = 0;
+ info->column = 0;
+ return 1;
+ }
+ first = false;
+ }
+ return 0;
+ }
+
+ private:
+ ModuleDesc *modules_; // List of module descriptions is leaked.
+};
+
+static Symbolizer symbolizer;
-AddressInfoList* SymbolizeCode(uptr address) {
- AddressInfoList *list = (AddressInfoList*)InternalAlloc(
- sizeof(AddressInfoList));
- list->next = 0;
- list->info.address = address;
- list->info.module = 0;
- list->info.module_offset = 0;
- list->info.function = 0;
- list->info.file = 0;
- list->info.line = 0;
- list->info.column = 0;
- return list;
+uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames) {
+ return symbolizer.SymbolizeCode(address, frames, max_frames);
}
} // namespace __sanitizer
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
index 17b3f6a97dd..c6104cd5d4f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
@@ -39,21 +39,18 @@ struct AddressInfo {
int line;
int column;
- // Deletes all strings.
+ AddressInfo() {
+ internal_memset(this, 0, sizeof(AddressInfo));
+ }
+ // Deletes all strings and sets all fields to zero.
void Clear();
};
-struct AddressInfoList {
- AddressInfoList *next;
- AddressInfo info;
-
- // Deletes all nodes in a list.
- void Clear();
-};
-
-// Returns a list of descriptions for a given address (in all inlined
-// functions). The ownership is transferred to the caller.
-AddressInfoList* SymbolizeCode(uptr address);
+// Fills at most "max_frames" elements of "frames" with descriptions
+// for a given address (in all inlined functions). Returns the number
+// of descriptions actually filled.
+// This function should NOT be called from two threads simultaneously.
+uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames);
} // namespace __sanitizer
OpenPOWER on IntegriCloud