2016-06-24 03:54:32 +08:00
|
|
|
#!/usr/bin/python
|
2019-03-22 02:27:40 +08:00
|
|
|
from __future__ import print_function
|
2016-06-24 03:54:32 +08:00
|
|
|
|
|
|
|
import lldb
|
|
|
|
import shlex
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-06-24 03:54:32 +08:00
|
|
|
@lldb.command("shadow")
|
2016-06-28 08:06:35 +08:00
|
|
|
def check_shadow_command(debugger, command, exe_ctx, result, dict):
|
|
|
|
'''Check the currently selected stack frame for shadowed variables'''
|
|
|
|
process = exe_ctx.GetProcess()
|
|
|
|
state = process.GetState()
|
|
|
|
if state != lldb.eStateStopped:
|
2019-03-22 02:27:40 +08:00
|
|
|
print("process must be stopped, state is %s" % lldb.SBDebugger.StateAsCString(
|
|
|
|
state), file=result)
|
2016-06-28 08:06:35 +08:00
|
|
|
return
|
|
|
|
frame = exe_ctx.GetFrame()
|
2016-06-24 03:54:32 +08:00
|
|
|
if not frame:
|
2019-03-22 02:27:40 +08:00
|
|
|
print("invalid frame", file=result)
|
2016-06-24 03:54:32 +08:00
|
|
|
return
|
|
|
|
# Parse command line args
|
|
|
|
command_args = shlex.split(command)
|
|
|
|
# TODO: add support for using arguments that are passed to this command...
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-06-24 03:54:32 +08:00
|
|
|
# Make a dictionary of variable name to "SBBlock and SBValue"
|
2016-06-28 08:06:35 +08:00
|
|
|
shadow_dict = {}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-06-28 08:06:35 +08:00
|
|
|
num_shadowed_variables = 0
|
2016-06-24 03:54:32 +08:00
|
|
|
# Get the deepest most block from the current frame
|
|
|
|
block = frame.GetBlock()
|
|
|
|
# Iterate through the block and all of its parents
|
|
|
|
while block.IsValid():
|
|
|
|
# Get block variables from the current block only
|
|
|
|
block_vars = block.GetVariables(frame, True, True, True, 0)
|
|
|
|
# Iterate through all variables in the current block
|
|
|
|
for block_var in block_vars:
|
|
|
|
# Since we can have multiple shadowed variables, we our variable
|
|
|
|
# name dictionary to have an array or "block + variable" pairs so
|
|
|
|
# We can correctly print out all shadowed variables and whow which
|
|
|
|
# blocks they come from
|
2016-06-28 08:06:35 +08:00
|
|
|
block_var_name = block_var.GetName()
|
|
|
|
if block_var_name in shadow_dict:
|
|
|
|
shadow_dict[block_var_name].append(block_var)
|
2016-06-24 03:54:32 +08:00
|
|
|
else:
|
2016-06-28 08:06:35 +08:00
|
|
|
shadow_dict[block_var_name] = [block_var]
|
2016-06-24 03:54:32 +08:00
|
|
|
# Get the parent block and continue
|
|
|
|
block = block.GetParent()
|
2016-06-28 08:06:35 +08:00
|
|
|
|
|
|
|
num_shadowed_variables = 0
|
|
|
|
if shadow_dict:
|
|
|
|
for name in shadow_dict.keys():
|
|
|
|
shadow_vars = shadow_dict[name]
|
|
|
|
if len(shadow_vars) > 1:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('"%s" is shadowed by the following declarations:' % (name))
|
2016-06-28 08:06:35 +08:00
|
|
|
num_shadowed_variables += 1
|
|
|
|
for shadow_var in shadow_vars:
|
2019-03-22 02:27:40 +08:00
|
|
|
print(str(shadow_var.GetDeclaration()), file=result)
|
2016-06-28 08:06:35 +08:00
|
|
|
if num_shadowed_variables == 0:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('no variables are shadowed', file=result)
|