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"""Commmand sets the path for all following commands to 'declared_file'.
8"""
9
10import os
11from pathlib import PurePath
12
13from dex.command.CommandBase import CommandBase, StepExpectInfo
14
15class DexDeclareAddress(CommandBase):
16    def __init__(self, addr_name, expression, **kwargs):
17
18        if not isinstance(addr_name, str):
19            raise TypeError('invalid argument type')
20
21        self.addr_name = addr_name
22        self.expression = expression
23        self.on_line = kwargs.pop('on_line')
24        self.hit_count = kwargs.pop('hit_count', 0)
25
26        self.address_resolutions = None
27
28        super(DexDeclareAddress, self).__init__()
29
30    @staticmethod
31    def get_name():
32        return __class__.__name__
33
34    def get_watches(self):
35        return [StepExpectInfo(self.expression, self.path, 0, range(self.on_line, self.on_line + 1))]
36
37    def get_address_name(self):
38        return self.addr_name
39
40    def eval(self, step_collection):
41        self.address_resolutions[self.get_address_name()] = None
42        for step in step_collection.steps:
43            loc = step.current_location
44
45            if (loc.path and self.path and
46                PurePath(loc.path) == PurePath(self.path) and
47                loc.lineno == self.on_line):
48                if self.hit_count > 0:
49                    self.hit_count -= 1
50                    continue
51                try:
52                    watch = step.program_state.frames[0].watches[self.expression]
53                except KeyError:
54                    continue
55                try:
56                    hex_val = int(watch.value, 16)
57                except ValueError:
58                    hex_val = None
59                self.address_resolutions[self.get_address_name()] = hex_val
60                break
61