xref: /llvm-project/cross-project-tests/debuginfo-tests/dexter/dex/tools/view/Tool.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"""View tool."""
8
9import os
10
11import pickle
12from dex.heuristic import Heuristic
13from dex.heuristic.Heuristic import add_heuristic_tool_arguments
14from dex.tools import ToolBase
15from dex.utils.Exceptions import Error, HeuristicException
16from dex.utils.ReturnCode import ReturnCode
17
18
19class Tool(ToolBase):
20    """Given a dextIR file, display the information in a human-readable form."""
21
22    @property
23    def name(self):
24        return "DExTer view"
25
26    def add_tool_arguments(self, parser, defaults):
27        add_heuristic_tool_arguments(parser)
28        parser.add_argument(
29            "input_path",
30            metavar="dextIR-file",
31            type=str,
32            default=None,
33            help="dexter dextIR file to view",
34        )
35        parser.description = Tool.__doc__
36
37    def handle_options(self, defaults):
38        options = self.context.options
39
40        options.input_path = os.path.abspath(options.input_path)
41        if not os.path.isfile(options.input_path):
42            raise Error(
43                '<d>could not find dextIR file</> <r>"{}"</>'.format(options.input_path)
44            )
45
46    def go(self) -> ReturnCode:
47        options = self.context.options
48
49        with open(options.input_path, "rb") as fp:
50            steps = pickle.load(fp)
51
52        try:
53            heuristic = Heuristic(self.context, steps)
54        except HeuristicException as e:
55            raise Error("could not apply heuristic: {}".format(e))
56
57        self.context.o.auto(
58            "{}\n\n{}\n\n{}\n\n".format(
59                heuristic.summary_string, steps, heuristic.verbose_output
60            )
61        )
62
63        return ReturnCode.OK
64