bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gdb server stub |
| 3 | * |
| 4 | * Copyright (c) 2003 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 | */ |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 20 | #ifdef CONFIG_USER_ONLY |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "qemu.h" |
| 29 | #else |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 30 | #include "vl.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 31 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 32 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 33 | #include <sys/socket.h> |
| 34 | #include <netinet/in.h> |
| 35 | #include <netinet/tcp.h> |
| 36 | #include <signal.h> |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 37 | |
bellard | 4abe615 | 2003-07-26 18:01:58 +0000 | [diff] [blame] | 38 | //#define DEBUG_GDB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 39 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 40 | enum RSState { |
| 41 | RS_IDLE, |
| 42 | RS_GETLINE, |
| 43 | RS_CHKSUM1, |
| 44 | RS_CHKSUM2, |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 45 | RS_CONTINUE |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 46 | }; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 47 | /* XXX: This is not thread safe. Do we care? */ |
| 48 | static int gdbserver_fd = -1; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 49 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 50 | typedef struct GDBState { |
| 51 | enum RSState state; |
| 52 | int fd; |
| 53 | char line_buf[4096]; |
| 54 | int line_buf_index; |
| 55 | int line_csum; |
| 56 | } GDBState; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 57 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 58 | #ifdef CONFIG_USER_ONLY |
| 59 | /* XXX: remove this hack. */ |
| 60 | static GDBState gdbserver_state; |
| 61 | #endif |
| 62 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 63 | static int get_char(GDBState *s) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 64 | { |
| 65 | uint8_t ch; |
| 66 | int ret; |
| 67 | |
| 68 | for(;;) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 69 | ret = read(s->fd, &ch, 1); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 70 | if (ret < 0) { |
| 71 | if (errno != EINTR && errno != EAGAIN) |
| 72 | return -1; |
| 73 | } else if (ret == 0) { |
| 74 | return -1; |
| 75 | } else { |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | return ch; |
| 80 | } |
| 81 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 82 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 83 | { |
| 84 | int ret; |
| 85 | |
| 86 | while (len > 0) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 87 | ret = write(s->fd, buf, len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 88 | if (ret < 0) { |
| 89 | if (errno != EINTR && errno != EAGAIN) |
| 90 | return; |
| 91 | } else { |
| 92 | buf += ret; |
| 93 | len -= ret; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static inline int fromhex(int v) |
| 99 | { |
| 100 | if (v >= '0' && v <= '9') |
| 101 | return v - '0'; |
| 102 | else if (v >= 'A' && v <= 'F') |
| 103 | return v - 'A' + 10; |
| 104 | else if (v >= 'a' && v <= 'f') |
| 105 | return v - 'a' + 10; |
| 106 | else |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static inline int tohex(int v) |
| 111 | { |
| 112 | if (v < 10) |
| 113 | return v + '0'; |
| 114 | else |
| 115 | return v - 10 + 'a'; |
| 116 | } |
| 117 | |
| 118 | static void memtohex(char *buf, const uint8_t *mem, int len) |
| 119 | { |
| 120 | int i, c; |
| 121 | char *q; |
| 122 | q = buf; |
| 123 | for(i = 0; i < len; i++) { |
| 124 | c = mem[i]; |
| 125 | *q++ = tohex(c >> 4); |
| 126 | *q++ = tohex(c & 0xf); |
| 127 | } |
| 128 | *q = '\0'; |
| 129 | } |
| 130 | |
| 131 | static void hextomem(uint8_t *mem, const char *buf, int len) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | for(i = 0; i < len; i++) { |
| 136 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
| 137 | buf += 2; |
| 138 | } |
| 139 | } |
| 140 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 141 | /* return -1 if error, 0 if OK */ |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 142 | static int put_packet(GDBState *s, char *buf) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 143 | { |
| 144 | char buf1[3]; |
| 145 | int len, csum, ch, i; |
| 146 | |
| 147 | #ifdef DEBUG_GDB |
| 148 | printf("reply='%s'\n", buf); |
| 149 | #endif |
| 150 | |
| 151 | for(;;) { |
| 152 | buf1[0] = '$'; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 153 | put_buffer(s, buf1, 1); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 154 | len = strlen(buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 155 | put_buffer(s, buf, len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 156 | csum = 0; |
| 157 | for(i = 0; i < len; i++) { |
| 158 | csum += buf[i]; |
| 159 | } |
| 160 | buf1[0] = '#'; |
| 161 | buf1[1] = tohex((csum >> 4) & 0xf); |
| 162 | buf1[2] = tohex((csum) & 0xf); |
| 163 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 164 | put_buffer(s, buf1, 3); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 165 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 166 | ch = get_char(s); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 167 | if (ch < 0) |
| 168 | return -1; |
| 169 | if (ch == '+') |
| 170 | break; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 175 | #if defined(TARGET_I386) |
| 176 | |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 177 | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
| 178 | { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 179 | uint32_t *registers = (uint32_t *)mem_buf; |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 180 | int i, fpus; |
| 181 | |
| 182 | for(i = 0; i < 8; i++) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 183 | registers[i] = env->regs[i]; |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 184 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 185 | registers[8] = env->eip; |
| 186 | registers[9] = env->eflags; |
| 187 | registers[10] = env->segs[R_CS].selector; |
| 188 | registers[11] = env->segs[R_SS].selector; |
| 189 | registers[12] = env->segs[R_DS].selector; |
| 190 | registers[13] = env->segs[R_ES].selector; |
| 191 | registers[14] = env->segs[R_FS].selector; |
| 192 | registers[15] = env->segs[R_GS].selector; |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 193 | /* XXX: convert floats */ |
| 194 | for(i = 0; i < 8; i++) { |
| 195 | memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
| 196 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 197 | registers[36] = env->fpuc; |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 198 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 199 | registers[37] = fpus; |
| 200 | registers[38] = 0; /* XXX: convert tags */ |
| 201 | registers[39] = 0; /* fiseg */ |
| 202 | registers[40] = 0; /* fioff */ |
| 203 | registers[41] = 0; /* foseg */ |
| 204 | registers[42] = 0; /* fooff */ |
| 205 | registers[43] = 0; /* fop */ |
| 206 | |
| 207 | for(i = 0; i < 16; i++) |
| 208 | tswapls(®isters[i]); |
| 209 | for(i = 36; i < 44; i++) |
| 210 | tswapls(®isters[i]); |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 211 | return 44 * 4; |
| 212 | } |
| 213 | |
| 214 | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
| 215 | { |
| 216 | uint32_t *registers = (uint32_t *)mem_buf; |
| 217 | int i; |
| 218 | |
| 219 | for(i = 0; i < 8; i++) { |
| 220 | env->regs[i] = tswapl(registers[i]); |
| 221 | } |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 222 | env->eip = tswapl(registers[8]); |
| 223 | env->eflags = tswapl(registers[9]); |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 224 | #if defined(CONFIG_USER_ONLY) |
| 225 | #define LOAD_SEG(index, sreg)\ |
| 226 | if (tswapl(registers[index]) != env->segs[sreg].selector)\ |
| 227 | cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
| 228 | LOAD_SEG(10, R_CS); |
| 229 | LOAD_SEG(11, R_SS); |
| 230 | LOAD_SEG(12, R_DS); |
| 231 | LOAD_SEG(13, R_ES); |
| 232 | LOAD_SEG(14, R_FS); |
| 233 | LOAD_SEG(15, R_GS); |
| 234 | #endif |
| 235 | } |
| 236 | |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 237 | #elif defined (TARGET_PPC) |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 238 | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
| 239 | { |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 240 | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 241 | int i; |
| 242 | |
| 243 | /* fill in gprs */ |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 244 | for(i = 0; i < 32; i++) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 245 | registers[i] = tswapl(env->gpr[i]); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 246 | } |
| 247 | /* fill in fprs */ |
| 248 | for (i = 0; i < 32; i++) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 249 | registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
| 250 | registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 251 | } |
| 252 | /* nip, msr, ccr, lnk, ctr, xer, mq */ |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 253 | registers[96] = tswapl(env->nip); |
| 254 | registers[97] = tswapl(_load_msr(env)); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 255 | tmp = 0; |
| 256 | for (i = 0; i < 8; i++) |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 257 | tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 258 | registers[98] = tswapl(tmp); |
| 259 | registers[99] = tswapl(env->lr); |
| 260 | registers[100] = tswapl(env->ctr); |
| 261 | registers[101] = tswapl(_load_xer(env)); |
| 262 | registers[102] = 0; |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 263 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 264 | return 103 * 4; |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
| 268 | { |
| 269 | uint32_t *registers = (uint32_t *)mem_buf; |
| 270 | int i; |
| 271 | |
| 272 | /* fill in gprs */ |
| 273 | for (i = 0; i < 32; i++) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 274 | env->gpr[i] = tswapl(registers[i]); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 275 | } |
| 276 | /* fill in fprs */ |
| 277 | for (i = 0; i < 32; i++) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 278 | *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
| 279 | *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 280 | } |
| 281 | /* nip, msr, ccr, lnk, ctr, xer, mq */ |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 282 | env->nip = tswapl(registers[96]); |
| 283 | _store_msr(env, tswapl(registers[97])); |
| 284 | registers[98] = tswapl(registers[98]); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 285 | for (i = 0; i < 8; i++) |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 286 | env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 287 | env->lr = tswapl(registers[99]); |
| 288 | env->ctr = tswapl(registers[100]); |
| 289 | _store_xer(env, tswapl(registers[101])); |
| 290 | } |
| 291 | #elif defined (TARGET_SPARC) |
| 292 | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
| 293 | { |
| 294 | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
| 295 | int i; |
| 296 | |
| 297 | /* fill in g0..g7 */ |
| 298 | for(i = 0; i < 7; i++) { |
| 299 | registers[i] = tswapl(env->gregs[i]); |
| 300 | } |
| 301 | /* fill in register window */ |
| 302 | for(i = 0; i < 24; i++) { |
| 303 | registers[i + 8] = tswapl(env->regwptr[i]); |
| 304 | } |
| 305 | /* fill in fprs */ |
| 306 | for (i = 0; i < 32; i++) { |
| 307 | registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
| 308 | } |
| 309 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ |
| 310 | registers[64] = tswapl(env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 311 | tmp = GET_PSR(env); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 312 | registers[65] = tswapl(tmp); |
| 313 | registers[66] = tswapl(env->wim); |
| 314 | registers[67] = tswapl(env->tbr); |
| 315 | registers[68] = tswapl(env->pc); |
| 316 | registers[69] = tswapl(env->npc); |
| 317 | registers[70] = tswapl(env->fsr); |
| 318 | registers[71] = 0; /* csr */ |
| 319 | registers[72] = 0; |
| 320 | |
| 321 | return 73 * 4; |
| 322 | } |
| 323 | |
| 324 | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
| 325 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 326 | uint32_t *registers = (uint32_t *)mem_buf; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 327 | int i; |
| 328 | |
| 329 | /* fill in g0..g7 */ |
| 330 | for(i = 0; i < 7; i++) { |
| 331 | env->gregs[i] = tswapl(registers[i]); |
| 332 | } |
| 333 | /* fill in register window */ |
| 334 | for(i = 0; i < 24; i++) { |
| 335 | env->regwptr[i] = tswapl(registers[i]); |
| 336 | } |
| 337 | /* fill in fprs */ |
| 338 | for (i = 0; i < 32; i++) { |
| 339 | *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]); |
| 340 | } |
| 341 | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */ |
| 342 | env->y = tswapl(registers[64]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 343 | PUT_PSR(env, tswapl(registers[65])); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 344 | env->wim = tswapl(registers[66]); |
| 345 | env->tbr = tswapl(registers[67]); |
| 346 | env->pc = tswapl(registers[68]); |
| 347 | env->npc = tswapl(registers[69]); |
| 348 | env->fsr = tswapl(registers[70]); |
bellard | 9e62fd7 | 2004-01-05 22:49:06 +0000 | [diff] [blame] | 349 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 350 | #elif defined (TARGET_ARM) |
| 351 | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
| 352 | { |
| 353 | int i; |
| 354 | uint8_t *ptr; |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 355 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 356 | ptr = mem_buf; |
| 357 | /* 16 core integer registers (4 bytes each). */ |
| 358 | for (i = 0; i < 16; i++) |
| 359 | { |
| 360 | *(uint32_t *)ptr = tswapl(env->regs[i]); |
| 361 | ptr += 4; |
| 362 | } |
| 363 | /* 8 FPA registers (12 bytes each), FPS (4 bytes). |
| 364 | Not yet implemented. */ |
| 365 | memset (ptr, 0, 8 * 12 + 4); |
| 366 | ptr += 8 * 12 + 4; |
| 367 | /* CPSR (4 bytes). */ |
| 368 | *(uint32_t *)ptr = tswapl (env->cpsr); |
| 369 | ptr += 4; |
| 370 | |
| 371 | return ptr - mem_buf; |
| 372 | } |
| 373 | |
| 374 | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
| 375 | { |
| 376 | int i; |
| 377 | uint8_t *ptr; |
| 378 | |
| 379 | ptr = mem_buf; |
| 380 | /* Core integer registers. */ |
| 381 | for (i = 0; i < 16; i++) |
| 382 | { |
| 383 | env->regs[i] = tswapl(*(uint32_t *)ptr); |
| 384 | ptr += 4; |
| 385 | } |
| 386 | /* Ignore FPA regs and scr. */ |
| 387 | ptr += 8 * 12 + 4; |
| 388 | env->cpsr = tswapl(*(uint32_t *)ptr); |
| 389 | } |
| 390 | #else |
bellard | 6da41ea | 2004-01-04 15:48:38 +0000 | [diff] [blame] | 391 | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 401 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 402 | static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 403 | { |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 404 | const char *p; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 405 | int ch, reg_size, type; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 406 | char buf[4096]; |
| 407 | uint8_t mem_buf[2000]; |
| 408 | uint32_t *registers; |
| 409 | uint32_t addr, len; |
| 410 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 411 | #ifdef DEBUG_GDB |
| 412 | printf("command='%s'\n", line_buf); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 413 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 414 | p = line_buf; |
| 415 | ch = *p++; |
| 416 | switch(ch) { |
| 417 | case '?': |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 418 | /* TODO: Make this return the correct value for user-mode. */ |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 419 | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
| 420 | put_packet(s, buf); |
| 421 | break; |
| 422 | case 'c': |
| 423 | if (*p != '\0') { |
| 424 | addr = strtoul(p, (char **)&p, 16); |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 425 | #if defined(TARGET_I386) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 426 | env->eip = addr; |
bellard | 5be1a8e | 2004-01-04 23:51:58 +0000 | [diff] [blame] | 427 | #elif defined (TARGET_PPC) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 428 | env->nip = addr; |
bellard | 8d5f07f | 2004-10-04 21:23:09 +0000 | [diff] [blame] | 429 | #elif defined (TARGET_SPARC) |
| 430 | env->pc = addr; |
| 431 | env->npc = addr + 4; |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 432 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 433 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 434 | return RS_CONTINUE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 435 | case 's': |
| 436 | if (*p != '\0') { |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 437 | addr = strtoul(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 438 | #if defined(TARGET_I386) |
| 439 | env->eip = addr; |
| 440 | #elif defined (TARGET_PPC) |
| 441 | env->nip = addr; |
bellard | 8d5f07f | 2004-10-04 21:23:09 +0000 | [diff] [blame] | 442 | #elif defined (TARGET_SPARC) |
| 443 | env->pc = addr; |
| 444 | env->npc = addr + 4; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 445 | #endif |
| 446 | } |
| 447 | cpu_single_step(env, 1); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 448 | return RS_CONTINUE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 449 | case 'g': |
| 450 | reg_size = cpu_gdb_read_registers(env, mem_buf); |
| 451 | memtohex(buf, mem_buf, reg_size); |
| 452 | put_packet(s, buf); |
| 453 | break; |
| 454 | case 'G': |
| 455 | registers = (void *)mem_buf; |
| 456 | len = strlen(p) / 2; |
| 457 | hextomem((uint8_t *)registers, p, len); |
| 458 | cpu_gdb_write_registers(env, mem_buf, len); |
| 459 | put_packet(s, "OK"); |
| 460 | break; |
| 461 | case 'm': |
| 462 | addr = strtoul(p, (char **)&p, 16); |
| 463 | if (*p == ',') |
| 464 | p++; |
| 465 | len = strtoul(p, NULL, 16); |
| 466 | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) |
| 467 | memset(mem_buf, 0, len); |
| 468 | memtohex(buf, mem_buf, len); |
| 469 | put_packet(s, buf); |
| 470 | break; |
| 471 | case 'M': |
| 472 | addr = strtoul(p, (char **)&p, 16); |
| 473 | if (*p == ',') |
| 474 | p++; |
| 475 | len = strtoul(p, (char **)&p, 16); |
bellard | b328f87 | 2005-01-17 22:03:16 +0000 | [diff] [blame] | 476 | if (*p == ':') |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 477 | p++; |
| 478 | hextomem(mem_buf, p, len); |
| 479 | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
| 480 | put_packet(s, "ENN"); |
| 481 | else |
| 482 | put_packet(s, "OK"); |
| 483 | break; |
| 484 | case 'Z': |
| 485 | type = strtoul(p, (char **)&p, 16); |
| 486 | if (*p == ',') |
| 487 | p++; |
| 488 | addr = strtoul(p, (char **)&p, 16); |
| 489 | if (*p == ',') |
| 490 | p++; |
| 491 | len = strtoul(p, (char **)&p, 16); |
| 492 | if (type == 0 || type == 1) { |
| 493 | if (cpu_breakpoint_insert(env, addr) < 0) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 494 | goto breakpoint_error; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 495 | put_packet(s, "OK"); |
| 496 | } else { |
| 497 | breakpoint_error: |
| 498 | put_packet(s, "ENN"); |
| 499 | } |
| 500 | break; |
| 501 | case 'z': |
| 502 | type = strtoul(p, (char **)&p, 16); |
| 503 | if (*p == ',') |
| 504 | p++; |
| 505 | addr = strtoul(p, (char **)&p, 16); |
| 506 | if (*p == ',') |
| 507 | p++; |
| 508 | len = strtoul(p, (char **)&p, 16); |
| 509 | if (type == 0 || type == 1) { |
| 510 | cpu_breakpoint_remove(env, addr); |
| 511 | put_packet(s, "OK"); |
| 512 | } else { |
| 513 | goto breakpoint_error; |
| 514 | } |
| 515 | break; |
| 516 | default: |
| 517 | // unknown_command: |
| 518 | /* put empty packet */ |
| 519 | buf[0] = '\0'; |
| 520 | put_packet(s, buf); |
| 521 | break; |
| 522 | } |
| 523 | return RS_IDLE; |
| 524 | } |
| 525 | |
bellard | 612458f | 2005-01-03 23:34:06 +0000 | [diff] [blame] | 526 | extern void tb_flush(CPUState *env); |
| 527 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 528 | #ifndef CONFIG_USER_ONLY |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 529 | static void gdb_vm_stopped(void *opaque, int reason) |
| 530 | { |
| 531 | GDBState *s = opaque; |
| 532 | char buf[256]; |
| 533 | int ret; |
| 534 | |
| 535 | /* disable single step if it was enable */ |
| 536 | cpu_single_step(cpu_single_env, 0); |
| 537 | |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 538 | if (reason == EXCP_DEBUG) { |
| 539 | tb_flush(cpu_single_env); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 540 | ret = SIGTRAP; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 541 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 542 | else |
| 543 | ret = 0; |
| 544 | snprintf(buf, sizeof(buf), "S%02x", ret); |
| 545 | put_packet(s, buf); |
| 546 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 547 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 548 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 549 | static void gdb_read_byte(GDBState *s, CPUState *env, int ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 550 | { |
| 551 | int i, csum; |
| 552 | char reply[1]; |
| 553 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 554 | #ifndef CONFIG_USER_ONLY |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 555 | if (vm_running) { |
| 556 | /* when the CPU is running, we cannot do anything except stop |
| 557 | it when receiving a char */ |
| 558 | vm_stop(EXCP_INTERRUPT); |
| 559 | } else { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 560 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 561 | switch(s->state) { |
| 562 | case RS_IDLE: |
| 563 | if (ch == '$') { |
| 564 | s->line_buf_index = 0; |
| 565 | s->state = RS_GETLINE; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 566 | } |
| 567 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 568 | case RS_GETLINE: |
| 569 | if (ch == '#') { |
| 570 | s->state = RS_CHKSUM1; |
| 571 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
| 572 | s->state = RS_IDLE; |
| 573 | } else { |
| 574 | s->line_buf[s->line_buf_index++] = ch; |
| 575 | } |
| 576 | break; |
| 577 | case RS_CHKSUM1: |
| 578 | s->line_buf[s->line_buf_index] = '\0'; |
| 579 | s->line_csum = fromhex(ch) << 4; |
| 580 | s->state = RS_CHKSUM2; |
| 581 | break; |
| 582 | case RS_CHKSUM2: |
| 583 | s->line_csum |= fromhex(ch); |
| 584 | csum = 0; |
| 585 | for(i = 0; i < s->line_buf_index; i++) { |
| 586 | csum += s->line_buf[i]; |
| 587 | } |
| 588 | if (s->line_csum != (csum & 0xff)) { |
| 589 | reply[0] = '-'; |
| 590 | put_buffer(s, reply, 1); |
| 591 | s->state = RS_IDLE; |
| 592 | } else { |
| 593 | reply[0] = '+'; |
| 594 | put_buffer(s, reply, 1); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 595 | s->state = gdb_handle_packet(s, env, s->line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 596 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 597 | break; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 598 | case RS_CONTINUE: |
| 599 | #ifndef CONFIG_USER_ONLY |
| 600 | vm_start(); |
| 601 | s->state = RS_IDLE; |
| 602 | #endif |
| 603 | break; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 604 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 605 | #ifndef CONFIG_USER_ONLY |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 606 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 607 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 608 | } |
| 609 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 610 | #ifdef CONFIG_USER_ONLY |
| 611 | int |
| 612 | gdb_handlesig (CPUState *env, int sig) |
| 613 | { |
| 614 | GDBState *s; |
| 615 | char buf[256]; |
| 616 | int n; |
| 617 | |
| 618 | if (gdbserver_fd < 0) |
| 619 | return sig; |
| 620 | |
| 621 | s = &gdbserver_state; |
| 622 | |
| 623 | /* disable single step if it was enabled */ |
| 624 | cpu_single_step(env, 0); |
| 625 | tb_flush(env); |
| 626 | |
| 627 | if (sig != 0) |
| 628 | { |
| 629 | snprintf(buf, sizeof(buf), "S%02x", sig); |
| 630 | put_packet(s, buf); |
| 631 | } |
| 632 | |
| 633 | /* TODO: How do we terminate this loop? */ |
| 634 | sig = 0; |
| 635 | s->state = RS_IDLE; |
| 636 | while (s->state != RS_CONTINUE) |
| 637 | { |
| 638 | n = read (s->fd, buf, 256); |
| 639 | if (n > 0) |
| 640 | { |
| 641 | int i; |
| 642 | |
| 643 | for (i = 0; i < n; i++) |
| 644 | gdb_read_byte (s, env, buf[i]); |
| 645 | } |
| 646 | else if (n == 0 || errno != EAGAIN) |
| 647 | { |
| 648 | /* XXX: Connection closed. Should probably wait for annother |
| 649 | connection before continuing. */ |
| 650 | return sig; |
| 651 | } |
| 652 | } |
| 653 | return sig; |
| 654 | } |
| 655 | #else |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 656 | static int gdb_can_read(void *opaque) |
| 657 | { |
| 658 | return 256; |
| 659 | } |
| 660 | |
| 661 | static void gdb_read(void *opaque, const uint8_t *buf, int size) |
| 662 | { |
| 663 | GDBState *s = opaque; |
| 664 | int i; |
| 665 | if (size == 0) { |
| 666 | /* end of connection */ |
| 667 | qemu_del_vm_stop_handler(gdb_vm_stopped, s); |
| 668 | qemu_del_fd_read_handler(s->fd); |
| 669 | qemu_free(s); |
| 670 | vm_start(); |
| 671 | } else { |
| 672 | for(i = 0; i < size; i++) |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 673 | gdb_read_byte(s, cpu_single_env, buf[i]); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 677 | #endif |
| 678 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 679 | static void gdb_accept(void *opaque, const uint8_t *buf, int size) |
| 680 | { |
| 681 | GDBState *s; |
| 682 | struct sockaddr_in sockaddr; |
| 683 | socklen_t len; |
| 684 | int val, fd; |
| 685 | |
| 686 | for(;;) { |
| 687 | len = sizeof(sockaddr); |
| 688 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); |
| 689 | if (fd < 0 && errno != EINTR) { |
| 690 | perror("accept"); |
| 691 | return; |
| 692 | } else if (fd >= 0) { |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* set short latency */ |
| 698 | val = 1; |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 699 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 700 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 701 | #ifdef CONFIG_USER_ONLY |
| 702 | s = &gdbserver_state; |
| 703 | memset (s, 0, sizeof (GDBState)); |
| 704 | #else |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 705 | s = qemu_mallocz(sizeof(GDBState)); |
| 706 | if (!s) { |
| 707 | close(fd); |
| 708 | return; |
| 709 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 710 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 711 | s->fd = fd; |
| 712 | |
| 713 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 714 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 715 | #ifndef CONFIG_USER_ONLY |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 716 | /* stop the VM */ |
| 717 | vm_stop(EXCP_INTERRUPT); |
| 718 | |
| 719 | /* start handling I/O */ |
| 720 | qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s); |
| 721 | /* when the VM is stopped, the following callback is called */ |
| 722 | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 723 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static int gdbserver_open(int port) |
| 727 | { |
| 728 | struct sockaddr_in sockaddr; |
| 729 | int fd, val, ret; |
| 730 | |
| 731 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 732 | if (fd < 0) { |
| 733 | perror("socket"); |
| 734 | return -1; |
| 735 | } |
| 736 | |
| 737 | /* allow fast reuse */ |
| 738 | val = 1; |
| 739 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
| 740 | |
| 741 | sockaddr.sin_family = AF_INET; |
| 742 | sockaddr.sin_port = htons(port); |
| 743 | sockaddr.sin_addr.s_addr = 0; |
| 744 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 745 | if (ret < 0) { |
| 746 | perror("bind"); |
| 747 | return -1; |
| 748 | } |
| 749 | ret = listen(fd, 0); |
| 750 | if (ret < 0) { |
| 751 | perror("listen"); |
| 752 | return -1; |
| 753 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 754 | #ifndef CONFIG_USER_ONLY |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 755 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 756 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 757 | return fd; |
| 758 | } |
| 759 | |
| 760 | int gdbserver_start(int port) |
| 761 | { |
| 762 | gdbserver_fd = gdbserver_open(port); |
| 763 | if (gdbserver_fd < 0) |
| 764 | return -1; |
| 765 | /* accept connections */ |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 766 | #ifdef CONFIG_USER_ONLY |
| 767 | gdb_accept (NULL, NULL, 0); |
| 768 | #else |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 769 | qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame^] | 770 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 771 | return 0; |
| 772 | } |