LLVM 20.0.0git
DebuggerSupportPlugin.cpp
Go to the documentation of this file.
1//===------- DebuggerSupportPlugin.cpp - Utils for debugger support -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
14
15#include "llvm/ADT/SmallSet.h"
20
21#include <chrono>
22
23#define DEBUG_TYPE "orc"
24
25using namespace llvm;
26using namespace llvm::jitlink;
27using namespace llvm::orc;
28
29static const char *SynthDebugSectionName = "__jitlink_synth_debug_object";
30
31namespace {
32
33class MachODebugObjectSynthesizerBase
35public:
36 static bool isDebugSection(Section &Sec) {
37 return Sec.getName().starts_with("__DWARF,");
38 }
39
40 MachODebugObjectSynthesizerBase(LinkGraph &G, ExecutorAddr RegisterActionAddr)
41 : G(G), RegisterActionAddr(RegisterActionAddr) {}
42 virtual ~MachODebugObjectSynthesizerBase() = default;
43
45 if (G.findSectionByName(SynthDebugSectionName)) {
47 dbgs() << "MachODebugObjectSynthesizer skipping graph " << G.getName()
48 << " which contains an unexpected existing "
49 << SynthDebugSectionName << " section.\n";
50 });
51 return Error::success();
52 }
53
55 dbgs() << "MachODebugObjectSynthesizer visiting graph " << G.getName()
56 << "\n";
57 });
58 for (auto &Sec : G.sections()) {
59 if (!isDebugSection(Sec))
60 continue;
61 // Preserve blocks in this debug section by marking one existing symbol
62 // live for each block, and introducing a new live, anonymous symbol for
63 // each currently unreferenced block.
65 dbgs() << " Preserving debug section " << Sec.getName() << "\n";
66 });
67 SmallSet<Block *, 8> PreservedBlocks;
68 for (auto *Sym : Sec.symbols()) {
69 bool NewPreservedBlock =
70 PreservedBlocks.insert(&Sym->getBlock()).second;
71 if (NewPreservedBlock)
72 Sym->setLive(true);
73 }
74 for (auto *B : Sec.blocks())
75 if (!PreservedBlocks.count(B))
76 G.addAnonymousSymbol(*B, 0, 0, false, true);
77 }
78
79 return Error::success();
80 }
81
82protected:
83 LinkGraph &G;
84 ExecutorAddr RegisterActionAddr;
85};
86
87template <typename MachOTraits>
88class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
89public:
90 MachODebugObjectSynthesizer(ExecutionSession &ES, LinkGraph &G,
91 ExecutorAddr RegisterActionAddr)
92 : MachODebugObjectSynthesizerBase(G, RegisterActionAddr),
93 Builder(ES.getPageSize()) {}
94
95 using MachODebugObjectSynthesizerBase::MachODebugObjectSynthesizerBase;
96
97 Error startSynthesis() override {
99 dbgs() << "Creating " << SynthDebugSectionName << " for " << G.getName()
100 << "\n";
101 });
102
103 for (auto &Sec : G.sections()) {
104 if (Sec.blocks().empty())
105 continue;
106
107 // Skip sections whose name's don't fit the MachO standard.
108 if (Sec.getName().empty() || Sec.getName().size() > 33 ||
109 Sec.getName().find(',') > 16)
110 continue;
111
112 if (isDebugSection(Sec))
113 DebugSections.push_back({&Sec, nullptr});
114 else if (Sec.getMemLifetime() != MemLifetime::NoAlloc)
115 NonDebugSections.push_back({&Sec, nullptr});
116 }
117
118 // Bail out early if no debug sections.
119 if (DebugSections.empty())
120 return Error::success();
121
122 // Write MachO header and debug section load commands.
123 Builder.Header.filetype = MachO::MH_OBJECT;
124 if (auto CPUType = MachO::getCPUType(G.getTargetTriple()))
125 Builder.Header.cputype = *CPUType;
126 else
127 return CPUType.takeError();
128 if (auto CPUSubType = MachO::getCPUSubType(G.getTargetTriple()))
129 Builder.Header.cpusubtype = *CPUSubType;
130 else
131 return CPUSubType.takeError();
132
133 Seg = &Builder.addSegment("");
134
136 StringRef DebugLineSectionData;
137 for (auto &DSec : DebugSections) {
138 auto [SegName, SecName] = DSec.GraphSec->getName().split(',');
139 DSec.BuilderSec = &Seg->addSection(SecName, SegName);
140
141 SectionRange SR(*DSec.GraphSec);
142 DSec.BuilderSec->Content.Size = SR.getSize();
143 if (!SR.empty()) {
144 DSec.BuilderSec->align = Log2_64(SR.getFirstBlock()->getAlignment());
145 StringRef SectionData(SR.getFirstBlock()->getContent().data(),
146 SR.getFirstBlock()->getSize());
147 DebugSectionMap[SecName.drop_front(2)] = // drop "__" prefix.
148 MemoryBuffer::getMemBuffer(SectionData, G.getName(), false);
149 if (SecName == "__debug_line")
150 DebugLineSectionData = SectionData;
151 }
152 }
153
154 std::optional<StringRef> FileName;
155 if (!DebugLineSectionData.empty()) {
156 assert((G.getEndianness() == llvm::endianness::big ||
157 G.getEndianness() == llvm::endianness::little) &&
158 "G.getEndianness() must be either big or little");
159 auto DWARFCtx =
160 DWARFContext::create(DebugSectionMap, G.getPointerSize(),
161 G.getEndianness() == llvm::endianness::little);
162 DWARFDataExtractor DebugLineData(
163 DebugLineSectionData, G.getEndianness() == llvm::endianness::little,
164 G.getPointerSize());
165 uint64_t Offset = 0;
167
168 // Try to parse line data. Consume error on failure.
169 if (auto Err = P.parse(DebugLineData, &Offset, consumeError, *DWARFCtx)) {
170 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
171 LLVM_DEBUG({
172 dbgs() << "Cannot parse line table for \"" << G.getName() << "\": ";
173 EIB.log(dbgs());
174 dbgs() << "\n";
175 });
176 });
177 } else {
178 for (auto &FN : P.FileNames)
179 if ((FileName = dwarf::toString(FN.Name))) {
180 LLVM_DEBUG({
181 dbgs() << "Using FileName = \"" << *FileName
182 << "\" from DWARF line table\n";
183 });
184 break;
185 }
186 }
187 }
188
189 // If no line table (or unable to use) then use graph name.
190 // FIXME: There are probably other debug sections we should look in first.
191 if (!FileName) {
192 LLVM_DEBUG({
193 dbgs() << "Could not find source name from DWARF line table. "
194 "Using FileName = \"\"\n";
195 });
196 FileName = "";
197 }
198
199 Builder.addSymbol("", MachO::N_SO, 0, 0, 0);
200 Builder.addSymbol(*FileName, MachO::N_SO, 0, 0, 0);
201 auto TimeStamp = std::chrono::duration_cast<std::chrono::seconds>(
202 std::chrono::system_clock::now().time_since_epoch())
203 .count();
204 Builder.addSymbol("", MachO::N_OSO, 3, 1, TimeStamp);
205
206 for (auto &NDSP : NonDebugSections) {
207 auto [SegName, SecName] = NDSP.GraphSec->getName().split(',');
208 NDSP.BuilderSec = &Seg->addSection(SecName, SegName);
209 SectionRange SR(*NDSP.GraphSec);
210 if (!SR.empty())
211 NDSP.BuilderSec->align = Log2_64(SR.getFirstBlock()->getAlignment());
212
213 // Add stabs.
214 for (auto *Sym : NDSP.GraphSec->symbols()) {
215 // Skip anonymous symbols.
216 if (!Sym->hasName())
217 continue;
218
220
221 Builder.addSymbol("", MachO::N_BNSYM, 1, 0, 0);
222 StabSymbols.push_back(
223 {*Sym, Builder.addSymbol(*Sym->getName(), SymType, 1, 0, 0),
224 Builder.addSymbol(*Sym->getName(), SymType, 0, 0, 0)});
225 Builder.addSymbol("", MachO::N_ENSYM, 1, 0, 0);
226 }
227 }
228
229 Builder.addSymbol("", MachO::N_SO, 1, 0, 0);
230
231 // Lay out the debug object, create a section and block for it.
232 size_t DebugObjectSize = Builder.layout();
233
234 auto &SDOSec = G.createSection(SynthDebugSectionName, MemProt::Read);
235 MachOContainerBlock = &G.createMutableContentBlock(
236 SDOSec, G.allocateBuffer(DebugObjectSize), orc::ExecutorAddr(), 8, 0);
237
238 return Error::success();
239 }
240
241 Error completeSynthesisAndRegister() override {
242 if (!MachOContainerBlock) {
243 LLVM_DEBUG({
244 dbgs() << "Not writing MachO debug object header for " << G.getName()
245 << " since createDebugSection failed\n";
246 });
247
248 return Error::success();
249 }
250 ExecutorAddr MaxAddr;
251 for (auto &NDSec : NonDebugSections) {
252 SectionRange SR(*NDSec.GraphSec);
253 NDSec.BuilderSec->addr = SR.getStart().getValue();
254 NDSec.BuilderSec->size = SR.getSize();
255 NDSec.BuilderSec->offset = SR.getStart().getValue();
256 if (SR.getEnd() > MaxAddr)
257 MaxAddr = SR.getEnd();
258 }
259
260 for (auto &DSec : DebugSections) {
261 if (DSec.GraphSec->blocks_size() != 1)
262 return make_error<StringError>(
263 "Unexpected number of blocks in debug info section",
265
266 if (ExecutorAddr(DSec.BuilderSec->addr) + DSec.BuilderSec->size > MaxAddr)
267 MaxAddr = ExecutorAddr(DSec.BuilderSec->addr) + DSec.BuilderSec->size;
268
269 auto &B = **DSec.GraphSec->blocks().begin();
270 DSec.BuilderSec->Content.Data = B.getContent().data();
271 DSec.BuilderSec->Content.Size = B.getContent().size();
272 DSec.BuilderSec->flags |= MachO::S_ATTR_DEBUG;
273 }
274
275 LLVM_DEBUG({
276 dbgs() << "Writing MachO debug object header for " << G.getName() << "\n";
277 });
278
279 // Update stab symbol addresses.
280 for (auto &SS : StabSymbols) {
281 SS.StartStab.nlist().n_value = SS.Sym.getAddress().getValue();
282 SS.EndStab.nlist().n_value = SS.Sym.getSize();
283 }
284
285 Builder.write(MachOContainerBlock->getAlreadyMutableContent());
286
287 static constexpr bool AutoRegisterCode = true;
288 SectionRange R(MachOContainerBlock->getSection());
289 G.allocActions().push_back(
292 RegisterActionAddr, R.getRange(), AutoRegisterCode)),
293 {}});
294
295 return Error::success();
296 }
297
298private:
299 struct SectionPair {
300 Section *GraphSec = nullptr;
301 typename MachOBuilder<MachOTraits>::Section *BuilderSec = nullptr;
302 };
303
304 struct StabSymbolsEntry {
305 using RelocTarget = typename MachOBuilder<MachOTraits>::RelocTarget;
306
307 StabSymbolsEntry(Symbol &Sym, RelocTarget StartStab, RelocTarget EndStab)
308 : Sym(Sym), StartStab(StartStab), EndStab(EndStab) {}
309
310 Symbol &Sym;
311 RelocTarget StartStab, EndStab;
312 };
313
314 using BuilderType = MachOBuilder<MachOTraits>;
315
316 Block *MachOContainerBlock = nullptr;
318 typename MachOBuilder<MachOTraits>::Segment *Seg = nullptr;
319 std::vector<StabSymbolsEntry> StabSymbols;
320 SmallVector<SectionPair, 16> DebugSections;
321 SmallVector<SectionPair, 16> NonDebugSections;
322};
323
324} // end anonymous namespace
325
326namespace llvm {
327namespace orc {
328
331 JITDylib &ProcessJD,
332 const Triple &TT) {
333 auto RegisterActionAddr =
334 TT.isOSBinFormatMachO()
335 ? ES.intern("_llvm_orc_registerJITLoaderGDBAllocAction")
336 : ES.intern("llvm_orc_registerJITLoaderGDBAllocAction");
337
338 if (auto RegisterSym = ES.lookup({&ProcessJD}, RegisterActionAddr))
339 return std::make_unique<GDBJITDebugInfoRegistrationPlugin>(
340 RegisterSym->getAddress());
341 else
342 return RegisterSym.takeError();
343}
344
347 return Error::success();
348}
349
351 JITDylib &JD, ResourceKey K) {
352 return Error::success();
353}
354
356 JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) {}
357
360 PassConfiguration &PassConfig) {
361
363 modifyPassConfigForMachO(MR, LG, PassConfig);
364 else {
365 LLVM_DEBUG({
366 dbgs() << "GDBJITDebugInfoRegistrationPlugin skipping unspported graph "
367 << LG.getName() << "(triple = " << LG.getTargetTriple().str()
368 << "\n";
369 });
370 }
371}
372
373void GDBJITDebugInfoRegistrationPlugin::modifyPassConfigForMachO(
375 jitlink::PassConfiguration &PassConfig) {
376
377 switch (LG.getTargetTriple().getArch()) {
378 case Triple::x86_64:
379 case Triple::aarch64:
380 // Supported, continue.
381 assert(LG.getPointerSize() == 8 && "Graph has incorrect pointer size");
383 "Graph has incorrect endianness");
384 break;
385 default:
386 // Unsupported.
387 LLVM_DEBUG({
388 dbgs() << "GDBJITDebugInfoRegistrationPlugin skipping unsupported "
389 << "MachO graph " << LG.getName()
390 << "(triple = " << LG.getTargetTriple().str()
391 << ", pointer size = " << LG.getPointerSize() << ", endianness = "
392 << (LG.getEndianness() == llvm::endianness::big ? "big" : "little")
393 << ")\n";
394 });
395 return;
396 }
397
398 // Scan for debug sections. If we find one then install passes.
399 bool HasDebugSections = false;
400 for (auto &Sec : LG.sections())
401 if (MachODebugObjectSynthesizerBase::isDebugSection(Sec)) {
402 HasDebugSections = true;
403 break;
404 }
405
406 if (HasDebugSections) {
407 LLVM_DEBUG({
408 dbgs() << "GDBJITDebugInfoRegistrationPlugin: Graph " << LG.getName()
409 << " contains debug info. Installing debugger support passes.\n";
410 });
411
412 auto MDOS = std::make_shared<MachODebugObjectSynthesizer<MachO64LE>>(
413 MR.getTargetJITDylib().getExecutionSession(), LG, RegisterActionAddr);
414 PassConfig.PrePrunePasses.push_back(
415 [=](LinkGraph &G) { return MDOS->preserveDebugSections(); });
416 PassConfig.PostPrunePasses.push_back(
417 [=](LinkGraph &G) { return MDOS->startSynthesis(); });
418 PassConfig.PostFixupPasses.push_back(
419 [=](LinkGraph &G) { return MDOS->completeSynthesisAndRegister(); });
420 } else {
421 LLVM_DEBUG({
422 dbgs() << "GDBJITDebugInfoRegistrationPlugin: Graph " << LG.getName()
423 << " contains no debug info. Skipping.\n";
424 });
425 }
426}
427
428} // namespace orc
429} // namespace llvm
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DEBUG(...)
Definition: Debug.h:106
static const char * SynthDebugSectionName
static bool isDebugSection(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:49
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define G(x, y, z)
Definition: MD5.cpp:56
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallSet class.
This file defines the SmallVector class.
const T * data() const
Definition: ArrayRef.h:165
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, ProcessDebugRelocations RelocAction=ProcessDebugRelocations::Process, const LoadedObjectInfo *L=nullptr, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler, bool ThreadSafe=false)
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Base class for error info classes.
Definition: Error.h:45
virtual void log(raw_ostream &OS) const =0
Print an error message to an output stream.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:700
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition: Triple.h:421
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:395
const std::string & str() const
Definition: Triple.h:462
An ExecutionSession represents a running JIT program.
Definition: Core.h:1340
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1394
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:1798
size_t getPageSize() const
Definition: Core.h:1386
Represents an address in the executor process.
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &LG, jitlink::PassConfiguration &PassConfig) override
static Expected< std::unique_ptr< GDBJITDebugInfoRegistrationPlugin > > Create(ExecutionSession &ES, JITDylib &ProcessJD, const Triple &TT)
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) override
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override
Error notifyFailed(MaterializationResponsibility &MR) override
Represents a JIT'd dynamic library.
Definition: Core.h:897
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:916
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:571
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:596
A utility class for serializing to a blob from a variadic list.
static Expected< WrapperFunctionCall > Create(ExecutorAddr FnAddr, const ArgTs &...Args)
Create a WrapperFunctionCall using the given SPS serializer to serialize the arguments.
@ N_ENSYM
Definition: MachO.h:372
@ N_GSYM
Definition: MachO.h:361
@ N_BNSYM
Definition: MachO.h:366
@ S_ATTR_DEBUG
S_ATTR_DEBUG - A debug section.
Definition: MachO.h:207
Expected< uint32_t > getCPUSubType(const Triple &T)
Definition: MachO.cpp:95
@ MH_OBJECT
Definition: MachO.h:43
Expected< uint32_t > getCPUType(const Triple &T)
Definition: MachO.cpp:77
@ SS
Definition: X86.h:212
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Error preserveDebugSections(jitlink::LinkGraph &G)
uintptr_t ResourceKey
Definition: Core.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:348
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069