1#!/usr/bin/python 2 3#---------------------------------------------------------------------- 4# For the shells csh, tcsh: 5# ( setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ; ./globals.py <path> [<path> ...]) 6# 7# For the shells sh, bash: 8# PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ./globals.py <path> [<path> ...] 9#---------------------------------------------------------------------- 10 11import lldb 12import commands 13import optparse 14import os 15import shlex 16import sys 17 18def get_globals(raw_path, options): 19 error = lldb.SBError() 20 # Resolve the path if needed 21 path = os.path.expanduser(raw_path) 22 # Create a target using path + options 23 target = lldb.debugger.CreateTarget(path, options.arch, options.platform, False, error) 24 if target: 25 # Get the executable module 26 module = target.module[target.executable.basename] 27 if module: 28 # Keep track of which variables we have already looked up 29 global_names = list() 30 # Iterate through all symbols in the symbol table and watch for any DATA symbols 31 for symbol in module.symbols: 32 if symbol.type == lldb.eSymbolTypeData: 33 # The symbol is a DATA symbol, lets try and find all global variables 34 # that match this name and print them 35 global_name = symbol.name 36 # Make sure we don't lookup the same variable twice 37 if global_name not in global_names: 38 global_names.append(global_name) 39 # Find all global variables by name 40 global_variable_list = module.FindGlobalVariables (target, global_name, lldb.UINT32_MAX) 41 if global_variable_list: 42 # Print results for anything that matched 43 for global_variable in global_variable_list: 44 print 'name = %s' % global_variable.name # returns the global variable name as a string 45 print 'value = %s' % global_variable.value # Returns the variable value as a string 46 print 'type = %s' % global_variable.type # Returns an lldb.SBType object 47 print 'addr = %s' % global_variable.addr # Returns an lldb.SBAddress (section offset address) for this global 48 print 'file_addr = 0x%x' % global_variable.addr.file_addr # Returns the file virtual address for this global 49 print 'location = %s' % global_variable.location # returns the global variable value as a string 50 print 'size = %s' % global_variable.size # Returns the size in bytes of this global variable 51 print 52 53def globals(command_args): 54 '''Extract all globals from any arguments which must be paths to object files.''' 55 usage = "usage: %prog [options] <PATH> [PATH ...]" 56 description='''This command will find all globals in the specified object file and return an list() of lldb.SBValue objects (which might be empty).''' 57 parser = optparse.OptionParser(description=description, prog='globals',usage=usage) 58 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) 59 parser.add_option('-a', '--arch', type='string', metavar='arch', dest='arch', help='Specify an architecture (or triple) to use when extracting from a file.') 60 parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".') 61 try: 62 (options, args) = parser.parse_args(command_args) 63 except: 64 return 65 66 for path in args: 67 get_globals (path, options) 68 69if __name__ == '__main__': 70 lldb.debugger = lldb.SBDebugger.Create() 71 globals (sys.argv[1:]) 72 73