Subject | Hash | Author | Date (UTC) |
---|---|---|---|
Initial commit. | 86c80da5a6a677f1686e50996b61fd51632259ba | Jan Allersma | 2018-05-18 13:12:38 |
File | Lines added | Lines deleted |
---|---|---|
.gitignore | 4 | 0 |
README.md | 35 | 0 |
main.cpp | 60 | 0 |
File .gitignore added (mode: 100644) (index 0000000..01db905) | |||
1 | * | ||
2 | !.gitignore | ||
3 | !main.cpp | ||
4 | !README.md |
File README.md added (mode: 100644) (index 0000000..4945c5f) | |||
1 | # Fault handling | ||
2 | |||
3 | ## Compiling | ||
4 | |||
5 | > Commands executed from repository's root. | ||
6 | |||
7 | ``` | ||
8 | g++ main.cpp -o exe | ||
9 | g++ main.cpp -g -o dbgexe | ||
10 | ``` | ||
11 | |||
12 | ## Post mortem debugging | ||
13 | |||
14 | Running `./exe` results in this output: | ||
15 | |||
16 | ``` | ||
17 | Fout 11 at 0: | ||
18 | Called from 4197438. | ||
19 | Aborted (geheugendump gemaakt) | ||
20 | ``` | ||
21 | |||
22 | See what caused the error: | ||
23 | |||
24 | > Use the output from running ./exe ('Called from 4197438'). | ||
25 | |||
26 | ``` | ||
27 | gdb ./dbgexe | ||
28 | l *4197438 | ||
29 | ``` | ||
30 | |||
31 | As `gdb` states, the error was caused at line 57: | ||
32 | |||
33 | ``` | ||
34 | 0x400c3e is in main() (main.cpp:57). | ||
35 | ``` |
File main.cpp added (mode: 100644) (index 0000000..60d05db) | |||
1 | #include <list> | ||
2 | #include <iostream> | ||
3 | |||
4 | #include <unistd.h> | ||
5 | #include <signal.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <sys/types.h> | ||
8 | #include <sys/wait.h> | ||
9 | #include <signal.h> | ||
10 | |||
11 | #include <sys/ucontext.h> | ||
12 | #include <ucontext.h> | ||
13 | |||
14 | void setHandler(void (*handler)(int,siginfo_t *,void *)) { | ||
15 | struct sigaction action; | ||
16 | action.sa_flags = SA_SIGINFO; | ||
17 | action.sa_sigaction = handler; | ||
18 | |||
19 | if (sigaction(SIGFPE, &action, NULL) == -1) { | ||
20 | std::cerr << "sigfpe: sigaction"; | ||
21 | _exit(1); | ||
22 | } | ||
23 | if (sigaction(SIGSEGV, &action, NULL) == -1) { | ||
24 | std::cerr << "sigsegv: sigaction"; | ||
25 | _exit(1); | ||
26 | } | ||
27 | if (sigaction(SIGILL, &action, NULL) == -1) { | ||
28 | std::cerr << "sigill: sigaction"; | ||
29 | _exit(1); | ||
30 | } | ||
31 | if (sigaction(SIGBUS, &action, NULL) == -1) { | ||
32 | std::cerr << "sigbus: sigaction"; | ||
33 | _exit(1); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | void faultHandler(int signo, siginfo_t* info, void* extra) { | ||
38 | int i; | ||
39 | |||
40 | i = ((ucontext_t*)extra)->uc_mcontext.gregs[REG_RIP]; // x86_64 | ||
41 | //i = ((ucontext_t*)extra)->uc_mcontext.arm_pc; // ARM | ||
42 | |||
43 | std::cout << "Fout " << (int)signo << " at "; | ||
44 | std::cout << (int&)info->si_addr << ": " << std::endl; | ||
45 | std::cout << "Called from " << (int)i << "." << std::endl; | ||
46 | |||
47 | abort(); | ||
48 | } | ||
49 | |||
50 | int main() { | ||
51 | std::list<size_t*> subIterators; | ||
52 | size_t pubIterator = 0; | ||
53 | int* badptr = nullptr; | ||
54 | |||
55 | setHandler(faultHandler); | ||
56 | |||
57 | *badptr = 15; | ||
58 | |||
59 | return 0; | ||
60 | } |