summaryrefslogtreecommitdiff
path: root/cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py
blob: 4ce0142513b650495882fe3d16f10da2ece6046e (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# 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
"""Interface for communicating with the Visual Studio debugger via DTE."""

import abc
import imp
import os
import sys
from pathlib import PurePath
from collections import defaultdict, namedtuple

from dex.command.CommandBase import StepExpectInfo
from dex.debugger.DebuggerBase import DebuggerBase, watch_is_active
from dex.dextIR import FrameIR, LocIR, StepIR, StopReason, ValueIR
from dex.dextIR import StackFrame, SourceLocation, ProgramState
from dex.utils.Exceptions import Error, LoadDebuggerException
from dex.utils.ReturnCode import ReturnCode


def _load_com_module():
    try:
        module_info = imp.find_module(
            'ComInterface',
            [os.path.join(os.path.dirname(__file__), 'windows')])
        return imp.load_module('ComInterface', *module_info)
    except ImportError as e:
        raise LoadDebuggerException(e, sys.exc_info())


# VSBreakpoint(path: PurePath, line: int, col: int, cond: str).  This is enough
# info to identify breakpoint equivalence in visual studio based on the
# properties we set through dexter currently.
VSBreakpoint = namedtuple('VSBreakpoint', 'path, line, col, cond')

class VisualStudio(DebuggerBase, metaclass=abc.ABCMeta):  # pylint: disable=abstract-method

    # Constants for results of Debugger.CurrentMode
    # (https://msdn.microsoft.com/en-us/library/envdte.debugger.currentmode.aspx)
    dbgDesignMode = 1
    dbgBreakMode = 2
    dbgRunMode = 3

    def __init__(self, *args):
        self.com_module = None
        self._debugger = None
        self._solution = None
        self._fn_step = None
        self._fn_go = None
        # The next available unique breakpoint id. Use self._get_next_id().
        self._next_bp_id = 0
        # VisualStudio appears to common identical breakpoints. That is, if you
        # ask for a breakpoint that already exists the Breakpoints list will
        # not grow. DebuggerBase requires all breakpoints have a unique id,
        # even for duplicates, so we'll need to do some bookkeeping.  Map
        # {VSBreakpoint: list(id)} where id is the unique dexter-side id for
        # the requested breakpoint.
        self._vs_to_dex_ids = defaultdict(list)
        # Map {id: VSBreakpoint} where id is unique and VSBreakpoint identifies
        # a breakpoint in Visual Studio. There may be many ids mapped to a
        # single VSBreakpoint. Use self._vs_to_dex_ids to find (dexter)
        # breakpoints mapped to the same visual studio breakpoint.
        self._dex_id_to_vs = {}

        super(VisualStudio, self).__init__(*args)

    def _create_solution(self):
        self._solution.Create(self.context.working_directory.path,
                              'DexterSolution')
        try:
            self._solution.AddFromFile(self._project_file)
        except OSError:
            raise LoadDebuggerException(
                'could not debug the specified executable', sys.exc_info())

    def _load_solution(self):
        try:
            self._solution.Open(self.context.options.vs_solution)
        except:
            raise LoadDebuggerException(
                    'could not load specified vs solution at {}'.
                    format(self.context.options.vs_solution), sys.exc_info())

    def _custom_init(self):
        try:
            self._debugger = self._interface.Debugger
            self._debugger.HexDisplayMode = False

            self._interface.MainWindow.Visible = (
                self.context.options.show_debugger)

            self._solution = self._interface.Solution
            if self.context.options.vs_solution is None:
                self._create_solution()
            else:
                self._load_solution()

            self._fn_step = self._debugger.StepInto
            self._fn_go = self._debugger.Go

        except AttributeError as e:
            raise LoadDebuggerException(str(e), sys.exc_info())

    def _custom_exit(self):
        if self._interface:
            self._interface.Quit()

    @property
    def _project_file(self):
        return self.context.options.executable

    @abc.abstractproperty
    def _dte_version(self):
        pass

    @property
    def _location(self):
        #TODO: Find a better way of determining path, line and column info
        # that doesn't require reading break points. This method requires
        # all lines to have a break point on them.
        bp = self._debugger.BreakpointLastHit
        return {
            'path': getattr(bp, 'File', None),
            'lineno': getattr(bp, 'FileLine', None),
            'column': getattr(bp, 'FileColumn', None)
        }

    @property
    def _mode(self):
        return self._debugger.CurrentMode

    def _load_interface(self):
        self.com_module = _load_com_module()
        return self.com_module.DTE(self._dte_version)

    @property
    def version(self):
        try:
            return self._interface.Version
        except AttributeError:
            return None

    def clear_breakpoints(self):
        for bp in self._debugger.Breakpoints:
            bp.Delete()
        self._vs_to_dex_ids.clear()
        self._dex_id_to_vs.clear()

    def _add_breakpoint(self, file_, line):
        return self._add_conditional_breakpoint(file_, line, '')

    def _get_next_id(self):
        # "Generate" a new unique id for the breakpoint.
        id = self._next_bp_id
        self._next_bp_id += 1
        return id

    def _add_conditional_breakpoint(self, file_, line, condition):
        col = 1
        vsbp = VSBreakpoint(PurePath(file_), line, col, condition)
        new_id = self._get_next_id()

        # Do we have an exact matching breakpoint already?
        if vsbp in self._vs_to_dex_ids:
            self._vs_to_dex_ids[vsbp].append(new_id)
            self._dex_id_to_vs[new_id] = vsbp
            return new_id

        # Breakpoint doesn't exist already. Add it now.
        count_before = self._debugger.Breakpoints.Count
        self._debugger.Breakpoints.Add('', file_, line, col, condition)
        # Our internal representation of VS says that the breakpoint doesn't
        # already exist so we do not expect this operation to fail here.
        assert count_before < self._debugger.Breakpoints.Count
        # We've added a new breakpoint, record its id.
        self._vs_to_dex_ids[vsbp].append(new_id)
        self._dex_id_to_vs[new_id] = vsbp
        return new_id

    def get_triggered_breakpoint_ids(self):
        """Returns a set of opaque ids for just-triggered breakpoints.
        """
        bps_hit = self._debugger.AllBreakpointsLastHit
        bp_id_list = []
        # Intuitively, AllBreakpointsLastHit breakpoints are the last hit
        # _bound_ breakpoints. A bound breakpoint's parent holds the info of
        # the breakpoint the user requested. Our internal state tracks the user
        # requested breakpoints so we look at the Parent of these triggered
        # breakpoints to determine which have been hit.
        for bp in bps_hit:
            # All bound breakpoints should have the user-defined breakpoint as
            # a parent.
            assert bp.Parent
            vsbp = VSBreakpoint(PurePath(bp.Parent.File), bp.Parent.FileLine,
                                bp.Parent.FileColumn, bp.Parent.Condition)
            try:
                ids = self._vs_to_dex_ids[vsbp]
            except KeyError:
                pass
            else:
                bp_id_list += ids
        return set(bp_id_list)

    def delete_breakpoints(self, ids):
        """Delete breakpoints by their ids.

        Raises a KeyError if no breakpoint with this id exists.
        """
        vsbp_set = set()
        for id in ids:
            vsbp = self._dex_id_to_vs[id]

            # Remove our id from the associated list of dex ids.
            self._vs_to_dex_ids[vsbp].remove(id)
            del self._dex_id_to_vs[id]

            # Bail if there are other uses of this vsbp.
            if len(self._vs_to_dex_ids[vsbp]) > 0:
                continue
            # Otherwise find and delete it.
            vsbp_set.add(vsbp)

        vsbp_to_del_count = len(vsbp_set)

        for bp in self._debugger.Breakpoints:
            # We're looking at the user-set breakpoints so there should be no
            # Parent.
            assert bp.Parent == None
            this_vsbp = VSBreakpoint(PurePath(bp.File), bp.FileLine,
                                     bp.FileColumn, bp.Condition)
            if this_vsbp in vsbp_set:
                bp.Delete()
                vsbp_to_del_count -= 1
                if vsbp_to_del_count == 0:
                    break
        if vsbp_to_del_count:
            raise KeyError('did not find breakpoint to be deleted')

    def _fetch_property(self, props, name):
        num_props = props.Count
        result = None
        for x in range(1, num_props+1):
            item = props.Item(x)
            if item.Name == name:
                return item
        assert False, "Couldn't find property {}".format(name)

    def launch(self, cmdline):
        cmdline_str = ' '.join(cmdline)
        if self.context.options.target_run_args:
          cmdline_str += f" {self.context.options.target_run_args}"

        # In a slightly baroque manner, lookup the VS project that runs when
        # you click "run", and set its command line options to the desired
        # command line options.
        startup_proj_name = str(self._fetch_property(self._interface.Solution.Properties, 'StartupProject'))
        project = self._fetch_property(self._interface.Solution, startup_proj_name)
        ActiveConfiguration = self._fetch_property(project.Properties, 'ActiveConfiguration').Object
        ActiveConfiguration.DebugSettings.CommandArguments = cmdline_str

        self._fn_go(False)

    def step(self):
        self._fn_step(False)

    def go(self) -> ReturnCode:
        self._fn_go(False)
        return ReturnCode.OK

    def set_current_stack_frame(self, idx: int = 0):
        thread = self._debugger.CurrentThread
        stack_frames = thread.StackFrames
        try:
            stack_frame = stack_frames[idx]
            self._debugger.CurrentStackFrame = stack_frame.raw
        except IndexError:
            raise Error('attempted to access stack frame {} out of {}'
                .format(idx, len(stack_frames)))

    def _get_step_info(self, watches, step_index):
        thread = self._debugger.CurrentThread
        stackframes = thread.StackFrames

        frames = []
        state_frames = []


        loc = LocIR(**self._location)
        valid_loc_for_watch = loc.path and os.path.exists(loc.path)

        for idx, sf in enumerate(stackframes):
            frame = FrameIR(
                function=self._sanitize_function_name(sf.FunctionName),
                is_inlined=sf.FunctionName.startswith('[Inline Frame]'),
                loc=LocIR(path=None, lineno=None, column=None))

            fname = frame.function or ''  # pylint: disable=no-member
            if any(name in fname for name in self.frames_below_main):
                break

            state_frame = StackFrame(function=frame.function,
                                     is_inlined=frame.is_inlined,
                                     watches={})

            if valid_loc_for_watch and idx == 0:
                for watch_info in watches:
                    if watch_is_active(watch_info, loc.path, idx, loc.lineno):
                        watch_expr = watch_info.expression
                        state_frame.watches[watch_expr] = self.evaluate_expression(watch_expr, idx)


            state_frames.append(state_frame)
            frames.append(frame)

        if frames:
            frames[0].loc = loc
            state_frames[0].location = SourceLocation(**self._location)

        reason = StopReason.BREAKPOINT
        if loc.path is None:  # pylint: disable=no-member
            reason = StopReason.STEP

        program_state = ProgramState(frames=state_frames)

        return StepIR(
            step_index=step_index, frames=frames, stop_reason=reason,
            program_state=program_state)

    @property
    def is_running(self):
        return self._mode == VisualStudio.dbgRunMode

    @property
    def is_finished(self):
        return self._mode == VisualStudio.dbgDesignMode

    @property
    def frames_below_main(self):
        return [
            '[Inline Frame] invoke_main', '__scrt_common_main_seh',
            '__tmainCRTStartup', 'mainCRTStartup'
        ]

    def evaluate_expression(self, expression, frame_idx=0) -> ValueIR:
        if frame_idx != 0:
            self.set_current_stack_frame(frame_idx)
        result = self._debugger.GetExpression(expression)
        if frame_idx != 0:
            self.set_current_stack_frame(0)
        value = result.Value

        is_optimized_away = any(s in value for s in [
            'Variable is optimized away and not available',
            'Value is not available, possibly due to optimization',
        ])

        is_irretrievable = any(s in value for s in [
            '???',
            '<Unable to read memory>',
        ])

        # an optimized away value is still counted as being able to be
        # evaluated.
        could_evaluate = (result.IsValidValue or is_optimized_away
                          or is_irretrievable)

        return ValueIR(
            expression=expression,
            value=value,
            type_name=result.Type,
            error_string=None,
            is_optimized_away=is_optimized_away,
            could_evaluate=could_evaluate,
            is_irretrievable=is_irretrievable,
        )