Skip to content

Commit

Permalink
And C++/Python API for DebuggerController::LaunchAndWait(). Update te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
xusheng6 committed Apr 11, 2023
1 parent e122766 commit 33542f3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ namespace BinaryNinjaDebuggerAPI {

// target control
bool Launch();
bool LaunchAndWait();
bool Execute();
void Restart();
void Quit();
Expand Down
6 changes: 6 additions & 0 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ bool DebuggerController::Launch()
}


bool DebuggerController::LaunchAndWait()
{
return BNDebuggerLaunchAndWait(m_object);
}


bool DebuggerController::Execute()
{
return BNDebuggerExecute(m_object);
Expand Down
1 change: 1 addition & 0 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ extern "C"

// target control
DEBUGGER_FFI_API bool BNDebuggerLaunch(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerLaunchAndWait(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerExecute(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerRestart(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerQuit(BNDebuggerController* controller);
Expand Down
6 changes: 6 additions & 0 deletions api/python/debuggercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,12 @@ def launch(self) -> bool:
"""
return dbgcore.BNDebuggerLaunch(self.handle)

def launch_and_wait(self) -> bool:
"""
Launch the target and wait for all debugger events to be processed
"""
return dbgcore.BNDebuggerLaunchAndWait(self.handle)

def restart(self) -> None:
"""
Restart the target
Expand Down
6 changes: 6 additions & 0 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ bool BNDebuggerLaunch(BNDebuggerController* controller)
}


bool BNDebuggerLaunchAndWait(BNDebuggerController* controller)
{
return controller->object->LaunchAndWait();
}


bool BNDebuggerExecute(BNDebuggerController* controller)
{
return controller->object->Execute();
Expand Down
22 changes: 11 additions & 11 deletions test/debugger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_repeated_use(self):
def run_once():
dbg = DebuggerController(bv)
dbg.cmd_line = 'foobar'
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())

# continue execution to the entry point, and check the stop reason
reason = dbg.step_into_and_wait()
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_return_code(self):
dbg = DebuggerController(bv)
dbg.cmd_line = arg

self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
reason = dbg.go_and_wait()
self.assertEqual(reason, DebugStopReason.ProcessExited)
exit_code = dbg.exit_code
Expand All @@ -111,7 +111,7 @@ def test_exception_segfault(self):
dbg = DebuggerController(bv)

dbg.cmd_line = 'segfault'
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
# time.sleep(1)
reason = dbg.go_and_wait()
self.expect_segfault(reason)
Expand All @@ -123,7 +123,7 @@ def test_exception_segfault(self):
# bv = BinaryViewType.get_view_of_file(fpath)
# dbg = DebuggerController(bv)
# dbg.cmd_line = 'illegalinstr'
# self.assertTrue(dbg.launch())
# self.assertTrue(dbg.launch_and_wait())
# dbg.go()
# reason = dbg.go()
# if platform.system() in ['Windows', 'Linux']:
Expand All @@ -146,7 +146,7 @@ def test_exception_divzero(self):
dbg = DebuggerController(bv)
if not self.arch == 'arm64':
dbg.cmd_line = 'divzero'
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
reason = dbg.go_and_wait()
self.expect_divide_by_zero(reason)
dbg.quit_and_wait()
Expand All @@ -156,7 +156,7 @@ def test_step_into(self):
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
dbg.cmd_line = 'foobar'
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
reason = dbg.step_into_and_wait()
self.assertEqual(reason, DebugStopReason.SingleStep)
reason = dbg.step_into_and_wait()
Expand All @@ -168,7 +168,7 @@ def test_breakpoint(self):
fpath = name_to_fpath('helloworld', self.arch)
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
# TODO: right now we are not returning whether the operation succeeds, so we cannot use assertTrue/assertFalse
# breakpoint set/clear should fail at 0
self.assertIsNone(dbg.add_breakpoint(0))
Expand All @@ -186,7 +186,7 @@ def test_register_read_write(self):
fpath = name_to_fpath('helloworld', self.arch)
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())

arch_name = bv.arch.name
if arch_name == 'x86':
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_memory_read_write(self):
fpath = name_to_fpath('helloworld', self.arch)
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())

# Due to https://github.com/Vector35/debugger/issues/124, we have to skip the bytes at the entry point
addr = dbg.ip + 10
Expand All @@ -239,7 +239,7 @@ def test_thread(self):
fpath = name_to_fpath('helloworld_thread', self.arch)
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())

dbg.go()
time.sleep(1)
Expand All @@ -264,7 +264,7 @@ def test_assembly_code(self):
fpath = name_to_fpath('asmtest', 'x86_64')
bv = BinaryViewType.get_view_of_file(fpath)
dbg = DebuggerController(bv)
self.assertTrue(dbg.launch())
self.assertTrue(dbg.launch_and_wait())
entry = dbg.live_view.entry_point
self.assertEqual(dbg.ip, entry)

Expand Down

0 comments on commit 33542f3

Please sign in to comment.