2 * A Win32 based proxy implementing the GBD remote protocol.
3 * This makes it possible to debug Wine (and any "emulated"
4 * program) under Linux using GDB.
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
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
51 enum be_xpoint_type type
;
64 #define QX_NAME_SIZE 32
65 #define QX_ANNEX_SIZE MAX_PATH
75 /* split into individual packet */
79 struct reply_buffer out_buf
;
81 /* generic GDB thread information */
82 int exec_tid
; /* tid used in step & continue */
83 int other_tid
; /* tid to be used in any other operation */
84 struct list xpoint_list
;
85 /* current Win32 trap env */
88 /* Win32 information */
89 struct dbg_process
* process
;
90 /* Unix environment */
91 ULONG_PTR wine_segs
[3]; /* load addresses of the ELF wine exec segments (text, bss and data) */
94 char qxfer_object_annex
[QX_ANNEX_SIZE
];
95 struct reply_buffer qxfer_buffer
;
98 /* assume standard signal and errno values */
122 static void gdbctx_delete_xpoint(struct gdb_context
*gdbctx
, struct dbg_thread
*thread
,
123 dbg_ctx_t
*ctx
, struct gdb_xpoint
*x
)
125 struct dbg_process
*process
= gdbctx
->process
;
126 struct backend_cpu
*cpu
= process
->be_cpu
;
128 if (!cpu
->remove_Xpoint(process
->handle
, process
->process_io
, ctx
, x
->type
, x
->addr
, x
->value
, x
->size
))
129 ERR("%04lx:%04lx: Couldn't remove breakpoint at:%p/%x type:%d\n", process
->pid
, thread
? thread
->tid
: ~0, x
->addr
, x
->size
, x
->type
);
131 list_remove(&x
->entry
);
135 static void gdbctx_insert_xpoint(struct gdb_context
*gdbctx
, struct dbg_thread
*thread
,
136 dbg_ctx_t
*ctx
, enum be_xpoint_type type
, void *addr
, int size
)
138 struct dbg_process
*process
= thread
->process
;
139 struct backend_cpu
*cpu
= process
->be_cpu
;
140 struct gdb_xpoint
*x
;
143 if (!cpu
->insert_Xpoint(process
->handle
, process
->process_io
, ctx
, type
, addr
, &value
, size
))
145 ERR("%04lx:%04lx: Couldn't insert breakpoint at:%p/%x type:%d\n", process
->pid
, thread
->tid
, addr
, size
, type
);
149 if (!(x
= malloc(sizeof(struct gdb_xpoint
))))
151 ERR("%04lx:%04lx: Couldn't allocate memory for breakpoint at:%p/%x type:%d\n", process
->pid
, thread
->tid
, addr
, size
, type
);
155 x
->pid
= process
->pid
;
156 x
->tid
= thread
->tid
;
161 list_add_head(&gdbctx
->xpoint_list
, &x
->entry
);
164 static struct gdb_xpoint
*gdb_find_xpoint(struct gdb_context
*gdbctx
, struct dbg_thread
*thread
,
165 enum be_xpoint_type type
, void *addr
, int size
)
167 struct gdb_xpoint
*x
;
169 LIST_FOR_EACH_ENTRY(x
, &gdbctx
->xpoint_list
, struct gdb_xpoint
, entry
)
171 if (thread
&& (x
->pid
!= thread
->process
->pid
|| x
->tid
!= thread
->tid
))
173 if (x
->type
== type
&& x
->addr
== addr
&& x
->size
== size
)
180 static BOOL
tgt_process_gdbproxy_read(HANDLE hProcess
, const void* addr
,
181 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
183 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
186 static BOOL
tgt_process_gdbproxy_write(HANDLE hProcess
, void* addr
,
187 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
189 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
192 static struct be_process_io be_process_gdbproxy_io
=
194 NULL
, /* we shouldn't use close_process() in gdbproxy */
195 tgt_process_gdbproxy_read
,
196 tgt_process_gdbproxy_write
199 /* =============================================== *
200 * B A S I C M A N I P U L A T I O N S *
201 * =============================================== *
204 static inline int hex_from0(char ch
)
206 if (ch
>= '0' && ch
<= '9') return ch
- '0';
207 if (ch
>= 'A' && ch
<= 'F') return ch
- 'A' + 10;
208 if (ch
>= 'a' && ch
<= 'f') return ch
- 'a' + 10;
214 static inline unsigned char hex_to0(int x
)
216 assert(x
>= 0 && x
< 16);
217 return "0123456789abcdef"[x
];
220 static void hex_from(void* dst
, const char* src
, size_t len
)
222 unsigned char *p
= dst
;
225 *p
++ = (hex_from0(src
[0]) << 4) | hex_from0(src
[1]);
230 static void hex_to(char* dst
, const void* src
, size_t len
)
232 const unsigned char *p
= src
;
235 *dst
++ = hex_to0(*p
>> 4);
236 *dst
++ = hex_to0(*p
& 0x0F);
241 static void reply_buffer_clear(struct reply_buffer
* reply
)
246 static void reply_buffer_grow(struct reply_buffer
* reply
, size_t size
)
248 size_t required_alloc
= reply
->len
+ size
;
250 if (reply
->alloc
< required_alloc
)
252 reply
->alloc
= reply
->alloc
* 3 / 2;
253 if (reply
->alloc
< required_alloc
)
254 reply
->alloc
= required_alloc
;
256 reply
->base
= realloc(reply
->base
, reply
->alloc
);
260 static void reply_buffer_append(struct reply_buffer
* reply
, const void* data
, size_t size
)
262 reply_buffer_grow(reply
, size
);
263 memcpy(reply
->base
+ reply
->len
, data
, size
);
267 static inline void reply_buffer_append_str(struct reply_buffer
* reply
, const char* str
)
269 reply_buffer_append(reply
, str
, strlen(str
));
272 static inline void reply_buffer_append_wstr(struct reply_buffer
* reply
, const WCHAR
* wstr
)
277 len
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
279 if (str
&& WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, str
, len
, NULL
, NULL
))
280 reply_buffer_append_str(reply
, str
);
284 static inline void reply_buffer_append_hex(struct reply_buffer
* reply
, const void* src
, size_t len
)
286 reply_buffer_grow(reply
, len
* 2);
287 hex_to((char *)reply
->base
+ reply
->len
, src
, len
);
288 reply
->len
+= len
* 2;
291 static inline void reply_buffer_append_uinthex(struct reply_buffer
* reply
, ULONG_PTR val
, int len
)
293 char buf
[sizeof(ULONG_PTR
) * 2], *ptr
;
295 assert(len
<= sizeof(ULONG_PTR
));
300 *--ptr
= hex_to0(val
& 0x0F);
304 reply_buffer_append(reply
, ptr
, len
* 2);
307 static const unsigned char xml_special_chars_lookup_table
[16] = {
308 /* The characters should be sorted by its value modulo table length. */
312 0x22, /* ": 0010|0010 */
314 0x26, /* &: 0010|0110 */
315 0x27, /* ': 0010|0111 */
317 0x3C, /* <: 0011|1100 */
319 0x3E, /* >: 0011|1110 */
323 static inline BOOL
is_nul_or_xml_special_char(unsigned char val
)
325 const size_t length
= ARRAY_SIZE(xml_special_chars_lookup_table
);
326 return xml_special_chars_lookup_table
[val
% length
] == val
;
329 static void reply_buffer_append_xmlstr(struct reply_buffer
* reply
, const char* str
)
331 const char *ptr
= str
, *curr
;
337 while (!is_nul_or_xml_special_char((unsigned char)*ptr
))
340 reply_buffer_append(reply
, curr
, ptr
- curr
);
344 case '"': reply_buffer_append_str(reply
, """); break;
345 case '&': reply_buffer_append_str(reply
, "&"); break;
346 case '\'': reply_buffer_append_str(reply
, "'"); break;
347 case '<': reply_buffer_append_str(reply
, "<"); break;
348 case '>': reply_buffer_append_str(reply
, ">"); break;
356 static unsigned char checksum(const void* data
, int len
)
359 const unsigned char* ptr
= data
;
366 static inline void* cpu_register_ptr(struct gdb_context
*gdbctx
,
367 dbg_ctx_t
*ctx
, unsigned idx
)
369 assert(idx
< gdbctx
->process
->be_cpu
->gdb_num_regs
);
370 return (char*)ctx
+ gdbctx
->process
->be_cpu
->gdb_register_map
[idx
].offset
;
373 static inline DWORD64
cpu_register(struct gdb_context
*gdbctx
,
374 dbg_ctx_t
*ctx
, unsigned idx
)
376 switch (gdbctx
->process
->be_cpu
->gdb_register_map
[idx
].length
)
378 case 1: return *(BYTE
*)cpu_register_ptr(gdbctx
, ctx
, idx
);
379 case 2: return *(WORD
*)cpu_register_ptr(gdbctx
, ctx
, idx
);
380 case 4: return *(DWORD
*)cpu_register_ptr(gdbctx
, ctx
, idx
);
381 case 8: return *(DWORD64
*)cpu_register_ptr(gdbctx
, ctx
, idx
);
383 ERR("got unexpected size: %u\n",
384 (unsigned)gdbctx
->process
->be_cpu
->gdb_register_map
[idx
].length
);
390 static inline void cpu_register_hex_from(struct gdb_context
*gdbctx
,
391 dbg_ctx_t
* ctx
, unsigned idx
, const char **phex
)
393 const struct gdb_register
*cpu_register_map
= gdbctx
->process
->be_cpu
->gdb_register_map
;
394 hex_from(cpu_register_ptr(gdbctx
, ctx
, idx
), *phex
, cpu_register_map
[idx
].length
);
397 /* =============================================== *
398 * W I N 3 2 D E B U G I N T E R F A C E *
399 * =============================================== *
402 static struct dbg_thread
* dbg_thread_from_tid(struct gdb_context
* gdbctx
, int tid
)
404 struct dbg_process
*process
= gdbctx
->process
;
405 struct dbg_thread
*thread
;
407 if (!process
) return NULL
;
409 if (tid
== 0) tid
= gdbctx
->de
.dwThreadId
;
410 LIST_FOR_EACH_ENTRY(thread
, &process
->threads
, struct dbg_thread
, entry
)
412 if (tid
> 0 && thread
->tid
!= tid
) continue;
419 static void dbg_thread_set_single_step(struct dbg_thread
*thread
, BOOL enable
)
421 struct backend_cpu
*backend
;
425 if (!thread
->process
) return;
426 if (!(backend
= thread
->process
->be_cpu
)) return;
428 if (!backend
->get_context(thread
->handle
, &ctx
))
430 ERR("get_context failed for thread %04lx:%04lx\n", thread
->process
->pid
, thread
->tid
);
433 backend
->single_step(&ctx
, enable
);
434 if (!backend
->set_context(thread
->handle
, &ctx
))
435 ERR("set_context failed for thread %04lx:%04lx\n", thread
->process
->pid
, thread
->tid
);
438 static unsigned char signal_from_debug_event(DEBUG_EVENT
* de
)
442 if (de
->dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
444 if (de
->dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
447 ec
= de
->u
.Exception
.ExceptionRecord
.ExceptionCode
;
450 case EXCEPTION_ACCESS_VIOLATION
:
451 case EXCEPTION_PRIV_INSTRUCTION
:
452 case EXCEPTION_STACK_OVERFLOW
:
453 case EXCEPTION_GUARD_PAGE
:
455 case EXCEPTION_DATATYPE_MISALIGNMENT
:
457 case EXCEPTION_SINGLE_STEP
:
458 case EXCEPTION_BREAKPOINT
:
460 case EXCEPTION_FLT_DENORMAL_OPERAND
:
461 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
462 case EXCEPTION_FLT_INEXACT_RESULT
:
463 case EXCEPTION_FLT_INVALID_OPERATION
:
464 case EXCEPTION_FLT_OVERFLOW
:
465 case EXCEPTION_FLT_STACK_CHECK
:
466 case EXCEPTION_FLT_UNDERFLOW
:
468 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
469 case EXCEPTION_INT_OVERFLOW
:
471 case EXCEPTION_ILLEGAL_INSTRUCTION
:
475 case STATUS_POSSIBLE_DEADLOCK
:
477 /* should not be here */
478 case EXCEPTION_INVALID_HANDLE
:
479 case EXCEPTION_WINE_NAME_THREAD
:
482 ERR("Unknown exception code 0x%08lx\n", ec
);
487 static BOOL
handle_exception(struct gdb_context
* gdbctx
, EXCEPTION_DEBUG_INFO
* exc
)
489 EXCEPTION_RECORD
* rec
= &exc
->ExceptionRecord
;
491 switch (rec
->ExceptionCode
)
493 case EXCEPTION_WINE_NAME_THREAD
:
495 const THREADNAME_INFO
*threadname
= (const THREADNAME_INFO
*)rec
->ExceptionInformation
;
496 struct dbg_thread
*thread
;
500 if (threadname
->dwType
!= 0x1000)
502 if (threadname
->dwThreadID
== -1)
503 thread
= dbg_get_thread(gdbctx
->process
, gdbctx
->de
.dwThreadId
);
505 thread
= dbg_get_thread(gdbctx
->process
, threadname
->dwThreadID
);
508 if (gdbctx
->process
->process_io
->read( gdbctx
->process
->handle
,
509 threadname
->szName
, name
, sizeof(name
), &read
) && read
== sizeof(name
))
511 fprintf(stderr
, "Thread ID=%04lx renamed to \"%.9s\"\n",
512 threadname
->dwThreadID
, name
);
516 ERR("Cannot set name of thread %04lx\n", threadname
->dwThreadID
);
519 case EXCEPTION_INVALID_HANDLE
:
526 static BOOL
handle_debug_event(struct gdb_context
* gdbctx
, BOOL stop_on_dll_load_unload
)
528 DEBUG_EVENT
*de
= &gdbctx
->de
;
529 struct dbg_thread
*thread
;
537 gdbctx
->exec_tid
= de
->dwThreadId
;
538 gdbctx
->other_tid
= de
->dwThreadId
;
539 gdbctx
->de_reply
= DBG_REPLY_LATER
;
541 switch (de
->dwDebugEventCode
)
543 case CREATE_PROCESS_DEBUG_EVENT
:
544 gdbctx
->process
= dbg_add_process(&be_process_gdbproxy_io
, de
->dwProcessId
,
545 de
->u
.CreateProcessInfo
.hProcess
);
546 if (!gdbctx
->process
)
549 size
= ARRAY_SIZE(u
.buffer
);
550 QueryFullProcessImageNameW( gdbctx
->process
->handle
, 0, u
.buffer
, &size
);
551 dbg_set_process_name(gdbctx
->process
, u
.buffer
);
553 fprintf(stderr
, "%04lx:%04lx: create process '%ls'/%p @%p (%lu<%lu>)\n",
554 de
->dwProcessId
, de
->dwThreadId
,
556 de
->u
.CreateProcessInfo
.lpImageName
,
557 de
->u
.CreateProcessInfo
.lpStartAddress
,
558 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
559 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
561 /* de->u.CreateProcessInfo.lpStartAddress; */
562 if (!dbg_init(gdbctx
->process
->handle
, u
.buffer
, TRUE
))
563 ERR("Couldn't initiate DbgHelp\n");
565 fprintf(stderr
, "%04lx:%04lx: create thread I @%p\n", de
->dwProcessId
,
566 de
->dwThreadId
, de
->u
.CreateProcessInfo
.lpStartAddress
);
568 dbg_load_module(gdbctx
->process
->handle
, de
->u
.CreateProcessInfo
.hFile
, u
.buffer
,
569 (DWORD_PTR
)de
->u
.CreateProcessInfo
.lpBaseOfImage
, 0);
571 dbg_add_thread(gdbctx
->process
, de
->dwThreadId
,
572 de
->u
.CreateProcessInfo
.hThread
,
573 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
576 case LOAD_DLL_DEBUG_EVENT
:
577 fetch_module_name( de
->u
.LoadDll
.lpImageName
, de
->u
.LoadDll
.lpBaseOfDll
,
578 u
.buffer
, ARRAY_SIZE(u
.buffer
) );
579 fprintf(stderr
, "%04lx:%04lx: loads DLL %ls @%p (%lu<%lu>)\n",
580 de
->dwProcessId
, de
->dwThreadId
,
582 de
->u
.LoadDll
.lpBaseOfDll
,
583 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
584 de
->u
.LoadDll
.nDebugInfoSize
);
585 dbg_load_module(gdbctx
->process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
586 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
587 if (stop_on_dll_load_unload
)
591 case UNLOAD_DLL_DEBUG_EVENT
:
592 fprintf(stderr
, "%08lx:%08lx: 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
);
596 if (stop_on_dll_load_unload
)
600 case EXCEPTION_DEBUG_EVENT
:
601 TRACE("%08lx:%08lx: exception code=0x%08lx\n", de
->dwProcessId
,
602 de
->dwThreadId
, de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
604 if (handle_exception(gdbctx
, &de
->u
.Exception
))
608 case CREATE_THREAD_DEBUG_EVENT
:
609 fprintf(stderr
, "%08lx:%08lx: create thread D @%p\n", de
->dwProcessId
,
610 de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
612 dbg_add_thread(gdbctx
->process
,
614 de
->u
.CreateThread
.hThread
,
615 de
->u
.CreateThread
.lpThreadLocalBase
);
618 case EXIT_THREAD_DEBUG_EVENT
:
619 fprintf(stderr
, "%08lx:%08lx: exit thread (%lu)\n",
620 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
621 if ((thread
= dbg_get_thread(gdbctx
->process
, de
->dwThreadId
)))
622 dbg_del_thread(thread
);
625 case EXIT_PROCESS_DEBUG_EVENT
:
626 fprintf(stderr
, "%08lx:%08lx: exit process (%lu)\n",
627 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
629 dbg_del_process(gdbctx
->process
);
630 gdbctx
->process
= NULL
;
633 case OUTPUT_DEBUG_STRING_EVENT
:
634 memory_get_string(gdbctx
->process
,
635 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
636 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
637 fprintf(stderr
, "%08lx:%08lx: output debug string (%s)\n",
638 de
->dwProcessId
, de
->dwThreadId
, debugstr_a(u
.bufferA
));
642 fprintf(stderr
, "%08lx:%08lx: rip error=%lu type=%lu\n", de
->dwProcessId
,
643 de
->dwThreadId
, de
->u
.RipInfo
.dwError
, de
->u
.RipInfo
.dwType
);
647 FIXME("%08lx:%08lx: unknown event (%lu)\n",
648 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
651 LIST_FOR_EACH_ENTRY(thread
, &gdbctx
->process
->threads
, struct dbg_thread
, entry
)
653 if (!thread
->suspended
) SuspendThread(thread
->handle
);
654 thread
->suspended
= TRUE
;
660 static void handle_step_or_continue(struct gdb_context
* gdbctx
, int tid
, BOOL step
, int sig
)
662 struct dbg_process
*process
= gdbctx
->process
;
663 struct dbg_thread
*thread
;
665 if (tid
== 0) tid
= gdbctx
->de
.dwThreadId
;
666 LIST_FOR_EACH_ENTRY(thread
, &process
->threads
, struct dbg_thread
, entry
)
668 if (tid
!= -1 && thread
->tid
!= tid
) continue;
669 if (!thread
->suspended
) continue;
670 thread
->suspended
= FALSE
;
672 if (process
->pid
== gdbctx
->de
.dwProcessId
&& thread
->tid
== gdbctx
->de
.dwThreadId
)
673 gdbctx
->de_reply
= (sig
== -1 ? DBG_CONTINUE
: DBG_EXCEPTION_NOT_HANDLED
);
675 dbg_thread_set_single_step(thread
, step
);
676 ResumeThread(thread
->handle
);
680 static BOOL
check_for_interrupt(struct gdb_context
* gdbctx
)
684 struct timeval tv
= { 0, 0 };
686 FD_ZERO( &read_fds
);
687 FD_SET( gdbctx
->sock
, &read_fds
);
689 if (select( 0, &read_fds
, NULL
, NULL
, &tv
) > 0)
691 if (recv(gdbctx
->sock
, &pkt
, 1, 0) != 1) {
692 ERR("read failed\n");
696 ERR("Unexpected break packet %#02x\n", pkt
);
704 static void wait_for_debuggee(struct gdb_context
* gdbctx
)
706 if (gdbctx
->de
.dwDebugEventCode
)
707 ContinueDebugEvent(gdbctx
->de
.dwProcessId
, gdbctx
->de
.dwThreadId
, gdbctx
->de_reply
);
711 if (!WaitForDebugEvent(&gdbctx
->de
, 10))
713 if (GetLastError() == ERROR_SEM_TIMEOUT
)
715 if (check_for_interrupt(gdbctx
)) {
716 if (!DebugBreakProcess(gdbctx
->process
->handle
)) {
717 ERR("Failed to break into debuggee\n");
720 WaitForDebugEvent(&gdbctx
->de
, INFINITE
);
728 if (!handle_debug_event(gdbctx
, TRUE
))
730 ContinueDebugEvent(gdbctx
->de
.dwProcessId
, gdbctx
->de
.dwThreadId
, DBG_CONTINUE
);
734 static void detach_debuggee(struct gdb_context
* gdbctx
, BOOL kill
)
736 handle_step_or_continue(gdbctx
, -1, FALSE
, -1);
738 if (gdbctx
->de
.dwDebugEventCode
)
739 ContinueDebugEvent(gdbctx
->de
.dwProcessId
, gdbctx
->de
.dwThreadId
, DBG_CONTINUE
);
742 DebugActiveProcessStop(gdbctx
->process
->pid
);
743 dbg_del_process(gdbctx
->process
);
744 gdbctx
->process
= NULL
;
747 static void get_process_info(struct gdb_context
* gdbctx
, char* buffer
, size_t len
)
751 if (!GetExitCodeProcess(gdbctx
->process
->handle
, &status
))
753 strcpy(buffer
, "Unknown process");
756 if (status
== STILL_ACTIVE
)
758 strcpy(buffer
, "Running");
761 snprintf(buffer
, len
, "Terminated (%lu)", status
);
763 switch (GetPriorityClass(gdbctx
->process
->handle
))
766 case ABOVE_NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", above normal priority"); break;
767 case BELOW_NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", below normal priority"); break;
768 case HIGH_PRIORITY_CLASS
: strcat(buffer
, ", high priority"); break;
769 case IDLE_PRIORITY_CLASS
: strcat(buffer
, ", idle priority"); break;
770 case NORMAL_PRIORITY_CLASS
: strcat(buffer
, ", normal priority"); break;
771 case REALTIME_PRIORITY_CLASS
: strcat(buffer
, ", realtime priority"); break;
773 strcat(buffer
, "\n");
776 static void get_thread_info(struct gdb_context
* gdbctx
, unsigned tid
,
777 char* buffer
, size_t len
)
779 struct dbg_thread
* thd
;
783 /* FIXME: use the size of buffer */
784 thd
= dbg_get_thread(gdbctx
->process
, tid
);
787 strcpy(buffer
, "No information");
790 if (GetExitCodeThread(thd
->handle
, &status
))
792 if (status
== STILL_ACTIVE
)
794 /* FIXME: this is a bit brutal... some nicer way shall be found */
795 switch (status
= SuspendThread(thd
->handle
))
798 case 0: strcpy(buffer
, "Running"); break;
799 default: snprintf(buffer
, len
, "Suspended (%lu)", status
- 1);
801 ResumeThread(thd
->handle
);
804 snprintf(buffer
, len
, "Terminated (exit code = %lu)", status
);
808 strcpy(buffer
, "Unknown threadID");
810 switch (prio
= GetThreadPriority(thd
->handle
))
812 case THREAD_PRIORITY_ERROR_RETURN
: break;
813 case THREAD_PRIORITY_ABOVE_NORMAL
: strcat(buffer
, ", priority +1 above normal"); break;
814 case THREAD_PRIORITY_BELOW_NORMAL
: strcat(buffer
, ", priority -1 below normal"); break;
815 case THREAD_PRIORITY_HIGHEST
: strcat(buffer
, ", priority +2 above normal"); break;
816 case THREAD_PRIORITY_LOWEST
: strcat(buffer
, ", priority -2 below normal"); break;
817 case THREAD_PRIORITY_IDLE
: strcat(buffer
, ", priority idle"); break;
818 case THREAD_PRIORITY_NORMAL
: strcat(buffer
, ", priority normal"); break;
819 case THREAD_PRIORITY_TIME_CRITICAL
: strcat(buffer
, ", priority time-critical"); break;
820 default: snprintf(buffer
+ strlen(buffer
), len
- strlen(buffer
), ", priority = %d", prio
);
822 assert(strlen(buffer
) < len
);
825 /* =============================================== *
826 * P A C K E T U T I L S *
827 * =============================================== *
830 static int addr_width(struct gdb_context
* gdbctx
)
832 int sz
= (gdbctx
&& gdbctx
->process
&& gdbctx
->process
->be_cpu
) ?
833 gdbctx
->process
->be_cpu
->pointer_size
: (int)sizeof(void*);
837 enum packet_return
{packet_error
= 0x00, packet_ok
= 0x01, packet_done
= 0x02,
838 packet_send_buffer
= 0x03, packet_last_f
= 0x80};
840 static void packet_reply_hex_to(struct gdb_context
* gdbctx
, const void* src
, int len
)
842 reply_buffer_append_hex(&gdbctx
->out_buf
, src
, len
);
845 static inline void packet_reply_hex_to_str(struct gdb_context
* gdbctx
, const char* src
)
847 packet_reply_hex_to(gdbctx
, src
, strlen(src
));
850 static void packet_reply_val(struct gdb_context
* gdbctx
, ULONG_PTR val
, int len
)
852 reply_buffer_append_uinthex(&gdbctx
->out_buf
, val
, len
);
855 static const unsigned char gdb_special_chars_lookup_table
[4] = {
856 /* The characters should be indexed by its value modulo table length. */
858 0x24, /* $: 001001|00 */
859 0x7D, /* }: 011111|01 */
860 0x2A, /* *: 001010|10 */
861 0x23 /* #: 001000|11 */
864 static inline BOOL
is_gdb_special_char(unsigned char val
)
866 /* A note on the GDB special character scanning code:
868 * We cannot use strcspn() since we plan to transmit binary data in
869 * packet reply, which can contain NULL (0x00) bytes. We also don't want
870 * to slow down memory dump transfers. Therefore, we use a tiny lookup
871 * table that contains all the four special characters to speed up scanning.
873 const size_t length
= ARRAY_SIZE(gdb_special_chars_lookup_table
);
874 return gdb_special_chars_lookup_table
[val
% length
] == val
;
877 static void packet_reply_add_data(struct gdb_context
* gdbctx
, const void* data
, size_t len
)
879 const unsigned char *ptr
= data
, *end
= ptr
+ len
, *curr
;
880 unsigned char esc_seq
[2];
886 while (ptr
!= end
&& !is_gdb_special_char(*ptr
))
889 reply_buffer_append(&gdbctx
->out_buf
, curr
, ptr
- curr
);
890 if (ptr
== end
) break;
893 esc_seq
[1] = 0x20 ^ *ptr
++;
894 reply_buffer_append(&gdbctx
->out_buf
, esc_seq
, 2);
898 static inline void packet_reply_add(struct gdb_context
* gdbctx
, const char* str
)
900 packet_reply_add_data(gdbctx
, str
, strlen(str
));
903 static void packet_reply_open(struct gdb_context
* gdbctx
)
905 assert(gdbctx
->out_curr_packet
== -1);
906 reply_buffer_append(&gdbctx
->out_buf
, "$", 1);
907 gdbctx
->out_curr_packet
= gdbctx
->out_buf
.len
;
910 static void packet_reply_close(struct gdb_context
* gdbctx
)
915 plen
= gdbctx
->out_buf
.len
- gdbctx
->out_curr_packet
;
916 reply_buffer_append(&gdbctx
->out_buf
, "#", 1);
917 cksum
= checksum(gdbctx
->out_buf
.base
+ gdbctx
->out_curr_packet
, plen
);
918 packet_reply_hex_to(gdbctx
, &cksum
, 1);
919 gdbctx
->out_curr_packet
= -1;
922 static enum packet_return
packet_reply(struct gdb_context
* gdbctx
, const char* packet
)
924 packet_reply_open(gdbctx
);
926 packet_reply_add(gdbctx
, packet
);
928 packet_reply_close(gdbctx
);
933 static enum packet_return
packet_reply_error(struct gdb_context
* gdbctx
, int error
)
935 packet_reply_open(gdbctx
);
937 packet_reply_add(gdbctx
, "E");
938 packet_reply_val(gdbctx
, error
, 1);
940 packet_reply_close(gdbctx
);
945 static inline void packet_reply_register_hex_to(struct gdb_context
* gdbctx
, dbg_ctx_t
* ctx
, unsigned idx
)
947 const struct gdb_register
*cpu_register_map
= gdbctx
->process
->be_cpu
->gdb_register_map
;
948 packet_reply_hex_to(gdbctx
, cpu_register_ptr(gdbctx
, ctx
, idx
), cpu_register_map
[idx
].length
);
951 static void packet_reply_xfer(struct gdb_context
* gdbctx
, size_t off
, size_t len
, BOOL
* more_p
)
954 size_t data_len
, trunc_len
;
956 packet_reply_open(gdbctx
);
957 data_len
= gdbctx
->qxfer_buffer
.len
;
959 /* check if off + len would overflow */
960 more
= off
< data_len
&& off
+ len
< data_len
;
962 packet_reply_add(gdbctx
, "m");
964 packet_reply_add(gdbctx
, "l");
968 trunc_len
= min(len
, data_len
- off
);
969 packet_reply_add_data(gdbctx
, gdbctx
->qxfer_buffer
.base
+ off
, trunc_len
);
972 packet_reply_close(gdbctx
);
977 /* =============================================== *
978 * P A C K E T H A N D L E R S *
979 * =============================================== *
982 static void packet_reply_status_xpoints(struct gdb_context
* gdbctx
, struct dbg_thread
*thread
,
985 struct dbg_process
*process
= thread
->process
;
986 struct backend_cpu
*cpu
= process
->be_cpu
;
987 struct gdb_xpoint
*x
;
989 LIST_FOR_EACH_ENTRY(x
, &gdbctx
->xpoint_list
, struct gdb_xpoint
, entry
)
991 if (x
->pid
!= process
->pid
|| x
->tid
!= thread
->tid
)
993 if (!cpu
->is_watchpoint_set(ctx
, x
->value
))
995 if (x
->type
== be_xpoint_watch_write
)
997 packet_reply_add(gdbctx
, "watch:");
998 packet_reply_val(gdbctx
, (ULONG_PTR
)x
->addr
, sizeof(x
->addr
));
999 packet_reply_add(gdbctx
, ";");
1001 if (x
->type
== be_xpoint_watch_read
)
1003 packet_reply_add(gdbctx
, "rwatch:");
1004 packet_reply_val(gdbctx
, (ULONG_PTR
)x
->addr
, sizeof(x
->addr
));
1005 packet_reply_add(gdbctx
, ";");
1010 static void packet_reply_begin_stop_reply(struct gdb_context
* gdbctx
, unsigned char signal
)
1012 packet_reply_add(gdbctx
, "T");
1013 packet_reply_val(gdbctx
, signal
, 1);
1015 /* We should always report the current thread ID for all stop replies.
1016 * Otherwise, GDB complains with the following message:
1018 * Warning: multi-threaded target stopped without sending a thread-id,
1019 * using first non-exited thread
1021 packet_reply_add(gdbctx
, "thread:");
1022 packet_reply_val(gdbctx
, gdbctx
->de
.dwThreadId
, 4);
1023 packet_reply_add(gdbctx
, ";");
1026 static enum packet_return
packet_reply_status(struct gdb_context
* gdbctx
)
1028 struct dbg_process
*process
= gdbctx
->process
;
1029 struct dbg_thread
*thread
;
1030 struct backend_cpu
*backend
;
1034 switch (gdbctx
->de
.dwDebugEventCode
)
1037 if (!process
) return packet_error
;
1038 if (!(backend
= process
->be_cpu
)) return packet_error
;
1039 if (!(thread
= dbg_get_thread(process
, gdbctx
->de
.dwThreadId
)) ||
1040 !backend
->get_context(thread
->handle
, &ctx
))
1041 return packet_error
;
1043 packet_reply_open(gdbctx
);
1044 packet_reply_begin_stop_reply(gdbctx
, signal_from_debug_event(&gdbctx
->de
));
1045 packet_reply_status_xpoints(gdbctx
, thread
, &ctx
);
1047 for (i
= 0; i
< backend
->gdb_num_regs
; i
++)
1049 packet_reply_val(gdbctx
, i
, 1);
1050 packet_reply_add(gdbctx
, ":");
1051 packet_reply_register_hex_to(gdbctx
, &ctx
, i
);
1052 packet_reply_add(gdbctx
, ";");
1055 packet_reply_close(gdbctx
);
1058 case EXIT_PROCESS_DEBUG_EVENT
:
1059 packet_reply_open(gdbctx
);
1060 packet_reply_add(gdbctx
, "W");
1061 packet_reply_val(gdbctx
, gdbctx
->de
.u
.ExitProcess
.dwExitCode
, 4);
1062 packet_reply_close(gdbctx
);
1063 return packet_done
| packet_last_f
;
1065 case LOAD_DLL_DEBUG_EVENT
:
1066 case UNLOAD_DLL_DEBUG_EVENT
:
1067 packet_reply_open(gdbctx
);
1068 packet_reply_begin_stop_reply(gdbctx
, HOST_SIGTRAP
);
1069 packet_reply_add(gdbctx
, "library:;");
1070 packet_reply_close(gdbctx
);
1075 static enum packet_return
packet_last_signal(struct gdb_context
* gdbctx
)
1077 assert(gdbctx
->in_packet_len
== 0);
1078 return packet_reply_status(gdbctx
);
1081 static enum packet_return
packet_continue(struct gdb_context
* gdbctx
)
1085 if (sscanf(gdbctx
->in_packet
, "%p", &addr
) == 1)
1086 FIXME("Continue at address %p not supported\n", addr
);
1088 handle_step_or_continue(gdbctx
, gdbctx
->exec_tid
, FALSE
, -1);
1090 wait_for_debuggee(gdbctx
);
1091 return packet_reply_status(gdbctx
);
1094 static enum packet_return
packet_verbose_cont(struct gdb_context
* gdbctx
)
1096 char *buf
= gdbctx
->in_packet
, *end
= gdbctx
->in_packet
+ gdbctx
->in_packet_len
;
1098 if (gdbctx
->in_packet
[4] == '?')
1100 packet_reply_open(gdbctx
);
1101 packet_reply_add(gdbctx
, "vCont");
1102 packet_reply_add(gdbctx
, ";c");
1103 packet_reply_add(gdbctx
, ";C");
1104 packet_reply_add(gdbctx
, ";s");
1105 packet_reply_add(gdbctx
, ";S");
1106 packet_reply_close(gdbctx
);
1110 while (buf
< end
&& (buf
= memchr(buf
+ 1, ';', end
- buf
- 1)))
1112 int tid
= -1, sig
= -1;
1115 switch ((action
= buf
[1]))
1118 return packet_error
;
1125 if (sscanf(buf
, ";%*c%2x", &sig
) <= 0 ||
1126 sig
!= signal_from_debug_event(&gdbctx
->de
))
1127 return packet_error
;
1133 return packet_error
;
1134 if (buf
< end
&& *buf
== ':' && (n
= sscanf(buf
, ":%x", &tid
)) <= 0)
1135 return packet_error
;
1137 handle_step_or_continue(gdbctx
, tid
, action
== 's' || action
== 'S', sig
);
1140 wait_for_debuggee(gdbctx
);
1141 return packet_reply_status(gdbctx
);
1144 static enum packet_return
packet_verbose(struct gdb_context
* gdbctx
)
1146 if (gdbctx
->in_packet_len
>= 4 && !memcmp(gdbctx
->in_packet
, "Cont", 4))
1148 return packet_verbose_cont(gdbctx
);
1151 if (gdbctx
->in_packet_len
== 14 && !memcmp(gdbctx
->in_packet
, "MustReplyEmpty", 14))
1152 return packet_reply(gdbctx
, "");
1154 return packet_error
;
1157 static enum packet_return
packet_continue_signal(struct gdb_context
* gdbctx
)
1162 if ((n
= sscanf(gdbctx
->in_packet
, "%x;%p", &sig
, &addr
)) == 2)
1163 FIXME("Continue at address %p not supported\n", addr
);
1164 if (n
< 1) return packet_error
;
1166 if (sig
!= signal_from_debug_event(&gdbctx
->de
))
1168 ERR("Changing signals is not supported.\n");
1169 return packet_error
;
1172 handle_step_or_continue(gdbctx
, gdbctx
->exec_tid
, FALSE
, sig
);
1174 wait_for_debuggee(gdbctx
);
1175 return packet_reply_status(gdbctx
);
1178 static enum packet_return
packet_delete_breakpoint(struct gdb_context
* gdbctx
)
1180 struct dbg_process
*process
= gdbctx
->process
;
1181 struct dbg_thread
*thread
;
1182 struct backend_cpu
*cpu
;
1183 struct gdb_xpoint
*x
;
1189 if (!process
) return packet_error
;
1190 if (!(cpu
= process
->be_cpu
)) return packet_error
;
1192 if (sscanf(gdbctx
->in_packet
, "%c,%p,%x", &type
, &addr
, &size
) < 3)
1193 return packet_error
;
1196 return packet_error
;
1198 LIST_FOR_EACH_ENTRY(thread
, &process
->threads
, struct dbg_thread
, entry
)
1200 if (!cpu
->get_context(thread
->handle
, &ctx
))
1202 if ((type
== '1') && (x
= gdb_find_xpoint(gdbctx
, thread
, be_xpoint_watch_exec
, addr
, size
)))
1203 gdbctx_delete_xpoint(gdbctx
, thread
, &ctx
, x
);
1204 if ((type
== '2' || type
== '4') && (x
= gdb_find_xpoint(gdbctx
, thread
, be_xpoint_watch_read
, addr
, size
)))
1205 gdbctx_delete_xpoint(gdbctx
, thread
, &ctx
, x
);
1206 if ((type
== '3' || type
== '4') && (x
= gdb_find_xpoint(gdbctx
, thread
, be_xpoint_watch_write
, addr
, size
)))
1207 gdbctx_delete_xpoint(gdbctx
, thread
, &ctx
, x
);
1208 cpu
->set_context(thread
->handle
, &ctx
);
1211 while ((type
== '1') && (x
= gdb_find_xpoint(gdbctx
, NULL
, be_xpoint_watch_exec
, addr
, size
)))
1212 gdbctx_delete_xpoint(gdbctx
, NULL
, NULL
, x
);
1213 while ((type
== '2' || type
== '4') && (x
= gdb_find_xpoint(gdbctx
, NULL
, be_xpoint_watch_read
, addr
, size
)))
1214 gdbctx_delete_xpoint(gdbctx
, NULL
, NULL
, x
);
1215 while ((type
== '3' || type
== '4') && (x
= gdb_find_xpoint(gdbctx
, NULL
, be_xpoint_watch_write
, addr
, size
)))
1216 gdbctx_delete_xpoint(gdbctx
, NULL
, NULL
, x
);
1221 static enum packet_return
packet_insert_breakpoint(struct gdb_context
* gdbctx
)
1223 struct dbg_process
*process
= gdbctx
->process
;
1224 struct dbg_thread
*thread
;
1225 struct backend_cpu
*cpu
;
1231 if (!process
) return packet_error
;
1232 if (!(cpu
= process
->be_cpu
)) return packet_error
;
1234 if (memchr(gdbctx
->in_packet
, ';', gdbctx
->in_packet_len
))
1236 FIXME("breakpoint commands not supported\n");
1237 return packet_error
;
1240 if (sscanf(gdbctx
->in_packet
, "%c,%p,%x", &type
, &addr
, &size
) < 3)
1241 return packet_error
;
1244 return packet_error
;
1246 LIST_FOR_EACH_ENTRY(thread
, &process
->threads
, struct dbg_thread
, entry
)
1248 if (!cpu
->get_context(thread
->handle
, &ctx
))
1251 gdbctx_insert_xpoint(gdbctx
, thread
, &ctx
, be_xpoint_watch_exec
, addr
, size
);
1252 if (type
== '2' || type
== '4')
1253 gdbctx_insert_xpoint(gdbctx
, thread
, &ctx
, be_xpoint_watch_read
, addr
, size
);
1254 if (type
== '3' || type
== '4')
1255 gdbctx_insert_xpoint(gdbctx
, thread
, &ctx
, be_xpoint_watch_write
, addr
, size
);
1256 cpu
->set_context(thread
->handle
, &ctx
);
1262 static enum packet_return
packet_detach(struct gdb_context
* gdbctx
)
1264 detach_debuggee(gdbctx
, FALSE
);
1265 return packet_ok
| packet_last_f
;
1268 static enum packet_return
packet_read_registers(struct gdb_context
* gdbctx
)
1270 struct dbg_thread
*thread
= dbg_thread_from_tid(gdbctx
, gdbctx
->other_tid
);
1271 struct backend_cpu
*backend
;
1275 if (!thread
) return packet_error
;
1276 if (!thread
->process
) return packet_error
;
1277 if (!(backend
= thread
->process
->be_cpu
)) return packet_error
;
1279 if (!backend
->get_context(thread
->handle
, &ctx
))
1280 return packet_error
;
1282 packet_reply_open(gdbctx
);
1283 for (i
= 0; i
< backend
->gdb_num_regs
; i
++)
1284 packet_reply_register_hex_to(gdbctx
, &ctx
, i
);
1286 packet_reply_close(gdbctx
);
1290 static enum packet_return
packet_write_registers(struct gdb_context
* gdbctx
)
1292 struct dbg_thread
*thread
= dbg_thread_from_tid(gdbctx
, gdbctx
->other_tid
);
1293 struct backend_cpu
*backend
;
1298 if (!thread
) return packet_error
;
1299 if (!thread
->process
) return packet_error
;
1300 if (!(backend
= thread
->process
->be_cpu
)) return packet_error
;
1302 if (!backend
->get_context(thread
->handle
, &ctx
))
1303 return packet_error
;
1305 if (gdbctx
->in_packet_len
< backend
->gdb_num_regs
* 2)
1306 return packet_error
;
1308 ptr
= gdbctx
->in_packet
;
1309 for (i
= 0; i
< backend
->gdb_num_regs
; i
++)
1310 cpu_register_hex_from(gdbctx
, &ctx
, i
, &ptr
);
1312 if (!backend
->set_context(thread
->handle
, &ctx
))
1314 ERR("Failed to set context for tid %04lx, error %lu\n", thread
->tid
, GetLastError());
1315 return packet_error
;
1321 static enum packet_return
packet_kill(struct gdb_context
* gdbctx
)
1323 detach_debuggee(gdbctx
, TRUE
);
1324 return packet_ok
| packet_last_f
;
1327 static enum packet_return
packet_thread(struct gdb_context
* gdbctx
)
1329 switch (gdbctx
->in_packet
[0])
1332 if (sscanf(gdbctx
->in_packet
, "c%x", &gdbctx
->exec_tid
) == 1)
1334 return packet_error
;
1336 if (sscanf(gdbctx
->in_packet
, "g%x", &gdbctx
->other_tid
) == 1)
1338 return packet_error
;
1340 FIXME("Unknown thread sub-command %c\n", gdbctx
->in_packet
[0]);
1341 return packet_error
;
1345 static enum packet_return
packet_read_memory(struct gdb_context
* gdbctx
)
1348 unsigned int len
, blk_len
, nread
;
1352 if (sscanf(gdbctx
->in_packet
, "%p,%x", &addr
, &len
) != 2) return packet_error
;
1353 if (len
<= 0) return packet_error
;
1354 TRACE("Read %u bytes at %p\n", len
, addr
);
1355 for (nread
= 0; nread
< len
; nread
+= r
, addr
+= r
)
1357 blk_len
= min(sizeof(buffer
), len
- nread
);
1358 if (!gdbctx
->process
->process_io
->read(gdbctx
->process
->handle
, addr
,
1359 buffer
, blk_len
, &r
) || r
== 0)
1361 /* fail at first address, return error */
1362 if (nread
== 0) return packet_reply_error(gdbctx
, HOST_EFAULT
);
1363 /* something has already been read, return partial information */
1366 if (nread
== 0) packet_reply_open(gdbctx
);
1367 packet_reply_hex_to(gdbctx
, buffer
, r
);
1369 packet_reply_close(gdbctx
);
1373 static enum packet_return
packet_write_memory(struct gdb_context
* gdbctx
)
1376 unsigned int len
, blk_len
;
1381 ptr
= memchr(gdbctx
->in_packet
, ':', gdbctx
->in_packet_len
);
1384 ERR("Cannot find ':' in %s\n", debugstr_an(gdbctx
->in_packet
, gdbctx
->in_packet_len
));
1385 return packet_error
;
1389 if (sscanf(gdbctx
->in_packet
, "%p,%x", &addr
, &len
) != 2)
1391 ERR("Failed to parse %s\n", debugstr_a(gdbctx
->in_packet
));
1392 return packet_error
;
1394 if (ptr
- gdbctx
->in_packet
+ len
* 2 != gdbctx
->in_packet_len
)
1396 ERR("Length %u does not match packet length %u\n",
1397 (int)(ptr
- gdbctx
->in_packet
) + len
* 2, gdbctx
->in_packet_len
);
1398 return packet_error
;
1400 TRACE("Write %u bytes at %p\n", len
, addr
);
1403 blk_len
= min(sizeof(buffer
), len
);
1404 hex_from(buffer
, ptr
, blk_len
);
1405 if (!gdbctx
->process
->process_io
->write(gdbctx
->process
->handle
, addr
, buffer
, blk_len
, &w
) ||
1412 return packet_ok
; /* FIXME: error while writing ? */
1415 static enum packet_return
packet_read_register(struct gdb_context
* gdbctx
)
1417 struct dbg_thread
*thread
= dbg_thread_from_tid(gdbctx
, gdbctx
->other_tid
);
1418 struct backend_cpu
*backend
;
1422 if (!thread
) return packet_error
;
1423 if (!thread
->process
) return packet_error
;
1424 if (!(backend
= thread
->process
->be_cpu
)) return packet_error
;
1426 if (!backend
->get_context(thread
->handle
, &ctx
))
1427 return packet_error
;
1429 if (sscanf(gdbctx
->in_packet
, "%Ix", ®
) != 1)
1430 return packet_error
;
1431 if (reg
>= backend
->gdb_num_regs
)
1433 WARN("Unhandled register %Iu\n", reg
);
1434 return packet_error
;
1437 TRACE("%Iu => %I64x\n", reg
, cpu_register(gdbctx
, &ctx
, reg
));
1439 packet_reply_open(gdbctx
);
1440 packet_reply_register_hex_to(gdbctx
, &ctx
, reg
);
1441 packet_reply_close(gdbctx
);
1445 static enum packet_return
packet_write_register(struct gdb_context
* gdbctx
)
1447 struct dbg_thread
*thread
= dbg_thread_from_tid(gdbctx
, gdbctx
->other_tid
);
1448 struct backend_cpu
*backend
;
1453 if (!thread
) return packet_error
;
1454 if (!thread
->process
) return packet_error
;
1455 if (!(backend
= thread
->process
->be_cpu
)) return packet_error
;
1457 if (!backend
->get_context(thread
->handle
, &ctx
))
1458 return packet_error
;
1460 if (!(ptr
= strchr(gdbctx
->in_packet
, '=')))
1461 return packet_error
;
1464 if (sscanf(gdbctx
->in_packet
, "%Ix", ®
) != 1)
1465 return packet_error
;
1466 if (reg
>= backend
->gdb_num_regs
)
1468 /* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
1469 * it wouldn't matter too much, and it fakes our support for all regs
1471 WARN("Unhandled register %Iu\n", reg
);
1475 TRACE("%Iu <= %s\n", reg
, debugstr_an(ptr
, (int)(gdbctx
->in_packet_len
- (ptr
- gdbctx
->in_packet
))));
1477 cpu_register_hex_from(gdbctx
, &ctx
, reg
, (const char**)&ptr
);
1478 if (!backend
->set_context(thread
->handle
, &ctx
))
1480 ERR("Failed to set context for tid %04lx, error %lu\n", thread
->tid
, GetLastError());
1481 return packet_error
;
1487 static void packet_query_monitor_wnd_helper(struct gdb_context
* gdbctx
, HWND hWnd
, int indent
)
1495 if (!GetClassNameA(hWnd
, clsName
, sizeof(clsName
)))
1496 strcpy(clsName
, "-- Unknown --");
1497 if (!GetWindowTextA(hWnd
, wndName
, sizeof(wndName
)))
1498 strcpy(wndName
, "-- Empty --");
1500 packet_reply_open(gdbctx
);
1501 packet_reply_add(gdbctx
, "O");
1502 snprintf(buffer
, sizeof(buffer
),
1503 "%*s%04Ix%*s%-17.17s %08lx %0*Ix %.14s\n",
1504 indent
, "", (ULONG_PTR
)hWnd
, 13 - indent
, "",
1505 clsName
, GetWindowLongW(hWnd
, GWL_STYLE
),
1506 addr_width(gdbctx
), (ULONG_PTR
)GetWindowLongPtrW(hWnd
, GWLP_WNDPROC
),
1508 packet_reply_hex_to_str(gdbctx
, buffer
);
1509 packet_reply_close(gdbctx
);
1511 if ((child
= GetWindow(hWnd
, GW_CHILD
)) != 0)
1512 packet_query_monitor_wnd_helper(gdbctx
, child
, indent
+ 1);
1513 } while ((hWnd
= GetWindow(hWnd
, GW_HWNDNEXT
)) != 0);
1516 static void packet_query_monitor_wnd(struct gdb_context
* gdbctx
, int len
, const char* str
)
1520 /* we do the output in several 'O' packets, with the last one being just OK for
1521 * marking the end of the output */
1522 packet_reply_open(gdbctx
);
1523 packet_reply_add(gdbctx
, "O");
1524 snprintf(buffer
, sizeof(buffer
),
1525 "%-16.16s %-17.17s %-8.8s %s\n",
1526 "hwnd", "Class Name", " Style", " WndProc Text");
1527 packet_reply_hex_to_str(gdbctx
, buffer
);
1528 packet_reply_close(gdbctx
);
1530 /* FIXME: could also add a pmt to this command in str... */
1531 packet_query_monitor_wnd_helper(gdbctx
, GetDesktopWindow(), 0);
1532 packet_reply(gdbctx
, "OK");
1535 static void packet_query_monitor_process(struct gdb_context
* gdbctx
, int len
, const char* str
)
1537 HANDLE snap
= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS
, 0);
1538 char buffer
[31+MAX_PATH
];
1540 PROCESSENTRY32 entry
;
1543 if (snap
== INVALID_HANDLE_VALUE
)
1546 entry
.dwSize
= sizeof(entry
);
1547 ok
= Process32First(snap
, &entry
);
1549 /* we do the output in several 'O' packets, with the last one being just OK for
1550 * marking the end of the output */
1552 packet_reply_open(gdbctx
);
1553 packet_reply_add(gdbctx
, "O");
1554 snprintf(buffer
, sizeof(buffer
),
1555 " %-8.8s %-8.8s %-8.8s %s\n",
1556 "pid", "threads", "parent", "executable");
1557 packet_reply_hex_to_str(gdbctx
, buffer
);
1558 packet_reply_close(gdbctx
);
1563 if (entry
.th32ProcessID
== gdbctx
->process
->pid
) deco
= '>';
1564 packet_reply_open(gdbctx
);
1565 packet_reply_add(gdbctx
, "O");
1566 snprintf(buffer
, sizeof(buffer
),
1567 "%c%08lx %-8ld %08lx '%s'\n",
1568 deco
, entry
.th32ProcessID
, entry
.cntThreads
,
1569 entry
.th32ParentProcessID
, entry
.szExeFile
);
1570 packet_reply_hex_to_str(gdbctx
, buffer
);
1571 packet_reply_close(gdbctx
);
1572 ok
= Process32Next(snap
, &entry
);
1575 packet_reply(gdbctx
, "OK");
1578 static void packet_query_monitor_mem(struct gdb_context
* gdbctx
, int len
, const char* str
)
1580 MEMORY_BASIC_INFORMATION mbi
;
1587 /* we do the output in several 'O' packets, with the last one being just OK for
1588 * marking the end of the output */
1589 packet_reply_open(gdbctx
);
1590 packet_reply_add(gdbctx
, "O");
1591 packet_reply_hex_to_str(gdbctx
, "Address Size State Type RWX\n");
1592 packet_reply_close(gdbctx
);
1594 while (VirtualQueryEx(gdbctx
->process
->handle
, addr
, &mbi
, sizeof(mbi
)) >= sizeof(mbi
))
1598 case MEM_COMMIT
: state
= "commit "; break;
1599 case MEM_FREE
: state
= "free "; break;
1600 case MEM_RESERVE
: state
= "reserve"; break;
1601 default: state
= "??? "; break;
1603 if (mbi
.State
!= MEM_FREE
)
1607 case MEM_IMAGE
: type
= "image "; break;
1608 case MEM_MAPPED
: type
= "mapped "; break;
1609 case MEM_PRIVATE
: type
= "private"; break;
1610 case 0: type
= " "; break;
1611 default: type
= "??? "; break;
1613 memset(prot
, ' ' , sizeof(prot
)-1);
1614 prot
[sizeof(prot
)-1] = '\0';
1615 if (mbi
.AllocationProtect
& (PAGE_READONLY
|PAGE_READWRITE
|PAGE_EXECUTE_READ
|PAGE_EXECUTE_READWRITE
|PAGE_WRITECOPY
|PAGE_EXECUTE_WRITECOPY
))
1617 if (mbi
.AllocationProtect
& (PAGE_READWRITE
|PAGE_EXECUTE_READWRITE
))
1619 if (mbi
.AllocationProtect
& (PAGE_WRITECOPY
|PAGE_EXECUTE_WRITECOPY
))
1621 if (mbi
.AllocationProtect
& (PAGE_EXECUTE
|PAGE_EXECUTE_READ
|PAGE_EXECUTE_READWRITE
|PAGE_EXECUTE_WRITECOPY
))
1629 packet_reply_open(gdbctx
);
1630 snprintf(buffer
, sizeof(buffer
), "%0*Ix %0*Ix %s %s %s\n",
1631 addr_width(gdbctx
), (DWORD_PTR
)addr
,
1632 addr_width(gdbctx
), mbi
.RegionSize
, state
, type
, prot
);
1633 packet_reply_add(gdbctx
, "O");
1634 packet_reply_hex_to_str(gdbctx
, buffer
);
1635 packet_reply_close(gdbctx
);
1637 if (addr
+ mbi
.RegionSize
< addr
) /* wrap around ? */
1639 addr
+= mbi
.RegionSize
;
1641 packet_reply(gdbctx
, "OK");
1649 void (*handler
)(struct gdb_context
*, int, const char*);
1652 {0, "wnd", 3, packet_query_monitor_wnd
},
1653 {0, "window", 6, packet_query_monitor_wnd
},
1654 {0, "proc", 4, packet_query_monitor_process
},
1655 {0, "process", 7, packet_query_monitor_process
},
1656 {0, "mem", 3, packet_query_monitor_mem
},
1660 static enum packet_return
packet_query_remote_command(struct gdb_context
* gdbctx
,
1661 const char* hxcmd
, size_t len
)
1664 struct query_detail
* qd
;
1666 assert((len
& 1) == 0 && len
< 2 * sizeof(buffer
));
1668 hex_from(buffer
, hxcmd
, len
);
1670 for (qd
= query_details
; qd
->name
!= NULL
; qd
++)
1672 if (len
< qd
->len
|| strncmp(buffer
, qd
->name
, qd
->len
) != 0) continue;
1673 if (!qd
->with_arg
&& len
!= qd
->len
) continue;
1675 (qd
->handler
)(gdbctx
, len
- qd
->len
, buffer
+ qd
->len
);
1678 return packet_reply_error(gdbctx
, HOST_EINVAL
);
1681 static BOOL CALLBACK
packet_query_libraries_cb(PCSTR mod_name
, DWORD64 base
, PVOID ctx
)
1683 struct gdb_context
* gdbctx
= ctx
;
1684 struct reply_buffer
* reply
= &gdbctx
->qxfer_buffer
;
1685 MEMORY_BASIC_INFORMATION mbi
;
1686 IMAGE_SECTION_HEADER
*sec
;
1687 IMAGE_DOS_HEADER
*dos
= NULL
;
1688 IMAGE_NT_HEADERS
*nth
= NULL
;
1689 IMAGEHLP_MODULE64 mod
;
1694 mod
.SizeOfStruct
= sizeof(mod
);
1695 SymGetModuleInfo64(gdbctx
->process
->handle
, base
, &mod
);
1697 reply_buffer_append_str(reply
, "<library name=\"");
1698 if (strcmp(mod
.LoadedImageName
, "[vdso].so") == 0)
1699 reply_buffer_append_xmlstr(reply
, "linux-vdso.so.1");
1700 else if (mod
.LoadedImageName
[0] == '/')
1701 reply_buffer_append_xmlstr(reply
, mod
.LoadedImageName
);
1704 UNICODE_STRING nt_name
;
1705 ANSI_STRING ansi_name
;
1706 char *unix_path
, *tmp
;
1708 RtlInitAnsiString(&ansi_name
, mod
.LoadedImageName
);
1709 RtlAnsiStringToUnicodeString(&nt_name
, &ansi_name
, TRUE
);
1711 if ((unix_path
= wine_get_unix_file_name(nt_name
.Buffer
)))
1713 if (IsWow64Process(gdbctx
->process
->handle
, &is_wow64
) &&
1714 is_wow64
&& (tmp
= strstr(unix_path
, "system32")))
1715 memcpy(tmp
, "syswow64", 8);
1716 reply_buffer_append_xmlstr(reply
, unix_path
);
1719 reply_buffer_append_xmlstr(reply
, mod
.LoadedImageName
);
1721 HeapFree(GetProcessHeap(), 0, unix_path
);
1722 RtlFreeUnicodeString(&nt_name
);
1724 reply_buffer_append_str(reply
, "\">");
1726 size
= sizeof(buffer
);
1727 if (VirtualQueryEx(gdbctx
->process
->handle
, (void *)(UINT_PTR
)mod
.BaseOfImage
, &mbi
, sizeof(mbi
)) >= sizeof(mbi
) &&
1728 mbi
.Type
== MEM_IMAGE
&& mbi
.State
!= MEM_FREE
)
1730 if (ReadProcessMemory(gdbctx
->process
->handle
, (void *)(UINT_PTR
)mod
.BaseOfImage
, buffer
, size
, &size
) &&
1731 size
>= sizeof(IMAGE_DOS_HEADER
))
1732 dos
= (IMAGE_DOS_HEADER
*)buffer
;
1734 if (dos
&& dos
->e_magic
== IMAGE_DOS_SIGNATURE
&& dos
->e_lfanew
< size
)
1735 nth
= (IMAGE_NT_HEADERS
*)(buffer
+ dos
->e_lfanew
);
1737 if (nth
&& memcmp(&nth
->Signature
, "PE\0\0", 4))
1741 if (!nth
) memset(buffer
, 0, sizeof(buffer
));
1743 /* if the module is not PE we have cleared buffer with 0, this makes
1744 * the following computation valid in all cases. */
1745 dos
= (IMAGE_DOS_HEADER
*)buffer
;
1746 nth
= (IMAGE_NT_HEADERS
*)(buffer
+ dos
->e_lfanew
);
1747 if (IsWow64Process(gdbctx
->process
->handle
, &is_wow64
) && is_wow64
)
1748 sec
= IMAGE_FIRST_SECTION((IMAGE_NT_HEADERS32
*)nth
);
1750 sec
= IMAGE_FIRST_SECTION((IMAGE_NT_HEADERS64
*)nth
);
1752 for (i
= 0; i
< max(nth
->FileHeader
.NumberOfSections
, 1); ++i
)
1754 if ((char *)(sec
+ i
) >= buffer
+ size
) break;
1755 reply_buffer_append_str(reply
, "<segment address=\"0x");
1756 reply_buffer_append_uinthex(reply
, mod
.BaseOfImage
+ sec
[i
].VirtualAddress
, sizeof(ULONG_PTR
));
1757 reply_buffer_append_str(reply
, "\"/>");
1760 reply_buffer_append_str(reply
, "</library>");
1765 static enum packet_return
packet_query_libraries(struct gdb_context
* gdbctx
)
1767 struct reply_buffer
* reply
= &gdbctx
->qxfer_buffer
;
1770 if (!gdbctx
->process
) return packet_error
;
1772 if (gdbctx
->qxfer_object_annex
[0])
1773 return packet_reply_error(gdbctx
, 0);
1775 /* this will resynchronize builtin dbghelp's internal ELF module list */
1776 SymLoadModule(gdbctx
->process
->handle
, 0, 0, 0, 0, 0);
1778 reply_buffer_append_str(reply
, "<library-list>");
1779 opt
= SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES
, TRUE
);
1780 SymEnumerateModules64(gdbctx
->process
->handle
, packet_query_libraries_cb
, gdbctx
);
1781 SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES
, opt
);
1782 reply_buffer_append_str(reply
, "</library-list>");
1784 return packet_send_buffer
;
1787 static enum packet_return
packet_query_threads(struct gdb_context
* gdbctx
)
1789 struct reply_buffer
* reply
= &gdbctx
->qxfer_buffer
;
1790 struct dbg_process
* process
= gdbctx
->process
;
1791 struct dbg_thread
* thread
;
1794 if (!process
) return packet_error
;
1796 if (gdbctx
->qxfer_object_annex
[0])
1797 return packet_reply_error(gdbctx
, 0);
1799 reply_buffer_append_str(reply
, "<threads>");
1800 LIST_FOR_EACH_ENTRY(thread
, &process
->threads
, struct dbg_thread
, entry
)
1802 reply_buffer_append_str(reply
, "<thread ");
1803 reply_buffer_append_str(reply
, "id=\"");
1804 reply_buffer_append_uinthex(reply
, thread
->tid
, 4);
1805 reply_buffer_append_str(reply
, "\" name=\"");
1806 if ((description
= fetch_thread_description(thread
->tid
)))
1808 reply_buffer_append_wstr(reply
, description
);
1809 LocalFree(description
);
1811 else if (strlen(thread
->name
))
1813 reply_buffer_append_str(reply
, thread
->name
);
1818 snprintf(tid
, sizeof(tid
), "%04lx", thread
->tid
);
1819 reply_buffer_append_str(reply
, tid
);
1821 reply_buffer_append_str(reply
, "\"/>");
1823 reply_buffer_append_str(reply
, "</threads>");
1825 return packet_send_buffer
;
1828 static void packet_query_target_xml(struct gdb_context
* gdbctx
, struct reply_buffer
* reply
, struct backend_cpu
* cpu
)
1830 const char* feature_prefix
= NULL
;
1831 const char* feature
= NULL
;
1835 reply_buffer_append_str(reply
, "<target>");
1836 switch (cpu
->machine
)
1838 case IMAGE_FILE_MACHINE_AMD64
:
1839 reply_buffer_append_str(reply
, "<architecture>i386:x86-64</architecture>");
1840 feature_prefix
= "org.gnu.gdb.i386.";
1842 case IMAGE_FILE_MACHINE_I386
:
1843 reply_buffer_append_str(reply
, "<architecture>i386</architecture>");
1844 feature_prefix
= "org.gnu.gdb.i386.";
1846 case IMAGE_FILE_MACHINE_ARMNT
:
1847 reply_buffer_append_str(reply
, "<architecture>arm</architecture>");
1848 feature_prefix
= "org.gnu.gdb.arm.";
1850 case IMAGE_FILE_MACHINE_ARM64
:
1851 reply_buffer_append_str(reply
, "<architecture>aarch64</architecture>");
1852 feature_prefix
= "org.gnu.gdb.aarch64.";
1856 for (i
= 0; i
< cpu
->gdb_num_regs
; ++i
)
1858 if (cpu
->gdb_register_map
[i
].feature
)
1860 if (feature
) reply_buffer_append_str(reply
, "</feature>");
1861 feature
= cpu
->gdb_register_map
[i
].feature
;
1863 reply_buffer_append_str(reply
, "<feature name=\"");
1864 if (feature_prefix
) reply_buffer_append_xmlstr(reply
, feature_prefix
);
1865 reply_buffer_append_xmlstr(reply
, feature
);
1866 reply_buffer_append_str(reply
, "\">");
1868 if (strcmp(feature_prefix
, "org.gnu.gdb.i386.") == 0 &&
1869 strcmp(feature
, "core") == 0)
1870 reply_buffer_append_str(reply
, "<flags id=\"i386_eflags\" size=\"4\">"
1871 "<field name=\"CF\" start=\"0\" end=\"0\"/>"
1872 "<field name=\"\" start=\"1\" end=\"1\"/>"
1873 "<field name=\"PF\" start=\"2\" end=\"2\"/>"
1874 "<field name=\"AF\" start=\"4\" end=\"4\"/>"
1875 "<field name=\"ZF\" start=\"6\" end=\"6\"/>"
1876 "<field name=\"SF\" start=\"7\" end=\"7\"/>"
1877 "<field name=\"TF\" start=\"8\" end=\"8\"/>"
1878 "<field name=\"IF\" start=\"9\" end=\"9\"/>"
1879 "<field name=\"DF\" start=\"10\" end=\"10\"/>"
1880 "<field name=\"OF\" start=\"11\" end=\"11\"/>"
1881 "<field name=\"NT\" start=\"14\" end=\"14\"/>"
1882 "<field name=\"RF\" start=\"16\" end=\"16\"/>"
1883 "<field name=\"VM\" start=\"17\" end=\"17\"/>"
1884 "<field name=\"AC\" start=\"18\" end=\"18\"/>"
1885 "<field name=\"VIF\" start=\"19\" end=\"19\"/>"
1886 "<field name=\"VIP\" start=\"20\" end=\"20\"/>"
1887 "<field name=\"ID\" start=\"21\" end=\"21\"/>"
1890 if (strcmp(feature_prefix
, "org.gnu.gdb.i386.") == 0 &&
1891 strcmp(feature
, "sse") == 0)
1892 reply_buffer_append_str(reply
, "<vector id=\"v4f\" type=\"ieee_single\" count=\"4\"/>"
1893 "<vector id=\"v2d\" type=\"ieee_double\" count=\"2\"/>"
1894 "<vector id=\"v16i8\" type=\"int8\" count=\"16\"/>"
1895 "<vector id=\"v8i16\" type=\"int16\" count=\"8\"/>"
1896 "<vector id=\"v4i32\" type=\"int32\" count=\"4\"/>"
1897 "<vector id=\"v2i64\" type=\"int64\" count=\"2\"/>"
1898 "<union id=\"vec128\">"
1899 "<field name=\"v4_float\" type=\"v4f\"/>"
1900 "<field name=\"v2_double\" type=\"v2d\"/>"
1901 "<field name=\"v16_int8\" type=\"v16i8\"/>"
1902 "<field name=\"v8_int16\" type=\"v8i16\"/>"
1903 "<field name=\"v4_int32\" type=\"v4i32\"/>"
1904 "<field name=\"v2_int64\" type=\"v2i64\"/>"
1905 "<field name=\"uint128\" type=\"uint128\"/>"
1907 "<flags id=\"i386_mxcsr\" size=\"4\">"
1908 "<field name=\"IE\" start=\"0\" end=\"0\"/>"
1909 "<field name=\"DE\" start=\"1\" end=\"1\"/>"
1910 "<field name=\"ZE\" start=\"2\" end=\"2\"/>"
1911 "<field name=\"OE\" start=\"3\" end=\"3\"/>"
1912 "<field name=\"UE\" start=\"4\" end=\"4\"/>"
1913 "<field name=\"PE\" start=\"5\" end=\"5\"/>"
1914 "<field name=\"DAZ\" start=\"6\" end=\"6\"/>"
1915 "<field name=\"IM\" start=\"7\" end=\"7\"/>"
1916 "<field name=\"DM\" start=\"8\" end=\"8\"/>"
1917 "<field name=\"ZM\" start=\"9\" end=\"9\"/>"
1918 "<field name=\"OM\" start=\"10\" end=\"10\"/>"
1919 "<field name=\"UM\" start=\"11\" end=\"11\"/>"
1920 "<field name=\"PM\" start=\"12\" end=\"12\"/>"
1921 "<field name=\"FZ\" start=\"15\" end=\"15\"/>"
1925 snprintf(buffer
, ARRAY_SIZE(buffer
), "<reg name=\"%s\" bitsize=\"%Iu\"",
1926 cpu
->gdb_register_map
[i
].name
, 8 * cpu
->gdb_register_map
[i
].length
);
1927 reply_buffer_append_str(reply
, buffer
);
1929 if (cpu
->gdb_register_map
[i
].type
)
1931 reply_buffer_append_str(reply
, " type=\"");
1932 reply_buffer_append_xmlstr(reply
, cpu
->gdb_register_map
[i
].type
);
1933 reply_buffer_append_str(reply
, "\"");
1936 reply_buffer_append_str(reply
, "/>");
1939 if (feature
) reply_buffer_append_str(reply
, "</feature>");
1940 reply_buffer_append_str(reply
, "</target>");
1943 static enum packet_return
packet_query_features(struct gdb_context
* gdbctx
)
1945 struct reply_buffer
* reply
= &gdbctx
->qxfer_buffer
;
1946 struct dbg_process
* process
= gdbctx
->process
;
1948 if (!process
) return packet_error
;
1950 if (strncmp(gdbctx
->qxfer_object_annex
, "target.xml", QX_ANNEX_SIZE
) == 0)
1952 struct backend_cpu
*cpu
= process
->be_cpu
;
1953 if (!cpu
) return packet_error
;
1955 packet_query_target_xml(gdbctx
, reply
, cpu
);
1957 return packet_send_buffer
;
1960 return packet_reply_error(gdbctx
, 0);
1963 static enum packet_return
packet_query_exec_file(struct gdb_context
* gdbctx
)
1965 struct reply_buffer
* reply
= &gdbctx
->qxfer_buffer
;
1966 struct dbg_process
* process
= gdbctx
->process
;
1971 if (!process
) return packet_error
;
1973 if (gdbctx
->qxfer_object_annex
[0] || !process
->imageName
)
1974 return packet_reply_error(gdbctx
, HOST_EPERM
);
1976 if (!(unix_path
= wine_get_unix_file_name(process
->imageName
)))
1977 return packet_reply_error(gdbctx
, GetLastError() == ERROR_NOT_ENOUGH_MEMORY
? HOST_ENOMEM
: HOST_ENOENT
);
1979 if (IsWow64Process(process
->handle
, &is_wow64
) &&
1980 is_wow64
&& (tmp
= strstr(unix_path
, "system32")))
1981 memcpy(tmp
, "syswow64", 8);
1983 reply_buffer_append_str(reply
, unix_path
);
1985 HeapFree(GetProcessHeap(), 0, unix_path
);
1987 return packet_send_buffer
;
1993 enum packet_return (*handler
)(struct gdb_context
* gdbctx
);
1994 } qxfer_handlers
[] =
1996 {"libraries", packet_query_libraries
},
1997 {"threads" , packet_query_threads
},
1998 {"features" , packet_query_features
},
1999 {"exec-file", packet_query_exec_file
},
2002 static enum packet_return
packet_query(struct gdb_context
* gdbctx
)
2004 char object_name
[QX_NAME_SIZE
], annex
[QX_ANNEX_SIZE
];
2005 unsigned int off
, len
;
2007 switch (gdbctx
->in_packet
[0])
2010 if (strncmp(gdbctx
->in_packet
+ 1, "ThreadInfo", gdbctx
->in_packet_len
- 1) == 0)
2012 struct dbg_thread
* thd
;
2014 packet_reply_open(gdbctx
);
2015 packet_reply_add(gdbctx
, "m");
2016 LIST_FOR_EACH_ENTRY(thd
, &gdbctx
->process
->threads
, struct dbg_thread
, entry
)
2018 packet_reply_val(gdbctx
, thd
->tid
, 4);
2019 if (list_next(&gdbctx
->process
->threads
, &thd
->entry
) != NULL
)
2020 packet_reply_add(gdbctx
, ",");
2022 packet_reply_close(gdbctx
);
2025 else if (strncmp(gdbctx
->in_packet
+ 1, "ProcessInfo", gdbctx
->in_packet_len
- 1) == 0)
2029 packet_reply_open(gdbctx
);
2030 packet_reply_add(gdbctx
, "O");
2031 get_process_info(gdbctx
, result
, sizeof(result
));
2032 packet_reply_hex_to_str(gdbctx
, result
);
2033 packet_reply_close(gdbctx
);
2038 if (strncmp(gdbctx
->in_packet
+ 1, "ThreadInfo", gdbctx
->in_packet_len
- 1) == 0)
2040 packet_reply(gdbctx
, "l");
2043 else if (strncmp(gdbctx
->in_packet
+ 1, "ProcessInfo", gdbctx
->in_packet_len
- 1) == 0)
2045 packet_reply(gdbctx
, "l");
2050 if (strncmp(gdbctx
->in_packet
, "Attached", gdbctx
->in_packet_len
) == 0)
2051 return packet_reply(gdbctx
, "1");
2054 if (gdbctx
->in_packet_len
== 1)
2056 struct dbg_thread
* thd
;
2057 /* FIXME: doc says 16 bit val ??? */
2058 /* grab first created thread, aka last in list */
2059 assert(gdbctx
->process
&& !list_empty(&gdbctx
->process
->threads
));
2060 thd
= LIST_ENTRY(list_tail(&gdbctx
->process
->threads
), struct dbg_thread
, entry
);
2061 packet_reply_open(gdbctx
);
2062 packet_reply_add(gdbctx
, "QC");
2063 packet_reply_val(gdbctx
, thd
->tid
, 4);
2064 packet_reply_close(gdbctx
);
2069 if (gdbctx
->in_packet_len
> 10 &&
2070 strncmp(gdbctx
->in_packet
, "GetTIBAddr", 10) == 0 &&
2071 gdbctx
->in_packet
[10] == ':')
2075 struct dbg_thread
* thd
;
2077 tid
= strtol(gdbctx
->in_packet
+ 11, &end
, 16);
2078 if (end
== NULL
) break;
2080 thd
= dbg_get_thread(gdbctx
->process
, tid
);
2082 return packet_reply_error(gdbctx
, HOST_EINVAL
);
2083 packet_reply_open(gdbctx
);
2084 packet_reply_val(gdbctx
, (ULONG_PTR
)thd
->teb
, sizeof(thd
->teb
));
2085 packet_reply_close(gdbctx
);
2090 if (strncmp(gdbctx
->in_packet
, "Offsets", gdbctx
->in_packet_len
) == 0)
2094 snprintf(buf
, sizeof(buf
),
2095 "Text=%08Ix;Data=%08Ix;Bss=%08Ix",
2096 gdbctx
->wine_segs
[0], gdbctx
->wine_segs
[1],
2097 gdbctx
->wine_segs
[2]);
2098 return packet_reply(gdbctx
, buf
);
2102 if (gdbctx
->in_packet_len
> 5 && strncmp(gdbctx
->in_packet
, "Rcmd,", 5) == 0)
2104 return packet_query_remote_command(gdbctx
, gdbctx
->in_packet
+ 5,
2105 gdbctx
->in_packet_len
- 5);
2109 if (strncmp(gdbctx
->in_packet
, "Symbol::", gdbctx
->in_packet_len
) == 0)
2111 if (strncmp(gdbctx
->in_packet
, "Supported", 9) == 0)
2115 packet_reply_open(gdbctx
);
2116 packet_reply_add(gdbctx
, "QStartNoAckMode+;");
2117 for (i
= 0; i
< ARRAY_SIZE(qxfer_handlers
); i
++)
2119 packet_reply_add(gdbctx
, "qXfer:");
2120 packet_reply_add(gdbctx
, qxfer_handlers
[i
].name
);
2121 packet_reply_add(gdbctx
, ":read+;");
2123 packet_reply_close(gdbctx
);
2128 if (gdbctx
->in_packet_len
> 15 &&
2129 strncmp(gdbctx
->in_packet
, "ThreadExtraInfo", 15) == 0 &&
2130 gdbctx
->in_packet
[15] == ',')
2136 tid
= strtol(gdbctx
->in_packet
+ 16, &end
, 16);
2137 if (end
== NULL
) break;
2138 get_thread_info(gdbctx
, tid
, result
, sizeof(result
));
2139 packet_reply_open(gdbctx
);
2140 packet_reply_hex_to_str(gdbctx
, result
);
2141 packet_reply_close(gdbctx
);
2144 if (strncmp(gdbctx
->in_packet
, "TStatus", 7) == 0)
2146 /* Tracepoints not supported */
2147 packet_reply_open(gdbctx
);
2148 packet_reply_close(gdbctx
);
2154 if (sscanf(gdbctx
->in_packet
, "Xfer:%31[^:]:read::%x,%x", object_name
, &off
, &len
) == 3 ||
2155 sscanf(gdbctx
->in_packet
, "Xfer:%31[^:]:read:%255[^:]:%x,%x", object_name
, annex
, &off
, &len
) == 4)
2157 enum packet_return result
;
2161 for (i
= 0; i
< ARRAY_SIZE(qxfer_handlers
); i
++)
2163 if (strcmp(qxfer_handlers
[i
].name
, object_name
) == 0)
2167 if (i
>= ARRAY_SIZE(qxfer_handlers
))
2169 ERR("unhandled qXfer %s read %s %u,%u\n", debugstr_a(object_name
), debugstr_a(annex
), off
, len
);
2170 return packet_error
;
2173 TRACE("qXfer %s read %s %u,%u\n", debugstr_a(object_name
), debugstr_a(annex
), off
, len
);
2176 gdbctx
->qxfer_buffer
.len
> 0 &&
2177 gdbctx
->qxfer_object_idx
== i
&&
2178 strcmp(gdbctx
->qxfer_object_annex
, annex
) == 0)
2180 result
= packet_send_buffer
;
2181 TRACE("qXfer read result = %d (cached)\n", result
);
2185 reply_buffer_clear(&gdbctx
->qxfer_buffer
);
2187 gdbctx
->qxfer_object_idx
= i
;
2188 strcpy(gdbctx
->qxfer_object_annex
, annex
);
2190 result
= (*qxfer_handlers
[i
].handler
)(gdbctx
);
2191 TRACE("qXfer read result = %d\n", result
);
2195 if ((result
& ~packet_last_f
) == packet_send_buffer
)
2197 packet_reply_xfer(gdbctx
, off
, len
, &more
);
2198 result
= (result
& packet_last_f
) | packet_done
;
2203 gdbctx
->qxfer_object_idx
= -1;
2204 gdbctx
->qxfer_object_annex
[0] = '\0';
2205 reply_buffer_clear(&gdbctx
->qxfer_buffer
);
2212 ERR("Unhandled query %s\n", debugstr_an(gdbctx
->in_packet
, gdbctx
->in_packet_len
));
2213 return packet_error
;
2216 static enum packet_return
packet_set(struct gdb_context
* gdbctx
)
2218 if (strncmp(gdbctx
->in_packet
, "StartNoAckMode", 14) == 0)
2220 gdbctx
->no_ack_mode
= TRUE
;
2224 return packet_error
;
2227 static enum packet_return
packet_step(struct gdb_context
* gdbctx
)
2231 if (sscanf(gdbctx
->in_packet
, "%p", &addr
) == 1)
2232 FIXME("Continue at address %p not supported\n", addr
);
2234 handle_step_or_continue(gdbctx
, gdbctx
->exec_tid
, TRUE
, -1);
2236 wait_for_debuggee(gdbctx
);
2237 return packet_reply_status(gdbctx
);
2240 static enum packet_return
packet_thread_alive(struct gdb_context
* gdbctx
)
2245 tid
= strtol(gdbctx
->in_packet
, &end
, 16);
2246 if (tid
== -1 || tid
== 0)
2247 return packet_reply_error(gdbctx
, HOST_EINVAL
);
2248 if (dbg_get_thread(gdbctx
->process
, tid
) != NULL
)
2250 return packet_reply_error(gdbctx
, HOST_ESRCH
);
2253 /* =============================================== *
2254 * P A C K E T I N F R A S T R U C T U R E *
2255 * =============================================== *
2261 enum packet_return (*handler
)(struct gdb_context
* gdbctx
);
2264 static struct packet_entry packet_entries
[] =
2266 {'?', packet_last_signal
},
2267 {'c', packet_continue
},
2268 {'C', packet_continue_signal
},
2269 {'D', packet_detach
},
2270 {'g', packet_read_registers
},
2271 {'G', packet_write_registers
},
2273 {'H', packet_thread
},
2274 {'m', packet_read_memory
},
2275 {'M', packet_write_memory
},
2276 {'p', packet_read_register
},
2277 {'P', packet_write_register
},
2278 {'q', packet_query
},
2281 {'T', packet_thread_alive
},
2282 {'v', packet_verbose
},
2283 {'z', packet_delete_breakpoint
},
2284 {'Z', packet_insert_breakpoint
},
2287 static BOOL
extract_packets(struct gdb_context
* gdbctx
)
2289 char *ptr
, *sum
= gdbctx
->in_buf
, *end
= gdbctx
->in_buf
+ gdbctx
->in_len
;
2290 enum packet_return ret
= packet_error
;
2294 /* ptr points to the beginning ('$') of the current packet
2295 * sum points to the beginning ('#') of the current packet checksum ("#xx")
2296 * len is the length of the current packet data (sum - ptr - 1)
2297 * end points to the end of the received data buffer
2300 while (!gdbctx
->no_ack_mode
&&
2301 (ptr
= memchr(sum
, '$', end
- sum
)) &&
2302 (sum
= memchr(ptr
, '#', end
- ptr
)) &&
2303 (end
- sum
>= 3) && sscanf(sum
, "#%02x", &cksum
) == 1)
2305 len
= sum
- ptr
- 1;
2308 if (cksum
== checksum(ptr
+ 1, len
))
2310 TRACE("Acking: %s\n", debugstr_an(ptr
, sum
- ptr
));
2311 send(gdbctx
->sock
, "+", 1, 0);
2315 ERR("Nacking: %s (checksum: %d != %d)\n", debugstr_an(ptr
, sum
- ptr
),
2316 cksum
, checksum(ptr
+ 1, len
));
2317 send(gdbctx
->sock
, "-", 1, 0);
2321 while ((ret
& packet_last_f
) == 0 &&
2322 (ptr
= memchr(gdbctx
->in_buf
, '$', gdbctx
->in_len
)) &&
2323 (sum
= memchr(ptr
, '#', end
- ptr
)) &&
2324 (end
- sum
>= 3) && sscanf(sum
, "#%02x", &cksum
) == 1)
2326 if (ptr
!= gdbctx
->in_buf
)
2327 WARN("Ignoring: %s\n", debugstr_an(gdbctx
->in_buf
, ptr
- gdbctx
->in_buf
));
2329 len
= sum
- ptr
- 1;
2332 if (cksum
== checksum(ptr
+ 1, len
))
2334 TRACE("Handling: %s\n", debugstr_an(ptr
, sum
- ptr
));
2337 gdbctx
->in_packet
= ptr
+ 2;
2338 gdbctx
->in_packet_len
= len
- 1;
2339 gdbctx
->in_packet
[gdbctx
->in_packet_len
] = '\0';
2341 for (i
= 0; i
< ARRAY_SIZE(packet_entries
); i
++)
2342 if (packet_entries
[i
].key
== ptr
[1])
2345 if (i
== ARRAY_SIZE(packet_entries
))
2346 WARN("Unhandled: %s\n", debugstr_an(ptr
+ 1, len
));
2347 else if (((ret
= (packet_entries
[i
].handler
)(gdbctx
)) & ~packet_last_f
) == packet_error
)
2348 WARN("Failed: %s\n", debugstr_an(ptr
+ 1, len
));
2350 switch (ret
& ~packet_last_f
)
2352 case packet_error
: packet_reply(gdbctx
, ""); break;
2353 case packet_ok
: packet_reply(gdbctx
, "OK"); break;
2354 case packet_done
: break;
2357 TRACE("Reply: %s\n", debugstr_an((char *)gdbctx
->out_buf
.base
, gdbctx
->out_buf
.len
));
2358 i
= send(gdbctx
->sock
, (char *)gdbctx
->out_buf
.base
, gdbctx
->out_buf
.len
, 0);
2359 assert(i
== gdbctx
->out_buf
.len
);
2360 reply_buffer_clear(&gdbctx
->out_buf
);
2363 WARN("Ignoring: %s (checksum: %d != %d)\n", debugstr_an(ptr
, sum
- ptr
),
2364 cksum
, checksum(ptr
+ 1, len
));
2366 gdbctx
->in_len
= end
- sum
;
2367 memmove(gdbctx
->in_buf
, sum
, end
- sum
);
2368 end
= gdbctx
->in_buf
+ gdbctx
->in_len
;
2371 return (ret
& packet_last_f
);
2374 static int fetch_data(struct gdb_context
* gdbctx
)
2376 int len
, in_len
= gdbctx
->in_len
;
2378 assert(gdbctx
->in_len
<= gdbctx
->in_buf_alloc
);
2382 if (gdbctx
->in_len
+ STEP
> gdbctx
->in_buf_alloc
)
2383 gdbctx
->in_buf
= realloc(gdbctx
->in_buf
, gdbctx
->in_buf_alloc
+= STEP
);
2385 len
= recv(gdbctx
->sock
, gdbctx
->in_buf
+ gdbctx
->in_len
, gdbctx
->in_buf_alloc
- gdbctx
->in_len
- 1, 0);
2386 if (len
<= 0) break;
2387 gdbctx
->in_len
+= len
;
2388 assert(gdbctx
->in_len
<= gdbctx
->in_buf_alloc
);
2389 if (len
< gdbctx
->in_buf_alloc
- gdbctx
->in_len
) break;
2392 gdbctx
->in_buf
[gdbctx
->in_len
] = '\0';
2393 return gdbctx
->in_len
- in_len
;
2396 #define FLAG_NO_START 1
2397 #define FLAG_WITH_XTERM 2
2399 static BOOL
gdb_exec(unsigned port
, unsigned flags
)
2401 WCHAR tmp
[MAX_PATH
], buf
[MAX_PATH
];
2402 const char *argv
[6];
2404 const char *gdb_path
;
2407 if (!(gdb_path
= getenv("WINE_GDB"))) gdb_path
= "gdb";
2408 GetTempPathW( MAX_PATH
, buf
);
2409 GetTempFileNameW( buf
, L
"gdb", 0, tmp
);
2410 if ((f
= _wfopen( tmp
, L
"w+" )) == NULL
) return FALSE
;
2411 unix_tmp
= wine_get_unix_file_name( tmp
);
2412 fprintf(f
, "target remote localhost:%d\n", ntohs(port
));
2413 fprintf(f
, "set prompt Wine-gdb>\\ \n");
2414 /* gdb 5.1 seems to require it, won't hurt anyway */
2415 fprintf(f
, "sharedlibrary\n");
2416 /* This is needed (but not a decent & final fix)
2417 * Without this, gdb would skip our inter-DLL relay code (because
2418 * we don't have any line number information for the relay code)
2419 * With this, we will stop on first instruction of the stub, and
2420 * reusing step, will get us through the relay stub at the actual
2421 * function we're looking at.
2423 fprintf(f
, "set step-mode on\n");
2424 /* tell gdb to delete this file when done handling it... */
2425 fprintf(f
, "shell rm -f \"%s\"\n", unix_tmp
);
2433 if (flags
& FLAG_WITH_XTERM
)
2434 __wine_unix_spawnvp( (char **)argv
, FALSE
);
2436 __wine_unix_spawnvp( (char **)argv
+ 2, FALSE
);
2437 HeapFree( GetProcessHeap(), 0, unix_tmp
);
2441 static BOOL
gdb_startup(struct gdb_context
* gdbctx
, unsigned flags
, unsigned port
)
2444 BOOL reuseaddr
= TRUE
;
2445 struct sockaddr_in s_addrs
= {0};
2446 int s_len
= sizeof(s_addrs
);
2451 WSAStartup( MAKEWORD(2, 2), &data
);
2453 /* step 1: create socket for gdb connection request */
2454 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == INVALID_SOCKET
)
2456 ERR("Failed to create socket: %u\n", WSAGetLastError());
2460 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuseaddr
, sizeof(reuseaddr
));
2462 s_addrs
.sin_family
= AF_INET
;
2463 s_addrs
.sin_addr
.S_un
.S_addr
= INADDR_ANY
;
2464 s_addrs
.sin_port
= htons(port
);
2465 if (bind(sock
, (struct sockaddr
*)&s_addrs
, sizeof(s_addrs
)) == -1)
2468 if (listen(sock
, 1) == -1 || getsockname(sock
, (struct sockaddr
*)&s_addrs
, &s_len
) == -1)
2471 /* step 2: do the process internal creation */
2472 handle_debug_event(gdbctx
, FALSE
);
2474 /* step 3: fire up gdb (if requested) */
2475 if (flags
& FLAG_NO_START
)
2476 fprintf(stderr
, "target remote localhost:%d\n", ntohs(s_addrs
.sin_port
));
2478 gdb_exec(s_addrs
.sin_port
, flags
);
2480 /* step 4: wait for gdb to connect actually */
2481 FD_ZERO( &read_fds
);
2482 FD_SET( sock
, &read_fds
);
2484 if (select( 0, &read_fds
, NULL
, NULL
, NULL
) > 0)
2488 gdbctx
->sock
= accept(sock
, (struct sockaddr
*)&s_addrs
, &s_len
);
2489 if (gdbctx
->sock
!= INVALID_SOCKET
)
2492 TRACE("connected on %Iu\n", gdbctx
->sock
);
2493 /* don't keep our small packets too long: send them ASAP back to GDB
2494 * without this, GDB really crawls
2496 setsockopt(gdbctx
->sock
, IPPROTO_TCP
, TCP_NODELAY
, (char*)&dummy
, sizeof(dummy
));
2499 else ERR("Failed to connect to gdb: %u\n", WSAGetLastError());
2506 static BOOL
gdb_init_context(struct gdb_context
* gdbctx
, unsigned flags
, unsigned port
)
2510 gdbctx
->sock
= INVALID_SOCKET
;
2511 gdbctx
->in_buf
= NULL
;
2512 gdbctx
->in_buf_alloc
= 0;
2514 memset(&gdbctx
->out_buf
, 0, sizeof(gdbctx
->out_buf
));
2515 gdbctx
->out_curr_packet
= -1;
2517 gdbctx
->exec_tid
= -1;
2518 gdbctx
->other_tid
= -1;
2519 list_init(&gdbctx
->xpoint_list
);
2520 gdbctx
->process
= NULL
;
2521 gdbctx
->no_ack_mode
= FALSE
;
2522 for (i
= 0; i
< ARRAY_SIZE(gdbctx
->wine_segs
); i
++)
2523 gdbctx
->wine_segs
[i
] = 0;
2525 gdbctx
->qxfer_object_idx
= -1;
2526 memset(gdbctx
->qxfer_object_annex
, 0, sizeof(gdbctx
->qxfer_object_annex
));
2527 memset(&gdbctx
->qxfer_buffer
, 0, sizeof(gdbctx
->qxfer_buffer
));
2529 /* wait for first trap */
2530 while (WaitForDebugEvent(&gdbctx
->de
, INFINITE
))
2532 if (gdbctx
->de
.dwDebugEventCode
== CREATE_PROCESS_DEBUG_EVENT
)
2534 /* this should be the first event we get,
2535 * and the only one of this type */
2536 assert(gdbctx
->process
== NULL
&& gdbctx
->de
.dwProcessId
== dbg_curr_pid
);
2537 /* gdbctx->dwProcessId = pid; */
2538 if (!gdb_startup(gdbctx
, flags
, port
)) return FALSE
;
2540 else if (!handle_debug_event(gdbctx
, FALSE
))
2542 ContinueDebugEvent(gdbctx
->de
.dwProcessId
, gdbctx
->de
.dwThreadId
, DBG_CONTINUE
);
2547 static int gdb_remote(unsigned flags
, unsigned port
)
2549 struct gdb_context gdbctx
;
2551 if (!gdb_init_context(&gdbctx
, flags
, port
)) return 0;
2552 /* don't handle ctrl-c, but let gdb do the job */
2553 SetConsoleCtrlHandler(NULL
, TRUE
);
2556 fd_set read_fds
, err_fds
;
2558 FD_ZERO( &read_fds
);
2559 FD_ZERO( &err_fds
);
2560 FD_SET( gdbctx
.sock
, &read_fds
);
2561 FD_SET( gdbctx
.sock
, &err_fds
);
2563 if (select( 0, &read_fds
, NULL
, &err_fds
, NULL
) == -1) break;
2565 if (FD_ISSET( gdbctx
.sock
, &err_fds
))
2567 ERR("gdb hung up\n");
2568 /* kill also debuggee process - questionnable - */
2569 detach_debuggee(&gdbctx
, TRUE
);
2572 if (FD_ISSET( gdbctx
.sock
, &read_fds
))
2574 if (fetch_data(&gdbctx
) > 0)
2576 if (extract_packets(&gdbctx
)) break;
2583 int gdb_main(int argc
, char* argv
[])
2585 unsigned gdb_flags
= 0, port
= 0;
2589 while (argc
> 0 && argv
[0][0] == '-')
2591 if (strcmp(argv
[0], "--no-start") == 0)
2593 gdb_flags
|= FLAG_NO_START
;
2597 if (strcmp(argv
[0], "--with-xterm") == 0)
2599 gdb_flags
|= FLAG_WITH_XTERM
;
2603 if (strcmp(argv
[0], "--port") == 0 && argc
> 1)
2605 port
= strtoul(argv
[1], &port_end
, 10);
2608 fprintf(stderr
, "Invalid port: %s\n", argv
[1]);
2611 argc
-= 2; argv
+= 2;
2616 if (dbg_active_attach(argc
, argv
) == start_ok
||
2617 dbg_active_launch(argc
, argv
) == start_ok
)
2618 return gdb_remote(gdb_flags
, port
);