Skip to content

Commit 8736e0e

Browse files
committed
Improve target creation in LLDB adapter. Fix #712. Fix #669
1 parent 6ee6dc8 commit 8736e0e

File tree

2 files changed

+41
-54
lines changed

2 files changed

+41
-54
lines changed

core/adapters/lldbadapter.cpp

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ using namespace std;
2525

2626
std::string lldbArchNameForBinaryNinjaArchName(std::string name)
2727
{
28+
if (name == "x86")
29+
return "x86";
2830
if (name == "x86_64")
2931
return "x86_64";
3032
else if (name == "aarch64")
@@ -368,24 +370,7 @@ bool LldbAdapter::ExecuteWithArgs(const std::string& path, const std::string& ar
368370
scope = SettingsResourceScope;
369371
auto envVariables = adapterSettings->Get<vector<string>>("launch.environmentVariables", data, &scope);
370372

371-
// *Attempt* to create a functional target triple for the binary.
372-
// This allows attaching to fat binaries. If the triple is empty, it will still attach on thin binaries.
373-
auto archName = lldbArchNameForBinaryNinjaArchName(m_defaultArchitecture);
374-
std::string triple = "";
375-
if (!archName.empty())
376-
triple = archName + "-unknown-none";
377-
378-
m_target = m_debugger.CreateTarget(executablePath.c_str(), triple.c_str(), "", true, err);
379-
380-
if (!m_target.IsValid())
381-
{
382-
// It is likely lldb did not like our target triple.
383-
if (err.GetCString() && std::string(err.GetCString()).find("is not compatible with") != std::string::npos)
384-
{
385-
// Last-ditch effort. If it is a thin binary, we will be able to attach without passing a triple.
386-
m_target = m_debugger.CreateTarget(executablePath.c_str(), "", "", true, err);
387-
}
388-
}
373+
CreateTarget(inputFile);
389374

390375
if (!m_target.IsValid())
391376
{
@@ -494,24 +479,7 @@ bool LldbAdapter::Attach(std::uint32_t pid)
494479
scope = SettingsResourceScope;
495480
auto attachPID = adapterSettings->Get<uint64_t>("attach.pid", data, &scope);
496481

497-
// *Attempt* to create a functional target triple for the binary.
498-
// This allows attaching to fat binaries. If the triple is empty, it will still attach on thin binaries.
499-
auto archName = lldbArchNameForBinaryNinjaArchName(m_defaultArchitecture);
500-
std::string triple = "";
501-
if (!archName.empty())
502-
triple = archName + "-unknown-none";
503-
504-
m_target = m_debugger.CreateTarget(inputFile.c_str(), triple.c_str(), "", true, err);
505-
506-
if (!m_target.IsValid())
507-
{
508-
// It is likely lldb did not like our target triple.
509-
if (err.GetCString() && std::string(err.GetCString()).find("is not compatible with") != std::string::npos)
510-
{
511-
// Last-ditch effort. If it is a thin binary, we will be able to attach without passing a triple.
512-
m_target = m_debugger.CreateTarget(inputFile.c_str(), "", "", true, err);
513-
}
514-
}
482+
CreateTarget(inputFile);
515483

516484
if (!m_target.IsValid())
517485
{
@@ -550,6 +518,40 @@ bool LldbAdapter::Attach(std::uint32_t pid)
550518
}
551519

552520

521+
bool LldbAdapter::CreateTarget(const std::string &file)
522+
{
523+
// We try different ways to create a target until one of them works...
524+
auto archName = lldbArchNameForBinaryNinjaArchName(m_defaultArchitecture);
525+
std::string triple = "";
526+
if (!archName.empty())
527+
triple = archName + "-unknown-none";
528+
529+
m_target = m_debugger.CreateTargetWithFileAndArch(file.c_str(), archName.c_str());
530+
if (m_target.IsValid())
531+
return true;
532+
533+
m_target = m_debugger.CreateTargetWithFileAndArch(file.c_str(), "");
534+
if (m_target.IsValid())
535+
return true;
536+
537+
SBError err;
538+
m_target = m_debugger.CreateTarget(file.c_str(), triple.c_str(), "", true, err);
539+
if (m_target.IsValid())
540+
return true;
541+
542+
m_target = m_debugger.CreateTarget(file.c_str(), "", "", true, err);
543+
if (m_target.IsValid())
544+
return true;
545+
546+
m_target = m_debugger.CreateTarget("", "", "", true, err);
547+
if (m_target.IsValid())
548+
return true;
549+
550+
return false;
551+
}
552+
553+
554+
553555
bool LldbAdapter::Connect(const std::string& server, std::uint32_t port)
554556
{
555557
m_debugger.SetAsync(true);
@@ -570,24 +572,7 @@ bool LldbAdapter::Connect(const std::string& server, std::uint32_t port)
570572
scope = SettingsResourceScope;
571573
auto processPlugin = adapterSettings->Get<std::string>("connect.processPlugin", data, &scope);
572574

573-
// *Attempt* to create a functional target triple for the binary.
574-
// This allows attaching to fat binaries. If the triple is empty, it will still attach on thin binaries.
575-
auto archName = lldbArchNameForBinaryNinjaArchName(m_defaultArchitecture);
576-
std::string triple = "";
577-
if (!archName.empty())
578-
triple = archName + "-unknown-none";
579-
580-
m_target = m_debugger.CreateTarget(inputFile.c_str(), triple.c_str(), "", true, err);
581-
582-
if (!m_target.IsValid())
583-
{
584-
// It is likely lldb did not like our target triple.
585-
if (err.GetCString() && std::string(err.GetCString()).find("is not compatible with") != std::string::npos)
586-
{
587-
// Last-ditch effort. If it is a thin binary, we will be able to attach without passing a triple.
588-
m_target = m_debugger.CreateTarget(inputFile.c_str(), "", "", true, err);
589-
}
590-
}
575+
CreateTarget(inputFile);
591576

592577
if (!m_target.IsValid())
593578
{

core/adapters/lldbadapter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ namespace BinaryNinjaDebugger {
4545
bool m_isElFWithoutDynamicLoader = false;
4646
bool IsELFWithoutDynamicLoader(BinaryView* data);
4747

48+
bool CreateTarget(const std::string& file);
49+
4850
public:
4951
LldbAdapter(BinaryView* data);
5052
virtual ~LldbAdapter();

0 commit comments

Comments
 (0)