Skip to content

Commit

Permalink
Re-apply "[ORC] Add N_SO and N_OSO stabs entries to MachO debug..." w…
Browse files Browse the repository at this point in the history
…ith fixes.

This re-applies db51e57, which was reverted in 05b1a2c due to bot
failures. The DebuggerSupportPlugin now depends on DWARF, so it has been moved
to the new OrcDebugging library (as has the enableDebuggerSupport API).
  • Loading branch information
lhames committed Sep 29, 2023
1 parent ab472cd commit b251897
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions clang/lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
MC
Option
OrcJit
OrcDebugging
OrcShared
OrcTargetProcess
Support
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/IncrementalExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "clang/Interpreter/PartialTranslationUnit.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ add_llvm_component_library(LLVMOrcJIT
CompileUtils.cpp
Core.cpp
DebugObjectManagerPlugin.cpp
DebuggerSupport.cpp
DebuggerSupportPlugin.cpp
DebugUtils.cpp
EPCDynamicLibrarySearchGenerator.cpp
EPCDebugObjectRegistrar.cpp
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ endif()

add_llvm_component_library(LLVMOrcDebugging
DebugInfoSupport.cpp
DebuggerSupport.cpp
DebuggerSupportPlugin.cpp
PerfSupportPlugin.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"

#define DEBUG_TYPE "orc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/MachOBuilder.h"

#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"

#include <chrono>

#define DEBUG_TYPE "orc"

Expand Down Expand Up @@ -97,8 +101,6 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
<< "\n";
});

auto &SDOSec = G.createSection(SynthDebugSectionName, MemProt::Read);

for (auto &Sec : G.sections()) {
if (Sec.blocks().empty())
continue;
Expand All @@ -114,6 +116,10 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
NonDebugSections.push_back({&Sec, nullptr});
}

// Bail out early if no debug sections.
if (DebugSections.empty())
return Error::success();

// Write MachO header and debug section load commands.
Builder.Header.filetype = MachO::MH_OBJECT;
switch (G.getTargetTriple().getArch()) {
Expand All @@ -131,16 +137,65 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {

Seg = &Builder.addSegment("");

StringMap<std::unique_ptr<MemoryBuffer>> DebugSectionMap;
StringRef DebugLineSectionData;
for (auto &DSec : DebugSections) {
auto [SegName, SecName] = DSec.GraphSec->getName().split(',');
DSec.BuilderSec = &Seg->addSection(SecName, SegName);

SectionRange SR(*DSec.GraphSec);
DSec.BuilderSec->Content.Size = SR.getSize();
if (!SR.empty())
if (!SR.empty()) {
DSec.BuilderSec->align = Log2_64(SR.getFirstBlock()->getAlignment());
StringRef SectionData(SR.getFirstBlock()->getContent().data(),
SR.getFirstBlock()->getSize());
DebugSectionMap[SecName] =
MemoryBuffer::getMemBuffer(SectionData, G.getName(), false);
if (SecName == "__debug_line")
DebugLineSectionData = SectionData;
}
}

std::optional<std::string> FileName;
if (!DebugLineSectionData.empty()) {
auto DWARFCtx = DWARFContext::create(DebugSectionMap, G.getPointerSize(),
G.getEndianness());
DWARFDataExtractor DebugLineData(
DebugLineSectionData,
G.getEndianness() == support::endianness::little, G.getPointerSize());
uint64_t Offset = 0;
DWARFDebugLine::LineTable LineTable;

// Try to parse line data. Consume error on failure.
if (auto Err = LineTable.parse(DebugLineData, &Offset, *DWARFCtx, nullptr,
consumeError)) {
handleAllErrors(
std::move(Err),
[&](ErrorInfoBase &EIB) {
LLVM_DEBUG({
dbgs() << "Cannot parse line table for \"" << G.getName() << "\": ";
EIB.log(dbgs());
dbgs() << "\n";
});
});
} else {
if (!LineTable.Prologue.FileNames.empty())
FileName = *dwarf::toString(LineTable.Prologue.FileNames[0].Name);
}
}

// If no line table (or unable to use) then use graph name.
// FIXME: There are probably other debug sections we should look in first.
if (!FileName)
FileName = G.getName();

Builder.addSymbol("", MachO::N_SO, 0, 0, 0);
Builder.addSymbol(*FileName, MachO::N_SO, 0, 0, 0);
auto TimeStamp = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
Builder.addSymbol("", MachO::N_OSO, 3, 1, TimeStamp);

for (auto &NDSP : NonDebugSections) {
auto [SegName, SecName] = NDSP.GraphSec->getName().split(',');
NDSP.BuilderSec = &Seg->addSection(SecName, SegName);
Expand All @@ -164,8 +219,12 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
}
}

Builder.addSymbol("", MachO::N_SO, 1, 0, 0);

// Lay out the debug object, create a section and block for it.
size_t DebugObjectSize = Builder.layout();

auto &SDOSec = G.createSection(SynthDebugSectionName, MemProt::Read);
MachOContainerBlock = &G.createMutableContentBlock(
SDOSec, G.allocateBuffer(DebugObjectSize), orc::ExecutorAddr(), 8, 0);

Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/lli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ set(LLVM_LINK_COMPONENTS
MC
MCJIT
Object
OrcShared
OrcJIT
OrcDebugging
OrcShared
OrcTargetProcess
Passes
RuntimeDyld
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/lli/lli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/ExecutionEngine/ObjectCache.h"
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
#include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
#include "llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h"
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/Debugging/PerfSupportPlugin.h"
#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
#include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
Expand Down

0 comments on commit b251897

Please sign in to comment.