Skip to content

Commit 2ebc27a

Browse files
committed
[Symbolize] Always use filename:line from debug info when debug info for the given address is available.
1 parent d76efd8 commit 2ebc27a

File tree

2 files changed

+192
-10
lines changed

2 files changed

+192
-10
lines changed

llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,12 @@ SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
276276
if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
277277
ModuleOffset.SectionIndex =
278278
getModuleSectionIndexForAddress(ModuleOffset.Address);
279-
DILineInfo LineInfo =
280-
DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
281-
279+
DILineInfo LineInfo;
280+
std::optional<DILineInfo> DBGLineInfo =
281+
DebugInfoContext->getOptionalLineInfoForAddress(ModuleOffset,
282+
LineInfoSpecifier);
283+
if (DBGLineInfo)
284+
LineInfo = *DBGLineInfo;
282285
// Override function name from symbol table if necessary.
283286
if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
284287
std::string FunctionName, FileName;
@@ -287,7 +290,7 @@ SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
287290
FileName)) {
288291
LineInfo.FunctionName = FunctionName;
289292
LineInfo.StartAddress = Start;
290-
if (LineInfo.FileName == DILineInfo::BadString && !FileName.empty())
293+
if (!DBGLineInfo && !FileName.empty())
291294
LineInfo.FileName = FileName;
292295
}
293296
}
@@ -304,8 +307,11 @@ DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
304307
ModuleOffset, LineInfoSpecifier);
305308

306309
// Make sure there is at least one frame in context.
307-
if (InlinedContext.getNumberOfFrames() == 0)
310+
bool EmptyFrameAdded = false;
311+
if (InlinedContext.getNumberOfFrames() == 0) {
312+
EmptyFrameAdded = true;
308313
InlinedContext.addFrame(DILineInfo());
314+
}
309315

