File: DebugControllerThread.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (209 lines) | stat: -rw-r--r-- 7,934 bytes parent folder | download | duplicates (7)
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
#region Using directives

using System;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.Win32;
#endregion

namespace System.Workflow.Runtime.DebugEngine
{
    internal sealed class DebugControllerThread
    {
        #region Data members

        private Thread controllerThread = null;
        private int threadId = 0;
        private ManualResetEvent threadInitializedEvent = null;
        private volatile bool runThread = false;
        private static readonly string ExpressionEvaluationFrameTypeName = "ExpressionEvaluationFrameTypeName";
        #endregion

        #region Methods

        public DebugControllerThread()
        {
            this.threadInitializedEvent = new ManualResetEvent(false);
            this.threadInitializedEvent.Reset();

            this.controllerThread = new Thread(ControllerThreadFunction);
            this.controllerThread.IsBackground = true;
            this.controllerThread.Priority = ThreadPriority.Lowest;
            this.controllerThread.Name = "__dct__";
        }

        public void RunThread(IInstanceTable instanceTable)
        {
            Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "WDE: DebugControllerThread.DebugControllerThread():"));
            if (this.controllerThread == null)
                return;

            this.runThread = true;
            this.controllerThread.Start(instanceTable);
            this.threadInitializedEvent.WaitOne();
        }

        public void StopThread()
        {
            Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "WDE: DebugControllerThread.StopThread():"));

            try
            {
                if (this.controllerThread != null)
                {
                    this.runThread = false;
                    Thread.Sleep(10);

                    // On x64 we put the debug controller thread to Sleep(Timeout.Infinite) in 
                    // ExpressionEvaluationFunction. This thread needs to be started before it
                    // a Join can execute.
                    if (this.controllerThread.IsAlive && IntPtr.Size == 8)
                    {
                        while (this.controllerThread.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
                        {
                            this.controllerThread.Start();
                            this.controllerThread.Join();
                        }
                    }
                    else
                        this.controllerThread.Join();
                }
            }
            catch (ThreadStateException)
            {
                // Ignore the ThreadStateException which will be thrown when StopThread() is called during 
                // AppDomain unload.
            }
            finally
            {
                this.controllerThread = null;
            }

            this.controllerThread = null;
            this.threadId = 0;
            this.threadInitializedEvent = null;
        }

        private void ControllerThreadFunction(object instanceTable)
        {
            try
            {
                Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "WDE: DebugControllerThread.ControllerThreadFunction():"));

                IExpressionEvaluationFrame expressionEvaluationFrame = null;

                try
                {
                    RegistryKey debugEngineSubKey = Registry.LocalMachine.OpenSubKey(RegistryKeys.DebuggerSubKey);
                    if (debugEngineSubKey != null)
                    {
                        string evaluationFrameTypeName = debugEngineSubKey.GetValue(ExpressionEvaluationFrameTypeName, String.Empty) as string;
                        if (!String.IsNullOrEmpty(evaluationFrameTypeName) && Type.GetType(evaluationFrameTypeName) != null)
                            expressionEvaluationFrame = Activator.CreateInstance(Type.GetType(evaluationFrameTypeName)) as IExpressionEvaluationFrame;
                    }
                }
                catch { }

                if (expressionEvaluationFrame == null)
                {
                    Type eeFrameType = null;

                    const string eeFrameTypeNameFormat = "Microsoft.Workflow.DebugEngine.ExpressionEvaluationFrame, Microsoft.Workflow.ExpressionEvaluation, Version={0}.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";

                    // Try versions 12.0.0.0, 11.0.0.0, 10.0.0.0
                    for (int version = 12; eeFrameType == null && version >= 10; --version)
                    {
                        try
                        {
                            eeFrameType = Type.GetType(string.Format(CultureInfo.InvariantCulture, eeFrameTypeNameFormat, version));
                        }
                        catch (TypeLoadException)
                        {
                            // Fall back to next-lower version
                        }
                    }

                    if (eeFrameType != null)
                    {
                        expressionEvaluationFrame = Activator.CreateInstance(eeFrameType) as IExpressionEvaluationFrame;
                    }
                }

                Debug.Assert(expressionEvaluationFrame != null, "Failed to create Expression Evaluation Frame.");

                if (expressionEvaluationFrame != null)
                    expressionEvaluationFrame.CreateEvaluationFrame((IInstanceTable)instanceTable, (DebugEngineCallback)Delegate.CreateDelegate(typeof(DebugEngineCallback), this, "ExpressionEvaluationFunction"));
            }
            catch
            {
                // Don't throw exceptions to the Runtime, it would terminate the debugee.
            }
        }

        // This thread spins forever. It is used by the Debug Engine to perform expression evaluation as a "carrier
        // wave". It is created only when the debugger is attached and terminates itself when the debugger isn't
        // attached anymore.
        public void ExpressionEvaluationFunction()
        {
            this.threadId = NativeMethods.GetCurrentThreadId();
            this.threadInitializedEvent.Set();


            using (new DebuggerThreadMarker())
            {
                // If an exception occurs somehow, continue to spin.
                while (this.runThread)
                {
                    try
                    {
                        // Expression eval on x64 does not work (bug 18143) so 
                        // don't let the thread spin.
                        if (IntPtr.Size == 8)
                        {
                            Thread.Sleep(Timeout.Infinite);
                        }
                        else
                            // Spin within the try catch.
                            while (this.runThread);
                    }
                    catch (ThreadAbortException)
                    {
                        Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "WDE: DebugControllerThread.ExpressionEvaluationFunction(): ThreadAbortException"));

                        // Explicitly do not call ResetAbort().
                        throw;
                    }
                    catch
                    {
                        Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "WDE: DebugControllerThread.ExpressionEvaluationFunction(): other exception"));
                    }
                }
            }
        }

        #endregion

        #region Properties

        public int ThreadId
        {
            get
            {
                return this.threadId;
            }
        }

        public int ManagedThreadId
        {
            get
            {
                return this.controllerThread.ManagedThreadId;
            }
        }

        #endregion
    }
}