root / gdbstub.c @ 4ad5b06d
History | View | Annotate | Download (30.3 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 |
#include "config.h" |
21 |
#ifdef CONFIG_USER_ONLY
|
22 |
#include <stdlib.h> |
23 |
#include <stdio.h> |
24 |
#include <stdarg.h> |
25 |
#include <string.h> |
26 |
#include <errno.h> |
27 |
#include <unistd.h> |
28 |
#include <fcntl.h> |
29 |
|
30 |
#include "qemu.h" |
31 |
#else
|
32 |
#include "vl.h" |
33 |
#endif
|
34 |
|
35 |
#include "qemu_socket.h" |
36 |
#ifdef _WIN32
|
37 |
/* XXX: these constants may be independent of the host ones even for Unix */
|
38 |
#ifndef SIGTRAP
|
39 |
#define SIGTRAP 5 |
40 |
#endif
|
41 |
#ifndef SIGINT
|
42 |
#define SIGINT 2 |
43 |
#endif
|
44 |
#else
|
45 |
#include <signal.h> |
46 |
#endif
|
47 |
|
48 |
//#define DEBUG_GDB
|
49 |
|
50 |
enum RSState {
|
51 |
RS_IDLE, |
52 |
RS_GETLINE, |
53 |
RS_CHKSUM1, |
54 |
RS_CHKSUM2, |
55 |
RS_SYSCALL, |
56 |
}; |
57 |
typedef struct GDBState { |
58 |
CPUState *env; /* current CPU */
|
59 |
enum RSState state; /* parsing state */ |
60 |
char line_buf[4096]; |
61 |
int line_buf_index;
|
62 |
int line_csum;
|
63 |
char last_packet[4100]; |
64 |
int last_packet_len;
|
65 |
#ifdef CONFIG_USER_ONLY
|
66 |
int fd;
|
67 |
int running_state;
|
68 |
#else
|
69 |
CharDriverState *chr; |
70 |
#endif
|
71 |
} GDBState; |
72 |
|
73 |
#ifdef CONFIG_USER_ONLY
|
74 |
/* XXX: This is not thread safe. Do we care? */
|
75 |
static int gdbserver_fd = -1; |
76 |
|
77 |
/* XXX: remove this hack. */
|
78 |
static GDBState gdbserver_state;
|
79 |
|
80 |
static int get_char(GDBState *s) |
81 |
{ |
82 |
uint8_t ch; |
83 |
int ret;
|
84 |
|
85 |
for(;;) {
|
86 |
ret = recv(s->fd, &ch, 1, 0); |
87 |
if (ret < 0) { |
88 |
if (errno != EINTR && errno != EAGAIN)
|
89 |
return -1; |
90 |
} else if (ret == 0) { |
91 |
return -1; |
92 |
} else {
|
93 |
break;
|
94 |
} |
95 |
} |
96 |
return ch;
|
97 |
} |
98 |
#endif
|
99 |
|
100 |
/* GDB stub state for use by semihosting syscalls. */
|
101 |
static GDBState *gdb_syscall_state;
|
102 |
static gdb_syscall_complete_cb gdb_current_syscall_cb;
|
103 |
|
104 |
enum {
|
105 |
GDB_SYS_UNKNOWN, |
106 |
GDB_SYS_ENABLED, |
107 |
GDB_SYS_DISABLED, |
108 |
} gdb_syscall_mode; |
109 |
|
110 |
/* If gdb is connected when the first semihosting syscall occurs then use
|
111 |
remote gdb syscalls. Otherwise use native file IO. */
|
112 |
int use_gdb_syscalls(void) |
113 |
{ |
114 |
if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
|
115 |
gdb_syscall_mode = (gdb_syscall_state ? GDB_SYS_ENABLED |
116 |
: GDB_SYS_DISABLED); |
117 |
} |
118 |
return gdb_syscall_mode == GDB_SYS_ENABLED;
|
119 |
} |
120 |
|
121 |
static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
122 |
{ |
123 |
#ifdef CONFIG_USER_ONLY
|
124 |
int ret;
|
125 |
|
126 |
while (len > 0) { |
127 |
ret = send(s->fd, buf, len, 0);
|
128 |
if (ret < 0) { |
129 |
if (errno != EINTR && errno != EAGAIN)
|
130 |
return;
|
131 |
} else {
|
132 |
buf += ret; |
133 |
len -= ret; |
134 |
} |
135 |
} |
136 |
#else
|
137 |
qemu_chr_write(s->chr, buf, len); |
138 |
#endif
|
139 |
} |
140 |
|
141 |
static inline int fromhex(int v) |
142 |
{ |
143 |
if (v >= '0' && v <= '9') |
144 |
return v - '0'; |
145 |
else if (v >= 'A' && v <= 'F') |
146 |
return v - 'A' + 10; |
147 |
else if (v >= 'a' && v <= 'f') |
148 |
return v - 'a' + 10; |
149 |
else
|
150 |
return 0; |
151 |
} |
152 |
|
153 |
static inline int tohex(int v) |
154 |
{ |
155 |
if (v < 10) |
156 |
return v + '0'; |
157 |
else
|
158 |
return v - 10 + 'a'; |
159 |
} |
160 |
|
161 |
static void memtohex(char *buf, const uint8_t *mem, int len) |
162 |
{ |
163 |
int i, c;
|
164 |
char *q;
|
165 |
q = buf; |
166 |
for(i = 0; i < len; i++) { |
167 |
c = mem[i]; |
168 |
*q++ = tohex(c >> 4);
|
169 |
*q++ = tohex(c & 0xf);
|
170 |
} |
171 |
*q = '\0';
|
172 |
} |
173 |
|
174 |
static void hextomem(uint8_t *mem, const char *buf, int len) |
175 |
{ |
176 |
int i;
|
177 |
|
178 |
for(i = 0; i < len; i++) { |
179 |
mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
180 |
buf += 2;
|
181 |
} |
182 |
} |
183 |
|
184 |
/* return -1 if error, 0 if OK */
|
185 |
static int put_packet(GDBState *s, char *buf) |
186 |
{ |
187 |
int len, csum, i;
|
188 |
char *p;
|
189 |
|
190 |
#ifdef DEBUG_GDB
|
191 |
printf("reply='%s'\n", buf);
|
192 |
#endif
|
193 |
|
194 |
for(;;) {
|
195 |
p = s->last_packet; |
196 |
*(p++) = '$';
|
197 |
len = strlen(buf); |
198 |
memcpy(p, buf, len); |
199 |
p += len; |
200 |
csum = 0;
|
201 |
for(i = 0; i < len; i++) { |
202 |
csum += buf[i]; |
203 |
} |
204 |
*(p++) = '#';
|
205 |
*(p++) = tohex((csum >> 4) & 0xf); |
206 |
*(p++) = tohex((csum) & 0xf);
|
207 |
|
208 |
s->last_packet_len = p - s->last_packet; |
209 |
put_buffer(s, s->last_packet, s->last_packet_len); |
210 |
|
211 |
#ifdef CONFIG_USER_ONLY
|
212 |
i = get_char(s); |
213 |
if (i < 0) |
214 |
return -1; |
215 |
if (i == '+') |
216 |
break;
|
217 |
#else
|
218 |
break;
|
219 |
#endif
|
220 |
} |
221 |
return 0; |
222 |
} |
223 |
|
224 |
#if defined(TARGET_I386)
|
225 |
|
226 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
227 |
{ |
228 |
uint32_t *registers = (uint32_t *)mem_buf; |
229 |
int i, fpus;
|
230 |
|
231 |
for(i = 0; i < 8; i++) { |
232 |
registers[i] = env->regs[i]; |
233 |
} |
234 |
registers[8] = env->eip;
|
235 |
registers[9] = env->eflags;
|
236 |
registers[10] = env->segs[R_CS].selector;
|
237 |
registers[11] = env->segs[R_SS].selector;
|
238 |
registers[12] = env->segs[R_DS].selector;
|
239 |
registers[13] = env->segs[R_ES].selector;
|
240 |
registers[14] = env->segs[R_FS].selector;
|
241 |
registers[15] = env->segs[R_GS].selector;
|
242 |
/* XXX: convert floats */
|
243 |
for(i = 0; i < 8; i++) { |
244 |
memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
245 |
} |
246 |
registers[36] = env->fpuc;
|
247 |
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
248 |
registers[37] = fpus;
|
249 |
registers[38] = 0; /* XXX: convert tags */ |
250 |
registers[39] = 0; /* fiseg */ |
251 |
registers[40] = 0; /* fioff */ |
252 |
registers[41] = 0; /* foseg */ |
253 |
registers[42] = 0; /* fooff */ |
254 |
registers[43] = 0; /* fop */ |
255 |
|
256 |
for(i = 0; i < 16; i++) |
257 |
tswapls(®isters[i]); |
258 |
for(i = 36; i < 44; i++) |
259 |
tswapls(®isters[i]); |
260 |
return 44 * 4; |
261 |
} |
262 |
|
263 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
264 |
{ |
265 |
uint32_t *registers = (uint32_t *)mem_buf; |
266 |
int i;
|
267 |
|
268 |
for(i = 0; i < 8; i++) { |
269 |
env->regs[i] = tswapl(registers[i]); |
270 |
} |
271 |
env->eip = tswapl(registers[8]);
|
272 |
env->eflags = tswapl(registers[9]);
|
273 |
#if defined(CONFIG_USER_ONLY)
|
274 |
#define LOAD_SEG(index, sreg)\
|
275 |
if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
276 |
cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
277 |
LOAD_SEG(10, R_CS);
|
278 |
LOAD_SEG(11, R_SS);
|
279 |
LOAD_SEG(12, R_DS);
|
280 |
LOAD_SEG(13, R_ES);
|
281 |
LOAD_SEG(14, R_FS);
|
282 |
LOAD_SEG(15, R_GS);
|
283 |
#endif
|
284 |
} |
285 |
|
286 |
#elif defined (TARGET_PPC)
|
287 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
288 |
{ |
289 |
uint32_t *registers = (uint32_t *)mem_buf, tmp; |
290 |
int i;
|
291 |
|
292 |
/* fill in gprs */
|
293 |
for(i = 0; i < 32; i++) { |
294 |
registers[i] = tswapl(env->gpr[i]); |
295 |
} |
296 |
/* fill in fprs */
|
297 |
for (i = 0; i < 32; i++) { |
298 |
registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
299 |
registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
300 |
} |
301 |
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
302 |
registers[96] = tswapl(env->nip);
|
303 |
registers[97] = tswapl(do_load_msr(env));
|
304 |
tmp = 0;
|
305 |
for (i = 0; i < 8; i++) |
306 |
tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
307 |
registers[98] = tswapl(tmp);
|
308 |
registers[99] = tswapl(env->lr);
|
309 |
registers[100] = tswapl(env->ctr);
|
310 |
registers[101] = tswapl(do_load_xer(env));
|
311 |
registers[102] = 0; |
312 |
|
313 |
return 103 * 4; |
314 |
} |
315 |
|
316 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
317 |
{ |
318 |
uint32_t *registers = (uint32_t *)mem_buf; |
319 |
int i;
|
320 |
|
321 |
/* fill in gprs */
|
322 |
for (i = 0; i < 32; i++) { |
323 |
env->gpr[i] = tswapl(registers[i]); |
324 |
} |
325 |
/* fill in fprs */
|
326 |
for (i = 0; i < 32; i++) { |
327 |
*((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
328 |
*((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
329 |
} |
330 |
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
331 |
env->nip = tswapl(registers[96]);
|
332 |
do_store_msr(env, tswapl(registers[97]));
|
333 |
registers[98] = tswapl(registers[98]); |
334 |
for (i = 0; i < 8; i++) |
335 |
env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
336 |
env->lr = tswapl(registers[99]);
|
337 |
env->ctr = tswapl(registers[100]);
|
338 |
do_store_xer(env, tswapl(registers[101]));
|
339 |
} |
340 |
#elif defined (TARGET_SPARC)
|
341 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
342 |
{ |
343 |
target_ulong *registers = (target_ulong *)mem_buf; |
344 |
int i;
|
345 |
|
346 |
/* fill in g0..g7 */
|
347 |
for(i = 0; i < 8; i++) { |
348 |
registers[i] = tswapl(env->gregs[i]); |
349 |
} |
350 |
/* fill in register window */
|
351 |
for(i = 0; i < 24; i++) { |
352 |
registers[i + 8] = tswapl(env->regwptr[i]);
|
353 |
} |
354 |
#ifndef TARGET_SPARC64
|
355 |
/* fill in fprs */
|
356 |
for (i = 0; i < 32; i++) { |
357 |
registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
358 |
} |
359 |
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
360 |
registers[64] = tswapl(env->y);
|
361 |
{ |
362 |
target_ulong tmp; |
363 |
|
364 |
tmp = GET_PSR(env); |
365 |
registers[65] = tswapl(tmp);
|
366 |
} |
367 |
registers[66] = tswapl(env->wim);
|
368 |
registers[67] = tswapl(env->tbr);
|
369 |
registers[68] = tswapl(env->pc);
|
370 |
registers[69] = tswapl(env->npc);
|
371 |
registers[70] = tswapl(env->fsr);
|
372 |
registers[71] = 0; /* csr */ |
373 |
registers[72] = 0; |
374 |
return 73 * sizeof(target_ulong); |
375 |
#else
|
376 |
/* fill in fprs */
|
377 |
for (i = 0; i < 64; i += 2) { |
378 |
uint64_t tmp; |
379 |
|
380 |
tmp = (uint64_t)tswap32(*((uint32_t *)&env->fpr[i])) << 32;
|
381 |
tmp |= tswap32(*((uint32_t *)&env->fpr[i + 1]));
|
382 |
registers[i/2 + 32] = tmp; |
383 |
} |
384 |
registers[64] = tswapl(env->pc);
|
385 |
registers[65] = tswapl(env->npc);
|
386 |
registers[66] = tswapl(env->tstate[env->tl]);
|
387 |
registers[67] = tswapl(env->fsr);
|
388 |
registers[68] = tswapl(env->fprs);
|
389 |
registers[69] = tswapl(env->y);
|
390 |
return 70 * sizeof(target_ulong); |
391 |
#endif
|
392 |
} |
393 |
|
394 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
395 |
{ |
396 |
target_ulong *registers = (target_ulong *)mem_buf; |
397 |
int i;
|
398 |
|
399 |
/* fill in g0..g7 */
|
400 |
for(i = 0; i < 7; i++) { |
401 |
env->gregs[i] = tswapl(registers[i]); |
402 |
} |
403 |
/* fill in register window */
|
404 |
for(i = 0; i < 24; i++) { |
405 |
env->regwptr[i] = tswapl(registers[i + 8]);
|
406 |
} |
407 |
#ifndef TARGET_SPARC64
|
408 |
/* fill in fprs */
|
409 |
for (i = 0; i < 32; i++) { |
410 |
*((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
411 |
} |
412 |
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
413 |
env->y = tswapl(registers[64]);
|
414 |
PUT_PSR(env, tswapl(registers[65]));
|
415 |
env->wim = tswapl(registers[66]);
|
416 |
env->tbr = tswapl(registers[67]);
|
417 |
env->pc = tswapl(registers[68]);
|
418 |
env->npc = tswapl(registers[69]);
|
419 |
env->fsr = tswapl(registers[70]);
|
420 |
#else
|
421 |
for (i = 0; i < 64; i += 2) { |
422 |
*((uint32_t *)&env->fpr[i]) = tswap32(registers[i/2 + 32] >> 32); |
423 |
*((uint32_t *)&env->fpr[i + 1]) = tswap32(registers[i/2 + 32] & 0xffffffff); |
424 |
} |
425 |
env->pc = tswapl(registers[64]);
|
426 |
env->npc = tswapl(registers[65]);
|
427 |
env->tstate[env->tl] = tswapl(registers[66]);
|
428 |
env->fsr = tswapl(registers[67]);
|
429 |
env->fprs = tswapl(registers[68]);
|
430 |
env->y = tswapl(registers[69]);
|
431 |
#endif
|
432 |
} |
433 |
#elif defined (TARGET_ARM)
|
434 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
435 |
{ |
436 |
int i;
|
437 |
uint8_t *ptr; |
438 |
|
439 |
ptr = mem_buf; |
440 |
/* 16 core integer registers (4 bytes each). */
|
441 |
for (i = 0; i < 16; i++) |
442 |
{ |
443 |
*(uint32_t *)ptr = tswapl(env->regs[i]); |
444 |
ptr += 4;
|
445 |
} |
446 |
/* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
447 |
Not yet implemented. */
|
448 |
memset (ptr, 0, 8 * 12 + 4); |
449 |
ptr += 8 * 12 + 4; |
450 |
/* CPSR (4 bytes). */
|
451 |
*(uint32_t *)ptr = tswapl (cpsr_read(env)); |
452 |
ptr += 4;
|
453 |
|
454 |
return ptr - mem_buf;
|
455 |
} |
456 |
|
457 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
458 |
{ |
459 |
int i;
|
460 |
uint8_t *ptr; |
461 |
|
462 |
ptr = mem_buf; |
463 |
/* Core integer registers. */
|
464 |
for (i = 0; i < 16; i++) |
465 |
{ |
466 |
env->regs[i] = tswapl(*(uint32_t *)ptr); |
467 |
ptr += 4;
|
468 |
} |
469 |
/* Ignore FPA regs and scr. */
|
470 |
ptr += 8 * 12 + 4; |
471 |
cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
|
472 |
} |
473 |
#elif defined (TARGET_M68K)
|
474 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
475 |
{ |
476 |
int i;
|
477 |
uint8_t *ptr; |
478 |
CPU_DoubleU u; |
479 |
|
480 |
ptr = mem_buf; |
481 |
/* D0-D7 */
|
482 |
for (i = 0; i < 8; i++) { |
483 |
*(uint32_t *)ptr = tswapl(env->dregs[i]); |
484 |
ptr += 4;
|
485 |
} |
486 |
/* A0-A7 */
|
487 |
for (i = 0; i < 8; i++) { |
488 |
*(uint32_t *)ptr = tswapl(env->aregs[i]); |
489 |
ptr += 4;
|
490 |
} |
491 |
*(uint32_t *)ptr = tswapl(env->sr); |
492 |
ptr += 4;
|
493 |
*(uint32_t *)ptr = tswapl(env->pc); |
494 |
ptr += 4;
|
495 |
/* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
496 |
ColdFire has 8-bit double precision registers. */
|
497 |
for (i = 0; i < 8; i++) { |
498 |
u.d = env->fregs[i]; |
499 |
*(uint32_t *)ptr = tswap32(u.l.upper); |
500 |
*(uint32_t *)ptr = tswap32(u.l.lower); |
501 |
} |
502 |
/* FP control regs (not implemented). */
|
503 |
memset (ptr, 0, 3 * 4); |
504 |
ptr += 3 * 4; |
505 |
|
506 |
return ptr - mem_buf;
|
507 |
} |
508 |
|
509 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
510 |
{ |
511 |
int i;
|
512 |
uint8_t *ptr; |
513 |
CPU_DoubleU u; |
514 |
|
515 |
ptr = mem_buf; |
516 |
/* D0-D7 */
|
517 |
for (i = 0; i < 8; i++) { |
518 |
env->dregs[i] = tswapl(*(uint32_t *)ptr); |
519 |
ptr += 4;
|
520 |
} |
521 |
/* A0-A7 */
|
522 |
for (i = 0; i < 8; i++) { |
523 |
env->aregs[i] = tswapl(*(uint32_t *)ptr); |
524 |
ptr += 4;
|
525 |
} |
526 |
env->sr = tswapl(*(uint32_t *)ptr); |
527 |
ptr += 4;
|
528 |
env->pc = tswapl(*(uint32_t *)ptr); |
529 |
ptr += 4;
|
530 |
/* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
531 |
ColdFire has 8-bit double precision registers. */
|
532 |
for (i = 0; i < 8; i++) { |
533 |
u.l.upper = tswap32(*(uint32_t *)ptr); |
534 |
u.l.lower = tswap32(*(uint32_t *)ptr); |
535 |
env->fregs[i] = u.d; |
536 |
} |
537 |
/* FP control regs (not implemented). */
|
538 |
ptr += 3 * 4; |
539 |
} |
540 |
#elif defined (TARGET_MIPS)
|
541 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
542 |
{ |
543 |
int i;
|
544 |
uint8_t *ptr; |
545 |
|
546 |
ptr = mem_buf; |
547 |
for (i = 0; i < 32; i++) |
548 |
{ |
549 |
*(uint32_t *)ptr = tswapl(env->gpr[i]); |
550 |
ptr += 4;
|
551 |
} |
552 |
|
553 |
*(uint32_t *)ptr = tswapl(env->CP0_Status); |
554 |
ptr += 4;
|
555 |
|
556 |
*(uint32_t *)ptr = tswapl(env->LO); |
557 |
ptr += 4;
|
558 |
|
559 |
*(uint32_t *)ptr = tswapl(env->HI); |
560 |
ptr += 4;
|
561 |
|
562 |
*(uint32_t *)ptr = tswapl(env->CP0_BadVAddr); |
563 |
ptr += 4;
|
564 |
|
565 |
*(uint32_t *)ptr = tswapl(env->CP0_Cause); |
566 |
ptr += 4;
|
567 |
|
568 |
*(uint32_t *)ptr = tswapl(env->PC); |
569 |
ptr += 4;
|
570 |
|
571 |
if (env->CP0_Config1 & (1 << CP0C1_FP)) |
572 |
{ |
573 |
for (i = 0; i < 32; i++) |
574 |
{ |
575 |
*(uint32_t *)ptr = tswapl(FPR_W (env, i)); |
576 |
ptr += 4;
|
577 |
} |
578 |
|
579 |
*(uint32_t *)ptr = tswapl(env->fcr31); |
580 |
ptr += 4;
|
581 |
|
582 |
*(uint32_t *)ptr = tswapl(env->fcr0); |
583 |
ptr += 4;
|
584 |
} |
585 |
|
586 |
/* 32 FP registers, fsr, fir, fp. Not yet implemented. */
|
587 |
/* what's 'fp' mean here? */
|
588 |
|
589 |
return ptr - mem_buf;
|
590 |
} |
591 |
|
592 |
/* convert MIPS rounding mode in FCR31 to IEEE library */
|
593 |
static unsigned int ieee_rm[] = |
594 |
{ |
595 |
float_round_nearest_even, |
596 |
float_round_to_zero, |
597 |
float_round_up, |
598 |
float_round_down |
599 |
}; |
600 |
#define RESTORE_ROUNDING_MODE \
|
601 |
set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
|
602 |
|
603 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
604 |
{ |
605 |
int i;
|
606 |
uint8_t *ptr; |
607 |
|
608 |
ptr = mem_buf; |
609 |
for (i = 0; i < 32; i++) |
610 |
{ |
611 |
env->gpr[i] = tswapl(*(uint32_t *)ptr); |
612 |
ptr += 4;
|
613 |
} |
614 |
|
615 |
env->CP0_Status = tswapl(*(uint32_t *)ptr); |
616 |
ptr += 4;
|
617 |
|
618 |
env->LO = tswapl(*(uint32_t *)ptr); |
619 |
ptr += 4;
|
620 |
|
621 |
env->HI = tswapl(*(uint32_t *)ptr); |
622 |
ptr += 4;
|
623 |
|
624 |
env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr); |
625 |
ptr += 4;
|
626 |
|
627 |
env->CP0_Cause = tswapl(*(uint32_t *)ptr); |
628 |
ptr += 4;
|
629 |
|
630 |
env->PC = tswapl(*(uint32_t *)ptr); |
631 |
ptr += 4;
|
632 |
|
633 |
if (env->CP0_Config1 & (1 << CP0C1_FP)) |
634 |
{ |
635 |
for (i = 0; i < 32; i++) |
636 |
{ |
637 |
FPR_W (env, i) = tswapl(*(uint32_t *)ptr); |
638 |
ptr += 4;
|
639 |
} |
640 |
|
641 |
env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
|
642 |
ptr += 4;
|
643 |
|
644 |
env->fcr0 = tswapl(*(uint32_t *)ptr); |
645 |
ptr += 4;
|
646 |
|
647 |
/* set rounding mode */
|
648 |
RESTORE_ROUNDING_MODE; |
649 |
|
650 |
#ifndef CONFIG_SOFTFLOAT
|
651 |
/* no floating point exception for native float */
|
652 |
SET_FP_ENABLE(env->fcr31, 0);
|
653 |
#endif
|
654 |
} |
655 |
} |
656 |
#elif defined (TARGET_SH4)
|
657 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
658 |
{ |
659 |
uint32_t *ptr = (uint32_t *)mem_buf; |
660 |
int i;
|
661 |
|
662 |
#define SAVE(x) *ptr++=tswapl(x)
|
663 |
if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
664 |
for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]); |
665 |
} else {
|
666 |
for (i = 0; i < 8; i++) SAVE(env->gregs[i]); |
667 |
} |
668 |
for (i = 8; i < 16; i++) SAVE(env->gregs[i]); |
669 |
SAVE (env->pc); |
670 |
SAVE (env->pr); |
671 |
SAVE (env->gbr); |
672 |
SAVE (env->vbr); |
673 |
SAVE (env->mach); |
674 |
SAVE (env->macl); |
675 |
SAVE (env->sr); |
676 |
SAVE (0); /* TICKS */ |
677 |
SAVE (0); /* STALLS */ |
678 |
SAVE (0); /* CYCLES */ |
679 |
SAVE (0); /* INSTS */ |
680 |
SAVE (0); /* PLR */ |
681 |
|
682 |
return ((uint8_t *)ptr - mem_buf);
|
683 |
} |
684 |
|
685 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
686 |
{ |
687 |
uint32_t *ptr = (uint32_t *)mem_buf; |
688 |
int i;
|
689 |
|
690 |
#define LOAD(x) (x)=*ptr++;
|
691 |
if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
692 |
for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]); |
693 |
} else {
|
694 |
for (i = 0; i < 8; i++) LOAD(env->gregs[i]); |
695 |
} |
696 |
for (i = 8; i < 16; i++) LOAD(env->gregs[i]); |
697 |
LOAD (env->pc); |
698 |
LOAD (env->pr); |
699 |
LOAD (env->gbr); |
700 |
LOAD (env->vbr); |
701 |
LOAD (env->mach); |
702 |
LOAD (env->macl); |
703 |
LOAD (env->sr); |
704 |
} |
705 |
#else
|
706 |
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
707 |
{ |
708 |
return 0; |
709 |
} |
710 |
|
711 |
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
712 |
{ |
713 |
} |
714 |
|
715 |
#endif
|
716 |
|
717 |
static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
718 |
{ |
719 |
const char *p; |
720 |
int ch, reg_size, type;
|
721 |
char buf[4096]; |
722 |
uint8_t mem_buf[2000];
|
723 |
uint32_t *registers; |
724 |
target_ulong addr, len; |
725 |
|
726 |
#ifdef DEBUG_GDB
|
727 |
printf("command='%s'\n", line_buf);
|
728 |
#endif
|
729 |
p = line_buf; |
730 |
ch = *p++; |
731 |
switch(ch) {
|
732 |
case '?': |
733 |
/* TODO: Make this return the correct value for user-mode. */
|
734 |
snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
735 |
put_packet(s, buf); |
736 |
break;
|
737 |
case 'c': |
738 |
if (*p != '\0') { |
739 |
addr = strtoull(p, (char **)&p, 16); |
740 |
#if defined(TARGET_I386)
|
741 |
env->eip = addr; |
742 |
#elif defined (TARGET_PPC)
|
743 |
env->nip = addr; |
744 |
#elif defined (TARGET_SPARC)
|
745 |
env->pc = addr; |
746 |
env->npc = addr + 4;
|
747 |
#elif defined (TARGET_ARM)
|
748 |
env->regs[15] = addr;
|
749 |
#elif defined (TARGET_SH4)
|
750 |
env->pc = addr; |
751 |
#endif
|
752 |
} |
753 |
#ifdef CONFIG_USER_ONLY
|
754 |
s->running_state = 1;
|
755 |
#else
|
756 |
vm_start(); |
757 |
#endif
|
758 |
return RS_IDLE;
|
759 |
case 's': |
760 |
if (*p != '\0') { |
761 |
addr = strtoul(p, (char **)&p, 16); |
762 |
#if defined(TARGET_I386)
|
763 |
env->eip = addr; |
764 |
#elif defined (TARGET_PPC)
|
765 |
env->nip = addr; |
766 |
#elif defined (TARGET_SPARC)
|
767 |
env->pc = addr; |
768 |
env->npc = addr + 4;
|
769 |
#elif defined (TARGET_ARM)
|
770 |
env->regs[15] = addr;
|
771 |
#elif defined (TARGET_SH4)
|
772 |
env->pc = addr; |
773 |
#endif
|
774 |
} |
775 |
cpu_single_step(env, 1);
|
776 |
#ifdef CONFIG_USER_ONLY
|
777 |
s->running_state = 1;
|
778 |
#else
|
779 |
vm_start(); |
780 |
#endif
|
781 |
return RS_IDLE;
|
782 |
case 'F': |
783 |
{ |
784 |
target_ulong ret; |
785 |
target_ulong err; |
786 |
|
787 |
ret = strtoull(p, (char **)&p, 16); |
788 |
if (*p == ',') { |
789 |
p++; |
790 |
err = strtoull(p, (char **)&p, 16); |
791 |
} else {
|
792 |
err = 0;
|
793 |
} |
794 |
if (*p == ',') |
795 |
p++; |
796 |
type = *p; |
797 |
if (gdb_current_syscall_cb)
|
798 |
gdb_current_syscall_cb(s->env, ret, err); |
799 |
if (type == 'C') { |
800 |
put_packet(s, "T02");
|
801 |
} else {
|
802 |
#ifdef CONFIG_USER_ONLY
|
803 |
s->running_state = 1;
|
804 |
#else
|
805 |
vm_start(); |
806 |
#endif
|
807 |
} |
808 |
} |
809 |
break;
|
810 |
case 'g': |
811 |
reg_size = cpu_gdb_read_registers(env, mem_buf); |
812 |
memtohex(buf, mem_buf, reg_size); |
813 |
put_packet(s, buf); |
814 |
break;
|
815 |
case 'G': |
816 |
registers = (void *)mem_buf;
|
817 |
len = strlen(p) / 2;
|
818 |
hextomem((uint8_t *)registers, p, len); |
819 |
cpu_gdb_write_registers(env, mem_buf, len); |
820 |
put_packet(s, "OK");
|
821 |
break;
|
822 |
case 'm': |
823 |
addr = strtoull(p, (char **)&p, 16); |
824 |
if (*p == ',') |
825 |
p++; |
826 |
len = strtoull(p, NULL, 16); |
827 |
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) { |
828 |
put_packet (s, "E14");
|
829 |
} else {
|
830 |
memtohex(buf, mem_buf, len); |
831 |
put_packet(s, buf); |
832 |
} |
833 |
break;
|
834 |
case 'M': |
835 |
addr = strtoull(p, (char **)&p, 16); |
836 |
if (*p == ',') |
837 |
p++; |
838 |
len = strtoull(p, (char **)&p, 16); |
839 |
if (*p == ':') |
840 |
p++; |
841 |
hextomem(mem_buf, p, len); |
842 |
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
843 |
put_packet(s, "E14");
|
844 |
else
|
845 |
put_packet(s, "OK");
|
846 |
break;
|
847 |
case 'Z': |
848 |
type = strtoul(p, (char **)&p, 16); |
849 |
if (*p == ',') |
850 |
p++; |
851 |
addr = strtoull(p, (char **)&p, 16); |
852 |
if (*p == ',') |
853 |
p++; |
854 |
len = strtoull(p, (char **)&p, 16); |
855 |
if (type == 0 || type == 1) { |
856 |
if (cpu_breakpoint_insert(env, addr) < 0) |
857 |
goto breakpoint_error;
|
858 |
put_packet(s, "OK");
|
859 |
} else {
|
860 |
breakpoint_error:
|
861 |
put_packet(s, "E22");
|
862 |
} |
863 |
break;
|
864 |
case 'z': |
865 |
type = strtoul(p, (char **)&p, 16); |
866 |
if (*p == ',') |
867 |
p++; |
868 |
addr = strtoull(p, (char **)&p, 16); |
869 |
if (*p == ',') |
870 |
p++; |
871 |
len = strtoull(p, (char **)&p, 16); |
872 |
if (type == 0 || type == 1) { |
873 |
cpu_breakpoint_remove(env, addr); |
874 |
put_packet(s, "OK");
|
875 |
} else {
|
876 |
goto breakpoint_error;
|
877 |
} |
878 |
break;
|
879 |
#ifdef CONFIG_LINUX_USER
|
880 |
case 'q': |
881 |
if (strncmp(p, "Offsets", 7) == 0) { |
882 |
TaskState *ts = env->opaque; |
883 |
|
884 |
sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
|
885 |
ts->info->data_offset, ts->info->data_offset); |
886 |
put_packet(s, buf); |
887 |
break;
|
888 |
} |
889 |
/* Fall through. */
|
890 |
#endif
|
891 |
default:
|
892 |
// unknown_command:
|
893 |
/* put empty packet */
|
894 |
buf[0] = '\0'; |
895 |
put_packet(s, buf); |
896 |
break;
|
897 |
} |
898 |
return RS_IDLE;
|
899 |
} |
900 |
|
901 |
extern void tb_flush(CPUState *env); |
902 |
|
903 |
#ifndef CONFIG_USER_ONLY
|
904 |
static void gdb_vm_stopped(void *opaque, int reason) |
905 |
{ |
906 |
GDBState *s = opaque; |
907 |
char buf[256]; |
908 |
int ret;
|
909 |
|
910 |
if (s->state == RS_SYSCALL)
|
911 |
return;
|
912 |
|
913 |
/* disable single step if it was enable */
|
914 |
cpu_single_step(s->env, 0);
|
915 |
|
916 |
if (reason == EXCP_DEBUG) {
|
917 |
tb_flush(s->env); |
918 |
ret = SIGTRAP; |
919 |
} else if (reason == EXCP_INTERRUPT) { |
920 |
ret = SIGINT; |
921 |
} else {
|
922 |
ret = 0;
|
923 |
} |
924 |
snprintf(buf, sizeof(buf), "S%02x", ret); |
925 |
put_packet(s, buf); |
926 |
} |
927 |
#endif
|
928 |
|
929 |
/* Send a gdb syscall request.
|
930 |
This accepts limited printf-style format specifiers, specifically:
|
931 |
%x - target_ulong argument printed in hex.
|
932 |
%s - string pointer (target_ulong) and length (int) pair. */
|
933 |
void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...) |
934 |
{ |
935 |
va_list va; |
936 |
char buf[256]; |
937 |
char *p;
|
938 |
target_ulong addr; |
939 |
GDBState *s; |
940 |
|
941 |
s = gdb_syscall_state; |
942 |
if (!s)
|
943 |
return;
|
944 |
gdb_current_syscall_cb = cb; |
945 |
s->state = RS_SYSCALL; |
946 |
#ifndef CONFIG_USER_ONLY
|
947 |
vm_stop(EXCP_DEBUG); |
948 |
#endif
|
949 |
s->state = RS_IDLE; |
950 |
va_start(va, fmt); |
951 |
p = buf; |
952 |
*(p++) = 'F';
|
953 |
while (*fmt) {
|
954 |
if (*fmt == '%') { |
955 |
fmt++; |
956 |
switch (*fmt++) {
|
957 |
case 'x': |
958 |
addr = va_arg(va, target_ulong); |
959 |
p += sprintf(p, TARGET_FMT_lx, addr); |
960 |
break;
|
961 |
case 's': |
962 |
addr = va_arg(va, target_ulong); |
963 |
p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int)); |
964 |
break;
|
965 |
default:
|
966 |
fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
|
967 |
fmt - 1);
|
968 |
break;
|
969 |
} |
970 |
} else {
|
971 |
*(p++) = *(fmt++); |
972 |
} |
973 |
} |
974 |
va_end(va); |
975 |
put_packet(s, buf); |
976 |
#ifdef CONFIG_USER_ONLY
|
977 |
gdb_handlesig(s->env, 0);
|
978 |
#else
|
979 |
cpu_interrupt(s->env, CPU_INTERRUPT_EXIT); |
980 |
#endif
|
981 |
} |
982 |
|
983 |
static void gdb_read_byte(GDBState *s, int ch) |
984 |
{ |
985 |
CPUState *env = s->env; |
986 |
int i, csum;
|
987 |
char reply[1]; |
988 |
|
989 |
#ifndef CONFIG_USER_ONLY
|
990 |
if (s->last_packet_len) {
|
991 |
/* Waiting for a response to the last packet. If we see the start
|
992 |
of a new command then abandon the previous response. */
|
993 |
if (ch == '-') { |
994 |
#ifdef DEBUG_GDB
|
995 |
printf("Got NACK, retransmitting\n");
|
996 |
#endif
|
997 |
put_buffer(s, s->last_packet, s->last_packet_len); |
998 |
} |
999 |
#ifdef DEBUG_GDB
|
1000 |
else if (ch == '+') |
1001 |
printf("Got ACK\n");
|
1002 |
else
|
1003 |
printf("Got '%c' when expecting ACK/NACK\n", ch);
|
1004 |
#endif
|
1005 |
if (ch == '+' || ch == '$') |
1006 |
s->last_packet_len = 0;
|
1007 |
if (ch != '$') |
1008 |
return;
|
1009 |
} |
1010 |
if (vm_running) {
|
1011 |
/* when the CPU is running, we cannot do anything except stop
|
1012 |
it when receiving a char */
|
1013 |
vm_stop(EXCP_INTERRUPT); |
1014 |
} else
|
1015 |
#endif
|
1016 |
{ |
1017 |
switch(s->state) {
|
1018 |
case RS_IDLE:
|
1019 |
if (ch == '$') { |
1020 |
s->line_buf_index = 0;
|
1021 |
s->state = RS_GETLINE; |
1022 |
} |
1023 |
break;
|
1024 |
case RS_GETLINE:
|
1025 |
if (ch == '#') { |
1026 |
s->state = RS_CHKSUM1; |
1027 |
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
1028 |
s->state = RS_IDLE; |
1029 |
} else {
|
1030 |
s->line_buf[s->line_buf_index++] = ch; |
1031 |
} |
1032 |
break;
|
1033 |
case RS_CHKSUM1:
|
1034 |
s->line_buf[s->line_buf_index] = '\0';
|
1035 |
s->line_csum = fromhex(ch) << 4;
|
1036 |
s->state = RS_CHKSUM2; |
1037 |
break;
|
1038 |
case RS_CHKSUM2:
|
1039 |
s->line_csum |= fromhex(ch); |
1040 |
csum = 0;
|
1041 |
for(i = 0; i < s->line_buf_index; i++) { |
1042 |
csum += s->line_buf[i]; |
1043 |
} |
1044 |
if (s->line_csum != (csum & 0xff)) { |
1045 |
reply[0] = '-'; |
1046 |
put_buffer(s, reply, 1);
|
1047 |
s->state = RS_IDLE; |
1048 |
} else {
|
1049 |
reply[0] = '+'; |
1050 |
put_buffer(s, reply, 1);
|
1051 |
s->state = gdb_handle_packet(s, env, s->line_buf); |
1052 |
} |
1053 |
break;
|
1054 |
default:
|
1055 |
abort(); |
1056 |
} |
1057 |
} |
1058 |
} |
1059 |
|
1060 |
#ifdef CONFIG_USER_ONLY
|
1061 |
int
|
1062 |
gdb_handlesig (CPUState *env, int sig)
|
1063 |
{ |
1064 |
GDBState *s; |
1065 |
char buf[256]; |
1066 |
int n;
|
1067 |
|
1068 |
if (gdbserver_fd < 0) |
1069 |
return sig;
|
1070 |
|
1071 |
s = &gdbserver_state; |
1072 |
|
1073 |
/* disable single step if it was enabled */
|
1074 |
cpu_single_step(env, 0);
|
1075 |
tb_flush(env); |
1076 |
|
1077 |
if (sig != 0) |
1078 |
{ |
1079 |
snprintf(buf, sizeof(buf), "S%02x", sig); |
1080 |
put_packet(s, buf); |
1081 |
} |
1082 |
|
1083 |
sig = 0;
|
1084 |
s->state = RS_IDLE; |
1085 |
s->running_state = 0;
|
1086 |
while (s->running_state == 0) { |
1087 |
n = read (s->fd, buf, 256);
|
1088 |
if (n > 0) |
1089 |
{ |
1090 |
int i;
|
1091 |
|
1092 |
for (i = 0; i < n; i++) |
1093 |
gdb_read_byte (s, buf[i]); |
1094 |
} |
1095 |
else if (n == 0 || errno != EAGAIN) |
1096 |
{ |
1097 |
/* XXX: Connection closed. Should probably wait for annother
|
1098 |
connection before continuing. */
|
1099 |
return sig;
|
1100 |
} |
1101 |
} |
1102 |
return sig;
|
1103 |
} |
1104 |
|
1105 |
/* Tell the remote gdb that the process has exited. */
|
1106 |
void gdb_exit(CPUState *env, int code) |
1107 |
{ |
1108 |
GDBState *s; |
1109 |
char buf[4]; |
1110 |
|
1111 |
if (gdbserver_fd < 0) |
1112 |
return;
|
1113 |
|
1114 |
s = &gdbserver_state; |
1115 |
|
1116 |
snprintf(buf, sizeof(buf), "W%02x", code); |
1117 |
put_packet(s, buf); |
1118 |
} |
1119 |
|
1120 |
|
1121 |
static void gdb_accept(void *opaque) |
1122 |
{ |
1123 |
GDBState *s; |
1124 |
struct sockaddr_in sockaddr;
|
1125 |
socklen_t len; |
1126 |
int val, fd;
|
1127 |
|
1128 |
for(;;) {
|
1129 |
len = sizeof(sockaddr);
|
1130 |
fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
1131 |
if (fd < 0 && errno != EINTR) { |
1132 |
perror("accept");
|
1133 |
return;
|
1134 |
} else if (fd >= 0) { |
1135 |
break;
|
1136 |
} |
1137 |
} |
1138 |
|
1139 |
/* set short latency */
|
1140 |
val = 1;
|
1141 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
1142 |
|
1143 |
s = &gdbserver_state; |
1144 |
memset (s, 0, sizeof (GDBState)); |
1145 |
s->env = first_cpu; /* XXX: allow to change CPU */
|
1146 |
s->fd = fd; |
1147 |
|
1148 |
gdb_syscall_state = s; |
1149 |
|
1150 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
1151 |
} |
1152 |
|
1153 |
static int gdbserver_open(int port) |
1154 |
{ |
1155 |
struct sockaddr_in sockaddr;
|
1156 |
int fd, val, ret;
|
1157 |
|
1158 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
1159 |
if (fd < 0) { |
1160 |
perror("socket");
|
1161 |
return -1; |
1162 |
} |
1163 |
|
1164 |
/* allow fast reuse */
|
1165 |
val = 1;
|
1166 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
1167 |
|
1168 |
sockaddr.sin_family = AF_INET; |
1169 |
sockaddr.sin_port = htons(port); |
1170 |
sockaddr.sin_addr.s_addr = 0;
|
1171 |
ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
1172 |
if (ret < 0) { |
1173 |
perror("bind");
|
1174 |
return -1; |
1175 |
} |
1176 |
ret = listen(fd, 0);
|
1177 |
if (ret < 0) { |
1178 |
perror("listen");
|
1179 |
return -1; |
1180 |
} |
1181 |
return fd;
|
1182 |
} |
1183 |
|
1184 |
int gdbserver_start(int port) |
1185 |
{ |
1186 |
gdbserver_fd = gdbserver_open(port); |
1187 |
if (gdbserver_fd < 0) |
1188 |
return -1; |
1189 |
/* accept connections */
|
1190 |
gdb_accept (NULL);
|
1191 |
return 0; |
1192 |
} |
1193 |
#else
|
1194 |
static int gdb_chr_can_recieve(void *opaque) |
1195 |
{ |
1196 |
return 1; |
1197 |
} |
1198 |
|
1199 |
static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size) |
1200 |
{ |
1201 |
GDBState *s = opaque; |
1202 |
int i;
|
1203 |
|
1204 |
for (i = 0; i < size; i++) { |
1205 |
gdb_read_byte(s, buf[i]); |
1206 |
} |
1207 |
} |
1208 |
|
1209 |
static void gdb_chr_event(void *opaque, int event) |
1210 |
{ |
1211 |
switch (event) {
|
1212 |
case CHR_EVENT_RESET:
|
1213 |
vm_stop(EXCP_INTERRUPT); |
1214 |
gdb_syscall_state = opaque; |
1215 |
break;
|
1216 |
default:
|
1217 |
break;
|
1218 |
} |
1219 |
} |
1220 |
|
1221 |
int gdbserver_start(const char *port) |
1222 |
{ |
1223 |
GDBState *s; |
1224 |
char gdbstub_port_name[128]; |
1225 |
int port_num;
|
1226 |
char *p;
|
1227 |
CharDriverState *chr; |
1228 |
|
1229 |
if (!port || !*port)
|
1230 |
return -1; |
1231 |
|
1232 |
port_num = strtol(port, &p, 10);
|
1233 |
if (*p == 0) { |
1234 |
/* A numeric value is interpreted as a port number. */
|
1235 |
snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
|
1236 |
"tcp::%d,nowait,nodelay,server", port_num);
|
1237 |
port = gdbstub_port_name; |
1238 |
} |
1239 |
|
1240 |
chr = qemu_chr_open(port); |
1241 |
if (!chr)
|
1242 |
return -1; |
1243 |
|
1244 |
s = qemu_mallocz(sizeof(GDBState));
|
1245 |
if (!s) {
|
1246 |
return -1; |
1247 |
} |
1248 |
s->env = first_cpu; /* XXX: allow to change CPU */
|
1249 |
s->chr = chr; |
1250 |
qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve, |
1251 |
gdb_chr_event, s); |
1252 |
qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
1253 |
return 0; |
1254 |
} |
1255 |
#endif
|