2 * A Win32 based proxy implementing the GBD remote protocol
3 * This allows to debug Wine (and any "emulated" program) under
6 * Copyright (c) Eric Pouech 2002-2004
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Protocol specification can be found here:
24 * http://sources.redhat.com/gdb/onlinedocs/gdb/Maintenance-Commands.html
28 #include "wine/port.h"
37 #ifdef HAVE_SYS_POLL_H
38 # include <sys/poll.h>
40 #ifdef HAVE_SYS_WAIT_H
41 # include <sys/wait.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
49 #ifdef HAVE_NETINET_TCP_H
50 # include <netinet/tcp.h>
56 /* if we don't have poll support on this system
57 * we won't provide gdb proxy support here...
67 #define GDBPXY_TRC_LOWLEVEL 0x01
68 #define GDBPXY_TRC_PACKET 0x02
69 #define GDBPXY_TRC_COMMAND 0x04
70 #define GDBPXY_TRC_COMMAND_ERROR 0x08
71 #define GDBPXY_TRC_WIN32_EVENT 0x10
72 #define GDBPXY_TRC_WIN32_ERROR 0x20
73 #define GDBPXY_TRC_COMMAND_FIXME 0x80
77 enum be_xpoint_type type
; /* -1 means free */
90 /* split into individual packet */
98 /* generic GDB thread information */
99 struct dbg_thread
* exec_thread
; /* thread used in step & continue */
100 struct dbg_thread
* other_thread
; /* thread to be used in any other operation */
102 /* current Win32 trap env */
106 /* Win32 information */
107 struct dbg_process
* process
;
108 #define NUM_XPOINT 32
109 struct gdb_ctx_Xpoint Xpoints
[NUM_XPOINT
];
110 /* Unix environment */
111 unsigned long wine_segs
[3]; /* load addresses of the ELF wine exec segments (text, bss and data) */
114 static BOOL
tgt_process_gdbproxy_read(HANDLE hProcess
, const void* addr
,
115 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
117 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
120 static BOOL
tgt_process_gdbproxy_write(HANDLE hProcess
, void* addr
,
121 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
123 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
126 static struct be_process_io be_process_gdbproxy_io
=
128 NULL
, /* we shouldn't use close_process() in gdbproxy */
129 tgt_process_gdbproxy_read
,
130 tgt_process_gdbproxy_write
133 /* =============================================== *
134 * B A S I C M A N I P U L A T I O N S *
135 * =============================================== *
138 static inline int hex_from0(char ch
)
140 if (ch
>= '0' && ch
<= '9') return ch
- '0';
141 if (ch
>= 'A' && ch
<= 'F') return ch
- 'A' + 10;
142 if (ch
>= 'a' && ch
<= 'f') return ch
- 'a' + 10;
148 static inline unsigned char hex_to0(int x
)
150 assert(x
>= 0 && x
< 16);
151 return "0123456789abcdef"[x
];
154 static int hex_to_int(const char* src
, size_t len
)
156 unsigned int returnval
= 0;
160 returnval
|= hex_from0(*src
++);
165 static void hex_from(void* dst
, const char* src
, size_t len
)
167 unsigned char *p
= dst
;
170 *p
++ = (hex_from0(src
[0]) << 4) | hex_from0(src
[1]);
175 static void hex_to(char* dst
, const void* src
, size_t len
)
177 const unsigned char *p
= src
;
180 *dst
++ = hex_to0(*p
>> 4);
181 *dst
++ = hex_to0(*p
& 0x0F);
186 static unsigned char checksum(const char* ptr
, int len
)
191 cksum
+= (unsigned char)*ptr
++;
195 /* =============================================== *
196 * C P U H A N D L E R S *
197 * =============================================== *
201 static size_t cpu_register_map
[] = {
202 FIELD_OFFSET(CONTEXT
, Eax
),
203 FIELD_OFFSET(CONTEXT
, Ecx
),
204 FIELD_OFFSET(CONTEXT
, Edx
),
205 FIELD_OFFSET(CONTEXT
, Ebx
),
206 FIELD_OFFSET(CONTEXT
, Esp
),
207 FIELD_OFFSET(CONTEXT
, Ebp
),
208 FIELD_OFFSET(CONTEXT
, Esi
),
209 FIELD_OFFSET(CONTEXT
, Edi
),
210 FIELD_OFFSET(CONTEXT
, Eip
),
211 FIELD_OFFSET(CONTEXT
, EFlags
),
212 FIELD_OFFSET(CONTEXT
, SegCs
),
213 FIELD_OFFSET(CONTEXT
, SegSs
),
214 FIELD_OFFSET(CONTEXT
, SegDs
),
215 FIELD_OFFSET(CONTEXT
, SegEs
),
216 FIELD_OFFSET(CONTEXT
, SegFs
),
217 FIELD_OFFSET(CONTEXT
, SegGs
),
219 #elif defined(__powerpc__)
220 static size_t cpu_register_map
[] = {
221 FIELD_OFFSET(CONTEXT
, Gpr0
),
222 FIELD_OFFSET(CONTEXT
, Gpr1
),
223 FIELD_OFFSET(CONTEXT
, Gpr2
),
224 FIELD_OFFSET(CONTEXT
, Gpr3
),
225 FIELD_OFFSET(CONTEXT
, Gpr4
),
226 FIELD_OFFSET(CONTEXT
, Gpr5
),
227 FIELD_OFFSET(CONTEXT
, Gpr6
),
228 FIELD_OFFSET(CONTEXT
, Gpr7
),
229 FIELD_OFFSET(CONTEXT
, Gpr8
),
230 FIELD_OFFSET(CONTEXT
, Gpr9
),
231 FIELD_OFFSET(CONTEXT
, Gpr10
),
232 FIELD_OFFSET(CONTEXT
, Gpr11
),
233 FIELD_OFFSET(CONTEXT
, Gpr12
),
234 FIELD_OFFSET(CONTEXT
, Gpr13
),
235 FIELD_OFFSET(CONTEXT
, Gpr14
),
236 FIELD_OFFSET(CONTEXT
, Gpr15
),
237 FIELD_OFFSET(CONTEXT
, Gpr16
),
238 FIELD_OFFSET(CONTEXT
, Gpr17
),
239 FIELD_OFFSET(CONTEXT
, Gpr18
),
240 FIELD_OFFSET(CONTEXT
, Gpr19
),
241 FIELD_OFFSET(CONTEXT
, Gpr20
),
242 FIELD_OFFSET(CONTEXT
, Gpr21
),
243 FIELD_OFFSET(CONTEXT
, Gpr22
),
244 FIELD_OFFSET(CONTEXT
, Gpr23
),
245 FIELD_OFFSET(CONTEXT
, Gpr24
),
246 FIELD_OFFSET(CONTEXT
, Gpr25
),
247 FIELD_OFFSET(CONTEXT
, Gpr26
),
248 FIELD_OFFSET(CONTEXT
, Gpr27
),
249 FIELD_OFFSET(CONTEXT
, Gpr28
),
250 FIELD_OFFSET(CONTEXT
, Gpr29
),
251 FIELD_OFFSET(CONTEXT
, Gpr30
),
252 FIELD_OFFSET(CONTEXT
, Gpr31
),
253 FIELD_OFFSET(CONTEXT
, Fpr0
),
254 FIELD_OFFSET(CONTEXT
, Fpr1
),
255 FIELD_OFFSET(CONTEXT
, Fpr2
),
256 FIELD_OFFSET(CONTEXT
, Fpr3
),
257 FIELD_OFFSET(CONTEXT
, Fpr4
),
258 FIELD_OFFSET(CONTEXT
, Fpr5
),
259 FIELD_OFFSET(CONTEXT
, Fpr6
),
260 FIELD_OFFSET(CONTEXT
, Fpr7
),
261 FIELD_OFFSET(CONTEXT
, Fpr8
),
262 FIELD_OFFSET(CONTEXT
, Fpr9
),
263 FIELD_OFFSET(CONTEXT
, Fpr10
),
264 FIELD_OFFSET(CONTEXT
, Fpr11
),
265 FIELD_OFFSET(CONTEXT
, Fpr12
),
266 FIELD_OFFSET(CONTEXT
, Fpr13
),
267 FIELD_OFFSET(CONTEXT
, Fpr14
),
268 FIELD_OFFSET(CONTEXT
, Fpr15
),
269 FIELD_OFFSET(CONTEXT
, Fpr16
),
270 FIELD_OFFSET(CONTEXT
, Fpr17
),
271 FIELD_OFFSET(CONTEXT
, Fpr18
),
272 FIELD_OFFSET(CONTEXT
, Fpr19
),
273 FIELD_OFFSET(CONTEXT
, Fpr20
),
274 FIELD_OFFSET(CONTEXT
, Fpr21
),
275 FIELD_OFFSET(CONTEXT
, Fpr22
),
276 FIELD_OFFSET(CONTEXT
, Fpr23
),
277 FIELD_OFFSET(CONTEXT
, Fpr24
),
278 FIELD_OFFSET(CONTEXT
, Fpr25
),
279 FIELD_OFFSET(CONTEXT
, Fpr26
),
280 FIELD_OFFSET(CONTEXT
, Fpr27
),
281 FIELD_OFFSET(CONTEXT
, Fpr28
),
282 FIELD_OFFSET(CONTEXT
, Fpr29
),
283 FIELD_OFFSET(CONTEXT
, Fpr30
),
284 FIELD_OFFSET(CONTEXT
, Fpr31
),
286 FIELD_OFFSET(CONTEXT
, Iar
),
287 FIELD_OFFSET(CONTEXT
, Msr
),
288 FIELD_OFFSET(CONTEXT
, Cr
),
289 FIELD_OFFSET(CONTEXT
, Lr
),
290 FIELD_OFFSET(CONTEXT
, Ctr
),
291 FIELD_OFFSET(CONTEXT
, Xer
),
292 /* FIXME: MQ is missing? FIELD_OFFSET(CONTEXT, Mq), */
293 /* see gdb/nlm/ppc.c */
295 #elif defined(__ALPHA__)
296 static size_t cpu_register_map
[] = {
297 FIELD_OFFSET(CONTEXT
, IntV0
),
298 FIELD_OFFSET(CONTEXT
, IntT0
),
299 FIELD_OFFSET(CONTEXT
, IntT1
),
300 FIELD_OFFSET(CONTEXT
, IntT2
),
301 FIELD_OFFSET(CONTEXT
, IntT3
),
302 FIELD_OFFSET(CONTEXT
, IntT4
),
303 FIELD_OFFSET(CONTEXT
, IntT5
),
304 FIELD_OFFSET(CONTEXT
, IntT6
),
305 FIELD_OFFSET(CONTEXT
, IntT7
),
306 FIELD_OFFSET(CONTEXT
, IntS0
),
307 FIELD_OFFSET(CONTEXT
, IntS1
),
308 FIELD_OFFSET(CONTEXT
, IntS2
),
309 FIELD_OFFSET(CONTEXT
, IntS3
),
310 FIELD_OFFSET(CONTEXT
, IntS4
),
311 FIELD_OFFSET(CONTEXT
, IntS5
),
312 FIELD_OFFSET(CONTEXT
, IntFp
),
313 FIELD_OFFSET(CONTEXT
, IntA0
),
314 FIELD_OFFSET(CONTEXT
, IntA1
),
315 FIELD_OFFSET(CONTEXT
, IntA2
),
316 FIELD_OFFSET(CONTEXT
, IntA3
),
317 FIELD_OFFSET(CONTEXT
, IntA4
),
318 FIELD_OFFSET(CONTEXT
, IntA5
),
319 FIELD_OFFSET(CONTEXT
, IntT8
),
320 FIELD_OFFSET(CONTEXT
, IntT9
),
321 FIELD_OFFSET(CONTEXT
, IntT10
),
322 FIELD_OFFSET(CONTEXT
, IntT11
),
323 FIELD_OFFSET(CONTEXT
, IntRa
),
324 FIELD_OFFSET(CONTEXT
, IntT12
),
325 FIELD_OFFSET(CONTEXT
, IntAt
),
326 FIELD_OFFSET(CONTEXT
, IntGp
),
327 FIELD_OFFSET(CONTEXT
, IntSp
),
328 FIELD_OFFSET(CONTEXT
, IntZero
),
329 FIELD_OFFSET(CONTEXT
, FltF0
),
330 FIELD_OFFSET(CONTEXT
, FltF1
),
331 FIELD_OFFSET(CONTEXT
, FltF2
),
332 FIELD_OFFSET(CONTEXT
, FltF3
),
333 FIELD_OFFSET(CONTEXT
, FltF4
),
334 FIELD_OFFSET(CONTEXT
, FltF5
),
335 FIELD_OFFSET(CONTEXT
, FltF6
),
336 FIELD_OFFSET(CONTEXT
, FltF7
),
337 FIELD_OFFSET(CONTEXT
, FltF8
),
338 FIELD_OFFSET(CONTEXT
, FltF9
),
339 FIELD_OFFSET(CONTEXT
, FltF10
),
340 FIELD_OFFSET(CONTEXT
, FltF11
),
341 FIELD_OFFSET(CONTEXT
, FltF12
),
342 FIELD_OFFSET(CONTEXT
, FltF13
),
343 FIELD_OFFSET(CONTEXT
, FltF14
),
344 FIELD_OFFSET(CONTEXT
, FltF15
),
345 FIELD_OFFSET(CONTEXT
, FltF16
),
346 FIELD_OFFSET(CONTEXT
, FltF17
),
347 FIELD_OFFSET(CONTEXT
, FltF18
),
348 FIELD_OFFSET(CONTEXT
, FltF19
),
349 FIELD_OFFSET(CONTEXT
, FltF20
),
350 FIELD_OFFSET(CONTEXT
, FltF21
),
351 FIELD_OFFSET(CONTEXT
, FltF22
),
352 FIELD_OFFSET(CONTEXT
, FltF23
),
353 FIELD_OFFSET(CONTEXT
, FltF24
),
354 FIELD_OFFSET(CONTEXT
, FltF25
),
355 FIELD_OFFSET(CONTEXT
, FltF26
),
356 FIELD_OFFSET(CONTEXT
, FltF27
),
357 FIELD_OFFSET(CONTEXT
, FltF28
),
358 FIELD_OFFSET(CONTEXT
, FltF29
),
359 FIELD_OFFSET(CONTEXT
, FltF30
),
360 FIELD_OFFSET(CONTEXT
, FltF31
),
362 /* FIXME: Didn't look for the right order yet */
363 FIELD_OFFSET(CONTEXT
, Fir
),
364 FIELD_OFFSET(CONTEXT
, Fpcr
),
365 FIELD_OFFSET(CONTEXT
, SoftFpcr
),
367 #elif defined(__x86_64__)
368 static size_t cpu_register_map
[] = {
369 FIELD_OFFSET(CONTEXT
, Rax
),
370 FIELD_OFFSET(CONTEXT
, Rbx
),
371 FIELD_OFFSET(CONTEXT
, Rcx
),
372 FIELD_OFFSET(CONTEXT
, Rdx
),
373 FIELD_OFFSET(CONTEXT
, Rsi
),
374 FIELD_OFFSET(CONTEXT
, Rdi
),
375 FIELD_OFFSET(CONTEXT
, Rbp
),
376 FIELD_OFFSET(CONTEXT
, Rsp
),
377 FIELD_OFFSET(CONTEXT
, R8
),
378 FIELD_OFFSET(CONTEXT
, R9
),
379 FIELD_OFFSET(CONTEXT
, R10
),
380 FIELD_OFFSET(CONTEXT
, R11
),
381 FIELD_OFFSET(CONTEXT
, R12
),
382 FIELD_OFFSET(CONTEXT
, R13
),
383 FIELD_OFFSET(CONTEXT
, R14
),
384 FIELD_OFFSET(CONTEXT
, R15
),
385 FIELD_OFFSET(CONTEXT
, Rip
),
386 FIELD_OFFSET(CONTEXT
, EFlags
),
387 FIELD_OFFSET(CONTEXT
, SegCs
),
388 FIELD_OFFSET(CONTEXT
, SegSs
),
389 FIELD_OFFSET(CONTEXT
, SegDs
),
390 FIELD_OFFSET(CONTEXT
, SegEs
),
391 FIELD_OFFSET(CONTEXT
, SegFs
),
392 FIELD_OFFSET(CONTEXT
, SegGs
),
394 #elif defined(__sparc__)
395 static size_t cpu_register_map
[] = {
396 FIELD_OFFSET(CONTEXT
, g0
),
397 FIELD_OFFSET(CONTEXT
, g1
),
398 FIELD_OFFSET(CONTEXT
, g2
),
399 FIELD_OFFSET(CONTEXT
, g3
),
400 FIELD_OFFSET(CONTEXT
, g4
),
401 FIELD_OFFSET(CONTEXT
, g5
),
402 FIELD_OFFSET(CONTEXT
, g6
),
403 FIELD_OFFSET(CONTEXT
, g7
),
404 FIELD_OFFSET(CONTEXT
, o0
),
405 FIELD_OFFSET(CONTEXT
, o1
),
406 FIELD_OFFSET(CONTEXT
, o2
),
407 FIELD_OFFSET(CONTEXT
, o3
),
408 FIELD_OFFSET(CONTEXT
, o4
),
409 FIELD_OFFSET(CONTEXT
, o5
),
410 FIELD_OFFSET(CONTEXT
, o6
),
411 FIELD_OFFSET(CONTEXT
, o7
),
412 FIELD_OFFSET(CONTEXT
, l0
),
413 FIELD_OFFSET(CONTEXT
, l1
),
414 FIELD_OFFSET(CONTEXT
, l2
),
415 FIELD_OFFSET(CONTEXT
, l3
),
416 FIELD_OFFSET(CONTEXT
, l4
),
417 FIELD_OFFSET(CONTEXT
, l5
),
418 FIELD_OFFSET(CONTEXT
, l6
),
419 FIELD_OFFSET(CONTEXT
, l7
),
420 FIELD_OFFSET(CONTEXT
, i0
),
421 FIELD_OFFSET(CONTEXT
, i1
),
422 FIELD_OFFSET(CONTEXT
, i2
),
423 FIELD_OFFSET(CONTEXT
, i3
),
424 FIELD_OFFSET(CONTEXT
, i4
),
425 FIELD_OFFSET(CONTEXT
, i5
),
426 FIELD_OFFSET(CONTEXT
, i6
),
427 FIELD_OFFSET(CONTEXT
, i7
),
430 # error Define the registers map for your CPU
433 static const size_t cpu_num_regs
= (sizeof(cpu_register_map
) / sizeof(cpu_register_map
[0]));
435 static inline unsigned long* cpu_register(CONTEXT
* ctx
, unsigned idx
)
437 assert(idx
< cpu_num_regs
);
438 return (unsigned long*)((char*)ctx
+ cpu_register_map
[idx
]);
441 /* =============================================== *
442 * W I N 3 2 D E B U G I N T E R F A C E *
443 * =============================================== *
446 static BOOL
fetch_context(struct gdb_context
* gdbctx
, HANDLE h
, CONTEXT
* ctx
)
448 ctx
->ContextFlags
= CONTEXT_CONTROL
450 #ifdef CONTEXT_SEGMENTS
453 #ifdef CONTEXT_DEBUG_REGISTERS
454 | CONTEXT_DEBUG_REGISTERS
457 if (!GetThreadContext(h
, ctx
))
459 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
460 fprintf(stderr
, "Can't get thread's context\n");
466 static BOOL
handle_exception(struct gdb_context
* gdbctx
, EXCEPTION_DEBUG_INFO
* exc
)
468 EXCEPTION_RECORD
* rec
= &exc
->ExceptionRecord
;
471 switch (rec
->ExceptionCode
)
473 case EXCEPTION_ACCESS_VIOLATION
:
474 case EXCEPTION_PRIV_INSTRUCTION
:
475 case EXCEPTION_STACK_OVERFLOW
:
476 case EXCEPTION_GUARD_PAGE
:
477 gdbctx
->last_sig
= SIGSEGV
;
480 case EXCEPTION_DATATYPE_MISALIGNMENT
:
481 gdbctx
->last_sig
= SIGBUS
;
484 case EXCEPTION_SINGLE_STEP
:
486 case EXCEPTION_BREAKPOINT
:
487 gdbctx
->last_sig
= SIGTRAP
;
490 case EXCEPTION_FLT_DENORMAL_OPERAND
:
491 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
492 case EXCEPTION_FLT_INEXACT_RESULT
:
493 case EXCEPTION_FLT_INVALID_OPERATION
:
494 case EXCEPTION_FLT_OVERFLOW
:
495 case EXCEPTION_FLT_STACK_CHECK
:
496 case EXCEPTION_FLT_UNDERFLOW
:
497 gdbctx
->last_sig
= SIGFPE
;
500 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
501 case EXCEPTION_INT_OVERFLOW
:
502 gdbctx
->last_sig
= SIGFPE
;
505 case EXCEPTION_ILLEGAL_INSTRUCTION
:
506 gdbctx
->last_sig
= SIGILL
;
510 gdbctx
->last_sig
= SIGINT
;
513 case STATUS_POSSIBLE_DEADLOCK
:
514 gdbctx
->last_sig
= SIGALRM
;
516 /* FIXME: we could also add here a O packet with additional information */
519 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
520 fprintf(stderr
, "Unhandled exception code 0x%08x\n", rec
->ExceptionCode
);
521 gdbctx
->last_sig
= SIGABRT
;
528 static void handle_debug_event(struct gdb_context
* gdbctx
, DEBUG_EVENT
* de
)
535 dbg_curr_thread
= dbg_get_thread(gdbctx
->process
, de
->dwThreadId
);
537 switch (de
->dwDebugEventCode
)
539 case CREATE_PROCESS_DEBUG_EVENT
:
540 gdbctx
->process
= dbg_add_process(&be_process_gdbproxy_io
, de
->dwProcessId
,
541 de
->u
.CreateProcessInfo
.hProcess
);
542 if (!gdbctx
->process
) break;
543 memory_get_string_indirect(gdbctx
->process
,
544 de
->u
.CreateProcessInfo
.lpImageName
,
545 de
->u
.CreateProcessInfo
.fUnicode
,
546 u
.buffer
, sizeof(u
.buffer
) / sizeof(WCHAR
));
547 dbg_set_process_name(gdbctx
->process
, u
.buffer
);
549 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
550 fprintf(stderr
, "%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
551 de
->dwProcessId
, de
->dwThreadId
,
552 dbg_W2A(u
.buffer
, -1),
553 de
->u
.CreateProcessInfo
.lpImageName
,
554 de
->u
.CreateProcessInfo
.lpStartAddress
,
555 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
556 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
558 /* de->u.CreateProcessInfo.lpStartAddress; */
559 if (!dbg_init(gdbctx
->process
->handle
, u
.buffer
, TRUE
))
560 fprintf(stderr
, "Couldn't initiate DbgHelp\n");
562 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
563 fprintf(stderr
, "%04x:%04x: create thread I @%p\n",
564 de
->dwProcessId
, de
->dwThreadId
,
565 de
->u
.CreateProcessInfo
.lpStartAddress
);
567 assert(dbg_curr_thread
== NULL
); /* shouldn't be there */
568 dbg_add_thread(gdbctx
->process
, de
->dwThreadId
,
569 de
->u
.CreateProcessInfo
.hThread
,
570 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
573 case LOAD_DLL_DEBUG_EVENT
:
574 assert(dbg_curr_thread
);
575 memory_get_string_indirect(gdbctx
->process
,
576 de
->u
.LoadDll
.lpImageName
,
577 de
->u
.LoadDll
.fUnicode
,
578 u
.buffer
, sizeof(u
.buffer
) / sizeof(WCHAR
));
579 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
580 fprintf(stderr
, "%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
581 de
->dwProcessId
, de
->dwThreadId
,
582 dbg_W2A(u
.buffer
, -1),
583 de
->u
.LoadDll
.lpBaseOfDll
,
584 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
585 de
->u
.LoadDll
.nDebugInfoSize
);
586 dbg_load_module(gdbctx
->process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
587 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
590 case UNLOAD_DLL_DEBUG_EVENT
:
591 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
592 fprintf(stderr
, "%08x:%08x: unload DLL @%p\n",
593 de
->dwProcessId
, de
->dwThreadId
, de
->u
.UnloadDll
.lpBaseOfDll
);
594 SymUnloadModule(gdbctx
->process
->handle
,
595 (DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
598 case EXCEPTION_DEBUG_EVENT
:
599 assert(dbg_curr_thread
);
600 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
601 fprintf(stderr
, "%08x:%08x: exception code=0x%08x\n",
602 de
->dwProcessId
, de
->dwThreadId
,
603 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
605 if (fetch_context(gdbctx
, dbg_curr_thread
->handle
, &gdbctx
->context
))
607 gdbctx
->in_trap
= handle_exception(gdbctx
, &de
->u
.Exception
);
611 case CREATE_THREAD_DEBUG_EVENT
:
612 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
613 fprintf(stderr
, "%08x:%08x: create thread D @%p\n",
614 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
616 dbg_add_thread(gdbctx
->process
,
618 de
->u
.CreateThread
.hThread
,
619 de
->u
.CreateThread
.lpThreadLocalBase
);
622 case EXIT_THREAD_DEBUG_EVENT
:
623 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
624 fprintf(stderr
, "%08x:%08x: exit thread (%u)\n",
625 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
627 assert(dbg_curr_thread
);
628 if (dbg_curr_thread
== gdbctx
->exec_thread
) gdbctx
->exec_thread
= NULL
;
629 if (dbg_curr_thread
== gdbctx
->other_thread
) gdbctx
->other_thread
= NULL
;
630 dbg_del_thread(dbg_curr_thread
);
633 case EXIT_PROCESS_DEBUG_EVENT
:
634 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
635 fprintf(stderr
, "%08x:%08x: exit process (%u)\n",
636 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
638 dbg_del_process(gdbctx
->process
);
639 gdbctx
->process
= NULL
;
640 /* now signal gdb that we're done */
641 gdbctx
->last_sig
= SIGTERM
;
642 gdbctx
->in_trap
= TRUE
;
645 case OUTPUT_DEBUG_STRING_EVENT
:
646 assert(dbg_curr_thread
);
647 memory_get_string(gdbctx
->process
,
648 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
649 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
650 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
651 fprintf(stderr
, "%08x:%08x: output debug string (%s)\n",
652 de
->dwProcessId
, de
->dwThreadId
, u
.bufferA
);
656 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
657 fprintf(stderr
, "%08x:%08x: rip error=%u type=%u\n",
658 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
659 de
->u
.RipInfo
.dwType
);
663 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_EVENT
)
664 fprintf(stderr
, "%08x:%08x: unknown event (%u)\n",
665 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
669 static void resume_debuggee(struct gdb_context
* gdbctx
, DWORD cont
)
673 if (!SetThreadContext(dbg_curr_thread
->handle
, &gdbctx
->context
))
674 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
675 fprintf(stderr
, "Cannot set context on thread %04x\n", dbg_curr_thread
->tid
);
676 if (!ContinueDebugEvent(gdbctx
->process
->pid
, dbg_curr_thread
->tid
, cont
))
677 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
678 fprintf(stderr
, "Cannot continue on %04x (%x)\n",
679 dbg_curr_thread
->tid
, cont
);
681 else if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
682 fprintf(stderr
, "Cannot find last thread\n");
686 static void resume_debuggee_thread(struct gdb_context
* gdbctx
, DWORD cont
, unsigned int threadid
)
691 if(dbg_curr_thread
->tid
== threadid
){
692 /* Windows debug and GDB don't seem to work well here, windows only likes ContinueDebugEvent being used on the reporter of the event */
693 if (!SetThreadContext(dbg_curr_thread
->handle
, &gdbctx
->context
))
694 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
695 fprintf(stderr
, "Cannot set context on thread %04x\n", dbg_curr_thread
->tid
);
696 if (!ContinueDebugEvent(gdbctx
->process
->pid
, dbg_curr_thread
->tid
, cont
))
697 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
698 fprintf(stderr
, "Cannot continue on %04x (%x)\n",
699 dbg_curr_thread
->tid
, cont
);
702 else if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
703 fprintf(stderr
, "Cannot find last thread\n");
706 static BOOL
check_for_interrupt(struct gdb_context
* gdbctx
)
708 struct pollfd pollfd
;
712 pollfd
.fd
= gdbctx
->sock
;
713 pollfd
.events
= POLLIN
;
716 if ((ret
= poll(&pollfd
, 1, 0)) == 1) {
717 ret
= read(gdbctx
->sock
, &pkt
, 1);
719 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
) {
720 fprintf(stderr
, "read failed\n");
725 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
) {
726 fprintf(stderr
, "Unexpected break packet (%c/0x%X)\n", pkt
, pkt
);
731 } else if (ret
== -1) {
732 fprintf(stderr
, "poll failed\n");
737 static void wait_for_debuggee(struct gdb_context
* gdbctx
)
741 gdbctx
->in_trap
= FALSE
;
744 if (!WaitForDebugEvent(&de
, 10))
746 if (GetLastError() == ERROR_SEM_TIMEOUT
)
748 if (check_for_interrupt(gdbctx
)) {
749 if (!DebugBreakProcess(gdbctx
->process
->handle
)) {
750 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
) {
751 fprintf(stderr
, "Failed to break into debugee\n");
755 WaitForDebugEvent(&de
, INFINITE
);
763 handle_debug_event(gdbctx
, &de
);
764 assert(!gdbctx
->process
||
765 gdbctx
->process
->pid
== 0 ||
766 de
.dwProcessId
== gdbctx
->process
->pid
);
767 assert(!dbg_curr_thread
|| de
.dwThreadId
== dbg_curr_thread
->tid
);
768 if (gdbctx
->in_trap
) break;
769 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
773 static void detach_debuggee(struct gdb_context
* gdbctx
, BOOL kill
)
775 be_cpu
->single_step(&gdbctx
->context
, FALSE
);
776 resume_debuggee(gdbctx
, DBG_CONTINUE
);
778 DebugActiveProcessStop(gdbctx
->process
->pid
);
779 dbg_del_process(gdbctx
->process
);
780 gdbctx
->process
= NULL
;
783 static void get_process_info(struct gdb_context
* gdbctx
, char* buffer
, size_t len
)
787 if (!GetExitCodeProcess(gdbctx
->process
->handle
, &status
))
789 strcpy(buffer
, "Unknown process");
792 if (status
== STILL_ACTIVE
)
794 strcpy(buffer
, "Running");
797 snprintf(buffer
, len
, "Terminated (%u)", status
);
799 switch (GetPriorityClass(gdbctx
->process
->handle
))
802 #ifdef ABOVE_NORMAL_PRIORITY_CLASS
803 case ABOVE_NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", above normal priority"); break;
805 #ifdef BELOW_NORMAL_PRIORITY_CLASS
806 case BELOW_NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", below normal priotity"); break;
808 case HIGH_PRIORITY_CLASS
: strcat(buffer
, ", high priority"); break;
809 case IDLE_PRIORITY_CLASS
: strcat(buffer
, ", idle priority"); break;
810 case NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", normal priority"); break;
811 case REALTIME_PRIORITY_CLASS
: strcat(buffer
, ", realtime priority"); break;
813 strcat(buffer
, "\n");
816 static void get_thread_info(struct gdb_context
* gdbctx
, unsigned tid
,
817 char* buffer
, size_t len
)
819 struct dbg_thread
* thd
;
823 /* FIXME: use the size of buffer */
824 thd
= dbg_get_thread(gdbctx
->process
, tid
);
827 strcpy(buffer
, "No information");
830 if (GetExitCodeThread(thd
->handle
, &status
))
832 if (status
== STILL_ACTIVE
)
834 /* FIXME: this is a bit brutal... some nicer way shall be found */
835 switch (status
= SuspendThread(thd
->handle
))
838 case 0: strcpy(buffer
, "Running"); break;
839 default: snprintf(buffer
, len
, "Suspended (%u)", status
- 1);
841 ResumeThread(thd
->handle
);
844 snprintf(buffer
, len
, "Terminated (exit code = %u)", status
);
848 strcpy(buffer
, "Unknown threadID");
850 switch (prio
= GetThreadPriority(thd
->handle
))
852 case THREAD_PRIORITY_ERROR_RETURN
: break;
853 case THREAD_PRIORITY_ABOVE_NORMAL
: strcat(buffer
, ", priority +1 above normal"); break;
854 case THREAD_PRIORITY_BELOW_NORMAL
: strcat(buffer
, ", priority -1 below normal"); break;
855 case THREAD_PRIORITY_HIGHEST
: strcat(buffer
, ", priority +2 above normal"); break;
856 case THREAD_PRIORITY_LOWEST
: strcat(buffer
, ", priority -2 below normal"); break;
857 case THREAD_PRIORITY_IDLE
: strcat(buffer
, ", priority idle"); break;
858 case THREAD_PRIORITY_NORMAL
: strcat(buffer
, ", priority normal"); break;
859 case THREAD_PRIORITY_TIME_CRITICAL
: strcat(buffer
, ", priority time-critical"); break;
860 default: snprintf(buffer
+ strlen(buffer
), len
- strlen(buffer
), ", priority = %d", prio
);
862 assert(strlen(buffer
) < len
);
865 /* =============================================== *
866 * P A C K E T U T I L S *
867 * =============================================== *
870 enum packet_return
{packet_error
= 0x00, packet_ok
= 0x01, packet_done
= 0x02,
871 packet_last_f
= 0x80};
873 static char* packet_realloc(char* buf
, int size
)
876 return HeapAlloc(GetProcessHeap(), 0, size
);
877 return HeapReAlloc(GetProcessHeap(), 0, buf
, size
);
881 static void packet_reply_grow(struct gdb_context
* gdbctx
, size_t size
)
883 if (gdbctx
->out_buf_alloc
< gdbctx
->out_len
+ size
)
885 gdbctx
->out_buf_alloc
= ((gdbctx
->out_len
+ size
) / 32 + 1) * 32;
886 gdbctx
->out_buf
= packet_realloc(gdbctx
->out_buf
, gdbctx
->out_buf_alloc
);
890 static void packet_reply_hex_to(struct gdb_context
* gdbctx
, const void* src
, int len
)
892 packet_reply_grow(gdbctx
, len
* 2);
893 hex_to(&gdbctx
->out_buf
[gdbctx
->out_len
], src
, len
);
894 gdbctx
->out_len
+= len
* 2;
897 static inline void packet_reply_hex_to_str(struct gdb_context
* gdbctx
, const char* src
)
899 packet_reply_hex_to(gdbctx
, src
, strlen(src
));
902 static void packet_reply_val(struct gdb_context
* gdbctx
, unsigned long val
, int len
)
906 shift
= (len
- 1) * 8;
907 packet_reply_grow(gdbctx
, len
* 2);
908 for (i
= 0; i
< len
; i
++, shift
-= 8)
910 gdbctx
->out_buf
[gdbctx
->out_len
++] = hex_to0((val
>> (shift
+ 4)) & 0x0F);
911 gdbctx
->out_buf
[gdbctx
->out_len
++] = hex_to0((val
>> shift
) & 0x0F);
915 static inline void packet_reply_add(struct gdb_context
* gdbctx
, const char* str
, int len
)
917 packet_reply_grow(gdbctx
, len
);
918 memcpy(&gdbctx
->out_buf
[gdbctx
->out_len
], str
, len
);
919 gdbctx
->out_len
+= len
;
922 static inline void packet_reply_cat(struct gdb_context
* gdbctx
, const char* str
)
924 packet_reply_add(gdbctx
, str
, strlen(str
));
927 static inline void packet_reply_catc(struct gdb_context
* gdbctx
, char ch
)
929 packet_reply_add(gdbctx
, &ch
, 1);
932 static void packet_reply_open(struct gdb_context
* gdbctx
)
934 assert(gdbctx
->out_curr_packet
== -1);
935 packet_reply_catc(gdbctx
, '$');
936 gdbctx
->out_curr_packet
= gdbctx
->out_len
;
939 static void packet_reply_close(struct gdb_context
* gdbctx
)
944 plen
= gdbctx
->out_len
- gdbctx
->out_curr_packet
;
945 packet_reply_catc(gdbctx
, '#');
946 cksum
= checksum(&gdbctx
->out_buf
[gdbctx
->out_curr_packet
], plen
);
947 packet_reply_hex_to(gdbctx
, &cksum
, 1);
948 if (gdbctx
->trace
& GDBPXY_TRC_PACKET
)
949 fprintf(stderr
, "Reply : %*.*s\n",
950 plen
, plen
, &gdbctx
->out_buf
[gdbctx
->out_curr_packet
]);
951 gdbctx
->out_curr_packet
= -1;
954 static enum packet_return
packet_reply(struct gdb_context
* gdbctx
, const char* packet
, int len
)
956 packet_reply_open(gdbctx
);
958 if (len
== -1) len
= strlen(packet
);
959 assert(memchr(packet
, '$', len
) == NULL
&& memchr(packet
, '#', len
) == NULL
);
961 packet_reply_add(gdbctx
, packet
, len
);
963 packet_reply_close(gdbctx
);
968 static enum packet_return
packet_reply_error(struct gdb_context
* gdbctx
, int error
)
970 packet_reply_open(gdbctx
);
972 packet_reply_add(gdbctx
, "E", 1);
973 packet_reply_val(gdbctx
, error
, 1);
975 packet_reply_close(gdbctx
);
980 /* =============================================== *
981 * P A C K E T H A N D L E R S *
982 * =============================================== *
985 static enum packet_return
packet_reply_status(struct gdb_context
* gdbctx
)
987 enum packet_return ret
= packet_done
;
989 packet_reply_open(gdbctx
);
991 if (gdbctx
->process
!= NULL
)
996 packet_reply_catc(gdbctx
, 'T');
997 sig
= gdbctx
->last_sig
;
998 packet_reply_val(gdbctx
, sig
, 1);
999 packet_reply_add(gdbctx
, "thread:", 7);
1000 packet_reply_val(gdbctx
, dbg_curr_thread
->tid
, 4);
1001 packet_reply_catc(gdbctx
, ';');
1003 for (i
= 0; i
< cpu_num_regs
; i
++)
1005 /* FIXME: this call will also grow the buffer...
1006 * unneeded, but not harmful
1008 packet_reply_val(gdbctx
, i
, 1);
1009 packet_reply_catc(gdbctx
, ':');
1010 packet_reply_hex_to(gdbctx
, cpu_register(&gdbctx
->context
, i
), 4);
1011 packet_reply_catc(gdbctx
, ';');
1016 /* Try to put an exit code
1017 * Cannot use GetExitCodeProcess, wouldn't fit in a 8 bit value, so
1018 * just indicate the end of process and exit */
1019 packet_reply_add(gdbctx
, "W00", 3);
1020 /*if (!gdbctx->extended)*/ ret
|= packet_last_f
;
1023 packet_reply_close(gdbctx
);
1029 static enum packet_return
packet_extended(struct gdb_context
* gdbctx
)
1031 gdbctx
->extended
= 1;
1036 static enum packet_return
packet_last_signal(struct gdb_context
* gdbctx
)
1038 assert(gdbctx
->in_packet_len
== 0);
1039 return packet_reply_status(gdbctx
);
1042 static enum packet_return
packet_continue(struct gdb_context
* gdbctx
)
1044 /* FIXME: add support for address in packet */
1045 assert(gdbctx
->in_packet_len
== 0);
1046 if (dbg_curr_thread
!= gdbctx
->exec_thread
&& gdbctx
->exec_thread
)
1047 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_FIXME
)
1048 fprintf(stderr
, "NIY: cont on %04x, while last thread is %04x\n",
1049 gdbctx
->exec_thread
->tid
, dbg_curr_thread
->tid
);
1050 resume_debuggee(gdbctx
, DBG_CONTINUE
);
1051 wait_for_debuggee(gdbctx
);
1052 return packet_reply_status(gdbctx
);
1055 static enum packet_return
packet_verbose(struct gdb_context
* gdbctx
)
1058 int defaultAction
= -1; /* magic non action */
1061 int actionIndex
[20]; /* allow for up to 20 actions */
1062 int threadIndex
[20];
1063 int threadCount
= 0;
1064 unsigned int threadIDs
[100]; /* TODO: Should make this dynamic */
1065 unsigned int threadID
= 0;
1066 struct dbg_thread
* thd
;
1069 assert(gdbctx
->in_packet_len
>= 4);
1071 /* OK we have vCont followed by..
1073 * c for packet_continue
1074 * Csig for packet_continue_signal
1076 * Ssig for step signal
1077 * and then an optional thread ID at the end..
1078 * *******************************************/
1080 fprintf(stderr
, "trying to process a verbose packet\n");
1081 /* now check that we've got Cont */
1082 assert(strncmp(gdbctx
->in_packet
, "Cont", 4) == 0);
1085 if (gdbctx
->in_packet
[4] == '?')
1090 The vCont packet is supported. Each action is a supported command in the vCont packet.
1092 The vCont packet is not supported. (this didn't seem to be obeyed!)
1094 packet_reply_open(gdbctx
);
1095 packet_reply_add(gdbctx
, "vCont", 5);
1096 /* add all the supported actions to the reply (all of them for now) */
1097 packet_reply_add(gdbctx
, ";c", 2);
1098 packet_reply_add(gdbctx
, ";C", 2);
1099 packet_reply_add(gdbctx
, ";s", 2);
1100 packet_reply_add(gdbctx
, ";S", 2);
1101 packet_reply_close(gdbctx
);
1105 /* This may not be the 'fastest' code in the world. but it should be nice and easy to debug.
1106 (as it's run when people are debugging break points I'm sure they won't notice the extra 100 cycles anyway)
1107 now if only gdb talked XML.... */
1108 #if 0 /* handy for debugging */
1109 fprintf(stderr
, "no, but can we find a default packet %.*s %d\n", gdbctx
->in_packet_len
, gdbctx
->in_packet
, gdbctx
->in_packet_len
);
1112 /* go through the packet and identify where all the actions start at */
1113 for (i
= 4; i
< gdbctx
->in_packet_len
- 1; i
++)
1115 if (gdbctx
->in_packet
[i
] == ';')
1117 threadIndex
[actions
] = 0;
1118 actionIndex
[actions
++] = i
;
1120 else if (gdbctx
->in_packet
[i
] == ':')
1122 threadIndex
[actions
- 1] = i
;
1126 /* now look up the default action */
1127 for (i
= 0 ; i
< actions
; i
++)
1129 if (threadIndex
[i
] == 0)
1131 if (defaultAction
!= -1)
1133 fprintf(stderr
,"Too many default actions specified\n");
1134 return packet_error
;
1140 /* Now, I have this default action thing that needs to be applied to all non counted threads */
1142 /* go through all the threads and stick their ids in the to be done list. */
1143 LIST_FOR_EACH_ENTRY(thd
, &gdbctx
->process
->threads
, struct dbg_thread
, entry
)
1145 threadIDs
[threadCount
++] = thd
->tid
;
1146 /* check to see if we have more threads than I counted on, and tell the user what to do
1147 * (they're running winedbg, so I'm sure they can fix the problem from the error message!) */
1148 if (threadCount
== 100)
1150 fprintf(stderr
, "Wow, that's a lot of threads, change threadIDs in wine/programms/winedgb/gdbproxy.c to be higher\n");
1155 /* Ok, now we have... actionIndex full of actions and we know what threads there are, so all
1156 * that remains is to apply the actions to the threads and the default action to any threads
1158 if (dbg_curr_thread
!= gdbctx
->exec_thread
&& gdbctx
->exec_thread
)
1159 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_FIXME
)
1160 fprintf(stderr
, "NIY: cont on %04x, while last thread is %04x\n",
1161 gdbctx
->exec_thread
->tid
, dbg_curr_thread
->tid
);
1163 /* deal with the threaded stuff first */
1164 for (i
= 0; i
< actions
; i
++)
1166 if (threadIndex
[i
] != 0)
1168 int j
, idLength
= 0;
1169 if (i
< actions
- 1)
1171 idLength
= (actionIndex
[i
+1] - threadIndex
[i
]) - 1;
1175 idLength
= (gdbctx
->in_packet_len
- threadIndex
[i
]) - 1;
1178 threadID
= hex_to_int(gdbctx
->in_packet
+ threadIndex
[i
] + 1 , idLength
);
1179 /* process the action */
1180 switch (gdbctx
->in_packet
[actionIndex
[i
] + 1])
1182 case 's': /* step */
1183 be_cpu
->single_step(&gdbctx
->context
, TRUE
);
1185 case 'c': /* continue */
1186 resume_debuggee_thread(gdbctx
, DBG_CONTINUE
, threadID
);
1188 case 'S': /* step Sig, */
1189 be_cpu
->single_step(&gdbctx
->context
, TRUE
);
1191 case 'C': /* continue sig */
1192 hex_from(&sig
, gdbctx
->in_packet
+ actionIndex
[i
] + 2, 1);
1193 /* cannot change signals on the fly */
1194 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1195 fprintf(stderr
, "sigs: %u %u\n", sig
, gdbctx
->last_sig
);
1196 if (sig
!= gdbctx
->last_sig
)
1197 return packet_error
;
1198 resume_debuggee_thread(gdbctx
, DBG_EXCEPTION_NOT_HANDLED
, threadID
);
1201 for (j
= 0 ; j
< threadCount
; j
++)
1203 if (threadIDs
[j
] == threadID
)
1210 } /* for i=0 ; i< actions */
1212 /* now we have manage the default action */
1213 if (defaultAction
>= 0)
1215 for (i
= 0 ; i
< threadCount
; i
++)
1217 /* check to see if we've already done something to the thread*/
1218 if (threadIDs
[i
] != 0)
1220 /* if not apply the default action*/
1221 threadID
= threadIDs
[i
];
1222 /* process the action (yes this is almost identical to the one above!) */
1223 switch (gdbctx
->in_packet
[actionIndex
[defaultAction
] + 1])
1225 case 's': /* step */
1226 be_cpu
->single_step(&gdbctx
->context
, TRUE
);
1228 case 'c': /* continue */
1229 resume_debuggee_thread(gdbctx
, DBG_CONTINUE
, threadID
);
1232 be_cpu
->single_step(&gdbctx
->context
, TRUE
);
1234 case 'C': /* continue sig */
1235 hex_from(&sig
, gdbctx
->in_packet
+ actionIndex
[defaultAction
] + 2, 1);
1236 /* cannot change signals on the fly */
1237 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1238 fprintf(stderr
, "sigs: %u %u\n", sig
, gdbctx
->last_sig
);
1239 if (sig
!= gdbctx
->last_sig
)
1240 return packet_error
;
1241 resume_debuggee_thread(gdbctx
, DBG_EXCEPTION_NOT_HANDLED
, threadID
);
1246 } /* if(defaultAction >=0) */
1248 wait_for_debuggee(gdbctx
);
1249 be_cpu
->single_step(&gdbctx
->context
, FALSE
);
1250 return packet_reply_status(gdbctx
);
1253 static enum packet_return
packet_continue_signal(struct gdb_context
* gdbctx
)
1257 /* FIXME: add support for address in packet */
1258 assert(gdbctx
->in_packet_len
== 2);
1259 if (dbg_curr_thread
!= gdbctx
->exec_thread
&& gdbctx
->exec_thread
)
1260 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_FIXME
)
1261 fprintf(stderr
, "NIY: cont/sig on %04x, while last thread is %04x\n",
1262 gdbctx
->exec_thread
->tid
, dbg_curr_thread
->tid
);
1263 hex_from(&sig
, gdbctx
->in_packet
, 1);
1264 /* cannot change signals on the fly */
1265 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1266 fprintf(stderr
, "sigs: %u %u\n", sig
, gdbctx
->last_sig
);
1267 if (sig
!= gdbctx
->last_sig
)
1268 return packet_error
;
1269 resume_debuggee(gdbctx
, DBG_EXCEPTION_NOT_HANDLED
);
1270 wait_for_debuggee(gdbctx
);
1271 return packet_reply_status(gdbctx
);
1274 static enum packet_return
packet_detach(struct gdb_context
* gdbctx
)
1276 detach_debuggee(gdbctx
, FALSE
);
1277 return packet_ok
| packet_last_f
;
1280 static enum packet_return
packet_read_registers(struct gdb_context
* gdbctx
)
1284 CONTEXT
* pctx
= &gdbctx
->context
;
1286 assert(gdbctx
->in_trap
);
1288 if (dbg_curr_thread
!= gdbctx
->other_thread
&& gdbctx
->other_thread
)
1290 if (!fetch_context(gdbctx
, gdbctx
->other_thread
->handle
, pctx
= &ctx
))
1291 return packet_error
;
1294 packet_reply_open(gdbctx
);
1295 for (i
= 0; i
< cpu_num_regs
; i
++)
1297 packet_reply_hex_to(gdbctx
, cpu_register(pctx
, i
), 4);
1299 packet_reply_close(gdbctx
);
1303 static enum packet_return
packet_write_registers(struct gdb_context
* gdbctx
)
1307 CONTEXT
* pctx
= &gdbctx
->context
;
1309 assert(gdbctx
->in_trap
);
1310 if (dbg_curr_thread
!= gdbctx
->other_thread
&& gdbctx
->other_thread
)
1312 if (!fetch_context(gdbctx
, gdbctx
->other_thread
->handle
, pctx
= &ctx
))
1313 return packet_error
;
1315 if (gdbctx
->in_packet_len
< cpu_num_regs
* 2) return packet_error
;
1317 for (i
= 0; i
< cpu_num_regs
; i
++)
1318 hex_from(cpu_register(pctx
, i
), &gdbctx
->in_packet
[8 * i
], 4);
1319 if (pctx
!= &gdbctx
->context
&& !SetThreadContext(gdbctx
->other_thread
->handle
, pctx
))
1321 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
1322 fprintf(stderr
, "Cannot set context on thread %04x\n", gdbctx
->other_thread
->tid
);
1323 return packet_error
;
1328 static enum packet_return
packet_kill(struct gdb_context
* gdbctx
)
1330 detach_debuggee(gdbctx
, TRUE
);
1332 if (!gdbctx
->extended
)
1333 /* dunno whether GDB cares or not */
1337 /* assume we can't really answer something here */
1338 /* return packet_done; */
1341 static enum packet_return
packet_thread(struct gdb_context
* gdbctx
)
1346 switch (gdbctx
->in_packet
[0])
1350 if (gdbctx
->in_packet
[1] == '-')
1351 thread
= -strtol(gdbctx
->in_packet
+ 2, &end
, 16);
1353 thread
= strtol(gdbctx
->in_packet
+ 1, &end
, 16);
1354 if (end
== NULL
|| end
> gdbctx
->in_packet
+ gdbctx
->in_packet_len
)
1356 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1357 fprintf(stderr
, "Cannot get threadid %*.*s\n",
1358 gdbctx
->in_packet_len
- 1, gdbctx
->in_packet_len
- 1,
1359 gdbctx
->in_packet
+ 1);
1360 return packet_error
;
1362 if (gdbctx
->in_packet
[0] == 'c')
1363 gdbctx
->exec_thread
= dbg_get_thread(gdbctx
->process
, thread
);
1365 gdbctx
->other_thread
= dbg_get_thread(gdbctx
->process
, thread
);
1368 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1369 fprintf(stderr
, "Unknown thread sub-command %c\n", gdbctx
->in_packet
[0]);
1370 return packet_error
;
1374 static enum packet_return
packet_read_memory(struct gdb_context
* gdbctx
)
1377 unsigned int len
, blk_len
, nread
;
1381 assert(gdbctx
->in_trap
);
1382 /* FIXME:check in_packet_len for reading %p,%x */
1383 if (sscanf(gdbctx
->in_packet
, "%p,%x", &addr
, &len
) != 2) return packet_error
;
1384 if (len
<= 0) return packet_error
;
1385 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1386 fprintf(stderr
, "Read mem at %p for %u bytes\n", addr
, len
);
1387 for (nread
= 0; nread
< len
; nread
+= r
, addr
+= r
)
1389 blk_len
= min(sizeof(buffer
), len
- nread
);
1390 if (!gdbctx
->process
->process_io
->read(gdbctx
->process
->handle
, addr
, buffer
, blk_len
, &r
) ||
1393 /* fail at first address, return error */
1394 if (nread
== 0) return packet_reply_error(gdbctx
, EFAULT
);
1395 /* something has already been read, return partial information */
1398 if (nread
== 0) packet_reply_open(gdbctx
);
1399 packet_reply_hex_to(gdbctx
, buffer
, r
);
1401 packet_reply_close(gdbctx
);
1405 static enum packet_return
packet_write_memory(struct gdb_context
* gdbctx
)
1408 unsigned int len
, blk_len
;
1413 assert(gdbctx
->in_trap
);
1414 ptr
= memchr(gdbctx
->in_packet
, ':', gdbctx
->in_packet_len
);
1417 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1418 fprintf(stderr
, "Cannot find ':' in %*.*s\n",
1419 gdbctx
->in_packet_len
, gdbctx
->in_packet_len
, gdbctx
->in_packet
);
1420 return packet_error
;
1424 if (sscanf(gdbctx
->in_packet
, "%p,%x", &addr
, &len
) != 2)
1426 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1427 fprintf(stderr
, "Cannot scan addr,len in %s\n", gdbctx
->in_packet
);
1428 return packet_error
;
1430 if (ptr
- gdbctx
->in_packet
+ len
* 2 != gdbctx
->in_packet_len
)
1432 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1433 fprintf(stderr
, "Wrong sizes %u <> %u\n",
1434 (int)(ptr
- gdbctx
->in_packet
) + len
* 2, gdbctx
->in_packet_len
);
1435 return packet_error
;
1437 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1438 fprintf(stderr
, "Write %u bytes at %p\n", len
, addr
);
1441 blk_len
= min(sizeof(buffer
), len
);
1442 hex_from(buffer
, ptr
, blk_len
);
1443 if (!gdbctx
->process
->process_io
->write(gdbctx
->process
->handle
, addr
, buffer
, blk_len
, &w
) ||
1450 return packet_ok
; /* FIXME: error while writing ? */
1453 static enum packet_return
packet_read_register(struct gdb_context
* gdbctx
)
1457 CONTEXT
* pctx
= &gdbctx
->context
;
1459 assert(gdbctx
->in_trap
);
1460 reg
= hex_to_int(gdbctx
->in_packet
, gdbctx
->in_packet_len
);
1461 if (reg
>= cpu_num_regs
)
1463 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1464 fprintf(stderr
, "Register out of bounds %x\n", reg
);
1465 return packet_error
;
1467 if (dbg_curr_thread
!= gdbctx
->other_thread
&& gdbctx
->other_thread
)
1469 if (!fetch_context(gdbctx
, gdbctx
->other_thread
->handle
, pctx
= &ctx
))
1470 return packet_error
;
1472 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1473 fprintf(stderr
, "Read register %x => %lx\n", reg
, *cpu_register(pctx
, reg
));
1474 packet_reply_open(gdbctx
);
1475 packet_reply_hex_to(gdbctx
, cpu_register(pctx
, reg
), 4);
1476 packet_reply_close(gdbctx
);
1480 static enum packet_return
packet_write_register(struct gdb_context
* gdbctx
)
1486 CONTEXT
* pctx
= &gdbctx
->context
;
1488 assert(gdbctx
->in_trap
);
1490 ptr
= memchr(gdbctx
->in_packet
, '=', gdbctx
->in_packet_len
);
1492 reg
= strtoul(gdbctx
->in_packet
, &end
, 16);
1493 if (end
== NULL
|| reg
> cpu_num_regs
)
1495 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1496 fprintf(stderr
, "Invalid register index %s\n", gdbctx
->in_packet
);
1497 /* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
1498 * it wouldn't matter too much, and it fakes our support for all regs
1500 return (end
== NULL
) ? packet_error
: packet_ok
;
1502 if (ptr
+ 8 - gdbctx
->in_packet
!= gdbctx
->in_packet_len
)
1504 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1505 fprintf(stderr
, "Wrong sizes %u <> %u\n",
1506 (int)(ptr
+ 8 - gdbctx
->in_packet
), gdbctx
->in_packet_len
);
1507 return packet_error
;
1509 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1511 int len
= gdbctx
->in_packet_len
- (ptr
- gdbctx
->in_packet
);
1512 fprintf(stderr
, "Writing reg %u <= %*.*s\n", reg
, len
, len
, ptr
);
1515 if (dbg_curr_thread
!= gdbctx
->other_thread
&& gdbctx
->other_thread
)
1517 if (!fetch_context(gdbctx
, gdbctx
->other_thread
->handle
, pctx
= &ctx
))
1518 return packet_error
;
1521 hex_from(cpu_register(pctx
, reg
), ptr
, 4);
1522 if (pctx
!= &gdbctx
->context
&& !SetThreadContext(gdbctx
->other_thread
->handle
, pctx
))
1524 if (gdbctx
->trace
& GDBPXY_TRC_WIN32_ERROR
)
1525 fprintf(stderr
, "Cannot set context for thread %04x\n", gdbctx
->other_thread
->tid
);
1526 return packet_error
;
1532 static void packet_query_monitor_wnd_helper(struct gdb_context
* gdbctx
, HWND hWnd
, int indent
)
1540 if (!GetClassNameA(hWnd
, clsName
, sizeof(clsName
)))
1541 strcpy(clsName
, "-- Unknown --");
1542 if (!GetWindowTextA(hWnd
, wndName
, sizeof(wndName
)))
1543 strcpy(wndName
, "-- Empty --");
1545 packet_reply_open(gdbctx
);
1546 packet_reply_catc(gdbctx
, 'O');
1547 snprintf(buffer
, sizeof(buffer
),
1548 "%*s%04lx%*s%-17.17s %08x %0*lx %.14s\n",
1549 indent
, "", (ULONG_PTR
)hWnd
, 13 - indent
, "",
1550 clsName
, GetWindowLongW(hWnd
, GWL_STYLE
),
1551 ADDRWIDTH
, (ULONG_PTR
)GetWindowLongPtrW(hWnd
, GWLP_WNDPROC
),
1553 packet_reply_hex_to_str(gdbctx
, buffer
);
1554 packet_reply_close(gdbctx
);
1556 if ((child
= GetWindow(hWnd
, GW_CHILD
)) != 0)
1557 packet_query_monitor_wnd_helper(gdbctx
, child
, indent
+ 1);
1558 } while ((hWnd
= GetWindow(hWnd
, GW_HWNDNEXT
)) != 0);
1561 static void packet_query_monitor_wnd(struct gdb_context
* gdbctx
, int len
, const char* str
)
1565 /* we do the output in several 'O' packets, with the last one being just OK for
1566 * marking the end of the output */
1567 packet_reply_open(gdbctx
);
1568 packet_reply_catc(gdbctx
, 'O');
1569 snprintf(buffer
, sizeof(buffer
),
1570 "%-16.16s %-17.17s %-8.8s %s\n",
1571 "hwnd", "Class Name", " Style", " WndProc Text");
1572 packet_reply_hex_to_str(gdbctx
, buffer
);
1573 packet_reply_close(gdbctx
);
1575 /* FIXME: could also add a pmt to this command in str... */
1576 packet_query_monitor_wnd_helper(gdbctx
, GetDesktopWindow(), 0);
1577 packet_reply(gdbctx
, "OK", 2);
1580 static void packet_query_monitor_process(struct gdb_context
* gdbctx
, int len
, const char* str
)
1582 HANDLE snap
= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS
, 0);
1585 PROCESSENTRY32 entry
;
1588 if (snap
== INVALID_HANDLE_VALUE
)
1591 entry
.dwSize
= sizeof(entry
);
1592 ok
= Process32First(snap
, &entry
);
1594 /* we do the output in several 'O' packets, with the last one being just OK for
1595 * marking the end of the output */
1597 packet_reply_open(gdbctx
);
1598 packet_reply_catc(gdbctx
, 'O');
1599 snprintf(buffer
, sizeof(buffer
),
1600 " %-8.8s %-8.8s %-8.8s %s\n",
1601 "pid", "threads", "parent", "executable");
1602 packet_reply_hex_to_str(gdbctx
, buffer
);
1603 packet_reply_close(gdbctx
);
1608 if (entry
.th32ProcessID
== gdbctx
->process
->pid
) deco
= '>';
1609 packet_reply_open(gdbctx
);
1610 packet_reply_catc(gdbctx
, 'O');
1611 snprintf(buffer
, sizeof(buffer
),
1612 "%c%08x %-8d %08x '%s'\n",
1613 deco
, entry
.th32ProcessID
, entry
.cntThreads
,
1614 entry
.th32ParentProcessID
, entry
.szExeFile
);
1615 packet_reply_hex_to_str(gdbctx
, buffer
);
1616 packet_reply_close(gdbctx
);
1617 ok
= Process32Next(snap
, &entry
);
1620 packet_reply(gdbctx
, "OK", 2);
1623 static void packet_query_monitor_mem(struct gdb_context
* gdbctx
, int len
, const char* str
)
1625 MEMORY_BASIC_INFORMATION mbi
;
1632 /* we do the output in several 'O' packets, with the last one being just OK for
1633 * marking the end of the output */
1634 packet_reply_open(gdbctx
);
1635 packet_reply_catc(gdbctx
, 'O');
1636 packet_reply_hex_to_str(gdbctx
, "Address Size State Type RWX\n");
1637 packet_reply_close(gdbctx
);
1639 while (VirtualQueryEx(gdbctx
->process
->handle
, addr
, &mbi
, sizeof(mbi
)) >= sizeof(mbi
))
1643 case MEM_COMMIT
: state
= "commit "; break;
1644 case MEM_FREE
: state
= "free "; break;
1645 case MEM_RESERVE
: state
= "reserve"; break;
1646 default: state
= "??? "; break;
1648 if (mbi
.State
!= MEM_FREE
)
1652 case MEM_IMAGE
: type
= "image "; break;
1653 case MEM_MAPPED
: type
= "mapped "; break;
1654 case MEM_PRIVATE
: type
= "private"; break;
1655 case 0: type
= " "; break;
1656 default: type
= "??? "; break;
1658 memset(prot
, ' ' , sizeof(prot
)-1);
1659 prot
[sizeof(prot
)-1] = '\0';
1660 if (mbi
.AllocationProtect
& (PAGE_READONLY
|PAGE_READWRITE
|PAGE_EXECUTE_READ
|PAGE_EXECUTE_READWRITE
))
1662 if (mbi
.AllocationProtect
& (PAGE_READWRITE
|PAGE_EXECUTE_READWRITE
))
1664 if (mbi
.AllocationProtect
& (PAGE_WRITECOPY
|PAGE_EXECUTE_WRITECOPY
))
1666 if (mbi
.AllocationProtect
& (PAGE_EXECUTE
|PAGE_EXECUTE_READ
|PAGE_EXECUTE_READWRITE
))
1674 packet_reply_open(gdbctx
);
1675 snprintf(buffer
, sizeof(buffer
), "%08lx %08lx %s %s %s\n",
1676 (DWORD_PTR
)addr
, mbi
.RegionSize
, state
, type
, prot
);
1677 packet_reply_catc(gdbctx
, 'O');
1678 packet_reply_hex_to_str(gdbctx
, buffer
);
1679 packet_reply_close(gdbctx
);
1681 if (addr
+ mbi
.RegionSize
< addr
) /* wrap around ? */
1683 addr
+= mbi
.RegionSize
;
1685 packet_reply(gdbctx
, "OK", 2);
1688 static void packet_query_monitor_trace(struct gdb_context
* gdbctx
,
1689 int len
, const char* str
)
1695 snprintf(buffer
, sizeof(buffer
), "trace=%x\n", gdbctx
->trace
);
1697 else if (len
>= 2 && str
[0] == '=')
1699 unsigned val
= atoi(&str
[1]);
1700 snprintf(buffer
, sizeof(buffer
), "trace: %x => %x\n", gdbctx
->trace
, val
);
1701 gdbctx
->trace
= val
;
1705 /* FIXME: ugly but can use error packet here */
1706 packet_reply_cat(gdbctx
, "E00");
1709 packet_reply_open(gdbctx
);
1710 packet_reply_hex_to_str(gdbctx
, buffer
);
1711 packet_reply_close(gdbctx
);
1719 void (*handler
)(struct gdb_context
*, int, const char*);
1722 {0, "wnd", 3, packet_query_monitor_wnd
},
1723 {0, "window", 6, packet_query_monitor_wnd
},
1724 {0, "proc", 4, packet_query_monitor_process
},
1725 {0, "process", 7, packet_query_monitor_process
},
1726 {0, "mem", 3, packet_query_monitor_mem
},
1727 {1, "trace", 5, packet_query_monitor_trace
},
1731 static enum packet_return
packet_query_remote_command(struct gdb_context
* gdbctx
,
1732 const char* hxcmd
, size_t len
)
1735 struct query_detail
* qd
;
1737 assert((len
& 1) == 0 && len
< 2 * sizeof(buffer
));
1739 hex_from(buffer
, hxcmd
, len
);
1741 for (qd
= &query_details
[0]; qd
->name
!= NULL
; qd
++)
1743 if (len
< qd
->len
|| strncmp(buffer
, qd
->name
, qd
->len
) != 0) continue;
1744 if (!qd
->with_arg
&& len
!= qd
->len
) continue;
1746 (qd
->handler
)(gdbctx
, len
- qd
->len
, buffer
+ qd
->len
);
1749 return packet_reply_error(gdbctx
, EINVAL
);
1752 static enum packet_return
packet_query(struct gdb_context
* gdbctx
)
1754 switch (gdbctx
->in_packet
[0])
1757 if (strncmp(gdbctx
->in_packet
+ 1, "ThreadInfo", gdbctx
->in_packet_len
- 1) == 0)
1759 struct dbg_thread
* thd
;
1761 packet_reply_open(gdbctx
);
1762 packet_reply_add(gdbctx
, "m", 1);
1763 LIST_FOR_EACH_ENTRY(thd
, &gdbctx
->process
->threads
, struct dbg_thread
, entry
)
1765 packet_reply_val(gdbctx
, thd
->tid
, 4);
1766 if (list_next(&gdbctx
->process
->threads
, &thd
->entry
) != NULL
)
1767 packet_reply_add(gdbctx
, ",", 1);
1769 packet_reply_close(gdbctx
);
1772 else if (strncmp(gdbctx
->in_packet
+ 1, "ProcessInfo", gdbctx
->in_packet_len
- 1) == 0)
1776 packet_reply_open(gdbctx
);
1777 packet_reply_catc(gdbctx
, 'O');
1778 get_process_info(gdbctx
, result
, sizeof(result
));
1779 packet_reply_hex_to_str(gdbctx
, result
);
1780 packet_reply_close(gdbctx
);
1785 if (strncmp(gdbctx
->in_packet
+ 1, "ThreadInfo", gdbctx
->in_packet_len
- 1) == 0)
1787 packet_reply(gdbctx
, "l", 1);
1790 else if (strncmp(gdbctx
->in_packet
+ 1, "ProcessInfo", gdbctx
->in_packet_len
- 1) == 0)
1792 packet_reply(gdbctx
, "l", 1);
1797 if (gdbctx
->in_packet_len
== 1)
1799 struct dbg_thread
* thd
;
1800 /* FIXME: doc says 16 bit val ??? */
1801 /* grab first created thread, aka last in list */
1802 assert(gdbctx
->process
&& !list_empty(&gdbctx
->process
->threads
));
1803 thd
= LIST_ENTRY(list_tail(&gdbctx
->process
->threads
), struct dbg_thread
, entry
);
1804 packet_reply_open(gdbctx
);
1805 packet_reply_add(gdbctx
, "QC", 2);
1806 packet_reply_val(gdbctx
, thd
->tid
, 4);
1807 packet_reply_close(gdbctx
);
1812 if (strncmp(gdbctx
->in_packet
, "Offsets", gdbctx
->in_packet_len
) == 0)
1816 snprintf(buf
, sizeof(buf
),
1817 "Text=%08lx;Data=%08lx;Bss=%08lx",
1818 gdbctx
->wine_segs
[0], gdbctx
->wine_segs
[1],
1819 gdbctx
->wine_segs
[2]);
1820 return packet_reply(gdbctx
, buf
, -1);
1824 if (gdbctx
->in_packet_len
> 5 && strncmp(gdbctx
->in_packet
, "Rcmd,", 5) == 0)
1826 return packet_query_remote_command(gdbctx
, gdbctx
->in_packet
+ 5,
1827 gdbctx
->in_packet_len
- 5);
1831 if (strncmp(gdbctx
->in_packet
, "Symbol::", gdbctx
->in_packet_len
) == 0)
1833 if (strncmp(gdbctx
->in_packet
, "Supported", gdbctx
->in_packet_len
) == 0)
1835 packet_reply_open(gdbctx
);
1836 packet_reply_close(gdbctx
);
1841 if (gdbctx
->in_packet_len
> 15 &&
1842 strncmp(gdbctx
->in_packet
, "ThreadExtraInfo", 15) == 0 &&
1843 gdbctx
->in_packet
[15] == ',')
1849 tid
= strtol(gdbctx
->in_packet
+ 16, &end
, 16);
1850 if (end
== NULL
) break;
1851 get_thread_info(gdbctx
, tid
, result
, sizeof(result
));
1852 packet_reply_open(gdbctx
);
1853 packet_reply_hex_to_str(gdbctx
, result
);
1854 packet_reply_close(gdbctx
);
1859 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1860 fprintf(stderr
, "Unknown or malformed query %*.*s\n",
1861 gdbctx
->in_packet_len
, gdbctx
->in_packet_len
, gdbctx
->in_packet
);
1862 return packet_error
;
1865 static enum packet_return
packet_step(struct gdb_context
* gdbctx
)
1867 /* FIXME: add support for address in packet */
1868 assert(gdbctx
->in_packet_len
== 0);
1869 if (dbg_curr_thread
!= gdbctx
->exec_thread
&& gdbctx
->exec_thread
)
1870 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_FIXME
)
1871 fprintf(stderr
, "NIY: step on %04x, while last thread is %04x\n",
1872 gdbctx
->exec_thread
->tid
, dbg_curr_thread
->tid
);
1873 be_cpu
->single_step(&gdbctx
->context
, TRUE
);
1874 resume_debuggee(gdbctx
, DBG_CONTINUE
);
1875 wait_for_debuggee(gdbctx
);
1876 be_cpu
->single_step(&gdbctx
->context
, FALSE
);
1877 return packet_reply_status(gdbctx
);
1881 static enum packet_return
packet_step_signal(struct gdb_context
* gdbctx
)
1885 /* FIXME: add support for address in packet */
1886 assert(gdbctx
->in_packet_len
== 2);
1887 if (dbg_curr_thread
->tid
!= gdbctx
->exec_thread
&& gdbctx
->exec_thread
)
1888 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
1889 fprintf(stderr
, "NIY: step/sig on %u, while last thread is %u\n",
1890 gdbctx
->exec_thread
, DEBUG_CurrThread
->tid
);
1891 hex_from(&sig
, gdbctx
->in_packet
, 1);
1892 /* cannot change signals on the fly */
1893 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1894 fprintf(stderr
, "sigs: %u %u\n", sig
, gdbctx
->last_sig
);
1895 if (sig
!= gdbctx
->last_sig
)
1896 return packet_error
;
1897 resume_debuggee(gdbctx
, DBG_EXCEPTION_NOT_HANDLED
);
1898 wait_for_debuggee(gdbctx
);
1899 return packet_reply_status(gdbctx
);
1903 static enum packet_return
packet_thread_alive(struct gdb_context
* gdbctx
)
1908 tid
= strtol(gdbctx
->in_packet
, &end
, 16);
1909 if (tid
== -1 || tid
== 0)
1910 return packet_reply_error(gdbctx
, EINVAL
);
1911 if (dbg_get_thread(gdbctx
->process
, tid
) != NULL
)
1913 return packet_reply_error(gdbctx
, ESRCH
);
1916 static enum packet_return
packet_remove_breakpoint(struct gdb_context
* gdbctx
)
1920 struct gdb_ctx_Xpoint
* xpt
;
1921 enum be_xpoint_type t
;
1923 /* FIXME: check packet_len */
1924 if (gdbctx
->in_packet
[0] < '0' || gdbctx
->in_packet
[0] > '4' ||
1925 gdbctx
->in_packet
[1] != ',' ||
1926 sscanf(gdbctx
->in_packet
+ 2, "%p,%x", &addr
, &len
) != 2)
1927 return packet_error
;
1928 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1929 fprintf(stderr
, "Remove bp %p[%u] typ=%c\n",
1930 addr
, len
, gdbctx
->in_packet
[0]);
1931 switch (gdbctx
->in_packet
[0])
1933 case '0': t
= be_xpoint_break
; len
= 0; break;
1934 case '1': t
= be_xpoint_watch_exec
; break;
1935 case '2': t
= be_xpoint_watch_read
; break;
1936 case '3': t
= be_xpoint_watch_write
; break;
1937 default: return packet_error
;
1939 for (xpt
= &gdbctx
->Xpoints
[NUM_XPOINT
- 1]; xpt
>= gdbctx
->Xpoints
; xpt
--)
1941 if (xpt
->addr
== addr
&& xpt
->type
== t
)
1943 if (be_cpu
->remove_Xpoint(gdbctx
->process
->handle
,
1944 gdbctx
->process
->process_io
, &gdbctx
->context
,
1945 t
, xpt
->addr
, xpt
->val
, len
))
1953 return packet_error
;
1956 static enum packet_return
packet_set_breakpoint(struct gdb_context
* gdbctx
)
1960 struct gdb_ctx_Xpoint
* xpt
;
1961 enum be_xpoint_type t
;
1963 /* FIXME: check packet_len */
1964 if (gdbctx
->in_packet
[0] < '0' || gdbctx
->in_packet
[0] > '4' ||
1965 gdbctx
->in_packet
[1] != ',' ||
1966 sscanf(gdbctx
->in_packet
+ 2, "%p,%x", &addr
, &len
) != 2)
1967 return packet_error
;
1968 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND
)
1969 fprintf(stderr
, "Set bp %p[%u] typ=%c\n",
1970 addr
, len
, gdbctx
->in_packet
[0]);
1971 switch (gdbctx
->in_packet
[0])
1973 case '0': t
= be_xpoint_break
; len
= 0; break;
1974 case '1': t
= be_xpoint_watch_exec
; break;
1975 case '2': t
= be_xpoint_watch_read
; break;
1976 case '3': t
= be_xpoint_watch_write
; break;
1977 default: return packet_error
;
1979 /* because of packet command handling, this should be made idempotent */
1980 for (xpt
= &gdbctx
->Xpoints
[NUM_XPOINT
- 1]; xpt
>= gdbctx
->Xpoints
; xpt
--)
1982 if (xpt
->addr
== addr
&& xpt
->type
== t
)
1983 return packet_ok
; /* nothing to do */
1985 /* really set the Xpoint */
1986 for (xpt
= &gdbctx
->Xpoints
[NUM_XPOINT
- 1]; xpt
>= gdbctx
->Xpoints
; xpt
--)
1988 if (xpt
->type
== -1)
1990 if (be_cpu
->insert_Xpoint(gdbctx
->process
->handle
,
1991 gdbctx
->process
->process_io
, &gdbctx
->context
,
1992 t
, addr
, &xpt
->val
, len
))
1998 fprintf(stderr
, "cannot set xpoint\n");
2002 /* no more entries... eech */
2003 fprintf(stderr
, "Running out of spots for {break|watch}points\n");
2004 return packet_error
;
2007 /* =============================================== *
2008 * P A C K E T I N F R A S T R U C T U R E *
2009 * =============================================== *
2015 enum packet_return (*handler
)(struct gdb_context
* gdbctx
);
2018 static struct packet_entry packet_entries
[] =
2020 /*{'!', packet_extended}, */
2021 {'?', packet_last_signal
},
2022 {'c', packet_continue
},
2023 {'C', packet_continue_signal
},
2024 {'D', packet_detach
},
2025 {'g', packet_read_registers
},
2026 {'G', packet_write_registers
},
2028 {'H', packet_thread
},
2029 {'m', packet_read_memory
},
2030 {'M', packet_write_memory
},
2031 {'p', packet_read_register
},
2032 {'P', packet_write_register
},
2033 {'q', packet_query
},
2034 /* {'Q', packet_set}, */
2035 /* {'R', packet,restart}, only in extended mode ! */
2037 /*{'S', packet_step_signal}, hard(er) to implement */
2038 {'T', packet_thread_alive
},
2039 {'v', packet_verbose
},
2040 {'z', packet_remove_breakpoint
},
2041 {'Z', packet_set_breakpoint
},
2044 static BOOL
extract_packets(struct gdb_context
* gdbctx
)
2048 unsigned char in_cksum
, loc_cksum
;
2050 enum packet_return ret
= packet_error
;
2053 while ((ret
& packet_last_f
) == 0)
2055 if (gdbctx
->in_len
&& (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
))
2056 fprintf(stderr
, "In-buf: %*.*s\n",
2057 gdbctx
->in_len
, gdbctx
->in_len
, gdbctx
->in_buf
);
2058 ptr
= memchr(gdbctx
->in_buf
, '$', gdbctx
->in_len
);
2059 if (ptr
== NULL
) return FALSE
;
2060 if (ptr
!= gdbctx
->in_buf
)
2062 int glen
= ptr
- gdbctx
->in_buf
; /* garbage len */
2063 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2064 fprintf(stderr
, "Removing garbage: %*.*s\n",
2065 glen
, glen
, gdbctx
->in_buf
);
2066 gdbctx
->in_len
-= glen
;
2067 memmove(gdbctx
->in_buf
, ptr
, gdbctx
->in_len
);
2069 end
= memchr(gdbctx
->in_buf
+ 1, '#', gdbctx
->in_len
);
2070 if (end
== NULL
) return FALSE
;
2071 /* no checksum yet */
2072 if (end
+ 3 > gdbctx
->in_buf
+ gdbctx
->in_len
) return FALSE
;
2073 plen
= end
- gdbctx
->in_buf
- 1;
2074 hex_from(&in_cksum
, end
+ 1, 1);
2075 loc_cksum
= checksum(gdbctx
->in_buf
+ 1, plen
);
2076 if (loc_cksum
== in_cksum
)
2078 if (num_packet
== 0) {
2083 write(gdbctx
->sock
, "+", 1);
2086 /* FIXME: should use bsearch if packet_entries was sorted */
2087 for (i
= 0; i
< sizeof(packet_entries
)/sizeof(packet_entries
[0]); i
++)
2089 if (packet_entries
[i
].key
== gdbctx
->in_buf
[1]) break;
2091 if (i
== sizeof(packet_entries
)/sizeof(packet_entries
[0]))
2093 if (gdbctx
->trace
& GDBPXY_TRC_COMMAND_ERROR
)
2094 fprintf(stderr
, "Unknown packet request %*.*s\n",
2095 plen
, plen
, &gdbctx
->in_buf
[1]);
2099 gdbctx
->in_packet
= gdbctx
->in_buf
+ 2;
2100 gdbctx
->in_packet_len
= plen
- 1;
2101 if (gdbctx
->trace
& GDBPXY_TRC_PACKET
)
2102 fprintf(stderr
, "Packet: %c%*.*s\n",
2104 gdbctx
->in_packet_len
, gdbctx
->in_packet_len
,
2106 ret
= (packet_entries
[i
].handler
)(gdbctx
);
2108 switch (ret
& ~packet_last_f
)
2110 case packet_error
: packet_reply(gdbctx
, "", 0); break;
2111 case packet_ok
: packet_reply(gdbctx
, "OK", 2); break;
2112 case packet_done
: break;
2114 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2115 fprintf(stderr
, "Reply-full: %*.*s\n",
2116 gdbctx
->out_len
, gdbctx
->out_len
, gdbctx
->out_buf
);
2117 i
= write(gdbctx
->sock
, gdbctx
->out_buf
, gdbctx
->out_len
);
2118 assert(i
== gdbctx
->out_len
);
2119 /* if this fails, we'll have to use POLLOUT...
2121 gdbctx
->out_len
= 0;
2126 /* FIXME: if we have in our input buffer more than one packet,
2127 * it's very likely that we took too long to answer to a given packet
2128 * and gdb is sending us again the same packet
2129 * We simply drop the second packet. This will lower the risk of error,
2130 * but there's still some race conditions here
2131 * A better fix (yet not perfect) would be to have two threads:
2132 * - one managing the packets for gdb
2133 * - the second one managing the commands...
2134 * This would allow us also the reply with the '+' character (Ack of
2135 * the command) way sooner than what we do now
2137 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2138 fprintf(stderr
, "Dropping packet, I was too slow to respond\n");
2143 write(gdbctx
->sock
, "+", 1);
2144 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2145 fprintf(stderr
, "Dropping packet, invalid checksum %d <> %d\n", in_cksum
, loc_cksum
);
2147 gdbctx
->in_len
-= plen
+ 4;
2148 memmove(gdbctx
->in_buf
, end
+ 3, gdbctx
->in_len
);
2153 static int fetch_data(struct gdb_context
* gdbctx
)
2155 int len
, in_len
= gdbctx
->in_len
;
2157 assert(gdbctx
->in_len
<= gdbctx
->in_buf_alloc
);
2161 if (gdbctx
->in_len
+ STEP
> gdbctx
->in_buf_alloc
)
2162 gdbctx
->in_buf
= packet_realloc(gdbctx
->in_buf
, gdbctx
->in_buf_alloc
+= STEP
);
2164 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2165 fprintf(stderr
, "%d %d %*.*s\n",
2166 gdbctx
->in_len
, gdbctx
->in_buf_alloc
,
2167 gdbctx
->in_len
, gdbctx
->in_len
, gdbctx
->in_buf
);
2168 len
= read(gdbctx
->sock
, gdbctx
->in_buf
+ gdbctx
->in_len
, gdbctx
->in_buf_alloc
- gdbctx
->in_len
);
2169 if (len
<= 0) break;
2170 gdbctx
->in_len
+= len
;
2171 assert(gdbctx
->in_len
<= gdbctx
->in_buf_alloc
);
2172 if (len
< gdbctx
->in_buf_alloc
- gdbctx
->in_len
) break;
2174 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2175 fprintf(stderr
, "=> %d\n", gdbctx
->in_len
- in_len
);
2176 return gdbctx
->in_len
- in_len
;
2179 #define FLAG_NO_START 1
2180 #define FLAG_WITH_XTERM 2
2182 static BOOL
gdb_exec(const char* wine_path
, unsigned port
, unsigned flags
)
2186 const char* gdb_path
;
2189 if (!(gdb_path
= getenv("WINE_GDB"))) gdb_path
= "gdb";
2190 strcpy(buf
,"/tmp/winegdb.XXXXXX");
2191 fd
= mkstemps(buf
, 0);
2192 if (fd
== -1) return FALSE
;
2193 if ((f
= fdopen(fd
, "w+")) == NULL
) return FALSE
;
2194 fprintf(f
, "file %s\n", wine_path
);
2195 fprintf(f
, "target remote localhost:%d\n", ntohs(port
));
2196 fprintf(f
, "monitor trace=%d\n", GDBPXY_TRC_COMMAND_FIXME
);
2197 fprintf(f
, "set prompt Wine-gdb>\\ \n");
2198 /* gdb 5.1 seems to require it, won't hurt anyway */
2199 fprintf(f
, "sharedlibrary\n");
2200 /* This is needed (but not a decent & final fix)
2201 * Without this, gdb would skip our inter-DLL relay code (because
2202 * we don't have any line number information for the relay code)
2203 * With this, we will stop on first instruction of the stub, and
2204 * reusing step, will get us through the relay stub at the actual
2205 * function we're looking at.
2207 fprintf(f
, "set step-mode on\n");
2208 /* tell gdb to delete this file when done handling it... */
2209 fprintf(f
, "shell rm -f \"%s\"\n", buf
);
2211 if (flags
& FLAG_WITH_XTERM
)
2212 execlp("xterm", "xterm", "-e", gdb_path
, "-x", buf
, NULL
);
2214 execlp(gdb_path
, gdb_path
, "-x", buf
, NULL
);
2215 assert(0); /* never reached */
2219 static BOOL
gdb_startup(struct gdb_context
* gdbctx
, DEBUG_EVENT
* de
, unsigned flags
)
2222 struct sockaddr_in s_addrs
;
2223 unsigned int s_len
= sizeof(s_addrs
);
2224 struct pollfd pollfd
;
2225 IMAGEHLP_MODULE64 imh_mod
;
2227 /* step 1: create socket for gdb connection request */
2228 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
2230 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2231 fprintf(stderr
, "Can't create socket");
2235 if (listen(sock
, 1) == -1 ||
2236 getsockname(sock
, (struct sockaddr
*)&s_addrs
, &s_len
) == -1)
2239 /* step 2: do the process internal creation */
2240 handle_debug_event(gdbctx
, de
);
2242 /* step3: get the wine loader name */
2243 if (!dbg_get_debuggee_info(gdbctx
->process
->handle
, &imh_mod
)) return FALSE
;
2245 /* step 4: fire up gdb (if requested) */
2246 if (flags
& FLAG_NO_START
)
2247 fprintf(stderr
, "target remote localhost:%d\n", ntohs(s_addrs
.sin_port
));
2251 case -1: /* error in parent... */
2252 fprintf(stderr
, "Cannot create gdb\n");
2254 default: /* in parent... success */
2256 case 0: /* in child... and alive */
2257 gdb_exec(imh_mod
.LoadedImageName
, s_addrs
.sin_port
, flags
);
2258 /* if we're here, exec failed, so report failure */
2262 /* step 5: wait for gdb to connect actually */
2264 pollfd
.events
= POLLIN
;
2267 switch (poll(&pollfd
, 1, -1))
2270 if (pollfd
.revents
& POLLIN
)
2273 gdbctx
->sock
= accept(sock
, (struct sockaddr
*)&s_addrs
, &s_len
);
2274 if (gdbctx
->sock
== -1)
2276 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2277 fprintf(stderr
, "Connected on %d\n", gdbctx
->sock
);
2278 /* don't keep our small packets too long: send them ASAP back to GDB
2279 * without this, GDB really crawls
2281 setsockopt(gdbctx
->sock
, IPPROTO_TCP
, TCP_NODELAY
, (char*)&dummy
, sizeof(dummy
));
2285 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2286 fprintf(stderr
, "Poll for cnx failed (timeout)\n");
2289 if (gdbctx
->trace
& GDBPXY_TRC_LOWLEVEL
)
2290 fprintf(stderr
, "Poll for cnx failed (error)\n");
2300 static BOOL
gdb_init_context(struct gdb_context
* gdbctx
, unsigned flags
)
2306 gdbctx
->in_buf
= NULL
;
2307 gdbctx
->in_buf_alloc
= 0;
2309 gdbctx
->out_buf
= NULL
;
2310 gdbctx
->out_buf_alloc
= 0;
2311 gdbctx
->out_len
= 0;
2312 gdbctx
->out_curr_packet
= -1;
2314 gdbctx
->exec_thread
= gdbctx
->other_thread
= NULL
;
2315 gdbctx
->last_sig
= 0;
2316 gdbctx
->in_trap
= FALSE
;
2317 gdbctx
->trace
= /*GDBPXY_TRC_PACKET | GDBPXY_TRC_COMMAND |*/ GDBPXY_TRC_COMMAND_ERROR
| GDBPXY_TRC_COMMAND_FIXME
| GDBPXY_TRC_WIN32_EVENT
;
2318 gdbctx
->process
= NULL
;
2319 for (i
= 0; i
< NUM_XPOINT
; i
++)
2320 gdbctx
->Xpoints
[i
].type
= -1;
2321 for (i
= 0; i
< sizeof(gdbctx
->wine_segs
) / sizeof(gdbctx
->wine_segs
[0]); i
++)
2322 gdbctx
->wine_segs
[i
] = 0;
2324 /* wait for first trap */
2325 while (WaitForDebugEvent(&de
, INFINITE
))
2327 if (de
.dwDebugEventCode
== CREATE_PROCESS_DEBUG_EVENT
)
2329 /* this should be the first event we get,
2330 * and the only one of this type */
2331 assert(gdbctx
->process
== NULL
&& de
.dwProcessId
== dbg_curr_pid
);
2332 /* gdbctx->dwProcessId = pid; */
2333 if (!gdb_startup(gdbctx
, &de
, flags
)) return FALSE
;
2334 assert(!gdbctx
->in_trap
);
2338 handle_debug_event(gdbctx
, &de
);
2339 if (gdbctx
->in_trap
) break;
2341 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
2346 static int gdb_remote(unsigned flags
)
2348 struct pollfd pollfd
;
2349 struct gdb_context gdbctx
;
2352 for (doLoop
= gdb_init_context(&gdbctx
, flags
); doLoop
;)
2354 pollfd
.fd
= gdbctx
.sock
;
2355 pollfd
.events
= POLLIN
;
2358 switch (poll(&pollfd
, 1, -1))
2362 if (pollfd
.revents
& (POLLHUP
| POLLERR
))
2364 if (gdbctx
.trace
& GDBPXY_TRC_LOWLEVEL
)
2365 fprintf(stderr
, "Gdb hung up\n");
2366 /* kill also debuggee process - questionnable - */
2367 detach_debuggee(&gdbctx
, TRUE
);
2371 if ((pollfd
.revents
& POLLIN
) && fetch_data(&gdbctx
) > 0)
2373 if (extract_packets(&gdbctx
)) doLoop
= FALSE
;
2377 /* timeout, should never happen (infinite timeout) */
2380 if (gdbctx
.trace
& GDBPXY_TRC_LOWLEVEL
)
2381 fprintf(stderr
, "Poll failed\n");
2391 int gdb_main(int argc
, char* argv
[])
2394 unsigned gdb_flags
= 0;
2397 while (argc
> 0 && argv
[0][0] == '-')
2399 if (strcmp(argv
[0], "--no-start") == 0)
2401 gdb_flags
|= FLAG_NO_START
;
2405 if (strcmp(argv
[0], "--with-xterm") == 0)
2407 gdb_flags
|= FLAG_WITH_XTERM
;
2413 if (dbg_active_attach(argc
, argv
) == start_ok
||
2414 dbg_active_launch(argc
, argv
) == start_ok
)
2415 return gdb_remote(gdb_flags
);
2417 fprintf(stderr
, "GdbProxy mode not supported on this platform\n");