xref: /freebsd/contrib/llvm-project/lldb/bindings/interface/SBTargetExtensions.i (revision 02e9120893770924227138ba49df1edb3896112a)
1 #ifdef SWIGPYTHON
2 %typemap(in) (const char **symbol_name, uint32_t num_names) {
3   using namespace lldb_private;
4   /* Check if is a list  */
5   if (PythonList::Check($input)) {
6     PythonList list(PyRefType::Borrowed, $input);
7     $2 = list.GetSize();
8     int i = 0;
9     $1 = (char**)malloc(($2+1)*sizeof(char*));
10     for (i = 0; i < $2; i++) {
11       PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
12       if (!py_str.IsAllocated()) {
13         PyErr_SetString(PyExc_TypeError,"list must contain strings and blubby");
14         free($1);
15         return nullptr;
16       }
17 
18       $1[i] = const_cast<char*>(py_str.GetString().data());
19     }
20     $1[i] = 0;
21   } else if ($input == Py_None) {
22     $1 =  NULL;
23   } else {
24     PyErr_SetString(PyExc_TypeError,"not a list");
25     return NULL;
26   }
27 }
28 #endif
29 
30 STRING_EXTENSION_LEVEL_OUTSIDE(SBTarget, lldb::eDescriptionLevelBrief)
31 
32 %extend lldb::SBTarget {
33 #ifdef SWIGPYTHON
34     %pythoncode %{
35         class modules_access(object):
36             '''A helper object that will lazily hand out lldb.SBModule objects for a target when supplied an index, or by full or partial path.'''
37             def __init__(self, sbtarget):
38                 self.sbtarget = sbtarget
39 
40             def __len__(self):
41                 if self.sbtarget:
42                     return int(self.sbtarget.GetNumModules())
43                 return 0
44 
45             def __getitem__(self, key):
46                 num_modules = self.sbtarget.GetNumModules()
47                 if type(key) is int:
48                     if -num_modules <= key < num_modules:
49                         key %= num_modules
50                         return self.sbtarget.GetModuleAtIndex(key)
51                 elif type(key) is str:
52                     if key.find('/') == -1:
53                         for idx in range(num_modules):
54                             module = self.sbtarget.GetModuleAtIndex(idx)
55                             if module.file.basename == key:
56                                 return module
57                     else:
58                         for idx in range(num_modules):
59                             module = self.sbtarget.GetModuleAtIndex(idx)
60                             if module.file.fullpath == key:
61                                 return module
62                     # See if the string is a UUID
63                     try:
64                         the_uuid = uuid.UUID(key)
65                         if the_uuid:
66                             for idx in range(num_modules):
67                                 module = self.sbtarget.GetModuleAtIndex(idx)
68                                 if module.uuid == the_uuid:
69                                     return module
70                     except:
71                         return None
72                 elif type(key) is uuid.UUID:
73                     for idx in range(num_modules):
74                         module = self.sbtarget.GetModuleAtIndex(idx)
75                         if module.uuid == key:
76                             return module
77                 elif type(key) is re.SRE_Pattern:
78                     matching_modules = []
79                     for idx in range(num_modules):
80                         module = self.sbtarget.GetModuleAtIndex(idx)
81                         re_match = key.search(module.path.fullpath)
82                         if re_match:
83                             matching_modules.append(module)
84                     return matching_modules
85                 else:
86                     print("error: unsupported item type: %s" % type(key))
87                 return None
88 
89         def get_modules_access_object(self):
90             '''An accessor function that returns a modules_access() object which allows lazy module access from a lldb.SBTarget object.'''
91             return self.modules_access(self)
92 
93         def get_modules_array(self):
94             '''An accessor function that returns a list() that contains all modules in a lldb.SBTarget object.'''
95             modules = []
96             for idx in range(self.GetNumModules()):
97                 modules.append(self.GetModuleAtIndex(idx))
98             return modules
99 
100         def module_iter(self):
101             '''Returns an iterator over all modules in a lldb.SBTarget
102             object.'''
103             return lldb_iter(self, 'GetNumModules', 'GetModuleAtIndex')
104 
105         def breakpoint_iter(self):
106             '''Returns an iterator over all breakpoints in a lldb.SBTarget
107             object.'''
108             return lldb_iter(self, 'GetNumBreakpoints', 'GetBreakpointAtIndex')
109 
110         class bkpts_access(object):
111             '''A helper object that will lazily hand out bkpts for a target when supplied an index.'''
112             def __init__(self, sbtarget):
113                 self.sbtarget = sbtarget
114 
115             def __len__(self):
116                 if self.sbtarget:
117                     return int(self.sbtarget.GetNumBreakpoints())
118                 return 0
119 
120             def __getitem__(self, key):
121                 if isinstance(key, int):
122                     count = len(self)
123                     if -count <= key < count:
124                         key %= count
125                         return self.sbtarget.GetBreakpointAtIndex(key)
126                 return None
127 
128         def get_bkpts_access_object(self):
129             '''An accessor function that returns a bkpts_access() object which allows lazy bkpt access from a lldb.SBtarget object.'''
130             return self.bkpts_access(self)
131 
132         def get_target_bkpts(self):
133             '''An accessor function that returns a list() that contains all bkpts in a lldb.SBtarget object.'''
134             bkpts = []
135             for idx in range(self.GetNumBreakpoints()):
136                 bkpts.append(self.GetBreakpointAtIndex(idx))
137             return bkpts
138 
139         def watchpoint_iter(self):
140             '''Returns an iterator over all watchpoints in a lldb.SBTarget
141             object.'''
142             return lldb_iter(self, 'GetNumWatchpoints', 'GetWatchpointAtIndex')
143 
144         class watchpoints_access(object):
145             '''A helper object that will lazily hand out watchpoints for a target when supplied an index.'''
146             def __init__(self, sbtarget):
147                 self.sbtarget = sbtarget
148 
149             def __len__(self):
150                 if self.sbtarget:
151                     return int(self.sbtarget.GetNumWatchpoints())
152                 return 0
153 
154             def __getitem__(self, key):
155                 if isinstance(key, int):
156                     count = len(self)
157                     if -count <= key < count:
158                         key %= count
159                         return self.sbtarget.GetWatchpointAtIndex(key)
160                 return None
161 
162         def get_watchpoints_access_object(self):
163             '''An accessor function that returns a watchpoints_access() object which allows lazy watchpoint access from a lldb.SBtarget object.'''
164             return self.watchpoints_access(self)
165 
166         def get_target_watchpoints(self):
167             '''An accessor function that returns a list() that contains all watchpoints in a lldb.SBtarget object.'''
168             watchpoints = []
169             for idx in range(self.GetNumWatchpoints()):
170                 bkpts.append(self.GetWatchpointAtIndex(idx))
171             return watchpoints
172 
173         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).''')
174         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.''')
175         process = property(GetProcess, None, doc='''A read only property that returns an lldb object that represents the process (lldb.SBProcess) that this target owns.''')
176         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.''')
177         debugger = property(GetDebugger, None, doc='''A read only property that returns an lldb object that represents the debugger (lldb.SBDebugger) that owns this target.''')
178         num_breakpoints = property(GetNumBreakpoints, None, doc='''A read only property that returns the number of breakpoints that this target has as an integer.''')
179         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.''')
180         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]").''')
181         num_watchpoints = property(GetNumWatchpoints, None, doc='''A read only property that returns the number of watchpoints that this target has as an integer.''')
182         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.''')
183         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]").''')
184         broadcaster = property(GetBroadcaster, None, doc='''A read only property that an lldb object that represents the broadcaster (lldb.SBBroadcaster) for this target.''')
185         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.''')
186         addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this target.''')
187         triple = property(GetTriple, None, doc='''A read only property that returns the target triple (arch-vendor-os) for this target as a string.''')
188         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.''')
189         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.''')
190         platform = property(GetPlatform, None, doc='''A read only property that returns the platform associated with with this target.''')
191     %}
192 #endif
193 }
194