aboutsummaryrefslogtreecommitdiff
path: root/lldb/examples/python/lldb_module_utils.py
blob: eb00a489ce014f02053454c49b07dd8dee3f3ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python

import lldb
import optparse
import shlex
import string
import sys


def create_dump_module_line_tables_options():
    usage = "usage: dump_module_line_tables [options] MODULE1 [MODULE2 ...]"
    description = '''Dumps all line tables from all compile units for any modules specified as arguments. Specifying the --verbose flag will output address ranges for each line entry.'''
    parser = optparse.OptionParser(
        description=description,
        prog='start_gdb_log',
        usage=usage)
    parser.add_option(
        '-v',
        '--verbose',
        action='store_true',
        dest='verbose',
        help='Display verbose output.',
        default=False)
    return parser


def dump_module_line_tables(debugger, command, result, dict):
    '''Dumps all line tables from all compile units for any modules specified as arguments.'''
    command_args = shlex.split(command)

    parser = create_dump_module_line_tables_options()
    try:
        (options, args) = parser.parse_args(command_args)
    except:
        return
    if command_args:
        target = debugger.GetSelectedTarget()
        lldb.target = target
        for module_name in command_args:
            result.PutCString('Searching for module "%s"' % (module_name,))
            module_fspec = lldb.SBFileSpec(module_name, False)
            module = target.FindModule(module_fspec)
            if module:
                for cu_idx in range(module.GetNumCompileUnits()):
                    cu = module.GetCompileUnitAtIndex(cu_idx)
                    result.PutCString("\n%s:" % (cu.file))
                    for line_idx in range(cu.GetNumLineEntries()):
                        line_entry = cu.GetLineEntryAtIndex(line_idx)
                        start_file_addr = line_entry.addr.file_addr
                        end_file_addr = line_entry.end_addr.file_addr
                        # If the two addresses are equal, this line table entry
                        # is a termination entry
                        if options.verbose:
                            if start_file_addr != end_file_addr:
                                result.PutCString(
                                    '[%#x - %#x): %s' %
                                    (start_file_addr, end_file_addr, line_entry))
                        else:
                            if start_file_addr == end_file_addr:
                                result.PutCString('%#x: END' %
                                                  (start_file_addr))
                            else:
                                result.PutCString(
                                    '%#x: %s' %
                                    (start_file_addr, line_entry))
                        if start_file_addr == end_file_addr:
                            result.Printf("\n")
            else:
                result.PutCString("no module for '%s'" % module)
    else:
        result.PutCString("error: invalid target")

parser = create_dump_module_line_tables_options()
dump_module_line_tables.__doc__ = parser.format_help()
lldb.debugger.HandleCommand(
    'command script add -f %s.dump_module_line_tables dump_module_line_tables' %
    __name__)
print 'Installed "dump_module_line_tables" command'