summaryrefslogtreecommitdiff
path: root/cross-project-tests/debuginfo-tests/dexter/dex/debugger/DebuggerBase.py
blob: efb23899b77e5c13ce0ec16cd51fffa04a359690 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# 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
"""Base class for all debugger interface implementations."""

import abc
import os
import sys
import traceback
import unittest

from types import SimpleNamespace
from dex.command.CommandBase import StepExpectInfo
from dex.dextIR import DebuggerIR, FrameIR, LocIR, StepIR, ValueIR
from dex.utils.Exceptions import DebuggerException
from dex.utils.ReturnCode import ReturnCode

def watch_is_active(watch_info: StepExpectInfo, path, frame_idx, line_no):
    _, watch_path, watch_frame_idx, watch_line_range = watch_info
    # If this watch should only be active for a specific file...
    if watch_path and os.path.isfile(watch_path):
        # If the current path does not match the expected file, this watch is
        # not active.
        if not (path and os.path.isfile(path) and
                os.path.samefile(path, watch_path)):
            return False
    if watch_frame_idx != frame_idx:
        return False
    if watch_line_range and line_no not in list(watch_line_range):
        return False
    return True

class DebuggerBase(object, metaclass=abc.ABCMeta):
    def __init__(self, context):
        self.context = context
        # Note: We can't already read values from options
        # as DebuggerBase is created before we initialize options
        # to read potential_debuggers.
        self.options = self.context.options

        self._interface = None
        self.has_loaded = False
        self._loading_error = None
        try:
            self._interface = self._load_interface()
            self.has_loaded = True
        except DebuggerException:
            self._loading_error = sys.exc_info()

    def __enter__(self):
        try:
            self._custom_init()
            self.clear_breakpoints()
        except DebuggerException:
            self._loading_error = sys.exc_info()
        return self

    def __exit__(self, *args):
        self._custom_exit()

    def _custom_init(self):
        pass

    def _custom_exit(self):
        pass

    @property
    def debugger_info(self):
        return DebuggerIR(name=self.name, version=self.version)

    @property
    def is_available(self):
        return self.has_loaded and self.loading_error is None

    @property
    def loading_error(self):
        return (str(self._loading_error[1])
                if self._loading_error is not None else None)

    @property
    def loading_error_trace(self):
        if not self._loading_error:
            return None

        tb = traceback.format_exception(*self._loading_error)

        if self._loading_error[1].orig_exception is not None:
            orig_exception = traceback.format_exception(
                *self._loading_error[1].orig_exception)

            if ''.join(orig_exception) not in ''.join(tb):
                tb.extend(['\n'])
                tb.extend(orig_exception)

        tb = ''.join(tb).splitlines(True)
        return tb

    def _sanitize_function_name(self, name):  # pylint: disable=no-self-use
        """If the function name returned by the debugger needs any post-
        processing to make it fit (for example, if it includes a byte offset),
        do that here.
        """
        return name

    @abc.abstractmethod
    def _load_interface(self):
        pass

    @classmethod
    def get_option_name(cls):
        """Short name that will be used on the command line to specify this
        debugger.
        """
        raise NotImplementedError()

    @classmethod
    def get_name(cls):
        """Full name of this debugger."""
        raise NotImplementedError()

    @property
    def name(self):
        return self.__class__.get_name()

    @property
    def option_name(self):
        return self.__class__.get_option_name()

    @abc.abstractproperty
    def version(self):
        pass

    @abc.abstractmethod
    def clear_breakpoints(self):
        pass

    def add_breakpoint(self, file_, line):
        """Returns a unique opaque breakpoint id.

        The ID type depends on the debugger being used, but will probably be
        an int.
        """
        return self._add_breakpoint(self._external_to_debug_path(file_), line)

    @abc.abstractmethod
    def _add_breakpoint(self, file_, line):
        """Returns a unique opaque breakpoint id.
        """
        pass

    def add_conditional_breakpoint(self, file_, line, condition):
        """Returns a unique opaque breakpoint id.

        The ID type depends on the debugger being used, but will probably be
        an int.
        """
        return self._add_conditional_breakpoint(
            self._external_to_debug_path(file_), line, condition)

    @abc.abstractmethod
    def _add_conditional_breakpoint(self, file_, line, condition):
        """Returns a unique opaque breakpoint id.
        """
        pass

    @abc.abstractmethod
    def delete_breakpoint(self, id):
        """Delete a breakpoint by id.

        Raises a KeyError if no breakpoint with this id exists.
        """
        pass

    @abc.abstractmethod
    def get_triggered_breakpoint_ids(self):
        """Returns a set of opaque ids for just-triggered breakpoints.
        """
        pass

    @abc.abstractmethod
    def launch(self):
        pass

    @abc.abstractmethod
    def step(self):
        pass

    @abc.abstractmethod
    def go(self) -> ReturnCode:
        pass

    def get_step_info(self, watches, step_index):
        step_info = self._get_step_info(watches, step_index)
        for frame in step_info.frames:
            frame.loc.path = self._debug_to_external_path(frame.loc.path)
        return step_info

    @abc.abstractmethod
    def _get_step_info(self, watches, step_index):
        pass

    @abc.abstractproperty
    def is_running(self):
        pass

    @abc.abstractproperty
    def is_finished(self):
        pass

    @abc.abstractproperty
    def frames_below_main(self):
        pass

    @abc.abstractmethod
    def evaluate_expression(self, expression, frame_idx=0) -> ValueIR:
        pass

    def _external_to_debug_path(self, path):
        if not self.options.debugger_use_relative_paths:
            return path
        root_dir = self.options.source_root_dir
        if not root_dir or not path:
            return path
        assert path.startswith(root_dir)
        return path[len(root_dir):].lstrip(os.path.sep)

    def _debug_to_external_path(self, path):
        if not self.options.debugger_use_relative_paths:
            return path
        if not path or not self.options.source_root_dir:
            return path
        for file in self.options.source_files:
            if path.endswith(self._external_to_debug_path(file)):
                return file
        return path

