OSDN Git Service

[Xray] tooling allow MachO format support
authorDavid Carlier <devnexen@gmail.com>
Mon, 10 Sep 2018 05:00:43 +0000 (05:00 +0000)
committerDavid Carlier <devnexen@gmail.com>
Mon, 10 Sep 2018 05:00:43 +0000 (05:00 +0000)
Getting writable xray __DATA sections from MachO as well.

Reviewers: dberris

Reviewed By: dberris

Differential Revision: https://reviews.llvm.org/D51758

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341772 91177308-0d34-0410-b5e6-96231b3b80d8

lib/XRay/InstrumentationMap.cpp
test/tools/llvm-xray/X86/Inputs/instr-map-mach.o [new file with mode: 0644]
test/tools/llvm-xray/X86/extract-instrmap-macho.ll [new file with mode: 0644]

index a7d6600..8431770 100644 (file)
@@ -47,18 +47,18 @@ Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
 }
 
 static Error
-loadELF64(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
+loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
           InstrumentationMap::SledContainer &Sleds,
           InstrumentationMap::FunctionAddressMap &FunctionAddresses,
           InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
   InstrumentationMap Map;
 
   // Find the section named "xray_instr_map".
-  if (!ObjFile.getBinary()->isELF() ||
+  if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
       !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
         ObjFile.getBinary()->getArch() == Triple::ppc64le))
     return make_error<StringError>(
-        "File format not supported (only does ELF little endian 64-bit).",
+        "File format not supported (only does ELF and Mach-O little endian 64-bit).",
         std::make_error_code(std::errc::not_supported));
 
   StringRef Contents = "";
@@ -191,7 +191,7 @@ llvm::xray::loadInstrumentationMap(StringRef Filename) {
     if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds,
                           Map.FunctionAddresses, Map.FunctionIds))
       return std::move(E);
-  } else if (auto E = loadELF64(Filename, *ObjectFileOrError, Map.Sleds,
+  } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
                                 Map.FunctionAddresses, Map.FunctionIds)) {
     return std::move(E);
   }
diff --git a/test/tools/llvm-xray/X86/Inputs/instr-map-mach.o b/test/tools/llvm-xray/X86/Inputs/instr-map-mach.o
new file mode 100644 (file)
index 0000000..44614fe
Binary files /dev/null and b/test/tools/llvm-xray/X86/Inputs/instr-map-mach.o differ
diff --git a/test/tools/llvm-xray/X86/extract-instrmap-macho.ll b/test/tools/llvm-xray/X86/extract-instrmap-macho.ll
new file mode 100644 (file)
index 0000000..a423079
--- /dev/null
@@ -0,0 +1,9 @@
+; This test makes sure we can extract the instrumentation map from an
+; XRay-instrumented object file.
+;
+; RUN: llvm-xray extract %S/Inputs/instr-map-mach.o -s | FileCheck %s
+
+; CHECK:      ---
+; CHECK-NEXT: - { id: 1, address: 0x0000000000000000, function: 0x0000000000000000, kind: function-enter, always-instrument: true, function-name: 'task(void*)' }
+; CHECK-NEXT: - { id: 1, address: 0x0000000000000162, function: 0x0000000000000000, kind: function-exit, always-instrument: true, function-name: 'task(void*)' }
+; CHECK-NEXT: ...