aboutsummaryrefslogtreecommitdiffstats
path: root/src/qtvstools/QML/Debugging/AD7/QmlDebugAD7Engine.cs
blob: cd2b39c344083af825cdcd9193b6a6589436255e (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
/****************************************************************************
**
** 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.Linq;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;

namespace QtVsTools.Qml.Debug.AD7
{
    [ComVisible(true)]
    [Guid(CLSID_ENGINE)]
    sealed partial class QmlEngine :

        IDebugEngine2,       // "This interface represents a debug engine (DE). It is used to manage
                             //  various aspects of a debugging session, from creating breakpoints
                             //  to setting and clearing exceptions."

        IDebugEngineLaunch2, // "Used by a debug engine (DE) to launch and terminate programs."

        IDebugEventCallback2 // "This interface is used by the debug engine (DE) to send debug
                             //  events to the session debug manager (SDM)."
    {
        const string CLSID_ENGINE = "fa2993e3-8b2a-40a6-8853-ac2db2daed5a";
        public static readonly Guid ClassId = new Guid(CLSID_ENGINE);

        const string ID_ENGINE = "86102a1b-4378-4964-a7ed-21852a8afb7f";
        public static readonly Guid Id = new Guid(ID_ENGINE);

        public IDebugEventCallback2 Callback { get; private set; }

        public FileSystem FileSystem { get; private set; }

        int IDebugEventCallback2.Event(
            IDebugEngine2 pEngine,
            IDebugProcess2 pProcess,
            IDebugProgram2 pProgram,
            IDebugThread2 pThread,
            IDebugEvent2 pEvent,
            ref Guid riidEvent,
            uint dwAttrib)
        {
            if (Callback == null)
                return VSConstants.S_OK;
            return Callback.Event(pEngine, pProcess,
                pProgram, pThread, pEvent, ref riidEvent, dwAttrib);
        }

        public QmlEngine()
        {
            FileSystem = FileSystem.Create();
        }

        Dictionary<Guid, Program> programs = new Dictionary<Guid, Program>();
        public IEnumerable<Program> Programs
        {
            get { return ThreadSafe(() => programs.Values.ToList()); }
        }

        HashSet<PendingBreakpoint> pendingBreakpoints = new HashSet<PendingBreakpoint>();
        public IEnumerable<PendingBreakpoint> PendingBreakpoints
        {
            get { return ThreadSafe(() => pendingBreakpoints.ToList()); }
        }

        int IDebugEngine2.GetEngineId(out Guid pguidEngine)
        {
            pguidEngine = Id;
            return VSConstants.S_OK;
        }

        int IDebugEngineLaunch2.LaunchSuspended(string pszServer, IDebugPort2 pPort,
            string pszExe, string pszArgs, string pszDir, string bstrEnv, string pszOptions,
            enum_LAUNCH_FLAGS dwLaunchFlags, uint hStdInput, uint hStdOutput, uint hStdError,
            IDebugEventCallback2 pCallback, out IDebugProcess2 ppProcess)
        {
            ppProcess = null;

            if (string.IsNullOrEmpty(pszOptions))
                return VSConstants.E_FAIL;

            uint procId;
            if (!uint.TryParse(pszOptions, out procId))
                return VSConstants.E_FAIL;

            var env = bstrEnv.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Split(new[] { '=' }))
                .Where(x => x.Length >= 2)
                .ToDictionary(x => x[0], x => x[1].Split(new[] { ';' }));

            if (env.ContainsKey("QTRCC")) {
                foreach (var rccFile in env["QTRCC"])
                    FileSystem.RegisterRccFile(rccFile);
            }

            IDebugProcess2 nativeProc;
            var nativeProcId = new AD_PROCESS_ID
            {
                ProcessIdType = (uint)enum_AD_PROCESS_ID.AD_PROCESS_ID_SYSTEM,
                dwProcessId = procId
            };
            if (pPort.GetProcess(nativeProcId, out nativeProc) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            var program = Program.Create(this, nativeProc, pszExe, pszArgs);
            if (program == null)
                return VSConstants.E_FAIL;

            programs.Add(program.ProcessId, program);
            ppProcess = program;
            return VSConstants.S_OK;
        }

        int IDebugEngineLaunch2.ResumeProcess(IDebugProcess2 process)
        {
            var program = process as Program;
            if (program == null)
                return VSConstants.E_FAIL;

            IDebugPort2 port;
            if (process.GetPort(out port) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            string portName;
            port.GetPortName(out portName);

            Guid guidPort;
            port.GetPortId(out guidPort);

            IDebugDefaultPort2 defaultPort = (IDebugDefaultPort2)port;

            IDebugPortNotify2 portNotify;
            if (defaultPort.GetPortNotify(out portNotify) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            if (portNotify.AddProgramNode(program) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            return VSConstants.S_OK;
        }

        int IDebugEngine2.Attach(
            IDebugProgram2[] rgpPrograms,
            IDebugProgramNode2[] rgpProgramNodes,
            uint celtPrograms,
            IDebugEventCallback2 pCallback,
            enum_ATTACH_REASON dwReason)
        {
            var program = rgpProgramNodes[0] as Program;
            if (program == null)
                return VSConstants.E_FAIL;

            Callback = pCallback;

            DebugEvent.Send(new EngineCreateEvent(this));

            Guid pguidProgramId;
            if (rgpPrograms[0].GetProgramId(out pguidProgramId) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            program.ProgramId = pguidProgramId;

            DebugEvent.Send(new ProgramCreateEvent(program));
            DebugEvent.Send(new ThreadCreateEvent(program));
            DebugEvent.Send(new LoadCompleteEvent(program));
            DebugEvent.Send(new EntryPointEvent(program));

            return VSConstants.S_OK;
        }

        int IDebugEngineLaunch2.CanTerminateProcess(IDebugProcess2 pProcess)
        {
            Guid procId;
            if (pProcess.GetProcessId(out procId) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            Program program;
            if (!programs.TryGetValue(procId, out program))
                return VSConstants.S_FALSE;

            return VSConstants.S_OK;
        }

        public bool ProgramIsRunning(Program program)
        {
            return programs.ContainsKey(program.ProcessId);
        }

        int IDebugEngineLaunch2.TerminateProcess(IDebugProcess2 pProcess)
        {
            Guid procId;
            if (pProcess.GetProcessId(out procId) != VSConstants.S_OK)
                return VSConstants.E_FAIL;

            Program program;
            if (!programs.TryGetValue(procId, out program))
                return VSConstants.S_FALSE;

            programs.Remove(procId);

            DebugEvent.Send(new ThreadDestroyEvent(program, 0));
            DebugEvent.Send(new ProgramDestroyEvent(program, 0));

            return VSConstants.S_OK;
        }

        int IDebugEngine2.ContinueFromSynchronousEvent(IDebugEvent2 pEvent)
        {
            var evtProgramDestroy = pEvent as ProgramDestroyEvent;
            if (evtProgramDestroy != null)
                evtProgramDestroy.Program.Dispose();

            return VSConstants.S_OK;
        }

        int IDebugEngine2.CreatePendingBreakpoint(
            IDebugBreakpointRequest2 pBPRequest,
            out IDebugPendingBreakpoint2 ppPendingBP)
        {
            ppPendingBP = null;

            var pendingBreakpoint = PendingBreakpoint.Create(this, pBPRequest);
            if (pendingBreakpoint == null)
                return VSConstants.E_FAIL;

            ppPendingBP = pendingBreakpoint;
            pendingBreakpoints.Add(pendingBreakpoint);

            return VSConstants.S_OK;
        }

        public void DisposePendingBreakpoint(PendingBreakpoint pendingBreakpoint)
        {
            pendingBreakpoints.Remove(pendingBreakpoint);
            pendingBreakpoint.Dispose();
        }


        #region //////////////////// Concurrent ///////////////////////////////////////////////////

        LocalConcurrent concurrent = new LocalConcurrent();
        class LocalConcurrent : Concurrent
        {
            public void LocalThreadSafe(Action action)
            { ThreadSafe(action); }

            public T LocalThreadSafe<T>(Func<T> func)
            { return ThreadSafe(func); }

            public new bool Atomic(Func<bool> test, Action action, Action actionElse = null)
            { return base.Atomic(test, action, actionElse); }
        }

        void ThreadSafe(Action action)
        {
            concurrent.LocalThreadSafe(action);
        }

        T ThreadSafe<T>(Func<T> func)
        {
            return concurrent.LocalThreadSafe(func);
        }

        bool Atomic(Func<bool> test, Action action, Action actionElse = null)
        {
            return concurrent.Atomic(test, action, actionElse);
        }

        #endregion //////////////////// Concurrent ////////////////////////////////////////////////
    }

    [ComVisible(true)]
    [Guid(CLSID_PROGRAMPROVIDER)]
    sealed partial class ProgramProvider :

        IDebugProgramProvider2 // "This registered interface allows the session debug manager (SDM)
                               //  to obtain information about programs that have been "published"
                               //  through the IDebugProgramPublisher2 interface."
    {
        public const string CLSID_PROGRAMPROVIDER = "f2ff34e2-7fa5-461b-9e59-b5997ee0a637";
        public static readonly Guid ClassId = new Guid(CLSID_PROGRAMPROVIDER);

        public ProgramProvider()
        { }
    }

    public static class NativeEngine
    {
        const string ID_NATIVEENGINE = "3b476d35-a401-11d2-aad4-00c04f990171";
        public static readonly Guid Id = new Guid(ID_NATIVEENGINE);

        const string ID_LANGUAGE_CPP = "3a12d0b7-c26c-11d0-b442-00a0244a1dd2";
        public static Guid IdLanguageCpp = new Guid(ID_LANGUAGE_CPP);
    }
}