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
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 = chain.from_iterable(frame.watches
60            for frame in self.expected_program_state.frames)
61        return set(frame_expects)
62
63    def eval(self, step_collection: DextIR) -> bool:
64        for step in step_collection.steps:
65            if self.expected_program_state.match(step.program_state):
66                self.encounters.append(step.step_index)
67
68        return self.times < 0 < len(self.encounters) or len(self.encounters) == self.times
69