gdbproxy.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. /*
  2. * A Win32 based proxy implementing the GBD remote protocol.
  3. * This makes it possible to debug Wine (and any "emulated"
  4. * program) under Linux using GDB.
  5. *
  6. * Copyright (c) Eric Pouech 2002-2004
  7. *
  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.
  12. *
  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.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. /* Protocol specification can be found here:
  23. * http://sources.redhat.com/gdb/onlinedocs/gdb/Maintenance-Commands.html
  24. */
  25. #define NONAMELESSUNION
  26. #define NONAMELESSSTRUCT
  27. #include <assert.h>
  28. #include <fcntl.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "debugger.h"
  33. #include "windef.h"
  34. #include "winbase.h"
  35. #include "winsock2.h"
  36. #include "tlhelp32.h"
  37. #include "wine/debug.h"
  38. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  39. struct gdb_xpoint
  40. {
  41. struct list entry;
  42. int pid;
  43. int tid;
  44. enum be_xpoint_type type;
  45. void *addr;
  46. int size;
  47. unsigned int value;
  48. };
  49. struct reply_buffer
  50. {
  51. unsigned char* base;
  52. size_t len;
  53. size_t alloc;
  54. };
  55. #define QX_NAME_SIZE 32
  56. #define QX_ANNEX_SIZE MAX_PATH
  57. struct gdb_context
  58. {
  59. /* gdb information */
  60. SOCKET sock;
  61. /* incoming buffer */
  62. char* in_buf;
  63. int in_buf_alloc;
  64. int in_len;
  65. /* split into individual packet */
  66. char* in_packet;
  67. int in_packet_len;
  68. /* outgoing buffer */
  69. struct reply_buffer out_buf;
  70. int out_curr_packet;
  71. /* generic GDB thread information */
  72. int exec_tid; /* tid used in step & continue */
  73. int other_tid; /* tid to be used in any other operation */
  74. struct list xpoint_list;
  75. /* current Win32 trap env */
  76. DEBUG_EVENT de;
  77. DWORD de_reply;
  78. /* Win32 information */
  79. struct dbg_process* process;
  80. /* Unix environment */
  81. ULONG_PTR wine_segs[3]; /* load addresses of the ELF wine exec segments (text, bss and data) */
  82. BOOL no_ack_mode;
  83. int qxfer_object_idx;
  84. char qxfer_object_annex[QX_ANNEX_SIZE];
  85. struct reply_buffer qxfer_buffer;
  86. };
  87. /* assume standard signal and errno values */
  88. enum host_error
  89. {
  90. HOST_EPERM = 1,
  91. HOST_ENOENT = 2,
  92. HOST_ESRCH = 3,
  93. HOST_ENOMEM = 12,
  94. HOST_EFAULT = 14,
  95. HOST_EINVAL = 22,
  96. };
  97. enum host_signal
  98. {
  99. HOST_SIGINT = 2,
  100. HOST_SIGILL = 4,
  101. HOST_SIGTRAP = 5,
  102. HOST_SIGABRT = 6,
  103. HOST_SIGFPE = 8,
  104. HOST_SIGBUS = 10,
  105. HOST_SIGSEGV = 11,
  106. HOST_SIGALRM = 14,
  107. HOST_SIGTERM = 15,
  108. };
  109. static void gdbctx_delete_xpoint(struct gdb_context *gdbctx, struct dbg_thread *thread,
  110. dbg_ctx_t *ctx, struct gdb_xpoint *x)
  111. {
  112. struct dbg_process *process = gdbctx->process;
  113. struct backend_cpu *cpu = process->be_cpu;
  114. if (!cpu->remove_Xpoint(process->handle, process->process_io, ctx, x->type, x->addr, x->value, x->size))
  115. ERR("%04x:%04x: Couldn't remove breakpoint at:%p/%x type:%d\n", process->pid, thread ? thread->tid : ~0, x->addr, x->size, x->type);
  116. list_remove(&x->entry);
  117. HeapFree(GetProcessHeap(), 0, x);
  118. }
  119. static void gdbctx_insert_xpoint(struct gdb_context *gdbctx, struct dbg_thread *thread,
  120. dbg_ctx_t *ctx, enum be_xpoint_type type, void *addr, int size)
  121. {
  122. struct dbg_process *process = thread->process;
  123. struct backend_cpu *cpu = process->be_cpu;
  124. struct gdb_xpoint *x;
  125. unsigned int value;
  126. if (!cpu->insert_Xpoint(process->handle, process->process_io, ctx, type, addr, &value, size))
  127. {
  128. ERR("%04x:%04x: Couldn't insert breakpoint at:%p/%x type:%d\n", process->pid, thread->tid, addr, size, type);
  129. return;
  130. }
  131. if (!(x = HeapAlloc(GetProcessHeap(), 0, sizeof(struct gdb_xpoint))))
  132. {
  133. ERR("%04x:%04x: Couldn't allocate memory for breakpoint at:%p/%x type:%d\n", process->pid, thread->tid, addr, size, type);
  134. return;
  135. }
  136. x->pid = process->pid;
  137. x->tid = thread->tid;
  138. x->type = type;
  139. x->addr = addr;
  140. x->size = size;
  141. x->value = value;
  142. list_add_head(&gdbctx->xpoint_list, &x->entry);
  143. }
  144. static struct gdb_xpoint *gdb_find_xpoint(struct gdb_context *gdbctx, struct dbg_thread *thread,
  145. enum be_xpoint_type type, void *addr, int size)
  146. {
  147. struct gdb_xpoint *x;
  148. LIST_FOR_EACH_ENTRY(x, &gdbctx->xpoint_list, struct gdb_xpoint, entry)
  149. {
  150. if (thread && (x->pid != thread->process->pid || x->tid != thread->tid))
  151. continue;
  152. if (x->type == type && x->addr == addr && x->size == size)
  153. return x;
  154. }
  155. return NULL;
  156. }
  157. static BOOL tgt_process_gdbproxy_read(HANDLE hProcess, const void* addr,
  158. void* buffer, SIZE_T len, SIZE_T* rlen)
  159. {
  160. return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
  161. }
  162. static BOOL tgt_process_gdbproxy_write(HANDLE hProcess, void* addr,
  163. const void* buffer, SIZE_T len, SIZE_T* wlen)
  164. {
  165. return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
  166. }
  167. static struct be_process_io be_process_gdbproxy_io =
  168. {
  169. NULL, /* we shouldn't use close_process() in gdbproxy */
  170. tgt_process_gdbproxy_read,
  171. tgt_process_gdbproxy_write
  172. };
  173. /* =============================================== *
  174. * B A S I C M A N I P U L A T I O N S *
  175. * =============================================== *
  176. */
  177. static inline int hex_from0(char ch)
  178. {
  179. if (ch >= '0' && ch <= '9') return ch - '0';
  180. if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
  181. if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
  182. assert(0);
  183. return 0;
  184. }
  185. static inline unsigned char hex_to0(int x)
  186. {
  187. assert(x >= 0 && x < 16);
  188. return "0123456789abcdef"[x];
  189. }
  190. static void hex_from(void* dst, const char* src, size_t len)
  191. {
  192. unsigned char *p = dst;
  193. while (len--)
  194. {
  195. *p++ = (hex_from0(src[0]) << 4) | hex_from0(src[1]);
  196. src += 2;
  197. }
  198. }
  199. static void hex_to(char* dst, const void* src, size_t len)
  200. {
  201. const unsigned char *p = src;
  202. while (len--)
  203. {
  204. *dst++ = hex_to0(*p >> 4);
  205. *dst++ = hex_to0(*p & 0x0F);
  206. p++;
  207. }
  208. }
  209. static void reply_buffer_clear(struct reply_buffer* reply)
  210. {
  211. reply->len = 0;
  212. }
  213. static void reply_buffer_grow(struct reply_buffer* reply, size_t size)
  214. {
  215. size_t required_alloc = reply->len + size;
  216. if (reply->alloc < required_alloc)
  217. {
  218. reply->alloc = reply->alloc * 3 / 2;
  219. if (reply->alloc < required_alloc)
  220. reply->alloc = required_alloc;
  221. reply->base = realloc(reply->base, reply->alloc);
  222. }
  223. }
  224. static void reply_buffer_append(struct reply_buffer* reply, const void* data, size_t size)
  225. {
  226. reply_buffer_grow(reply, size);
  227. memcpy(reply->base + reply->len, data, size);
  228. reply->len += size;
  229. }
  230. static inline void reply_buffer_append_str(struct reply_buffer* reply, const char* str)
  231. {
  232. reply_buffer_append(reply, str, strlen(str));
  233. }
  234. static inline void reply_buffer_append_hex(struct reply_buffer* reply, const void* src, size_t len)
  235. {
  236. reply_buffer_grow(reply, len * 2);
  237. hex_to((char *)reply->base + reply->len, src, len);
  238. reply->len += len * 2;
  239. }
  240. static inline void reply_buffer_append_uinthex(struct reply_buffer* reply, ULONG_PTR val, int len)
  241. {
  242. char buf[sizeof(ULONG_PTR) * 2], *ptr;
  243. assert(len <= sizeof(ULONG_PTR));
  244. ptr = buf + len * 2;
  245. while (ptr != buf)
  246. {
  247. *--ptr = hex_to0(val & 0x0F);
  248. val >>= 4;
  249. }
  250. reply_buffer_append(reply, ptr, len * 2);
  251. }
  252. static const unsigned char xml_special_chars_lookup_table[16] = {
  253. /* The characters should be sorted by its value modulo table length. */
  254. 0x00, /* NUL */
  255. 0,
  256. 0x22, /* ": 0010|0010 */
  257. 0, 0, 0,
  258. 0x26, /* &: 0010|0110 */
  259. 0x27, /* ': 0010|0111 */
  260. 0, 0, 0, 0,
  261. 0x3C, /* <: 0011|1100 */
  262. 0,
  263. 0x3E, /* >: 0011|1110 */
  264. 0
  265. };
  266. static inline BOOL is_nul_or_xml_special_char(unsigned char val)
  267. {
  268. const size_t length = ARRAY_SIZE(xml_special_chars_lookup_table);
  269. return xml_special_chars_lookup_table[val % length] == val;
  270. }
  271. static void reply_buffer_append_xmlstr(struct reply_buffer* reply, const char* str)
  272. {
  273. const char *ptr = str, *curr;
  274. for (;;)
  275. {
  276. curr = ptr;
  277. while (!is_nul_or_xml_special_char((unsigned char)*ptr))
  278. ptr++;
  279. reply_buffer_append(reply, curr, ptr - curr);
  280. switch (*ptr++)
  281. {
  282. case '"': reply_buffer_append_str(reply, "&quot;"); break;
  283. case '&': reply_buffer_append_str(reply, "&amp;"); break;
  284. case '\'': reply_buffer_append_str(reply, "&apos;"); break;
  285. case '<': reply_buffer_append_str(reply, "&lt;"); break;
  286. case '>': reply_buffer_append_str(reply, "&gt;"); break;
  287. case '\0':
  288. default:
  289. return;
  290. }
  291. }
  292. }
  293. static unsigned char checksum(const void* data, int len)
  294. {
  295. unsigned cksum = 0;
  296. const unsigned char* ptr = data;
  297. while (len-- > 0)
  298. cksum += *ptr++;
  299. return cksum;
  300. }
  301. static inline void* cpu_register_ptr(struct gdb_context *gdbctx,
  302. dbg_ctx_t *ctx, unsigned idx)
  303. {
  304. assert(idx < gdbctx->process->be_cpu->gdb_num_regs);
  305. return (char*)ctx + gdbctx->process->be_cpu->gdb_register_map[idx].offset;
  306. }
  307. static inline DWORD64 cpu_register(struct gdb_context *gdbctx,
  308. dbg_ctx_t *ctx, unsigned idx)
  309. {
  310. switch (gdbctx->process->be_cpu->gdb_register_map[idx].length)
  311. {
  312. case 1: return *(BYTE*)cpu_register_ptr(gdbctx, ctx, idx);
  313. case 2: return *(WORD*)cpu_register_ptr(gdbctx, ctx, idx);
  314. case 4: return *(DWORD*)cpu_register_ptr(gdbctx, ctx, idx);
  315. case 8: return *(DWORD64*)cpu_register_ptr(gdbctx, ctx, idx);
  316. default:
  317. ERR("got unexpected size: %u\n",
  318. (unsigned)gdbctx->process->be_cpu->gdb_register_map[idx].length);
  319. assert(0);
  320. return 0;
  321. }
  322. }
  323. static inline void cpu_register_hex_from(struct gdb_context *gdbctx,
  324. dbg_ctx_t* ctx, unsigned idx, const char **phex)
  325. {
  326. const struct gdb_register *cpu_register_map = gdbctx->process->be_cpu->gdb_register_map;
  327. hex_from(cpu_register_ptr(gdbctx, ctx, idx), *phex, cpu_register_map[idx].length);
  328. }
  329. /* =============================================== *
  330. * W I N 3 2 D E B U G I N T E R F A C E *
  331. * =============================================== *
  332. */
  333. static struct dbg_thread* dbg_thread_from_tid(struct gdb_context* gdbctx, int tid)
  334. {
  335. struct dbg_process *process = gdbctx->process;
  336. struct dbg_thread *thread;
  337. if (!process) return NULL;
  338. if (tid == 0) tid = gdbctx->de.dwThreadId;
  339. LIST_FOR_EACH_ENTRY(thread, &process->threads, struct dbg_thread, entry)
  340. {
  341. if (tid > 0 && thread->tid != tid) continue;
  342. return thread;
  343. }
  344. return NULL;
  345. }
  346. static void dbg_thread_set_single_step(struct dbg_thread *thread, BOOL enable)
  347. {
  348. struct backend_cpu *backend;
  349. dbg_ctx_t ctx;
  350. if (!thread) return;
  351. if (!thread->process) return;
  352. if (!(backend = thread->process->be_cpu)) return;
  353. if (!backend->get_context(thread->handle, &ctx))
  354. {
  355. ERR("get_context failed for thread %04x:%04x\n", thread->process->pid, thread->tid);
  356. return;
  357. }
  358. backend->single_step(&ctx, enable);
  359. if (!backend->set_context(thread->handle, &ctx))
  360. ERR("set_context failed for thread %04x:%04x\n", thread->process->pid, thread->tid);
  361. }
  362. static unsigned char signal_from_debug_event(DEBUG_EVENT* de)
  363. {
  364. DWORD ec;
  365. if (de->dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
  366. return HOST_SIGTERM;
  367. if (de->dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
  368. return HOST_SIGTRAP;
  369. ec = de->u.Exception.ExceptionRecord.ExceptionCode;
  370. switch (ec)
  371. {
  372. case EXCEPTION_ACCESS_VIOLATION:
  373. case EXCEPTION_PRIV_INSTRUCTION:
  374. case EXCEPTION_STACK_OVERFLOW:
  375. case EXCEPTION_GUARD_PAGE:
  376. return HOST_SIGSEGV;
  377. case EXCEPTION_DATATYPE_MISALIGNMENT:
  378. return HOST_SIGBUS;
  379. case EXCEPTION_SINGLE_STEP:
  380. case EXCEPTION_BREAKPOINT:
  381. return HOST_SIGTRAP;
  382. case EXCEPTION_FLT_DENORMAL_OPERAND:
  383. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  384. case EXCEPTION_FLT_INEXACT_RESULT:
  385. case EXCEPTION_FLT_INVALID_OPERATION:
  386. case EXCEPTION_FLT_OVERFLOW:
  387. case EXCEPTION_FLT_STACK_CHECK:
  388. case EXCEPTION_FLT_UNDERFLOW:
  389. return HOST_SIGFPE;
  390. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  391. case EXCEPTION_INT_OVERFLOW:
  392. return HOST_SIGFPE;
  393. case EXCEPTION_ILLEGAL_INSTRUCTION:
  394. return HOST_SIGILL;
  395. case CONTROL_C_EXIT:
  396. return HOST_SIGINT;
  397. case STATUS_POSSIBLE_DEADLOCK:
  398. return HOST_SIGALRM;
  399. /* should not be here */
  400. case EXCEPTION_INVALID_HANDLE:
  401. case EXCEPTION_WINE_NAME_THREAD:
  402. return HOST_SIGTRAP;
  403. default:
  404. ERR("Unknown exception code 0x%08x\n", ec);
  405. return HOST_SIGABRT;
  406. }
  407. }
  408. static BOOL handle_exception(struct gdb_context* gdbctx, EXCEPTION_DEBUG_INFO* exc)
  409. {
  410. EXCEPTION_RECORD* rec = &exc->ExceptionRecord;
  411. switch (rec->ExceptionCode)
  412. {
  413. case EXCEPTION_WINE_NAME_THREAD:
  414. {
  415. const THREADNAME_INFO *threadname = (const THREADNAME_INFO *)rec->ExceptionInformation;
  416. struct dbg_thread *thread;
  417. char name[9];
  418. SIZE_T read;
  419. if (threadname->dwThreadID == -1)
  420. thread = dbg_get_thread(gdbctx->process, gdbctx->de.dwThreadId);
  421. else
  422. thread = dbg_get_thread(gdbctx->process, threadname->dwThreadID);
  423. if (thread)
  424. {
  425. if (gdbctx->process->process_io->read( gdbctx->process->handle,
  426. threadname->szName, name, sizeof(name), &read) && read == sizeof(name))
  427. {
  428. fprintf(stderr, "Thread ID=%04x renamed to \"%.9s\"\n",
  429. threadname->dwThreadID, name);
  430. }
  431. }
  432. else
  433. ERR("Cannot set name of thread %04x\n", threadname->dwThreadID);
  434. return TRUE;
  435. }
  436. case EXCEPTION_INVALID_HANDLE:
  437. return TRUE;
  438. default:
  439. return FALSE;
  440. }
  441. }
  442. static BOOL handle_debug_event(struct gdb_context* gdbctx, BOOL stop_on_dll_load_unload)
  443. {
  444. DEBUG_EVENT *de = &gdbctx->de;
  445. struct dbg_thread *thread;
  446. union {
  447. char bufferA[256];
  448. WCHAR buffer[256];
  449. } u;
  450. DWORD size;
  451. gdbctx->exec_tid = de->dwThreadId;
  452. gdbctx->other_tid = de->dwThreadId;
  453. gdbctx->de_reply = DBG_REPLY_LATER;
  454. switch (de->dwDebugEventCode)
  455. {
  456. case CREATE_PROCESS_DEBUG_EVENT:
  457. gdbctx->process = dbg_add_process(&be_process_gdbproxy_io, de->dwProcessId,
  458. de->u.CreateProcessInfo.hProcess);
  459. if (!gdbctx->process)
  460. return TRUE;
  461. size = ARRAY_SIZE(u.buffer);
  462. QueryFullProcessImageNameW( gdbctx->process->handle, 0, u.buffer, &size );
  463. dbg_set_process_name(gdbctx->process, u.buffer);
  464. fprintf(stderr, "%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
  465. de->dwProcessId, de->dwThreadId,
  466. dbg_W2A(u.buffer, -1),
  467. de->u.CreateProcessInfo.lpImageName,
  468. de->u.CreateProcessInfo.lpStartAddress,
  469. de->u.CreateProcessInfo.dwDebugInfoFileOffset,
  470. de->u.CreateProcessInfo.nDebugInfoSize);
  471. /* de->u.CreateProcessInfo.lpStartAddress; */
  472. if (!dbg_init(gdbctx->process->handle, u.buffer, TRUE))
  473. ERR("Couldn't initiate DbgHelp\n");
  474. fprintf(stderr, "%04x:%04x: create thread I @%p\n", de->dwProcessId,
  475. de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
  476. dbg_load_module(gdbctx->process->handle, de->u.CreateProcessInfo.hFile, u.buffer,
  477. (DWORD_PTR)de->u.CreateProcessInfo.lpBaseOfImage, 0);
  478. dbg_add_thread(gdbctx->process, de->dwThreadId,
  479. de->u.CreateProcessInfo.hThread,
  480. de->u.CreateProcessInfo.lpThreadLocalBase);
  481. return TRUE;
  482. case LOAD_DLL_DEBUG_EVENT:
  483. fetch_module_name( de->u.LoadDll.lpImageName, de->u.LoadDll.lpBaseOfDll,
  484. u.buffer, ARRAY_SIZE(u.buffer) );
  485. fprintf(stderr, "%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
  486. de->dwProcessId, de->dwThreadId,
  487. dbg_W2A(u.buffer, -1),
  488. de->u.LoadDll.lpBaseOfDll,
  489. de->u.LoadDll.dwDebugInfoFileOffset,
  490. de->u.LoadDll.nDebugInfoSize);
  491. dbg_load_module(gdbctx->process->handle, de->u.LoadDll.hFile, u.buffer,
  492. (DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
  493. if (stop_on_dll_load_unload)
  494. break;
  495. return TRUE;
  496. case UNLOAD_DLL_DEBUG_EVENT:
  497. fprintf(stderr, "%08x:%08x: unload DLL @%p\n",
  498. de->dwProcessId, de->dwThreadId, de->u.UnloadDll.lpBaseOfDll);
  499. SymUnloadModule(gdbctx->process->handle,
  500. (DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
  501. if (stop_on_dll_load_unload)
  502. break;
  503. return TRUE;
  504. case EXCEPTION_DEBUG_EVENT:
  505. TRACE("%08x:%08x: exception code=0x%08x\n", de->dwProcessId,
  506. de->dwThreadId, de->u.Exception.ExceptionRecord.ExceptionCode);
  507. if (handle_exception(gdbctx, &de->u.Exception))
  508. return TRUE;
  509. break;
  510. case CREATE_THREAD_DEBUG_EVENT:
  511. fprintf(stderr, "%08x:%08x: create thread D @%p\n", de->dwProcessId,
  512. de->dwThreadId, de->u.CreateThread.lpStartAddress);
  513. dbg_add_thread(gdbctx->process,
  514. de->dwThreadId,
  515. de->u.CreateThread.hThread,
  516. de->u.CreateThread.lpThreadLocalBase);
  517. return TRUE;
  518. case EXIT_THREAD_DEBUG_EVENT:
  519. fprintf(stderr, "%08x:%08x: exit thread (%u)\n",
  520. de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
  521. if ((thread = dbg_get_thread(gdbctx->process, de->dwThreadId)))
  522. dbg_del_thread(thread);
  523. return TRUE;
  524. case EXIT_PROCESS_DEBUG_EVENT:
  525. fprintf(stderr, "%08x:%08x: exit process (%u)\n",
  526. de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
  527. dbg_del_process(gdbctx->process);
  528. gdbctx->process = NULL;
  529. return FALSE;
  530. case OUTPUT_DEBUG_STRING_EVENT:
  531. memory_get_string(gdbctx->process,
  532. de->u.DebugString.lpDebugStringData, TRUE,
  533. de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
  534. fprintf(stderr, "%08x:%08x: output debug string (%s)\n",
  535. de->dwProcessId, de->dwThreadId, debugstr_a(u.bufferA));
  536. return TRUE;
  537. case RIP_EVENT:
  538. fprintf(stderr, "%08x:%08x: rip error=%u type=%u\n", de->dwProcessId,
  539. de->dwThreadId, de->u.RipInfo.dwError, de->u.RipInfo.dwType);
  540. return TRUE;
  541. default:
  542. FIXME("%08x:%08x: unknown event (%u)\n",
  543. de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
  544. }
  545. LIST_FOR_EACH_ENTRY(thread, &gdbctx->process->threads, struct dbg_thread, entry)
  546. {
  547. if (!thread->suspended) SuspendThread(thread->handle);
  548. thread->suspended = TRUE;
  549. }
  550. return FALSE;
  551. }
  552. static void handle_step_or_continue(struct gdb_context* gdbctx, int tid, BOOL step, int sig)
  553. {
  554. struct dbg_process *process = gdbctx->process;
  555. struct dbg_thread *thread;
  556. if (tid == 0) tid = gdbctx->de.dwThreadId;
  557. LIST_FOR_EACH_ENTRY(thread, &process->threads, struct dbg_thread, entry)
  558. {
  559. if (tid != -1 && thread->tid != tid) continue;
  560. if (!thread->suspended) continue;
  561. thread->suspended = FALSE;
  562. if (process->pid == gdbctx->de.dwProcessId && thread->tid == gdbctx->de.dwThreadId)
  563. gdbctx->de_reply = (sig == -1 ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED);
  564. dbg_thread_set_single_step(thread, step);
  565. ResumeThread(thread->handle);
  566. }
  567. }
  568. static BOOL check_for_interrupt(struct gdb_context* gdbctx)
  569. {
  570. char pkt;
  571. fd_set read_fds;
  572. struct timeval tv = { 0, 0 };
  573. FD_ZERO( &read_fds );
  574. FD_SET( gdbctx->sock, &read_fds );
  575. if (select( 0, &read_fds, NULL, NULL, &tv ) > 0)
  576. {
  577. if (recv(gdbctx->sock, &pkt, 1, 0) != 1) {
  578. ERR("read failed\n");
  579. return FALSE;
  580. }
  581. if (pkt != '\003') {
  582. ERR("Unexpected break packet %#02x\n", pkt);
  583. return FALSE;
  584. }
  585. return TRUE;
  586. }
  587. return FALSE;
  588. }
  589. static void wait_for_debuggee(struct gdb_context* gdbctx)
  590. {
  591. if (gdbctx->de.dwDebugEventCode)
  592. ContinueDebugEvent(gdbctx->de.dwProcessId, gdbctx->de.dwThreadId, gdbctx->de_reply);
  593. for (;;)
  594. {
  595. if (!WaitForDebugEvent(&gdbctx->de, 10))
  596. {
  597. if (GetLastError() == ERROR_SEM_TIMEOUT)
  598. {
  599. if (check_for_interrupt(gdbctx)) {
  600. if (!DebugBreakProcess(gdbctx->process->handle)) {
  601. ERR("Failed to break into debuggee\n");
  602. break;
  603. }
  604. WaitForDebugEvent(&gdbctx->de, INFINITE);
  605. } else {
  606. continue;
  607. }
  608. } else {
  609. break;
  610. }
  611. }
  612. if (!handle_debug_event(gdbctx, TRUE))
  613. break;
  614. ContinueDebugEvent(gdbctx->de.dwProcessId, gdbctx->de.dwThreadId, DBG_CONTINUE);
  615. }
  616. }
  617. static void detach_debuggee(struct gdb_context* gdbctx, BOOL kill)
  618. {
  619. handle_step_or_continue(gdbctx, -1, FALSE, -1);
  620. if (gdbctx->de.dwDebugEventCode)
  621. ContinueDebugEvent(gdbctx->de.dwProcessId, gdbctx->de.dwThreadId, DBG_CONTINUE);
  622. if (!kill)
  623. DebugActiveProcessStop(gdbctx->process->pid);
  624. dbg_del_process(gdbctx->process);
  625. gdbctx->process = NULL;
  626. }
  627. static void get_process_info(struct gdb_context* gdbctx, char* buffer, size_t len)
  628. {
  629. DWORD status;
  630. if (!GetExitCodeProcess(gdbctx->process->handle, &status))
  631. {
  632. strcpy(buffer, "Unknown process");
  633. return;
  634. }
  635. if (status == STILL_ACTIVE)
  636. {
  637. strcpy(buffer, "Running");
  638. }
  639. else
  640. snprintf(buffer, len, "Terminated (%u)", status);
  641. switch (GetPriorityClass(gdbctx->process->handle))
  642. {
  643. case 0: break;
  644. case ABOVE_NORMAL_PRIORITY_CLASS: strcat(buffer, ", above normal priority"); break;
  645. case BELOW_NORMAL_PRIORITY_CLASS: strcat(buffer, ", below normal priority"); break;
  646. case HIGH_PRIORITY_CLASS: strcat(buffer, ", high priority"); break;
  647. case IDLE_PRIORITY_CLASS: strcat(buffer, ", idle priority"); break;
  648. case NORMAL_PRIORITY_CLASS: strcat(buffer, ", normal priority"); break;
  649. case REALTIME_PRIORITY_CLASS: strcat(buffer, ", realtime priority"); break;
  650. }
  651. strcat(buffer, "\n");
  652. }
  653. static void get_thread_info(struct gdb_context* gdbctx, unsigned tid,
  654. char* buffer, size_t len)
  655. {
  656. struct dbg_thread* thd;
  657. DWORD status;
  658. int prio;
  659. /* FIXME: use the size of buffer */
  660. thd = dbg_get_thread(gdbctx->process, tid);
  661. if (thd == NULL)
  662. {
  663. strcpy(buffer, "No information");
  664. return;
  665. }
  666. if (GetExitCodeThread(thd->handle, &status))
  667. {
  668. if (status == STILL_ACTIVE)
  669. {
  670. /* FIXME: this is a bit brutal... some nicer way shall be found */
  671. switch (status = SuspendThread(thd->handle))
  672. {
  673. case -1: break;
  674. case 0: strcpy(buffer, "Running"); break;
  675. default: snprintf(buffer, len, "Suspended (%u)", status - 1);
  676. }
  677. ResumeThread(thd->handle);
  678. }
  679. else
  680. snprintf(buffer, len, "Terminated (exit code = %u)", status);
  681. }
  682. else
  683. {
  684. strcpy(buffer, "Unknown threadID");
  685. }
  686. switch (prio = GetThreadPriority(thd->handle))
  687. {
  688. case THREAD_PRIORITY_ERROR_RETURN: break;
  689. case THREAD_PRIORITY_ABOVE_NORMAL: strcat(buffer, ", priority +1 above normal"); break;
  690. case THREAD_PRIORITY_BELOW_NORMAL: strcat(buffer, ", priority -1 below normal"); break;
  691. case THREAD_PRIORITY_HIGHEST: strcat(buffer, ", priority +2 above normal"); break;
  692. case THREAD_PRIORITY_LOWEST: strcat(buffer, ", priority -2 below normal"); break;
  693. case THREAD_PRIORITY_IDLE: strcat(buffer, ", priority idle"); break;
  694. case THREAD_PRIORITY_NORMAL: strcat(buffer, ", priority normal"); break;
  695. case THREAD_PRIORITY_TIME_CRITICAL: strcat(buffer, ", priority time-critical"); break;
  696. default: snprintf(buffer + strlen(buffer), len - strlen(buffer), ", priority = %d", prio);
  697. }
  698. assert(strlen(buffer) < len);
  699. }
  700. /* =============================================== *
  701. * P A C K E T U T I L S *
  702. * =============================================== *
  703. */
  704. static int addr_width(struct gdb_context* gdbctx)
  705. {
  706. int sz = (gdbctx && gdbctx->process && gdbctx->process->be_cpu) ?
  707. gdbctx->process->be_cpu->pointer_size : (int)sizeof(void*);
  708. return sz * 2;
  709. }
  710. enum packet_return {packet_error = 0x00, packet_ok = 0x01, packet_done = 0x02,
  711. packet_send_buffer = 0x03, packet_last_f = 0x80};
  712. static void packet_reply_hex_to(struct gdb_context* gdbctx, const void* src, int len)
  713. {
  714. reply_buffer_append_hex(&gdbctx->out_buf, src, len);
  715. }
  716. static inline void packet_reply_hex_to_str(struct gdb_context* gdbctx, const char* src)
  717. {
  718. packet_reply_hex_to(gdbctx, src, strlen(src));
  719. }
  720. static void packet_reply_val(struct gdb_context* gdbctx, ULONG_PTR val, int len)
  721. {
  722. reply_buffer_append_uinthex(&gdbctx->out_buf, val, len);
  723. }
  724. static const unsigned char gdb_special_chars_lookup_table[4] = {
  725. /* The characters should be indexed by its value modulo table length. */
  726. 0x24, /* $: 001001|00 */
  727. 0x7D, /* }: 011111|01 */
  728. 0x2A, /* *: 001010|10 */
  729. 0x23 /* #: 001000|11 */
  730. };
  731. static inline BOOL is_gdb_special_char(unsigned char val)
  732. {
  733. /* A note on the GDB special character scanning code:
  734. *
  735. * We cannot use strcspn() since we plan to transmit binary data in
  736. * packet reply, which can contain NULL (0x00) bytes. We also don't want
  737. * to slow down memory dump transfers. Therefore, we use a tiny lookup
  738. * table that contains all the four special characters to speed up scanning.
  739. */
  740. const size_t length = ARRAY_SIZE(gdb_special_chars_lookup_table);
  741. return gdb_special_chars_lookup_table[val % length] == val;
  742. }
  743. static void packet_reply_add_data(struct gdb_context* gdbctx, const void* data, size_t len)
  744. {
  745. const unsigned char *ptr = data, *end = ptr + len, *curr;
  746. unsigned char esc_seq[2];
  747. while (ptr != end)
  748. {
  749. curr = ptr;
  750. while (ptr != end && !is_gdb_special_char(*ptr))
  751. ptr++;
  752. reply_buffer_append(&gdbctx->out_buf, curr, ptr - curr);
  753. if (ptr == end) break;
  754. esc_seq[0] = 0x7D;
  755. esc_seq[1] = 0x20 ^ *ptr++;
  756. reply_buffer_append(&gdbctx->out_buf, esc_seq, 2);
  757. }
  758. }
  759. static inline void packet_reply_add(struct gdb_context* gdbctx, const char* str)
  760. {
  761. packet_reply_add_data(gdbctx, str, strlen(str));
  762. }
  763. static void packet_reply_open(struct gdb_context* gdbctx)
  764. {
  765. assert(gdbctx->out_curr_packet == -1);
  766. reply_buffer_append(&gdbctx->out_buf, "$", 1);
  767. gdbctx->out_curr_packet = gdbctx->out_buf.len;
  768. }
  769. static void packet_reply_close(struct gdb_context* gdbctx)
  770. {
  771. unsigned char cksum;
  772. int plen;
  773. plen = gdbctx->out_buf.len - gdbctx->out_curr_packet;
  774. reply_buffer_append(&gdbctx->out_buf, "#", 1);
  775. cksum = checksum(gdbctx->out_buf.base + gdbctx->out_curr_packet, plen);
  776. packet_reply_hex_to(gdbctx, &cksum, 1);
  777. gdbctx->out_curr_packet = -1;
  778. }
  779. static enum packet_return packet_reply(struct gdb_context* gdbctx, const char* packet)
  780. {
  781. packet_reply_open(gdbctx);
  782. packet_reply_add(gdbctx, packet);
  783. packet_reply_close(gdbctx);
  784. return packet_done;
  785. }
  786. static enum packet_return packet_reply_error(struct gdb_context* gdbctx, int error)
  787. {
  788. packet_reply_open(gdbctx);
  789. packet_reply_add(gdbctx, "E");
  790. packet_reply_val(gdbctx, error, 1);
  791. packet_reply_close(gdbctx);
  792. return packet_done;
  793. }
  794. static inline void packet_reply_register_hex_to(struct gdb_context* gdbctx, dbg_ctx_t* ctx, unsigned idx)
  795. {
  796. const struct gdb_register *cpu_register_map = gdbctx->process->be_cpu->gdb_register_map;
  797. packet_reply_hex_to(gdbctx, cpu_register_ptr(gdbctx, ctx, idx), cpu_register_map[idx].length);
  798. }
  799. static void packet_reply_xfer(struct gdb_context* gdbctx, size_t off, size_t len, BOOL* more_p)
  800. {
  801. BOOL more;
  802. size_t data_len, trunc_len;
  803. packet_reply_open(gdbctx);
  804. data_len = gdbctx->qxfer_buffer.len;
  805. /* check if off + len would overflow */
  806. more = off < data_len && off + len < data_len;
  807. if (more)
  808. packet_reply_add(gdbctx, "m");
  809. else
  810. packet_reply_add(gdbctx, "l");
  811. if (off < data_len)
  812. {
  813. trunc_len = min(len, data_len - off);
  814. packet_reply_add_data(gdbctx, gdbctx->qxfer_buffer.base + off, trunc_len);
  815. }
  816. packet_reply_close(gdbctx);
  817. *more_p = more;
  818. }
  819. /* =============================================== *
  820. * P A C K E T H A N D L E R S *
  821. * =============================================== *
  822. */
  823. static void packet_reply_status_xpoints(struct gdb_context* gdbctx, struct dbg_thread *thread,
  824. dbg_ctx_t *ctx)
  825. {
  826. struct dbg_process *process = thread->process;
  827. struct backend_cpu *cpu = process->be_cpu;
  828. struct gdb_xpoint *x;
  829. LIST_FOR_EACH_ENTRY(x, &gdbctx->xpoint_list, struct gdb_xpoint, entry)
  830. {
  831. if (x->pid != process->pid || x->tid != thread->tid)
  832. continue;
  833. if (!cpu->is_watchpoint_set(ctx, x->value))
  834. continue;
  835. if (x->type == be_xpoint_watch_write)
  836. {
  837. packet_reply_add(gdbctx, "watch:");
  838. packet_reply_val(gdbctx, (ULONG_PTR)x->addr, sizeof(x->addr));
  839. packet_reply_add(gdbctx, ";");
  840. }
  841. if (x->type == be_xpoint_watch_read)
  842. {
  843. packet_reply_add(gdbctx, "rwatch:");
  844. packet_reply_val(gdbctx, (ULONG_PTR)x->addr, sizeof(x->addr));
  845. packet_reply_add(gdbctx, ";");
  846. }
  847. }
  848. }
  849. static void packet_reply_begin_stop_reply(struct gdb_context* gdbctx, unsigned char signal)
  850. {
  851. packet_reply_add(gdbctx, "T");
  852. packet_reply_val(gdbctx, signal, 1);
  853. /* We should always report the current thread ID for all stop replies.
  854. * Otherwise, GDB complains with the following message:
  855. *
  856. * Warning: multi-threaded target stopped without sending a thread-id,
  857. * using first non-exited thread
  858. */
  859. packet_reply_add(gdbctx, "thread:");
  860. packet_reply_val(gdbctx, gdbctx->de.dwThreadId, 4);
  861. packet_reply_add(gdbctx, ";");
  862. }
  863. static enum packet_return packet_reply_status(struct gdb_context* gdbctx)
  864. {
  865. struct dbg_process *process = gdbctx->process;
  866. struct dbg_thread *thread;
  867. struct backend_cpu *backend;
  868. dbg_ctx_t ctx;
  869. size_t i;
  870. switch (gdbctx->de.dwDebugEventCode)
  871. {
  872. default:
  873. if (!process) return packet_error;
  874. if (!(backend = process->be_cpu)) return packet_error;
  875. if (!(thread = dbg_get_thread(process, gdbctx->de.dwThreadId)) ||
  876. !backend->get_context(thread->handle, &ctx))
  877. return packet_error;
  878. packet_reply_open(gdbctx);
  879. packet_reply_begin_stop_reply(gdbctx, signal_from_debug_event(&gdbctx->de));
  880. packet_reply_status_xpoints(gdbctx, thread, &ctx);
  881. for (i = 0; i < backend->gdb_num_regs; i++)
  882. {
  883. packet_reply_val(gdbctx, i, 1);
  884. packet_reply_add(gdbctx, ":");
  885. packet_reply_register_hex_to(gdbctx, &ctx, i);
  886. packet_reply_add(gdbctx, ";");
  887. }
  888. packet_reply_close(gdbctx);
  889. return packet_done;
  890. case EXIT_PROCESS_DEBUG_EVENT:
  891. packet_reply_open(gdbctx);
  892. packet_reply_add(gdbctx, "W");
  893. packet_reply_val(gdbctx, gdbctx->de.u.ExitProcess.dwExitCode, 4);
  894. packet_reply_close(gdbctx);
  895. return packet_done | packet_last_f;
  896. case LOAD_DLL_DEBUG_EVENT:
  897. case UNLOAD_DLL_DEBUG_EVENT:
  898. packet_reply_open(gdbctx);
  899. packet_reply_begin_stop_reply(gdbctx, HOST_SIGTRAP);
  900. packet_reply_add(gdbctx, "library:;");
  901. packet_reply_close(gdbctx);
  902. return packet_done;
  903. }
  904. }
  905. static enum packet_return packet_last_signal(struct gdb_context* gdbctx)
  906. {
  907. assert(gdbctx->in_packet_len == 0);
  908. return packet_reply_status(gdbctx);
  909. }
  910. static enum packet_return packet_continue(struct gdb_context* gdbctx)
  911. {
  912. void *addr;
  913. if (sscanf(gdbctx->in_packet, "%p", &addr) == 1)
  914. FIXME("Continue at address %p not supported\n", addr);
  915. handle_step_or_continue(gdbctx, gdbctx->exec_tid, FALSE, -1);
  916. wait_for_debuggee(gdbctx);
  917. return packet_reply_status(gdbctx);
  918. }
  919. static enum packet_return packet_verbose_cont(struct gdb_context* gdbctx)
  920. {
  921. char *buf = gdbctx->in_packet, *end = gdbctx->in_packet + gdbctx->in_packet_len;
  922. if (gdbctx->in_packet[4] == '?')
  923. {
  924. packet_reply_open(gdbctx);
  925. packet_reply_add(gdbctx, "vCont");
  926. packet_reply_add(gdbctx, ";c");
  927. packet_reply_add(gdbctx, ";C");
  928. packet_reply_add(gdbctx, ";s");
  929. packet_reply_add(gdbctx, ";S");
  930. packet_reply_close(gdbctx);
  931. return packet_done;
  932. }
  933. while (buf < end && (buf = memchr(buf + 1, ';', end - buf - 1)))
  934. {
  935. int tid = -1, sig = -1;
  936. int action, n;
  937. switch ((action = buf[1]))
  938. {
  939. default:
  940. return packet_error;
  941. case 'c':
  942. case 's':
  943. buf += 2;
  944. break;
  945. case 'C':
  946. case 'S':
  947. if (sscanf(buf, ";%*c%2x", &sig) <= 0 ||
  948. sig != signal_from_debug_event(&gdbctx->de))
  949. return packet_error;
  950. buf += 4;
  951. break;
  952. }
  953. if (buf > end)
  954. return packet_error;
  955. if (buf < end && *buf == ':' && (n = sscanf(buf, ":%x", &tid)) <= 0)
  956. return packet_error;
  957. handle_step_or_continue(gdbctx, tid, action == 's' || action == 'S', sig);
  958. }
  959. wait_for_debuggee(gdbctx);
  960. return packet_reply_status(gdbctx);
  961. }
  962. static enum packet_return packet_verbose(struct gdb_context* gdbctx)
  963. {
  964. if (gdbctx->in_packet_len >= 4 && !memcmp(gdbctx->in_packet, "Cont", 4))
  965. {
  966. return packet_verbose_cont(gdbctx);
  967. }
  968. if (gdbctx->in_packet_len == 14 && !memcmp(gdbctx->in_packet, "MustReplyEmpty", 14))
  969. return packet_reply(gdbctx, "");
  970. return packet_error;
  971. }
  972. static enum packet_return packet_continue_signal(struct gdb_context* gdbctx)
  973. {
  974. void *addr;
  975. int sig, n;
  976. if ((n = sscanf(gdbctx->in_packet, "%x;%p", &sig, &addr)) == 2)
  977. FIXME("Continue at address %p not supported\n", addr);
  978. if (n < 1) return packet_error;
  979. if (sig != signal_from_debug_event(&gdbctx->de))
  980. {
  981. ERR("Changing signals is not supported.\n");
  982. return packet_error;
  983. }
  984. handle_step_or_continue(gdbctx, gdbctx->exec_tid, FALSE, sig);
  985. wait_for_debuggee(gdbctx);
  986. return packet_reply_status(gdbctx);
  987. }
  988. static enum packet_return packet_delete_breakpoint(struct gdb_context* gdbctx)
  989. {
  990. struct dbg_process *process = gdbctx->process;
  991. struct dbg_thread *thread;
  992. struct backend_cpu *cpu;
  993. struct gdb_xpoint *x;
  994. dbg_ctx_t ctx;
  995. char type;
  996. void *addr;
  997. int size;
  998. if (!process) return packet_error;
  999. if (!(cpu = process->be_cpu)) return packet_error;
  1000. if (sscanf(gdbctx->in_packet, "%c,%p,%x", &type, &addr, &size) < 3)
  1001. return packet_error;
  1002. if (type == '0')
  1003. return packet_error;
  1004. LIST_FOR_EACH_ENTRY(thread, &process->threads, struct dbg_thread, entry)
  1005. {
  1006. if (!cpu->get_context(thread->handle, &ctx))
  1007. continue;
  1008. if ((type == '1') && (x = gdb_find_xpoint(gdbctx, thread, be_xpoint_watch_exec, addr, size)))
  1009. gdbctx_delete_xpoint(gdbctx, thread, &ctx, x);
  1010. if ((type == '2' || type == '4') && (x = gdb_find_xpoint(gdbctx, thread, be_xpoint_watch_read, addr, size)))
  1011. gdbctx_delete_xpoint(gdbctx, thread, &ctx, x);
  1012. if ((type == '3' || type == '4') && (x = gdb_find_xpoint(gdbctx, thread, be_xpoint_watch_write, addr, size)))
  1013. gdbctx_delete_xpoint(gdbctx, thread, &ctx, x);
  1014. cpu->set_context(thread->handle, &ctx);
  1015. }
  1016. while ((type == '1') && (x = gdb_find_xpoint(gdbctx, NULL, be_xpoint_watch_exec, addr, size)))
  1017. gdbctx_delete_xpoint(gdbctx, NULL, NULL, x);
  1018. while ((type == '2' || type == '4') && (x = gdb_find_xpoint(gdbctx, NULL, be_xpoint_watch_read, addr, size)))
  1019. gdbctx_delete_xpoint(gdbctx, NULL, NULL, x);
  1020. while ((type == '3' || type == '4') && (x = gdb_find_xpoint(gdbctx, NULL, be_xpoint_watch_write, addr, size)))
  1021. gdbctx_delete_xpoint(gdbctx, NULL, NULL, x);
  1022. return packet_ok;
  1023. }
  1024. static enum packet_return packet_insert_breakpoint(struct gdb_context* gdbctx)
  1025. {
  1026. struct dbg_process *process = gdbctx->process;
  1027. struct dbg_thread *thread;
  1028. struct backend_cpu *cpu;
  1029. dbg_ctx_t ctx;
  1030. char type;
  1031. void *addr;
  1032. int size;
  1033. if (!process) return packet_error;
  1034. if (!(cpu = process->be_cpu)) return packet_error;
  1035. if (memchr(gdbctx->in_packet, ';', gdbctx->in_packet_len))
  1036. {
  1037. FIXME("breakpoint commands not supported\n");
  1038. return packet_error;
  1039. }
  1040. if (sscanf(gdbctx->in_packet, "%c,%p,%x", &type, &addr, &size) < 3)
  1041. return packet_error;
  1042. if (type == '0')
  1043. return packet_error;
  1044. LIST_FOR_EACH_ENTRY(thread, &process->threads, struct dbg_thread, entry)
  1045. {
  1046. if (!cpu->get_context(thread->handle, &ctx))
  1047. continue;
  1048. if (type == '1')
  1049. gdbctx_insert_xpoint(gdbctx, thread, &ctx, be_xpoint_watch_exec, addr, size);
  1050. if (type == '2' || type == '4')
  1051. gdbctx_insert_xpoint(gdbctx, thread, &ctx, be_xpoint_watch_read, addr, size);
  1052. if (type == '3' || type == '4')
  1053. gdbctx_insert_xpoint(gdbctx, thread, &ctx, be_xpoint_watch_write, addr, size);
  1054. cpu->set_context(thread->handle, &ctx);
  1055. }
  1056. return packet_ok;
  1057. }
  1058. static enum packet_return packet_detach(struct gdb_context* gdbctx)
  1059. {
  1060. detach_debuggee(gdbctx, FALSE);
  1061. return packet_ok | packet_last_f;
  1062. }
  1063. static enum packet_return packet_read_registers(struct gdb_context* gdbctx)
  1064. {
  1065. struct dbg_thread *thread = dbg_thread_from_tid(gdbctx, gdbctx->other_tid);
  1066. struct backend_cpu *backend;
  1067. dbg_ctx_t ctx;
  1068. size_t i;
  1069. if (!thread) return packet_error;
  1070. if (!thread->process) return packet_error;
  1071. if (!(backend = thread->process->be_cpu)) return packet_error;
  1072. if (!backend->get_context(thread->handle, &ctx))
  1073. return packet_error;
  1074. packet_reply_open(gdbctx);
  1075. for (i = 0; i < backend->gdb_num_regs; i++)
  1076. packet_reply_register_hex_to(gdbctx, &ctx, i);
  1077. packet_reply_close(gdbctx);
  1078. return packet_done;
  1079. }
  1080. static enum packet_return packet_write_registers(struct gdb_context* gdbctx)
  1081. {
  1082. struct dbg_thread *thread = dbg_thread_from_tid(gdbctx, gdbctx->other_tid);
  1083. struct backend_cpu *backend;
  1084. dbg_ctx_t ctx;
  1085. const char *ptr;
  1086. size_t i;
  1087. if (!thread) return packet_error;
  1088. if (!thread->process) return packet_error;
  1089. if (!(backend = thread->process->be_cpu)) return packet_error;
  1090. if (!backend->get_context(thread->handle, &ctx))
  1091. return packet_error;
  1092. if (gdbctx->in_packet_len < backend->gdb_num_regs * 2)
  1093. return packet_error;
  1094. ptr = gdbctx->in_packet;
  1095. for (i = 0; i < backend->gdb_num_regs; i++)
  1096. cpu_register_hex_from(gdbctx, &ctx, i, &ptr);
  1097. if (!backend->set_context(thread->handle, &ctx))
  1098. {
  1099. ERR("Failed to set context for tid %04x, error %u\n", thread->tid, GetLastError());
  1100. return packet_error;
  1101. }
  1102. return packet_ok;
  1103. }
  1104. static enum packet_return packet_kill(struct gdb_context* gdbctx)
  1105. {
  1106. detach_debuggee(gdbctx, TRUE);
  1107. return packet_ok | packet_last_f;
  1108. }
  1109. static enum packet_return packet_thread(struct gdb_context* gdbctx)
  1110. {
  1111. switch (gdbctx->in_packet[0])
  1112. {
  1113. case 'c':
  1114. if (sscanf(gdbctx->in_packet, "c%x", &gdbctx->exec_tid) == 1)
  1115. return packet_ok;
  1116. return packet_error;
  1117. case 'g':
  1118. if (sscanf(gdbctx->in_packet, "g%x", &gdbctx->other_tid) == 1)
  1119. return packet_ok;
  1120. return packet_error;
  1121. default:
  1122. FIXME("Unknown thread sub-command %c\n", gdbctx->in_packet[0]);
  1123. return packet_error;
  1124. }
  1125. }
  1126. static enum packet_return packet_read_memory(struct gdb_context* gdbctx)
  1127. {
  1128. char *addr;
  1129. unsigned int len, blk_len, nread;
  1130. char buffer[32];
  1131. SIZE_T r = 0;
  1132. if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2) return packet_error;
  1133. if (len <= 0) return packet_error;
  1134. TRACE("Read %u bytes at %p\n", len, addr);
  1135. for (nread = 0; nread < len; nread += r, addr += r)
  1136. {
  1137. blk_len = min(sizeof(buffer), len - nread);
  1138. if (!gdbctx->process->process_io->read(gdbctx->process->handle, addr,
  1139. buffer, blk_len, &r) || r == 0)
  1140. {
  1141. /* fail at first address, return error */
  1142. if (nread == 0) return packet_reply_error(gdbctx, HOST_EFAULT );
  1143. /* something has already been read, return partial information */
  1144. break;
  1145. }
  1146. if (nread == 0) packet_reply_open(gdbctx);
  1147. packet_reply_hex_to(gdbctx, buffer, r);
  1148. }
  1149. packet_reply_close(gdbctx);
  1150. return packet_done;
  1151. }
  1152. static enum packet_return packet_write_memory(struct gdb_context* gdbctx)
  1153. {
  1154. char* addr;
  1155. unsigned int len, blk_len;
  1156. char* ptr;
  1157. char buffer[32];
  1158. SIZE_T w;
  1159. ptr = memchr(gdbctx->in_packet, ':', gdbctx->in_packet_len);
  1160. if (ptr == NULL)
  1161. {
  1162. ERR("Cannot find ':' in %s\n", debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
  1163. return packet_error;
  1164. }
  1165. *ptr++ = '\0';
  1166. if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2)
  1167. {
  1168. ERR("Failed to parse %s\n", debugstr_a(gdbctx->in_packet));
  1169. return packet_error;
  1170. }
  1171. if (ptr - gdbctx->in_packet + len * 2 != gdbctx->in_packet_len)
  1172. {
  1173. ERR("Length %u does not match packet length %u\n",
  1174. (int)(ptr - gdbctx->in_packet) + len * 2, gdbctx->in_packet_len);
  1175. return packet_error;
  1176. }
  1177. TRACE("Write %u bytes at %p\n", len, addr);
  1178. while (len > 0)
  1179. {
  1180. blk_len = min(sizeof(buffer), len);
  1181. hex_from(buffer, ptr, blk_len);
  1182. if (!gdbctx->process->process_io->write(gdbctx->process->handle, addr, buffer, blk_len, &w) ||
  1183. w != blk_len)
  1184. break;
  1185. addr += blk_len;
  1186. len -= blk_len;
  1187. ptr += blk_len;
  1188. }
  1189. return packet_ok; /* FIXME: error while writing ? */
  1190. }
  1191. static enum packet_return packet_read_register(struct gdb_context* gdbctx)
  1192. {
  1193. struct dbg_thread *thread = dbg_thread_from_tid(gdbctx, gdbctx->other_tid);
  1194. struct backend_cpu *backend;
  1195. dbg_ctx_t ctx;
  1196. size_t reg;
  1197. if (!thread) return packet_error;
  1198. if (!thread->process) return packet_error;
  1199. if (!(backend = thread->process->be_cpu)) return packet_error;
  1200. if (!backend->get_context(thread->handle, &ctx))
  1201. return packet_error;
  1202. if (sscanf(gdbctx->in_packet, "%Ix", &reg) != 1)
  1203. return packet_error;
  1204. if (reg >= backend->gdb_num_regs)
  1205. {
  1206. WARN("Unhandled register %Iu\n", reg);
  1207. return packet_error;
  1208. }
  1209. TRACE("%Iu => %I64x\n", reg, cpu_register(gdbctx, &ctx, reg));
  1210. packet_reply_open(gdbctx);
  1211. packet_reply_register_hex_to(gdbctx, &ctx, reg);
  1212. packet_reply_close(gdbctx);
  1213. return packet_done;
  1214. }
  1215. static enum packet_return packet_write_register(struct gdb_context* gdbctx)
  1216. {
  1217. struct dbg_thread *thread = dbg_thread_from_tid(gdbctx, gdbctx->other_tid);
  1218. struct backend_cpu *backend;
  1219. dbg_ctx_t ctx;
  1220. size_t reg;
  1221. char *ptr;
  1222. if (!thread) return packet_error;
  1223. if (!thread->process) return packet_error;
  1224. if (!(backend = thread->process->be_cpu)) return packet_error;
  1225. if (!backend->get_context(thread->handle, &ctx))
  1226. return packet_error;
  1227. if (!(ptr = strchr(gdbctx->in_packet, '=')))
  1228. return packet_error;
  1229. *ptr++ = '\0';
  1230. if (sscanf(gdbctx->in_packet, "%Ix", &reg) != 1)
  1231. return packet_error;
  1232. if (reg >= backend->gdb_num_regs)
  1233. {
  1234. /* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
  1235. * it wouldn't matter too much, and it fakes our support for all regs
  1236. */
  1237. WARN("Unhandled register %Iu\n", reg);
  1238. return packet_ok;
  1239. }
  1240. TRACE("%Iu <= %s\n", reg, debugstr_an(ptr, (int)(gdbctx->in_packet_len - (ptr - gdbctx->in_packet))));
  1241. cpu_register_hex_from(gdbctx, &ctx, reg, (const char**)&ptr);
  1242. if (!backend->set_context(thread->handle, &ctx))
  1243. {
  1244. ERR("Failed to set context for tid %04x, error %u\n", thread->tid, GetLastError());
  1245. return packet_error;
  1246. }
  1247. return packet_ok;
  1248. }
  1249. static void packet_query_monitor_wnd_helper(struct gdb_context* gdbctx, HWND hWnd, int indent)
  1250. {
  1251. char buffer[128];
  1252. char clsName[128];
  1253. char wndName[128];
  1254. HWND child;
  1255. do {
  1256. if (!GetClassNameA(hWnd, clsName, sizeof(clsName)))
  1257. strcpy(clsName, "-- Unknown --");
  1258. if (!GetWindowTextA(hWnd, wndName, sizeof(wndName)))
  1259. strcpy(wndName, "-- Empty --");
  1260. packet_reply_open(gdbctx);
  1261. packet_reply_add(gdbctx, "O");
  1262. snprintf(buffer, sizeof(buffer),
  1263. "%*s%04Ix%*s%-17.17s %08x %0*Ix %.14s\n",
  1264. indent, "", (ULONG_PTR)hWnd, 13 - indent, "",
  1265. clsName, GetWindowLongW(hWnd, GWL_STYLE),
  1266. addr_width(gdbctx), (ULONG_PTR)GetWindowLongPtrW(hWnd, GWLP_WNDPROC),
  1267. wndName);
  1268. packet_reply_hex_to_str(gdbctx, buffer);
  1269. packet_reply_close(gdbctx);
  1270. if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
  1271. packet_query_monitor_wnd_helper(gdbctx, child, indent + 1);
  1272. } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
  1273. }
  1274. static void packet_query_monitor_wnd(struct gdb_context* gdbctx, int len, const char* str)
  1275. {
  1276. char buffer[128];
  1277. /* we do the output in several 'O' packets, with the last one being just OK for
  1278. * marking the end of the output */
  1279. packet_reply_open(gdbctx);
  1280. packet_reply_add(gdbctx, "O");
  1281. snprintf(buffer, sizeof(buffer),
  1282. "%-16.16s %-17.17s %-8.8s %s\n",
  1283. "hwnd", "Class Name", " Style", " WndProc Text");
  1284. packet_reply_hex_to_str(gdbctx, buffer);
  1285. packet_reply_close(gdbctx);
  1286. /* FIXME: could also add a pmt to this command in str... */
  1287. packet_query_monitor_wnd_helper(gdbctx, GetDesktopWindow(), 0);
  1288. packet_reply(gdbctx, "OK");
  1289. }
  1290. static void packet_query_monitor_process(struct gdb_context* gdbctx, int len, const char* str)
  1291. {
  1292. HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1293. char buffer[31+MAX_PATH];
  1294. char deco;
  1295. PROCESSENTRY32 entry;
  1296. BOOL ok;
  1297. if (snap == INVALID_HANDLE_VALUE)
  1298. return;
  1299. entry.dwSize = sizeof(entry);
  1300. ok = Process32First(snap, &entry);
  1301. /* we do the output in several 'O' packets, with the last one being just OK for
  1302. * marking the end of the output */
  1303. packet_reply_open(gdbctx);
  1304. packet_reply_add(gdbctx, "O");
  1305. snprintf(buffer, sizeof(buffer),
  1306. " %-8.8s %-8.8s %-8.8s %s\n",
  1307. "pid", "threads", "parent", "executable");
  1308. packet_reply_hex_to_str(gdbctx, buffer);
  1309. packet_reply_close(gdbctx);
  1310. while (ok)
  1311. {
  1312. deco = ' ';
  1313. if (entry.th32ProcessID == gdbctx->process->pid) deco = '>';
  1314. packet_reply_open(gdbctx);
  1315. packet_reply_add(gdbctx, "O");
  1316. snprintf(buffer, sizeof(buffer),
  1317. "%c%08x %-8d %08x '%s'\n",
  1318. deco, entry.th32ProcessID, entry.cntThreads,
  1319. entry.th32ParentProcessID, entry.szExeFile);
  1320. packet_reply_hex_to_str(gdbctx, buffer);
  1321. packet_reply_close(gdbctx);
  1322. ok = Process32Next(snap, &entry);
  1323. }
  1324. CloseHandle(snap);
  1325. packet_reply(gdbctx, "OK");
  1326. }
  1327. static void packet_query_monitor_mem(struct gdb_context* gdbctx, int len, const char* str)
  1328. {
  1329. MEMORY_BASIC_INFORMATION mbi;
  1330. char* addr = 0;
  1331. const char* state;
  1332. const char* type;
  1333. char prot[3+1];
  1334. char buffer[128];
  1335. /* we do the output in several 'O' packets, with the last one being just OK for
  1336. * marking the end of the output */
  1337. packet_reply_open(gdbctx);
  1338. packet_reply_add(gdbctx, "O");
  1339. packet_reply_hex_to_str(gdbctx, "Address Size State Type RWX\n");
  1340. packet_reply_close(gdbctx);
  1341. while (VirtualQueryEx(gdbctx->process->handle, addr, &mbi, sizeof(mbi)) >= sizeof(mbi))
  1342. {
  1343. switch (mbi.State)
  1344. {
  1345. case MEM_COMMIT: state = "commit "; break;
  1346. case MEM_FREE: state = "free "; break;
  1347. case MEM_RESERVE: state = "reserve"; break;
  1348. default: state = "??? "; break;
  1349. }
  1350. if (mbi.State != MEM_FREE)
  1351. {
  1352. switch (mbi.Type)
  1353. {
  1354. case MEM_IMAGE: type = "image "; break;
  1355. case MEM_MAPPED: type = "mapped "; break;
  1356. case MEM_PRIVATE: type = "private"; break;
  1357. case 0: type = " "; break;
  1358. default: type = "??? "; break;
  1359. }
  1360. memset(prot, ' ' , sizeof(prot)-1);
  1361. prot[sizeof(prot)-1] = '\0';
  1362. if (mbi.AllocationProtect & (PAGE_READONLY|PAGE_READWRITE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
  1363. prot[0] = 'R';
  1364. if (mbi.AllocationProtect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE))
  1365. prot[1] = 'W';
  1366. if (mbi.AllocationProtect & (PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
  1367. prot[1] = 'C';
  1368. if (mbi.AllocationProtect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
  1369. prot[2] = 'X';
  1370. }
  1371. else
  1372. {
  1373. type = "";
  1374. prot[0] = '\0';
  1375. }
  1376. packet_reply_open(gdbctx);
  1377. snprintf(buffer, sizeof(buffer), "%0*Ix %0*Ix %s %s %s\n",
  1378. addr_width(gdbctx), (DWORD_PTR)addr,
  1379. addr_width(gdbctx), mbi.RegionSize, state, type, prot);
  1380. packet_reply_add(gdbctx, "O");
  1381. packet_reply_hex_to_str(gdbctx, buffer);
  1382. packet_reply_close(gdbctx);
  1383. if (addr + mbi.RegionSize < addr) /* wrap around ? */
  1384. break;
  1385. addr += mbi.RegionSize;
  1386. }
  1387. packet_reply(gdbctx, "OK");
  1388. }
  1389. struct query_detail
  1390. {
  1391. int with_arg;
  1392. const char* name;
  1393. size_t len;
  1394. void (*handler)(struct gdb_context*, int, const char*);
  1395. } query_details[] =
  1396. {
  1397. {0, "wnd", 3, packet_query_monitor_wnd},
  1398. {0, "window", 6, packet_query_monitor_wnd},
  1399. {0, "proc", 4, packet_query_monitor_process},
  1400. {0, "process", 7, packet_query_monitor_process},
  1401. {0, "mem", 3, packet_query_monitor_mem},
  1402. {0, NULL, 0, NULL},
  1403. };
  1404. static enum packet_return packet_query_remote_command(struct gdb_context* gdbctx,
  1405. const char* hxcmd, size_t len)
  1406. {
  1407. char buffer[128];
  1408. struct query_detail* qd;
  1409. assert((len & 1) == 0 && len < 2 * sizeof(buffer));
  1410. len /= 2;
  1411. hex_from(buffer, hxcmd, len);
  1412. for (qd = query_details; qd->name != NULL; qd++)
  1413. {
  1414. if (len < qd->len || strncmp(buffer, qd->name, qd->len) != 0) continue;
  1415. if (!qd->with_arg && len != qd->len) continue;
  1416. (qd->handler)(gdbctx, len - qd->len, buffer + qd->len);
  1417. return packet_done;
  1418. }
  1419. return packet_reply_error(gdbctx, HOST_EINVAL );
  1420. }
  1421. static BOOL CALLBACK packet_query_libraries_cb(PCSTR mod_name, DWORD64 base, PVOID ctx)
  1422. {
  1423. struct gdb_context* gdbctx = ctx;
  1424. struct reply_buffer* reply = &gdbctx->qxfer_buffer;
  1425. MEMORY_BASIC_INFORMATION mbi;
  1426. IMAGE_SECTION_HEADER *sec;
  1427. IMAGE_DOS_HEADER *dos = NULL;
  1428. IMAGE_NT_HEADERS *nth = NULL;
  1429. IMAGEHLP_MODULE64 mod;
  1430. SIZE_T size, i;
  1431. BOOL is_wow64;
  1432. char buffer[0x400];
  1433. mod.SizeOfStruct = sizeof(mod);
  1434. SymGetModuleInfo64(gdbctx->process->handle, base, &mod);
  1435. reply_buffer_append_str(reply, "<library name=\"");
  1436. if (strcmp(mod.LoadedImageName, "[vdso].so") == 0)
  1437. reply_buffer_append_xmlstr(reply, "linux-vdso.so.1");
  1438. else if (mod.LoadedImageName[0] == '/')
  1439. reply_buffer_append_xmlstr(reply, mod.LoadedImageName);
  1440. else
  1441. {
  1442. UNICODE_STRING nt_name;
  1443. ANSI_STRING ansi_name;
  1444. char *unix_path, *tmp;
  1445. RtlInitAnsiString(&ansi_name, mod.LoadedImageName);
  1446. RtlAnsiStringToUnicodeString(&nt_name, &ansi_name, TRUE);
  1447. if ((unix_path = wine_get_unix_file_name(nt_name.Buffer)))
  1448. {
  1449. if (IsWow64Process(gdbctx->process->handle, &is_wow64) &&
  1450. is_wow64 && (tmp = strstr(unix_path, "system32")))
  1451. memcpy(tmp, "syswow64", 8);
  1452. reply_buffer_append_xmlstr(reply, unix_path);
  1453. }
  1454. else
  1455. reply_buffer_append_xmlstr(reply, mod.LoadedImageName);
  1456. HeapFree(GetProcessHeap(), 0, unix_path);
  1457. RtlFreeUnicodeString(&nt_name);
  1458. }
  1459. reply_buffer_append_str(reply, "\">");
  1460. size = sizeof(buffer);
  1461. if (VirtualQueryEx(gdbctx->process->handle, (void *)(UINT_PTR)mod.BaseOfImage, &mbi, sizeof(mbi)) >= sizeof(mbi) &&
  1462. mbi.Type == MEM_IMAGE && mbi.State != MEM_FREE)
  1463. {
  1464. if (ReadProcessMemory(gdbctx->process->handle, (void *)(UINT_PTR)mod.BaseOfImage, buffer, size, &size) &&
  1465. size >= sizeof(IMAGE_DOS_HEADER))
  1466. dos = (IMAGE_DOS_HEADER *)buffer;
  1467. if (dos && dos->e_magic == IMAGE_DOS_SIGNATURE && dos->e_lfanew < size)
  1468. nth = (IMAGE_NT_HEADERS *)(buffer + dos->e_lfanew);
  1469. if (nth && memcmp(&nth->Signature, "PE\0\0", 4))
  1470. nth = NULL;
  1471. }
  1472. if (!nth) memset(buffer, 0, sizeof(buffer));
  1473. /* if the module is not PE we have cleared buffer with 0, this makes
  1474. * the following computation valid in all cases. */
  1475. dos = (IMAGE_DOS_HEADER *)buffer;
  1476. nth = (IMAGE_NT_HEADERS *)(buffer + dos->e_lfanew);
  1477. if (IsWow64Process(gdbctx->process->handle, &is_wow64) && is_wow64)
  1478. sec = IMAGE_FIRST_SECTION((IMAGE_NT_HEADERS32 *)nth);
  1479. else
  1480. sec = IMAGE_FIRST_SECTION((IMAGE_NT_HEADERS64 *)nth);
  1481. for (i = 0; i < max(nth->FileHeader.NumberOfSections, 1); ++i)
  1482. {
  1483. if ((char *)(sec + i) >= buffer + size) break;
  1484. reply_buffer_append_str(reply, "<segment address=\"0x");
  1485. reply_buffer_append_uinthex(reply, mod.BaseOfImage + sec[i].VirtualAddress, sizeof(ULONG_PTR));
  1486. reply_buffer_append_str(reply, "\"/>");
  1487. }
  1488. reply_buffer_append_str(reply, "</library>");
  1489. return TRUE;
  1490. }
  1491. static enum packet_return packet_query_libraries(struct gdb_context* gdbctx)
  1492. {
  1493. struct reply_buffer* reply = &gdbctx->qxfer_buffer;
  1494. BOOL opt;
  1495. if (!gdbctx->process) return packet_error;
  1496. if (gdbctx->qxfer_object_annex[0])
  1497. return packet_reply_error(gdbctx, 0);
  1498. /* this will resynchronize builtin dbghelp's internal ELF module list */
  1499. SymLoadModule(gdbctx->process->handle, 0, 0, 0, 0, 0);
  1500. reply_buffer_append_str(reply, "<library-list>");
  1501. opt = SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, TRUE);
  1502. SymEnumerateModules64(gdbctx->process->handle, packet_query_libraries_cb, gdbctx);
  1503. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  1504. reply_buffer_append_str(reply, "</library-list>");
  1505. return packet_send_buffer;
  1506. }
  1507. static enum packet_return packet_query_threads(struct gdb_context* gdbctx)
  1508. {
  1509. struct reply_buffer* reply = &gdbctx->qxfer_buffer;
  1510. struct dbg_process* process = gdbctx->process;
  1511. struct dbg_thread* thread;
  1512. if (!process) return packet_error;
  1513. if (gdbctx->qxfer_object_annex[0])
  1514. return packet_reply_error(gdbctx, 0);
  1515. reply_buffer_append_str(reply, "<threads>");
  1516. LIST_FOR_EACH_ENTRY(thread, &process->threads, struct dbg_thread, entry)
  1517. {
  1518. reply_buffer_append_str(reply, "<thread ");
  1519. reply_buffer_append_str(reply, "id=\"");
  1520. reply_buffer_append_uinthex(reply, thread->tid, 4);
  1521. reply_buffer_append_str(reply, "\" name=\"");
  1522. reply_buffer_append_str(reply, thread->name);
  1523. reply_buffer_append_str(reply, "\"/>");
  1524. }
  1525. reply_buffer_append_str(reply, "</threads>");
  1526. return packet_send_buffer;
  1527. }
  1528. static void packet_query_target_xml(struct gdb_context* gdbctx, struct reply_buffer* reply, struct backend_cpu* cpu)
  1529. {
  1530. const char* feature_prefix = NULL;
  1531. const char* feature = NULL;
  1532. char buffer[256];
  1533. int i;
  1534. reply_buffer_append_str(reply, "<target>");
  1535. switch (cpu->machine)
  1536. {
  1537. case IMAGE_FILE_MACHINE_AMD64:
  1538. reply_buffer_append_str(reply, "<architecture>i386:x86-64</architecture>");
  1539. feature_prefix = "org.gnu.gdb.i386.";
  1540. break;
  1541. case IMAGE_FILE_MACHINE_I386:
  1542. reply_buffer_append_str(reply, "<architecture>i386</architecture>");
  1543. feature_prefix = "org.gnu.gdb.i386.";
  1544. break;
  1545. case IMAGE_FILE_MACHINE_ARMNT:
  1546. reply_buffer_append_str(reply, "<architecture>arm</architecture>");
  1547. feature_prefix = "org.gnu.gdb.arm.";
  1548. break;
  1549. case IMAGE_FILE_MACHINE_ARM64:
  1550. reply_buffer_append_str(reply, "<architecture>aarch64</architecture>");
  1551. feature_prefix = "org.gnu.gdb.aarch64.";
  1552. break;
  1553. }
  1554. for (i = 0; i < cpu->gdb_num_regs; ++i)
  1555. {
  1556. if (cpu->gdb_register_map[i].feature)
  1557. {
  1558. if (feature) reply_buffer_append_str(reply, "</feature>");
  1559. feature = cpu->gdb_register_map[i].feature;
  1560. reply_buffer_append_str(reply, "<feature name=\"");
  1561. if (feature_prefix) reply_buffer_append_xmlstr(reply, feature_prefix);
  1562. reply_buffer_append_xmlstr(reply, feature);
  1563. reply_buffer_append_str(reply, "\">");
  1564. if (strcmp(feature_prefix, "org.gnu.gdb.i386.") == 0 &&
  1565. strcmp(feature, "core") == 0)
  1566. reply_buffer_append_str(reply, "<flags id=\"i386_eflags\" size=\"4\">"
  1567. "<field name=\"CF\" start=\"0\" end=\"0\"/>"
  1568. "<field name=\"\" start=\"1\" end=\"1\"/>"
  1569. "<field name=\"PF\" start=\"2\" end=\"2\"/>"
  1570. "<field name=\"AF\" start=\"4\" end=\"4\"/>"
  1571. "<field name=\"ZF\" start=\"6\" end=\"6\"/>"
  1572. "<field name=\"SF\" start=\"7\" end=\"7\"/>"
  1573. "<field name=\"TF\" start=\"8\" end=\"8\"/>"
  1574. "<field name=\"IF\" start=\"9\" end=\"9\"/>"
  1575. "<field name=\"DF\" start=\"10\" end=\"10\"/>"
  1576. "<field name=\"OF\" start=\"11\" end=\"11\"/>"
  1577. "<field name=\"NT\" start=\"14\" end=\"14\"/>"
  1578. "<field name=\"RF\" start=\"16\" end=\"16\"/>"
  1579. "<field name=\"VM\" start=\"17\" end=\"17\"/>"
  1580. "<field name=\"AC\" start=\"18\" end=\"18\"/>"
  1581. "<field name=\"VIF\" start=\"19\" end=\"19\"/>"
  1582. "<field name=\"VIP\" start=\"20\" end=\"20\"/>"
  1583. "<field name=\"ID\" start=\"21\" end=\"21\"/>"
  1584. "</flags>");
  1585. if (strcmp(feature_prefix, "org.gnu.gdb.i386.") == 0 &&
  1586. strcmp(feature, "sse") == 0)
  1587. reply_buffer_append_str(reply, "<vector id=\"v4f\" type=\"ieee_single\" count=\"4\"/>"
  1588. "<vector id=\"v2d\" type=\"ieee_double\" count=\"2\"/>"
  1589. "<vector id=\"v16i8\" type=\"int8\" count=\"16\"/>"
  1590. "<vector id=\"v8i16\" type=\"int16\" count=\"8\"/>"
  1591. "<vector id=\"v4i32\" type=\"int32\" count=\"4\"/>"
  1592. "<vector id=\"v2i64\" type=\"int64\" count=\"2\"/>"
  1593. "<union id=\"vec128\">"
  1594. "<field name=\"v4_float\" type=\"v4f\"/>"
  1595. "<field name=\"v2_double\" type=\"v2d\"/>"
  1596. "<field name=\"v16_int8\" type=\"v16i8\"/>"
  1597. "<field name=\"v8_int16\" type=\"v8i16\"/>"
  1598. "<field name=\"v4_int32\" type=\"v4i32\"/>"
  1599. "<field name=\"v2_int64\" type=\"v2i64\"/>"
  1600. "<field name=\"uint128\" type=\"uint128\"/>"
  1601. "</union>"
  1602. "<flags id=\"i386_mxcsr\" size=\"4\">"
  1603. "<field name=\"IE\" start=\"0\" end=\"0\"/>"
  1604. "<field name=\"DE\" start=\"1\" end=\"1\"/>"
  1605. "<field name=\"ZE\" start=\"2\" end=\"2\"/>"
  1606. "<field name=\"OE\" start=\"3\" end=\"3\"/>"
  1607. "<field name=\"UE\" start=\"4\" end=\"4\"/>"
  1608. "<field name=\"PE\" start=\"5\" end=\"5\"/>"
  1609. "<field name=\"DAZ\" start=\"6\" end=\"6\"/>"
  1610. "<field name=\"IM\" start=\"7\" end=\"7\"/>"
  1611. "<field name=\"DM\" start=\"8\" end=\"8\"/>"
  1612. "<field name=\"ZM\" start=\"9\" end=\"9\"/>"
  1613. "<field name=\"OM\" start=\"10\" end=\"10\"/>"
  1614. "<field name=\"UM\" start=\"11\" end=\"11\"/>"
  1615. "<field name=\"PM\" start=\"12\" end=\"12\"/>"
  1616. "<field name=\"FZ\" start=\"15\" end=\"15\"/>"
  1617. "</flags>");
  1618. }
  1619. snprintf(buffer, ARRAY_SIZE(buffer), "<reg name=\"%s\" bitsize=\"%Iu\"",
  1620. cpu->gdb_register_map[i].name, 8 * cpu->gdb_register_map[i].length);
  1621. reply_buffer_append_str(reply, buffer);
  1622. if (cpu->gdb_register_map[i].type)
  1623. {
  1624. reply_buffer_append_str(reply, " type=\"");
  1625. reply_buffer_append_xmlstr(reply, cpu->gdb_register_map[i].type);
  1626. reply_buffer_append_str(reply, "\"");
  1627. }
  1628. reply_buffer_append_str(reply, "/>");
  1629. }
  1630. if (feature) reply_buffer_append_str(reply, "</feature>");
  1631. reply_buffer_append_str(reply, "</target>");
  1632. }
  1633. static enum packet_return packet_query_features(struct gdb_context* gdbctx)
  1634. {
  1635. struct reply_buffer* reply = &gdbctx->qxfer_buffer;
  1636. struct dbg_process* process = gdbctx->process;
  1637. if (!process) return packet_error;
  1638. if (strncmp(gdbctx->qxfer_object_annex, "target.xml", QX_ANNEX_SIZE) == 0)
  1639. {
  1640. struct backend_cpu *cpu = process->be_cpu;
  1641. if (!cpu) return packet_error;
  1642. packet_query_target_xml(gdbctx, reply, cpu);
  1643. return packet_send_buffer;
  1644. }
  1645. return packet_reply_error(gdbctx, 0);
  1646. }
  1647. static enum packet_return packet_query_exec_file(struct gdb_context* gdbctx)
  1648. {
  1649. struct reply_buffer* reply = &gdbctx->qxfer_buffer;
  1650. struct dbg_process* process = gdbctx->process;
  1651. char *unix_path;
  1652. BOOL is_wow64;
  1653. char *tmp;
  1654. if (!process) return packet_error;
  1655. if (gdbctx->qxfer_object_annex[0] || !process->imageName)
  1656. return packet_reply_error(gdbctx, HOST_EPERM);
  1657. if (!(unix_path = wine_get_unix_file_name(process->imageName)))
  1658. return packet_reply_error(gdbctx, GetLastError() == ERROR_NOT_ENOUGH_MEMORY ? HOST_ENOMEM : HOST_ENOENT);
  1659. if (IsWow64Process(process->handle, &is_wow64) &&
  1660. is_wow64 && (tmp = strstr(unix_path, "system32")))
  1661. memcpy(tmp, "syswow64", 8);
  1662. reply_buffer_append_str(reply, unix_path);
  1663. HeapFree(GetProcessHeap(), 0, unix_path);
  1664. return packet_send_buffer;
  1665. }
  1666. struct qxfer
  1667. {
  1668. const char* name;
  1669. enum packet_return (*handler)(struct gdb_context* gdbctx);
  1670. } qxfer_handlers[] =
  1671. {
  1672. {"libraries", packet_query_libraries},
  1673. {"threads" , packet_query_threads },
  1674. {"features" , packet_query_features },
  1675. {"exec-file", packet_query_exec_file},
  1676. };
  1677. static enum packet_return packet_query(struct gdb_context* gdbctx)
  1678. {
  1679. char object_name[QX_NAME_SIZE], annex[QX_ANNEX_SIZE];
  1680. unsigned int off, len;
  1681. switch (gdbctx->in_packet[0])
  1682. {
  1683. case 'f':
  1684. if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
  1685. {
  1686. struct dbg_thread* thd;
  1687. packet_reply_open(gdbctx);
  1688. packet_reply_add(gdbctx, "m");
  1689. LIST_FOR_EACH_ENTRY(thd, &gdbctx->process->threads, struct dbg_thread, entry)
  1690. {
  1691. packet_reply_val(gdbctx, thd->tid, 4);
  1692. if (list_next(&gdbctx->process->threads, &thd->entry) != NULL)
  1693. packet_reply_add(gdbctx, ",");
  1694. }
  1695. packet_reply_close(gdbctx);
  1696. return packet_done;
  1697. }
  1698. else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
  1699. {
  1700. char result[128];
  1701. packet_reply_open(gdbctx);
  1702. packet_reply_add(gdbctx, "O");
  1703. get_process_info(gdbctx, result, sizeof(result));
  1704. packet_reply_hex_to_str(gdbctx, result);
  1705. packet_reply_close(gdbctx);
  1706. return packet_done;
  1707. }
  1708. break;
  1709. case 's':
  1710. if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
  1711. {
  1712. packet_reply(gdbctx, "l");
  1713. return packet_done;
  1714. }
  1715. else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
  1716. {
  1717. packet_reply(gdbctx, "l");
  1718. return packet_done;
  1719. }
  1720. break;
  1721. case 'A':
  1722. if (strncmp(gdbctx->in_packet, "Attached", gdbctx->in_packet_len) == 0)
  1723. return packet_reply(gdbctx, "1");
  1724. break;
  1725. case 'C':
  1726. if (gdbctx->in_packet_len == 1)
  1727. {
  1728. struct dbg_thread* thd;
  1729. /* FIXME: doc says 16 bit val ??? */
  1730. /* grab first created thread, aka last in list */
  1731. assert(gdbctx->process && !list_empty(&gdbctx->process->threads));
  1732. thd = LIST_ENTRY(list_tail(&gdbctx->process->threads), struct dbg_thread, entry);
  1733. packet_reply_open(gdbctx);
  1734. packet_reply_add(gdbctx, "QC");
  1735. packet_reply_val(gdbctx, thd->tid, 4);
  1736. packet_reply_close(gdbctx);
  1737. return packet_done;
  1738. }
  1739. break;
  1740. case 'O':
  1741. if (strncmp(gdbctx->in_packet, "Offsets", gdbctx->in_packet_len) == 0)
  1742. {
  1743. char buf[64];
  1744. snprintf(buf, sizeof(buf),
  1745. "Text=%08Ix;Data=%08Ix;Bss=%08Ix",
  1746. gdbctx->wine_segs[0], gdbctx->wine_segs[1],
  1747. gdbctx->wine_segs[2]);
  1748. return packet_reply(gdbctx, buf);
  1749. }
  1750. break;
  1751. case 'R':
  1752. if (gdbctx->in_packet_len > 5 && strncmp(gdbctx->in_packet, "Rcmd,", 5) == 0)
  1753. {
  1754. return packet_query_remote_command(gdbctx, gdbctx->in_packet + 5,
  1755. gdbctx->in_packet_len - 5);
  1756. }
  1757. break;
  1758. case 'S':
  1759. if (strncmp(gdbctx->in_packet, "Symbol::", gdbctx->in_packet_len) == 0)
  1760. return packet_ok;
  1761. if (strncmp(gdbctx->in_packet, "Supported", 9) == 0)
  1762. {
  1763. size_t i;
  1764. packet_reply_open(gdbctx);
  1765. packet_reply_add(gdbctx, "QStartNoAckMode+;");
  1766. for (i = 0; i < ARRAY_SIZE(qxfer_handlers); i++)
  1767. {
  1768. packet_reply_add(gdbctx, "qXfer:");
  1769. packet_reply_add(gdbctx, qxfer_handlers[i].name);
  1770. packet_reply_add(gdbctx, ":read+;");
  1771. }
  1772. packet_reply_close(gdbctx);
  1773. return packet_done;
  1774. }
  1775. break;
  1776. case 'T':
  1777. if (gdbctx->in_packet_len > 15 &&
  1778. strncmp(gdbctx->in_packet, "ThreadExtraInfo", 15) == 0 &&
  1779. gdbctx->in_packet[15] == ',')
  1780. {
  1781. unsigned tid;
  1782. char* end;
  1783. char result[128];
  1784. tid = strtol(gdbctx->in_packet + 16, &end, 16);
  1785. if (end == NULL) break;
  1786. get_thread_info(gdbctx, tid, result, sizeof(result));
  1787. packet_reply_open(gdbctx);
  1788. packet_reply_hex_to_str(gdbctx, result);
  1789. packet_reply_close(gdbctx);
  1790. return packet_done;
  1791. }
  1792. if (strncmp(gdbctx->in_packet, "TStatus", 7) == 0)
  1793. {
  1794. /* Tracepoints not supported */
  1795. packet_reply_open(gdbctx);
  1796. packet_reply_close(gdbctx);
  1797. return packet_done;
  1798. }
  1799. break;
  1800. case 'X':
  1801. annex[0] = '\0';
  1802. if (sscanf(gdbctx->in_packet, "Xfer:%31[^:]:read::%x,%x", object_name, &off, &len) == 3 ||
  1803. sscanf(gdbctx->in_packet, "Xfer:%31[^:]:read:%255[^:]:%x,%x", object_name, annex, &off, &len) == 4)
  1804. {
  1805. enum packet_return result;
  1806. int i;
  1807. BOOL more;
  1808. for (i = 0; i < ARRAY_SIZE(qxfer_handlers); i++)
  1809. {
  1810. if (strcmp(qxfer_handlers[i].name, object_name) == 0)
  1811. break;
  1812. }
  1813. if (i >= ARRAY_SIZE(qxfer_handlers))
  1814. {
  1815. ERR("unhandled qXfer %s read %s %u,%u\n", debugstr_a(object_name), debugstr_a(annex), off, len);
  1816. return packet_error;
  1817. }
  1818. TRACE("qXfer %s read %s %u,%u\n", debugstr_a(object_name), debugstr_a(annex), off, len);
  1819. if (off > 0 &&
  1820. gdbctx->qxfer_buffer.len > 0 &&
  1821. gdbctx->qxfer_object_idx == i &&
  1822. strcmp(gdbctx->qxfer_object_annex, annex) == 0)
  1823. {
  1824. result = packet_send_buffer;
  1825. TRACE("qXfer read result = %d (cached)\n", result);
  1826. }
  1827. else
  1828. {
  1829. reply_buffer_clear(&gdbctx->qxfer_buffer);
  1830. gdbctx->qxfer_object_idx = i;
  1831. strcpy(gdbctx->qxfer_object_annex, annex);
  1832. result = (*qxfer_handlers[i].handler)(gdbctx);
  1833. TRACE("qXfer read result = %d\n", result);
  1834. }
  1835. more = FALSE;
  1836. if ((result & ~packet_last_f) == packet_send_buffer)
  1837. {
  1838. packet_reply_xfer(gdbctx, off, len, &more);
  1839. result = (result & packet_last_f) | packet_done;
  1840. }
  1841. if (!more)
  1842. {
  1843. gdbctx->qxfer_object_idx = -1;
  1844. gdbctx->qxfer_object_annex[0] = '\0';
  1845. reply_buffer_clear(&gdbctx->qxfer_buffer);
  1846. }
  1847. return result;
  1848. }
  1849. break;
  1850. }
  1851. ERR("Unhandled query %s\n", debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
  1852. return packet_error;
  1853. }
  1854. static enum packet_return packet_set(struct gdb_context* gdbctx)
  1855. {
  1856. if (strncmp(gdbctx->in_packet, "StartNoAckMode", 14) == 0)
  1857. {
  1858. gdbctx->no_ack_mode = TRUE;
  1859. return packet_ok;
  1860. }
  1861. return packet_error;
  1862. }
  1863. static enum packet_return packet_step(struct gdb_context* gdbctx)
  1864. {
  1865. void *addr;
  1866. if (sscanf(gdbctx->in_packet, "%p", &addr) == 1)
  1867. FIXME("Continue at address %p not supported\n", addr);
  1868. handle_step_or_continue(gdbctx, gdbctx->exec_tid, TRUE, -1);
  1869. wait_for_debuggee(gdbctx);
  1870. return packet_reply_status(gdbctx);
  1871. }
  1872. static enum packet_return packet_thread_alive(struct gdb_context* gdbctx)
  1873. {
  1874. char* end;
  1875. unsigned tid;
  1876. tid = strtol(gdbctx->in_packet, &end, 16);
  1877. if (tid == -1 || tid == 0)
  1878. return packet_reply_error(gdbctx, HOST_EINVAL );
  1879. if (dbg_get_thread(gdbctx->process, tid) != NULL)
  1880. return packet_ok;
  1881. return packet_reply_error(gdbctx, HOST_ESRCH );
  1882. }
  1883. /* =============================================== *
  1884. * P A C K E T I N F R A S T R U C T U R E *
  1885. * =============================================== *
  1886. */
  1887. struct packet_entry
  1888. {
  1889. char key;
  1890. enum packet_return (*handler)(struct gdb_context* gdbctx);
  1891. };
  1892. static struct packet_entry packet_entries[] =
  1893. {
  1894. {'?', packet_last_signal},
  1895. {'c', packet_continue},
  1896. {'C', packet_continue_signal},
  1897. {'D', packet_detach},
  1898. {'g', packet_read_registers},
  1899. {'G', packet_write_registers},
  1900. {'k', packet_kill},
  1901. {'H', packet_thread},
  1902. {'m', packet_read_memory},
  1903. {'M', packet_write_memory},
  1904. {'p', packet_read_register},
  1905. {'P', packet_write_register},
  1906. {'q', packet_query},
  1907. {'Q', packet_set},
  1908. {'s', packet_step},
  1909. {'T', packet_thread_alive},
  1910. {'v', packet_verbose},
  1911. {'z', packet_delete_breakpoint},
  1912. {'Z', packet_insert_breakpoint},
  1913. };
  1914. static BOOL extract_packets(struct gdb_context* gdbctx)
  1915. {
  1916. char *ptr, *sum = gdbctx->in_buf, *end = gdbctx->in_buf + gdbctx->in_len;
  1917. enum packet_return ret = packet_error;
  1918. unsigned int cksum;
  1919. int i, len;
  1920. /* ptr points to the beginning ('$') of the current packet
  1921. * sum points to the beginning ('#') of the current packet checksum ("#xx")
  1922. * len is the length of the current packet data (sum - ptr - 1)
  1923. * end points to the end of the received data buffer
  1924. */
  1925. while (!gdbctx->no_ack_mode &&
  1926. (ptr = memchr(sum, '$', end - sum)) &&
  1927. (sum = memchr(ptr, '#', end - ptr)) &&
  1928. (end - sum >= 3) && sscanf(sum, "#%02x", &cksum) == 1)
  1929. {
  1930. len = sum - ptr - 1;
  1931. sum += 3;
  1932. if (cksum == checksum(ptr + 1, len))
  1933. {
  1934. TRACE("Acking: %s\n", debugstr_an(ptr, sum - ptr));
  1935. send(gdbctx->sock, "+", 1, 0);
  1936. }
  1937. else
  1938. {
  1939. ERR("Nacking: %s (checksum: %d != %d)\n", debugstr_an(ptr, sum - ptr),
  1940. cksum, checksum(ptr + 1, len));
  1941. send(gdbctx->sock, "-", 1, 0);
  1942. }
  1943. }
  1944. while ((ret & packet_last_f) == 0 &&
  1945. (ptr = memchr(gdbctx->in_buf, '$', gdbctx->in_len)) &&
  1946. (sum = memchr(ptr, '#', end - ptr)) &&
  1947. (end - sum >= 3) && sscanf(sum, "#%02x", &cksum) == 1)
  1948. {
  1949. if (ptr != gdbctx->in_buf)
  1950. WARN("Ignoring: %s\n", debugstr_an(gdbctx->in_buf, ptr - gdbctx->in_buf));
  1951. len = sum - ptr - 1;
  1952. sum += 3;
  1953. if (cksum == checksum(ptr + 1, len))
  1954. {
  1955. TRACE("Handling: %s\n", debugstr_an(ptr, sum - ptr));
  1956. ret = packet_error;
  1957. gdbctx->in_packet = ptr + 2;
  1958. gdbctx->in_packet_len = len - 1;
  1959. gdbctx->in_packet[gdbctx->in_packet_len] = '\0';
  1960. for (i = 0; i < ARRAY_SIZE(packet_entries); i++)
  1961. if (packet_entries[i].key == ptr[1])
  1962. break;
  1963. if (i == ARRAY_SIZE(packet_entries))
  1964. WARN("Unhandled: %s\n", debugstr_an(ptr + 1, len));
  1965. else if (((ret = (packet_entries[i].handler)(gdbctx)) & ~packet_last_f) == packet_error)
  1966. WARN("Failed: %s\n", debugstr_an(ptr + 1, len));
  1967. switch (ret & ~packet_last_f)
  1968. {
  1969. case packet_error: packet_reply(gdbctx, ""); break;
  1970. case packet_ok: packet_reply(gdbctx, "OK"); break;
  1971. case packet_done: break;
  1972. }
  1973. TRACE("Reply: %s\n", debugstr_an((char *)gdbctx->out_buf.base, gdbctx->out_buf.len));
  1974. i = send(gdbctx->sock, (char *)gdbctx->out_buf.base, gdbctx->out_buf.len, 0);
  1975. assert(i == gdbctx->out_buf.len);
  1976. reply_buffer_clear(&gdbctx->out_buf);
  1977. }
  1978. else
  1979. WARN("Ignoring: %s (checksum: %d != %d)\n", debugstr_an(ptr, sum - ptr),
  1980. cksum, checksum(ptr + 1, len));
  1981. gdbctx->in_len = end - sum;
  1982. memmove(gdbctx->in_buf, sum, end - sum);
  1983. end = gdbctx->in_buf + gdbctx->in_len;
  1984. }
  1985. return (ret & packet_last_f);
  1986. }
  1987. static int fetch_data(struct gdb_context* gdbctx)
  1988. {
  1989. int len, in_len = gdbctx->in_len;
  1990. assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
  1991. for (;;)
  1992. {
  1993. #define STEP 128
  1994. if (gdbctx->in_len + STEP > gdbctx->in_buf_alloc)
  1995. gdbctx->in_buf = realloc(gdbctx->in_buf, gdbctx->in_buf_alloc += STEP);
  1996. #undef STEP
  1997. len = recv(gdbctx->sock, gdbctx->in_buf + gdbctx->in_len, gdbctx->in_buf_alloc - gdbctx->in_len - 1, 0);
  1998. if (len <= 0) break;
  1999. gdbctx->in_len += len;
  2000. assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
  2001. if (len < gdbctx->in_buf_alloc - gdbctx->in_len) break;
  2002. }
  2003. gdbctx->in_buf[gdbctx->in_len] = '\0';
  2004. return gdbctx->in_len - in_len;
  2005. }
  2006. #define FLAG_NO_START 1
  2007. #define FLAG_WITH_XTERM 2
  2008. static BOOL gdb_exec(unsigned port, unsigned flags)
  2009. {
  2010. WCHAR tmp[MAX_PATH], buf[MAX_PATH];
  2011. const char *argv[6];
  2012. char *unix_tmp;
  2013. const char *gdb_path;
  2014. FILE* f;
  2015. if (!(gdb_path = getenv("WINE_GDB"))) gdb_path = "gdb";
  2016. GetTempPathW( MAX_PATH, buf );
  2017. GetTempFileNameW( buf, L"gdb", 0, tmp );
  2018. if ((f = _wfopen( tmp, L"w+" )) == NULL) return FALSE;
  2019. unix_tmp = wine_get_unix_file_name( tmp );
  2020. fprintf(f, "target remote localhost:%d\n", ntohs(port));
  2021. fprintf(f, "set prompt Wine-gdb>\\ \n");
  2022. /* gdb 5.1 seems to require it, won't hurt anyway */
  2023. fprintf(f, "sharedlibrary\n");
  2024. /* This is needed (but not a decent & final fix)
  2025. * Without this, gdb would skip our inter-DLL relay code (because
  2026. * we don't have any line number information for the relay code)
  2027. * With this, we will stop on first instruction of the stub, and
  2028. * reusing step, will get us through the relay stub at the actual
  2029. * function we're looking at.
  2030. */
  2031. fprintf(f, "set step-mode on\n");
  2032. /* tell gdb to delete this file when done handling it... */
  2033. fprintf(f, "shell rm -f \"%s\"\n", unix_tmp);
  2034. fclose(f);
  2035. argv[0] = "xterm";
  2036. argv[1] = "-e";
  2037. argv[2] = gdb_path;
  2038. argv[3] = "-x";
  2039. argv[4] = unix_tmp;
  2040. argv[5] = NULL;
  2041. if (flags & FLAG_WITH_XTERM)
  2042. __wine_unix_spawnvp( (char **)argv, FALSE );
  2043. else
  2044. __wine_unix_spawnvp( (char **)argv + 2, FALSE );
  2045. HeapFree( GetProcessHeap(), 0, unix_tmp );
  2046. return TRUE;
  2047. }
  2048. static BOOL gdb_startup(struct gdb_context* gdbctx, unsigned flags, unsigned port)
  2049. {
  2050. SOCKET sock;
  2051. BOOL reuseaddr = TRUE;
  2052. struct sockaddr_in s_addrs = {0};
  2053. int s_len = sizeof(s_addrs);
  2054. fd_set read_fds;
  2055. WSADATA data;
  2056. BOOL ret = FALSE;
  2057. WSAStartup( MAKEWORD(2, 2), &data );
  2058. /* step 1: create socket for gdb connection request */
  2059. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  2060. {
  2061. ERR("Failed to create socket: %u\n", WSAGetLastError());
  2062. return FALSE;
  2063. }
  2064. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof(reuseaddr));
  2065. s_addrs.sin_family = AF_INET;
  2066. s_addrs.sin_addr.S_un.S_addr = INADDR_ANY;
  2067. s_addrs.sin_port = htons(port);
  2068. if (bind(sock, (struct sockaddr *)&s_addrs, sizeof(s_addrs)) == -1)
  2069. goto cleanup;
  2070. if (listen(sock, 1) == -1 || getsockname(sock, (struct sockaddr *)&s_addrs, &s_len) == -1)
  2071. goto cleanup;
  2072. /* step 2: do the process internal creation */
  2073. handle_debug_event(gdbctx, FALSE);
  2074. /* step 3: fire up gdb (if requested) */
  2075. if (flags & FLAG_NO_START)
  2076. fprintf(stderr, "target remote localhost:%d\n", ntohs(s_addrs.sin_port));
  2077. else
  2078. gdb_exec(s_addrs.sin_port, flags);
  2079. /* step 4: wait for gdb to connect actually */
  2080. FD_ZERO( &read_fds );
  2081. FD_SET( sock, &read_fds );
  2082. if (select( 0, &read_fds, NULL, NULL, NULL ) > 0)
  2083. {
  2084. int dummy = 1;
  2085. gdbctx->sock = accept(sock, (struct sockaddr *)&s_addrs, &s_len);
  2086. if (gdbctx->sock != INVALID_SOCKET)
  2087. {
  2088. ret = TRUE;
  2089. TRACE("connected on %Iu\n", gdbctx->sock);
  2090. /* don't keep our small packets too long: send them ASAP back to GDB
  2091. * without this, GDB really crawls
  2092. */
  2093. setsockopt(gdbctx->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&dummy, sizeof(dummy));
  2094. }
  2095. }
  2096. else ERR("Failed to connect to gdb: %u\n", WSAGetLastError());
  2097. cleanup:
  2098. closesocket(sock);
  2099. return ret;
  2100. }
  2101. static BOOL gdb_init_context(struct gdb_context* gdbctx, unsigned flags, unsigned port)
  2102. {
  2103. int i;
  2104. gdbctx->sock = INVALID_SOCKET;
  2105. gdbctx->in_buf = NULL;
  2106. gdbctx->in_buf_alloc = 0;
  2107. gdbctx->in_len = 0;
  2108. memset(&gdbctx->out_buf, 0, sizeof(gdbctx->out_buf));
  2109. gdbctx->out_curr_packet = -1;
  2110. gdbctx->exec_tid = -1;
  2111. gdbctx->other_tid = -1;
  2112. list_init(&gdbctx->xpoint_list);
  2113. gdbctx->process = NULL;
  2114. gdbctx->no_ack_mode = FALSE;
  2115. for (i = 0; i < ARRAY_SIZE(gdbctx->wine_segs); i++)
  2116. gdbctx->wine_segs[i] = 0;
  2117. gdbctx->qxfer_object_idx = -1;
  2118. memset(gdbctx->qxfer_object_annex, 0, sizeof(gdbctx->qxfer_object_annex));
  2119. memset(&gdbctx->qxfer_buffer, 0, sizeof(gdbctx->qxfer_buffer));
  2120. /* wait for first trap */
  2121. while (WaitForDebugEvent(&gdbctx->de, INFINITE))
  2122. {
  2123. if (gdbctx->de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
  2124. {
  2125. /* this should be the first event we get,
  2126. * and the only one of this type */
  2127. assert(gdbctx->process == NULL && gdbctx->de.dwProcessId == dbg_curr_pid);
  2128. /* gdbctx->dwProcessId = pid; */
  2129. if (!gdb_startup(gdbctx, flags, port)) return FALSE;
  2130. }
  2131. else if (!handle_debug_event(gdbctx, FALSE))
  2132. break;
  2133. ContinueDebugEvent(gdbctx->de.dwProcessId, gdbctx->de.dwThreadId, DBG_CONTINUE);
  2134. }
  2135. return TRUE;
  2136. }
  2137. static int gdb_remote(unsigned flags, unsigned port)
  2138. {
  2139. struct gdb_context gdbctx;
  2140. if (!gdb_init_context(&gdbctx, flags, port)) return 0;
  2141. /* don't handle ctrl-c, but let gdb do the job */
  2142. SetConsoleCtrlHandler(NULL, TRUE);
  2143. for (;;)
  2144. {
  2145. fd_set read_fds, err_fds;
  2146. FD_ZERO( &read_fds );
  2147. FD_ZERO( &err_fds );
  2148. FD_SET( gdbctx.sock, &read_fds );
  2149. FD_SET( gdbctx.sock, &err_fds );
  2150. if (select( 0, &read_fds, NULL, &err_fds, NULL ) == -1) break;
  2151. if (FD_ISSET( gdbctx.sock, &err_fds ))
  2152. {
  2153. ERR("gdb hung up\n");
  2154. /* kill also debuggee process - questionnable - */
  2155. detach_debuggee(&gdbctx, TRUE);
  2156. break;
  2157. }
  2158. if (FD_ISSET( gdbctx.sock, &read_fds ))
  2159. {
  2160. if (fetch_data(&gdbctx) > 0)
  2161. {
  2162. if (extract_packets(&gdbctx)) break;
  2163. }
  2164. }
  2165. }
  2166. return 0;
  2167. }
  2168. int gdb_main(int argc, char* argv[])
  2169. {
  2170. unsigned gdb_flags = 0, port = 0;
  2171. char *port_end;
  2172. argc--; argv++;
  2173. while (argc > 0 && argv[0][0] == '-')
  2174. {
  2175. if (strcmp(argv[0], "--no-start") == 0)
  2176. {
  2177. gdb_flags |= FLAG_NO_START;
  2178. argc--; argv++;
  2179. continue;
  2180. }
  2181. if (strcmp(argv[0], "--with-xterm") == 0)
  2182. {
  2183. gdb_flags |= FLAG_WITH_XTERM;
  2184. argc--; argv++;
  2185. continue;
  2186. }
  2187. if (strcmp(argv[0], "--port") == 0 && argc > 1)
  2188. {
  2189. port = strtoul(argv[1], &port_end, 10);
  2190. if (*port_end)
  2191. {
  2192. fprintf(stderr, "Invalid port: %s\n", argv[1]);
  2193. return -1;
  2194. }
  2195. argc -= 2; argv += 2;
  2196. continue;
  2197. }
  2198. return -1;
  2199. }
  2200. if (dbg_active_attach(argc, argv) == start_ok ||
  2201. dbg_active_launch(argc, argv) == start_ok)
  2202. return gdb_remote(gdb_flags, port);
  2203. return -1;
  2204. }