Skip to content

Commit 66187f5

Browse files
[llvm-debuginfo-analyzer] Add support for LLVM IR format.
Add support for the LLVM IR format and be able to generate logical views. Both textual representation (.ll) and bitcode (.bc) format are supported. Note: This patch requires: Add DebugSSAUpdater class to track debug value liveness llvm#135349
1 parent 8460dbb commit 66187f5

32 files changed

+4756
-23
lines changed

llvm/docs/CommandGuide/llvm-debuginfo-analyzer.rst

Lines changed: 165 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ SYNOPSIS
1313
DESCRIPTION
1414
-----------
1515
:program:`llvm-debuginfo-analyzer` parses debug and text sections in
16-
binary object files and prints their contents in a logical view, which
17-
is a human-readable representation that closely matches the structure
18-
of the original user source code. Supported object file formats include
19-
ELF, Mach-O, WebAssembly, PDB and COFF.
16+
binary object files and textual IR representations and prints their
17+
contents in a logical view, which is a human readable representation
18+
that closely matches the structure of the original user source code.
19+
Supported object file formats include ELF, Mach-O, WebAssembly, PDB,
20+
COFF, IR (textual representation and bitcode).
2021

2122
The **logical view** abstracts the complexity associated with the
2223
different low-level representations of the debugging information that
@@ -2131,6 +2132,138 @@ layout and given the number of matches.
21312132
-----------------------------
21322133
Total 71 8
21332134
2135+
IR (Textual representation and bitcode) SUPPORT
2136+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2137+
The below example is used to show the IR output generated by
2138+
:program:`llvm-debuginfo-analyzer`. We compiled the example for a
2139+
IR 64-bit target with Clang (-O0 -g --target=x86_64-linux):
2140+
2141+
.. code-block:: c++
2142+
2143+
1 using INTPTR = const int *;
2144+
2 int foo(INTPTR ParamPtr, unsigned ParamUnsigned, bool ParamBool) {
2145+
3 if (ParamBool) {
2146+
4 typedef int INTEGER;
2147+
5 const INTEGER CONSTANT = 7;
2148+
6 return CONSTANT;
2149+
7 }
2150+
8 return ParamUnsigned;
2151+
9 }
2152+
2153+
PRINT BASIC DETAILS
2154+
^^^^^^^^^^^^^^^^^^^
2155+
The following command prints basic details for all the logical elements
2156+
sorted by the debug information internal offset; it includes its lexical
2157+
level and debug info format.
2158+
2159+
.. code-block:: none
2160+
2161+
llvm-debuginfo-analyzer --attribute=level,format
2162+
--output-sort=offset
2163+
--print=scopes,symbols,types,lines,instructions
2164+
test-clang.ll
2165+
2166+
or
2167+
2168+
.. code-block:: none
2169+
2170+
llvm-debuginfo-analyzer --attribute=level,format
2171+
--output-sort=offset
2172+
--print=elements
2173+
test-clang.ll
2174+
2175+
Each row represents an element that is present within the debug
2176+
information. The first column represents the scope level, followed by
2177+
the associated line number (if any), and finally the description of
2178+
the element.
2179+
2180+
.. code-block:: none
2181+
2182+
Logical View:
2183+
[000] {File} 'test-clang.ll' -> Textual IR
2184+
2185+
[001] {CompileUnit} 'test.cpp'
2186+
[002] 2 {Function} extern not_inlined 'foo' -> 'int'
2187+
[003] {Block}
2188+
[004] 5 {Variable} 'CONSTANT' -> 'const INTEGER'
2189+
[004] 5 {Line}
2190+
[004] {Code} 'store i32 7, ptr %CONSTANT, align 4, !dbg !32'
2191+
[004] 6 {Line}
2192+
[004] {Code} 'store i32 7, ptr %retval, align 4, !dbg !33'
2193+
[004] 6 {Line}
2194+
[004] {Code} 'br label %return, !dbg !33'
2195+
[003] 2 {Parameter} 'ParamPtr' -> 'INTPTR'
2196+
[003] 2 {Parameter} 'ParamUnsigned' -> 'unsigned int'
2197+
[003] 2 {Parameter} 'ParamBool' -> 'bool'
2198+
[003] 4 {TypeAlias} 'INTEGER' -> 'int'
2199+
[003] 2 {Line}
2200+
[003] {Code} '%retval = alloca i32, align 4'
2201+
[003] {Code} '%ParamPtr.addr = alloca ptr, align 8'
2202+
[003] {Code} '%ParamUnsigned.addr = alloca i32, align 4'
2203+
[003] {Code} '%ParamBool.addr = alloca i8, align 1'
2204+
[003] {Code} '%CONSTANT = alloca i32, align 4'
2205+
[003] {Code} 'store ptr %ParamPtr, ptr %ParamPtr.addr, align 8'
2206+
[003] {Code} 'store i32 %ParamUnsigned, ptr %ParamUnsigned.addr, align 4'
2207+
[003] {Code} '%storedv = zext i1 %ParamBool to i8'
2208+
[003] {Code} 'store i8 %storedv, ptr %ParamBool.addr, align 1'
2209+
[003] 8 {Line}
2210+
[003] {Code} '%1 = load i32, ptr %ParamUnsigned.addr, align 4, !dbg !34'
2211+
[003] 8 {Line}
2212+
[003] {Code} 'store i32 %1, ptr %retval, align 4, !dbg !35'
2213+
[003] 8 {Line}
2214+
[003] {Code} 'br label %return, !dbg !35'
2215+
[003] 9 {Line}
2216+
[003] {Code} '%2 = load i32, ptr %retval, align 4, !dbg !36'
2217+
[003] 9 {Line}
2218+
[003] {Code} 'ret i32 %2, !dbg !36'
2219+
[003] 3 {Line}
2220+
[003] 3 {Line}
2221+
[003] 3 {Line}
2222+
[003] {Code} 'br i1 %loadedv, label %if.then, label %if.end, !dbg !26'
2223+
[002] 1 {TypeAlias} 'INTPTR' -> '* const int'
2224+
2225+
SELECT LOGICAL ELEMENTS
2226+
^^^^^^^^^^^^^^^^^^^^^^^
2227+
The following prints all *instructions*, *symbols* and *types* that
2228+
contain **'block'** or **'.store'** in their names or types, using a tab
2229+
layout and given the number of matches.
2230+
2231+
.. code-block:: none
2232+
2233+
llvm-debuginfo-analyzer --attribute=level
2234+
--select-nocase --select-regex
2235+
--select=LOAD --select=store
2236+
--report=list
2237+
--print=symbols,types,instructions,summary
2238+
test-clang.ll
2239+
2240+
Logical View:
2241+
[000] {File} 'test-clang.ll'
2242+
2243+
[001] {CompileUnit} 'test.cpp'
2244+
[003] {Code} '%0 = load i8, ptr %ParamBool.addr, align 1, !dbg !26'
2245+
[003] {Code} '%1 = load i32, ptr %ParamUnsigned.addr, align 4, !dbg !34'
2246+
[003] {Code} '%2 = load i32, ptr %retval, align 4, !dbg !36'
2247+
[004] {Code} '%loadedv = trunc i8 %0 to i1, !dbg !26'
2248+
[003] {Code} '%storedv = zext i1 %ParamBool to i8'
2249+
[003] {Code} 'br i1 %loadedv, label %if.then, label %if.end, !dbg !26'
2250+
[003] {Code} 'store i32 %1, ptr %retval, align 4, !dbg !35'
2251+
[003] {Code} 'store i32 %ParamUnsigned, ptr %ParamUnsigned.addr, align 4'
2252+
[004] {Code} 'store i32 7, ptr %CONSTANT, align 4, !dbg !32'
2253+
[004] {Code} 'store i32 7, ptr %retval, align 4, !dbg !33'
2254+
[003] {Code} 'store i8 %storedv, ptr %ParamBool.addr, align 1'
2255+
[003] {Code} 'store ptr %ParamPtr, ptr %ParamPtr.addr, align 8'
2256+
2257+
-----------------------------
2258+
Element Total Printed
2259+
-----------------------------
2260+
Scopes 5 0
2261+
Symbols 4 0
2262+
Types 2 0
2263+
Lines 22 12
2264+
-----------------------------
2265+
Total 33 12
2266+
21342267
COMPARISON MODE
21352268
^^^^^^^^^^^^^^^
21362269
Given the previous example we found the above debug information issue
@@ -2204,6 +2337,34 @@ giving more context by swapping the reference and target object files.
22042337
The output shows the merging view path (reference and target) with the
22052338
missing and added elements.
22062339

