xref: /openbsd-src/gnu/llvm/lldb/examples/python/globals.py (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1*be691f3bSpatrick#!/usr/bin/env python
2061da546Spatrick
3061da546Spatrick#----------------------------------------------------------------------
4061da546Spatrick# For the shells csh, tcsh:
5061da546Spatrick#   ( setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ; ./globals.py <path> [<path> ...])
6061da546Spatrick#
7061da546Spatrick# For the shells sh, bash:
8061da546Spatrick#   PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python ./globals.py <path> [<path> ...]
9061da546Spatrick#----------------------------------------------------------------------
10061da546Spatrick
11061da546Spatrickimport lldb
12061da546Spatrickimport optparse
13061da546Spatrickimport os
14061da546Spatrickimport shlex
15061da546Spatrickimport sys
16061da546Spatrick
17061da546Spatrick
18061da546Spatrickdef get_globals(raw_path, options):
19061da546Spatrick    error = lldb.SBError()
20061da546Spatrick    # Resolve the path if needed
21061da546Spatrick    path = os.path.expanduser(raw_path)
22061da546Spatrick    # Create a target using path + options
23061da546Spatrick    target = lldb.debugger.CreateTarget(
24061da546Spatrick        path, options.arch, options.platform, False, error)
25061da546Spatrick    if target:
26061da546Spatrick        # Get the executable module
27061da546Spatrick        module = target.module[target.executable.basename]
28061da546Spatrick        if module:
29061da546Spatrick            # Keep track of which variables we have already looked up
30061da546Spatrick            global_names = list()
31061da546Spatrick            # Iterate through all symbols in the symbol table and watch for any
32061da546Spatrick            # DATA symbols
33061da546Spatrick            for symbol in module.symbols:
34061da546Spatrick                if symbol.type == lldb.eSymbolTypeData:
35061da546Spatrick                    # The symbol is a DATA symbol, lets try and find all global variables
36061da546Spatrick                    # that match this name and print them
37061da546Spatrick                    global_name = symbol.name
38061da546Spatrick                    # Make sure we don't lookup the same variable twice
39061da546Spatrick                    if global_name not in global_names:
40061da546Spatrick                        global_names.append(global_name)
41061da546Spatrick                        # Find all global variables by name
42061da546Spatrick                        global_variable_list = module.FindGlobalVariables(
43061da546Spatrick                            target, global_name, lldb.UINT32_MAX)
44061da546Spatrick                        if global_variable_list:
45061da546Spatrick                            # Print results for anything that matched
46061da546Spatrick                            for global_variable in global_variable_list:
47061da546Spatrick                                # returns the global variable name as a string
48061da546Spatrick                                print('name = %s' % global_variable.name)
49061da546Spatrick                                # Returns the variable value as a string
50061da546Spatrick                                print('value = %s' % global_variable.value)
51061da546Spatrick                                print('type = %s' % global_variable.type)    # Returns an lldb.SBType object
52061da546Spatrick                                # Returns an lldb.SBAddress (section offset
53061da546Spatrick                                # address) for this global
54061da546Spatrick                                print('addr = %s' % global_variable.addr)
55061da546Spatrick                                # Returns the file virtual address for this
56061da546Spatrick                                # global
57061da546Spatrick                                print('file_addr = 0x%x' % global_variable.addr.file_addr)
58061da546Spatrick                                # returns the global variable value as a string
59061da546Spatrick                                print('location = %s' % global_variable.location)
60061da546Spatrick                                # Returns the size in bytes of this global
61061da546Spatrick                                # variable
62061da546Spatrick                                print('size = %s' % global_variable.size)
63061da546Spatrick                                print()
64061da546Spatrick
65061da546Spatrick
66061da546Spatrickdef globals(command_args):
67061da546Spatrick    '''Extract all globals from any arguments which must be paths to object files.'''
68061da546Spatrick    usage = "usage: %prog [options] <PATH> [PATH ...]"
69061da546Spatrick    description = '''This command will find all globals in the specified object file and return an list() of lldb.SBValue objects (which might be empty).'''
70061da546Spatrick    parser = optparse.OptionParser(
71061da546Spatrick        description=description,
72061da546Spatrick        prog='globals',
73061da546Spatrick        usage=usage)
74061da546Spatrick    parser.add_option(
75061da546Spatrick        '-v',
76061da546Spatrick        '--verbose',
77061da546Spatrick        action='store_true',
78061da546Spatrick        dest='verbose',
79061da546Spatrick        help='display verbose debug info',
80061da546Spatrick        default=False)
81061da546Spatrick    parser.add_option(
82061da546Spatrick        '-a',
83061da546Spatrick        '--arch',
84061da546Spatrick        type='string',
85061da546Spatrick        metavar='arch',
86061da546Spatrick        dest='arch',
87061da546Spatrick        help='Specify an architecture (or triple) to use when extracting from a file.')
88061da546Spatrick    parser.add_option(
89061da546Spatrick        '-p',
90061da546Spatrick        '--platform',
91061da546Spatrick        type='string',
92061da546Spatrick        metavar='platform',
93061da546Spatrick        dest='platform',
94061da546Spatrick        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".')
95061da546Spatrick    try:
96061da546Spatrick        (options, args) = parser.parse_args(command_args)
97061da546Spatrick    except:
98061da546Spatrick        return
99061da546Spatrick
100061da546Spatrick    for path in args:
101061da546Spatrick        get_globals(path, options)
102061da546Spatrick
103061da546Spatrickif __name__ == '__main__':
104061da546Spatrick    lldb.debugger = lldb.SBDebugger.Create()
105061da546Spatrick    globals(sys.argv[1:])
106