2 * Windows specific functions for TCC
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* return the PC at frame level 'level'. Return non zero if not found */
23 static int rt_get_caller_pc(unsigned long *paddr
,
24 CONTEXT
*uc
, int level
)
34 for(i
=1;i
<level
;i
++) {
35 /* XXX: check address validity with program info */
36 if (fp
<= 0x1000 || fp
>= 0xc0000000)
38 fp
= ((unsigned long *)fp
)[0];
40 *paddr
= ((unsigned long *)fp
)[1];
45 /* emit a run time error at position 'pc' */
46 static void rt_error(CONTEXT
*uc
, const char *fmt
, ...)
53 fprintf(stderr
, "Runtime error: ");
54 vfprintf(stderr
, fmt
, ap
);
55 fprintf(stderr
, "\n");
56 for(i
=0;i
<num_callers
;i
++) {
57 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
60 fprintf(stderr
, "at ");
62 fprintf(stderr
, "by ");
63 pc
= rt_printline(pc
);
64 if (pc
== rt_prog_main
&& pc
)
70 static long __stdcall
cpu_exception_handler(EXCEPTION_POINTERS
*ex_info
)
72 printf("CPU exception: code=0x%8.8x addr=0x%8.8x",
73 ex_info
->ExceptionRecord
->ExceptionCode
,
74 ex_info
->ExceptionRecord
->ExceptionAddress
);
76 rt_error(ex_info
->ContextRecord
, "error");
78 /* TODO: Call longjmp here to return control to user code. */
80 return EXCEPTION_CONTINUE_SEARCH
;
83 /* Generate a stack backtrace when a CPU exception occurs. */
84 void handle_cpu_exception(void)
86 SetUnhandledExceptionFilter(cpu_exception_handler
);