2003-06-27 19:34:32 +02:00
|
|
|
/*
|
|
|
|
* gdb server stub
|
|
|
|
*
|
2005-07-02 16:31:34 +02:00
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
2003-06-27 19:34:32 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "qemu.h"
|
|
|
|
#else
|
2004-04-01 01:37:16 +02:00
|
|
|
#include "vl.h"
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2004-04-01 01:37:16 +02:00
|
|
|
|
2003-06-27 19:34:32 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2003-07-26 20:01:58 +02:00
|
|
|
//#define DEBUG_GDB
|
2003-06-27 19:34:32 +02:00
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
enum RSState {
|
|
|
|
RS_IDLE,
|
|
|
|
RS_GETLINE,
|
|
|
|
RS_CHKSUM1,
|
|
|
|
RS_CHKSUM2,
|
|
|
|
};
|
2005-04-17 21:16:13 +02:00
|
|
|
/* XXX: This is not thread safe. Do we care? */
|
|
|
|
static int gdbserver_fd = -1;
|
2003-06-27 19:34:32 +02:00
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
typedef struct GDBState {
|
2005-04-24 12:07:11 +02:00
|
|
|
enum RSState state; /* parsing state */
|
2004-03-31 20:52:07 +02:00
|
|
|
int fd;
|
|
|
|
char line_buf[4096];
|
|
|
|
int line_buf_index;
|
|
|
|
int line_csum;
|
2005-04-24 12:07:11 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
int running_state;
|
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
} GDBState;
|
2003-06-27 19:34:32 +02:00
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
/* XXX: remove this hack. */
|
|
|
|
static GDBState gdbserver_state;
|
|
|
|
#endif
|
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
static int get_char(GDBState *s)
|
2003-06-27 19:34:32 +02:00
|
|
|
{
|
|
|
|
uint8_t ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for(;;) {
|
2004-03-31 20:52:07 +02:00
|
|
|
ret = read(s->fd, &ch, 1);
|
2003-06-27 19:34:32 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
static void put_buffer(GDBState *s, const uint8_t *buf, int len)
|
2003-06-27 19:34:32 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (len > 0) {
|
2004-03-31 20:52:07 +02:00
|
|
|
ret = write(s->fd, buf, len);
|
2003-06-27 19:34:32 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buf += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fromhex(int v)
|
|
|
|
{
|
|
|
|
if (v >= '0' && v <= '9')
|
|
|
|
return v - '0';
|
|
|
|
else if (v >= 'A' && v <= 'F')
|
|
|
|
return v - 'A' + 10;
|
|
|
|
else if (v >= 'a' && v <= 'f')
|
|
|
|
return v - 'a' + 10;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int tohex(int v)
|
|
|
|
{
|
|
|
|
if (v < 10)
|
|
|
|
return v + '0';
|
|
|
|
else
|
|
|
|
return v - 10 + 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void memtohex(char *buf, const uint8_t *mem, int len)
|
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
char *q;
|
|
|
|
q = buf;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
c = mem[i];
|
|
|
|
*q++ = tohex(c >> 4);
|
|
|
|
*q++ = tohex(c & 0xf);
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hextomem(uint8_t *mem, const char *buf, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
|
|
|
|
buf += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return -1 if error, 0 if OK */
|
2004-03-31 20:52:07 +02:00
|
|
|
static int put_packet(GDBState *s, char *buf)
|
2003-06-27 19:34:32 +02:00
|
|
|
{
|
|
|
|
char buf1[3];
|
|
|
|
int len, csum, ch, i;
|
|
|
|
|
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printf("reply='%s'\n", buf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
buf1[0] = '$';
|
2004-03-31 20:52:07 +02:00
|
|
|
put_buffer(s, buf1, 1);
|
2003-06-27 19:34:32 +02:00
|
|
|
len = strlen(buf);
|
2004-03-31 20:52:07 +02:00
|
|
|
put_buffer(s, buf, len);
|
2003-06-27 19:34:32 +02:00
|
|
|
csum = 0;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
csum += buf[i];
|
|
|
|
}
|
|
|
|
buf1[0] = '#';
|
|
|
|
buf1[1] = tohex((csum >> 4) & 0xf);
|
|
|
|
buf1[2] = tohex((csum) & 0xf);
|
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
put_buffer(s, buf1, 3);
|
2003-06-27 19:34:32 +02:00
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
ch = get_char(s);
|
2003-06-27 19:34:32 +02:00
|
|
|
if (ch < 0)
|
|
|
|
return -1;
|
|
|
|
if (ch == '+')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-04 16:48:38 +01:00
|
|
|
#if defined(TARGET_I386)
|
|
|
|
|
|
|
|
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
|
|
|
|
{
|
2004-10-01 00:22:08 +02:00
|
|
|
uint32_t *registers = (uint32_t *)mem_buf;
|
2004-01-04 16:48:38 +01:00
|
|
|
int i, fpus;
|
|
|
|
|
|
|
|
for(i = 0; i < 8; i++) {
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[i] = env->regs[i];
|
2004-01-04 16:48:38 +01:00
|
|
|
}
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[8] = env->eip;
|
|
|
|
registers[9] = env->eflags;
|
|
|
|
registers[10] = env->segs[R_CS].selector;
|
|
|
|
registers[11] = env->segs[R_SS].selector;
|
|
|
|
registers[12] = env->segs[R_DS].selector;
|
|
|
|
registers[13] = env->segs[R_ES].selector;
|
|
|
|
registers[14] = env->segs[R_FS].selector;
|
|
|
|
registers[15] = env->segs[R_GS].selector;
|
2004-01-04 16:48:38 +01:00
|
|
|
/* XXX: convert floats */
|
|
|
|
for(i = 0; i < 8; i++) {
|
|
|
|
memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
|
|
|
|
}
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[36] = env->fpuc;
|
2004-01-04 16:48:38 +01:00
|
|
|
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[37] = fpus;
|
|
|
|
registers[38] = 0; /* XXX: convert tags */
|
|
|
|
registers[39] = 0; /* fiseg */
|
|
|
|
registers[40] = 0; /* fioff */
|
|
|
|
registers[41] = 0; /* foseg */
|
|
|
|
registers[42] = 0; /* fooff */
|
|
|
|
registers[43] = 0; /* fop */
|
|
|
|
|
|
|
|
for(i = 0; i < 16; i++)
|
|
|
|
tswapls(®isters[i]);
|
|
|
|
for(i = 36; i < 44; i++)
|
|
|
|
tswapls(®isters[i]);
|
2004-01-04 16:48:38 +01:00
|
|
|
return 44 * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
|
|
|
|
{
|
|
|
|
uint32_t *registers = (uint32_t *)mem_buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < 8; i++) {
|
|
|
|
env->regs[i] = tswapl(registers[i]);
|
|
|
|
}
|
2004-10-01 00:22:08 +02:00
|
|
|
env->eip = tswapl(registers[8]);
|
|
|
|
env->eflags = tswapl(registers[9]);
|
2004-01-04 16:48:38 +01:00
|
|
|
#if defined(CONFIG_USER_ONLY)
|
|
|
|
#define LOAD_SEG(index, sreg)\
|
|
|
|
if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
|
|
|
cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
|
|
|
|
LOAD_SEG(10, R_CS);
|
|
|
|
LOAD_SEG(11, R_SS);
|
|
|
|
LOAD_SEG(12, R_DS);
|
|
|
|
LOAD_SEG(13, R_ES);
|
|
|
|
LOAD_SEG(14, R_FS);
|
|
|
|
LOAD_SEG(15, R_GS);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-01-05 23:49:06 +01:00
|
|
|
#elif defined (TARGET_PPC)
|
|
|
|
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
|
|
|
|
{
|
2004-04-12 22:39:29 +02:00
|
|
|
uint32_t *registers = (uint32_t *)mem_buf, tmp;
|
2004-01-05 23:49:06 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* fill in gprs */
|
2004-04-12 22:39:29 +02:00
|
|
|
for(i = 0; i < 32; i++) {
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[i] = tswapl(env->gpr[i]);
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
|
|
|
/* fill in fprs */
|
|
|
|
for (i = 0; i < 32; i++) {
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
|
|
|
registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
|
|
|
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[96] = tswapl(env->nip);
|
|
|
|
registers[97] = tswapl(_load_msr(env));
|
2004-01-05 23:49:06 +01:00
|
|
|
tmp = 0;
|
|
|
|
for (i = 0; i < 8; i++)
|
2004-04-12 22:39:29 +02:00
|
|
|
tmp |= env->crf[i] << (32 - ((i + 1) * 4));
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[98] = tswapl(tmp);
|
|
|
|
registers[99] = tswapl(env->lr);
|
|
|
|
registers[100] = tswapl(env->ctr);
|
|
|
|
registers[101] = tswapl(_load_xer(env));
|
|
|
|
registers[102] = 0;
|
2004-04-12 22:39:29 +02:00
|
|
|
|
|
|
|
return 103 * 4;
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
|
|
|
|
{
|
|
|
|
uint32_t *registers = (uint32_t *)mem_buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* fill in gprs */
|
|
|
|
for (i = 0; i < 32; i++) {
|
2004-10-01 00:22:08 +02:00
|
|
|
env->gpr[i] = tswapl(registers[i]);
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
|
|
|
/* fill in fprs */
|
|
|
|
for (i = 0; i < 32; i++) {
|
2004-10-01 00:22:08 +02:00
|
|
|
*((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
|
|
|
|
*((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
|
|
|
/* nip, msr, ccr, lnk, ctr, xer, mq */
|
2004-10-01 00:22:08 +02:00
|
|
|
env->nip = tswapl(registers[96]);
|
|
|
|
_store_msr(env, tswapl(registers[97]));
|
|
|
|
registers[98] = tswapl(registers[98]);
|
2004-01-05 23:49:06 +01:00
|
|
|
for (i = 0; i < 8; i++)
|
2004-04-12 22:39:29 +02:00
|
|
|
env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
|
2004-10-01 00:22:08 +02:00
|
|
|
env->lr = tswapl(registers[99]);
|
|
|
|
env->ctr = tswapl(registers[100]);
|
|
|
|
_store_xer(env, tswapl(registers[101]));
|
|
|
|
}
|
|
|
|
#elif defined (TARGET_SPARC)
|
|
|
|
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
|
|
|
|
{
|
2005-07-02 16:31:34 +02:00
|
|
|
target_ulong *registers = (target_ulong *)mem_buf;
|
2004-10-01 00:22:08 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* fill in g0..g7 */
|
|
|
|
for(i = 0; i < 7; i++) {
|
|
|
|
registers[i] = tswapl(env->gregs[i]);
|
|
|
|
}
|
|
|
|
/* fill in register window */
|
|
|
|
for(i = 0; i < 24; i++) {
|
|
|
|
registers[i + 8] = tswapl(env->regwptr[i]);
|
|
|
|
}
|
|
|
|
/* fill in fprs */
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
|
|
|
}
|
2005-07-02 16:31:34 +02:00
|
|
|
#ifndef TARGET_SPARC64
|
2004-10-01 00:22:08 +02:00
|
|
|
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
|
|
|
registers[64] = tswapl(env->y);
|
2005-07-02 16:31:34 +02:00
|
|
|
{
|
|
|
|
target_ulong tmp;
|
|
|
|
|
|
|
|
tmp = GET_PSR(env);
|
|
|
|
registers[65] = tswapl(tmp);
|
|
|
|
}
|
2004-10-01 00:22:08 +02:00
|
|
|
registers[66] = tswapl(env->wim);
|
|
|
|
registers[67] = tswapl(env->tbr);
|
|
|
|
registers[68] = tswapl(env->pc);
|
|
|
|
registers[69] = tswapl(env->npc);
|
|
|
|
registers[70] = tswapl(env->fsr);
|
|
|
|
registers[71] = 0; /* csr */
|
|
|
|
registers[72] = 0;
|
2005-07-02 16:31:34 +02:00
|
|
|
return 73 * sizeof(target_ulong);
|
|
|
|
#else
|
|
|
|
for (i = 0; i < 32; i += 2) {
|
|
|
|
registers[i/2 + 64] = tswapl(*((uint64_t *)&env->fpr[i]));
|
|
|
|
}
|
|
|
|
registers[81] = tswapl(env->pc);
|
|
|
|
registers[82] = tswapl(env->npc);
|
|
|
|
registers[83] = tswapl(env->tstate[env->tl]);
|
|
|
|
registers[84] = tswapl(env->fsr);
|
|
|
|
registers[85] = tswapl(env->fprs);
|
|
|
|
registers[86] = tswapl(env->y);
|
|
|
|
return 87 * sizeof(target_ulong);
|
|
|
|
#endif
|
2004-10-01 00:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
|
|
|
|
{
|
2005-07-02 16:31:34 +02:00
|
|
|
target_ulong *registers = (target_ulong *)mem_buf;
|
2004-10-01 00:22:08 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* fill in g0..g7 */
|
|
|
|
for(i = 0; i < 7; i++) {
|
|
|
|
env->gregs[i] = tswapl(registers[i]);
|
|
|
|
}
|
|
|
|
/* fill in register window */
|
|
|
|
for(i = 0; i < 24; i++) {
|
2005-07-02 16:31:34 +02:00
|
|
|
env->regwptr[i] = tswapl(registers[i + 8]);
|
2004-10-01 00:22:08 +02:00
|
|
|
}
|
|
|
|
/* fill in fprs */
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
*((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
|
|
|
}
|
2005-07-02 16:31:34 +02:00
|
|
|
#ifndef TARGET_SPARC64
|
2004-10-01 00:22:08 +02:00
|
|
|
/* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
|
|
|
env->y = tswapl(registers[64]);
|
2004-12-20 00:18:01 +01:00
|
|
|
PUT_PSR(env, tswapl(registers[65]));
|
2004-10-01 00:22:08 +02:00
|
|
|
env->wim = tswapl(registers[66]);
|
|
|
|
env->tbr = tswapl(registers[67]);
|
|
|
|
env->pc = tswapl(registers[68]);
|
|
|
|
env->npc = tswapl(registers[69]);
|
|
|
|
env->fsr = tswapl(registers[70]);
|
2005-07-02 16:31:34 +02:00
|
|
|
#else
|
|
|
|
for (i = 0; i < 32; i += 2) {
|
|
|
|
uint64_t tmp;
|
|
|
|
tmp = tswapl(registers[i/2 + 64]) << 32;
|
|
|
|
tmp |= tswapl(registers[i/2 + 64 + 1]);
|
|
|
|
*((uint64_t *)&env->fpr[i]) = tmp;
|
|
|
|
}
|
|
|
|
env->pc = tswapl(registers[81]);
|
|
|
|
env->npc = tswapl(registers[82]);
|
|
|
|
env->tstate[env->tl] = tswapl(registers[83]);
|
|
|
|
env->fsr = tswapl(registers[84]);
|
|
|
|
env->fprs = tswapl(registers[85]);
|
|
|
|
env->y = tswapl(registers[86]);
|
|
|
|
#endif
|
2004-01-05 23:49:06 +01:00
|
|
|
}
|
2005-04-17 21:16:13 +02:00
|
|
|
#elif defined (TARGET_ARM)
|
|
|
|
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
ptr = mem_buf;
|
|
|
|
/* 16 core integer registers (4 bytes each). */
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
*(uint32_t *)ptr = tswapl(env->regs[i]);
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
/* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
|
|
|
Not yet implemented. */
|
|
|
|
memset (ptr, 0, 8 * 12 + 4);
|
|
|
|
ptr += 8 * 12 + 4;
|
|
|
|
/* CPSR (4 bytes). */
|
|
|
|
*(uint32_t *)ptr = tswapl (env->cpsr);
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
return ptr - mem_buf;
|
|
|
|
}
|
2004-01-04 16:48:38 +01:00
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
ptr = mem_buf;
|
|
|
|
/* Core integer registers. */
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
env->regs[i] = tswapl(*(uint32_t *)ptr);
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
/* Ignore FPA regs and scr. */
|
|
|
|
ptr += 8 * 12 + 4;
|
|
|
|
env->cpsr = tswapl(*(uint32_t *)ptr);
|
|
|
|
}
|
|
|
|
#else
|
2004-01-04 16:48:38 +01:00
|
|
|
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2003-06-27 19:34:32 +02:00
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
|
2003-06-27 19:34:32 +02:00
|
|
|
{
|
|
|
|
const char *p;
|
2004-03-31 20:52:07 +02:00
|
|
|
int ch, reg_size, type;
|
2003-06-27 19:34:32 +02:00
|
|
|
char buf[4096];
|
|
|
|
uint8_t mem_buf[2000];
|
|
|
|
uint32_t *registers;
|
|
|
|
uint32_t addr, len;
|
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printf("command='%s'\n", line_buf);
|
|
|
|
#endif
|
|
|
|
p = line_buf;
|
|
|
|
ch = *p++;
|
|
|
|
switch(ch) {
|
|
|
|
case '?':
|
2005-04-17 21:16:13 +02:00
|
|
|
/* TODO: Make this return the correct value for user-mode. */
|
2004-03-31 20:52:07 +02:00
|
|
|
snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
|
|
|
|
put_packet(s, buf);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (*p != '\0') {
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
2003-07-26 14:06:08 +02:00
|
|
|
#if defined(TARGET_I386)
|
2004-03-31 20:52:07 +02:00
|
|
|
env->eip = addr;
|
2004-01-05 00:51:58 +01:00
|
|
|
#elif defined (TARGET_PPC)
|
2004-03-31 20:52:07 +02:00
|
|
|
env->nip = addr;
|
2004-10-04 23:23:09 +02:00
|
|
|
#elif defined (TARGET_SPARC)
|
|
|
|
env->pc = addr;
|
|
|
|
env->npc = addr + 4;
|
2003-07-26 14:06:08 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
2005-04-24 12:07:11 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
s->running_state = 1;
|
|
|
|
#else
|
|
|
|
vm_start();
|
|
|
|
#endif
|
|
|
|
return RS_IDLE;
|
2004-03-31 20:52:07 +02:00
|
|
|
case 's':
|
|
|
|
if (*p != '\0') {
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
2003-07-29 22:50:33 +02:00
|
|
|
#if defined(TARGET_I386)
|
2004-03-31 20:52:07 +02:00
|
|
|
env->eip = addr;
|
2004-01-05 00:51:58 +01:00
|
|
|
#elif defined (TARGET_PPC)
|
2004-03-31 20:52:07 +02:00
|
|
|
env->nip = addr;
|
2004-10-04 23:23:09 +02:00
|
|
|
#elif defined (TARGET_SPARC)
|
|
|
|
env->pc = addr;
|
|
|
|
env->npc = addr + 4;
|
2003-07-29 22:50:33 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
|
|
|
cpu_single_step(env, 1);
|
2005-04-24 12:07:11 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
s->running_state = 1;
|
|
|
|
#else
|
|
|
|
vm_start();
|
|
|
|
#endif
|
|
|
|
return RS_IDLE;
|
2004-03-31 20:52:07 +02:00
|
|
|
case 'g':
|
|
|
|
reg_size = cpu_gdb_read_registers(env, mem_buf);
|
|
|
|
memtohex(buf, mem_buf, reg_size);
|
|
|
|
put_packet(s, buf);
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
registers = (void *)mem_buf;
|
|
|
|
len = strlen(p) / 2;
|
|
|
|
hextomem((uint8_t *)registers, p, len);
|
|
|
|
cpu_gdb_write_registers(env, mem_buf, len);
|
|
|
|
put_packet(s, "OK");
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
len = strtoul(p, NULL, 16);
|
|
|
|
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0)
|
|
|
|
memset(mem_buf, 0, len);
|
|
|
|
memtohex(buf, mem_buf, len);
|
|
|
|
put_packet(s, buf);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
len = strtoul(p, (char **)&p, 16);
|
2005-01-17 23:03:16 +01:00
|
|
|
if (*p == ':')
|
2004-03-31 20:52:07 +02:00
|
|
|
p++;
|
|
|
|
hextomem(mem_buf, p, len);
|
|
|
|
if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
|
2005-04-26 23:09:55 +02:00
|
|
|
put_packet(s, "E14");
|
2004-03-31 20:52:07 +02:00
|
|
|
else
|
|
|
|
put_packet(s, "OK");
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
type = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
len = strtoul(p, (char **)&p, 16);
|
|
|
|
if (type == 0 || type == 1) {
|
|
|
|
if (cpu_breakpoint_insert(env, addr) < 0)
|
|
|
|
goto breakpoint_error;
|
|
|
|
put_packet(s, "OK");
|
|
|
|
} else {
|
|
|
|
breakpoint_error:
|
2005-04-26 23:09:55 +02:00
|
|
|
put_packet(s, "E22");
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
type = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
addr = strtoul(p, (char **)&p, 16);
|
|
|
|
if (*p == ',')
|
|
|
|
p++;
|
|
|
|
len = strtoul(p, (char **)&p, 16);
|
|
|
|
if (type == 0 || type == 1) {
|
|
|
|
cpu_breakpoint_remove(env, addr);
|
|
|
|
put_packet(s, "OK");
|
|
|
|
} else {
|
|
|
|
goto breakpoint_error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// unknown_command:
|
|
|
|
/* put empty packet */
|
|
|
|
buf[0] = '\0';
|
|
|
|
put_packet(s, buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return RS_IDLE;
|
|
|
|
}
|
|
|
|
|
2005-01-04 00:34:06 +01:00
|
|
|
extern void tb_flush(CPUState *env);
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2004-03-31 20:52:07 +02:00
|
|
|
static void gdb_vm_stopped(void *opaque, int reason)
|
|
|
|
{
|
|
|
|
GDBState *s = opaque;
|
|
|
|
char buf[256];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* disable single step if it was enable */
|
|
|
|
cpu_single_step(cpu_single_env, 0);
|
|
|
|
|
2004-12-20 00:18:01 +01:00
|
|
|
if (reason == EXCP_DEBUG) {
|
|
|
|
tb_flush(cpu_single_env);
|
2004-03-31 20:52:07 +02:00
|
|
|
ret = SIGTRAP;
|
2004-12-20 00:18:01 +01:00
|
|
|
}
|
2004-03-31 20:52:07 +02:00
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
snprintf(buf, sizeof(buf), "S%02x", ret);
|
|
|
|
put_packet(s, buf);
|
|
|
|
}
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
static void gdb_read_byte(GDBState *s, CPUState *env, int ch)
|
2004-03-31 20:52:07 +02:00
|
|
|
{
|
|
|
|
int i, csum;
|
|
|
|
char reply[1];
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2004-03-31 20:52:07 +02:00
|
|
|
if (vm_running) {
|
|
|
|
/* when the CPU is running, we cannot do anything except stop
|
|
|
|
it when receiving a char */
|
|
|
|
vm_stop(EXCP_INTERRUPT);
|
2005-04-24 12:07:11 +02:00
|
|
|
} else
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2005-04-24 12:07:11 +02:00
|
|
|
{
|
2004-03-31 20:52:07 +02:00
|
|
|
switch(s->state) {
|
|
|
|
case RS_IDLE:
|
|
|
|
if (ch == '$') {
|
|
|
|
s->line_buf_index = 0;
|
|
|
|
s->state = RS_GETLINE;
|
2003-07-29 22:50:33 +02:00
|
|
|
}
|
2003-06-27 19:34:32 +02:00
|
|
|
break;
|
2004-03-31 20:52:07 +02:00
|
|
|
case RS_GETLINE:
|
|
|
|
if (ch == '#') {
|
|
|
|
s->state = RS_CHKSUM1;
|
|
|
|
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
|
|
|
|
s->state = RS_IDLE;
|
2003-07-26 14:06:08 +02:00
|
|
|
} else {
|
2004-03-31 20:52:07 +02:00
|
|
|
s->line_buf[s->line_buf_index++] = ch;
|
2003-07-26 14:06:08 +02:00
|
|
|
}
|
|
|
|
break;
|
2004-03-31 20:52:07 +02:00
|
|
|
case RS_CHKSUM1:
|
|
|
|
s->line_buf[s->line_buf_index] = '\0';
|
|
|
|
s->line_csum = fromhex(ch) << 4;
|
|
|
|
s->state = RS_CHKSUM2;
|
|
|
|
break;
|
|
|
|
case RS_CHKSUM2:
|
|
|
|
s->line_csum |= fromhex(ch);
|
|
|
|
csum = 0;
|
|
|
|
for(i = 0; i < s->line_buf_index; i++) {
|
|
|
|
csum += s->line_buf[i];
|
|
|
|
}
|
|
|
|
if (s->line_csum != (csum & 0xff)) {
|
|
|
|
reply[0] = '-';
|
|
|
|
put_buffer(s, reply, 1);
|
|
|
|
s->state = RS_IDLE;
|
2003-07-26 14:06:08 +02:00
|
|
|
} else {
|
2004-03-31 20:52:07 +02:00
|
|
|
reply[0] = '+';
|
|
|
|
put_buffer(s, reply, 1);
|
2005-04-17 21:16:13 +02:00
|
|
|
s->state = gdb_handle_packet(s, env, s->line_buf);
|
2003-07-26 14:06:08 +02:00
|
|
|
}
|
|
|
|
break;
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
int
|
|
|
|
gdb_handlesig (CPUState *env, int sig)
|
|
|
|
{
|
|
|
|
GDBState *s;
|
|
|
|
char buf[256];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (gdbserver_fd < 0)
|
|
|
|
return sig;
|
|
|
|
|
|
|
|
s = &gdbserver_state;
|
|
|
|
|
|
|
|
/* disable single step if it was enabled */
|
|
|
|
cpu_single_step(env, 0);
|
|
|
|
tb_flush(env);
|
|
|
|
|
|
|
|
if (sig != 0)
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "S%02x", sig);
|
|
|
|
put_packet(s, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
sig = 0;
|
|
|
|
s->state = RS_IDLE;
|
2005-04-24 12:07:11 +02:00
|
|
|
s->running_state = 0;
|
|
|
|
while (s->running_state == 0) {
|
2005-04-17 21:16:13 +02:00
|
|
|
n = read (s->fd, buf, 256);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
gdb_read_byte (s, env, buf[i]);
|
|
|
|
}
|
|
|
|
else if (n == 0 || errno != EAGAIN)
|
|
|
|
{
|
|
|
|
/* XXX: Connection closed. Should probably wait for annother
|
|
|
|
connection before continuing. */
|
|
|
|
return sig;
|
|
|
|
}
|
2005-04-24 12:07:11 +02:00
|
|
|
}
|
2005-04-17 21:16:13 +02:00
|
|
|
return sig;
|
|
|
|
}
|
2005-04-26 22:42:36 +02:00
|
|
|
|
|
|
|
/* Tell the remote gdb that the process has exited. */
|
|
|
|
void gdb_exit(CPUState *env, int code)
|
|
|
|
{
|
|
|
|
GDBState *s;
|
|
|
|
char buf[4];
|
|
|
|
|
|
|
|
if (gdbserver_fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = &gdbserver_state;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "W%02x", code);
|
|
|
|
put_packet(s, buf);
|
|
|
|
}
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#else
|
2004-03-31 20:52:07 +02:00
|
|
|
static int gdb_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
return 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gdb_read(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
GDBState *s = opaque;
|
|
|
|
int i;
|
|
|
|
if (size == 0) {
|
|
|
|
/* end of connection */
|
|
|
|
qemu_del_vm_stop_handler(gdb_vm_stopped, s);
|
|
|
|
qemu_del_fd_read_handler(s->fd);
|
|
|
|
qemu_free(s);
|
|
|
|
vm_start();
|
|
|
|
} else {
|
|
|
|
for(i = 0; i < size; i++)
|
2005-04-17 21:16:13 +02:00
|
|
|
gdb_read_byte(s, cpu_single_env, buf[i]);
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
|
|
|
|
2004-03-31 20:52:07 +02:00
|
|
|
static void gdb_accept(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
GDBState *s;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
socklen_t len;
|
|
|
|
int val, fd;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
len = sizeof(sockaddr);
|
|
|
|
fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
|
|
|
if (fd < 0 && errno != EINTR) {
|
|
|
|
perror("accept");
|
|
|
|
return;
|
|
|
|
} else if (fd >= 0) {
|
2003-06-27 19:34:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-03-31 20:52:07 +02:00
|
|
|
|
|
|
|
/* set short latency */
|
|
|
|
val = 1;
|
2004-05-12 21:32:15 +02:00
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
|
2004-03-31 20:52:07 +02:00
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
s = &gdbserver_state;
|
|
|
|
memset (s, 0, sizeof (GDBState));
|
|
|
|
#else
|
2004-03-31 20:52:07 +02:00
|
|
|
s = qemu_mallocz(sizeof(GDBState));
|
|
|
|
if (!s) {
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
s->fd = fd;
|
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2004-03-31 20:52:07 +02:00
|
|
|
/* stop the VM */
|
|
|
|
vm_stop(EXCP_INTERRUPT);
|
|
|
|
|
|
|
|
/* start handling I/O */
|
|
|
|
qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s);
|
|
|
|
/* when the VM is stopped, the following callback is called */
|
|
|
|
qemu_add_vm_stop_handler(gdb_vm_stopped, s);
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gdbserver_open(int port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
int fd, val, ret;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allow fast reuse */
|
|
|
|
val = 1;
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
|
|
|
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_port = htons(port);
|
|
|
|
sockaddr.sin_addr.s_addr = 0;
|
|
|
|
ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("bind");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = listen(fd, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("listen");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2004-03-31 20:52:07 +02:00
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2004-03-31 20:52:07 +02:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gdbserver_start(int port)
|
|
|
|
{
|
|
|
|
gdbserver_fd = gdbserver_open(port);
|
|
|
|
if (gdbserver_fd < 0)
|
|
|
|
return -1;
|
|
|
|
/* accept connections */
|
2005-04-17 21:16:13 +02:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
gdb_accept (NULL, NULL, 0);
|
|
|
|
#else
|
2004-03-31 20:52:07 +02:00
|
|
|
qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL);
|
2005-04-17 21:16:13 +02:00
|
|
|
#endif
|
2003-06-27 19:34:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|