aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Breakpoint.cs
blob: 5aefbf294afc8567fef0c3d8a8f677c5cf2fbcfb (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;

namespace QtVsTools.Qml.Debug.AD7
{
    sealed partial class PendingBreakpoint : Disposable,

        IDebugPendingBreakpoint2 // "This interface represents a breakpoint that is ready to bind
                                 //  to a code location."
    {
        static readonly string[] ValidExtensions = new string[] { ".qml", ".js" };

        public QmlEngine Engine { get; private set; }
        private IDebugBreakpointRequest2 Request { get; set; }
        private enum_BP_LOCATION_TYPE LocationType { get; set; }
        private BP_REQUEST_INFO RequestInfo { get; set; }
        public string FileName { get; private set; }
        public TEXT_POSITION BeginPosition { get; private set; }
        private TEXT_POSITION EndPosition { get; set; }
        public bool Enabled { get; private set; }

        HashSet<Breakpoint> breakpoints;

        public static PendingBreakpoint Create(QmlEngine engine, IDebugBreakpointRequest2 request)
        {
            var _this = new PendingBreakpoint();
            return _this.Initialize(engine, request) ? _this : null;
        }

        private PendingBreakpoint()
        { }

        private bool Initialize(QmlEngine engine, IDebugBreakpointRequest2 request)
        {
            var locationType = new enum_BP_LOCATION_TYPE[1];
            if (request.GetLocationType(locationType) != VSConstants.S_OK)
                return false;

            var requestInfo = new BP_REQUEST_INFO[1];
            if (request.GetRequestInfo(enum_BPREQI_FIELDS.BPREQI_ALLFIELDS, requestInfo)
                != VSConstants.S_OK) {
                return false;
            }

            if (requestInfo[0].bpLocation.bpLocationType
                != (uint)enum_BP_LOCATION_TYPE.BPLT_CODE_FILE_LINE) {
                return false;
            }

            var docPosition = Marshal.GetObjectForIUnknown(requestInfo[0].bpLocation.unionmember2)
                as IDebugDocumentPosition2;
            if (docPosition == null)
                return false;

            string fileName;
            if (docPosition.GetFileName(out fileName) != VSConstants.S_OK)
                return false;

            if (!ValidExtensions.Where(x => string.Equals(x, Path.GetExtension(fileName))).Any())
                return false;

            TEXT_POSITION[] beginPosition = new TEXT_POSITION[1];
            TEXT_POSITION[] endPosition = new TEXT_POSITION[1];
            if (docPosition.GetRange(beginPosition, endPosition) != VSConstants.S_OK)
                return false;

            Engine = engine;
            Request = request;
            LocationType = locationType[0];
            RequestInfo = requestInfo[0];
            FileName = fileName;
            BeginPosition = beginPosition[0];
            EndPosition = endPosition[0];

            breakpoints = new HashSet<Breakpoint>();

            return true;
        }

        protected override void DisposeManaged()
        {
            foreach (var breakpoint in ThreadSafe(() => breakpoints.ToList()))
                breakpoint.Dispose();

            ThreadSafe(() => breakpoints.Clear());
        }

        public void DisposeBreakpoint(Breakpoint breakpoint)
        {
            ThreadSafe(() => breakpoints.Remove(breakpoint));
            breakpoint.Dispose();
        }

        int IDebugPendingBreakpoint2.Bind()
        {
            foreach (var program in Engine.Programs) {
                var breakpoint = Breakpoint.Create(this, program);
                ThreadSafe(() => breakpoints.Add(breakpoint));
                program.SetBreakpoint(breakpoint);
            }
            return VSConstants.S_OK;
        }

        int IDebugPendingBreakpoint2.Enable(int fEnable)
        {
            bool enable = (fEnable != 0);
            if (Atomic(() => Enabled != enable, () => Enabled = enable)) {
                foreach (var breakpoint in ThreadSafe(() => breakpoints.ToList()))
                    (breakpoint as IDebugBoundBreakpoint2).Enable(fEnable);
            }
            return VSConstants.S_OK;
        }

        int IDebugPendingBreakpoint2.Delete()
        {
            Engine.DisposePendingBreakpoint(this);
            return VSConstants.S_OK;
        }

        int IDebugPendingBreakpoint2.EnumBoundBreakpoints(out IEnumDebugBoundBreakpoints2 ppEnum)
        {
            ppEnum = BoundBreakpointsEnum.Create(ThreadSafe(() => breakpoints.ToList()));
            return VSConstants.S_OK;
        }

        int IDebugPendingBreakpoint2.GetState(PENDING_BP_STATE_INFO[] pState)
        {
            if (Disposed) {
                pState[0].state = (enum_PENDING_BP_STATE)enum_BP_STATE.BPS_DELETED;

            } else if (Enabled) {
                pState[0].state = (enum_PENDING_BP_STATE)enum_BP_STATE.BPS_ENABLED;

            } else {
                pState[0].state = (enum_PENDING_BP_STATE)enum_BP_STATE.BPS_DISABLED;
            }

            return VSConstants.S_OK;
        }

        int IDebugPendingBreakpoint2.GetBreakpointRequest(out IDebugBreakpointRequest2 ppBPRequest)
        {
            ppBPRequest = Request;
            return VSConstants.S_OK;
        }
    }

    sealed partial class Breakpoint : Disposable, IBreakpoint,

        IDebugBoundBreakpoint2,     // "This interface represents a breakpoint that is bound to a
                                    //  code location."

        IDebugBreakpointResolution2 // "This interface represents the information that describes a
                                    //  bound breakpoint."
    {
        private QmlDebugger Debugger { get; set; }
        private QmlEngine Engine { get; set; }
        public Program Program { get; private set; }
        public PendingBreakpoint Parent { get; private set; }
        private CodeContext CodeContext { get; set; }
        private bool Enabled { get; set; }

        bool supressNotify;

        string IBreakpoint.QrcPath
        {
            get
            {
                var qrcPath = Engine.FileSystem[Parent.FileName].QrcPath;
                if (qrcPath == null)
                    return string.Empty;
                if (qrcPath.StartsWith("qrc:///", StringComparison.InvariantCultureIgnoreCase))
                    qrcPath = qrcPath.Substring("qrc:///".Length);
                return qrcPath;
            }
        }

        uint IBreakpoint.Line
        {
            get { return Parent.BeginPosition.dwLine; }
        }

        public static Breakpoint Create(PendingBreakpoint parent, Program program)
        {
            return new Breakpoint
            {
                Engine = parent.Engine,
                Parent = parent,
                Program = program,
                Debugger = program.Debugger,
                Enabled = parent.Enabled,
                CodeContext = CodeContext.Create(
                    parent.Engine,
                    program,
                    parent.FileName,
                    parent.BeginPosition.dwLine),
            };
        }

        private Breakpoint()
        { }

        protected override void DisposeManaged()
        {
            Program.ClearBreakpoint(this);
        }

        int IDebugBoundBreakpoint2.Enable(int fEnable)
        {
            bool enable = (fEnable != 0);
            if (Atomic(() => Enabled != enable,
                       () => { Enabled = enable; supressNotify = true; })) {

                if (enable)
                    Debugger.SetBreakpoint(this);
                else
                    Debugger.ClearBreakpoint(this);
            }

            return VSConstants.S_OK;
        }

        int IDebugBoundBreakpoint2.Delete()
        {
            Parent.DisposeBreakpoint(this);
            return VSConstants.S_OK;
        }

        void IBreakpoint.NotifySet()
        {
            if (!Atomic(() => supressNotify, () => supressNotify = false))
                Program.NotifyBreakpointSet(this);
        }

        void IBreakpoint.NotifyClear()
        {
            if (!Atomic(() => supressNotify, () => supressNotify = false))
                Program.NotifyBreakpointCleared(this);
        }

        void IBreakpoint.NotifyBreak()
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            Program.NotifyBreakpointHit(this);
        }

        void IBreakpoint.NotifyError(string errorMessage)
        {
            Program.OutputWriteLine(errorMessage);
        }

        int IDebugBoundBreakpoint2.GetPendingBreakpoint(
            out IDebugPendingBreakpoint2 ppPendingBreakpoint)
        {
            ppPendingBreakpoint = Parent;
            return VSConstants.S_OK;
        }

        int IDebugBoundBreakpoint2.GetState(enum_BP_STATE[] pState)
        {
            pState[0] = 0;
            if (Disposed) {
                pState[0] = enum_BP_STATE.BPS_DELETED;

            } else if (Enabled) {
                pState[0] = enum_BP_STATE.BPS_ENABLED;

            } else {
                pState[0] = enum_BP_STATE.BPS_DISABLED;
            }

            return VSConstants.S_OK;
        }


        #region //////////////////// IDebugBreakpointResolution2 //////////////////////////////////

        int IDebugBoundBreakpoint2.GetBreakpointResolution(
            out IDebugBreakpointResolution2 ppBPResolution)
        {
            ppBPResolution = this;
            return VSConstants.S_OK;
        }

        int IDebugBreakpointResolution2.GetBreakpointType(enum_BP_TYPE[] pBPType)
        {
            pBPType[0] = enum_BP_TYPE.BPT_CODE;
            return VSConstants.S_OK;
        }

        int IDebugBreakpointResolution2.GetResolutionInfo(
            enum_BPRESI_FIELDS dwFields,
            BP_RESOLUTION_INFO[] pBPResolutionInfo)
        {
            if ((dwFields & enum_BPRESI_FIELDS.BPRESI_BPRESLOCATION) != 0) {
                BP_RESOLUTION_LOCATION location = new BP_RESOLUTION_LOCATION();
                location.bpType = (uint)enum_BP_TYPE.BPT_CODE;
                location.unionmember1
                    = Marshal.GetComInterfaceForObject(CodeContext, typeof(IDebugCodeContext2));
                pBPResolutionInfo[0].bpResLocation = location;
                pBPResolutionInfo[0].dwFields |= enum_BPRESI_FIELDS.BPRESI_BPRESLOCATION;
            }
            if ((dwFields & enum_BPRESI_FIELDS.BPRESI_PROGRAM) != 0) {
                pBPResolutionInfo[0].pProgram = Program as IDebugProgram2;
                pBPResolutionInfo[0].dwFields |= enum_BPRESI_FIELDS.BPRESI_PROGRAM;
            }

            return VSConstants.S_OK;
        }

        #endregion //////////////////// IDebugBreakpointResolution2 ///////////////////////////////

    }
}