root / gdbstub.c @ 86cc1ce0
History | View | Annotate | Download (30.9 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 | a2d1ebaf | pbrook | RS_SYSCALL, |
56 | 858693c6 | bellard | }; |
57 | 858693c6 | bellard | typedef struct GDBState { |
58 | 6a00d601 | bellard | CPUState *env; /* current CPU */
|
59 | 41625033 | bellard | enum RSState state; /* parsing state */ |
60 | 858693c6 | bellard | char line_buf[4096]; |
61 | 858693c6 | bellard | int line_buf_index;
|
62 | 858693c6 | bellard | int line_csum;
|
63 | 4046d913 | pbrook | char last_packet[4100]; |
64 | 4046d913 | pbrook | int last_packet_len;
|
65 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
66 | 4046d913 | pbrook | int fd;
|
67 | 41625033 | bellard | int running_state;
|
68 | 4046d913 | pbrook | #else
|
69 | 4046d913 | pbrook | CharDriverState *chr; |
70 | 41625033 | bellard | #endif
|
71 | 858693c6 | bellard | } GDBState; |
72 | b4608c04 | bellard | |
73 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
74 | 4046d913 | pbrook | /* XXX: This is not thread safe. Do we care? */
|
75 | 4046d913 | pbrook | static int gdbserver_fd = -1; |
76 | 4046d913 | pbrook | |
77 | 1fddef4b | bellard | /* XXX: remove this hack. */
|
78 | 1fddef4b | bellard | static GDBState gdbserver_state;
|
79 | 1fddef4b | bellard | |
80 | 858693c6 | bellard | static int get_char(GDBState *s) |
81 | b4608c04 | bellard | { |
82 | b4608c04 | bellard | uint8_t ch; |
83 | b4608c04 | bellard | int ret;
|
84 | b4608c04 | bellard | |
85 | b4608c04 | bellard | for(;;) {
|
86 | 8f447cc7 | bellard | ret = recv(s->fd, &ch, 1, 0); |
87 | b4608c04 | bellard | if (ret < 0) { |
88 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
89 | b4608c04 | bellard | return -1; |
90 | b4608c04 | bellard | } else if (ret == 0) { |
91 | b4608c04 | bellard | return -1; |
92 | b4608c04 | bellard | } else {
|
93 | b4608c04 | bellard | break;
|
94 | b4608c04 | bellard | } |
95 | b4608c04 | bellard | } |
96 | b4608c04 | bellard | return ch;
|
97 | b4608c04 | bellard | } |
98 | 4046d913 | pbrook | #endif
|
99 | b4608c04 | bellard | |
100 | a2d1ebaf | pbrook | /* GDB stub state for use by semihosting syscalls. */
|
101 | a2d1ebaf | pbrook | static GDBState *gdb_syscall_state;
|
102 | a2d1ebaf | pbrook | static gdb_syscall_complete_cb gdb_current_syscall_cb;
|
103 | a2d1ebaf | pbrook | |
104 | a2d1ebaf | pbrook | enum {
|
105 | a2d1ebaf | pbrook | GDB_SYS_UNKNOWN, |
106 | a2d1ebaf | pbrook | GDB_SYS_ENABLED, |
107 | a2d1ebaf | pbrook | GDB_SYS_DISABLED, |
108 | a2d1ebaf | pbrook | } gdb_syscall_mode; |
109 | a2d1ebaf | pbrook | |
110 | a2d1ebaf | pbrook | /* If gdb is connected when the first semihosting syscall occurs then use
|
111 | a2d1ebaf | pbrook | remote gdb syscalls. Otherwise use native file IO. */
|
112 | a2d1ebaf | pbrook | int use_gdb_syscalls(void) |
113 | a2d1ebaf | pbrook | { |
114 | a2d1ebaf | pbrook | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
|
115 | a2d1ebaf | pbrook | gdb_syscall_mode = (gdb_syscall_state ? GDB_SYS_ENABLED |
116 | a2d1ebaf | pbrook | : GDB_SYS_DISABLED); |
117 | a2d1ebaf | pbrook | } |
118 | a2d1ebaf | pbrook | return gdb_syscall_mode == GDB_SYS_ENABLED;
|
119 | a2d1ebaf | pbrook | } |
120 | a2d1ebaf | pbrook | |
121 | 858693c6 | bellard | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
122 | b4608c04 | bellard | { |
123 | 4046d913 | pbrook | #ifdef CONFIG_USER_ONLY
|
124 | b4608c04 | bellard | int ret;
|
125 | b4608c04 | bellard | |
126 | b4608c04 | bellard | while (len > 0) { |
127 | 8f447cc7 | bellard | ret = send(s->fd, buf, len, 0);
|
128 | b4608c04 | bellard | if (ret < 0) { |
129 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
130 | b4608c04 | bellard | return;
|
131 | b4608c04 | bellard | } else {
|
132 | b4608c04 | bellard | buf += ret; |
133 | b4608c04 | bellard | len -= ret; |
134 | b4608c04 | bellard | } |
135 | b4608c04 | bellard | } |
136 | 4046d913 | pbrook | #else
|
137 | 4046d913 | pbrook | qemu_chr_write(s->chr, buf, len); |
138 | 4046d913 | pbrook | #endif
|
139 | b4608c04 | bellard | } |
140 | b4608c04 | bellard | |
141 | b4608c04 | bellard | static inline int fromhex(int v) |
142 | b4608c04 | bellard | { |
143 | b4608c04 | bellard | if (v >= '0' && v <= '9') |
144 | b4608c04 | bellard | return v - '0'; |
145 | b4608c04 | bellard | else if (v >= 'A' && v <= 'F') |
146 | b4608c04 | bellard | return v - 'A' + 10; |
147 | b4608c04 | bellard | else if (v >= 'a' && v <= 'f') |
148 | b4608c04 | bellard | return v - 'a' + 10; |
149 | b4608c04 | bellard | else
|
150 | b4608c04 | bellard | return 0; |
151 | b4608c04 | bellard | } |
152 | b4608c04 | bellard | |
153 | b4608c04 | bellard | static inline int tohex(int v) |
154 | b4608c04 | bellard | { |
155 | b4608c04 | bellard | if (v < 10) |
156 | b4608c04 | bellard | return v + '0'; |
157 | b4608c04 | bellard | else
|
158 | b4608c04 | bellard | return v - 10 + 'a'; |
159 | b4608c04 | bellard | } |
160 | b4608c04 | bellard | |
161 | b4608c04 | bellard | static void memtohex(char *buf, const uint8_t *mem, int len) |
162 | b4608c04 | bellard | { |
163 | b4608c04 | bellard | int i, c;
|
164 | b4608c04 | bellard | char *q;
|
165 | b4608c04 | bellard | q = buf; |
166 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
167 | b4608c04 | bellard | c = mem[i]; |
168 | b4608c04 | bellard | *q++ = tohex(c >> 4);
|
169 | b4608c04 | bellard | *q++ = tohex(c & 0xf);
|
170 | b4608c04 | bellard | } |
171 | b4608c04 | bellard | *q = '\0';
|
172 | b4608c04 | bellard | } |
173 | b4608c04 | bellard | |
174 | b4608c04 | bellard | static void hextomem(uint8_t *mem, const char *buf, int len) |
175 | b4608c04 | bellard | { |
176 | b4608c04 | bellard | int i;
|
177 | b4608c04 | bellard | |
178 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
179 | b4608c04 | bellard | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
180 | b4608c04 | bellard | buf += 2;
|
181 | b4608c04 | bellard | } |
182 | b4608c04 | bellard | } |
183 | b4608c04 | bellard | |
184 | b4608c04 | bellard | /* return -1 if error, 0 if OK */
|
185 | 858693c6 | bellard | static int put_packet(GDBState *s, char *buf) |
186 | b4608c04 | bellard | { |
187 | 4046d913 | pbrook | int len, csum, i;
|
188 | 4046d913 | pbrook | char *p;
|
189 | b4608c04 | bellard | |
190 | b4608c04 | bellard | #ifdef DEBUG_GDB
|
191 | b4608c04 | bellard | printf("reply='%s'\n", buf);
|
192 | b4608c04 | bellard | #endif
|
193 | b4608c04 | bellard | |
194 | b4608c04 | bellard | for(;;) {
|
195 | 4046d913 | pbrook | p = s->last_packet; |
196 | 4046d913 | pbrook | *(p++) = '$';
|
197 | b4608c04 | bellard | len = strlen(buf); |
198 | 4046d913 | pbrook | memcpy(p, buf, len); |
199 | 4046d913 | pbrook | p += len; |
200 | b4608c04 | bellard | csum = 0;
|
201 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
202 | b4608c04 | bellard | csum += buf[i]; |
203 | b4608c04 | bellard | } |
204 | 4046d913 | pbrook | *(p++) = '#';
|
205 | 4046d913 | pbrook | *(p++) = tohex((csum >> 4) & 0xf); |
206 | 4046d913 | pbrook | *(p++) = tohex((csum) & 0xf);
|
207 | b4608c04 | bellard | |
208 | 4046d913 | pbrook | s->last_packet_len = p - s->last_packet; |
209 | 4046d913 | pbrook | put_buffer(s, s->last_packet, s->last_packet_len); |
210 | b4608c04 | bellard | |
211 | 4046d913 | pbrook | #ifdef CONFIG_USER_ONLY
|
212 | 4046d913 | pbrook | i = get_char(s); |
213 | 4046d913 | pbrook | if (i < 0) |
214 | b4608c04 | bellard | return -1; |
215 | 4046d913 | pbrook | if (i == '+') |
216 | b4608c04 | bellard | break;
|
217 | 4046d913 | pbrook | #else
|
218 | 4046d913 | pbrook | break;
|
219 | 4046d913 | pbrook | #endif
|
220 | b4608c04 | bellard | } |
221 | b4608c04 | bellard | return 0; |
222 | b4608c04 | bellard | } |
223 | b4608c04 | bellard | |
224 | 6da41eaf | bellard | #if defined(TARGET_I386)
|
225 | 6da41eaf | bellard | |
226 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
227 | 6da41eaf | bellard | { |
228 | e95c8d51 | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
229 | 6da41eaf | bellard | int i, fpus;
|
230 | 6da41eaf | bellard | |
231 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
232 | e95c8d51 | bellard | registers[i] = env->regs[i]; |
233 | 6da41eaf | bellard | } |
234 | e95c8d51 | bellard | registers[8] = env->eip;
|
235 | e95c8d51 | bellard | registers[9] = env->eflags;
|
236 | e95c8d51 | bellard | registers[10] = env->segs[R_CS].selector;
|
237 | e95c8d51 | bellard | registers[11] = env->segs[R_SS].selector;
|
238 | e95c8d51 | bellard | registers[12] = env->segs[R_DS].selector;
|
239 | e95c8d51 | bellard | registers[13] = env->segs[R_ES].selector;
|
240 | e95c8d51 | bellard | registers[14] = env->segs[R_FS].selector;
|
241 | e95c8d51 | bellard | registers[15] = env->segs[R_GS].selector;
|
242 | 6da41eaf | bellard | /* XXX: convert floats */
|
243 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
244 | 6da41eaf | bellard | memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
245 | 6da41eaf | bellard | } |
246 | e95c8d51 | bellard | registers[36] = env->fpuc;
|
247 | 6da41eaf | bellard | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
248 | e95c8d51 | bellard | registers[37] = fpus;
|
249 | e95c8d51 | bellard | registers[38] = 0; /* XXX: convert tags */ |
250 | e95c8d51 | bellard | registers[39] = 0; /* fiseg */ |
251 | e95c8d51 | bellard | registers[40] = 0; /* fioff */ |
252 | e95c8d51 | bellard | registers[41] = 0; /* foseg */ |
253 | e95c8d51 | bellard | registers[42] = 0; /* fooff */ |
254 | e95c8d51 | bellard | registers[43] = 0; /* fop */ |
255 | e95c8d51 | bellard | |
256 | e95c8d51 | bellard | for(i = 0; i < 16; i++) |
257 | e95c8d51 | bellard | tswapls(®isters[i]); |
258 | e95c8d51 | bellard | for(i = 36; i < 44; i++) |
259 | e95c8d51 | bellard | tswapls(®isters[i]); |
260 | 6da41eaf | bellard | return 44 * 4; |
261 | 6da41eaf | bellard | } |
262 | 6da41eaf | bellard | |
263 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
264 | 6da41eaf | bellard | { |
265 | 6da41eaf | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
266 | 6da41eaf | bellard | int i;
|
267 | 6da41eaf | bellard | |
268 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
269 | 6da41eaf | bellard | env->regs[i] = tswapl(registers[i]); |
270 | 6da41eaf | bellard | } |
271 | e95c8d51 | bellard | env->eip = tswapl(registers[8]);
|
272 | e95c8d51 | bellard | env->eflags = tswapl(registers[9]);
|
273 | 6da41eaf | bellard | #if defined(CONFIG_USER_ONLY)
|
274 | 6da41eaf | bellard | #define LOAD_SEG(index, sreg)\
|
275 | 6da41eaf | bellard | if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
276 | 6da41eaf | bellard | cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
277 | 6da41eaf | bellard | LOAD_SEG(10, R_CS);
|
278 | 6da41eaf | bellard | LOAD_SEG(11, R_SS);
|
279 | 6da41eaf | bellard | LOAD_SEG(12, R_DS);
|
280 | 6da41eaf | bellard | LOAD_SEG(13, R_ES);
|
281 | 6da41eaf | bellard | LOAD_SEG(14, R_FS);
|
282 | 6da41eaf | bellard | LOAD_SEG(15, R_GS);
|
283 | 6da41eaf | bellard | #endif
|
284 | 6da41eaf | bellard | } |
285 | 6da41eaf | bellard | |
286 | 9e62fd7f | bellard | #elif defined (TARGET_PPC)
|
287 | 9e62fd7f | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
288 | 9e62fd7f | bellard | { |
289 | a541f297 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
290 | 9e62fd7f | bellard | int i;
|
291 | 9e62fd7f | bellard | |
292 | 9e62fd7f | bellard | /* fill in gprs */
|
293 | a541f297 | bellard | for(i = 0; i < 32; i++) { |
294 | e95c8d51 | bellard | registers[i] = tswapl(env->gpr[i]); |
295 | 9e62fd7f | bellard | } |
296 | 9e62fd7f | bellard | /* fill in fprs */
|
297 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
298 | e95c8d51 | bellard | registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
299 | e95c8d51 | bellard | registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
300 | 9e62fd7f | bellard | } |
301 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
302 | e95c8d51 | bellard | registers[96] = tswapl(env->nip);
|
303 | 3fc6c082 | bellard | registers[97] = tswapl(do_load_msr(env));
|
304 | 9e62fd7f | bellard | tmp = 0;
|
305 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
306 | a541f297 | bellard | tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
307 | e95c8d51 | bellard | registers[98] = tswapl(tmp);
|
308 | e95c8d51 | bellard | registers[99] = tswapl(env->lr);
|
309 | e95c8d51 | bellard | registers[100] = tswapl(env->ctr);
|
310 | 76a66253 | j_mayer | registers[101] = tswapl(ppc_load_xer(env));
|
311 | e95c8d51 | bellard | registers[102] = 0; |
312 | a541f297 | bellard | |
313 | a541f297 | bellard | return 103 * 4; |
314 | 9e62fd7f | bellard | } |
315 | 9e62fd7f | bellard | |
316 | 9e62fd7f | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
317 | 9e62fd7f | bellard | { |
318 | 9e62fd7f | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
319 | 9e62fd7f | bellard | int i;
|
320 | 9e62fd7f | bellard | |
321 | 9e62fd7f | bellard | /* fill in gprs */
|
322 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
323 | e95c8d51 | bellard | env->gpr[i] = tswapl(registers[i]); |
324 | 9e62fd7f | bellard | } |
325 | 9e62fd7f | bellard | /* fill in fprs */
|
326 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
327 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
328 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
329 | 9e62fd7f | bellard | } |
330 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
331 | e95c8d51 | bellard | env->nip = tswapl(registers[96]);
|
332 | 3fc6c082 | bellard | do_store_msr(env, tswapl(registers[97]));
|
333 | e95c8d51 | bellard | registers[98] = tswapl(registers[98]); |
334 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
335 | a541f297 | bellard | env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
336 | e95c8d51 | bellard | env->lr = tswapl(registers[99]);
|
337 | e95c8d51 | bellard | env->ctr = tswapl(registers[100]);
|
338 | 76a66253 | j_mayer | ppc_store_xer(env, tswapl(registers[101]));
|
339 | e95c8d51 | bellard | } |
340 | e95c8d51 | bellard | #elif defined (TARGET_SPARC)
|
341 | e95c8d51 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
342 | e95c8d51 | bellard | { |
343 | 3475187d | bellard | target_ulong *registers = (target_ulong *)mem_buf; |
344 | e95c8d51 | bellard | int i;
|
345 | e95c8d51 | bellard | |
346 | e95c8d51 | bellard | /* fill in g0..g7 */
|
347 | 48b2c193 | bellard | for(i = 0; i < 8; i++) { |
348 | e95c8d51 | bellard | registers[i] = tswapl(env->gregs[i]); |
349 | e95c8d51 | bellard | } |
350 | e95c8d51 | bellard | /* fill in register window */
|
351 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
352 | e95c8d51 | bellard | registers[i + 8] = tswapl(env->regwptr[i]);
|
353 | e95c8d51 | bellard | } |
354 | 9d9754a3 | bellard | #ifndef TARGET_SPARC64
|
355 | e95c8d51 | bellard | /* fill in fprs */
|
356 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
357 | e95c8d51 | bellard | registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
358 | e95c8d51 | bellard | } |
359 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
360 | e95c8d51 | bellard | registers[64] = tswapl(env->y);
|
361 | 3475187d | bellard | { |
362 | 3475187d | bellard | target_ulong tmp; |
363 | 3475187d | bellard | |
364 | 3475187d | bellard | tmp = GET_PSR(env); |
365 | 3475187d | bellard | registers[65] = tswapl(tmp);
|
366 | 3475187d | bellard | } |
367 | e95c8d51 | bellard | registers[66] = tswapl(env->wim);
|
368 | e95c8d51 | bellard | registers[67] = tswapl(env->tbr);
|
369 | e95c8d51 | bellard | registers[68] = tswapl(env->pc);
|
370 | e95c8d51 | bellard | registers[69] = tswapl(env->npc);
|
371 | e95c8d51 | bellard | registers[70] = tswapl(env->fsr);
|
372 | e95c8d51 | bellard | registers[71] = 0; /* csr */ |
373 | e95c8d51 | bellard | registers[72] = 0; |
374 | 3475187d | bellard | return 73 * sizeof(target_ulong); |
375 | 3475187d | bellard | #else
|
376 | 9d9754a3 | bellard | /* fill in fprs */
|
377 | 9d9754a3 | bellard | for (i = 0; i < 64; i += 2) { |
378 | 9d9754a3 | bellard | uint64_t tmp; |
379 | 9d9754a3 | bellard | |
380 | 9d9754a3 | bellard | tmp = (uint64_t)tswap32(*((uint32_t *)&env->fpr[i])) << 32;
|
381 | 9d9754a3 | bellard | tmp |= tswap32(*((uint32_t *)&env->fpr[i + 1]));
|
382 | 9d9754a3 | bellard | registers[i/2 + 32] = tmp; |
383 | 3475187d | bellard | } |
384 | 9d9754a3 | bellard | registers[64] = tswapl(env->pc);
|
385 | 9d9754a3 | bellard | registers[65] = tswapl(env->npc);
|
386 | 9d9754a3 | bellard | registers[66] = tswapl(env->tstate[env->tl]);
|
387 | 9d9754a3 | bellard | registers[67] = tswapl(env->fsr);
|
388 | 9d9754a3 | bellard | registers[68] = tswapl(env->fprs);
|
389 | 9d9754a3 | bellard | registers[69] = tswapl(env->y);
|
390 | 9d9754a3 | bellard | return 70 * sizeof(target_ulong); |
391 | 3475187d | bellard | #endif
|
392 | e95c8d51 | bellard | } |
393 | e95c8d51 | bellard | |
394 | e95c8d51 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
395 | e95c8d51 | bellard | { |
396 | 3475187d | bellard | target_ulong *registers = (target_ulong *)mem_buf; |
397 | e95c8d51 | bellard | int i;
|
398 | e95c8d51 | bellard | |
399 | e95c8d51 | bellard | /* fill in g0..g7 */
|
400 | e95c8d51 | bellard | for(i = 0; i < 7; i++) { |
401 | e95c8d51 | bellard | env->gregs[i] = tswapl(registers[i]); |
402 | e95c8d51 | bellard | } |
403 | e95c8d51 | bellard | /* fill in register window */
|
404 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
405 | 3475187d | bellard | env->regwptr[i] = tswapl(registers[i + 8]);
|
406 | e95c8d51 | bellard | } |
407 | 9d9754a3 | bellard | #ifndef TARGET_SPARC64
|
408 | e95c8d51 | bellard | /* fill in fprs */
|
409 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
410 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
411 | e95c8d51 | bellard | } |
412 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
413 | e95c8d51 | bellard | env->y = tswapl(registers[64]);
|
414 | e80cfcfc | bellard | PUT_PSR(env, tswapl(registers[65]));
|
415 | e95c8d51 | bellard | env->wim = tswapl(registers[66]);
|
416 | e95c8d51 | bellard | env->tbr = tswapl(registers[67]);
|
417 | e95c8d51 | bellard | env->pc = tswapl(registers[68]);
|
418 | e95c8d51 | bellard | env->npc = tswapl(registers[69]);
|
419 | e95c8d51 | bellard | env->fsr = tswapl(registers[70]);
|
420 | 3475187d | bellard | #else
|
421 | 9d9754a3 | bellard | for (i = 0; i < 64; i += 2) { |
422 | 9d9754a3 | bellard | *((uint32_t *)&env->fpr[i]) = tswap32(registers[i/2 + 32] >> 32); |
423 | 9d9754a3 | bellard | *((uint32_t *)&env->fpr[i + 1]) = tswap32(registers[i/2 + 32] & 0xffffffff); |
424 | 3475187d | bellard | } |
425 | 9d9754a3 | bellard | env->pc = tswapl(registers[64]);
|
426 | 9d9754a3 | bellard | env->npc = tswapl(registers[65]);
|
427 | 9d9754a3 | bellard | env->tstate[env->tl] = tswapl(registers[66]);
|
428 | 9d9754a3 | bellard | env->fsr = tswapl(registers[67]);
|
429 | 9d9754a3 | bellard | env->fprs = tswapl(registers[68]);
|
430 | 9d9754a3 | bellard | env->y = tswapl(registers[69]);
|
431 | 3475187d | bellard | #endif
|
432 | 9e62fd7f | bellard | } |
433 | 1fddef4b | bellard | #elif defined (TARGET_ARM)
|
434 | 1fddef4b | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
435 | 1fddef4b | bellard | { |
436 | 1fddef4b | bellard | int i;
|
437 | 1fddef4b | bellard | uint8_t *ptr; |
438 | 1fddef4b | bellard | |
439 | 1fddef4b | bellard | ptr = mem_buf; |
440 | 1fddef4b | bellard | /* 16 core integer registers (4 bytes each). */
|
441 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
442 | 1fddef4b | bellard | { |
443 | 1fddef4b | bellard | *(uint32_t *)ptr = tswapl(env->regs[i]); |
444 | 1fddef4b | bellard | ptr += 4;
|
445 | 1fddef4b | bellard | } |
446 | 1fddef4b | bellard | /* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
447 | 1fddef4b | bellard | Not yet implemented. */
|
448 | 1fddef4b | bellard | memset (ptr, 0, 8 * 12 + 4); |
449 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
450 | 1fddef4b | bellard | /* CPSR (4 bytes). */
|
451 | b5ff1b31 | bellard | *(uint32_t *)ptr = tswapl (cpsr_read(env)); |
452 | 1fddef4b | bellard | ptr += 4;
|
453 | 1fddef4b | bellard | |
454 | 1fddef4b | bellard | return ptr - mem_buf;
|
455 | 1fddef4b | bellard | } |
456 | 6da41eaf | bellard | |
457 | 1fddef4b | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
458 | 1fddef4b | bellard | { |
459 | 1fddef4b | bellard | int i;
|
460 | 1fddef4b | bellard | uint8_t *ptr; |
461 | 1fddef4b | bellard | |
462 | 1fddef4b | bellard | ptr = mem_buf; |
463 | 1fddef4b | bellard | /* Core integer registers. */
|
464 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
465 | 1fddef4b | bellard | { |
466 | 1fddef4b | bellard | env->regs[i] = tswapl(*(uint32_t *)ptr); |
467 | 1fddef4b | bellard | ptr += 4;
|
468 | 1fddef4b | bellard | } |
469 | 1fddef4b | bellard | /* Ignore FPA regs and scr. */
|
470 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
471 | b5ff1b31 | bellard | cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
|
472 | 1fddef4b | bellard | } |
473 | e6e5906b | pbrook | #elif defined (TARGET_M68K)
|
474 | e6e5906b | pbrook | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
475 | e6e5906b | pbrook | { |
476 | e6e5906b | pbrook | int i;
|
477 | e6e5906b | pbrook | uint8_t *ptr; |
478 | e6e5906b | pbrook | CPU_DoubleU u; |
479 | e6e5906b | pbrook | |
480 | e6e5906b | pbrook | ptr = mem_buf; |
481 | e6e5906b | pbrook | /* D0-D7 */
|
482 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
483 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->dregs[i]); |
484 | e6e5906b | pbrook | ptr += 4;
|
485 | e6e5906b | pbrook | } |
486 | e6e5906b | pbrook | /* A0-A7 */
|
487 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
488 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->aregs[i]); |
489 | e6e5906b | pbrook | ptr += 4;
|
490 | e6e5906b | pbrook | } |
491 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->sr); |
492 | e6e5906b | pbrook | ptr += 4;
|
493 | e6e5906b | pbrook | *(uint32_t *)ptr = tswapl(env->pc); |
494 | e6e5906b | pbrook | ptr += 4;
|
495 | e6e5906b | pbrook | /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
496 | e6e5906b | pbrook | ColdFire has 8-bit double precision registers. */
|
497 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
498 | e6e5906b | pbrook | u.d = env->fregs[i]; |
499 | e6e5906b | pbrook | *(uint32_t *)ptr = tswap32(u.l.upper); |
500 | e6e5906b | pbrook | *(uint32_t *)ptr = tswap32(u.l.lower); |
501 | e6e5906b | pbrook | } |
502 | e6e5906b | pbrook | /* FP control regs (not implemented). */
|
503 | e6e5906b | pbrook | memset (ptr, 0, 3 * 4); |
504 | e6e5906b | pbrook | ptr += 3 * 4; |
505 | e6e5906b | pbrook | |
506 | e6e5906b | pbrook | return ptr - mem_buf;
|
507 | e6e5906b | pbrook | } |
508 | e6e5906b | pbrook | |
509 | e6e5906b | pbrook | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
510 | e6e5906b | pbrook | { |
511 | e6e5906b | pbrook | int i;
|
512 | e6e5906b | pbrook | uint8_t *ptr; |
513 | e6e5906b | pbrook | CPU_DoubleU u; |
514 | e6e5906b | pbrook | |
515 | e6e5906b | pbrook | ptr = mem_buf; |
516 | e6e5906b | pbrook | /* D0-D7 */
|
517 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
518 | e6e5906b | pbrook | env->dregs[i] = tswapl(*(uint32_t *)ptr); |
519 | e6e5906b | pbrook | ptr += 4;
|
520 | e6e5906b | pbrook | } |
521 | e6e5906b | pbrook | /* A0-A7 */
|
522 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
523 | e6e5906b | pbrook | env->aregs[i] = tswapl(*(uint32_t *)ptr); |
524 | e6e5906b | pbrook | ptr += 4;
|
525 | e6e5906b | pbrook | } |
526 | e6e5906b | pbrook | env->sr = tswapl(*(uint32_t *)ptr); |
527 | e6e5906b | pbrook | ptr += 4;
|
528 | e6e5906b | pbrook | env->pc = tswapl(*(uint32_t *)ptr); |
529 | e6e5906b | pbrook | ptr += 4;
|
530 | e6e5906b | pbrook | /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
|
531 | e6e5906b | pbrook | ColdFire has 8-bit double precision registers. */
|
532 | e6e5906b | pbrook | for (i = 0; i < 8; i++) { |
533 | e6e5906b | pbrook | u.l.upper = tswap32(*(uint32_t *)ptr); |
534 | e6e5906b | pbrook | u.l.lower = tswap32(*(uint32_t *)ptr); |
535 | e6e5906b | pbrook | env->fregs[i] = u.d; |
536 | e6e5906b | pbrook | } |
537 | e6e5906b | pbrook | /* FP control regs (not implemented). */
|
538 | e6e5906b | pbrook | ptr += 3 * 4; |
539 | e6e5906b | pbrook | } |
540 | 6f970bd9 | bellard | #elif defined (TARGET_MIPS)
|
541 | 6f970bd9 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
542 | 6f970bd9 | bellard | { |
543 | 6f970bd9 | bellard | int i;
|
544 | 6f970bd9 | bellard | uint8_t *ptr; |
545 | 6f970bd9 | bellard | |
546 | 6f970bd9 | bellard | ptr = mem_buf; |
547 | 6f970bd9 | bellard | for (i = 0; i < 32; i++) |
548 | 6f970bd9 | bellard | { |
549 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->gpr[i]); |
550 | 6f970bd9 | bellard | ptr += 4;
|
551 | 6f970bd9 | bellard | } |
552 | 6f970bd9 | bellard | |
553 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->CP0_Status); |
554 | 6f970bd9 | bellard | ptr += 4;
|
555 | 6f970bd9 | bellard | |
556 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->LO); |
557 | 6f970bd9 | bellard | ptr += 4;
|
558 | 6f970bd9 | bellard | |
559 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->HI); |
560 | 6f970bd9 | bellard | ptr += 4;
|
561 | 6f970bd9 | bellard | |
562 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr); |
563 | 6f970bd9 | bellard | ptr += 4;
|
564 | 6f970bd9 | bellard | |
565 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->CP0_Cause); |
566 | 6f970bd9 | bellard | ptr += 4;
|
567 | 6f970bd9 | bellard | |
568 | 6f970bd9 | bellard | *(uint32_t *)ptr = tswapl(env->PC); |
569 | 6f970bd9 | bellard | ptr += 4;
|
570 | 6f970bd9 | bellard | |
571 | 36d23958 | ths | if (env->CP0_Config1 & (1 << CP0C1_FP)) |
572 | 8e33c08c | ths | { |
573 | 36d23958 | ths | for (i = 0; i < 32; i++) |
574 | 36d23958 | ths | { |
575 | 36d23958 | ths | *(uint32_t *)ptr = tswapl(FPR_W (env, i)); |
576 | 36d23958 | ths | ptr += 4;
|
577 | 36d23958 | ths | } |
578 | 8e33c08c | ths | |
579 | 36d23958 | ths | *(uint32_t *)ptr = tswapl(env->fcr31); |
580 | 36d23958 | ths | ptr += 4;
|
581 | 8e33c08c | ths | |
582 | 36d23958 | ths | *(uint32_t *)ptr = tswapl(env->fcr0); |
583 | 36d23958 | ths | ptr += 4;
|
584 | 36d23958 | ths | } |
585 | 8e33c08c | ths | |
586 | 6f970bd9 | bellard | /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
|
587 | 8e33c08c | ths | /* what's 'fp' mean here? */
|
588 | 6f970bd9 | bellard | |
589 | 6f970bd9 | bellard | return ptr - mem_buf;
|
590 | 6f970bd9 | bellard | } |
591 | 6f970bd9 | bellard | |
592 | 8e33c08c | ths | /* convert MIPS rounding mode in FCR31 to IEEE library */
|
593 | 8e33c08c | ths | static unsigned int ieee_rm[] = |
594 | 8e33c08c | ths | { |
595 | 8e33c08c | ths | float_round_nearest_even, |
596 | 8e33c08c | ths | float_round_to_zero, |
597 | 8e33c08c | ths | float_round_up, |
598 | 8e33c08c | ths | float_round_down |
599 | 8e33c08c | ths | }; |
600 | 8e33c08c | ths | #define RESTORE_ROUNDING_MODE \
|
601 | 8e33c08c | ths | set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
|
602 | 8e33c08c | ths | |
603 | 6f970bd9 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
604 | 6f970bd9 | bellard | { |
605 | 6f970bd9 | bellard | int i;
|
606 | 6f970bd9 | bellard | uint8_t *ptr; |
607 | 6f970bd9 | bellard | |
608 | 6f970bd9 | bellard | ptr = mem_buf; |
609 | 6f970bd9 | bellard | for (i = 0; i < 32; i++) |
610 | 6f970bd9 | bellard | { |
611 | 6f970bd9 | bellard | env->gpr[i] = tswapl(*(uint32_t *)ptr); |
612 | 6f970bd9 | bellard | ptr += 4;
|
613 | 6f970bd9 | bellard | } |
614 | 6f970bd9 | bellard | |
615 | 6f970bd9 | bellard | env->CP0_Status = tswapl(*(uint32_t *)ptr); |
616 | 6f970bd9 | bellard | ptr += 4;
|
617 | 6f970bd9 | bellard | |
618 | 6f970bd9 | bellard | env->LO = tswapl(*(uint32_t *)ptr); |
619 | 6f970bd9 | bellard | ptr += 4;
|
620 | 6f970bd9 | bellard | |
621 | 6f970bd9 | bellard | env->HI = tswapl(*(uint32_t *)ptr); |
622 | 6f970bd9 | bellard | ptr += 4;
|
623 | 6f970bd9 | bellard | |
624 | 6f970bd9 | bellard | env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr); |
625 | 6f970bd9 | bellard | ptr += 4;
|
626 | 6f970bd9 | bellard | |
627 | 6f970bd9 | bellard | env->CP0_Cause = tswapl(*(uint32_t *)ptr); |
628 | 6f970bd9 | bellard | ptr += 4;
|
629 | 6f970bd9 | bellard | |
630 | 6f970bd9 | bellard | env->PC = tswapl(*(uint32_t *)ptr); |
631 | 6f970bd9 | bellard | ptr += 4;
|
632 | 8e33c08c | ths | |
633 | 36d23958 | ths | if (env->CP0_Config1 & (1 << CP0C1_FP)) |
634 | 8e33c08c | ths | { |
635 | 36d23958 | ths | for (i = 0; i < 32; i++) |
636 | 36d23958 | ths | { |
637 | 36d23958 | ths | FPR_W (env, i) = tswapl(*(uint32_t *)ptr); |
638 | 36d23958 | ths | ptr += 4;
|
639 | 36d23958 | ths | } |
640 | 8e33c08c | ths | |
641 | 36d23958 | ths | env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
|
642 | 36d23958 | ths | ptr += 4;
|
643 | 8e33c08c | ths | |
644 | 36d23958 | ths | env->fcr0 = tswapl(*(uint32_t *)ptr); |
645 | 36d23958 | ths | ptr += 4;
|
646 | 8e33c08c | ths | |
647 | 36d23958 | ths | /* set rounding mode */
|
648 | 36d23958 | ths | RESTORE_ROUNDING_MODE; |
649 | 8e33c08c | ths | |
650 | 8e33c08c | ths | #ifndef CONFIG_SOFTFLOAT
|
651 | 36d23958 | ths | /* no floating point exception for native float */
|
652 | 36d23958 | ths | SET_FP_ENABLE(env->fcr31, 0);
|
653 | 8e33c08c | ths | #endif
|
654 | 36d23958 | ths | } |
655 | 6f970bd9 | bellard | } |
656 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
657 | fdf9b3e8 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
658 | fdf9b3e8 | bellard | { |
659 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
660 | fdf9b3e8 | bellard | int i;
|
661 | fdf9b3e8 | bellard | |
662 | fdf9b3e8 | bellard | #define SAVE(x) *ptr++=tswapl(x)
|
663 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
664 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]); |
665 | 9c2a9ea1 | pbrook | } else {
|
666 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i]); |
667 | 9c2a9ea1 | pbrook | } |
668 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) SAVE(env->gregs[i]); |
669 | fdf9b3e8 | bellard | SAVE (env->pc); |
670 | fdf9b3e8 | bellard | SAVE (env->pr); |
671 | fdf9b3e8 | bellard | SAVE (env->gbr); |
672 | fdf9b3e8 | bellard | SAVE (env->vbr); |
673 | fdf9b3e8 | bellard | SAVE (env->mach); |
674 | fdf9b3e8 | bellard | SAVE (env->macl); |
675 | fdf9b3e8 | bellard | SAVE (env->sr); |
676 | fdf9b3e8 | bellard | SAVE (0); /* TICKS */ |
677 | fdf9b3e8 | bellard | SAVE (0); /* STALLS */ |
678 | fdf9b3e8 | bellard | SAVE (0); /* CYCLES */ |
679 | fdf9b3e8 | bellard | SAVE (0); /* INSTS */ |
680 | fdf9b3e8 | bellard | SAVE (0); /* PLR */ |
681 | fdf9b3e8 | bellard | |
682 | fdf9b3e8 | bellard | return ((uint8_t *)ptr - mem_buf);
|
683 | fdf9b3e8 | bellard | } |
684 | fdf9b3e8 | bellard | |
685 | fdf9b3e8 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
686 | fdf9b3e8 | bellard | { |
687 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
688 | fdf9b3e8 | bellard | int i;
|
689 | fdf9b3e8 | bellard | |
690 | fdf9b3e8 | bellard | #define LOAD(x) (x)=*ptr++;
|
691 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
692 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]); |
693 | 9c2a9ea1 | pbrook | } else {
|
694 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i]); |
695 | 9c2a9ea1 | pbrook | } |
696 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) LOAD(env->gregs[i]); |
697 | fdf9b3e8 | bellard | LOAD (env->pc); |
698 | fdf9b3e8 | bellard | LOAD (env->pr); |
699 | fdf9b3e8 | bellard | LOAD (env->gbr); |
700 | fdf9b3e8 | bellard | LOAD (env->vbr); |
701 | fdf9b3e8 | bellard | LOAD (env->mach); |
702 | fdf9b3e8 | bellard | LOAD (env->macl); |
703 | fdf9b3e8 | bellard | LOAD (env->sr); |
704 | fdf9b3e8 | bellard | } |
705 | 1fddef4b | bellard | #else
|
706 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
707 | 6da41eaf | bellard | { |
708 | 6da41eaf | bellard | return 0; |
709 | 6da41eaf | bellard | } |
710 | 6da41eaf | bellard | |
711 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
712 | 6da41eaf | bellard | { |
713 | 6da41eaf | bellard | } |
714 | 6da41eaf | bellard | |
715 | 6da41eaf | bellard | #endif
|
716 | b4608c04 | bellard | |
717 | 1fddef4b | bellard | static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
718 | b4608c04 | bellard | { |
719 | b4608c04 | bellard | const char *p; |
720 | 858693c6 | bellard | int ch, reg_size, type;
|
721 | b4608c04 | bellard | char buf[4096]; |
722 | b4608c04 | bellard | uint8_t mem_buf[2000];
|
723 | b4608c04 | bellard | uint32_t *registers; |
724 | 9d9754a3 | bellard | target_ulong addr, len; |
725 | b4608c04 | bellard | |
726 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
727 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
728 | 858693c6 | bellard | #endif
|
729 | 858693c6 | bellard | p = line_buf; |
730 | 858693c6 | bellard | ch = *p++; |
731 | 858693c6 | bellard | switch(ch) {
|
732 | 858693c6 | bellard | case '?': |
733 | 1fddef4b | bellard | /* TODO: Make this return the correct value for user-mode. */
|
734 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
735 | 858693c6 | bellard | put_packet(s, buf); |
736 | 858693c6 | bellard | break;
|
737 | 858693c6 | bellard | case 'c': |
738 | 858693c6 | bellard | if (*p != '\0') { |
739 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
740 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
741 | 858693c6 | bellard | env->eip = addr; |
742 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
743 | 858693c6 | bellard | env->nip = addr; |
744 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
745 | 8d5f07fa | bellard | env->pc = addr; |
746 | 8d5f07fa | bellard | env->npc = addr + 4;
|
747 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
748 | b5ff1b31 | bellard | env->regs[15] = addr;
|
749 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
750 | fdf9b3e8 | bellard | env->pc = addr; |
751 | 4c3a88a2 | bellard | #endif
|
752 | 858693c6 | bellard | } |
753 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
754 | 41625033 | bellard | s->running_state = 1;
|
755 | 41625033 | bellard | #else
|
756 | 41625033 | bellard | vm_start(); |
757 | 41625033 | bellard | #endif
|
758 | 41625033 | bellard | return RS_IDLE;
|
759 | 858693c6 | bellard | case 's': |
760 | 858693c6 | bellard | if (*p != '\0') { |
761 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
762 | c33a346e | bellard | #if defined(TARGET_I386)
|
763 | 858693c6 | bellard | env->eip = addr; |
764 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
765 | 858693c6 | bellard | env->nip = addr; |
766 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
767 | 8d5f07fa | bellard | env->pc = addr; |
768 | 8d5f07fa | bellard | env->npc = addr + 4;
|
769 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
770 | b5ff1b31 | bellard | env->regs[15] = addr;
|
771 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
772 | fdf9b3e8 | bellard | env->pc = addr; |
773 | c33a346e | bellard | #endif
|
774 | 858693c6 | bellard | } |
775 | 858693c6 | bellard | cpu_single_step(env, 1);
|
776 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
777 | 41625033 | bellard | s->running_state = 1;
|
778 | 41625033 | bellard | #else
|
779 | 41625033 | bellard | vm_start(); |
780 | 41625033 | bellard | #endif
|
781 | 41625033 | bellard | return RS_IDLE;
|
782 | a2d1ebaf | pbrook | case 'F': |
783 | a2d1ebaf | pbrook | { |
784 | a2d1ebaf | pbrook | target_ulong ret; |
785 | a2d1ebaf | pbrook | target_ulong err; |
786 | a2d1ebaf | pbrook | |
787 | a2d1ebaf | pbrook | ret = strtoull(p, (char **)&p, 16); |
788 | a2d1ebaf | pbrook | if (*p == ',') { |
789 | a2d1ebaf | pbrook | p++; |
790 | a2d1ebaf | pbrook | err = strtoull(p, (char **)&p, 16); |
791 | a2d1ebaf | pbrook | } else {
|
792 | a2d1ebaf | pbrook | err = 0;
|
793 | a2d1ebaf | pbrook | } |
794 | a2d1ebaf | pbrook | if (*p == ',') |
795 | a2d1ebaf | pbrook | p++; |
796 | a2d1ebaf | pbrook | type = *p; |
797 | a2d1ebaf | pbrook | if (gdb_current_syscall_cb)
|
798 | a2d1ebaf | pbrook | gdb_current_syscall_cb(s->env, ret, err); |
799 | a2d1ebaf | pbrook | if (type == 'C') { |
800 | a2d1ebaf | pbrook | put_packet(s, "T02");
|
801 | a2d1ebaf | pbrook | } else {
|
802 | a2d1ebaf | pbrook | #ifdef CONFIG_USER_ONLY
|
803 | a2d1ebaf | pbrook | s->running_state = 1;
|
804 | a2d1ebaf | pbrook | #else
|
805 | a2d1ebaf | pbrook | vm_start(); |
806 | a2d1ebaf | pbrook | #endif
|
807 | a2d1ebaf | pbrook | } |
808 | a2d1ebaf | pbrook | } |
809 | a2d1ebaf | pbrook | break;
|
810 | 858693c6 | bellard | case 'g': |
811 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
812 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
813 | 858693c6 | bellard | put_packet(s, buf); |
814 | 858693c6 | bellard | break;
|
815 | 858693c6 | bellard | case 'G': |
816 | 858693c6 | bellard | registers = (void *)mem_buf;
|
817 | 858693c6 | bellard | len = strlen(p) / 2;
|
818 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
819 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
820 | 858693c6 | bellard | put_packet(s, "OK");
|
821 | 858693c6 | bellard | break;
|
822 | 858693c6 | bellard | case 'm': |
823 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
824 | 858693c6 | bellard | if (*p == ',') |
825 | 858693c6 | bellard | p++; |
826 | 9d9754a3 | bellard | len = strtoull(p, NULL, 16); |
827 | 6f970bd9 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) { |
828 | 6f970bd9 | bellard | put_packet (s, "E14");
|
829 | 6f970bd9 | bellard | } else {
|
830 | 6f970bd9 | bellard | memtohex(buf, mem_buf, len); |
831 | 6f970bd9 | bellard | put_packet(s, buf); |
832 | 6f970bd9 | bellard | } |
833 | 858693c6 | bellard | break;
|
834 | 858693c6 | bellard | case 'M': |
835 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
836 | 858693c6 | bellard | if (*p == ',') |
837 | 858693c6 | bellard | p++; |
838 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
839 | b328f873 | bellard | if (*p == ':') |
840 | 858693c6 | bellard | p++; |
841 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
842 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
843 | 905f20b1 | bellard | put_packet(s, "E14");
|
844 | 858693c6 | bellard | else
|
845 | 858693c6 | bellard | put_packet(s, "OK");
|
846 | 858693c6 | bellard | break;
|
847 | 858693c6 | bellard | case 'Z': |
848 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
849 | 858693c6 | bellard | if (*p == ',') |
850 | 858693c6 | bellard | p++; |
851 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
852 | 858693c6 | bellard | if (*p == ',') |
853 | 858693c6 | bellard | p++; |
854 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
855 | 858693c6 | bellard | if (type == 0 || type == 1) { |
856 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
857 | 858693c6 | bellard | goto breakpoint_error;
|
858 | 858693c6 | bellard | put_packet(s, "OK");
|
859 | 6658ffb8 | pbrook | #ifndef CONFIG_USER_ONLY
|
860 | 6658ffb8 | pbrook | } else if (type == 2) { |
861 | 6658ffb8 | pbrook | if (cpu_watchpoint_insert(env, addr) < 0) |
862 | 6658ffb8 | pbrook | goto breakpoint_error;
|
863 | 6658ffb8 | pbrook | put_packet(s, "OK");
|
864 | 6658ffb8 | pbrook | #endif
|
865 | 858693c6 | bellard | } else {
|
866 | 858693c6 | bellard | breakpoint_error:
|
867 | 905f20b1 | bellard | put_packet(s, "E22");
|
868 | 858693c6 | bellard | } |
869 | 858693c6 | bellard | break;
|
870 | 858693c6 | bellard | case 'z': |
871 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
872 | 858693c6 | bellard | if (*p == ',') |
873 | 858693c6 | bellard | p++; |
874 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
875 | 858693c6 | bellard | if (*p == ',') |
876 | 858693c6 | bellard | p++; |
877 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
878 | 858693c6 | bellard | if (type == 0 || type == 1) { |
879 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
880 | 858693c6 | bellard | put_packet(s, "OK");
|
881 | 6658ffb8 | pbrook | #ifndef CONFIG_USER_ONLY
|
882 | 6658ffb8 | pbrook | } else if (type == 2) { |
883 | 6658ffb8 | pbrook | cpu_watchpoint_remove(env, addr); |
884 | 6658ffb8 | pbrook | put_packet(s, "OK");
|
885 | 6658ffb8 | pbrook | #endif
|
886 | 858693c6 | bellard | } else {
|
887 | 858693c6 | bellard | goto breakpoint_error;
|
888 | 858693c6 | bellard | } |
889 | 858693c6 | bellard | break;
|
890 | 831b7825 | ths | #ifdef CONFIG_LINUX_USER
|
891 | 978efd6a | pbrook | case 'q': |
892 | 978efd6a | pbrook | if (strncmp(p, "Offsets", 7) == 0) { |
893 | 978efd6a | pbrook | TaskState *ts = env->opaque; |
894 | 978efd6a | pbrook | |
895 | 978efd6a | pbrook | sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
|
896 | 978efd6a | pbrook | ts->info->data_offset, ts->info->data_offset); |
897 | 978efd6a | pbrook | put_packet(s, buf); |
898 | 978efd6a | pbrook | break;
|
899 | 978efd6a | pbrook | } |
900 | 978efd6a | pbrook | /* Fall through. */
|
901 | 978efd6a | pbrook | #endif
|
902 | 858693c6 | bellard | default:
|
903 | 858693c6 | bellard | // unknown_command:
|
904 | 858693c6 | bellard | /* put empty packet */
|
905 | 858693c6 | bellard | buf[0] = '\0'; |
906 | 858693c6 | bellard | put_packet(s, buf); |
907 | 858693c6 | bellard | break;
|
908 | 858693c6 | bellard | } |
909 | 858693c6 | bellard | return RS_IDLE;
|
910 | 858693c6 | bellard | } |
911 | 858693c6 | bellard | |
912 | 612458f5 | bellard | extern void tb_flush(CPUState *env); |
913 | 612458f5 | bellard | |
914 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
915 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
916 | 858693c6 | bellard | { |
917 | 858693c6 | bellard | GDBState *s = opaque; |
918 | 858693c6 | bellard | char buf[256]; |
919 | 858693c6 | bellard | int ret;
|
920 | 858693c6 | bellard | |
921 | a2d1ebaf | pbrook | if (s->state == RS_SYSCALL)
|
922 | a2d1ebaf | pbrook | return;
|
923 | a2d1ebaf | pbrook | |
924 | 858693c6 | bellard | /* disable single step if it was enable */
|
925 | 6a00d601 | bellard | cpu_single_step(s->env, 0);
|
926 | 858693c6 | bellard | |
927 | e80cfcfc | bellard | if (reason == EXCP_DEBUG) {
|
928 | 6658ffb8 | pbrook | if (s->env->watchpoint_hit) {
|
929 | 6658ffb8 | pbrook | snprintf(buf, sizeof(buf), "T%02xwatch:%x;", SIGTRAP, |
930 | 6658ffb8 | pbrook | s->env->watchpoint[s->env->watchpoint_hit - 1].vaddr);
|
931 | 6658ffb8 | pbrook | put_packet(s, buf); |
932 | 6658ffb8 | pbrook | s->env->watchpoint_hit = 0;
|
933 | 6658ffb8 | pbrook | return;
|
934 | 6658ffb8 | pbrook | } |
935 | 6a00d601 | bellard | tb_flush(s->env); |
936 | 858693c6 | bellard | ret = SIGTRAP; |
937 | bbeb7b5c | bellard | } else if (reason == EXCP_INTERRUPT) { |
938 | bbeb7b5c | bellard | ret = SIGINT; |
939 | bbeb7b5c | bellard | } else {
|
940 | 858693c6 | bellard | ret = 0;
|
941 | bbeb7b5c | bellard | } |
942 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
943 | 858693c6 | bellard | put_packet(s, buf); |
944 | 858693c6 | bellard | } |
945 | 1fddef4b | bellard | #endif
|
946 | 858693c6 | bellard | |
947 | a2d1ebaf | pbrook | /* Send a gdb syscall request.
|
948 | a2d1ebaf | pbrook | This accepts limited printf-style format specifiers, specifically:
|
949 | a2d1ebaf | pbrook | %x - target_ulong argument printed in hex.
|
950 | a2d1ebaf | pbrook | %s - string pointer (target_ulong) and length (int) pair. */
|
951 | a2d1ebaf | pbrook | void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...) |
952 | a2d1ebaf | pbrook | { |
953 | a2d1ebaf | pbrook | va_list va; |
954 | a2d1ebaf | pbrook | char buf[256]; |
955 | a2d1ebaf | pbrook | char *p;
|
956 | a2d1ebaf | pbrook | target_ulong addr; |
957 | a2d1ebaf | pbrook | GDBState *s; |
958 | a2d1ebaf | pbrook | |
959 | a2d1ebaf | pbrook | s = gdb_syscall_state; |
960 | a2d1ebaf | pbrook | if (!s)
|
961 | a2d1ebaf | pbrook | return;
|
962 | a2d1ebaf | pbrook | gdb_current_syscall_cb = cb; |
963 | a2d1ebaf | pbrook | s->state = RS_SYSCALL; |
964 | a2d1ebaf | pbrook | #ifndef CONFIG_USER_ONLY
|
965 | a2d1ebaf | pbrook | vm_stop(EXCP_DEBUG); |
966 | a2d1ebaf | pbrook | #endif
|
967 | a2d1ebaf | pbrook | s->state = RS_IDLE; |
968 | a2d1ebaf | pbrook | va_start(va, fmt); |
969 | a2d1ebaf | pbrook | p = buf; |
970 | a2d1ebaf | pbrook | *(p++) = 'F';
|
971 | a2d1ebaf | pbrook | while (*fmt) {
|
972 | a2d1ebaf | pbrook | if (*fmt == '%') { |
973 | a2d1ebaf | pbrook | fmt++; |
974 | a2d1ebaf | pbrook | switch (*fmt++) {
|
975 | a2d1ebaf | pbrook | case 'x': |
976 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
977 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx, addr); |
978 | a2d1ebaf | pbrook | break;
|
979 | a2d1ebaf | pbrook | case 's': |
980 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
981 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int)); |
982 | a2d1ebaf | pbrook | break;
|
983 | a2d1ebaf | pbrook | default:
|
984 | a2d1ebaf | pbrook | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
|
985 | a2d1ebaf | pbrook | fmt - 1);
|
986 | a2d1ebaf | pbrook | break;
|
987 | a2d1ebaf | pbrook | } |
988 | a2d1ebaf | pbrook | } else {
|
989 | a2d1ebaf | pbrook | *(p++) = *(fmt++); |
990 | a2d1ebaf | pbrook | } |
991 | a2d1ebaf | pbrook | } |
992 | a2d1ebaf | pbrook | va_end(va); |
993 | a2d1ebaf | pbrook | put_packet(s, buf); |
994 | a2d1ebaf | pbrook | #ifdef CONFIG_USER_ONLY
|
995 | a2d1ebaf | pbrook | gdb_handlesig(s->env, 0);
|
996 | a2d1ebaf | pbrook | #else
|
997 | a2d1ebaf | pbrook | cpu_interrupt(s->env, CPU_INTERRUPT_EXIT); |
998 | a2d1ebaf | pbrook | #endif
|
999 | a2d1ebaf | pbrook | } |
1000 | a2d1ebaf | pbrook | |
1001 | 6a00d601 | bellard | static void gdb_read_byte(GDBState *s, int ch) |
1002 | 858693c6 | bellard | { |
1003 | 6a00d601 | bellard | CPUState *env = s->env; |
1004 | 858693c6 | bellard | int i, csum;
|
1005 | 858693c6 | bellard | char reply[1]; |
1006 | 858693c6 | bellard | |
1007 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
1008 | 4046d913 | pbrook | if (s->last_packet_len) {
|
1009 | 4046d913 | pbrook | /* Waiting for a response to the last packet. If we see the start
|
1010 | 4046d913 | pbrook | of a new command then abandon the previous response. */
|
1011 | 4046d913 | pbrook | if (ch == '-') { |
1012 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
1013 | 4046d913 | pbrook | printf("Got NACK, retransmitting\n");
|
1014 | 4046d913 | pbrook | #endif
|
1015 | 4046d913 | pbrook | put_buffer(s, s->last_packet, s->last_packet_len); |
1016 | 4046d913 | pbrook | } |
1017 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
1018 | 4046d913 | pbrook | else if (ch == '+') |
1019 | 4046d913 | pbrook | printf("Got ACK\n");
|
1020 | 4046d913 | pbrook | else
|
1021 | 4046d913 | pbrook | printf("Got '%c' when expecting ACK/NACK\n", ch);
|
1022 | 4046d913 | pbrook | #endif
|
1023 | 4046d913 | pbrook | if (ch == '+' || ch == '$') |
1024 | 4046d913 | pbrook | s->last_packet_len = 0;
|
1025 | 4046d913 | pbrook | if (ch != '$') |
1026 | 4046d913 | pbrook | return;
|
1027 | 4046d913 | pbrook | } |
1028 | 858693c6 | bellard | if (vm_running) {
|
1029 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
1030 | 858693c6 | bellard | it when receiving a char */
|
1031 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
1032 | 41625033 | bellard | } else
|
1033 | 1fddef4b | bellard | #endif
|
1034 | 41625033 | bellard | { |
1035 | 858693c6 | bellard | switch(s->state) {
|
1036 | 858693c6 | bellard | case RS_IDLE:
|
1037 | 858693c6 | bellard | if (ch == '$') { |
1038 | 858693c6 | bellard | s->line_buf_index = 0;
|
1039 | 858693c6 | bellard | s->state = RS_GETLINE; |
1040 | c33a346e | bellard | } |
1041 | b4608c04 | bellard | break;
|
1042 | 858693c6 | bellard | case RS_GETLINE:
|
1043 | 858693c6 | bellard | if (ch == '#') { |
1044 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
1045 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
1046 | 858693c6 | bellard | s->state = RS_IDLE; |
1047 | 4c3a88a2 | bellard | } else {
|
1048 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
1049 | 4c3a88a2 | bellard | } |
1050 | 4c3a88a2 | bellard | break;
|
1051 | 858693c6 | bellard | case RS_CHKSUM1:
|
1052 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
1053 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
1054 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
1055 | 858693c6 | bellard | break;
|
1056 | 858693c6 | bellard | case RS_CHKSUM2:
|
1057 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
1058 | 858693c6 | bellard | csum = 0;
|
1059 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
1060 | 858693c6 | bellard | csum += s->line_buf[i]; |
1061 | 858693c6 | bellard | } |
1062 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
1063 | 858693c6 | bellard | reply[0] = '-'; |
1064 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
1065 | 858693c6 | bellard | s->state = RS_IDLE; |
1066 | 4c3a88a2 | bellard | } else {
|
1067 | 858693c6 | bellard | reply[0] = '+'; |
1068 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
1069 | 1fddef4b | bellard | s->state = gdb_handle_packet(s, env, s->line_buf); |
1070 | 4c3a88a2 | bellard | } |
1071 | 4c3a88a2 | bellard | break;
|
1072 | a2d1ebaf | pbrook | default:
|
1073 | a2d1ebaf | pbrook | abort(); |
1074 | 858693c6 | bellard | } |
1075 | 858693c6 | bellard | } |
1076 | 858693c6 | bellard | } |
1077 | 858693c6 | bellard | |
1078 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
1079 | 1fddef4b | bellard | int
|
1080 | 1fddef4b | bellard | gdb_handlesig (CPUState *env, int sig)
|
1081 | 1fddef4b | bellard | { |
1082 | 1fddef4b | bellard | GDBState *s; |
1083 | 1fddef4b | bellard | char buf[256]; |
1084 | 1fddef4b | bellard | int n;
|
1085 | 1fddef4b | bellard | |
1086 | 1fddef4b | bellard | if (gdbserver_fd < 0) |
1087 | 1fddef4b | bellard | return sig;
|
1088 | 1fddef4b | bellard | |
1089 | 1fddef4b | bellard | s = &gdbserver_state; |
1090 | 1fddef4b | bellard | |
1091 | 1fddef4b | bellard | /* disable single step if it was enabled */
|
1092 | 1fddef4b | bellard | cpu_single_step(env, 0);
|
1093 | 1fddef4b | bellard | tb_flush(env); |
1094 | 1fddef4b | bellard | |
1095 | 1fddef4b | bellard | if (sig != 0) |
1096 | 1fddef4b | bellard | { |
1097 | 1fddef4b | bellard | snprintf(buf, sizeof(buf), "S%02x", sig); |
1098 | 1fddef4b | bellard | put_packet(s, buf); |
1099 | 1fddef4b | bellard | } |
1100 | 1fddef4b | bellard | |
1101 | 1fddef4b | bellard | sig = 0;
|
1102 | 1fddef4b | bellard | s->state = RS_IDLE; |
1103 | 41625033 | bellard | s->running_state = 0;
|
1104 | 41625033 | bellard | while (s->running_state == 0) { |
1105 | 1fddef4b | bellard | n = read (s->fd, buf, 256);
|
1106 | 1fddef4b | bellard | if (n > 0) |
1107 | 1fddef4b | bellard | { |
1108 | 1fddef4b | bellard | int i;
|
1109 | 1fddef4b | bellard | |
1110 | 1fddef4b | bellard | for (i = 0; i < n; i++) |
1111 | 6a00d601 | bellard | gdb_read_byte (s, buf[i]); |
1112 | 1fddef4b | bellard | } |
1113 | 1fddef4b | bellard | else if (n == 0 || errno != EAGAIN) |
1114 | 1fddef4b | bellard | { |
1115 | 1fddef4b | bellard | /* XXX: Connection closed. Should probably wait for annother
|
1116 | 1fddef4b | bellard | connection before continuing. */
|
1117 | 1fddef4b | bellard | return sig;
|
1118 | 1fddef4b | bellard | } |
1119 | 41625033 | bellard | } |
1120 | 1fddef4b | bellard | return sig;
|
1121 | 1fddef4b | bellard | } |
1122 | e9009676 | bellard | |
1123 | e9009676 | bellard | /* Tell the remote gdb that the process has exited. */
|
1124 | e9009676 | bellard | void gdb_exit(CPUState *env, int code) |
1125 | e9009676 | bellard | { |
1126 | e9009676 | bellard | GDBState *s; |
1127 | e9009676 | bellard | char buf[4]; |
1128 | e9009676 | bellard | |
1129 | e9009676 | bellard | if (gdbserver_fd < 0) |
1130 | e9009676 | bellard | return;
|
1131 | e9009676 | bellard | |
1132 | e9009676 | bellard | s = &gdbserver_state; |
1133 | e9009676 | bellard | |
1134 | e9009676 | bellard | snprintf(buf, sizeof(buf), "W%02x", code); |
1135 | e9009676 | bellard | put_packet(s, buf); |
1136 | e9009676 | bellard | } |
1137 | e9009676 | bellard | |
1138 | 1fddef4b | bellard | |
1139 | 7c9d8e07 | bellard | static void gdb_accept(void *opaque) |
1140 | 858693c6 | bellard | { |
1141 | 858693c6 | bellard | GDBState *s; |
1142 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1143 | 858693c6 | bellard | socklen_t len; |
1144 | 858693c6 | bellard | int val, fd;
|
1145 | 858693c6 | bellard | |
1146 | 858693c6 | bellard | for(;;) {
|
1147 | 858693c6 | bellard | len = sizeof(sockaddr);
|
1148 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
1149 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
1150 | 858693c6 | bellard | perror("accept");
|
1151 | 858693c6 | bellard | return;
|
1152 | 858693c6 | bellard | } else if (fd >= 0) { |
1153 | b4608c04 | bellard | break;
|
1154 | b4608c04 | bellard | } |
1155 | b4608c04 | bellard | } |
1156 | 858693c6 | bellard | |
1157 | 858693c6 | bellard | /* set short latency */
|
1158 | 858693c6 | bellard | val = 1;
|
1159 | 8f447cc7 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
1160 | 858693c6 | bellard | |
1161 | 1fddef4b | bellard | s = &gdbserver_state; |
1162 | 1fddef4b | bellard | memset (s, 0, sizeof (GDBState)); |
1163 | 6a00d601 | bellard | s->env = first_cpu; /* XXX: allow to change CPU */
|
1164 | 858693c6 | bellard | s->fd = fd; |
1165 | 858693c6 | bellard | |
1166 | a2d1ebaf | pbrook | gdb_syscall_state = s; |
1167 | a2d1ebaf | pbrook | |
1168 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
1169 | 858693c6 | bellard | } |
1170 | 858693c6 | bellard | |
1171 | 858693c6 | bellard | static int gdbserver_open(int port) |
1172 | 858693c6 | bellard | { |
1173 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1174 | 858693c6 | bellard | int fd, val, ret;
|
1175 | 858693c6 | bellard | |
1176 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
1177 | 858693c6 | bellard | if (fd < 0) { |
1178 | 858693c6 | bellard | perror("socket");
|
1179 | 858693c6 | bellard | return -1; |
1180 | 858693c6 | bellard | } |
1181 | 858693c6 | bellard | |
1182 | 858693c6 | bellard | /* allow fast reuse */
|
1183 | 858693c6 | bellard | val = 1;
|
1184 | 8f447cc7 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
1185 | 858693c6 | bellard | |
1186 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
1187 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
1188 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
1189 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
1190 | 858693c6 | bellard | if (ret < 0) { |
1191 | 858693c6 | bellard | perror("bind");
|
1192 | 858693c6 | bellard | return -1; |
1193 | 858693c6 | bellard | } |
1194 | 858693c6 | bellard | ret = listen(fd, 0);
|
1195 | 858693c6 | bellard | if (ret < 0) { |
1196 | 858693c6 | bellard | perror("listen");
|
1197 | 858693c6 | bellard | return -1; |
1198 | 858693c6 | bellard | } |
1199 | 858693c6 | bellard | return fd;
|
1200 | 858693c6 | bellard | } |
1201 | 858693c6 | bellard | |
1202 | 858693c6 | bellard | int gdbserver_start(int port) |
1203 | 858693c6 | bellard | { |
1204 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
1205 | 858693c6 | bellard | if (gdbserver_fd < 0) |
1206 | 858693c6 | bellard | return -1; |
1207 | 858693c6 | bellard | /* accept connections */
|
1208 | 7c9d8e07 | bellard | gdb_accept (NULL);
|
1209 | 4046d913 | pbrook | return 0; |
1210 | 4046d913 | pbrook | } |
1211 | 1fddef4b | bellard | #else
|
1212 | 4046d913 | pbrook | static int gdb_chr_can_recieve(void *opaque) |
1213 | 4046d913 | pbrook | { |
1214 | 4046d913 | pbrook | return 1; |
1215 | 4046d913 | pbrook | } |
1216 | 4046d913 | pbrook | |
1217 | 4046d913 | pbrook | static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size) |
1218 | 4046d913 | pbrook | { |
1219 | 4046d913 | pbrook | GDBState *s = opaque; |
1220 | 4046d913 | pbrook | int i;
|
1221 | 4046d913 | pbrook | |
1222 | 4046d913 | pbrook | for (i = 0; i < size; i++) { |
1223 | 4046d913 | pbrook | gdb_read_byte(s, buf[i]); |
1224 | 4046d913 | pbrook | } |
1225 | 4046d913 | pbrook | } |
1226 | 4046d913 | pbrook | |
1227 | 4046d913 | pbrook | static void gdb_chr_event(void *opaque, int event) |
1228 | 4046d913 | pbrook | { |
1229 | 4046d913 | pbrook | switch (event) {
|
1230 | 4046d913 | pbrook | case CHR_EVENT_RESET:
|
1231 | 4046d913 | pbrook | vm_stop(EXCP_INTERRUPT); |
1232 | a2d1ebaf | pbrook | gdb_syscall_state = opaque; |
1233 | 4046d913 | pbrook | break;
|
1234 | 4046d913 | pbrook | default:
|
1235 | 4046d913 | pbrook | break;
|
1236 | 4046d913 | pbrook | } |
1237 | 4046d913 | pbrook | } |
1238 | 4046d913 | pbrook | |
1239 | cfc3475a | pbrook | int gdbserver_start(const char *port) |
1240 | 4046d913 | pbrook | { |
1241 | 4046d913 | pbrook | GDBState *s; |
1242 | cfc3475a | pbrook | char gdbstub_port_name[128]; |
1243 | cfc3475a | pbrook | int port_num;
|
1244 | cfc3475a | pbrook | char *p;
|
1245 | cfc3475a | pbrook | CharDriverState *chr; |
1246 | cfc3475a | pbrook | |
1247 | cfc3475a | pbrook | if (!port || !*port)
|
1248 | cfc3475a | pbrook | return -1; |
1249 | 4046d913 | pbrook | |
1250 | cfc3475a | pbrook | port_num = strtol(port, &p, 10);
|
1251 | cfc3475a | pbrook | if (*p == 0) { |
1252 | cfc3475a | pbrook | /* A numeric value is interpreted as a port number. */
|
1253 | cfc3475a | pbrook | snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
|
1254 | cfc3475a | pbrook | "tcp::%d,nowait,nodelay,server", port_num);
|
1255 | cfc3475a | pbrook | port = gdbstub_port_name; |
1256 | cfc3475a | pbrook | } |
1257 | cfc3475a | pbrook | |
1258 | cfc3475a | pbrook | chr = qemu_chr_open(port); |
1259 | 4046d913 | pbrook | if (!chr)
|
1260 | 4046d913 | pbrook | return -1; |
1261 | 4046d913 | pbrook | |
1262 | 4046d913 | pbrook | s = qemu_mallocz(sizeof(GDBState));
|
1263 | 4046d913 | pbrook | if (!s) {
|
1264 | 4046d913 | pbrook | return -1; |
1265 | 4046d913 | pbrook | } |
1266 | 4046d913 | pbrook | s->env = first_cpu; /* XXX: allow to change CPU */
|
1267 | 4046d913 | pbrook | s->chr = chr; |
1268 | 4046d913 | pbrook | qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve, |
1269 | 4046d913 | pbrook | gdb_chr_event, s); |
1270 | 4046d913 | pbrook | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
1271 | b4608c04 | bellard | return 0; |
1272 | b4608c04 | bellard | } |
1273 | 4046d913 | pbrook | #endif |