xref: /llvm-project/lldb/examples/python/shadow.py (revision fe9d1ee9e45c91fab05c69e672117efd4ed8c0cc)
1#!/usr/bin/python
2
3import lldb
4import shlex
5
6@lldb.command("shadow")
7def check_shadow_command(debugger, command, result, dict):
8    target = debugger.GetSelectedTarget()
9    if not target:
10        print >>result, "invalid target"
11        return
12    process = target.GetProcess()
13    if not process:
14        print >>result, "invalid process"
15        return
16    thread = process.GetSelectedThread()
17    if not thread:
18        print >>result, "invalid thread"
19        return
20    frame = thread.GetSelectedFrame()
21    if not frame:
22        print >>result, "invalid frame"
23        return
24    # Parse command line args
25    command_args = shlex.split(command)
26    # TODO: add support for using arguments that are passed to this command...
27
28    # Make a dictionary of variable name to "SBBlock and SBValue"
29    var_dict = {}
30
31    # Get the deepest most block from the current frame
32    block = frame.GetBlock()
33    # Iterate through the block and all of its parents
34    while block.IsValid():
35        # Get block variables from the current block only
36        block_vars = block.GetVariables(frame, True, True, True, 0)
37        # Iterate through all variables in the current block
38        for block_var in block_vars:
39            # Get the variable name and see if we already have a variable by this name?
40            block_var_name = block_var.GetName()
41            if block_var_name in var_dict:
42                # We already have seen a variable with this name, so it is shadowed
43                shadow_block_and_vars = var_dict[block_var_name]
44                for shadow_block_and_var in shadow_block_and_vars:
45                    print shadow_block_and_var[0], shadow_block_and_var[1]
46                print 'is shadowed by:'
47                print block, block_var
48            # Since we can have multiple shadowed variables, we our variable
49            # name dictionary to have an array or "block + variable" pairs so
50            # We can correctly print out all shadowed variables and whow which
51            # blocks they come from
52            if block_var_name in var_dict:
53                var_dict[block_var_name].append([block, block_var])
54            else:
55                var_dict[block_var_name] = [[block, block_var]]
56        # Get the parent block and continue
57        block = block.GetParent()
58
59
60