debugger.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "windef.h"
  28. #include "winbase.h"
  29. #include "handle.h"
  30. #include "process.h"
  31. #include "thread.h"
  32. #include "request.h"
  33. #include "console.h"
  34. enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
  35. /* debug event */
  36. struct debug_event
  37. {
  38. struct object obj; /* object header */
  39. struct debug_event *next; /* event queue */
  40. struct debug_event *prev;
  41. struct thread *sender; /* thread which sent this event */
  42. struct thread *debugger; /* debugger thread receiving the event */
  43. enum debug_event_state state; /* event state */
  44. int status; /* continuation status */
  45. debug_event_t data; /* event data */
  46. CONTEXT context; /* register context */
  47. };
  48. /* debug context */
  49. struct debug_ctx
  50. {
  51. struct object obj; /* object header */
  52. struct debug_event *event_head; /* head of pending events queue */
  53. struct debug_event *event_tail; /* tail of 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 thread *thread );
  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. add_queue, /* add_queue */
  64. remove_queue, /* remove_queue */
  65. debug_event_signaled, /* signaled */
  66. no_satisfied, /* satisfied */
  67. no_get_fd, /* get_fd */
  68. debug_event_destroy /* destroy */
  69. };
  70. static void debug_ctx_dump( struct object *obj, int verbose );
  71. static int debug_ctx_signaled( struct object *obj, struct thread *thread );
  72. static void debug_ctx_destroy( struct object *obj );
  73. static const struct object_ops debug_ctx_ops =
  74. {
  75. sizeof(struct debug_ctx), /* size */
  76. debug_ctx_dump, /* dump */
  77. add_queue, /* add_queue */
  78. remove_queue, /* remove_queue */
  79. debug_ctx_signaled, /* signaled */
  80. no_satisfied, /* satisfied */
  81. no_get_fd, /* get_fd */
  82. debug_ctx_destroy /* destroy */
  83. };
  84. /* routines to build an event according to its type */
  85. static int fill_exception_event( struct debug_event *event, void *arg )
  86. {
  87. memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
  88. return 1;
  89. }
  90. static int fill_create_thread_event( struct debug_event *event, void *arg )
  91. {
  92. struct process *debugger = event->debugger->process;
  93. struct thread *thread = event->sender;
  94. obj_handle_t handle;
  95. /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
  96. if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
  97. event->data.info.create_thread.handle = handle;
  98. event->data.info.create_thread.teb = thread->teb;
  99. event->data.info.create_thread.start = arg;
  100. return 1;
  101. }
  102. static int fill_create_process_event( struct debug_event *event, void *arg )
  103. {
  104. struct process *debugger = event->debugger->process;
  105. struct thread *thread = event->sender;
  106. struct process *process = thread->process;
  107. obj_handle_t handle;
  108. /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
  109. if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
  110. event->data.info.create_process.process = handle;
  111. /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
  112. if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
  113. {
  114. close_handle( debugger, event->data.info.create_process.process, NULL );
  115. return 0;
  116. }
  117. event->data.info.create_process.thread = handle;
  118. handle = 0;
  119. if (process->exe.file &&
  120. /* the doc says write access too, but this doesn't seem a good idea */
  121. !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
  122. {
  123. close_handle( debugger, event->data.info.create_process.process, NULL );
  124. close_handle( debugger, event->data.info.create_process.thread, NULL );
  125. return 0;
  126. }
  127. event->data.info.create_process.file = handle;
  128. event->data.info.create_process.teb = thread->teb;
  129. event->data.info.create_process.base = process->exe.base;
  130. event->data.info.create_process.start = arg;
  131. event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
  132. event->data.info.create_process.dbg_size = process->exe.dbg_size;
  133. event->data.info.create_process.name = process->exe.name;
  134. event->data.info.create_process.unicode = 1;
  135. return 1;
  136. }
  137. static int fill_exit_thread_event( struct debug_event *event, void *arg )
  138. {
  139. struct thread *thread = arg;
  140. event->data.info.exit.exit_code = thread->exit_code;
  141. return 1;
  142. }
  143. static int fill_exit_process_event( struct debug_event *event, void *arg )
  144. {
  145. struct process *process = arg;
  146. event->data.info.exit.exit_code = process->exit_code;
  147. return 1;
  148. }
  149. static int fill_load_dll_event( struct debug_event *event, void *arg )
  150. {
  151. struct process *debugger = event->debugger->process;
  152. struct process_dll *dll = arg;
  153. obj_handle_t handle = 0;
  154. if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
  155. return 0;
  156. event->data.info.load_dll.handle = handle;
  157. event->data.info.load_dll.base = dll->base;
  158. event->data.info.load_dll.dbg_offset = dll->dbg_offset;
  159. event->data.info.load_dll.dbg_size = dll->dbg_size;
  160. event->data.info.load_dll.name = dll->name;
  161. event->data.info.load_dll.unicode = 1;
  162. return 1;
  163. }
  164. static int fill_unload_dll_event( struct debug_event *event, void *arg )
  165. {
  166. event->data.info.unload_dll.base = arg;
  167. return 1;
  168. }
  169. static int fill_output_debug_string_event( struct debug_event *event, void *arg )
  170. {
  171. struct debug_event_output_string *data = arg;
  172. event->data.info.output_string = *data;
  173. return 1;
  174. }
  175. typedef int (*fill_event_func)( struct debug_event *event, void *arg );
  176. #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
  177. static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
  178. {
  179. fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
  180. fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
  181. fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
  182. fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
  183. fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
  184. fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
  185. fill_unload_dll_event, /* UNLOAD_DLL_DEBUG_EVENT */
  186. fill_output_debug_string_event /* OUTPUT_DEBUG_STRING_EVENT */
  187. };
  188. /* unlink the first event from the queue */
  189. static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
  190. {
  191. if (event->prev) event->prev->next = event->next;
  192. else debug_ctx->event_head = event->next;
  193. if (event->next) event->next->prev = event->prev;
  194. else debug_ctx->event_tail = event->prev;
  195. event->next = event->prev = NULL;
  196. if (event->sender->debug_event == event) event->sender->debug_event = NULL;
  197. release_object( event );
  198. }
  199. /* link an event at the end of the queue */
  200. static void link_event( struct debug_event *event )
  201. {
  202. struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
  203. assert( debug_ctx );
  204. grab_object( event );
  205. event->next = NULL;
  206. event->prev = debug_ctx->event_tail;
  207. debug_ctx->event_tail = event;
  208. if (event->prev) event->prev->next = event;
  209. else debug_ctx->event_head = event;
  210. if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
  211. }
  212. /* find the next event that we can send to the debugger */
  213. static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
  214. {
  215. struct debug_event *event;
  216. for (event = debug_ctx->event_head; event; event = event->next)
  217. {
  218. if (event->state == EVENT_SENT) continue; /* already sent */
  219. if (event->sender->debug_event) continue; /* thread busy with another one */
  220. break;
  221. }
  222. return event;
  223. }
  224. static void debug_event_dump( struct object *obj, int verbose )
  225. {
  226. struct debug_event *debug_event = (struct debug_event *)obj;
  227. assert( obj->ops == &debug_event_ops );
  228. fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
  229. debug_event->sender, debug_event->data.code, debug_event->state );
  230. }
  231. static int debug_event_signaled( struct object *obj, struct thread *thread )
  232. {
  233. struct debug_event *debug_event = (struct debug_event *)obj;
  234. assert( obj->ops == &debug_event_ops );
  235. return debug_event->state == EVENT_CONTINUED;
  236. }
  237. static void debug_event_destroy( struct object *obj )
  238. {
  239. struct debug_event *event = (struct debug_event *)obj;
  240. assert( obj->ops == &debug_event_ops );
  241. /* cannot still be in the queue */
  242. assert( !event->next );
  243. assert( !event->prev );
  244. /* If the event has been sent already, the handles are now under the */
  245. /* responsibility of the debugger process, so we don't touch them */
  246. if (event->state == EVENT_QUEUED)
  247. {
  248. struct process *debugger = event->debugger->process;
  249. switch(event->data.code)
  250. {
  251. case CREATE_THREAD_DEBUG_EVENT:
  252. close_handle( debugger, event->data.info.create_thread.handle, NULL );
  253. break;
  254. case CREATE_PROCESS_DEBUG_EVENT:
  255. if (event->data.info.create_process.file)
  256. close_handle( debugger, event->data.info.create_process.file, NULL );
  257. close_handle( debugger, event->data.info.create_process.thread, NULL );
  258. close_handle( debugger, event->data.info.create_process.process, NULL );
  259. break;
  260. case LOAD_DLL_DEBUG_EVENT:
  261. if (event->data.info.load_dll.handle)
  262. close_handle( debugger, event->data.info.load_dll.handle, NULL );
  263. break;
  264. }
  265. }
  266. if (event->sender->context == &event->context) event->sender->context = NULL;
  267. release_object( event->sender );
  268. release_object( event->debugger );
  269. }
  270. static void debug_ctx_dump( struct object *obj, int verbose )
  271. {
  272. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  273. assert( obj->ops == &debug_ctx_ops );
  274. fprintf( stderr, "Debug context head=%p tail=%p\n",
  275. debug_ctx->event_head, debug_ctx->event_tail );
  276. }
  277. static int debug_ctx_signaled( struct object *obj, struct thread *thread )
  278. {
  279. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  280. assert( obj->ops == &debug_ctx_ops );
  281. return find_event_to_send( debug_ctx ) != NULL;
  282. }
  283. static void debug_ctx_destroy( struct object *obj )
  284. {
  285. struct debug_event *event;
  286. struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
  287. assert( obj->ops == &debug_ctx_ops );
  288. /* free all pending events */
  289. while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
  290. }
  291. /* continue a debug event */
  292. static int continue_debug_event( struct process *process, struct thread *thread, int status )
  293. {
  294. struct debug_event *event;
  295. struct debug_ctx *debug_ctx = current->debug_ctx;
  296. if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
  297. /* find the event in the queue */
  298. for (event = debug_ctx->event_head; event; event = event->next)
  299. {
  300. if (event->state != EVENT_SENT) continue;
  301. if (event->sender == thread) break;
  302. }
  303. if (!event) goto error;
  304. assert( event->sender->debug_event == event );
  305. event->status = status;
  306. event->state = EVENT_CONTINUED;
  307. wake_up( &event->obj, 0 );
  308. unlink_event( debug_ctx, event );
  309. resume_process( process );
  310. return 1;
  311. error:
  312. /* not debugging this process, or no such event */
  313. set_error( STATUS_ACCESS_DENIED ); /* FIXME */
  314. return 0;
  315. }
  316. /* alloc a debug event for a debugger */
  317. static struct debug_event *alloc_debug_event( struct thread *thread, int code,
  318. void *arg, const CONTEXT *context )
  319. {
  320. struct thread *debugger = thread->process->debugger;
  321. struct debug_event *event;
  322. assert( code > 0 && code <= NB_DEBUG_EVENTS );
  323. /* cannot queue a debug event for myself */
  324. assert( debugger->process != thread->process );
  325. /* build the event */
  326. if (!(event = alloc_object( &debug_event_ops ))) return NULL;
  327. event->next = NULL;
  328. event->prev = NULL;
  329. event->state = EVENT_QUEUED;
  330. event->sender = (struct thread *)grab_object( thread );
  331. event->debugger = (struct thread *)grab_object( debugger );
  332. event->data.code = code;
  333. if (!fill_debug_event[code-1]( event, arg ))
  334. {
  335. event->data.code = -1; /* make sure we don't attempt to close handles */
  336. release_object( event );
  337. return NULL;
  338. }
  339. if (context)
  340. {
  341. memcpy( &event->context, context, sizeof(event->context) );
  342. thread->context = &event->context;
  343. }
  344. return event;
  345. }
  346. /* generate a debug event from inside the server and queue it */
  347. void generate_debug_event( struct thread *thread, int code, void *arg )
  348. {
  349. if (thread->process->debugger)
  350. {
  351. struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
  352. if (event)
  353. {
  354. link_event( event );
  355. suspend_process( thread->process );
  356. release_object( event );
  357. }
  358. }
  359. }
  360. /* attach a process to a debugger thread and suspend it */
  361. static int debugger_attach( struct process *process, struct thread *debugger )
  362. {
  363. struct thread *thread;
  364. if (process->debugger) goto error; /* already being debugged */
  365. if (!is_process_init_done( process )) goto error; /* still starting up */
  366. if (!process->thread_list) goto error; /* no thread running in the process */
  367. /* make sure we don't create a debugging loop */
  368. for (thread = debugger; thread; thread = thread->process->debugger)
  369. if (thread->process == process) goto error;
  370. /* don't let a debugger debug its console... won't work */
  371. if (debugger->process->console && debugger->process->console->renderer->process == process)
  372. goto error;
  373. suspend_process( process );
  374. if (!attach_process( process ) || !set_process_debugger( process, debugger ))
  375. {
  376. resume_process( process );
  377. return 0;
  378. }
  379. if (!set_process_debug_flag( process, 1 ))
  380. {
  381. process->debugger = NULL;
  382. resume_process( process );
  383. return 0;
  384. }
  385. return 1;
  386. error:
  387. set_error( STATUS_ACCESS_DENIED );
  388. return 0;
  389. }
  390. /* detach a process from a debugger thread (and resume it ?) */
  391. int debugger_detach( struct process *process, struct thread *debugger )
  392. {
  393. struct debug_event *event;
  394. struct debug_ctx *debug_ctx;
  395. if (!process->debugger || process->debugger != debugger)
  396. goto error; /* not currently debugged, or debugged by another debugger */
  397. if (!debugger->debug_ctx ) goto error; /* should be a debugger */
  398. /* init should be done, otherwise wouldn't be attached */
  399. assert(is_process_init_done(process));
  400. suspend_process( process );
  401. /* send continue indication for all events */
  402. debug_ctx = debugger->debug_ctx;
  403. /* find the event in the queue
  404. * FIXME: could loop on process' threads and look the debug_event field */
  405. for (event = debug_ctx->event_head; event; event = event->next)
  406. {
  407. if (event->state != EVENT_QUEUED) continue;
  408. if (event->sender->process == process)
  409. {
  410. assert( event->sender->debug_event == event );
  411. event->status = DBG_CONTINUE;
  412. event->state = EVENT_CONTINUED;
  413. wake_up( &event->obj, 0 );
  414. unlink_event( debug_ctx, event );
  415. /* from queued debug event */
  416. resume_process( process );
  417. }
  418. }
  419. /* remove relationships between process and its debugger */
  420. process->debugger = NULL;
  421. if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
  422. detach_process( process );
  423. /* from this function */
  424. resume_process( process );
  425. return 0;
  426. error:
  427. set_error( STATUS_ACCESS_DENIED );
  428. return 0;
  429. }
  430. /* generate all startup events of a given process */
  431. void generate_startup_debug_events( struct process *process, void *entry )
  432. {
  433. struct process_dll *dll;
  434. struct thread *thread = process->thread_list;
  435. /* generate creation events */
  436. generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
  437. while ((thread = thread->proc_next))
  438. generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
  439. /* generate dll events (in loading order, i.e. reverse list order) */
  440. dll = &process->exe;
  441. while (dll->next) dll = dll->next;
  442. while (dll != &process->exe)
  443. {
  444. generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
  445. dll = dll->prev;
  446. }
  447. }
  448. /* set the debugger of a given process */
  449. int set_process_debugger( struct process *process, struct thread *debugger )
  450. {
  451. struct debug_ctx *debug_ctx;
  452. assert( !process->debugger );
  453. if (!debugger->debug_ctx) /* need to allocate a context */
  454. {
  455. if (!(debug_ctx = alloc_object( &debug_ctx_ops ))) return 0;
  456. debug_ctx->event_head = NULL;
  457. debug_ctx->event_tail = NULL;
  458. debug_ctx->kill_on_exit = 1;
  459. debugger->debug_ctx = debug_ctx;
  460. }
  461. process->debugger = debugger;
  462. return 1;
  463. }
  464. /* a thread is exiting */
  465. void debug_exit_thread( struct thread *thread )
  466. {
  467. if (thread->debug_ctx) /* this thread is a debugger */
  468. {
  469. if (thread->debug_ctx->kill_on_exit)
  470. {
  471. /* kill all debugged processes */
  472. kill_debugged_processes( thread, thread->exit_code );
  473. }
  474. else
  475. {
  476. detach_debugged_processes( thread );
  477. }
  478. release_object( thread->debug_ctx );
  479. thread->debug_ctx = NULL;
  480. }
  481. }
  482. /* Wait for a debug event */
  483. DECL_HANDLER(wait_debug_event)
  484. {
  485. struct debug_ctx *debug_ctx = current->debug_ctx;
  486. struct debug_event *event;
  487. if (!debug_ctx) /* current thread is not a debugger */
  488. {
  489. set_error( STATUS_INVALID_HANDLE );
  490. return;
  491. }
  492. reply->wait = 0;
  493. if ((event = find_event_to_send( debug_ctx )))
  494. {
  495. size_t size = get_reply_max_size();
  496. event->state = EVENT_SENT;
  497. event->sender->debug_event = event;
  498. reply->pid = get_process_id( event->sender->process );
  499. reply->tid = get_thread_id( event->sender );
  500. if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
  501. set_reply_data( &event->data, size );
  502. }
  503. else /* no event ready */
  504. {
  505. reply->pid = 0;
  506. reply->tid = 0;
  507. if (req->get_handle)
  508. reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
  509. }
  510. }
  511. /* Continue a debug event */
  512. DECL_HANDLER(continue_debug_event)
  513. {
  514. struct process *process = get_process_from_id( req->pid );
  515. if (process)
  516. {
  517. struct thread *thread = get_thread_from_id( req->tid );
  518. if (thread)
  519. {
  520. continue_debug_event( process, thread, req->status );
  521. release_object( thread );
  522. }
  523. release_object( process );
  524. }
  525. }
  526. /* Start debugging an existing process */
  527. DECL_HANDLER(debug_process)
  528. {
  529. struct process *process = get_process_from_id( req->pid );
  530. if (!process) return;
  531. if (!req->attach)
  532. {
  533. debugger_detach( process, current );
  534. }
  535. else if (debugger_attach( process, current ))
  536. {
  537. struct debug_event_exception data;
  538. generate_startup_debug_events( process, NULL );
  539. resume_process( process );
  540. data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
  541. data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
  542. data.record.ExceptionRecord = NULL;
  543. data.record.ExceptionAddress = get_thread_ip( process->thread_list );
  544. data.record.NumberParameters = 0;
  545. data.first = 1;
  546. generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
  547. }
  548. release_object( process );
  549. }
  550. /* queue an exception event */
  551. DECL_HANDLER(queue_exception_event)
  552. {
  553. reply->handle = 0;
  554. if (current->process->debugger)
  555. {
  556. struct debug_event_exception data;
  557. struct debug_event *event;
  558. const CONTEXT *context = get_req_data();
  559. const EXCEPTION_RECORD *rec = (const EXCEPTION_RECORD *)(context + 1);
  560. if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
  561. {
  562. set_error( STATUS_INVALID_PARAMETER );
  563. return;
  564. }
  565. data.record = *rec;
  566. data.first = req->first;
  567. if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
  568. {
  569. if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
  570. {
  571. link_event( event );
  572. suspend_process( current->process );
  573. }
  574. release_object( event );
  575. }
  576. }
  577. }
  578. /* retrieve the status of an exception event */
  579. DECL_HANDLER(get_exception_status)
  580. {
  581. struct debug_event *event;
  582. reply->status = 0;
  583. if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
  584. 0, &debug_event_ops )))
  585. {
  586. if (event->state == EVENT_CONTINUED)
  587. {
  588. reply->status = event->status;
  589. if (current->context == &event->context)
  590. {
  591. size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
  592. set_reply_data( &event->context, size );
  593. current->context = NULL;
  594. }
  595. }
  596. else set_error( STATUS_PENDING );
  597. release_object( event );
  598. }
  599. }
  600. /* send an output string to the debugger */
  601. DECL_HANDLER(output_debug_string)
  602. {
  603. struct debug_event_output_string data;
  604. data.string = req->string;
  605. data.unicode = req->unicode;
  606. data.length = req->length;
  607. generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
  608. }
  609. /* simulate a breakpoint in a process */
  610. DECL_HANDLER(debug_break)
  611. {
  612. struct process *process;
  613. reply->self = 0;
  614. if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION /*FIXME*/ )))
  615. return;
  616. if (process != current->process)
  617. {
  618. /* find a suitable thread to signal */
  619. struct thread *thread;
  620. for (thread = process->thread_list; thread; thread = thread->proc_next)
  621. {
  622. if (send_thread_signal( thread, SIGTRAP )) break;
  623. }
  624. if (!thread) set_error( STATUS_ACCESS_DENIED );
  625. }
  626. else reply->self = 1;
  627. release_object( process );
  628. }
  629. /* set debugger kill on exit flag */
  630. DECL_HANDLER(set_debugger_kill_on_exit)
  631. {
  632. if (!current->debug_ctx)
  633. {
  634. set_error( STATUS_ACCESS_DENIED );
  635. return;
  636. }
  637. current->debug_ctx->kill_on_exit = req->kill_on_exit;
  638. }