Skip to content

Commit 9668c6f

Browse files
committed
autotest: add context option to send debug trap on exception
1 parent a254b0c commit 9668c6f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Tools/autotest/vehicle_test_suite.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def __init__(self):
216216
self.installed_scripts = []
217217
self.installed_modules = []
218218
self.overridden_message_rates = {}
219+
self.raising_debug_trap_on_exceptions = False
219220

220221

221222
# https://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python
@@ -3684,6 +3685,35 @@ def HIGH_LATENCY2(self):
36843685
raise NotAchievedException("Air Temperature not received from HIGH_LATENCY2")
36853686
self.HIGH_LATENCY2_links()
36863687

3688+
def context_set_send_debug_trap_on_exceptions(self, value=True):
3689+
'''send a debug trap to ArduPilot if an ErrorException is raised.'''
3690+
3691+
# this is a diagnostic tool, only expected to be used for
3692+
# debugging, never for committed code
3693+
3694+
def trace_calls(frame, event, arg):
3695+
if event == 'exception':
3696+
exc_type, exc_value, tb = arg
3697+
if issubclass(exc_type, ErrorException):
3698+
print(f"[Tracer] Exception raised: {exc_type}")
3699+
self.send_debug_trap()
3700+
return trace_calls
3701+
3702+
context = self.context_get()
3703+
3704+
if value:
3705+
if sys.gettrace() is not None:
3706+
raise ValueError("Can't trace, something else already is")
3707+
sys.settrace(trace_calls)
3708+
context.raising_debug_trap_on_exceptions = True
3709+
return
3710+
3711+
if not sys.gettrace():
3712+
raise ValueError("Expected to see something tracing")
3713+
3714+
context.raising_debug_trap_on_exceptions = False
3715+
sys.settrace(None)
3716+
36873717
def context_set_message_rate_hz(self, id, rate_hz, run_cmd=None):
36883718
if run_cmd is None:
36893719
run_cmd = self.run_cmd
@@ -6456,6 +6486,8 @@ def context_pop(self, process_interaction_allowed=True, hooks_already_removed=Fa
64566486
self.remove_installed_modules(module)
64576487
if dead.sitl_commandline_customised and len(self.contexts):
64586488
self.contexts[-1].sitl_commandline_customised = True
6489+
if dead.raising_debug_trap_on_exceptions:
6490+
sys.settrace(None)
64596491

64606492
dead_parameters_dict = {}
64616493
for p in dead.parameters:

0 commit comments

Comments
 (0)