Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ namespace BinaryNinjaDebuggerAPI {

std::vector<DebugProcess> GetProcessList();

std::uint32_t GetActivePID();

std::vector<DebugThread> GetThreads();
DebugThread GetActiveThread();
void SetActiveThread(const DebugThread& thread);
Expand Down
6 changes: 6 additions & 0 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ std::vector<DebugProcess> DebuggerController::GetProcessList()
}


std::uint32_t DebuggerController::GetActivePID()
{
return BNDebuggerGetActivePID(m_object);
}


std::vector<DebugThread> DebuggerController::GetThreads()
{
size_t count;
Expand Down
2 changes: 2 additions & 0 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ extern "C"
DEBUGGER_FFI_API BNDebugProcess* BNDebuggerGetProcessList(BNDebuggerController* controller, size_t* count);
DEBUGGER_FFI_API void BNDebuggerFreeProcessList(BNDebugProcess* processes, size_t count);

DEBUGGER_FFI_API uint32_t BNDebuggerGetActivePID(BNDebuggerController* controller);

DEBUGGER_FFI_API BNDebugThread* BNDebuggerGetThreads(BNDebuggerController* controller, size_t* count);
DEBUGGER_FFI_API void BNDebuggerFreeThreads(BNDebugThread* threads, size_t count);

Expand Down
11 changes: 11 additions & 0 deletions api/python/debuggercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,17 @@ def pid_attach(self) -> int:
def pid_attach(self, pid: int) -> None:
dbgcore.BNDebuggerSetPIDAttach(self.handle, pid)

@property
def active_pid(self) -> int:
"""
The PID of the process currently being debugged. (read-only)

This returns the PID of the currently attached or running process.

:return: the PID of the active process, or 0 if no process is active or the PID is unavailable
"""
return dbgcore.BNDebuggerGetActivePID(self.handle)

@property
def executable_path(self) -> str:
"""
Expand Down
7 changes: 7 additions & 0 deletions core/adapters/corelliumadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ bool CorelliumAdapter::Connect(const std::string& server, std::uint32_t port)
const auto reply = this->m_rspConnector->TransmitAndReceive(RspData("?"));
auto map = RspConnector::PacketToUnorderedMap(reply);
this->m_lastActiveThreadId = map["thread"];
this->m_processPid = map["thread"];
m_isTargetRunning = false;

Ref<Settings> settings = Settings::Instance();
Expand Down Expand Up @@ -1030,6 +1031,12 @@ std::vector<DebugProcess> CorelliumAdapter::GetProcessList()
}


std::uint32_t CorelliumAdapter::GetActivePID()
{
return m_processPid;
}


bool CorelliumAdapter::SuspendThread(std::uint32_t tid)
{
return false;
Expand Down
2 changes: 2 additions & 0 deletions core/adapters/corelliumadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace BinaryNinjaDebugger
std::vector<DebugBreakpoint> m_debugBreakpoints{};

std::uint32_t m_lastActiveThreadId{};
std::uint32_t m_processPid{};
uint8_t m_exitCode{};

std::string GetGDBServerPath();
Expand Down Expand Up @@ -120,6 +121,7 @@ namespace BinaryNinjaDebugger
std::string InvokeBackendCommand(const std::string& command) override;
std::string RunMonitorCommand(const std::string& command);
uint64_t GetInstructionOffset() override;
std::uint32_t GetActivePID() override;

DebugStopReason ResponseHandler();

Expand Down
14 changes: 14 additions & 0 deletions core/adapters/dbgengadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,20 @@ uint64_t DbgEngAdapter::GetStackPointer()
return stackPointer;
}


std::uint32_t DbgEngAdapter::GetActivePID()
{
if (!m_debugSystemObjects)
return 0;

ULONG pid {};
if (m_debugSystemObjects->GetCurrentProcessSystemId(&pid) != S_OK)
return 0;

return pid;
}


unsigned long DbgEngEventCallbacks::AddRef()
{
return 1;
Expand Down
1 change: 1 addition & 0 deletions core/adapters/dbgengadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ namespace BinaryNinjaDebugger {
std::string InvokeBackendCommand(const std::string& command) override;
uint64_t GetInstructionOffset() override;
uint64_t GetStackPointer() override;
std::uint32_t GetActivePID() override;

bool SupportFeature(DebugAdapterCapacity feature) override;

Expand Down
8 changes: 8 additions & 0 deletions core/adapters/esrevenadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ bool EsrevenAdapter::Connect(const std::string& server, std::uint32_t port)
const auto reply = this->m_rspConnector->TransmitAndReceive(RspData("?"));
auto map = RspConnector::PacketToUnorderedMap(reply);
this->m_lastActiveThreadId = map["thread"];
this->m_processPid = map["thread"];
m_isTargetRunning = false;

if (Settings::Instance()->Get<bool>("debugger.stopAtEntryPoint") && m_hasEntryFunction)
Expand Down Expand Up @@ -1199,6 +1200,13 @@ uint64_t EsrevenAdapter::GetStackPointer()
return value;
}


std::uint32_t EsrevenAdapter::GetActivePID()
{
return m_processPid;
}


DebugStopReason EsrevenAdapter::StopReason()
{
return this->m_lastStopReason;
Expand Down
2 changes: 2 additions & 0 deletions core/adapters/esrevenadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace BinaryNinjaDebugger
std::optional<std::vector<DebugModule>> m_moduleCache{};

std::uint32_t m_lastActiveThreadId{};
std::uint32_t m_processPid{};
uint8_t m_exitCode{};

std::string GetGDBServerPath();
Expand Down Expand Up @@ -145,6 +146,7 @@ namespace BinaryNinjaDebugger
std::string RunMonitorCommand(const std::string& command);
uint64_t GetInstructionOffset() override;
uint64_t GetStackPointer() override;
std::uint32_t GetActivePID() override;

DebugStopReason ResponseHandler(bool notifyStopped = true);

Expand Down
8 changes: 8 additions & 0 deletions core/adapters/gdbadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ bool GdbAdapter::Connect(const std::string& server, std::uint32_t port)
const auto reply = this->m_rspConnector->TransmitAndReceive(RspData("?"));
auto map = RspConnector::PacketToUnorderedMap(reply);
this->m_lastActiveThreadId = map["thread"];
this->m_processPid = map["thread"];
m_isTargetRunning = false;

if (Settings::Instance()->Get<bool>("debugger.stopAtEntryPoint") && m_hasEntryFunction)
Expand Down Expand Up @@ -1247,6 +1248,13 @@ uint64_t GdbAdapter::GetStackPointer()
return value;
}


std::uint32_t GdbAdapter::GetActivePID()
{
return m_processPid;
}


DebugStopReason GdbAdapter::StopReason()
{
return this->m_lastStopReason;
Expand Down
2 changes: 2 additions & 0 deletions core/adapters/gdbadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace BinaryNinjaDebugger
std::optional<std::vector<DebugModule>> m_moduleCache{};

std::uint32_t m_lastActiveThreadId{};
std::uint32_t m_processPid{};
uint8_t m_exitCode{};

std::string GetGDBServerPath();
Expand Down Expand Up @@ -145,6 +146,7 @@ namespace BinaryNinjaDebugger
std::string RunMonitorCommand(const std::string& command);
uint64_t GetInstructionOffset() override;
uint64_t GetStackPointer() override;
std::uint32_t GetActivePID() override;

DebugStopReason ResponseHandler(bool notifyStopped = true);

Expand Down
9 changes: 9 additions & 0 deletions core/adapters/lldbadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,15 @@ std::vector<DebugProcess> LldbAdapter::GetProcessList()
}


std::uint32_t LldbAdapter::GetActivePID()
{
if (!m_process.IsValid())
return 0;

return m_process.GetProcessID();
}


std::vector<DebugThread> LldbAdapter::GetThreadList()
{
size_t threadCount = m_process.GetNumThreads();
Expand Down
2 changes: 2 additions & 0 deletions core/adapters/lldbadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace BinaryNinjaDebugger {

std::vector<DebugProcess> GetProcessList() override;

std::uint32_t GetActivePID() override;

std::vector<DebugThread> GetThreadList() override;

DebugThread GetActiveThread() const override;
Expand Down
9 changes: 9 additions & 0 deletions core/adapters/lldbcoredumpadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,12 @@ void LldbCoreDumpAdapter::GenerateDefaultAdapterSettings(BinaryView* data)
if (scope != SettingsResourceScope)
adapterSettings->Set("common.inputFile", data->GetFile()->GetOriginalFilename(), data, SettingsResourceScope);
}


std::uint32_t LldbCoreDumpAdapter::GetActivePID()
{
if (!m_process.IsValid())
return 0;

return m_process.GetProcessID();
}
1 change: 1 addition & 0 deletions core/adapters/lldbcoredumpadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace BinaryNinjaDebugger {
uint64_t GetInstructionOffset() override;

uint64_t GetStackPointer() override;
std::uint32_t GetActivePID() override;

bool SupportFeature(DebugAdapterCapacity feature) override;

Expand Down
2 changes: 2 additions & 0 deletions core/debugadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ namespace BinaryNinjaDebugger {

virtual std::vector<DebugProcess> GetProcessList() = 0;

virtual std::uint32_t GetActivePID() = 0;

virtual std::vector<DebugThread> GetThreadList() = 0;

virtual DebugThread GetActiveThread() const = 0;
Expand Down
8 changes: 8 additions & 0 deletions core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,14 @@ uint32_t DebuggerController::GetExitCode()
}


uint32_t DebuggerController::GetActivePID()
{
if (!m_adapter)
return 0;
return m_adapter->GetActivePID();
}


void DebuggerController::WriteStdIn(const std::string message)
{
if (m_adapter && m_state->IsRunning())
Expand Down
1 change: 1 addition & 0 deletions core/debuggercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ namespace BinaryNinjaDebugger {
DebuggerFileAccessor* GetMemoryAccessor() const { return m_accessor; }

uint32_t GetExitCode();
uint32_t GetActivePID();

void WriteStdIn(const std::string message);

Expand Down
6 changes: 6 additions & 0 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ void BNDebuggerFreeProcessList(BNDebugProcess* processes, size_t count)
}


uint32_t BNDebuggerGetActivePID(BNDebuggerController* controller)
{
return controller->object->GetActivePID();
}


BNDebugThread* BNDebuggerGetThreads(BNDebuggerController* controller, size_t* size)
{
std::vector<DebugThread> threads = controller->object->GetAllThreads();
Expand Down