Skip to content

Commit ca60c7b

Browse files
committed
add project code and readme
1 parent 0ad753f commit ca60c7b

18 files changed

+1737
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.o
2+
*.out
3+
*.so
4+
*/testcase/
5+
.vscode/

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CC = gcc
2+
CFLAGS = -Wall
3+
LDFLAGS = -lelftool -lcapstone -lelf
4+
INCLUDE_PATH = ./inc
5+
OBJS = util.o command.o vmmap.o disasm-tool.o tracee.o sdb.o
6+
PROG = sdb
7+
8+
all: $(OBJS)
9+
$(CC) $^ -L. $(LDFLAGS) -I $(INCLUDE_PATH) -o $(PROG)
10+
11+
%.o: %.c
12+
$(CC) -c $< $(CFLAGS) -o $@
13+
14+
.PHONY: clean
15+
clean:
16+
rm -rf $(OBJS) $(PROG)

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
SDB
2+
===
3+
4+
simple C debugger like gdb
5+
6+
### How can I do with SDB ?
7+
support basic debugger usage, like set/delete breakpoint, step by step run instruction, and dump memory content, etc.
8+
9+
## Getting started
10+
### Compile
11+
```bash
12+
# install dependencies
13+
sudo apt-get update
14+
sudo apt-get install libelf-dev libcapstone-dev
15+
16+
# compile the debugger
17+
make
18+
```
19+
20+
### Run the debugger
21+
```bash
22+
# start debugger only
23+
./sdb
24+
25+
# start debugger and load the debug target
26+
./sdb <program_path>
27+
```
28+
29+
## Commands
30+
31+
#### load *path/to/a/program*
32+
load a program to the debugger
33+
34+
#### start
35+
start the program and stop at the first
36+
37+
#### run
38+
run the program
39+
40+
#### break *address*
41+
add a break point at *address*
42+
43+
#### delete *breakpoint-id*
44+
delete a break point by id, which you can get the breakpoint id by **list** commands
45+
46+
#### list
47+
list break points and their ids
48+
49+
#### cont
50+
continue running your program
51+
52+
#### si
53+
Execute one instruction
54+
55+
#### disasm *address*
56+
disassemble instructions in a file or a memory region
57+
58+
#### dump *address* *length*
59+
dump memory content start from *address* and dump *length*
60+
61+
#### vmmap
62+
show memory layout
63+
64+
#### getregs
65+
get the valus of all registers
66+
67+
all supported register is listed as following
68+
- rax, rbx, rcx, rdx, rbp, rsi, rdi, rip, rsp, eflags
69+
- r8 ~ r15
70+
71+
#### get *reg*
72+
get a single value from a register
73+
74+
#### set *reg* *val*
75+
set specific value to a register
76+
77+
#### help
78+
show the help message
79+
80+
#### exit
81+
terminate the debugger
82+
83+
## Example
84+
```
85+
TODO...
86+
```
87+
88+
## License
89+
MIT

