forked from nothingelsematters/sigsegv-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandler.cpp
122 lines (107 loc) · 4.16 KB
/
Handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Created by Павел Пономарев on 2019-06-06.
//
#include "Handler.h"
#include <iostream>
#include <cmath>
#include <climits>
#include <string.h>
namespace Writer {
void partialWrite(uint8_t n) {
char c = n < 10 ? n + '0' : n + 'A' - 10;
write(1, &c, 1);
}
void writeByte(uint8_t byte) {
partialWrite(byte / 16);
partialWrite(byte % 16);
}
void writeSafe(const char* chars) {
write(1, chars, strlen(chars));
}
void writeSafe(uint64_t num) {
for (int i = 7; i >= 0; --i) {
writeByte(0xFF && (num >> (8 * i)));
}
}
void writeSafeRegister(const char* reg, greg_t greg) {
writeSafe(reg);
writeSafe(" --- ");
writeSafe(greg);
writeSafe("\n");
}
};
void Handler::handleSignal(int signum, siginfo_t* info, void* context) {
if (info->si_signo == SIGSEGV) {
Writer::writeSafe("Aborted: ");
Writer::writeSafe(strsignal(signum));
Writer::writeSafe("\n");
switch (info->si_code) {
case SEGV_ACCERR:
Writer::writeSafe("SEGV_ACCERR\n");
break;
case SEGV_MAPERR:
Writer::writeSafe("SEGV_MAPPER\n");
break;
default:
Writer::writeSafe("Unknown si_code\n");
}
Writer::writeSafe("Dumping registers...\n");
auto rContext = reinterpret_cast<ucontext_t*>(context);
dumpRegisters(rContext);
Writer::writeSafe("Registers dumped.\n");
Writer::writeSafe("Dumping memory...\n");
dumpMemory(info->si_addr);
Writer::writeSafe("Memory dumped.\n");
}
_exit(EXIT_FAILURE);
}
void Handler::dumpRegisters(ucontext_t* context) {
Writer::writeSafeRegister("R8", context->uc_mcontext.gregs[REG_R8]);
Writer::writeSafeRegister("R9", context->uc_mcontext.gregs[REG_R9]);
Writer::writeSafeRegister("R10", context->uc_mcontext.gregs[REG_R10]);
Writer::writeSafeRegister("R11", context->uc_mcontext.gregs[REG_R11]);
Writer::writeSafeRegister("R12", context->uc_mcontext.gregs[REG_R12]);
Writer::writeSafeRegister("R13", context->uc_mcontext.gregs[REG_R13]);
Writer::writeSafeRegister("R14", context->uc_mcontext.gregs[REG_R14]);
Writer::writeSafeRegister("R15", context->uc_mcontext.gregs[REG_R15]);
Writer::writeSafeRegister("RDI", context->uc_mcontext.gregs[REG_RDI]);
Writer::writeSafeRegister("RSI", context->uc_mcontext.gregs[REG_RSI]);
Writer::writeSafeRegister("RBP", context->uc_mcontext.gregs[REG_RBP]);
Writer::writeSafeRegister("RBX", context->uc_mcontext.gregs[REG_RBX]);
Writer::writeSafeRegister("RDX", context->uc_mcontext.gregs[REG_RDX]);
Writer::writeSafeRegister("RAX", context->uc_mcontext.gregs[REG_RAX]);
Writer::writeSafeRegister("RCX", context->uc_mcontext.gregs[REG_RCX]);
Writer::writeSafeRegister("RSP", context->uc_mcontext.gregs[REG_RSP]);
Writer::writeSafeRegister("RIP", context->uc_mcontext.gregs[REG_RIP]);
Writer::writeSafeRegister("EFL", context->uc_mcontext.gregs[REG_EFL]);
Writer::writeSafeRegister("CSGSFS", context->uc_mcontext.gregs[REG_CSGSFS]);
Writer::writeSafeRegister("ERR", context->uc_mcontext.gregs[REG_ERR]);
Writer::writeSafeRegister("TRAPNO", context->uc_mcontext.gregs[REG_TRAPNO]);
Writer::writeSafeRegister("OLDMASK", context->uc_mcontext.gregs[REG_OLDMASK]);
Writer::writeSafeRegister("CR2", context->uc_mcontext.gregs[REG_CR2]);
}
void Handler::dumpMemory(void* address) {
int pipefd[2];
const auto p = pipe(pipefd);
if (p == -1) {
Writer::writeSafe("Can't pipe\n");
_exit(EXIT_FAILURE);
}
if (address == nullptr) {
Writer::writeSafe("address is nullptr\n");
_exit(EXIT_FAILURE);
}
for (int i = -MEMORY_SIZE; i < MEMORY_SIZE; ++i) {
uint8_t buffer;
char* t = ((char*)address) + i;
if (t == address) {
Writer::writeSafe("--> ");
}
if (write(pipefd[1], t, 1) < 0 || read(pipefd[0], &buffer, 1) < 0) {
Writer::writeSafe("????");
} else {
Writer::writeSafe(buffer);
}
Writer::writeSafe("\n");
}
}