xref: /llvm-project/cross-project-tests/debuginfo-tests/dexter/dex/command/commands/DexDeclareAddress.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
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
15
16class DexDeclareAddress(CommandBase):
17    def __init__(self, addr_name, expression, **kwargs):
18
19        if not isinstance(addr_name, str):
20            raise TypeError("invalid argument type")
21
22        self.addr_name = addr_name
23        self.expression = expression
24        self.on_line = kwargs.pop("on_line")
25        self.hit_count = kwargs.pop("hit_count", 0)
26
27        self.address_resolutions = None
28
29        super(DexDeclareAddress, self).__init__()
30
31    @staticmethod
32    def get_name():
33        return __class__.__name__
34
35    def get_watches(self):
36        return [
37            StepExpectInfo(
38                self.expression, self.path, 0, range(self.on_line, self.on_line + 1)
39            )
40        ]
41
42    def get_address_name(self):
43        return self.addr_name
44
45    def eval(self, step_collection):
46        self.address_resolutions[self.get_address_name()] = None
47        for step in step_collection.steps:
48            loc = step.current_location
49
50            if (
51                loc.path
52                and self.path
53                and PurePath(loc.path) == PurePath(self.path)
54                and loc.lineno == self.on_line
55            ):
56                if self.hit_count > 0:
57                    self.hit_count -= 1
58                    continue
59                try:
60                    watch = step.program_state.frames[0].watches[self.expression]
61                except KeyError:
62                    continue
63                try:
64                    hex_val = int(watch.value, 16)
65                except ValueError:
66                    hex_val = None
67                self.address_resolutions[self.get_address_name()] = hex_val
68                break
69