Skip to content

Commit

Permalink
Create a debugger controller per file metadata rather than per binary…
Browse files Browse the repository at this point in the history
… view
  • Loading branch information
xusheng6 authored and D0ntPanic committed Feb 14, 2024
1 parent c58b7ca commit 6eb7670
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
48 changes: 19 additions & 29 deletions core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ limitations under the License.

using namespace BinaryNinjaDebugger;

DebuggerController::DebuggerController(BinaryViewRef data) : m_data(data)
DebuggerController::DebuggerController(BinaryViewRef data)
{
INIT_DEBUGGER_API_OBJECT();

m_file = data->GetFile();
m_viewName = data->GetTypeName();

m_state = new DebuggerState(data, this);
m_adapter = nullptr;
RegisterEventCallback([this](const DebuggerEvent& event) { EventHandler(event); }, "Debugger Core");
Expand Down Expand Up @@ -274,7 +277,7 @@ bool DebuggerController::CreateDebugAdapter()
LogWarn("Failed to get an debug adapter of type %s", m_state->GetAdapterType().c_str());
return false;
}
m_adapter = type->Create(m_data);
m_adapter = type->Create(GetData());
if (!m_adapter)
{
LogWarn("Failed to create an adapter of type %s", m_state->GetAdapterType().c_str());
Expand Down Expand Up @@ -726,17 +729,18 @@ bool DebuggerController::CreateDebuggerBinaryView()
if (!viewType)
return false;

BinaryViewRef liveView = viewType->Create(m_data);
BinaryViewRef liveView = viewType->Create(GetData());
if (!liveView)
return false;

if (!m_data->GetDefaultArchitecture() || !m_data->GetDefaultPlatform())
BinaryViewRef data = GetData();
if (!data->GetDefaultArchitecture() || !data->GetDefaultPlatform())
{
LogWarn("Fail to create debugger view. The input view must have an architecture and platform");
return false;
}
liveView->SetDefaultArchitecture(m_data->GetDefaultArchitecture());
liveView->SetDefaultPlatform(m_data->GetDefaultPlatform());
liveView->SetDefaultArchitecture(data->GetDefaultArchitecture());
liveView->SetDefaultPlatform(data->GetDefaultPlatform());
SetLiveView(liveView);

return true;
Expand Down Expand Up @@ -764,22 +768,18 @@ void DebuggerController::DetectLoadedModule()
}
else
{
FileMetadataRef fileMetadata = m_data->GetFile();
if (remoteBase != m_data->GetStart())
if (remoteBase != GetData()->GetStart())
{
// remote base is different from the local base, first need a rebase
if (!fileMetadata->Rebase(m_data, remoteBase, [&](size_t cur, size_t total) { return true; }))
if (!m_file->Rebase(GetData(), remoteBase, [&](size_t cur, size_t total) { return true; }))
{
LogWarn("rebase failed");
}
}

Ref<BinaryView> rebasedView = fileMetadata->GetViewOfType(m_data->GetTypeName());
SetData(rebasedView);
Ref<BinaryView> rebasedView = m_file->GetViewOfType(m_viewName);

bool ok = fileMetadata->CreateSnapshotedView(rebasedView, "Debugger", [&](size_t cur, size_t total) {
return true;
});
bool ok = m_file->CreateSnapshotedView(rebasedView, "Debugger", [&](size_t cur, size_t total) { return true; });
if (!ok)
LogWarn("create snapshoted view failed");
}
Expand Down Expand Up @@ -1051,9 +1051,9 @@ void DebuggerController::LaunchOrConnect()
if (!adapterType)
return;

if (adapterType->CanExecute(m_data))
if (adapterType->CanExecute(GetData()))
Launch();
else if (adapterType->CanConnect(m_data))
else if (adapterType->CanConnect(GetData()))
Connect();
}

Expand All @@ -1070,18 +1070,8 @@ DbgRef<DebuggerController> DebuggerController::GetController(BinaryViewRef data)
DebuggerController* controller = g_debuggerControllers[i];
if (!controller)
continue;
if (controller->GetData() == data)
return controller;
if (controller->GetLiveView() == data)
if (controller->m_file.operator==(data->GetFile()))
return controller;
//if (controller->GetData()->GetFile()->GetOriginalFilename() == data->GetFile()->GetOriginalFilename())
// return controller;
//if (controller->GetData()->GetFile()->GetOriginalFilename() == data->GetParentView()->GetFile()->GetOriginalFilename())
// return controller;
//if (controller->GetData()->GetFile() == data->GetFile())
// return controller;
//if (controller->GetLiveView() && (controller->GetLiveView()->GetFile() == data->GetFile()))
// return controller;
}

if (data->GetTypeName() == "Debugger")
Expand Down Expand Up @@ -1137,8 +1127,8 @@ bool DebuggerController::ControllerExists(BinaryViewRef data)

void DebuggerController::Destroy()
{
DebuggerController::DeleteController(m_data);
m_data = nullptr;
DebuggerController::DeleteController(GetData());
m_file = nullptr;
m_liveView = nullptr;
}

Expand Down
7 changes: 4 additions & 3 deletions core/debuggercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ namespace BinaryNinjaDebugger {
private:
DebugAdapter* m_adapter;
DebuggerState* m_state;
BinaryViewRef m_data;
FileMetadataRef m_file;
std::string m_viewName;
BinaryViewRef m_liveView;

// inline static std::vector<DbgRef<DebuggerController>> g_debuggerControllers;
Expand Down Expand Up @@ -250,8 +251,8 @@ namespace BinaryNinjaDebugger {
// getters
DebugAdapter* GetAdapter() { return m_adapter; }
DebuggerState* GetState() { return m_state; }
BinaryViewRef GetData() const { return m_data; }
void SetData(BinaryViewRef view) { m_data = view; }
BinaryViewRef GetData() { return m_file->GetViewOfType(m_viewName); }
void SetData(BinaryViewRef view) {}
BinaryViewRef GetLiveView() const { return m_liveView; }

uint32_t GetExitCode();
Expand Down
1 change: 0 additions & 1 deletion ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,6 @@ void DebuggerUI::updateUI(const DebuggerEvent& event)
}

Ref<BinaryView> rebasedView = fileMetadata->GetViewOfType(data->GetTypeName());
m_controller->SetData(rebasedView);

bool result = false;
QString text = QString("Adding the input view into the debugger view...");
Expand Down

0 comments on commit 6eb7670

Please sign in to comment.