History

tccrun: win32: improve exception handler

grischka committed 16 years ago

Commit d483ab31 parent 08083dd1 file · +29 −28Browse files

Unified · Split

tccrun.c+29−28
@@ -76,6 +76,7 @@ int tcc_relocate(TCCState *s1) · 75 unmodified lines
7676 int tcc_run(TCCState *s1, int argc, char **argv)
7777 {
7878 int (*prog_main)(int, char **);
79+ int ret;
7980
8081 if (tcc_relocate(s1) < 0)
8182 return -1;
@@ -83,38 +84,30 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 1 unmodified line
8384 prog_main = tcc_get_symbol_err(s1, "main");
8485
8586 #ifdef CONFIG_TCC_BACKTRACE
86− if (s1->do_debug)
87+ if (s1->do_debug) {
8788 set_exception_handler();
89+ rt_prog_main = prog_main;
90+ }
8891 #endif
8992
9093 #ifdef CONFIG_TCC_BCHECK
9194 if (s1->do_bounds_check) {
9295 void (*bound_init)(void);
9396 void (*bound_exit)(void);
94− int ret;
9597 /* set error function */
9698 rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
97− rt_prog_main = prog_main;
9899 /* XXX: use .init section so that it also work in binary ? */
99100 bound_init = tcc_get_symbol_err(s1, "__bound_init");
100101 bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
101102 bound_init();
102103 ret = (*prog_main)(argc, argv);
103104 bound_exit();
104− return ret;
105− }
106−#endif
107−
108−#ifdef TCC_TARGET_PE
109− {
110− unsigned char *p = tcc_get_symbol(s1, "tinyc_no_getbp");
111− if (p) *p = 0;
112− }
105+ } else
113106 #endif
114− return (*prog_main)(argc, argv);
107+ ret = (*prog_main)(argc, argv);
108+ return ret;
115109 }
116110
117−
118111 /* relocate code. Return -1 on error, required size if ptr is NULL,
119112 otherwise copy code into buffer passed by the caller */
120113 static int tcc_relocate_ex(TCCState *s1, void *ptr)
@@ -370,9 +363,10 @@ static void rt_error(ucontext_t *uc, const char *fmt, ...) · 249 unmodified lines
370363 uplong pc;
371364 int i;
372365
373− va_start(ap, fmt);
374366 fprintf(stderr, "Runtime error: ");
367+ va_start(ap, fmt);
375368 vfprintf(stderr, fmt, ap);
369+ va_end(ap);
376370 fprintf(stderr, "\n");
377371
378372 for(i=0;i<num_callers;i++) {
@@ -382,8 +376,6 @@ static void rt_error(ucontext_t *uc, const char *fmt, ...) · 3 unmodified lines
382376 if (pc == (uplong)rt_prog_main && pc)
383377 break;
384378 }
385− exit(255);
386− va_end(ap);
387379 }
388380
389381 /* ------------------------------------------------------------- */
@@ -581,18 +573,27 @@ static int rt_get_caller_pc(unsigned long *paddr, · 191 unmodified lines
581573
582574 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
583575 {
584− CONTEXT *uc = ex_info->ContextRecord;
585−/*
586576 EXCEPTION_RECORD *er = ex_info->ExceptionRecord;
587− printf("CPU exception: code=%08lx addr=%p\n",
588− er->ExceptionCode, er->ExceptionAddress);
589−*/
590− if (rt_bound_error_msg && *rt_bound_error_msg)
591− rt_error(uc, *rt_bound_error_msg);
592− else
593− rt_error(uc, "dereferencing invalid pointer");
594− exit(255);
595− //return EXCEPTION_CONTINUE_SEARCH;
577+ CONTEXT *uc = ex_info->ContextRecord;
578+ switch (er->ExceptionCode) {
579+ case EXCEPTION_ACCESS_VIOLATION:
580+ if (rt_bound_error_msg && *rt_bound_error_msg)
581+ rt_error(uc, *rt_bound_error_msg);
582+ else
583+ rt_error(uc, "access violation");
584+ break;
585+ case EXCEPTION_STACK_OVERFLOW:
586+ rt_error(uc, "stack overflow");
587+ break;
588+ case EXCEPTION_INT_DIVIDE_BY_ZERO:
589+ rt_error(uc, "division by zero");
590+ break;
591+ default:
592+ rt_error(uc, "exception caught");
593+ break;
594+ }
595+ exit(-1);
596+ return EXCEPTION_CONTINUE_SEARCH;
596597 }
597598
598599 /* Generate a stack backtrace when a CPU exception occurs. */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t