blob: 2ec2f33f8dcf913c2c1a17a2f8ce67aeb4df9e5d [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
3 *
bellard34751872005-07-02 14:31:34 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00005 *
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 */
pbrook978efd62006-06-17 18:30:42 +000020#include "config.h"
bellard1fddef42005-04-17 19:16:13 +000021#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>
pbrook978efd62006-06-17 18:30:42 +000028#include <fcntl.h>
bellard1fddef42005-04-17 19:16:13 +000029
30#include "qemu.h"
31#else
bellard67b915a2004-03-31 23:37:16 +000032#include "vl.h"
bellard1fddef42005-04-17 19:16:13 +000033#endif
bellard67b915a2004-03-31 23:37:16 +000034
bellard8f447cc2006-06-14 15:21:14 +000035#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
bellardb4608c02003-06-27 17:34:32 +000045#include <signal.h>
bellard8f447cc2006-06-14 15:21:14 +000046#endif
bellardb4608c02003-06-27 17:34:32 +000047
bellard4abe6152003-07-26 18:01:58 +000048//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +000049
bellard858693c2004-03-31 18:52:07 +000050enum RSState {
51 RS_IDLE,
52 RS_GETLINE,
53 RS_CHKSUM1,
54 RS_CHKSUM2,
pbrooka2d1eba2007-01-28 03:10:55 +000055 RS_SYSCALL,
bellard858693c2004-03-31 18:52:07 +000056};
bellard858693c2004-03-31 18:52:07 +000057typedef struct GDBState {
bellard6a00d602005-11-21 23:25:50 +000058 CPUState *env; /* current CPU */
bellard41625032005-04-24 10:07:11 +000059 enum RSState state; /* parsing state */
bellard858693c2004-03-31 18:52:07 +000060 char line_buf[4096];
61 int line_buf_index;
62 int line_csum;
pbrook4046d912007-01-28 01:53:16 +000063 char last_packet[4100];
64 int last_packet_len;
bellard41625032005-04-24 10:07:11 +000065#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +000066 int fd;
bellard41625032005-04-24 10:07:11 +000067 int running_state;
pbrook4046d912007-01-28 01:53:16 +000068#else
69 CharDriverState *chr;
bellard41625032005-04-24 10:07:11 +000070#endif
bellard858693c2004-03-31 18:52:07 +000071} GDBState;
bellardb4608c02003-06-27 17:34:32 +000072
bellard1fddef42005-04-17 19:16:13 +000073#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +000074/* XXX: This is not thread safe. Do we care? */
75static int gdbserver_fd = -1;
76
bellard1fddef42005-04-17 19:16:13 +000077/* XXX: remove this hack. */
78static GDBState gdbserver_state;
bellard1fddef42005-04-17 19:16:13 +000079
bellard858693c2004-03-31 18:52:07 +000080static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +000081{
82 uint8_t ch;
83 int ret;
84
85 for(;;) {
bellard8f447cc2006-06-14 15:21:14 +000086 ret = recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +000087 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}
pbrook4046d912007-01-28 01:53:16 +000098#endif
bellardb4608c02003-06-27 17:34:32 +000099
pbrooka2d1eba2007-01-28 03:10:55 +0000100/* GDB stub state for use by semihosting syscalls. */
101static GDBState *gdb_syscall_state;
102static gdb_syscall_complete_cb gdb_current_syscall_cb;
103
104enum {
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. */
112int 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
bellard858693c2004-03-31 18:52:07 +0000121static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000122{
pbrook4046d912007-01-28 01:53:16 +0000123#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000124 int ret;
125
126 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000127 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000128 if (ret < 0) {
129 if (errno != EINTR && errno != EAGAIN)
130 return;
131 } else {
132 buf += ret;
133 len -= ret;
134 }
135 }
pbrook4046d912007-01-28 01:53:16 +0000136#else
137 qemu_chr_write(s->chr, buf, len);
138#endif
bellardb4608c02003-06-27 17:34:32 +0000139}
140
141static 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
153static inline int tohex(int v)
154{
155 if (v < 10)
156 return v + '0';
157 else
158 return v - 10 + 'a';
159}
160
161static 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
174static 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
bellardb4608c02003-06-27 17:34:32 +0000184/* return -1 if error, 0 if OK */
bellard858693c2004-03-31 18:52:07 +0000185static int put_packet(GDBState *s, char *buf)
bellardb4608c02003-06-27 17:34:32 +0000186{
pbrook4046d912007-01-28 01:53:16 +0000187 int len, csum, i;
188 char *p;
bellardb4608c02003-06-27 17:34:32 +0000189
190#ifdef DEBUG_GDB
191 printf("reply='%s'\n", buf);
192#endif
193
194 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000195 p = s->last_packet;
196 *(p++) = '$';
bellardb4608c02003-06-27 17:34:32 +0000197 len = strlen(buf);
pbrook4046d912007-01-28 01:53:16 +0000198 memcpy(p, buf, len);
199 p += len;
bellardb4608c02003-06-27 17:34:32 +0000200 csum = 0;
201 for(i = 0; i < len; i++) {
202 csum += buf[i];
203 }
pbrook4046d912007-01-28 01:53:16 +0000204 *(p++) = '#';
205 *(p++) = tohex((csum >> 4) & 0xf);
206 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000207
pbrook4046d912007-01-28 01:53:16 +0000208 s->last_packet_len = p - s->last_packet;
209 put_buffer(s, s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000210
pbrook4046d912007-01-28 01:53:16 +0000211#ifdef CONFIG_USER_ONLY
212 i = get_char(s);
213 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000214 return -1;
pbrook4046d912007-01-28 01:53:16 +0000215 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000216 break;
pbrook4046d912007-01-28 01:53:16 +0000217#else
218 break;
219#endif
bellardb4608c02003-06-27 17:34:32 +0000220 }
221 return 0;
222}
223
bellard6da41ea2004-01-04 15:48:38 +0000224#if defined(TARGET_I386)
225
bellard6da41ea2004-01-04 15:48:38 +0000226static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
227{
bellarde95c8d52004-09-30 22:22:08 +0000228 uint32_t *registers = (uint32_t *)mem_buf;
bellard6da41ea2004-01-04 15:48:38 +0000229 int i, fpus;
230
231 for(i = 0; i < 8; i++) {
bellarde95c8d52004-09-30 22:22:08 +0000232 registers[i] = env->regs[i];
bellard6da41ea2004-01-04 15:48:38 +0000233 }
bellarde95c8d52004-09-30 22:22:08 +0000234 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;
bellard6da41ea2004-01-04 15:48:38 +0000242 /* XXX: convert floats */
243 for(i = 0; i < 8; i++) {
244 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
245 }
bellarde95c8d52004-09-30 22:22:08 +0000246 registers[36] = env->fpuc;
bellard6da41ea2004-01-04 15:48:38 +0000247 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
bellarde95c8d52004-09-30 22:22:08 +0000248 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(&registers[i]);
258 for(i = 36; i < 44; i++)
259 tswapls(&registers[i]);
bellard6da41ea2004-01-04 15:48:38 +0000260 return 44 * 4;
261}
262
263static 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 }
bellarde95c8d52004-09-30 22:22:08 +0000271 env->eip = tswapl(registers[8]);
272 env->eflags = tswapl(registers[9]);
bellard6da41ea2004-01-04 15:48:38 +0000273#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
bellard9e62fd72004-01-05 22:49:06 +0000286#elif defined (TARGET_PPC)
bellard9e62fd72004-01-05 22:49:06 +0000287static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
288{
bellarda541f292004-04-12 20:39:29 +0000289 uint32_t *registers = (uint32_t *)mem_buf, tmp;
bellard9e62fd72004-01-05 22:49:06 +0000290 int i;
291
292 /* fill in gprs */
bellarda541f292004-04-12 20:39:29 +0000293 for(i = 0; i < 32; i++) {
bellarde95c8d52004-09-30 22:22:08 +0000294 registers[i] = tswapl(env->gpr[i]);
bellard9e62fd72004-01-05 22:49:06 +0000295 }
296 /* fill in fprs */
297 for (i = 0; i < 32; i++) {
bellarde95c8d52004-09-30 22:22:08 +0000298 registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
299 registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
bellard9e62fd72004-01-05 22:49:06 +0000300 }
301 /* nip, msr, ccr, lnk, ctr, xer, mq */
bellarde95c8d52004-09-30 22:22:08 +0000302 registers[96] = tswapl(env->nip);
bellard3fc6c082005-07-02 20:59:34 +0000303 registers[97] = tswapl(do_load_msr(env));
bellard9e62fd72004-01-05 22:49:06 +0000304 tmp = 0;
305 for (i = 0; i < 8; i++)
bellarda541f292004-04-12 20:39:29 +0000306 tmp |= env->crf[i] << (32 - ((i + 1) * 4));
bellarde95c8d52004-09-30 22:22:08 +0000307 registers[98] = tswapl(tmp);
308 registers[99] = tswapl(env->lr);
309 registers[100] = tswapl(env->ctr);
j_mayer76a66252007-03-07 08:32:30 +0000310 registers[101] = tswapl(ppc_load_xer(env));
bellarde95c8d52004-09-30 22:22:08 +0000311 registers[102] = 0;
bellard9e62fd72004-01-05 22:49:06 +0000312
bellarda541f292004-04-12 20:39:29 +0000313 return 103 * 4;
bellard9e62fd72004-01-05 22:49:06 +0000314}
315
316static 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++) {
bellarde95c8d52004-09-30 22:22:08 +0000323 env->gpr[i] = tswapl(registers[i]);
bellard9e62fd72004-01-05 22:49:06 +0000324 }
325 /* fill in fprs */
326 for (i = 0; i < 32; i++) {
bellarde95c8d52004-09-30 22:22:08 +0000327 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
328 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
bellard9e62fd72004-01-05 22:49:06 +0000329 }
330 /* nip, msr, ccr, lnk, ctr, xer, mq */
bellarde95c8d52004-09-30 22:22:08 +0000331 env->nip = tswapl(registers[96]);
bellard3fc6c082005-07-02 20:59:34 +0000332 do_store_msr(env, tswapl(registers[97]));
bellarde95c8d52004-09-30 22:22:08 +0000333 registers[98] = tswapl(registers[98]);
bellard9e62fd72004-01-05 22:49:06 +0000334 for (i = 0; i < 8; i++)
bellarda541f292004-04-12 20:39:29 +0000335 env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
bellarde95c8d52004-09-30 22:22:08 +0000336 env->lr = tswapl(registers[99]);
337 env->ctr = tswapl(registers[100]);
j_mayer76a66252007-03-07 08:32:30 +0000338 ppc_store_xer(env, tswapl(registers[101]));
bellarde95c8d52004-09-30 22:22:08 +0000339}
340#elif defined (TARGET_SPARC)
341static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
342{
bellard34751872005-07-02 14:31:34 +0000343 target_ulong *registers = (target_ulong *)mem_buf;
bellarde95c8d52004-09-30 22:22:08 +0000344 int i;
345
346 /* fill in g0..g7 */
bellard48b2c192005-10-30 16:08:23 +0000347 for(i = 0; i < 8; i++) {
bellarde95c8d52004-09-30 22:22:08 +0000348 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 }
bellard9d9754a2006-06-25 15:32:37 +0000354#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +0000355 /* 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);
bellard34751872005-07-02 14:31:34 +0000361 {
362 target_ulong tmp;
363
364 tmp = GET_PSR(env);
365 registers[65] = tswapl(tmp);
366 }
bellarde95c8d52004-09-30 22:22:08 +0000367 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;
bellard34751872005-07-02 14:31:34 +0000374 return 73 * sizeof(target_ulong);
375#else
bellard9d9754a2006-06-25 15:32:37 +0000376 /* fill in fprs */
377 for (i = 0; i < 64; i += 2) {
378 uint64_t tmp;
379
blueswir189795962007-04-14 16:15:48 +0000380 tmp = ((uint64_t)*(uint32_t *)&env->fpr[i]) << 32;
381 tmp |= *(uint32_t *)&env->fpr[i + 1];
382 registers[i / 2 + 32] = tswap64(tmp);
bellard34751872005-07-02 14:31:34 +0000383 }
bellard9d9754a2006-06-25 15:32:37 +0000384 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);
bellard34751872005-07-02 14:31:34 +0000391#endif
bellarde95c8d52004-09-30 22:22:08 +0000392}
393
394static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
395{
bellard34751872005-07-02 14:31:34 +0000396 target_ulong *registers = (target_ulong *)mem_buf;
bellarde95c8d52004-09-30 22:22:08 +0000397 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++) {
bellard34751872005-07-02 14:31:34 +0000405 env->regwptr[i] = tswapl(registers[i + 8]);
bellarde95c8d52004-09-30 22:22:08 +0000406 }
bellard9d9754a2006-06-25 15:32:37 +0000407#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +0000408 /* 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]);
bellarde80cfcf2004-12-19 23:18:01 +0000414 PUT_PSR(env, tswapl(registers[65]));
bellarde95c8d52004-09-30 22:22:08 +0000415 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]);
bellard34751872005-07-02 14:31:34 +0000420#else
bellard9d9754a2006-06-25 15:32:37 +0000421 for (i = 0; i < 64; i += 2) {
blueswir189795962007-04-14 16:15:48 +0000422 uint64_t tmp;
423
424 tmp = tswap64(registers[i / 2 + 32]);
425 *((uint32_t *)&env->fpr[i]) = tmp >> 32;
426 *((uint32_t *)&env->fpr[i + 1]) = tmp & 0xffffffff;
bellard34751872005-07-02 14:31:34 +0000427 }
bellard9d9754a2006-06-25 15:32:37 +0000428 env->pc = tswapl(registers[64]);
429 env->npc = tswapl(registers[65]);
430 env->tstate[env->tl] = tswapl(registers[66]);
431 env->fsr = tswapl(registers[67]);
432 env->fprs = tswapl(registers[68]);
433 env->y = tswapl(registers[69]);
bellard34751872005-07-02 14:31:34 +0000434#endif
bellard9e62fd72004-01-05 22:49:06 +0000435}
bellard1fddef42005-04-17 19:16:13 +0000436#elif defined (TARGET_ARM)
437static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
438{
439 int i;
440 uint8_t *ptr;
bellard6da41ea2004-01-04 15:48:38 +0000441
bellard1fddef42005-04-17 19:16:13 +0000442 ptr = mem_buf;
443 /* 16 core integer registers (4 bytes each). */
444 for (i = 0; i < 16; i++)
445 {
446 *(uint32_t *)ptr = tswapl(env->regs[i]);
447 ptr += 4;
448 }
449 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
450 Not yet implemented. */
451 memset (ptr, 0, 8 * 12 + 4);
452 ptr += 8 * 12 + 4;
453 /* CPSR (4 bytes). */
bellardb5ff1b32005-11-26 10:38:39 +0000454 *(uint32_t *)ptr = tswapl (cpsr_read(env));
bellard1fddef42005-04-17 19:16:13 +0000455 ptr += 4;
456
457 return ptr - mem_buf;
458}
459
460static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
461{
462 int i;
463 uint8_t *ptr;
464
465 ptr = mem_buf;
466 /* Core integer registers. */
467 for (i = 0; i < 16; i++)
468 {
469 env->regs[i] = tswapl(*(uint32_t *)ptr);
470 ptr += 4;
471 }
472 /* Ignore FPA regs and scr. */
473 ptr += 8 * 12 + 4;
bellardb5ff1b32005-11-26 10:38:39 +0000474 cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
bellard1fddef42005-04-17 19:16:13 +0000475}
pbrooke6e59062006-10-22 00:18:54 +0000476#elif defined (TARGET_M68K)
477static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
478{
479 int i;
480 uint8_t *ptr;
481 CPU_DoubleU u;
482
483 ptr = mem_buf;
484 /* D0-D7 */
485 for (i = 0; i < 8; i++) {
486 *(uint32_t *)ptr = tswapl(env->dregs[i]);
487 ptr += 4;
488 }
489 /* A0-A7 */
490 for (i = 0; i < 8; i++) {
491 *(uint32_t *)ptr = tswapl(env->aregs[i]);
492 ptr += 4;
493 }
494 *(uint32_t *)ptr = tswapl(env->sr);
495 ptr += 4;
496 *(uint32_t *)ptr = tswapl(env->pc);
497 ptr += 4;
498 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
499 ColdFire has 8-bit double precision registers. */
500 for (i = 0; i < 8; i++) {
501 u.d = env->fregs[i];
502 *(uint32_t *)ptr = tswap32(u.l.upper);
503 *(uint32_t *)ptr = tswap32(u.l.lower);
504 }
505 /* FP control regs (not implemented). */
506 memset (ptr, 0, 3 * 4);
507 ptr += 3 * 4;
508
509 return ptr - mem_buf;
510}
511
512static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
513{
514 int i;
515 uint8_t *ptr;
516 CPU_DoubleU u;
517
518 ptr = mem_buf;
519 /* D0-D7 */
520 for (i = 0; i < 8; i++) {
521 env->dregs[i] = tswapl(*(uint32_t *)ptr);
522 ptr += 4;
523 }
524 /* A0-A7 */
525 for (i = 0; i < 8; i++) {
526 env->aregs[i] = tswapl(*(uint32_t *)ptr);
527 ptr += 4;
528 }
529 env->sr = tswapl(*(uint32_t *)ptr);
530 ptr += 4;
531 env->pc = tswapl(*(uint32_t *)ptr);
532 ptr += 4;
533 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
534 ColdFire has 8-bit double precision registers. */
535 for (i = 0; i < 8; i++) {
536 u.l.upper = tswap32(*(uint32_t *)ptr);
537 u.l.lower = tswap32(*(uint32_t *)ptr);
538 env->fregs[i] = u.d;
539 }
540 /* FP control regs (not implemented). */
541 ptr += 3 * 4;
542}
bellard6f970bd2005-12-05 19:55:19 +0000543#elif defined (TARGET_MIPS)
544static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
545{
546 int i;
547 uint8_t *ptr;
548
549 ptr = mem_buf;
550 for (i = 0; i < 32; i++)
551 {
ths2052caa2007-06-01 13:18:19 +0000552 *(target_ulong *)ptr = tswapl(env->gpr[i]);
553 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000554 }
555
ths2052caa2007-06-01 13:18:19 +0000556 *(target_ulong *)ptr = tswapl(env->CP0_Status);
557 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000558
ths2052caa2007-06-01 13:18:19 +0000559 *(target_ulong *)ptr = tswapl(env->LO);
560 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000561
ths2052caa2007-06-01 13:18:19 +0000562 *(target_ulong *)ptr = tswapl(env->HI);
563 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000564
ths2052caa2007-06-01 13:18:19 +0000565 *(target_ulong *)ptr = tswapl(env->CP0_BadVAddr);
566 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000567
ths2052caa2007-06-01 13:18:19 +0000568 *(target_ulong *)ptr = tswapl(env->CP0_Cause);
569 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000570
ths2052caa2007-06-01 13:18:19 +0000571 *(target_ulong *)ptr = tswapl(env->PC);
572 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000573
ths36d23952007-02-28 22:37:42 +0000574 if (env->CP0_Config1 & (1 << CP0C1_FP))
ths8e33c082006-12-11 19:22:27 +0000575 {
ths36d23952007-02-28 22:37:42 +0000576 for (i = 0; i < 32; i++)
577 {
ths2052caa2007-06-01 13:18:19 +0000578 *(target_ulong *)ptr = tswapl(env->fpr[i].fs[FP_ENDIAN_IDX]);
579 ptr += sizeof(target_ulong);
ths36d23952007-02-28 22:37:42 +0000580 }
581
ths2052caa2007-06-01 13:18:19 +0000582 *(target_ulong *)ptr = tswapl(env->fcr31);
583 ptr += sizeof(target_ulong);
ths36d23952007-02-28 22:37:42 +0000584
ths2052caa2007-06-01 13:18:19 +0000585 *(target_ulong *)ptr = tswapl(env->fcr0);
586 ptr += sizeof(target_ulong);
ths8e33c082006-12-11 19:22:27 +0000587 }
588
bellard6f970bd2005-12-05 19:55:19 +0000589 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
ths8e33c082006-12-11 19:22:27 +0000590 /* what's 'fp' mean here? */
bellard6f970bd2005-12-05 19:55:19 +0000591
592 return ptr - mem_buf;
593}
594
ths8e33c082006-12-11 19:22:27 +0000595/* convert MIPS rounding mode in FCR31 to IEEE library */
596static unsigned int ieee_rm[] =
597 {
598 float_round_nearest_even,
599 float_round_to_zero,
600 float_round_up,
601 float_round_down
602 };
603#define RESTORE_ROUNDING_MODE \
604 set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
605
bellard6f970bd2005-12-05 19:55:19 +0000606static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
607{
608 int i;
609 uint8_t *ptr;
610
611 ptr = mem_buf;
612 for (i = 0; i < 32; i++)
613 {
ths2052caa2007-06-01 13:18:19 +0000614 env->gpr[i] = tswapl(*(target_ulong *)ptr);
615 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000616 }
617
ths2052caa2007-06-01 13:18:19 +0000618 env->CP0_Status = tswapl(*(target_ulong *)ptr);
619 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000620
ths2052caa2007-06-01 13:18:19 +0000621 env->LO = tswapl(*(target_ulong *)ptr);
622 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000623
ths2052caa2007-06-01 13:18:19 +0000624 env->HI = tswapl(*(target_ulong *)ptr);
625 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000626
ths2052caa2007-06-01 13:18:19 +0000627 env->CP0_BadVAddr = tswapl(*(target_ulong *)ptr);
628 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000629
ths2052caa2007-06-01 13:18:19 +0000630 env->CP0_Cause = tswapl(*(target_ulong *)ptr);
631 ptr += sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +0000632
ths2052caa2007-06-01 13:18:19 +0000633 env->PC = tswapl(*(target_ulong *)ptr);
634 ptr += sizeof(target_ulong);
ths8e33c082006-12-11 19:22:27 +0000635
ths36d23952007-02-28 22:37:42 +0000636 if (env->CP0_Config1 & (1 << CP0C1_FP))
ths8e33c082006-12-11 19:22:27 +0000637 {
ths36d23952007-02-28 22:37:42 +0000638 for (i = 0; i < 32; i++)
639 {
ths2052caa2007-06-01 13:18:19 +0000640 env->fpr[i].fs[FP_ENDIAN_IDX] = tswapl(*(target_ulong *)ptr);
641 ptr += sizeof(target_ulong);
ths36d23952007-02-28 22:37:42 +0000642 }
643
ths2052caa2007-06-01 13:18:19 +0000644 env->fcr31 = tswapl(*(target_ulong *)ptr) & 0x0183FFFF;
645 ptr += sizeof(target_ulong);
ths8e33c082006-12-11 19:22:27 +0000646
ths2052caa2007-06-01 13:18:19 +0000647 env->fcr0 = tswapl(*(target_ulong *)ptr);
648 ptr += sizeof(target_ulong);
ths8e33c082006-12-11 19:22:27 +0000649
ths36d23952007-02-28 22:37:42 +0000650 /* set rounding mode */
651 RESTORE_ROUNDING_MODE;
ths8e33c082006-12-11 19:22:27 +0000652
653#ifndef CONFIG_SOFTFLOAT
ths36d23952007-02-28 22:37:42 +0000654 /* no floating point exception for native float */
655 SET_FP_ENABLE(env->fcr31, 0);
ths8e33c082006-12-11 19:22:27 +0000656#endif
ths36d23952007-02-28 22:37:42 +0000657 }
bellard6f970bd2005-12-05 19:55:19 +0000658}
bellardfdf9b3e2006-04-27 21:07:38 +0000659#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +0000660
661/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
662
bellardfdf9b3e2006-04-27 21:07:38 +0000663static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
664{
665 uint32_t *ptr = (uint32_t *)mem_buf;
666 int i;
667
668#define SAVE(x) *ptr++=tswapl(x)
pbrook9c2a9ea2006-06-18 19:12:54 +0000669 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
670 for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]);
671 } else {
672 for (i = 0; i < 8; i++) SAVE(env->gregs[i]);
673 }
674 for (i = 8; i < 16; i++) SAVE(env->gregs[i]);
bellardfdf9b3e2006-04-27 21:07:38 +0000675 SAVE (env->pc);
676 SAVE (env->pr);
677 SAVE (env->gbr);
678 SAVE (env->vbr);
679 SAVE (env->mach);
680 SAVE (env->macl);
681 SAVE (env->sr);
ths6ef99fc2007-05-13 16:36:24 +0000682 SAVE (env->fpul);
683 SAVE (env->fpscr);
684 for (i = 0; i < 16; i++)
685 SAVE(env->fregs[i + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
686 SAVE (env->ssr);
687 SAVE (env->spc);
688 for (i = 0; i < 8; i++) SAVE(env->gregs[i]);
689 for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]);
bellardfdf9b3e2006-04-27 21:07:38 +0000690 return ((uint8_t *)ptr - mem_buf);
691}
692
693static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
694{
695 uint32_t *ptr = (uint32_t *)mem_buf;
696 int i;
697
698#define LOAD(x) (x)=*ptr++;
pbrook9c2a9ea2006-06-18 19:12:54 +0000699 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
700 for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]);
701 } else {
702 for (i = 0; i < 8; i++) LOAD(env->gregs[i]);
703 }
704 for (i = 8; i < 16; i++) LOAD(env->gregs[i]);
bellardfdf9b3e2006-04-27 21:07:38 +0000705 LOAD (env->pc);
706 LOAD (env->pr);
707 LOAD (env->gbr);
708 LOAD (env->vbr);
709 LOAD (env->mach);
710 LOAD (env->macl);
711 LOAD (env->sr);
ths6ef99fc2007-05-13 16:36:24 +0000712 LOAD (env->fpul);
713 LOAD (env->fpscr);
714 for (i = 0; i < 16; i++)
715 LOAD(env->fregs[i + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
716 LOAD (env->ssr);
717 LOAD (env->spc);
718 for (i = 0; i < 8; i++) LOAD(env->gregs[i]);
719 for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]);
bellardfdf9b3e2006-04-27 21:07:38 +0000720}
bellard1fddef42005-04-17 19:16:13 +0000721#else
bellard6da41ea2004-01-04 15:48:38 +0000722static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
723{
724 return 0;
725}
726
727static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
728{
729}
730
731#endif
bellardb4608c02003-06-27 17:34:32 +0000732
bellard1fddef42005-04-17 19:16:13 +0000733static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000734{
bellardb4608c02003-06-27 17:34:32 +0000735 const char *p;
bellard858693c2004-03-31 18:52:07 +0000736 int ch, reg_size, type;
bellardb4608c02003-06-27 17:34:32 +0000737 char buf[4096];
738 uint8_t mem_buf[2000];
739 uint32_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000740 target_ulong addr, len;
bellardb4608c02003-06-27 17:34:32 +0000741
bellard858693c2004-03-31 18:52:07 +0000742#ifdef DEBUG_GDB
743 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000744#endif
bellard858693c2004-03-31 18:52:07 +0000745 p = line_buf;
746 ch = *p++;
747 switch(ch) {
748 case '?':
bellard1fddef42005-04-17 19:16:13 +0000749 /* TODO: Make this return the correct value for user-mode. */
bellard858693c2004-03-31 18:52:07 +0000750 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
751 put_packet(s, buf);
752 break;
753 case 'c':
754 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000755 addr = strtoull(p, (char **)&p, 16);
bellardc33a3462003-07-29 20:50:33 +0000756#if defined(TARGET_I386)
bellard858693c2004-03-31 18:52:07 +0000757 env->eip = addr;
bellard5be1a8e2004-01-04 23:51:58 +0000758#elif defined (TARGET_PPC)
bellard858693c2004-03-31 18:52:07 +0000759 env->nip = addr;
bellard8d5f07f2004-10-04 21:23:09 +0000760#elif defined (TARGET_SPARC)
761 env->pc = addr;
762 env->npc = addr + 4;
bellardb5ff1b32005-11-26 10:38:39 +0000763#elif defined (TARGET_ARM)
764 env->regs[15] = addr;
bellardfdf9b3e2006-04-27 21:07:38 +0000765#elif defined (TARGET_SH4)
766 env->pc = addr;
bellardc33a3462003-07-29 20:50:33 +0000767#endif
bellard858693c2004-03-31 18:52:07 +0000768 }
bellard41625032005-04-24 10:07:11 +0000769#ifdef CONFIG_USER_ONLY
770 s->running_state = 1;
771#else
772 vm_start();
773#endif
774 return RS_IDLE;
bellard858693c2004-03-31 18:52:07 +0000775 case 's':
776 if (*p != '\0') {
bellardb4608c02003-06-27 17:34:32 +0000777 addr = strtoul(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000778#if defined(TARGET_I386)
779 env->eip = addr;
780#elif defined (TARGET_PPC)
781 env->nip = addr;
bellard8d5f07f2004-10-04 21:23:09 +0000782#elif defined (TARGET_SPARC)
783 env->pc = addr;
784 env->npc = addr + 4;
bellardb5ff1b32005-11-26 10:38:39 +0000785#elif defined (TARGET_ARM)
786 env->regs[15] = addr;
bellardfdf9b3e2006-04-27 21:07:38 +0000787#elif defined (TARGET_SH4)
788 env->pc = addr;
bellard858693c2004-03-31 18:52:07 +0000789#endif
790 }
791 cpu_single_step(env, 1);
bellard41625032005-04-24 10:07:11 +0000792#ifdef CONFIG_USER_ONLY
793 s->running_state = 1;
794#else
795 vm_start();
796#endif
797 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000798 case 'F':
799 {
800 target_ulong ret;
801 target_ulong err;
802
803 ret = strtoull(p, (char **)&p, 16);
804 if (*p == ',') {
805 p++;
806 err = strtoull(p, (char **)&p, 16);
807 } else {
808 err = 0;
809 }
810 if (*p == ',')
811 p++;
812 type = *p;
813 if (gdb_current_syscall_cb)
814 gdb_current_syscall_cb(s->env, ret, err);
815 if (type == 'C') {
816 put_packet(s, "T02");
817 } else {
818#ifdef CONFIG_USER_ONLY
819 s->running_state = 1;
820#else
821 vm_start();
822#endif
823 }
824 }
825 break;
bellard858693c2004-03-31 18:52:07 +0000826 case 'g':
827 reg_size = cpu_gdb_read_registers(env, mem_buf);
828 memtohex(buf, mem_buf, reg_size);
829 put_packet(s, buf);
830 break;
831 case 'G':
832 registers = (void *)mem_buf;
833 len = strlen(p) / 2;
834 hextomem((uint8_t *)registers, p, len);
835 cpu_gdb_write_registers(env, mem_buf, len);
836 put_packet(s, "OK");
837 break;
838 case 'm':
bellard9d9754a2006-06-25 15:32:37 +0000839 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000840 if (*p == ',')
841 p++;
bellard9d9754a2006-06-25 15:32:37 +0000842 len = strtoull(p, NULL, 16);
bellard6f970bd2005-12-05 19:55:19 +0000843 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
844 put_packet (s, "E14");
845 } else {
846 memtohex(buf, mem_buf, len);
847 put_packet(s, buf);
848 }
bellard858693c2004-03-31 18:52:07 +0000849 break;
850 case 'M':
bellard9d9754a2006-06-25 15:32:37 +0000851 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000852 if (*p == ',')
853 p++;
bellard9d9754a2006-06-25 15:32:37 +0000854 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +0000855 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +0000856 p++;
857 hextomem(mem_buf, p, len);
858 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
bellard905f20b2005-04-26 21:09:55 +0000859 put_packet(s, "E14");
bellard858693c2004-03-31 18:52:07 +0000860 else
861 put_packet(s, "OK");
862 break;
863 case 'Z':
864 type = strtoul(p, (char **)&p, 16);
865 if (*p == ',')
866 p++;
bellard9d9754a2006-06-25 15:32:37 +0000867 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000868 if (*p == ',')
869 p++;
bellard9d9754a2006-06-25 15:32:37 +0000870 len = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000871 if (type == 0 || type == 1) {
872 if (cpu_breakpoint_insert(env, addr) < 0)
bellard4c3a88a2003-07-26 12:06:08 +0000873 goto breakpoint_error;
bellard858693c2004-03-31 18:52:07 +0000874 put_packet(s, "OK");
pbrook6658ffb2007-03-16 23:58:11 +0000875#ifndef CONFIG_USER_ONLY
876 } else if (type == 2) {
877 if (cpu_watchpoint_insert(env, addr) < 0)
878 goto breakpoint_error;
879 put_packet(s, "OK");
880#endif
bellard858693c2004-03-31 18:52:07 +0000881 } else {
882 breakpoint_error:
bellard905f20b2005-04-26 21:09:55 +0000883 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +0000884 }
885 break;
886 case 'z':
887 type = strtoul(p, (char **)&p, 16);
888 if (*p == ',')
889 p++;
bellard9d9754a2006-06-25 15:32:37 +0000890 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000891 if (*p == ',')
892 p++;
bellard9d9754a2006-06-25 15:32:37 +0000893 len = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000894 if (type == 0 || type == 1) {
895 cpu_breakpoint_remove(env, addr);
896 put_packet(s, "OK");
pbrook6658ffb2007-03-16 23:58:11 +0000897#ifndef CONFIG_USER_ONLY
898 } else if (type == 2) {
899 cpu_watchpoint_remove(env, addr);
900 put_packet(s, "OK");
901#endif
bellard858693c2004-03-31 18:52:07 +0000902 } else {
903 goto breakpoint_error;
904 }
905 break;
ths831b7822007-01-18 20:06:33 +0000906#ifdef CONFIG_LINUX_USER
pbrook978efd62006-06-17 18:30:42 +0000907 case 'q':
908 if (strncmp(p, "Offsets", 7) == 0) {
909 TaskState *ts = env->opaque;
910
thsfe834d02007-06-03 17:08:32 +0000911 sprintf(buf,
912 "Text=" TARGET_FMT_lx ";Data=" TARGET_FMT_lx ";Bss=" TARGET_FMT_lx,
913 ts->info->code_offset,
914 ts->info->data_offset,
915 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +0000916 put_packet(s, buf);
917 break;
918 }
919 /* Fall through. */
920#endif
bellard858693c2004-03-31 18:52:07 +0000921 default:
922 // unknown_command:
923 /* put empty packet */
924 buf[0] = '\0';
925 put_packet(s, buf);
926 break;
927 }
928 return RS_IDLE;
929}
930
bellard612458f2005-01-03 23:34:06 +0000931extern void tb_flush(CPUState *env);
932
bellard1fddef42005-04-17 19:16:13 +0000933#ifndef CONFIG_USER_ONLY
bellard858693c2004-03-31 18:52:07 +0000934static void gdb_vm_stopped(void *opaque, int reason)
935{
936 GDBState *s = opaque;
937 char buf[256];
938 int ret;
939
pbrooka2d1eba2007-01-28 03:10:55 +0000940 if (s->state == RS_SYSCALL)
941 return;
942
bellard858693c2004-03-31 18:52:07 +0000943 /* disable single step if it was enable */
bellard6a00d602005-11-21 23:25:50 +0000944 cpu_single_step(s->env, 0);
bellard858693c2004-03-31 18:52:07 +0000945
bellarde80cfcf2004-12-19 23:18:01 +0000946 if (reason == EXCP_DEBUG) {
pbrook6658ffb2007-03-16 23:58:11 +0000947 if (s->env->watchpoint_hit) {
pbrookaa6290b2007-04-14 22:35:50 +0000948 snprintf(buf, sizeof(buf), "T%02xwatch:" TARGET_FMT_lx ";",
949 SIGTRAP,
pbrook6658ffb2007-03-16 23:58:11 +0000950 s->env->watchpoint[s->env->watchpoint_hit - 1].vaddr);
951 put_packet(s, buf);
952 s->env->watchpoint_hit = 0;
953 return;
954 }
bellard6a00d602005-11-21 23:25:50 +0000955 tb_flush(s->env);
bellard858693c2004-03-31 18:52:07 +0000956 ret = SIGTRAP;
bellardbbeb7b52006-04-23 18:42:15 +0000957 } else if (reason == EXCP_INTERRUPT) {
958 ret = SIGINT;
959 } else {
bellard858693c2004-03-31 18:52:07 +0000960 ret = 0;
bellardbbeb7b52006-04-23 18:42:15 +0000961 }
bellard858693c2004-03-31 18:52:07 +0000962 snprintf(buf, sizeof(buf), "S%02x", ret);
963 put_packet(s, buf);
964}
bellard1fddef42005-04-17 19:16:13 +0000965#endif
bellard858693c2004-03-31 18:52:07 +0000966
pbrooka2d1eba2007-01-28 03:10:55 +0000967/* Send a gdb syscall request.
968 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +0000969 %x - target_ulong argument printed in hex.
970 %lx - 64-bit argument printed in hex.
971 %s - string pointer (target_ulong) and length (int) pair. */
pbrooka2d1eba2007-01-28 03:10:55 +0000972void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...)
973{
974 va_list va;
975 char buf[256];
976 char *p;
977 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +0000978 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +0000979 GDBState *s;
980
981 s = gdb_syscall_state;
982 if (!s)
983 return;
984 gdb_current_syscall_cb = cb;
985 s->state = RS_SYSCALL;
986#ifndef CONFIG_USER_ONLY
987 vm_stop(EXCP_DEBUG);
988#endif
989 s->state = RS_IDLE;
990 va_start(va, fmt);
991 p = buf;
992 *(p++) = 'F';
993 while (*fmt) {
994 if (*fmt == '%') {
995 fmt++;
996 switch (*fmt++) {
997 case 'x':
998 addr = va_arg(va, target_ulong);
999 p += sprintf(p, TARGET_FMT_lx, addr);
1000 break;
pbrooka87295e2007-05-26 15:09:38 +00001001 case 'l':
1002 if (*(fmt++) != 'x')
1003 goto bad_format;
1004 i64 = va_arg(va, uint64_t);
1005 p += sprintf(p, "%" PRIx64, i64);
1006 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001007 case 's':
1008 addr = va_arg(va, target_ulong);
1009 p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int));
1010 break;
1011 default:
pbrooka87295e2007-05-26 15:09:38 +00001012 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001013 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1014 fmt - 1);
1015 break;
1016 }
1017 } else {
1018 *(p++) = *(fmt++);
1019 }
1020 }
1021 va_end(va);
1022 put_packet(s, buf);
1023#ifdef CONFIG_USER_ONLY
1024 gdb_handlesig(s->env, 0);
1025#else
1026 cpu_interrupt(s->env, CPU_INTERRUPT_EXIT);
1027#endif
1028}
1029
bellard6a00d602005-11-21 23:25:50 +00001030static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001031{
bellard6a00d602005-11-21 23:25:50 +00001032 CPUState *env = s->env;
bellard858693c2004-03-31 18:52:07 +00001033 int i, csum;
1034 char reply[1];
1035
bellard1fddef42005-04-17 19:16:13 +00001036#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001037 if (s->last_packet_len) {
1038 /* Waiting for a response to the last packet. If we see the start
1039 of a new command then abandon the previous response. */
1040 if (ch == '-') {
1041#ifdef DEBUG_GDB
1042 printf("Got NACK, retransmitting\n");
1043#endif
1044 put_buffer(s, s->last_packet, s->last_packet_len);
1045 }
1046#ifdef DEBUG_GDB
1047 else if (ch == '+')
1048 printf("Got ACK\n");
1049 else
1050 printf("Got '%c' when expecting ACK/NACK\n", ch);
1051#endif
1052 if (ch == '+' || ch == '$')
1053 s->last_packet_len = 0;
1054 if (ch != '$')
1055 return;
1056 }
bellard858693c2004-03-31 18:52:07 +00001057 if (vm_running) {
1058 /* when the CPU is running, we cannot do anything except stop
1059 it when receiving a char */
1060 vm_stop(EXCP_INTERRUPT);
bellard41625032005-04-24 10:07:11 +00001061 } else
bellard1fddef42005-04-17 19:16:13 +00001062#endif
bellard41625032005-04-24 10:07:11 +00001063 {
bellard858693c2004-03-31 18:52:07 +00001064 switch(s->state) {
1065 case RS_IDLE:
1066 if (ch == '$') {
1067 s->line_buf_index = 0;
1068 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001069 }
1070 break;
bellard858693c2004-03-31 18:52:07 +00001071 case RS_GETLINE:
1072 if (ch == '#') {
1073 s->state = RS_CHKSUM1;
1074 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1075 s->state = RS_IDLE;
1076 } else {
1077 s->line_buf[s->line_buf_index++] = ch;
1078 }
1079 break;
1080 case RS_CHKSUM1:
1081 s->line_buf[s->line_buf_index] = '\0';
1082 s->line_csum = fromhex(ch) << 4;
1083 s->state = RS_CHKSUM2;
1084 break;
1085 case RS_CHKSUM2:
1086 s->line_csum |= fromhex(ch);
1087 csum = 0;
1088 for(i = 0; i < s->line_buf_index; i++) {
1089 csum += s->line_buf[i];
1090 }
1091 if (s->line_csum != (csum & 0xff)) {
1092 reply[0] = '-';
1093 put_buffer(s, reply, 1);
1094 s->state = RS_IDLE;
1095 } else {
1096 reply[0] = '+';
1097 put_buffer(s, reply, 1);
bellard1fddef42005-04-17 19:16:13 +00001098 s->state = gdb_handle_packet(s, env, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001099 }
bellardb4608c02003-06-27 17:34:32 +00001100 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001101 default:
1102 abort();
bellardb4608c02003-06-27 17:34:32 +00001103 }
1104 }
bellard858693c2004-03-31 18:52:07 +00001105}
1106
bellard1fddef42005-04-17 19:16:13 +00001107#ifdef CONFIG_USER_ONLY
1108int
1109gdb_handlesig (CPUState *env, int sig)
1110{
1111 GDBState *s;
1112 char buf[256];
1113 int n;
1114
1115 if (gdbserver_fd < 0)
1116 return sig;
1117
1118 s = &gdbserver_state;
1119
1120 /* disable single step if it was enabled */
1121 cpu_single_step(env, 0);
1122 tb_flush(env);
1123
1124 if (sig != 0)
1125 {
1126 snprintf(buf, sizeof(buf), "S%02x", sig);
1127 put_packet(s, buf);
1128 }
1129
bellard1fddef42005-04-17 19:16:13 +00001130 sig = 0;
1131 s->state = RS_IDLE;
bellard41625032005-04-24 10:07:11 +00001132 s->running_state = 0;
1133 while (s->running_state == 0) {
bellard1fddef42005-04-17 19:16:13 +00001134 n = read (s->fd, buf, 256);
1135 if (n > 0)
1136 {
1137 int i;
1138
1139 for (i = 0; i < n; i++)
bellard6a00d602005-11-21 23:25:50 +00001140 gdb_read_byte (s, buf[i]);
bellard1fddef42005-04-17 19:16:13 +00001141 }
1142 else if (n == 0 || errno != EAGAIN)
1143 {
1144 /* XXX: Connection closed. Should probably wait for annother
1145 connection before continuing. */
1146 return sig;
1147 }
bellard41625032005-04-24 10:07:11 +00001148 }
bellard1fddef42005-04-17 19:16:13 +00001149 return sig;
1150}
bellarde9009672005-04-26 20:42:36 +00001151
1152/* Tell the remote gdb that the process has exited. */
1153void gdb_exit(CPUState *env, int code)
1154{
1155 GDBState *s;
1156 char buf[4];
1157
1158 if (gdbserver_fd < 0)
1159 return;
1160
1161 s = &gdbserver_state;
1162
1163 snprintf(buf, sizeof(buf), "W%02x", code);
1164 put_packet(s, buf);
1165}
1166
bellard1fddef42005-04-17 19:16:13 +00001167
bellard7c9d8e02005-11-15 22:16:05 +00001168static void gdb_accept(void *opaque)
bellard858693c2004-03-31 18:52:07 +00001169{
1170 GDBState *s;
1171 struct sockaddr_in sockaddr;
1172 socklen_t len;
1173 int val, fd;
1174
1175 for(;;) {
1176 len = sizeof(sockaddr);
1177 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1178 if (fd < 0 && errno != EINTR) {
1179 perror("accept");
1180 return;
1181 } else if (fd >= 0) {
1182 break;
1183 }
1184 }
1185
1186 /* set short latency */
1187 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00001188 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00001189
bellard1fddef42005-04-17 19:16:13 +00001190 s = &gdbserver_state;
1191 memset (s, 0, sizeof (GDBState));
bellard6a00d602005-11-21 23:25:50 +00001192 s->env = first_cpu; /* XXX: allow to change CPU */
bellard858693c2004-03-31 18:52:07 +00001193 s->fd = fd;
1194
pbrooka2d1eba2007-01-28 03:10:55 +00001195 gdb_syscall_state = s;
1196
bellard858693c2004-03-31 18:52:07 +00001197 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001198}
1199
1200static int gdbserver_open(int port)
1201{
1202 struct sockaddr_in sockaddr;
1203 int fd, val, ret;
1204
1205 fd = socket(PF_INET, SOCK_STREAM, 0);
1206 if (fd < 0) {
1207 perror("socket");
1208 return -1;
1209 }
1210
1211 /* allow fast reuse */
1212 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00001213 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00001214
1215 sockaddr.sin_family = AF_INET;
1216 sockaddr.sin_port = htons(port);
1217 sockaddr.sin_addr.s_addr = 0;
1218 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1219 if (ret < 0) {
1220 perror("bind");
1221 return -1;
1222 }
1223 ret = listen(fd, 0);
1224 if (ret < 0) {
1225 perror("listen");
1226 return -1;
1227 }
bellard858693c2004-03-31 18:52:07 +00001228 return fd;
1229}
1230
1231int gdbserver_start(int port)
1232{
1233 gdbserver_fd = gdbserver_open(port);
1234 if (gdbserver_fd < 0)
1235 return -1;
1236 /* accept connections */
bellard7c9d8e02005-11-15 22:16:05 +00001237 gdb_accept (NULL);
bellardb4608c02003-06-27 17:34:32 +00001238 return 0;
1239}
pbrook4046d912007-01-28 01:53:16 +00001240#else
1241static int gdb_chr_can_recieve(void *opaque)
1242{
1243 return 1;
1244}
1245
1246static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size)
1247{
1248 GDBState *s = opaque;
1249 int i;
1250
1251 for (i = 0; i < size; i++) {
1252 gdb_read_byte(s, buf[i]);
1253 }
1254}
1255
1256static void gdb_chr_event(void *opaque, int event)
1257{
1258 switch (event) {
1259 case CHR_EVENT_RESET:
1260 vm_stop(EXCP_INTERRUPT);
pbrooka2d1eba2007-01-28 03:10:55 +00001261 gdb_syscall_state = opaque;
pbrook4046d912007-01-28 01:53:16 +00001262 break;
1263 default:
1264 break;
1265 }
1266}
1267
pbrookcfc34752007-02-22 01:48:01 +00001268int gdbserver_start(const char *port)
pbrook4046d912007-01-28 01:53:16 +00001269{
1270 GDBState *s;
pbrookcfc34752007-02-22 01:48:01 +00001271 char gdbstub_port_name[128];
1272 int port_num;
1273 char *p;
1274 CharDriverState *chr;
pbrook4046d912007-01-28 01:53:16 +00001275
pbrookcfc34752007-02-22 01:48:01 +00001276 if (!port || !*port)
1277 return -1;
1278
1279 port_num = strtol(port, &p, 10);
1280 if (*p == 0) {
1281 /* A numeric value is interpreted as a port number. */
1282 snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
1283 "tcp::%d,nowait,nodelay,server", port_num);
1284 port = gdbstub_port_name;
1285 }
1286
1287 chr = qemu_chr_open(port);
pbrook4046d912007-01-28 01:53:16 +00001288 if (!chr)
1289 return -1;
1290
1291 s = qemu_mallocz(sizeof(GDBState));
1292 if (!s) {
1293 return -1;
1294 }
1295 s->env = first_cpu; /* XXX: allow to change CPU */
1296 s->chr = chr;
1297 qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve,
1298 gdb_chr_event, s);
1299 qemu_add_vm_stop_handler(gdb_vm_stopped, s);
1300 return 0;
1301}
1302#endif