-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdebug.c
295 lines (262 loc) · 8.89 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
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
#include "debug.h"
#include "cilk-internal.h"
#include "global.h"
#include <assert.h>
#include <search.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if ALERT_LVL & (ALERT_CFRAME|ALERT_RETURN)
unsigned int alert_level = 0;
#else
CHEETAH_INTERNAL unsigned int alert_level = 0;
#endif
CHEETAH_INTERNAL unsigned int debug_level = 0;
/* To reduce overhead of logging messages are accumulated into memory
and written to stderr in batches of about 5,000 bytes. */
static size_t alert_log_size = 0, alert_log_offset = 0;
static char *alert_log = NULL;
/**
* Represents a usable alert level with a human-readable
* <code>name<\code> and the corresponding
* <code>mask_value<\code> used by the runtime.
**/
typedef struct __alert_level_t {
const char *name;
int mask_value;
} alert_level_t;
/**
* A table relating a human-readable alert level name to
* the corresponding bitfield value used by the runtime.
**/
static const alert_level_t alert_table[] = {
{"none", ALERT_NONE},
{"fiber", ALERT_FIBER},
{"memory", ALERT_MEMORY},
{"sync", ALERT_SYNC},
{"sched", ALERT_SCHED},
{"steal", ALERT_STEAL},
{"return", ALERT_RETURN},
{"except", ALERT_EXCEPT},
{"cframe", ALERT_CFRAME},
{"reduce", ALERT_REDUCE},
{"reduce_id", ALERT_REDUCE_ID},
{"boot", ALERT_BOOT},
{"start", ALERT_START},
{"closure", ALERT_CLOSURE},
{"nobuf", ALERT_NOBUF},
};
/**
* Compare the <code>name<\code> of the <code>alert_level_t<\code> pointed to by
* <code>left<code> to the <code>name<\code> of the <code>alert_level_t<\code>
* pointed to by <code>right<\code>, ignoring case.
*
* @param left <code>alert_level_t<\code> to compare
* @param right <code>alert_level_t<\code> to compare
*
* @return Ignoring case, returns negative if left->name < right->name,
* 0 if they are equal, or positive if left->name > right->name
**/
static int alert_name_comparison(const void *left, const void *right) {
const alert_level_t *al_left = (const alert_level_t*)left;
const alert_level_t *al_right = (const alert_level_t*)right;
// TODO: If Windows, use _stricmp
return strcasecmp(al_left->name, al_right->name);
}
/**
* Parse an alert level represented by a string into the proper bitmask value.
* If the string is not represented in <code>alert_table<\code>, then prints
* an error and returns ALERT_NONE.
*
* @param alert_str A C string to attempt to parse
*
* @return The bitmask corresponding to <code>alert_str<\code>, if in
* <code>alert_table<\code>, else ALERT_NONE.
**/
static int parse_alert_level_str(const char *const alert_str) {
size_t table_size = sizeof(alert_table) / sizeof(alert_table[0]);
const alert_level_t search_key = { .name = alert_str, .mask_value = ALERT_NONE };
// The table is small, and performance isn't critical for loading
// debug options, so use linear search (lfind)
alert_level_t *table_element =
(alert_level_t*)lfind(&search_key, alert_table, &table_size,
sizeof(search_key), alert_name_comparison
);
if (table_element != NULL) {
return table_element->mask_value;
}
fprintf(stderr, "Invalid CILK_ALERT value: %s\n", alert_str);
return ALERT_NONE;
}
/**
* Parse a CSV line representing which alert levels should be enabled, and
* return the bitmask representing all of the passed in options. If the CSV line
* is a single number, then treat that number as the bitmask.
* <code>alert_csv<\code> is copied, as <code>strtok<\code> is used, and it
* modifies its arguments.
*
* @param alert_csv A C string that is either a comma-separated list of alert
* level names -or- a single number.
*
* @return The bitmask representing the passed in
* <code>alert_csv<\code>. If <code>alert_csv<\code> cannot
* be copied, then returns the current
* <code>alert_level<\code> value.
**/
static int parse_alert_level_csv(const char *const alert_csv) {
int new_alert_lvl = ALERT_NONE;
size_t csv_len = strlen(alert_csv);
// strtok modifies the passed in string, so copy alert_csv and use
// the copy instead
char *alert_csv_cpy = strdup(alert_csv);
if (!alert_csv_cpy) {
// Non-critical error, so just print a warning
fprintf(stderr, "Cilk: unable to copy CILK_ALERT settings (%s)\n",
strerror(errno)
);
return alert_level;
}
char *alert_str = strtok(alert_csv_cpy, ",");
if (alert_str) {
if (strlen(alert_str) == csv_len) {
// Can be a number, as there is no other option in the string
char *tol_end = alert_csv_cpy;
new_alert_lvl = strtol(alert_csv_cpy, &tol_end, 0);
if (new_alert_lvl == 0 && (*tol_end != '\0' || tol_end == alert_csv_cpy)) {
new_alert_lvl |= parse_alert_level_str(alert_str);
}
} else {
for (; alert_str; alert_str = strtok(NULL, ",")) {
new_alert_lvl |= parse_alert_level_str(alert_str);
}
}
}
free(alert_csv_cpy);
return new_alert_lvl;
}
/**
* Parse a CSV line representing which alert levels should be enabled, and
* and set the current <code>alert_level<\code> bitamsk based on the result.
* If the passed in C string is NULL, then no change is made.
*
* @param alert_csv A C string that is either a comma-separated list of alert
* level names -or- a single number.
**/
void set_alert_level_from_str(const char *const alert_csv) {
if (alert_csv) {
int new_alert_lvl = parse_alert_level_csv(alert_csv);
set_alert_level(new_alert_lvl);
}
}
void set_alert_level(unsigned int level) {
alert_level = level;
if (level == 0) {
flush_alert_log();
return;
}
if (level & ALERT_NOBUF) {
return;
}
if (alert_log == NULL) {
alert_log_size = 5000;
alert_log = malloc(alert_log_size);
if (alert_log) {
memset(alert_log, ' ', alert_log_size);
}
}
}
void set_debug_level(unsigned int level) { debug_level = level; }
const char *const __cilkrts_assertion_failed =
"%s:%d: cilk assertion failed: %s\n";
void cilk_die_internal(struct global_state *const g, const char *fmt, ...) {
fflush(stdout);
va_list l;
va_start(l, fmt);
cilk_mutex_lock(&(g->print_lock));
flush_alert_log();
fprintf(stderr, "Fatal error: ");
vfprintf(stderr, fmt, l);
fputc('\n', stderr);
fflush(stderr);
cilk_mutex_unlock(&(g->print_lock));
exit(1);
}
CHEETAH_INTERNAL_NORETURN
void cilkrts_bug(const char *fmt, ...) {
fflush(NULL);
__cilkrts_worker *w = __cilkrts_get_tls_worker();
if (w) {
cilk_mutex_lock(&(w->g->print_lock));
flush_alert_log();
cilk_mutex_unlock(&(w->g->print_lock));
fprintf(stderr, "[W%02u]: ", w->self);
}
/* Without a worker there is no safe way to flush the log */
va_list l;
va_start(l, fmt);
vfprintf(stderr, fmt, l);
va_end(l);
fputc('\n', stderr);
fflush(stderr);
abort(); // generate core file
}
void flush_alert_log() {
if (ALERT_LVL == 0)
return;
if (alert_log == NULL) {
return;
}
if (alert_log_offset > 0) {
fflush(stdout);
fwrite(alert_log, 1, alert_log_offset, stderr);
alert_log_offset = 0;
}
alert_log_size = 0;
free(alert_log);
alert_log = NULL;
}
#undef cilkrts_alert
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#if !(ALERT_LVL & (ALERT_CFRAME|ALERT_RETURN))
CHEETAH_INTERNAL
#endif
void cilkrts_alert(const int lvl, const char *fmt, ...) {
if ((ALERT_LVL & lvl) == 0)
return;
char prefix[10], body[200];
size_t size1 = 0, size2 = 0;
__cilkrts_worker *w = __cilkrts_get_tls_worker();
if (w) {
size1 = snprintf(prefix, sizeof prefix, "[W%02u]: ", w->self);
assert(size1 >= 7 && size1 < 10);
}
{
va_list l;
va_start(l, fmt);
int tmp = vsnprintf(body, sizeof body, fmt, l);
assert(tmp >= 0);
size2 = tmp;
if (size2 > sizeof body - 1)
size2 = sizeof body - 1;
va_end(l);
}
pthread_mutex_lock(&lock);
if (alert_log) {
if (alert_log_offset + size1 + size2 + 1 >= alert_log_size) {
fwrite(alert_log, 1, alert_log_offset, stderr);
memset(alert_log, ' ', alert_log_offset);
alert_log_offset = 0;
}
memcpy(alert_log + alert_log_offset, prefix, size1);
memcpy(alert_log + alert_log_offset + size1, body, size2);
alert_log[alert_log_offset + size1 + size2] = '\n';
alert_log_offset += size1 + size2 + 1;
} else {
if (w)
fprintf(stderr, "%s%s\n", prefix, body);
else
fprintf(stderr, "%s\n", body);
}
pthread_mutex_unlock(&lock);
}