2340+
.. code-block:: none
2341+
2342+
llvm-debuginfo-analyzer --attribute=level,format
2343+
--compare=types
2344+
--report=view
2345+
--print=symbols,types
2346+
test-clang.bc test-dwarf-gcc.o
2347+
2348+
Reference: 'test-clang.bc'
2349+
Target: 'test-dwarf-gcc.o'
2350+
2351+
Logical View:
2352+
[000] {File} 'test-clang.bc' -> Bitcode IR
2353+
2354+
[001] {CompileUnit} 'test.cpp'
2355+
[002] 1 {TypeAlias} 'INTPTR' -> '* const int'
2356+
[002] 2 {Function} extern not_inlined 'foo' -> 'int'
2357+
[003] {Block}
2358+
[004] 5 {Variable} 'CONSTANT' -> 'const INTEGER'
2359+
+[004] 4 {TypeAlias} 'INTEGER' -> 'int'
2360+
[003] 2 {Parameter} 'ParamBool' -> 'bool'
2361+
[003] 2 {Parameter} 'ParamPtr' -> 'INTPTR'
2362+
[003] 2 {Parameter} 'ParamUnsigned' -> 'unsigned int'
2363+
-[003] 4 {TypeAlias} 'INTEGER' -> 'int'
2364+
2365+
The same output but this time comparing the Clang bitcode with the
2366+
binary object (DWARF) generated by GCC.
2367+
22072368
LOGICAL ELEMENTS
22082369
""""""""""""""""
22092370
It compares individual logical elements without considering if their

llvm/include/llvm/DebugInfo/LogicalView/Core/LVSupport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ template <typename T> class LVProperties {
119119
#define KIND_3(ENUM, FIELD, F1, F2, F3) \
120120
BOOL_BIT_3(Kinds, ENUM, FIELD, F1, F2, F3)
121121

122+
const int DEC_WIDTH = 8;
123+
inline FormattedNumber decValue(uint64_t N, unsigned Width = DEC_WIDTH) {
124+
return format_decimal(N, Width);
125+
}
126+
127+
// Output the decimal representation of 'Value'.
128+
inline std::string decString(uint64_t Value, size_t Width = DEC_WIDTH) {
129+
std::string String;
130+
raw_string_ostream Stream(String);
131+
Stream << decValue(Value, Width);
132+
return Stream.str();
133+
}
134+
122135
const int HEX_WIDTH = 12;
123136
inline FormattedNumber hexValue(uint64_t N, unsigned Width = HEX_WIDTH,
124137
bool Upper = false) {

llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
1818
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
1919
#include "llvm/Object/Archive.h"
20+
#include "llvm/Object/IRObjectFile.h"
2021
#include "llvm/Object/MachOUniversal.h"
2122
#include "llvm/Object/ObjectFile.h"
2223
#include "llvm/Support/Compiler.h"
@@ -30,7 +31,9 @@ namespace logicalview {
3031

3132
using LVReaders = std::vector<std::unique_ptr<LVReader>>;
3233
using ArgVector = std::vector<std::string>;
33-
using PdbOrObj = PointerUnion<object::ObjectFile *, pdb::PDBFile *>;
34+
using PdbOrObjOrIr =
35+
PointerUnion<object::ObjectFile *, pdb::PDBFile *, object::IRObjectFile *,
36+
MemoryBufferRef *, StringRef *>;
3437

3538
// This class performs the following tasks:
3639
// - Creates a logical reader for every binary file in the command line,
@@ -61,9 +64,12 @@ class LVReaderHandler {
6164
object::Binary &Binary);
6265
Error handleObject(LVReaders &Readers, StringRef Filename, StringRef Buffer,
6366
StringRef ExePath);
67+
Error handleObject(LVReaders &Readers, StringRef Filename,
68+
MemoryBufferRef Buffer);
6469

65-
Error createReader(StringRef Filename, LVReaders &Readers, PdbOrObj &Input,
66-
StringRef FileFormatName, StringRef ExePath = {});
70+
Error createReader(StringRef Filename, LVReaders &Readers,
71+
PdbOrObjOrIr &Input, StringRef FileFormatName,
72+
StringRef ExePath = {});
6773

6874
public:
6975
LVReaderHandler() = delete;

llvm/include/llvm/DebugInfo/LogicalView/Readers/LVBinaryReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/MC/MCSubtargetInfo.h"
2626
#include "llvm/MC/TargetRegistry.h"
2727
#include "llvm/Object/COFF.h"
28+
#include "llvm/Object/IRObjectFile.h"
2829
#include "llvm/Object/ObjectFile.h"
2930

3031
namespace llvm {

0 commit comments

Comments
 (0)