2 * A Win32 based proxy implementing the GBD remote protocol
3 * This allows to debug Wine (and any "emulated" program) under
6 * Copyright (c) Eric Pouech 2002-2004
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 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 enum be_xpoint_type type; /* -1 means free */
81 /* split into individual packet */
89 /* generic GDB thread information */
90 struct dbg_thread* exec_thread; /* thread used in step & continue */
91 struct dbg_thread* other_thread; /* thread to be used in any other operation */
93 /* current Win32 trap env */
97 /* Win32 information */
98 struct dbg_process* process;
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 /* =============================================== *
106 * B A S I C M A N I P U L A T I O N S *
107 * =============================================== *
110 static inline int hex_from0(char ch)
112 if (ch >= '0' && ch <= '9') return ch - '0';
113 if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
114 if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
120 static inline unsigned char hex_to0(int x)
122 assert(x >= 0 && x < 16);
123 return "0123456789abcdef"[x];
126 static void hex_from(void* dst, const char* src, size_t len)
128 unsigned char *p = dst;
131 *p++ = (hex_from0(src[0]) << 4) | hex_from0(src[1]);
136 static void hex_to(char* dst, const void* src, size_t len)
138 const unsigned char *p = src;
141 *dst++ = hex_to0(*p >> 4);
142 *dst++ = hex_to0(*p & 0x0F);
147 static unsigned char checksum(const char* ptr, int len)
152 cksum += (unsigned char)*ptr++;
156 /* =============================================== *
157 * C P U H A N D L E R S *
158 * =============================================== *
162 static size_t cpu_register_map[] = {
163 FIELD_OFFSET(CONTEXT, Eax),
164 FIELD_OFFSET(CONTEXT, Ecx),
165 FIELD_OFFSET(CONTEXT, Edx),
166 FIELD_OFFSET(CONTEXT, Ebx),
167 FIELD_OFFSET(CONTEXT, Esp),
168 FIELD_OFFSET(CONTEXT, Ebp),
169 FIELD_OFFSET(CONTEXT, Esi),
170 FIELD_OFFSET(CONTEXT, Edi),
171 FIELD_OFFSET(CONTEXT, Eip),
172 FIELD_OFFSET(CONTEXT, EFlags),
173 FIELD_OFFSET(CONTEXT, SegCs),
174 FIELD_OFFSET(CONTEXT, SegSs),
175 FIELD_OFFSET(CONTEXT, SegDs),
176 FIELD_OFFSET(CONTEXT, SegEs),
177 FIELD_OFFSET(CONTEXT, SegFs),
178 FIELD_OFFSET(CONTEXT, SegGs),
182 static size_t cpu_register_map[] = {
183 FIELD_OFFSET(CONTEXT, Gpr0),
184 FIELD_OFFSET(CONTEXT, Gpr1),
185 FIELD_OFFSET(CONTEXT, Gpr2),
186 FIELD_OFFSET(CONTEXT, Gpr3),
187 FIELD_OFFSET(CONTEXT, Gpr4),
188 FIELD_OFFSET(CONTEXT, Gpr5),
189 FIELD_OFFSET(CONTEXT, Gpr6),
190 FIELD_OFFSET(CONTEXT, Gpr7),
191 FIELD_OFFSET(CONTEXT, Gpr8),
192 FIELD_OFFSET(CONTEXT, Gpr9),
193 FIELD_OFFSET(CONTEXT, Gpr10),
194 FIELD_OFFSET(CONTEXT, Gpr11),
195 FIELD_OFFSET(CONTEXT, Gpr12),
196 FIELD_OFFSET(CONTEXT, Gpr13),
197 FIELD_OFFSET(CONTEXT, Gpr14),
198 FIELD_OFFSET(CONTEXT, Gpr15),
199 FIELD_OFFSET(CONTEXT, Gpr16),
200 FIELD_OFFSET(CONTEXT, Gpr17),
201 FIELD_OFFSET(CONTEXT, Gpr18),
202 FIELD_OFFSET(CONTEXT, Gpr19),
203 FIELD_OFFSET(CONTEXT, Gpr20),
204 FIELD_OFFSET(CONTEXT, Gpr21),
205 FIELD_OFFSET(CONTEXT, Gpr22),
206 FIELD_OFFSET(CONTEXT, Gpr23),
207 FIELD_OFFSET(CONTEXT, Gpr24),
208 FIELD_OFFSET(CONTEXT, Gpr25),
209 FIELD_OFFSET(CONTEXT, Gpr26),
210 FIELD_OFFSET(CONTEXT, Gpr27),
211 FIELD_OFFSET(CONTEXT, Gpr28),
212 FIELD_OFFSET(CONTEXT, Gpr29),
213 FIELD_OFFSET(CONTEXT, Gpr30),
214 FIELD_OFFSET(CONTEXT, Gpr31),
215 FIELD_OFFSET(CONTEXT, Fpr0),
216 FIELD_OFFSET(CONTEXT, Fpr1),
217 FIELD_OFFSET(CONTEXT, Fpr2),
218 FIELD_OFFSET(CONTEXT, Fpr3),
219 FIELD_OFFSET(CONTEXT, Fpr4),
220 FIELD_OFFSET(CONTEXT, Fpr5),
221 FIELD_OFFSET(CONTEXT, Fpr6),
222 FIELD_OFFSET(CONTEXT, Fpr7),
223 FIELD_OFFSET(CONTEXT, Fpr8),
224 FIELD_OFFSET(CONTEXT, Fpr9),
225 FIELD_OFFSET(CONTEXT, Fpr10),
226 FIELD_OFFSET(CONTEXT, Fpr11),
227 FIELD_OFFSET(CONTEXT, Fpr12),
228 FIELD_OFFSET(CONTEXT, Fpr13),
229 FIELD_OFFSET(CONTEXT, Fpr14),
230 FIELD_OFFSET(CONTEXT, Fpr15),
231 FIELD_OFFSET(CONTEXT, Fpr16),
232 FIELD_OFFSET(CONTEXT, Fpr17),
233 FIELD_OFFSET(CONTEXT, Fpr18),
234 FIELD_OFFSET(CONTEXT, Fpr19),
235 FIELD_OFFSET(CONTEXT, Fpr20),
236 FIELD_OFFSET(CONTEXT, Fpr21),
237 FIELD_OFFSET(CONTEXT, Fpr22),
238 FIELD_OFFSET(CONTEXT, Fpr23),
239 FIELD_OFFSET(CONTEXT, Fpr24),
240 FIELD_OFFSET(CONTEXT, Fpr25),
241 FIELD_OFFSET(CONTEXT, Fpr26),
242 FIELD_OFFSET(CONTEXT, Fpr27),
243 FIELD_OFFSET(CONTEXT, Fpr28),
244 FIELD_OFFSET(CONTEXT, Fpr29),
245 FIELD_OFFSET(CONTEXT, Fpr30),
246 FIELD_OFFSET(CONTEXT, Fpr31),
248 FIELD_OFFSET(CONTEXT, Iar),
249 FIELD_OFFSET(CONTEXT, Msr),
250 FIELD_OFFSET(CONTEXT, Cr),
251 FIELD_OFFSET(CONTEXT, Lr),
252 FIELD_OFFSET(CONTEXT, Ctr),
253 FIELD_OFFSET(CONTEXT, Xer),
254 /* FIXME: MQ is missing? FIELD_OFFSET(CONTEXT, Mq), */
255 /* see gdb/nlm/ppc.c */
258 # error "Define the registers map for your CPU"
262 static const size_t cpu_num_regs = (sizeof(cpu_register_map) / sizeof(cpu_register_map[0]));
264 static inline unsigned long* cpu_register(CONTEXT* ctx, unsigned idx)
266 assert(idx < cpu_num_regs);
267 return (unsigned long*)((char*)ctx + cpu_register_map[idx]);
270 /* =============================================== *
271 * W I N 3 2 D E B U G I N T E R F A C E *
272 * =============================================== *
275 static BOOL fetch_context(struct gdb_context* gdbctx, HANDLE h, CONTEXT* ctx)
277 ctx->ContextFlags = CONTEXT_CONTROL
279 #ifdef CONTEXT_SEGMENTS
282 #ifdef CONTEXT_DEBUG_REGISTERS
283 | CONTEXT_DEBUG_REGISTERS
286 if (!GetThreadContext(h, ctx))
288 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
289 fprintf(stderr, "Can't get thread's context\n");
295 static BOOL handle_exception(struct gdb_context* gdbctx, EXCEPTION_DEBUG_INFO* exc)
297 EXCEPTION_RECORD* rec = &exc->ExceptionRecord;
300 switch (rec->ExceptionCode)
302 case EXCEPTION_ACCESS_VIOLATION:
303 case EXCEPTION_PRIV_INSTRUCTION:
304 case EXCEPTION_STACK_OVERFLOW:
305 case EXCEPTION_GUARD_PAGE:
306 gdbctx->last_sig = SIGSEGV;
309 case EXCEPTION_DATATYPE_MISALIGNMENT:
310 gdbctx->last_sig = SIGBUS;
313 case EXCEPTION_SINGLE_STEP:
315 case EXCEPTION_BREAKPOINT:
316 gdbctx->last_sig = SIGTRAP;
319 case EXCEPTION_FLT_DENORMAL_OPERAND:
320 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
321 case EXCEPTION_FLT_INEXACT_RESULT:
322 case EXCEPTION_FLT_INVALID_OPERATION:
323 case EXCEPTION_FLT_OVERFLOW:
324 case EXCEPTION_FLT_STACK_CHECK:
325 case EXCEPTION_FLT_UNDERFLOW:
326 gdbctx->last_sig = SIGFPE;
329 case EXCEPTION_INT_DIVIDE_BY_ZERO:
330 case EXCEPTION_INT_OVERFLOW:
331 gdbctx->last_sig = SIGFPE;
334 case EXCEPTION_ILLEGAL_INSTRUCTION:
335 gdbctx->last_sig = SIGILL;
339 gdbctx->last_sig = SIGINT;
342 case STATUS_POSSIBLE_DEADLOCK:
343 gdbctx->last_sig = SIGALRM;
345 /* FIXME: we could also add here a O packet with additional information */
348 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
349 fprintf(stderr, "Unhandled exception code 0x%08lx\n", rec->ExceptionCode);
350 gdbctx->last_sig = SIGABRT;
357 static void handle_debug_event(struct gdb_context* gdbctx, DEBUG_EVENT* de)
361 dbg_curr_thread = dbg_get_thread(gdbctx->process, de->dwThreadId);
363 switch (de->dwDebugEventCode)
365 case CREATE_PROCESS_DEBUG_EVENT:
366 memory_get_string_indirect(de->u.CreateProcessInfo.hProcess,
367 de->u.CreateProcessInfo.lpImageName,
368 de->u.CreateProcessInfo.fUnicode,
369 buffer, sizeof(buffer));
371 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
372 fprintf(stderr, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
373 de->dwProcessId, de->dwThreadId,
374 buffer, de->u.CreateProcessInfo.lpImageName,
375 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress,
376 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
377 de->u.CreateProcessInfo.nDebugInfoSize);
379 gdbctx->process = dbg_add_process(de->dwProcessId,
380 de->u.CreateProcessInfo.hProcess,
382 /* de->u.CreateProcessInfo.lpStartAddress; */
383 if (!SymInitialize(gdbctx->process->handle, NULL, TRUE))
384 fprintf(stderr, "Couldn't initiate DbgHelp\n");
386 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
387 fprintf(stderr, "%08lx:%08lx: create thread I @%08lx\n",
388 de->dwProcessId, de->dwThreadId,
389 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress);
391 assert(dbg_curr_thread == NULL); /* shouldn't be there */
392 dbg_add_thread(gdbctx->process, de->dwThreadId,
393 de->u.CreateProcessInfo.hThread,
394 de->u.CreateProcessInfo.lpThreadLocalBase);
397 case LOAD_DLL_DEBUG_EVENT:
398 assert(dbg_curr_thread);
399 memory_get_string_indirect(gdbctx->process->handle,
400 de->u.LoadDll.lpImageName,
401 de->u.LoadDll.fUnicode,
402 buffer, sizeof(buffer));
403 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
404 fprintf(stderr, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
405 de->dwProcessId, de->dwThreadId,
406 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
407 de->u.LoadDll.dwDebugInfoFileOffset,
408 de->u.LoadDll.nDebugInfoSize);
409 SymLoadModule(gdbctx->process->handle, de->u.LoadDll.hFile, buffer, NULL,
410 (unsigned long)de->u.LoadDll.lpBaseOfDll, 0);
413 case UNLOAD_DLL_DEBUG_EVENT:
414 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
415 fprintf(stderr, "%08lx:%08lx: unload DLL @%08lx\n",
416 de->dwProcessId, de->dwThreadId, (unsigned long)de->u.UnloadDll.lpBaseOfDll);
417 SymUnloadModule(gdbctx->process->handle,
418 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
421 case EXCEPTION_DEBUG_EVENT:
422 assert(dbg_curr_thread);
423 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
424 fprintf(stderr, "%08lx:%08lx: exception code=0x%08lx\n",
425 de->dwProcessId, de->dwThreadId,
426 de->u.Exception.ExceptionRecord.ExceptionCode);
428 if (fetch_context(gdbctx, dbg_curr_thread->handle, &gdbctx->context))
430 gdbctx->in_trap = handle_exception(gdbctx, &de->u.Exception);
434 case CREATE_THREAD_DEBUG_EVENT:
435 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
436 fprintf(stderr, "%08lx:%08lx: create thread D @%08lx\n",
437 de->dwProcessId, de->dwThreadId, (unsigned long)(void*)de->u.CreateThread.lpStartAddress);
439 dbg_add_thread(gdbctx->process,
441 de->u.CreateThread.hThread,
442 de->u.CreateThread.lpThreadLocalBase);
445 case EXIT_THREAD_DEBUG_EVENT:
446 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
447 fprintf(stderr, "%08lx:%08lx: exit thread (%ld)\n",
448 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
450 assert(dbg_curr_thread);
451 if (dbg_curr_thread == gdbctx->exec_thread) gdbctx->exec_thread = NULL;
452 if (dbg_curr_thread == gdbctx->other_thread) gdbctx->other_thread = NULL;
453 dbg_del_thread(dbg_curr_thread);
456 case EXIT_PROCESS_DEBUG_EVENT:
457 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
458 fprintf(stderr, "%08lx:%08lx: exit process (%ld)\n",
459 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
461 dbg_del_process(gdbctx->process);
462 gdbctx->process = NULL;
463 /* now signal gdb that we're done */
464 gdbctx->last_sig = SIGTERM;
465 gdbctx->in_trap = TRUE;
468 case OUTPUT_DEBUG_STRING_EVENT:
469 assert(dbg_curr_thread);
470 memory_get_string(gdbctx->process->handle,
471 de->u.DebugString.lpDebugStringData, TRUE,
472 de->u.DebugString.fUnicode, buffer, sizeof(buffer));
473 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
474 fprintf(stderr, "%08lx:%08lx: output debug string (%s)\n",
475 de->dwProcessId, de->dwThreadId, buffer);
479 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
480 fprintf(stderr, "%08lx:%08lx: rip error=%ld type=%ld\n",
481 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
482 de->u.RipInfo.dwType);
486 if (gdbctx->trace & GDBPXY_TRC_WIN32_EVENT)
487 fprintf(stderr, "%08lx:%08lx: unknown event (%ld)\n",
488 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
492 static void resume_debuggee(struct gdb_context* gdbctx, unsigned long cont)
496 if (!SetThreadContext(dbg_curr_thread->handle, &gdbctx->context))
497 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
498 fprintf(stderr, "Cannot set context on thread %lu\n", dbg_curr_thread->tid);
499 if (!ContinueDebugEvent(gdbctx->process->pid, dbg_curr_thread->tid, cont))
500 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
501 fprintf(stderr, "Cannot continue on %lu (%lu)\n",
502 dbg_curr_thread->tid, cont);
504 else if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
505 fprintf(stderr, "Cannot find last thread (%lu)\n", dbg_curr_thread->tid);
508 static void wait_for_debuggee(struct gdb_context* gdbctx)
512 gdbctx->in_trap = FALSE;
513 while (WaitForDebugEvent(&de, INFINITE))
515 handle_debug_event(gdbctx, &de);
516 assert(!gdbctx->process ||
517 gdbctx->process->pid == 0 ||
518 de.dwProcessId == gdbctx->process->pid);
519 assert(!dbg_curr_thread || de.dwThreadId == dbg_curr_thread->tid);
520 if (gdbctx->in_trap) break;
521 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
525 static void detach_debuggee(struct gdb_context* gdbctx, BOOL kill)
527 be_cpu->single_step(&gdbctx->context, FALSE);
528 resume_debuggee(gdbctx, DBG_CONTINUE);
530 DebugActiveProcessStop(gdbctx->process->pid);
531 dbg_del_process(gdbctx->process);
532 gdbctx->process = NULL;
535 static void get_process_info(struct gdb_context* gdbctx, char* buffer, size_t len)
537 unsigned long status;
539 if (!GetExitCodeProcess(gdbctx->process->handle, &status))
541 strcpy(buffer, "Unknown process");
544 if (status == STILL_ACTIVE)
546 strcpy(buffer, "Running");
549 snprintf(buffer, len, "Terminated (%lu)", status);
551 switch (GetPriorityClass(gdbctx->process->handle))
554 #ifdef ABOVE_NORMAL_PRIORITY_CLASS
555 case ABOVE_NORMAL_PRIORITY_CLASS: strcat(buffer, ", above normal priority"); break;
557 #ifdef BELOW_NORMAL_PRIORITY_CLASS
558 case BELOW_NORMAL_PRIORITY_CLASS: strcat(buffer, ", below normal priotity"); break;
560 case HIGH_PRIORITY_CLASS: strcat(buffer, ", high priority"); break;
561 case IDLE_PRIORITY_CLASS: strcat(buffer, ", idle priority"); break;
562 case NORMAL_PRIORITY_CLASS: strcat(buffer, ", normal priority"); break;
563 case REALTIME_PRIORITY_CLASS: strcat(buffer, ", realtime priority"); break;
565 strcat(buffer, "\n");
568 static void get_thread_info(struct gdb_context* gdbctx, unsigned tid,
569 char* buffer, size_t len)
571 struct dbg_thread* thd;
572 unsigned long status;
575 /* FIXME: use the size of buffer */
576 thd = dbg_get_thread(gdbctx->process, tid);
579 strcpy(buffer, "No information");
582 if (GetExitCodeThread(thd->handle, &status))
584 if (status == STILL_ACTIVE)
586 /* FIXME: this is a bit brutal... some nicer way shall be found */
587 switch (status = SuspendThread(thd->handle))
590 case 0: strcpy(buffer, "Running"); break;
591 default: snprintf(buffer, len, "Suspended (%lu)", status - 1);
593 ResumeThread(thd->handle);
596 snprintf(buffer, len, "Terminated (exit code = %lu)", status);
600 strcpy(buffer, "Unknown threadID");
602 switch (prio = GetThreadPriority(thd->handle))
604 case THREAD_PRIORITY_ERROR_RETURN: break;
605 case THREAD_PRIORITY_ABOVE_NORMAL: strcat(buffer, ", priority +1 above normal"); break;
606 case THREAD_PRIORITY_BELOW_NORMAL: strcat(buffer, ", priority -1 below normal"); break;
607 case THREAD_PRIORITY_HIGHEST: strcat(buffer, ", priority +2 above normal"); break;
608 case THREAD_PRIORITY_LOWEST: strcat(buffer, ", priority -2 below normal"); break;
609 case THREAD_PRIORITY_IDLE: strcat(buffer, ", priority idle"); break;
610 case THREAD_PRIORITY_NORMAL: strcat(buffer, ", priority normal"); break;
611 case THREAD_PRIORITY_TIME_CRITICAL: strcat(buffer, ", priority time-critical"); break;
612 default: snprintf(buffer + strlen(buffer), len - strlen(buffer), ", priority = %d", prio);
614 assert(strlen(buffer) < len);
617 /* =============================================== *
618 * P A C K E T U T I L S *
619 * =============================================== *
622 enum packet_return {packet_error = 0x00, packet_ok = 0x01, packet_done = 0x02,
623 packet_last_f = 0x80};
625 static void packet_reply_grow(struct gdb_context* gdbctx, size_t size)
627 if (gdbctx->out_buf_alloc < gdbctx->out_len + size)
629 gdbctx->out_buf_alloc = ((gdbctx->out_len + size) / 32 + 1) * 32;
630 gdbctx->out_buf = realloc(gdbctx->out_buf, gdbctx->out_buf_alloc);
634 static void packet_reply_hex_to(struct gdb_context* gdbctx, const void* src, int len)
636 packet_reply_grow(gdbctx, len * 2);
637 hex_to(&gdbctx->out_buf[gdbctx->out_len], src, len);
638 gdbctx->out_len += len * 2;
641 static inline void packet_reply_hex_to_str(struct gdb_context* gdbctx, const char* src)
643 packet_reply_hex_to(gdbctx, src, strlen(src));
646 static void packet_reply_val(struct gdb_context* gdbctx, unsigned long val, int len)
650 shift = (len - 1) * 8;
651 packet_reply_grow(gdbctx, len * 2);
652 for (i = 0; i < len; i++, shift -= 8)
654 gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> (shift + 4)) & 0x0F);
655 gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> shift ) & 0x0F);
659 static inline void packet_reply_add(struct gdb_context* gdbctx, const char* str, int len)
661 packet_reply_grow(gdbctx, len);
662 memcpy(&gdbctx->out_buf[gdbctx->out_len], str, len);
663 gdbctx->out_len += len;
666 static inline void packet_reply_cat(struct gdb_context* gdbctx, const char* str)
668 packet_reply_add(gdbctx, str, strlen(str));
671 static inline void packet_reply_catc(struct gdb_context* gdbctx, char ch)
673 packet_reply_add(gdbctx, &ch, 1);
676 static void packet_reply_open(struct gdb_context* gdbctx)
678 assert(gdbctx->out_curr_packet == -1);
679 packet_reply_catc(gdbctx, '$');
680 gdbctx->out_curr_packet = gdbctx->out_len;
683 static void packet_reply_close(struct gdb_context* gdbctx)
688 plen = gdbctx->out_len - gdbctx->out_curr_packet;
689 packet_reply_catc(gdbctx, '#');
690 cksum = checksum(&gdbctx->out_buf[gdbctx->out_curr_packet], plen);
691 packet_reply_hex_to(gdbctx, &cksum, 1);
692 if (gdbctx->trace & GDBPXY_TRC_PACKET)
693 fprintf(stderr, "Reply : %*.*s\n",
694 plen, plen, &gdbctx->out_buf[gdbctx->out_curr_packet]);
695 gdbctx->out_curr_packet = -1;
698 static enum packet_return packet_reply(struct gdb_context* gdbctx, const char* packet, int len)
700 packet_reply_open(gdbctx);
702 if (len == -1) len = strlen(packet);
703 assert(memchr(packet, '$', len) == NULL && memchr(packet, '#', len) == NULL);
705 packet_reply_add(gdbctx, packet, len);
707 packet_reply_close(gdbctx);
712 static enum packet_return packet_reply_error(struct gdb_context* gdbctx, int error)
714 packet_reply_open(gdbctx);
716 packet_reply_add(gdbctx, "E", 1);
717 packet_reply_val(gdbctx, error, 1);
719 packet_reply_close(gdbctx);
724 /* =============================================== *
725 * P A C K E T H A N D L E R S *
726 * =============================================== *
729 static enum packet_return packet_reply_status(struct gdb_context* gdbctx)
731 enum packet_return ret = packet_done;
733 packet_reply_open(gdbctx);
735 if (gdbctx->process != NULL)
740 packet_reply_catc(gdbctx, 'T');
741 sig = gdbctx->last_sig;
742 packet_reply_val(gdbctx, sig, 1);
743 packet_reply_add(gdbctx, "thread:", 7);
744 packet_reply_val(gdbctx, dbg_curr_thread->tid, 4);
745 packet_reply_catc(gdbctx, ';');
747 for (i = 0; i < cpu_num_regs; i++)
749 /* FIXME: this call will also grow the buffer...
750 * unneeded, but not harmful
752 packet_reply_val(gdbctx, i, 1);
753 packet_reply_catc(gdbctx, ':');
754 packet_reply_hex_to(gdbctx, cpu_register(&gdbctx->context, i), 4);
755 packet_reply_catc(gdbctx, ';');
760 /* Try to put an exit code
761 * Cannot use GetExitCodeProcess, wouldn't fit in a 8 bit value, so
762 * just indicate the end of process and exit */
763 packet_reply_add(gdbctx, "W00", 3);
764 /*if (!gdbctx->extended)*/ ret |= packet_last_f;
767 packet_reply_close(gdbctx);
773 static enum packet_return packet_extended(struct gdb_context* gdbctx)
775 gdbctx->extended = 1;
780 static enum packet_return packet_last_signal(struct gdb_context* gdbctx)
782 assert(gdbctx->in_packet_len == 0);
783 return packet_reply_status(gdbctx);
786 static enum packet_return packet_continue(struct gdb_context* gdbctx)
788 /* FIXME: add support for address in packet */
789 assert(gdbctx->in_packet_len == 0);
790 if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
791 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
792 fprintf(stderr, "NIY: cont on %lu, while last thread is %lu\n",
793 gdbctx->exec_thread->tid, dbg_curr_thread->tid);
794 resume_debuggee(gdbctx, DBG_CONTINUE);
795 wait_for_debuggee(gdbctx);
796 return packet_reply_status(gdbctx);
799 static enum packet_return packet_continue_signal(struct gdb_context* gdbctx)
803 /* FIXME: add support for address in packet */
804 assert(gdbctx->in_packet_len == 2);
805 if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
806 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
807 fprintf(stderr, "NIY: cont/sig on %lu, while last thread is %lu\n",
808 gdbctx->exec_thread->tid, dbg_curr_thread->tid);
809 hex_from(&sig, gdbctx->in_packet, 1);
810 /* cannot change signals on the fly */
811 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
812 fprintf(stderr, "sigs: %u %u\n", sig, gdbctx->last_sig);
813 if (sig != gdbctx->last_sig)
815 resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
816 wait_for_debuggee(gdbctx);
817 return packet_reply_status(gdbctx);
820 static enum packet_return packet_detach(struct gdb_context* gdbctx)
822 detach_debuggee(gdbctx, FALSE);
823 return packet_ok | packet_last_f;
826 static enum packet_return packet_read_registers(struct gdb_context* gdbctx)
830 CONTEXT* pctx = &gdbctx->context;
832 assert(gdbctx->in_trap);
834 if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
836 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
840 packet_reply_open(gdbctx);
841 for (i = 0; i < cpu_num_regs; i++)
843 packet_reply_hex_to(gdbctx, cpu_register(pctx, i), 4);
845 packet_reply_close(gdbctx);
849 static enum packet_return packet_write_registers(struct gdb_context* gdbctx)
853 CONTEXT* pctx = &gdbctx->context;
855 assert(gdbctx->in_trap);
856 if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
858 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
861 if (gdbctx->in_packet_len < cpu_num_regs * 2) return packet_error;
863 for (i = 0; i < cpu_num_regs; i++)
864 hex_from(cpu_register(pctx, i), &gdbctx->in_packet[8 * i], 4);
865 if (pctx != &gdbctx->context && !SetThreadContext(gdbctx->other_thread->handle, pctx))
867 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
868 fprintf(stderr, "Cannot set context on thread %lu\n", gdbctx->other_thread->tid);
874 static enum packet_return packet_kill(struct gdb_context* gdbctx)
876 detach_debuggee(gdbctx, TRUE);
878 if (!gdbctx->extended)
879 /* dunno whether GDB cares or not */
883 /* assume we can't really answer something here */
884 /* return packet_done; */
887 static enum packet_return packet_thread(struct gdb_context* gdbctx)
892 switch (gdbctx->in_packet[0])
896 if (gdbctx->in_packet[1] == '-')
897 thread = -strtol(gdbctx->in_packet + 2, &end, 16);
899 thread = strtol(gdbctx->in_packet + 1, &end, 16);
900 if (end == NULL || end > gdbctx->in_packet + gdbctx->in_packet_len)
902 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
903 fprintf(stderr, "Cannot get threadid %*.*s\n",
904 gdbctx->in_packet_len - 1, gdbctx->in_packet_len - 1,
905 gdbctx->in_packet + 1);
908 if (gdbctx->in_packet[0] == 'c')
909 gdbctx->exec_thread = dbg_get_thread(gdbctx->process, thread);
911 gdbctx->other_thread = dbg_get_thread(gdbctx->process, thread);
914 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
915 fprintf(stderr, "Unknown thread sub-command %c\n", gdbctx->in_packet[0]);
920 static enum packet_return packet_read_memory(struct gdb_context* gdbctx)
923 size_t len, blk_len, nread;
927 assert(gdbctx->in_trap);
928 /* FIXME:check in_packet_len for reading %p,%x */
929 if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2) return packet_error;
930 if (len <= 0) return packet_error;
931 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
932 fprintf(stderr, "Read mem at %p for %u bytes\n", addr, len);
933 for (nread = 0; nread < len; nread += r, addr += r)
935 blk_len = min(sizeof(buffer), len - nread);
936 if (!ReadProcessMemory(gdbctx->process->handle, addr, buffer, blk_len, &r) ||
939 /* fail at first address, return error */
940 if (nread == 0) return packet_reply_error(gdbctx, EFAULT);
941 /* something has already been read, return partial information */
944 if (nread == 0) packet_reply_open(gdbctx);
945 packet_reply_hex_to(gdbctx, buffer, r);
947 packet_reply_close(gdbctx);
951 static enum packet_return packet_write_memory(struct gdb_context* gdbctx)
959 assert(gdbctx->in_trap);
960 ptr = memchr(gdbctx->in_packet, ':', gdbctx->in_packet_len);
963 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
964 fprintf(stderr, "Cannot find ':' in %*.*s\n",
965 gdbctx->in_packet_len, gdbctx->in_packet_len, gdbctx->in_packet);
970 if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2)
972 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
973 fprintf(stderr, "Cannot scan addr,len in %s\n", gdbctx->in_packet);
976 if (ptr - gdbctx->in_packet + len * 2 != gdbctx->in_packet_len)
978 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
979 fprintf(stderr, "Wrong sizes %u <> %u\n",
980 ptr - gdbctx->in_packet + len * 2, gdbctx->in_packet_len);
983 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
984 fprintf(stderr, "Write %u bytes at %p\n", len, addr);
987 blk_len = min(sizeof(buffer), len);
988 hex_from(buffer, ptr, blk_len);
992 ret = WriteProcessMemory(gdbctx->process->handle, addr, buffer, blk_len, &w);
993 if (!ret || w != blk_len)
1000 return packet_ok; /* FIXME: error while writing ? */
1003 static enum packet_return packet_write_register(struct gdb_context* gdbctx)
1009 CONTEXT* pctx = &gdbctx->context;
1011 assert(gdbctx->in_trap);
1013 ptr = memchr(gdbctx->in_packet, '=', gdbctx->in_packet_len);
1015 reg = strtoul(gdbctx->in_packet, &end, 16);
1016 if (end == NULL || reg > cpu_num_regs)
1018 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1019 fprintf(stderr, "Invalid register index %s\n", gdbctx->in_packet);
1020 /* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
1021 * it wouldn't matter too much, and it fakes our support for all regs
1023 return (end == NULL) ? packet_error : packet_ok;
1025 if (ptr + 8 - gdbctx->in_packet != gdbctx->in_packet_len)
1027 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1028 fprintf(stderr, "Wrong sizes %u <> %u\n",
1029 ptr + 8 - gdbctx->in_packet, gdbctx->in_packet_len);
1030 return packet_error;
1032 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1033 fprintf(stderr, "Writing reg %u <= %*.*s\n",
1034 reg, gdbctx->in_packet_len - (ptr - gdbctx->in_packet),
1035 gdbctx->in_packet_len - (ptr - gdbctx->in_packet), ptr);
1037 if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
1039 if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
1040 return packet_error;
1043 hex_from(cpu_register(pctx, reg), ptr, 4);
1044 if (pctx != &gdbctx->context && !SetThreadContext(gdbctx->other_thread->handle, pctx))
1046 if (gdbctx->trace & GDBPXY_TRC_WIN32_ERROR)
1047 fprintf(stderr, "Cannot set context for thread %lu\n", gdbctx->other_thread->tid);
1048 return packet_error;
1054 static void packet_query_monitor_wnd_helper(struct gdb_context* gdbctx, HWND hWnd, int indent)
1062 if (!GetClassName(hWnd, clsName, sizeof(clsName)))
1063 strcpy(clsName, "-- Unknown --");
1064 if (!GetWindowText(hWnd, wndName, sizeof(wndName)))
1065 strcpy(wndName, "-- Empty --");
1067 packet_reply_open(gdbctx);
1068 packet_reply_catc(gdbctx, 'O');
1069 snprintf(buffer, sizeof(buffer),
1070 "%*s%04x%*s%-17.17s %08lx %08lx %.14s\n",
1071 indent, "", (UINT)hWnd, 13 - indent, "",
1072 clsName, GetWindowLong(hWnd, GWL_STYLE),
1073 GetWindowLong(hWnd, GWL_WNDPROC), wndName);
1074 packet_reply_hex_to_str(gdbctx, buffer);
1075 packet_reply_close(gdbctx);
1077 if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
1078 packet_query_monitor_wnd_helper(gdbctx, child, indent + 1);
1079 } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
1082 static void packet_query_monitor_wnd(struct gdb_context* gdbctx, int len, const char* str)
1086 /* we do the output in several 'O' packets, with the last one being just OK for
1087 * marking the end of the output */
1088 packet_reply_open(gdbctx);
1089 packet_reply_catc(gdbctx, 'O');
1090 snprintf(buffer, sizeof(buffer),
1091 "%-16.16s %-17.17s %-8.8s %s\n",
1092 "hwnd", "Class Name", " Style", " WndProc Text");
1093 packet_reply_hex_to_str(gdbctx, buffer);
1094 packet_reply_close(gdbctx);
1096 /* FIXME: could also add a pmt to this command in str... */
1097 packet_query_monitor_wnd_helper(gdbctx, GetDesktopWindow(), 0);
1098 packet_reply(gdbctx, "OK", 2);
1101 static void packet_query_monitor_process(struct gdb_context* gdbctx, int len, const char* str)
1103 HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
1106 PROCESSENTRY32 entry;
1109 if (snap == INVALID_HANDLE_VALUE)
1112 entry.dwSize = sizeof(entry);
1113 ok = Process32First(snap, &entry);
1115 /* we do the output in several 'O' packets, with the last one being just OK for
1116 * marking the end of the output */
1118 packet_reply_open(gdbctx);
1119 packet_reply_catc(gdbctx, 'O');
1120 snprintf(buffer, sizeof(buffer),
1121 " %-8.8s %-8.8s %-8.8s %s\n",
1122 "pid", "threads", "parent", "executable");
1123 packet_reply_hex_to_str(gdbctx, buffer);
1124 packet_reply_close(gdbctx);
1129 if (entry.th32ProcessID == gdbctx->process->pid) deco = '>';
1130 packet_reply_open(gdbctx);
1131 packet_reply_catc(gdbctx, 'O');
1132 snprintf(buffer, sizeof(buffer),
1133 "%c%08lx %-8ld %08lx '%s'\n",
1134 deco, entry.th32ProcessID, entry.cntThreads,
1135 entry.th32ParentProcessID, entry.szExeFile);
1136 packet_reply_hex_to_str(gdbctx, buffer);
1137 packet_reply_close(gdbctx);
1138 ok = Process32Next(snap, &entry);
1141 packet_reply(gdbctx, "OK", 2);
1144 static void packet_query_monitor_mem(struct gdb_context* gdbctx, int len, const char* str)
1146 MEMORY_BASIC_INFORMATION mbi;
1153 /* we do the output in several 'O' packets, with the last one being just OK for
1154 * marking the end of the output */
1155 packet_reply_open(gdbctx);
1156 packet_reply_catc(gdbctx, 'O');
1157 packet_reply_hex_to_str(gdbctx, "Address Size State Type RWX\n");
1158 packet_reply_close(gdbctx);
1160 while (VirtualQueryEx(gdbctx->process->handle, addr, &mbi, sizeof(mbi)) >= sizeof(mbi))
1164 case MEM_COMMIT: state = "commit "; break;
1165 case MEM_FREE: state = "free "; break;
1166 case MEM_RESERVE: state = "reserve"; break;
1167 default: state = "??? "; break;
1169 if (mbi.State != MEM_FREE)
1173 case MEM_IMAGE: type = "image "; break;
1174 case MEM_MAPPED: type = "mapped "; break;
1175 case MEM_PRIVATE: type = "private"; break;
1176 case 0: type = " "; break;
1177 default: type = "??? "; break;
1179 memset(prot, ' ' , sizeof(prot)-1);
1180 prot[sizeof(prot)-1] = '\0';
1181 if (mbi.AllocationProtect & (PAGE_READONLY|PAGE_READWRITE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
1183 if (mbi.AllocationProtect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE))
1185 if (mbi.AllocationProtect & (PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
1187 if (mbi.AllocationProtect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
1195 packet_reply_open(gdbctx);
1196 snprintf(buffer, sizeof(buffer),
1197 "%08lx %08lx %s %s %s\n",
1198 (DWORD)addr, mbi.RegionSize, state, type, prot);
1199 packet_reply_catc(gdbctx, 'O');
1200 packet_reply_hex_to_str(gdbctx, buffer);
1201 packet_reply_close(gdbctx);
1203 if (addr + mbi.RegionSize < addr) /* wrap around ? */
1205 addr += mbi.RegionSize;
1207 packet_reply(gdbctx, "OK", 2);
1210 static void packet_query_monitor_trace(struct gdb_context* gdbctx,
1211 int len, const char* str)
1217 snprintf(buffer, sizeof(buffer), "trace=%x\n", gdbctx->trace);
1219 else if (len >= 2 && str[0] == '=')
1221 unsigned val = atoi(&str[1]);
1222 snprintf(buffer, sizeof(buffer), "trace: %x => %x\n", gdbctx->trace, val);
1223 gdbctx->trace = val;
1227 /* FIXME: ugly but can use error packet here */
1228 packet_reply_cat(gdbctx, "E00");
1231 packet_reply_open(gdbctx);
1232 packet_reply_hex_to_str(gdbctx, buffer);
1233 packet_reply_close(gdbctx);
1241 void (*handler)(struct gdb_context*, int, const char*);
1244 {0, "wnd", 3, packet_query_monitor_wnd},
1245 {0, "window", 6, packet_query_monitor_wnd},
1246 {0, "proc", 4, packet_query_monitor_process},
1247 {0, "process", 7, packet_query_monitor_process},
1248 {0, "mem", 3, packet_query_monitor_mem},
1249 {1, "trace", 5, packet_query_monitor_trace},
1253 static enum packet_return packet_query_remote_command(struct gdb_context* gdbctx,
1254 const char* hxcmd, size_t len)
1257 struct query_detail* qd;
1259 assert((len & 1) == 0 && len < 2 * sizeof(buffer));
1261 hex_from(buffer, hxcmd, len);
1263 for (qd = &query_details[0]; qd->name != NULL; qd++)
1265 if (len < qd->len || strncmp(buffer, qd->name, qd->len) != 0) continue;
1266 if (!qd->with_arg && len != qd->len) continue;
1268 (qd->handler)(gdbctx, len - qd->len, buffer + qd->len);
1271 return packet_reply_error(gdbctx, EINVAL);
1274 static enum packet_return packet_query(struct gdb_context* gdbctx)
1276 switch (gdbctx->in_packet[0])
1279 if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
1281 struct dbg_thread* thd;
1283 packet_reply_open(gdbctx);
1284 packet_reply_add(gdbctx, "m", 1);
1285 for (thd = gdbctx->process->threads; thd; thd = thd->next)
1287 packet_reply_val(gdbctx, thd->tid, 4);
1288 if (thd->next != NULL)
1289 packet_reply_add(gdbctx, ",", 1);
1291 packet_reply_close(gdbctx);
1294 else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
1298 packet_reply_open(gdbctx);
1299 packet_reply_catc(gdbctx, 'O');
1300 get_process_info(gdbctx, result, sizeof(result));
1301 packet_reply_hex_to_str(gdbctx, result);
1302 packet_reply_close(gdbctx);
1307 if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
1309 packet_reply(gdbctx, "l", 1);
1312 else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
1314 packet_reply(gdbctx, "l", 1);
1319 if (gdbctx->in_packet_len == 1)
1321 struct dbg_thread* thd;
1322 /* FIXME: doc says 16 bit val ??? */
1323 /* grab first created thread, aka last in list */
1324 assert(gdbctx->process && gdbctx->process->threads);
1325 for (thd = gdbctx->process->threads; thd->next; thd = thd->next);
1326 packet_reply_open(gdbctx);
1327 packet_reply_add(gdbctx, "QC", 2);
1328 packet_reply_val(gdbctx, thd->tid, 4);
1329 packet_reply_close(gdbctx);
1334 if (strncmp(gdbctx->in_packet, "Offsets", gdbctx->in_packet_len) == 0)
1338 if (gdbctx->wine_segs[0] == 0 && gdbctx->wine_segs[1] == 0 &&
1339 gdbctx->wine_segs[2] == 0)
1340 return packet_error;
1341 snprintf(buf, sizeof(buf),
1342 "Text=%08lx;Data=%08lx;Bss=%08lx",
1343 gdbctx->wine_segs[0], gdbctx->wine_segs[1],
1344 gdbctx->wine_segs[2]);
1345 return packet_reply(gdbctx, buf, -1);
1349 if (gdbctx->in_packet_len > 5 && strncmp(gdbctx->in_packet, "Rcmd,", 5) == 0)
1351 return packet_query_remote_command(gdbctx, gdbctx->in_packet + 5,
1352 gdbctx->in_packet_len - 5);
1356 if (strncmp(gdbctx->in_packet, "Symbol::", gdbctx->in_packet_len) == 0)
1360 if (gdbctx->in_packet_len > 15 &&
1361 strncmp(gdbctx->in_packet, "ThreadExtraInfo", 15) == 0 &&
1362 gdbctx->in_packet[15] == ',')
1368 tid = strtol(gdbctx->in_packet + 16, &end, 16);
1369 if (end == NULL) break;
1370 get_thread_info(gdbctx, tid, result, sizeof(result));
1371 packet_reply_open(gdbctx);
1372 packet_reply_hex_to_str(gdbctx, result);
1373 packet_reply_close(gdbctx);
1378 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1379 fprintf(stderr, "Unknown or malformed query %*.*s\n",
1380 gdbctx->in_packet_len, gdbctx->in_packet_len, gdbctx->in_packet);
1381 return packet_error;
1384 static enum packet_return packet_step(struct gdb_context* gdbctx)
1386 /* FIXME: add support for address in packet */
1387 assert(gdbctx->in_packet_len == 0);
1388 if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
1389 if (gdbctx->trace & GDBPXY_TRC_COMMAND_FIXME)
1390 fprintf(stderr, "NIY: step on %lu, while last thread is %lu\n",
1391 gdbctx->exec_thread->tid, dbg_curr_thread->tid);
1392 be_cpu->single_step(&gdbctx->context, TRUE);
1393 resume_debuggee(gdbctx, DBG_CONTINUE);
1394 wait_for_debuggee(gdbctx);
1395 be_cpu->single_step(&gdbctx->context, FALSE);
1396 return packet_reply_status(gdbctx);
1400 static enum packet_return packet_step_signal(struct gdb_context* gdbctx)
1404 /* FIXME: add support for address in packet */
1405 assert(gdbctx->in_packet_len == 2);
1406 if (dbg_curr_thread->tid != gdbctx->exec_thread && gdbctx->exec_thread)
1407 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1408 fprintf(stderr, "NIY: step/sig on %u, while last thread is %u\n",
1409 gdbctx->exec_thread, DEBUG_CurrThread->tid);
1410 hex_from(&sig, gdbctx->in_packet, 1);
1411 /* cannot change signals on the fly */
1412 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1413 fprintf(stderr, "sigs: %u %u\n", sig, gdbctx->last_sig);
1414 if (sig != gdbctx->last_sig)
1415 return packet_error;
1416 resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
1417 wait_for_debuggee(gdbctx);
1418 return packet_reply_status(gdbctx);
1422 static enum packet_return packet_thread_alive(struct gdb_context* gdbctx)
1427 tid = strtol(gdbctx->in_packet, &end, 16);
1428 if (tid == -1 || tid == 0)
1429 return packet_reply_error(gdbctx, EINVAL);
1430 if (dbg_get_thread(gdbctx->process, tid) != NULL)
1432 return packet_reply_error(gdbctx, ESRCH);
1435 static enum packet_return packet_remove_breakpoint(struct gdb_context* gdbctx)
1439 struct gdb_ctx_Xpoint* xpt;
1440 enum be_xpoint_type t;
1442 /* FIXME: check packet_len */
1443 if (gdbctx->in_packet[0] < '0' || gdbctx->in_packet[0] > '4' ||
1444 gdbctx->in_packet[1] != ',' ||
1445 sscanf(gdbctx->in_packet + 2, "%p,%x", &addr, &len) != 2)
1446 return packet_error;
1447 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1448 fprintf(stderr, "Remove bp %p[%u] typ=%c\n",
1449 addr, len, gdbctx->in_packet[0]);
1450 switch (gdbctx->in_packet[0])
1452 case '0': t = be_xpoint_break; len = 0; break;
1453 case '1': t = be_xpoint_watch_exec; break;
1454 case '2': t = be_xpoint_watch_read; break;
1455 case '3': t = be_xpoint_watch_write; break;
1456 default: return packet_error;
1458 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1460 if (xpt->addr == addr && xpt->type == t)
1462 if (be_cpu->remove_Xpoint(gdbctx->process->handle, &gdbctx->context,
1463 t, xpt->addr, xpt->val, len))
1471 return packet_error;
1474 static enum packet_return packet_set_breakpoint(struct gdb_context* gdbctx)
1478 struct gdb_ctx_Xpoint* xpt;
1479 enum be_xpoint_type t;
1481 /* FIXME: check packet_len */
1482 if (gdbctx->in_packet[0] < '0' || gdbctx->in_packet[0] > '4' ||
1483 gdbctx->in_packet[1] != ',' ||
1484 sscanf(gdbctx->in_packet + 2, "%p,%x", &addr, &len) != 2)
1485 return packet_error;
1486 if (gdbctx->trace & GDBPXY_TRC_COMMAND)
1487 fprintf(stderr, "Set bp %p[%u] typ=%c\n",
1488 addr, len, gdbctx->in_packet[0]);
1489 switch (gdbctx->in_packet[0])
1491 case '0': t = be_xpoint_break; len = 0; break;
1492 case '1': t = be_xpoint_watch_exec; break;
1493 case '2': t = be_xpoint_watch_read; break;
1494 case '3': t = be_xpoint_watch_write; break;
1495 default: return packet_error;
1497 /* because of packet command handling, this should be made idempotent */
1498 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1500 if (xpt->addr == addr && xpt->type == t)
1501 return packet_ok; /* nothing to do */
1503 /* really set the Xpoint */
1504 for (xpt = &gdbctx->Xpoints[NUM_XPOINT - 1]; xpt >= gdbctx->Xpoints; xpt--)
1506 if (xpt->type == -1)
1508 if (be_cpu->insert_Xpoint(gdbctx->process->handle, &gdbctx->context,
1509 t, addr, &xpt->val, len))
1515 fprintf(stderr, "cannot set xpoint\n");
1519 /* no more entries... eech */
1520 fprintf(stderr, "Running out of spots for {break|watch}points\n");
1521 return packet_error;
1524 /* =============================================== *
1525 * P A C K E T I N F R A S T R U C T U R E *
1526 * =============================================== *
1532 enum packet_return (*handler)(struct gdb_context* gdbctx);
1535 static struct packet_entry packet_entries[] =
1537 /* {'!', packet_extended}, */
1538 {'?', packet_last_signal},
1539 {'c', packet_continue},
1540 {'C', packet_continue_signal},
1541 {'D', packet_detach},
1542 {'g', packet_read_registers},
1543 {'G', packet_write_registers},
1545 {'H', packet_thread},
1546 {'m', packet_read_memory},
1547 {'M', packet_write_memory},
1548 /* {'p', packet_read_register}, doesn't seem needed */
1549 {'P', packet_write_register},
1550 {'q', packet_query},
1552 /*{'S', packet_step_signal}, hard(er) to implement */
1553 {'T', packet_thread_alive},
1554 {'z', packet_remove_breakpoint},
1555 {'Z', packet_set_breakpoint},
1558 static BOOL extract_packets(struct gdb_context* gdbctx)
1562 unsigned char in_cksum, loc_cksum;
1564 enum packet_return ret = packet_error;
1567 while ((ret & packet_last_f) == 0)
1569 if (gdbctx->in_len && (gdbctx->trace & GDBPXY_TRC_LOWLEVEL))
1570 fprintf(stderr, "In-buf: %*.*s\n",
1571 gdbctx->in_len, gdbctx->in_len, gdbctx->in_buf);
1572 ptr = memchr(gdbctx->in_buf, '$', gdbctx->in_len);
1573 if (ptr == NULL) return FALSE;
1574 if (ptr != gdbctx->in_buf)
1576 int glen = ptr - gdbctx->in_buf; /* garbage len */
1577 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1578 fprintf(stderr, "Removing garbage: %*.*s\n",
1579 glen, glen, gdbctx->in_buf);
1580 gdbctx->in_len -= glen;
1581 memmove(gdbctx->in_buf, ptr, gdbctx->in_len);
1583 end = memchr(gdbctx->in_buf + 1, '#', gdbctx->in_len);
1584 if (end == NULL) return FALSE;
1585 /* no checksum yet */
1586 if (end + 3 > gdbctx->in_buf + gdbctx->in_len) return FALSE;
1587 plen = end - gdbctx->in_buf - 1;
1588 hex_from(&in_cksum, end + 1, 1);
1589 loc_cksum = checksum(gdbctx->in_buf + 1, plen);
1590 if (loc_cksum == in_cksum)
1592 if (num_packet == 0) {
1597 write(gdbctx->sock, "+", 1);
1600 /* FIXME: should use bsearch if packet_entries was sorted */
1601 for (i = 0; i < sizeof(packet_entries)/sizeof(packet_entries[0]); i++)
1603 if (packet_entries[i].key == gdbctx->in_buf[1]) break;
1605 if (i == sizeof(packet_entries)/sizeof(packet_entries[0]))
1607 if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
1608 fprintf(stderr, "Unknown packet request %*.*s\n",
1609 plen, plen, &gdbctx->in_buf[1]);
1613 gdbctx->in_packet = gdbctx->in_buf + 2;
1614 gdbctx->in_packet_len = plen - 1;
1615 if (gdbctx->trace & GDBPXY_TRC_PACKET)
1616 fprintf(stderr, "Packet: %c%*.*s\n",
1618 gdbctx->in_packet_len, gdbctx->in_packet_len,
1620 ret = (packet_entries[i].handler)(gdbctx);
1622 switch (ret & ~packet_last_f)
1624 case packet_error: packet_reply(gdbctx, "", 0); break;
1625 case packet_ok: packet_reply(gdbctx, "OK", 2); break;
1626 case packet_done: break;
1628 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1629 fprintf(stderr, "Reply-full: %*.*s\n",
1630 gdbctx->out_len, gdbctx->out_len, gdbctx->out_buf);
1631 i = write(gdbctx->sock, gdbctx->out_buf, gdbctx->out_len);
1632 assert(i == gdbctx->out_len);
1633 /* if this fails, we'll have to use POLLOUT...
1635 gdbctx->out_len = 0;
1640 /* FIXME: if we have in our input buffer more than one packet,
1641 * it's very likely that we took too long to answer to a given packet
1642 * and gdb is sending us again the same packet
1643 * We simply drop the second packet. This will lower the risk of error,
1644 * but there's still some race conditions here
1645 * A better fix (yet not perfect) would be to have two threads:
1646 * - one managing the packets for gdb
1647 * - the second one managing the commands...
1648 * This would allow us also the reply with the '+' character (Ack of
1649 * the command) way sooner than what we do now
1651 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1652 fprintf(stderr, "Dropping packet, I was too slow to respond\n");
1657 write(gdbctx->sock, "+", 1);
1658 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1659 fprintf(stderr, "Dropping packet, invalid checksum %d <> %d\n", in_cksum, loc_cksum);
1661 gdbctx->in_len -= plen + 4;
1662 memmove(gdbctx->in_buf, end + 3, gdbctx->in_len);
1667 static int fetch_data(struct gdb_context* gdbctx)
1669 int len, in_len = gdbctx->in_len;
1671 assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
1675 if (gdbctx->in_len + STEP > gdbctx->in_buf_alloc)
1676 gdbctx->in_buf = realloc(gdbctx->in_buf, gdbctx->in_buf_alloc += STEP);
1678 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1679 fprintf(stderr, "%d %d %*.*s\n",
1680 gdbctx->in_len, gdbctx->in_buf_alloc,
1681 gdbctx->in_len, gdbctx->in_len, gdbctx->in_buf);
1682 len = read(gdbctx->sock, gdbctx->in_buf + gdbctx->in_len, gdbctx->in_buf_alloc - gdbctx->in_len);
1683 if (len <= 0) break;
1684 gdbctx->in_len += len;
1685 assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
1686 if (len < gdbctx->in_buf_alloc - gdbctx->in_len) break;
1688 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1689 fprintf(stderr, "=> %d\n", gdbctx->in_len - in_len);
1690 return gdbctx->in_len - in_len;
1693 static BOOL gdb_exec(const char* wine_path, unsigned port, unsigned flags)
1697 const char* gdb_path;
1700 if (!(gdb_path = getenv("WINE_GDB"))) gdb_path = "gdb";
1701 strcpy(buf,"/tmp/winegdb.XXXXXX");
1702 fd = mkstemps(buf, 0);
1703 if (fd == -1) return FALSE;
1704 if ((f = fdopen(fd, "w+")) == NULL) return FALSE;
1705 fprintf(f, "file %s\n", wine_path);
1706 fprintf(f, "target remote localhost:%d\n", ntohs(port));
1707 fprintf(f, "monitor trace=%d\n", GDBPXY_TRC_COMMAND_FIXME);
1708 fprintf(f, "set prompt Wine-gdb>\\ \n");
1709 /* gdb 5.1 seems to require it, won't hurt anyway */
1710 fprintf(f, "sharedlibrary\n");
1711 /* This is needed (but not a decent & final fix)
1712 * Without this, gdb would skip our inter-DLL relay code (because
1713 * we don't have any line number information for the relay code)
1714 * With this, we will stop on first instruction of the stub, and
1715 * reusing step, will get us through the relay stub at the actual
1716 * function we're looking at.
1718 fprintf(f, "set step-mode on\n");
1719 /* tell gdb to delete this file when done handling it... */
1720 fprintf(f, "shell rm -f \"%s\"\n", buf);
1723 execlp("xterm", "xterm", "-e", gdb_path, "-x", buf, NULL);
1725 execlp(gdb_path, gdb_path, "-x", buf, NULL);
1726 assert(0); /* never reached */
1730 static BOOL gdb_startup(struct gdb_context* gdbctx, DEBUG_EVENT* de, unsigned flags)
1733 struct sockaddr_in s_addrs;
1734 int s_len = sizeof(s_addrs);
1735 struct pollfd pollfd;
1736 IMAGEHLP_MODULE imh_mod;
1738 /* step 1: create socket for gdb connection request */
1739 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
1741 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1742 fprintf(stderr, "Can't create socket");
1746 if (listen(sock, 1) == -1 ||
1747 getsockname(sock, (struct sockaddr*)&s_addrs, &s_len) == -1)
1750 /* step 2: do the process internal creation */
1751 handle_debug_event(gdbctx, de);
1753 /* step3: get the wine loader name */
1754 if (!dbg_get_debuggee_info(gdbctx->process->handle, &imh_mod)) return FALSE;
1756 /* step 4: fire up gdb (if requested) */
1758 fprintf(stderr, "target remote localhost:%d\n", ntohs(s_addrs.sin_port));
1762 case -1: /* error in parent... */
1763 fprintf(stderr, "Cannot create gdb\n");
1766 default: /* in parent... success */
1768 case 0: /* in child... and alive */
1769 gdb_exec(imh_mod.LoadedImageName, s_addrs.sin_port, flags);
1770 /* if we're here, exec failed, so report failure */
1774 /* step 5: wait for gdb to connect actually */
1776 pollfd.events = POLLIN;
1779 switch (poll(&pollfd, 1, -1))
1782 if (pollfd.revents & POLLIN)
1785 gdbctx->sock = accept(sock, (struct sockaddr*)&s_addrs, &s_len);
1786 if (gdbctx->sock == -1)
1788 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1789 fprintf(stderr, "Connected on %d\n", gdbctx->sock);
1790 /* don't keep our small packets too long: send them ASAP back to GDB
1791 * without this, GDB really crawls
1793 setsockopt(gdbctx->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&dummy, sizeof(dummy));
1797 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1798 fprintf(stderr, "Poll for cnx failed (timeout)\n");
1801 if (gdbctx->trace & GDBPXY_TRC_LOWLEVEL)
1802 fprintf(stderr, "Poll for cnx failed (error)\n");
1812 static BOOL gdb_init_context(struct gdb_context* gdbctx, unsigned flags)
1818 gdbctx->in_buf = NULL;
1819 gdbctx->in_buf_alloc = 0;
1821 gdbctx->out_buf = NULL;
1822 gdbctx->out_buf_alloc = 0;
1823 gdbctx->out_len = 0;
1824 gdbctx->out_curr_packet = -1;
1826 gdbctx->exec_thread = gdbctx->other_thread = NULL;
1827 gdbctx->last_sig = 0;
1828 gdbctx->in_trap = FALSE;
1829 gdbctx->trace = /*GDBPXY_TRC_PACKET | GDBPXY_TRC_COMMAND |*/ GDBPXY_TRC_COMMAND_ERROR | GDBPXY_TRC_COMMAND_FIXME | GDBPXY_TRC_WIN32_EVENT;
1830 gdbctx->process = NULL;
1831 for (i = 0; i < NUM_XPOINT; i++)
1832 gdbctx->Xpoints[i].type = -1;
1834 /* wait for first trap */
1835 while (WaitForDebugEvent(&de, INFINITE))
1837 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
1839 /* this should be the first event we get,
1840 * and the only one of this type */
1841 assert(gdbctx->process == NULL && de.dwProcessId == dbg_curr_pid);
1842 /* gdbctx->dwProcessId = pid; */
1843 if (!gdb_startup(gdbctx, &de, flags)) return FALSE;
1844 assert(!gdbctx->in_trap);
1848 handle_debug_event(gdbctx, &de);
1849 if (gdbctx->in_trap) break;
1851 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1856 BOOL gdb_remote(unsigned flags)
1858 struct pollfd pollfd;
1859 struct gdb_context gdbctx;
1862 for (doLoop = gdb_init_context(&gdbctx, flags); doLoop;)
1864 pollfd.fd = gdbctx.sock;
1865 pollfd.events = POLLIN;
1868 switch (poll(&pollfd, 1, -1))
1872 if (pollfd.revents & (POLLHUP | POLLERR))
1874 if (gdbctx.trace & GDBPXY_TRC_LOWLEVEL)
1875 fprintf(stderr, "Gdb hung up\n");
1876 /* kill also debuggee process - questionnable - */
1877 detach_debuggee(&gdbctx, TRUE);
1881 if ((pollfd.revents & POLLIN) && fetch_data(&gdbctx) > 0)
1883 if (extract_packets(&gdbctx)) doLoop = FALSE;
1887 /* timeout, should never happen (infinite timeout) */
1890 if (gdbctx.trace & GDBPXY_TRC_LOWLEVEL)
1891 fprintf(stderr, "Poll failed\n");