310316
// Override the function name in lower frame with name from symbol table.
311317
if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
@@ -317,7 +323,7 @@ DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
317323
InlinedContext.getNumberOfFrames() - 1);
318324
LI->FunctionName = FunctionName;
319325
LI->StartAddress = Start;
320-
if (LI->FileName == DILineInfo::BadString && !FileName.empty())
326+
if (EmptyFrameAdded && !FileName.empty())
321327
LI->FileName = FileName;
322328
}
323329
}
@@ -334,10 +340,11 @@ DIGlobal SymbolizableObjectFile::symbolizeData(
334340
Res.DeclFile = FileName;
335341

336342
// Try and get a better filename:lineno pair from the debuginfo, if present.
337-
DILineInfo DL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
338-
if (DL.Line != 0) {
339-
Res.DeclFile = DL.FileName;
340-
Res.DeclLine = DL.Line;
343+
std::optional<DILineInfo> DL =
344+
DebugInfoContext->getOptionalLineInfoForDataAddress(ModuleOffset);
345+
if (DL) {
346+
Res.DeclFile = DL->FileName;
347+
Res.DeclLine = DL->Line;
341348
}
342349
return Res;
343350
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Test llvm-symbolizer always uses line info from debug info if present.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-symbolizer --obj=%t 0x1 | FileCheck %s
4+
5+
# CHECK: foo(bool)
6+
# CHECK-NEXT: ??:0:0
7+
8+
--- !ELF
9+
FileHeader:
10+
Class: ELFCLASS64
11+
Data: ELFDATA2LSB
12+
Type: ET_REL
13+
Machine: EM_X86_64
14+
SectionHeaderStringTable: .strtab
15+
Sections:
16+
- Name: .text
17+
Type: SHT_PROGBITS
18+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
19+
AddressAlign: 0x10
20+
Content: 50E80000000031C059C3
21+
- Name: .debug_abbrev
22+
Type: SHT_PROGBITS
23+
AddressAlign: 0x1
24+
Content: 01110025251305032572171017111B12067317000000
25+
- Name: .debug_info
26+
Type: SHT_PROGBITS
27+
AddressAlign: 0x1
28+
Content: 1E000000050001080000000001002100010000000000000000000A00000000000000
29+
- Name: .debug_str_offsets
30+
Type: SHT_PROGBITS
31+
AddressAlign: 0x1
32+
Content: 0C000000050000000000000000000000
33+
- Name: .debug_line
34+
Type: SHT_PROGBITS
35+
AddressAlign: 0x1
36+
Content: 5C0000000500080037000000010101FB0E0D00010101010000000100000101011F010000000003011F020F051E0100000000009C5BB3AA3D0567AB9CB3F5A35C9F9B230400000902000000000000000013061E0505060A5F060B2E0202000101
37+
- Name: .debug_line_str
38+
Type: SHT_PROGBITS
39+
Flags: [ SHF_MERGE, SHF_STRINGS ]
40+
AddressAlign: 0x1
41+
EntSize: 0x1
42+
Content: 003C696E76616C69643E00
43+
- Name: .rela.text
44+
Type: SHT_RELA
45+
Flags: [ SHF_INFO_LINK ]
46+
Link: .symtab
47+
AddressAlign: 0x8
48+
Info: .text
49+
Relocations:
50+
- Offset: 0x2
51+
Symbol: _Z3barv
52+
Type: R_X86_64_PLT32
53+
Addend: -4
54+
- Name: .rela.debug_info
55+
Type: SHT_RELA
56+
Flags: [ SHF_INFO_LINK ]
57+
Link: .symtab
58+
AddressAlign: 0x8
59+
Info: .debug_info
60+
Relocations:
61+
- Offset: 0x8
62+
Symbol: .debug_abbrev
63+
Type: R_X86_64_32
64+
- Offset: 0x11
65+
Symbol: .debug_str_offsets
66+
Type: R_X86_64_32
67+
Addend: 8
68+
- Offset: 0x15
69+
Symbol: .debug_line
70+
Type: R_X86_64_32
71+
- Offset: 0x1E
72+
Symbol: .debug_addr
73+
Type: R_X86_64_32
74+
Addend: 8
75+
- Name: .rela.debug_str_offsets
76+
Type: SHT_RELA
77+
Flags: [ SHF_INFO_LINK ]
78+
Link: .symtab
79+
AddressAlign: 0x8
80+
Info: .debug_str_offsets
81+
Relocations:
82+
- Offset: 0x8
83+
Symbol: .debug_str
84+
Type: R_X86_64_32
85+
- Offset: 0xC
86+
Symbol: .debug_str
87+
Type: R_X86_64_32
88+
Addend: 24
89+
- Name: .rela.debug_addr
90+
Type: SHT_RELA
91+
Flags: [ SHF_INFO_LINK ]
92+
Link: .symtab
93+
AddressAlign: 0x8
94+
Info: .debug_addr
95+
Relocations:
96+
- Offset: 0x8
97+
Symbol: .text
98+
Type: R_X86_64_64
99+
- Name: .rela.debug_line
100+
Type: SHT_RELA
101+
Flags: [ SHF_INFO_LINK ]
102+
Link: .symtab
103+
AddressAlign: 0x8
104+
Info: .debug_line
105+
Relocations:
106+
- Offset: 0x22
107+
Symbol: .debug_line_str
108+
Type: R_X86_64_32
109+
- Offset: 0x2E
110+
Symbol: .debug_line_str
111+
Type: R_X86_64_32
112+
Addend: 1
113+
- Offset: 0x48
114+
Symbol: .text
115+
Type: R_X86_64_64
116+
- Type: SectionHeaderTable
117+
Sections:
118+
- Name: .strtab
119+
- Name: .text
120+
- Name: .rela.text
121+
- Name: .debug_abbrev
122+
- Name: .debug_info
123+
- Name: .rela.debug_info
124+
- Name: .debug_str_offsets
125+
- Name: .rela.debug_str_offsets
126+
- Name: .debug_str
127+
- Name: .debug_addr
128+
- Name: .rela.debug_addr
129+
- Name: .debug_line
130+
- Name: .rela.debug_line
131+
- Name: .debug_line_str
132+
- Name: .symtab
133+
Symbols:
134+
- Name: main.cpp
135+
Type: STT_FILE
136+
Index: SHN_ABS
137+
- Name: .text
138+
Type: STT_SECTION
139+
Section: .text
140+
- Name: .debug_abbrev
141+
Type: STT_SECTION
142+
Section: .debug_abbrev
143+
- Name: .debug_str_offsets
144+
Type: STT_SECTION
145+
Section: .debug_str_offsets
146+
- Name: .debug_str
147+
Type: STT_SECTION
148+
Section: .debug_str
149+
- Name: .debug_addr
150+
Type: STT_SECTION
151+
Section: .debug_addr
152+
- Name: .debug_line
153+
Type: STT_SECTION
154+
Section: .debug_line
155+
- Name: .debug_line_str
156+
Type: STT_SECTION
157+
Section: .debug_line_str
158+
- Name: _Z3foob
159+
Type: STT_FUNC
160+
Section: .text
161+
Binding: STB_LOCAL
162+
Size: 0xA
163+
- Name: _Z3barv
164+
Binding: STB_LOCAL
165+
DWARF:
166+
debug_str:
167+
- clang version 21.0.0git
168+
- '<invalid>'
169+
debug_addr:
170+
- Length: 0xC
171+
Version: 0x5
172+
AddressSize: 0x8
173+
Entries:
174+
- {}
175+
...

0 commit comments

Comments
 (0)