Newer
Older
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
// -*- mode: c++; c-file-style: "k&r"; c-basic-offset: 4 -*-
/***********************************************************************
*
* message.cc:
* logging functions
*
* Copyright 2013 Dan R. K. Ports <drkp@cs.washington.edu>
* Copyright 2009-2012 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************/
#include "message.h"
#include <ctype.h>
#include <errno.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <mutex>
#define BACKTRACE_ON_PANIC 1
#if BACKTRACE_ON_PANIC
#include <execinfo.h>
#endif
#define TIMESTAMP_BASE62 0
#define TIMESTAMP_NUMERIC 1
std::mutex mtx;
void __attribute__((weak))
Message_VA(enum Message_Type type,
const char *fname, int line, const char *func,
const char *fmt, va_list args)
{
_Message_VA(type, stderr, fname, line, func, fmt, args);
}
void
_Message(enum Message_Type type,
const char *fname, int line, const char *func,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
Message_VA(type, fname, line, func, fmt, args);
va_end(args);
Message_DoFrees();
}
void
_Message_VA(enum Message_Type type, FILE *fp,
const char *fname, int line, const char *func,
const char *fmt, va_list args)
{
// Lock mutex to make sure the output is not mangled.
mtx.lock();
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
static int haveColor = -1;
struct msg_desc {
const char *prefix;
const char *color;
};
const msg_desc descs[MSG_NUM_TYPES+1] = {
{"PANIC", "1;31"},
{"!", "1;33"},
{"*", 0},
{" ", "22;37"},
{"<Invalid message type>", 0},
};
if (haveColor == -1)
haveColor = isatty(fileno(fp));
int nDesc = type & (~MSG_PERROR);
if (nDesc > MSG_NUM_TYPES)
nDesc = MSG_NUM_TYPES;
if (haveColor && descs[nDesc].color)
fprintf(fp, "\033[%sm", descs[nDesc].color);
#if TIMESTAMP_NUMERIC || TIMESTAMP_BASE62
struct timeval tv;
if (gettimeofday(&tv, NULL) >= 0) {
if (TIMESTAMP_NUMERIC) {
struct tm *tm = localtime(&tv.tv_sec);
fprintf(fp,
"%04d%02d%02d-%02d%02d%02d-%04d %05d ",
1900+tm->tm_year, tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
(int)(tv.tv_usec / 100), getpid());
} else if (TIMESTAMP_BASE62) {
static const char base62[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
// Convert to tenths of milliseconds.
uint64_t val = (uint64_t)tv.tv_sec * 10000 +
tv.tv_usec / 100;
char buf[5];
for (size_t i = 0; i < sizeof buf; ++i) {
buf[sizeof buf - i - 1] =
base62[val % 62];
val /= 62;
}
fprintf(fp, "%.*s %05d ", (int)sizeof buf, buf,
getpid());
}
}
#endif // TIMESTAMP_NUMERIC || TIMESTAMP_BASE62
fprintf(fp, "%s ", descs[nDesc].prefix);
if (fname) {
const char *fbasename = strrchr(fname, '/');
if (fbasename)
++fbasename;
else
fbasename = fname;
char filepos[32];
snprintf(filepos, sizeof(filepos)/sizeof(filepos[0]),
"(%s:%d):", fbasename, line);
fprintf(fp, "%-15s %-19s ",
func, filepos);
}
vfprintf(fp, fmt, args);
if (type & MSG_PERROR)
fprintf(fp, ": %s", strerror(errno));
if (haveColor && descs[nDesc].color)
fputs("\033[0m", fp);
fprintf(fp, "\n");
fflush(fp);
// Unlock mutex.
mtx.unlock();
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
}
void _Panic(void)
{
#if BACKTRACE_ON_PANIC
void *bt[100];
size_t size = backtrace(bt, 100);
char **strings = backtrace_symbols(bt, size);
if (strings) {
for (unsigned int i = 0; i < size; ++i) {
Warning("%s", strings[i]);
}
}
#endif
abort();
exit(1);
}
#define MAX_DEFERRED_FREES 16
static void *deferredFrees[MAX_DEFERRED_FREES];
static int nDeferredFrees;
const char *
Message_DFree(char *buf)
{
if (buf) {
if (nDeferredFrees == MAX_DEFERRED_FREES)
Panic("Too many deferred frees");
deferredFrees[nDeferredFrees++] = buf;
}
return buf;
}
void
Message_DoFrees(void)
{
for (int i = 0; i < nDeferredFrees; ++i)
free(deferredFrees[i]);
nDeferredFrees = 0;
}
bool
_Message_DebugEnabled(const char *fname)
{
static bool parsed;
static char *buf;
static char **pats;
static int nPats;
if (!parsed) {
parsed = true;
const char *env = getenv("DEBUG");
if (env && strlen(env)) {
buf = strdup(env);
if (!buf)
Panic("Failed to allocate buffer");
nPats = 1;
for (size_t i = 0; i < strlen(buf); ++i) {
if (buf[i] == ',' || buf[i] == ' ')
++nPats;
}
pats = (char **)malloc(nPats * sizeof *pats);
if (!pats)
Panic("Failed to allocate buffer");
pats[0] = buf;
int patOut = 1;
for (size_t i = 0; i < strlen(buf); ++i) {
if (buf[i] == ',' || buf[i] == ' ') {
pats[patOut] = &buf[i+1];
buf[i] = '\0';
}
}
}
}
if (!buf)
return false;
bool result = false;
if (strcmp(pats[0], "all") == 0 || pats[0][0] == '^')
result = true;
const char *fbasename = strrchr(fname, '/');
if (fbasename)
++fbasename;
for (int i = 0; i < nPats; ++i) {
char *pat = pats[i];
if (pat[0] == '^')
++pat;
if (fnmatch(pat, fname, FNM_PATHNAME) == 0 ||
(fbasename &&
fnmatch(pat, fbasename, FNM_PATHNAME) == 0)) {
if (pats[i][0] == '^')
result = false;
else
result = true;
}
}
return result;
}
void
_Message_Hexdump(const void *data, int len)
{
const unsigned char *d = (const unsigned char *)data;
char buf[80];
char *out;
for (int base = 0; base < len; base += 16) {
out = buf;
sprintf(out, "%08x", base);
out += 8;
for (int offset = 0; offset < 16; ++offset) {
if (offset == 8)
*(out++) = ' ';
if (base + offset >= len) {
strcpy(out, " ");
} else {
sprintf(out, " %02x", d[base+offset]);
}
out += 3;
}
strcpy(out, " |");
out += 2;
for (int offset = 0; offset < 16; ++offset) {
if (base + offset >= len)
break;
else if (isprint(d[base+offset]))
*out = d[base+offset];
else
*out = '.';
++out;
}
strcpy(out, "|");
_Message(MSG_DEBUG, NULL, 0, NULL, "%s", buf);
}
}
char *
Message_FmtBlob(const void *data, int len)
{
static int blobmax = -1;
if (blobmax == -1) {
const char *env = getenv("BLOBMAX");
if (!env) {
blobmax = 32;
} else {
blobmax = atoi(env);
}
}
int plen = len;
if (plen > blobmax)
plen = blobmax;
char *buf = (char *)malloc(plen + 3);
if (!buf)
return NULL;
buf[0] = '|';
int i;
for (i = 0; i < plen; ++i) {
if (isprint(((char*)data)[i]))
buf[i+1] = ((char*)data)[i];
else
buf[i+1] = '.';
}
if (len != plen)
buf[i+1] = '>';
else
buf[i+1] = '|';
buf[i+2] = '\0';
return buf;
}
void
PanicOnSignal(int signo)
{
Panic("Received signal %d", signo);
}