root / gdbstub.c @ 7ed40acf
History | View | Annotate | Download (30.1 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 | 3fc6c082 | bellard | registers[101] = tswapl(do_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 | 3fc6c082 | bellard | do_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 | 8e33c08c | ths | #ifdef MIPS_USES_FPU
|
572 | 8e33c08c | ths | for (i = 0; i < 32; i++) |
573 | 8e33c08c | ths | { |
574 | 8e33c08c | ths | *(uint32_t *)ptr = tswapl(FPR_W (env, i)); |
575 | 8e33c08c | ths | ptr += 4;
|
576 | 8e33c08c | ths | } |
577 | 8e33c08c | ths | |
578 | 8e33c08c | ths | *(uint32_t *)ptr = tswapl(env->fcr31); |
579 | 8e33c08c | ths | ptr += 4;
|
580 | 8e33c08c | ths | |
581 | 8e33c08c | ths | *(uint32_t *)ptr = tswapl(env->fcr0); |
582 | 8e33c08c | ths | ptr += 4;
|
583 | 8e33c08c | ths | #endif
|
584 | 8e33c08c | ths | |
585 | 6f970bd9 | bellard | /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
|
586 | 8e33c08c | ths | /* what's 'fp' mean here? */
|
587 | 6f970bd9 | bellard | |
588 | 6f970bd9 | bellard | return ptr - mem_buf;
|
589 | 6f970bd9 | bellard | } |
590 | 6f970bd9 | bellard | |
591 | 8e33c08c | ths | /* convert MIPS rounding mode in FCR31 to IEEE library */
|
592 | 8e33c08c | ths | static unsigned int ieee_rm[] = |
593 | 8e33c08c | ths | { |
594 | 8e33c08c | ths | float_round_nearest_even, |
595 | 8e33c08c | ths | float_round_to_zero, |
596 | 8e33c08c | ths | float_round_up, |
597 | 8e33c08c | ths | float_round_down |
598 | 8e33c08c | ths | }; |
599 | 8e33c08c | ths | #define RESTORE_ROUNDING_MODE \
|
600 | 8e33c08c | ths | set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
|
601 | 8e33c08c | ths | |
602 | 6f970bd9 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
603 | 6f970bd9 | bellard | { |
604 | 6f970bd9 | bellard | int i;
|
605 | 6f970bd9 | bellard | uint8_t *ptr; |
606 | 6f970bd9 | bellard | |
607 | 6f970bd9 | bellard | ptr = mem_buf; |
608 | 6f970bd9 | bellard | for (i = 0; i < 32; i++) |
609 | 6f970bd9 | bellard | { |
610 | 6f970bd9 | bellard | env->gpr[i] = tswapl(*(uint32_t *)ptr); |
611 | 6f970bd9 | bellard | ptr += 4;
|
612 | 6f970bd9 | bellard | } |
613 | 6f970bd9 | bellard | |
614 | 6f970bd9 | bellard | env->CP0_Status = tswapl(*(uint32_t *)ptr); |
615 | 6f970bd9 | bellard | ptr += 4;
|
616 | 6f970bd9 | bellard | |
617 | 6f970bd9 | bellard | env->LO = tswapl(*(uint32_t *)ptr); |
618 | 6f970bd9 | bellard | ptr += 4;
|
619 | 6f970bd9 | bellard | |
620 | 6f970bd9 | bellard | env->HI = tswapl(*(uint32_t *)ptr); |
621 | 6f970bd9 | bellard | ptr += 4;
|
622 | 6f970bd9 | bellard | |
623 | 6f970bd9 | bellard | env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr); |
624 | 6f970bd9 | bellard | ptr += 4;
|
625 | 6f970bd9 | bellard | |
626 | 6f970bd9 | bellard | env->CP0_Cause = tswapl(*(uint32_t *)ptr); |
627 | 6f970bd9 | bellard | ptr += 4;
|
628 | 6f970bd9 | bellard | |
629 | 6f970bd9 | bellard | env->PC = tswapl(*(uint32_t *)ptr); |
630 | 6f970bd9 | bellard | ptr += 4;
|
631 | 8e33c08c | ths | |
632 | 8e33c08c | ths | #ifdef MIPS_USES_FPU
|
633 | 8e33c08c | ths | for (i = 0; i < 32; i++) |
634 | 8e33c08c | ths | { |
635 | 8e33c08c | ths | FPR_W (env, i) = tswapl(*(uint32_t *)ptr); |
636 | 8e33c08c | ths | ptr += 4;
|
637 | 8e33c08c | ths | } |
638 | 8e33c08c | ths | |
639 | 8e33c08c | ths | env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
|
640 | 8e33c08c | ths | ptr += 4;
|
641 | 8e33c08c | ths | |
642 | 8e33c08c | ths | env->fcr0 = tswapl(*(uint32_t *)ptr); |
643 | 8e33c08c | ths | ptr += 4;
|
644 | 8e33c08c | ths | |
645 | 8e33c08c | ths | /* set rounding mode */
|
646 | 8e33c08c | ths | RESTORE_ROUNDING_MODE; |
647 | 8e33c08c | ths | |
648 | 8e33c08c | ths | #ifndef CONFIG_SOFTFLOAT
|
649 | 8e33c08c | ths | /* no floating point exception for native float */
|
650 | 8e33c08c | ths | SET_FP_ENABLE(env->fcr31, 0);
|
651 | 8e33c08c | ths | #endif
|
652 | 8e33c08c | ths | #endif
|
653 | 6f970bd9 | bellard | } |
654 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
655 | fdf9b3e8 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
656 | fdf9b3e8 | bellard | { |
657 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
658 | fdf9b3e8 | bellard | int i;
|
659 | fdf9b3e8 | bellard | |
660 | fdf9b3e8 | bellard | #define SAVE(x) *ptr++=tswapl(x)
|
661 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
662 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]); |
663 | 9c2a9ea1 | pbrook | } else {
|
664 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) SAVE(env->gregs[i]); |
665 | 9c2a9ea1 | pbrook | } |
666 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) SAVE(env->gregs[i]); |
667 | fdf9b3e8 | bellard | SAVE (env->pc); |
668 | fdf9b3e8 | bellard | SAVE (env->pr); |
669 | fdf9b3e8 | bellard | SAVE (env->gbr); |
670 | fdf9b3e8 | bellard | SAVE (env->vbr); |
671 | fdf9b3e8 | bellard | SAVE (env->mach); |
672 | fdf9b3e8 | bellard | SAVE (env->macl); |
673 | fdf9b3e8 | bellard | SAVE (env->sr); |
674 | fdf9b3e8 | bellard | SAVE (0); /* TICKS */ |
675 | fdf9b3e8 | bellard | SAVE (0); /* STALLS */ |
676 | fdf9b3e8 | bellard | SAVE (0); /* CYCLES */ |
677 | fdf9b3e8 | bellard | SAVE (0); /* INSTS */ |
678 | fdf9b3e8 | bellard | SAVE (0); /* PLR */ |
679 | fdf9b3e8 | bellard | |
680 | fdf9b3e8 | bellard | return ((uint8_t *)ptr - mem_buf);
|
681 | fdf9b3e8 | bellard | } |
682 | fdf9b3e8 | bellard | |
683 | fdf9b3e8 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
684 | fdf9b3e8 | bellard | { |
685 | fdf9b3e8 | bellard | uint32_t *ptr = (uint32_t *)mem_buf; |
686 | fdf9b3e8 | bellard | int i;
|
687 | fdf9b3e8 | bellard | |
688 | fdf9b3e8 | bellard | #define LOAD(x) (x)=*ptr++;
|
689 | 9c2a9ea1 | pbrook | if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
|
690 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]); |
691 | 9c2a9ea1 | pbrook | } else {
|
692 | 9c2a9ea1 | pbrook | for (i = 0; i < 8; i++) LOAD(env->gregs[i]); |
693 | 9c2a9ea1 | pbrook | } |
694 | 9c2a9ea1 | pbrook | for (i = 8; i < 16; i++) LOAD(env->gregs[i]); |
695 | fdf9b3e8 | bellard | LOAD (env->pc); |
696 | fdf9b3e8 | bellard | LOAD (env->pr); |
697 | fdf9b3e8 | bellard | LOAD (env->gbr); |
698 | fdf9b3e8 | bellard | LOAD (env->vbr); |
699 | fdf9b3e8 | bellard | LOAD (env->mach); |
700 | fdf9b3e8 | bellard | LOAD (env->macl); |
701 | fdf9b3e8 | bellard | LOAD (env->sr); |
702 | fdf9b3e8 | bellard | } |
703 | 1fddef4b | bellard | #else
|
704 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
705 | 6da41eaf | bellard | { |
706 | 6da41eaf | bellard | return 0; |
707 | 6da41eaf | bellard | } |
708 | 6da41eaf | bellard | |
709 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
710 | 6da41eaf | bellard | { |
711 | 6da41eaf | bellard | } |
712 | 6da41eaf | bellard | |
713 | 6da41eaf | bellard | #endif
|
714 | b4608c04 | bellard | |
715 | 1fddef4b | bellard | static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
716 | b4608c04 | bellard | { |
717 | b4608c04 | bellard | const char *p; |
718 | 858693c6 | bellard | int ch, reg_size, type;
|
719 | b4608c04 | bellard | char buf[4096]; |
720 | b4608c04 | bellard | uint8_t mem_buf[2000];
|
721 | b4608c04 | bellard | uint32_t *registers; |
722 | 9d9754a3 | bellard | target_ulong addr, len; |
723 | b4608c04 | bellard | |
724 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
725 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
726 | 858693c6 | bellard | #endif
|
727 | 858693c6 | bellard | p = line_buf; |
728 | 858693c6 | bellard | ch = *p++; |
729 | 858693c6 | bellard | switch(ch) {
|
730 | 858693c6 | bellard | case '?': |
731 | 1fddef4b | bellard | /* TODO: Make this return the correct value for user-mode. */
|
732 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
733 | 858693c6 | bellard | put_packet(s, buf); |
734 | 858693c6 | bellard | break;
|
735 | 858693c6 | bellard | case 'c': |
736 | 858693c6 | bellard | if (*p != '\0') { |
737 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
738 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
739 | 858693c6 | bellard | env->eip = addr; |
740 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
741 | 858693c6 | bellard | env->nip = addr; |
742 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
743 | 8d5f07fa | bellard | env->pc = addr; |
744 | 8d5f07fa | bellard | env->npc = addr + 4;
|
745 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
746 | b5ff1b31 | bellard | env->regs[15] = addr;
|
747 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
748 | fdf9b3e8 | bellard | env->pc = addr; |
749 | 4c3a88a2 | bellard | #endif
|
750 | 858693c6 | bellard | } |
751 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
752 | 41625033 | bellard | s->running_state = 1;
|
753 | 41625033 | bellard | #else
|
754 | 41625033 | bellard | vm_start(); |
755 | 41625033 | bellard | #endif
|
756 | 41625033 | bellard | return RS_IDLE;
|
757 | 858693c6 | bellard | case 's': |
758 | 858693c6 | bellard | if (*p != '\0') { |
759 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
760 | c33a346e | bellard | #if defined(TARGET_I386)
|
761 | 858693c6 | bellard | env->eip = addr; |
762 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
763 | 858693c6 | bellard | env->nip = addr; |
764 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
765 | 8d5f07fa | bellard | env->pc = addr; |
766 | 8d5f07fa | bellard | env->npc = addr + 4;
|
767 | b5ff1b31 | bellard | #elif defined (TARGET_ARM)
|
768 | b5ff1b31 | bellard | env->regs[15] = addr;
|
769 | fdf9b3e8 | bellard | #elif defined (TARGET_SH4)
|
770 | fdf9b3e8 | bellard | env->pc = addr; |
771 | c33a346e | bellard | #endif
|
772 | 858693c6 | bellard | } |
773 | 858693c6 | bellard | cpu_single_step(env, 1);
|
774 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
775 | 41625033 | bellard | s->running_state = 1;
|
776 | 41625033 | bellard | #else
|
777 | 41625033 | bellard | vm_start(); |
778 | 41625033 | bellard | #endif
|
779 | 41625033 | bellard | return RS_IDLE;
|
780 | a2d1ebaf | pbrook | case 'F': |
781 | a2d1ebaf | pbrook | { |
782 | a2d1ebaf | pbrook | target_ulong ret; |
783 | a2d1ebaf | pbrook | target_ulong err; |
784 | a2d1ebaf | pbrook | |
785 | a2d1ebaf | pbrook | ret = strtoull(p, (char **)&p, 16); |
786 | a2d1ebaf | pbrook | if (*p == ',') { |
787 | a2d1ebaf | pbrook | p++; |
788 | a2d1ebaf | pbrook | err = strtoull(p, (char **)&p, 16); |
789 | a2d1ebaf | pbrook | } else {
|
790 | a2d1ebaf | pbrook | err = 0;
|
791 | a2d1ebaf | pbrook | } |
792 | a2d1ebaf | pbrook | if (*p == ',') |
793 | a2d1ebaf | pbrook | p++; |
794 | a2d1ebaf | pbrook | type = *p; |
795 | a2d1ebaf | pbrook | if (gdb_current_syscall_cb)
|
796 | a2d1ebaf | pbrook | gdb_current_syscall_cb(s->env, ret, err); |
797 | a2d1ebaf | pbrook | if (type == 'C') { |
798 | a2d1ebaf | pbrook | put_packet(s, "T02");
|
799 | a2d1ebaf | pbrook | } else {
|
800 | a2d1ebaf | pbrook | #ifdef CONFIG_USER_ONLY
|
801 | a2d1ebaf | pbrook | s->running_state = 1;
|
802 | a2d1ebaf | pbrook | #else
|
803 | a2d1ebaf | pbrook | vm_start(); |
804 | a2d1ebaf | pbrook | #endif
|
805 | a2d1ebaf | pbrook | } |
806 | a2d1ebaf | pbrook | } |
807 | a2d1ebaf | pbrook | break;
|
808 | 858693c6 | bellard | case 'g': |
809 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
810 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
811 | 858693c6 | bellard | put_packet(s, buf); |
812 | 858693c6 | bellard | break;
|
813 | 858693c6 | bellard | case 'G': |
814 | 858693c6 | bellard | registers = (void *)mem_buf;
|
815 | 858693c6 | bellard | len = strlen(p) / 2;
|
816 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
817 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
818 | 858693c6 | bellard | put_packet(s, "OK");
|
819 | 858693c6 | bellard | break;
|
820 | 858693c6 | bellard | case 'm': |
821 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
822 | 858693c6 | bellard | if (*p == ',') |
823 | 858693c6 | bellard | p++; |
824 | 9d9754a3 | bellard | len = strtoull(p, NULL, 16); |
825 | 6f970bd9 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) { |
826 | 6f970bd9 | bellard | put_packet (s, "E14");
|
827 | 6f970bd9 | bellard | } else {
|
828 | 6f970bd9 | bellard | memtohex(buf, mem_buf, len); |
829 | 6f970bd9 | bellard | put_packet(s, buf); |
830 | 6f970bd9 | bellard | } |
831 | 858693c6 | bellard | break;
|
832 | 858693c6 | bellard | case 'M': |
833 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
834 | 858693c6 | bellard | if (*p == ',') |
835 | 858693c6 | bellard | p++; |
836 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
837 | b328f873 | bellard | if (*p == ':') |
838 | 858693c6 | bellard | p++; |
839 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
840 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
841 | 905f20b1 | bellard | put_packet(s, "E14");
|
842 | 858693c6 | bellard | else
|
843 | 858693c6 | bellard | put_packet(s, "OK");
|
844 | 858693c6 | bellard | break;
|
845 | 858693c6 | bellard | case 'Z': |
846 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
847 | 858693c6 | bellard | if (*p == ',') |
848 | 858693c6 | bellard | p++; |
849 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
850 | 858693c6 | bellard | if (*p == ',') |
851 | 858693c6 | bellard | p++; |
852 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
853 | 858693c6 | bellard | if (type == 0 || type == 1) { |
854 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
855 | 858693c6 | bellard | goto breakpoint_error;
|
856 | 858693c6 | bellard | put_packet(s, "OK");
|
857 | 858693c6 | bellard | } else {
|
858 | 858693c6 | bellard | breakpoint_error:
|
859 | 905f20b1 | bellard | put_packet(s, "E22");
|
860 | 858693c6 | bellard | } |
861 | 858693c6 | bellard | break;
|
862 | 858693c6 | bellard | case 'z': |
863 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
864 | 858693c6 | bellard | if (*p == ',') |
865 | 858693c6 | bellard | p++; |
866 | 9d9754a3 | bellard | addr = strtoull(p, (char **)&p, 16); |
867 | 858693c6 | bellard | if (*p == ',') |
868 | 858693c6 | bellard | p++; |
869 | 9d9754a3 | bellard | len = strtoull(p, (char **)&p, 16); |
870 | 858693c6 | bellard | if (type == 0 || type == 1) { |
871 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
872 | 858693c6 | bellard | put_packet(s, "OK");
|
873 | 858693c6 | bellard | } else {
|
874 | 858693c6 | bellard | goto breakpoint_error;
|
875 | 858693c6 | bellard | } |
876 | 858693c6 | bellard | break;
|
877 | 831b7825 | ths | #ifdef CONFIG_LINUX_USER
|
878 | 978efd6a | pbrook | case 'q': |
879 | 978efd6a | pbrook | if (strncmp(p, "Offsets", 7) == 0) { |
880 | 978efd6a | pbrook | TaskState *ts = env->opaque; |
881 | 978efd6a | pbrook | |
882 | 978efd6a | pbrook | sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
|
883 | 978efd6a | pbrook | ts->info->data_offset, ts->info->data_offset); |
884 | 978efd6a | pbrook | put_packet(s, buf); |
885 | 978efd6a | pbrook | break;
|
886 | 978efd6a | pbrook | } |
887 | 978efd6a | pbrook | /* Fall through. */
|
888 | 978efd6a | pbrook | #endif
|
889 | 858693c6 | bellard | default:
|
890 | 858693c6 | bellard | // unknown_command:
|
891 | 858693c6 | bellard | /* put empty packet */
|
892 | 858693c6 | bellard | buf[0] = '\0'; |
893 | 858693c6 | bellard | put_packet(s, buf); |
894 | 858693c6 | bellard | break;
|
895 | 858693c6 | bellard | } |
896 | 858693c6 | bellard | return RS_IDLE;
|
897 | 858693c6 | bellard | } |
898 | 858693c6 | bellard | |
899 | 612458f5 | bellard | extern void tb_flush(CPUState *env); |
900 | 612458f5 | bellard | |
901 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
902 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
903 | 858693c6 | bellard | { |
904 | 858693c6 | bellard | GDBState *s = opaque; |
905 | 858693c6 | bellard | char buf[256]; |
906 | 858693c6 | bellard | int ret;
|
907 | 858693c6 | bellard | |
908 | a2d1ebaf | pbrook | if (s->state == RS_SYSCALL)
|
909 | a2d1ebaf | pbrook | return;
|
910 | a2d1ebaf | pbrook | |
911 | 858693c6 | bellard | /* disable single step if it was enable */
|
912 | 6a00d601 | bellard | cpu_single_step(s->env, 0);
|
913 | 858693c6 | bellard | |
914 | e80cfcfc | bellard | if (reason == EXCP_DEBUG) {
|
915 | 6a00d601 | bellard | tb_flush(s->env); |
916 | 858693c6 | bellard | ret = SIGTRAP; |
917 | bbeb7b5c | bellard | } else if (reason == EXCP_INTERRUPT) { |
918 | bbeb7b5c | bellard | ret = SIGINT; |
919 | bbeb7b5c | bellard | } else {
|
920 | 858693c6 | bellard | ret = 0;
|
921 | bbeb7b5c | bellard | } |
922 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
923 | 858693c6 | bellard | put_packet(s, buf); |
924 | 858693c6 | bellard | } |
925 | 1fddef4b | bellard | #endif
|
926 | 858693c6 | bellard | |
927 | a2d1ebaf | pbrook | /* Send a gdb syscall request.
|
928 | a2d1ebaf | pbrook | This accepts limited printf-style format specifiers, specifically:
|
929 | a2d1ebaf | pbrook | %x - target_ulong argument printed in hex.
|
930 | a2d1ebaf | pbrook | %s - string pointer (target_ulong) and length (int) pair. */
|
931 | a2d1ebaf | pbrook | void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...) |
932 | a2d1ebaf | pbrook | { |
933 | a2d1ebaf | pbrook | va_list va; |
934 | a2d1ebaf | pbrook | char buf[256]; |
935 | a2d1ebaf | pbrook | char *p;
|
936 | a2d1ebaf | pbrook | target_ulong addr; |
937 | a2d1ebaf | pbrook | GDBState *s; |
938 | a2d1ebaf | pbrook | |
939 | a2d1ebaf | pbrook | s = gdb_syscall_state; |
940 | a2d1ebaf | pbrook | if (!s)
|
941 | a2d1ebaf | pbrook | return;
|
942 | a2d1ebaf | pbrook | gdb_current_syscall_cb = cb; |
943 | a2d1ebaf | pbrook | s->state = RS_SYSCALL; |
944 | a2d1ebaf | pbrook | #ifndef CONFIG_USER_ONLY
|
945 | a2d1ebaf | pbrook | vm_stop(EXCP_DEBUG); |
946 | a2d1ebaf | pbrook | #endif
|
947 | a2d1ebaf | pbrook | s->state = RS_IDLE; |
948 | a2d1ebaf | pbrook | va_start(va, fmt); |
949 | a2d1ebaf | pbrook | p = buf; |
950 | a2d1ebaf | pbrook | *(p++) = 'F';
|
951 | a2d1ebaf | pbrook | while (*fmt) {
|
952 | a2d1ebaf | pbrook | if (*fmt == '%') { |
953 | a2d1ebaf | pbrook | fmt++; |
954 | a2d1ebaf | pbrook | switch (*fmt++) {
|
955 | a2d1ebaf | pbrook | case 'x': |
956 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
957 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx, addr); |
958 | a2d1ebaf | pbrook | break;
|
959 | a2d1ebaf | pbrook | case 's': |
960 | a2d1ebaf | pbrook | addr = va_arg(va, target_ulong); |
961 | a2d1ebaf | pbrook | p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int)); |
962 | a2d1ebaf | pbrook | break;
|
963 | a2d1ebaf | pbrook | default:
|
964 | a2d1ebaf | pbrook | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
|
965 | a2d1ebaf | pbrook | fmt - 1);
|
966 | a2d1ebaf | pbrook | break;
|
967 | a2d1ebaf | pbrook | } |
968 | a2d1ebaf | pbrook | } else {
|
969 | a2d1ebaf | pbrook | *(p++) = *(fmt++); |
970 | a2d1ebaf | pbrook | } |
971 | a2d1ebaf | pbrook | } |
972 | a2d1ebaf | pbrook | va_end(va); |
973 | a2d1ebaf | pbrook | put_packet(s, buf); |
974 | a2d1ebaf | pbrook | #ifdef CONFIG_USER_ONLY
|
975 | a2d1ebaf | pbrook | gdb_handlesig(s->env, 0);
|
976 | a2d1ebaf | pbrook | #else
|
977 | a2d1ebaf | pbrook | cpu_interrupt(s->env, CPU_INTERRUPT_EXIT); |
978 | a2d1ebaf | pbrook | #endif
|
979 | a2d1ebaf | pbrook | } |
980 | a2d1ebaf | pbrook | |
981 | 6a00d601 | bellard | static void gdb_read_byte(GDBState *s, int ch) |
982 | 858693c6 | bellard | { |
983 | 6a00d601 | bellard | CPUState *env = s->env; |
984 | 858693c6 | bellard | int i, csum;
|
985 | 858693c6 | bellard | char reply[1]; |
986 | 858693c6 | bellard | |
987 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
988 | 4046d913 | pbrook | if (s->last_packet_len) {
|
989 | 4046d913 | pbrook | /* Waiting for a response to the last packet. If we see the start
|
990 | 4046d913 | pbrook | of a new command then abandon the previous response. */
|
991 | 4046d913 | pbrook | if (ch == '-') { |
992 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
993 | 4046d913 | pbrook | printf("Got NACK, retransmitting\n");
|
994 | 4046d913 | pbrook | #endif
|
995 | 4046d913 | pbrook | put_buffer(s, s->last_packet, s->last_packet_len); |
996 | 4046d913 | pbrook | } |
997 | 4046d913 | pbrook | #ifdef DEBUG_GDB
|
998 | 4046d913 | pbrook | else if (ch == '+') |
999 | 4046d913 | pbrook | printf("Got ACK\n");
|
1000 | 4046d913 | pbrook | else
|
1001 | 4046d913 | pbrook | printf("Got '%c' when expecting ACK/NACK\n", ch);
|
1002 | 4046d913 | pbrook | #endif
|
1003 | 4046d913 | pbrook | if (ch == '+' || ch == '$') |
1004 | 4046d913 | pbrook | s->last_packet_len = 0;
|
1005 | 4046d913 | pbrook | if (ch != '$') |
1006 | 4046d913 | pbrook | return;
|
1007 | 4046d913 | pbrook | } |
1008 | 858693c6 | bellard | if (vm_running) {
|
1009 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
1010 | 858693c6 | bellard | it when receiving a char */
|
1011 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
1012 | 41625033 | bellard | } else
|
1013 | 1fddef4b | bellard | #endif
|
1014 | 41625033 | bellard | { |
1015 | 858693c6 | bellard | switch(s->state) {
|
1016 | 858693c6 | bellard | case RS_IDLE:
|
1017 | 858693c6 | bellard | if (ch == '$') { |
1018 | 858693c6 | bellard | s->line_buf_index = 0;
|
1019 | 858693c6 | bellard | s->state = RS_GETLINE; |
1020 | c33a346e | bellard | } |
1021 | b4608c04 | bellard | break;
|
1022 | 858693c6 | bellard | case RS_GETLINE:
|
1023 | 858693c6 | bellard | if (ch == '#') { |
1024 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
1025 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
1026 | 858693c6 | bellard | s->state = RS_IDLE; |
1027 | 4c3a88a2 | bellard | } else {
|
1028 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
1029 | 4c3a88a2 | bellard | } |
1030 | 4c3a88a2 | bellard | break;
|
1031 | 858693c6 | bellard | case RS_CHKSUM1:
|
1032 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
1033 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
1034 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
1035 | 858693c6 | bellard | break;
|
1036 | 858693c6 | bellard | case RS_CHKSUM2:
|
1037 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
1038 | 858693c6 | bellard | csum = 0;
|
1039 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
1040 | 858693c6 | bellard | csum += s->line_buf[i]; |
1041 | 858693c6 | bellard | } |
1042 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
1043 | 858693c6 | bellard | reply[0] = '-'; |
1044 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
1045 | 858693c6 | bellard | s->state = RS_IDLE; |
1046 | 4c3a88a2 | bellard | } else {
|
1047 | 858693c6 | bellard | reply[0] = '+'; |
1048 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
1049 | 1fddef4b | bellard | s->state = gdb_handle_packet(s, env, s->line_buf); |
1050 | 4c3a88a2 | bellard | } |
1051 | 4c3a88a2 | bellard | break;
|
1052 | a2d1ebaf | pbrook | default:
|
1053 | a2d1ebaf | pbrook | abort(); |
1054 | 858693c6 | bellard | } |
1055 | 858693c6 | bellard | } |
1056 | 858693c6 | bellard | } |
1057 | 858693c6 | bellard | |
1058 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
1059 | 1fddef4b | bellard | int
|
1060 | 1fddef4b | bellard | gdb_handlesig (CPUState *env, int sig)
|
1061 | 1fddef4b | bellard | { |
1062 | 1fddef4b | bellard | GDBState *s; |
1063 | 1fddef4b | bellard | char buf[256]; |
1064 | 1fddef4b | bellard | int n;
|
1065 | 1fddef4b | bellard | |
1066 | 1fddef4b | bellard | if (gdbserver_fd < 0) |
1067 | 1fddef4b | bellard | return sig;
|
1068 | 1fddef4b | bellard | |
1069 | 1fddef4b | bellard | s = &gdbserver_state; |
1070 | 1fddef4b | bellard | |
1071 | 1fddef4b | bellard | /* disable single step if it was enabled */
|
1072 | 1fddef4b | bellard | cpu_single_step(env, 0);
|
1073 | 1fddef4b | bellard | tb_flush(env); |
1074 | 1fddef4b | bellard | |
1075 | 1fddef4b | bellard | if (sig != 0) |
1076 | 1fddef4b | bellard | { |
1077 | 1fddef4b | bellard | snprintf(buf, sizeof(buf), "S%02x", sig); |
1078 | 1fddef4b | bellard | put_packet(s, buf); |
1079 | 1fddef4b | bellard | } |
1080 | 1fddef4b | bellard | |
1081 | 1fddef4b | bellard | sig = 0;
|
1082 | 1fddef4b | bellard | s->state = RS_IDLE; |
1083 | 41625033 | bellard | s->running_state = 0;
|
1084 | 41625033 | bellard | while (s->running_state == 0) { |
1085 | 1fddef4b | bellard | n = read (s->fd, buf, 256);
|
1086 | 1fddef4b | bellard | if (n > 0) |
1087 | 1fddef4b | bellard | { |
1088 | 1fddef4b | bellard | int i;
|
1089 | 1fddef4b | bellard | |
1090 | 1fddef4b | bellard | for (i = 0; i < n; i++) |
1091 | 6a00d601 | bellard | gdb_read_byte (s, buf[i]); |
1092 | 1fddef4b | bellard | } |
1093 | 1fddef4b | bellard | else if (n == 0 || errno != EAGAIN) |
1094 | 1fddef4b | bellard | { |
1095 | 1fddef4b | bellard | /* XXX: Connection closed. Should probably wait for annother
|
1096 | 1fddef4b | bellard | connection before continuing. */
|
1097 | 1fddef4b | bellard | return sig;
|
1098 | 1fddef4b | bellard | } |
1099 | 41625033 | bellard | } |
1100 | 1fddef4b | bellard | return sig;
|
1101 | 1fddef4b | bellard | } |
1102 | e9009676 | bellard | |
1103 | e9009676 | bellard | /* Tell the remote gdb that the process has exited. */
|
1104 | e9009676 | bellard | void gdb_exit(CPUState *env, int code) |
1105 | e9009676 | bellard | { |
1106 | e9009676 | bellard | GDBState *s; |
1107 | e9009676 | bellard | char buf[4]; |
1108 | e9009676 | bellard | |
1109 | e9009676 | bellard | if (gdbserver_fd < 0) |
1110 | e9009676 | bellard | return;
|
1111 | e9009676 | bellard | |
1112 | e9009676 | bellard | s = &gdbserver_state; |
1113 | e9009676 | bellard | |
1114 | e9009676 | bellard | snprintf(buf, sizeof(buf), "W%02x", code); |
1115 | e9009676 | bellard | put_packet(s, buf); |
1116 | e9009676 | bellard | } |
1117 | e9009676 | bellard | |
1118 | 1fddef4b | bellard | |
1119 | 7c9d8e07 | bellard | static void gdb_accept(void *opaque) |
1120 | 858693c6 | bellard | { |
1121 | 858693c6 | bellard | GDBState *s; |
1122 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1123 | 858693c6 | bellard | socklen_t len; |
1124 | 858693c6 | bellard | int val, fd;
|
1125 | 858693c6 | bellard | |
1126 | 858693c6 | bellard | for(;;) {
|
1127 | 858693c6 | bellard | len = sizeof(sockaddr);
|
1128 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
1129 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
1130 | 858693c6 | bellard | perror("accept");
|
1131 | 858693c6 | bellard | return;
|
1132 | 858693c6 | bellard | } else if (fd >= 0) { |
1133 | b4608c04 | bellard | break;
|
1134 | b4608c04 | bellard | } |
1135 | b4608c04 | bellard | } |
1136 | 858693c6 | bellard | |
1137 | 858693c6 | bellard | /* set short latency */
|
1138 | 858693c6 | bellard | val = 1;
|
1139 | 8f447cc7 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
1140 | 858693c6 | bellard | |
1141 | 1fddef4b | bellard | s = &gdbserver_state; |
1142 | 1fddef4b | bellard | memset (s, 0, sizeof (GDBState)); |
1143 | 6a00d601 | bellard | s->env = first_cpu; /* XXX: allow to change CPU */
|
1144 | 858693c6 | bellard | s->fd = fd; |
1145 | 858693c6 | bellard | |
1146 | a2d1ebaf | pbrook | gdb_syscall_state = s; |
1147 | a2d1ebaf | pbrook | |
1148 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
1149 | 858693c6 | bellard | } |
1150 | 858693c6 | bellard | |
1151 | 858693c6 | bellard | static int gdbserver_open(int port) |
1152 | 858693c6 | bellard | { |
1153 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
1154 | 858693c6 | bellard | int fd, val, ret;
|
1155 | 858693c6 | bellard | |
1156 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
1157 | 858693c6 | bellard | if (fd < 0) { |
1158 | 858693c6 | bellard | perror("socket");
|
1159 | 858693c6 | bellard | return -1; |
1160 | 858693c6 | bellard | } |
1161 | 858693c6 | bellard | |
1162 | 858693c6 | bellard | /* allow fast reuse */
|
1163 | 858693c6 | bellard | val = 1;
|
1164 | 8f447cc7 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); |
1165 | 858693c6 | bellard | |
1166 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
1167 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
1168 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
1169 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
1170 | 858693c6 | bellard | if (ret < 0) { |
1171 | 858693c6 | bellard | perror("bind");
|
1172 | 858693c6 | bellard | return -1; |
1173 | 858693c6 | bellard | } |
1174 | 858693c6 | bellard | ret = listen(fd, 0);
|
1175 | 858693c6 | bellard | if (ret < 0) { |
1176 | 858693c6 | bellard | perror("listen");
|
1177 | 858693c6 | bellard | return -1; |
1178 | 858693c6 | bellard | } |
1179 | 858693c6 | bellard | return fd;
|
1180 | 858693c6 | bellard | } |
1181 | 858693c6 | bellard | |
1182 | 858693c6 | bellard | int gdbserver_start(int port) |
1183 | 858693c6 | bellard | { |
1184 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
1185 | 858693c6 | bellard | if (gdbserver_fd < 0) |
1186 | 858693c6 | bellard | return -1; |
1187 | 858693c6 | bellard | /* accept connections */
|
1188 | 7c9d8e07 | bellard | gdb_accept (NULL);
|
1189 | 4046d913 | pbrook | return 0; |
1190 | 4046d913 | pbrook | } |
1191 | 1fddef4b | bellard | #else
|
1192 | 4046d913 | pbrook | static int gdb_chr_can_recieve(void *opaque) |
1193 | 4046d913 | pbrook | { |
1194 | 4046d913 | pbrook | return 1; |
1195 | 4046d913 | pbrook | } |
1196 | 4046d913 | pbrook | |
1197 | 4046d913 | pbrook | static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size) |
1198 | 4046d913 | pbrook | { |
1199 | 4046d913 | pbrook | GDBState *s = opaque; |
1200 | 4046d913 | pbrook | int i;
|
1201 | 4046d913 | pbrook | |
1202 | 4046d913 | pbrook | for (i = 0; i < size; i++) { |
1203 | 4046d913 | pbrook | gdb_read_byte(s, buf[i]); |
1204 | 4046d913 | pbrook | } |
1205 | 4046d913 | pbrook | } |
1206 | 4046d913 | pbrook | |
1207 | 4046d913 | pbrook | static void gdb_chr_event(void *opaque, int event) |
1208 | 4046d913 | pbrook | { |
1209 | 4046d913 | pbrook | switch (event) {
|
1210 | 4046d913 | pbrook | case CHR_EVENT_RESET:
|
1211 | 4046d913 | pbrook | vm_stop(EXCP_INTERRUPT); |
1212 | a2d1ebaf | pbrook | gdb_syscall_state = opaque; |
1213 | 4046d913 | pbrook | break;
|
1214 | 4046d913 | pbrook | default:
|
1215 | 4046d913 | pbrook | break;
|
1216 | 4046d913 | pbrook | } |
1217 | 4046d913 | pbrook | } |
1218 | 4046d913 | pbrook | |
1219 | cfc3475a | pbrook | int gdbserver_start(const char *port) |
1220 | 4046d913 | pbrook | { |
1221 | 4046d913 | pbrook | GDBState *s; |
1222 | cfc3475a | pbrook | char gdbstub_port_name[128]; |
1223 | cfc3475a | pbrook | int port_num;
|
1224 | cfc3475a | pbrook | char *p;
|
1225 | cfc3475a | pbrook | CharDriverState *chr; |
1226 | cfc3475a | pbrook | |
1227 | cfc3475a | pbrook | if (!port || !*port)
|
1228 | cfc3475a | pbrook | return -1; |
1229 | 4046d913 | pbrook | |
1230 | cfc3475a | pbrook | port_num = strtol(port, &p, 10);
|
1231 | cfc3475a | pbrook | if (*p == 0) { |
1232 | cfc3475a | pbrook | /* A numeric value is interpreted as a port number. */
|
1233 | cfc3475a | pbrook | snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
|
1234 | cfc3475a | pbrook | "tcp::%d,nowait,nodelay,server", port_num);
|
1235 | cfc3475a | pbrook | port = gdbstub_port_name; |
1236 | cfc3475a | pbrook | } |
1237 | cfc3475a | pbrook | |
1238 | cfc3475a | pbrook | chr = qemu_chr_open(port); |
1239 | 4046d913 | pbrook | if (!chr)
|
1240 | 4046d913 | pbrook | return -1; |
1241 | 4046d913 | pbrook | |
1242 | 4046d913 | pbrook | s = qemu_mallocz(sizeof(GDBState));
|
1243 | 4046d913 | pbrook | if (!s) {
|
1244 | 4046d913 | pbrook | return -1; |
1245 | 4046d913 | pbrook | } |
1246 | 4046d913 | pbrook | s->env = first_cpu; /* XXX: allow to change CPU */
|
1247 | 4046d913 | pbrook | s->chr = chr; |
1248 | 4046d913 | pbrook | qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve, |
1249 | 4046d913 | pbrook | gdb_chr_event, s); |
1250 | 4046d913 | pbrook | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
1251 | b4608c04 | bellard | return 0; |
1252 | b4608c04 | bellard | } |
1253 | 4046d913 | pbrook | #endif |