This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLogReader.cs
263 lines (239 loc) · 10.5 KB
/
LogReader.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
/*
* PROJECT: ReactOS System Regression Testing Utility, Windows/VirtualBox version
* LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
* PURPOSE: Processing the incoming debugging data
* COPYRIGHT: Copyright Aleksey Bragin <[email protected]>. Based around ideas and code from
* sysreg created by Christoph von Wittich <[email protected]> and
* Colin Finck <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.IO;
using VirtualBox;
using System.Runtime.InteropServices;
namespace sysreg3
{
public class LogReader
{
NamedPipeClientStream pipe;
string debugLogFilename;
ISession vmSession;
int stageNum;
int timeOut;
const string KDBPROMPT = "kdb:>";
const string KDBBTCONT = "--- Press q";
const string KDBASSERT = "Break repea";
String[] stageCheckpoint;
private ContinueType _result;
private bool _timedOut;
private AutoResetEvent _timeOutEvent;
Timer watchdog;
public LogReader(string namedPipeName, string logName, ISession session, int stage, int vmTimeout)
{
debugLogFilename = logName;
vmSession = session;
stageNum = stage;
timeOut = vmTimeout;
_timedOut = true;
stageCheckpoint = new String[3];
stageCheckpoint[0] = "It's the final countdown...";
stageCheckpoint[1] = "It's the final countdown...";
stageCheckpoint[2] = "SYSREG_CHECKPOINT:THIRDBOOT_COMPLETE";
_result = ContinueType.EXIT_DONT_CONTINUE;
_timeOutEvent = new AutoResetEvent(false);
watchdog = new Timer(WatchdogCallback, _timeOutEvent, timeOut, Timeout.Infinite);
// Connect to the named pipe of the VM to receive its debug info.
pipe = new NamedPipeClientStream(".", namedPipeName, PipeDirection.InOut);
}
public void WatchdogCallback(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
// Signal the event
autoEvent.Set();
}
public void Run()
{
try
{
using (StreamReader sr = new StreamReader(pipe))
using (TextWriter debugLogWriter =
(debugLogFilename != null) ? new StreamWriter(debugLogFilename, false) : null)
{
pipe.Connect(3000);
string line, cacheLine = "";
int kdbgHit = 0, cacheHits = 0;
bool quitLoop = false;
bool kdserial = false;
while (!quitLoop)
{
/* Read up to 512 chars */
StringBuilder buffer = new StringBuilder(512);
int index = 0, read;
while ((read = sr.Read()) != -1)
{
buffer.Append((char)read);
/* Break on newlines or in case of KDBG messages (which aren't terminated by newlines) */
if (read == (int)'\n')
break;
if (buffer.ToString().Contains(KDBPROMPT)
|| buffer.ToString().Contains(KDBBTCONT)
|| buffer.ToString().Contains(KDBASSERT))
break;
index++;
if (index >= buffer.Capacity)
break;
}
line = buffer.ToString();
{
/* Reset the watchdog timer */
watchdog.Change(timeOut, Timeout.Infinite);
Console.Write(line);
if(debugLogWriter != null)
debugLogWriter.Write(line);
/* Detect whether the same line appears over and over again.
If that is the case, cancel this test after a specified number of repetitions. */
if (line == cacheLine)
{
cacheHits++;
if (cacheHits > RegTester.maxCacheHits)
{
Console.WriteLine("[SYSREG] Test seems to be stuck in an endless loop, canceled!\n");
_result = ContinueType.EXIT_CONTINUE;
quitLoop = true;
break;
}
}
else
{
cacheHits = 0;
cacheLine = line;
}
/* Check for magic sequences */
if (line.Contains("KDSERIAL"))
{
#if TRACE
Console.WriteLine("[SYSREG] Switching to kdserial mode");
#endif
kdserial = true;
continue;
}
if (line.Contains(KDBPROMPT))
{
#if TRACE
Console.Write("-[TRACE] kdb prompt hit-");
#endif
kdbgHit++;
if (kdbgHit == 1)
{
/* It happened for the first time, backtrace */
if (kdserial)
{
char[] bt = { 'b', 't', '\r' };
foreach (char c in bt)
pipe.WriteByte((byte)c);
}
else
{
vmSession.Console.Keyboard.PutScancode(0x30); // b make
vmSession.Console.Keyboard.PutScancode(0xb0); // b release
vmSession.Console.Keyboard.PutScancode(0x14); // t make
vmSession.Console.Keyboard.PutScancode(0x94); // t release
vmSession.Console.Keyboard.PutScancode(0x1c); // Enter make
vmSession.Console.Keyboard.PutScancode(0x9c); // Enter release
}
continue;
}
else
{
/* It happened once again, no reason to continue */
Console.WriteLine();
_result = ContinueType.EXIT_CONTINUE;
quitLoop = true;
break;
}
}
else if (line.Contains(KDBBTCONT))
{
/* Send Return to get more data from Kdbg */
if (kdserial)
{
pipe.WriteByte((byte)'\r');
}
else
{
vmSession.Console.Keyboard.PutScancode(0x1c); // Enter make
vmSession.Console.Keyboard.PutScancode(0x9c); // Enter release
}
continue;
}
else if (line.Contains(KDBASSERT))
{
/* Break once */
if (kdserial)
{
pipe.WriteByte((byte)'o');
}
else
{
vmSession.Console.Keyboard.PutScancode(0x18); // 'O' make
vmSession.Console.Keyboard.PutScancode(0x98); // 'O' release
}
}
else if (line.Contains("SYSREG_ROSAUTOTEST_FAILURE"))
{
quitLoop = true;
break;
}
else if (line.Contains(stageCheckpoint[stageNum]))
{
_result = ContinueType.EXIT_CHECKPOINT_REACHED;
quitLoop = true;
break;
}
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("Exception occured in the LogReader.Run():");
Console.WriteLine(e.Message);
}
finally
{
pipe.Close();
Thread.Sleep(1000);
}
// Signal that we're done and it's not a timed out state
_timedOut = false;
_timeOutEvent.Set();
}
public ContinueType Result
{
get
{
return _result;
}
}
public AutoResetEvent TimeOutEvent
{
get
{
return _timeOutEvent;
}
}
public bool TimedOut
{
get
{
return _timedOut;
}
}
}
}