class TestDebuggerBase(unittest.TestCase):

    class MockDebugger(DebuggerBase):

        def __init__(self, context, *args):
            super().__init__(context, *args)
            self.step_info = None
            self.breakpoint_file = None

        def _add_breakpoint(self, file, line):
            self.breakpoint_file = file

        def _get_step_info(self, watches, step_index):
            return self.step_info

    def __init__(self, *args):
        super().__init__(*args)
        TestDebuggerBase.MockDebugger.__abstractmethods__ = set()
        self.options = SimpleNamespace(source_root_dir = '', source_files = [])
        context = SimpleNamespace(options = self.options)
        self.dbg = TestDebuggerBase.MockDebugger(context)

    def _new_step(self, paths):
        frames = [
            FrameIR(
                function=None,
                is_inlined=False,
                loc=LocIR(path=path, lineno=0, column=0)) for path in paths
        ]
        return StepIR(step_index=0, stop_reason=None, frames=frames)

    def _step_paths(self, step):
        return [frame.loc.path for frame in step.frames]

    def test_add_breakpoint_no_source_root_dir(self):
        self.options.debugger_use_relative_paths = True
        self.options.source_root_dir = ''
        path = os.path.join(os.path.sep + 'root', 'some_file')
        self.dbg.add_breakpoint(path, 12)
        self.assertEqual(path, self.dbg.breakpoint_file)

    def test_add_breakpoint_with_source_root_dir(self):
        self.options.debugger_use_relative_paths = True
        self.options.source_root_dir = os.path.sep + 'my_root'
        path = os.path.join(self.options.source_root_dir, 'some_file')
        self.dbg.add_breakpoint(path, 12)
        self.assertEqual('some_file', self.dbg.breakpoint_file)

    def test_add_breakpoint_with_source_root_dir_slash_suffix(self):
        self.options.debugger_use_relative_paths = True
        self.options.source_root_dir = os.path.sep + 'my_root' + os.path.sep
        path = os.path.join(self.options.source_root_dir, 'some_file')
        self.dbg.add_breakpoint(path, 12)
        self.assertEqual('some_file', self.dbg.breakpoint_file)

    def test_get_step_info_no_source_root_dir(self):
        self.options.debugger_use_relative_paths = True
        path = os.path.join(os.path.sep + 'root', 'some_file')
        self.dbg.step_info = self._new_step([path])
        self.assertEqual([path],
            self._step_paths(self.dbg.get_step_info([], 0)))

    def test_get_step_info_no_frames(self):
        self.options.debugger_use_relative_paths = True
        self.options.source_root_dir = os.path.sep + 'my_root'
        self.dbg.step_info = self._new_step([])
        self.assertEqual([],
            self._step_paths(self.dbg.get_step_info([], 0)))

    def test_get_step_info(self):
        self.options.debugger_use_relative_paths = True
        self.options.source_root_dir = os.path.sep + 'my_root'
        path = os.path.join(self.options.source_root_dir, 'some_file')
        self.options.source_files = [path]
        other_path = os.path.join(os.path.sep + 'other', 'file')
        dbg_path = os.path.join(os.path.sep + 'dbg', 'some_file')
        self.dbg.step_info = self._new_step(
            [None, other_path, dbg_path])
        self.assertEqual([None, other_path, path],
            self._step_paths(self.dbg.get_step_info([], 0)))