/
DebugWindow.xaml.cs
213 lines (181 loc) · 6.75 KB
/
DebugWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime.Exceptions;
using System.Threading;
using IronPython.Runtime;
namespace LightweightDebuggerDemo
{
public static class DebugCommands
{
public static readonly RoutedUICommand StepIn = new RoutedUICommand("Step In", "StepIn", typeof(DebugWindow));
public static readonly RoutedUICommand StepOut = new RoutedUICommand("Step Out", "StepOut", typeof(DebugWindow));
public static readonly RoutedUICommand StepOver = new RoutedUICommand("Step Over", "StepOver", typeof(DebugWindow));
}
/// <summary>
/// Interaction logic for DebugWindow.xaml
/// </summary>
public partial class DebugWindow : Window
{
static Thread _debugThread;
static DebugWindow _debugWindow;
static ManualResetEvent _debugWindowReady = new ManualResetEvent(false);
public static void InitDebugWindow(ScriptEngine engine)
{
_debugThread = new Thread(() =>
{
_debugWindow = new DebugWindow(engine);
_debugWindow.Show();
System.Windows.Threading.Dispatcher.Run();
});
_debugThread.SetApartmentState(ApartmentState.STA);
_debugThread.Start();
_debugWindowReady.WaitOne();
engine.SetTrace(_debugWindow.OnTracebackReceived);
}
public static void Shutdown()
{
_debugWindow._engine.SetTrace(null);
_debugWindow.Dispatcher.InvokeShutdown();
}
ScriptEngine _engine;
Paragraph _source;
AutoResetEvent _dbgContinue = new AutoResetEvent(false);
Action<TraceBackFrame, string, object> _tracebackAction;
TraceBackFrame _curFrame;
FunctionCode _curCode;
string _curResult;
object _curPayload;
private DebugWindow(ScriptEngine engine)
{
InitializeComponent();
_tracebackAction = new Action<TraceBackFrame, string, object>(this.OnTraceback);
_engine = engine;
}
private void Window_Closed(object sender, EventArgs e)
{
//Since the DebugWindow is on a seperate thread, we have
//to shut down the dispatcher manually when we close the window
this.Dispatcher.InvokeShutdown();
}
private void HighlightLine(int linenum, Brush foreground, Brush background)
{
var curline = _source.Inlines.ElementAtOrDefault(linenum - 1);
if (curline != null)
{
var visible_start = rtbSource.GetPositionFromPoint(new Point(0,0), true);
var visible_end = rtbSource.GetPositionFromPoint(new Point(0, rtbSource.ActualHeight), true);
if (visible_start.CompareTo(curline.ContentStart) > 0
|| visible_end.CompareTo(curline.ContentStart) < 0)
{
var top = curline.ContentStart.GetCharacterRect(LogicalDirection.Forward).Top - 3;
rtbSource.ScrollToVerticalOffset(top);
}
curline.Background = foreground;
curline.Foreground = background;
}
}
private void TracebackCall()
{
dbgStatus.Text = string.Format("Call {0}", _curCode.co_name);
HighlightLine((int)_curFrame.f_lineno, Brushes.LightGreen, Brushes.Black);
}
private void TracebackReturn()
{
dbgStatus.Text = string.Format("Return {0}", _curCode.co_name);
HighlightLine(_curCode.co_firstlineno, Brushes.LightPink, Brushes.Black);
}
private void TracebackLine()
{
dbgStatus.Text = string.Format("Line {0}", _curFrame.f_lineno);
HighlightLine((int)_curFrame.f_lineno, Brushes.Yellow, Brushes.Black);
}
private void OnTraceback(TraceBackFrame frame, string result, object payload)
{
var code = (FunctionCode)frame.f_code;
if (_curCode == null || _curCode.co_filename != code.co_filename)
{
_source.Inlines.Clear();
foreach (var line in System.IO.File.ReadAllLines(code.co_filename))
{
_source.Inlines.Add(new Run(line + "\r\n"));
}
}
_curFrame = frame;
_curCode = code;
_curResult = result;
_curPayload = payload;
switch (result)
{
case "call":
TracebackCall();
break;
case "line":
TracebackLine();
break;
case "return":
TracebackReturn();
break;
default:
MessageBox.Show(string.Format("{0} not supported!", result));
break;
}
}
bool breaktrace = true;
private TracebackDelegate OnTracebackReceived(TraceBackFrame frame, string result, object payload)
{
if (breaktrace)
{
this.Dispatcher.BeginInvoke(_tracebackAction, frame, result, payload);
_dbgContinue.WaitOne();
return _traceback;
}
else
return null;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_source = new Paragraph();
rtbSource.Document = new FlowDocument(_source);
rtbSource.Document.PageWidth = 10000;
_debugWindowReady.Set();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
breaktrace = false;
_dbgContinue.Set();
}
TracebackDelegate _traceback;
private void StepInExecuted(object sender, ExecutedRoutedEventArgs e)
{
_traceback = this.OnTracebackReceived;
ExecuteStep();
}
private void ExecuteStep()
{
dbgStatus.Text = "Running";
foreach (var i in _source.Inlines)
{
i.Background = Brushes.Black;
i.Foreground = Brushes.White;
}
_dbgContinue.Set();
}
private void StepOutExecuted(object sender, ExecutedRoutedEventArgs e)
{
_traceback = null;
ExecuteStep();
}
}
}