root / gdbstub.c @ c5d6edc3
History | View | Annotate | Download (22.6 kB)
1 |
/*
|
---|---|
2 |
* gdb server stub
|
3 |
*
|
4 |
* Copyright (c) 2003-2005 Fabrice Bellard
|
5 |
*
|
6 |
* This library is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU Lesser General Public
|
8 |
* License as published by the Free Software Foundation; either
|
9 |
* version 2 of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This library is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
* Lesser General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU Lesser General Public
|
17 |
* License along with this library; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
*/
|
20 |
#ifdef CONFIG_USER_ONLY
|
21 |
#include <stdlib.h> |
22 |
#include <stdio.h> |
23 |
#include <stdarg.h> |
24 |
#include <string.h> |
25 |
#include <errno.h> |
26 |
#include <unistd.h> |
27 |
|
28 |
#include "qemu.h" |
29 |
#else
|
30 |
#include "vl.h" |
31 |
#endif
|
32 |
|
33 |
#include "qemu_socket.h" |
34 |
#ifdef _WIN32
|
35 |
/* XXX: these constants may be independent of the host ones even for Unix */
|
36 |
#ifndef SIGTRAP
|
37 |
#define SIGTRAP 5 |
38 |
#endif
|
39 |
#ifndef SIGINT
|
40 |
#define SIGINT 2 |
41 |
#endif
|
42 |
#else
|
43 |
#include <signal.h> |
44 |
#endif
|
45 |
|
46 |
//#define DEBUG_GDB
|
47 |
|
48 |
enum RSState {
|
49 |
RS_IDLE, |
50 |
RS_GETLINE, |
51 |
RS_CHKSUM1, |
52 |
RS_CHKSUM2, |
53 |
}; |
54 |
/* XXX: This is not thread safe. Do we care? */
|
55 |
static int gdbserver_fd = -1; |
56 |
|
57 |
typedef struct GDBState { |
58 |
CPUState *env; /* current CPU */
|
59 |
enum RSState state; /* parsing state */ |
60 |
int fd;
|
61 |
char line_buf[4096]; |
62 |
int line_buf_index;
|
63 |
int line_csum;
|
64 |
#ifdef CONFIG_USER_ONLY
|
65 |
int running_state;
|
66 |
#endif
|
67 |
} GDBState; |
68 |
|
69 |
#ifdef CONFIG_USER_ONLY
|
70 |
/* XXX: remove this hack. */
|
71 |
static GDBState gdbserver_state;
|
72 |
#endif
|
73 |
|
74 |
static int get_char(GDBState *s) |
75 |
{ |
76 |
uint8_t ch; |
77 |
int ret;
|
78 |
|
79 |
for(;;) {
|
80 |
ret = recv(s->fd, &ch, 1, 0); |
81 |
if (ret < 0) { |
82 |
if (errno != EINTR && errno != EAGAIN)
|
83 |
return -1; |
84 |
} else if (ret == 0) { |
85 |
return -1; |
86 |
} else {
|
87 |
break;
|
88 |
} |
89 |
} |
90 |
return ch;
|
91 |
} |
92 |
|
93 |
static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
94 |
{ |
95 |
int ret;
|
96 |
|
97 |
while (len > 0) { |
98 |
ret = send(s->fd, buf, len, 0);
|
99 |
if (ret < 0) { |
100 |
if (errno != EINTR && errno != EAGAIN)
|
101 |
return;
|
102 |
} else {
|
103 |
buf += ret; |
104 |
len -= ret; |
105 |
} |
106 |
} |
107 |
} |
108 |
|
109 |
static inline int fromhex(int v) |
110 |
{ |
111 |
if (v >= '0' && v <= '9') |
112 |
return v - '0'; |
113 |
else if (v >= 'A' && v <= 'F') |
114 |
return v - 'A' + 10; |
115 |
else if (v >= 'a' && v <= 'f') |
116 |
return v - 'a' + 10; |
117 |
else
|
118 |
return 0; |
119 |
} |
120 |
|
121 |
static inline int tohex(int v) |
122 |
{ |
123 |
if (v < 10) |
124 |
return v + '0'; |
125 |
else
|
126 |
return v - 10 + 'a'; |
127 |
} |
128 |
|
129 |
static void memtohex(char *buf, const uint8_t *mem, int len) |
130 |
{ |
131 |
int i, c;
|
132 |
char *q;
|
133 |
q = buf; |
134 |
for(i = 0; i < len; i++) { |
135 |
c = mem[i]; |
136 |
*q++ = tohex(c >> 4);
|
137 |
*q++ = tohex(c & 0xf);
|
138 |
} |
139 |
*q = '\0';
|
140 |
} |
141 |
|
142 |
static void hextomem(uint8_t *mem, const char *buf, int len) |
143 |
{ |
144 |
int i;
|
145 |
|
146 |
for(i = 0; i < len; i++) { |
147 |
mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
148 |
buf += 2;
|
149 |
} |
150 |
} |
151 |
|
152 |
/* return -1 if error, 0 if OK */
|
153 |
static int put_packet(GDBState *s, char *buf) |
154 |
{ |
155 |
char buf1[3]; |
156 |
int len, csum, ch, i;
|
157 |
|
158 |
#ifdef DEBUG_GDB
|
159 |
printf("reply='%s'\n", buf);
|
160 |
#endif
|
161 |
|
162 |
for(;;) {
|
163 |
buf1[0] = '$'; |
164 |
put_buffer(s, buf1, 1);
|
165 |
len = strlen(buf); |
166 |
put_buffer(s, buf, len); |
167 |
csum = 0;
|
168 |
for(i = 0; i < len; i++) { |
169 |
csum += buf[i]; |
170 |
} |
171 |
buf1[0] = '#'; |
172 |
buf1[1] = tohex((csum >> 4) & 0xf); |
173 |
buf1[2] = tohex((csum) & 0xf); |
174 |
|
175 |
put_buffer(s, buf1, 3);
|
176 |
|
177 |
ch = get_char(s); |
178 |
if (ch < 0) |
179 |
return -1; |
180 |
if (ch == '+') |
181 |
break;
|
182 |
} |
183 |
return 0; |
184 |
} |
185 |
|
186 |
#if defined(TARGET_I386)
|
187 |
|
188 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
189 |
{ |
190 |
uint32_t *registers = (uint32_t *)mem_buf; |
191 |
int i, fpus;
|
192 |
|
193 |
for(i = 0; i < 8; i++) { |
194 |
registers[i] = env->regs[i]; |
195 |
} |
196 |
registers[8] = env->eip;
|
197 |
registers[9] = env->eflags;
|
198 |
registers[10] = env->segs[R_CS].selector;
|
199 |
registers[11] = env->segs[R_SS].selector;
|
200 |
registers[12] = env->segs[R_DS].selector;
|
201 |
registers[13] = env->segs[R_ES].selector;
|
202 |
registers[14] = env->segs[R_FS].selector;
|
203 |
registers[15] = env->segs[R_GS].selector;
|
204 |
/* XXX: convert floats */
|
205 |
for(i = 0; i < 8; i++) { |
206 |
memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
207 |
} |
208 |
registers[36] = env->fpuc;
|
209 |
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
210 |
registers[37] = fpus;
|
211 |
registers[38] = 0; /* XXX: convert tags */ |
212 |
registers[39] = 0; /* fiseg */ |
213 |
registers[40] = 0; /* fioff */ |
214 |
registers[41] = 0; /* foseg */ |
215 |
registers[42] = 0; /* fooff */ |
216 |
registers[43] = 0; /* fop */ |
217 |
|
218 |
for(i = 0; i < 16; i++) |
219 |
tswapls(®isters[i]); |
220 |
for(i = 36; i < 44; i++) |
221 |
tswapls(®isters[i]); |
222 |
return 44 * 4; |
223 |
} |
224 |
|
225 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
226 |
{ |
227 |
uint32_t *registers = (uint32_t *)mem_buf; |
228 |
int i;
|
229 |
|
230 |
for(i = 0; i < 8; i++) { |
231 |
env->regs[i] = tswapl(registers[i]); |
232 |
} |
233 |
env->eip = tswapl(registers[8]);
|
234 |
env->eflags = tswapl(registers[9]);
|
235 |
#if defined(CONFIG_USER_ONLY)
|
236 |
#define LOAD_SEG(index, sreg)\
|
237 |
if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
238 |
cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
239 |
LOAD_SEG(10, R_CS);
|
240 |
LOAD_SEG(11, R_SS);
|
241 |
LOAD_SEG(12, R_DS);
|
242 |
LOAD_SEG(13, R_ES);
|
243 |
LOAD_SEG(14, R_FS);
|
244 |
LOAD_SEG(15, R_GS);
|
245 |
#endif
|
246 |
} |
247 |
|
248 |
#elif defined (TARGET_PPC)
|
249 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
250 |
{ |
251 |
uint32_t *registers = (uint32_t *)mem_buf, tmp; |
252 |
int i;
|
253 |
|
254 |
/* fill in gprs */
|
255 |
for(i = 0; i < 32; i++) { |
256 |
registers[i] = tswapl(env->gpr[i]); |
257 |
} |
258 |
/* fill in fprs */
|
259 |
for (i = 0; i < 32; i++) { |
260 |
registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
261 |
registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
262 |
} |
263 |
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
264 |
registers[96] = tswapl(env->nip);
|
265 |
registers[97] = tswapl(do_load_msr(env));
|
266 |
tmp = 0;
|
267 |
for (i = 0; i < 8; i++) |
268 |
tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
269 |
registers[98] = tswapl(tmp);
|
270 |
registers[99] = tswapl(env->lr);
|
271 |
registers[100] = tswapl(env->ctr);
|
272 |
registers[101] = tswapl(do_load_xer(env));
|
273 |
registers[102] = 0; |
274 |
|
275 |
return 103 * 4; |
276 |
} |
277 |
|
278 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
279 |
{ |
280 |
uint32_t *registers = (uint32_t *)mem_buf; |
281 |
int i;
|
282 |
|
283 |
/* fill in gprs */
|
284 |
for (i = 0; i < 32; i++) { |
285 |
env->gpr[i] = tswapl(registers[i]); |
286 |
} |
287 |
/* fill in fprs */
|
288 |
for (i = 0; i < 32; i++) { |
289 |
*((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
290 |
*((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
291 |
} |
292 |
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
293 |
env->nip = tswapl(registers[96]);
|
294 |
do_store_msr(env, tswapl(registers[97]));
|
295 |
registers[98] = tswapl(registers[98]); |
296 |
for (i = 0; i < 8; i++) |
297 |
env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
298 |
env->lr = tswapl(registers[99]);
|
299 |
env->ctr = tswapl(registers[100]);
|
300 |
do_store_xer(env, tswapl(registers[101]));
|
301 |
} |
302 |
#elif defined (TARGET_SPARC)
|
303 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
304 |
{ |
305 |
target_ulong *registers = (target_ulong *)mem_buf; |
306 |
int i;
|
307 |
|
308 |
/* fill in g0..g7 */
|
309 |
for(i = 0; i < 8; i++) { |
310 |
registers[i] = tswapl(env->gregs[i]); |
311 |
} |
312 |
/* fill in register window */
|
313 |
for(i = 0; i < 24; i++) { |
314 |
registers[i + 8] = tswapl(env->regwptr[i]);
|
315 |
} |
316 |
/* fill in fprs */
|
317 |
for (i = 0; i < 32; i++) { |
318 |
registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
319 |
} |
320 |
#ifndef TARGET_SPARC64
|
321 |
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
322 |
registers[64] = tswapl(env->y);
|
323 |
{ |
324 |
target_ulong tmp; |
325 |
|
326 |
tmp = GET_PSR(env); |
327 |
registers[65] = tswapl(tmp);
|
328 |
} |
329 |
registers[66] = tswapl(env->wim);
|
330 |
registers[67] = tswapl(env->tbr);
|
331 |
registers[68] = tswapl(env->pc);
|
332 |
registers[69] = tswapl(env->npc);
|
333 |
registers[70] = tswapl(env->fsr);
|
334 |
registers[71] = 0; /* csr */ |
335 |
registers[72] = 0; |
336 |
return 73 * sizeof(target_ulong); |
337 |
#else
|
338 |
for (i = 0; i < 32; i += 2) { |
339 |
registers[i/2 + 64] = tswapl(*((uint64_t *)&env->fpr[i])); |
340 |
} |
341 |
registers[81] = tswapl(env->pc);
|
342 |
registers[82] = tswapl(env->npc);
|
343 |
registers[83] = tswapl(env->tstate[env->tl]);
|
344 |
registers[84] = tswapl(env->fsr);
|
345 |
registers[85] = tswapl(env->fprs);
|
346 |
registers[86] = tswapl(env->y);
|
347 |
return 87 * sizeof(target_ulong); |
348 |
#endif
|
349 |
} |
350 |
|
351 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
352 |
{ |
353 |
target_ulong *registers = (target_ulong *)mem_buf; |
354 |
int i;
|
355 |
|
356 |
/* fill in g0..g7 */
|
357 |
for(i = 0; i < 7; i++) { |
358 |
env->gregs[i] = tswapl(registers[i]); |
359 |
} |
360 |
/* fill in register window */
|
361 |
for(i = 0; i < 24; i++) { |
362 |
env->regwptr[i] = tswapl(registers[i + 8]);
|
363 |
} |
364 |
/* fill in fprs */
|
365 |
for (i = 0; i < 32; i++) { |
366 |
*((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
367 |
} |
368 |
#ifndef TARGET_SPARC64
|
369 |
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
370 |
env->y = tswapl(registers[64]);
|
371 |
PUT_PSR(env, tswapl(registers[65]));
|
372 |
env->wim = tswapl(registers[66]);
|
373 |
env->tbr = tswapl(registers[67]);
|
374 |
env->pc = tswapl(registers[68]);
|
375 |
env->npc = tswapl(registers[69]);
|
376 |
env->fsr = tswapl(registers[70]);
|
377 |
#else
|
378 |
for (i = 0; i < 32; i += 2) { |
379 |
uint64_t tmp; |
380 |
tmp = tswapl(registers[i/2 + 64]) << 32; |
381 |
tmp |= tswapl(registers[i/2 + 64 + 1]); |
382 |
*((uint64_t *)&env->fpr[i]) = tmp; |
383 |
} |
384 |
env->pc = tswapl(registers[81]);
|
385 |
env->npc = tswapl(registers[82]);
|
386 |
env->tstate[env->tl] = tswapl(registers[83]);
|
387 |
env->fsr = tswapl(registers[84]);
|
388 |
env->fprs = tswapl(registers[85]);
|
389 |
env->y = tswapl(registers[86]);
|
390 |
#endif
|
391 |
} |
392 |
#elif defined (TARGET_ARM)
|
393 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
394 |
{ |
395 |
int i;
|
396 |
uint8_t *ptr; |
397 |
|
398 |
ptr = mem_buf; |
399 |
/* 16 core integer registers (4 bytes each). */
|
400 |
for (i = 0; i < 16; i++) |
401 |
{ |
402 |
*(uint32_t *)ptr = tswapl(env->regs[i]); |
403 |
ptr += 4;
|
404 |
} |
405 |
/* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
406 |
Not yet implemented. */
|
407 |
memset (ptr, 0, 8 * 12 + 4); |
408 |
ptr += 8 * 12 + 4; |
409 |
/* CPSR (4 bytes). */
|
410 |
*(uint32_t *)ptr = tswapl (cpsr_read(env)); |
411 |
ptr += 4;
|
412 |
|
413 |
return ptr - mem_buf;
|
414 |
} |
415 |
|
416 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
417 |
{ |
418 |
int i;
|
419 |
uint8_t *ptr; |
420 |
|
421 |
ptr = mem_buf; |
422 |
/* Core integer registers. */
|
423 |
for (i = 0; i < 16; i++) |
424 |
{ |
425 |
env->regs[i] = tswapl(*(uint32_t *)ptr); |
426 |
ptr += 4;
|
427 |
} |
428 |
/* Ignore FPA regs and scr. */
|
429 |
ptr += 8 * 12 + 4; |
430 |
cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
|
431 |
} |
432 |
#elif defined (TARGET_MIPS)
|
433 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
434 |
{ |
435 |
int i;
|
436 |
uint8_t *ptr; |
437 |
|
438 |
ptr = mem_buf; |
439 |
for (i = 0; i < 32; i++) |
440 |
{ |
441 |
*(uint32_t *)ptr = tswapl(env->gpr[i]); |
442 |
ptr += 4;
|
443 |
} |
444 |
|
445 |
*(uint32_t *)ptr = tswapl(env->CP0_Status); |
446 |
ptr += 4;
|
447 |
|
448 |
*(uint32_t *)ptr = tswapl(env->LO); |
449 |
ptr += 4;
|
450 |
|
451 |
*(uint32_t *)ptr = tswapl(env->HI); |
452 |
ptr += 4;
|
453 |
|
454 |
*(uint32_t *)ptr = tswapl(env->CP0_BadVAddr); |
455 |
ptr += 4;
|
456 |
|
457 |
*(uint32_t *)ptr = tswapl(env->CP0_Cause); |
458 |
ptr += 4;
|
459 |
|
460 |
*(uint32_t *)ptr = tswapl(env->PC); |
461 |
ptr += 4;
|
462 |
|
463 |
/* 32 FP registers, fsr, fir, fp. Not yet implemented. */
|
464 |
|
465 |
return ptr - mem_buf;
|
466 |
} |
467 |
|
468 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
469 |
{ |
470 |
int i;
|
471 |
uint8_t *ptr; |
472 |
|
473 |
ptr = mem_buf; |
474 |
for (i = 0; i < 32; i++) |
475 |
{ |
476 |
env->gpr[i] = tswapl(*(uint32_t *)ptr); |
477 |
ptr += 4;
|
478 |
} |
479 |
|
480 |
env->CP0_Status = tswapl(*(uint32_t *)ptr); |
481 |
ptr += 4;
|
482 |
|
483 |
env->LO = tswapl(*(uint32_t *)ptr); |
484 |
ptr += 4;
|
485 |
|
486 |
env->HI = tswapl(*(uint32_t *)ptr); |
487 |
ptr += 4;
|
488 |
|
489 |
env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr); |
490 |
ptr += 4;
|
491 |
|
492 |
env->CP0_Cause = tswapl(*(uint32_t *)ptr); |
493 |
ptr += 4;
|
494 |
|
495 |
env->PC = tswapl(*(uint32_t *)ptr); |
496 |
ptr += 4;
|
497 |
} |
498 |
#elif defined (TARGET_SH4)
|
499 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
500 |
{ |
501 |
uint32_t *ptr = (uint32_t *)mem_buf; |
502 |
int i;
|
503 |
|
504 |
#define SAVE(x) *ptr++=tswapl(x)
|
505 |
for (i = 0; i < 16; i++) SAVE(env->gregs[i]); |
506 |
SAVE (env->pc); |
507 |
SAVE (env->pr); |
508 |
SAVE (env->gbr); |
509 |
SAVE (env->vbr); |
510 |
SAVE (env->mach); |
511 |
SAVE (env->macl); |
512 |
SAVE (env->sr); |
513 |
SAVE (0); /* TICKS */ |
514 |
SAVE (0); /* STALLS */ |
515 |
SAVE (0); /* CYCLES */ |
516 |
SAVE (0); /* INSTS */ |
517 |
SAVE (0); /* PLR */ |
518 |
|
519 |
return ((uint8_t *)ptr - mem_buf);
|
520 |
} |
521 |
|
522 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
523 |
{ |
524 |
uint32_t *ptr = (uint32_t *)mem_buf; |
525 |
int i;
|
526 |
|
527 |
#define LOAD(x) (x)=*ptr++;
|
528 |
for (i = 0; i < 16; i++) LOAD(env->gregs[i]); |
529 |
LOAD (env->pc); |
530 |
LOAD (env->pr); |
531 |
LOAD (env->gbr); |
532 |
LOAD (env->vbr); |
533 |
LOAD (env->mach); |
534 |
LOAD (env->macl); |
535 |
LOAD (env->sr); |
536 |
} |
537 |
#else
|
538 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
539 |
{ |
540 |
return 0; |
541 |
} |
542 |
|
543 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
544 |
{ |
545 |
} |
546 |
|
547 |
#endif
|
548 |
|
549 |
static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
550 |
{ |
551 |
const char *p; |
552 |
int ch, reg_size, type;
|
553 |
char buf[4096]; |
554 |
uint8_t mem_buf[2000];
|
555 |
uint32_t *registers; |
556 |
uint32_t addr, len; |
557 |
|
558 |
#ifdef DEBUG_GDB
|
559 |
printf("command='%s'\n", line_buf);
|
560 |
#endif
|
561 |
p = line_buf; |
562 |
ch = *p++; |
563 |
switch(ch) {
|
564 |
case '?': |
565 |
/* TODO: Make this return the correct value for user-mode. */
|
566 |
snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
567 |
put_packet(s, buf); |
568 |
break;
|
569 |
case 'c': |
570 |
if (*p != '\0') { |
571 |
addr = strtoul(p, (char **)&p, 16); |
572 |
#if defined(TARGET_I386)
|
573 |
env->eip = addr; |
574 |
#elif defined (TARGET_PPC)
|
575 |
env->nip = addr; |
576 |
#elif defined (TARGET_SPARC)
|
577 |
env->pc = addr; |
578 |
env->npc = addr + 4;
|
579 |
#elif defined (TARGET_ARM)
|
580 |
env->regs[15] = addr;
|
581 |
#elif defined (TARGET_SH4)
|
582 |
env->pc = addr; |
583 |
#endif
|
584 |
} |
585 |
#ifdef CONFIG_USER_ONLY
|
586 |
s->running_state = 1;
|
587 |
#else
|
588 |
vm_start(); |
589 |
#endif
|
590 |
return RS_IDLE;
|
591 |
case 's': |
592 |
if (*p != '\0') { |
593 |
addr = strtoul(p, (char **)&p, 16); |
594 |
#if defined(TARGET_I386)
|
595 |
env->eip = addr; |
596 |
#elif defined (TARGET_PPC)
|
597 |
env->nip = addr; |
598 |
#elif defined (TARGET_SPARC)
|
599 |
env->pc = addr; |
600 |
env->npc = addr + 4;
|
601 |
#elif defined (TARGET_ARM)
|
602 |
env->regs[15] = addr;
|
603 |
#elif defined (TARGET_SH4)
|
604 |
env->pc = addr; |
605 |
#endif
|
606 |
} |
607 |
cpu_single_step(env, 1);
|
608 |
#ifdef CONFIG_USER_ONLY
|
609 |
s->running_state = 1;
|
610 |
#else
|
611 |
vm_start(); |
612 |
#endif
|
613 |
return RS_IDLE;
|
614 |
case 'g': |
615 |
reg_size = cpu_gdb_read_registers(env, mem_buf); |
616 |
memtohex(buf, mem_buf, reg_size); |
617 |
put_packet(s, buf); |
618 |
break;
|
619 |
case 'G': |
620 |
registers = (void *)mem_buf;
|
621 |
len = strlen(p) / 2;
|
622 |
hextomem((uint8_t *)registers, p, len); |
623 |
cpu_gdb_write_registers(env, mem_buf, len); |
624 |
put_packet(s, "OK");
|
625 |
break;
|
626 |
case 'm': |
627 |
addr = strtoul(p, (char **)&p, 16); |
628 |
if (*p == ',') |
629 |
p++; |
630 |
len = strtoul(p, NULL, 16); |
631 |
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) { |
632 |
put_packet (s, "E14");
|
633 |
} else {
|
634 |
memtohex(buf, mem_buf, len); |
635 |
put_packet(s, buf); |
636 |
} |
637 |
break;
|
638 |
case 'M': |
639 |
addr = strtoul(p, (char **)&p, 16); |
640 |
if (*p == ',') |
641 |
p++; |
642 |
len = strtoul(p, (char **)&p, 16); |
643 |
if (*p == ':') |
644 |
p++; |
645 |
hextomem(mem_buf, p, len); |
646 |
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
647 |
put_packet(s, "E14");
|
648 |
else
|
649 |
put_packet(s, "OK");
|
650 |
break;
|
651 |
case 'Z': |
652 |
type = strtoul(p, (char **)&p, 16); |
653 |
if (*p == ',') |
654 |
p++; |
655 |
addr = strtoul(p, (char **)&p, 16); |
656 |
if (*p == ',') |
657 |
p++; |
658 |
len = strtoul(p, (char **)&p, 16); |
659 |
if (type == 0 || type == 1) { |
660 |
if (cpu_breakpoint_insert(env, addr) < 0) |
661 |
goto breakpoint_error;
|
662 |
put_packet(s, "OK");
|
663 |
} else {
|
664 |
breakpoint_error:
|
665 |
put_packet(s, "E22");
|
666 |
} |
667 |
break;
|
668 |
case 'z': |
669 |
type = strtoul(p, (char **)&p, 16); |
670 |
if (*p == ',') |
671 |
p++; |
672 |
addr = strtoul(p, (char **)&p, 16); |
673 |
if (*p == ',') |
674 |
p++; |
675 |
len = strtoul(p, (char **)&p, 16); |
676 |
if (type == 0 || type == 1) { |
677 |
cpu_breakpoint_remove(env, addr); |
678 |
put_packet(s, "OK");
|
679 |
} else {
|
680 |
goto breakpoint_error;
|
681 |
} |
682 |
break;
|
683 |
default:
|
684 |
// unknown_command:
|
685 |
/* put empty packet */
|
686 |
buf[0] = '\0'; |
687 |
put_packet(s, buf); |
688 |
break;
|
689 |
} |
690 |
return RS_IDLE;
|
691 |
} |
692 |
|
693 |
extern void tb_flush(CPUState *env); |
694 |
|
695 |
#ifndef CONFIG_USER_ONLY
|
696 |
static void gdb_vm_stopped(void *opaque, int reason) |
697 |
{ |
698 |
GDBState *s = opaque; |
699 |
char buf[256]; |
700 |
int ret;
|
701 |
|
702 |
/* disable single step if it was enable */
|
703 |
cpu_single_step(s->env, 0);
|
704 |
|
705 |
if (reason == EXCP_DEBUG) {
|
706 |
tb_flush(s->env); |
707 |
ret = SIGTRAP; |
708 |
} else if (reason == EXCP_INTERRUPT) { |
709 |
ret = SIGINT; |
710 |
} else {
|
711 |
ret = 0;
|
712 |
} |
713 |
snprintf(buf, sizeof(buf), "S%02x", ret); |
714 |
put_packet(s, buf); |
715 |
} |
716 |
#endif
|
717 |
|
718 |
static void gdb_read_byte(GDBState *s, int ch) |
719 |
{ |
720 |
CPUState *env = s->env; |
721 |
int i, csum;
|
722 |
char reply[1]; |
723 |
|
724 |
#ifndef CONFIG_USER_ONLY
|
725 |
if (vm_running) {
|
726 |
/* when the CPU is running, we cannot do anything except stop
|
727 |
it when receiving a char */
|
728 |
vm_stop(EXCP_INTERRUPT); |
729 |
} else
|
730 |
#endif
|
731 |
{ |
732 |
switch(s->state) {
|
733 |
case RS_IDLE:
|
734 |
if (ch == '$') { |
735 |
s->line_buf_index = 0;
|
736 |
s->state = RS_GETLINE; |
737 |
} |
738 |
break;
|
739 |
case RS_GETLINE:
|
740 |
if (ch == '#') { |
741 |
s->state = RS_CHKSUM1; |
742 |
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
743 |
s->state = RS_IDLE; |
744 |
} else {
|
745 |
s->line_buf[s->line_buf_index++] = ch; |
746 |
} |
747 |
break;
|
748 |
case RS_CHKSUM1:
|
749 |
s->line_buf[s->line_buf_index] = '\0';
|
750 |
s->line_csum = fromhex(ch) << 4;
|
751 |
s->state = RS_CHKSUM2; |
752 |
break;
|
753 |
case RS_CHKSUM2:
|
754 |
s->line_csum |= fromhex(ch); |
755 |
csum = 0;
|
756 |
for(i = 0; i < s->line_buf_index; i++) { |
757 |
csum += s->line_buf[i]; |
758 |
} |
759 |
if (s->line_csum != (csum & 0xff)) { |
760 |
reply[0] = '-'; |
761 |
put_buffer(s, reply, 1);
|
762 |
s->state = RS_IDLE; |
763 |
} else {
|
764 |
reply[0] = '+'; |
765 |
put_buffer(s, reply, 1);
|
766 |
s->state = gdb_handle_packet(s, env, s->line_buf); |
767 |
} |
768 |
break;
|
769 |
} |
770 |
} |
771 |
} |
772 |
|
773 |
#ifdef CONFIG_USER_ONLY
|
774 |
int
|
775 |
gdb_handlesig (CPUState *env, int sig)
|
776 |
{ |
777 |
GDBState *s; |
778 |
char buf[256]; |
779 |
int n;
|
780 |
|
781 |
if (gdbserver_fd < 0) |
782 |
return sig;
|
783 |
|
784 |
s = &gdbserver_state; |
785 |
|
786 |
/* disable single step if it was enabled */
|
787 |
cpu_single_step(env, 0);
|
788 |
tb_flush(env); |
789 |
|
790 |
if (sig != 0) |
791 |
{ |
792 |
snprintf(buf, sizeof(buf), "S%02x", sig); |
793 |
put_packet(s, buf); |
794 |
} |
795 |
|
796 |
sig = 0;
|
797 |
s->state = RS_IDLE; |
798 |
s->running_state = 0;
|
799 |
while (s->running_state == 0) { |
800 |
n = read (s->fd, buf, 256);
|
801 |
if (n > 0) |
802 |
{ |
803 |
int i;
|
804 |
|
805 |
for (i = 0; i < n; i++) |
806 |
gdb_read_byte (s, buf[i]); |
807 |
} |
808 |
else if (n == 0 || errno != EAGAIN) |
809 |
{ |
810 |
/* XXX: Connection closed. Should probably wait for annother
|
811 |
connection before continuing. */
|
812 |
return sig;
|
813 |
} |
814 |
} |
815 |
return sig;
|
816 |
} |
817 |
|
818 |
/* Tell the remote gdb that the process has exited. */
|
819 |
void gdb_exit(CPUState *env, int code) |
820 |
{ |
821 |
GDBState *s; |
822 |
char buf[4]; |
823 |
|
824 |
if (gdbserver_fd < 0) |
825 |
return;
|
826 |
|
827 |
s = &gdbserver_state; |
828 |
|
829 |
snprintf(buf, sizeof(buf), "W%02x", code); |
830 |
put_packet(s, buf); |
831 |
} |
832 |
|
833 |
#else
|
834 |
static void gdb_read(void *opaque) |
835 |
{ |
836 |
GDBState *s = opaque; |
837 |
int i, size;
|
838 |
uint8_t buf[4096];
|
839 |
|
840 |
size = recv(s->fd, buf, sizeof(buf), 0); |
841 |
if (size < 0) |
842 |
return;
|
843 |
if (size == 0) { |
844 |
/* end of connection */
|
845 |
qemu_del_vm_stop_handler(gdb_vm_stopped, s); |
846 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
847 |
qemu_free(s); |
848 |
vm_start(); |
849 |
} else {
|
850 |
for(i = 0; i < size; i++) |
851 |
gdb_read_byte(s, buf[i]); |
852 |
} |
853 |
} |
854 |
|
855 |
#endif
|
856 |
|
857 |
static void gdb_accept(void *opaque) |
858 |
{ |
859 |
GDBState *s; |
860 |
struct sockaddr_in sockaddr;
|
861 |
socklen_t len; |
862 |
int val, fd;
|
863 |
|
864 |
for(;;) {
|
865 |
len = sizeof(sockaddr);
|
866 |
fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
867 |
if (fd < 0 && errno != EINTR) { |
868 |
perror("accept");
|
869 |
return;
|
870 |
} else if (fd >= 0) { |
871 |
break;
|
872 |
} |
873 |
} |
874 |
|
875 |
/* set short latency */
|
876 |
val = 1;
|
877 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
878 |
|
879 |
#ifdef CONFIG_USER_ONLY
|
880 |
s = &gdbserver_state; |
881 |
memset (s, 0, sizeof (GDBState)); |
882 |
#else
|
883 |
s = qemu_mallocz(sizeof(GDBState));
|
884 |
if (!s) {
|
885 |
close(fd); |
886 |
return;
|
887 |
} |
888 |
#endif
|
889 |
s->env = first_cpu; /* XXX: allow to change CPU */
|
890 |
s->fd = fd; |
891 |
|
892 |
#ifdef CONFIG_USER_ONLY
|
893 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
894 |
#else
|
895 |
socket_set_nonblock(fd); |
896 |
|
897 |
/* stop the VM */
|
898 |
vm_stop(EXCP_INTERRUPT); |
899 |
|
900 |
/* start handling I/O */
|
901 |
qemu_set_fd_handler(s->fd, gdb_read, NULL, s);
|
902 |
/* when the VM is stopped, the following callback is called */
|
903 |
qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
904 |
#endif
|
905 |
} |
906 |
|
907 |
static int gdbserver_open(int port) |
908 |
{ |
909 |
struct sockaddr_in sockaddr;
|
910 |
int fd, val, ret;
|
911 |
|
912 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
913 |
if (fd < 0) { |
914 |
perror("socket");
|
915 |
return -1; |
916 |
} |
917 |
|
918 |
/* allow fast reuse */
|
919 |
val = 1;
|
920 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
921 |
|
922 |
sockaddr.sin_family = AF_INET; |
923 |
sockaddr.sin_port = htons(port); |
924 |
sockaddr.sin_addr.s_addr = 0;
|
925 |
ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
926 |
if (ret < 0) { |
927 |
perror("bind");
|
928 |
return -1; |
929 |
} |
930 |
ret = listen(fd, 0);
|
931 |
if (ret < 0) { |
932 |
perror("listen");
|
933 |
return -1; |
934 |
} |
935 |
#ifndef CONFIG_USER_ONLY
|
936 |
socket_set_nonblock(fd); |
937 |
#endif
|
938 |
return fd;
|
939 |
} |
940 |
|
941 |
int gdbserver_start(int port) |
942 |
{ |
943 |
gdbserver_fd = gdbserver_open(port); |
944 |
if (gdbserver_fd < 0) |
945 |
return -1; |
946 |
/* accept connections */
|
947 |
#ifdef CONFIG_USER_ONLY
|
948 |
gdb_accept (NULL);
|
949 |
#else
|
950 |
qemu_set_fd_handler(gdbserver_fd, gdb_accept, NULL, NULL); |
951 |
#endif
|
952 |
return 0; |
953 |
} |