Statistics
| Branch: | Revision:

root / tests / testsig.c @ 9de5e440

History | View | Annotate | Download (2.4 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
void dump_regs(struct ucontext *uc)
19
{
20
    printf("EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
21
           "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
22
           "EFL=%08x EIP=%08x\n",
23
           uc->uc_mcontext.gregs[EAX],
24
           uc->uc_mcontext.gregs[EBX],
25
           uc->uc_mcontext.gregs[ECX],
26
           uc->uc_mcontext.gregs[EDX],
27
           uc->uc_mcontext.gregs[ESI],
28
           uc->uc_mcontext.gregs[EDI],
29
           uc->uc_mcontext.gregs[EBP],
30
           uc->uc_mcontext.gregs[ESP],
31
           uc->uc_mcontext.gregs[EFL],
32
           uc->uc_mcontext.gregs[EIP]);
33
}
34

    
35
void sig_handler(int sig, siginfo_t *info, void *puc)
36
{
37
    struct ucontext *uc = puc;
38

    
39
    printf("%s: si_signo=%d si_errno=%d si_code=%d si_addr=0x%08lx\n",
40
           strsignal(info->si_signo),
41
           info->si_signo, info->si_errno, info->si_code, 
42
           (unsigned long)info->si_addr);
43
    dump_regs(uc);
44
    longjmp(jmp_env, 1);
45
}
46

    
47
int v1;
48

    
49
int main(int argc, char **argv)
50
{
51
    struct sigaction act;
52
    int i;
53
    
54
    /* test division by zero reporting */
55
    if (setjmp(jmp_env) == 0) {
56
        act.sa_sigaction = sig_handler;
57
        sigemptyset(&act.sa_mask);
58
        act.sa_flags = SA_SIGINFO | SA_ONESHOT;
59
        sigaction(SIGFPE, &act, NULL);
60
        
61
        /* now divide by zero */
62
        v1 = 0;
63
        v1 = 2 / v1;
64
    }
65

    
66
    /* test illegal instruction reporting */
67
    if (setjmp(jmp_env) == 0) {
68
        act.sa_sigaction = sig_handler;
69
        sigemptyset(&act.sa_mask);
70
        act.sa_flags = SA_SIGINFO | SA_ONESHOT;
71
        sigaction(SIGILL, &act, NULL);
72
        
73
        /* now execute an invalid instruction */
74
        asm volatile("ud2");
75
    }
76
    
77
    /* test SEGV reporting */
78
    if (setjmp(jmp_env) == 0) {
79
        act.sa_sigaction = sig_handler;
80
        sigemptyset(&act.sa_mask);
81
        act.sa_flags = SA_SIGINFO | SA_ONESHOT;
82
        sigaction(SIGSEGV, &act, NULL);
83
        
84
        /* now store in an invalid address */
85
        *(char *)0x1234 = 1;
86
    }
87
    
88
    act.sa_handler = alarm_handler;
89
    sigemptyset(&act.sa_mask);
90
    act.sa_flags = 0;
91
    sigaction(SIGALRM, &act, NULL);
92
    alarm(1);
93
    for(i = 0;i < 2; i++) {
94
        sleep(1);
95
    }
96
    return 0;
97
}