1#!/usr/bin/python 2 3import lldb 4import shlex 5 6@lldb.command("shadow") 7def check_shadow_command(debugger, command, exe_ctx, result, dict): 8 '''Check the currently selected stack frame for shadowed variables''' 9 process = exe_ctx.GetProcess() 10 state = process.GetState() 11 if state != lldb.eStateStopped: 12 print >>result, "process must be stopped, state is %s" % lldb.SBDebugger.StateAsCString(state) 13 return 14 frame = exe_ctx.GetFrame() 15 if not frame: 16 print >>result, "invalid frame" 17 return 18 # Parse command line args 19 command_args = shlex.split(command) 20 # TODO: add support for using arguments that are passed to this command... 21 22 # Make a dictionary of variable name to "SBBlock and SBValue" 23 shadow_dict = {} 24 25 num_shadowed_variables = 0 26 # Get the deepest most block from the current frame 27 block = frame.GetBlock() 28 # Iterate through the block and all of its parents 29 while block.IsValid(): 30 # Get block variables from the current block only 31 block_vars = block.GetVariables(frame, True, True, True, 0) 32 # Iterate through all variables in the current block 33 for block_var in block_vars: 34 # Since we can have multiple shadowed variables, we our variable 35 # name dictionary to have an array or "block + variable" pairs so 36 # We can correctly print out all shadowed variables and whow which 37 # blocks they come from 38 block_var_name = block_var.GetName() 39 if block_var_name in shadow_dict: 40 shadow_dict[block_var_name].append(block_var) 41 else: 42 shadow_dict[block_var_name] = [block_var] 43 # Get the parent block and continue 44 block = block.GetParent() 45 46 num_shadowed_variables = 0 47 if shadow_dict: 48 for name in shadow_dict.keys(): 49 shadow_vars = shadow_dict[name] 50 if len(shadow_vars) > 1: 51 print '"%s" is shadowed by the following declarations:' % (name) 52 num_shadowed_variables += 1 53 for shadow_var in shadow_vars: 54 print >>result, str(shadow_var.GetDeclaration()) 55 if num_shadowed_variables == 0: 56 print >>result, 'no variables are shadowed' 57 58