debugger.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 file *file; /* file object for events that need one */
  44. enum debug_event_state state; /* event state */
  45. int status; /* continuation status */
  46. debug_event_t data; /* event data */
  47. };
  48. static const WCHAR debug_obj_name[] = {'D','e','b','u','g','O','b','j','e','c','t'};
  49. struct type_descr debug_obj_type =
  50. {
  51. { debug_obj_name, sizeof(debug_obj_name) }, /* name */
  52. DEBUG_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
  53. { /* mapping */
  54. STANDARD_RIGHTS_READ | DEBUG_READ_EVENT,
  55. STANDARD_RIGHTS_WRITE | DEBUG_PROCESS_ASSIGN,
  56. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  57. DEBUG_ALL_ACCESS
  58. },
  59. };
  60. /* debug object */
  61. struct debug_obj
  62. {
  63. struct object obj; /* object header */
  64. struct list event_queue; /* pending events queue */
  65. unsigned int flags; /* debug flags */
  66. };
  67. static void debug_event_dump( struct object *obj, int verbose );
  68. static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry );
  69. static void debug_event_destroy( struct object *obj );
  70. static const struct object_ops debug_event_ops =
  71. {
  72. sizeof(struct debug_event), /* size */
  73. &no_type, /* type */
  74. debug_event_dump, /* dump */
  75. add_queue, /* add_queue */
  76. remove_queue, /* remove_queue */
  77. debug_event_signaled, /* signaled */
  78. no_satisfied, /* satisfied */
  79. no_signal, /* signal */
  80. no_get_fd, /* get_fd */
  81. default_map_access, /* map_access */
  82. default_get_sd, /* get_sd */
  83. default_set_sd, /* set_sd */
  84. no_get_full_name, /* get_full_name */
  85. no_lookup_name, /* lookup_name */
  86. no_link_name, /* link_name */
  87. NULL, /* unlink_name */
  88. no_open_file, /* open_file */
  89. no_kernel_obj_list, /* get_kernel_obj_list */
  90. no_close_handle, /* close_handle */
  91. debug_event_destroy /* destroy */
  92. };
  93. static void debug_obj_dump( struct object *obj, int verbose );
  94. static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry );
  95. static void debug_obj_destroy( struct object *obj );
  96. static const struct object_ops debug_obj_ops =
  97. {
  98. sizeof(struct debug_obj), /* size */
  99. &debug_obj_type, /* type */
  100. debug_obj_dump, /* dump */
  101. add_queue, /* add_queue */
  102. remove_queue, /* remove_queue */
  103. debug_obj_signaled, /* signaled */
  104. no_satisfied, /* satisfied */
  105. no_signal, /* signal */
  106. no_get_fd, /* get_fd */
  107. default_map_access, /* map_access */
  108. default_get_sd, /* get_sd */
  109. default_set_sd, /* set_sd */
  110. default_get_full_name, /* get_full_name */
  111. no_lookup_name, /* lookup_name */
  112. directory_link_name, /* link_name */
  113. default_unlink_name, /* unlink_name */
  114. no_open_file, /* open_file */
  115. no_kernel_obj_list, /* get_kernel_obj_list */
  116. no_close_handle, /* close_handle */
  117. debug_obj_destroy /* destroy */
  118. };
  119. /* get a pointer to TEB->ArbitraryUserPointer in the client address space */
  120. static client_ptr_t get_teb_user_ptr( struct thread *thread )
  121. {
  122. unsigned int ptr_size = (CPU_FLAG( thread->process->cpu ) & CPU_64BIT_MASK) ? 8 : 4;
  123. return thread->teb + 5 * ptr_size;
  124. }
  125. /* routines to build an event according to its type */
  126. static void fill_exception_event( struct debug_event *event, const void *arg )
  127. {
  128. const debug_event_t *data = arg;
  129. event->data.exception = data->exception;
  130. event->data.exception.nb_params = min( event->data.exception.nb_params, EXCEPTION_MAXIMUM_PARAMETERS );
  131. }
  132. static void fill_create_thread_event( struct debug_event *event, const void *arg )
  133. {
  134. const client_ptr_t *entry = arg;
  135. if (entry) event->data.create_thread.start = *entry;
  136. }
  137. static void fill_create_process_event( struct debug_event *event, const void *arg )
  138. {
  139. const struct memory_view *view = arg;
  140. const pe_image_info_t *image_info = get_view_image_info( view, &event->data.create_process.base );
  141. event->data.create_process.start = image_info->entry_point;
  142. event->data.create_process.dbg_offset = image_info->dbg_offset;
  143. event->data.create_process.dbg_size = image_info->dbg_size;
  144. /* the doc says write access too, but this doesn't seem a good idea */
  145. event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
  146. }
  147. static void fill_exit_thread_event( struct debug_event *event, const void *arg )
  148. {
  149. const struct thread *thread = arg;
  150. event->data.exit.exit_code = thread->exit_code;
  151. }
  152. static void fill_exit_process_event( struct debug_event *event, const void *arg )
  153. {
  154. const struct process *process = arg;
  155. event->data.exit.exit_code = process->exit_code;
  156. }
  157. static void fill_load_dll_event( struct debug_event *event, const void *arg )
  158. {
  159. const struct memory_view *view = arg;
  160. const pe_image_info_t *image_info = get_view_image_info( view, &event->data.load_dll.base );
  161. event->data.load_dll.dbg_offset = image_info->dbg_offset;
  162. event->data.load_dll.dbg_size = image_info->dbg_size;
  163. event->data.load_dll.name = get_teb_user_ptr( event->sender );
  164. event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
  165. }
  166. static void fill_unload_dll_event( struct debug_event *event, const void *arg )
  167. {
  168. const struct memory_view *view = arg;
  169. get_view_image_info( view, &event->data.unload_dll.base );
  170. }
  171. typedef void (*fill_event_func)( struct debug_event *event, const void *arg );
  172. static const fill_event_func fill_debug_event[] =
  173. {
  174. fill_create_thread_event, /* DbgCreateThreadStateChange */
  175. fill_create_process_event, /* DbgCreateProcessStateChange */
  176. fill_exit_thread_event, /* DbgExitThreadStateChange */
  177. fill_exit_process_event, /* DbgExitProcessStateChange */
  178. fill_exception_event, /* DbgExceptionStateChange */
  179. fill_exception_event, /* DbgBreakpointStateChange */
  180. fill_exception_event, /* DbgSingleStepStateChange */
  181. fill_load_dll_event, /* DbgLoadDllStateChange */
  182. fill_unload_dll_event /* DbgUnloadDllStateChange */
  183. };
  184. /* allocate the necessary handles in the event data */
  185. static void alloc_event_handles( struct debug_event *event, struct process *process )
  186. {
  187. switch (event->data.code)
  188. {
  189. case DbgCreateThreadStateChange:
  190. /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
  191. event->data.create_thread.handle = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
  192. break;
  193. case DbgCreateProcessStateChange:
  194. event->data.create_process.thread = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
  195. /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
  196. event->data.create_process.process = alloc_handle( process, event->sender->process,
  197. PROCESS_ALL_ACCESS, 0 );
  198. if (event->file)
  199. event->data.create_process.file = alloc_handle( process, event->file, GENERIC_READ, 0 );
  200. break;
  201. case DbgLoadDllStateChange:
  202. if (event->file)
  203. event->data.load_dll.handle = alloc_handle( process, event->file, GENERIC_READ, 0 );
  204. break;
  205. }
  206. clear_error(); /* ignore errors, simply set handles to 0 */
  207. }
  208. /* unlink the first event from the queue */
  209. static void unlink_event( struct debug_obj *debug_obj, struct debug_event *event )
  210. {
  211. list_remove( &event->entry );
  212. if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
  213. release_object( event );
  214. }
  215. /* link an event at the end of the queue */
  216. static void link_event( struct debug_obj *debug_obj, struct debug_event *event )
  217. {
  218. grab_object( event );
  219. list_add_tail( &debug_obj->event_queue, &event->entry );
  220. if (!event->sender->process->debug_event)
  221. {
  222. /* grab reference since debugger could be killed while trying to wake up */
  223. grab_object( debug_obj );
  224. wake_up( &debug_obj->obj, 0 );
  225. release_object( debug_obj );
  226. }
  227. }
  228. /* resume a delayed debug event already in the queue */
  229. static void resume_event( struct debug_obj *debug_obj, struct debug_event *event )
  230. {
  231. event->state = EVENT_QUEUED;
  232. if (!event->sender->process->debug_event)
  233. {
  234. grab_object( debug_obj );
  235. wake_up( &debug_obj->obj, 0 );
  236. release_object( debug_obj );
  237. }
  238. }
  239. /* delay a debug event already in the queue to be replayed when thread wakes up */
  240. static void delay_event( struct debug_obj *debug_obj, struct debug_event *event )
  241. {
  242. event->state = EVENT_DELAYED;
  243. if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
  244. }
  245. /* find the next event that we can send to the debugger */
  246. static struct debug_event *find_event_to_send( struct debug_obj *debug_obj )
  247. {
  248. struct debug_event *event;
  249. LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
  250. {
  251. if (event->state == EVENT_SENT) continue; /* already sent */
  252. if (event->state == EVENT_DELAYED) continue; /* delayed until thread resumes */
  253. if (event->sender->process->debug_event) continue; /* process busy with another one */
  254. return event;
  255. }
  256. return NULL;
  257. }
  258. static void debug_event_dump( struct object *obj, int verbose )
  259. {
  260. struct debug_event *debug_event = (struct debug_event *)obj;
  261. assert( obj->ops == &debug_event_ops );
  262. fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
  263. debug_event->sender, debug_event->data.code, debug_event->state );
  264. }
  265. static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry )
  266. {
  267. struct debug_event *debug_event = (struct debug_event *)obj;
  268. assert( obj->ops == &debug_event_ops );
  269. return debug_event->state == EVENT_CONTINUED;
  270. }
  271. static void debug_event_destroy( struct object *obj )
  272. {
  273. struct debug_event *event = (struct debug_event *)obj;
  274. assert( obj->ops == &debug_event_ops );
  275. if (event->file) release_object( event->file );
  276. release_object( event->sender );
  277. }
  278. static void debug_obj_dump( struct object *obj, int verbose )
  279. {
  280. struct debug_obj *debug_obj = (struct debug_obj *)obj;
  281. assert( obj->ops == &debug_obj_ops );
  282. fprintf( stderr, "Debug context head=%p tail=%p\n",
  283. debug_obj->event_queue.next, debug_obj->event_queue.prev );
  284. }
  285. static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry )
  286. {
  287. struct debug_obj *debug_obj = (struct debug_obj *)obj;
  288. assert( obj->ops == &debug_obj_ops );
  289. return find_event_to_send( debug_obj ) != NULL;
  290. }
  291. static void debug_obj_destroy( struct object *obj )
  292. {
  293. struct list *ptr;
  294. struct debug_obj *debug_obj = (struct debug_obj *)obj;
  295. assert( obj->ops == &debug_obj_ops );
  296. detach_debugged_processes( debug_obj,
  297. (debug_obj->flags & DEBUG_KILL_ON_CLOSE) ? STATUS_DEBUGGER_INACTIVE : 0 );
  298. /* free all pending events */
  299. while ((ptr = list_head( &debug_obj->event_queue )))
  300. unlink_event( debug_obj, LIST_ENTRY( ptr, struct debug_event, entry ));
  301. }
  302. struct debug_obj *get_debug_obj( struct process *process, obj_handle_t handle, unsigned int access )
  303. {
  304. return (struct debug_obj *)get_handle_obj( process, handle, access, &debug_obj_ops );
  305. }
  306. static struct debug_obj *create_debug_obj( struct object *root, const struct unicode_str *name,
  307. unsigned int attr, unsigned int flags,
  308. const struct security_descriptor *sd )
  309. {
  310. struct debug_obj *debug_obj;
  311. if ((debug_obj = create_named_object( root, &debug_obj_ops, name, attr, sd )))
  312. {
  313. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  314. {
  315. debug_obj->flags = flags;
  316. list_init( &debug_obj->event_queue );
  317. }
  318. }
  319. return debug_obj;
  320. }
  321. /* continue a debug event */
  322. static int continue_debug_event( struct debug_obj *debug_obj, struct process *process,
  323. struct thread *thread, int status )
  324. {
  325. if (process->debug_obj == debug_obj && thread->process == process)
  326. {
  327. struct debug_event *event;
  328. if (status == DBG_REPLY_LATER)
  329. {
  330. /* if thread is suspended, delay all its events and resume process
  331. * if not, reset the event for immediate replay */
  332. LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
  333. {
  334. if (event->sender != thread) continue;
  335. if (thread->suspend)
  336. {
  337. delay_event( debug_obj, event );
  338. resume_process( process );
  339. }
  340. else if (event->state == EVENT_SENT)
  341. {
  342. assert( event->sender->process->debug_event == event );
  343. event->sender->process->debug_event = NULL;
  344. resume_event( debug_obj, event );
  345. return 1;
  346. }
  347. }
  348. return 1;
  349. }
  350. /* find the event in the queue */
  351. LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
  352. {
  353. if (event->state != EVENT_SENT) continue;
  354. if (event->sender == thread)
  355. {
  356. assert( event->sender->process->debug_event == event );
  357. event->status = status;
  358. event->state = EVENT_CONTINUED;
  359. wake_up( &event->obj, 0 );
  360. unlink_event( debug_obj, event );
  361. resume_process( process );
  362. return 1;
  363. }
  364. }
  365. }
  366. /* not debugging this process, or no such event */
  367. set_error( STATUS_ACCESS_DENIED ); /* FIXME */
  368. return 0;
  369. }
  370. /* alloc a debug event for a debugger */
  371. static struct debug_event *alloc_debug_event( struct thread *thread, int code, const void *arg )
  372. {
  373. struct debug_event *event;
  374. assert( code >= DbgCreateThreadStateChange && code <= DbgUnloadDllStateChange );
  375. /* build the event */
  376. if (!(event = alloc_object( &debug_event_ops ))) return NULL;
  377. event->state = EVENT_QUEUED;
  378. event->sender = (struct thread *)grab_object( thread );
  379. event->file = NULL;
  380. memset( &event->data, 0, sizeof(event->data) );
  381. fill_debug_event[code - DbgCreateThreadStateChange]( event, arg );
  382. event->data.code = code;
  383. return event;
  384. }
  385. /* generate a debug event from inside the server and queue it */
  386. void generate_debug_event( struct thread *thread, int code, const void *arg )
  387. {
  388. struct debug_obj *debug_obj = thread->process->debug_obj;
  389. if (debug_obj)
  390. {
  391. struct debug_event *event = alloc_debug_event( thread, code, arg );
  392. if (event)
  393. {
  394. link_event( debug_obj, event );
  395. suspend_process( thread->process );
  396. release_object( event );
  397. }
  398. clear_error(); /* ignore errors */
  399. }
  400. }
  401. void resume_delayed_debug_events( struct thread *thread )
  402. {
  403. struct debug_obj *debug_obj = thread->process->debug_obj;
  404. struct debug_event *event;
  405. if (debug_obj)
  406. {
  407. LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
  408. {
  409. if (event->sender != thread) continue;
  410. if (event->state != EVENT_DELAYED) continue;
  411. resume_event( debug_obj, event );
  412. suspend_process( thread->process );
  413. }
  414. }
  415. }
  416. /* attach a process to a debugger thread and suspend it */
  417. static int debugger_attach( struct process *process, struct thread *debugger, struct debug_obj *debug_obj )
  418. {
  419. if (debugger->process == process) goto error;
  420. if (!is_process_init_done( process )) goto error; /* still starting up */
  421. if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
  422. /* don't let a debugger debug its console... won't work */
  423. if (debugger->process->console)
  424. {
  425. struct thread *renderer = console_get_renderer(debugger->process->console);
  426. if (renderer && renderer->process == process) goto error;
  427. }
  428. suspend_process( process );
  429. if (!set_process_debug_flag( process, 1 ))
  430. {
  431. resume_process( process );
  432. return 0;
  433. }
  434. process->debug_obj = debug_obj;
  435. process->debug_children = 0;
  436. generate_startup_debug_events( process );
  437. resume_process( process );
  438. return 1;
  439. error:
  440. set_error( STATUS_ACCESS_DENIED );
  441. return 0;
  442. }
  443. /* detach a process from a debugger thread (and resume it) */
  444. void debugger_detach( struct process *process, struct debug_obj *debug_obj )
  445. {
  446. struct debug_event *event, *next;
  447. suspend_process( process );
  448. /* send continue indication for all events */
  449. LIST_FOR_EACH_ENTRY_SAFE( event, next, &debug_obj->event_queue, struct debug_event, entry )
  450. {
  451. if (event->sender->process != process) continue;
  452. assert( event->state != EVENT_CONTINUED );
  453. event->status = DBG_CONTINUE;
  454. event->state = EVENT_CONTINUED;
  455. wake_up( &event->obj, 0 );
  456. unlink_event( debug_obj, event );
  457. /* from queued debug event */
  458. resume_process( process );
  459. }
  460. /* remove relationships between process and its debugger */
  461. process->debug_obj = NULL;
  462. if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
  463. resume_process( process );
  464. }
  465. /* create a debug object */
  466. DECL_HANDLER(create_debug_obj)
  467. {
  468. struct debug_obj *debug_obj;
  469. struct unicode_str name;
  470. struct object *root;
  471. const struct security_descriptor *sd;
  472. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  473. if (!objattr) return;
  474. if ((debug_obj = create_debug_obj( root, &name, objattr->attributes, req->flags, sd )))
  475. {
  476. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  477. reply->handle = alloc_handle( current->process, debug_obj, req->access, objattr->attributes );
  478. else
  479. reply->handle = alloc_handle_no_access_check( current->process, debug_obj,
  480. req->access, objattr->attributes );
  481. release_object( debug_obj );
  482. }
  483. if (root) release_object( root );
  484. }
  485. /* Wait for a debug event */
  486. DECL_HANDLER(wait_debug_event)
  487. {
  488. struct debug_obj *debug_obj;
  489. struct debug_event *event;
  490. if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
  491. if ((event = find_event_to_send( debug_obj )))
  492. {
  493. event->state = EVENT_SENT;
  494. event->sender->process->debug_event = event;
  495. reply->pid = get_process_id( event->sender->process );
  496. reply->tid = get_thread_id( event->sender );
  497. alloc_event_handles( event, current->process );
  498. set_reply_data( &event->data, min( get_reply_max_size(), sizeof(event->data) ));
  499. }
  500. else
  501. {
  502. int state = list_empty( &debug_obj->event_queue ) ? DbgIdle : DbgReplyPending;
  503. set_reply_data( &state, min( get_reply_max_size(), sizeof(state) ));
  504. }
  505. release_object( debug_obj );
  506. }
  507. /* Continue a debug event */
  508. DECL_HANDLER(continue_debug_event)
  509. {
  510. struct debug_obj *debug_obj;
  511. struct process *process;
  512. if (req->status != DBG_EXCEPTION_NOT_HANDLED &&
  513. req->status != DBG_EXCEPTION_HANDLED &&
  514. req->status != DBG_CONTINUE &&
  515. req->status != DBG_REPLY_LATER)
  516. {
  517. set_error( STATUS_INVALID_PARAMETER );
  518. return;
  519. }
  520. if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
  521. if ((process = get_process_from_id( req->pid )))
  522. {
  523. struct thread *thread = get_thread_from_id( req->tid );
  524. if (thread)
  525. {
  526. continue_debug_event( debug_obj, process, thread, req->status );
  527. release_object( thread );
  528. }
  529. release_object( process );
  530. }
  531. release_object( debug_obj );
  532. }
  533. /* start or stop debugging an existing process */
  534. DECL_HANDLER(debug_process)
  535. {
  536. struct debug_obj *debug_obj;
  537. struct process *process = get_process_from_handle( req->handle,
  538. PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_SUSPEND_RESUME );
  539. if (!process) return;
  540. if ((debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
  541. {
  542. if (req->attach)
  543. {
  544. if (!process->debug_obj) debugger_attach( process, current, debug_obj );
  545. else set_error( STATUS_ACCESS_DENIED );
  546. }
  547. else
  548. {
  549. if (process->debug_obj == debug_obj) debugger_detach( process, debug_obj );
  550. else set_error( STATUS_ACCESS_DENIED );
  551. }
  552. release_object( debug_obj );
  553. }
  554. release_object( process );
  555. }
  556. /* queue an exception event */
  557. DECL_HANDLER(queue_exception_event)
  558. {
  559. struct debug_obj *debug_obj = current->process->debug_obj;
  560. reply->handle = 0;
  561. if (debug_obj)
  562. {
  563. debug_event_t data;
  564. struct debug_event *event;
  565. struct thread *thread = current;
  566. if ((req->len % sizeof(client_ptr_t)) != 0 ||
  567. req->len > get_req_data_size() ||
  568. req->len > EXCEPTION_MAXIMUM_PARAMETERS * sizeof(client_ptr_t))
  569. {
  570. set_error( STATUS_INVALID_PARAMETER );
  571. return;
  572. }
  573. memset( &data, 0, sizeof(data) );
  574. data.exception.first = req->first;
  575. data.exception.exc_code = req->code;
  576. data.exception.flags = req->flags;
  577. data.exception.record = req->record;
  578. data.exception.address = req->address;
  579. data.exception.nb_params = req->len / sizeof(client_ptr_t);
  580. memcpy( data.exception.params, get_req_data(), req->len );
  581. if ((event = alloc_debug_event( thread, DbgExceptionStateChange, &data )))
  582. {
  583. if ((reply->handle = alloc_handle( thread->process, event, SYNCHRONIZE, 0 )))
  584. {
  585. link_event( debug_obj, event );
  586. suspend_process( thread->process );
  587. }
  588. release_object( event );
  589. }
  590. }
  591. }
  592. /* retrieve the status of an exception event */
  593. DECL_HANDLER(get_exception_status)
  594. {
  595. struct debug_event *event;
  596. if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
  597. 0, &debug_event_ops )))
  598. {
  599. close_handle( current->process, req->handle );
  600. set_error( event->state == EVENT_CONTINUED ? event->status : STATUS_PENDING );
  601. release_object( event );
  602. }
  603. }
  604. /* set debug object information */
  605. DECL_HANDLER(set_debug_obj_info)
  606. {
  607. struct debug_obj *debug_obj;
  608. if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_SET_INFORMATION ))) return;
  609. debug_obj->flags = req->flags;
  610. release_object( debug_obj );
  611. }