-
Notifications
You must be signed in to change notification settings - Fork 9
/
console.c
362 lines (319 loc) · 11.8 KB
/
console.c
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
/*
* PROJECT: ReactOS System Regression Testing Utility
* LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
* PURPOSE: Processing the incoming debugging data
* COPYRIGHT: Copyright 2008-2009 Christoph von Wittich <[email protected]>
* Copyright 2009 Colin Finck <[email protected]>
* Copyright 2012-2013 Pierre Schweitzer <[email protected]>
*/
#include "sysreg.h"
#define BUFFER_SIZE 512
int ProcessDebugData(const char* tty, int timeout, int stage )
{
char Buffer[BUFFER_SIZE];
char CacheBuffer[BUFFER_SIZE];
char Raddr2LineBuffer[BUFFER_SIZE];
char* bp = Buffer;
int got;
int Ret = EXIT_DONT_CONTINUE;
int ttyfd;
struct termios ttyattr, rawattr;
unsigned int CacheHits = 0;
unsigned int i;
unsigned int KdbgHit = 0;
unsigned int Cont = 0;
bool AlreadyBooted = false;
bool Prompt = false;
bool CheckpointReached = false;
bool BrokeToDebugger = false;
bool MonitorStdin = false;
/* Initialize CacheBuffer with an empty string */
*CacheBuffer = 0;
if (AppSettings.VMType == TYPE_VMWARE_PLAYER || AppSettings.VMType == TYPE_VIRTUALBOX)
{
/* Wait for VMware connection */
if ((ttyfd = accept(AppSettings.Specific.VMwarePlayer.Socket, NULL, NULL)) < 0)
{
SysregPrintf("error getting socket\n");
return Ret;
}
/* Set non blocking */
if (fcntl(ttyfd, F_SETFL, O_NONBLOCK) < 0)
{
SysregPrintf("error setting flag\n");
close(ttyfd);
return Ret;
}
}
else
{
/* ttyfd is the file descriptor of the virtual COM port */
if ((ttyfd = open(tty, O_NOCTTY | O_RDWR | O_NONBLOCK)) < 0)
{
SysregPrintf("error opening tty\n");
return Ret;
}
}
/* We also monitor STDIN_FILENO, so a user can cancel the process with ESC */
if (tcgetattr(STDIN_FILENO, &ttyattr) >= 0)
{
rawattr = ttyattr;
rawattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| IGNCR | ICRNL | IXON);
rawattr.c_lflag &= ~(ICANON | ECHO | ECHONL);
rawattr.c_oflag &= ~OPOST;
rawattr.c_cflag &= ~(CSIZE | PARENB);
rawattr.c_cflag |= CS8;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) >= 0)
{
MonitorStdin = true;
}
}
if (!MonitorStdin)
{
SysregPrintf("No STDIN, sysreg2 won't monitor it\n");
}
for(;;)
{
struct pollfd fds[] = {
{ ttyfd, POLLIN | POLLHUP | POLLERR, 0 },
{ STDIN_FILENO, POLLIN, 0 }, /* Always keep it as the end of the FDs */
};
nfds_t nfds = (sizeof(fds) / sizeof(struct pollfd));
if (!MonitorStdin)
--nfds;
got = poll(fds, nfds, timeout);
if (got < 0)
{
/* Just try it again on simple errors */
if (errno == EINTR || errno == EAGAIN)
continue;
SysregPrintf("poll failed with error %d\n", errno);
goto cleanup;
}
else if (got == 0)
{
/* timeout - only break once then, quit */
if (!BreakToDebugger() || BrokeToDebugger)
{
SysregPrintf("timeout\n");
Ret = EXIT_CONTINUE;
goto cleanup;
}
else
{
BrokeToDebugger = true;
}
}
/* Check for global timeout */
if (time(0) >= AppSettings.GlobalTimeout)
{
/* global timeout */
SysregPrintf("global timeout\n");
Ret = EXIT_DONT_CONTINUE;
goto cleanup;
}
for (i = 0; i < nfds; i++)
{
if ((fds[i].fd == ttyfd) && (
(fds[i].revents & POLLHUP) ||
(fds[i].revents & POLLERR)))
{
/* This might indicate VM shutdown (KVM), so continue and move to next stage */
Ret = EXIT_CONTINUE;
goto cleanup;
}
/* Wait till we get some input from the fd */
if (!(fds[i].revents & POLLIN))
continue;
/* Reset buffer only when we read complete line */
if (bp != Buffer && (*bp == '\n' || (bp - Buffer >= (BUFFER_SIZE - 1))))
bp = Buffer;
/* Read one line or a maximum of 511 bytes into a buffer (leave space for the null character) */
while (bp - Buffer < (BUFFER_SIZE - 1))
{
got = read(fds[i].fd, bp, 1);
if (got < 0)
{
/* Give it another chance */
if (errno == EINTR)
continue;
/* There's nothing more to read */
else if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
SysregPrintf("read failed with error %d\n", errno);
goto cleanup;
}
else if (got == 0)
{
/* No more data */
break;
}
if (fds[i].fd == STDIN_FILENO)
{
/* break on ESC */
if (*bp == '\33')
goto cleanup;
}
else
{
/* Null-terminate the line */
bp[1] = 0;
/* Break on newlines or in case of KDBG messages (which aren't terminated by newlines) */
if(*bp == '\n')
break;
if (strstr(Buffer, "kdb:>") || strstr(Buffer, "--- Press q to abort, any other key to continue ---"))
{
/* Set EOL */
++bp;
*bp = '\n';
bp[1] = 0;
break;
}
++bp;
}
}
/* The rest of this logic is just about processing the serial output */
if(fds[i].fd == STDIN_FILENO)
continue;
/* Check whether the message is of zero length */
if (bp == Buffer && got == 0)
{
/* This can happen when the machine shut down (like after 1st or 2nd stage)
or after we got a Kdbg backtrace. */
Ret = EXIT_CONTINUE;
goto cleanup;
}
/* Only process complete lines */
if (*bp != '\n' && (bp - Buffer < (BUFFER_SIZE - 1)))
continue;
/* Hackish way to detect reboot under VMware... */
if (((AppSettings.VMType == TYPE_VMWARE_PLAYER) || (AppSettings.VMType == TYPE_VIRTUALBOX)) &&
strstr(Buffer, "-----------------------------------------------------"))
{
if (AlreadyBooted)
{
Ret = EXIT_CONTINUE;
goto cleanup;
}
else
{
AlreadyBooted = true;
BrokeToDebugger = false;
}
}
/* 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(!strcmp(Buffer, CacheBuffer))
{
++CacheHits;
if(CacheHits > AppSettings.MaxCacheHits)
{
SysregPrintf("Test seems to be stuck in an endless loop, canceled!\n");
Ret = EXIT_CONTINUE;
goto cleanup;
}
}
else
{
CacheHits = 0;
memcpy(CacheBuffer, Buffer, bp - Buffer + 1);
CacheBuffer[bp - Buffer + 1] = 0;
}
/* Output the line, raddr2line the included addresses if necessary */
if (KdbgHit == 1 && ResolveAddressFromFile(Raddr2LineBuffer, sizeof(Raddr2LineBuffer), Buffer))
printf("%s", Raddr2LineBuffer);
else
printf("%s", Buffer);
/* Check for "magic" sequences */
if (strstr(Buffer, "kdb:>"))
{
++KdbgHit;
if (KdbgHit == 1)
{
/* If we have a call to RtlAssert(), break once
* Otherwise we hit Kdbg for the first time, get a backtrace for the log
*/
if (safewriteex(ttyfd, (Prompt ? "o\r" : "bt\r"), (Prompt ? 2 : 3), timeout) < 0
&& errno == EWOULDBLOCK)
{
/* timeout */
SysregPrintf("timeout\n");
Ret = EXIT_CONTINUE;
goto cleanup;
/* No need to reset Prompt here, we will quit */
}
if (Prompt)
{
/* We're not prompted afterwards, so reset */
Prompt = false;
/* On next hit, we'll have broken once, so prepare for bt */
KdbgHit = 0;
}
continue;
}
else
{
++Cont;
/* We won't cont if we reached max tries */
if (Cont <= AppSettings.MaxConts || BrokeToDebugger)
{
KdbgHit = 0;
/* Try to continue */
if (safewrite(ttyfd, "cont\r", timeout) < 0 && errno == EWOULDBLOCK)
{
/* timeout */
SysregPrintf("timeout\n");
Ret = EXIT_CONTINUE;
goto cleanup;
}
/* Reduce timeout to let ROS properly shutdown (if possible) */
if (BrokeToDebugger)
{
timeout = 5000;
}
continue;
}
else
{
/* We tried to continue too many times - abort */
printf("\n");
Ret = EXIT_CONTINUE;
goto cleanup;
}
}
}
else if (strstr(Buffer, "--- Press q"))
{
/* Send Return to get more data from Kdbg */
if (safewrite(ttyfd, "\r", timeout) < 0 && errno == EWOULDBLOCK)
{
/* timeout */
SysregPrintf("timeout\n");
Ret = EXIT_CONTINUE;
goto cleanup;
}
continue;
}
else if (strstr(Buffer, "Break repea"))
{
/* This is a call to DbgPrompt, next kdb prompt will be for selecting behavior */
Prompt = true;
}
else if (strstr(Buffer, "SYSREG_ROSAUTOTEST_FAILURE"))
{
/* rosautotest itself has problems, so there's no reason to continue */
goto cleanup;
}
else if (*AppSettings.Stage[stage].Checkpoint && strstr(Buffer, AppSettings.Stage[stage].Checkpoint))
{
/* We reached a checkpoint, so return success */
CheckpointReached = true;
}
}
}
cleanup:
tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);
close(ttyfd);
return (CheckpointReached ? EXIT_CHECKPOINT_REACHED : Ret);
}