sim_error.c
7.25 KB
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
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* error.c
*
* Error handling routines
*
* Author: $Author: blythe $
* Date: $Date: 2002/06/25 11:56:03 $
*****************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "sim_error.h"
#include "cpu_interface.h"
#include "tcl_init.h"
FILE *cpulogF = 0;
int loopOnError = 1;
int CPUErrorLoop = 1;
int SimErrorKeepLogs =0;
void SimErrorInit(char *log_file)
{
char filename[256];
ASSERT( !cpulogF );
if( SimErrorKeepLogs && (access(log_file,F_OK)==0) ) {
int CPnum;
for (CPnum = 1; ; CPnum++) {
sprintf(filename, "%s.%03i", log_file, CPnum);
if (access(filename, F_OK) == 0) {
if (CPnum > 999) { CPUError("Too many opened log files"); }
continue;
}
if( rename(log_file, filename) ) {
perror("Rename log file error");
} else {
/* renamed file */
}
break;
}
}
cpulogF = fopen(log_file,"w");
if (cpulogF == NULL) {
sprintf(filename, "fopen of %s", log_file);
perror(filename);
exit(1);
}
}
void CPUError(char* string, ...)
{
va_list ap;
char llstring[1024];
SubstituteLLx(llstring, string, 1024);
va_start(ap, string);
fprintf(stderr, "\nERROR, pid=%i :",(int)getpid());
vfprintf(stderr, llstring, ap);
va_end(ap);
fprintf(stderr, "\n");
fprintf(stderr, "\r");
if (!CPUVec.CycleCount || CPUVec.CycleCount(0)==0) {
loopOnError = 0;
}
if (loopOnError) {
while (CPUErrorLoop) {
#ifdef sgi
sginap(20);
#else
sleep(1);
#endif
}
} else {
exit(-1);
}
}
void Sim_Error(char* string, ...)
{
fprintf(stderr,"Sim_Error called. Please replace with CPUError!!! \n");
fprintf(stderr, "\nERROR, pid=%i :",(int)getpid());
if (loopOnError) {
while (CPUErrorLoop) {
#ifdef sgi
sginap(20);
#else
sleep(1);
#endif
}
} else {
exit(-1);
}
}
void CPUWarning( char *text,...)
{
va_list ap;
char lltext[1024];
va_start(ap,text);
SubstituteLLx(lltext, text, 1024);
vfprintf(stderr, lltext, ap);
vCPUPrint(lltext, ap);
fprintf(stderr, "\r");
fflush(stderr);
va_end(ap);
}
void Sim_Warning( char *text,...)
{
va_list ap;
char lltext[1024];
va_start(ap,text);
SubstituteLLx(lltext, text, 1024);
vfprintf(stderr, lltext, ap);
vCPUPrint(lltext, ap);
fprintf(stderr, "\r");
fflush(stderr);
va_end(ap);
}
/*****************************************************************
* Don't print anything using CPUPrint or CPUWarning here or you can
* end up stepping on yourself by opening the cpu log twice.
*****************************************************************/
void vCPUPrint(char *text, va_list ap)
{
#ifdef __alpha
char out[1024];
SubstituteLLx(out,text,1024);
ASSERT(cpulogF);
vfprintf(cpulogF,out,ap);
fflush(cpulogF);
#else
ASSERT(cpulogF);
vfprintf(cpulogF,text,ap);
fflush(cpulogF);
#endif
}
void CPUPrint( char *text,...)
{
va_list ap;
va_start(ap,text);
vCPUPrint(text,ap);
va_end(ap);
}
void CPUPrintFlush(void)
{
if( cpulogF ) {
fflush(cpulogF);
}
}
void CPUPut(char* string, ...)
{
va_list ap;
char llstring[1024];
va_start(ap, string);
SubstituteLLx(llstring, string, 1024);
vfprintf(stderr, llstring, ap);
va_end(ap);
fprintf(stderr, "\r");
fflush(stderr);
}
void lCPUPut(char* string, ...)
{
va_list ap;
va_start(ap, string);
vCPUPrint(string, ap);
va_end(ap);
}
/*
* Debugging help
*/
void CPU_nop(void)
{
/* nop */
}
void CPURestartLog(void)
{
cpulogF = NULL;
CPUPrint(" Restarting Log PID=%i \n", getpid());
}
/*****************************************************************
* DEBUGGING SUPPORT
* Taken from Nachos' utility.cc
*****************************************************************/
/*****************************************************************
* Use Tcl to print more detail to the log... including process ID
****************************************************************/
void
LogEntry(char *symbol, int cpuNum, char *text,...)
{
va_list ap;
SimTime x=0;
char cpuName[32];
char *pid, *proc;
sprintf(cpuName,"%d",cpuNum);
pid = Tcl_GetVar2(TCLInterp,"PID",cpuName,0);
proc = Tcl_GetVar2(TCLInterp,"PROCESS",cpuName,0);
if (!pid) { pid = "-1";}
if (!proc) { proc = "NONAME";}
if (CPUVec.CycleCount) {
x = CPUVec.CycleCount(cpuNum);
}
CPUPrint("LOG %lld\t%-12s\tcpu=%i\t%s-%s\t ",
(uint64)x,
symbol,
cpuNum,
pid, proc);
va_start(ap,text);
vCPUPrint(text,ap);
va_end(ap);
}
/****************************************************************
* DebugInit
* Initialize so that only DEBUG messages with a flag in flagList
* will be printed. If the flag is "+", we enable all DEBUG messages.
*****************************************************************/
static char *enableFlags = NULL;
void
DebugInit(char *flagList)
{
enableFlags = flagList;
}
/*****************************************************************
* DebugIsEnabled
* Return TRUE if DEBUG messages with "flag" are to be printed.
****************************************************************/
static bool
DebugIsEnabled(char flag)
{
if (enableFlags != NULL) {
return (strchr(enableFlags, flag) != 0) || (strchr(enableFlags, '+') != 0);
} else {
return FALSE;
}
}
/*****************************************************************
* Debug
* Print a debug message, if flag is enabled. Like printf,
* only with an extra argument on the front.
****************************************************************/
void
Debug(char flag, char *format, ...)
{
if (DebugIsEnabled(flag)) {
char llformat[1024];
va_list ap;
va_start(ap, format);
SubstituteLLx(llformat,format,1024);
/* Make this go to stderr also... */
vfprintf(stderr, llformat, ap);
vCPUPrint(llformat, ap);
fprintf(stderr, "\r");
fflush(stderr);
va_end(ap);
}
}
/*****************************************************************
* DebugDetail
* Print a detailed debug message, if flag is enabled. Like printf,
* only with an extra argument on the front.
****************************************************************/
void
DebugDetail(char flag, char *symbol, int cpuNum, char *text,...)
{
if (DebugIsEnabled(flag)) {
va_list ap;
SimTime x=0;
char cpuName[32];
char *pid, *proc;
sprintf(cpuName,"%d",cpuNum);
pid = Tcl_GetVar2(TCLInterp,"PID",cpuName,0);
proc = Tcl_GetVar2(TCLInterp,"PROCESS",cpuName,0);
if (!pid) { pid = "-1";}
if (!proc) { proc = "NONAME";}
if (CPUVec.CycleCount) {
x = CPUVec.CycleCount(cpuNum);
}
CPUPrint("LOG %lld\t%-12s\tcpu=%i\t%s-%s\t ",
(uint64)x,
symbol,
cpuNum,
pid, proc);
va_start(ap,text);
vCPUPrint(text,ap);
va_end(ap);
}
}