root / tests / testsig.c @ fe1e3ce3
History | View | Annotate | Download (2.7 kB)
1 | 9de5e440 | bellard | #define _GNU_SOURCE
|
---|---|---|---|
2 | 1b6b029e | bellard | #include <stdlib.h> |
3 | 1b6b029e | bellard | #include <stdio.h> |
4 | 9de5e440 | bellard | #include <string.h> |
5 | 1b6b029e | bellard | #include <signal.h> |
6 | 1b6b029e | bellard | #include <unistd.h> |
7 | 9de5e440 | bellard | #include <setjmp.h> |
8 | 9de5e440 | bellard | #include <sys/ucontext.h> |
9 | 9de5e440 | bellard | |
10 | 9de5e440 | bellard | jmp_buf jmp_env; |
11 | 1b6b029e | bellard | |
12 | 1b6b029e | bellard | void alarm_handler(int sig) |
13 | 1b6b029e | bellard | { |
14 | 1b6b029e | bellard | printf("alarm signal=%d\n", sig);
|
15 | 1b6b029e | bellard | alarm(1);
|
16 | 1b6b029e | bellard | } |
17 | 1b6b029e | bellard | |
18 | d691f669 | bellard | #ifndef REG_EAX
|
19 | d691f669 | bellard | #define REG_EAX EAX
|
20 | d691f669 | bellard | #define REG_EBX EBX
|
21 | d691f669 | bellard | #define REG_ECX ECX
|
22 | d691f669 | bellard | #define REG_EDX EDX
|
23 | d691f669 | bellard | #define REG_ESI ESI
|
24 | d691f669 | bellard | #define REG_EDI EDI
|
25 | d691f669 | bellard | #define REG_EBP EBP
|
26 | d691f669 | bellard | #define REG_ESP ESP
|
27 | d691f669 | bellard | #define REG_EIP EIP
|
28 | d691f669 | bellard | #define REG_EFL EFL
|
29 | d691f669 | bellard | #endif
|
30 | d691f669 | bellard | |
31 | 9de5e440 | bellard | void dump_regs(struct ucontext *uc) |
32 | 9de5e440 | bellard | { |
33 | 9de5e440 | bellard | printf("EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
|
34 | 9de5e440 | bellard | "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
|
35 | 9de5e440 | bellard | "EFL=%08x EIP=%08x\n",
|
36 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EAX], |
37 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EBX], |
38 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_ECX], |
39 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EDX], |
40 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_ESI], |
41 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EDI], |
42 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EBP], |
43 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_ESP], |
44 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EFL], |
45 | d691f669 | bellard | uc->uc_mcontext.gregs[REG_EIP]); |
46 | 9de5e440 | bellard | } |
47 | 9de5e440 | bellard | |
48 | 9de5e440 | bellard | void sig_handler(int sig, siginfo_t *info, void *puc) |
49 | 9de5e440 | bellard | { |
50 | 9de5e440 | bellard | struct ucontext *uc = puc;
|
51 | 9de5e440 | bellard | |
52 | 9de5e440 | bellard | printf("%s: si_signo=%d si_errno=%d si_code=%d si_addr=0x%08lx\n",
|
53 | 9de5e440 | bellard | strsignal(info->si_signo), |
54 | 9de5e440 | bellard | info->si_signo, info->si_errno, info->si_code, |
55 | 9de5e440 | bellard | (unsigned long)info->si_addr); |
56 | 9de5e440 | bellard | dump_regs(uc); |
57 | 9de5e440 | bellard | longjmp(jmp_env, 1);
|
58 | 9de5e440 | bellard | } |
59 | 9de5e440 | bellard | |
60 | 9de5e440 | bellard | int v1;
|
61 | 9de5e440 | bellard | |
62 | 1b6b029e | bellard | int main(int argc, char **argv) |
63 | 1b6b029e | bellard | { |
64 | 1b6b029e | bellard | struct sigaction act;
|
65 | 9de5e440 | bellard | int i;
|
66 | 9de5e440 | bellard | |
67 | 9de5e440 | bellard | /* test division by zero reporting */
|
68 | 9de5e440 | bellard | if (setjmp(jmp_env) == 0) { |
69 | 9de5e440 | bellard | act.sa_sigaction = sig_handler; |
70 | 9de5e440 | bellard | sigemptyset(&act.sa_mask); |
71 | 9de5e440 | bellard | act.sa_flags = SA_SIGINFO | SA_ONESHOT; |
72 | 9de5e440 | bellard | sigaction(SIGFPE, &act, NULL);
|
73 | 9de5e440 | bellard | |
74 | 9de5e440 | bellard | /* now divide by zero */
|
75 | 9de5e440 | bellard | v1 = 0;
|
76 | 9de5e440 | bellard | v1 = 2 / v1;
|
77 | 9de5e440 | bellard | } |
78 | 9de5e440 | bellard | |
79 | 9de5e440 | bellard | /* test illegal instruction reporting */
|
80 | 9de5e440 | bellard | if (setjmp(jmp_env) == 0) { |
81 | 9de5e440 | bellard | act.sa_sigaction = sig_handler; |
82 | 9de5e440 | bellard | sigemptyset(&act.sa_mask); |
83 | 9de5e440 | bellard | act.sa_flags = SA_SIGINFO | SA_ONESHOT; |
84 | 9de5e440 | bellard | sigaction(SIGILL, &act, NULL);
|
85 | 9de5e440 | bellard | |
86 | 9de5e440 | bellard | /* now execute an invalid instruction */
|
87 | 9de5e440 | bellard | asm volatile("ud2"); |
88 | 9de5e440 | bellard | } |
89 | 9de5e440 | bellard | |
90 | 9de5e440 | bellard | /* test SEGV reporting */
|
91 | 9de5e440 | bellard | if (setjmp(jmp_env) == 0) { |
92 | 9de5e440 | bellard | act.sa_sigaction = sig_handler; |
93 | 9de5e440 | bellard | sigemptyset(&act.sa_mask); |
94 | 9de5e440 | bellard | act.sa_flags = SA_SIGINFO | SA_ONESHOT; |
95 | 9de5e440 | bellard | sigaction(SIGSEGV, &act, NULL);
|
96 | 9de5e440 | bellard | |
97 | 9de5e440 | bellard | /* now store in an invalid address */
|
98 | 9de5e440 | bellard | *(char *)0x1234 = 1; |
99 | 9de5e440 | bellard | } |
100 | 9de5e440 | bellard | |
101 | 1b6b029e | bellard | act.sa_handler = alarm_handler; |
102 | 1b6b029e | bellard | sigemptyset(&act.sa_mask); |
103 | 1b6b029e | bellard | act.sa_flags = 0;
|
104 | 1b6b029e | bellard | sigaction(SIGALRM, &act, NULL);
|
105 | 1b6b029e | bellard | alarm(1);
|
106 | 9de5e440 | bellard | for(i = 0;i < 2; i++) { |
107 | 1b6b029e | bellard | sleep(1);
|
108 | 1b6b029e | bellard | } |
109 | 1b6b029e | bellard | return 0; |
110 | 1b6b029e | bellard | } |