comparison src/SDL_assert.c @ 3661:22b6a0c7ea6e

Handle assertion failures during assertion handler.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 13 Jan 2010 09:13:37 +0000
parents eaea59cee6f2
children 6a0b3048f271
comparison
equal deleted inserted replaced
3660:2d17445190f6 3661:22b6a0c7ea6e
40 #if !SDL_ASSERTION_REPORT_DISABLED 40 #if !SDL_ASSERTION_REPORT_DISABLED
41 static SDL_assert_data assertion_list_terminator = { 0, 0, 0, 0, 0, 0, 0 }; 41 static SDL_assert_data assertion_list_terminator = { 0, 0, 0, 0, 0, 0, 0 };
42 static SDL_assert_data *triggered_assertions = &assertion_list_terminator; 42 static SDL_assert_data *triggered_assertions = &assertion_list_terminator;
43 #endif 43 #endif
44 44
45 static void
46 debug_print(const char *fmt, ...)
47 #ifdef __GNUC__ 45 #ifdef __GNUC__
48 __attribute__((format (printf, 1, 2))) 46 static void
49 #endif 47 debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
50 ; 48 #endif
51 49
52 static void 50 static void
53 debug_print(const char *fmt, ...) 51 debug_print(const char *fmt, ...)
54 { 52 {
55 #ifdef _WINDOWS 53 #ifdef _WINDOWS
237 triggered_assertions = &assertion_list_terminator; 235 triggered_assertions = &assertion_list_terminator;
238 } 236 }
239 #endif 237 #endif
240 } 238 }
241 239
242 240 static void SDL_ExitProcess(int exitcode)
243 static void SDL_AbortAssertion(void) 241 {
244 {
245 SDL_Quit();
246 #ifdef _WINDOWS 242 #ifdef _WINDOWS
247 ExitProcess(42); 243 ExitProcess(42);
248 #else 244 #else
249 _exit(42); 245 _exit(42);
250 #endif 246 #endif
251 } 247 }
252 248
249 static void SDL_AbortAssertion(void)
250 {
251 SDL_Quit();
252 SDL_ExitProcess(42);
253 }
254
253 255
254 static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data) 256 static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data)
255 { 257 {
256 const char *envr; 258 const char *envr;
257 SDL_assert_state state = SDL_ASSERTION_ABORT; 259 SDL_assert_state state = SDL_ASSERTION_ABORT;
346 348
347 SDL_assert_state 349 SDL_assert_state
348 SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file, 350 SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
349 int line) 351 int line)
350 { 352 {
351 SDL_assert_state state; 353 static int assertion_running = 0;
354 SDL_assert_state state = SDL_ASSERTION_IGNORE;
352 355
353 if (SDL_LockMutex(assertion_mutex) < 0) { 356 if (SDL_LockMutex(assertion_mutex) < 0) {
354 return SDL_ASSERTION_IGNORE; /* oh well, I guess. */ 357 return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
355 } 358 }
356 359
362 } 365 }
363 366
364 SDL_AddAssertionToReport(data); 367 SDL_AddAssertionToReport(data);
365 368
366 data->trigger_count++; 369 data->trigger_count++;
367 if (data->always_ignore) { 370
368 SDL_UnlockMutex(assertion_mutex); 371 assertion_running++;
369 return SDL_ASSERTION_IGNORE; 372 if (assertion_running > 1) { /* assert during assert! Abort. */
370 } 373 if (assertion_running == 2) {
371 374 SDL_AbortAssertion();
372 state = SDL_PromptAssertion(data); 375 } else if (assertion_running == 3) { /* Abort asserted! */
376 SDL_ExitProcess(42);
377 } else {
378 while (1) { /* do nothing but spin; what else can you do?! */ }
379 }
380 }
381
382 if (!data->always_ignore) {
383 state = SDL_PromptAssertion(data);
384 }
373 385
374 switch (state) 386 switch (state)
375 { 387 {
376 case SDL_ASSERTION_ABORT: 388 case SDL_ASSERTION_ABORT:
377 SDL_UnlockMutex(assertion_mutex); /* in case we assert in quit. */
378 SDL_AbortAssertion(); 389 SDL_AbortAssertion();
379 return SDL_ASSERTION_IGNORE; /* shouldn't return, but oh well. */ 390 return SDL_ASSERTION_IGNORE; /* shouldn't return, but oh well. */
380 391
381 case SDL_ASSERTION_ALWAYS_IGNORE: 392 case SDL_ASSERTION_ALWAYS_IGNORE:
382 state = SDL_ASSERTION_IGNORE; 393 state = SDL_ASSERTION_IGNORE;
387 case SDL_ASSERTION_RETRY: 398 case SDL_ASSERTION_RETRY:
388 case SDL_ASSERTION_BREAK: 399 case SDL_ASSERTION_BREAK:
389 break; /* macro handles these. */ 400 break; /* macro handles these. */
390 } 401 }
391 402
403 assertion_running--;
392 SDL_UnlockMutex(assertion_mutex); 404 SDL_UnlockMutex(assertion_mutex);
393 405
394 return state; 406 return state;
395 } 407 }
396 408