Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 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 | |
| 11 | import lldb |
| 12 | import commands |
| 13 | import optparse |
| 14 | import os |
| 15 | import shlex |
| 16 | import sys |
| 17 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 19 | def 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 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | target = lldb.debugger.CreateTarget( |
| 25 | path, options.arch, options.platform, False, error) |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 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() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | # Iterate through all symbols in the symbol table and watch for any |
| 33 | # DATA symbols |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 34 | for symbol in module.symbols: |
| 35 | if symbol.type == lldb.eSymbolTypeData: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | # The symbol is a DATA symbol, lets try and find all global variables |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 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 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | global_variable_list = module.FindGlobalVariables( |
| 44 | target, global_name, lldb.UINT32_MAX) |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 45 | if global_variable_list: |
| 46 | # Print results for anything that matched |
| 47 | for global_variable in global_variable_list: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 52 | print 'type = %s' % global_variable.type # Returns an lldb.SBType object |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 64 | print |
| 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 67 | def globals(command_args): |
| 68 | '''Extract all globals from any arguments which must be paths to object files.''' |
| 69 | usage = "usage: %prog [options] <PATH> [PATH ...]" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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".') |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 96 | try: |
| 97 | (options, args) = parser.parse_args(command_args) |
| 98 | except: |
| 99 | return |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 101 | for path in args: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | get_globals(path, options) |
| 103 | |
Greg Clayton | 39caceb | 2012-07-05 20:24:41 +0000 | [diff] [blame] | 104 | if __name__ == '__main__': |
| 105 | lldb.debugger = lldb.SBDebugger.Create() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | globals(sys.argv[1:]) |