@@ -216,6 +216,7 @@ def __init__(self):
216
216
self.installed_scripts = []
217
217
self.installed_modules = []
218
218
self.overridden_message_rates = {}
219
+ self.raising_debug_trap_on_exceptions = False
219
220
220
221
221
222
# 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):
3684
3685
raise NotAchievedException("Air Temperature not received from HIGH_LATENCY2")
3685
3686
self.HIGH_LATENCY2_links()
3686
3687
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
+
3687
3717
def context_set_message_rate_hz(self, id, rate_hz, run_cmd=None):
3688
3718
if run_cmd is None:
3689
3719
run_cmd = self.run_cmd
@@ -6456,6 +6486,8 @@ def context_pop(self, process_interaction_allowed=True, hooks_already_removed=Fa
6456
6486
self.remove_installed_modules(module)
6457
6487
if dead.sitl_commandline_customised and len(self.contexts):
6458
6488
self.contexts[-1].sitl_commandline_customised = True
6489
+ if dead.raising_debug_trap_on_exceptions:
6490
+ sys.settrace(None)
6459
6491
6460
6492
dead_parameters_dict = {}
6461
6493
for p in dead.parameters:
0 commit comments