win32-low.cc 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /* Low level interface to Windows debugging, for gdbserver.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by Leo Zayas. Based on "win32-nat.c" from GDB.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "server.h"
  16. #include "regcache.h"
  17. #include "gdb/fileio.h"
  18. #include "mem-break.h"
  19. #include "win32-low.h"
  20. #include "gdbthread.h"
  21. #include "dll.h"
  22. #include "hostio.h"
  23. #include <windows.h>
  24. #include <winnt.h>
  25. #include <imagehlp.h>
  26. #include <tlhelp32.h>
  27. #include <psapi.h>
  28. #include <process.h>
  29. #include "gdbsupport/gdb_tilde_expand.h"
  30. #include "gdbsupport/common-inferior.h"
  31. #include "gdbsupport/gdb_wait.h"
  32. using namespace windows_nat;
  33. static windows_process_info windows_process;
  34. #ifndef USE_WIN32API
  35. #include <sys/cygwin.h>
  36. #endif
  37. #define OUTMSG(X) do { printf X; fflush (stderr); } while (0)
  38. #define OUTMSG2(X) \
  39. do \
  40. { \
  41. if (debug_threads) \
  42. { \
  43. printf X; \
  44. fflush (stderr); \
  45. } \
  46. } while (0)
  47. #ifndef _T
  48. #define _T(x) TEXT (x)
  49. #endif
  50. #ifndef COUNTOF
  51. #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
  52. #endif
  53. int using_threads = 1;
  54. /* Globals. */
  55. static int attaching = 0;
  56. /* A status that hasn't been reported to the core yet, and so
  57. win32_wait should return it next, instead of fetching the next
  58. debug event off the win32 API. */
  59. static struct target_waitstatus cached_status;
  60. /* Non zero if an interrupt request is to be satisfied by suspending
  61. all threads. */
  62. static int soft_interrupt_requested = 0;
  63. /* Non zero if the inferior is stopped in a simulated breakpoint done
  64. by suspending all the threads. */
  65. static int faked_breakpoint = 0;
  66. /* True if current_process_handle needs to be closed. */
  67. static bool open_process_used = false;
  68. const struct target_desc *win32_tdesc;
  69. #ifdef __x86_64__
  70. const struct target_desc *wow64_win32_tdesc;
  71. #endif
  72. #define NUM_REGS (the_low_target.num_regs ())
  73. /* Get the thread ID from the current selected inferior (the current
  74. thread). */
  75. static ptid_t
  76. current_thread_ptid (void)
  77. {
  78. return current_ptid;
  79. }
  80. /* The current debug event from WaitForDebugEvent. */
  81. static ptid_t
  82. debug_event_ptid (DEBUG_EVENT *event)
  83. {
  84. return ptid_t (event->dwProcessId, event->dwThreadId, 0);
  85. }
  86. /* Get the thread context of the thread associated with TH. */
  87. static void
  88. win32_get_thread_context (windows_thread_info *th)
  89. {
  90. #ifdef __x86_64__
  91. if (wow64_process)
  92. memset (&th->wow64_context, 0, sizeof (WOW64_CONTEXT));
  93. else
  94. #endif
  95. memset (&th->context, 0, sizeof (CONTEXT));
  96. (*the_low_target.get_thread_context) (th);
  97. }
  98. /* Set the thread context of the thread associated with TH. */
  99. static void
  100. win32_set_thread_context (windows_thread_info *th)
  101. {
  102. #ifdef __x86_64__
  103. if (wow64_process)
  104. Wow64SetThreadContext (th->h, &th->wow64_context);
  105. else
  106. #endif
  107. SetThreadContext (th->h, &th->context);
  108. }
  109. /* Set the thread context of the thread associated with TH. */
  110. static void
  111. win32_prepare_to_resume (windows_thread_info *th)
  112. {
  113. if (the_low_target.prepare_to_resume != NULL)
  114. (*the_low_target.prepare_to_resume) (th);
  115. }
  116. /* See win32-low.h. */
  117. void
  118. win32_require_context (windows_thread_info *th)
  119. {
  120. DWORD context_flags;
  121. #ifdef __x86_64__
  122. if (wow64_process)
  123. context_flags = th->wow64_context.ContextFlags;
  124. else
  125. #endif
  126. context_flags = th->context.ContextFlags;
  127. if (context_flags == 0)
  128. {
  129. th->suspend ();
  130. win32_get_thread_context (th);
  131. }
  132. }
  133. /* See nat/windows-nat.h. */
  134. windows_thread_info *
  135. windows_nat::windows_process_info::thread_rec
  136. (ptid_t ptid, thread_disposition_type disposition)
  137. {
  138. thread_info *thread = find_thread_ptid (ptid);
  139. if (thread == NULL)
  140. return NULL;
  141. windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
  142. if (disposition != DONT_INVALIDATE_CONTEXT)
  143. win32_require_context (th);
  144. return th;
  145. }
  146. /* Add a thread to the thread list. */
  147. static windows_thread_info *
  148. child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
  149. {
  150. windows_thread_info *th;
  151. ptid_t ptid = ptid_t (pid, tid, 0);
  152. if ((th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
  153. return th;
  154. CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb;
  155. #ifdef __x86_64__
  156. /* For WOW64 processes, this is actually the pointer to the 64bit TIB,
  157. and the 32bit TIB is exactly 2 pages after it. */
  158. if (wow64_process)
  159. base += 2 * 4096; /* page size = 4096 */
  160. #endif
  161. th = new windows_thread_info (tid, h, base);
  162. add_thread (ptid, th);
  163. if (the_low_target.thread_added != NULL)
  164. (*the_low_target.thread_added) (th);
  165. return th;
  166. }
  167. /* Delete a thread from the list of threads. */
  168. static void
  169. delete_thread_info (thread_info *thread)
  170. {
  171. windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
  172. remove_thread (thread);
  173. delete th;
  174. }
  175. /* Delete a thread from the list of threads. */
  176. static void
  177. child_delete_thread (DWORD pid, DWORD tid)
  178. {
  179. /* If the last thread is exiting, just return. */
  180. if (all_threads.size () == 1)
  181. return;
  182. thread_info *thread = find_thread_ptid (ptid_t (pid, tid));
  183. if (thread == NULL)
  184. return;
  185. delete_thread_info (thread);
  186. }
  187. /* These watchpoint related wrapper functions simply pass on the function call
  188. if the low target has registered a corresponding function. */
  189. bool
  190. win32_process_target::supports_z_point_type (char z_type)
  191. {
  192. return (z_type == Z_PACKET_SW_BP
  193. || (the_low_target.supports_z_point_type != NULL
  194. && the_low_target.supports_z_point_type (z_type)));
  195. }
  196. int
  197. win32_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  198. int size, raw_breakpoint *bp)
  199. {
  200. if (type == raw_bkpt_type_sw)
  201. return insert_memory_breakpoint (bp);
  202. else if (the_low_target.insert_point != NULL)
  203. return the_low_target.insert_point (type, addr, size, bp);
  204. else
  205. /* Unsupported (see target.h). */
  206. return 1;
  207. }
  208. int
  209. win32_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  210. int size, raw_breakpoint *bp)
  211. {
  212. if (type == raw_bkpt_type_sw)
  213. return remove_memory_breakpoint (bp);
  214. else if (the_low_target.remove_point != NULL)
  215. return the_low_target.remove_point (type, addr, size, bp);
  216. else
  217. /* Unsupported (see target.h). */
  218. return 1;
  219. }
  220. bool
  221. win32_process_target::stopped_by_watchpoint ()
  222. {
  223. if (the_low_target.stopped_by_watchpoint != NULL)
  224. return the_low_target.stopped_by_watchpoint ();
  225. else
  226. return false;
  227. }
  228. CORE_ADDR
  229. win32_process_target::stopped_data_address ()
  230. {
  231. if (the_low_target.stopped_data_address != NULL)
  232. return the_low_target.stopped_data_address ();
  233. else
  234. return 0;
  235. }
  236. /* Transfer memory from/to the debugged process. */
  237. static int
  238. child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
  239. int write, process_stratum_target *target)
  240. {
  241. BOOL success;
  242. SIZE_T done = 0;
  243. DWORD lasterror = 0;
  244. uintptr_t addr = (uintptr_t) memaddr;
  245. if (write)
  246. {
  247. success = WriteProcessMemory (windows_process.handle, (LPVOID) addr,
  248. (LPCVOID) our, len, &done);
  249. if (!success)
  250. lasterror = GetLastError ();
  251. FlushInstructionCache (windows_process.handle, (LPCVOID) addr, len);
  252. }
  253. else
  254. {
  255. success = ReadProcessMemory (windows_process.handle, (LPCVOID) addr,
  256. (LPVOID) our, len, &done);
  257. if (!success)
  258. lasterror = GetLastError ();
  259. }
  260. if (!success && lasterror == ERROR_PARTIAL_COPY && done > 0)
  261. return done;
  262. else
  263. return success ? done : -1;
  264. }
  265. /* Clear out any old thread list and reinitialize it to a pristine
  266. state. */
  267. static void
  268. child_init_thread_list (void)
  269. {
  270. for_each_thread (delete_thread_info);
  271. }
  272. /* Zero during the child initialization phase, and nonzero otherwise. */
  273. static int child_initialization_done = 0;
  274. static void
  275. do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
  276. {
  277. struct process_info *proc;
  278. windows_process.last_sig = GDB_SIGNAL_0;
  279. windows_process.handle = proch;
  280. windows_process.id = pid;
  281. windows_process.main_thread_id = 0;
  282. soft_interrupt_requested = 0;
  283. faked_breakpoint = 0;
  284. open_process_used = true;
  285. memset (&windows_process.current_event, 0,
  286. sizeof (windows_process.current_event));
  287. #ifdef __x86_64__
  288. BOOL wow64;
  289. if (!IsWow64Process (proch, &wow64))
  290. {
  291. DWORD err = GetLastError ();
  292. error ("Check if WOW64 process failed (error %d): %s\n",
  293. (int) err, strwinerror (err));
  294. }
  295. wow64_process = wow64;
  296. if (wow64_process
  297. && (Wow64GetThreadContext == nullptr
  298. || Wow64SetThreadContext == nullptr))
  299. error ("WOW64 debugging is not supported on this system.\n");
  300. ignore_first_breakpoint = !attached && wow64_process;
  301. #endif
  302. proc = add_process (pid, attached);
  303. #ifdef __x86_64__
  304. if (wow64_process)
  305. proc->tdesc = wow64_win32_tdesc;
  306. else
  307. #endif
  308. proc->tdesc = win32_tdesc;
  309. child_init_thread_list ();
  310. child_initialization_done = 0;
  311. if (the_low_target.initial_stuff != NULL)
  312. (*the_low_target.initial_stuff) ();
  313. cached_status.set_ignore ();
  314. /* Flush all currently pending debug events (thread and dll list) up
  315. to the initial breakpoint. */
  316. while (1)
  317. {
  318. struct target_waitstatus status;
  319. the_target->wait (minus_one_ptid, &status, 0);
  320. /* Note win32_wait doesn't return thread events. */
  321. if (status.kind () != TARGET_WAITKIND_LOADED)
  322. {
  323. cached_status = status;
  324. break;
  325. }
  326. {
  327. struct thread_resume resume;
  328. resume.thread = minus_one_ptid;
  329. resume.kind = resume_continue;
  330. resume.sig = 0;
  331. the_target->resume (&resume, 1);
  332. }
  333. }
  334. /* Now that the inferior has been started and all DLLs have been mapped,
  335. we can iterate over all DLLs and load them in.
  336. We avoid doing it any earlier because, on certain versions of Windows,
  337. LOAD_DLL_DEBUG_EVENTs are sometimes not complete. In particular,
  338. we have seen on Windows 8.1 that the ntdll.dll load event does not
  339. include the DLL name, preventing us from creating an associated SO.
  340. A possible explanation is that ntdll.dll might be mapped before
  341. the SO info gets created by the Windows system -- ntdll.dll is
  342. the first DLL to be reported via LOAD_DLL_DEBUG_EVENT and other DLLs
  343. do not seem to suffer from that problem.
  344. Rather than try to work around this sort of issue, it is much
  345. simpler to just ignore DLL load/unload events during the startup
  346. phase, and then process them all in one batch now. */
  347. windows_process.add_all_dlls ();
  348. child_initialization_done = 1;
  349. }
  350. /* Resume all artificially suspended threads if we are continuing
  351. execution. */
  352. static void
  353. continue_one_thread (thread_info *thread, int thread_id)
  354. {
  355. windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
  356. if (thread_id == -1 || thread_id == th->tid)
  357. {
  358. win32_prepare_to_resume (th);
  359. if (th->suspended)
  360. {
  361. DWORD *context_flags;
  362. #ifdef __x86_64__
  363. if (wow64_process)
  364. context_flags = &th->wow64_context.ContextFlags;
  365. else
  366. #endif
  367. context_flags = &th->context.ContextFlags;
  368. if (*context_flags)
  369. {
  370. win32_set_thread_context (th);
  371. *context_flags = 0;
  372. }
  373. th->resume ();
  374. }
  375. }
  376. }
  377. static BOOL
  378. child_continue (DWORD continue_status, int thread_id)
  379. {
  380. windows_process.desired_stop_thread_id = thread_id;
  381. if (windows_process.matching_pending_stop (debug_threads))
  382. return TRUE;
  383. /* The inferior will only continue after the ContinueDebugEvent
  384. call. */
  385. for_each_thread ([&] (thread_info *thread)
  386. {
  387. continue_one_thread (thread, thread_id);
  388. });
  389. faked_breakpoint = 0;
  390. return continue_last_debug_event (continue_status, debug_threads);
  391. }
  392. /* Fetch register(s) from the current thread context. */
  393. static void
  394. child_fetch_inferior_registers (struct regcache *regcache, int r)
  395. {
  396. int regno;
  397. windows_thread_info *th
  398. = windows_process.thread_rec (current_thread_ptid (),
  399. INVALIDATE_CONTEXT);
  400. if (r == -1 || r > NUM_REGS)
  401. child_fetch_inferior_registers (regcache, NUM_REGS);
  402. else
  403. for (regno = 0; regno < r; regno++)
  404. (*the_low_target.fetch_inferior_register) (regcache, th, regno);
  405. }
  406. /* Store a new register value into the current thread context. We don't
  407. change the program's context until later, when we resume it. */
  408. static void
  409. child_store_inferior_registers (struct regcache *regcache, int r)
  410. {
  411. int regno;
  412. windows_thread_info *th
  413. = windows_process.thread_rec (current_thread_ptid (),
  414. INVALIDATE_CONTEXT);
  415. if (r == -1 || r == 0 || r > NUM_REGS)
  416. child_store_inferior_registers (regcache, NUM_REGS);
  417. else
  418. for (regno = 0; regno < r; regno++)
  419. (*the_low_target.store_inferior_register) (regcache, th, regno);
  420. }
  421. /* Map the Windows error number in ERROR to a locale-dependent error
  422. message string and return a pointer to it. Typically, the values
  423. for ERROR come from GetLastError.
  424. The string pointed to shall not be modified by the application,
  425. but may be overwritten by a subsequent call to strwinerror
  426. The strwinerror function does not change the current setting
  427. of GetLastError. */
  428. char *
  429. strwinerror (DWORD error)
  430. {
  431. static char buf[1024];
  432. TCHAR *msgbuf;
  433. DWORD lasterr = GetLastError ();
  434. DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
  435. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  436. NULL,
  437. error,
  438. 0, /* Default language */
  439. (LPTSTR) &msgbuf,
  440. 0,
  441. NULL);
  442. if (chars != 0)
  443. {
  444. /* If there is an \r\n appended, zap it. */
  445. if (chars >= 2
  446. && msgbuf[chars - 2] == '\r'
  447. && msgbuf[chars - 1] == '\n')
  448. {
  449. chars -= 2;
  450. msgbuf[chars] = 0;
  451. }
  452. if (chars > ((COUNTOF (buf)) - 1))
  453. {
  454. chars = COUNTOF (buf) - 1;
  455. msgbuf [chars] = 0;
  456. }
  457. #ifdef UNICODE
  458. wcstombs (buf, msgbuf, chars + 1);
  459. #else
  460. strncpy (buf, msgbuf, chars + 1);
  461. #endif
  462. LocalFree (msgbuf);
  463. }
  464. else
  465. sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
  466. SetLastError (lasterr);
  467. return buf;
  468. }
  469. static BOOL
  470. create_process (const char *program, char *args,
  471. DWORD flags, PROCESS_INFORMATION *pi)
  472. {
  473. const std::string &inferior_cwd = get_inferior_cwd ();
  474. BOOL ret;
  475. size_t argslen, proglen;
  476. proglen = strlen (program) + 1;
  477. argslen = strlen (args) + proglen;
  478. STARTUPINFOA si = { sizeof (STARTUPINFOA) };
  479. char *program_and_args = (char *) alloca (argslen + 1);
  480. strcpy (program_and_args, program);
  481. strcat (program_and_args, " ");
  482. strcat (program_and_args, args);
  483. ret = CreateProcessA (program, /* image name */
  484. program_and_args, /* command line */
  485. NULL, /* security */
  486. NULL, /* thread */
  487. TRUE, /* inherit handles */
  488. flags, /* start flags */
  489. NULL, /* environment */
  490. /* current directory */
  491. (inferior_cwd.empty ()
  492. ? NULL
  493. : gdb_tilde_expand (inferior_cwd.c_str ()).c_str()),
  494. &si, /* start info */
  495. pi); /* proc info */
  496. return ret;
  497. }
  498. /* Start a new process.
  499. PROGRAM is the program name.
  500. PROGRAM_ARGS is the vector containing the inferior's args.
  501. Returns the new PID on success, -1 on failure. Registers the new
  502. process with the process list. */
  503. int
  504. win32_process_target::create_inferior (const char *program,
  505. const std::vector<char *> &program_args)
  506. {
  507. client_state &cs = get_client_state ();
  508. #ifndef USE_WIN32API
  509. char real_path[PATH_MAX];
  510. char *orig_path, *new_path, *path_ptr;
  511. #endif
  512. BOOL ret;
  513. DWORD flags;
  514. PROCESS_INFORMATION pi;
  515. DWORD err;
  516. std::string str_program_args = construct_inferior_arguments (program_args);
  517. char *args = (char *) str_program_args.c_str ();
  518. /* win32_wait needs to know we're not attaching. */
  519. attaching = 0;
  520. if (!program)
  521. error ("No executable specified, specify executable to debug.\n");
  522. flags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
  523. #ifndef USE_WIN32API
  524. orig_path = NULL;
  525. path_ptr = getenv ("PATH");
  526. if (path_ptr)
  527. {
  528. int size = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, NULL, 0);
  529. orig_path = (char *) alloca (strlen (path_ptr) + 1);
  530. new_path = (char *) alloca (size);
  531. strcpy (orig_path, path_ptr);
  532. cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, path_ptr, new_path, size);
  533. setenv ("PATH", new_path, 1);
  534. }
  535. cygwin_conv_path (CCP_POSIX_TO_WIN_A, program, real_path, PATH_MAX);
  536. program = real_path;
  537. #endif
  538. OUTMSG2 (("Command line is \"%s %s\"\n", program, args));
  539. #ifdef CREATE_NEW_PROCESS_GROUP
  540. flags |= CREATE_NEW_PROCESS_GROUP;
  541. #endif
  542. ret = create_process (program, args, flags, &pi);
  543. err = GetLastError ();
  544. if (!ret && err == ERROR_FILE_NOT_FOUND)
  545. {
  546. char *exename = (char *) alloca (strlen (program) + 5);
  547. strcat (strcpy (exename, program), ".exe");
  548. ret = create_process (exename, args, flags, &pi);
  549. err = GetLastError ();
  550. }
  551. #ifndef USE_WIN32API
  552. if (orig_path)
  553. setenv ("PATH", orig_path, 1);
  554. #endif
  555. if (!ret)
  556. {
  557. error ("Error creating process \"%s %s\", (error %d): %s\n",
  558. program, args, (int) err, strwinerror (err));
  559. }
  560. else
  561. {
  562. OUTMSG2 (("Process created: %s %s\n", program, (char *) args));
  563. }
  564. CloseHandle (pi.hThread);
  565. do_initial_child_stuff (pi.hProcess, pi.dwProcessId, 0);
  566. /* Wait till we are at 1st instruction in program, return new pid
  567. (assuming success). */
  568. cs.last_ptid = wait (ptid_t (windows_process.id), &cs.last_status, 0);
  569. /* Necessary for handle_v_kill. */
  570. signal_pid = windows_process.id;
  571. return windows_process.id;
  572. }
  573. /* Attach to a running process.
  574. PID is the process ID to attach to, specified by the user
  575. or a higher layer. */
  576. int
  577. win32_process_target::attach (unsigned long pid)
  578. {
  579. HANDLE h;
  580. DWORD err;
  581. h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
  582. if (h != NULL)
  583. {
  584. if (DebugActiveProcess (pid))
  585. {
  586. DebugSetProcessKillOnExit (FALSE);
  587. /* win32_wait needs to know we're attaching. */
  588. attaching = 1;
  589. do_initial_child_stuff (h, pid, 1);
  590. return 0;
  591. }
  592. CloseHandle (h);
  593. }
  594. err = GetLastError ();
  595. error ("Attach to process failed (error %d): %s\n",
  596. (int) err, strwinerror (err));
  597. }
  598. /* See nat/windows-nat.h. */
  599. int
  600. windows_nat::windows_process_info::handle_output_debug_string
  601. (struct target_waitstatus *ourstatus)
  602. {
  603. #define READ_BUFFER_LEN 1024
  604. CORE_ADDR addr;
  605. char s[READ_BUFFER_LEN + 1] = { 0 };
  606. DWORD nbytes = current_event.u.DebugString.nDebugStringLength;
  607. if (nbytes == 0)
  608. return 0;
  609. if (nbytes > READ_BUFFER_LEN)
  610. nbytes = READ_BUFFER_LEN;
  611. addr = (CORE_ADDR) (size_t) current_event.u.DebugString.lpDebugStringData;
  612. if (current_event.u.DebugString.fUnicode)
  613. {
  614. /* The event tells us how many bytes, not chars, even
  615. in Unicode. */
  616. WCHAR buffer[(READ_BUFFER_LEN + 1) / sizeof (WCHAR)] = { 0 };
  617. if (read_inferior_memory (addr, (unsigned char *) buffer, nbytes) != 0)
  618. return 0;
  619. wcstombs (s, buffer, (nbytes + 1) / sizeof (WCHAR));
  620. }
  621. else
  622. {
  623. if (read_inferior_memory (addr, (unsigned char *) s, nbytes) != 0)
  624. return 0;
  625. }
  626. if (!startswith (s, "cYg"))
  627. {
  628. if (!server_waiting)
  629. {
  630. OUTMSG2(("%s", s));
  631. return 0;
  632. }
  633. monitor_output (s);
  634. }
  635. #undef READ_BUFFER_LEN
  636. return 0;
  637. }
  638. static void
  639. win32_clear_inferiors (void)
  640. {
  641. if (open_process_used)
  642. {
  643. CloseHandle (windows_process.handle);
  644. open_process_used = false;
  645. }
  646. for_each_thread (delete_thread_info);
  647. windows_process.siginfo_er.ExceptionCode = 0;
  648. clear_inferiors ();
  649. }
  650. /* Implementation of target_ops::kill. */
  651. int
  652. win32_process_target::kill (process_info *process)
  653. {
  654. TerminateProcess (windows_process.handle, 0);
  655. for (;;)
  656. {
  657. if (!child_continue (DBG_CONTINUE, -1))
  658. break;
  659. if (!wait_for_debug_event (&windows_process.current_event, INFINITE))
  660. break;
  661. if (windows_process.current_event.dwDebugEventCode
  662. == EXIT_PROCESS_DEBUG_EVENT)
  663. break;
  664. else if (windows_process.current_event.dwDebugEventCode
  665. == OUTPUT_DEBUG_STRING_EVENT)
  666. windows_process.handle_output_debug_string (nullptr);
  667. }
  668. win32_clear_inferiors ();
  669. remove_process (process);
  670. return 0;
  671. }
  672. /* Implementation of target_ops::detach. */
  673. int
  674. win32_process_target::detach (process_info *process)
  675. {
  676. struct thread_resume resume;
  677. resume.thread = minus_one_ptid;
  678. resume.kind = resume_continue;
  679. resume.sig = 0;
  680. this->resume (&resume, 1);
  681. if (!DebugActiveProcessStop (windows_process.id))
  682. return -1;
  683. DebugSetProcessKillOnExit (FALSE);
  684. remove_process (process);
  685. win32_clear_inferiors ();
  686. return 0;
  687. }
  688. void
  689. win32_process_target::mourn (struct process_info *process)
  690. {
  691. remove_process (process);
  692. }
  693. /* Implementation of target_ops::join. */
  694. void
  695. win32_process_target::join (int pid)
  696. {
  697. HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
  698. if (h != NULL)
  699. {
  700. WaitForSingleObject (h, INFINITE);
  701. CloseHandle (h);
  702. }
  703. }
  704. /* Return true iff the thread with thread ID TID is alive. */
  705. bool
  706. win32_process_target::thread_alive (ptid_t ptid)
  707. {
  708. /* Our thread list is reliable; don't bother to poll target
  709. threads. */
  710. return find_thread_ptid (ptid) != NULL;
  711. }
  712. /* Resume the inferior process. RESUME_INFO describes how we want
  713. to resume. */
  714. void
  715. win32_process_target::resume (thread_resume *resume_info, size_t n)
  716. {
  717. DWORD tid;
  718. enum gdb_signal sig;
  719. int step;
  720. windows_thread_info *th;
  721. DWORD continue_status = DBG_CONTINUE;
  722. ptid_t ptid;
  723. /* This handles the very limited set of resume packets that GDB can
  724. currently produce. */
  725. if (n == 1 && resume_info[0].thread == minus_one_ptid)
  726. tid = -1;
  727. else if (n > 1)
  728. tid = -1;
  729. else
  730. /* Yes, we're ignoring resume_info[0].thread. It'd be tricky to make
  731. the Windows resume code do the right thing for thread switching. */
  732. tid = windows_process.current_event.dwThreadId;
  733. if (resume_info[0].thread != minus_one_ptid)
  734. {
  735. sig = gdb_signal_from_host (resume_info[0].sig);
  736. step = resume_info[0].kind == resume_step;
  737. }
  738. else
  739. {
  740. sig = GDB_SIGNAL_0;
  741. step = 0;
  742. }
  743. if (sig != GDB_SIGNAL_0)
  744. {
  745. if (windows_process.current_event.dwDebugEventCode
  746. != EXCEPTION_DEBUG_EVENT)
  747. {
  748. OUTMSG (("Cannot continue with signal %s here.\n",
  749. gdb_signal_to_string (sig)));
  750. }
  751. else if (sig == windows_process.last_sig)
  752. continue_status = DBG_EXCEPTION_NOT_HANDLED;
  753. else
  754. OUTMSG (("Can only continue with received signal %s.\n",
  755. gdb_signal_to_string (windows_process.last_sig)));
  756. }
  757. windows_process.last_sig = GDB_SIGNAL_0;
  758. /* Get context for the currently selected thread. */
  759. ptid = debug_event_ptid (&windows_process.current_event);
  760. th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
  761. if (th)
  762. {
  763. win32_prepare_to_resume (th);
  764. DWORD *context_flags;
  765. #ifdef __x86_64__
  766. if (wow64_process)
  767. context_flags = &th->wow64_context.ContextFlags;
  768. else
  769. #endif
  770. context_flags = &th->context.ContextFlags;
  771. if (*context_flags)
  772. {
  773. /* Move register values from the inferior into the thread
  774. context structure. */
  775. regcache_invalidate ();
  776. if (step)
  777. {
  778. if (the_low_target.single_step != NULL)
  779. (*the_low_target.single_step) (th);
  780. else
  781. error ("Single stepping is not supported "
  782. "in this configuration.\n");
  783. }
  784. win32_set_thread_context (th);
  785. *context_flags = 0;
  786. }
  787. }
  788. /* Allow continuing with the same signal that interrupted us.
  789. Otherwise complain. */
  790. child_continue (continue_status, tid);
  791. }
  792. /* See nat/windows-nat.h. */
  793. void
  794. windows_nat::windows_process_info::handle_load_dll (const char *name,
  795. LPVOID base)
  796. {
  797. CORE_ADDR load_addr = (CORE_ADDR) (uintptr_t) base;
  798. char buf[MAX_PATH + 1];
  799. char buf2[MAX_PATH + 1];
  800. WIN32_FIND_DATAA w32_fd;
  801. HANDLE h = FindFirstFileA (name, &w32_fd);
  802. /* The symbols in a dll are offset by 0x1000, which is the
  803. offset from 0 of the first byte in an image - because
  804. of the file header and the section alignment. */
  805. load_addr += 0x1000;
  806. if (h == INVALID_HANDLE_VALUE)
  807. strcpy (buf, name);
  808. else
  809. {
  810. FindClose (h);
  811. strcpy (buf, name);
  812. {
  813. char cwd[MAX_PATH + 1];
  814. char *p;
  815. if (GetCurrentDirectoryA (MAX_PATH + 1, cwd))
  816. {
  817. p = strrchr (buf, '\\');
  818. if (p)
  819. p[1] = '\0';
  820. SetCurrentDirectoryA (buf);
  821. GetFullPathNameA (w32_fd.cFileName, MAX_PATH, buf, &p);
  822. SetCurrentDirectoryA (cwd);
  823. }
  824. }
  825. }
  826. if (strcasecmp (buf, "ntdll.dll") == 0)
  827. {
  828. GetSystemDirectoryA (buf, sizeof (buf));
  829. strcat (buf, "\\ntdll.dll");
  830. }
  831. #ifdef __CYGWIN__
  832. cygwin_conv_path (CCP_WIN_A_TO_POSIX, buf, buf2, sizeof (buf2));
  833. #else
  834. strcpy (buf2, buf);
  835. #endif
  836. loaded_dll (buf2, load_addr);
  837. }
  838. /* See nat/windows-nat.h. */
  839. void
  840. windows_nat::windows_process_info::handle_unload_dll ()
  841. {
  842. CORE_ADDR load_addr =
  843. (CORE_ADDR) (uintptr_t) current_event.u.UnloadDll.lpBaseOfDll;
  844. /* The symbols in a dll are offset by 0x1000, which is the
  845. offset from 0 of the first byte in an image - because
  846. of the file header and the section alignment. */
  847. load_addr += 0x1000;
  848. unloaded_dll (NULL, load_addr);
  849. }
  850. static void
  851. suspend_one_thread (thread_info *thread)
  852. {
  853. windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
  854. th->suspend ();
  855. }
  856. static void
  857. fake_breakpoint_event (void)
  858. {
  859. OUTMSG2(("fake_breakpoint_event\n"));
  860. faked_breakpoint = 1;
  861. memset (&windows_process.current_event, 0,
  862. sizeof (windows_process.current_event));
  863. windows_process.current_event.dwThreadId = windows_process.main_thread_id;
  864. windows_process.current_event.dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
  865. windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
  866. = EXCEPTION_BREAKPOINT;
  867. for_each_thread (suspend_one_thread);
  868. }
  869. /* See nat/windows-nat.h. */
  870. bool
  871. windows_nat::windows_process_info::handle_ms_vc_exception
  872. (const EXCEPTION_RECORD *rec)
  873. {
  874. return false;
  875. }
  876. /* See nat/windows-nat.h. */
  877. bool
  878. windows_nat::windows_process_info::handle_access_violation
  879. (const EXCEPTION_RECORD *rec)
  880. {
  881. return false;
  882. }
  883. /* A helper function that will, if needed, set
  884. 'stopped_at_software_breakpoint' on the thread and adjust the
  885. PC. */
  886. static void
  887. maybe_adjust_pc ()
  888. {
  889. struct regcache *regcache = get_thread_regcache (current_thread, 1);
  890. child_fetch_inferior_registers (regcache, -1);
  891. windows_thread_info *th
  892. = windows_process.thread_rec (current_thread_ptid (),
  893. DONT_INVALIDATE_CONTEXT);
  894. th->stopped_at_software_breakpoint = false;
  895. if (windows_process.current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
  896. && ((windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
  897. == EXCEPTION_BREAKPOINT)
  898. || (windows_process.current_event.u.Exception.ExceptionRecord.ExceptionCode
  899. == STATUS_WX86_BREAKPOINT))
  900. && child_initialization_done)
  901. {
  902. th->stopped_at_software_breakpoint = true;
  903. CORE_ADDR pc = regcache_read_pc (regcache);
  904. CORE_ADDR sw_breakpoint_pc = pc - the_low_target.decr_pc_after_break;
  905. regcache_write_pc (regcache, sw_breakpoint_pc);
  906. }
  907. }
  908. /* Get the next event from the child. */
  909. static int
  910. get_child_debug_event (DWORD *continue_status,
  911. struct target_waitstatus *ourstatus)
  912. {
  913. ptid_t ptid;
  914. windows_process.last_sig = GDB_SIGNAL_0;
  915. ourstatus->set_spurious ();
  916. *continue_status = DBG_CONTINUE;
  917. /* Check if GDB sent us an interrupt request. */
  918. check_remote_input_interrupt_request ();
  919. DEBUG_EVENT *current_event = &windows_process.current_event;
  920. if (soft_interrupt_requested)
  921. {
  922. soft_interrupt_requested = 0;
  923. fake_breakpoint_event ();
  924. goto gotevent;
  925. }
  926. attaching = 0;
  927. {
  928. gdb::optional<pending_stop> stop
  929. = windows_process.fetch_pending_stop (debug_threads);
  930. if (stop.has_value ())
  931. {
  932. *ourstatus = stop->status;
  933. windows_process.current_event = stop->event;
  934. ptid = debug_event_ptid (&windows_process.current_event);
  935. switch_to_thread (find_thread_ptid (ptid));
  936. return 1;
  937. }
  938. /* Keep the wait time low enough for comfortable remote
  939. interruption, but high enough so gdbserver doesn't become a
  940. bottleneck. */
  941. if (!wait_for_debug_event (&windows_process.current_event, 250))
  942. {
  943. DWORD e = GetLastError();
  944. if (e == ERROR_PIPE_NOT_CONNECTED)
  945. {
  946. /* This will happen if the loader fails to succesfully
  947. load the application, e.g., if the main executable
  948. tries to pull in a non-existing export from a
  949. DLL. */
  950. ourstatus->set_exited (1);
  951. return 1;
  952. }
  953. return 0;
  954. }
  955. }
  956. gotevent:
  957. switch (current_event->dwDebugEventCode)
  958. {
  959. case CREATE_THREAD_DEBUG_EVENT:
  960. OUTMSG2 (("gdbserver: kernel event CREATE_THREAD_DEBUG_EVENT "
  961. "for pid=%u tid=%x)\n",
  962. (unsigned) current_event->dwProcessId,
  963. (unsigned) current_event->dwThreadId));
  964. /* Record the existence of this thread. */
  965. child_add_thread (current_event->dwProcessId,
  966. current_event->dwThreadId,
  967. current_event->u.CreateThread.hThread,
  968. current_event->u.CreateThread.lpThreadLocalBase);
  969. break;
  970. case EXIT_THREAD_DEBUG_EVENT:
  971. OUTMSG2 (("gdbserver: kernel event EXIT_THREAD_DEBUG_EVENT "
  972. "for pid=%u tid=%x\n",
  973. (unsigned) current_event->dwProcessId,
  974. (unsigned) current_event->dwThreadId));
  975. child_delete_thread (current_event->dwProcessId,
  976. current_event->dwThreadId);
  977. switch_to_thread (get_first_thread ());
  978. return 1;
  979. case CREATE_PROCESS_DEBUG_EVENT:
  980. OUTMSG2 (("gdbserver: kernel event CREATE_PROCESS_DEBUG_EVENT "
  981. "for pid=%u tid=%x\n",
  982. (unsigned) current_event->dwProcessId,
  983. (unsigned) current_event->dwThreadId));
  984. CloseHandle (current_event->u.CreateProcessInfo.hFile);
  985. if (open_process_used)
  986. {
  987. CloseHandle (windows_process.handle);
  988. open_process_used = false;
  989. }
  990. windows_process.handle = current_event->u.CreateProcessInfo.hProcess;
  991. windows_process.main_thread_id = current_event->dwThreadId;
  992. /* Add the main thread. */
  993. child_add_thread (current_event->dwProcessId,
  994. windows_process.main_thread_id,
  995. current_event->u.CreateProcessInfo.hThread,
  996. current_event->u.CreateProcessInfo.lpThreadLocalBase);
  997. break;
  998. case EXIT_PROCESS_DEBUG_EVENT:
  999. OUTMSG2 (("gdbserver: kernel event EXIT_PROCESS_DEBUG_EVENT "
  1000. "for pid=%u tid=%x\n",
  1001. (unsigned) current_event->dwProcessId,
  1002. (unsigned) current_event->dwThreadId));
  1003. {
  1004. DWORD exit_status = current_event->u.ExitProcess.dwExitCode;
  1005. /* If the exit status looks like a fatal exception, but we
  1006. don't recognize the exception's code, make the original
  1007. exit status value available, to avoid losing information. */
  1008. int exit_signal
  1009. = WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1;
  1010. if (exit_signal == -1)
  1011. ourstatus->set_exited (exit_status);
  1012. else
  1013. ourstatus->set_signalled (gdb_signal_from_host (exit_signal));
  1014. }
  1015. child_continue (DBG_CONTINUE, windows_process.desired_stop_thread_id);
  1016. break;
  1017. case LOAD_DLL_DEBUG_EVENT:
  1018. OUTMSG2 (("gdbserver: kernel event LOAD_DLL_DEBUG_EVENT "
  1019. "for pid=%u tid=%x\n",
  1020. (unsigned) current_event->dwProcessId,
  1021. (unsigned) current_event->dwThreadId));
  1022. CloseHandle (current_event->u.LoadDll.hFile);
  1023. if (! child_initialization_done)
  1024. break;
  1025. windows_process.dll_loaded_event ();
  1026. ourstatus->set_loaded ();
  1027. break;
  1028. case UNLOAD_DLL_DEBUG_EVENT:
  1029. OUTMSG2 (("gdbserver: kernel event UNLOAD_DLL_DEBUG_EVENT "
  1030. "for pid=%u tid=%x\n",
  1031. (unsigned) current_event->dwProcessId,
  1032. (unsigned) current_event->dwThreadId));
  1033. if (! child_initialization_done)
  1034. break;
  1035. windows_process.handle_unload_dll ();
  1036. ourstatus->set_loaded ();
  1037. break;
  1038. case EXCEPTION_DEBUG_EVENT:
  1039. OUTMSG2 (("gdbserver: kernel event EXCEPTION_DEBUG_EVENT "
  1040. "for pid=%u tid=%x\n",
  1041. (unsigned) current_event->dwProcessId,
  1042. (unsigned) current_event->dwThreadId));
  1043. if (windows_process.handle_exception (ourstatus, debug_threads)
  1044. == HANDLE_EXCEPTION_UNHANDLED)
  1045. *continue_status = DBG_EXCEPTION_NOT_HANDLED;
  1046. break;
  1047. case OUTPUT_DEBUG_STRING_EVENT:
  1048. /* A message from the kernel (or Cygwin). */
  1049. OUTMSG2 (("gdbserver: kernel event OUTPUT_DEBUG_STRING_EVENT "
  1050. "for pid=%u tid=%x\n",
  1051. (unsigned) current_event->dwProcessId,
  1052. (unsigned) current_event->dwThreadId));
  1053. windows_process.handle_output_debug_string (nullptr);
  1054. break;
  1055. default:
  1056. OUTMSG2 (("gdbserver: kernel event unknown "
  1057. "for pid=%u tid=%x code=%x\n",
  1058. (unsigned) current_event->dwProcessId,
  1059. (unsigned) current_event->dwThreadId,
  1060. (unsigned) current_event->dwDebugEventCode));
  1061. break;
  1062. }
  1063. ptid = debug_event_ptid (&windows_process.current_event);
  1064. if (windows_process.desired_stop_thread_id != -1
  1065. && windows_process.desired_stop_thread_id != ptid.lwp ())
  1066. {
  1067. /* Pending stop. See the comment by the definition of
  1068. "pending_stops" for details on why this is needed. */
  1069. OUTMSG2 (("get_windows_debug_event - "
  1070. "unexpected stop in 0x%lx (expecting 0x%x)\n",
  1071. ptid.lwp (), windows_process.desired_stop_thread_id));
  1072. maybe_adjust_pc ();
  1073. windows_process.pending_stops.push_back
  1074. ({(DWORD) ptid.lwp (), *ourstatus, *current_event});
  1075. ourstatus->set_spurious ();
  1076. }
  1077. else
  1078. switch_to_thread (find_thread_ptid (ptid));
  1079. return 1;
  1080. }
  1081. /* Wait for the inferior process to change state.
  1082. STATUS will be filled in with a response code to send to GDB.
  1083. Returns the signal which caused the process to stop. */
  1084. ptid_t
  1085. win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
  1086. target_wait_flags options)
  1087. {
  1088. if (cached_status.kind () != TARGET_WAITKIND_IGNORE)
  1089. {
  1090. /* The core always does a wait after creating the inferior, and
  1091. do_initial_child_stuff already ran the inferior to the
  1092. initial breakpoint (or an exit, if creating the process
  1093. fails). Report it now. */
  1094. *ourstatus = cached_status;
  1095. cached_status.set_ignore ();
  1096. return debug_event_ptid (&windows_process.current_event);
  1097. }
  1098. while (1)
  1099. {
  1100. DWORD continue_status;
  1101. if (!get_child_debug_event (&continue_status, ourstatus))
  1102. continue;
  1103. switch (ourstatus->kind ())
  1104. {
  1105. case TARGET_WAITKIND_EXITED:
  1106. OUTMSG2 (("Child exited with retcode = %x\n",
  1107. ourstatus->exit_status ()));
  1108. win32_clear_inferiors ();
  1109. return ptid_t (windows_process.current_event.dwProcessId);
  1110. case TARGET_WAITKIND_STOPPED:
  1111. case TARGET_WAITKIND_SIGNALLED:
  1112. case TARGET_WAITKIND_LOADED:
  1113. {
  1114. OUTMSG2 (("Child Stopped with signal = %d \n",
  1115. ourstatus->sig ()));
  1116. maybe_adjust_pc ();
  1117. return debug_event_ptid (&windows_process.current_event);
  1118. }
  1119. default:
  1120. OUTMSG (("Ignoring unknown internal event, %d\n",
  1121. ourstatus->kind ()));
  1122. /* fall-through */
  1123. case TARGET_WAITKIND_SPURIOUS:
  1124. /* do nothing, just continue */
  1125. child_continue (continue_status,
  1126. windows_process.desired_stop_thread_id);
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. /* Fetch registers from the inferior process.
  1132. If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
  1133. void
  1134. win32_process_target::fetch_registers (regcache *regcache, int regno)
  1135. {
  1136. child_fetch_inferior_registers (regcache, regno);
  1137. }
  1138. /* Store registers to the inferior process.
  1139. If REGNO is -1, store all registers; otherwise, store at least REGNO. */
  1140. void
  1141. win32_process_target::store_registers (regcache *regcache, int regno)
  1142. {
  1143. child_store_inferior_registers (regcache, regno);
  1144. }
  1145. /* Read memory from the inferior process. This should generally be
  1146. called through read_inferior_memory, which handles breakpoint shadowing.
  1147. Read LEN bytes at MEMADDR into a buffer at MYADDR. */
  1148. int
  1149. win32_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
  1150. int len)
  1151. {
  1152. return child_xfer_memory (memaddr, (char *) myaddr, len, 0, 0) != len;
  1153. }
  1154. /* Write memory to the inferior process. This should generally be
  1155. called through write_inferior_memory, which handles breakpoint shadowing.
  1156. Write LEN bytes from the buffer at MYADDR to MEMADDR.
  1157. Returns 0 on success and errno on failure. */
  1158. int
  1159. win32_process_target::write_memory (CORE_ADDR memaddr,
  1160. const unsigned char *myaddr, int len)
  1161. {
  1162. return child_xfer_memory (memaddr, (char *) myaddr, len, 1, 0) != len;
  1163. }
  1164. /* Send an interrupt request to the inferior process. */
  1165. void
  1166. win32_process_target::request_interrupt ()
  1167. {
  1168. if (GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, windows_process.id))
  1169. return;
  1170. /* GenerateConsoleCtrlEvent can fail if process id being debugged is
  1171. not a process group id.
  1172. Fallback to XP/Vista 'DebugBreakProcess', which generates a
  1173. breakpoint exception in the interior process. */
  1174. if (DebugBreakProcess (windows_process.handle))
  1175. return;
  1176. /* Last resort, suspend all threads manually. */
  1177. soft_interrupt_requested = 1;
  1178. }
  1179. bool
  1180. win32_process_target::supports_hardware_single_step ()
  1181. {
  1182. return true;
  1183. }
  1184. bool
  1185. win32_process_target::supports_qxfer_siginfo ()
  1186. {
  1187. return true;
  1188. }
  1189. /* Write Windows signal info. */
  1190. int
  1191. win32_process_target::qxfer_siginfo (const char *annex,
  1192. unsigned char *readbuf,
  1193. unsigned const char *writebuf,
  1194. CORE_ADDR offset, int len)
  1195. {
  1196. if (windows_process.siginfo_er.ExceptionCode == 0)
  1197. return -1;
  1198. if (readbuf == nullptr)
  1199. return -1;
  1200. char *buf = (char *) &windows_process.siginfo_er;
  1201. size_t bufsize = sizeof (windows_process.siginfo_er);
  1202. #ifdef __x86_64__
  1203. EXCEPTION_RECORD32 er32;
  1204. if (wow64_process)
  1205. {
  1206. buf = (char *) &er32;
  1207. bufsize = sizeof (er32);
  1208. er32.ExceptionCode = siginfo_er.ExceptionCode;
  1209. er32.ExceptionFlags = siginfo_er.ExceptionFlags;
  1210. er32.ExceptionRecord = (uintptr_t) siginfo_er.ExceptionRecord;
  1211. er32.ExceptionAddress = (uintptr_t) siginfo_er.ExceptionAddress;
  1212. er32.NumberParameters = siginfo_er.NumberParameters;
  1213. int i;
  1214. for (i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
  1215. er32.ExceptionInformation[i] = siginfo_er.ExceptionInformation[i];
  1216. }
  1217. #endif
  1218. if (offset > bufsize)
  1219. return -1;
  1220. if (offset + len > bufsize)
  1221. len = bufsize - offset;
  1222. memcpy (readbuf, buf + offset, len);
  1223. return len;
  1224. }
  1225. bool
  1226. win32_process_target::supports_get_tib_address ()
  1227. {
  1228. return true;
  1229. }
  1230. /* Write Windows OS Thread Information Block address. */
  1231. int
  1232. win32_process_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
  1233. {
  1234. windows_thread_info *th;
  1235. th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
  1236. if (th == NULL)
  1237. return 0;
  1238. if (addr != NULL)
  1239. *addr = th->thread_local_base;
  1240. return 1;
  1241. }
  1242. /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
  1243. const gdb_byte *
  1244. win32_process_target::sw_breakpoint_from_kind (int kind, int *size)
  1245. {
  1246. *size = the_low_target.breakpoint_len;
  1247. return the_low_target.breakpoint;
  1248. }
  1249. bool
  1250. win32_process_target::stopped_by_sw_breakpoint ()
  1251. {
  1252. windows_thread_info *th
  1253. = windows_process.thread_rec (current_thread_ptid (),
  1254. DONT_INVALIDATE_CONTEXT);
  1255. return th == nullptr ? false : th->stopped_at_software_breakpoint;
  1256. }
  1257. bool
  1258. win32_process_target::supports_stopped_by_sw_breakpoint ()
  1259. {
  1260. return true;
  1261. }
  1262. CORE_ADDR
  1263. win32_process_target::read_pc (struct regcache *regcache)
  1264. {
  1265. return (*the_low_target.get_pc) (regcache);
  1266. }
  1267. void
  1268. win32_process_target::write_pc (struct regcache *regcache, CORE_ADDR pc)
  1269. {
  1270. return (*the_low_target.set_pc) (regcache, pc);
  1271. }
  1272. /* The win32 target ops object. */
  1273. static win32_process_target the_win32_target;
  1274. /* Initialize the Win32 backend. */
  1275. void
  1276. initialize_low (void)
  1277. {
  1278. set_target_ops (&the_win32_target);
  1279. the_low_target.arch_setup ();
  1280. initialize_loadable ();
  1281. }