root / gdbstub.c @ 71be0fc3
History | View | Annotate | Download (19 kB)
1 | b4608c04 | bellard | /*
|
---|---|---|---|
2 | b4608c04 | bellard | * gdb server stub
|
3 | b4608c04 | bellard | *
|
4 | b4608c04 | bellard | * Copyright (c) 2003 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 | e95c8d51 | bellard | registers[97] = tswapl(_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 | e95c8d51 | bellard | registers[101] = tswapl(_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 | e95c8d51 | bellard | _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 | e95c8d51 | bellard | _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 | e95c8d51 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
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 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
312 | e95c8d51 | bellard | registers[64] = tswapl(env->y);
|
313 | e80cfcfc | bellard | tmp = GET_PSR(env); |
314 | e95c8d51 | bellard | registers[65] = tswapl(tmp);
|
315 | e95c8d51 | bellard | registers[66] = tswapl(env->wim);
|
316 | e95c8d51 | bellard | registers[67] = tswapl(env->tbr);
|
317 | e95c8d51 | bellard | registers[68] = tswapl(env->pc);
|
318 | e95c8d51 | bellard | registers[69] = tswapl(env->npc);
|
319 | e95c8d51 | bellard | registers[70] = tswapl(env->fsr);
|
320 | e95c8d51 | bellard | registers[71] = 0; /* csr */ |
321 | e95c8d51 | bellard | registers[72] = 0; |
322 | e95c8d51 | bellard | |
323 | e95c8d51 | bellard | return 73 * 4; |
324 | e95c8d51 | bellard | } |
325 | e95c8d51 | bellard | |
326 | e95c8d51 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
327 | e95c8d51 | bellard | { |
328 | e80cfcfc | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
329 | e95c8d51 | bellard | int i;
|
330 | e95c8d51 | bellard | |
331 | e95c8d51 | bellard | /* fill in g0..g7 */
|
332 | e95c8d51 | bellard | for(i = 0; i < 7; i++) { |
333 | e95c8d51 | bellard | env->gregs[i] = tswapl(registers[i]); |
334 | e95c8d51 | bellard | } |
335 | e95c8d51 | bellard | /* fill in register window */
|
336 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
337 | e95c8d51 | bellard | env->regwptr[i] = tswapl(registers[i]); |
338 | e95c8d51 | bellard | } |
339 | e95c8d51 | bellard | /* fill in fprs */
|
340 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
341 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
342 | e95c8d51 | bellard | } |
343 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
344 | e95c8d51 | bellard | env->y = tswapl(registers[64]);
|
345 | e80cfcfc | bellard | PUT_PSR(env, tswapl(registers[65]));
|
346 | e95c8d51 | bellard | env->wim = tswapl(registers[66]);
|
347 | e95c8d51 | bellard | env->tbr = tswapl(registers[67]);
|
348 | e95c8d51 | bellard | env->pc = tswapl(registers[68]);
|
349 | e95c8d51 | bellard | env->npc = tswapl(registers[69]);
|
350 | e95c8d51 | bellard | env->fsr = tswapl(registers[70]);
|
351 | 9e62fd7f | bellard | } |
352 | 1fddef4b | bellard | #elif defined (TARGET_ARM)
|
353 | 1fddef4b | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
354 | 1fddef4b | bellard | { |
355 | 1fddef4b | bellard | int i;
|
356 | 1fddef4b | bellard | uint8_t *ptr; |
357 | 1fddef4b | bellard | |
358 | 1fddef4b | bellard | ptr = mem_buf; |
359 | 1fddef4b | bellard | /* 16 core integer registers (4 bytes each). */
|
360 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
361 | 1fddef4b | bellard | { |
362 | 1fddef4b | bellard | *(uint32_t *)ptr = tswapl(env->regs[i]); |
363 | 1fddef4b | bellard | ptr += 4;
|
364 | 1fddef4b | bellard | } |
365 | 1fddef4b | bellard | /* 8 FPA registers (12 bytes each), FPS (4 bytes).
|
366 | 1fddef4b | bellard | Not yet implemented. */
|
367 | 1fddef4b | bellard | memset (ptr, 0, 8 * 12 + 4); |
368 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
369 | 1fddef4b | bellard | /* CPSR (4 bytes). */
|
370 | 1fddef4b | bellard | *(uint32_t *)ptr = tswapl (env->cpsr); |
371 | 1fddef4b | bellard | ptr += 4;
|
372 | 1fddef4b | bellard | |
373 | 1fddef4b | bellard | return ptr - mem_buf;
|
374 | 1fddef4b | bellard | } |
375 | 6da41eaf | bellard | |
376 | 1fddef4b | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
377 | 1fddef4b | bellard | { |
378 | 1fddef4b | bellard | int i;
|
379 | 1fddef4b | bellard | uint8_t *ptr; |
380 | 1fddef4b | bellard | |
381 | 1fddef4b | bellard | ptr = mem_buf; |
382 | 1fddef4b | bellard | /* Core integer registers. */
|
383 | 1fddef4b | bellard | for (i = 0; i < 16; i++) |
384 | 1fddef4b | bellard | { |
385 | 1fddef4b | bellard | env->regs[i] = tswapl(*(uint32_t *)ptr); |
386 | 1fddef4b | bellard | ptr += 4;
|
387 | 1fddef4b | bellard | } |
388 | 1fddef4b | bellard | /* Ignore FPA regs and scr. */
|
389 | 1fddef4b | bellard | ptr += 8 * 12 + 4; |
390 | 1fddef4b | bellard | env->cpsr = tswapl(*(uint32_t *)ptr); |
391 | 1fddef4b | bellard | } |
392 | 1fddef4b | bellard | #else
|
393 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
394 | 6da41eaf | bellard | { |
395 | 6da41eaf | bellard | return 0; |
396 | 6da41eaf | bellard | } |
397 | 6da41eaf | bellard | |
398 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
399 | 6da41eaf | bellard | { |
400 | 6da41eaf | bellard | } |
401 | 6da41eaf | bellard | |
402 | 6da41eaf | bellard | #endif
|
403 | b4608c04 | bellard | |
404 | 1fddef4b | bellard | static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf) |
405 | b4608c04 | bellard | { |
406 | b4608c04 | bellard | const char *p; |
407 | 858693c6 | bellard | int ch, reg_size, type;
|
408 | b4608c04 | bellard | char buf[4096]; |
409 | b4608c04 | bellard | uint8_t mem_buf[2000];
|
410 | b4608c04 | bellard | uint32_t *registers; |
411 | b4608c04 | bellard | uint32_t addr, len; |
412 | b4608c04 | bellard | |
413 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
414 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
415 | 858693c6 | bellard | #endif
|
416 | 858693c6 | bellard | p = line_buf; |
417 | 858693c6 | bellard | ch = *p++; |
418 | 858693c6 | bellard | switch(ch) {
|
419 | 858693c6 | bellard | case '?': |
420 | 1fddef4b | bellard | /* TODO: Make this return the correct value for user-mode. */
|
421 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
422 | 858693c6 | bellard | put_packet(s, buf); |
423 | 858693c6 | bellard | break;
|
424 | 858693c6 | bellard | case 'c': |
425 | 858693c6 | bellard | if (*p != '\0') { |
426 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
427 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
428 | 858693c6 | bellard | env->eip = addr; |
429 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
430 | 858693c6 | bellard | env->nip = addr; |
431 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
432 | 8d5f07fa | bellard | env->pc = addr; |
433 | 8d5f07fa | bellard | env->npc = addr + 4;
|
434 | 4c3a88a2 | bellard | #endif
|
435 | 858693c6 | bellard | } |
436 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
437 | 41625033 | bellard | s->running_state = 1;
|
438 | 41625033 | bellard | #else
|
439 | 41625033 | bellard | vm_start(); |
440 | 41625033 | bellard | #endif
|
441 | 41625033 | bellard | return RS_IDLE;
|
442 | 858693c6 | bellard | case 's': |
443 | 858693c6 | bellard | if (*p != '\0') { |
444 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
445 | c33a346e | bellard | #if defined(TARGET_I386)
|
446 | 858693c6 | bellard | env->eip = addr; |
447 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
448 | 858693c6 | bellard | env->nip = addr; |
449 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
450 | 8d5f07fa | bellard | env->pc = addr; |
451 | 8d5f07fa | bellard | env->npc = addr + 4;
|
452 | c33a346e | bellard | #endif
|
453 | 858693c6 | bellard | } |
454 | 858693c6 | bellard | cpu_single_step(env, 1);
|
455 | 41625033 | bellard | #ifdef CONFIG_USER_ONLY
|
456 | 41625033 | bellard | s->running_state = 1;
|
457 | 41625033 | bellard | #else
|
458 | 41625033 | bellard | vm_start(); |
459 | 41625033 | bellard | #endif
|
460 | 41625033 | bellard | return RS_IDLE;
|
461 | 858693c6 | bellard | case 'g': |
462 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
463 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
464 | 858693c6 | bellard | put_packet(s, buf); |
465 | 858693c6 | bellard | break;
|
466 | 858693c6 | bellard | case 'G': |
467 | 858693c6 | bellard | registers = (void *)mem_buf;
|
468 | 858693c6 | bellard | len = strlen(p) / 2;
|
469 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
470 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
471 | 858693c6 | bellard | put_packet(s, "OK");
|
472 | 858693c6 | bellard | break;
|
473 | 858693c6 | bellard | case 'm': |
474 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
475 | 858693c6 | bellard | if (*p == ',') |
476 | 858693c6 | bellard | p++; |
477 | 858693c6 | bellard | len = strtoul(p, NULL, 16); |
478 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) |
479 | 858693c6 | bellard | memset(mem_buf, 0, len);
|
480 | 858693c6 | bellard | memtohex(buf, mem_buf, len); |
481 | 858693c6 | bellard | put_packet(s, buf); |
482 | 858693c6 | bellard | break;
|
483 | 858693c6 | bellard | case 'M': |
484 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
485 | 858693c6 | bellard | if (*p == ',') |
486 | 858693c6 | bellard | p++; |
487 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
488 | b328f873 | bellard | if (*p == ':') |
489 | 858693c6 | bellard | p++; |
490 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
491 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
492 | 905f20b1 | bellard | put_packet(s, "E14");
|
493 | 858693c6 | bellard | else
|
494 | 858693c6 | bellard | put_packet(s, "OK");
|
495 | 858693c6 | bellard | break;
|
496 | 858693c6 | bellard | case 'Z': |
497 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
498 | 858693c6 | bellard | if (*p == ',') |
499 | 858693c6 | bellard | p++; |
500 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
501 | 858693c6 | bellard | if (*p == ',') |
502 | 858693c6 | bellard | p++; |
503 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
504 | 858693c6 | bellard | if (type == 0 || type == 1) { |
505 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
506 | 858693c6 | bellard | goto breakpoint_error;
|
507 | 858693c6 | bellard | put_packet(s, "OK");
|
508 | 858693c6 | bellard | } else {
|
509 | 858693c6 | bellard | breakpoint_error:
|
510 | 905f20b1 | bellard | put_packet(s, "E22");
|
511 | 858693c6 | bellard | } |
512 | 858693c6 | bellard | break;
|
513 | 858693c6 | bellard | case 'z': |
514 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
515 | 858693c6 | bellard | if (*p == ',') |
516 | 858693c6 | bellard | p++; |
517 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
518 | 858693c6 | bellard | if (*p == ',') |
519 | 858693c6 | bellard | p++; |
520 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
521 | 858693c6 | bellard | if (type == 0 || type == 1) { |
522 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
523 | 858693c6 | bellard | put_packet(s, "OK");
|
524 | 858693c6 | bellard | } else {
|
525 | 858693c6 | bellard | goto breakpoint_error;
|
526 | 858693c6 | bellard | } |
527 | 858693c6 | bellard | break;
|
528 | 858693c6 | bellard | default:
|
529 | 858693c6 | bellard | // unknown_command:
|
530 | 858693c6 | bellard | /* put empty packet */
|
531 | 858693c6 | bellard | buf[0] = '\0'; |
532 | 858693c6 | bellard | put_packet(s, buf); |
533 | 858693c6 | bellard | break;
|
534 | 858693c6 | bellard | } |
535 | 858693c6 | bellard | return RS_IDLE;
|
536 | 858693c6 | bellard | } |
537 | 858693c6 | bellard | |
538 | 612458f5 | bellard | extern void tb_flush(CPUState *env); |
539 | 612458f5 | bellard | |
540 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
541 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
542 | 858693c6 | bellard | { |
543 | 858693c6 | bellard | GDBState *s = opaque; |
544 | 858693c6 | bellard | char buf[256]; |
545 | 858693c6 | bellard | int ret;
|
546 | 858693c6 | bellard | |
547 | 858693c6 | bellard | /* disable single step if it was enable */
|
548 | 858693c6 | bellard | cpu_single_step(cpu_single_env, 0);
|
549 | 858693c6 | bellard | |
550 | e80cfcfc | bellard | if (reason == EXCP_DEBUG) {
|
551 | e80cfcfc | bellard | tb_flush(cpu_single_env); |
552 | 858693c6 | bellard | ret = SIGTRAP; |
553 | e80cfcfc | bellard | } |
554 | 858693c6 | bellard | else
|
555 | 858693c6 | bellard | ret = 0;
|
556 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
557 | 858693c6 | bellard | put_packet(s, buf); |
558 | 858693c6 | bellard | } |
559 | 1fddef4b | bellard | #endif
|
560 | 858693c6 | bellard | |
561 | 1fddef4b | bellard | static void gdb_read_byte(GDBState *s, CPUState *env, int ch) |
562 | 858693c6 | bellard | { |
563 | 858693c6 | bellard | int i, csum;
|
564 | 858693c6 | bellard | char reply[1]; |
565 | 858693c6 | bellard | |
566 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
567 | 858693c6 | bellard | if (vm_running) {
|
568 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
569 | 858693c6 | bellard | it when receiving a char */
|
570 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
571 | 41625033 | bellard | } else
|
572 | 1fddef4b | bellard | #endif
|
573 | 41625033 | bellard | { |
574 | 858693c6 | bellard | switch(s->state) {
|
575 | 858693c6 | bellard | case RS_IDLE:
|
576 | 858693c6 | bellard | if (ch == '$') { |
577 | 858693c6 | bellard | s->line_buf_index = 0;
|
578 | 858693c6 | bellard | s->state = RS_GETLINE; |
579 | c33a346e | bellard | } |
580 | b4608c04 | bellard | break;
|
581 | 858693c6 | bellard | case RS_GETLINE:
|
582 | 858693c6 | bellard | if (ch == '#') { |
583 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
584 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
585 | 858693c6 | bellard | s->state = RS_IDLE; |
586 | 4c3a88a2 | bellard | } else {
|
587 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
588 | 4c3a88a2 | bellard | } |
589 | 4c3a88a2 | bellard | break;
|
590 | 858693c6 | bellard | case RS_CHKSUM1:
|
591 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
592 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
593 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
594 | 858693c6 | bellard | break;
|
595 | 858693c6 | bellard | case RS_CHKSUM2:
|
596 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
597 | 858693c6 | bellard | csum = 0;
|
598 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
599 | 858693c6 | bellard | csum += s->line_buf[i]; |
600 | 858693c6 | bellard | } |
601 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
602 | 858693c6 | bellard | reply[0] = '-'; |
603 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
604 | 858693c6 | bellard | s->state = RS_IDLE; |
605 | 4c3a88a2 | bellard | } else {
|
606 | 858693c6 | bellard | reply[0] = '+'; |
607 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
608 | 1fddef4b | bellard | s->state = gdb_handle_packet(s, env, s->line_buf); |
609 | 4c3a88a2 | bellard | } |
610 | 4c3a88a2 | bellard | break;
|
611 | 858693c6 | bellard | } |
612 | 858693c6 | bellard | } |
613 | 858693c6 | bellard | } |
614 | 858693c6 | bellard | |
615 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
616 | 1fddef4b | bellard | int
|
617 | 1fddef4b | bellard | gdb_handlesig (CPUState *env, int sig)
|
618 | 1fddef4b | bellard | { |
619 | 1fddef4b | bellard | GDBState *s; |
620 | 1fddef4b | bellard | char buf[256]; |
621 | 1fddef4b | bellard | int n;
|
622 | 1fddef4b | bellard | |
623 | 1fddef4b | bellard | if (gdbserver_fd < 0) |
624 | 1fddef4b | bellard | return sig;
|
625 | 1fddef4b | bellard | |
626 | 1fddef4b | bellard | s = &gdbserver_state; |
627 | 1fddef4b | bellard | |
628 | 1fddef4b | bellard | /* disable single step if it was enabled */
|
629 | 1fddef4b | bellard | cpu_single_step(env, 0);
|
630 | 1fddef4b | bellard | tb_flush(env); |
631 | 1fddef4b | bellard | |
632 | 1fddef4b | bellard | if (sig != 0) |
633 | 1fddef4b | bellard | { |
634 | 1fddef4b | bellard | snprintf(buf, sizeof(buf), "S%02x", sig); |
635 | 1fddef4b | bellard | put_packet(s, buf); |
636 | 1fddef4b | bellard | } |
637 | 1fddef4b | bellard | |
638 | 1fddef4b | bellard | sig = 0;
|
639 | 1fddef4b | bellard | s->state = RS_IDLE; |
640 | 41625033 | bellard | s->running_state = 0;
|
641 | 41625033 | bellard | while (s->running_state == 0) { |
642 | 1fddef4b | bellard | n = read (s->fd, buf, 256);
|
643 | 1fddef4b | bellard | if (n > 0) |
644 | 1fddef4b | bellard | { |
645 | 1fddef4b | bellard | int i;
|
646 | 1fddef4b | bellard | |
647 | 1fddef4b | bellard | for (i = 0; i < n; i++) |
648 | 1fddef4b | bellard | gdb_read_byte (s, env, buf[i]); |
649 | 1fddef4b | bellard | } |
650 | 1fddef4b | bellard | else if (n == 0 || errno != EAGAIN) |
651 | 1fddef4b | bellard | { |
652 | 1fddef4b | bellard | /* XXX: Connection closed. Should probably wait for annother
|
653 | 1fddef4b | bellard | connection before continuing. */
|
654 | 1fddef4b | bellard | return sig;
|
655 | 1fddef4b | bellard | } |
656 | 41625033 | bellard | } |
657 | 1fddef4b | bellard | return sig;
|
658 | 1fddef4b | bellard | } |
659 | e9009676 | bellard | |
660 | e9009676 | bellard | /* Tell the remote gdb that the process has exited. */
|
661 | e9009676 | bellard | void gdb_exit(CPUState *env, int code) |
662 | e9009676 | bellard | { |
663 | e9009676 | bellard | GDBState *s; |
664 | e9009676 | bellard | char buf[4]; |
665 | e9009676 | bellard | |
666 | e9009676 | bellard | if (gdbserver_fd < 0) |
667 | e9009676 | bellard | return;
|
668 | e9009676 | bellard | |
669 | e9009676 | bellard | s = &gdbserver_state; |
670 | e9009676 | bellard | |
671 | e9009676 | bellard | snprintf(buf, sizeof(buf), "W%02x", code); |
672 | e9009676 | bellard | put_packet(s, buf); |
673 | e9009676 | bellard | } |
674 | e9009676 | bellard | |
675 | 1fddef4b | bellard | #else
|
676 | 858693c6 | bellard | static int gdb_can_read(void *opaque) |
677 | 858693c6 | bellard | { |
678 | 858693c6 | bellard | return 256; |
679 | 858693c6 | bellard | } |
680 | 858693c6 | bellard | |
681 | 858693c6 | bellard | static void gdb_read(void *opaque, const uint8_t *buf, int size) |
682 | 858693c6 | bellard | { |
683 | 858693c6 | bellard | GDBState *s = opaque; |
684 | 858693c6 | bellard | int i;
|
685 | 858693c6 | bellard | if (size == 0) { |
686 | 858693c6 | bellard | /* end of connection */
|
687 | 858693c6 | bellard | qemu_del_vm_stop_handler(gdb_vm_stopped, s); |
688 | 858693c6 | bellard | qemu_del_fd_read_handler(s->fd); |
689 | 858693c6 | bellard | qemu_free(s); |
690 | 858693c6 | bellard | vm_start(); |
691 | 858693c6 | bellard | } else {
|
692 | 858693c6 | bellard | for(i = 0; i < size; i++) |
693 | 1fddef4b | bellard | gdb_read_byte(s, cpu_single_env, buf[i]); |
694 | 858693c6 | bellard | } |
695 | 858693c6 | bellard | } |
696 | 858693c6 | bellard | |
697 | 1fddef4b | bellard | #endif
|
698 | 1fddef4b | bellard | |
699 | 858693c6 | bellard | static void gdb_accept(void *opaque, const uint8_t *buf, int size) |
700 | 858693c6 | bellard | { |
701 | 858693c6 | bellard | GDBState *s; |
702 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
703 | 858693c6 | bellard | socklen_t len; |
704 | 858693c6 | bellard | int val, fd;
|
705 | 858693c6 | bellard | |
706 | 858693c6 | bellard | for(;;) {
|
707 | 858693c6 | bellard | len = sizeof(sockaddr);
|
708 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
709 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
710 | 858693c6 | bellard | perror("accept");
|
711 | 858693c6 | bellard | return;
|
712 | 858693c6 | bellard | } else if (fd >= 0) { |
713 | b4608c04 | bellard | break;
|
714 | b4608c04 | bellard | } |
715 | b4608c04 | bellard | } |
716 | 858693c6 | bellard | |
717 | 858693c6 | bellard | /* set short latency */
|
718 | 858693c6 | bellard | val = 1;
|
719 | 7d3505c5 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
|
720 | 858693c6 | bellard | |
721 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
722 | 1fddef4b | bellard | s = &gdbserver_state; |
723 | 1fddef4b | bellard | memset (s, 0, sizeof (GDBState)); |
724 | 1fddef4b | bellard | #else
|
725 | 858693c6 | bellard | s = qemu_mallocz(sizeof(GDBState));
|
726 | 858693c6 | bellard | if (!s) {
|
727 | 858693c6 | bellard | close(fd); |
728 | 858693c6 | bellard | return;
|
729 | 858693c6 | bellard | } |
730 | 1fddef4b | bellard | #endif
|
731 | 858693c6 | bellard | s->fd = fd; |
732 | 858693c6 | bellard | |
733 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
734 | 858693c6 | bellard | |
735 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
736 | 858693c6 | bellard | /* stop the VM */
|
737 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
738 | 858693c6 | bellard | |
739 | 858693c6 | bellard | /* start handling I/O */
|
740 | 858693c6 | bellard | qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s); |
741 | 858693c6 | bellard | /* when the VM is stopped, the following callback is called */
|
742 | 858693c6 | bellard | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
743 | 1fddef4b | bellard | #endif
|
744 | 858693c6 | bellard | } |
745 | 858693c6 | bellard | |
746 | 858693c6 | bellard | static int gdbserver_open(int port) |
747 | 858693c6 | bellard | { |
748 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
749 | 858693c6 | bellard | int fd, val, ret;
|
750 | 858693c6 | bellard | |
751 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
752 | 858693c6 | bellard | if (fd < 0) { |
753 | 858693c6 | bellard | perror("socket");
|
754 | 858693c6 | bellard | return -1; |
755 | 858693c6 | bellard | } |
756 | 858693c6 | bellard | |
757 | 858693c6 | bellard | /* allow fast reuse */
|
758 | 858693c6 | bellard | val = 1;
|
759 | 858693c6 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
760 | 858693c6 | bellard | |
761 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
762 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
763 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
764 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
765 | 858693c6 | bellard | if (ret < 0) { |
766 | 858693c6 | bellard | perror("bind");
|
767 | 858693c6 | bellard | return -1; |
768 | 858693c6 | bellard | } |
769 | 858693c6 | bellard | ret = listen(fd, 0);
|
770 | 858693c6 | bellard | if (ret < 0) { |
771 | 858693c6 | bellard | perror("listen");
|
772 | 858693c6 | bellard | return -1; |
773 | 858693c6 | bellard | } |
774 | 1fddef4b | bellard | #ifndef CONFIG_USER_ONLY
|
775 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
776 | 1fddef4b | bellard | #endif
|
777 | 858693c6 | bellard | return fd;
|
778 | 858693c6 | bellard | } |
779 | 858693c6 | bellard | |
780 | 858693c6 | bellard | int gdbserver_start(int port) |
781 | 858693c6 | bellard | { |
782 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
783 | 858693c6 | bellard | if (gdbserver_fd < 0) |
784 | 858693c6 | bellard | return -1; |
785 | 858693c6 | bellard | /* accept connections */
|
786 | 1fddef4b | bellard | #ifdef CONFIG_USER_ONLY
|
787 | 1fddef4b | bellard | gdb_accept (NULL, NULL, 0); |
788 | 1fddef4b | bellard | #else
|
789 | 858693c6 | bellard | qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL); |
790 | 1fddef4b | bellard | #endif
|
791 | b4608c04 | bellard | return 0; |
792 | b4608c04 | bellard | } |