Skip to content

Commit 9c73bb6

Browse files
authored
Add active_pid property to get debugged process ID (#873)
1 parent 378eaa0 commit 9c73bb6

20 files changed

Lines changed: 103 additions & 0 deletions

api/debuggerapi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ namespace BinaryNinjaDebuggerAPI {
628628

629629
std::vector<DebugProcess> GetProcessList();
630630

631+
std::uint32_t GetActivePID();
632+
631633
std::vector<DebugThread> GetThreads();
632634
DebugThread GetActiveThread();
633635
void SetActiveThread(const DebugThread& thread);

api/debuggercontroller.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ std::vector<DebugProcess> DebuggerController::GetProcessList()
164164
}
165165

166166

167+
std::uint32_t DebuggerController::GetActivePID()
168+
{
169+
return BNDebuggerGetActivePID(m_object);
170+
}
171+
172+
167173
std::vector<DebugThread> DebuggerController::GetThreads()
168174
{
169175
size_t count;

api/ffi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ extern "C"
474474
DEBUGGER_FFI_API BNDebugProcess* BNDebuggerGetProcessList(BNDebuggerController* controller, size_t* count);
475475
DEBUGGER_FFI_API void BNDebuggerFreeProcessList(BNDebugProcess* processes, size_t count);
476476

477+
DEBUGGER_FFI_API uint32_t BNDebuggerGetActivePID(BNDebuggerController* controller);
478+
477479
DEBUGGER_FFI_API BNDebugThread* BNDebuggerGetThreads(BNDebuggerController* controller, size_t* count);
478480
DEBUGGER_FFI_API void BNDebuggerFreeThreads(BNDebugThread* threads, size_t count);
479481

api/python/debuggercontroller.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,17 @@ def pid_attach(self) -> int:
17941794
def pid_attach(self, pid: int) -> None:
17951795
dbgcore.BNDebuggerSetPIDAttach(self.handle, pid)
17961796

1797+
@property
1798+
def active_pid(self) -> int:
1799+
"""
1800+
The PID of the process currently being debugged. (read-only)
1801+
1802+
This returns the PID of the currently attached or running process.
1803+
1804+
:return: the PID of the active process, or 0 if no process is active or the PID is unavailable
1805+
"""
1806+
return dbgcore.BNDebuggerGetActivePID(self.handle)
1807+
17971808
@property
17981809
def executable_path(self) -> str:
17991810
"""

core/adapters/corelliumadapter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ bool CorelliumAdapter::Connect(const std::string& server, std::uint32_t port)
227227
const auto reply = this->m_rspConnector->TransmitAndReceive(RspData("?"));
228228
auto map = RspConnector::PacketToUnorderedMap(reply);
229229
this->m_lastActiveThreadId = map["thread"];
230+
this->m_processPid = map["thread"];
230231
m_isTargetRunning = false;
231232

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

10321033

1034+
std::uint32_t CorelliumAdapter::GetActivePID()
1035+
{
1036+
return m_processPid;
1037+
}
1038+
1039+
10331040
bool CorelliumAdapter::SuspendThread(std::uint32_t tid)
10341041
{
10351042
return false;

core/adapters/corelliumadapter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace BinaryNinjaDebugger
4848
std::vector<DebugBreakpoint> m_debugBreakpoints{};
4949

5050
std::uint32_t m_lastActiveThreadId{};
51+
std::uint32_t m_processPid{};
5152
uint8_t m_exitCode{};
5253

5354
std::string GetGDBServerPath();
@@ -120,6 +121,7 @@ namespace BinaryNinjaDebugger
120121
std::string InvokeBackendCommand(const std::string& command) override;
121122
std::string RunMonitorCommand(const std::string& command);
122123
uint64_t GetInstructionOffset() override;
124+
std::uint32_t GetActivePID() override;
123125

124126
DebugStopReason ResponseHandler();
125127

core/adapters/dbgengadapter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,20 @@ uint64_t DbgEngAdapter::GetStackPointer()
15061506
return stackPointer;
15071507
}
15081508

1509+
1510+
std::uint32_t DbgEngAdapter::GetActivePID()
1511+
{
1512+
if (!m_debugSystemObjects)
1513+
return 0;
1514+
1515+
ULONG pid {};
1516+
if (m_debugSystemObjects->GetCurrentProcessSystemId(&pid) != S_OK)
1517+
return 0;
1518+
1519+
return pid;
1520+
}
1521+
1522+
15091523
unsigned long DbgEngEventCallbacks::AddRef()
15101524
{
15111525
return 1;

core/adapters/dbgengadapter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ namespace BinaryNinjaDebugger {
228228
std::string InvokeBackendCommand(const std::string& command) override;
229229
uint64_t GetInstructionOffset() override;
230230
uint64_t GetStackPointer() override;
231+
std::uint32_t GetActivePID() override;
231232

232233
bool SupportFeature(DebugAdapterCapacity feature) override;
233234

core/adapters/esrevenadapter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ bool EsrevenAdapter::Connect(const std::string& server, std::uint32_t port)
259259
const auto reply = this->m_rspConnector->TransmitAndReceive(RspData("?"));
260260
auto map = RspConnector::PacketToUnorderedMap(reply);
261261
this->m_lastActiveThreadId = map["thread"];
262+
this->m_processPid = map["thread"];
262263
m_isTargetRunning = false;
263264

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

1203+
1204+
std::uint32_t EsrevenAdapter::GetActivePID()
1205+
{
1206+
return m_processPid;
1207+
}
1208+
1209+
12021210
DebugStopReason EsrevenAdapter::StopReason()
12031211
{
12041212
return this->m_lastStopReason;

core/adapters/esrevenadapter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace BinaryNinjaDebugger
6161
std::optional<std::vector<DebugModule>> m_moduleCache{};
6262

6363
std::uint32_t m_lastActiveThreadId{};
64+
std::uint32_t m_processPid{};
6465
uint8_t m_exitCode{};
6566

6667
std::string GetGDBServerPath();
@@ -145,6 +146,7 @@ namespace BinaryNinjaDebugger
145146
std::string RunMonitorCommand(const std::string& command);
146147
uint64_t GetInstructionOffset() override;
147148
uint64_t GetStackPointer() override;
149+
std::uint32_t GetActivePID() override;
148150

149151
DebugStopReason ResponseHandler(bool notifyStopped = true);
150152

0 commit comments

Comments
 (0)