globals.py revision 1.1.1.2
1#!/usr/bin/env 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#---------------------------------------------------------------------- 10from __future__ import print_function 11 12import lldb 13import optparse 14import os 15import shlex 16import sys 17 18 19def get_globals(raw_path, options): 20 error = lldb.SBError() 21 # Resolve the path if needed 22 path = os.path.expanduser(raw_path) 23 # Create a target using path + options 24 target = lldb.debugger.CreateTarget( 25 path, options.arch, options.platform, False, error) 26 if target: 27 # Get the executable module 28 module = target.module[target.executable.basename] 29 if module: 30 # Keep track of which variables we have already looked up 31 global_names = list() 32 # Iterate through all symbols in the symbol table and watch for any 33 # DATA symbols 34 for symbol in module.symbols: 35 if symbol.type == lldb.eSymbolTypeData: 36 # The symbol is a DATA symbol, lets try and find all global variables 37 # that match this name and print them 38 global_name = symbol.name 39 # Make sure we don't lookup the same variable twice 40 if global_name not in global_names: 41 global_names.append(global_name) 42 # Find all global variables by name 43 global_variable_list = module.FindGlobalVariables( 44 target, global_name, lldb.UINT32_MAX) 45 if global_variable_list: 46 # Print results for anything that matched 47 for global_variable in global_variable_list: 48 # returns the global variable name as a string 49 print('name = %s' % global_variable.name) 50 # Returns the variable value as a string 51 print('value = %s' % global_variable.value) 52 print('type = %s' % global_variable.type) # Returns an lldb.SBType object 53 # Returns an lldb.SBAddress (section offset 54 # address) for this global 55 print('addr = %s' % global_variable.addr) 56 # Returns the file virtual address for this 57 # global 58 print('file_addr = 0x%x' % global_variable.addr.file_addr) 59 # returns the global variable value as a string 60 print('location = %s' % global_variable.location) 61 # Returns the size in bytes of this global 62 # variable 63 print('size = %s' % global_variable.size) 64 print() 65 66 67def globals(command_args): 68 '''Extract all globals from any arguments which must be paths to object files.''' 69 usage = "usage: %prog [options] <PATH> [PATH ...]" 70 description = '''This command will find all globals in the specified object file and return an list() of lldb.SBValue objects (which might be empty).''' 71 parser = optparse.OptionParser( 72 description=description, 73 prog='globals', 74 usage=usage) 75 parser.add_option( 76 '-v', 77 '--verbose', 78 action='store_true', 79 dest='verbose', 80 help='display verbose debug info', 81 default=False) 82 parser.add_option( 83 '-a', 84 '--arch', 85 type='string', 86 metavar='arch', 87 dest='arch', 88 help='Specify an architecture (or triple) to use when extracting from a file.') 89 parser.add_option( 90 '-p', 91 '--platform', 92 type='string', 93 metavar='platform', 94 dest='platform', 95 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".') 96 try: 97 (options, args) = parser.parse_args(command_args) 98 except: 99 return 100 101 for path in args: 102 get_globals(path, options) 103 104if __name__ == '__main__': 105 lldb.debugger = lldb.SBDebugger.Create() 106 globals(sys.argv[1:]) 107