Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output image info for LLDB debugging #350

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions oi/OICompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <array>
#include <boost/range/combine.hpp>
#include <boost/scope_exit.hpp>
#include <fstream>

#include "oi/Headers.h"
#include "oi/Metrics.h"
Expand Down Expand Up @@ -126,6 +127,10 @@ OICompiler::Disassembler::operator()() {
return inst;
}

static StringRef CurrentObjectName;
static std::unordered_map<uintptr_t, std::pair<std::string, std::string>>
AddrToSection;

/*
* Manage memory for the object files and handle symbol resolution.
* The interface is defined by LLVM and we setup our hooks to
Expand Down Expand Up @@ -313,7 +318,13 @@ uint8_t* OIMemoryManager::allocateCodeSection(
<< ", Alignment = " << alignment
<< ", SectionName = " << sectionName.data() << ")";

return currentSlab().allocate(size, alignment, true /* isCode */);
auto* allocatedSection =
currentSlab().allocate(size, alignment, true /* isCode */);

AddrToSection.emplace((uintptr_t)allocatedSection,
std::make_pair(CurrentObjectName, sectionName));

return allocatedSection;
}

uint8_t* OIMemoryManager::allocateDataSection(
Expand All @@ -326,7 +337,13 @@ uint8_t* OIMemoryManager::allocateDataSection(
<< ", Alignment = " << alignment
<< ", SectionName = " << sectionName.data() << ")";

return currentSlab().allocate(size, alignment, false /* isCode */);
auto* allocatedSection =
currentSlab().allocate(size, alignment, false /* isCode */);

AddrToSection.emplace((uintptr_t)allocatedSection,
std::make_pair(CurrentObjectName, sectionName));

return allocatedSection;
}

/*
Expand Down Expand Up @@ -616,6 +633,12 @@ std::optional<OICompiler::RelocResult> OICompiler::applyRelocs(
memMgr = std::make_unique<OIMemoryManager>(symbols, syntheticSymbols);
RuntimeDyld dyld(*memMgr, *memMgr);

std::ofstream lldbImageInfoCmd;
if (config.features[Feature::GenJitDebug]) {
auto lldbImageInfoPath = "/tmp/oi-" + std::to_string(getpid()) + ".lldb";
lldbImageInfoCmd.open(lldbImageInfoPath);
}

/* Load all the object files into the MemoryManager */
for (const auto& objPath : objectFiles) {
VLOG(1) << "Loading object file " << objPath;
Expand All @@ -626,7 +649,12 @@ std::optional<OICompiler::RelocResult> OICompiler::applyRelocs(
return std::nullopt;
}

CurrentObjectName = objPath.c_str();
dyld.loadObject(*objFile->getBinary());
CurrentObjectName = StringRef(); // clear CurrentObjectName

lldbImageInfoCmd << "image add " << objPath << "\n";

if (dyld.hasError()) {
LOG(ERROR) << "load object failed: " << dyld.getErrorString().data();
return std::nullopt;
Expand All @@ -644,6 +672,16 @@ std::optional<OICompiler::RelocResult> OICompiler::applyRelocs(
(uintptr_t)funcSection.base() - (uintptr_t)slab.memBlock.base();
dyld.mapSectionAddress(funcSection.base(), currentRelocAddress + offset);

if (auto it = AddrToSection.find((uintptr_t)funcSection.base());
it != AddrToSection.end()) {
lldbImageInfoCmd << "image load --address " << it->second.first << " "
<< it->second.second
<< (void*)(currentRelocAddress + offset) << "\n";
} else {
LOG(WARNING) << "No mapping for function section "
<< funcSection.base();
}

VLOG(1) << std::hex << "Relocated code " << funcSection.base() << " to "
<< currentRelocAddress + offset;
}
Expand All @@ -653,6 +691,16 @@ std::optional<OICompiler::RelocResult> OICompiler::applyRelocs(
(uintptr_t)dataSection.base() - (uintptr_t)slab.memBlock.base();
dyld.mapSectionAddress(dataSection.base(), currentRelocAddress + offset);

if (auto it = AddrToSection.find((uintptr_t)dataSection.base());
it != AddrToSection.end()) {
lldbImageInfoCmd << "image load --address " << it->second.first << " "
<< it->second.second
<< (void*)(currentRelocAddress + offset) << "\n";
} else {
LOG(WARNING) << "No mapping for function section "
<< dataSection.base();
}

VLOG(1) << std::hex << "Relocated data " << dataSection.base() << " to "
<< currentRelocAddress + offset;
}
Expand Down