[XRay] Implement `llvm-xray extract`, start of the llvm-xray tool

Usage:

  llvm-xray extract <object file> [-o <filename or '-'>]

The tool gets the XRay instrumentation map from an object file and turns
it into YAML.  We first support ELF64 sleds on x86_64 binaries, with
provision for supporting other supported platforms and formats later.

This is the first of a many-part change to fully implement the
`llvm-xray` tool.

We also define a subcommand registration and dispatch mechanism to be
used by other further subcommand implementations for llvm-xray.

Diffusion Revision: https://reviews.llvm.org/D21987

llvm-svn: 285165
diff --git a/llvm/test/lit.cfg b/llvm/test/lit.cfg
index 780f639..9c038b4 100644
--- a/llvm/test/lit.cfg
+++ b/llvm/test/lit.cfg
@@ -310,6 +310,7 @@
                 r"\bllvm-tblgen\b",
                 r"\bllvm-c-test\b",
                 r"\bllvm-cxxfilt\b",
+                r"\bllvm-xray\b",
                 NOJUNK + r"\bllvm-symbolizer\b",
                 NOJUNK + r"\bopt\b",
                 r"\bFileCheck\b",
diff --git a/llvm/test/tools/llvm-xray/X86/Inputs/elf32-noxray.bin b/llvm/test/tools/llvm-xray/X86/Inputs/elf32-noxray.bin
new file mode 100755
index 0000000..06446e7
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/Inputs/elf32-noxray.bin
Binary files differ
diff --git a/llvm/test/tools/llvm-xray/X86/Inputs/elf64-badentrysizes.bin b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-badentrysizes.bin
new file mode 100755
index 0000000..702ea14
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-badentrysizes.bin
Binary files differ
diff --git a/llvm/test/tools/llvm-xray/X86/Inputs/elf64-example.bin b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-example.bin
new file mode 100755
index 0000000..a6e6d3d
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-example.bin
Binary files differ
diff --git a/llvm/test/tools/llvm-xray/X86/Inputs/elf64-noinstr-map.bin b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-noinstr-map.bin
new file mode 100755
index 0000000..6f2d9d2
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/Inputs/elf64-noinstr-map.bin
Binary files differ
diff --git a/llvm/test/tools/llvm-xray/X86/Inputs/empty-file.bin b/llvm/test/tools/llvm-xray/X86/Inputs/empty-file.bin
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/Inputs/empty-file.bin
diff --git a/llvm/test/tools/llvm-xray/X86/bad-instrmap-sizes.bin b/llvm/test/tools/llvm-xray/X86/bad-instrmap-sizes.bin
new file mode 100644
index 0000000..4ea3351
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/bad-instrmap-sizes.bin
@@ -0,0 +1,3 @@
+; RUN: not llvm-xray extract %S/Inputs/elf64-badentrysizes.bin 2>&1 | FileCheck %s
+; CHECK: llvm-xray: Cannot extract instrumentation map from '{{.*}}elf64-badentrysizes.bin'.
+; CHECK-NEXT: Instrumentation map entries not evenly divisible by size of an XRay sled entry in ELF64.
diff --git a/llvm/test/tools/llvm-xray/X86/empty.txt b/llvm/test/tools/llvm-xray/X86/empty.txt
new file mode 100644
index 0000000..65dd2b7
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/empty.txt
@@ -0,0 +1,4 @@
+; RUN: not llvm-xray extract %S/Inputs/empty-file.bin 2>&1 | FileCheck %s
+
+; CHECK: llvm-xray: Cannot extract instrumentation map from '{{.*}}empty-file.bin'.
+; CHECK-NEXT: The file was not recognized as a valid object file
diff --git a/llvm/test/tools/llvm-xray/X86/extract-instrmap.ll b/llvm/test/tools/llvm-xray/X86/extract-instrmap.ll
new file mode 100644
index 0000000..8155d57
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/extract-instrmap.ll
@@ -0,0 +1,15 @@
+; This test makes sure we can extract the instrumentation map from an
+; XRay-instrumented object file.
+;
+; RUN: llvm-xray extract %S/Inputs/elf64-example.bin | FileCheck %s
+
+; CHECK:      ---
+; CHECK-NEXT: - { id: 1, address: 0x000000000041C900, function: 0x000000000041C900, kind: function-enter,
+; CHECK-NEXT:     always-instrument: true }
+; CHECK-NEXT: - { id: 1, address: 0x000000000041C912, function: 0x000000000041C900, kind: function-exit,
+; CHECK-NEXT:     always-instrument: true }
+; CHECK-NEXT: - { id: 2, address: 0x000000000041C930, function: 0x000000000041C930, kind: function-enter,
+; CHECK-NEXT:    always-instrument: true }
+; CHECK-NEXT: - { id: 2, address: 0x000000000041C946, function: 0x000000000041C930, kind: function-exit,
+; CHECK-NEXT:     always-instrument: true }
+; CHECK-NEXT: ...
diff --git a/llvm/test/tools/llvm-xray/X86/lit.local.cfg b/llvm/test/tools/llvm-xray/X86/lit.local.cfg
new file mode 100644
index 0000000..4f00369
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/lit.local.cfg
@@ -0,0 +1 @@
+config.suffixes = ['.yaml', '.ll', '.txt']
diff --git a/llvm/test/tools/llvm-xray/X86/no-instr-map.txt b/llvm/test/tools/llvm-xray/X86/no-instr-map.txt
new file mode 100644
index 0000000..c256c07
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/no-instr-map.txt
@@ -0,0 +1,4 @@
+; RUN: not llvm-xray extract %S/Inputs/elf64-noinstr-map.bin 2>&1 | FileCheck %s
+
+; CHECK: llvm-xray: Cannot extract instrumentation map from '{{.*}}elf64-noinstr-map.bin'.
+; CHECK-NEXT: Failed to find XRay instrumentation map.
diff --git a/llvm/test/tools/llvm-xray/X86/no-such-file.txt b/llvm/test/tools/llvm-xray/X86/no-such-file.txt
new file mode 100644
index 0000000..0bc0b5b
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/no-such-file.txt
@@ -0,0 +1,4 @@
+; RUN: not llvm-xray extract no-such-file 2>&1 | FileCheck %s
+
+; CHECK: llvm-xray: Cannot extract instrumentation map from 'no-such-file'.
+; CHECK-NEXT: No such file or directory
diff --git a/llvm/test/tools/llvm-xray/X86/unsupported-elf32.txt b/llvm/test/tools/llvm-xray/X86/unsupported-elf32.txt
new file mode 100644
index 0000000..1dde473
--- /dev/null
+++ b/llvm/test/tools/llvm-xray/X86/unsupported-elf32.txt
@@ -0,0 +1,3 @@
+; RUN: not llvm-xray extract %S/Inputs/elf32-noxray.bin 2>&1 | FileCheck %s
+; CHECK: llvm-xray: Cannot extract instrumentation map from '{{.*}}elf32-noxray.bin'.
+; CHECK-NEXT: File format not supported (only does ELF little endian 64-bit).