xref: /freebsd/contrib/llvm-project/lldb/bindings/interface/SBProcessExtensions.i (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1*06c3fb27SDimitry Andric STRING_EXTENSION_OUTSIDE(SBProcess)
2*06c3fb27SDimitry Andric %extend lldb::SBProcess {
3*06c3fb27SDimitry Andric #ifdef SWIGPYTHON
4*06c3fb27SDimitry Andric     %pythoncode %{
5*06c3fb27SDimitry Andric         def WriteMemoryAsCString(self, addr, str, error):
6*06c3fb27SDimitry Andric             '''
7*06c3fb27SDimitry Andric               WriteMemoryAsCString(self, addr, str, error):
8*06c3fb27SDimitry Andric                 This functions the same as `WriteMemory` except a null-terminator is appended
9*06c3fb27SDimitry Andric                 to the end of the buffer if it is not there already.
10*06c3fb27SDimitry Andric             '''
11*06c3fb27SDimitry Andric             if not str or len(str) == 0:
12*06c3fb27SDimitry Andric                 return 0
13*06c3fb27SDimitry Andric             if not str[-1] == '\0':
14*06c3fb27SDimitry Andric                 str += '\0'
15*06c3fb27SDimitry Andric             return self.WriteMemory(addr, str, error)
16*06c3fb27SDimitry Andric 
17*06c3fb27SDimitry Andric         def __get_is_alive__(self):
18*06c3fb27SDimitry Andric             '''Returns "True" if the process is currently alive, "False" otherwise'''
19*06c3fb27SDimitry Andric             s = self.GetState()
20*06c3fb27SDimitry Andric             if (s == eStateAttaching or
21*06c3fb27SDimitry Andric                 s == eStateLaunching or
22*06c3fb27SDimitry Andric                 s == eStateStopped or
23*06c3fb27SDimitry Andric                 s == eStateRunning or
24*06c3fb27SDimitry Andric                 s == eStateStepping or
25*06c3fb27SDimitry Andric                 s == eStateCrashed or
26*06c3fb27SDimitry Andric                 s == eStateSuspended):
27*06c3fb27SDimitry Andric                 return True
28*06c3fb27SDimitry Andric             return False
29*06c3fb27SDimitry Andric 
30*06c3fb27SDimitry Andric         def __get_is_running__(self):
31*06c3fb27SDimitry Andric             '''Returns "True" if the process is currently running, "False" otherwise'''
32*06c3fb27SDimitry Andric             state = self.GetState()
33*06c3fb27SDimitry Andric             if state == eStateRunning or state == eStateStepping:
34*06c3fb27SDimitry Andric                 return True
35*06c3fb27SDimitry Andric             return False
36*06c3fb27SDimitry Andric 
37*06c3fb27SDimitry Andric         def __get_is_stopped__(self):
38*06c3fb27SDimitry Andric             '''Returns "True" if the process is currently stopped, "False" otherwise'''
39*06c3fb27SDimitry Andric             state = self.GetState()
40*06c3fb27SDimitry Andric             if state == eStateStopped or state == eStateCrashed or state == eStateSuspended:
41*06c3fb27SDimitry Andric                 return True
42*06c3fb27SDimitry Andric             return False
43*06c3fb27SDimitry Andric 
44*06c3fb27SDimitry Andric         class threads_access(object):
45*06c3fb27SDimitry Andric             '''A helper object that will lazily hand out thread for a process when supplied an index.'''
46*06c3fb27SDimitry Andric             def __init__(self, sbprocess):
47*06c3fb27SDimitry Andric                 self.sbprocess = sbprocess
48*06c3fb27SDimitry Andric 
49*06c3fb27SDimitry Andric             def __len__(self):
50*06c3fb27SDimitry Andric                 if self.sbprocess:
51*06c3fb27SDimitry Andric                     return int(self.sbprocess.GetNumThreads())
52*06c3fb27SDimitry Andric                 return 0
53*06c3fb27SDimitry Andric 
54*06c3fb27SDimitry Andric             def __getitem__(self, key):
55*06c3fb27SDimitry Andric                 if isinstance(key, int):
56*06c3fb27SDimitry Andric                     count = len(self)
57*06c3fb27SDimitry Andric                     if -count <= key < count:
58*06c3fb27SDimitry Andric                         key %= count
59*06c3fb27SDimitry Andric                         return self.sbprocess.GetThreadAtIndex(key)
60*06c3fb27SDimitry Andric                 return None
61*06c3fb27SDimitry Andric 
62*06c3fb27SDimitry Andric         def get_threads_access_object(self):
63*06c3fb27SDimitry Andric             '''An accessor function that returns a modules_access() object which allows lazy thread access from a lldb.SBProcess object.'''
64*06c3fb27SDimitry Andric             return self.threads_access (self)
65*06c3fb27SDimitry Andric 
66*06c3fb27SDimitry Andric         def get_process_thread_list(self):
67*06c3fb27SDimitry Andric             '''An accessor function that returns a list() that contains all threads in a lldb.SBProcess object.'''
68*06c3fb27SDimitry Andric             threads = []
69*06c3fb27SDimitry Andric             accessor = self.get_threads_access_object()
70*06c3fb27SDimitry Andric             for idx in range(len(accessor)):
71*06c3fb27SDimitry Andric                 threads.append(accessor[idx])
72*06c3fb27SDimitry Andric             return threads
73*06c3fb27SDimitry Andric 
74*06c3fb27SDimitry Andric         def __iter__(self):
75*06c3fb27SDimitry Andric             '''Iterate over all threads in a lldb.SBProcess object.'''
76*06c3fb27SDimitry Andric             return lldb_iter(self, 'GetNumThreads', 'GetThreadAtIndex')
77*06c3fb27SDimitry Andric 
78*06c3fb27SDimitry Andric         def __len__(self):
79*06c3fb27SDimitry Andric             '''Return the number of threads in a lldb.SBProcess object.'''
80*06c3fb27SDimitry Andric             return self.GetNumThreads()
81*06c3fb27SDimitry Andric 
82*06c3fb27SDimitry Andric 
83*06c3fb27SDimitry Andric         threads = property(get_process_thread_list, None, doc='''A read only property that returns a list() of lldb.SBThread objects for this process.''')
84*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         id = property(GetProcessID, None, doc='''A read only property that returns the process ID as an integer.''')
89*06c3fb27SDimitry Andric         target = property(GetTarget, None, doc='''A read only property that an lldb object that represents the target (lldb.SBTarget) that owns this process.''')
90*06c3fb27SDimitry Andric         num_threads = property(GetNumThreads, None, doc='''A read only property that returns the number of threads in this process as an integer.''')
91*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         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*06c3fb27SDimitry Andric         broadcaster = property(GetBroadcaster, None, doc='''A read only property that an lldb object that represents the broadcaster (lldb.SBBroadcaster) for this process.''')
96*06c3fb27SDimitry Andric     %}
97*06c3fb27SDimitry Andric #endif
98*06c3fb27SDimitry Andric }
99