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-2003
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Protocol specification can be found here:
24 * http://sources.redhat.com/gdb/onlinedocs/gdb_32.html
28 #include "wine/port.h"
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
52 /* those two are needed only for the SHOWNORMAL flag */
58 #define GDBPXY_TRC_LOWLEVEL 0x01
59 #define GDBPXY_TRC_PACKET 0x02
60 #define GDBPXY_TRC_COMMAND 0x04
61 #define GDBPXY_TRC_COMMAND_ERROR 0x08
62 #define GDBPXY_TRC_WIN32_EVENT 0x10
63 #define GDBPXY_TRC_WIN32_ERROR 0x20
64 #define GDBPXY_TRC_COMMAND_FIXME 0x80
68 int type; /* -1 means free */
81 /* split into individual packet */
89 /* generic GDB thread information */
90 DBG_THREAD* exec_thread; /* thread used in step & continue */
91 DBG_THREAD* other_thread; /* thread to be used in any other operation */
93 /* current Win32 trap env */
97 /* Win32 information */
100 struct gdb_ctx_Xpoint Xpoints[NUM_XPOINT];
101 /* Unix environment */
102 unsigned long wine_segs[3]; /* load addresses of the ELF wine exec segments (text, bss and data) */
105 extern int read_elf_info(const char* filename, unsigned long tab[]);
107 /* =============================================== *
108 * B A S I C M A N I P U L A T I O N S *
109 * =============================================== *
112 static inline int hex_from0(char ch)
114 if (ch >= '0' && ch <= '9') return ch - '0';
115 if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
116 if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
122 static inline unsigned char hex_to0(int x)
124 assert(x >= 0 && x < 16);
125 return "0123456789abcdef"[x];
128 static void hex_from(void* dst, const char* src, size_t len)
130 unsigned char *p = dst;
133 *p++ = (hex_from0(src[0]) << 4) | hex_from0(src[1]);
138 static void hex_to(char* dst, const void* src, size_t len)
140 const unsigned char *p = src;
143 *dst++ = hex_to0(*p >> 4);
144 *dst++ = hex_to0(*p & 0x0F);
149 static unsigned char checksum(const char* ptr, int len)
154 cksum += (unsigned char)*ptr++;
158 /* =============================================== *
159 * C P U H A N D L E R S *
160 * =============================================== *
163 #define OFFSET_OF(__c,__f) ((int)(((char*)&(((__c*)0)->__f))-((char*)0)))
166 static size_t cpu_register_map[] = {
167 OFFSET_OF(CONTEXT, Eax),
168 OFFSET_OF(CONTEXT, Ecx),
169 OFFSET_OF(CONTEXT, Edx),
170 OFFSET_OF(CONTEXT, Ebx),
171 OFFSET_OF(CONTEXT, Esp),
172 OFFSET_OF(CONTEXT, Ebp),
173 OFFSET_OF(CONTEXT, Esi),
174 OFFSET_OF(CONTEXT, Edi),
175 OFFSET_OF(CONTEXT, Eip),
176 OFFSET_OF(CONTEXT, EFlags),
177 OFFSET_OF(CONTEXT, SegCs),
178 OFFSET_OF(CONTEXT, SegSs),
179 OFFSET_OF(CONTEXT, SegDs),
180 OFFSET_OF(CONTEXT, SegEs),
181 OFFSET_OF(CONTEXT, SegFs),
182 OFFSET_OF(CONTEXT, SegGs),
186 static size_t cpu_register_map[] = {
187 OFFSET_OF(CONTEXT, Gpr0),
188 OFFSET_OF(CONTEXT, Gpr1),
189 OFFSET_OF(CONTEXT, Gpr2),
190 OFFSET_OF(CONTEXT, Gpr3),
191 OFFSET_OF(CONTEXT, Gpr4),
192 OFFSET_OF(CONTEXT, Gpr5),
193 OFFSET_OF(CONTEXT, Gpr6),
194 OFFSET_OF(CONTEXT, Gpr7),
195 OFFSET_OF(CONTEXT, Gpr8),
196 OFFSET_OF(CONTEXT, Gpr9),
197 OFFSET_OF(CONTEXT, Gpr10),
198 OFFSET_OF(CONTEXT, Gpr11),
199 OFFSET_OF(CONTEXT, Gpr12),
200 OFFSET_OF(CONTEXT, Gpr13),
201 OFFSET_OF(CONTEXT, Gpr14),
202 OFFSET_OF(CONTEXT, Gpr15),
203 OFFSET_OF(CONTEXT, Gpr16),
204 OFFSET_OF(CONTEXT, Gpr17),
205 OFFSET_OF(CONTEXT, Gpr18),
206 OFFSET_OF(CONTEXT, Gpr19),
207 OFFSET_OF(CONTEXT, Gpr20),
208 OFFSET_OF(CONTEXT, Gpr21),
209 OFFSET_OF(CONTEXT, Gpr22),
210 OFFSET_OF(CONTEXT, Gpr23),
211 OFFSET_OF(CONTEXT, Gpr24),
212 OFFSET_OF(CONTEXT, Gpr25),
213 OFFSET_OF(CONTEXT, Gpr26),
214 OFFSET_OF(CONTEXT, Gpr27),
215 OFFSET_OF(CONTEXT, Gpr28),
216 OFFSET_OF(CONTEXT, Gpr29),
217 OFFSET_OF(CONTEXT, Gpr30),
218 OFFSET_OF(CONTEXT, Gpr31),
219 OFFSET_OF(CONTEXT, Fpr0),
220 OFFSET_OF(CONTEXT, Fpr1),
221 OFFSET_OF(CONTEXT, Fpr2),
222 OFFSET_OF(CONTEXT, Fpr3),
223 OFFSET_OF(CONTEXT, Fpr4),
224 OFFSET_OF(CONTEXT, Fpr5),
225 OFFSET_OF(CONTEXT, Fpr6),
226 OFFSET_OF(CONTEXT, Fpr7),
227 OFFSET_OF(CONTEXT, Fpr8),
228 OFFSET_OF(CONTEXT, Fpr9),
229 OFFSET_OF(CONTEXT, Fpr10),
230 OFFSET_OF(CONTEXT, Fpr11),
231 OFFSET_OF(CONTEXT, Fpr12),
232 OFFSET_OF(CONTEXT, Fpr13),
233 OFFSET_OF(CONTEXT, Fpr14),
234 OFFSET_OF(CONTEXT, Fpr15),
235 OFFSET_OF(CONTEXT, Fpr16),
236 OFFSET_OF(CONTEXT, Fpr17),
237 OFFSET_OF(CONTEXT, Fpr18),
238 OFFSET_OF(CONTEXT, Fpr19),
239 OFFSET_OF(CONTEXT, Fpr20),
240 OFFSET_OF(CONTEXT, Fpr21),
241 OFFSET_OF(CONTEXT, Fpr22),
242 OFFSET_OF(CONTEXT, Fpr23),
243 OFFSET_OF(CONTEXT, Fpr24),
244 OFFSET_OF(CONTEXT, Fpr25),
245 OFFSET_OF(CONTEXT, Fpr26),
246 OFFSET_OF(CONTEXT, Fpr27),
247 OFFSET_OF(CONTEXT, Fpr28),
248 OFFSET_OF(CONTEXT, Fpr29),
249 OFFSET_OF(CONTEXT, Fpr30),
250 OFFSET_OF(CONTEXT, Fpr31),
252 OFFSET_OF(CONTEXT, Iar),
253 OFFSET_OF(CONTEXT, Msr),
254 OFFSET_OF(CONTEXT, Cr),
255 OFFSET_OF(CONTEXT, Lr),
256 OFFSET_OF(CONTEXT, Ctr),
257 OFFSET_OF(CONTEXT, Xer),
258 /* FIXME: MQ is missing? OFFSET_OF(CONTEXT, Mq), */
259 /* see gdb/nlm/ppc.c */
262 # error "Define the registers map for your CPU"
267 static const size_t cpu_num_regs = (sizeof(cpu_register_map) / sizeof(cpu_register_map[0]));
269 static inline unsigned long* cpu_register(const CONTEXT* ctx, unsigned idx)
271 assert(idx < cpu_num_regs);
272 return (unsigned long*)((char*)ctx + cpu_register_map[idx]);
275 static inline BOOL cpu_enter_stepping(struct gdb_context* gdbctx)
278 gdbctx->context.EFlags |= 0x100;
282 # define MSR_SE (1<<10)
284 gdbctx->context.Msr |= MSR_SE;
287 #error "Define step mode enter for your CPU"
292 static inline BOOL cpu_leave_stepping(struct gdb_context* gdbctx)
295 /* The Win32 debug API always resets the Step bit in EFlags after
296 * a single step instruction, so we don't need to clear when the
301 gdbctx->context.Msr &= MSR_SE;
304 #error "Define step mode leave for your CPU"
310 #define DR7_CONTROL_SHIFT 16
311 #define DR7_CONTROL_SIZE 4
313 #define DR7_RW_EXECUTE (0x0)
314 #define DR7_RW_WRITE (0x1)
315 #define DR7_RW_READ (0x3)
317 #define DR7_LEN_1 (0x0)
318 #define DR7_LEN_2 (0x4)
319 #define DR7_LEN_4 (0xC)
321 #define DR7_LOCAL_ENABLE_SHIFT 0
322 #define DR7_GLOBAL_ENABLE_SHIFT 1
323 #define DR7_ENABLE_SIZE 2
325 #define DR7_LOCAL_ENABLE_MASK (0x55)
326 #define DR7_GLOBAL_ENABLE_MASK (0xAA)
328 #define DR7_CONTROL_RESERVED (0xFC00)
329 #define DR7_LOCAL_SLOWDOWN (0x100)
330 #define DR7_GLOBAL_SLOWDOWN (0x200)
332 #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
333 #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
335 static inline int i386_get_unused_DR(struct gdb_context* gdbctx,
338 if (!IS_DR7_SET(gdbctx->context.Dr7, 0))
340 *r = &gdbctx->context.Dr0;
343 if (!IS_DR7_SET(gdbctx->context.Dr7, 1))
345 *r = &gdbctx->context.Dr1;
348 if (!IS_DR7_SET(gdbctx->context.Dr7, 2))
350 *r = &gdbctx->context.Dr2;
353 if (!IS_DR7_SET(gdbctx->context.Dr7, 3))
355 *r = &gdbctx->context.Dr3;
362 /******************************************************************
367 * -1 if operation isn't supported by CPU
369 static inline int cpu_insert_Xpoint(struct gdb_context* gdbctx,
370 struct gdb_ctx_Xpoint* xpt, size_t len)
382 if (len != 1) return 0;
383 if (!ReadProcessMemory(gdbctx->process->handle, xpt->addr, &ch, 1, &sz) || sz != 1) return 0;
386 if (!WriteProcessMemory(gdbctx->process->handle, xpt->addr, &ch, 1, &sz) || sz != 1) return 0;
389 bits = DR7_RW_EXECUTE;
397 if ((reg = i386_get_unused_DR(gdbctx, &pr)) == -1) return 0;
398 *pr = (unsigned long)xpt->addr;
399 if (xpt->type != '1') switch (len)
401 case 4: bits |= DR7_LEN_4; break;
402 case 2: bits |= DR7_LEN_2; break;
403 case 1: bits |= DR7_LEN_1; break;
407 /* clear old values */
408 gdbctx->context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
409 /* set the correct ones */
410 gdbctx->context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
411 gdbctx->context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
414 fprintf(stderr, "Unknown bp type %c\n", xpt->type);
418 #elif defined(__powerpc__)
425 if (len != 4) return 0;
426 if (!ReadProcessMemory(gdbctx->process->handle, xpt->addr, &xbp, 4, &sz) || sz != 4) return 0;
428 xbp = 0x7d821008; /* 7d 82 10 08 ... in big endian */
429 if (!WriteProcessMemory(gdbctx->process->handle, xpt->addr, &xbp, 4, &sz) || sz != 4) return 0;
432 fprintf(stderr, "Unknown/unsupported bp type %c\n", xpt->type);
437 #error "Define insert Xpoint for your CPU"
442 /******************************************************************
447 * -1 if operation isn't supported by CPU
449 static inline BOOL cpu_remove_Xpoint(struct gdb_context* gdbctx,
450 struct gdb_ctx_Xpoint* xpt, size_t len)
459 if (len != 1) return 0;
460 ch = (unsigned char)xpt->val;
461 if (!WriteProcessMemory(gdbctx->process->handle, xpt->addr, &ch, 1, &sz) || sz != 1) return 0;
466 /* simply disable the entry */
467 gdbctx->context.Dr7 &= ~DR7_ENABLE_MASK(xpt->val);
470 fprintf(stderr, "Unknown bp type %c\n", xpt->type);
474 #elif defined(__powerpc__)
481 if (len != 4) return 0;
483 if (!WriteProcessMemory(gdbctx->process->handle, xpt->addr, &xbp, 4, &sz) || sz != 4) return 0;
489 fprintf(stderr, "Unknown/unsupported bp type %c\n", xpt->type);
494 #error "Define remove Xpoint for your CPU"
498 /* =============================================== *
499 * W I N 3 2 D E B U G I N T E R F A C E *
500 * =============================================== *
503 static BOOL fetch_context(struct gdb_context* gdbctx, HANDLE h, CONTEXT* ctx)
505 ctx->ContextFlags = CONTEXT_CONTROL
507 #ifdef CONTEXT_SEGMENTS
510 #ifdef CONTEXT_DEBUG_REGISTERS
511 | CONTEXT_DEBUG_REGISTERS
514 if (!GetThreadContext(h, ctx))
516 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
517 fprintf(stderr, "Can't get thread's context\n");
523 static BOOL handle_exception(struct gdb_context* gdbctx, EXCEPTION_DEBUG_INFO* exc)
525 EXCEPTION_RECORD* rec = &exc->ExceptionRecord;
528 switch (rec->ExceptionCode)
530 case EXCEPTION_ACCESS_VIOLATION:
531 case EXCEPTION_PRIV_INSTRUCTION:
532 case EXCEPTION_STACK_OVERFLOW:
533 case EXCEPTION_GUARD_PAGE:
534 gdbctx->last_sig = SIGSEGV;
537 case EXCEPTION_DATATYPE_MISALIGNMENT:
538 gdbctx->last_sig = SIGBUS;
541 case EXCEPTION_SINGLE_STEP:
543 case EXCEPTION_BREAKPOINT:
544 gdbctx->last_sig = SIGTRAP;
547 case EXCEPTION_FLT_DENORMAL_OPERAND:
548 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
549 case EXCEPTION_FLT_INEXACT_RESULT:
550 case EXCEPTION_FLT_INVALID_OPERATION:
551 case EXCEPTION_FLT_OVERFLOW:
552 case EXCEPTION_FLT_STACK_CHECK:
553 case EXCEPTION_FLT_UNDERFLOW:
554 gdbctx->last_sig = SIGFPE;
557 case EXCEPTION_INT_DIVIDE_BY_ZERO:
558 case EXCEPTION_INT_OVERFLOW:
559 gdbctx->last_sig = SIGFPE;
562 case EXCEPTION_ILLEGAL_INSTRUCTION:
563 gdbctx->last_sig = SIGILL;
567 gdbctx->last_sig = SIGINT;
570 case STATUS_POSSIBLE_DEADLOCK:
571 gdbctx->last_sig = SIGALRM;
573 /* FIXME: we could also add here a O packet with additional information */
576 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
577 fprintf(stderr, "Unhandled exception code %08lx\n", rec->ExceptionCode);
578 gdbctx->last_sig = SIGABRT;
585 static void handle_debug_event(struct gdb_context* gdbctx, DEBUG_EVENT* de)
589 DEBUG_CurrThread = DEBUG_GetThread(gdbctx->process, de->dwThreadId);
591 switch (de->dwDebugEventCode)
593 case CREATE_PROCESS_DEBUG_EVENT:
594 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
595 de->u.CreateProcessInfo.hProcess,
596 de->u.CreateProcessInfo.lpImageName,
597 de->u.CreateProcessInfo.fUnicode);
599 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
600 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
601 fprintf(stderr, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
602 de->dwProcessId, de->dwThreadId,
603 buffer, de->u.CreateProcessInfo.lpImageName,
604 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
605 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
606 de->u.CreateProcessInfo.nDebugInfoSize);
608 gdbctx->process = DEBUG_AddProcess(de->dwProcessId,
609 de->u.CreateProcessInfo.hProcess,
611 /* de->u.CreateProcessInfo.lpStartAddress; */
613 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
614 fprintf(stderr, "%08lx:%08lx: create thread I @%08lx\n",
615 de->dwProcessId, de->dwThreadId,
616 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
618 assert(DEBUG_CurrThread == NULL); /* shouldn't be there */
619 DEBUG_AddThread(gdbctx->process, de->dwThreadId,
620 de->u.CreateProcessInfo.hThread,
621 de->u.CreateProcessInfo.lpStartAddress,
622 de->u.CreateProcessInfo.lpThreadLocalBase);
624 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
625 de->u.CreateProcessInfo.lpBaseOfImage);
627 if (buffer[0]) /* we got a process name */
630 if (!GetBinaryTypeA( buffer, &type ))
632 /* not a Windows binary, assume it's a Unix executable then */
633 char unixname[MAX_PATH];
634 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
635 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
637 DEBUG_ReadExecutableDbgInfo( unixname );
642 /* if it is a Windows binary, or an invalid or missing file name,
643 * we use wine itself as the main executable */
644 DEBUG_ReadExecutableDbgInfo( "wine" );
648 case LOAD_DLL_DEBUG_EVENT:
649 assert(DEBUG_CurrThread);
650 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
651 gdbctx->process->handle,
652 de->u.LoadDll.lpImageName,
653 de->u.LoadDll.fUnicode);
655 /* FIXME unicode: de->u.LoadDll.fUnicode */
656 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
657 fprintf(stderr, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
658 de->dwProcessId, de->dwThreadId,
659 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
660 de->u.LoadDll.dwDebugInfoFileOffset,
661 de->u.LoadDll.nDebugInfoSize);
664 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, de->u.LoadDll.lpBaseOfDll);
665 DEBUG_CheckDelayedBP();
666 if (DBG_IVAR(BreakOnDllLoad))
668 DEBUG_Printf("Stopping on DLL %s loading at %08lx\n",
669 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
675 case UNLOAD_DLL_DEBUG_EVENT:
676 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
677 fprintf(stderr, "%08lx:%08lx: unload DLL @%08lx\n",
678 de->dwProcessId, de->dwThreadId, (unsigned long)de->u.UnloadDll.lpBaseOfDll);
681 case EXCEPTION_DEBUG_EVENT:
682 assert(DEBUG_CurrThread);
683 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
684 fprintf(stderr, "%08lx:%08lx: exception code=%08lx\n",
685 de->dwProcessId, de->dwThreadId,
686 de->u.Exception.ExceptionRecord.ExceptionCode);
688 if (fetch_context(gdbctx, DEBUG_CurrThread->handle, &gdbctx->context))
690 gdbctx->in_trap = handle_exception(gdbctx, &de->u.Exception);
694 case CREATE_THREAD_DEBUG_EVENT:
695 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
696 fprintf(stderr, "%08lx:%08lx: create thread D @%08lx\n",
697 de->dwProcessId, de->dwThreadId, (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
699 DEBUG_AddThread(gdbctx->process,
701 de->u.CreateThread.hThread,
702 de->u.CreateThread.lpStartAddress,
703 de->u.CreateThread.lpThreadLocalBase);
706 case EXIT_THREAD_DEBUG_EVENT:
707 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
708 fprintf(stderr, "%08lx:%08lx: exit thread (%ld)\n",
709 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
711 assert(DEBUG_CurrThread);
712 if (DEBUG_CurrThread == gdbctx->exec_thread) gdbctx->exec_thread = NULL;
713 if (DEBUG_CurrThread == gdbctx->other_thread) gdbctx->other_thread = NULL;
714 DEBUG_DelThread(DEBUG_CurrThread);
717 case EXIT_PROCESS_DEBUG_EVENT:
718 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
719 fprintf(stderr, "%08lx:%08lx: exit process (%ld)\n",
720 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
722 DEBUG_DelProcess(gdbctx->process);
723 gdbctx->process = NULL;
724 /* now signal gdb that we're done */
725 gdbctx->last_sig = SIGTERM;
726 gdbctx->in_trap = TRUE;
729 case OUTPUT_DEBUG_STRING_EVENT:
730 assert(DEBUG_CurrThread);
731 DEBUG_ProcessGetString(buffer, sizeof(buffer),
732 gdbctx->process->handle,
733 de->u.DebugString.lpDebugStringData);
734 /* FIXME unicode de->u.DebugString.fUnicode ? */
735 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
736 fprintf(stderr, "%08lx:%08lx: output debug string (%s)\n",
737 de->dwProcessId, de->dwThreadId, buffer);
741 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
742 fprintf(stderr, "%08lx:%08lx: rip error=%ld type=%ld\n",
743 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
744 de->u.RipInfo.dwType);
748 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
749 fprintf(stderr, "%08lx:%08lx: unknown event (%ld)\n",
750 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
754 static void resume_debuggee(struct gdb_context* gdbctx, unsigned long cont)
756 if (DEBUG_CurrThread)
758 if (!SetThreadContext(DEBUG_CurrThread->handle, &gdbctx->context))
759 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
760 fprintf(stderr, "Cannot set context on thread %lu\n", DEBUG_CurrThread->tid);
761 if (!ContinueDebugEvent(gdbctx->process->pid, DEBUG_CurrThread->tid, cont))
762 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
763 fprintf(stderr, "Cannot continue on %lu (%lu)\n",
764 DEBUG_CurrThread->tid, cont);
766 else if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
767 fprintf(stderr, "Cannot find last thread (%lu)\n", DEBUG_CurrThread->tid);
770 static void wait_for_debuggee(struct gdb_context* gdbctx)
774 gdbctx->in_trap = FALSE;
775 while (WaitForDebugEvent(&de, INFINITE))
777 handle_debug_event(gdbctx, &de);
778 assert(!gdbctx->process ||
779 gdbctx->process->pid == 0 ||
780 de.dwProcessId == gdbctx->process->pid);
781 assert(!DEBUG_CurrThread || de.dwThreadId == DEBUG_CurrThread->tid);
782 if (gdbctx->in_trap) break;
783 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
787 static void detach_debuggee(struct gdb_context* gdbctx, BOOL kill)
789 cpu_leave_stepping(gdbctx);
790 resume_debuggee(gdbctx, DBG_CONTINUE);
792 DebugActiveProcessStop(gdbctx->process->pid);
793 DEBUG_DelProcess(gdbctx->process);
794 gdbctx->process = NULL;
797 static void get_process_info(struct gdb_context* gdbctx, char* buffer, size_t len)
799 unsigned long status;
801 if (!GetExitCodeProcess(gdbctx->process->handle, &status))
803 strcpy(buffer, "Unknown process");
806 if (status == STILL_ACTIVE)
808 strcpy(buffer, "Running");
811 snprintf(buffer, len, "Terminated (%lu)", status);
813 switch (GetPriorityClass(gdbctx->process->handle))
816 #ifdef ABOVE_NORMAL_PRIORITY_CLASS
817 case ABOVE_NORMAL_PRIORITY_CLASS: strcat(buffer, ", above normal priority"); break;
819 #ifdef BELOW_NORMAL_PRIORITY_CLASS
820 case BELOW_NORMAL_PRIORITY_CLASS: strcat(buffer, ", below normal priotity"); break;
822 case HIGH_PRIORITY_CLASS: strcat(buffer, ", high priority"); break;
823 case IDLE_PRIORITY_CLASS: strcat(buffer, ", idle priority"); break;
824 case NORMAL_PRIORITY_CLASS: strcat(buffer, ", normal priority"); break;
825 case REALTIME_PRIORITY_CLASS: strcat(buffer, ", realtime priority"); break;
827 strcat(buffer, "\n");
830 static void get_thread_info(struct gdb_context* gdbctx, unsigned tid,
831 char* buffer, size_t len)
834 unsigned long status;
837 /* FIXME: use the size of buffer */
838 thd = DEBUG_GetThread(gdbctx->process, tid);
841 strcpy(buffer, "No information");
844 if (GetExitCodeThread(thd->handle, &status))
846 if (status == STILL_ACTIVE)
848 /* FIXME: this is a bit brutal... some nicer way shall be found */
849 switch (status = SuspendThread(thd->handle))
852 case 0: strcpy(buffer, "Running"); break;
853 default: snprintf(buffer, len, "Suspended (%lu)", status - 1);
855 ResumeThread(thd->handle);
858 snprintf(buffer, len, "Terminated (exit code = %lu)", status);
862 strcpy(buffer, "Unknown threadID");
864 switch (prio = GetThreadPriority(thd->handle))
866 case THREAD_PRIORITY_ERROR_RETURN: break;
867 case THREAD_PRIORITY_ABOVE_NORMAL: strcat(buffer, ", priority +1 above normal"); break;
868 case THREAD_PRIORITY_BELOW_NORMAL: strcat(buffer, ", priority -1 below normal"); break;
869 case THREAD_PRIORITY_HIGHEST: strcat(buffer, ", priority +2 above normal"); break;
870 case THREAD_PRIORITY_LOWEST: strcat(buffer, ", priority -2 below normal"); break;
871 case THREAD_PRIORITY_IDLE: strcat(buffer, ", priority idle"); break;
872 case THREAD_PRIORITY_NORMAL: strcat(buffer, ", priority normal"); break;
873 case THREAD_PRIORITY_TIME_CRITICAL: strcat(buffer, ", priority time-critical"); break;
874 default: snprintf(buffer + strlen(buffer), len - strlen(buffer), ", priority = %d", prio);
876 assert(strlen(buffer) < len);
879 /* =============================================== *
880 * P A C K E T U T I L S *
881 * =============================================== *
884 enum packet_return {packet_error = 0x00, packet_ok = 0x01, packet_done = 0x02,
885 packet_last_f = 0x80};
887 static void packet_reply_grow(struct gdb_context* gdbctx, size_t size)
889 if (gdbctx->out_buf_alloc < gdbctx->out_len + size)
891 gdbctx->out_buf_alloc = ((gdbctx->out_len + size) / 32 + 1) * 32;
892 gdbctx->out_buf = realloc(gdbctx->out_buf, gdbctx->out_buf_alloc);
896 static void packet_reply_hex_to(struct gdb_context* gdbctx, const void* src, int len)
898 packet_reply_grow(gdbctx, len * 2);
899 hex_to(&gdbctx->out_buf[gdbctx->out_len], src, len);
900 gdbctx->out_len += len * 2;
903 static inline void packet_reply_hex_to_str(struct gdb_context* gdbctx, const char* src)
905 packet_reply_hex_to(gdbctx, src, strlen(src));
908 static void packet_reply_val(struct gdb_context* gdbctx, unsigned long val, int len)
912 shift = (len - 1) * 8;
913 packet_reply_grow(gdbctx, len * 2);
914 for (i = 0; i < len; i++, shift -= 8)
916 gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> (shift + 4)) & 0x0F);
917 gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> shift ) & 0x0F);
921 static inline void packet_reply_add(struct gdb_context* gdbctx, const char* str, int len)
923 packet_reply_grow(gdbctx, len);
924 memcpy(&gdbctx->out_buf[gdbctx->out_len], str, len);
925 gdbctx->out_len += len;
928 static inline void packet_reply_cat(struct gdb_context* gdbctx, const char* str)
930 packet_reply_add(gdbctx, str, strlen(str));
933 static inline void packet_reply_catc(struct gdb_context* gdbctx, char ch)
935 packet_reply_add(gdbctx, &ch, 1);
938 static void packet_reply_open(struct gdb_context* gdbctx)
940 assert(gdbctx->out_curr_packet == -1);
941 packet_reply_catc(gdbctx, '$');
942 gdbctx->out_curr_packet = gdbctx->out_len;
945 static void packet_reply_close(struct gdb_context* gdbctx)
950 plen = gdbctx->out_len - gdbctx->out_curr_packet;
951 packet_reply_catc(gdbctx, '#');
952 cksum = checksum(&gdbctx->out_buf[gdbctx->out_curr_packet], plen);
953 packet_reply_hex_to(gdbctx, &cksum, 1);
954 if (gdbctx->trace & GDBPXY_TRC_PACKET)
955 fprintf(stderr, "Reply : %*.*s\n",
956 plen, plen, &gdbctx->out_buf[gdbctx->out_curr_packet]);
957 gdbctx->out_curr_packet = -1;
960 static enum packet_return packet_reply(struct gdb_context* gdbctx, const char* packet, int len)
962 packet_reply_open(gdbctx);
964 if (len == -1) len = strlen(packet);
965 assert(memchr(packet, '$', len) == NULL && memchr(packet, '#', len) == NULL);
967 packet_reply_add(gdbctx, packet, len);
969 packet_reply_close(gdbctx);
974 static enum packet_return packet_reply_error(struct gdb_context* gdbctx, int error)
976 packet_reply_open(gdbctx);
978 packet_reply_add(gdbctx, "E", 1);
979 packet_reply_val(gdbctx, error, 1);
981 packet_reply_close(gdbctx);
986 /* =============================================== *
987 * P A C K E T H A N D L E R S *
988 * =============================================== *
991 static enum packet_return packet_reply_status(struct gdb_context* gdbctx)
993 enum packet_return ret = packet_done;
995 packet_reply_open(gdbctx);
997 if (gdbctx->process != NULL)
1002 packet_reply_catc(gdbctx, 'T');
1003 sig = gdbctx->last_sig;
1004 packet_reply_val(gdbctx, sig, 1);
1005 packet_reply_add(gdbctx, "thread:", 7);
1006 packet_reply_val(gdbctx, DEBUG_CurrThread->tid, 4);
1007 packet_reply_catc(gdbctx, ';');
1009 for (i = 0; i < cpu_num_regs; i++)
1011 /* FIXME: this call will also grow the buffer...
1012 * unneeded, but not harmful
1014 packet_reply_val(gdbctx, i, 1);
1015 packet_reply_catc(gdbctx, ':');
1016 packet_reply_hex_to(gdbctx, cpu_register(&gdbctx->context, i), 4);
1017 packet_reply_catc(gdbctx, ';');
1022 /* Try to put an exit code
1023 * Cannot use GetExitCodeProcess, wouldn't fit in a 8 bit value, so
1024 * just indicate the end of process and exit */
1025 packet_reply_add(gdbctx, "W00", 3);
1026 /*if (!gdbctx->extended)*/ ret |= packet_last_f;
1029 packet_reply_close(gdbctx);
1035 static enum packet_return packet_extended(struct gdb_context* gdbctx)
1037 gdbctx->extended = 1;
1042 static enum packet_return packet_last_signal(struct gdb_context* gdbctx)
1044 assert(gdbctx->in_packet_len == 0);
1045 return packet_reply_status(gdbctx);
1048 static enum packet_return packet_continue(struct gdb_context* gdbctx)
1050 /* FIXME: add support for address in packet */
1051 assert(gdbctx->in_packet_len == 0);
1052 if (DEBUG_CurrThread != gdbctx->exec_thread && gdbctx->exec_thread)
1053 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
1054 fprintf(stderr, "NIY: cont on %lu, while last thread is %lu\n",
1055 gdbctx->exec_thread->tid, DEBUG_CurrThread->tid);
1056 resume_debuggee(gdbctx, DBG_CONTINUE);
1057 wait_for_debuggee(gdbctx);
1058 return packet_reply_status(gdbctx);
1061 static enum packet_return packet_continue_signal(struct gdb_context* gdbctx)
1065 /* FIXME: add support for address in packet */
1066 assert(gdbctx->in_packet_len == 2);
1067 if (DEBUG_CurrThread != gdbctx->exec_thread && gdbctx->exec_thread)
1068 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
1069 fprintf(stderr, "NIY: cont/sig on %lu, while last thread is %lu\n",
1070 gdbctx->exec_thread->tid, DEBUG_CurrThread->tid);
1071 hex_from(&sig, gdbctx->in_packet, 1);
1072 /* cannot change signals on the fly */
1073 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1074 fprintf(stderr, "sigs: %u %u\n", sig, gdbctx->last_sig);
1075 if (sig != gdbctx->last_sig)
1076 return packet_error;
1077 resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
1078 wait_for_debuggee(gdbctx);
1079 return packet_reply_status(gdbctx);
1082 static enum packet_return packet_detach(struct gdb_context* gdbctx)
1084 detach_debuggee(gdbctx, FALSE);
1085 return packet_ok | packet_last_f;
1088 static enum packet_return packet_read_registers(struct gdb_context* gdbctx)
1092 CONTEXT* pctx = &gdbctx->context;
1094 assert(gdbctx->in_trap);
1096 if (DEBUG_CurrThread != gdbctx->other_thread && gdbctx->other_thread)
1098 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
1099 return packet_error;
1102 packet_reply_open(gdbctx);
1103 for (i = 0; i < cpu_num_regs; i++)
1105 packet_reply_hex_to(gdbctx, cpu_register(pctx, i), 4);
1107 packet_reply_close(gdbctx);
1111 static enum packet_return packet_write_registers(struct gdb_context* gdbctx)
1115 CONTEXT* pctx = &gdbctx->context;
1117 assert(gdbctx->in_trap);
1118 if (DEBUG_CurrThread != gdbctx->other_thread && gdbctx->other_thread)
1120 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
1121 return packet_error;
1123 if (gdbctx->in_packet_len < cpu_num_regs * 2) return packet_error;
1125 for (i = 0; i < cpu_num_regs; i++)
1126 hex_from(cpu_register(pctx, i), &gdbctx->in_packet[8 * i], 4);
1127 if (pctx != &gdbctx->context && !SetThreadContext(gdbctx->other_thread->handle, pctx))
1129 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
1130 fprintf(stderr, "Cannot set context on thread %lu\n", gdbctx->other_thread->tid);
1131 return packet_error;
1136 static enum packet_return packet_kill(struct gdb_context* gdbctx)
1138 detach_debuggee(gdbctx, TRUE);
1140 if (!gdbctx->extended)
1141 /* dunno whether GDB cares or not */
1145 /* assume we can't really answer something here */
1146 /* return packet_done; */
1149 static enum packet_return packet_thread(struct gdb_context* gdbctx)
1154 switch (gdbctx->in_packet[0])
1158 if (gdbctx->in_packet[1] == '-')
1159 thread = -strtol(gdbctx->in_packet + 2, &end, 16);
1161 thread = strtol(gdbctx->in_packet + 1, &end, 16);
1162 if (end == NULL || end > gdbctx->in_packet + gdbctx->in_packet_len)
1164 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1165 fprintf(stderr, "Cannot get threadid %*.*s\n",
1166 gdbctx->in_packet_len - 1, gdbctx->in_packet_len - 1,
1167 gdbctx->in_packet + 1);
1168 return packet_error;
1170 if (gdbctx->in_packet[0] == 'c')
1171 gdbctx->exec_thread = DEBUG_GetThread(gdbctx->process, thread);
1173 gdbctx->other_thread = DEBUG_GetThread(gdbctx->process, thread);
1176 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1177 fprintf(stderr, "Unknown thread sub-command %c\n", gdbctx->in_packet[0]);
1178 return packet_error;
1182 static enum packet_return packet_read_memory(struct gdb_context* gdbctx)
1185 size_t len, blk_len, nread;
1187 unsigned long r = 0;
1189 assert(gdbctx->in_trap);
1190 /* FIXME:check in_packet_len for reading %p,%x */
1191 if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2) return packet_error;
1192 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1193 fprintf(stderr, "Read mem at %p for %u bytes\n", addr, len);
1194 for (nread = 0; nread < len > 0; nread += r, addr += r)
1196 blk_len = min(sizeof(buffer), len - nread);
1197 if (!ReadProcessMemory(gdbctx->process->handle, addr, buffer, blk_len, &r) ||
1200 /* fail at first address, return error */
1201 if (nread == 0) return packet_reply_error(gdbctx, EFAULT);
1202 /* something has already been read, return partial information */
1205 if (nread == 0) packet_reply_open(gdbctx);
1206 packet_reply_hex_to(gdbctx, buffer, r);
1208 packet_reply_close(gdbctx);
1212 static enum packet_return packet_write_memory(struct gdb_context* gdbctx)
1215 size_t len, blk_len;
1220 assert(gdbctx->in_trap);
1221 ptr = memchr(gdbctx->in_packet, ':', gdbctx->in_packet_len);
1224 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1225 fprintf(stderr, "Cannot find ':' in %*.*s\n",
1226 gdbctx->in_packet_len, gdbctx->in_packet_len, gdbctx->in_packet);
1227 return packet_error;
1231 if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2)
1233 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1234 fprintf(stderr, "Cannot scan addr,len in %s\n", gdbctx->in_packet);
1235 return packet_error;
1237 if (ptr - gdbctx->in_packet + len * 2 != gdbctx->in_packet_len)
1239 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1240 fprintf(stderr, "Wrong sizes %u <> %u\n",
1241 ptr - gdbctx->in_packet + len * 2, gdbctx->in_packet_len);
1242 return packet_error;
1244 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1245 fprintf(stderr, "Write %u bytes at %p\n", len, addr);
1248 blk_len = min(sizeof(buffer), len);
1249 hex_from(buffer, ptr, blk_len);
1253 ret = WriteProcessMemory(gdbctx->process->handle, addr, buffer, blk_len, &w);
1254 if (!ret || w != blk_len)
1261 return packet_ok; /* FIXME: error while writing ? */
1264 static enum packet_return packet_write_register(struct gdb_context* gdbctx)
1270 CONTEXT* pctx = &gdbctx->context;
1272 assert(gdbctx->in_trap);
1274 ptr = memchr(gdbctx->in_packet, '=', gdbctx->in_packet_len);
1276 reg = strtoul(gdbctx->in_packet, &end, 16);
1277 if (end == NULL || reg > cpu_num_regs)
1279 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1280 fprintf(stderr, "Invalid register index %s\n", gdbctx->in_packet);
1281 /* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
1282 * it wouldn't matter too much, and it fakes our support for all regs
1284 return (end == NULL) ? packet_error : packet_ok;
1286 if (ptr + 8 - gdbctx->in_packet != gdbctx->in_packet_len)
1288 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1289 fprintf(stderr, "Wrong sizes %u <> %u\n",
1290 ptr + 8 - gdbctx->in_packet, gdbctx->in_packet_len);
1291 return packet_error;
1293 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1294 fprintf(stderr, "Writing reg %u <= %*.*s\n",
1295 reg, gdbctx->in_packet_len - (ptr - gdbctx->in_packet),
1296 gdbctx->in_packet_len - (ptr - gdbctx->in_packet), ptr);
1298 if (DEBUG_CurrThread != gdbctx->other_thread && gdbctx->other_thread)
1300 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
1301 return packet_error;
1304 hex_from(cpu_register(pctx, reg), ptr, 4);
1305 if (pctx != &gdbctx->context && !SetThreadContext(gdbctx->other_thread->handle, pctx))
1307 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
1308 fprintf(stderr, "Cannot set context for thread %lu\n", gdbctx->other_thread->tid);
1309 return packet_error;
1315 static void packet_query_monitor_wnd_helper(struct gdb_context* gdbctx, HWND hWnd, int indent)
1323 if (!GetClassName(hWnd, clsName, sizeof(clsName)))
1324 strcpy(clsName, "-- Unknown --");
1325 if (!GetWindowText(hWnd, wndName, sizeof(wndName)))
1326 strcpy(wndName, "-- Empty --");
1328 packet_reply_open(gdbctx);
1329 packet_reply_catc(gdbctx, 'O');
1330 snprintf(buffer, sizeof(buffer),
1331 "%*s%04x%*s%-17.17s %08lx %08lx %.14s\n",
1332 indent, "", (UINT)hWnd, 13 - indent, "",
1333 clsName, GetWindowLong(hWnd, GWL_STYLE),
1334 GetWindowLong(hWnd, GWL_WNDPROC), wndName);
1335 packet_reply_hex_to_str(gdbctx, buffer);
1336 packet_reply_close(gdbctx);
1338 if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
1339 packet_query_monitor_wnd_helper(gdbctx, child, indent + 1);
1340 } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
1343 static void packet_query_monitor_wnd(struct gdb_context* gdbctx, int len, const char* str)
1347 /* we do the output in several 'O' packets, with the last one being just OK for
1348 * marking the end of the output */
1349 packet_reply_open(gdbctx);
1350 packet_reply_catc(gdbctx, 'O');
1351 snprintf(buffer, sizeof(buffer),
1352 "%-16.16s %-17.17s %-8.8s %s\n",
1353 "hwnd", "Class Name", " Style", " WndProc Text");
1354 packet_reply_hex_to_str(gdbctx, buffer);
1355 packet_reply_close(gdbctx);
1357 /* FIXME: could also add a pmt to this command in str... */
1358 packet_query_monitor_wnd_helper(gdbctx, GetDesktopWindow(), 0);
1359 packet_reply(gdbctx, "OK", 2);
1362 static void packet_query_monitor_process(struct gdb_context* gdbctx, int len, const char* str)
1364 HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
1367 PROCESSENTRY32 entry;
1370 if (snap == INVALID_HANDLE_VALUE)
1373 entry.dwSize = sizeof(entry);
1374 ok = Process32First( snap, &entry );
1376 /* we do the output in several 'O' packets, with the last one being just OK for
1377 * marking the end of the output */
1379 packet_reply_open(gdbctx);
1380 packet_reply_catc(gdbctx, 'O');
1381 snprintf(buffer, sizeof(buffer),
1382 " %-8.8s %-8.8s %-8.8s %s\n",
1383 "pid", "threads", "parent", "executable" );
1384 packet_reply_hex_to_str(gdbctx, buffer);
1385 packet_reply_close(gdbctx);
1390 if (entry.th32ProcessID == gdbctx->process->pid) deco = '>';
1391 packet_reply_open(gdbctx);
1392 packet_reply_catc(gdbctx, 'O');
1393 snprintf(buffer, sizeof(buffer),
1394 "%c%08lx %-8ld %08lx '%s'\n",
1395 deco, entry.th32ProcessID, entry.cntThreads,
1396 entry.th32ParentProcessID, entry.szExeFile);
1397 packet_reply_hex_to_str(gdbctx, buffer);
1398 packet_reply_close(gdbctx);
1399 ok = Process32Next(snap, &entry);
1402 packet_reply(gdbctx, "OK", 2);
1405 static void packet_query_monitor_mem(struct gdb_context* gdbctx, int len, const char* str)
1407 MEMORY_BASIC_INFORMATION mbi;
1414 /* we do the output in several 'O' packets, with the last one being just OK for
1415 * marking the end of the output */
1416 packet_reply_open(gdbctx);
1417 packet_reply_catc(gdbctx, 'O');
1418 packet_reply_hex_to_str(gdbctx, "Address Size State Type RWX\n");
1419 packet_reply_close(gdbctx);
1421 while (VirtualQueryEx(gdbctx->process->handle, addr, &mbi, sizeof(mbi)) >= sizeof(mbi))
1425 case MEM_COMMIT: state = "commit "; break;
1426 case MEM_FREE: state = "free "; break;
1427 case MEM_RESERVE: state = "reserve"; break;
1428 default: state = "??? "; break;
1430 if (mbi.State != MEM_FREE)
1434 case MEM_IMAGE: type = "image "; break;
1435 case MEM_MAPPED: type = "mapped "; break;
1436 case MEM_PRIVATE: type = "private"; break;
1437 case 0: type = " "; break;
1438 default: type = "??? "; break;
1440 memset(prot, ' ' , sizeof(prot)-1);
1441 prot[sizeof(prot)-1] = '\0';
1442 if (mbi.AllocationProtect & (PAGE_READONLY|PAGE_READWRITE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
1444 if (mbi.AllocationProtect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE))
1446 if (mbi.AllocationProtect & (PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
1448 if (mbi.AllocationProtect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
1456 packet_reply_open(gdbctx);
1457 snprintf(buffer, sizeof(buffer),
1458 "%08lx %08lx %s %s %s\n",
1459 (DWORD)addr, mbi.RegionSize, state, type, prot);
1460 packet_reply_catc(gdbctx, 'O');
1461 packet_reply_hex_to_str(gdbctx, buffer);
1462 packet_reply_close(gdbctx);
1464 if (addr + mbi.RegionSize < addr) /* wrap around ? */
1466 addr += mbi.RegionSize;
1468 packet_reply(gdbctx, "OK", 2);
1471 static void packet_query_monitor_trace(struct gdb_context* gdbctx,
1472 int len, const char* str)
1478 snprintf(buffer, sizeof(buffer), "trace=%x\n", gdbctx->trace);
1480 else if (len >= 2 && str[0] == '=')
1482 unsigned val = atoi(&str[1]);
1483 snprintf(buffer, sizeof(buffer), "trace: %x => %x\n", gdbctx->trace, val);
1484 gdbctx->trace = val;
1488 /* FIXME: ugly but can use error packet here */
1489 packet_reply_cat(gdbctx, "E00");
1492 packet_reply_open(gdbctx);
1493 packet_reply_hex_to_str(gdbctx, buffer);
1494 packet_reply_close(gdbctx);
1498 static void packet_query_monitor_linear(struct gdb_context* gdbctx,
1499 int len, const char* str)
1506 while (len > 0 && (*str == ' ' || *str == '\t'))
1510 /* FIXME: do a better scanning (allow both decimal and hex numbers) */
1511 if (!len || sscanf(str, "%x:%x", &seg, &ofs) != 2)
1513 packet_reply_error(gdbctx, 0);
1518 if (gdbctx->context.EFlags & 0x00020000) linear = (LOWORD(seg) << 4) + ofs;
1519 /* linux system selector ? */
1520 else if (!(seg & 4) || ((seg >> 3) < 17)) linear = ofs;
1521 /* standard selector */
1522 else if (GetThreadSelectorEntry(gdbctx->process->threads->handle, seg, &le))
1523 linear = (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) +
1527 snprintf(buffer, sizeof(buffer), "0x%x", linear);
1528 packet_reply_open(gdbctx);
1529 packet_reply_hex_to_str(gdbctx, buffer);
1530 packet_reply_close(gdbctx);
1539 void (*handler)(struct gdb_context*, int, const char*);
1542 {0, "wnd", 3, packet_query_monitor_wnd},
1543 {0, "window", 6, packet_query_monitor_wnd},
1544 {0, "proc", 4, packet_query_monitor_process},
1545 {0, "process", 7, packet_query_monitor_process},
1546 {0, "mem", 3, packet_query_monitor_mem},
1547 {1, "trace", 5, packet_query_monitor_trace},
1549 {1, "linear", 6, packet_query_monitor_linear},
1554 static enum packet_return packet_query_remote_command(struct gdb_context* gdbctx,
1555 const char* hxcmd, size_t len)
1558 struct query_detail* qd;
1560 assert((len & 1) == 0 && len < 2 * sizeof(buffer));
1562 hex_from(buffer, hxcmd, len);
1564 for (qd = &query_details[0]; qd->name != NULL; qd++)
1566 if (len < qd->len || strncmp(buffer, qd->name, qd->len) != 0) continue;
1567 if (!qd->with_arg && len != qd->len) continue;
1569 (qd->handler)(gdbctx, len - qd->len, buffer + qd->len);
1572 return packet_reply_error(gdbctx, EINVAL);
1575 static enum packet_return packet_query(struct gdb_context* gdbctx)
1577 switch (gdbctx->in_packet[0])
1580 if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
1584 packet_reply_open(gdbctx);
1585 packet_reply_add(gdbctx, "m", 1);
1586 for (thd = gdbctx->process->threads; thd; thd = thd->next)
1588 packet_reply_val(gdbctx, thd->tid, 4);
1589 if (thd->next != NULL)
1590 packet_reply_add(gdbctx, ",", 1);
1592 packet_reply_close(gdbctx);
1595 else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
1599 packet_reply_open(gdbctx);
1600 packet_reply_catc(gdbctx, 'O');
1601 get_process_info(gdbctx, result, sizeof(result));
1602 packet_reply_hex_to_str(gdbctx, result);
1603 packet_reply_close(gdbctx);
1608 if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
1610 packet_reply(gdbctx, "l", 1);
1613 else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
1615 packet_reply(gdbctx, "l", 1);
1620 if (gdbctx->in_packet_len == 1)
1623 /* FIXME: doc says 16 bit val ??? */
1624 /* grab first created thread, aka last in list */
1625 assert(gdbctx->process && gdbctx->process->threads);
1626 for (thd = gdbctx->process->threads; thd->next; thd = thd->next);
1627 packet_reply_open(gdbctx);
1628 packet_reply_add(gdbctx, "QC", 2);
1629 packet_reply_val(gdbctx, thd->tid, 4);
1630 packet_reply_close(gdbctx);
1635 if (strncmp(gdbctx->in_packet, "Offsets", gdbctx->in_packet_len) == 0)
1639 if (gdbctx->wine_segs[0] == 0 && gdbctx->wine_segs[1] == 0 &&
1640 gdbctx->wine_segs[2] == 0)
1641 return packet_error;
1642 snprintf(buf, sizeof(buf),
1643 "Text=%08lx;Data=%08lx;Bss=%08lx",
1644 gdbctx->wine_segs[0], gdbctx->wine_segs[1],
1645 gdbctx->wine_segs[2]);
1646 return packet_reply(gdbctx, buf, -1);
1650 if (gdbctx->in_packet_len > 5 && strncmp(gdbctx->in_packet, "Rcmd,", 5) == 0)
1652 return packet_query_remote_command(gdbctx, gdbctx->in_packet + 5,
1653 gdbctx->in_packet_len - 5);
1657 if (strncmp(gdbctx->in_packet, "Symbol::", gdbctx->in_packet_len) == 0)
1661 if (gdbctx->in_packet_len > 15 &&
1662 strncmp(gdbctx->in_packet, "ThreadExtraInfo", 15) == 0 &&
1663 gdbctx->in_packet[15] == ',')
1669 tid = strtol(gdbctx->in_packet + 16, &end, 16);
1670 if (end == NULL) break;
1671 get_thread_info(gdbctx, tid, result, sizeof(result));
1672 packet_reply_open(gdbctx);
1673 packet_reply_hex_to_str(gdbctx, result);
1674 packet_reply_close(gdbctx);
1679 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1680 fprintf(stderr, "Unknown or malformed query %*.*s\n",
1681 gdbctx->in_packet_len, gdbctx->in_packet_len, gdbctx->in_packet);
1682 return packet_error;
1685 static enum packet_return packet_step(struct gdb_context* gdbctx)
1687 /* FIXME: add support for address in packet */
1688 assert(gdbctx->in_packet_len == 0);
1689 if (DEBUG_CurrThread != gdbctx->exec_thread && gdbctx->exec_thread)
1690 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
1691 fprintf(stderr, "NIY: step on %lu, while last thread is %lu\n",
1692 gdbctx->exec_thread->tid, DEBUG_CurrThread->tid);
1693 if (!cpu_enter_stepping(gdbctx)) return packet_error;
1694 resume_debuggee(gdbctx, DBG_CONTINUE);
1695 wait_for_debuggee(gdbctx);
1696 if (!cpu_leave_stepping(gdbctx)) return packet_error;
1697 return packet_reply_status(gdbctx);
1701 static enum packet_return packet_step_signal(struct gdb_context* gdbctx)
1705 /* FIXME: add support for address in packet */
1706 assert(gdbctx->in_packet_len == 2);
1707 if (DEBUG_CurrThread->tid != gdbctx->exec_thread && gdbctx->exec_thread)
1708 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1709 fprintf(stderr, "NIY: step/sig on %u, while last thread is %u\n",
1710 gdbctx->exec_thread, DEBUG_CurrThread->tid);
1711 hex_from(&sig, gdbctx->in_packet, 1);
1712 /* cannot change signals on the fly */
1713 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1714 fprintf(stderr, "sigs: %u %u\n", sig, gdbctx->last_sig);
1715 if (sig != gdbctx->last_sig)
1716 return packet_error;
1717 resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
1718 wait_for_debuggee(gdbctx);
1719 return packet_reply_status(gdbctx);
1723 static enum packet_return packet_thread_alive(struct gdb_context* gdbctx)
1728 tid = strtol(gdbctx->in_packet, &end, 16);
1729 if (tid == -1 || tid == 0)
1730 return packet_reply_error(gdbctx, EINVAL);
1731 if (DEBUG_GetThread(gdbctx->process, tid) != NULL)
1733 return packet_reply_error(gdbctx, ESRCH);
1736 static enum packet_return packet_remove_breakpoint(struct gdb_context* gdbctx)
1740 struct gdb_ctx_Xpoint* xpt;
1742 /* FIXME: check packet_len */
1743 if (gdbctx->in_packet[0] < '0' || gdbctx->in_packet[0] > '4' ||
1744 gdbctx->in_packet[1] != ',' ||
1745 sscanf(gdbctx->in_packet + 2, "%p,%x", &addr, &len) != 2)
1746 return packet_error;
1747 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1748 fprintf(stderr, "Remove bp %p[%u] typ=%c\n",
1749 addr, len, gdbctx->in_packet[0]);
1750 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1752 if (xpt->addr == addr && xpt->type == gdbctx->in_packet[0])
1754 switch (cpu_remove_Xpoint(gdbctx, xpt, len))
1756 case 1: xpt->type = -1; return packet_ok;
1757 case 0: return packet_error;
1758 case -1: return packet_done;
1763 return packet_error;
1766 static enum packet_return packet_set_breakpoint(struct gdb_context* gdbctx)
1770 struct gdb_ctx_Xpoint* xpt;
1772 /* FIXME: check packet_len */
1773 if (gdbctx->in_packet[0] < '0' || gdbctx->in_packet[0] > '4' ||
1774 gdbctx->in_packet[1] != ',' ||
1775 sscanf(gdbctx->in_packet + 2, "%p,%x", &addr, &len) != 2)
1776 return packet_error;
1777 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1778 fprintf(stderr, "Set bp %p[%u] typ=%c\n",
1779 addr, len, gdbctx->in_packet[0]);
1780 /* because of packet command handling, this should be made idempotent */
1781 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1783 if (xpt->addr == addr && xpt->type == gdbctx->in_packet[0])
1784 return packet_ok; /* nothing to do */
1786 /* really set the Xpoint */
1787 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1789 if (xpt->type == -1)
1792 xpt->type = gdbctx->in_packet[0];
1793 switch (cpu_insert_Xpoint(gdbctx, xpt, len))
1795 case 1: return packet_ok;
1796 case 0: return packet_error;
1797 case -1: return packet_done;
1802 /* no more entries... eech */
1803 fprintf(stderr, "Running out of spots for {break|watch}points\n");
1804 return packet_error;
1807 /* =============================================== *
1808 * P A C K E T I N F R A S T R U C T U R E *
1809 * =============================================== *
1815 enum packet_return (*handler)(struct gdb_context* gdbctx);
1818 static struct packet_entry packet_entries[] =
1820 /* {'!', packet_extended}, */
1821 {'?', packet_last_signal},
1822 {'c', packet_continue},
1823 {'C', packet_continue_signal},
1824 {'D', packet_detach},
1825 {'g', packet_read_registers},
1826 {'G', packet_write_registers},
1828 {'H', packet_thread},
1829 {'m', packet_read_memory},
1830 {'M', packet_write_memory},
1831 /* {'p', packet_read_register}, doesn't seem needed */
1832 {'P', packet_write_register},
1833 {'q', packet_query},
1835 /*{'S', packet_step_signal}, hard(er) to implement */
1836 {'T', packet_thread_alive},
1837 {'z', packet_remove_breakpoint},
1838 {'Z', packet_set_breakpoint},
1841 static BOOL extract_packets(struct gdb_context* gdbctx)
1845 unsigned char in_cksum, loc_cksum;
1847 enum packet_return ret = packet_error;
1850 while ((ret & packet_last_f) == 0)
1852 if (gdbctx->in_len && (gdbctx->trace & GDBPXY_TRC_LOWLEVEL))
1853 fprintf(stderr, "In-buf: %*.*s\n",
1854 gdbctx->in_len, gdbctx->in_len, gdbctx->in_buf);
1855 ptr = memchr(gdbctx->in_buf, '$', gdbctx->in_len);
1856 if (ptr == NULL) return FALSE;
1857 if (ptr != gdbctx->in_buf)
1859 int glen = ptr - gdbctx->in_buf; /* garbage len */
1860 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1861 fprintf(stderr, "Removing garbage: %*.*s\n",
1862 glen, glen, gdbctx->in_buf);
1863 gdbctx->in_len -= glen;
1864 memmove(gdbctx->in_buf, ptr, gdbctx->in_len);
1866 end = memchr(gdbctx->in_buf + 1, '#', gdbctx->in_len);
1867 if (end == NULL) return FALSE;
1868 /* no checksum yet */
1869 if (end + 3 > gdbctx->in_buf + gdbctx->in_len) return FALSE;
1870 plen = end - gdbctx->in_buf - 1;
1871 hex_from(&in_cksum, end + 1, 1);
1872 loc_cksum = checksum(gdbctx->in_buf + 1, plen);
1873 if (loc_cksum == in_cksum)
1875 if (num_packet == 0) {
1880 write(gdbctx->sock, "+", 1);
1883 /* FIXME: should use bsearch if packet_entries was sorted */
1884 for (i = 0; i < sizeof(packet_entries)/sizeof(packet_entries[0]); i++)
1886 if (packet_entries[i].key == gdbctx->in_buf[1]) break;
1888 if (i == sizeof(packet_entries)/sizeof(packet_entries[0]))
1890 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1891 fprintf(stderr, "Unknown packet request %*.*s\n",
1892 plen, plen, &gdbctx->in_buf[1]);
1896 gdbctx->in_packet = gdbctx->in_buf + 2;
1897 gdbctx->in_packet_len = plen - 1;
1898 if (gdbctx->trace & GDBPXY_TRC_PACKET)
1899 fprintf(stderr, "Packet: %c%*.*s\n",
1901 gdbctx->in_packet_len, gdbctx->in_packet_len,
1903 ret = (packet_entries[i].handler)(gdbctx);
1905 switch (ret & ~packet_last_f)
1907 case packet_error: packet_reply(gdbctx, "", 0); break;
1908 case packet_ok: packet_reply(gdbctx, "OK", 2); break;
1909 case packet_done: break;
1911 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1912 fprintf(stderr, "Reply-full: %*.*s\n",
1913 gdbctx->out_len, gdbctx->out_len, gdbctx->out_buf);
1914 i = write(gdbctx->sock, gdbctx->out_buf, gdbctx->out_len);
1915 assert(i == gdbctx->out_len);
1916 /* if this fails, we'll have to use POLLOUT...
1918 gdbctx->out_len = 0;
1923 /* FIXME: if we have in our input buffer more than one packet,
1924 * it's very likely that we took too long to answer to a given packet
1925 * and gdb is sending us again the same packet
1926 * We simply drop the second packet. This will lower the risk of error,
1927 * but there's still some race conditions here
1928 * A better fix (yet not perfect) would be to have two threads:
1929 * - one managing the packets for gdb
1930 * - the second one managing the commands...
1931 * This would allow us also the reply with the '+' character (Ack of
1932 * the command) way sooner than what we do now
1934 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1935 fprintf(stderr, "Dropping packet, I was too slow to respond\n");
1940 write(gdbctx->sock, "+", 1);
1941 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1942 fprintf(stderr, "Dropping packet, invalid checksum %d <> %d\n", in_cksum, loc_cksum);
1944 gdbctx->in_len -= plen + 4;
1945 memmove(gdbctx->in_buf, end + 3, gdbctx->in_len);
1950 static int fetch_data(struct gdb_context* gdbctx)
1952 int len, in_len = gdbctx->in_len;
1954 assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
1958 if (gdbctx->in_len + STEP > gdbctx->in_buf_alloc)
1959 gdbctx->in_buf = realloc(gdbctx->in_buf, gdbctx->in_buf_alloc += STEP);
1961 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1962 fprintf(stderr, "%d %d %*.*s\n",
1963 gdbctx->in_len, gdbctx->in_buf_alloc,
1964 gdbctx->in_len, gdbctx->in_len, gdbctx->in_buf);
1965 len = read(gdbctx->sock, gdbctx->in_buf + gdbctx->in_len, gdbctx->in_buf_alloc - gdbctx->in_len);
1966 if (len <= 0) break;
1967 gdbctx->in_len += len;
1968 assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
1969 if (len < gdbctx->in_buf_alloc - gdbctx->in_len) break;
1971 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1972 fprintf(stderr, "=> %d\n", gdbctx->in_len - in_len);
1973 return gdbctx->in_len - in_len;
1976 static BOOL gdb_startup(struct gdb_context* gdbctx, DEBUG_EVENT* de, unsigned flags)
1979 struct sockaddr_in s_addrs;
1980 int s_len = sizeof(s_addrs);
1981 struct pollfd pollfd;
1982 char wine_path[MAX_PATH];
1985 /* step 1: create socket for gdb connection request */
1986 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
1988 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1989 fprintf(stderr, "Can't create socket");
1993 if (listen(sock, 1) == -1 ||
1994 getsockname(sock, (struct sockaddr*)&s_addrs, &s_len) == -1)
1997 /* step 2: find out wine executable location (as a Unix filename) */
1998 ptr = getenv("WINELOADER");
1999 strcpy(wine_path, ptr ? ptr : "wine");
2001 fprintf(stderr, "Using wine_path: %s\n", wine_path);
2002 read_elf_info(wine_path, gdbctx->wine_segs);
2004 /* step 3: fire up gdb (if requested) */
2006 fprintf(stderr, "target remote localhost:%d\n", ntohs(s_addrs.sin_port));
2010 case -1: /* error in parent... */
2011 fprintf(stderr, "Cannot create gdb\n");
2014 default: /* in parent... success */
2016 case 0: /* in child... and alive */
2020 const char* gdb_path;
2023 if (!(gdb_path = getenv("WINE_GDB"))) gdb_path = "gdb";
2024 strcpy(buf,"/tmp/winegdb.XXXXXX");
2025 fd = mkstemps(buf,0);
2026 if (fd == -1) return FALSE;
2027 if ((f = fdopen(fd, "w+")) == NULL) return FALSE;
2028 fprintf(f, "file %s\n", wine_path);
2029 fprintf(f, "target remote localhost:%d\n", ntohs(s_addrs.sin_port));
2030 fprintf(f, "monitor trace=%d\n", GDBPXY_TRC_COMMAND_FIXME);
2031 fprintf(f, "set prompt Wine-gdb>\\ \n");
2032 /* gdb 5.1 seems to require it, won't hurt anyway */
2033 fprintf(f, "sharedlibrary\n");
2034 /* tell gdb to delete this file when done handling it... */
2035 fprintf(f, "shell rm -f \"%s\"\n", buf);
2038 execlp("xterm", "xterm", "-e", gdb_path, "-x", buf, NULL);
2040 execlp(gdb_path, gdb_path, "-x", buf, NULL);
2041 assert(0); /* never reached */
2047 /* step 4: do the process internal creation */
2048 handle_debug_event(gdbctx, de);
2050 /* step 5: wait for gdb to connect actually */
2052 pollfd.events = POLLIN;
2055 switch (poll(&pollfd, 1, -1))
2058 if (pollfd.revents & POLLIN)
2061 gdbctx->sock = accept(sock, (struct sockaddr*)&s_addrs, &s_len);
2062 if (gdbctx->sock == -1)
2064 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
2065 fprintf(stderr, "Connected on %d\n", gdbctx->sock);
2066 /* don't keep our small packets too long: send them ASAP back to GDB
2067 * without this, GDB really crawls
2069 setsockopt(gdbctx->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&dummy, sizeof(dummy));
2073 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
2074 fprintf(stderr, "Poll for cnx failed (timeout)\n");
2077 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
2078 fprintf(stderr, "Poll for cnx failed (error)\n");
2088 static BOOL gdb_init_context(struct gdb_context* gdbctx, unsigned flags)
2094 gdbctx->in_buf = NULL;
2095 gdbctx->in_buf_alloc = 0;
2097 gdbctx->out_buf = NULL;
2098 gdbctx->out_buf_alloc = 0;
2099 gdbctx->out_len = 0;
2100 gdbctx->out_curr_packet = -1;
2102 gdbctx->exec_thread = gdbctx->other_thread = NULL;
2103 gdbctx->last_sig = 0;
2104 gdbctx->in_trap = FALSE;
2105 gdbctx->trace = /*GDBPXY_TRC_PACKET | GDBPXY_TRC_COMMAND |*/ GDBPXY_TRC_COMMAND_ERROR | GDBPXY_TRC_COMMAND_FIXME | GDBPXY_TRC_WIN32_EVENT;
2106 gdbctx->process = NULL;
2107 for (i = 0; i < NUM_XPOINT; i++)
2108 gdbctx->Xpoints[i].type = -1;
2110 /* wait for first trap */
2111 while (WaitForDebugEvent(&de, INFINITE))
2113 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
2115 /* this should be the first event we get,
2116 * and the only one of this type */
2117 assert(gdbctx->process == NULL && de.dwProcessId == DEBUG_CurrPid);
2118 /*gdbctx->dwProcessId = pid; */
2119 if (!gdb_startup(gdbctx, &de, flags)) return FALSE;
2120 assert(!gdbctx->in_trap);
2124 handle_debug_event(gdbctx, &de);
2125 if (gdbctx->in_trap) break;
2127 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
2132 BOOL DEBUG_GdbRemote(unsigned flags)
2134 struct pollfd pollfd;
2135 struct gdb_context gdbctx;
2138 for (doLoop = gdb_init_context(&gdbctx, flags); doLoop;)
2140 pollfd.fd = gdbctx.sock;
2141 pollfd.events = POLLIN;
2144 switch (poll(&pollfd, 1, -1))
2148 if (pollfd.revents & (POLLHUP | POLLERR))
2150 if (gdbctx.trace & GDBPXY_TRC_LOWLEVEL)
2151 fprintf(stderr, "Gdb hung up\n");
2152 /* kill also debuggee process - questionnable - */
2153 detach_debuggee(&gdbctx, TRUE);
2157 if ((pollfd.revents & POLLIN) && fetch_data(&gdbctx) > 0)
2159 if (extract_packets(&gdbctx)) doLoop = FALSE;
2163 /* timeout, should never happen (infinite timeout) */
2166 if (gdbctx.trace & GDBPXY_TRC_LOWLEVEL)
2167 fprintf(stderr, "Poll failed\n");