-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdb_headers.hpp
162 lines (132 loc) · 3.99 KB
/
mdb_headers.hpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
/* C standard library */
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
/* POSIX */
#include <unistd.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <libelf.h>
#include <gelf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <bits/stdc++.h>
/* Linux */
#include <syscall.h>
#include <sys/ptrace.h>
#include <sys/personality.h>
/*C++ headers*/
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <sstream>
#include "elf_loader.hpp"
#include "breakpoint.hpp"
#include "disassembler.hpp"
#define TOOL "mdb"
#define maxbuf 1024
#define DEBUG 1
//@ macros
#define die(...) \
do \
{ \
fprintf(stderr, TOOL ": " __VA_ARGS__); \
fputc('\n', stderr); \
exit(EXIT_FAILURE); \
} while (0)
//@structs and classes
class debugger
{
private:
public:
elf_file *elf; // mapped elf representation
std::map<std::string, long> *symbol_mappings; // symbol mappings
std::map<std::intptr_t, breakpoint> *breakpoints; // active breakpoints
long base_address;
// int active_breakpoints;
disassembler *disas;
// int pedding_Waits;
pid_t tracee_pid;
bool running = false;
bool is_traced = false;
char **argv;
//@functions
// constructor
debugger(char *argv[]);
// destructor
~debugger();
/*
reads /proc/pid/maps to get the base address for the
child process
*/
void read_base_Address();
/*
Begins the tracing process, by creating tracer and tracee processes
*/
void start_tracing(char *argv[]);
/*function that prints symbols*/
void print_symbols();
/*
Function that is called using the debugger object and sets the break point
for the given address.
*/
void set_breakpoint(unsigned long breakpoint_address);
/*function that sets a breakpoint using symbol str*/
void set_bp_symbol(std::string input);
/*function that prints a list of the active breakpoints*/
void print_active_bp();
/*
function that deletes breakpoint @ the index given and uses ptrace to
restore the code to that particular address
*/
void delete_bp(int indext_bp);
/*command that starts the process*/
void run();
/*
function that serves each break point, it restores original code
and resets the program counter to execute that command
*/
void serve_breakpoint();
/*
function that tells the process to continue and then wait for signal
Returns true if child process is still running
Reuturn false if child process exited, thus continuing execution was not
succesful
*/
bool continue_exec();
/*Restarts the debug process, by recreating a new process
and reapplying breakpoints.
This function will also run the program and cause it ot hit first
break point
*/
void restart_debug(char *argv[]);
/*
Restarts the debug process, by recreating a new process
and reapplying breakpoints. This function does not run the program
*/
void restart_tracing(char *argv[]);
/*function for reapplying already set breakpoints if exist*/
void reapply_bp();
/*function that performs the step instruction command and prints disas
for single command*/
void step_instruction(bool print_disas);
/*prints the dissassembly of current instruction until the end of function*/
void disasembly();
/*disables all set breakpoints*/
void disable_all();
/*enables all set breakpoints*/
void enable_all();
};
//@ assisting function signatures
// function that begins the debugging process
void begin_debuging(debugger *mdb, char *argv[]);
/*prints the usage of mdb tool*/
void Usage();
/*prints input character times times*/
void print_char(int times, const char ch);