Skip to content

Commit

Permalink
Support explicitly force updating all memory cache and analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Feb 1, 2023
1 parent b1f63d4 commit 0ea39c4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ namespace BinaryNinjaDebuggerAPI {
bool SetAdapterProperty(const std::string& name, const BinaryNinja::Ref<Metadata>& value);

bool ActivateDebugAdapter();

void PostDebuggerEvent(const DebuggerEvent& event);
};


Expand Down
33 changes: 33 additions & 0 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,36 @@ bool DebuggerController::ActivateDebugAdapter()
{
return BNDebuggerActivateDebugAdapter(m_object);
}


void DebuggerController::PostDebuggerEvent(const DebuggerEvent &event)
{
BNDebuggerEvent* evt = new BNDebuggerEvent;

evt->type = event.type;
evt->data.targetStoppedData.reason = event.data.targetStoppedData.reason;
evt->data.targetStoppedData.exitCode = event.data.targetStoppedData.exitCode;
evt->data.targetStoppedData.lastActiveThread = event.data.targetStoppedData.lastActiveThread;
evt->data.targetStoppedData.data = event.data.targetStoppedData.data;

evt->data.errorData.error = BNDebuggerAllocString(event.data.errorData.error.c_str());
evt->data.errorData.shortError = BNDebuggerAllocString(event.data.errorData.shortError.c_str());
evt->data.errorData.data = event.data.errorData.data;

evt->data.exitData.exitCode = event.data.exitData.exitCode;

evt->data.relativeAddress.module = BNDebuggerAllocString(event.data.relativeAddress.module.c_str());
evt->data.relativeAddress.offset = event.data.relativeAddress.offset;

evt->data.absoluteAddress = event.data.absoluteAddress;

evt->data.messageData.message = BNDebuggerAllocString(event.data.messageData.message.c_str());

BNDebuggerPostDebuggerEvent(m_object, evt);

BNDebuggerFreeString(evt->data.errorData.error);
BNDebuggerFreeString(evt->data.errorData.shortError);
BNDebuggerFreeString(evt->data.relativeAddress.module);
BNDebuggerFreeString(evt->data.messageData.message);
delete evt;
}
4 changes: 4 additions & 0 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ extern "C"
// in the register widget, etc.). It is not emitted when the target executes and then stops.
RegisterChangedEvent,
ThreadStateChangedEvent,

ForceMemoryCacheUpdateEvent,
};


Expand Down Expand Up @@ -431,6 +433,8 @@ extern "C"

DEBUGGER_FFI_API bool BNDebuggerActivateDebugAdapter(BNDebuggerController* controller);

DEBUGGER_FFI_API void BNDebuggerPostDebuggerEvent(BNDebuggerController* controller, BNDebuggerEvent* event);

// DebugAdapterType
DEBUGGER_FFI_API BNDebugAdapterType* BNGetDebugAdapterTypeByName(const char* name);
DEBUGGER_FFI_API bool BNDebugAdapterTypeCanExecute(BNDebugAdapterType* adapter, BNBinaryView* data);
Expand Down
26 changes: 26 additions & 0 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,29 @@ bool BNDebuggerActivateDebugAdapter(BNDebuggerController* controller)
{
return controller->object->ActivateDebugAdapter();
}


void BNDebuggerPostDebuggerEvent(BNDebuggerController* controller, BNDebuggerEvent* event)
{
DebuggerEvent evt;
evt.type = event->type;
evt.data.targetStoppedData.reason = event->data.targetStoppedData.reason;
evt.data.targetStoppedData.exitCode = event->data.targetStoppedData.exitCode;
evt.data.targetStoppedData.lastActiveThread = event->data.targetStoppedData.lastActiveThread;
evt.data.targetStoppedData.data = event->data.targetStoppedData.data;

evt.data.errorData.error = event->data.errorData.error;
evt.data.errorData.shortError = event->data.errorData.shortError;
evt.data.errorData.data = event->data.errorData.data;

evt.data.exitData.exitCode = event->data.exitData.exitCode;

evt.data.relativeAddress.module = event->data.relativeAddress.module;
evt.data.relativeAddress.offset = event->data.relativeAddress.offset;

evt.data.absoluteAddress = event->data.absoluteAddress;

evt.data.messageData.message = event->data.messageData.message;

controller->object->PostDebuggerEvent(evt);
}
9 changes: 9 additions & 0 deletions core/processview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ void DebugProcessView::MarkDirty()
}


void DebugProcessView::ForceMemoryCacheUpdate()
{
BinaryView::NotifyDataWritten(0, GetLength());
}


void DebugProcessView::eventHandler(const DebuggerEvent &event)
{
switch (event.type)
Expand All @@ -180,6 +186,9 @@ void DebugProcessView::eventHandler(const DebuggerEvent &event)
// deleted. And it can cause a crash in certain cases.
MarkDirty();
break;
case ForceMemoryCacheUpdateEvent:
ForceMemoryCacheUpdate();
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions core/processview.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace BinaryNinjaDebugger
virtual bool Init() override;

void MarkDirty();
void ForceMemoryCacheUpdate();
void eventHandler(const DebuggerEvent& event);
};

Expand Down
18 changes: 18 additions & 0 deletions ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,24 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context)
connectedAndStopped));
debuggerMenu->addAction("Create Stack View", "Misc");

UIAction::registerAction("Force Update Memory Cache");
context->globalActions()->bindAction("Force Update Memory Cache",
UIAction(
[=](const UIActionContext& ctxt) {
if (!ctxt.binaryView)
return;

auto controller = DebuggerController::GetController(ctxt.binaryView);
if (!controller)
return;

DebuggerEvent event;
event.type = ForceMemoryCacheUpdateEvent;
controller->PostDebuggerEvent(event);
},
connectedAndStopped));
debuggerMenu->addAction("Force Update Memory Cache", "Misc");

#ifdef WIN32
UIAction::registerAction("Reinstall DbgEng Redistributable");
context->globalActions()->bindAction("Reinstall DbgEng Redistributable", UIAction([=](const UIActionContext& ctxt) {
Expand Down

0 comments on commit 0ea39c4

Please sign in to comment.