blob: 0556cfc553cec3aa5b8d0775ba2b61706f6723b6 [file] [log] [blame]
Greg Claytonfe9d1ee2016-06-23 19:54:32 +00001#!/usr/bin/python
2
3import lldb
4import shlex
5
Kate Stoneb9c1b512016-09-06 20:57:50 +00006
Greg Claytonfe9d1ee2016-06-23 19:54:32 +00007@lldb.command("shadow")
Greg Clayton296f1662016-06-28 00:06:35 +00008def check_shadow_command(debugger, command, exe_ctx, result, dict):
9 '''Check the currently selected stack frame for shadowed variables'''
10 process = exe_ctx.GetProcess()
11 state = process.GetState()
12 if state != lldb.eStateStopped:
Kate Stoneb9c1b512016-09-06 20:57:50 +000013 print >>result, "process must be stopped, state is %s" % lldb.SBDebugger.StateAsCString(
14 state)
15 return
Greg Clayton296f1662016-06-28 00:06:35 +000016 frame = exe_ctx.GetFrame()
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000017 if not frame:
18 print >>result, "invalid frame"
19 return
20 # Parse command line args
21 command_args = shlex.split(command)
22 # TODO: add support for using arguments that are passed to this command...
Kate Stoneb9c1b512016-09-06 20:57:50 +000023
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000024 # Make a dictionary of variable name to "SBBlock and SBValue"
Greg Clayton296f1662016-06-28 00:06:35 +000025 shadow_dict = {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000026
Greg Clayton296f1662016-06-28 00:06:35 +000027 num_shadowed_variables = 0
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000028 # Get the deepest most block from the current frame
29 block = frame.GetBlock()
30 # Iterate through the block and all of its parents
31 while block.IsValid():
32 # Get block variables from the current block only
33 block_vars = block.GetVariables(frame, True, True, True, 0)
34 # Iterate through all variables in the current block
35 for block_var in block_vars:
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000036 # Since we can have multiple shadowed variables, we our variable
37 # name dictionary to have an array or "block + variable" pairs so
38 # We can correctly print out all shadowed variables and whow which
39 # blocks they come from
Greg Clayton296f1662016-06-28 00:06:35 +000040 block_var_name = block_var.GetName()
41 if block_var_name in shadow_dict:
42 shadow_dict[block_var_name].append(block_var)
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000043 else:
Greg Clayton296f1662016-06-28 00:06:35 +000044 shadow_dict[block_var_name] = [block_var]
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 # Get the parent block and continue
Greg Claytonfe9d1ee2016-06-23 19:54:32 +000046 block = block.GetParent()
Greg Clayton296f1662016-06-28 00:06:35 +000047
48 num_shadowed_variables = 0
49 if shadow_dict:
50 for name in shadow_dict.keys():
51 shadow_vars = shadow_dict[name]
52 if len(shadow_vars) > 1:
53 print '"%s" is shadowed by the following declarations:' % (name)
54 num_shadowed_variables += 1
55 for shadow_var in shadow_vars:
56 print >>result, str(shadow_var.GetDeclaration())
57 if num_shadowed_variables == 0:
58 print >>result, 'no variables are shadowed'