command.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#include "inc/command.h"
2+
3+
#define MATCH_STR(str1, str2) strcmp(str1, str2) == 0
4+
5+
int parse_command(char *command, const int debugger_state, int *result)
6+
{
7+
if(MATCH_STR("break", command) || MATCH_STR("b", command))
8+
{
9+
if(debugger_state != STATE_LOADED && debugger_state != STATE_RUNNING)
10+
{
11+
*result = ERROR_STATE_NOT_LOADED_NOR_RUNNING;
12+
return PARSE_ERROR;
13+
}
14+
15+
*result = COMMAND_BREAK;
16+
return PARSE_OK;
17+
}
18+
19+
if(MATCH_STR("cont", command) || MATCH_STR("c", command))
20+
{
21+
if(debugger_state != STATE_RUNNING)
22+
{
23+
*result = ERROR_STATE_NOT_RUNNING;
24+
return PARSE_ERROR;
25+
}
26+
27+
*result = COMMAND_CONTINUE;
28+
return PARSE_OK;
29+
}
30+
31+
if(MATCH_STR("delete", command))
32+
{
33+
*result = COMMAND_DELETE;
34+
return PARSE_OK;
35+
}
36+
37+
if(MATCH_STR("disasm", command) || MATCH_STR("d", command))
38+
{
39+
if(debugger_state != STATE_LOADED && debugger_state != STATE_RUNNING)
40+
{
41+
*result = ERROR_STATE_NOT_LOADED_NOR_RUNNING;
42+
return PARSE_ERROR;
43+
}
44+
45+
*result = COMMAND_DISASM;
46+
return PARSE_OK;
47+
}
48+
49+
if(MATCH_STR("dump", command) || MATCH_STR("x", command))
50+
{
51+
if(debugger_state != STATE_RUNNING)
52+
{
53+
*result = ERROR_STATE_NOT_RUNNING;
54+
return PARSE_ERROR;
55+
}
56+
57+
*result = COMMAND_DUMP;
58+
return PARSE_OK;
59+
}
60+
61+
if(MATCH_STR("exit", command) || MATCH_STR("q", command))
62+
{
63+
*result = COMMAND_EXIT;
64+
return PARSE_OK;
65+
}
66+
67+
if(MATCH_STR("get", command) || MATCH_STR("g", command))
68+
{
69+
if(debugger_state != STATE_RUNNING)
70+
{
71+
*result = ERROR_STATE_NOT_RUNNING;
72+
return PARSE_ERROR;
73+
}
74+
75+
*result = COMMAND_GET;
76+
return PARSE_OK;
77+
}
78+
79+
if(MATCH_STR("getregs", command))
80+
{
81+
if(debugger_state != STATE_RUNNING)
82+
{
83+
*result = ERROR_STATE_NOT_RUNNING;
84+
return PARSE_ERROR;
85+
}
86+
87+
*result = COMMAND_GETREGS;
88+
return PARSE_OK;
89+
}
90+
91+
if(MATCH_STR("help", command) || MATCH_STR("h", command))
92+
{
93+
*result = COMMAND_HELP;
94+
return PARSE_OK;
95+
}
96+
97+
if(MATCH_STR("list", command) || MATCH_STR("l", command))
98+
{
99+
*result = COMMAND_LIST;
100+
return PARSE_OK;
101+
}
102+
103+
if(MATCH_STR("load", command))
104+
{
105+
if(debugger_state != STATE_INIT)
106+
{
107+
*result = ERROR_STATE_NOT_INIT;
108+
return PARSE_ERROR;
109+
}
110+
111+
*result = COMMAND_LOAD;
112+
return PARSE_OK;
113+
}
114+
115+
if(MATCH_STR("run", command) || MATCH_STR("r", command))
116+
{
117+
if(debugger_state != STATE_LOADED && debugger_state != STATE_RUNNING)
118+
{
119+
*result = ERROR_STATE_NOT_LOADED_NOR_RUNNING;
120+
return PARSE_ERROR;
121+
}
122+
123+
*result = COMMAND_RUN;
124+
return PARSE_OK;
125+
}
126+
127+
if(MATCH_STR("vmmap", command) || MATCH_STR("m", command))
128+
{
129+
if(debugger_state != STATE_LOADED && debugger_state != STATE_RUNNING)
130+
{
131+
*result = ERROR_STATE_NOT_LOADED_NOR_RUNNING;
132+
return PARSE_ERROR;
133+
}
134+
135+
*result = COMMAND_VMMAP;
136+
return PARSE_OK;
137+
}
138+
139+
if(MATCH_STR("set", command) || MATCH_STR("s", command))
140+
{
141+
if(debugger_state != STATE_RUNNING)
142+
{
143+
*result = ERROR_STATE_NOT_RUNNING;
144+
return PARSE_ERROR;
145+
}
146+
147+
*result = COMMAND_SET;
148+
return PARSE_OK;
149+
}
150+
151+
if(MATCH_STR("si", command))
152+
{
153+
if(debugger_state != STATE_RUNNING)
154+
{
155+
*result = ERROR_STATE_NOT_RUNNING;
156+
return PARSE_ERROR;
157+
}
158+
159+
*result = COMMAND_SI;
160+
return PARSE_OK;
161+
}
162+
163+
if(MATCH_STR("start", command))
164+
{
165+
if(debugger_state != STATE_LOADED)
166+
{
167+
*result = ERROR_STATE_NOT_LOADED;
168+
return PARSE_ERROR;
169+
}
170+
171+
*result = COMMAND_START;
172+
return PARSE_OK;
173+
}
174+
175+
*result = ERROR_COMMAND_NOT_FOUND;
176+
return PARSE_ERROR;
177+
}
178+
179+
void print_command_error(int code)
180+
{
181+
switch (code)
182+
{
183+
case ERROR_COMMAND_NOT_FOUND:
184+
fprintf(stderr, "** command not found.\n");
185+
break;
186+
case ERROR_STATE_NOT_INIT:
187+
fprintf(stderr, "** has already loaded one prgram.\n");
188+
break;
189+
case ERROR_STATE_NOT_LOADED:
190+
fprintf(stderr, "** no program is loaded.\n");
191+
break;
192+
case ERROR_STATE_NOT_RUNNING:
193+
fprintf(stderr, "** no program is running.\n");
194+
break;
195+
case ERROR_STATE_NOT_LOADED_NOR_RUNNING:
196+
fprintf(stderr, "** no program is loaded or running.\n");
197+
break;
198+
default:
199+
break;
200+
}
201+
202+
return;
203+
}

0 commit comments

Comments
 (0)