gdbproxy.c 85 KB

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