diff options
Diffstat (limited to 'debuginfo-tests/dexter/dex/command/commands')
9 files changed, 525 insertions, 0 deletions
diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectProgramState.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectProgramState.py new file mode 100644 index 00000000000..78335838a65 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectProgramState.py @@ -0,0 +1,83 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command for specifying a partial or complete state for the program to enter +during execution. +""" + +from itertools import chain + +from dex.command.CommandBase import CommandBase +from dex.dextIR import ProgramState, SourceLocation, StackFrame, DextIR + +def frame_from_dict(source: dict) -> StackFrame: + if 'location' in source: + assert isinstance(source['location'], dict) + source['location'] = SourceLocation(**source['location']) + return StackFrame(**source) + +def state_from_dict(source: dict) -> ProgramState: + if 'frames' in source: + assert isinstance(source['frames'], list) + source['frames'] = list(map(frame_from_dict, source['frames'])) + return ProgramState(**source) + +class DexExpectProgramState(CommandBase): + """Expect to see a given program `state` a certain numer of `times`. + + DexExpectProgramState(state [,**times]) + + See Commands.md for more info. + """ + + def __init__(self, *args, **kwargs): + if len(args) != 1: + raise TypeError('expected exactly one unnamed arg') + + self.program_state_text = str(args[0]) + + self.expected_program_state = state_from_dict(args[0]) + + self.times = kwargs.pop('times', -1) + if kwargs: + raise TypeError('unexpected named args: {}'.format( + ', '.join(kwargs))) + + # Step indices at which the expected program state was encountered. + self.encounters = [] + + super(DexExpectProgramState, self).__init__() + + @staticmethod + def get_name(): + return __class__.__name__ + + def get_watches(self): + frame_expects = chain.from_iterable(frame.watches + for frame in self.expected_program_state.frames) + return set(frame_expects) + + def eval(self, step_collection: DextIR) -> bool: + for step in step_collection.steps: + if self.expected_program_state.match(step.program_state): + self.encounters.append(step.step_index) + + return self.times < 0 < len(self.encounters) or len(self.encounters) == self.times + + def has_labels(self): + return len(self.get_label_args()) > 0 + + def get_label_args(self): + return [frame.location.lineno + for frame in self.expected_program_state.frames + if frame.location and + isinstance(frame.location.lineno, str)] + + def resolve_label(self, label_line__pair): + label, line = label_line__pair + for frame in self.expected_program_state.frames: + if frame.location and frame.location.lineno == label: + frame.location.lineno = line diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectStepKind.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectStepKind.py new file mode 100644 index 00000000000..6370f5d32c7 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectStepKind.py @@ -0,0 +1,45 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command for specifying an expected number of steps of a particular kind.""" + +from dex.command.CommandBase import CommandBase +from dex.dextIR.StepIR import StepKind + + +class DexExpectStepKind(CommandBase): + """Expect to see a particular step `kind` a number of `times` while stepping + through the program. + + DexExpectStepKind(kind, times) + + See Commands.md for more info. + """ + + def __init__(self, *args): + if len(args) != 2: + raise TypeError('expected two args') + + try: + step_kind = StepKind[args[0]] + except KeyError: + raise TypeError('expected arg 0 to be one of {}'.format( + [kind for kind, _ in StepKind.__members__.items()])) + + self.name = step_kind + self.count = args[1] + + super(DexExpectStepKind, self).__init__() + + @staticmethod + def get_name(): + return __class__.__name__ + + def eval(self): + # DexExpectStepKind eval() implementation is mixed into + # Heuristic.__init__() + # [TODO] Fix this ^. + pass diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectStepOrder.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectStepOrder.py new file mode 100644 index 00000000000..4342bc5e80b --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectStepOrder.py @@ -0,0 +1,39 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +from dex.command.CommandBase import CommandBase +from dex.dextIR import ValueIR + +class DexExpectStepOrder(CommandBase): + """Expect the line every `DexExpectStepOrder` is found on to be stepped on + in `order`. Each instance must have a set of unique ascending indicies. + + DexExpectStepOrder(*order) + + See Commands.md for more info. + """ + + def __init__(self, *args): + if not args: + raise TypeError('Need at least one order number') + + self.sequence = [int(x) for x in args] + super(DexExpectStepOrder, self).__init__() + + @staticmethod + def get_name(): + return __class__.__name__ + + def eval(self, debugger): + step_info = debugger.get_step_info() + loc = step_info.current_location + return {'DexExpectStepOrder': ValueIR(expression=str(loc.lineno), + value=str(debugger.step_index), type_name=None, + error_string=None, + could_evaluate=True, + is_optimized_away=True, + is_irretrievable=False)} diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchBase.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchBase.py new file mode 100644 index 00000000000..e6422d14098 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchBase.py @@ -0,0 +1,197 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +"""DexExpectWatch base class, holds logic for how to build and process expected + watch commands. +""" + +import abc +import difflib +import os + +from dex.command.CommandBase import CommandBase +from dex.command.StepValueInfo import StepValueInfo + + +class DexExpectWatchBase(CommandBase): + def __init__(self, *args, **kwargs): + if len(args) < 2: + raise TypeError('expected at least two args') + + self.expression = args[0] + self.values = [str(arg) for arg in args[1:]] + try: + on_line = kwargs.pop('on_line') + self._from_line = on_line + self._to_line = on_line + except KeyError: + self._from_line = kwargs.pop('from_line', 1) + self._to_line = kwargs.pop('to_line', 999999) + self._require_in_order = kwargs.pop('require_in_order', True) + if kwargs: + raise TypeError('unexpected named args: {}'.format( + ', '.join(kwargs))) + + # Number of times that this watch has been encountered. + self.times_encountered = 0 + + # We'll pop from this set as we encounter values so anything left at + # the end can be considered as not having been seen. + self._missing_values = set(self.values) + + self.misordered_watches = [] + + # List of StepValueInfos for any watch that is encountered as invalid. + self.invalid_watches = [] + + # List of StepValueInfo any any watch where we couldn't retrieve its + # data. + self.irretrievable_watches = [] + + # List of StepValueInfos for any watch that is encountered as having + # been optimized out. + self.optimized_out_watches = [] + + # List of StepValueInfos for any watch that is encountered that has an + # expected value. + self.expected_watches = [] + + # List of StepValueInfos for any watch that is encountered that has an + # unexpected value. + self.unexpected_watches = [] + + super(DexExpectWatchBase, self).__init__() + + + def get_watches(self): + return [self.expression] + + @property + def line_range(self): + return list(range(self._from_line, self._to_line + 1)) + + @property + def missing_values(self): + return sorted(list(self._missing_values)) + + @property + def encountered_values(self): + return sorted(list(set(self.values) - self._missing_values)) + + + def resolve_label(self, label_line_pair): + # from_line and to_line could have the same label. + label, lineno = label_line_pair + if self._to_line == label: + self._to_line = lineno + if self._from_line == label: + self._from_line = lineno + + def has_labels(self): + return len(self.get_label_args()) > 0 + + def get_label_args(self): + return [label for label in (self._from_line, self._to_line) + if isinstance(label, str)] + + @abc.abstractmethod + def _get_expected_field(self, watch): + """Return a field from watch that this ExpectWatch command is checking. + """ + + def _handle_watch(self, step_info): + self.times_encountered += 1 + + if not step_info.watch_info.could_evaluate: + self.invalid_watches.append(step_info) + return + + if step_info.watch_info.is_optimized_away: + self.optimized_out_watches.append(step_info) + return + + if step_info.watch_info.is_irretrievable: + self.irretrievable_watches.append(step_info) + return + + if step_info.expected_value not in self.values: + self.unexpected_watches.append(step_info) + return + + self.expected_watches.append(step_info) + try: + self._missing_values.remove(step_info.expected_value) + except KeyError: + pass + + def _check_watch_order(self, actual_watches, expected_values): + """Use difflib to figure out whether the values are in the expected order + or not. + """ + differences = [] + actual_values = [w.expected_value for w in actual_watches] + value_differences = list(difflib.Differ().compare(actual_values, + expected_values)) + + missing_value = False + index = 0 + for vd in value_differences: + kind = vd[0] + if kind == '+': + # A value that is encountered in the expected list but not in the + # actual list. We'll keep a note that something is wrong and flag + # the next value that matches as misordered. + missing_value = True + elif kind == ' ': + # This value is as expected. It might still be wrong if we've + # previously encountered a value that is in the expected list but + # not the actual list. + if missing_value: + missing_value = False + differences.append(actual_watches[index]) + index += 1 + elif kind == '-': + # A value that is encountered in the actual list but not the + # expected list. + differences.append(actual_watches[index]) + index += 1 + else: + assert False, 'unexpected diff:{}'.format(vd) + + return differences + + def eval(self, step_collection): + for step in step_collection.steps: + loc = step.current_location + + if (os.path.exists(loc.path) and os.path.exists(self.path) and + os.path.samefile(loc.path, self.path) and + loc.lineno in self.line_range): + try: + watch = step.program_state.frames[0].watches[self.expression] + except KeyError: + pass + else: + expected_field = self._get_expected_field(watch) + step_info = StepValueInfo(step.step_index, watch, + expected_field) + self._handle_watch(step_info) + + if self._require_in_order: + # A list of all watches where the value has changed. + value_change_watches = [] + prev_value = None + for watch in self.expected_watches: + if watch.expected_value != prev_value: + value_change_watches.append(watch) + prev_value = watch.expected_value + + self.misordered_watches = self._check_watch_order( + value_change_watches, [ + v for v in self.values if v in + [w.expected_value for w in self.expected_watches] + ]) diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchType.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchType.py new file mode 100644 index 00000000000..f2336de4828 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchType.py @@ -0,0 +1,26 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command for specifying an expected set of types for a particular watch.""" + + +from dex.command.commands.DexExpectWatchBase import DexExpectWatchBase + +class DexExpectWatchType(DexExpectWatchBase): + """Expect the expression `expr` to evaluate be evaluated and have each + evaluation's type checked against the list of `types`. + + DexExpectWatchType(expr, *types [,**from_line=1][,**to_line=Max] + [,**on_line]) + + See Commands.md for more info. + """ + @staticmethod + def get_name(): + return __class__.__name__ + + def _get_expected_field(self, watch): + return watch.type_name diff --git a/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchValue.py b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchValue.py new file mode 100644 index 00000000000..d6da006ee8c --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexExpectWatchValue.py @@ -0,0 +1,27 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command for specifying an expected set of values for a particular watch.""" + + +from dex.command.commands.DexExpectWatchBase import DexExpectWatchBase + +class DexExpectWatchValue(DexExpectWatchBase): + """Expect the expression `expr` to evaluate to the list of `values` + sequentially. + + DexExpectWatchValue(expr, *values [,**from_line=1][,**to_line=Max] + [,**on_line]) + + See Commands.md for more info. + """ + + @staticmethod + def get_name(): + return __class__.__name__ + + def _get_expected_field(self, watch): + return watch.value diff --git a/debuginfo-tests/dexter/dex/command/commands/DexLabel.py b/debuginfo-tests/dexter/dex/command/commands/DexLabel.py new file mode 100644 index 00000000000..8a0325e6634 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexLabel.py @@ -0,0 +1,31 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command used to give a line in a test a named psuedonym. Every DexLabel has + a line number and Label string component. +""" + +from dex.command.CommandBase import CommandBase + + +class DexLabel(CommandBase): + def __init__(self, label): + + if not isinstance(label, str): + raise TypeError('invalid argument type') + + self._label = label + super(DexLabel, self).__init__() + + def get_as_pair(self): + return (self._label, self.lineno) + + @staticmethod + def get_name(): + return __class__.__name__ + + def eval(self): + return self._label diff --git a/debuginfo-tests/dexter/dex/command/commands/DexUnreachable.py b/debuginfo-tests/dexter/dex/command/commands/DexUnreachable.py new file mode 100644 index 00000000000..188a5d8180d --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexUnreachable.py @@ -0,0 +1,38 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + + +from dex.command.CommandBase import CommandBase +from dex.dextIR import ValueIR + + +class DexUnreachable(CommandBase): + """Expect the source line this is found on will never be stepped on to. + + DexUnreachable() + + See Commands.md for more info. + """ + + def __init(self): + super(DexUnreachable, self).__init__() + pass + + @staticmethod + def get_name(): + return __class__.__name__ + + def eval(self, debugger): + # If we're ever called, at all, then we're evaluating a line that has + # been marked as unreachable. Which means a failure. + vir = ValueIR(expression="Unreachable", + value="True", type_name=None, + error_string=None, + could_evaluate=True, + is_optimized_away=True, + is_irretrievable=False) + return {'DexUnreachable' : vir} diff --git a/debuginfo-tests/dexter/dex/command/commands/DexWatch.py b/debuginfo-tests/dexter/dex/command/commands/DexWatch.py new file mode 100644 index 00000000000..2dfa3a36fb3 --- /dev/null +++ b/debuginfo-tests/dexter/dex/command/commands/DexWatch.py @@ -0,0 +1,39 @@ +# DExTer : Debugging Experience Tester +# ~~~~~~ ~ ~~ ~ ~~ +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Command to instruct the debugger to inspect the value of some set of +expressions on the current source line. +""" + +from dex.command.CommandBase import CommandBase + + +class DexWatch(CommandBase): + """[Deprecated] Evaluate each given `expression` when the debugger steps onto the + line this command is found on + + DexWatch(*expressions) + + See Commands.md for more info. + """ + + def __init__(self, *args): + if not args: + raise TypeError('expected some arguments') + + for arg in args: + if not isinstance(arg, str): + raise TypeError('invalid argument type') + + self._args = args + super(DexWatch, self).__init__() + + @staticmethod + def get_name(): + return __class__.__name__ + + def eval(self, debugger): + return {arg: debugger.evaluate_expression(arg) for arg in self._args} |