1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""Command for specifying a partial or complete state for the program to enter
8during execution.
9"""
10
11from itertools import chain
12
13from dex.command.CommandBase import CommandBase, StepExpectInfo
14from dex.dextIR import ProgramState, SourceLocation, StackFrame, DextIR
15
16def frame_from_dict(source: dict) -> StackFrame:
17    if 'location' in source:
18        assert isinstance(source['location'], dict)
19        source['location'] = SourceLocation(**source['location'])
20    return StackFrame(**source)
21
22def state_from_dict(source: dict) -> ProgramState:
23    if 'frames' in source:
24        assert isinstance(source['frames'], list)
25        source['frames'] = list(map(frame_from_dict, source['frames']))
26    return ProgramState(**source)
27
28class DexExpectProgramState(CommandBase):
29    """Expect to see a given program `state` a certain numer of `times`.
30
31    DexExpectProgramState(state [,**times])
32
33    See Commands.md for more info.
34    """
35
36    def __init__(self, *args, **kwargs):
37        if len(args) != 1:
38            raise TypeError('expected exactly one unnamed arg')
39
40        self.program_state_text = str(args[0])
41
42        self.expected_program_state = state_from_dict(args[0])
43
44        self.times = kwargs.pop('times', -1)
45        if kwargs:
46            raise TypeError('unexpected named args: {}'.format(
47                ', '.join(kwargs)))
48
49        # Step indices at which the expected program state was encountered.
50        self.encounters = []
51
52        super(DexExpectProgramState, self).__init__()
53
54    @staticmethod
55    def get_name():
56        return __class__.__name__
57
58    def get_watches(self):
59        frame_expects = set()
60        for idx, frame in enumerate(self.expected_program_state.frames):
61            path = (frame.location.path if
62                    frame.location and frame.location.path else self.path)
63            line_range = (
64                range(frame.location.lineno, frame.location.lineno + 1)
65                if frame.location and frame.location.lineno else None)
66            for watch in frame.watches:
67                frame_expects.add(
68                    StepExpectInfo(
69                        expression=watch,
70                        path=path,
71                        frame_idx=idx,
72                        line_range=line_range
73                    )
74                )
75        return frame_expects
76
77    def eval(self, step_collection: DextIR) -> bool:
78        for step in step_collection.steps:
79            if self.expected_program_state.match(step.program_state):
80                self.encounters.append(step.step_index)
81
82        return self.times < 0 < len(self.encounters) or len(self.encounters) == self.times
83