xref: /llvm-project/lldb/test/API/python_api/event/stop_hook.py (revision a7816c8e0086c1ae9b8ea15a6c252ca97f0405d1)
1*a7816c8eSJim Inghamimport lldb
2*a7816c8eSJim Inghamimport time
3*a7816c8eSJim Ingham
4*a7816c8eSJim Inghamclass StopHook:
5*a7816c8eSJim Ingham    # These dictionaries are used to pass data back to the test case.
6*a7816c8eSJim Ingham    # Since these are global, we need to know which test run is which.
7*a7816c8eSJim Ingham    # The test passes a key in the extra_args, we use that as the key
8*a7816c8eSJim Ingham    # for these dictionaries, and then the test can fetch out the right
9*a7816c8eSJim Ingham    # one.
10*a7816c8eSJim Ingham    counter = {}
11*a7816c8eSJim Ingham    non_stops = {}
12*a7816c8eSJim Ingham    def __init__(self, target, extra_args, dict):
13*a7816c8eSJim Ingham        self.target = target
14*a7816c8eSJim Ingham        self.regs = {}
15*a7816c8eSJim Ingham        self.instance = extra_args.GetValueForKey("instance").GetStringValue(100)
16*a7816c8eSJim Ingham        StopHook.counter[self.instance] = 0
17*a7816c8eSJim Ingham        StopHook.non_stops[self.instance] = 0
18*a7816c8eSJim Ingham
19*a7816c8eSJim Ingham    def handle_stop(self, exe_ctx, stream):
20*a7816c8eSJim Ingham        import time
21*a7816c8eSJim Ingham        # All this stop hook does is sleep a bit and count.  There was a bug
22*a7816c8eSJim Ingham        # where we were sending the secondary listener events when the
23*a7816c8eSJim Ingham        # private state thread's DoOnRemoval completed, rather than when
24*a7816c8eSJim Ingham        # the primary public process Listener consumes the event.  That
25*a7816c8eSJim Ingham        # became really clear when a stop hook artificially delayed the
26*a7816c8eSJim Ingham        # delivery of the primary listener's event - since IT had to come
27*a7816c8eSJim Ingham        # after the stop hook ran.
28*a7816c8eSJim Ingham        time.sleep(0.5)
29*a7816c8eSJim Ingham        StopHook.counter[self.instance] += 1
30*a7816c8eSJim Ingham        # When we were sending events too early, one symptom was the stop
31*a7816c8eSJim Ingham        # event would get triggered before the state had been changed.
32*a7816c8eSJim Ingham        # Watch for that here.
33*a7816c8eSJim Ingham        if exe_ctx.process.GetState() != lldb.eStateStopped:
34*a7816c8eSJim Ingham            StopHook.non_stops[self.instance] += 1
35*a7816c8eSJim Ingham
36