1 STRING_EXTENSION_OUTSIDE(SBProcess)
2 %extend lldb::SBProcess {
3 #ifdef SWIGPYTHON
4     %pythoncode %{
5         def WriteMemoryAsCString(self, addr, str, error):
6             '''
7               WriteMemoryAsCString(self, addr, str, error):
8                 This functions the same as `WriteMemory` except a null-terminator is appended
9                 to the end of the buffer if it is not there already.
10             '''
11             if not str or len(str) == 0:
12                 return 0
13             if not str[-1] == '\0':
14                 str += '\0'
15             return self.WriteMemory(addr, str, error)
16 
17         def __get_is_alive__(self):
18             '''Returns "True" if the process is currently alive, "False" otherwise'''
19             s = self.GetState()
20             if (s == eStateAttaching or
21                 s == eStateLaunching or
22                 s == eStateStopped or
23                 s == eStateRunning or
24                 s == eStateStepping or
25                 s == eStateCrashed or
26                 s == eStateSuspended):
27                 return True
28             return False
29 
30         def __get_is_running__(self):
31             '''Returns "True" if the process is currently running, "False" otherwise'''
32             state = self.GetState()
33             if state == eStateRunning or state == eStateStepping:
34                 return True
35             return False
36 
37         def __get_is_stopped__(self):
38             '''Returns "True" if the process is currently stopped, "False" otherwise'''
39             state = self.GetState()
40             if state == eStateStopped or state == eStateCrashed or state == eStateSuspended:
41                 return True
42             return False
43 
44         class threads_access(object):
45             '''A helper object that will lazily hand out thread for a process when supplied an index.'''
46             def __init__(self, sbprocess):
47                 self.sbprocess = sbprocess
48 
49             def __len__(self):
50                 if self.sbprocess:
51                     return int(self.sbprocess.GetNumThreads())
52                 return 0
53 
54             def __getitem__(self, key):
55                 if isinstance(key, int):
56                     count = len(self)
57                     if -count <= key < count:
58                         key %= count
59                         return self.sbprocess.GetThreadAtIndex(key)
60                 return None
61 
62         def get_threads_access_object(self):
63             '''An accessor function that returns a modules_access() object which allows lazy thread access from a lldb.SBProcess object.'''
64             return self.threads_access (self)
65 
66         def get_process_thread_list(self):
67             '''An accessor function that returns a list() that contains all threads in a lldb.SBProcess object.'''
68             threads = []
69             accessor = self.get_threads_access_object()
70             for idx in range(len(accessor)):
71                 threads.append(accessor[idx])
72             return threads
73 
74         def __iter__(self):
75             '''Iterate over all threads in a lldb.SBProcess object.'''
76             return lldb_iter(self, 'GetNumThreads', 'GetThreadAtIndex')
77 
78         def __len__(self):
79             '''Return the number of threads in a lldb.SBProcess object.'''
80             return self.GetNumThreads()
81 
82 
83         threads = property(get_process_thread_list, None, doc='''A read only property that returns a list() of lldb.SBThread objects for this process.''')
84         thread = property(get_threads_access_object, None, doc='''A read only property that returns an object that can access threads by thread index (thread = lldb.process.thread[12]).''')
85         is_alive = property(__get_is_alive__, None, doc='''A read only property that returns a boolean value that indicates if this process is currently alive.''')
86         is_running = property(__get_is_running__, None, doc='''A read only property that returns a boolean value that indicates if this process is currently running.''')
87         is_stopped = property(__get_is_stopped__, None, doc='''A read only property that returns a boolean value that indicates if this process is currently stopped.''')
88         id = property(GetProcessID, None, doc='''A read only property that returns the process ID as an integer.''')
89         target = property(GetTarget, None, doc='''A read only property that an lldb object that represents the target (lldb.SBTarget) that owns this process.''')
90         num_threads = property(GetNumThreads, None, doc='''A read only property that returns the number of threads in this process as an integer.''')
91         selected_thread = property(GetSelectedThread, SetSelectedThread, doc='''A read/write property that gets/sets the currently selected thread in this process. The getter returns a lldb.SBThread object and the setter takes an lldb.SBThread object.''')
92         state = property(GetState, None, doc='''A read only property that returns an lldb enumeration value (see enumerations that start with "lldb.eState") that represents the current state of this process (running, stopped, exited, etc.).''')
93         exit_state = property(GetExitStatus, None, doc='''A read only property that returns an exit status as an integer of this process when the process state is lldb.eStateExited.''')
94         exit_description = property(GetExitDescription, None, doc='''A read only property that returns an exit description as a string of this process when the process state is lldb.eStateExited.''')
95         broadcaster = property(GetBroadcaster, None, doc='''A read only property that an lldb object that represents the broadcaster (lldb.SBBroadcaster) for this process.''')
96     %}
97 #endif
98 }
99