Added debug events support.

diff --git a/server/thread.c b/server/thread.c
index d4fea46..28cdefb 100644
--- a/server/thread.c
+++ b/server/thread.c
@@ -87,6 +87,7 @@
     thread->mutex       = NULL;
     thread->debug_ctx   = NULL;
     thread->debug_first = NULL;
+    thread->debug_event = NULL;
     thread->wait        = NULL;
     thread->apc         = NULL;
     thread->apc_count   = 0;
@@ -157,6 +158,7 @@
     struct thread *thread = (struct thread *)obj;
     assert( obj->ops == &thread_ops );
 
+    assert( !thread->debug_ctx );  /* cannot still be debugging something */
     release_object( thread->process );
     if (thread->next) thread->next->prev = thread->prev;
     if (thread->prev) thread->prev->next = thread->next;
@@ -522,6 +524,7 @@
     thread->state = TERMINATED;
     thread->exit_code = exit_code;
     if (thread->wait) end_wait( thread );
+    debug_exit_thread( thread, exit_code );
     abandon_mutexes( thread );
     remove_process_thread( thread->process, thread );
     wake_up( &thread->obj, 0 );