[llvm-commits] [compiler-rt] r158522 - in /compiler-rt/trunk/lib: asan/asan_stack.cc sanitizer_common/sanitizer_symbolizer.cc sanitizer_common/sanitizer_symbolizer.h

Alexey Samsonov samsonov at google.com
Fri Jun 15 07:00:26 PDT 2012


Author: samsonov
Date: Fri Jun 15 09:00:25 2012
New Revision: 158522

URL: http://llvm.org/viewvc/llvm-project?rev=158522&view=rev
Log:
[Sanitizer] Use ProcessMaps in symbolizer to get module name and offset for instruction address

Modified:
    compiler-rt/trunk/lib/asan/asan_stack.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h

Modified: compiler-rt/trunk/lib/asan/asan_stack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_stack.cc?rev=158522&r1=158521&r2=158522&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_stack.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_stack.cc Fri Jun 15 09:00:25 2012
@@ -42,22 +42,27 @@
   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,

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc?rev=158522&r1=158521&r2=158522&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc Fri Jun 15 09:00:25 2012
@@ -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 @@
   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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h?rev=158522&r1=158521&r2=158522&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h Fri Jun 15 09:00:25 2012
@@ -39,21 +39,18 @@
   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
 





More information about the llvm-commits mailing list