Johnny Chen | 6cf1bc3 | 2011-07-18 22:11:53 +0000 | [diff] [blame] | 1 | //===-- SWIG Interface for SBEvent ------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | namespace lldb { |
| 11 | |
| 12 | class SBBroadcaster; |
| 13 | |
| 14 | %feature("docstring", |
| 15 | "API clients can register to receive events. |
| 16 | |
| 17 | For example, check out the following output: |
| 18 | |
| 19 | Try wait for event... |
| 20 | Event description: 0x103d0bb70 Event: broadcaster = 0x1009c8410, type = 0x00000001, data = { process = 0x1009c8400 (pid = 21528), state = running} |
| 21 | Event data flavor: Process::ProcessEventData |
| 22 | Process state: running |
| 23 | |
| 24 | Try wait for event... |
| 25 | Event description: 0x103a700a0 Event: broadcaster = 0x1009c8410, type = 0x00000001, data = { process = 0x1009c8400 (pid = 21528), state = stopped} |
| 26 | Event data flavor: Process::ProcessEventData |
| 27 | Process state: stopped |
| 28 | |
| 29 | Try wait for event... |
| 30 | Event description: 0x103d0d4a0 Event: broadcaster = 0x1009c8410, type = 0x00000001, data = { process = 0x1009c8400 (pid = 21528), state = exited} |
| 31 | Event data flavor: Process::ProcessEventData |
| 32 | Process state: exited |
| 33 | |
| 34 | Try wait for event... |
| 35 | timeout occurred waiting for event... |
| 36 | |
| 37 | from test/python_api/event/TestEventspy: |
| 38 | |
| 39 | def do_listen_for_and_print_event(self): |
| 40 | '''Create a listener and use SBEvent API to print the events received.''' |
| 41 | exe = os.path.join(os.getcwd(), 'a.out') |
| 42 | |
| 43 | # Create a target by the debugger. |
| 44 | target = self.dbg.CreateTarget(exe) |
| 45 | self.assertTrue(target, VALID_TARGET) |
| 46 | |
| 47 | # Now create a breakpoint on main.c by name 'c'. |
| 48 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 49 | |
| 50 | # Now launch the process, and do not stop at the entry point. |
| 51 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 52 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
| 53 | PROCESS_STOPPED) |
| 54 | |
| 55 | # Get a handle on the process's broadcaster. |
| 56 | broadcaster = process.GetBroadcaster() |
| 57 | |
| 58 | # Create an empty event object. |
| 59 | event = lldb.SBEvent() |
| 60 | |
| 61 | # Create a listener object and register with the broadcaster. |
| 62 | listener = lldb.SBListener('my listener') |
| 63 | rc = broadcaster.AddListener(listener, lldb.SBProcess.eBroadcastBitStateChanged) |
| 64 | self.assertTrue(rc, 'AddListener successfully retruns') |
| 65 | |
| 66 | traceOn = self.TraceOn() |
| 67 | if traceOn: |
| 68 | lldbutil.print_stacktraces(process) |
| 69 | |
| 70 | # Create MyListeningThread class to wait for any kind of event. |
| 71 | import threading |
| 72 | class MyListeningThread(threading.Thread): |
| 73 | def run(self): |
| 74 | count = 0 |
| 75 | # Let's only try at most 4 times to retrieve any kind of event. |
| 76 | # After that, the thread exits. |
| 77 | while not count > 3: |
| 78 | if traceOn: |
| 79 | print 'Try wait for event...' |
| 80 | if listener.WaitForEventForBroadcasterWithType(5, |
| 81 | broadcaster, |
| 82 | lldb.SBProcess.eBroadcastBitStateChanged, |
| 83 | event): |
| 84 | if traceOn: |
| 85 | desc = lldbutil.get_description(event) |
| 86 | print 'Event description:', desc |
| 87 | print 'Event data flavor:', event.GetDataFlavor() |
| 88 | print 'Process state:', lldbutil.state_type_to_str(process.GetState()) |
| 89 | print |
| 90 | else: |
| 91 | if traceOn: |
| 92 | print 'timeout occurred waiting for event...' |
| 93 | count = count + 1 |
| 94 | return |
| 95 | |
| 96 | # Let's start the listening thread to retrieve the events. |
| 97 | my_thread = MyListeningThread() |
| 98 | my_thread.start() |
| 99 | |
| 100 | # Use Python API to continue the process. The listening thread should be |
| 101 | # able to receive the state changed events. |
| 102 | process.Continue() |
| 103 | |
| 104 | # Use Python API to kill the process. The listening thread should be |
| 105 | # able to receive the state changed event, too. |
| 106 | process.Kill() |
| 107 | |
| 108 | # Wait until the 'MyListeningThread' terminates. |
| 109 | my_thread.join() |
| 110 | ") SBEvent; |
| 111 | class SBEvent |
| 112 | { |
| 113 | public: |
| 114 | SBEvent(); |
| 115 | |
| 116 | SBEvent (const lldb::SBEvent &rhs); |
| 117 | |
| 118 | %feature("autodoc", |
| 119 | "__init__(self, int type, str data) -> SBEvent (make an event that contains a C string)" |
| 120 | ) SBEvent; |
| 121 | SBEvent (uint32_t event, const char *cstr, uint32_t cstr_len); |
| 122 | |
| 123 | ~SBEvent(); |
| 124 | |
| 125 | bool |
| 126 | IsValid() const; |
| 127 | |
| 128 | const char * |
| 129 | GetDataFlavor (); |
| 130 | |
| 131 | uint32_t |
| 132 | GetType () const; |
| 133 | |
| 134 | lldb::SBBroadcaster |
| 135 | GetBroadcaster () const; |
| 136 | |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 137 | const char * |
| 138 | GetBroadcasterClass () const; |
| 139 | |
Johnny Chen | 6cf1bc3 | 2011-07-18 22:11:53 +0000 | [diff] [blame] | 140 | bool |
| 141 | BroadcasterMatchesRef (const lldb::SBBroadcaster &broadcaster); |
| 142 | |
| 143 | void |
| 144 | Clear(); |
| 145 | |
| 146 | static const char * |
| 147 | GetCStringFromEvent (const lldb::SBEvent &event); |
| 148 | |
| 149 | bool |
| 150 | GetDescription (lldb::SBStream &description) const; |
| 151 | }; |
| 152 | |
| 153 | } // namespace lldb |