xref: /freebsd-src/contrib/llvm-project/lldb/bindings/interface/SBModuleExtensions.i (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
106c3fb27SDimitry Andric #ifdef SWIGPYTHON
206c3fb27SDimitry Andric %pythoncode%{
306c3fb27SDimitry Andric # ==================================
406c3fb27SDimitry Andric # Helper function for SBModule class
506c3fb27SDimitry Andric # ==================================
606c3fb27SDimitry Andric def in_range(symbol, section):
706c3fb27SDimitry Andric     """Test whether a symbol is within the range of a section."""
806c3fb27SDimitry Andric     symSA = symbol.GetStartAddress().GetFileAddress()
906c3fb27SDimitry Andric     symEA = symbol.GetEndAddress().GetFileAddress()
1006c3fb27SDimitry Andric     secSA = section.GetFileAddress()
1106c3fb27SDimitry Andric     secEA = secSA + section.GetByteSize()
1206c3fb27SDimitry Andric 
1306c3fb27SDimitry Andric     if symEA != LLDB_INVALID_ADDRESS:
1406c3fb27SDimitry Andric         if secSA <= symSA and symEA <= secEA:
1506c3fb27SDimitry Andric             return True
1606c3fb27SDimitry Andric         else:
1706c3fb27SDimitry Andric             return False
1806c3fb27SDimitry Andric     else:
1906c3fb27SDimitry Andric         if secSA <= symSA and symSA < secEA:
2006c3fb27SDimitry Andric             return True
2106c3fb27SDimitry Andric         else:
2206c3fb27SDimitry Andric             return False
2306c3fb27SDimitry Andric %}
2406c3fb27SDimitry Andric #endif
2506c3fb27SDimitry Andric 
2606c3fb27SDimitry Andric STRING_EXTENSION_OUTSIDE(SBModule)
2706c3fb27SDimitry Andric 
2806c3fb27SDimitry Andric %extend lldb::SBModule {
2906c3fb27SDimitry Andric #ifdef SWIGPYTHON
3006c3fb27SDimitry Andric     %pythoncode %{
31*5f757f3fSDimitry Andric         # operator== is a free function, which swig does not handle, so we inject
32*5f757f3fSDimitry Andric         # our own equality operator here
33*5f757f3fSDimitry Andric         def __eq__(self, other):
34*5f757f3fSDimitry Andric             return not self.__ne__(other)
35*5f757f3fSDimitry Andric 
3606c3fb27SDimitry Andric         def __len__(self):
3706c3fb27SDimitry Andric             '''Return the number of symbols in a lldb.SBModule object.'''
3806c3fb27SDimitry Andric             return self.GetNumSymbols()
3906c3fb27SDimitry Andric 
4006c3fb27SDimitry Andric         def __iter__(self):
4106c3fb27SDimitry Andric             '''Iterate over all symbols in a lldb.SBModule object.'''
4206c3fb27SDimitry Andric             return lldb_iter(self, 'GetNumSymbols', 'GetSymbolAtIndex')
4306c3fb27SDimitry Andric 
4406c3fb27SDimitry Andric         def section_iter(self):
4506c3fb27SDimitry Andric             '''Iterate over all sections in a lldb.SBModule object.'''
4606c3fb27SDimitry Andric             return lldb_iter(self, 'GetNumSections', 'GetSectionAtIndex')
4706c3fb27SDimitry Andric 
4806c3fb27SDimitry Andric         def compile_unit_iter(self):
4906c3fb27SDimitry Andric             '''Iterate over all compile units in a lldb.SBModule object.'''
5006c3fb27SDimitry Andric             return lldb_iter(self, 'GetNumCompileUnits', 'GetCompileUnitAtIndex')
5106c3fb27SDimitry Andric 
5206c3fb27SDimitry Andric         def symbol_in_section_iter(self, section):
5306c3fb27SDimitry Andric             '''Given a module and its contained section, returns an iterator on the
5406c3fb27SDimitry Andric             symbols within the section.'''
5506c3fb27SDimitry Andric             for sym in self:
5606c3fb27SDimitry Andric                 if in_range(sym, section):
5706c3fb27SDimitry Andric                     yield sym
5806c3fb27SDimitry Andric 
5906c3fb27SDimitry Andric         class symbols_access(object):
6006c3fb27SDimitry Andric             re_compile_type = type(re.compile('.'))
6106c3fb27SDimitry Andric             '''A helper object that will lazily hand out lldb.SBSymbol objects for a module when supplied an index, name, or regular expression.'''
6206c3fb27SDimitry Andric             def __init__(self, sbmodule):
6306c3fb27SDimitry Andric                 self.sbmodule = sbmodule
6406c3fb27SDimitry Andric 
6506c3fb27SDimitry Andric             def __len__(self):
6606c3fb27SDimitry Andric                 if self.sbmodule:
6706c3fb27SDimitry Andric                     return int(self.sbmodule.GetNumSymbols())
6806c3fb27SDimitry Andric                 return 0
6906c3fb27SDimitry Andric 
7006c3fb27SDimitry Andric             def __getitem__(self, key):
7106c3fb27SDimitry Andric                 count = len(self)
7206c3fb27SDimitry Andric                 if type(key) is int:
7306c3fb27SDimitry Andric                     if -count <= key < count:
7406c3fb27SDimitry Andric                         key %= count
7506c3fb27SDimitry Andric                         return self.sbmodule.GetSymbolAtIndex(key)
7606c3fb27SDimitry Andric                 elif type(key) is str:
7706c3fb27SDimitry Andric                     matches = []
7806c3fb27SDimitry Andric                     sc_list = self.sbmodule.FindSymbols(key)
7906c3fb27SDimitry Andric                     for sc in sc_list:
8006c3fb27SDimitry Andric                         symbol = sc.symbol
8106c3fb27SDimitry Andric                         if symbol:
8206c3fb27SDimitry Andric                             matches.append(symbol)
8306c3fb27SDimitry Andric                     return matches
8406c3fb27SDimitry Andric                 elif isinstance(key, self.re_compile_type):
8506c3fb27SDimitry Andric                     matches = []
8606c3fb27SDimitry Andric                     for idx in range(count):
8706c3fb27SDimitry Andric                         symbol = self.sbmodule.GetSymbolAtIndex(idx)
8806c3fb27SDimitry Andric                         added = False
8906c3fb27SDimitry Andric                         name = symbol.name
9006c3fb27SDimitry Andric                         if name:
9106c3fb27SDimitry Andric                             re_match = key.search(name)
9206c3fb27SDimitry Andric                             if re_match:
9306c3fb27SDimitry Andric                                 matches.append(symbol)
9406c3fb27SDimitry Andric                                 added = True
9506c3fb27SDimitry Andric                         if not added:
9606c3fb27SDimitry Andric                             mangled = symbol.mangled
9706c3fb27SDimitry Andric                             if mangled:
9806c3fb27SDimitry Andric                                 re_match = key.search(mangled)
9906c3fb27SDimitry Andric                                 if re_match:
10006c3fb27SDimitry Andric                                     matches.append(symbol)
10106c3fb27SDimitry Andric                     return matches
10206c3fb27SDimitry Andric                 else:
10306c3fb27SDimitry Andric                     print("error: unsupported item type: %s" % type(key))
10406c3fb27SDimitry Andric                 return None
10506c3fb27SDimitry Andric 
10606c3fb27SDimitry Andric         def get_symbols_access_object(self):
10706c3fb27SDimitry Andric             '''An accessor function that returns a symbols_access() object which allows lazy symbol access from a lldb.SBModule object.'''
10806c3fb27SDimitry Andric             return self.symbols_access (self)
10906c3fb27SDimitry Andric 
11006c3fb27SDimitry Andric         def get_compile_units_access_object (self):
11106c3fb27SDimitry Andric             '''An accessor function that returns a compile_units_access() object which allows lazy compile unit access from a lldb.SBModule object.'''
11206c3fb27SDimitry Andric             return self.compile_units_access (self)
11306c3fb27SDimitry Andric 
11406c3fb27SDimitry Andric         def get_symbols_array(self):
11506c3fb27SDimitry Andric             '''An accessor function that returns a list() that contains all symbols in a lldb.SBModule object.'''
11606c3fb27SDimitry Andric             symbols = []
11706c3fb27SDimitry Andric             for idx in range(self.num_symbols):
11806c3fb27SDimitry Andric                 symbols.append(self.GetSymbolAtIndex(idx))
11906c3fb27SDimitry Andric             return symbols
12006c3fb27SDimitry Andric 
12106c3fb27SDimitry Andric         class sections_access(object):
12206c3fb27SDimitry Andric             re_compile_type = type(re.compile('.'))
12306c3fb27SDimitry Andric             '''A helper object that will lazily hand out lldb.SBSection objects for a module when supplied an index, name, or regular expression.'''
12406c3fb27SDimitry Andric             def __init__(self, sbmodule):
12506c3fb27SDimitry Andric                 self.sbmodule = sbmodule
12606c3fb27SDimitry Andric 
12706c3fb27SDimitry Andric             def __len__(self):
12806c3fb27SDimitry Andric                 if self.sbmodule:
12906c3fb27SDimitry Andric                     return int(self.sbmodule.GetNumSections())
13006c3fb27SDimitry Andric                 return 0
13106c3fb27SDimitry Andric 
13206c3fb27SDimitry Andric             def __getitem__(self, key):
13306c3fb27SDimitry Andric                 count = len(self)
13406c3fb27SDimitry Andric                 if type(key) is int:
13506c3fb27SDimitry Andric                     if -count <= key < count:
13606c3fb27SDimitry Andric                         key %= count
13706c3fb27SDimitry Andric                         return self.sbmodule.GetSectionAtIndex(key)
13806c3fb27SDimitry Andric                 elif type(key) is str:
13906c3fb27SDimitry Andric                     for idx in range(count):
14006c3fb27SDimitry Andric                         section = self.sbmodule.GetSectionAtIndex(idx)
14106c3fb27SDimitry Andric                         if section.name == key:
14206c3fb27SDimitry Andric                             return section
14306c3fb27SDimitry Andric                 elif isinstance(key, self.re_compile_type):
14406c3fb27SDimitry Andric                     matches = []
14506c3fb27SDimitry Andric                     for idx in range(count):
14606c3fb27SDimitry Andric                         section = self.sbmodule.GetSectionAtIndex(idx)
14706c3fb27SDimitry Andric                         name = section.name
14806c3fb27SDimitry Andric                         if name:
14906c3fb27SDimitry Andric                             re_match = key.search(name)
15006c3fb27SDimitry Andric                             if re_match:
15106c3fb27SDimitry Andric                                 matches.append(section)
15206c3fb27SDimitry Andric                     return matches
15306c3fb27SDimitry Andric                 else:
15406c3fb27SDimitry Andric                     print("error: unsupported item type: %s" % type(key))
15506c3fb27SDimitry Andric                 return None
15606c3fb27SDimitry Andric 
15706c3fb27SDimitry Andric         class compile_units_access(object):
15806c3fb27SDimitry Andric             re_compile_type = type(re.compile('.'))
15906c3fb27SDimitry Andric             '''A helper object that will lazily hand out lldb.SBCompileUnit objects for a module when supplied an index, full or partial path, or regular expression.'''
16006c3fb27SDimitry Andric             def __init__(self, sbmodule):
16106c3fb27SDimitry Andric                 self.sbmodule = sbmodule
16206c3fb27SDimitry Andric 
16306c3fb27SDimitry Andric             def __len__(self):
16406c3fb27SDimitry Andric                 if self.sbmodule:
16506c3fb27SDimitry Andric                     return int(self.sbmodule.GetNumCompileUnits())
16606c3fb27SDimitry Andric                 return 0
16706c3fb27SDimitry Andric 
16806c3fb27SDimitry Andric             def __getitem__(self, key):
16906c3fb27SDimitry Andric                 count = len(self)
17006c3fb27SDimitry Andric                 if type(key) is int:
17106c3fb27SDimitry Andric                     if -count <= key < count:
17206c3fb27SDimitry Andric                         key %= count
17306c3fb27SDimitry Andric                         return self.sbmodule.GetCompileUnitAtIndex(key)
17406c3fb27SDimitry Andric                 elif type(key) is str:
17506c3fb27SDimitry Andric                     is_full_path = key[0] == '/'
17606c3fb27SDimitry Andric                     for idx in range(count):
17706c3fb27SDimitry Andric                         comp_unit = self.sbmodule.GetCompileUnitAtIndex(idx)
17806c3fb27SDimitry Andric                         if is_full_path:
17906c3fb27SDimitry Andric                             if comp_unit.file.fullpath == key:
18006c3fb27SDimitry Andric                                 return comp_unit
18106c3fb27SDimitry Andric                         else:
18206c3fb27SDimitry Andric                             if comp_unit.file.basename == key:
18306c3fb27SDimitry Andric                                 return comp_unit
18406c3fb27SDimitry Andric                 elif isinstance(key, self.re_compile_type):
18506c3fb27SDimitry Andric                     matches = []
18606c3fb27SDimitry Andric                     for idx in range(count):
18706c3fb27SDimitry Andric                         comp_unit = self.sbmodule.GetCompileUnitAtIndex(idx)
18806c3fb27SDimitry Andric                         fullpath = comp_unit.file.fullpath
18906c3fb27SDimitry Andric                         if fullpath:
19006c3fb27SDimitry Andric                             re_match = key.search(fullpath)
19106c3fb27SDimitry Andric                             if re_match:
19206c3fb27SDimitry Andric                                 matches.append(comp_unit)
19306c3fb27SDimitry Andric                     return matches
19406c3fb27SDimitry Andric                 else:
19506c3fb27SDimitry Andric                     print("error: unsupported item type: %s" % type(key))
19606c3fb27SDimitry Andric                 return None
19706c3fb27SDimitry Andric 
19806c3fb27SDimitry Andric         def get_sections_access_object(self):
19906c3fb27SDimitry Andric             '''An accessor function that returns a sections_access() object which allows lazy section array access.'''
20006c3fb27SDimitry Andric             return self.sections_access (self)
20106c3fb27SDimitry Andric 
20206c3fb27SDimitry Andric         def get_sections_array(self):
20306c3fb27SDimitry Andric             '''An accessor function that returns an array object that contains all sections in this module object.'''
20406c3fb27SDimitry Andric             if not hasattr(self, 'sections_array'):
20506c3fb27SDimitry Andric                 self.sections_array = []
20606c3fb27SDimitry Andric                 for idx in range(self.num_sections):
20706c3fb27SDimitry Andric                     self.sections_array.append(self.GetSectionAtIndex(idx))
20806c3fb27SDimitry Andric             return self.sections_array
20906c3fb27SDimitry Andric 
21006c3fb27SDimitry Andric         def get_compile_units_array(self):
21106c3fb27SDimitry Andric             '''An accessor function that returns an array object that contains all compile_units in this module object.'''
21206c3fb27SDimitry Andric             if not hasattr(self, 'compile_units_array'):
21306c3fb27SDimitry Andric                 self.compile_units_array = []
21406c3fb27SDimitry Andric                 for idx in range(self.GetNumCompileUnits()):
21506c3fb27SDimitry Andric                     self.compile_units_array.append(self.GetCompileUnitAtIndex(idx))
21606c3fb27SDimitry Andric             return self.compile_units_array
21706c3fb27SDimitry Andric 
21806c3fb27SDimitry Andric         symbols = property(get_symbols_array, None, doc='''A read only property that returns a list() of lldb.SBSymbol objects contained in this module.''')
21906c3fb27SDimitry Andric         symbol = property(get_symbols_access_object, None, doc='''A read only property that can be used to access symbols by index ("symbol = module.symbol[0]"), name ("symbols = module.symbol['main']"), or using a regular expression ("symbols = module.symbol[re.compile(...)]"). The return value is a single lldb.SBSymbol object for array access, and a list() of lldb.SBSymbol objects for name and regular expression access''')
22006c3fb27SDimitry Andric         sections = property(get_sections_array, None, doc='''A read only property that returns a list() of lldb.SBSection objects contained in this module.''')
22106c3fb27SDimitry Andric         compile_units = property(get_compile_units_array, None, doc='''A read only property that returns a list() of lldb.SBCompileUnit objects contained in this module.''')
22206c3fb27SDimitry Andric         section = property(get_sections_access_object, None, doc='''A read only property that can be used to access symbols by index ("section = module.section[0]"), name ("sections = module.section[\'main\']"), or using a regular expression ("sections = module.section[re.compile(...)]"). The return value is a single lldb.SBSection object for array access, and a list() of lldb.SBSection objects for name and regular expression access''')
22306c3fb27SDimitry Andric         section = property(get_sections_access_object, None, doc='''A read only property that can be used to access compile units by index ("compile_unit = module.compile_unit[0]"), name ("compile_unit = module.compile_unit[\'main.cpp\']"), or using a regular expression ("compile_unit = module.compile_unit[re.compile(...)]"). The return value is a single lldb.SBCompileUnit object for array access or by full or partial path, and a list() of lldb.SBCompileUnit objects regular expressions.''')
22406c3fb27SDimitry Andric 
22506c3fb27SDimitry Andric         def get_uuid(self):
22606c3fb27SDimitry Andric             return uuid.UUID (self.GetUUIDString())
22706c3fb27SDimitry Andric 
22806c3fb27SDimitry Andric         uuid = property(get_uuid, None, doc='''A read only property that returns a standard python uuid.UUID object that represents the UUID of this module.''')
22906c3fb27SDimitry Andric         file = property(GetFileSpec, None, doc='''A read only property that returns an lldb object that represents the file (lldb.SBFileSpec) for this object file for this module as it is represented where it is being debugged.''')
23006c3fb27SDimitry Andric         platform_file = property(GetPlatformFileSpec, None, doc='''A read only property that returns an lldb object that represents the file (lldb.SBFileSpec) for this object file for this module as it is represented on the current host system.''')
23106c3fb27SDimitry Andric         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 module.''')
23206c3fb27SDimitry Andric         addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this module.''')
23306c3fb27SDimitry Andric         triple = property(GetTriple, None, doc='''A read only property that returns the target triple (arch-vendor-os) for this module.''')
23406c3fb27SDimitry Andric         num_symbols = property(GetNumSymbols, None, doc='''A read only property that returns number of symbols in the module symbol table as an integer.''')
23506c3fb27SDimitry Andric         num_sections = property(GetNumSections, None, doc='''A read only property that returns number of sections in the module as an integer.''')
23606c3fb27SDimitry Andric 
23706c3fb27SDimitry Andric     %}
23806c3fb27SDimitry Andric #endif
23906c3fb27SDimitry Andric }
240