Skip to content

Commit 6854229

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 #135349
1 parent 4b42b62 commit 6854229

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

2337+
.. code-block:: none
2338+
2339+
llvm-debuginfo-analyzer --attribute=level,format
2340+
--compare=types
2341+
--report=view
2342+
--print=symbols,types
2343+
test-clang.bc test-dwarf-gcc.o
2344+
2345+
Reference: 'test-clang.bc'
2346+
Target: 'test-dwarf-gcc.o'
2347+
2348+
Logical View:
2349+
[000] {File} 'test-clang.bc' -> Bitcode IR
2350+
2351+
[001] {CompileUnit} 'test.cpp'
2352+
[002] 1 {TypeAlias} 'INTPTR' -> '* const int'
2353+
[002] 2 {Function} extern not_inlined 'foo' -> 'int'
2354+
[003] {Block}
2355+
[004] 5 {Variable} 'CONSTANT' -> 'const INTEGER'
2356+
+[004] 4 {TypeAlias} 'INTEGER' -> 'int'
2357+
[003] 2 {Parameter} 'ParamBool' -> 'bool'
2358+
[003] 2 {Parameter} 'ParamPtr' -> 'INTPTR'
2359+
[003] 2 {Parameter} 'ParamUnsigned' -> 'unsigned int'
2360+
-[003] 4 {TypeAlias} 'INTEGER' -> 'int'
2361+
2362+
The same output but this time comparing the Clang bitcode with the
2363+
binary object (DWARF) generated by GCC.
2364+
22042365
LOGICAL ELEMENTS
22052366
""""""""""""""""
22062367
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
@@ -99,6 +99,19 @@ template <typename T> class LVProperties {
9999
#define KIND_3(ENUM, FIELD, F1, F2, F3) \
100100
BOOL_BIT_3(Kinds, ENUM, FIELD, F1, F2, F3)
101101

102+
const int DEC_WIDTH = 8;
103+
inline FormattedNumber decValue(uint64_t N, unsigned Width = DEC_WIDTH) {
104+
return format_decimal(N, Width);
105+
}
106+
107+
// Output the decimal representation of 'Value'.
108+
inline std::string decString(uint64_t Value, size_t Width = DEC_WIDTH) {
109+
std::string String;
110+
raw_string_ostream Stream(String);
111+
Stream << decValue(Value, Width);
112+
return Stream.str();
113+
}
114+
102115
const int HEX_WIDTH = 12;
103116
inline FormattedNumber hexValue(uint64_t N, unsigned Width = HEX_WIDTH,
104117
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/MemoryBuffer.h"
@@ -29,7 +30,9 @@ namespace logicalview {
2930

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

3437
// This class performs the following tasks:
3538
// - Creates a logical reader for every binary file in the command line,
@@ -60,9 +63,12 @@ class LVReaderHandler {
6063
object::Binary &Binary);
6164
Error handleObject(LVReaders &Readers, StringRef Filename, StringRef Buffer,
6265
StringRef ExePath);
66+
Error handleObject(LVReaders &Readers, StringRef Filename,
67+
MemoryBufferRef Buffer);
6368

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

6773
public:
6874
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)