blob: 43bd915b3933d71760e9219ab075d30b7aa1cdb9 [file] [log] [blame]
Greg Clayton39caceb2012-07-05 20:24:41 +00001#!/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
Kate Stoneb9c1b512016-09-06 20:57:50 +000018
Greg Clayton39caceb2012-07-05 20:24:41 +000019def 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 Stoneb9c1b512016-09-06 20:57:50 +000024 target = lldb.debugger.CreateTarget(
25 path, options.arch, options.platform, False, error)
Greg Clayton39caceb2012-07-05 20:24:41 +000026 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 Stoneb9c1b512016-09-06 20:57:50 +000032 # Iterate through all symbols in the symbol table and watch for any
33 # DATA symbols
Greg Clayton39caceb2012-07-05 20:24:41 +000034 for symbol in module.symbols:
35 if symbol.type == lldb.eSymbolTypeData:
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 # The symbol is a DATA symbol, lets try and find all global variables
Greg Clayton39caceb2012-07-05 20:24:41 +000037 # 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 Stoneb9c1b512016-09-06 20:57:50 +000043 global_variable_list = module.FindGlobalVariables(
44 target, global_name, lldb.UINT32_MAX)
Greg Clayton39caceb2012-07-05 20:24:41 +000045 if global_variable_list:
46 # Print results for anything that matched
47 for global_variable in global_variable_list:
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 # 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 Clayton39caceb2012-07-05 20:24:41 +000052 print 'type = %s' % global_variable.type # Returns an lldb.SBType object
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 # 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 Clayton39caceb2012-07-05 20:24:41 +000064 print
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
Greg Clayton39caceb2012-07-05 20:24:41 +000067def 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 Stoneb9c1b512016-09-06 20:57:50 +000070 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 Clayton39caceb2012-07-05 20:24:41 +000096 try:
97 (options, args) = parser.parse_args(command_args)
98 except:
99 return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100
Greg Clayton39caceb2012-07-05 20:24:41 +0000101 for path in args:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 get_globals(path, options)
103
Greg Clayton39caceb2012-07-05 20:24:41 +0000104if __name__ == '__main__':
105 lldb.debugger = lldb.SBDebugger.Create()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 globals(sys.argv[1:])