Skip to content

Commit

Permalink
Fix DbgEng fails to launch the target when the .exe file name is diff…
Browse files Browse the repository at this point in the history
…erent from the pdf file name. Fix Vector35#159
  • Loading branch information
xusheng6 committed Dec 19, 2022
1 parent 180dbdb commit 2d59443
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 15 additions & 2 deletions core/adapters/dbgengadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ bool DbgEngAdapter::ConnectToDebugServerInternal(const std::string& connectionSt
QUERY_DEBUG_INTERFACE(IDebugControl5, &this->m_debugControl);
QUERY_DEBUG_INTERFACE(IDebugDataSpaces, &this->m_debugDataSpaces);
QUERY_DEBUG_INTERFACE(IDebugRegisters, &this->m_debugRegisters);
QUERY_DEBUG_INTERFACE(IDebugSymbols, &this->m_debugSymbols);
QUERY_DEBUG_INTERFACE(IDebugSymbols3, &this->m_debugSymbols);
QUERY_DEBUG_INTERFACE(IDebugSystemObjects, &this->m_debugSystemObjects);

constexpr size_t CONNECTION_MAX_TRY = 300;
Expand Down Expand Up @@ -302,6 +302,9 @@ void DbgEngAdapter::Reset()

DbgEngAdapter::DbgEngAdapter(BinaryView* data) : DebugAdapter(data)
{
auto metadata = data->QueryMetadata("PDB_FILENAME");
if (metadata && metadata->IsString())
m_pdbFileName = metadata->GetString();
LoadDngEngLibraries();
}

Expand Down Expand Up @@ -748,8 +751,15 @@ DebugBreakpoint DbgEngAdapter::AddBreakpoint(const ModuleNameAndOffset& address,
// and add them when we launch/attach the target.
if (m_debugActive)
{
auto moduleToUse = address.module;
if (DebugModule::IsSameBaseModule(moduleToUse, m_originalFileName))
{
if (!m_pdbFileName.empty())
moduleToUse = m_pdbFileName;
}

// DbgEng does not take a full path. It can take "hello.exe", or simply "hello". E.g., "bp helloworld+0x1338"
auto fileName = std::filesystem::path(address.module).stem();
auto fileName = std::filesystem::path(moduleToUse).stem();
std::string breakpointCommand =
fmt::format("bp {}+0x{:x}", EscapeModuleName(fileName.string()), address.offset);
auto ret = InvokeBackendCommand(breakpointCommand);
Expand Down Expand Up @@ -967,6 +977,9 @@ std::vector<DebugModule> DbgEngAdapter::GetModuleList()
!= S_OK)
continue;

if ((!m_pdbFileName.empty()) && DebugModule::IsSameBaseModule(short_name, m_pdbFileName))
strcpy_s(name, 1024, m_originalFileName.c_str());

modules.emplace_back(
name, short_name, parameters.Base, parameters.Size, !(parameters.Flags & DEBUG_MODULE_UNLOADED));
}
Expand Down
4 changes: 3 additions & 1 deletion core/adapters/dbgengadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace BinaryNinjaDebugger {
IDebugControl5* m_debugControl {nullptr};
IDebugDataSpaces* m_debugDataSpaces {nullptr};
IDebugRegisters* m_debugRegisters {nullptr};
IDebugSymbols* m_debugSymbols {nullptr};
IDebugSymbols3* m_debugSymbols {nullptr};
IDebugSystemObjects* m_debugSystemObjects {nullptr};
bool m_debugActive {false};

Expand All @@ -120,6 +120,8 @@ namespace BinaryNinjaDebugger {

bool m_aboutToBeKilled = false;

std::string m_pdbFileName {};

public:
inline static ProcessCallbackInformation ProcessCallbackInfo {};
static constexpr unsigned long StepoutBreakpointID = 0x5be9c948;
Expand Down

0 comments on commit 2d59443

Please sign in to comment.