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