root / gdbstub.c @ 8636b5d8
History | View | Annotate | Download (16.1 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 | 67b915a5 | bellard | #include "vl.h" |
21 | 67b915a5 | bellard | |
22 | b4608c04 | bellard | #include <sys/socket.h> |
23 | b4608c04 | bellard | #include <netinet/in.h> |
24 | b4608c04 | bellard | #include <netinet/tcp.h> |
25 | b4608c04 | bellard | #include <signal.h> |
26 | b4608c04 | bellard | |
27 | 4abe615b | bellard | //#define DEBUG_GDB
|
28 | b4608c04 | bellard | |
29 | 858693c6 | bellard | enum RSState {
|
30 | 858693c6 | bellard | RS_IDLE, |
31 | 858693c6 | bellard | RS_GETLINE, |
32 | 858693c6 | bellard | RS_CHKSUM1, |
33 | 858693c6 | bellard | RS_CHKSUM2, |
34 | 858693c6 | bellard | }; |
35 | b4608c04 | bellard | |
36 | 858693c6 | bellard | static int gdbserver_fd; |
37 | b4608c04 | bellard | |
38 | 858693c6 | bellard | typedef struct GDBState { |
39 | 858693c6 | bellard | enum RSState state;
|
40 | 858693c6 | bellard | int fd;
|
41 | 858693c6 | bellard | char line_buf[4096]; |
42 | 858693c6 | bellard | int line_buf_index;
|
43 | 858693c6 | bellard | int line_csum;
|
44 | 858693c6 | bellard | } GDBState; |
45 | b4608c04 | bellard | |
46 | 858693c6 | bellard | static int get_char(GDBState *s) |
47 | b4608c04 | bellard | { |
48 | b4608c04 | bellard | uint8_t ch; |
49 | b4608c04 | bellard | int ret;
|
50 | b4608c04 | bellard | |
51 | b4608c04 | bellard | for(;;) {
|
52 | 858693c6 | bellard | ret = read(s->fd, &ch, 1);
|
53 | b4608c04 | bellard | if (ret < 0) { |
54 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
55 | b4608c04 | bellard | return -1; |
56 | b4608c04 | bellard | } else if (ret == 0) { |
57 | b4608c04 | bellard | return -1; |
58 | b4608c04 | bellard | } else {
|
59 | b4608c04 | bellard | break;
|
60 | b4608c04 | bellard | } |
61 | b4608c04 | bellard | } |
62 | b4608c04 | bellard | return ch;
|
63 | b4608c04 | bellard | } |
64 | b4608c04 | bellard | |
65 | 858693c6 | bellard | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
66 | b4608c04 | bellard | { |
67 | b4608c04 | bellard | int ret;
|
68 | b4608c04 | bellard | |
69 | b4608c04 | bellard | while (len > 0) { |
70 | 858693c6 | bellard | ret = write(s->fd, buf, len); |
71 | b4608c04 | bellard | if (ret < 0) { |
72 | b4608c04 | bellard | if (errno != EINTR && errno != EAGAIN)
|
73 | b4608c04 | bellard | return;
|
74 | b4608c04 | bellard | } else {
|
75 | b4608c04 | bellard | buf += ret; |
76 | b4608c04 | bellard | len -= ret; |
77 | b4608c04 | bellard | } |
78 | b4608c04 | bellard | } |
79 | b4608c04 | bellard | } |
80 | b4608c04 | bellard | |
81 | b4608c04 | bellard | static inline int fromhex(int v) |
82 | b4608c04 | bellard | { |
83 | b4608c04 | bellard | if (v >= '0' && v <= '9') |
84 | b4608c04 | bellard | return v - '0'; |
85 | b4608c04 | bellard | else if (v >= 'A' && v <= 'F') |
86 | b4608c04 | bellard | return v - 'A' + 10; |
87 | b4608c04 | bellard | else if (v >= 'a' && v <= 'f') |
88 | b4608c04 | bellard | return v - 'a' + 10; |
89 | b4608c04 | bellard | else
|
90 | b4608c04 | bellard | return 0; |
91 | b4608c04 | bellard | } |
92 | b4608c04 | bellard | |
93 | b4608c04 | bellard | static inline int tohex(int v) |
94 | b4608c04 | bellard | { |
95 | b4608c04 | bellard | if (v < 10) |
96 | b4608c04 | bellard | return v + '0'; |
97 | b4608c04 | bellard | else
|
98 | b4608c04 | bellard | return v - 10 + 'a'; |
99 | b4608c04 | bellard | } |
100 | b4608c04 | bellard | |
101 | b4608c04 | bellard | static void memtohex(char *buf, const uint8_t *mem, int len) |
102 | b4608c04 | bellard | { |
103 | b4608c04 | bellard | int i, c;
|
104 | b4608c04 | bellard | char *q;
|
105 | b4608c04 | bellard | q = buf; |
106 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
107 | b4608c04 | bellard | c = mem[i]; |
108 | b4608c04 | bellard | *q++ = tohex(c >> 4);
|
109 | b4608c04 | bellard | *q++ = tohex(c & 0xf);
|
110 | b4608c04 | bellard | } |
111 | b4608c04 | bellard | *q = '\0';
|
112 | b4608c04 | bellard | } |
113 | b4608c04 | bellard | |
114 | b4608c04 | bellard | static void hextomem(uint8_t *mem, const char *buf, int len) |
115 | b4608c04 | bellard | { |
116 | b4608c04 | bellard | int i;
|
117 | b4608c04 | bellard | |
118 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
119 | b4608c04 | bellard | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
120 | b4608c04 | bellard | buf += 2;
|
121 | b4608c04 | bellard | } |
122 | b4608c04 | bellard | } |
123 | b4608c04 | bellard | |
124 | b4608c04 | bellard | /* return -1 if error, 0 if OK */
|
125 | 858693c6 | bellard | static int put_packet(GDBState *s, char *buf) |
126 | b4608c04 | bellard | { |
127 | b4608c04 | bellard | char buf1[3]; |
128 | b4608c04 | bellard | int len, csum, ch, i;
|
129 | b4608c04 | bellard | |
130 | b4608c04 | bellard | #ifdef DEBUG_GDB
|
131 | b4608c04 | bellard | printf("reply='%s'\n", buf);
|
132 | b4608c04 | bellard | #endif
|
133 | b4608c04 | bellard | |
134 | b4608c04 | bellard | for(;;) {
|
135 | b4608c04 | bellard | buf1[0] = '$'; |
136 | 858693c6 | bellard | put_buffer(s, buf1, 1);
|
137 | b4608c04 | bellard | len = strlen(buf); |
138 | 858693c6 | bellard | put_buffer(s, buf, len); |
139 | b4608c04 | bellard | csum = 0;
|
140 | b4608c04 | bellard | for(i = 0; i < len; i++) { |
141 | b4608c04 | bellard | csum += buf[i]; |
142 | b4608c04 | bellard | } |
143 | b4608c04 | bellard | buf1[0] = '#'; |
144 | b4608c04 | bellard | buf1[1] = tohex((csum >> 4) & 0xf); |
145 | b4608c04 | bellard | buf1[2] = tohex((csum) & 0xf); |
146 | b4608c04 | bellard | |
147 | 858693c6 | bellard | put_buffer(s, buf1, 3);
|
148 | b4608c04 | bellard | |
149 | 858693c6 | bellard | ch = get_char(s); |
150 | b4608c04 | bellard | if (ch < 0) |
151 | b4608c04 | bellard | return -1; |
152 | b4608c04 | bellard | if (ch == '+') |
153 | b4608c04 | bellard | break;
|
154 | b4608c04 | bellard | } |
155 | b4608c04 | bellard | return 0; |
156 | b4608c04 | bellard | } |
157 | b4608c04 | bellard | |
158 | 6da41eaf | bellard | #if defined(TARGET_I386)
|
159 | 6da41eaf | bellard | |
160 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
161 | 6da41eaf | bellard | { |
162 | e95c8d51 | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
163 | 6da41eaf | bellard | int i, fpus;
|
164 | 6da41eaf | bellard | |
165 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
166 | e95c8d51 | bellard | registers[i] = env->regs[i]; |
167 | 6da41eaf | bellard | } |
168 | e95c8d51 | bellard | registers[8] = env->eip;
|
169 | e95c8d51 | bellard | registers[9] = env->eflags;
|
170 | e95c8d51 | bellard | registers[10] = env->segs[R_CS].selector;
|
171 | e95c8d51 | bellard | registers[11] = env->segs[R_SS].selector;
|
172 | e95c8d51 | bellard | registers[12] = env->segs[R_DS].selector;
|
173 | e95c8d51 | bellard | registers[13] = env->segs[R_ES].selector;
|
174 | e95c8d51 | bellard | registers[14] = env->segs[R_FS].selector;
|
175 | e95c8d51 | bellard | registers[15] = env->segs[R_GS].selector;
|
176 | 6da41eaf | bellard | /* XXX: convert floats */
|
177 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
178 | 6da41eaf | bellard | memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10); |
179 | 6da41eaf | bellard | } |
180 | e95c8d51 | bellard | registers[36] = env->fpuc;
|
181 | 6da41eaf | bellard | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
182 | e95c8d51 | bellard | registers[37] = fpus;
|
183 | e95c8d51 | bellard | registers[38] = 0; /* XXX: convert tags */ |
184 | e95c8d51 | bellard | registers[39] = 0; /* fiseg */ |
185 | e95c8d51 | bellard | registers[40] = 0; /* fioff */ |
186 | e95c8d51 | bellard | registers[41] = 0; /* foseg */ |
187 | e95c8d51 | bellard | registers[42] = 0; /* fooff */ |
188 | e95c8d51 | bellard | registers[43] = 0; /* fop */ |
189 | e95c8d51 | bellard | |
190 | e95c8d51 | bellard | for(i = 0; i < 16; i++) |
191 | e95c8d51 | bellard | tswapls(®isters[i]); |
192 | e95c8d51 | bellard | for(i = 36; i < 44; i++) |
193 | e95c8d51 | bellard | tswapls(®isters[i]); |
194 | 6da41eaf | bellard | return 44 * 4; |
195 | 6da41eaf | bellard | } |
196 | 6da41eaf | bellard | |
197 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
198 | 6da41eaf | bellard | { |
199 | 6da41eaf | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
200 | 6da41eaf | bellard | int i;
|
201 | 6da41eaf | bellard | |
202 | 6da41eaf | bellard | for(i = 0; i < 8; i++) { |
203 | 6da41eaf | bellard | env->regs[i] = tswapl(registers[i]); |
204 | 6da41eaf | bellard | } |
205 | e95c8d51 | bellard | env->eip = tswapl(registers[8]);
|
206 | e95c8d51 | bellard | env->eflags = tswapl(registers[9]);
|
207 | 6da41eaf | bellard | #if defined(CONFIG_USER_ONLY)
|
208 | 6da41eaf | bellard | #define LOAD_SEG(index, sreg)\
|
209 | 6da41eaf | bellard | if (tswapl(registers[index]) != env->segs[sreg].selector)\
|
210 | 6da41eaf | bellard | cpu_x86_load_seg(env, sreg, tswapl(registers[index])); |
211 | 6da41eaf | bellard | LOAD_SEG(10, R_CS);
|
212 | 6da41eaf | bellard | LOAD_SEG(11, R_SS);
|
213 | 6da41eaf | bellard | LOAD_SEG(12, R_DS);
|
214 | 6da41eaf | bellard | LOAD_SEG(13, R_ES);
|
215 | 6da41eaf | bellard | LOAD_SEG(14, R_FS);
|
216 | 6da41eaf | bellard | LOAD_SEG(15, R_GS);
|
217 | 6da41eaf | bellard | #endif
|
218 | 6da41eaf | bellard | } |
219 | 6da41eaf | bellard | |
220 | 9e62fd7f | bellard | #elif defined (TARGET_PPC)
|
221 | 9e62fd7f | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
222 | 9e62fd7f | bellard | { |
223 | a541f297 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
224 | 9e62fd7f | bellard | int i;
|
225 | 9e62fd7f | bellard | |
226 | 9e62fd7f | bellard | /* fill in gprs */
|
227 | a541f297 | bellard | for(i = 0; i < 32; i++) { |
228 | e95c8d51 | bellard | registers[i] = tswapl(env->gpr[i]); |
229 | 9e62fd7f | bellard | } |
230 | 9e62fd7f | bellard | /* fill in fprs */
|
231 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
232 | e95c8d51 | bellard | registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i])); |
233 | e95c8d51 | bellard | registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1)); |
234 | 9e62fd7f | bellard | } |
235 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
236 | e95c8d51 | bellard | registers[96] = tswapl(env->nip);
|
237 | e95c8d51 | bellard | registers[97] = tswapl(_load_msr(env));
|
238 | 9e62fd7f | bellard | tmp = 0;
|
239 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
240 | a541f297 | bellard | tmp |= env->crf[i] << (32 - ((i + 1) * 4)); |
241 | e95c8d51 | bellard | registers[98] = tswapl(tmp);
|
242 | e95c8d51 | bellard | registers[99] = tswapl(env->lr);
|
243 | e95c8d51 | bellard | registers[100] = tswapl(env->ctr);
|
244 | e95c8d51 | bellard | registers[101] = tswapl(_load_xer(env));
|
245 | e95c8d51 | bellard | registers[102] = 0; |
246 | a541f297 | bellard | |
247 | a541f297 | bellard | return 103 * 4; |
248 | 9e62fd7f | bellard | } |
249 | 9e62fd7f | bellard | |
250 | 9e62fd7f | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
251 | 9e62fd7f | bellard | { |
252 | 9e62fd7f | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
253 | 9e62fd7f | bellard | int i;
|
254 | 9e62fd7f | bellard | |
255 | 9e62fd7f | bellard | /* fill in gprs */
|
256 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
257 | e95c8d51 | bellard | env->gpr[i] = tswapl(registers[i]); |
258 | 9e62fd7f | bellard | } |
259 | 9e62fd7f | bellard | /* fill in fprs */
|
260 | 9e62fd7f | bellard | for (i = 0; i < 32; i++) { |
261 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]); |
262 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]); |
263 | 9e62fd7f | bellard | } |
264 | 9e62fd7f | bellard | /* nip, msr, ccr, lnk, ctr, xer, mq */
|
265 | e95c8d51 | bellard | env->nip = tswapl(registers[96]);
|
266 | e95c8d51 | bellard | _store_msr(env, tswapl(registers[97]));
|
267 | e95c8d51 | bellard | registers[98] = tswapl(registers[98]); |
268 | 9e62fd7f | bellard | for (i = 0; i < 8; i++) |
269 | a541f297 | bellard | env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF; |
270 | e95c8d51 | bellard | env->lr = tswapl(registers[99]);
|
271 | e95c8d51 | bellard | env->ctr = tswapl(registers[100]);
|
272 | e95c8d51 | bellard | _store_xer(env, tswapl(registers[101]));
|
273 | e95c8d51 | bellard | } |
274 | e95c8d51 | bellard | #elif defined (TARGET_SPARC)
|
275 | e95c8d51 | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
276 | e95c8d51 | bellard | { |
277 | e95c8d51 | bellard | uint32_t *registers = (uint32_t *)mem_buf, tmp; |
278 | e95c8d51 | bellard | int i;
|
279 | e95c8d51 | bellard | |
280 | e95c8d51 | bellard | /* fill in g0..g7 */
|
281 | e95c8d51 | bellard | for(i = 0; i < 7; i++) { |
282 | e95c8d51 | bellard | registers[i] = tswapl(env->gregs[i]); |
283 | e95c8d51 | bellard | } |
284 | e95c8d51 | bellard | /* fill in register window */
|
285 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
286 | e95c8d51 | bellard | registers[i + 8] = tswapl(env->regwptr[i]);
|
287 | e95c8d51 | bellard | } |
288 | e95c8d51 | bellard | /* fill in fprs */
|
289 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
290 | e95c8d51 | bellard | registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
|
291 | e95c8d51 | bellard | } |
292 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
293 | e95c8d51 | bellard | registers[64] = tswapl(env->y);
|
294 | e80cfcfc | bellard | tmp = GET_PSR(env); |
295 | e95c8d51 | bellard | registers[65] = tswapl(tmp);
|
296 | e95c8d51 | bellard | registers[66] = tswapl(env->wim);
|
297 | e95c8d51 | bellard | registers[67] = tswapl(env->tbr);
|
298 | e95c8d51 | bellard | registers[68] = tswapl(env->pc);
|
299 | e95c8d51 | bellard | registers[69] = tswapl(env->npc);
|
300 | e95c8d51 | bellard | registers[70] = tswapl(env->fsr);
|
301 | e95c8d51 | bellard | registers[71] = 0; /* csr */ |
302 | e95c8d51 | bellard | registers[72] = 0; |
303 | e95c8d51 | bellard | |
304 | e95c8d51 | bellard | return 73 * 4; |
305 | e95c8d51 | bellard | } |
306 | e95c8d51 | bellard | |
307 | e95c8d51 | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
308 | e95c8d51 | bellard | { |
309 | e80cfcfc | bellard | uint32_t *registers = (uint32_t *)mem_buf; |
310 | e95c8d51 | bellard | int i;
|
311 | e95c8d51 | bellard | |
312 | e95c8d51 | bellard | /* fill in g0..g7 */
|
313 | e95c8d51 | bellard | for(i = 0; i < 7; i++) { |
314 | e95c8d51 | bellard | env->gregs[i] = tswapl(registers[i]); |
315 | e95c8d51 | bellard | } |
316 | e95c8d51 | bellard | /* fill in register window */
|
317 | e95c8d51 | bellard | for(i = 0; i < 24; i++) { |
318 | e95c8d51 | bellard | env->regwptr[i] = tswapl(registers[i]); |
319 | e95c8d51 | bellard | } |
320 | e95c8d51 | bellard | /* fill in fprs */
|
321 | e95c8d51 | bellard | for (i = 0; i < 32; i++) { |
322 | e95c8d51 | bellard | *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
|
323 | e95c8d51 | bellard | } |
324 | e95c8d51 | bellard | /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
|
325 | e95c8d51 | bellard | env->y = tswapl(registers[64]);
|
326 | e80cfcfc | bellard | PUT_PSR(env, tswapl(registers[65]));
|
327 | e95c8d51 | bellard | env->wim = tswapl(registers[66]);
|
328 | e95c8d51 | bellard | env->tbr = tswapl(registers[67]);
|
329 | e95c8d51 | bellard | env->pc = tswapl(registers[68]);
|
330 | e95c8d51 | bellard | env->npc = tswapl(registers[69]);
|
331 | e95c8d51 | bellard | env->fsr = tswapl(registers[70]);
|
332 | 9e62fd7f | bellard | } |
333 | 6da41eaf | bellard | #else
|
334 | 6da41eaf | bellard | |
335 | 6da41eaf | bellard | static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf) |
336 | 6da41eaf | bellard | { |
337 | 6da41eaf | bellard | return 0; |
338 | 6da41eaf | bellard | } |
339 | 6da41eaf | bellard | |
340 | 6da41eaf | bellard | static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size) |
341 | 6da41eaf | bellard | { |
342 | 6da41eaf | bellard | } |
343 | 6da41eaf | bellard | |
344 | 6da41eaf | bellard | #endif
|
345 | b4608c04 | bellard | |
346 | b4608c04 | bellard | /* port = 0 means default port */
|
347 | 858693c6 | bellard | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
348 | b4608c04 | bellard | { |
349 | 858693c6 | bellard | CPUState *env = cpu_single_env; |
350 | b4608c04 | bellard | const char *p; |
351 | 858693c6 | bellard | int ch, reg_size, type;
|
352 | b4608c04 | bellard | char buf[4096]; |
353 | b4608c04 | bellard | uint8_t mem_buf[2000];
|
354 | b4608c04 | bellard | uint32_t *registers; |
355 | b4608c04 | bellard | uint32_t addr, len; |
356 | b4608c04 | bellard | |
357 | 858693c6 | bellard | #ifdef DEBUG_GDB
|
358 | 858693c6 | bellard | printf("command='%s'\n", line_buf);
|
359 | 858693c6 | bellard | #endif
|
360 | 858693c6 | bellard | p = line_buf; |
361 | 858693c6 | bellard | ch = *p++; |
362 | 858693c6 | bellard | switch(ch) {
|
363 | 858693c6 | bellard | case '?': |
364 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", SIGTRAP); |
365 | 858693c6 | bellard | put_packet(s, buf); |
366 | 858693c6 | bellard | break;
|
367 | 858693c6 | bellard | case 'c': |
368 | 858693c6 | bellard | if (*p != '\0') { |
369 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
370 | 4c3a88a2 | bellard | #if defined(TARGET_I386)
|
371 | 858693c6 | bellard | env->eip = addr; |
372 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
373 | 858693c6 | bellard | env->nip = addr; |
374 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
375 | 8d5f07fa | bellard | env->pc = addr; |
376 | 8d5f07fa | bellard | env->npc = addr + 4;
|
377 | 4c3a88a2 | bellard | #endif
|
378 | 858693c6 | bellard | } |
379 | 858693c6 | bellard | vm_start(); |
380 | 858693c6 | bellard | break;
|
381 | 858693c6 | bellard | case 's': |
382 | 858693c6 | bellard | if (*p != '\0') { |
383 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
384 | c33a346e | bellard | #if defined(TARGET_I386)
|
385 | 858693c6 | bellard | env->eip = addr; |
386 | 5be1a8e0 | bellard | #elif defined (TARGET_PPC)
|
387 | 858693c6 | bellard | env->nip = addr; |
388 | 8d5f07fa | bellard | #elif defined (TARGET_SPARC)
|
389 | 8d5f07fa | bellard | env->pc = addr; |
390 | 8d5f07fa | bellard | env->npc = addr + 4;
|
391 | c33a346e | bellard | #endif
|
392 | 858693c6 | bellard | } |
393 | 858693c6 | bellard | cpu_single_step(env, 1);
|
394 | 858693c6 | bellard | vm_start(); |
395 | 858693c6 | bellard | break;
|
396 | 858693c6 | bellard | case 'g': |
397 | 858693c6 | bellard | reg_size = cpu_gdb_read_registers(env, mem_buf); |
398 | 858693c6 | bellard | memtohex(buf, mem_buf, reg_size); |
399 | 858693c6 | bellard | put_packet(s, buf); |
400 | 858693c6 | bellard | break;
|
401 | 858693c6 | bellard | case 'G': |
402 | 858693c6 | bellard | registers = (void *)mem_buf;
|
403 | 858693c6 | bellard | len = strlen(p) / 2;
|
404 | 858693c6 | bellard | hextomem((uint8_t *)registers, p, len); |
405 | 858693c6 | bellard | cpu_gdb_write_registers(env, mem_buf, len); |
406 | 858693c6 | bellard | put_packet(s, "OK");
|
407 | 858693c6 | bellard | break;
|
408 | 858693c6 | bellard | case 'm': |
409 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
410 | 858693c6 | bellard | if (*p == ',') |
411 | 858693c6 | bellard | p++; |
412 | 858693c6 | bellard | len = strtoul(p, NULL, 16); |
413 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) |
414 | 858693c6 | bellard | memset(mem_buf, 0, len);
|
415 | 858693c6 | bellard | memtohex(buf, mem_buf, len); |
416 | 858693c6 | bellard | put_packet(s, buf); |
417 | 858693c6 | bellard | break;
|
418 | 858693c6 | bellard | case 'M': |
419 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
420 | 858693c6 | bellard | if (*p == ',') |
421 | 858693c6 | bellard | p++; |
422 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
423 | 858693c6 | bellard | if (*p == ',') |
424 | 858693c6 | bellard | p++; |
425 | 858693c6 | bellard | hextomem(mem_buf, p, len); |
426 | 858693c6 | bellard | if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0) |
427 | 858693c6 | bellard | put_packet(s, "ENN");
|
428 | 858693c6 | bellard | else
|
429 | 858693c6 | bellard | put_packet(s, "OK");
|
430 | 858693c6 | bellard | break;
|
431 | 858693c6 | bellard | case 'Z': |
432 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
433 | 858693c6 | bellard | if (*p == ',') |
434 | 858693c6 | bellard | p++; |
435 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
436 | 858693c6 | bellard | if (*p == ',') |
437 | 858693c6 | bellard | p++; |
438 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
439 | 858693c6 | bellard | if (type == 0 || type == 1) { |
440 | 858693c6 | bellard | if (cpu_breakpoint_insert(env, addr) < 0) |
441 | 858693c6 | bellard | goto breakpoint_error;
|
442 | 858693c6 | bellard | put_packet(s, "OK");
|
443 | 858693c6 | bellard | } else {
|
444 | 858693c6 | bellard | breakpoint_error:
|
445 | 858693c6 | bellard | put_packet(s, "ENN");
|
446 | 858693c6 | bellard | } |
447 | 858693c6 | bellard | break;
|
448 | 858693c6 | bellard | case 'z': |
449 | 858693c6 | bellard | type = strtoul(p, (char **)&p, 16); |
450 | 858693c6 | bellard | if (*p == ',') |
451 | 858693c6 | bellard | p++; |
452 | 858693c6 | bellard | addr = strtoul(p, (char **)&p, 16); |
453 | 858693c6 | bellard | if (*p == ',') |
454 | 858693c6 | bellard | p++; |
455 | 858693c6 | bellard | len = strtoul(p, (char **)&p, 16); |
456 | 858693c6 | bellard | if (type == 0 || type == 1) { |
457 | 858693c6 | bellard | cpu_breakpoint_remove(env, addr); |
458 | 858693c6 | bellard | put_packet(s, "OK");
|
459 | 858693c6 | bellard | } else {
|
460 | 858693c6 | bellard | goto breakpoint_error;
|
461 | 858693c6 | bellard | } |
462 | 858693c6 | bellard | break;
|
463 | 858693c6 | bellard | default:
|
464 | 858693c6 | bellard | // unknown_command:
|
465 | 858693c6 | bellard | /* put empty packet */
|
466 | 858693c6 | bellard | buf[0] = '\0'; |
467 | 858693c6 | bellard | put_packet(s, buf); |
468 | 858693c6 | bellard | break;
|
469 | 858693c6 | bellard | } |
470 | 858693c6 | bellard | return RS_IDLE;
|
471 | 858693c6 | bellard | } |
472 | 858693c6 | bellard | |
473 | 612458f5 | bellard | extern void tb_flush(CPUState *env); |
474 | 612458f5 | bellard | |
475 | 858693c6 | bellard | static void gdb_vm_stopped(void *opaque, int reason) |
476 | 858693c6 | bellard | { |
477 | 858693c6 | bellard | GDBState *s = opaque; |
478 | 858693c6 | bellard | char buf[256]; |
479 | 858693c6 | bellard | int ret;
|
480 | 858693c6 | bellard | |
481 | 858693c6 | bellard | /* disable single step if it was enable */
|
482 | 858693c6 | bellard | cpu_single_step(cpu_single_env, 0);
|
483 | 858693c6 | bellard | |
484 | e80cfcfc | bellard | if (reason == EXCP_DEBUG) {
|
485 | e80cfcfc | bellard | tb_flush(cpu_single_env); |
486 | 858693c6 | bellard | ret = SIGTRAP; |
487 | e80cfcfc | bellard | } |
488 | 858693c6 | bellard | else
|
489 | 858693c6 | bellard | ret = 0;
|
490 | 858693c6 | bellard | snprintf(buf, sizeof(buf), "S%02x", ret); |
491 | 858693c6 | bellard | put_packet(s, buf); |
492 | 858693c6 | bellard | } |
493 | 858693c6 | bellard | |
494 | 858693c6 | bellard | static void gdb_read_byte(GDBState *s, int ch) |
495 | 858693c6 | bellard | { |
496 | 858693c6 | bellard | int i, csum;
|
497 | 858693c6 | bellard | char reply[1]; |
498 | 858693c6 | bellard | |
499 | 858693c6 | bellard | if (vm_running) {
|
500 | 858693c6 | bellard | /* when the CPU is running, we cannot do anything except stop
|
501 | 858693c6 | bellard | it when receiving a char */
|
502 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
503 | 858693c6 | bellard | } else {
|
504 | 858693c6 | bellard | switch(s->state) {
|
505 | 858693c6 | bellard | case RS_IDLE:
|
506 | 858693c6 | bellard | if (ch == '$') { |
507 | 858693c6 | bellard | s->line_buf_index = 0;
|
508 | 858693c6 | bellard | s->state = RS_GETLINE; |
509 | c33a346e | bellard | } |
510 | b4608c04 | bellard | break;
|
511 | 858693c6 | bellard | case RS_GETLINE:
|
512 | 858693c6 | bellard | if (ch == '#') { |
513 | 858693c6 | bellard | s->state = RS_CHKSUM1; |
514 | 858693c6 | bellard | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
515 | 858693c6 | bellard | s->state = RS_IDLE; |
516 | 4c3a88a2 | bellard | } else {
|
517 | 858693c6 | bellard | s->line_buf[s->line_buf_index++] = ch; |
518 | 4c3a88a2 | bellard | } |
519 | 4c3a88a2 | bellard | break;
|
520 | 858693c6 | bellard | case RS_CHKSUM1:
|
521 | 858693c6 | bellard | s->line_buf[s->line_buf_index] = '\0';
|
522 | 858693c6 | bellard | s->line_csum = fromhex(ch) << 4;
|
523 | 858693c6 | bellard | s->state = RS_CHKSUM2; |
524 | 858693c6 | bellard | break;
|
525 | 858693c6 | bellard | case RS_CHKSUM2:
|
526 | 858693c6 | bellard | s->line_csum |= fromhex(ch); |
527 | 858693c6 | bellard | csum = 0;
|
528 | 858693c6 | bellard | for(i = 0; i < s->line_buf_index; i++) { |
529 | 858693c6 | bellard | csum += s->line_buf[i]; |
530 | 858693c6 | bellard | } |
531 | 858693c6 | bellard | if (s->line_csum != (csum & 0xff)) { |
532 | 858693c6 | bellard | reply[0] = '-'; |
533 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
534 | 858693c6 | bellard | s->state = RS_IDLE; |
535 | 4c3a88a2 | bellard | } else {
|
536 | 858693c6 | bellard | reply[0] = '+'; |
537 | 858693c6 | bellard | put_buffer(s, reply, 1);
|
538 | 858693c6 | bellard | s->state = gdb_handle_packet(s, s->line_buf); |
539 | 4c3a88a2 | bellard | } |
540 | 4c3a88a2 | bellard | break;
|
541 | 858693c6 | bellard | } |
542 | 858693c6 | bellard | } |
543 | 858693c6 | bellard | } |
544 | 858693c6 | bellard | |
545 | 858693c6 | bellard | static int gdb_can_read(void *opaque) |
546 | 858693c6 | bellard | { |
547 | 858693c6 | bellard | return 256; |
548 | 858693c6 | bellard | } |
549 | 858693c6 | bellard | |
550 | 858693c6 | bellard | static void gdb_read(void *opaque, const uint8_t *buf, int size) |
551 | 858693c6 | bellard | { |
552 | 858693c6 | bellard | GDBState *s = opaque; |
553 | 858693c6 | bellard | int i;
|
554 | 858693c6 | bellard | if (size == 0) { |
555 | 858693c6 | bellard | /* end of connection */
|
556 | 858693c6 | bellard | qemu_del_vm_stop_handler(gdb_vm_stopped, s); |
557 | 858693c6 | bellard | qemu_del_fd_read_handler(s->fd); |
558 | 858693c6 | bellard | qemu_free(s); |
559 | 858693c6 | bellard | vm_start(); |
560 | 858693c6 | bellard | } else {
|
561 | 858693c6 | bellard | for(i = 0; i < size; i++) |
562 | 858693c6 | bellard | gdb_read_byte(s, buf[i]); |
563 | 858693c6 | bellard | } |
564 | 858693c6 | bellard | } |
565 | 858693c6 | bellard | |
566 | 858693c6 | bellard | static void gdb_accept(void *opaque, const uint8_t *buf, int size) |
567 | 858693c6 | bellard | { |
568 | 858693c6 | bellard | GDBState *s; |
569 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
570 | 858693c6 | bellard | socklen_t len; |
571 | 858693c6 | bellard | int val, fd;
|
572 | 858693c6 | bellard | |
573 | 858693c6 | bellard | for(;;) {
|
574 | 858693c6 | bellard | len = sizeof(sockaddr);
|
575 | 858693c6 | bellard | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
576 | 858693c6 | bellard | if (fd < 0 && errno != EINTR) { |
577 | 858693c6 | bellard | perror("accept");
|
578 | 858693c6 | bellard | return;
|
579 | 858693c6 | bellard | } else if (fd >= 0) { |
580 | b4608c04 | bellard | break;
|
581 | b4608c04 | bellard | } |
582 | b4608c04 | bellard | } |
583 | 858693c6 | bellard | |
584 | 858693c6 | bellard | /* set short latency */
|
585 | 858693c6 | bellard | val = 1;
|
586 | 7d3505c5 | bellard | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
|
587 | 858693c6 | bellard | |
588 | 858693c6 | bellard | s = qemu_mallocz(sizeof(GDBState));
|
589 | 858693c6 | bellard | if (!s) {
|
590 | 858693c6 | bellard | close(fd); |
591 | 858693c6 | bellard | return;
|
592 | 858693c6 | bellard | } |
593 | 858693c6 | bellard | s->fd = fd; |
594 | 858693c6 | bellard | |
595 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
596 | 858693c6 | bellard | |
597 | 858693c6 | bellard | /* stop the VM */
|
598 | 858693c6 | bellard | vm_stop(EXCP_INTERRUPT); |
599 | 858693c6 | bellard | |
600 | 858693c6 | bellard | /* start handling I/O */
|
601 | 858693c6 | bellard | qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s); |
602 | 858693c6 | bellard | /* when the VM is stopped, the following callback is called */
|
603 | 858693c6 | bellard | qemu_add_vm_stop_handler(gdb_vm_stopped, s); |
604 | 858693c6 | bellard | } |
605 | 858693c6 | bellard | |
606 | 858693c6 | bellard | static int gdbserver_open(int port) |
607 | 858693c6 | bellard | { |
608 | 858693c6 | bellard | struct sockaddr_in sockaddr;
|
609 | 858693c6 | bellard | int fd, val, ret;
|
610 | 858693c6 | bellard | |
611 | 858693c6 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
612 | 858693c6 | bellard | if (fd < 0) { |
613 | 858693c6 | bellard | perror("socket");
|
614 | 858693c6 | bellard | return -1; |
615 | 858693c6 | bellard | } |
616 | 858693c6 | bellard | |
617 | 858693c6 | bellard | /* allow fast reuse */
|
618 | 858693c6 | bellard | val = 1;
|
619 | 858693c6 | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
620 | 858693c6 | bellard | |
621 | 858693c6 | bellard | sockaddr.sin_family = AF_INET; |
622 | 858693c6 | bellard | sockaddr.sin_port = htons(port); |
623 | 858693c6 | bellard | sockaddr.sin_addr.s_addr = 0;
|
624 | 858693c6 | bellard | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
625 | 858693c6 | bellard | if (ret < 0) { |
626 | 858693c6 | bellard | perror("bind");
|
627 | 858693c6 | bellard | return -1; |
628 | 858693c6 | bellard | } |
629 | 858693c6 | bellard | ret = listen(fd, 0);
|
630 | 858693c6 | bellard | if (ret < 0) { |
631 | 858693c6 | bellard | perror("listen");
|
632 | 858693c6 | bellard | return -1; |
633 | 858693c6 | bellard | } |
634 | 858693c6 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
635 | 858693c6 | bellard | return fd;
|
636 | 858693c6 | bellard | } |
637 | 858693c6 | bellard | |
638 | 858693c6 | bellard | int gdbserver_start(int port) |
639 | 858693c6 | bellard | { |
640 | 858693c6 | bellard | gdbserver_fd = gdbserver_open(port); |
641 | 858693c6 | bellard | if (gdbserver_fd < 0) |
642 | 858693c6 | bellard | return -1; |
643 | 858693c6 | bellard | /* accept connections */
|
644 | 858693c6 | bellard | qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL); |
645 | b4608c04 | bellard | return 0; |
646 | b4608c04 | bellard | } |