Skip to content

Commit

Permalink
Show the progress of rebasing and adding the input view to the debugg…
Browse files Browse the repository at this point in the history
…er view when launching the target
  • Loading branch information
xusheng6 committed Apr 11, 2023
1 parent e10caa3 commit e03d4cc
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ namespace BinaryNinjaDebuggerAPI {
void Destroy();
Ref<BinaryView> GetLiveView();
Ref<BinaryView> GetData();
void SetData(const Ref<BinaryView>& data);
Ref<Architecture> GetRemoteArchitecture();

bool IsConnected();
Expand Down
6 changes: 6 additions & 0 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ Ref<BinaryView> DebuggerController::GetData()
}


void DebuggerController::SetData(const Ref<BinaryView>& data)
{
BNDebuggerSetData(m_object, data->GetObject());
}


Ref<Architecture> DebuggerController::GetRemoteArchitecture()
{
BNArchitecture* arch = BNDebuggerGetRemoteArchitecture(m_object);
Expand Down
2 changes: 2 additions & 0 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ extern "C"
ThreadStateChangedEvent,

ForceMemoryCacheUpdateEvent,
ModuleLoadedEvent,
};


Expand Down Expand Up @@ -313,6 +314,7 @@ extern "C"
DEBUGGER_FFI_API bool BNDebuggerControllerExists(BNBinaryView* data);
DEBUGGER_FFI_API BNBinaryView* BNDebuggerGetLiveView(BNDebuggerController* controller);
DEBUGGER_FFI_API BNBinaryView* BNDebuggerGetData(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerSetData(BNDebuggerController* controller, BNBinaryView* data);
DEBUGGER_FFI_API BNArchitecture* BNDebuggerGetRemoteArchitecture(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerIsConnected(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerIsConnectedToDebugServer(BNDebuggerController* controller);
Expand Down
40 changes: 25 additions & 15 deletions core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,26 +728,36 @@ void DebuggerController::DetectLoadedModule()
if (m_inputFileLoaded || (!m_state->GetRemoteBase(remoteBase)))
return;

FileMetadataRef fileMetadata = m_data->GetFile();
if (remoteBase != m_data->GetStart())
if (BinaryNinja::IsUIEnabled())
{
// remote base is different from the local base, first need a rebase
// ProgressIndicator progress(nullptr, "Rebase", "Rebasing...");
if (!fileMetadata->Rebase(m_data, remoteBase, [&](size_t cur, size_t total) { return true; }))
// When the UI is enabled, let the debugger UI do the work. It can show a progress bar if the operation takes
// a while.
DebuggerEvent event;
event.type = ModuleLoadedEvent;
event.data.absoluteAddress = remoteBase;
PostDebuggerEvent(event);
}
else
{
FileMetadataRef fileMetadata = m_data->GetFile();
if (remoteBase != m_data->GetStart())
{
LogWarn("rebase failed");
// 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; }))
{
LogWarn("rebase failed");
}
}
}

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

// ProgressIndicator progress(nullptr, "Debugger View", "Creating debugger view...");
bool ok = fileMetadata->CreateSnapshotedView(rebasedView, "Debugger", [&](size_t cur, size_t total) {
return true;
});
if (!ok)
LogWarn("create snapshoted view failed");
bool ok = fileMetadata->CreateSnapshotedView(rebasedView, "Debugger", [&](size_t cur, size_t total) {
return true;
});
if (!ok)
LogWarn("create snapshoted view failed");
}

m_liveView->UpdateAnalysis();
m_inputFileLoaded = true;
Expand Down
3 changes: 1 addition & 2 deletions core/debuggercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ namespace BinaryNinjaDebugger {

void SetLiveView(BinaryViewRef view) { m_liveView = view; }

void SetData(BinaryViewRef view) { m_data = view; }

DebugStopReason StepIntoIL(BNFunctionGraphType il);
DebugStopReason StepOverIL(BNFunctionGraphType il);

Expand Down Expand Up @@ -252,6 +250,7 @@ namespace BinaryNinjaDebugger {
DebugAdapter* GetAdapter() { return m_adapter; }
DebuggerState* GetState() { return m_state; }
BinaryViewRef GetData() const { return m_data; }
void SetData(BinaryViewRef view) { m_data = view; }
BinaryViewRef GetLiveView() const { return m_liveView; }

uint32_t GetExitCode();
Expand Down
6 changes: 6 additions & 0 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ BNBinaryView* BNDebuggerGetData(BNDebuggerController* controller)
}


void BNDebuggerSetData(BNDebuggerController* controller, BNBinaryView* data)
{
controller->object->SetData(new BinaryView(data));
}


BNArchitecture* BNDebuggerGetRemoteArchitecture(BNDebuggerController* controller)
{
return API_OBJECT_STATIC(controller->object->GetRemoteArchitecture());
Expand Down
45 changes: 45 additions & 0 deletions ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ limitations under the License.
#include "targetscriptingprovier.h"
#include "progresstask.h"
#include "attachprocess.h"
#include "progresstask.h"

using namespace BinaryNinja;
using namespace BinaryNinjaDebuggerAPI;
Expand Down Expand Up @@ -1124,6 +1125,50 @@ void DebuggerUI::updateUI(const DebuggerEvent& event)
break;
}

case ModuleLoadedEvent:
{
uint64_t remoteBase = event.data.absoluteAddress;
Ref<BinaryView> data = m_controller->GetData();
FileMetadataRef fileMetadata = data->GetFile();
ViewFrame* frame = m_context->getCurrentViewFrame();

if (remoteBase != data->GetStart())
{
bool result = false;
QString text = QString("Rebasing the input view...");
ProgressTask* task =
new ProgressTask(frame, "Rebase", text, "Cancel", [&](std::function<bool(size_t, size_t)> progress) {
result = fileMetadata->Rebase(data, remoteBase, progress);
});
task->wait();

if (!result)
{
LogWarn("failed to rebase the input view");
break;
}
}

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...");
ProgressTask* task =
new ProgressTask(frame, "Adding view", text, "Cancel", [&](std::function<bool(size_t, size_t)> progress) {
result = fileMetadata->CreateSnapshotedView(rebasedView, "Debugger", progress);
});
task->wait();

if (!result)
{
LogWarn("failed add the input view into the debugger view");
break;
}

break;
}

case RelativeBreakpointAddedEvent:
{
uint64_t address = m_controller->RelativeAddressToAbsolute(event.data.relativeAddress);
Expand Down

0 comments on commit e03d4cc

Please sign in to comment.