xref: /llvm-project/lldb/examples/python/globals.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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# ----------------------------------------------------------------------
10
11import lldb
12import optparse
13import os
14import shlex
15import sys
16
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(
24        path, options.arch, options.platform, False, error
25    )
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                        )
46                        if global_variable_list:
47                            # Print results for anything that matched
48                            for global_variable in global_variable_list:
49                                # returns the global variable name as a string
50                                print("name = %s" % global_variable.name)
51                                # Returns the variable value as a string
52                                print("value = %s" % global_variable.value)
53                                print(
54                                    "type = %s" % global_variable.type
55                                )  # Returns an lldb.SBType object
56                                # Returns an lldb.SBAddress (section offset
57                                # address) for this global
58                                print("addr = %s" % global_variable.addr)
59                                # Returns the file virtual address for this
60                                # global
61                                print(
62                                    "file_addr = 0x%x" % global_variable.addr.file_addr
63                                )
64                                # returns the global variable value as a string
65                                print("location = %s" % global_variable.location)
66                                # Returns the size in bytes of this global
67                                # variable
68                                print("size = %s" % global_variable.size)
69                                print()
70
71
72def globals(command_args):
73    """Extract all globals from any arguments which must be paths to object files."""
74    usage = "usage: %prog [options] <PATH> [PATH ...]"
75    description = """This command will find all globals in the specified object file and return an list() of lldb.SBValue objects (which might be empty)."""
76    parser = optparse.OptionParser(description=description, prog="globals", usage=usage)
77    parser.add_option(
78        "-v",
79        "--verbose",
80        action="store_true",
81        dest="verbose",
82        help="display verbose debug info",
83        default=False,
84    )
85    parser.add_option(
86        "-a",
87        "--arch",
88        type="string",
89        metavar="arch",
90        dest="arch",
91        help="Specify an architecture (or triple) to use when extracting from a file.",
92    )
93    parser.add_option(
94        "-p",
95        "--platform",
96        type="string",
97        metavar="platform",
98        dest="platform",
99        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".',
100    )
101    try:
102        (options, args) = parser.parse_args(command_args)
103    except:
104        return
105
106    for path in args:
107        get_globals(path, options)
108
109
110if __name__ == "__main__":
111    lldb.debugger = lldb.SBDebugger.Create()
112    globals(sys.argv[1:])
113