-
Notifications
You must be signed in to change notification settings - Fork 65
/
debug.c
222 lines (173 loc) · 4.49 KB
/
debug.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
#include "pharovm/pharo.h"
#include <stdarg.h>
#ifdef _WIN32
#else
#include <sys/time.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
char * GetAttributeString(sqInt id);
#if defined(DEBUG) && DEBUG
static int max_error_level = 4;
#else
static int max_error_level = 1;
#endif
/*
* This function set the logLevel to use in the VM
*
* LOG_NONE 0
* LOG_ERROR 1
* LOG_WARN 2
* LOG_INFO 3
* LOG_DEBUG 4
* LOG_TRACE 5
*
*/
EXPORT(void) logLevel(int value){
max_error_level = value;
}
EXPORT(int) getLogLevel(){
return max_error_level;
}
EXPORT(int) isLogDebug(){
return getLogLevel() >= LOG_DEBUG;
}
void error(char *errorMessage){
logError(errorMessage);
printStatusAfterError();
abort();
}
static const char* severityName[5] = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
EXPORT(void) logAssert(const char* fileName, const char* functionName, int line, char* msg){
logMessage(LOG_WARN, fileName, functionName, line, msg);
}
void logMessageFromErrno(int level, const char* msg, const char* fileName, const char* functionName, int line){
char buffer[1024+1];
#ifdef WIN32
strerror_s(buffer, 1024, errno);
#else
strerror_r(errno, buffer, 1024);
#endif
logMessage(level, fileName, functionName, line, "%s: %s", msg, buffer);
}
FILE* getStreamForLevel(int level){
if(level <= LOG_ERROR){
return stderr;
}else{
return stdout;
}
}
EXPORT(void) logMessage(int level, const char* fileName, const char* functionName, int line, ...){
char * format;
char timestamp[20];
FILE* outputStream;
outputStream = getStreamForLevel(level);
if(level > max_error_level){
return;
}
time_t now = time(NULL);
#if defined(_WIN32)
struct tm ltime_struct;
localtime_s(<ime_struct, &now);
struct tm* ltime = <ime_struct;
#else
struct tm* ltime = localtime(&now);
#endif
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", ltime);
//Printing the header.
// Ex: [DEBUG] 2017-11-14 21:57:53,661 functionName (filename:line) - This is a debug log message.
fprintf_impl(outputStream, "[%-5s] %s.%03d %s (%s:%d):", severityName[level - 1], timestamp, 0 /* milliseconds */ , functionName, fileName, line);
//Printint the message from the var_args.
va_list list;
va_start(list, line);
format = va_arg(list, char*);
vfprintf_impl(outputStream, format, list);
va_end(list);
int formatLength = strlen(format);
if(formatLength == 0 || format[formatLength - 1] != '\n'){
fprintf_impl(outputStream,"\n");
}
fflush(outputStream);
}
void getCrashDumpFilenameInto(char *buf)
{
#ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strcat_s(buf, PATH_MAX + 1, "crash.dmp");
#else
strcat(buf, "crash.dmp");
#endif
}
char *getVersionInfo(int verbose)
{
#if STACKVM
extern char *__interpBuildInfo;
# define INTERP_BUILD __interpBuildInfo
# if COGVM
extern char *__cogitBuildInfo;
# endif
#else
# define INTERP_BUILD interpreterVersion
#endif
extern char *revisionAsString();
#define BUFFER_SIZE 4096
char *info= (char *)malloc(BUFFER_SIZE);
info[0]= '\0';
#if SPURVM
# if BytesPerOop == 8
# define ObjectMemory " Spur 64-bit"
# else
# define ObjectMemory " Spur"
# endif
#else
# define ObjectMemory
#endif
#if defined(NDEBUG)
# define BuildVariant "Production" ObjectMemory
#elif DEBUGVM
# define BuildVariant "Debug" ObjectMemory
# else
# define BuildVariant "Assert" ObjectMemory
#endif
#if USE_XSHM
#define USE_XSHM_STRING " XShm"
#else
#define USE_XSHM_STRING ""
#endif
#if ITIMER_HEARTBEAT
# define HBID " ITHB"
#else
# define HBID
#endif
if(verbose){
snprintf(info, BUFFER_SIZE, IMAGE_DIALECT_NAME "VM version:" VM_VERSION "-" VM_BUILD_STRING USE_XSHM_STRING " " COMPILER_VERSION " [" BuildVariant HBID " VM]\nBuilt from: %s\n With:%s\n Revision: " VM_BUILD_SOURCE_STRING, INTERP_BUILD, GetAttributeString(1008));
}else{
snprintf(info, BUFFER_SIZE, VM_VERSION "-" VM_BUILD_STRING USE_XSHM_STRING " " COMPILER_VERSION " [" BuildVariant HBID " VM]\n%s\n%s\n" VM_BUILD_SOURCE_STRING, INTERP_BUILD, GetAttributeString(1008));
}
return info;
}
/***
* This SHOULD be rewritten passing the FILE* as a parameter.
*/
static FILE* outputStream = NULL;
void
vm_setVMOutputStream(FILE * stream){
fflush(outputStream);
outputStream = stream;
}
int
vm_printf(const char * format, ... ){
va_list list;
va_start(list, format);
if(outputStream == NULL){
outputStream = stdout;
}
int returnValue = vfprintf_impl(outputStream, format, list);
va_end(list);
return returnValue;
}