Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 1 | //===-- SWIG Interface for SBProcess ----------------------------*- 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 | %feature("docstring", |
| 13 | "Represents the process associated with the target program. |
| 14 | |
| 15 | SBProcess supports thread iteration. For example (from test/lldbutil.py), |
| 16 | |
| 17 | # ================================================== |
| 18 | # Utility functions related to Threads and Processes |
| 19 | # ================================================== |
| 20 | |
| 21 | def get_stopped_threads(process, reason): |
| 22 | '''Returns the thread(s) with the specified stop reason in a list. |
| 23 | |
| 24 | The list can be empty if no such thread exists. |
| 25 | ''' |
| 26 | threads = [] |
| 27 | for t in process: |
| 28 | if t.GetStopReason() == reason: |
| 29 | threads.append(t) |
| 30 | return threads |
| 31 | |
| 32 | ... |
| 33 | " |
| 34 | ) SBProcess; |
| 35 | class SBProcess |
| 36 | { |
| 37 | public: |
| 38 | //------------------------------------------------------------------ |
| 39 | /// Broadcaster event bits definitions. |
| 40 | //------------------------------------------------------------------ |
| 41 | enum |
| 42 | { |
| 43 | eBroadcastBitStateChanged = (1 << 0), |
| 44 | eBroadcastBitInterrupt = (1 << 1), |
| 45 | eBroadcastBitSTDOUT = (1 << 2), |
| 46 | eBroadcastBitSTDERR = (1 << 3) |
| 47 | }; |
| 48 | |
| 49 | SBProcess (); |
| 50 | |
| 51 | SBProcess (const lldb::SBProcess& rhs); |
| 52 | |
| 53 | ~SBProcess(); |
| 54 | |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 55 | static const char * |
| 56 | GetBroadcasterClassName (); |
| 57 | |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 58 | void |
| 59 | Clear (); |
| 60 | |
| 61 | bool |
| 62 | IsValid() const; |
| 63 | |
| 64 | lldb::SBTarget |
| 65 | GetTarget() const; |
| 66 | |
| 67 | lldb::ByteOrder |
| 68 | GetByteOrder() const; |
| 69 | |
Johnny Chen | 23038b6 | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 70 | %feature("autodoc", " |
| 71 | Writes data into the current process's stdin. API client specifies a Python |
| 72 | string as the only argument. |
| 73 | ") PutSTDIN; |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 74 | size_t |
| 75 | PutSTDIN (const char *src, size_t src_len); |
| 76 | |
Johnny Chen | 609b9ce | 2011-11-28 19:12:25 +0000 | [diff] [blame] | 77 | %feature("autodoc", " |
| 78 | Reads data from the current process's stdout stream. API client specifies |
| 79 | the size of the buffer to read data into. It returns the byte buffer in a |
| 80 | Python string. |
| 81 | ") GetSTDOUT; |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 82 | size_t |
| 83 | GetSTDOUT (char *dst, size_t dst_len) const; |
| 84 | |
Johnny Chen | 609b9ce | 2011-11-28 19:12:25 +0000 | [diff] [blame] | 85 | %feature("autodoc", " |
| 86 | Reads data from the current process's stderr stream. API client specifies |
| 87 | the size of the buffer to read data into. It returns the byte buffer in a |
| 88 | Python string. |
| 89 | ") GetSTDERR; |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 90 | size_t |
| 91 | GetSTDERR (char *dst, size_t dst_len) const; |
| 92 | |
| 93 | void |
| 94 | ReportEventState (const lldb::SBEvent &event, FILE *out) const; |
| 95 | |
| 96 | void |
| 97 | AppendEventStateReport (const lldb::SBEvent &event, lldb::SBCommandReturnObject &result); |
| 98 | |
| 99 | %feature("docstring", " |
| 100 | //------------------------------------------------------------------ |
| 101 | /// Remote connection related functions. These will fail if the |
| 102 | /// process is not in eStateConnected. They are intended for use |
| 103 | /// when connecting to an externally managed debugserver instance. |
| 104 | //------------------------------------------------------------------ |
| 105 | ") RemoteAttachToProcessWithID; |
| 106 | bool |
| 107 | RemoteAttachToProcessWithID (lldb::pid_t pid, |
| 108 | lldb::SBError& error); |
| 109 | |
| 110 | %feature("docstring", |
| 111 | "See SBTarget.Launch for argument description and usage." |
| 112 | ) RemoteLaunch; |
| 113 | bool |
| 114 | RemoteLaunch (char const **argv, |
| 115 | char const **envp, |
| 116 | const char *stdin_path, |
| 117 | const char *stdout_path, |
| 118 | const char *stderr_path, |
| 119 | const char *working_directory, |
| 120 | uint32_t launch_flags, |
| 121 | bool stop_at_entry, |
| 122 | lldb::SBError& error); |
| 123 | |
| 124 | //------------------------------------------------------------------ |
| 125 | // Thread related functions |
| 126 | //------------------------------------------------------------------ |
| 127 | uint32_t |
| 128 | GetNumThreads (); |
| 129 | |
| 130 | lldb::SBThread |
| 131 | GetThreadAtIndex (size_t index); |
| 132 | |
| 133 | lldb::SBThread |
| 134 | GetThreadByID (lldb::tid_t sb_thread_id); |
| 135 | |
| 136 | lldb::SBThread |
| 137 | GetSelectedThread () const; |
| 138 | |
| 139 | bool |
| 140 | SetSelectedThread (const lldb::SBThread &thread); |
| 141 | |
| 142 | bool |
| 143 | SetSelectedThreadByID (uint32_t tid); |
| 144 | |
| 145 | //------------------------------------------------------------------ |
| 146 | // Stepping related functions |
| 147 | //------------------------------------------------------------------ |
| 148 | |
| 149 | lldb::StateType |
| 150 | GetState (); |
| 151 | |
| 152 | int |
| 153 | GetExitStatus (); |
| 154 | |
| 155 | const char * |
| 156 | GetExitDescription (); |
| 157 | |
| 158 | lldb::pid_t |
| 159 | GetProcessID (); |
| 160 | |
| 161 | uint32_t |
| 162 | GetAddressByteSize() const; |
| 163 | |
| 164 | %feature("docstring", " |
| 165 | Kills the process and shuts down all threads that were spawned to |
| 166 | track and monitor process. |
| 167 | ") Destroy; |
| 168 | lldb::SBError |
| 169 | Destroy (); |
| 170 | |
| 171 | lldb::SBError |
| 172 | Continue (); |
| 173 | |
| 174 | lldb::SBError |
| 175 | Stop (); |
| 176 | |
| 177 | %feature("docstring", "Same as Destroy(self).") Destroy; |
| 178 | lldb::SBError |
| 179 | Kill (); |
| 180 | |
| 181 | lldb::SBError |
| 182 | Detach (); |
| 183 | |
| 184 | %feature("docstring", "Sends the process a unix signal.") Signal; |
| 185 | lldb::SBError |
| 186 | Signal (int signal); |
| 187 | |
| 188 | %feature("autodoc", " |
| 189 | Reads memory from the current process's address space and removes any |
| 190 | traps that may have been inserted into the memory. It returns the byte |
| 191 | buffer in a Python string. Example: |
| 192 | |
| 193 | # Read 4 bytes from address 'addr' and assume error.Success() is True. |
| 194 | content = process.ReadMemory(addr, 4, error) |
| 195 | # Use 'ascii' encoding as each byte of 'content' is within [0..255]. |
| 196 | new_bytes = bytearray(content, 'ascii') |
| 197 | ") ReadMemory; |
| 198 | size_t |
| 199 | ReadMemory (addr_t addr, void *buf, size_t size, lldb::SBError &error); |
| 200 | |
| 201 | %feature("autodoc", " |
| 202 | Writes memory to the current process's address space and maintains any |
| 203 | traps that might be present due to software breakpoints. Example: |
| 204 | |
| 205 | # Create a Python string from the byte array. |
| 206 | new_value = str(bytes) |
| 207 | result = process.WriteMemory(addr, new_value, error) |
| 208 | if not error.Success() or result != len(bytes): |
| 209 | print 'SBProcess.WriteMemory() failed!' |
| 210 | ") WriteMemory; |
| 211 | size_t |
| 212 | WriteMemory (addr_t addr, const void *buf, size_t size, lldb::SBError &error); |
| 213 | |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 214 | %feature("autodoc", " |
| 215 | Reads a NULL terminated C string from the current process's address space. |
| 216 | It returns a python string of the exact length, or truncates the string if |
| 217 | the maximum character limit is reached. Example: |
| 218 | |
| 219 | # Read a C string of at most 256 bytes from address '0x1000' |
| 220 | error = lldb.SBError() |
Johnny Chen | 0841386 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 221 | cstring = process.ReadCStringFromMemory(0x1000, 256, error) |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 222 | if error.Success(): |
| 223 | print 'cstring: ', cstring |
| 224 | else |
| 225 | print 'error: ', error |
| 226 | ") ReadCStringFromMemory; |
| 227 | |
| 228 | size_t |
| 229 | ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &error); |
| 230 | |
| 231 | %feature("autodoc", " |
| 232 | Reads an unsigned integer from memory given a byte size and an address. |
| 233 | Returns the unsigned integer that was read. Example: |
| 234 | |
| 235 | # Read a 4 byte unsigned integer from address 0x1000 |
| 236 | error = lldb.SBError() |
| 237 | uint = ReadUnsignedFromMemory(0x1000, 4, error) |
| 238 | if error.Success(): |
| 239 | print 'integer: %u' % uint |
| 240 | else |
| 241 | print 'error: ', error |
| 242 | |
| 243 | ") ReadUnsignedFromMemory; |
| 244 | |
| 245 | uint64_t |
| 246 | ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &error); |
| 247 | |
| 248 | %feature("autodoc", " |
| 249 | Reads a pointer from memory from an address and returns the value. Example: |
| 250 | |
| 251 | # Read a pointer from address 0x1000 |
| 252 | error = lldb.SBError() |
| 253 | ptr = ReadPointerFromMemory(0x1000, error) |
| 254 | if error.Success(): |
| 255 | print 'pointer: 0x%x' % ptr |
| 256 | else |
| 257 | print 'error: ', error |
| 258 | |
| 259 | ") ReadPointerFromMemory; |
| 260 | |
| 261 | lldb::addr_t |
| 262 | ReadPointerFromMemory (addr_t addr, lldb::SBError &error); |
| 263 | |
| 264 | |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 265 | // Events |
| 266 | static lldb::StateType |
| 267 | GetStateFromEvent (const lldb::SBEvent &event); |
| 268 | |
| 269 | static bool |
| 270 | GetRestartedFromEvent (const lldb::SBEvent &event); |
| 271 | |
| 272 | static lldb::SBProcess |
| 273 | GetProcessFromEvent (const lldb::SBEvent &event); |
| 274 | |
Jim Ingham | 28e2386 | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 275 | static bool |
| 276 | EventIsProcessEvent (const lldb::SBEvent &event); |
| 277 | |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 278 | lldb::SBBroadcaster |
| 279 | GetBroadcaster () const; |
| 280 | |
| 281 | bool |
| 282 | GetDescription (lldb::SBStream &description); |
| 283 | |
| 284 | uint32_t |
| 285 | LoadImage (lldb::SBFileSpec &image_spec, lldb::SBError &error); |
| 286 | |
| 287 | lldb::SBError |
| 288 | UnloadImage (uint32_t image_token); |
Johnny Chen | 9f074f0 | 2012-01-06 00:46:12 +0000 | [diff] [blame] | 289 | |
Greg Clayton | 1b92520 | 2012-01-29 06:07:39 +0000 | [diff] [blame] | 290 | %pythoncode %{ |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 291 | def __get_is_alive__(self): |
| 292 | '''Returns "True" if the process is currently alive, "False" otherwise''' |
| 293 | s = self.GetState() |
| 294 | if (s == eStateAttaching or |
| 295 | s == eStateLaunching or |
| 296 | s == eStateStopped or |
| 297 | s == eStateRunning or |
| 298 | s == eStateStepping or |
| 299 | s == eStateCrashed or |
| 300 | s == eStateSuspended): |
| 301 | return True |
| 302 | return False |
| 303 | |
| 304 | def __get_is_running__(self): |
| 305 | '''Returns "True" if the process is currently running, "False" otherwise''' |
| 306 | state = self.GetState() |
| 307 | if state == eStateRunning or state == eStateStepping: |
| 308 | return True |
| 309 | return False |
| 310 | |
| 311 | def __get_is_running__(self): |
| 312 | '''Returns "True" if the process is currently stopped, "False" otherwise''' |
| 313 | state = self.GetState() |
| 314 | if state == eStateStopped or state == eStateCrashed or state == eStateSuspended: |
| 315 | return True |
| 316 | return False |
| 317 | |
Greg Clayton | b6a5ba6 | 2012-02-03 03:22:53 +0000 | [diff] [blame] | 318 | class threads_access(object): |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 319 | '''A helper object that will lazily hand out thread for a process when supplied an index.''' |
| 320 | def __init__(self, sbprocess): |
| 321 | self.sbprocess = sbprocess |
| 322 | |
| 323 | def __len__(self): |
Filipe Cabecinhas | 3cae38b | 2012-05-11 20:39:42 +0000 | [diff] [blame] | 324 | if self.sbprocess: |
| 325 | return int(self.sbprocess.GetNumThreads()) |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 326 | return 0 |
| 327 | |
| 328 | def __getitem__(self, key): |
| 329 | if type(key) is int and key < len(self): |
| 330 | return self.sbprocess.GetThreadAtIndex(key) |
| 331 | return None |
| 332 | |
Greg Clayton | b6a5ba6 | 2012-02-03 03:22:53 +0000 | [diff] [blame] | 333 | def get_threads_access_object(self): |
| 334 | '''An accessor function that returns a modules_access() object which allows lazy thread access from a lldb.SBProcess object.''' |
| 335 | return self.threads_access (self) |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 336 | |
| 337 | def get_process_thread_list(self): |
Greg Clayton | b6a5ba6 | 2012-02-03 03:22:53 +0000 | [diff] [blame] | 338 | '''An accessor function that returns a list() that contains all threads in a lldb.SBProcess object.''' |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 339 | threads = [] |
| 340 | for idx in range(self.GetNumThreads()): |
| 341 | threads.append(GetThreadAtIndex(idx)) |
| 342 | return threads |
| 343 | |
| 344 | __swig_getmethods__["threads"] = get_process_thread_list |
| 345 | if _newclass: x = property(get_process_thread_list, None) |
| 346 | |
Greg Clayton | b6a5ba6 | 2012-02-03 03:22:53 +0000 | [diff] [blame] | 347 | __swig_getmethods__["thread"] = get_threads_access_object |
| 348 | if _newclass: x = property(get_threads_access_object, None) |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 349 | |
| 350 | __swig_getmethods__["is_alive"] = __get_is_alive__ |
| 351 | if _newclass: x = property(__get_is_alive__, None) |
| 352 | |
| 353 | __swig_getmethods__["is_running"] = __get_is_running__ |
| 354 | if _newclass: x = property(__get_is_running__, None) |
| 355 | |
| 356 | __swig_getmethods__["is_stopped"] = __get_is_running__ |
| 357 | if _newclass: x = property(__get_is_running__, None) |
| 358 | |
Greg Clayton | 1b92520 | 2012-01-29 06:07:39 +0000 | [diff] [blame] | 359 | __swig_getmethods__["id"] = GetProcessID |
| 360 | if _newclass: x = property(GetProcessID, None) |
| 361 | |
| 362 | __swig_getmethods__["target"] = GetTarget |
| 363 | if _newclass: x = property(GetTarget, None) |
| 364 | |
| 365 | __swig_getmethods__["num_threads"] = GetNumThreads |
| 366 | if _newclass: x = property(GetNumThreads, None) |
| 367 | |
| 368 | __swig_getmethods__["selected_thread"] = GetSelectedThread |
| 369 | __swig_setmethods__["selected_thread"] = SetSelectedThread |
| 370 | if _newclass: x = property(GetSelectedThread, SetSelectedThread) |
| 371 | |
| 372 | __swig_getmethods__["state"] = GetState |
| 373 | if _newclass: x = property(GetState, None) |
| 374 | |
| 375 | __swig_getmethods__["exit_state"] = GetExitStatus |
| 376 | if _newclass: x = property(GetExitStatus, None) |
| 377 | |
| 378 | __swig_getmethods__["exit_description"] = GetExitDescription |
| 379 | if _newclass: x = property(GetExitDescription, None) |
| 380 | |
| 381 | __swig_getmethods__["broadcaster"] = GetBroadcaster |
| 382 | if _newclass: x = property(GetBroadcaster, None) |
| 383 | %} |
| 384 | |
Johnny Chen | c3fba81 | 2011-07-18 20:13:38 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | } // namespace lldb |