-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdbstub.c
247 lines (201 loc) · 4.38 KB
/
gdbstub.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "hex.h"
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef int i32;
#define error(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
fflush(stderr); \
exit(1); \
}
struct gio {
u8 (*get_char)(void);
void (*put_char)(u8);
void (*init)(i32);
};
struct socket_io {
int fd;
} socket_io;
static int socket_io_init(int fd)
{
socket_io.fd = fd;
return 0;
}
static u8 socket_get_char(void)
{
u8 buf;
int fd = socket_io.fd;
int n = recv(fd, &buf, 1, 0);
if (!n) {
error("no more contents.\n");
}
if (n < 0) {
error("read failed.");
}
return buf;
}
static void socket_put_char(u8 ch)
{
int fd = socket_io.fd;
if (send(fd, &ch, 1, 0) < 0) {
error("send failed.\n");
}
}
struct gio debug_io = {
.get_char = socket_get_char,
.put_char = socket_put_char,
};
static int _recv_packet(u8 *buf, size_t buf_len, size_t *len)
{
u8 ch = '0';
u8 checksum = 0;
while (ch != '$') {
ch = debug_io.get_char();
}
*len = 0;
while (*len < buf_len - 1) {
ch = debug_io.get_char();
if (ch == '#') {
break;
}
checksum += ch;
buf[*len] = ch;
*len += 1;
}
u8 ck1, ck2;
if (char2hex(debug_io.get_char(), &ck1) ||
char2hex(debug_io.get_char(), &ck2)) {
error("char2hex failed;");
}
u8 expected_checksum = (ck1 << 4) + ck2;
if (checksum != expected_checksum) {
debug_io.put_char('-');
error("checksum error, got %d, expected checksum: %d.\n",
checksum, expected_checksum);
}
debug_io.put_char('+');
return 0;
}
static int _send_packet(const char *buf, size_t len)
{
u8 checksum = 0;
debug_io.put_char('$');
while (len-- > 0) {
checksum += *buf;
debug_io.put_char(*buf++);
}
debug_io.put_char('#');
u8 checksum_buf[2] = { 0 };
if (byte2hex(&checksum, 1, checksum_buf, sizeof(checksum_buf))) {
return -1;
}
debug_io.put_char(checksum_buf[0]);
debug_io.put_char(checksum_buf[1]);
if (debug_io.get_char() != '+') {
return -1;
}
return 0;
}
struct regs {
u64 gprs[32];
u32 pc;
u32 msr;
u32 cr;
u32 lr;
u32 ctr;
u32 xer;
};
int send_registers()
{
char buf[1024] = { 0 };
u32 regs_len = sizeof(struct regs);
struct regs r;
for (int i = 0; i < 32; i++) {
r.gprs[i] = 0;
}
r.pc = 0x12345678;
r.msr = 0x00002830;
if (byte2hex((u8 *)&r, regs_len, (u8 *)buf, regs_len * 2)) {
return -1;
}
_send_packet(buf, regs_len * 2);
return 0;
}
int main(int argc, char *argv[])
{
int port = atoi(argv[1]);
struct sockaddr_in server, client;
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
error("server_fd create failed!\n");
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
int optval = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
error("Could not bind socket!\n");
}
if (listen(server_fd, 128) < 0) {
error("Could not listen on socket!\n");
}
printf("Server started!\n");
while (1) {
socklen_t len = sizeof(client);
printf("accepting\n");
int client_fd =
accept(server_fd, (struct sockaddr *)&client, &len);
if (client_fd < 0) {
error("Could not accept new connection.\n");
}
printf("accepted\n");
socket_io_init(client_fd);
while (1) {
u8 buf[1024] = { 0 };
size_t len = 0;
_recv_packet(buf, 1024, &len);
printf("recv: %s\n", buf);
u32 addr;
u32 addr_len;
u8 *ptr = buf;
switch (*ptr++) {
case '?':
_send_packet("S05", 3);
break;
case 'g':
send_registers();
break;
case 'p':
/* TODO */
addr = strtol((const char *)ptr, (char **)&ptr,
16);
break;
case 'm':
addr = strtol((const char *)ptr, (char **)&ptr,
16);
if (*ptr != ',') {
error("m command parse failed.\n");
}
ptr++;
addr_len = strtol((const char *)ptr,
(char **)&ptr, 16);
printf("addr: %d, len: %d\n", addr, addr_len);
break;
default:
_send_packet("", 0);
break;
}
}
}
return 0;
}