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