-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbortHandler.cs
426 lines (354 loc) · 14.4 KB
/
AbortHandler.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Threading;
using System.Runtime.Serialization;
namespace Microsoft.SPOT.Debugger
{
public class AbortHandler
{
public class Report
{
public const int c_Noise = 0x00000000;
public const int c_ReadMemory = 0x00000001;
public const int c_Register = 0x00000002;
public const int c_Layout = 0x00000003;
public const int c_Error = 0x00000004;
public int type;
public string line;
public string[] values;
public Report( int type, string line, Regex regex )
{
this.type = type;
this.line = line;
if(regex != null)
{
GroupCollection group = regex.Match( line ).Groups;
this.values = new string[group.Count];
for(int i=0; i<group.Count; i++)
{
this.values[i] = group[i].Value;
}
}
}
}
private Engine m_eng;
private bool m_deviceRunning;
private ArrayList m_reports = new ArrayList();
private AutoResetEvent m_ready = new AutoResetEvent( false );
private byte[] m_buffer = new byte[1024];
private int m_pos = 0;
private Regex m_re_ReadMemory = new Regex( "^\\[0x([0-9a-fA-F]*)\\](.*)" , RegexOptions.IgnoreCase );
private Regex m_re_Register = new Regex( "^([^=]+)=([0-9a-fA-F]*)" , RegexOptions.IgnoreCase );
private Regex m_re_Layout = new Regex( "^(rb|rs|fb|fs)=0x([0-9a-fA-F]*)", RegexOptions.IgnoreCase );
private Regex m_re_Error = new Regex( "^ERROR: (.*)" , RegexOptions.IgnoreCase );
public AbortHandler( Engine eng, bool deviceRunning )
{
m_eng = eng;
m_deviceRunning = deviceRunning;
}
public void Dispose()
{
Stop();
}
#if ABORTHANDLER_RAWDUMP
Stream m_stream;
#endif
public void Start()
{
#if ABORTHANDLER_RAWDUMP
m_stream = new FileStream( @"RawDump.bin", FileMode.Create );
#endif
m_eng.WaitForPort();
m_eng.OnNoise += new NoiseEventHandler( this.Process );
}
public void Stop()
{
m_eng.OnNoise -= new NoiseEventHandler( this.Process );
#if ABORTHANDLER_RAWDUMP
if(m_stream != null)
{
m_stream.Flush();
m_stream.Close();
}
#endif
}
private void Process( byte[] buf, int offset, int count )
{
#if ABORTHANDLER_RAWDUMP
if(m_stream != null)
{
m_stream.Write( buf, offset, count );
m_stream.Flush();
}
#endif
while(count-- > 0)
{
byte c = buf[offset++];
if(c == '\r') continue;
if(c == '\n')
{
string line = Encoding.UTF8.GetString( m_buffer, 0, m_pos );
Regex regex = null;
int rep;
if(m_re_ReadMemory.IsMatch( line ))
{
rep = Report.c_ReadMemory;
regex = m_re_ReadMemory;
}
else if(m_re_Layout.IsMatch( line ))
{
rep = Report.c_Layout;
regex = m_re_Layout;
}
else if(m_re_Register.IsMatch( line ))
{
rep = Report.c_Register;
regex = m_re_Register;
}
else if(m_re_Error.IsMatch( line ))
{
rep = Report.c_Error;
regex = m_re_Error;
}
else
{
rep = Report.c_Noise;
}
lock(m_reports)
{
m_reports.Add( new Report( rep, line, regex ) );
m_ready.Set();
}
m_pos = 0;
}
else if(m_pos >= m_buffer.Length)
{
m_pos = 0;
}
else
{
m_buffer[m_pos++] = c;
}
}
}
public Report GetReport( int timeout )
{
while(true)
{
lock(m_reports)
{
if(m_reports.Count > 0)
{
Report r = (Report)m_reports[0]; m_reports.RemoveAt( 0 );
return r;
}
}
if(timeout <= 0) return null;
DateTime start = DateTime.Now;
m_ready.WaitOne( timeout, false );
TimeSpan diff = DateTime.Now - start;
timeout -= (int)diff.TotalMilliseconds;
}
}
public bool ReadMemory( uint address, byte[] data, int offset, int count )
{
if(m_deviceRunning)
{
byte[] buf;
if(m_eng.ReadMemory( address, (uint)count, out buf ))
{
Array.Copy( buf, 0, data, offset, count );
return true;
}
return false;
}
else
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter( stream, Encoding.Unicode );
writer.Write( (byte)'M' );
writer.Write( (byte)0 );
writer.Write( (byte)0 );
writer.Write( (byte)0 );
writer.Write( (uint) address );
writer.Write( (uint)(address+count));
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Flush();
SendBuffer( stream.ToArray() );
while(count > 0)
{
Report r = GetReport( 100 ); if(r == null) return false;
if(r.type == Report.c_ReadMemory)
{
try
{
uint addressReply = UInt32.Parse( r.values[1], System.Globalization.NumberStyles.HexNumber );
string dataText = r.values[2];
if(addressReply == address)
{
int size = dataText.Length / 2;
for(int pos=0; pos<size; pos++)
{
data[offset+pos] = Byte.Parse( dataText.Substring( pos*2, 2 ), System.Globalization.NumberStyles.HexNumber );
}
address += (uint)size;
offset += size;
count -= size;
}
}
catch
{
return false;
}
}
else if(r.type == Report.c_Error)
{
return false;
}
else if(r.type == Report.c_Noise)
{
m_eng.InjectMessage( "{0}\r\n", r.line );
}
}
return true;
}
}
public bool ReadRegisters( uint[] registers, out uint cpsr, out uint BWA, out uint BWC )
{
if(m_deviceRunning)
{
cpsr = 0;
BWA = 0;
BWC = 0;
return true;
}
else
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter( stream, Encoding.Unicode );
int count = 16 + 3;
writer.Write( (byte)'R' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Flush();
SendBuffer( stream.ToArray() );
cpsr = 0;
BWA = 0;
BWC = 0;
while(count > 0)
{
Report r = GetReport( 100 ); if(r == null) return false;
if(r.type == Report.c_Register )
{
try
{
string reg = r.values[1];
uint data = UInt32.Parse( r.values[2], System.Globalization.NumberStyles.HexNumber );
switch(reg)
{
case "r0" : registers[ 0] = data; break;
case "r1" : registers[ 1] = data; break;
case "r2" : registers[ 2] = data; break;
case "r3" : registers[ 3] = data; break;
case "r4" : registers[ 4] = data; break;
case "r5" : registers[ 5] = data; break;
case "r6" : registers[ 6] = data; break;
case "r7" : registers[ 7] = data; break;
case "r8" : registers[ 8] = data; break;
case "r9" : registers[ 9] = data; break;
case "r10" : registers[10] = data; break;
case "r11" : registers[11] = data; break;
case "r12" : registers[12] = data; break;
case "sp" : registers[13] = data; break;
case "lr" : registers[14] = data; break;
case "pc" : registers[15] = data; break;
case "cpsr": cpsr = data; break;
case "BWA" : BWA = data; break;
case "BWC" : BWC = data; break;
default: return false;
}
count--;
}
catch
{
return false;
}
}
else if(r.type == Report.c_Error)
{
return false;
}
else if(r.type == Report.c_Noise)
{
m_eng.InjectMessage( "{0}\r\n", r.line );
}
}
return true;
}
}
public bool ReadLayout( out uint flashBase, out uint flashSize, out uint ramBase, out uint ramSize )
{
flashBase = flashSize = ramBase = ramSize = 0;
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter( stream, Encoding.Unicode );
int count = 4;
writer.Write( (byte)'L' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Write( (byte)'\n' );
writer.Flush();
SendBuffer( stream.ToArray() );
while(count > 0)
{
Report r = GetReport( 100 ); if(r == null) return false;
if(r.type == Report.c_Layout )
{
try
{
string reg = r.values[1];
uint data = UInt32.Parse( r.values[2], System.Globalization.NumberStyles.HexNumber );
switch(reg)
{
case "rb": ramBase = data; break;
case "rs": ramSize = data; break;
case "fb": flashBase = data; break;
case "fs": flashSize = data; break;
default: return false;
}
count--;
}
catch
{
return false;
}
}
else if(r.type == Report.c_Error)
{
return false;
}
else if(r.type == Report.c_Noise)
{
m_eng.InjectMessage( "{0}\r\n", r.line );
}
}
return true;
}
void SendBuffer( byte[] buf )
{
m_eng.SendRawBuffer( buf );
}
}
}