-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.cpp
68 lines (57 loc) · 1.62 KB
/
debugger.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
#include <vector>
#include <sstream>
#include <cstdint>
#include "linenoise.h"
#include "debugger.hpp"
void Debugger::set_breakpoint_at_address(std::intptr_t addr) {
std::cout << "Set breakpoint at address Ox" << std::hex << addr << '\n';
Breakpoint bp{m_pid, addr};
bp.enable();
m_breakpoints[addr] = bp;
}
std::vector<std::string> split(const std::string& s, char delim) {
std::vector<std::string> out;
std::stringstream ss{s};
std::string item;
while (std::getline(ss, item, delim)) {
out.push_back(item);
}
return out;
}
bool is_prefix(const std::string& s, const std::string& of) {
if (s.size() > of.size()) return false;
return std::equal(s.begin(), s.end(), of.begin());
}
void Debugger::continue_execution() {
ptrace(PTRACE_CONT, m_pid, nullptr, nullptr);
int wait_status;
auto options = 0;
waitpid(m_pid, &wait_status, options);
}
void Debugger::handle_command(const std::string& line) {
auto args = split(line, ' ');
auto command = args[0];
if (is_prefix(command, "continue")) {
continue_execution();
} else if (is_prefix(command, "break")) {
// TODO: Add check that address in format 0xXXXX
std::string addr{args[1], 2};
set_breakpoint_at_address(std::stol(addr, 0, 16));
} else if (is_prefix(command, "quit")) {
kill(m_pid, 0);
exit(0);
} else {
std::cerr << "Unknows debugging command\n";
}
}
void Debugger::run() {
int wait_status;
auto options = 0;
waitpid(m_pid, &wait_status, options);
char* line = nullptr;
while ((line = linenoise("kidbg> ")) != nullptr) {
handle_command(line);
linenoiseHistoryAdd(line);
linenoiseFree(line);
}
}