debugger.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Server-side debugger functions
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include "ntstatus.h"
  28. #define WIN32_NO_STATUS
  29. #include "windef.h"
  30. #include "winternl.h"
  31. #include "handle.h"
  32. #include "file.h"
  33. #include "process.h"
  34. #include "thread.h"
  35. #include "request.h"
  36. enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_DELAYED, EVENT_CONTINUED };
  37. /* debug event */
  38. struct debug_event
  39. {
  40. struct object obj; /* object header */
  41. struct list entry; /* entry in event queue */
  42. struct thread *sender; /* thread which sent this event */
  43. struct thread *debugger; /* debugger thread receiving the event */
  44. enum debug_event_state state; /* event state */
  45. int status; /* continuation status */
  46. debug_event_t data; /* event data */
  47. context_t context; /* register context */
  48. };
  49. /* debug context */
  50. struct debug_ctx
  51. {
  52. struct object obj; /* object header */
  53. struct list event_queue; /* pending events queue */
  54. int kill_on_exit;/* kill debuggees on debugger exit ? */
  55. };
  56. static void debug_event_dump( struct object *obj, int verbose );
  57. static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry );
  58. static void debug_event_destroy( struct object *obj );
  59. static const struct object_ops debug_event_ops =
  60. {
  61. sizeof(struct debug_event), /* size */
  62. debug_event_dump, /* dump */
  63. no_get_type, /* get_type */
  64. add_queue, /* add_queue */
  65. remove_queue, /* remove_queue */
  66. debug_event_signaled, /* signaled */
  67. NULL, /* get_esync_fd */
  68. no_satisfied, /* satisfied */
  69. no_signal, /* signal */
  70. no_get_fd, /* get_fd */
  71. no_map_access, /* map_access */
  72. default_get_sd, /* get_sd */
  73. default_set_sd, /* set_sd */
  74. no_lookup_name, /* lookup_name */
  75. no_link_name, /* link_name */
  76. NULL, /* unlink_name */
  77. no_open_file, /* open_file */
  78. no_kernel_obj_list, /* get_kernel_obj_list */
  79. no_alloc_handle, /* alloc_handle */
  80. no_close_handle, /* close_handle */
  81. debug_event_destroy /* destroy */
  82. };
  83. static void debug_ctx_dump( struct object *obj, int verbose );
  84. static int debug_ctx_signaled( struct object *obj, struct wait_queue_entry *entry );
  85. static void debug_ctx_destroy( struct object *obj );
  86. static const struct object_ops debug_ctx_ops =
  87. {
  88. sizeof(struct debug_ctx), /* size */
  89. debug_ctx_dump, /* dump */
  90. no_get_type, /* get_type */
  91. add_queue, /* add_queue */
  92. remove_queue, /* remove_queue */
  93. debug_ctx_signaled, /* signaled */
  94. NULL, /* get_esync_fd */
  95. no_satisfied, /* satisfied */
  96. no_signal, /* signal */
  97. no_get_fd, /* get_fd */
  98. no_map_access, /* map_access */
  99. default_get_sd, /* get_sd */
  100. default_set_sd, /* set_sd */
  101. no_lookup_name, /* lookup_name */
  102. no_link_name, /* link_name */
  103. NULL, /* unlink_name */
  104. no_open_file, /* open_file */
  105. no_kernel_obj_list, /* get_kernel_obj_list */
  106. no_alloc_handle, /* alloc_handle */
  107. no_close_handle, /* close_handle */
  108. debug_ctx_destroy /* destroy */
  109. };
  110. /* routines to build an event according to its type */
  111. static int fill_exception_event( struct debug_event *event, const void *arg )
  112. {
  113. const debug_event_t *data = arg;
  114. event->data.exception = data->exception;
  115. event->data.exception.nb_params = min( event->data.exception.nb_params, EXCEPTION_MAXIMUM_PARAMETERS );
  116. return 1;
  117. }
  118. static int fill_create_thread_event( struct debug_event *event, const void *arg )
  119. {
  120. struct process *debugger = event->debugger->process;
  121. struct thread *thread = event->sender;
  122. const client_ptr_t *entry = arg;
  123. obj_handle_t handle;
  124. /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
  125. if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, 0 ))) return 0;
  126. event->data.create_thread.handle = handle;
  127. event->data.create_thread.teb = thread->teb;
  128. if (entry) event->data.create_thread.start = *entry;
  129. return 1;
  130. }
  131. static int fill_create_process_event( struct debug_event *event, const void *arg )
  132. {
  133. struct process *debugger = event->debugger->process;
  134. struct thread *thread = event->sender;
  135. struct process *process = thread->process;
  136. struct process_dll *exe_module = get_process_exe_module( process );
  137. const client_ptr_t *entry = arg;
  138. struct file *file;
  139. obj_handle_t handle;
  140. /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
  141. if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, 0 ))) return 0;
  142. event->data.create_process.process = handle;
  143. /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
  144. if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, 0 )))
  145. {
  146. close_handle( debugger, event->data.create_process.process );
  147. return 0;
  148. }
  149. event->data.create_process.thread = handle;
  150. event->data.create_process.file = 0;
  151. event->data.create_process.teb = thread->teb;
  152. event->data.create_process.base = exe_module->base;
  153. event->data.create_process.start = *entry;
  154. event->data.create_process.dbg_offset = exe_module->dbg_offset;
  155. event->data.create_process.dbg_size = exe_module->dbg_size;
  156. event->data.create_process.name = exe_module->name;
  157. event->data.create_process.unicode = 1;
  158. /* the doc says write access too, but this doesn't seem a good idea */
  159. if ((file = get_mapping_file( process, exe_module->base, GENERIC_READ,
  160. FILE_SHARE_READ | FILE_SHARE_WRITE )))
  161. {
  162. event->data.create_process.file = alloc_handle( debugger, file, GENERIC_READ, 0 );
  163. release_object( file );
  164. }
  165. return 1;
  166. }
  167. static int fill_exit_thread_event( struct debug_event *event, const void *arg )
  168. {
  169. const struct thread *thread = arg;
  170. event->data.exit.exit_code = thread->exit_code;
  171. return 1;
  172. }
  173. static int fill_exit_process_event( struct debug_event *event, const void *arg )
  174. {
  175. const struct process *process = arg;
  176. event->data.exit.exit_code = process->exit_code;
  177. return 1;
  178. }
  179. static int fill_load_dll_event( struct debug_event *event, const void *arg )
  180. {
  181. struct process *process = event->sender->process;
  182. struct process *debugger = event->debugger->process;
  183. const struct process_dll *dll = arg;
  184. struct file *file;
  185. event->data.load_dll.handle = 0;
  186. event->data.load_dll.base = dll->base;
  187. event->data.load_dll.dbg_offset = dll->dbg_offset;
  188. event->data.load_dll.dbg_size = dll->dbg_size;
  189. event->data.load_dll.name = dll->name;
  190. event->data.load_dll.unicode = 1;
  191. if ((file = get_mapping_file( process, dll->base, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE )))
  192. {
  193. event->data.load_dll.handle = alloc_handle( debugger, file, GENERIC_READ, 0 );
  194. release_object( file );
  195. }
  196. return 1;
  197. }
  198. static int fill_unload_dll_event( struct debug_event *event, const void *arg )
  199. {
  200. const mod_handle_t *base = arg;
  201. event->data.unload_dll.base = *base;
  202. return 1;
  203. }
  204. typedef int (*fill_event_func)( struct debug_event *event, const void *arg );
  205. #define NB_DEBUG_EVENTS UNLOAD_DLL_DEBUG_EVENT
  206. static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
  207. {
  208. fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
  209. fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
  210. fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
  211. fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
  212. fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
  213. fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
  214. fill_unload_dll_event /* UNLOAD_DLL_DEBUG_EVENT */
  215. };
  216. /* unlink the first event from the queue */
  217. static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
  218. {
  219. list_remove( &event->entry );
  220. if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
  221. release_object( event );
  222. }
  223. /* link an event at the end of the queue */
  224. static void link_event( struct debug_event *event )
  225. {
  226. struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
  227. assert( debug_ctx );
  228. grab_object( event );
  229. list_add_tail( &debug_ctx->event_queue, &event->entry );
  230. if (!event->sender->process->debug_event)
  231. {
  232. /* grab reference since debugger could be killed while trying to wake up */
  233. grab_object( debug_ctx );
  234. wake_up( &debug_ctx->obj, 0 );
  235. release_object( debug_ctx );
  236. }
  237. }
  238. /* resume a delayed debug event already in the queue */
  239. static void resume_event( struct debug_event *event )
  240. {
  241. struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
  242. assert( debug_ctx );
  243. event->state = EVENT_QUEUED;
  244. if (!event->sender->process->debug_event)
  245. {
  246. grab_object( debug_ctx );
  247. wake_up( &debug_ctx->obj, 0 );
  248. release_object( debug_ctx );
  249. }
  250. }
  251. /* delay a debug event already in the queue to be replayed when thread wakes up */
  252. static void delay_event( struct debug_event *event )
  253. {
  254. struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
  255. assert( debug_ctx );
  256. event->state = EVENT_DELAYED;
  257. if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
  258. }
  259. /* find the next event that we can send to the debugger */
  260. static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
  261. {
  262. struct debug_event *event;
  263. LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
  264. {
  265. if (event->state == EVENT_SENT) continue; /* already sent */
  266. if (event->state == EVENT_DELAYED) continue; /* delayed until thread resumes */
  267. if (event->sender->process->debug_event) continue; /* process busy with another one */
  268. return event;
  269. }
  270. return NULL;
  271. }
  272. static void debug_event_dump( struct object *obj, int verbose )
  273. {
  274. struct debug_event *debug_event = (struct debug_event *)obj;
  275. assert( obj->ops == &debug_event_ops );
  276. fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
  277. debug_event->sender, debug_event->data.code, debug_event->state );
  278. }
  279. static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry )
  280. {
  281. struct debug_event *debug_event = (struct debug_event *)obj;
  282. assert( obj->ops == &debug_event_ops );
  283. return debug_event->state == EVENT_CONTINUED;
  284. }
  285. static void debug_event_destroy( struct object *obj )
  286. {
  287. struct debug_event *event = (struct debug_event *)obj;
  288. assert( obj->ops == &debug_event_ops );
  289. /* If the event has been sent already, the handles are now under the */
  290. /* responsibility of the debugger process, so we don't touch them */
  291. if (event->state == EVENT_QUEUED)
  292. {
  293. struct process *debugger = event->debugger->process;
  294. switch(event->data.code)
  295. {
  296. case CREATE_THREAD_DEBUG_EVENT:
  297. close_handle( debugger, event->data.create_thread.handle );
  298. break;
  299. case CREATE_PROCESS_DEBUG_EVENT:
  300. if (event->data.create_process.file)
  301. close_handle( debugger, event->data.create_process.file );
  302. close_handle( debugger, event->data.create_process.thread );
  303. close_handle( debugger, event->data.create_process.process );
  304. break;
  305. case LOAD_DLL_DEBUG_EVENT:
  306. if (event->data.load_dll.handle)
  307. close_handle( debugger, event->data.load_dll.handle );
  308. break;
  309. }
  310. }
  311. if (event->sender->context == &event->context)
  312. {
  313. event->sender->context = NULL;
  314. stop_thread_if_suspended( event->sender );
  315. }
  316. release_object( event->sender );
  317. release_object( event->debugger );
  318. }
  319. static void debug_ctx_dump( struct object *obj, int verbose )
  320. {
  321. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  322. assert( obj->ops == &debug_ctx_ops );
  323. fprintf( stderr, "Debug context head=%p tail=%p\n",
  324. debug_ctx->event_queue.next, debug_ctx->event_queue.prev );
  325. }
  326. static int debug_ctx_signaled( struct object *obj, struct wait_queue_entry *entry )
  327. {
  328. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  329. assert( obj->ops == &debug_ctx_ops );
  330. return find_event_to_send( debug_ctx ) != NULL;
  331. }
  332. static void debug_ctx_destroy( struct object *obj )
  333. {
  334. struct list *ptr;
  335. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  336. assert( obj->ops == &debug_ctx_ops );
  337. /* free all pending events */
  338. while ((ptr = list_head( &debug_ctx->event_queue )))
  339. unlink_event( debug_ctx, LIST_ENTRY( ptr, struct debug_event, entry ));
  340. }
  341. /* continue a debug event */
  342. static int continue_debug_event( struct process *process, struct thread *thread, int status )
  343. {
  344. struct debug_ctx *debug_ctx = current->debug_ctx;
  345. if (debug_ctx && process->debugger == current && thread->process == process)
  346. {
  347. struct debug_event *event;
  348. if (status == DBG_REPLY_LATER)
  349. {
  350. /* if thread is suspended, delay all its events and resume process
  351. * if not, reset the event for immediate replay */
  352. LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
  353. {
  354. if (event->sender != thread) continue;
  355. if (thread->suspend)
  356. {
  357. delay_event( event );
  358. resume_process( process );
  359. }
  360. else if (event->state == EVENT_SENT)
  361. {
  362. assert( event->sender->process->debug_event == event );
  363. event->sender->process->debug_event = NULL;
  364. resume_event( event );
  365. return 1;
  366. }
  367. }
  368. return 1;
  369. }
  370. /* find the event in the queue */
  371. LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
  372. {
  373. if (event->state != EVENT_SENT) continue;
  374. if (event->sender == thread)
  375. {
  376. assert( event->sender->process->debug_event == event );
  377. event->status = status;
  378. event->state = EVENT_CONTINUED;
  379. wake_up( &event->obj, 0 );
  380. unlink_event( debug_ctx, event );
  381. resume_process( process );
  382. return 1;
  383. }
  384. }
  385. }
  386. /* not debugging this process, or no such event */
  387. set_error( STATUS_ACCESS_DENIED ); /* FIXME */
  388. return 0;
  389. }
  390. /* alloc a debug event for a debugger */
  391. static struct debug_event *alloc_debug_event( struct thread *thread, int code, const void *arg )
  392. {
  393. struct thread *debugger = thread->process->debugger;
  394. struct debug_event *event;
  395. assert( code > 0 && code <= NB_DEBUG_EVENTS );
  396. /* cannot queue a debug event for myself */
  397. assert( debugger->process != thread->process );
  398. /* build the event */
  399. if (!(event = alloc_object( &debug_event_ops ))) return NULL;
  400. event->state = EVENT_QUEUED;
  401. event->sender = (struct thread *)grab_object( thread );
  402. event->debugger = (struct thread *)grab_object( debugger );
  403. memset( &event->data, 0, sizeof(event->data) );
  404. if (!fill_debug_event[code-1]( event, arg ))
  405. {
  406. event->data.code = -1; /* make sure we don't attempt to close handles */
  407. release_object( event );
  408. return NULL;
  409. }
  410. event->data.code = code;
  411. return event;
  412. }
  413. /* generate a debug event from inside the server and queue it */
  414. void generate_debug_event( struct thread *thread, int code, const void *arg )
  415. {
  416. if (thread->process->debugger)
  417. {
  418. struct debug_event *event = alloc_debug_event( thread, code, arg );
  419. if (event)
  420. {
  421. link_event( event );
  422. suspend_process( thread->process );
  423. release_object( event );
  424. }
  425. clear_error(); /* ignore errors */
  426. }
  427. }
  428. void resume_delayed_debug_events( struct thread *thread )
  429. {
  430. struct thread *debugger = thread->process->debugger;
  431. struct debug_event *event;
  432. if (debugger)
  433. {
  434. assert( debugger->debug_ctx );
  435. LIST_FOR_EACH_ENTRY( event, &debugger->debug_ctx->event_queue, struct debug_event, entry )
  436. {
  437. if (event->sender != thread) continue;
  438. if (event->state != EVENT_DELAYED) continue;
  439. resume_event( event );
  440. suspend_process( thread->process );
  441. }
  442. }
  443. }
  444. /* attach a process to a debugger thread and suspend it */
  445. static int debugger_attach( struct process *process, struct thread *debugger )
  446. {
  447. if (process->debugger) goto error; /* already being debugged */
  448. if (debugger->process == process) goto error;
  449. if (!is_process_init_done( process )) goto error; /* still starting up */
  450. if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
  451. /* don't let a debugger debug its console... won't work */
  452. if (debugger->process->console)
  453. {
  454. struct thread *renderer = console_get_renderer(debugger->process->console);
  455. if (renderer && renderer->process == process)
  456. goto error;
  457. }
  458. suspend_process( process );
  459. if (!set_process_debugger( process, debugger ))
  460. {
  461. resume_process( process );
  462. return 0;
  463. }
  464. if (!set_process_debug_flag( process, 1 ))
  465. {
  466. process->debugger = NULL;
  467. resume_process( process );
  468. return 0;
  469. }
  470. process->debug_children = 0;
  471. return 1;
  472. error:
  473. set_error( STATUS_ACCESS_DENIED );
  474. return 0;
  475. }
  476. /* detach a process from a debugger thread (and resume it ?) */
  477. int debugger_detach( struct process *process, struct thread *debugger )
  478. {
  479. struct debug_event *event, *next;
  480. struct debug_ctx *debug_ctx;
  481. if (!process->debugger || process->debugger != debugger)
  482. goto error; /* not currently debugged, or debugged by another debugger */
  483. if (!debugger->debug_ctx ) goto error; /* should be a debugger */
  484. /* init should be done, otherwise wouldn't be attached */
  485. assert(is_process_init_done(process));
  486. suspend_process( process );
  487. /* send continue indication for all events */
  488. debug_ctx = debugger->debug_ctx;
  489. /* free all events from this process */
  490. LIST_FOR_EACH_ENTRY_SAFE( event, next, &debug_ctx->event_queue, struct debug_event, entry )
  491. {
  492. if (event->sender->process != process) continue;
  493. assert( event->state != EVENT_CONTINUED );
  494. event->status = DBG_CONTINUE;
  495. event->state = EVENT_CONTINUED;
  496. wake_up( &event->obj, 0 );
  497. unlink_event( debug_ctx, event );
  498. /* from queued debug event */
  499. resume_process( process );
  500. }
  501. /* remove relationships between process and its debugger */
  502. process->debugger = NULL;
  503. if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
  504. /* from this function */
  505. resume_process( process );
  506. return 0;
  507. error:
  508. set_error( STATUS_ACCESS_DENIED );
  509. return 0;
  510. }
  511. /* generate all startup events of a given process */
  512. void generate_startup_debug_events( struct process *process, client_ptr_t entry )
  513. {
  514. struct list *ptr;
  515. struct thread *thread, *first_thread = get_process_first_thread( process );
  516. generate_debug_event( first_thread, CREATE_PROCESS_DEBUG_EVENT, &entry );
  517. ptr = list_head( &process->dlls ); /* skip main module reported in create process event */
  518. /* generate ntdll.dll load event */
  519. if (ptr && (ptr = list_next( &process->dlls, ptr )))
  520. {
  521. struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
  522. generate_debug_event( first_thread, LOAD_DLL_DEBUG_EVENT, dll );
  523. }
  524. /* generate creation events */
  525. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  526. {
  527. if (thread != first_thread)
  528. generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
  529. }
  530. /* generate dll events (in loading order) */
  531. while (ptr && (ptr = list_next( &process->dlls, ptr )))
  532. {
  533. struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
  534. generate_debug_event( first_thread, LOAD_DLL_DEBUG_EVENT, dll );
  535. }
  536. }
  537. /* set the debugger of a given process */
  538. int set_process_debugger( struct process *process, struct thread *debugger )
  539. {
  540. struct debug_ctx *debug_ctx;
  541. assert( !process->debugger );
  542. if (!debugger->debug_ctx) /* need to allocate a context */
  543. {
  544. if (!(debug_ctx = alloc_object( &debug_ctx_ops ))) return 0;
  545. debug_ctx->kill_on_exit = 1;
  546. list_init( &debug_ctx->event_queue );
  547. debugger->debug_ctx = debug_ctx;
  548. }
  549. process->debugger = debugger;
  550. return 1;
  551. }
  552. /* a thread is exiting */
  553. void debug_exit_thread( struct thread *thread )
  554. {
  555. if (thread->debug_ctx) /* this thread is a debugger */
  556. {
  557. if (thread->debug_ctx->kill_on_exit)
  558. {
  559. /* kill all debugged processes */
  560. kill_debugged_processes( thread, STATUS_DEBUGGER_INACTIVE );
  561. }
  562. else
  563. {
  564. detach_debugged_processes( thread );
  565. }
  566. release_object( thread->debug_ctx );
  567. thread->debug_ctx = NULL;
  568. }
  569. }
  570. /* Wait for a debug event */
  571. DECL_HANDLER(wait_debug_event)
  572. {
  573. struct debug_ctx *debug_ctx = current->debug_ctx;
  574. struct debug_event *event;
  575. if (!debug_ctx) /* current thread is not a debugger */
  576. {
  577. set_error( STATUS_INVALID_HANDLE );
  578. return;
  579. }
  580. reply->wait = 0;
  581. if ((event = find_event_to_send( debug_ctx )))
  582. {
  583. data_size_t size = get_reply_max_size();
  584. event->state = EVENT_SENT;
  585. event->sender->process->debug_event = event;
  586. reply->pid = get_process_id( event->sender->process );
  587. reply->tid = get_thread_id( event->sender );
  588. if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
  589. set_reply_data( &event->data, size );
  590. }
  591. else /* no event ready */
  592. {
  593. reply->pid = 0;
  594. reply->tid = 0;
  595. if (req->get_handle)
  596. reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, 0 );
  597. }
  598. }
  599. /* Continue a debug event */
  600. DECL_HANDLER(continue_debug_event)
  601. {
  602. struct process *process;
  603. if (req->status != DBG_EXCEPTION_NOT_HANDLED &&
  604. req->status != DBG_CONTINUE &&
  605. req->status != DBG_REPLY_LATER)
  606. {
  607. set_error( STATUS_INVALID_PARAMETER );
  608. return;
  609. }
  610. if ((process = get_process_from_id( req->pid )))
  611. {
  612. struct thread *thread = get_thread_from_id( req->tid );
  613. if (thread)
  614. {
  615. continue_debug_event( process, thread, req->status );
  616. release_object( thread );
  617. }
  618. release_object( process );
  619. }
  620. }
  621. /* Start debugging an existing process */
  622. DECL_HANDLER(debug_process)
  623. {
  624. struct process *process = get_process_from_id( req->pid );
  625. if (!process) return;
  626. if (!req->attach)
  627. {
  628. debugger_detach( process, current );
  629. }
  630. else if (debugger_attach( process, current ))
  631. {
  632. generate_startup_debug_events( process, 0 );
  633. resume_process( process );
  634. }
  635. release_object( process );
  636. }
  637. /* queue an exception event */
  638. DECL_HANDLER(queue_exception_event)
  639. {
  640. reply->handle = 0;
  641. if (current->process->debugger)
  642. {
  643. debug_event_t data;
  644. struct debug_event *event;
  645. struct thread *thread = current;
  646. if ((req->len % sizeof(client_ptr_t)) != 0 ||
  647. req->len > get_req_data_size() ||
  648. req->len > EXCEPTION_MAXIMUM_PARAMETERS * sizeof(client_ptr_t))
  649. {
  650. set_error( STATUS_INVALID_PARAMETER );
  651. return;
  652. }
  653. memset( &data, 0, sizeof(data) );
  654. data.exception.first = req->first;
  655. data.exception.exc_code = req->code;
  656. data.exception.flags = req->flags;
  657. data.exception.record = req->record;
  658. data.exception.address = req->address;
  659. data.exception.nb_params = req->len / sizeof(client_ptr_t);
  660. memcpy( data.exception.params, get_req_data(), req->len );
  661. if ((event = alloc_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data )))
  662. {
  663. const context_t *context = (const context_t *)((const char *)get_req_data() + req->len);
  664. data_size_t size = get_req_data_size() - req->len;
  665. memset( &event->context, 0, sizeof(event->context) );
  666. memcpy( &event->context, context, min( sizeof(event->context), size ) );
  667. thread->context = &event->context;
  668. if ((reply->handle = alloc_handle( thread->process, event, SYNCHRONIZE, 0 )))
  669. {
  670. link_event( event );
  671. suspend_process( thread->process );
  672. }
  673. release_object( event );
  674. }
  675. }
  676. }
  677. /* retrieve the status of an exception event */
  678. DECL_HANDLER(get_exception_status)
  679. {
  680. struct debug_event *event;
  681. if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
  682. 0, &debug_event_ops )))
  683. {
  684. close_handle( current->process, req->handle );
  685. if (event->state == EVENT_CONTINUED)
  686. {
  687. if (current->context == &event->context)
  688. {
  689. data_size_t size = min( sizeof(context_t), get_reply_max_size() );
  690. set_reply_data( &event->context, size );
  691. current->context = NULL;
  692. stop_thread_if_suspended( current );
  693. }
  694. set_error( event->status );
  695. }
  696. else set_error( STATUS_PENDING );
  697. release_object( event );
  698. }
  699. }
  700. /* set debugger kill on exit flag */
  701. DECL_HANDLER(set_debugger_kill_on_exit)
  702. {
  703. if (!current->debug_ctx)
  704. {
  705. set_error( STATUS_ACCESS_DENIED );
  706. return;
  707. }
  708. current->debug_ctx->kill_on_exit = req->kill_on_exit;
  709. }