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