debugger.c 24 KB

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