Skip to content

Commit

Permalink
Add a small amount of delay before resuming the target
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Sep 18, 2024
1 parent 81ba6e5 commit 2012056
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions test/debugger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def is_wow64(fpath):
return a == '64bit' and b.startswith('Windows')


def sleep_and_go(dbg):
time.sleep(0.1)
return dbg.go_and_wait()


def sleep_and_step_into(dbg):
time.sleep(0.1)
return dbg.step_into_and_wait()


class DebuggerAPI(unittest.TestCase):
# Always skip the base class so it will never be executed
@unittest.skip("do not run the base test class")
Expand All @@ -60,14 +70,14 @@ def run_once():
self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])

# continue execution to the entry point, and check the stop reason
reason = dbg.step_into_and_wait()
reason = sleep_and_step_into(dbg)
self.assertEqual(reason, DebugStopReason.SingleStep)
reason = dbg.step_into_and_wait()
reason = sleep_and_step_into(dbg)
self.assertEqual(reason, DebugStopReason.SingleStep)
reason = dbg.step_into_and_wait()
reason = sleep_and_step_into(dbg)
self.assertEqual(reason, DebugStopReason.SingleStep)
# go until executing done
reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.assertEqual(reason, DebugStopReason.ProcessExited)

# Do the same thing for 10 times
Expand All @@ -94,7 +104,7 @@ def test_return_code(self):
dbg.cmd_line = arg

self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])
reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.assertEqual(reason, DebugStopReason.ProcessExited)
exit_code = dbg.exit_code
self.assertIn(exit_code, expected)
Expand All @@ -113,7 +123,7 @@ def test_exception_segfault(self):
dbg.cmd_line = 'segfault'
self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])
# time.sleep(1)
reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.expect_segfault(reason)
dbg.quit_and_wait()

Expand Down Expand Up @@ -147,7 +157,7 @@ def test_exception_divzero(self):
if not self.arch == 'arm64':
dbg.cmd_line = 'divzero'
self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])
reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.expect_divide_by_zero(reason)
dbg.quit_and_wait()

Expand All @@ -157,11 +167,11 @@ def test_step_into(self):
dbg = DebuggerController(bv)
dbg.cmd_line = 'foobar'
self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])
reason = dbg.step_into_and_wait()
reason = sleep_and_step_into(dbg)
self.assertEqual(reason, DebugStopReason.SingleStep)
reason = dbg.step_into_and_wait()
reason = sleep_and_step_into(dbg)
self.assertEqual(reason, DebugStopReason.SingleStep)
reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.assertEqual(reason, DebugStopReason.ProcessExited)

def test_breakpoint(self):
Expand Down Expand Up @@ -259,20 +269,24 @@ def test_thread(self):
self.assertGreater(len(threads), 1)
dbg.quit_and_wait()

@unittest.skipIf(platform.system() == 'Windows', 'Skip restart test on Windows for now')
def test_restart(self):
fpath = name_to_fpath('helloworld_thread', self.arch)
bv = load(fpath)
dbg = DebuggerController(bv)
self.assertNotIn(dbg.launch_and_wait(), [DebugStopReason.ProcessExited, DebugStopReason.InternalError])

time.sleep(0.1)
dbg.go()
time.sleep(1)
dbg.pause_and_wait()
self.assertGreater(len(dbg.threads), 1)

time.sleep(0.1)
ret = dbg.restart_and_wait()
self.assertNotIn(ret, [DebugStopReason.ProcessExited, DebugStopReason.InternalError])

time.sleep(0.1)
dbg.go()
time.sleep(1)
ret = dbg.restart_and_wait()
Expand All @@ -290,21 +304,21 @@ def test_assembly_code(self):

# TODO: we can use BN to disassemble the binary and find out how long is the instruction
# step into nop
dbg.step_into_and_wait()
sleep_and_step_into(dbg)
self.assertEqual(dbg.ip, entry+1)
# step into call, return
dbg.step_into_and_wait()
dbg.step_into_and_wait()
sleep_and_step_into(dbg)
sleep_and_step_into(dbg)
# back
self.assertEqual(dbg.ip, entry+6)
dbg.step_into_and_wait()
sleep_and_step_into(dbg)
# step into call, return
dbg.step_into_and_wait()
dbg.step_into_and_wait()
sleep_and_step_into(dbg)
sleep_and_step_into(dbg)
# back
self.assertEqual(dbg.ip, entry+12)

reason = dbg.go_and_wait()
reason = sleep_and_go(dbg)
self.assertEqual(reason, DebugStopReason.ProcessExited)

@unittest.skipIf(platform.system() == 'Linux', 'Cannot attach to pid unless running as root')
Expand Down

0 comments on commit 2012056

Please sign in to comment.