summaryrefslogtreecommitdiffstats
path: root/debuginfo-tests/dexter/dex/tools/run_debugger_internal_/Tool.py
blob: 494b4e1d0a991a8aa9fe48452c8e0da525c75734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# DExTer : Debugging Experience Tester
# ~~~~~~   ~         ~~         ~   ~~
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""This is an internal subtool used to sandbox the communication with a
debugger into a separate process so that any crashes inside the debugger will
not bring down the entire DExTer tool.
"""

import pickle

from dex.debugger import Debuggers
from dex.tools import ToolBase
from dex.utils import Timer
from dex.utils.Exceptions import DebuggerException, Error
from dex.utils.ReturnCode import ReturnCode


class Tool(ToolBase):
    def __init__(self, *args, **kwargs):
        super(Tool, self).__init__(*args, **kwargs)
        self.dextIR = None

    @property
    def name(self):
        return 'DExTer run debugger internal'

    def add_tool_arguments(self, parser, defaults):
        parser.add_argument('dextIR_path', type=str, help='dextIR file')
        parser.add_argument(
            'pickled_options', type=str, help='pickled options file')

    def handle_options(self, defaults):
        with open(self.context.options.dextIR_path, 'rb') as fp:
            self.dextIR = pickle.load(fp)

        with open(self.context.options.pickled_options, 'rb') as fp:
            poptions = pickle.load(fp)
            poptions.working_directory = (
                self.context.options.working_directory[:])
            poptions.unittest = self.context.options.unittest
            poptions.dextIR_path = self.context.options.dextIR_path
            self.context.options = poptions

        Timer.display = self.context.options.time_report

    def go(self) -> ReturnCode:
        options = self.context.options

        with Timer('loading debugger'):
            debugger = Debuggers(self.context).load(options.debugger,
                                                    self.dextIR)
            self.dextIR.debugger = debugger.debugger_info

        with Timer('running debugger'):
            if not debugger.is_available:
                msg = '<d>could not load {}</> ({})\n'.format(
                    debugger.name, debugger.loading_error)
                if options.verbose:
                    msg = '{}\n    {}'.format(
                        msg, '    '.join(debugger.loading_error_trace))
                raise Error(msg)

            with debugger:
                try:
                    debugger.start()
                except DebuggerException as e:
                    raise Error(e)

        with open(self.context.options.dextIR_path, 'wb') as fp:
            pickle.dump(self.dextIR, fp)
        return ReturnCode.OK
OpenPOWER on IntegriCloud