xref: /llvm-project/lldb/bindings/interface/SBTargetExtensions.i (revision 170e1fe5a5211420923e32995d8bf3da196c2a54)
1662548c8SAlex Langford #ifdef SWIGPYTHON
2662548c8SAlex Langford %typemap(in) (const char **symbol_name, uint32_t num_names) {
3662548c8SAlex Langford   using namespace lldb_private;
4662548c8SAlex Langford   /* Check if is a list  */
5662548c8SAlex Langford   if (PythonList::Check($input)) {
6662548c8SAlex Langford     PythonList list(PyRefType::Borrowed, $input);
7662548c8SAlex Langford     $2 = list.GetSize();
8662548c8SAlex Langford     int i = 0;
9662548c8SAlex Langford     $1 = (char**)malloc(($2+1)*sizeof(char*));
10662548c8SAlex Langford     for (i = 0; i < $2; i++) {
11662548c8SAlex Langford       PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
12662548c8SAlex Langford       if (!py_str.IsAllocated()) {
13662548c8SAlex Langford         PyErr_SetString(PyExc_TypeError,"list must contain strings and blubby");
14662548c8SAlex Langford         free($1);
15662548c8SAlex Langford         return nullptr;
16662548c8SAlex Langford       }
17662548c8SAlex Langford 
18662548c8SAlex Langford       $1[i] = const_cast<char*>(py_str.GetString().data());
19662548c8SAlex Langford     }
20662548c8SAlex Langford     $1[i] = 0;
21662548c8SAlex Langford   } else if ($input == Py_None) {
22662548c8SAlex Langford     $1 =  NULL;
23662548c8SAlex Langford   } else {
24662548c8SAlex Langford     PyErr_SetString(PyExc_TypeError,"not a list");
25662548c8SAlex Langford     return NULL;
26662548c8SAlex Langford   }
27662548c8SAlex Langford }
28662548c8SAlex Langford #endif
29662548c8SAlex Langford 
30662548c8SAlex Langford STRING_EXTENSION_LEVEL_OUTSIDE(SBTarget, lldb::eDescriptionLevelBrief)
31662548c8SAlex Langford 
32662548c8SAlex Langford %extend lldb::SBTarget {
33662548c8SAlex Langford #ifdef SWIGPYTHON
34662548c8SAlex Langford     %pythoncode %{
356813ef37SMed Ismail Bennani         # operator== is a free function, which swig does not handle, so we inject
366813ef37SMed Ismail Bennani         # our own equality operator here
376813ef37SMed Ismail Bennani         def __eq__(self, other):
386813ef37SMed Ismail Bennani             return not self.__ne__(other)
396813ef37SMed Ismail Bennani 
40662548c8SAlex Langford         class modules_access(object):
41662548c8SAlex Langford             '''A helper object that will lazily hand out lldb.SBModule objects for a target when supplied an index, or by full or partial path.'''
42662548c8SAlex Langford             def __init__(self, sbtarget):
43662548c8SAlex Langford                 self.sbtarget = sbtarget
44662548c8SAlex Langford 
45662548c8SAlex Langford             def __len__(self):
46662548c8SAlex Langford                 if self.sbtarget:
47662548c8SAlex Langford                     return int(self.sbtarget.GetNumModules())
48662548c8SAlex Langford                 return 0
49662548c8SAlex Langford 
50662548c8SAlex Langford             def __getitem__(self, key):
51662548c8SAlex Langford                 num_modules = self.sbtarget.GetNumModules()
52662548c8SAlex Langford                 if type(key) is int:
53662548c8SAlex Langford                     if -num_modules <= key < num_modules:
54662548c8SAlex Langford                         key %= num_modules
55662548c8SAlex Langford                         return self.sbtarget.GetModuleAtIndex(key)
56662548c8SAlex Langford                 elif type(key) is str:
57662548c8SAlex Langford                     if key.find('/') == -1:
58662548c8SAlex Langford                         for idx in range(num_modules):
59662548c8SAlex Langford                             module = self.sbtarget.GetModuleAtIndex(idx)
60662548c8SAlex Langford                             if module.file.basename == key:
61662548c8SAlex Langford                                 return module
62662548c8SAlex Langford                     else:
63662548c8SAlex Langford                         for idx in range(num_modules):
64662548c8SAlex Langford                             module = self.sbtarget.GetModuleAtIndex(idx)
65662548c8SAlex Langford                             if module.file.fullpath == key:
66662548c8SAlex Langford                                 return module
67662548c8SAlex Langford                     # See if the string is a UUID
68662548c8SAlex Langford                     try:
69662548c8SAlex Langford                         the_uuid = uuid.UUID(key)
70662548c8SAlex Langford                         if the_uuid:
71662548c8SAlex Langford                             for idx in range(num_modules):
72662548c8SAlex Langford                                 module = self.sbtarget.GetModuleAtIndex(idx)
73662548c8SAlex Langford                                 if module.uuid == the_uuid:
74662548c8SAlex Langford                                     return module
75662548c8SAlex Langford                     except:
76662548c8SAlex Langford                         return None
77662548c8SAlex Langford                 elif type(key) is uuid.UUID:
78662548c8SAlex Langford                     for idx in range(num_modules):
79662548c8SAlex Langford                         module = self.sbtarget.GetModuleAtIndex(idx)
80662548c8SAlex Langford                         if module.uuid == key:
81662548c8SAlex Langford                             return module
82*170e1fe5SDave Lee                 elif isinstance(key, type(re.compile(''))):
83662548c8SAlex Langford                     matching_modules = []
84662548c8SAlex Langford                     for idx in range(num_modules):
85662548c8SAlex Langford                         module = self.sbtarget.GetModuleAtIndex(idx)
86*170e1fe5SDave Lee                         re_match = key.search(module.file.fullpath)
87662548c8SAlex Langford                         if re_match:
88662548c8SAlex Langford                             matching_modules.append(module)
89662548c8SAlex Langford                     return matching_modules
90662548c8SAlex Langford                 else:
91662548c8SAlex Langford                     print("error: unsupported item type: %s" % type(key))
92662548c8SAlex Langford                 return None
93662548c8SAlex Langford 
94662548c8SAlex Langford         def get_modules_access_object(self):
95662548c8SAlex Langford             '''An accessor function that returns a modules_access() object which allows lazy module access from a lldb.SBTarget object.'''
96662548c8SAlex Langford             return self.modules_access(self)
97662548c8SAlex Langford 
98662548c8SAlex Langford         def get_modules_array(self):
99662548c8SAlex Langford             '''An accessor function that returns a list() that contains all modules in a lldb.SBTarget object.'''
100662548c8SAlex Langford             modules = []
101662548c8SAlex Langford             for idx in range(self.GetNumModules()):
102662548c8SAlex Langford                 modules.append(self.GetModuleAtIndex(idx))
103662548c8SAlex Langford             return modules
104662548c8SAlex Langford 
105662548c8SAlex Langford         def module_iter(self):
106662548c8SAlex Langford             '''Returns an iterator over all modules in a lldb.SBTarget
107662548c8SAlex Langford             object.'''
108662548c8SAlex Langford             return lldb_iter(self, 'GetNumModules', 'GetModuleAtIndex')
109662548c8SAlex Langford 
110662548c8SAlex Langford         def breakpoint_iter(self):
111662548c8SAlex Langford             '''Returns an iterator over all breakpoints in a lldb.SBTarget
112662548c8SAlex Langford             object.'''
113662548c8SAlex Langford             return lldb_iter(self, 'GetNumBreakpoints', 'GetBreakpointAtIndex')
114662548c8SAlex Langford 
115e31d0c20SMed Ismail Bennani         class bkpts_access(object):
116e31d0c20SMed Ismail Bennani             '''A helper object that will lazily hand out bkpts for a target when supplied an index.'''
117e31d0c20SMed Ismail Bennani             def __init__(self, sbtarget):
118e31d0c20SMed Ismail Bennani                 self.sbtarget = sbtarget
119e31d0c20SMed Ismail Bennani 
120e31d0c20SMed Ismail Bennani             def __len__(self):
121e31d0c20SMed Ismail Bennani                 if self.sbtarget:
122e31d0c20SMed Ismail Bennani                     return int(self.sbtarget.GetNumBreakpoints())
123e31d0c20SMed Ismail Bennani                 return 0
124e31d0c20SMed Ismail Bennani 
125e31d0c20SMed Ismail Bennani             def __getitem__(self, key):
126e31d0c20SMed Ismail Bennani                 if isinstance(key, int):
127e31d0c20SMed Ismail Bennani                     count = len(self)
128e31d0c20SMed Ismail Bennani                     if -count <= key < count:
129e31d0c20SMed Ismail Bennani                         key %= count
130e31d0c20SMed Ismail Bennani                         return self.sbtarget.GetBreakpointAtIndex(key)
131e31d0c20SMed Ismail Bennani                 return None
132e31d0c20SMed Ismail Bennani 
133e31d0c20SMed Ismail Bennani         def get_bkpts_access_object(self):
134e31d0c20SMed Ismail Bennani             '''An accessor function that returns a bkpts_access() object which allows lazy bkpt access from a lldb.SBtarget object.'''
135e31d0c20SMed Ismail Bennani             return self.bkpts_access(self)
136e31d0c20SMed Ismail Bennani 
137e31d0c20SMed Ismail Bennani         def get_target_bkpts(self):
138e31d0c20SMed Ismail Bennani             '''An accessor function that returns a list() that contains all bkpts in a lldb.SBtarget object.'''
139e31d0c20SMed Ismail Bennani             bkpts = []
140e31d0c20SMed Ismail Bennani             for idx in range(self.GetNumBreakpoints()):
141e31d0c20SMed Ismail Bennani                 bkpts.append(self.GetBreakpointAtIndex(idx))
142e31d0c20SMed Ismail Bennani             return bkpts
143e31d0c20SMed Ismail Bennani 
144662548c8SAlex Langford         def watchpoint_iter(self):
145662548c8SAlex Langford             '''Returns an iterator over all watchpoints in a lldb.SBTarget
146662548c8SAlex Langford             object.'''
147662548c8SAlex Langford             return lldb_iter(self, 'GetNumWatchpoints', 'GetWatchpointAtIndex')
148662548c8SAlex Langford 
149e31d0c20SMed Ismail Bennani         class watchpoints_access(object):
150e31d0c20SMed Ismail Bennani             '''A helper object that will lazily hand out watchpoints for a target when supplied an index.'''
151e31d0c20SMed Ismail Bennani             def __init__(self, sbtarget):
152e31d0c20SMed Ismail Bennani                 self.sbtarget = sbtarget
153e31d0c20SMed Ismail Bennani 
154e31d0c20SMed Ismail Bennani             def __len__(self):
155e31d0c20SMed Ismail Bennani                 if self.sbtarget:
156e31d0c20SMed Ismail Bennani                     return int(self.sbtarget.GetNumWatchpoints())
157e31d0c20SMed Ismail Bennani                 return 0
158e31d0c20SMed Ismail Bennani 
159e31d0c20SMed Ismail Bennani             def __getitem__(self, key):
160e31d0c20SMed Ismail Bennani                 if isinstance(key, int):
161e31d0c20SMed Ismail Bennani                     count = len(self)
162e31d0c20SMed Ismail Bennani                     if -count <= key < count:
163e31d0c20SMed Ismail Bennani                         key %= count
164e31d0c20SMed Ismail Bennani                         return self.sbtarget.GetWatchpointAtIndex(key)
165e31d0c20SMed Ismail Bennani                 return None
166e31d0c20SMed Ismail Bennani 
167e31d0c20SMed Ismail Bennani         def get_watchpoints_access_object(self):
168e31d0c20SMed Ismail Bennani             '''An accessor function that returns a watchpoints_access() object which allows lazy watchpoint access from a lldb.SBtarget object.'''
169e31d0c20SMed Ismail Bennani             return self.watchpoints_access(self)
170e31d0c20SMed Ismail Bennani 
171e31d0c20SMed Ismail Bennani         def get_target_watchpoints(self):
172e31d0c20SMed Ismail Bennani             '''An accessor function that returns a list() that contains all watchpoints in a lldb.SBtarget object.'''
173e31d0c20SMed Ismail Bennani             watchpoints = []
174e31d0c20SMed Ismail Bennani             for idx in range(self.GetNumWatchpoints()):
175dc5dfc10Snikitalita                 watchpoints.append(self.GetWatchpointAtIndex(idx))
176e31d0c20SMed Ismail Bennani             return watchpoints
177e31d0c20SMed Ismail Bennani 
178662548c8SAlex Langford         modules = property(get_modules_array, None, doc='''A read only property that returns a list() of lldb.SBModule objects contained in this target. This list is a list all modules that the target currently is tracking (the main executable and all dependent shared libraries).''')
179662548c8SAlex Langford         module = property(get_modules_access_object, None, doc=r'''A read only property that returns an object that implements python operator overloading with the square brackets().\n    target.module[<int>] allows array access to any modules.\n    target.module[<str>] allows access to modules by basename, full path, or uuid string value.\n    target.module[uuid.UUID()] allows module access by UUID.\n    target.module[re] allows module access using a regular expression that matches the module full path.''')
180662548c8SAlex Langford         process = property(GetProcess, None, doc='''A read only property that returns an lldb object that represents the process (lldb.SBProcess) that this target owns.''')
181662548c8SAlex Langford         executable = property(GetExecutable, None, doc='''A read only property that returns an lldb object that represents the main executable module (lldb.SBModule) for this target.''')
182662548c8SAlex Langford         debugger = property(GetDebugger, None, doc='''A read only property that returns an lldb object that represents the debugger (lldb.SBDebugger) that owns this target.''')
183662548c8SAlex Langford         num_breakpoints = property(GetNumBreakpoints, None, doc='''A read only property that returns the number of breakpoints that this target has as an integer.''')
184e31d0c20SMed Ismail Bennani         breakpoints = property(get_target_bkpts, None, doc='''A read only property that returns a list() of lldb.SBBreakpoint objects for all breakpoints in this target.''')
185e31d0c20SMed Ismail Bennani         breakpoint = property(get_bkpts_access_object, None, doc='''A read only property that returns an object that can be used to access breakpoints as an array ("bkpt_12 = lldb.target.bkpt[12]").''')
186662548c8SAlex Langford         num_watchpoints = property(GetNumWatchpoints, None, doc='''A read only property that returns the number of watchpoints that this target has as an integer.''')
187e31d0c20SMed Ismail Bennani         watchpoints = property(get_target_watchpoints, None, doc='''A read only property that returns a list() of lldb.SBwatchpoint objects for all watchpoints in this target.''')
188e31d0c20SMed Ismail Bennani         watchpoint = property(get_watchpoints_access_object, None, doc='''A read only property that returns an object that can be used to access watchpoints as an array ("watchpoint_12 = lldb.target.watchpoint[12]").''')
189662548c8SAlex Langford         broadcaster = property(GetBroadcaster, None, doc='''A read only property that an lldb object that represents the broadcaster (lldb.SBBroadcaster) for this target.''')
190662548c8SAlex Langford         byte_order = property(GetByteOrder, None, doc='''A read only property that returns an lldb enumeration value (lldb.eByteOrderLittle, lldb.eByteOrderBig, lldb.eByteOrderInvalid) that represents the byte order for this target.''')
191662548c8SAlex Langford         addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this target.''')
192662548c8SAlex Langford         triple = property(GetTriple, None, doc='''A read only property that returns the target triple (arch-vendor-os) for this target as a string.''')
193662548c8SAlex Langford         data_byte_size = property(GetDataByteSize, None, doc='''A read only property that returns the size in host bytes of a byte in the data address space for this target.''')
194662548c8SAlex Langford         code_byte_size = property(GetCodeByteSize, None, doc='''A read only property that returns the size in host bytes of a byte in the code address space for this target.''')
195662548c8SAlex Langford         platform = property(GetPlatform, None, doc='''A read only property that returns the platform associated with with this target.''')
196662548c8SAlex Langford     %}
197662548c8SAlex Langford #endif
198662548c8SAlex Langford }
199