1061da546Spatrick //===-- SWIG Interface for SBModule -----------------------------*- C++ -*-===// 2061da546Spatrick // 3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6061da546Spatrick // 7061da546Spatrick //===----------------------------------------------------------------------===// 8061da546Spatrick 9061da546Spatrick namespace lldb { 10061da546Spatrick 11061da546Spatrick #ifdef SWIGPYTHON 12061da546Spatrick %pythoncode%{ 13061da546Spatrick # ================================== 14061da546Spatrick # Helper function for SBModule class 15061da546Spatrick # ================================== 16061da546Spatrick def in_range(symbol, section): 17061da546Spatrick """Test whether a symbol is within the range of a section.""" 18061da546Spatrick symSA = symbol.GetStartAddress().GetFileAddress() 19061da546Spatrick symEA = symbol.GetEndAddress().GetFileAddress() 20061da546Spatrick secSA = section.GetFileAddress() 21061da546Spatrick secEA = secSA + section.GetByteSize() 22061da546Spatrick 23061da546Spatrick if symEA != LLDB_INVALID_ADDRESS: 24061da546Spatrick if secSA <= symSA and symEA <= secEA: 25061da546Spatrick return True 26061da546Spatrick else: 27061da546Spatrick return False 28061da546Spatrick else: 29061da546Spatrick if secSA <= symSA and symSA < secEA: 30061da546Spatrick return True 31061da546Spatrick else: 32061da546Spatrick return False 33061da546Spatrick %} 34061da546Spatrick #endif 35061da546Spatrick 36061da546Spatrick %feature("docstring", 37061da546Spatrick "Represents an executable image and its associated object and symbol files. 38061da546Spatrick 39061da546Spatrick The module is designed to be able to select a single slice of an 40061da546Spatrick executable image as it would appear on disk and during program 41061da546Spatrick execution. 42061da546Spatrick 43be691f3bSpatrick You can retrieve SBModule from :py:class:`SBSymbolContext` , which in turn is available 44061da546Spatrick from SBFrame. 45061da546Spatrick 46be691f3bSpatrick SBModule supports symbol iteration, for example, :: 47061da546Spatrick 48061da546Spatrick for symbol in module: 49061da546Spatrick name = symbol.GetName() 50061da546Spatrick saddr = symbol.GetStartAddress() 51061da546Spatrick eaddr = symbol.GetEndAddress() 52061da546Spatrick 53be691f3bSpatrick and rich comparison methods which allow the API program to use, :: 54061da546Spatrick 55061da546Spatrick if thisModule == thatModule: 56061da546Spatrick print('This module is the same as that module') 57061da546Spatrick 58061da546Spatrick to test module equality. A module also contains object file sections, namely 59be691f3bSpatrick :py:class:`SBSection` . SBModule supports section iteration through section_iter(), for 60be691f3bSpatrick example, :: 61061da546Spatrick 62061da546Spatrick print('Number of sections: %d' % module.GetNumSections()) 63061da546Spatrick for sec in module.section_iter(): 64061da546Spatrick print(sec) 65061da546Spatrick 66be691f3bSpatrick And to iterate the symbols within a SBSection, use symbol_in_section_iter(), :: 67061da546Spatrick 68061da546Spatrick # Iterates the text section and prints each symbols within each sub-section. 69061da546Spatrick for subsec in text_sec: 70061da546Spatrick print(INDENT + repr(subsec)) 71061da546Spatrick for sym in exe_module.symbol_in_section_iter(subsec): 72061da546Spatrick print(INDENT2 + repr(sym)) 73061da546Spatrick print(INDENT2 + 'symbol type: %s' % symbol_type_to_str(sym.GetType())) 74061da546Spatrick 75be691f3bSpatrick produces this following output: :: 76061da546Spatrick 77061da546Spatrick [0x0000000100001780-0x0000000100001d5c) a.out.__TEXT.__text 78061da546Spatrick id = {0x00000004}, name = 'mask_access(MaskAction, unsigned int)', range = [0x00000001000017c0-0x0000000100001870) 79061da546Spatrick symbol type: code 80061da546Spatrick id = {0x00000008}, name = 'thread_func(void*)', range = [0x0000000100001870-0x00000001000019b0) 81061da546Spatrick symbol type: code 82061da546Spatrick id = {0x0000000c}, name = 'main', range = [0x00000001000019b0-0x0000000100001d5c) 83061da546Spatrick symbol type: code 84061da546Spatrick id = {0x00000023}, name = 'start', address = 0x0000000100001780 85061da546Spatrick symbol type: code 86061da546Spatrick [0x0000000100001d5c-0x0000000100001da4) a.out.__TEXT.__stubs 87061da546Spatrick id = {0x00000024}, name = '__stack_chk_fail', range = [0x0000000100001d5c-0x0000000100001d62) 88061da546Spatrick symbol type: trampoline 89061da546Spatrick id = {0x00000028}, name = 'exit', range = [0x0000000100001d62-0x0000000100001d68) 90061da546Spatrick symbol type: trampoline 91061da546Spatrick id = {0x00000029}, name = 'fflush', range = [0x0000000100001d68-0x0000000100001d6e) 92061da546Spatrick symbol type: trampoline 93061da546Spatrick id = {0x0000002a}, name = 'fgets', range = [0x0000000100001d6e-0x0000000100001d74) 94061da546Spatrick symbol type: trampoline 95061da546Spatrick id = {0x0000002b}, name = 'printf', range = [0x0000000100001d74-0x0000000100001d7a) 96061da546Spatrick symbol type: trampoline 97061da546Spatrick id = {0x0000002c}, name = 'pthread_create', range = [0x0000000100001d7a-0x0000000100001d80) 98061da546Spatrick symbol type: trampoline 99061da546Spatrick id = {0x0000002d}, name = 'pthread_join', range = [0x0000000100001d80-0x0000000100001d86) 100061da546Spatrick symbol type: trampoline 101061da546Spatrick id = {0x0000002e}, name = 'pthread_mutex_lock', range = [0x0000000100001d86-0x0000000100001d8c) 102061da546Spatrick symbol type: trampoline 103061da546Spatrick id = {0x0000002f}, name = 'pthread_mutex_unlock', range = [0x0000000100001d8c-0x0000000100001d92) 104061da546Spatrick symbol type: trampoline 105061da546Spatrick id = {0x00000030}, name = 'rand', range = [0x0000000100001d92-0x0000000100001d98) 106061da546Spatrick symbol type: trampoline 107061da546Spatrick id = {0x00000031}, name = 'strtoul', range = [0x0000000100001d98-0x0000000100001d9e) 108061da546Spatrick symbol type: trampoline 109061da546Spatrick id = {0x00000032}, name = 'usleep', range = [0x0000000100001d9e-0x0000000100001da4) 110061da546Spatrick symbol type: trampoline 111061da546Spatrick [0x0000000100001da4-0x0000000100001e2c) a.out.__TEXT.__stub_helper 112061da546Spatrick [0x0000000100001e2c-0x0000000100001f10) a.out.__TEXT.__cstring 113061da546Spatrick [0x0000000100001f10-0x0000000100001f68) a.out.__TEXT.__unwind_info 114061da546Spatrick [0x0000000100001f68-0x0000000100001ff8) a.out.__TEXT.__eh_frame 115061da546Spatrick " 116061da546Spatrick ) SBModule; 117061da546Spatrick class SBModule 118061da546Spatrick { 119061da546Spatrick public: 120061da546Spatrick 121061da546Spatrick SBModule (); 122061da546Spatrick 123061da546Spatrick SBModule (const lldb::SBModule &rhs); 124061da546Spatrick 125061da546Spatrick SBModule (const lldb::SBModuleSpec &module_spec); 126061da546Spatrick 127061da546Spatrick SBModule (lldb::SBProcess &process, 128061da546Spatrick lldb::addr_t header_addr); 129061da546Spatrick 130061da546Spatrick ~SBModule (); 131061da546Spatrick 132061da546Spatrick bool 133061da546Spatrick IsValid () const; 134061da546Spatrick 135061da546Spatrick explicit operator bool() const; 136061da546Spatrick 137061da546Spatrick void 138061da546Spatrick Clear(); 139061da546Spatrick 140061da546Spatrick %feature("docstring", " 141*f6aab3d8Srobert Check if the module is file backed. 142*f6aab3d8Srobert 143*f6aab3d8Srobert @return 144*f6aab3d8Srobert 145*f6aab3d8Srobert True, if the module is backed by an object file on disk. 146*f6aab3d8Srobert False, if the module is backed by an object file in memory.") IsFileBacked; 147*f6aab3d8Srobert bool 148*f6aab3d8Srobert IsFileBacked() const; 149*f6aab3d8Srobert 150*f6aab3d8Srobert %feature("docstring", " 151061da546Spatrick Get const accessor for the module file specification. 152061da546Spatrick 153061da546Spatrick This function returns the file for the module on the host system 154061da546Spatrick that is running LLDB. This can differ from the path on the 155061da546Spatrick platform since we might be doing remote debugging. 156061da546Spatrick 157061da546Spatrick @return 158061da546Spatrick A const reference to the file specification object.") GetFileSpec; 159061da546Spatrick lldb::SBFileSpec 160061da546Spatrick GetFileSpec () const; 161061da546Spatrick 162061da546Spatrick %feature("docstring", " 163061da546Spatrick Get accessor for the module platform file specification. 164061da546Spatrick 165061da546Spatrick Platform file refers to the path of the module as it is known on 166061da546Spatrick the remote system on which it is being debugged. For local 167061da546Spatrick debugging this is always the same as Module::GetFileSpec(). But 168061da546Spatrick remote debugging might mention a file '/usr/lib/liba.dylib' 169061da546Spatrick which might be locally downloaded and cached. In this case the 170061da546Spatrick platform file could be something like: 171061da546Spatrick '/tmp/lldb/platform-cache/remote.host.computer/usr/lib/liba.dylib' 172061da546Spatrick The file could also be cached in a local developer kit directory. 173061da546Spatrick 174061da546Spatrick @return 175061da546Spatrick A const reference to the file specification object.") GetPlatformFileSpec; 176061da546Spatrick lldb::SBFileSpec 177061da546Spatrick GetPlatformFileSpec () const; 178061da546Spatrick 179061da546Spatrick bool 180061da546Spatrick SetPlatformFileSpec (const lldb::SBFileSpec &platform_file); 181061da546Spatrick 182061da546Spatrick lldb::SBFileSpec 183061da546Spatrick GetRemoteInstallFileSpec (); 184061da546Spatrick 185061da546Spatrick bool 186061da546Spatrick SetRemoteInstallFileSpec (lldb::SBFileSpec &file); 187061da546Spatrick 188061da546Spatrick %feature("docstring", "Returns the UUID of the module as a Python string." 189061da546Spatrick ) GetUUIDString; 190061da546Spatrick const char * 191061da546Spatrick GetUUIDString () const; 192061da546Spatrick 193061da546Spatrick bool operator==(const lldb::SBModule &rhs) const; 194061da546Spatrick 195061da546Spatrick bool operator!=(const lldb::SBModule &rhs) const; 196061da546Spatrick 197061da546Spatrick lldb::SBSection 198061da546Spatrick FindSection (const char *sect_name); 199061da546Spatrick 200061da546Spatrick lldb::SBAddress 201061da546Spatrick ResolveFileAddress (lldb::addr_t vm_addr); 202061da546Spatrick 203061da546Spatrick lldb::SBSymbolContext 204061da546Spatrick ResolveSymbolContextForAddress (const lldb::SBAddress& addr, 205061da546Spatrick uint32_t resolve_scope); 206061da546Spatrick 207061da546Spatrick bool 208061da546Spatrick GetDescription (lldb::SBStream &description); 209061da546Spatrick 210061da546Spatrick uint32_t 211061da546Spatrick GetNumCompileUnits(); 212061da546Spatrick 213061da546Spatrick lldb::SBCompileUnit 214061da546Spatrick GetCompileUnitAtIndex (uint32_t); 215061da546Spatrick 216061da546Spatrick %feature("docstring", " 217be691f3bSpatrick Find compile units related to this module and passed source 218061da546Spatrick file. 219061da546Spatrick 220061da546Spatrick @param[in] sb_file_spec 221be691f3bSpatrick A :py:class:`SBFileSpec` object that contains source file 222061da546Spatrick specification. 223061da546Spatrick 224061da546Spatrick @return 225be691f3bSpatrick A :py:class:`SBSymbolContextList` that gets filled in with all of 226061da546Spatrick the symbol contexts for all the matches.") FindCompileUnits; 227061da546Spatrick lldb::SBSymbolContextList 228061da546Spatrick FindCompileUnits (const lldb::SBFileSpec &sb_file_spec); 229061da546Spatrick 230061da546Spatrick size_t 231061da546Spatrick GetNumSymbols (); 232061da546Spatrick 233061da546Spatrick lldb::SBSymbol 234061da546Spatrick GetSymbolAtIndex (size_t idx); 235061da546Spatrick 236061da546Spatrick lldb::SBSymbol 237061da546Spatrick FindSymbol (const char *name, 238061da546Spatrick lldb::SymbolType type = eSymbolTypeAny); 239061da546Spatrick 240061da546Spatrick lldb::SBSymbolContextList 241061da546Spatrick FindSymbols (const char *name, 242061da546Spatrick lldb::SymbolType type = eSymbolTypeAny); 243061da546Spatrick 244061da546Spatrick 245061da546Spatrick size_t 246061da546Spatrick GetNumSections (); 247061da546Spatrick 248061da546Spatrick lldb::SBSection 249061da546Spatrick GetSectionAtIndex (size_t idx); 250061da546Spatrick 251061da546Spatrick 252061da546Spatrick %feature("docstring", " 253061da546Spatrick Find functions by name. 254061da546Spatrick 255061da546Spatrick @param[in] name 256061da546Spatrick The name of the function we are looking for. 257061da546Spatrick 258061da546Spatrick @param[in] name_type_mask 259061da546Spatrick A logical OR of one or more FunctionNameType enum bits that 260061da546Spatrick indicate what kind of names should be used when doing the 261061da546Spatrick lookup. Bits include fully qualified names, base names, 262061da546Spatrick C++ methods, or ObjC selectors. 263061da546Spatrick See FunctionNameType for more details. 264061da546Spatrick 265061da546Spatrick @return 266061da546Spatrick A symbol context list that gets filled in with all of the 267061da546Spatrick matches.") FindFunctions; 268061da546Spatrick lldb::SBSymbolContextList 269061da546Spatrick FindFunctions (const char *name, 270061da546Spatrick uint32_t name_type_mask = lldb::eFunctionNameTypeAny); 271061da546Spatrick 272061da546Spatrick lldb::SBType 273061da546Spatrick FindFirstType (const char* name); 274061da546Spatrick 275061da546Spatrick lldb::SBTypeList 276061da546Spatrick FindTypes (const char* type); 277061da546Spatrick 278061da546Spatrick lldb::SBType 279061da546Spatrick GetTypeByID (lldb::user_id_t uid); 280061da546Spatrick 281061da546Spatrick lldb::SBType 282061da546Spatrick GetBasicType(lldb::BasicType type); 283061da546Spatrick 284061da546Spatrick %feature("docstring", " 285061da546Spatrick Get all types matching type_mask from debug info in this 286061da546Spatrick module. 287061da546Spatrick 288061da546Spatrick @param[in] type_mask 289061da546Spatrick A bitfield that consists of one or more bits logically OR'ed 290061da546Spatrick together from the lldb::TypeClass enumeration. This allows 291061da546Spatrick you to request only structure types, or only class, struct 292061da546Spatrick and union types. Passing in lldb::eTypeClassAny will return 293061da546Spatrick all types found in the debug information for this module. 294061da546Spatrick 295061da546Spatrick @return 296061da546Spatrick A list of types in this module that match type_mask") GetTypes; 297061da546Spatrick lldb::SBTypeList 298061da546Spatrick GetTypes (uint32_t type_mask = lldb::eTypeClassAny); 299061da546Spatrick 300061da546Spatrick %feature("docstring", " 301061da546Spatrick Find global and static variables by name. 302061da546Spatrick 303061da546Spatrick @param[in] target 304061da546Spatrick A valid SBTarget instance representing the debuggee. 305061da546Spatrick 306061da546Spatrick @param[in] name 307061da546Spatrick The name of the global or static variable we are looking 308061da546Spatrick for. 309061da546Spatrick 310061da546Spatrick @param[in] max_matches 311061da546Spatrick Allow the number of matches to be limited to max_matches. 312061da546Spatrick 313061da546Spatrick @return 314061da546Spatrick A list of matched variables in an SBValueList.") FindGlobalVariables; 315061da546Spatrick lldb::SBValueList 316061da546Spatrick FindGlobalVariables (lldb::SBTarget &target, 317061da546Spatrick const char *name, 318061da546Spatrick uint32_t max_matches); 319061da546Spatrick 320061da546Spatrick %feature("docstring", " 321061da546Spatrick Find the first global (or static) variable by name. 322061da546Spatrick 323061da546Spatrick @param[in] target 324061da546Spatrick A valid SBTarget instance representing the debuggee. 325061da546Spatrick 326061da546Spatrick @param[in] name 327061da546Spatrick The name of the global or static variable we are looking 328061da546Spatrick for. 329061da546Spatrick 330061da546Spatrick @return 331061da546Spatrick An SBValue that gets filled in with the found variable (if any).") FindFirstGlobalVariable; 332061da546Spatrick lldb::SBValue 333061da546Spatrick FindFirstGlobalVariable (lldb::SBTarget &target, const char *name); 334061da546Spatrick 335061da546Spatrick lldb::ByteOrder 336061da546Spatrick GetByteOrder (); 337061da546Spatrick 338061da546Spatrick uint32_t 339061da546Spatrick GetAddressByteSize(); 340061da546Spatrick 341061da546Spatrick const char * 342061da546Spatrick GetTriple (); 343061da546Spatrick 344061da546Spatrick uint32_t 345061da546Spatrick GetVersion (uint32_t *versions, 346061da546Spatrick uint32_t num_versions); 347061da546Spatrick 348061da546Spatrick lldb::SBFileSpec 349061da546Spatrick GetSymbolFileSpec() const; 350061da546Spatrick 351061da546Spatrick lldb::SBAddress 352061da546Spatrick GetObjectFileHeaderAddress() const; 353061da546Spatrick 354061da546Spatrick lldb::SBAddress 355061da546Spatrick GetObjectFileEntryPointAddress() const; 356061da546Spatrick 357dda28197Spatrick %feature("docstring", " 358dda28197Spatrick Returns the number of modules in the module cache. This is an 359dda28197Spatrick implementation detail exposed for testing and should not be relied upon. 360dda28197Spatrick 361dda28197Spatrick @return 362dda28197Spatrick The number of modules in the module cache.") GetNumberAllocatedModules; 363dda28197Spatrick static uint32_t 364dda28197Spatrick GetNumberAllocatedModules(); 365dda28197Spatrick 366be691f3bSpatrick %feature("docstring", " 367be691f3bSpatrick Removes all modules which are no longer needed by any part of LLDB from 368be691f3bSpatrick the module cache. 369be691f3bSpatrick 370be691f3bSpatrick This is an implementation detail exposed for testing and should not be 371be691f3bSpatrick relied upon. Use SBDebugger::MemoryPressureDetected instead to reduce 372be691f3bSpatrick LLDB's memory consumption during execution. 373be691f3bSpatrick ") GarbageCollectAllocatedModules; 374be691f3bSpatrick static void 375be691f3bSpatrick GarbageCollectAllocatedModules(); 376be691f3bSpatrick 377061da546Spatrick STRING_EXTENSION(SBModule) 378061da546Spatrick 379061da546Spatrick #ifdef SWIGPYTHON 380061da546Spatrick %pythoncode %{ 381061da546Spatrick def __len__(self): 382061da546Spatrick '''Return the number of symbols in a lldb.SBModule object.''' 383061da546Spatrick return self.GetNumSymbols() 384061da546Spatrick 385061da546Spatrick def __iter__(self): 386061da546Spatrick '''Iterate over all symbols in a lldb.SBModule object.''' 387061da546Spatrick return lldb_iter(self, 'GetNumSymbols', 'GetSymbolAtIndex') 388061da546Spatrick 389061da546Spatrick def section_iter(self): 390061da546Spatrick '''Iterate over all sections in a lldb.SBModule object.''' 391061da546Spatrick return lldb_iter(self, 'GetNumSections', 'GetSectionAtIndex') 392061da546Spatrick 393061da546Spatrick def compile_unit_iter(self): 394061da546Spatrick '''Iterate over all compile units in a lldb.SBModule object.''' 395061da546Spatrick return lldb_iter(self, 'GetNumCompileUnits', 'GetCompileUnitAtIndex') 396061da546Spatrick 397061da546Spatrick def symbol_in_section_iter(self, section): 398061da546Spatrick '''Given a module and its contained section, returns an iterator on the 399061da546Spatrick symbols within the section.''' 400061da546Spatrick for sym in self: 401061da546Spatrick if in_range(sym, section): 402061da546Spatrick yield sym 403061da546Spatrick 404061da546Spatrick class symbols_access(object): 405061da546Spatrick re_compile_type = type(re.compile('.')) 406061da546Spatrick '''A helper object that will lazily hand out lldb.SBSymbol objects for a module when supplied an index, name, or regular expression.''' 407061da546Spatrick def __init__(self, sbmodule): 408061da546Spatrick self.sbmodule = sbmodule 409061da546Spatrick 410061da546Spatrick def __len__(self): 411061da546Spatrick if self.sbmodule: 412061da546Spatrick return int(self.sbmodule.GetNumSymbols()) 413061da546Spatrick return 0 414061da546Spatrick 415061da546Spatrick def __getitem__(self, key): 416061da546Spatrick count = len(self) 417061da546Spatrick if type(key) is int: 418061da546Spatrick if key < count: 419061da546Spatrick return self.sbmodule.GetSymbolAtIndex(key) 420061da546Spatrick elif type(key) is str: 421061da546Spatrick matches = [] 422061da546Spatrick sc_list = self.sbmodule.FindSymbols(key) 423061da546Spatrick for sc in sc_list: 424061da546Spatrick symbol = sc.symbol 425061da546Spatrick if symbol: 426061da546Spatrick matches.append(symbol) 427061da546Spatrick return matches 428061da546Spatrick elif isinstance(key, self.re_compile_type): 429061da546Spatrick matches = [] 430061da546Spatrick for idx in range(count): 431061da546Spatrick symbol = self.sbmodule.GetSymbolAtIndex(idx) 432061da546Spatrick added = False 433061da546Spatrick name = symbol.name 434061da546Spatrick if name: 435061da546Spatrick re_match = key.search(name) 436061da546Spatrick if re_match: 437061da546Spatrick matches.append(symbol) 438061da546Spatrick added = True 439061da546Spatrick if not added: 440061da546Spatrick mangled = symbol.mangled 441061da546Spatrick if mangled: 442061da546Spatrick re_match = key.search(mangled) 443061da546Spatrick if re_match: 444061da546Spatrick matches.append(symbol) 445061da546Spatrick return matches 446061da546Spatrick else: 447061da546Spatrick print("error: unsupported item type: %s" % type(key)) 448061da546Spatrick return None 449061da546Spatrick 450061da546Spatrick def get_symbols_access_object(self): 451061da546Spatrick '''An accessor function that returns a symbols_access() object which allows lazy symbol access from a lldb.SBModule object.''' 452061da546Spatrick return self.symbols_access (self) 453061da546Spatrick 454061da546Spatrick def get_compile_units_access_object (self): 455061da546Spatrick '''An accessor function that returns a compile_units_access() object which allows lazy compile unit access from a lldb.SBModule object.''' 456061da546Spatrick return self.compile_units_access (self) 457061da546Spatrick 458061da546Spatrick def get_symbols_array(self): 459061da546Spatrick '''An accessor function that returns a list() that contains all symbols in a lldb.SBModule object.''' 460061da546Spatrick symbols = [] 461061da546Spatrick for idx in range(self.num_symbols): 462061da546Spatrick symbols.append(self.GetSymbolAtIndex(idx)) 463061da546Spatrick return symbols 464061da546Spatrick 465061da546Spatrick class sections_access(object): 466061da546Spatrick re_compile_type = type(re.compile('.')) 467061da546Spatrick '''A helper object that will lazily hand out lldb.SBSection objects for a module when supplied an index, name, or regular expression.''' 468061da546Spatrick def __init__(self, sbmodule): 469061da546Spatrick self.sbmodule = sbmodule 470061da546Spatrick 471061da546Spatrick def __len__(self): 472061da546Spatrick if self.sbmodule: 473061da546Spatrick return int(self.sbmodule.GetNumSections()) 474061da546Spatrick return 0 475061da546Spatrick 476061da546Spatrick def __getitem__(self, key): 477061da546Spatrick count = len(self) 478061da546Spatrick if type(key) is int: 479061da546Spatrick if key < count: 480061da546Spatrick return self.sbmodule.GetSectionAtIndex(key) 481061da546Spatrick elif type(key) is str: 482061da546Spatrick for idx in range(count): 483061da546Spatrick section = self.sbmodule.GetSectionAtIndex(idx) 484061da546Spatrick if section.name == key: 485061da546Spatrick return section 486061da546Spatrick elif isinstance(key, self.re_compile_type): 487061da546Spatrick matches = [] 488061da546Spatrick for idx in range(count): 489061da546Spatrick section = self.sbmodule.GetSectionAtIndex(idx) 490061da546Spatrick name = section.name 491061da546Spatrick if name: 492061da546Spatrick re_match = key.search(name) 493061da546Spatrick if re_match: 494061da546Spatrick matches.append(section) 495061da546Spatrick return matches 496061da546Spatrick else: 497061da546Spatrick print("error: unsupported item type: %s" % type(key)) 498061da546Spatrick return None 499061da546Spatrick 500061da546Spatrick class compile_units_access(object): 501061da546Spatrick re_compile_type = type(re.compile('.')) 502061da546Spatrick '''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.''' 503061da546Spatrick def __init__(self, sbmodule): 504061da546Spatrick self.sbmodule = sbmodule 505061da546Spatrick 506061da546Spatrick def __len__(self): 507061da546Spatrick if self.sbmodule: 508061da546Spatrick return int(self.sbmodule.GetNumCompileUnits()) 509061da546Spatrick return 0 510061da546Spatrick 511061da546Spatrick def __getitem__(self, key): 512061da546Spatrick count = len(self) 513061da546Spatrick if type(key) is int: 514061da546Spatrick if key < count: 515061da546Spatrick return self.sbmodule.GetCompileUnitAtIndex(key) 516061da546Spatrick elif type(key) is str: 517061da546Spatrick is_full_path = key[0] == '/' 518061da546Spatrick for idx in range(count): 519061da546Spatrick comp_unit = self.sbmodule.GetCompileUnitAtIndex(idx) 520061da546Spatrick if is_full_path: 521061da546Spatrick if comp_unit.file.fullpath == key: 522061da546Spatrick return comp_unit 523061da546Spatrick else: 524061da546Spatrick if comp_unit.file.basename == key: 525061da546Spatrick return comp_unit 526061da546Spatrick elif isinstance(key, self.re_compile_type): 527061da546Spatrick matches = [] 528061da546Spatrick for idx in range(count): 529061da546Spatrick comp_unit = self.sbmodule.GetCompileUnitAtIndex(idx) 530061da546Spatrick fullpath = comp_unit.file.fullpath 531061da546Spatrick if fullpath: 532061da546Spatrick re_match = key.search(fullpath) 533061da546Spatrick if re_match: 534061da546Spatrick matches.append(comp_unit) 535061da546Spatrick return matches 536061da546Spatrick else: 537061da546Spatrick print("error: unsupported item type: %s" % type(key)) 538061da546Spatrick return None 539061da546Spatrick 540061da546Spatrick def get_sections_access_object(self): 541061da546Spatrick '''An accessor function that returns a sections_access() object which allows lazy section array access.''' 542061da546Spatrick return self.sections_access (self) 543061da546Spatrick 544061da546Spatrick def get_sections_array(self): 545061da546Spatrick '''An accessor function that returns an array object that contains all sections in this module object.''' 546061da546Spatrick if not hasattr(self, 'sections_array'): 547061da546Spatrick self.sections_array = [] 548061da546Spatrick for idx in range(self.num_sections): 549061da546Spatrick self.sections_array.append(self.GetSectionAtIndex(idx)) 550061da546Spatrick return self.sections_array 551061da546Spatrick 552061da546Spatrick def get_compile_units_array(self): 553061da546Spatrick '''An accessor function that returns an array object that contains all compile_units in this module object.''' 554061da546Spatrick if not hasattr(self, 'compile_units_array'): 555061da546Spatrick self.compile_units_array = [] 556061da546Spatrick for idx in range(self.GetNumCompileUnits()): 557061da546Spatrick self.compile_units_array.append(self.GetCompileUnitAtIndex(idx)) 558061da546Spatrick return self.compile_units_array 559061da546Spatrick 560061da546Spatrick symbols = property(get_symbols_array, None, doc='''A read only property that returns a list() of lldb.SBSymbol objects contained in this module.''') 561061da546Spatrick 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''') 562061da546Spatrick sections = property(get_sections_array, None, doc='''A read only property that returns a list() of lldb.SBSection objects contained in this module.''') 563061da546Spatrick 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.''') 564061da546Spatrick 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''') 565061da546Spatrick 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.''') 566061da546Spatrick 567061da546Spatrick def get_uuid(self): 568061da546Spatrick return uuid.UUID (self.GetUUIDString()) 569061da546Spatrick 570061da546Spatrick 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.''') 571061da546Spatrick 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.''') 572061da546Spatrick 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.''') 573061da546Spatrick 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.''') 574061da546Spatrick addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this module.''') 575061da546Spatrick triple = property(GetTriple, None, doc='''A read only property that returns the target triple (arch-vendor-os) for this module.''') 576061da546Spatrick num_symbols = property(GetNumSymbols, None, doc='''A read only property that returns number of symbols in the module symbol table as an integer.''') 577061da546Spatrick num_sections = property(GetNumSections, None, doc='''A read only property that returns number of sections in the module as an integer.''') 578061da546Spatrick 579061da546Spatrick %} 580061da546Spatrick #endif 581061da546Spatrick 582061da546Spatrick }; 583061da546Spatrick 584061da546Spatrick } // namespace lldb 585