root / gdbstub.c @ d1d9f421
History | View | Annotate | Download (14.6 kB)
1 | b4608c04 | bellard | /*
|
---|---|---|---|
2 | b4608c04 | bellard | * gdb server stub
|
3 | b4608c04 | bellard | *
|
4 | b4608c04 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | b4608c04 | bellard | *
|
6 | b4608c04 | bellard | * This library is free software; you can redistribute it and/or
|
7 | b4608c04 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | b4608c04 | bellard | * License as published by the Free Software Foundation; either
|
9 | b4608c04 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | b4608c04 | bellard | *
|
11 | b4608c04 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | b4608c04 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | b4608c04 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | b4608c04 | bellard | * Lesser General Public License for more details.
|
15 | b4608c04 | bellard | *
|
16 | b4608c04 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | b4608c04 | bellard | * License along with this library; if not, write to the Free Software
|
18 | b4608c04 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | b4608c04 | bellard | */
|
20 | 67b915a5 | bellard | #include "vl.h" |
21 | 67b915a5 | bellard | |
22 | b4608c04 | bellard | #include <sys/socket.h> |
23 | b4608c04 | bellard | #include <netinet/in.h> |
24 | b4608c04 | bellard | #include <netinet/tcp.h> |
25 | b4608c04 | bellard | #include <signal.h> |
26 | b4608c04 | bellard | |
27 | 4abe615b | bellard | //#define DEBUG_GDB
|
28 | b4608c04 | bellard | |
29 | 858693c6 | bellard | enum RSState {
|
30 | 858693c6 | bellard | RS_IDLE, |
31 | 858693c6 | bellard | RS_GETLINE, |
32 | 858693c6 | bellard | RS_CHKSUM1, |
33 | 858693c6 | bellard | RS_CHKSUM2, |
34 | 858693c6 | bellard | }; |
35 | b4608c04 | bellard | |
36 | 858693c6 | bellard | static int gdbserver_fd; |
37 | b4608c04 | bellard | |
38 | 858693c6 | bellard | typedef struct GDBState { |
39 | 858693c6 | bellard | enum RSState state;
|
40 | 858693c6 | bellard | int fd;
|
41 | 858693c6 | bellard | char line_buf[4096]; |
42 | 858693c6 | bellard | int line_buf_index;
|
43 | 858693c6 | bellard | int line_csum;
|
44 | 858693c6 | bellard | } GDBState; |
45 | b4608c04 | bellard | |
46 | 858693c6 | bellard | static int get_char(GDBState *s) |
47 | b4608c04 | bellard | { |
48 | b4608c04 | bellard | uint8_t ch; |
49 | b4608c04 | bellard | int ret;
|
50 | b4608c04 | bellard | |
51 | b4608c04 | bellard | for(;;) {
|
52 | 858693c6 | bellard | ret = read(s->fd, &ch, 1);
|
53 | b4608c04 | bellard | if (ret < 0) { |
54 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
55 | b4608c04 | bellard | return -1; |
56 | b4608c04 | bellard | } else if (ret == 0) { |
57 | b4608c04 | bellard | return -1; |
58 | b4608c04 | bellard | } else {
|
59 | b4608c04 | bellard | break;
|
60 | b4608c04 | bellard | } |
61 | b4608c04 | bellard | } |
62 | b4608c04 | bellard | return ch;
|
63 | b4608c04 | bellard | } |
64 | b4608c04 | bellard | |
65 | 858693c6 | bellard | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
66 | b4608c04 | bellard | { |
67 | b4608c04 | bellard | int ret;
|
68 | b4608c04 | bellard | |
69 | b4608c04 | bellard | while (len > 0) { |
70 | 858693c6 | bellard | ret = write(s->fd, buf, len); |
71 | b4608c04 | bellard | if (ret < 0) { |
72 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
73 | b4608c04 | bellard | return;
|
74 | b4608c04 | bellard | } else {
|
75 | b4608c04 | bellard | buf += ret; |
76 | b4608c04 | bellard | len -= ret; |
77 | b4608c04 | bellard | } |
78 | b4608c04 | bellard | } |
79 | b4608c04 | bellard | } |
80 | b4608c04 | bellard | |
81 | b4608c04 | bellard | static inline int fromhex(int v) |
82 | b4608c04 | bellard | { |
83 | b4608c04 | bellard | if (v >= '0' && v <= '9') |
84 | b4608c04 | bellard | return v - '0'; |
85 | b4608c04 | bellard | else if (v >= 'A' && v <= 'F') |
86 | b4608c04 | bellard | return v - 'A' + 10; |
87 | b4608c04 | bellard | else if (v >= 'a' && v <= 'f') |
88 | b4608c04 | bellard | return v - 'a' + 10; |
89 | b4608c04 | bellard | else
|
90 | b4608c04 | bellard | return 0; |
91 | b4608c04 | bellard | } |
92 | b4608c04 | bellard | |
93 | b4608c04 | bellard | static inline int tohex(int v) |
94 | b4608c04 | bellard | { |
95 | b4608c04 | bellard | if (v < 10) |
96 | b4608c04 | bellard | return v + '0'; |
97 | b4608c04 | bellard | else
|
98 | b4608c04 | bellard | return v - 10 + 'a'; |
99 | b4608c04 | bellard | } |
100 | b4608c04 | bellard | |
101 | b4608c04 | bellard | static void memtohex(char *buf, const uint8_t *mem, int len) |
102 | b4608c04 | bellard | { |
103 | b4608c04 | bellard | int i, c;
|
104 | b4608c04 | bellard | char *q;
|
105 | b4608c04 | bellard | q = buf; |
106 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
107 | b4608c04 | bellard | c = mem[i]; |
108 | b4608c04 | bellard | *q++ = tohex(c >> 4);
|
109 | b4608c04 | bellard | *q++ = tohex(c & 0xf);
|
110 | b4608c04 | bellard | } |
111 | b4608c04 | bellard | *q = '\0';
|
112 | b4608c04 | bellard | } |
113 | b4608c04 | bellard | |
114 | b4608c04 | bellard | static void hextomem(uint8_t *mem, const char *buf, int len) |
115 | b4608c04 | bellard | { |
116 | b4608c04 | bellard | int i;
|
117 | b4608c04 | bellard | |
118 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
119 | b4608c04 | bellard | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
120 | b4608c04 | bellard | buf += 2;
|
121 | b4608c04 | bellard | } |
122 | b4608c04 | bellard | } |
123 | b4608c04 | bellard | |
124 | b4608c04 | bellard | /* return -1 if error, 0 if OK */
|
125 | 858693c6 | bellard | static int put_packet(GDBState *s, char *buf) |
126 | b4608c04 | bellard | { |
127 | b4608c04 | bellard | char buf1[3]; |
128 | b4608c04 | bellard | int len, csum, ch, i;
|
129 | b4608c04 | bellard | |
130 | b4608c04 | bellard | #ifdef DEBUG_GDB
|
131 | b4608c04 | bellard | printf("reply='%s'\n", buf);
|
132 | b4608c04 | bellard | #endif
|
133 | b4608c04 | bellard | |
134 | b4608c04 | bellard | for(;;) {
|
135 | b4608c04 | bellard | buf1[0] = '$'; |
136 | 858693c6 | bellard | put_buffer(s, buf1, 1);
|
137 | b4608c04 | bellard | len = strlen(buf); |
138 | 858693c6 | bellard | put_buffer(s, buf, len); |
139 | b4608c04 | bellard | csum = 0;
|
140 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
141 | b4608c04 | bellard | csum += buf[i]; |
142 | b4608c04 | bellard | } |
143 | b4608c04 | bellard | buf1[0] = '#'; |
144 | b4608c04 | bellard | buf1[1] = tohex((csum >> 4) & 0xf); |
145 | b4608c04 | bellard | buf1[2] = tohex((csum) & 0xf); |
146 | b4608c04 | bellard | |
147 | 858693c6 | bellard | put_buffer(s, buf1, 3);
|
148 | b4608c04 | bellard | |
149 | 858693c6 | bellard | ch = get_char(s); |
150 | b4608c04 | bellard | if (ch < 0) |
151 | b4608c04 | bellard | return -1; |
152 | b4608c04 | bellard | if (ch == '+') |
153 | b4608c04 | bellard | break;
|
154 | b4608c04 | bellard | } |
155 | b4608c04 | bellard | return 0; |
156 | b4608c04 | bellard | } |
157 | b4608c04 | bellard | |
158 | 6da41eaf | bellard | #if defined(TARGET_I386)
|
159 | 6da41eaf | bellard | |
160 | 6da41eaf | bellard | static void to_le32(uint8_t *p, int v) |
161 | 6da41eaf | bellard | { |
162 | 6da41eaf | bellard | p[0] = v;
|
163 | 6da41eaf | bellard | p[1] = v >> 8; |
164 | 6da41eaf | bellard | p[2] = v >> 16; |
165 | 6da41eaf | bellard | p[3] = v >> 24; |
166 | 6da41eaf | bellard | } |
167 | 6da41eaf | bellard | |
168 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
169 | 6da41eaf | bellard | { |
170 | 6da41eaf | bellard | int i, fpus;
|
171 | 6da41eaf | bellard | |
172 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
173 | 6da41eaf | bellard | to_le32(mem_buf + i * 4, env->regs[i]);
|
174 | 6da41eaf | bellard | } |
175 | 6da41eaf | bellard | to_le32(mem_buf + 8 * 4, env->eip); |
176 | 6da41eaf | bellard | to_le32(mem_buf + 9 * 4, env->eflags); |
177 | 6da41eaf | bellard | to_le32(mem_buf + 10 * 4, env->segs[R_CS].selector); |
178 | 6da41eaf | bellard | to_le32(mem_buf + 11 * 4, env->segs[R_SS].selector); |
179 | 6da41eaf | bellard | to_le32(mem_buf + 12 * 4, env->segs[R_DS].selector); |
180 | 6da41eaf | bellard | to_le32(mem_buf + 13 * 4, env->segs[R_ES].selector); |
181 | 6da41eaf | bellard | to_le32(mem_buf + 14 * 4, env->segs[R_FS].selector); |
182 | 6da41eaf | bellard | to_le32(mem_buf + 15 * 4, env->segs[R_GS].selector); |
183 | 6da41eaf | bellard | /* XXX: convert floats */
|
184 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
185 | 6da41eaf | bellard | memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
186 | 6da41eaf | bellard | } |
187 | 6da41eaf | bellard | to_le32(mem_buf + 36 * 4, env->fpuc); |
188 | 6da41eaf | bellard | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
189 | 6da41eaf | bellard | to_le32(mem_buf + 37 * 4, fpus); |
190 | 6da41eaf | bellard | to_le32(mem_buf + 38 * 4, 0); /* XXX: convert tags */ |
191 | 6da41eaf | bellard | to_le32(mem_buf + 39 * 4, 0); /* fiseg */ |
192 | 6da41eaf | bellard | to_le32(mem_buf + 40 * 4, 0); /* fioff */ |
193 | 6da41eaf | bellard | to_le32(mem_buf + 41 * 4, 0); /* foseg */ |
194 | 6da41eaf | bellard | to_le32(mem_buf + 42 * 4, 0); /* fooff */ |
195 | 6da41eaf | bellard | to_le32(mem_buf + 43 * 4, 0); /* fop */ |
196 | 6da41eaf | bellard | return 44 * 4; |
197 | 6da41eaf | bellard | } |
198 | 6da41eaf | bellard | |
199 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
200 | 6da41eaf | bellard | { |
201 | 6da41eaf | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
202 | 6da41eaf | bellard | int i;
|
203 | 6da41eaf | bellard | |
204 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
205 | 6da41eaf | bellard | env->regs[i] = tswapl(registers[i]); |
206 | 6da41eaf | bellard | } |
207 | 6da41eaf | bellard | env->eip = registers[8];
|
208 | 6da41eaf | bellard | env->eflags = registers[9];
|
209 | 6da41eaf | bellard | #if defined(CONFIG_USER_ONLY)
|
210 | 6da41eaf | bellard | #define LOAD_SEG(index, sreg)\
|
211 | 6da41eaf | bellard | if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
212 | 6da41eaf | bellard | cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
213 | 6da41eaf | bellard | LOAD_SEG(10, R_CS);
|
214 | 6da41eaf | bellard | LOAD_SEG(11, R_SS);
|
215 | 6da41eaf | bellard | LOAD_SEG(12, R_DS);
|
216 | 6da41eaf | bellard | LOAD_SEG(13, R_ES);
|
217 | 6da41eaf | bellard | LOAD_SEG(14, R_FS);
|
218 | 6da41eaf | bellard | LOAD_SEG(15, R_GS);
|
219 | 6da41eaf | bellard | #endif
|
220 | 6da41eaf | bellard | } |
221 | 6da41eaf | bellard | |
222 | 9e62fd7f | bellard | #elif defined (TARGET_PPC)
|
223 | a541f297 | bellard | static void to_le32(uint32_t *buf, uint32_t v) |
224 | 9e62fd7f | bellard | { |
225 | a541f297 | bellard | uint8_t *p = (uint8_t *)buf; |
226 | 9e62fd7f | bellard | p[3] = v;
|
227 | 9e62fd7f | bellard | p[2] = v >> 8; |
228 | 9e62fd7f | bellard | p[1] = v >> 16; |
229 | 9e62fd7f | bellard | p[0] = v >> 24; |
230 | 9e62fd7f | bellard | } |
231 | 9e62fd7f | bellard | |
232 | a541f297 | bellard | static uint32_t from_le32 (uint32_t *buf)
|
233 | a541f297 | bellard | { |
234 | a541f297 | bellard | uint8_t *p = (uint8_t *)buf; |
235 | a541f297 | bellard | |
236 | a541f297 | bellard | return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); |
237 | a541f297 | bellard | } |
238 | a541f297 | bellard | |
239 | 9e62fd7f | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
240 | 9e62fd7f | bellard | { |
241 | a541f297 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
242 | 9e62fd7f | bellard | int i;
|
243 | 9e62fd7f | bellard | |
244 | 9e62fd7f | bellard | /* fill in gprs */
|
245 | a541f297 | bellard | for(i = 0; i < 32; i++) { |
246 | a541f297 | bellard | to_le32(®isters[i], env->gpr[i]); |
247 | 9e62fd7f | bellard | } |
248 | 9e62fd7f | bellard | /* fill in fprs */
|
249 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
250 | a541f297 | bellard | to_le32(®isters[(i * 2) + 32], *((uint32_t *)&env->fpr[i])); |
251 | a541f297 | bellard | to_le32(®isters[(i * 2) + 33], *((uint32_t *)&env->fpr[i] + 1)); |
252 | 9e62fd7f | bellard | } |
253 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
254 | a541f297 | bellard | to_le32(®isters[96], (uint32_t)env->nip/* - 4*/); |
255 | a541f297 | bellard | to_le32(®isters[97], _load_msr(env));
|
256 | 9e62fd7f | bellard | tmp = 0;
|
257 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
258 | a541f297 | bellard | tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
259 | a541f297 | bellard | to_le32(®isters[98], tmp);
|
260 | a541f297 | bellard | to_le32(®isters[99], env->lr);
|
261 | a541f297 | bellard | to_le32(®isters[100], env->ctr);
|
262 | a541f297 | bellard | to_le32(®isters[101], _load_xer(env));
|
263 | a541f297 | bellard | to_le32(®isters[102], 0); |
264 | a541f297 | bellard | |
265 | a541f297 | bellard | return 103 * 4; |
266 | 9e62fd7f | bellard | } |
267 | 9e62fd7f | bellard | |
268 | 9e62fd7f | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
269 | 9e62fd7f | bellard | { |
270 | 9e62fd7f | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
271 | 9e62fd7f | bellard | int i;
|
272 | 9e62fd7f | bellard | |
273 | 9e62fd7f | bellard | /* fill in gprs */
|
274 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
275 | a541f297 | bellard | env->gpr[i] = from_le32(®isters[i]); |
276 | 9e62fd7f | bellard | } |
277 | 9e62fd7f | bellard | /* fill in fprs */
|
278 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
279 | a541f297 | bellard | *((uint32_t *)&env->fpr[i]) = from_le32(®isters[(i * 2) + 32]); |
280 | a541f297 | bellard | *((uint32_t *)&env->fpr[i] + 1) = from_le32(®isters[(i * 2) + 33]); |
281 | 9e62fd7f | bellard | } |
282 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
283 | a541f297 | bellard | env->nip = from_le32(®isters[96]);
|
284 | a541f297 | bellard | _store_msr(env, from_le32(®isters[97]));
|
285 | a541f297 | bellard | registers[98] = from_le32(®isters[98]); |
286 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
287 | a541f297 | bellard | env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
288 | a541f297 | bellard | env->lr = from_le32(®isters[99]);
|
289 | a541f297 | bellard | env->ctr = from_le32(®isters[100]);
|
290 | a541f297 | bellard | _store_xer(env, from_le32(®isters[101]));
|
291 | 9e62fd7f | bellard | } |
292 | 6da41eaf | bellard | #else
|
293 | 6da41eaf | bellard | |
294 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
295 | 6da41eaf | bellard | { |
296 | 6da41eaf | bellard | return 0; |
297 | 6da41eaf | bellard | } |
298 | 6da41eaf | bellard | |
299 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
300 | 6da41eaf | bellard | { |
301 | 6da41eaf | bellard | } |
302 | 6da41eaf | bellard | |
303 | 6da41eaf | bellard | #endif
|
304 | b4608c04 | bellard | |
305 | b4608c04 | bellard | /* port = 0 means default port */
|
306 | 858693c6 | bellard | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
307 | b4608c04 | bellard | { |
308 | 858693c6 | bellard | CPUState *env = cpu_single_env; |
309 | b4608c04 | bellard | const char *p; |
310 | 858693c6 | bellard | int ch, reg_size, type;
|
311 | b4608c04 | bellard | char buf[4096]; |
312 | b4608c04 | bellard | uint8_t mem_buf[2000];
|
313 | b4608c04 | bellard | uint32_t *registers; |
314 | b4608c04 | bellard | uint32_t addr, len; |
315 | b4608c04 | bellard | |
316 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
317 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
318 | 858693c6 | bellard | #endif
|
319 | 858693c6 | bellard | p = line_buf; |
320 | 858693c6 | bellard | ch = *p++; |
321 | 858693c6 | bellard | switch(ch) {
|
322 | 858693c6 | bellard | case '?': |
323 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
324 | 858693c6 | bellard | put_packet(s, buf); |
325 | 858693c6 | bellard | break;
|
326 | 858693c6 | bellard | case 'c': |
327 | 858693c6 | bellard | if (*p != '\0') { |
328 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
329 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
330 | 858693c6 | bellard | env->eip = addr; |
331 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
332 | 858693c6 | bellard | env->nip = addr; |
333 | 4c3a88a2 | bellard | #endif
|
334 | 858693c6 | bellard | } |
335 | 858693c6 | bellard | vm_start(); |
336 | 858693c6 | bellard | break;
|
337 | 858693c6 | bellard | case 's': |
338 | 858693c6 | bellard | if (*p != '\0') { |
339 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
340 | c33a346e | bellard | #if defined(TARGET_I386)
|
341 | 858693c6 | bellard | env->eip = addr; |
342 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
343 | 858693c6 | bellard | env->nip = addr; |
344 | c33a346e | bellard | #endif
|
345 | 858693c6 | bellard | } |
346 | 858693c6 | bellard | cpu_single_step(env, 1);
|
347 | 858693c6 | bellard | vm_start(); |
348 | 858693c6 | bellard | break;
|
349 | 858693c6 | bellard | case 'g': |
350 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
351 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
352 | 858693c6 | bellard | put_packet(s, buf); |
353 | 858693c6 | bellard | break;
|
354 | 858693c6 | bellard | case 'G': |
355 | 858693c6 | bellard | registers = (void *)mem_buf;
|
356 | 858693c6 | bellard | len = strlen(p) / 2;
|
357 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
358 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
359 | 858693c6 | bellard | put_packet(s, "OK");
|
360 | 858693c6 | bellard | break;
|
361 | 858693c6 | bellard | case 'm': |
362 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
363 | 858693c6 | bellard | if (*p == ',') |
364 | 858693c6 | bellard | p++; |
365 | 858693c6 | bellard | len = strtoul(p, NULL, 16); |
366 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) |
367 | 858693c6 | bellard | memset(mem_buf, 0, len);
|
368 | 858693c6 | bellard | memtohex(buf, mem_buf, len); |
369 | 858693c6 | bellard | put_packet(s, buf); |
370 | 858693c6 | bellard | break;
|
371 | 858693c6 | bellard | case 'M': |
372 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
373 | 858693c6 | bellard | if (*p == ',') |
374 | 858693c6 | bellard | p++; |
375 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
376 | 858693c6 | bellard | if (*p == ',') |
377 | 858693c6 | bellard | p++; |
378 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
379 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
380 | 858693c6 | bellard | put_packet(s, "ENN");
|
381 | 858693c6 | bellard | else
|
382 | 858693c6 | bellard | put_packet(s, "OK");
|
383 | 858693c6 | bellard | break;
|
384 | 858693c6 | bellard | case 'Z': |
385 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
386 | 858693c6 | bellard | if (*p == ',') |
387 | 858693c6 | bellard | p++; |
388 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
389 | 858693c6 | bellard | if (*p == ',') |
390 | 858693c6 | bellard | p++; |
391 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
392 | 858693c6 | bellard | if (type == 0 || type == 1) { |
393 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
394 | 858693c6 | bellard | goto breakpoint_error;
|
395 | 858693c6 | bellard | put_packet(s, "OK");
|
396 | 858693c6 | bellard | } else {
|
397 | 858693c6 | bellard | breakpoint_error:
|
398 | 858693c6 | bellard | put_packet(s, "ENN");
|
399 | 858693c6 | bellard | } |
400 | 858693c6 | bellard | break;
|
401 | 858693c6 | bellard | case 'z': |
402 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
403 | 858693c6 | bellard | if (*p == ',') |
404 | 858693c6 | bellard | p++; |
405 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
406 | 858693c6 | bellard | if (*p == ',') |
407 | 858693c6 | bellard | p++; |
408 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
409 | 858693c6 | bellard | if (type == 0 || type == 1) { |
410 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
411 | 858693c6 | bellard | put_packet(s, "OK");
|
412 | 858693c6 | bellard | } else {
|
413 | 858693c6 | bellard | goto breakpoint_error;
|
414 | 858693c6 | bellard | } |
415 | 858693c6 | bellard | break;
|
416 | 858693c6 | bellard | default:
|
417 | 858693c6 | bellard | // unknown_command:
|
418 | 858693c6 | bellard | /* put empty packet */
|
419 | 858693c6 | bellard | buf[0] = '\0'; |
420 | 858693c6 | bellard | put_packet(s, buf); |
421 | 858693c6 | bellard | break;
|
422 | 858693c6 | bellard | } |
423 | 858693c6 | bellard | return RS_IDLE;
|
424 | 858693c6 | bellard | } |
425 | 858693c6 | bellard | |
426 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
427 | 858693c6 | bellard | { |
428 | 858693c6 | bellard | GDBState *s = opaque; |
429 | 858693c6 | bellard | char buf[256]; |
430 | 858693c6 | bellard | int ret;
|
431 | 858693c6 | bellard | |
432 | 858693c6 | bellard | /* disable single step if it was enable */
|
433 | 858693c6 | bellard | cpu_single_step(cpu_single_env, 0);
|
434 | 858693c6 | bellard | |
435 | 858693c6 | bellard | if (reason == EXCP_DEBUG)
|
436 | 858693c6 | bellard | ret = SIGTRAP; |
437 | 858693c6 | bellard | else
|
438 | 858693c6 | bellard | ret = 0;
|
439 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
440 | 858693c6 | bellard | put_packet(s, buf); |
441 | 858693c6 | bellard | } |
442 | 858693c6 | bellard | |
443 | 858693c6 | bellard | static void gdb_read_byte(GDBState *s, int ch) |
444 | 858693c6 | bellard | { |
445 | 858693c6 | bellard | int i, csum;
|
446 | 858693c6 | bellard | char reply[1]; |
447 | 858693c6 | bellard | |
448 | 858693c6 | bellard | if (vm_running) {
|
449 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
450 | 858693c6 | bellard | it when receiving a char */
|
451 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
452 | 858693c6 | bellard | } else {
|
453 | 858693c6 | bellard | switch(s->state) {
|
454 | 858693c6 | bellard | case RS_IDLE:
|
455 | 858693c6 | bellard | if (ch == '$') { |
456 | 858693c6 | bellard | s->line_buf_index = 0;
|
457 | 858693c6 | bellard | s->state = RS_GETLINE; |
458 | c33a346e | bellard | } |
459 | b4608c04 | bellard | break;
|
460 | 858693c6 | bellard | case RS_GETLINE:
|
461 | 858693c6 | bellard | if (ch == '#') { |
462 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
463 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
464 | 858693c6 | bellard | s->state = RS_IDLE; |
465 | 4c3a88a2 | bellard | } else {
|
466 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
467 | 4c3a88a2 | bellard | } |
468 | 4c3a88a2 | bellard | break;
|
469 | 858693c6 | bellard | case RS_CHKSUM1:
|
470 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
471 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
472 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
473 | 858693c6 | bellard | break;
|
474 | 858693c6 | bellard | case RS_CHKSUM2:
|
475 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
476 | 858693c6 | bellard | csum = 0;
|
477 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
478 | 858693c6 | bellard | csum += s->line_buf[i]; |
479 | 858693c6 | bellard | } |
480 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
481 | 858693c6 | bellard | reply[0] = '-'; |
482 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
483 | 858693c6 | bellard | s->state = RS_IDLE; |
484 | 4c3a88a2 | bellard | } else {
|
485 | 858693c6 | bellard | reply[0] = '+'; |
486 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
487 | 858693c6 | bellard | s->state = gdb_handle_packet(s, s->line_buf); |
488 | 4c3a88a2 | bellard | } |
489 | 4c3a88a2 | bellard | break;
|
490 | 858693c6 | bellard | } |
491 | 858693c6 | bellard | } |
492 | 858693c6 | bellard | } |
493 | 858693c6 | bellard | |
494 | 858693c6 | bellard | static int gdb_can_read(void *opaque) |
495 | 858693c6 | bellard | { |
496 | 858693c6 | bellard | return 256; |
497 | 858693c6 | bellard | } |
498 | 858693c6 | bellard | |
499 | 858693c6 | bellard | static void gdb_read(void *opaque, const uint8_t *buf, int size) |
500 | 858693c6 | bellard | { |
501 | 858693c6 | bellard | GDBState *s = opaque; |
502 | 858693c6 | bellard | int i;
|
503 | 858693c6 | bellard | if (size == 0) { |
504 | 858693c6 | bellard | /* end of connection */
|
505 | 858693c6 | bellard | qemu_del_vm_stop_handler(gdb_vm_stopped, s); |
506 | 858693c6 | bellard | qemu_del_fd_read_handler(s->fd); |
507 | 858693c6 | bellard | qemu_free(s); |
508 | 858693c6 | bellard | vm_start(); |
509 | 858693c6 | bellard | } else {
|
510 | 858693c6 | bellard | for(i = 0; i < size; i++) |
511 | 858693c6 | bellard | gdb_read_byte(s, buf[i]); |
512 | 858693c6 | bellard | } |
513 | 858693c6 | bellard | } |
514 | 858693c6 | bellard | |
515 | 858693c6 | bellard | static void gdb_accept(void *opaque, const uint8_t *buf, int size) |
516 | 858693c6 | bellard | { |
517 | 858693c6 | bellard | GDBState *s; |
518 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
519 | 858693c6 | bellard | socklen_t len; |
520 | 858693c6 | bellard | int val, fd;
|
521 | 858693c6 | bellard | |
522 | 858693c6 | bellard | for(;;) {
|
523 | 858693c6 | bellard | len = sizeof(sockaddr);
|
524 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
525 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
526 | 858693c6 | bellard | perror("accept");
|
527 | 858693c6 | bellard | return;
|
528 | 858693c6 | bellard | } else if (fd >= 0) { |
529 | b4608c04 | bellard | break;
|
530 | b4608c04 | bellard | } |
531 | b4608c04 | bellard | } |
532 | 858693c6 | bellard | |
533 | 858693c6 | bellard | /* set short latency */
|
534 | 858693c6 | bellard | val = 1;
|
535 | 7d3505c5 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
|
536 | 858693c6 | bellard | |
537 | 858693c6 | bellard | s = qemu_mallocz(sizeof(GDBState));
|
538 | 858693c6 | bellard | if (!s) {
|
539 | 858693c6 | bellard | close(fd); |
540 | 858693c6 | bellard | return;
|
541 | 858693c6 | bellard | } |
542 | 858693c6 | bellard | s->fd = fd; |
543 | 858693c6 | bellard | |
544 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
545 | 858693c6 | bellard | |
546 | 858693c6 | bellard | /* stop the VM */
|
547 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
548 | 858693c6 | bellard | |
549 | 858693c6 | bellard | /* start handling I/O */
|
550 | 858693c6 | bellard | qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s); |
551 | 858693c6 | bellard | /* when the VM is stopped, the following callback is called */
|
552 | 858693c6 | bellard | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
553 | 858693c6 | bellard | } |
554 | 858693c6 | bellard | |
555 | 858693c6 | bellard | static int gdbserver_open(int port) |
556 | 858693c6 | bellard | { |
557 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
558 | 858693c6 | bellard | int fd, val, ret;
|
559 | 858693c6 | bellard | |
560 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
561 | 858693c6 | bellard | if (fd < 0) { |
562 | 858693c6 | bellard | perror("socket");
|
563 | 858693c6 | bellard | return -1; |
564 | 858693c6 | bellard | } |
565 | 858693c6 | bellard | |
566 | 858693c6 | bellard | /* allow fast reuse */
|
567 | 858693c6 | bellard | val = 1;
|
568 | 858693c6 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
569 | 858693c6 | bellard | |
570 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
571 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
572 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
573 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
574 | 858693c6 | bellard | if (ret < 0) { |
575 | 858693c6 | bellard | perror("bind");
|
576 | 858693c6 | bellard | return -1; |
577 | 858693c6 | bellard | } |
578 | 858693c6 | bellard | ret = listen(fd, 0);
|
579 | 858693c6 | bellard | if (ret < 0) { |
580 | 858693c6 | bellard | perror("listen");
|
581 | 858693c6 | bellard | return -1; |
582 | 858693c6 | bellard | } |
583 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
584 | 858693c6 | bellard | return fd;
|
585 | 858693c6 | bellard | } |
586 | 858693c6 | bellard | |
587 | 858693c6 | bellard | int gdbserver_start(int port) |
588 | 858693c6 | bellard | { |
589 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
590 | 858693c6 | bellard | if (gdbserver_fd < 0) |
591 | 858693c6 | bellard | return -1; |
592 | 858693c6 | bellard | /* accept connections */
|
593 | 858693c6 | bellard | qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL); |
594 | b4608c04 | bellard | return 0; |
595 | b4608c04 | bellard | } |