root / gdbstub.c @ 6191b059
History | View | Annotate | Download (38.5 kB)
1 | b4608c04 | bellard | /*
|
---|---|---|---|
2 | b4608c04 | bellard | * gdb server stub
|
3 | 5fafdf24 | ths | *
|
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 | 87ecb68b | pbrook | #include "qemu-common.h" |
33 | 87ecb68b | pbrook | #include "qemu-char.h" |
34 | 87ecb68b | pbrook | #include "sysemu.h" |
35 | 87ecb68b | pbrook | #include "gdbstub.h" |
36 | 1fddef4b | bellard | #endif
|
37 | 67b915a5 | bellard | |
38 | 8f447cc7 | bellard | #include "qemu_socket.h" |
39 | 8f447cc7 | bellard | #ifdef _WIN32
|
40 | 8f447cc7 | bellard | /* XXX: these constants may be independent of the host ones even for Unix */
|
41 | 8f447cc7 | bellard | #ifndef SIGTRAP
|
42 | 8f447cc7 | bellard | #define SIGTRAP 5 |
43 | 8f447cc7 | bellard | #endif
|
44 | 8f447cc7 | bellard | #ifndef SIGINT
|
45 | 8f447cc7 | bellard | #define SIGINT 2 |
46 | 8f447cc7 | bellard | #endif
|
47 | 8f447cc7 | bellard | #else
|
48 | b4608c04 | bellard | #include <signal.h> |
49 | 8f447cc7 | bellard | #endif
|
50 | b4608c04 | bellard | |
51 | 4abe615b | bellard | //#define DEBUG_GDB
|
52 | b4608c04 | bellard | |
53 | 858693c6 | bellard | enum RSState {
|
54 | 858693c6 | bellard | RS_IDLE, |
55 | 858693c6 | bellard | RS_GETLINE, |
56 | 858693c6 | bellard | RS_CHKSUM1, |
57 | 858693c6 | bellard | RS_CHKSUM2, |
58 | a2d1ebaf | pbrook | RS_SYSCALL, |
59 | 858693c6 | bellard | }; |
60 | 858693c6 | bellard | typedef struct GDBState { |
61 | 6a00d601 | bellard | CPUState *env; /* current CPU */
|
62 | 41625033 | bellard | enum RSState state; /* parsing state */ |
63 | 858693c6 | bellard | char line_buf[4096]; |
64 | 858693c6 | bellard | int line_buf_index;
|
65 | 858693c6 | bellard | int line_csum;
|
66 | 60fe76f3 | ths | uint8_t last_packet[4100];
|
67 | 4046d913 | pbrook | int last_packet_len;
|
68 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
69 | 4046d913 | pbrook | int fd;
|
70 | 41625033 | bellard | int running_state;
|
71 | 4046d913 | pbrook | #else
|
72 | 4046d913 | pbrook | CharDriverState *chr; |
73 | 41625033 | bellard | #endif
|
74 | 858693c6 | bellard | } GDBState; |
75 | b4608c04 | bellard | |
76 | 60897d36 | edgar_igl | /* By default use no IRQs and no timers while single stepping so as to
|
77 | 60897d36 | edgar_igl | * make single stepping like an ICE HW step.
|
78 | 60897d36 | edgar_igl | */
|
79 | 60897d36 | edgar_igl | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; |
80 | 60897d36 | edgar_igl | |
81 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
82 | 4046d913 | pbrook | /* XXX: This is not thread safe. Do we care? */
|
83 | 4046d913 | pbrook | static int gdbserver_fd = -1; |
84 | 4046d913 | pbrook | |
85 | 1fddef4b | bellard | /* XXX: remove this hack. */
|
86 | 1fddef4b | bellard | static GDBState gdbserver_state;
|
87 | 1fddef4b | bellard | |
88 | 858693c6 | bellard | static int get_char(GDBState *s) |
89 | b4608c04 | bellard | { |
90 | b4608c04 | bellard | uint8_t ch; |
91 | b4608c04 | bellard | int ret;
|
92 | b4608c04 | bellard | |
93 | b4608c04 | bellard | for(;;) {
|
94 | 8f447cc7 | bellard | ret = recv(s->fd, &ch, 1, 0); |
95 | b4608c04 | bellard | if (ret < 0) { |
96 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
97 | b4608c04 | bellard | return -1; |
98 | b4608c04 | bellard | } else if (ret == 0) { |
99 | b4608c04 | bellard | return -1; |
100 | b4608c04 | bellard | } else {
|
101 | b4608c04 | bellard | break;
|
102 | b4608c04 | bellard | } |
103 | b4608c04 | bellard | } |
104 | b4608c04 | bellard | return ch;
|
105 | b4608c04 | bellard | } |
106 | 4046d913 | pbrook | #endif
|
107 | b4608c04 | bellard | |
108 | a2d1ebaf | pbrook | /* GDB stub state for use by semihosting syscalls. */
|
109 | a2d1ebaf | pbrook | static GDBState *gdb_syscall_state;
|
110 | a2d1ebaf | pbrook | static gdb_syscall_complete_cb gdb_current_syscall_cb;
|
111 | a2d1ebaf | pbrook | |
112 | a2d1ebaf | pbrook | enum {
|
113 | a2d1ebaf | pbrook | GDB_SYS_UNKNOWN, |
114 | a2d1ebaf | pbrook | GDB_SYS_ENABLED, |
115 | a2d1ebaf | pbrook | GDB_SYS_DISABLED, |
116 | a2d1ebaf | pbrook | } gdb_syscall_mode; |
117 | a2d1ebaf | pbrook | |
118 | a2d1ebaf | pbrook | /* If gdb is connected when the first semihosting syscall occurs then use
|
119 | a2d1ebaf | pbrook | remote gdb syscalls. Otherwise use native file IO. */
|
120 | a2d1ebaf | pbrook | int use_gdb_syscalls(void) |
121 | a2d1ebaf | pbrook | { |
122 | a2d1ebaf | pbrook | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
|
123 | a2d1ebaf | pbrook | gdb_syscall_mode = (gdb_syscall_state ? GDB_SYS_ENABLED |
124 | a2d1ebaf | pbrook | : GDB_SYS_DISABLED); |
125 | a2d1ebaf | pbrook | } |
126 | a2d1ebaf | pbrook | return gdb_syscall_mode == GDB_SYS_ENABLED;
|
127 | a2d1ebaf | pbrook | } |
128 | a2d1ebaf | pbrook | |
129 | ba70a624 | edgar_igl | /* Resume execution. */
|
130 | ba70a624 | edgar_igl | static inline void gdb_continue(GDBState *s) |
131 | ba70a624 | edgar_igl | { |
132 | ba70a624 | edgar_igl | #ifdef CONFIG_USER_ONLY
|
133 | ba70a624 | edgar_igl | s->running_state = 1;
|
134 | ba70a624 | edgar_igl | #else
|
135 | ba70a624 | edgar_igl | vm_start(); |
136 | ba70a624 | edgar_igl | #endif
|
137 | ba70a624 | edgar_igl | } |
138 | ba70a624 | edgar_igl | |
139 | 858693c6 | bellard | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
140 | b4608c04 | bellard | { |
141 | 4046d913 | pbrook | #ifdef CONFIG_USER_ONLY
|
142 | b4608c04 | bellard | int ret;
|
143 | b4608c04 | bellard | |
144 | b4608c04 | bellard | while (len > 0) { |
145 | 8f447cc7 | bellard | ret = send(s->fd, buf, len, 0);
|
146 | b4608c04 | bellard | if (ret < 0) { |
147 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
148 | b4608c04 | bellard | return;
|
149 | b4608c04 | bellard | } else {
|
150 | b4608c04 | bellard | buf += ret; |
151 | b4608c04 | bellard | len -= ret; |
152 | b4608c04 | bellard | } |
153 | b4608c04 | bellard | } |
154 | 4046d913 | pbrook | #else
|
155 | 4046d913 | pbrook | qemu_chr_write(s->chr, buf, len); |
156 | 4046d913 | pbrook | #endif
|
157 | b4608c04 | bellard | } |
158 | b4608c04 | bellard | |
159 | b4608c04 | bellard | static inline int fromhex(int v) |
160 | b4608c04 | bellard | { |
161 | b4608c04 | bellard | if (v >= '0' && v <= '9') |
162 | b4608c04 | bellard | return v - '0'; |
163 | b4608c04 | bellard | else if (v >= 'A' && v <= 'F') |
164 | b4608c04 | bellard | return v - 'A' + 10; |
165 | b4608c04 | bellard | else if (v >= 'a' && v <= 'f') |
166 | b4608c04 | bellard | return v - 'a' + 10; |
167 | b4608c04 | bellard | else
|
168 | b4608c04 | bellard | return 0; |
169 | b4608c04 | bellard | } |
170 | b4608c04 | bellard | |
171 | b4608c04 | bellard | static inline int tohex(int v) |
172 | b4608c04 | bellard | { |
173 | b4608c04 | bellard | if (v < 10) |
174 | b4608c04 | bellard | return v + '0'; |
175 | b4608c04 | bellard | else
|
176 | b4608c04 | bellard | return v - 10 + 'a'; |
177 | b4608c04 | bellard | } |
178 | b4608c04 | bellard | |
179 | b4608c04 | bellard | static void memtohex(char *buf, const uint8_t *mem, int len) |
180 | b4608c04 | bellard | { |
181 | b4608c04 | bellard | int i, c;
|
182 | b4608c04 | bellard | char *q;
|
183 | b4608c04 | bellard | q = buf; |
184 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
185 | b4608c04 | bellard | c = mem[i]; |
186 | b4608c04 | bellard | *q++ = tohex(c >> 4);
|
187 | b4608c04 | bellard | *q++ = tohex(c & 0xf);
|
188 | b4608c04 | bellard | } |
189 | b4608c04 | bellard | *q = '\0';
|
190 | b4608c04 | bellard | } |
191 | b4608c04 | bellard | |
192 | b4608c04 | bellard | static void hextomem(uint8_t *mem, const char *buf, int len) |
193 | b4608c04 | bellard | { |
194 | b4608c04 | bellard | int i;
|
195 | b4608c04 | bellard | |
196 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
197 | b4608c04 | bellard | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
198 | b4608c04 | bellard | buf += 2;
|
199 | b4608c04 | bellard | } |
200 | b4608c04 | bellard | } |
201 | b4608c04 | bellard | |
202 | b4608c04 | bellard | /* return -1 if error, 0 if OK */
|
203 | 858693c6 | bellard | static int put_packet(GDBState *s, char *buf) |
204 | b4608c04 | bellard | { |
205 | 4046d913 | pbrook | int len, csum, i;
|
206 | 60fe76f3 | ths | uint8_t *p; |
207 | b4608c04 | bellard | |
208 | b4608c04 | bellard | #ifdef DEBUG_GDB
|
209 | b4608c04 | bellard | printf("reply='%s'\n", buf);
|
210 | b4608c04 | bellard | #endif
|
211 | b4608c04 | bellard | |
212 | b4608c04 | bellard | for(;;) {
|
213 | 4046d913 | pbrook | p = s->last_packet; |
214 | 4046d913 | pbrook | *(p++) = '$';
|
215 | b4608c04 | bellard | len = strlen(buf); |
216 | 4046d913 | pbrook | memcpy(p, buf, len); |
217 | 4046d913 | pbrook | p += len; |
218 | b4608c04 | bellard | csum = 0;
|
219 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
220 | b4608c04 | bellard | csum += buf[i]; |
221 | b4608c04 | bellard | } |
222 | 4046d913 | pbrook | *(p++) = '#';
|
223 | 4046d913 | pbrook | *(p++) = tohex((csum >> 4) & 0xf); |
224 | 4046d913 | pbrook | *(p++) = tohex((csum) & 0xf);
|
225 | b4608c04 | bellard | |
226 | 4046d913 | pbrook | s->last_packet_len = p - s->last_packet; |
227 | ffe8ab83 | ths | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
228 | b4608c04 | bellard | |
229 | 4046d913 | pbrook | #ifdef CONFIG_USER_ONLY
|
230 | 4046d913 | pbrook | i = get_char(s); |
231 | 4046d913 | pbrook | if (i < 0) |
232 | b4608c04 | bellard | return -1; |
233 | 4046d913 | pbrook | if (i == '+') |
234 | b4608c04 | bellard | break;
|
235 | 4046d913 | pbrook | #else
|
236 | 4046d913 | pbrook | break;
|
237 | 4046d913 | pbrook | #endif
|
238 | b4608c04 | bellard | } |
239 | b4608c04 | bellard | return 0; |
240 | b4608c04 | bellard | } |
241 | b4608c04 | bellard | |
242 | fde3fd61 | edgar_igl | #if defined(TARGET_I386)
|
243 | 79808573 | bellard | |
244 | 79808573 | bellard | #ifdef TARGET_X86_64
|
245 | 79808573 | bellard | static const uint8_t gdb_x86_64_regs[16] = { |
246 | 79808573 | bellard | R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP, |
247 | 79808573 | bellard | 8, 9, 10, 11, 12, 13, 14, 15, |
248 | 79808573 | bellard | }; |
249 | 79808573 | bellard | #endif
|
250 | 79808573 | bellard | |
251 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
252 | 6da41eaf | bellard | { |
253 | 79808573 | bellard | int i, fpus, nb_regs;
|
254 | 79808573 | bellard | uint8_t *p; |
255 | 5ad265ee | balrog | |
256 | 79808573 | bellard | p = mem_buf; |
257 | 5ad265ee | balrog | #ifdef TARGET_X86_64
|
258 | 5ad265ee | balrog | if (env->hflags & HF_CS64_MASK) {
|
259 | 79808573 | bellard | nb_regs = 16;
|
260 | 79808573 | bellard | for(i = 0; i < 16; i++) { |
261 | 79808573 | bellard | *(uint64_t *)p = tswap64(env->regs[gdb_x86_64_regs[i]]); |
262 | 79808573 | bellard | p += 8;
|
263 | 5ad265ee | balrog | } |
264 | 79808573 | bellard | *(uint64_t *)p = tswap64(env->eip); |
265 | 79808573 | bellard | p += 8;
|
266 | 79808573 | bellard | } else
|
267 | 79808573 | bellard | #endif
|
268 | 79808573 | bellard | { |
269 | 79808573 | bellard | nb_regs = 8;
|
270 | 5ad265ee | balrog | for(i = 0; i < 8; i++) { |
271 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->regs[i]); |
272 | 79808573 | bellard | p += 4;
|
273 | 5ad265ee | balrog | } |
274 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->eip); |
275 | 79808573 | bellard | p += 4;
|
276 | 5ad265ee | balrog | } |
277 | 6da41eaf | bellard | |
278 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->eflags); |
279 | 79808573 | bellard | p += 4;
|
280 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_CS].selector); |
281 | 79808573 | bellard | p += 4;
|
282 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_SS].selector); |
283 | 79808573 | bellard | p += 4;
|
284 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_DS].selector); |
285 | 79808573 | bellard | p += 4;
|
286 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_ES].selector); |
287 | 79808573 | bellard | p += 4;
|
288 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_FS].selector); |
289 | 79808573 | bellard | p += 4;
|
290 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->segs[R_GS].selector); |
291 | 79808573 | bellard | p += 4;
|
292 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
293 | 79808573 | bellard | /* XXX: convert floats */
|
294 | 79808573 | bellard | #ifdef USE_X86LDOUBLE
|
295 | 79808573 | bellard | memcpy(p, &env->fpregs[i], 10);
|
296 | 79808573 | bellard | #else
|
297 | 79808573 | bellard | memset(p, 0, 10); |
298 | 79808573 | bellard | #endif
|
299 | 79808573 | bellard | p += 10;
|
300 | 6da41eaf | bellard | } |
301 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->fpuc); /* fctrl */
|
302 | 79808573 | bellard | p += 4;
|
303 | 6da41eaf | bellard | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
304 | 79808573 | bellard | *(uint32_t *)p = tswap32(fpus); /* fstat */
|
305 | 79808573 | bellard | p += 4;
|
306 | 79808573 | bellard | *(uint32_t *)p = 0; /* ftag */ |
307 | 79808573 | bellard | p += 4;
|
308 | 79808573 | bellard | *(uint32_t *)p = 0; /* fiseg */ |
309 | 79808573 | bellard | p += 4;
|
310 | 79808573 | bellard | *(uint32_t *)p = 0; /* fioff */ |
311 | 79808573 | bellard | p += 4;
|
312 | 79808573 | bellard | *(uint32_t *)p = 0; /* foseg */ |
313 | 79808573 | bellard | p += 4;
|
314 | 79808573 | bellard | *(uint32_t *)p = 0; /* fooff */ |
315 | 79808573 | bellard | p += 4;
|
316 | 79808573 | bellard | *(uint32_t *)p = 0; /* fop */ |
317 | 79808573 | bellard | p += 4;
|
318 | 79808573 | bellard | for(i = 0; i < nb_regs; i++) { |
319 | 79808573 | bellard | *(uint64_t *)p = tswap64(env->xmm_regs[i].XMM_Q(0));
|
320 | 79808573 | bellard | p += 8;
|
321 | 79808573 | bellard | *(uint64_t *)p = tswap64(env->xmm_regs[i].XMM_Q(1));
|
322 | 79808573 | bellard | p += 8;
|
323 | 79808573 | bellard | } |
324 | 79808573 | bellard | *(uint32_t *)p = tswap32(env->mxcsr); |
325 | 79808573 | bellard | p += 4;
|
326 | 79808573 | bellard | return p - mem_buf;
|
327 | 79808573 | bellard | } |
328 | 79808573 | bellard | |
329 | 79808573 | bellard | static inline void cpu_gdb_load_seg(CPUState *env, const uint8_t **pp, |
330 | 79808573 | bellard | int sreg)
|
331 | 79808573 | bellard | { |
332 | 79808573 | bellard | const uint8_t *p;
|
333 | 79808573 | bellard | uint32_t sel; |
334 | 79808573 | bellard | p = *pp; |
335 | 79808573 | bellard | sel = tswap32(*(uint32_t *)p); |
336 | 79808573 | bellard | p += 4;
|
337 | 79808573 | bellard | if (sel != env->segs[sreg].selector) {
|
338 | 79808573 | bellard | #if defined(CONFIG_USER_ONLY)
|
339 | 79808573 | bellard | cpu_x86_load_seg(env, sreg, sel); |
340 | 79808573 | bellard | #else
|
341 | 79808573 | bellard | /* XXX: do it with a debug function which does not raise an
|
342 | 79808573 | bellard | exception */
|
343 | 79808573 | bellard | #endif
|
344 | 79808573 | bellard | } |
345 | 79808573 | bellard | *pp = p; |
346 | 6da41eaf | bellard | } |
347 | 6da41eaf | bellard | |
348 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
349 | 6da41eaf | bellard | { |
350 | 79808573 | bellard | const uint8_t *p = mem_buf;
|
351 | 79808573 | bellard | int i, nb_regs;
|
352 | 79808573 | bellard | uint16_t fpus; |
353 | 6da41eaf | bellard | |
354 | 79808573 | bellard | #ifdef TARGET_X86_64
|
355 | 79808573 | bellard | if (env->hflags & HF_CS64_MASK) {
|
356 | 79808573 | bellard | nb_regs = 16;
|
357 | 79808573 | bellard | for(i = 0; i < 16; i++) { |
358 | 79808573 | bellard | env->regs[gdb_x86_64_regs[i]] = tswap64(*(uint64_t *)p); |
359 | 79808573 | bellard | p += 8;
|
360 | 79808573 | bellard | } |
361 | 79808573 | bellard | env->eip = tswap64(*(uint64_t *)p); |
362 | 79808573 | bellard | p += 8;
|
363 | 79808573 | bellard | } else
|
364 | 79808573 | bellard | #endif
|
365 | 79808573 | bellard | { |
366 | 79808573 | bellard | nb_regs = 8;
|
367 | 79808573 | bellard | for(i = 0; i < 8; i++) { |
368 | 79808573 | bellard | env->regs[i] = tswap32(*(uint32_t *)p); |
369 | 79808573 | bellard | p += 4;
|
370 | 79808573 | bellard | } |
371 | 79808573 | bellard | env->eip = tswap32(*(uint32_t *)p); |
372 | 79808573 | bellard | p += 4;
|
373 | 6da41eaf | bellard | } |
374 | 79808573 | bellard | env->eflags = tswap32(*(uint32_t *)p); |
375 | 79808573 | bellard | p += 4;
|
376 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_CS); |
377 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_SS); |
378 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_DS); |
379 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_ES); |
380 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_FS); |
381 | 79808573 | bellard | cpu_gdb_load_seg(env, &p, R_GS); |
382 | 79808573 | bellard | |
383 | 79808573 | bellard | /* FPU state */
|
384 | 79808573 | bellard | for(i = 0; i < 8; i++) { |
385 | 79808573 | bellard | /* XXX: convert floats */
|
386 | 79808573 | bellard | #ifdef USE_X86LDOUBLE
|
387 | 79808573 | bellard | memcpy(&env->fpregs[i], p, 10);
|
388 | 6da41eaf | bellard | #endif
|
389 | 79808573 | bellard | p += 10;
|
390 | 79808573 | bellard | } |
391 | 79808573 | bellard | env->fpuc = tswap32(*(uint32_t *)p); /* fctrl */
|
392 | 79808573 | bellard | p += 4;
|
393 | 79808573 | bellard | fpus = tswap32(*(uint32_t *)p); |
394 | 79808573 | bellard | p += 4;
|
395 | 79808573 | bellard | env->fpstt = (fpus >> 11) & 7; |
396 | 79808573 | bellard | env->fpus = fpus & ~0x3800;
|
397 | 79808573 | bellard | p += 4 * 6; |
398 | 79808573 | bellard | |
399 | 79808573 | bellard | if (size >= ((p - mem_buf) + 16 * nb_regs + 4)) { |
400 | 79808573 | bellard | /* SSE state */
|
401 | 79808573 | bellard | for(i = 0; i < nb_regs; i++) { |
402 | 79808573 | bellard | env->xmm_regs[i].XMM_Q(0) = tswap64(*(uint64_t *)p);
|
403 | 79808573 | bellard | p += 8;
|
404 | 79808573 | bellard | env->xmm_regs[i].XMM_Q(1) = tswap64(*(uint64_t *)p);
|
405 | 79808573 | bellard | p += 8;
|
406 | 79808573 | bellard | } |
407 | 79808573 | bellard | env->mxcsr = tswap32(*(uint32_t *)p); |
408 | 79808573 | bellard | p += 4;
|
409 | 79808573 | bellard | } |
410 | 6da41eaf | bellard | } |
411 | 6da41eaf | bellard | |
412 | 9e62fd7f | bellard | #elif defined (TARGET_PPC)
|
413 | 9e62fd7f | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
414 | 9e62fd7f | bellard | { |
415 | a541f297 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
416 | 9e62fd7f | bellard | int i;
|
417 | 9e62fd7f | bellard | |
418 | 9e62fd7f | bellard | /* fill in gprs */
|
419 | a541f297 | bellard | for(i = 0; i < 32; i++) { |
420 | e95c8d51 | bellard | registers[i] = tswapl(env->gpr[i]); |
421 | 9e62fd7f | bellard | } |
422 | 9e62fd7f | bellard | /* fill in fprs */
|
423 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
424 | e95c8d51 | bellard | registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
425 | e95c8d51 | bellard | registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
426 | 9e62fd7f | bellard | } |
427 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
428 | e95c8d51 | bellard | registers[96] = tswapl(env->nip);
|
429 | 0411a972 | j_mayer | registers[97] = tswapl(env->msr);
|
430 | 9e62fd7f | bellard | tmp = 0;
|
431 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
432 | a541f297 | bellard | tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
433 | e95c8d51 | bellard | registers[98] = tswapl(tmp);
|
434 | e95c8d51 | bellard | registers[99] = tswapl(env->lr);
|
435 | e95c8d51 | bellard | registers[100] = tswapl(env->ctr);
|
436 | 76a66253 | j_mayer | registers[101] = tswapl(ppc_load_xer(env));
|
437 | e95c8d51 | bellard | registers[102] = 0; |
438 | a541f297 | bellard | |
439 | a541f297 | bellard | return 103 * 4; |
440 | 9e62fd7f | bellard | } |
441 | 9e62fd7f | bellard | |
442 | 9e62fd7f | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
443 | 9e62fd7f | bellard | { |
444 | 9e62fd7f | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
445 | 9e62fd7f | bellard | int i;
|
446 | 9e62fd7f | bellard | |
447 | 9e62fd7f | bellard | /* fill in gprs */
|
448 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
449 | e95c8d51 | bellard | env->gpr[i] = tswapl(registers[i]); |
450 | 9e62fd7f | bellard | } |
451 | 9e62fd7f | bellard | /* fill in fprs */
|
452 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
453 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
454 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
455 | 9e62fd7f | bellard | } |
456 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
457 | e95c8d51 | bellard | env->nip = tswapl(registers[96]);
|
458 | 0411a972 | j_mayer | ppc_store_msr(env, tswapl(registers[97]));
|
459 | e95c8d51 | bellard | registers[98] = tswapl(registers[98]); |
460 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
461 | a541f297 | bellard | env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
462 | e95c8d51 | bellard | env->lr = tswapl(registers[99]);
|
463 | e95c8d51 | bellard | env->ctr = tswapl(registers[100]);
|
464 | 76a66253 | j_mayer | ppc_store_xer(env, tswapl(registers[101]));
|
465 | e95c8d51 | bellard | } |
466 | e95c8d51 | bellard | #elif defined (TARGET_SPARC)
|
467 | e95c8d51 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
468 | e95c8d51 | bellard | { |
469 | 3475187d | bellard | target_ulong *registers = (target_ulong *)mem_buf; |
470 | e95c8d51 | bellard | int i;
|
471 | e95c8d51 | bellard | |
472 | e95c8d51 | bellard | /* fill in g0..g7 */
|
473 | 48b2c193 | bellard | for(i = 0; i < 8; i++) { |
474 | e95c8d51 | bellard | registers[i] = tswapl(env->gregs[i]); |
475 | e95c8d51 | bellard | } |
476 | e95c8d51 | bellard | /* fill in register window */
|
477 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
478 | e95c8d51 | bellard | registers[i + 8] = tswapl(env->regwptr[i]);
|
479 | e95c8d51 | bellard | } |
480 | 9d9754a3 | bellard | #ifndef TARGET_SPARC64
|
481 | e95c8d51 | bellard | /* fill in fprs */
|
482 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
483 | e95c8d51 | bellard | registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
484 | e95c8d51 | bellard | } |
485 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
486 | e95c8d51 | bellard | registers[64] = tswapl(env->y);
|
487 | 3475187d | bellard | { |
488 | 3475187d | bellard | target_ulong tmp; |
489 | 3475187d | bellard | |
490 | 3475187d | bellard | tmp = GET_PSR(env); |
491 | 3475187d | bellard | registers[65] = tswapl(tmp);
|
492 | 3475187d | bellard | } |
493 | e95c8d51 | bellard | registers[66] = tswapl(env->wim);
|
494 | e95c8d51 | bellard | registers[67] = tswapl(env->tbr);
|
495 | e95c8d51 | bellard | registers[68] = tswapl(env->pc);
|
496 | e95c8d51 | bellard | registers[69] = tswapl(env->npc);
|
497 | e95c8d51 | bellard | registers[70] = tswapl(env->fsr);
|
498 | e95c8d51 | bellard | registers[71] = 0; /* csr */ |
499 | e95c8d51 | bellard | registers[72] = 0; |
500 | 3475187d | bellard | return 73 * sizeof(target_ulong); |
501 | 3475187d | bellard | #else
|
502 | 9d9754a3 | bellard | /* fill in fprs */
|
503 | 9d9754a3 | bellard | for (i = 0; i < 64; i += 2) { |
504 | 9d9754a3 | bellard | uint64_t tmp; |
505 | 9d9754a3 | bellard | |
506 | 8979596d | blueswir1 | tmp = ((uint64_t)*(uint32_t *)&env->fpr[i]) << 32;
|
507 | 8979596d | blueswir1 | tmp |= *(uint32_t *)&env->fpr[i + 1];
|
508 | 8979596d | blueswir1 | registers[i / 2 + 32] = tswap64(tmp); |
509 | 3475187d | bellard | } |
510 | 9d9754a3 | bellard | registers[64] = tswapl(env->pc);
|
511 | 9d9754a3 | bellard | registers[65] = tswapl(env->npc);
|
512 | 17d996e1 | blueswir1 | registers[66] = tswapl(((uint64_t)GET_CCR(env) << 32) | |
513 | 17d996e1 | blueswir1 | ((env->asi & 0xff) << 24) | |
514 | 17d996e1 | blueswir1 | ((env->pstate & 0xfff) << 8) | |
515 | 17d996e1 | blueswir1 | GET_CWP64(env)); |
516 | 9d9754a3 | bellard | registers[67] = tswapl(env->fsr);
|
517 | 9d9754a3 | bellard | registers[68] = tswapl(env->fprs);
|
518 | 9d9754a3 | bellard | registers[69] = tswapl(env->y);
|
519 | 9d9754a3 | bellard | return 70 * sizeof(target_ulong); |
520 | 3475187d | bellard | #endif
|
521 | e95c8d51 | bellard | } |
522 | e95c8d51 | bellard | |
523 | e95c8d51 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
524 | e95c8d51 | bellard | { |
525 | 3475187d | bellard | target_ulong *registers = (target_ulong *)mem_buf; |
526 | e95c8d51 | bellard | int i;
|
527 | e95c8d51 | bellard | |
528 | e95c8d51 | bellard | /* fill in g0..g7 */
|
529 | e95c8d51 | bellard | for(i = 0; i < 7; i++) { |
530 | e95c8d51 | bellard | env->gregs[i] = tswapl(registers[i]); |
531 | e95c8d51 | bellard | } |
532 | e95c8d51 | bellard | /* fill in register window */
|
533 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
534 | 3475187d | bellard | env->regwptr[i] = tswapl(registers[i + 8]);
|
535 | e95c8d51 | bellard | } |
536 | 9d9754a3 | bellard | #ifndef TARGET_SPARC64
|
537 | e95c8d51 | bellard | /* fill in fprs */
|
538 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
539 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
540 | e95c8d51 | bellard | } |
541 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
542 | e95c8d51 | bellard | env->y = tswapl(registers[64]);
|
543 | e80cfcfc | bellard | PUT_PSR(env, tswapl(registers[65]));
|
544 | e95c8d51 | bellard | env->wim = tswapl(registers[66]);
|
545 | e95c8d51 | bellard | env->tbr = tswapl(registers[67]);
|
546 | e95c8d51 | bellard | env->pc = tswapl(registers[68]);
|
547 | e95c8d51 | bellard | env->npc = tswapl(registers[69]);
|
548 | e95c8d51 | bellard | env->fsr = tswapl(registers[70]);
|
549 | 3475187d | bellard | #else
|
550 | 9d9754a3 | bellard | for (i = 0; i < 64; i += 2) { |
551 | 8979596d | blueswir1 | uint64_t tmp; |
552 | 8979596d | blueswir1 | |
553 | 8979596d | blueswir1 | tmp = tswap64(registers[i / 2 + 32]); |
554 | 8979596d | blueswir1 | *((uint32_t *)&env->fpr[i]) = tmp >> 32;
|
555 | 8979596d | blueswir1 | *((uint32_t *)&env->fpr[i + 1]) = tmp & 0xffffffff; |
556 | 3475187d | bellard | } |
557 | 9d9754a3 | bellard | env->pc = tswapl(registers[64]);
|
558 | 9d9754a3 | bellard | env->npc = tswapl(registers[65]);
|
559 | 17d996e1 | blueswir1 | { |
560 | 17d996e1 | blueswir1 | uint64_t tmp = tswapl(registers[66]);
|
561 | 17d996e1 | blueswir1 | |
562 | 17d996e1 | blueswir1 | PUT_CCR(env, tmp >> 32);
|
563 | 17d996e1 | blueswir1 | env->asi = (tmp >> 24) & 0xff; |
564 | 17d996e1 | blueswir1 | env->pstate = (tmp >> 8) & 0xfff; |
565 | 17d996e1 | blueswir1 | PUT_CWP64(env, tmp & 0xff);
|
566 | 17d996e1 | blueswir1 | } |
567 | 9d9754a3 | bellard | env->fsr = tswapl(registers[67]);
|
568 | 9d9754a3 | bellard | env->fprs = tswapl(registers[68]);
|
569 | 9d9754a3 | bellard | env->y = tswapl(registers[69]);
|
570 | 3475187d | bellard | #endif
|
571 | 9e62fd7f | bellard | } |
572 | 1fddef4b | bellard | #elif defined (TARGET_ARM)
|
573 | 1fddef4b | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
574 | 1fddef4b | bellard | { |
575 | 1fddef4b | bellard | int i;
|
576 | 1fddef4b | bellard | uint8_t *ptr; |
577 | 1fddef4b | bellard | |
578 | 1fddef4b | bellard | ptr = mem_buf; |
579 | 1fddef4b | bellard | /* 16 core integer registers (4 bytes each). */
|
580 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
581 | 1fddef4b | bellard | { |
582 | 1fddef4b | bellard | *(uint32_t *)ptr = tswapl(env->regs[i]); |
583 | 1fddef4b | bellard | ptr += 4;
|
584 | 1fddef4b | bellard | } |
585 | 1fddef4b | bellard | /* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
586 | 1fddef4b | bellard | Not yet implemented. */
|
587 | 1fddef4b | bellard | memset (ptr, 0, 8 * 12 + 4); |
588 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
589 | 1fddef4b | bellard | /* CPSR (4 bytes). */
|
590 | b5ff1b31 | bellard | *(uint32_t *)ptr = tswapl (cpsr_read(env)); |
591 | 1fddef4b | bellard | ptr += 4;
|
592 | 1fddef4b | bellard | |
593 | 1fddef4b | bellard | return ptr - mem_buf;
|
594 | 1fddef4b | bellard | } |
595 | 6da41eaf | bellard | |
596 | 1fddef4b | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
597 | 1fddef4b | bellard | { |
598 | 1fddef4b | bellard | int i;
|
599 | 1fddef4b | bellard | uint8_t *ptr; |
600 | 1fddef4b | bellard | |
601 | 1fddef4b | bellard | ptr = mem_buf; |
602 | 1fddef4b | bellard | /* Core integer registers. */
|
603 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
604 | 1fddef4b | bellard | { |
605 | 1fddef4b | bellard | env->regs[i] = tswapl(*(uint32_t *)ptr); |
606 | 1fddef4b | bellard | ptr += 4;
|
607 | 1fddef4b | bellard | } |
608 | 1fddef4b | bellard | /* Ignore FPA regs and scr. */
|
609 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
610 | b5ff1b31 | bellard | cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
|
611 | 1fddef4b | bellard | } |
612 | e6e5906b | pbrook | #elif defined (TARGET_M68K)
|
613 | e6e5906b | pbrook | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
614 | e6e5906b | pbrook | { |
615 | e6e5906b | pbrook | int i;
|
616 | e6e5906b | pbrook | uint8_t *ptr; |
617 | e6e5906b | pbrook | CPU_DoubleU u; |
618 | e6e5906b | pbrook | |
619 | e6e5906b | pbrook | ptr = mem_buf; |
620 | e6e5906b | pbrook | /* D0-D7 */
|
621 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
622 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->dregs[i]); |
623 | e6e5906b | pbrook | ptr += 4;
|
624 | e6e5906b | pbrook | } |
625 | e6e5906b | pbrook | /* A0-A7 */
|
626 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
627 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->aregs[i]); |
628 | e6e5906b | pbrook | ptr += 4;
|
629 | e6e5906b | pbrook | } |
630 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->sr); |
631 | e6e5906b | pbrook | ptr += 4;
|
632 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->pc); |
633 | e6e5906b | pbrook | ptr += 4;
|
634 | e6e5906b | pbrook | /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
635 | e6e5906b | pbrook | ColdFire has 8-bit double precision registers. */
|
636 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
637 | e6e5906b | pbrook | u.d = env->fregs[i]; |
638 | e6e5906b | pbrook | *(uint32_t *)ptr = tswap32(u.l.upper); |
639 | e6e5906b | pbrook | *(uint32_t *)ptr = tswap32(u.l.lower); |
640 | e6e5906b | pbrook | } |
641 | e6e5906b | pbrook | /* FP control regs (not implemented). */
|
642 | e6e5906b | pbrook | memset (ptr, 0, 3 * 4); |
643 | e6e5906b | pbrook | ptr += 3 * 4; |
644 | e6e5906b | pbrook | |
645 | e6e5906b | pbrook | return ptr - mem_buf;
|
646 | e6e5906b | pbrook | } |
647 | e6e5906b | pbrook | |
648 | e6e5906b | pbrook | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
649 | e6e5906b | pbrook | { |
650 | e6e5906b | pbrook | int i;
|
651 | e6e5906b | pbrook | uint8_t *ptr; |
652 | e6e5906b | pbrook | CPU_DoubleU u; |
653 | e6e5906b | pbrook | |
654 | e6e5906b | pbrook | ptr = mem_buf; |
655 | e6e5906b | pbrook | /* D0-D7 */
|
656 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
657 | e6e5906b | pbrook | env->dregs[i] = tswapl(*(uint32_t *)ptr); |
658 | e6e5906b | pbrook | ptr += 4;
|
659 | e6e5906b | pbrook | } |
660 | e6e5906b | pbrook | /* A0-A7 */
|
661 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
662 | e6e5906b | pbrook | env->aregs[i] = tswapl(*(uint32_t *)ptr); |
663 | e6e5906b | pbrook | ptr += 4;
|
664 | e6e5906b | pbrook | } |
665 | e6e5906b | pbrook | env->sr = tswapl(*(uint32_t *)ptr); |
666 | e6e5906b | pbrook | ptr += 4;
|
667 | e6e5906b | pbrook | env->pc = tswapl(*(uint32_t *)ptr); |
668 | e6e5906b | pbrook | ptr += 4;
|
669 | e6e5906b | pbrook | /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
670 | e6e5906b | pbrook | ColdFire has 8-bit double precision registers. */
|
671 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
672 | 5fafdf24 | ths | u.l.upper = tswap32(*(uint32_t *)ptr); |
673 | e6e5906b | pbrook | u.l.lower = tswap32(*(uint32_t *)ptr); |
674 | e6e5906b | pbrook | env->fregs[i] = u.d; |
675 | e6e5906b | pbrook | } |
676 | e6e5906b | pbrook | /* FP control regs (not implemented). */
|
677 | e6e5906b | pbrook | ptr += 3 * 4; |
678 | e6e5906b | pbrook | } |
679 | 6f970bd9 | bellard | #elif defined (TARGET_MIPS)
|
680 | 6f970bd9 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
681 | 6f970bd9 | bellard | { |
682 | 6f970bd9 | bellard | int i;
|
683 | 6f970bd9 | bellard | uint8_t *ptr; |
684 | 6f970bd9 | bellard | |
685 | 6f970bd9 | bellard | ptr = mem_buf; |
686 | 6f970bd9 | bellard | for (i = 0; i < 32; i++) |
687 | 6f970bd9 | bellard | { |
688 | d0dc7dc3 | ths | *(target_ulong *)ptr = tswapl(env->gpr[env->current_tc][i]); |
689 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
690 | 6f970bd9 | bellard | } |
691 | 6f970bd9 | bellard | |
692 | 7ac256b8 | ths | *(target_ulong *)ptr = (int32_t)tswap32(env->CP0_Status); |
693 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
694 | 6f970bd9 | bellard | |
695 | d0dc7dc3 | ths | *(target_ulong *)ptr = tswapl(env->LO[env->current_tc][0]);
|
696 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
697 | 6f970bd9 | bellard | |
698 | d0dc7dc3 | ths | *(target_ulong *)ptr = tswapl(env->HI[env->current_tc][0]);
|
699 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
700 | 6f970bd9 | bellard | |
701 | 2052caa7 | ths | *(target_ulong *)ptr = tswapl(env->CP0_BadVAddr); |
702 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
703 | 6f970bd9 | bellard | |
704 | 7ac256b8 | ths | *(target_ulong *)ptr = (int32_t)tswap32(env->CP0_Cause); |
705 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
706 | 6f970bd9 | bellard | |
707 | ead9360e | ths | *(target_ulong *)ptr = tswapl(env->PC[env->current_tc]); |
708 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
709 | 6f970bd9 | bellard | |
710 | 36d23958 | ths | if (env->CP0_Config1 & (1 << CP0C1_FP)) |
711 | 8e33c08c | ths | { |
712 | 36d23958 | ths | for (i = 0; i < 32; i++) |
713 | 36d23958 | ths | { |
714 | 7ac256b8 | ths | if (env->CP0_Status & (1 << CP0St_FR)) |
715 | 7ac256b8 | ths | *(target_ulong *)ptr = tswapl(env->fpu->fpr[i].d); |
716 | 7ac256b8 | ths | else
|
717 | 7ac256b8 | ths | *(target_ulong *)ptr = tswap32(env->fpu->fpr[i].w[FP_ENDIAN_IDX]); |
718 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
719 | 36d23958 | ths | } |
720 | 8e33c08c | ths | |
721 | 7ac256b8 | ths | *(target_ulong *)ptr = (int32_t)tswap32(env->fpu->fcr31); |
722 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
723 | 8e33c08c | ths | |
724 | 7ac256b8 | ths | *(target_ulong *)ptr = (int32_t)tswap32(env->fpu->fcr0); |
725 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
726 | 36d23958 | ths | } |
727 | 8e33c08c | ths | |
728 | 7ac256b8 | ths | /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
|
729 | 7ac256b8 | ths | *(target_ulong *)ptr = 0;
|
730 | 7ac256b8 | ths | ptr += sizeof(target_ulong);
|
731 | 7ac256b8 | ths | |
732 | 7ac256b8 | ths | /* Registers for embedded use, we just pad them. */
|
733 | 7ac256b8 | ths | for (i = 0; i < 16; i++) |
734 | 7ac256b8 | ths | { |
735 | 7ac256b8 | ths | *(target_ulong *)ptr = 0;
|
736 | 7ac256b8 | ths | ptr += sizeof(target_ulong);
|
737 | 7ac256b8 | ths | } |
738 | 7ac256b8 | ths | |
739 | 7ac256b8 | ths | /* Processor ID. */
|
740 | 7ac256b8 | ths | *(target_ulong *)ptr = (int32_t)tswap32(env->CP0_PRid); |
741 | 7ac256b8 | ths | ptr += sizeof(target_ulong);
|
742 | 6f970bd9 | bellard | |
743 | 6f970bd9 | bellard | return ptr - mem_buf;
|
744 | 6f970bd9 | bellard | } |
745 | 6f970bd9 | bellard | |
746 | 8e33c08c | ths | /* convert MIPS rounding mode in FCR31 to IEEE library */
|
747 | 8e33c08c | ths | static unsigned int ieee_rm[] = |
748 | 8e33c08c | ths | { |
749 | 8e33c08c | ths | float_round_nearest_even, |
750 | 8e33c08c | ths | float_round_to_zero, |
751 | 8e33c08c | ths | float_round_up, |
752 | 8e33c08c | ths | float_round_down |
753 | 8e33c08c | ths | }; |
754 | 8e33c08c | ths | #define RESTORE_ROUNDING_MODE \
|
755 | ead9360e | ths | set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
|
756 | 8e33c08c | ths | |
757 | 6f970bd9 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
758 | 6f970bd9 | bellard | { |
759 | 6f970bd9 | bellard | int i;
|
760 | 6f970bd9 | bellard | uint8_t *ptr; |
761 | 6f970bd9 | bellard | |
762 | 6f970bd9 | bellard | ptr = mem_buf; |
763 | 6f970bd9 | bellard | for (i = 0; i < 32; i++) |
764 | 6f970bd9 | bellard | { |
765 | d0dc7dc3 | ths | env->gpr[env->current_tc][i] = tswapl(*(target_ulong *)ptr); |
766 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
767 | 6f970bd9 | bellard | } |
768 | 6f970bd9 | bellard | |
769 | 2052caa7 | ths | env->CP0_Status = tswapl(*(target_ulong *)ptr); |
770 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
771 | 6f970bd9 | bellard | |
772 | d0dc7dc3 | ths | env->LO[env->current_tc][0] = tswapl(*(target_ulong *)ptr);
|
773 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
774 | 6f970bd9 | bellard | |
775 | d0dc7dc3 | ths | env->HI[env->current_tc][0] = tswapl(*(target_ulong *)ptr);
|
776 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
777 | 6f970bd9 | bellard | |
778 | 2052caa7 | ths | env->CP0_BadVAddr = tswapl(*(target_ulong *)ptr); |
779 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
780 | 6f970bd9 | bellard | |
781 | 2052caa7 | ths | env->CP0_Cause = tswapl(*(target_ulong *)ptr); |
782 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
783 | 6f970bd9 | bellard | |
784 | ead9360e | ths | env->PC[env->current_tc] = tswapl(*(target_ulong *)ptr); |
785 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
786 | 8e33c08c | ths | |
787 | 36d23958 | ths | if (env->CP0_Config1 & (1 << CP0C1_FP)) |
788 | 8e33c08c | ths | { |
789 | 36d23958 | ths | for (i = 0; i < 32; i++) |
790 | 36d23958 | ths | { |
791 | 7ac256b8 | ths | if (env->CP0_Status & (1 << CP0St_FR)) |
792 | 7ac256b8 | ths | env->fpu->fpr[i].d = tswapl(*(target_ulong *)ptr); |
793 | 7ac256b8 | ths | else
|
794 | 7ac256b8 | ths | env->fpu->fpr[i].w[FP_ENDIAN_IDX] = tswapl(*(target_ulong *)ptr); |
795 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
796 | 36d23958 | ths | } |
797 | 8e33c08c | ths | |
798 | 7ac256b8 | ths | env->fpu->fcr31 = tswapl(*(target_ulong *)ptr) & 0xFF83FFFF;
|
799 | 2052caa7 | ths | ptr += sizeof(target_ulong);
|
800 | 8e33c08c | ths | |
801 | 7ac256b8 | ths | /* The remaining registers are assumed to be read-only. */
|
802 | 8e33c08c | ths | |
803 | 36d23958 | ths | /* set rounding mode */
|
804 | 36d23958 | ths | RESTORE_ROUNDING_MODE; |
805 | 8e33c08c | ths | |
806 | 8e33c08c | ths | #ifndef CONFIG_SOFTFLOAT
|
807 | 36d23958 | ths | /* no floating point exception for native float */
|
808 | 36d23958 | ths | SET_FP_ENABLE(env->fcr31, 0);
|
809 | 8e33c08c | ths | #endif
|
810 | 36d23958 | ths | } |
811 | 6f970bd9 | bellard | } |
812 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
813 | 6ef99fc5 | ths | |
814 | 6ef99fc5 | ths | /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
|
815 | 6ef99fc5 | ths | |
816 | fdf9b3e8 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
817 | fdf9b3e8 | bellard | { |
818 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
819 | fdf9b3e8 | bellard | int i;
|
820 | fdf9b3e8 | bellard | |
821 | fdf9b3e8 | bellard | #define SAVE(x) *ptr++=tswapl(x)
|
822 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
823 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]); |
824 | 9c2a9ea1 | pbrook | } else {
|
825 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i]); |
826 | 9c2a9ea1 | pbrook | } |
827 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) SAVE(env->gregs[i]); |
828 | fdf9b3e8 | bellard | SAVE (env->pc); |
829 | fdf9b3e8 | bellard | SAVE (env->pr); |
830 | fdf9b3e8 | bellard | SAVE (env->gbr); |
831 | fdf9b3e8 | bellard | SAVE (env->vbr); |
832 | fdf9b3e8 | bellard | SAVE (env->mach); |
833 | fdf9b3e8 | bellard | SAVE (env->macl); |
834 | fdf9b3e8 | bellard | SAVE (env->sr); |
835 | 6ef99fc5 | ths | SAVE (env->fpul); |
836 | 6ef99fc5 | ths | SAVE (env->fpscr); |
837 | 6ef99fc5 | ths | for (i = 0; i < 16; i++) |
838 | 6ef99fc5 | ths | SAVE(env->fregs[i + ((env->fpscr & FPSCR_FR) ? 16 : 0)]); |
839 | 6ef99fc5 | ths | SAVE (env->ssr); |
840 | 6ef99fc5 | ths | SAVE (env->spc); |
841 | 6ef99fc5 | ths | for (i = 0; i < 8; i++) SAVE(env->gregs[i]); |
842 | 6ef99fc5 | ths | for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]); |
843 | fdf9b3e8 | bellard | return ((uint8_t *)ptr - mem_buf);
|
844 | fdf9b3e8 | bellard | } |
845 | fdf9b3e8 | bellard | |
846 | fdf9b3e8 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
847 | fdf9b3e8 | bellard | { |
848 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
849 | fdf9b3e8 | bellard | int i;
|
850 | fdf9b3e8 | bellard | |
851 | fdf9b3e8 | bellard | #define LOAD(x) (x)=*ptr++;
|
852 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
853 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]); |
854 | 9c2a9ea1 | pbrook | } else {
|
855 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i]); |
856 | 9c2a9ea1 | pbrook | } |
857 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) LOAD(env->gregs[i]); |
858 | fdf9b3e8 | bellard | LOAD (env->pc); |
859 | fdf9b3e8 | bellard | LOAD (env->pr); |
860 | fdf9b3e8 | bellard | LOAD (env->gbr); |
861 | fdf9b3e8 | bellard | LOAD (env->vbr); |
862 | fdf9b3e8 | bellard | LOAD (env->mach); |
863 | fdf9b3e8 | bellard | LOAD (env->macl); |
864 | fdf9b3e8 | bellard | LOAD (env->sr); |
865 | 6ef99fc5 | ths | LOAD (env->fpul); |
866 | 6ef99fc5 | ths | LOAD (env->fpscr); |
867 | 6ef99fc5 | ths | for (i = 0; i < 16; i++) |
868 | 6ef99fc5 | ths | LOAD(env->fregs[i + ((env->fpscr & FPSCR_FR) ? 16 : 0)]); |
869 | 6ef99fc5 | ths | LOAD (env->ssr); |
870 | 6ef99fc5 | ths | LOAD (env->spc); |
871 | 6ef99fc5 | ths | for (i = 0; i < 8; i++) LOAD(env->gregs[i]); |
872 | 6ef99fc5 | ths | for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]); |
873 | fdf9b3e8 | bellard | } |
874 | f1ccf904 | ths | #elif defined (TARGET_CRIS)
|
875 | f1ccf904 | ths | |
876 | f1ccf904 | ths | static int cris_save_32 (unsigned char *d, uint32_t value) |
877 | f1ccf904 | ths | { |
878 | f1ccf904 | ths | *d++ = (value); |
879 | f1ccf904 | ths | *d++ = (value >>= 8);
|
880 | f1ccf904 | ths | *d++ = (value >>= 8);
|
881 | f1ccf904 | ths | *d++ = (value >>= 8);
|
882 | f1ccf904 | ths | return 4; |
883 | f1ccf904 | ths | } |
884 | f1ccf904 | ths | static int cris_save_16 (unsigned char *d, uint32_t value) |
885 | f1ccf904 | ths | { |
886 | f1ccf904 | ths | *d++ = (value); |
887 | f1ccf904 | ths | *d++ = (value >>= 8);
|
888 | f1ccf904 | ths | return 2; |
889 | f1ccf904 | ths | } |
890 | f1ccf904 | ths | static int cris_save_8 (unsigned char *d, uint32_t value) |
891 | f1ccf904 | ths | { |
892 | f1ccf904 | ths | *d++ = (value); |
893 | f1ccf904 | ths | return 1; |
894 | f1ccf904 | ths | } |
895 | f1ccf904 | ths | |
896 | f1ccf904 | ths | /* FIXME: this will bug on archs not supporting unaligned word accesses. */
|
897 | f1ccf904 | ths | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
898 | f1ccf904 | ths | { |
899 | f1ccf904 | ths | uint8_t *ptr = mem_buf; |
900 | f1ccf904 | ths | uint8_t srs; |
901 | f1ccf904 | ths | int i;
|
902 | f1ccf904 | ths | |
903 | f1ccf904 | ths | for (i = 0; i < 16; i++) |
904 | f1ccf904 | ths | ptr += cris_save_32 (ptr, env->regs[i]); |
905 | f1ccf904 | ths | |
906 | 9004627f | edgar_igl | srs = env->pregs[PR_SRS]; |
907 | f1ccf904 | ths | |
908 | f1ccf904 | ths | ptr += cris_save_8 (ptr, env->pregs[0]);
|
909 | f1ccf904 | ths | ptr += cris_save_8 (ptr, env->pregs[1]);
|
910 | f1ccf904 | ths | ptr += cris_save_32 (ptr, env->pregs[2]);
|
911 | f1ccf904 | ths | ptr += cris_save_8 (ptr, srs); |
912 | f1ccf904 | ths | ptr += cris_save_16 (ptr, env->pregs[4]);
|
913 | f1ccf904 | ths | |
914 | f1ccf904 | ths | for (i = 5; i < 16; i++) |
915 | f1ccf904 | ths | ptr += cris_save_32 (ptr, env->pregs[i]); |
916 | f1ccf904 | ths | |
917 | f1ccf904 | ths | ptr += cris_save_32 (ptr, env->pc); |
918 | f1ccf904 | ths | |
919 | f1ccf904 | ths | for (i = 0; i < 16; i++) |
920 | f1ccf904 | ths | ptr += cris_save_32 (ptr, env->sregs[srs][i]); |
921 | f1ccf904 | ths | |
922 | f1ccf904 | ths | return ((uint8_t *)ptr - mem_buf);
|
923 | f1ccf904 | ths | } |
924 | f1ccf904 | ths | |
925 | f1ccf904 | ths | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
926 | f1ccf904 | ths | { |
927 | f1ccf904 | ths | uint32_t *ptr = (uint32_t *)mem_buf; |
928 | f1ccf904 | ths | int i;
|
929 | f1ccf904 | ths | |
930 | f1ccf904 | ths | #define LOAD(x) (x)=*ptr++;
|
931 | f1ccf904 | ths | for (i = 0; i < 16; i++) LOAD(env->regs[i]); |
932 | f1ccf904 | ths | LOAD (env->pc); |
933 | f1ccf904 | ths | } |
934 | 1fddef4b | bellard | #else
|
935 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
936 | 6da41eaf | bellard | { |
937 | 6da41eaf | bellard | return 0; |
938 | 6da41eaf | bellard | } |
939 | 6da41eaf | bellard | |
940 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
941 | 6da41eaf | bellard | { |
942 | 6da41eaf | bellard | } |
943 | 6da41eaf | bellard | |
944 | 6da41eaf | bellard | #endif
|
945 | b4608c04 | bellard | |
946 | 1fddef4b | bellard | static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
947 | b4608c04 | bellard | { |
948 | b4608c04 | bellard | const char *p; |
949 | 858693c6 | bellard | int ch, reg_size, type;
|
950 | b4608c04 | bellard | char buf[4096]; |
951 | f1ccf904 | ths | uint8_t mem_buf[4096];
|
952 | b4608c04 | bellard | uint32_t *registers; |
953 | 9d9754a3 | bellard | target_ulong addr, len; |
954 | 3b46e624 | ths | |
955 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
956 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
957 | 858693c6 | bellard | #endif
|
958 | 858693c6 | bellard | p = line_buf; |
959 | 858693c6 | bellard | ch = *p++; |
960 | 858693c6 | bellard | switch(ch) {
|
961 | 858693c6 | bellard | case '?': |
962 | 1fddef4b | bellard | /* TODO: Make this return the correct value for user-mode. */
|
963 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
964 | 858693c6 | bellard | put_packet(s, buf); |
965 | 858693c6 | bellard | break;
|
966 | 858693c6 | bellard | case 'c': |
967 | 858693c6 | bellard | if (*p != '\0') { |
968 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
969 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
970 | 858693c6 | bellard | env->eip = addr; |
971 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
972 | 858693c6 | bellard | env->nip = addr; |
973 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
974 | 8d5f07fa | bellard | env->pc = addr; |
975 | 8d5f07fa | bellard | env->npc = addr + 4;
|
976 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
977 | b5ff1b31 | bellard | env->regs[15] = addr;
|
978 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
979 | 8fac5803 | ths | env->pc = addr; |
980 | 8fac5803 | ths | #elif defined (TARGET_MIPS)
|
981 | ead9360e | ths | env->PC[env->current_tc] = addr; |
982 | f1ccf904 | ths | #elif defined (TARGET_CRIS)
|
983 | f1ccf904 | ths | env->pc = addr; |
984 | 4c3a88a2 | bellard | #endif
|
985 | 858693c6 | bellard | } |
986 | ba70a624 | edgar_igl | gdb_continue(s); |
987 | 41625033 | bellard | return RS_IDLE;
|
988 | 858693c6 | bellard | case 's': |
989 | 858693c6 | bellard | if (*p != '\0') { |
990 | 8fac5803 | ths | addr = strtoull(p, (char **)&p, 16); |
991 | c33a346e | bellard | #if defined(TARGET_I386)
|
992 | 858693c6 | bellard | env->eip = addr; |
993 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
994 | 858693c6 | bellard | env->nip = addr; |
995 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
996 | 8d5f07fa | bellard | env->pc = addr; |
997 | 8d5f07fa | bellard | env->npc = addr + 4;
|
998 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
999 | b5ff1b31 | bellard | env->regs[15] = addr;
|
1000 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
1001 | 8fac5803 | ths | env->pc = addr; |
1002 | 8fac5803 | ths | #elif defined (TARGET_MIPS)
|
1003 | ead9360e | ths | env->PC[env->current_tc] = addr; |
1004 | f1ccf904 | ths | #elif defined (TARGET_CRIS)
|
1005 | f1ccf904 | ths | env->pc = addr; |
1006 | c33a346e | bellard | #endif
|
1007 | 858693c6 | bellard | } |
1008 | 60897d36 | edgar_igl | cpu_single_step(env, sstep_flags); |
1009 | ba70a624 | edgar_igl | gdb_continue(s); |
1010 | 41625033 | bellard | return RS_IDLE;
|
1011 | a2d1ebaf | pbrook | case 'F': |
1012 | a2d1ebaf | pbrook | { |
1013 | a2d1ebaf | pbrook | target_ulong ret; |
1014 | a2d1ebaf | pbrook | target_ulong err; |
1015 | a2d1ebaf | pbrook | |
1016 | a2d1ebaf | pbrook | ret = strtoull(p, (char **)&p, 16); |
1017 | a2d1ebaf | pbrook | if (*p == ',') { |
1018 | a2d1ebaf | pbrook | p++; |
1019 | a2d1ebaf | pbrook | err = strtoull(p, (char **)&p, 16); |
1020 | a2d1ebaf | pbrook | } else {
|
1021 | a2d1ebaf | pbrook | err = 0;
|
1022 | a2d1ebaf | pbrook | } |
1023 | a2d1ebaf | pbrook | if (*p == ',') |
1024 | a2d1ebaf | pbrook | p++; |
1025 | a2d1ebaf | pbrook | type = *p; |
1026 | a2d1ebaf | pbrook | if (gdb_current_syscall_cb)
|
1027 | a2d1ebaf | pbrook | gdb_current_syscall_cb(s->env, ret, err); |
1028 | a2d1ebaf | pbrook | if (type == 'C') { |
1029 | a2d1ebaf | pbrook | put_packet(s, "T02");
|
1030 | a2d1ebaf | pbrook | } else {
|
1031 | ba70a624 | edgar_igl | gdb_continue(s); |
1032 | a2d1ebaf | pbrook | } |
1033 | a2d1ebaf | pbrook | } |
1034 | a2d1ebaf | pbrook | break;
|
1035 | 858693c6 | bellard | case 'g': |
1036 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
1037 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
1038 | 858693c6 | bellard | put_packet(s, buf); |
1039 | 858693c6 | bellard | break;
|
1040 | 858693c6 | bellard | case 'G': |
1041 | 858693c6 | bellard | registers = (void *)mem_buf;
|
1042 | 858693c6 | bellard | len = strlen(p) / 2;
|
1043 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
1044 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
1045 | 858693c6 | bellard | put_packet(s, "OK");
|
1046 | 858693c6 | bellard | break;
|
1047 | 858693c6 | bellard | case 'm': |
1048 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
1049 | 858693c6 | bellard | if (*p == ',') |
1050 | 858693c6 | bellard | p++; |
1051 | 9d9754a3 | bellard | len = strtoull(p, NULL, 16); |
1052 | 6f970bd9 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) { |
1053 | 6f970bd9 | bellard | put_packet (s, "E14");
|
1054 | 6f970bd9 | bellard | } else {
|
1055 | 6f970bd9 | bellard | memtohex(buf, mem_buf, len); |
1056 | 6f970bd9 | bellard | put_packet(s, buf); |
1057 | 6f970bd9 | bellard | } |
1058 | 858693c6 | bellard | break;
|
1059 | 858693c6 | bellard | case 'M': |
1060 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
1061 | 858693c6 | bellard | if (*p == ',') |
1062 | 858693c6 | bellard | p++; |
1063 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
1064 | b328f873 | bellard | if (*p == ':') |
1065 | 858693c6 | bellard | p++; |
1066 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
1067 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
1068 | 905f20b1 | bellard | put_packet(s, "E14");
|
1069 | 858693c6 | bellard | else
|
1070 | 858693c6 | bellard | put_packet(s, "OK");
|
1071 | 858693c6 | bellard | break;
|
1072 | 858693c6 | bellard | case 'Z': |
1073 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
1074 | 858693c6 | bellard | if (*p == ',') |
1075 | 858693c6 | bellard | p++; |
1076 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
1077 | 858693c6 | bellard | if (*p == ',') |
1078 | 858693c6 | bellard | p++; |
1079 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
1080 | 858693c6 | bellard | if (type == 0 || type == 1) { |
1081 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
1082 | 858693c6 | bellard | goto breakpoint_error;
|
1083 | 858693c6 | bellard | put_packet(s, "OK");
|
1084 | 6658ffb8 | pbrook | #ifndef CONFIG_USER_ONLY
|
1085 | 6658ffb8 | pbrook | } else if (type == 2) { |
1086 | 6658ffb8 | pbrook | if (cpu_watchpoint_insert(env, addr) < 0) |
1087 | 6658ffb8 | pbrook | goto breakpoint_error;
|
1088 | 6658ffb8 | pbrook | put_packet(s, "OK");
|
1089 | 6658ffb8 | pbrook | #endif
|
1090 | 858693c6 | bellard | } else {
|
1091 | 858693c6 | bellard | breakpoint_error:
|
1092 | 905f20b1 | bellard | put_packet(s, "E22");
|
1093 | 858693c6 | bellard | } |
1094 | 858693c6 | bellard | break;
|
1095 | 858693c6 | bellard | case 'z': |
1096 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
1097 | 858693c6 | bellard | if (*p == ',') |
1098 | 858693c6 | bellard | p++; |
1099 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
1100 | 858693c6 | bellard | if (*p == ',') |
1101 | 858693c6 | bellard | p++; |
1102 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
1103 | 858693c6 | bellard | if (type == 0 || type == 1) { |
1104 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
1105 | 858693c6 | bellard | put_packet(s, "OK");
|
1106 | 6658ffb8 | pbrook | #ifndef CONFIG_USER_ONLY
|
1107 | 6658ffb8 | pbrook | } else if (type == 2) { |
1108 | 6658ffb8 | pbrook | cpu_watchpoint_remove(env, addr); |
1109 | 6658ffb8 | pbrook | put_packet(s, "OK");
|
1110 | 6658ffb8 | pbrook | #endif
|
1111 | 858693c6 | bellard | } else {
|
1112 | 858693c6 | bellard | goto breakpoint_error;
|
1113 | 858693c6 | bellard | } |
1114 | 858693c6 | bellard | break;
|
1115 | 978efd6a | pbrook | case 'q': |
1116 | 60897d36 | edgar_igl | case 'Q': |
1117 | 60897d36 | edgar_igl | /* parse any 'q' packets here */
|
1118 | 60897d36 | edgar_igl | if (!strcmp(p,"qemu.sstepbits")) { |
1119 | 60897d36 | edgar_igl | /* Query Breakpoint bit definitions */
|
1120 | 60897d36 | edgar_igl | sprintf(buf,"ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
|
1121 | 60897d36 | edgar_igl | SSTEP_ENABLE, |
1122 | 60897d36 | edgar_igl | SSTEP_NOIRQ, |
1123 | 60897d36 | edgar_igl | SSTEP_NOTIMER); |
1124 | 60897d36 | edgar_igl | put_packet(s, buf); |
1125 | 60897d36 | edgar_igl | break;
|
1126 | 60897d36 | edgar_igl | } else if (strncmp(p,"qemu.sstep",10) == 0) { |
1127 | 60897d36 | edgar_igl | /* Display or change the sstep_flags */
|
1128 | 60897d36 | edgar_igl | p += 10;
|
1129 | 60897d36 | edgar_igl | if (*p != '=') { |
1130 | 60897d36 | edgar_igl | /* Display current setting */
|
1131 | 60897d36 | edgar_igl | sprintf(buf,"0x%x", sstep_flags);
|
1132 | 60897d36 | edgar_igl | put_packet(s, buf); |
1133 | 60897d36 | edgar_igl | break;
|
1134 | 60897d36 | edgar_igl | } |
1135 | 60897d36 | edgar_igl | p++; |
1136 | 60897d36 | edgar_igl | type = strtoul(p, (char **)&p, 16); |
1137 | 60897d36 | edgar_igl | sstep_flags = type; |
1138 | 60897d36 | edgar_igl | put_packet(s, "OK");
|
1139 | 60897d36 | edgar_igl | break;
|
1140 | 60897d36 | edgar_igl | } |
1141 | 60897d36 | edgar_igl | #ifdef CONFIG_LINUX_USER
|
1142 | 60897d36 | edgar_igl | else if (strncmp(p, "Offsets", 7) == 0) { |
1143 | 978efd6a | pbrook | TaskState *ts = env->opaque; |
1144 | 978efd6a | pbrook | |
1145 | fe834d04 | ths | sprintf(buf, |
1146 | cd041681 | pbrook | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx |
1147 | cd041681 | pbrook | ";Bss=" TARGET_ABI_FMT_lx,
|
1148 | fe834d04 | ths | ts->info->code_offset, |
1149 | fe834d04 | ths | ts->info->data_offset, |
1150 | fe834d04 | ths | ts->info->data_offset); |
1151 | 978efd6a | pbrook | put_packet(s, buf); |
1152 | 978efd6a | pbrook | break;
|
1153 | 978efd6a | pbrook | } |
1154 | 978efd6a | pbrook | #endif
|
1155 | 60897d36 | edgar_igl | /* Fall through. */
|
1156 | 858693c6 | bellard | default:
|
1157 | 858693c6 | bellard | /* put empty packet */
|
1158 | 858693c6 | bellard | buf[0] = '\0'; |
1159 | 858693c6 | bellard | put_packet(s, buf); |
1160 | 858693c6 | bellard | break;
|
1161 | 858693c6 | bellard | } |
1162 | 858693c6 | bellard | return RS_IDLE;
|
1163 | 858693c6 | bellard | } |
1164 | 858693c6 | bellard | |
1165 | 612458f5 | bellard | extern void tb_flush(CPUState *env); |
1166 | 612458f5 | bellard | |
1167 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
1168 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
1169 | 858693c6 | bellard | { |
1170 | 858693c6 | bellard | GDBState *s = opaque; |
1171 | 858693c6 | bellard | char buf[256]; |
1172 | 858693c6 | bellard | int ret;
|
1173 | 858693c6 | bellard | |
1174 | a2d1ebaf | pbrook | if (s->state == RS_SYSCALL)
|
1175 | a2d1ebaf | pbrook | return;
|
1176 | a2d1ebaf | pbrook | |
1177 | 858693c6 | bellard | /* disable single step if it was enable */
|
1178 | 6a00d601 | bellard | cpu_single_step(s->env, 0);
|
1179 | 858693c6 | bellard | |
1180 | e80cfcfc | bellard | if (reason == EXCP_DEBUG) {
|
1181 | 6658ffb8 | pbrook | if (s->env->watchpoint_hit) {
|
1182 | aa6290b7 | pbrook | snprintf(buf, sizeof(buf), "T%02xwatch:" TARGET_FMT_lx ";", |
1183 | aa6290b7 | pbrook | SIGTRAP, |
1184 | 6658ffb8 | pbrook | s->env->watchpoint[s->env->watchpoint_hit - 1].vaddr);
|
1185 | 6658ffb8 | pbrook | put_packet(s, buf); |
1186 | 6658ffb8 | pbrook | s->env->watchpoint_hit = 0;
|
1187 | 6658ffb8 | pbrook | return;
|
1188 | 6658ffb8 | pbrook | } |
1189 | 6a00d601 | bellard | tb_flush(s->env); |
1190 | 858693c6 | bellard | ret = SIGTRAP; |
1191 | bbeb7b5c | bellard | } else if (reason == EXCP_INTERRUPT) { |
1192 | bbeb7b5c | bellard | ret = SIGINT; |
1193 | bbeb7b5c | bellard | } else {
|
1194 | 858693c6 | bellard | ret = 0;
|
1195 | bbeb7b5c | bellard | } |
1196 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
1197 | 858693c6 | bellard | put_packet(s, buf); |
1198 | 858693c6 | bellard | } |
1199 | 1fddef4b | bellard | #endif
|
1200 | 858693c6 | bellard | |
1201 | a2d1ebaf | pbrook | /* Send a gdb syscall request.
|
1202 | a2d1ebaf | pbrook | This accepts limited printf-style format specifiers, specifically:
|
1203 | a87295e8 | pbrook | %x - target_ulong argument printed in hex.
|
1204 | a87295e8 | pbrook | %lx - 64-bit argument printed in hex.
|
1205 | a87295e8 | pbrook | %s - string pointer (target_ulong) and length (int) pair. */
|
1206 | a2d1ebaf | pbrook | void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...) |
1207 | a2d1ebaf | pbrook | { |
1208 | a2d1ebaf | pbrook | va_list va; |
1209 | a2d1ebaf | pbrook | char buf[256]; |
1210 | a2d1ebaf | pbrook | char *p;
|
1211 | a2d1ebaf | pbrook | target_ulong addr; |
1212 | a87295e8 | pbrook | uint64_t i64; |
1213 | a2d1ebaf | pbrook | GDBState *s; |
1214 | a2d1ebaf | pbrook | |
1215 | a2d1ebaf | pbrook | s = gdb_syscall_state; |
1216 | a2d1ebaf | pbrook | if (!s)
|
1217 | a2d1ebaf | pbrook | return;
|
1218 | a2d1ebaf | pbrook | gdb_current_syscall_cb = cb; |
1219 | a2d1ebaf | pbrook | s->state = RS_SYSCALL; |
1220 | a2d1ebaf | pbrook | #ifndef CONFIG_USER_ONLY
|
1221 | a2d1ebaf | pbrook | vm_stop(EXCP_DEBUG); |
1222 | a2d1ebaf | pbrook | #endif
|
1223 | a2d1ebaf | pbrook | s->state = RS_IDLE; |
1224 | a2d1ebaf | pbrook | va_start(va, fmt); |
1225 | a2d1ebaf | pbrook | p = buf; |
1226 | a2d1ebaf | pbrook | *(p++) = 'F';
|
1227 | a2d1ebaf | pbrook | while (*fmt) {
|
1228 | a2d1ebaf | pbrook | if (*fmt == '%') { |
1229 | a2d1ebaf | pbrook | fmt++; |
1230 | a2d1ebaf | pbrook | switch (*fmt++) {
|
1231 | a2d1ebaf | pbrook | case 'x': |
1232 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
1233 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx, addr); |
1234 | a2d1ebaf | pbrook | break;
|
1235 | a87295e8 | pbrook | case 'l': |
1236 | a87295e8 | pbrook | if (*(fmt++) != 'x') |
1237 | a87295e8 | pbrook | goto bad_format;
|
1238 | a87295e8 | pbrook | i64 = va_arg(va, uint64_t); |
1239 | a87295e8 | pbrook | p += sprintf(p, "%" PRIx64, i64);
|
1240 | a87295e8 | pbrook | break;
|
1241 | a2d1ebaf | pbrook | case 's': |
1242 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
1243 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int)); |
1244 | a2d1ebaf | pbrook | break;
|
1245 | a2d1ebaf | pbrook | default:
|
1246 | a87295e8 | pbrook | bad_format:
|
1247 | a2d1ebaf | pbrook | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
|
1248 | a2d1ebaf | pbrook | fmt - 1);
|
1249 | a2d1ebaf | pbrook | break;
|
1250 | a2d1ebaf | pbrook | } |
1251 | a2d1ebaf | pbrook | } else {
|
1252 | a2d1ebaf | pbrook | *(p++) = *(fmt++); |
1253 | a2d1ebaf | pbrook | } |
1254 | a2d1ebaf | pbrook | } |
1255 | 8a93e02a | pbrook | *p = 0;
|
1256 | a2d1ebaf | pbrook | va_end(va); |
1257 | a2d1ebaf | pbrook | put_packet(s, buf); |
1258 | a2d1ebaf | pbrook | #ifdef CONFIG_USER_ONLY
|
1259 | a2d1ebaf | pbrook | gdb_handlesig(s->env, 0);
|
1260 | a2d1ebaf | pbrook | #else
|
1261 | a2d1ebaf | pbrook | cpu_interrupt(s->env, CPU_INTERRUPT_EXIT); |
1262 | a2d1ebaf | pbrook | #endif
|
1263 | a2d1ebaf | pbrook | } |
1264 | a2d1ebaf | pbrook | |
1265 | 6a00d601 | bellard | static void gdb_read_byte(GDBState *s, int ch) |
1266 | 858693c6 | bellard | { |
1267 | 6a00d601 | bellard | CPUState *env = s->env; |
1268 | 858693c6 | bellard | int i, csum;
|
1269 | 60fe76f3 | ths | uint8_t reply; |
1270 | 858693c6 | bellard | |
1271 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
1272 | 4046d913 | pbrook | if (s->last_packet_len) {
|
1273 | 4046d913 | pbrook | /* Waiting for a response to the last packet. If we see the start
|
1274 | 4046d913 | pbrook | of a new command then abandon the previous response. */
|
1275 | 4046d913 | pbrook | if (ch == '-') { |
1276 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
1277 | 4046d913 | pbrook | printf("Got NACK, retransmitting\n");
|
1278 | 4046d913 | pbrook | #endif
|
1279 | ffe8ab83 | ths | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
1280 | 4046d913 | pbrook | } |
1281 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
1282 | 4046d913 | pbrook | else if (ch == '+') |
1283 | 4046d913 | pbrook | printf("Got ACK\n");
|
1284 | 4046d913 | pbrook | else
|
1285 | 4046d913 | pbrook | printf("Got '%c' when expecting ACK/NACK\n", ch);
|
1286 | 4046d913 | pbrook | #endif
|
1287 | 4046d913 | pbrook | if (ch == '+' || ch == '$') |
1288 | 4046d913 | pbrook | s->last_packet_len = 0;
|
1289 | 4046d913 | pbrook | if (ch != '$') |
1290 | 4046d913 | pbrook | return;
|
1291 | 4046d913 | pbrook | } |
1292 | 858693c6 | bellard | if (vm_running) {
|
1293 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
1294 | 858693c6 | bellard | it when receiving a char */
|
1295 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
1296 | 5fafdf24 | ths | } else
|
1297 | 1fddef4b | bellard | #endif
|
1298 | 41625033 | bellard | { |
1299 | 858693c6 | bellard | switch(s->state) {
|
1300 | 858693c6 | bellard | case RS_IDLE:
|
1301 | 858693c6 | bellard | if (ch == '$') { |
1302 | 858693c6 | bellard | s->line_buf_index = 0;
|
1303 | 858693c6 | bellard | s->state = RS_GETLINE; |
1304 | c33a346e | bellard | } |
1305 | b4608c04 | bellard | break;
|
1306 | 858693c6 | bellard | case RS_GETLINE:
|
1307 | 858693c6 | bellard | if (ch == '#') { |
1308 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
1309 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
1310 | 858693c6 | bellard | s->state = RS_IDLE; |
1311 | 4c3a88a2 | bellard | } else {
|
1312 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
1313 | 4c3a88a2 | bellard | } |
1314 | 4c3a88a2 | bellard | break;
|
1315 | 858693c6 | bellard | case RS_CHKSUM1:
|
1316 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
1317 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
1318 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
1319 | 858693c6 | bellard | break;
|
1320 | 858693c6 | bellard | case RS_CHKSUM2:
|
1321 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
1322 | 858693c6 | bellard | csum = 0;
|
1323 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
1324 | 858693c6 | bellard | csum += s->line_buf[i]; |
1325 | 858693c6 | bellard | } |
1326 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
1327 | 60fe76f3 | ths | reply = '-';
|
1328 | 60fe76f3 | ths | put_buffer(s, &reply, 1);
|
1329 | 858693c6 | bellard | s->state = RS_IDLE; |
1330 | 4c3a88a2 | bellard | } else {
|
1331 | 60fe76f3 | ths | reply = '+';
|
1332 | 60fe76f3 | ths | put_buffer(s, &reply, 1);
|
1333 | 1fddef4b | bellard | s->state = gdb_handle_packet(s, env, s->line_buf); |
1334 | 4c3a88a2 | bellard | } |
1335 | 4c3a88a2 | bellard | break;
|
1336 | a2d1ebaf | pbrook | default:
|
1337 | a2d1ebaf | pbrook | abort(); |
1338 | 858693c6 | bellard | } |
1339 | 858693c6 | bellard | } |
1340 | 858693c6 | bellard | } |
1341 | 858693c6 | bellard | |
1342 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
1343 | 1fddef4b | bellard | int
|
1344 | 1fddef4b | bellard | gdb_handlesig (CPUState *env, int sig)
|
1345 | 1fddef4b | bellard | { |
1346 | 1fddef4b | bellard | GDBState *s; |
1347 | 1fddef4b | bellard | char buf[256]; |
1348 | 1fddef4b | bellard | int n;
|
1349 | 1fddef4b | bellard | |
1350 | 1fddef4b | bellard | if (gdbserver_fd < 0) |
1351 | 1fddef4b | bellard | return sig;
|
1352 | 1fddef4b | bellard | |
1353 | 1fddef4b | bellard | s = &gdbserver_state; |
1354 | 1fddef4b | bellard | |
1355 | 1fddef4b | bellard | /* disable single step if it was enabled */
|
1356 | 1fddef4b | bellard | cpu_single_step(env, 0);
|
1357 | 1fddef4b | bellard | tb_flush(env); |
1358 | 1fddef4b | bellard | |
1359 | 1fddef4b | bellard | if (sig != 0) |
1360 | 1fddef4b | bellard | { |
1361 | 1fddef4b | bellard | snprintf(buf, sizeof(buf), "S%02x", sig); |
1362 | 1fddef4b | bellard | put_packet(s, buf); |
1363 | 1fddef4b | bellard | } |
1364 | 1fddef4b | bellard | |
1365 | 1fddef4b | bellard | sig = 0;
|
1366 | 1fddef4b | bellard | s->state = RS_IDLE; |
1367 | 41625033 | bellard | s->running_state = 0;
|
1368 | 41625033 | bellard | while (s->running_state == 0) { |
1369 | 1fddef4b | bellard | n = read (s->fd, buf, 256);
|
1370 | 1fddef4b | bellard | if (n > 0) |
1371 | 1fddef4b | bellard | { |
1372 | 1fddef4b | bellard | int i;
|
1373 | 1fddef4b | bellard | |
1374 | 1fddef4b | bellard | for (i = 0; i < n; i++) |
1375 | 6a00d601 | bellard | gdb_read_byte (s, buf[i]); |
1376 | 1fddef4b | bellard | } |
1377 | 1fddef4b | bellard | else if (n == 0 || errno != EAGAIN) |
1378 | 1fddef4b | bellard | { |
1379 | 1fddef4b | bellard | /* XXX: Connection closed. Should probably wait for annother
|
1380 | 1fddef4b | bellard | connection before continuing. */
|
1381 | 1fddef4b | bellard | return sig;
|
1382 | 1fddef4b | bellard | } |
1383 | 41625033 | bellard | } |
1384 | 1fddef4b | bellard | return sig;
|
1385 | 1fddef4b | bellard | } |
1386 | e9009676 | bellard | |
1387 | e9009676 | bellard | /* Tell the remote gdb that the process has exited. */
|
1388 | e9009676 | bellard | void gdb_exit(CPUState *env, int code) |
1389 | e9009676 | bellard | { |
1390 | e9009676 | bellard | GDBState *s; |
1391 | e9009676 | bellard | char buf[4]; |
1392 | e9009676 | bellard | |
1393 | e9009676 | bellard | if (gdbserver_fd < 0) |
1394 | e9009676 | bellard | return;
|
1395 | e9009676 | bellard | |
1396 | e9009676 | bellard | s = &gdbserver_state; |
1397 | e9009676 | bellard | |
1398 | e9009676 | bellard | snprintf(buf, sizeof(buf), "W%02x", code); |
1399 | e9009676 | bellard | put_packet(s, buf); |
1400 | e9009676 | bellard | } |
1401 | e9009676 | bellard | |
1402 | 1fddef4b | bellard | |
1403 | 7c9d8e07 | bellard | static void gdb_accept(void *opaque) |
1404 | 858693c6 | bellard | { |
1405 | 858693c6 | bellard | GDBState *s; |
1406 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1407 | 858693c6 | bellard | socklen_t len; |
1408 | 858693c6 | bellard | int val, fd;
|
1409 | 858693c6 | bellard | |
1410 | 858693c6 | bellard | for(;;) {
|
1411 | 858693c6 | bellard | len = sizeof(sockaddr);
|
1412 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
1413 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
1414 | 858693c6 | bellard | perror("accept");
|
1415 | 858693c6 | bellard | return;
|
1416 | 858693c6 | bellard | } else if (fd >= 0) { |
1417 | b4608c04 | bellard | break;
|
1418 | b4608c04 | bellard | } |
1419 | b4608c04 | bellard | } |
1420 | 858693c6 | bellard | |
1421 | 858693c6 | bellard | /* set short latency */
|
1422 | 858693c6 | bellard | val = 1;
|
1423 | 8f447cc7 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
1424 | 3b46e624 | ths | |
1425 | 1fddef4b | bellard | s = &gdbserver_state; |
1426 | 1fddef4b | bellard | memset (s, 0, sizeof (GDBState)); |
1427 | 6a00d601 | bellard | s->env = first_cpu; /* XXX: allow to change CPU */
|
1428 | 858693c6 | bellard | s->fd = fd; |
1429 | 858693c6 | bellard | |
1430 | a2d1ebaf | pbrook | gdb_syscall_state = s; |
1431 | a2d1ebaf | pbrook | |
1432 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
1433 | 858693c6 | bellard | } |
1434 | 858693c6 | bellard | |
1435 | 858693c6 | bellard | static int gdbserver_open(int port) |
1436 | 858693c6 | bellard | { |
1437 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1438 | 858693c6 | bellard | int fd, val, ret;
|
1439 | 858693c6 | bellard | |
1440 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
1441 | 858693c6 | bellard | if (fd < 0) { |
1442 | 858693c6 | bellard | perror("socket");
|
1443 | 858693c6 | bellard | return -1; |
1444 | 858693c6 | bellard | } |
1445 | 858693c6 | bellard | |
1446 | 858693c6 | bellard | /* allow fast reuse */
|
1447 | 858693c6 | bellard | val = 1;
|
1448 | 8f447cc7 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
1449 | 858693c6 | bellard | |
1450 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
1451 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
1452 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
1453 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
1454 | 858693c6 | bellard | if (ret < 0) { |
1455 | 858693c6 | bellard | perror("bind");
|
1456 | 858693c6 | bellard | return -1; |
1457 | 858693c6 | bellard | } |
1458 | 858693c6 | bellard | ret = listen(fd, 0);
|
1459 | 858693c6 | bellard | if (ret < 0) { |
1460 | 858693c6 | bellard | perror("listen");
|
1461 | 858693c6 | bellard | return -1; |
1462 | 858693c6 | bellard | } |
1463 | 858693c6 | bellard | return fd;
|
1464 | 858693c6 | bellard | } |
1465 | 858693c6 | bellard | |
1466 | 858693c6 | bellard | int gdbserver_start(int port) |
1467 | 858693c6 | bellard | { |
1468 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
1469 | 858693c6 | bellard | if (gdbserver_fd < 0) |
1470 | 858693c6 | bellard | return -1; |
1471 | 858693c6 | bellard | /* accept connections */
|
1472 | 7c9d8e07 | bellard | gdb_accept (NULL);
|
1473 | 4046d913 | pbrook | return 0; |
1474 | 4046d913 | pbrook | } |
1475 | 1fddef4b | bellard | #else
|
1476 | aa1f17c1 | ths | static int gdb_chr_can_receive(void *opaque) |
1477 | 4046d913 | pbrook | { |
1478 | 4046d913 | pbrook | return 1; |
1479 | 4046d913 | pbrook | } |
1480 | 4046d913 | pbrook | |
1481 | aa1f17c1 | ths | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
1482 | 4046d913 | pbrook | { |
1483 | 4046d913 | pbrook | GDBState *s = opaque; |
1484 | 4046d913 | pbrook | int i;
|
1485 | 4046d913 | pbrook | |
1486 | 4046d913 | pbrook | for (i = 0; i < size; i++) { |
1487 | 4046d913 | pbrook | gdb_read_byte(s, buf[i]); |
1488 | 4046d913 | pbrook | } |
1489 | 4046d913 | pbrook | } |
1490 | 4046d913 | pbrook | |
1491 | 4046d913 | pbrook | static void gdb_chr_event(void *opaque, int event) |
1492 | 4046d913 | pbrook | { |
1493 | 4046d913 | pbrook | switch (event) {
|
1494 | 4046d913 | pbrook | case CHR_EVENT_RESET:
|
1495 | 4046d913 | pbrook | vm_stop(EXCP_INTERRUPT); |
1496 | a2d1ebaf | pbrook | gdb_syscall_state = opaque; |
1497 | 4046d913 | pbrook | break;
|
1498 | 4046d913 | pbrook | default:
|
1499 | 4046d913 | pbrook | break;
|
1500 | 4046d913 | pbrook | } |
1501 | 4046d913 | pbrook | } |
1502 | 4046d913 | pbrook | |
1503 | cfc3475a | pbrook | int gdbserver_start(const char *port) |
1504 | 4046d913 | pbrook | { |
1505 | 4046d913 | pbrook | GDBState *s; |
1506 | cfc3475a | pbrook | char gdbstub_port_name[128]; |
1507 | cfc3475a | pbrook | int port_num;
|
1508 | cfc3475a | pbrook | char *p;
|
1509 | cfc3475a | pbrook | CharDriverState *chr; |
1510 | cfc3475a | pbrook | |
1511 | cfc3475a | pbrook | if (!port || !*port)
|
1512 | cfc3475a | pbrook | return -1; |
1513 | 4046d913 | pbrook | |
1514 | cfc3475a | pbrook | port_num = strtol(port, &p, 10);
|
1515 | cfc3475a | pbrook | if (*p == 0) { |
1516 | cfc3475a | pbrook | /* A numeric value is interpreted as a port number. */
|
1517 | cfc3475a | pbrook | snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
|
1518 | cfc3475a | pbrook | "tcp::%d,nowait,nodelay,server", port_num);
|
1519 | cfc3475a | pbrook | port = gdbstub_port_name; |
1520 | cfc3475a | pbrook | } |
1521 | cfc3475a | pbrook | |
1522 | cfc3475a | pbrook | chr = qemu_chr_open(port); |
1523 | 4046d913 | pbrook | if (!chr)
|
1524 | 4046d913 | pbrook | return -1; |
1525 | 4046d913 | pbrook | |
1526 | 4046d913 | pbrook | s = qemu_mallocz(sizeof(GDBState));
|
1527 | 4046d913 | pbrook | if (!s) {
|
1528 | 4046d913 | pbrook | return -1; |
1529 | 4046d913 | pbrook | } |
1530 | 4046d913 | pbrook | s->env = first_cpu; /* XXX: allow to change CPU */
|
1531 | 4046d913 | pbrook | s->chr = chr; |
1532 | aa1f17c1 | ths | qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive, |
1533 | 4046d913 | pbrook | gdb_chr_event, s); |
1534 | 4046d913 | pbrook | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
1535 | b4608c04 | bellard | return 0; |
1536 | b4608c04 | bellard | } |
1537 | 4046d913 | pbrook | #endif |