Reimplemented DebugBreakProcess.
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * FIXME: heap snapshots not implemented
7  */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "windef.h"
14 #include "tlhelp32.h"
15
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
20
21
22 struct snapshot
23 {
24     struct object             obj;           /* object header */
25     struct process           *process;       /* process of this snapshot (for modules and heaps) */
26     struct process_snapshot  *processes;     /* processes snapshot */
27     int                       process_count; /* count of processes */
28     int                       process_pos;   /* current position in proc snapshot */
29     struct thread_snapshot   *threads;       /* threads snapshot */
30     int                       thread_count;  /* count of threads */
31     int                       thread_pos;    /* current position in thread snapshot */
32     struct module_snapshot   *modules;       /* modules snapshot */
33     int                       module_count;  /* count of modules */
34     int                       module_pos;    /* current position in module snapshot */
35 };
36
37 static void snapshot_dump( struct object *obj, int verbose );
38 static void snapshot_destroy( struct object *obj );
39
40 static const struct object_ops snapshot_ops =
41 {
42     sizeof(struct snapshot),      /* size */
43     snapshot_dump,                /* dump */
44     no_add_queue,                 /* add_queue */
45     NULL,                         /* remove_queue */
46     NULL,                         /* signaled */
47     NULL,                         /* satisfied */
48     NULL,                         /* get_poll_events */
49     NULL,                         /* poll_event */
50     no_get_fd,                    /* get_fd */
51     no_flush,                     /* flush */
52     no_get_file_info,             /* get_file_info */
53     NULL,                         /* queue_async */
54     snapshot_destroy              /* destroy */
55 };
56
57
58 /* create a new snapshot */
59 static struct snapshot *create_snapshot( void *pid, int flags )
60 {
61     struct process *process = NULL;
62     struct snapshot *snapshot;
63
64     /* need a process for modules and heaps */
65     if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
66     {
67         if (!pid) process = (struct process *)grab_object( current->process );
68         else if (!(process = get_process_from_id( pid ))) return NULL;
69     }
70
71     if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
72     {
73         if (process) release_object( process );
74         return NULL;
75     }
76
77     snapshot->process = process;
78
79     snapshot->process_pos = 0;
80     snapshot->process_count = 0;
81     if (flags & TH32CS_SNAPPROCESS)
82         snapshot->processes = process_snap( &snapshot->process_count );
83
84     snapshot->thread_pos = 0;
85     snapshot->thread_count = 0;
86     if (flags & TH32CS_SNAPTHREAD)
87         snapshot->threads = thread_snap( &snapshot->thread_count );
88
89     snapshot->module_pos = 0;
90     snapshot->module_count = 0;
91     if (flags & TH32CS_SNAPMODULE)
92         snapshot->modules = module_snap( process, &snapshot->module_count );
93
94     return snapshot;
95 }
96
97 /* get the next process in the snapshot */
98 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
99 {
100     struct process_snapshot *ptr;
101
102     if (!snapshot->process_count)
103     {
104         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
105         return 0;
106     }
107     if (snapshot->process_pos >= snapshot->process_count)
108     {
109         set_error( STATUS_NO_MORE_FILES );
110         return 0;
111     }
112     ptr = &snapshot->processes[snapshot->process_pos++];
113     reply->count    = ptr->count;
114     reply->pid      = get_process_id( ptr->process );
115     reply->threads  = ptr->threads;
116     reply->priority = ptr->priority;
117     return 1;
118 }
119
120 /* get the next thread in the snapshot */
121 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
122 {
123     struct thread_snapshot *ptr;
124
125     if (!snapshot->thread_count)
126     {
127         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
128         return 0;
129     }
130     if (snapshot->thread_pos >= snapshot->thread_count)
131     {
132         set_error( STATUS_NO_MORE_FILES );
133         return 0;
134     }
135     ptr = &snapshot->threads[snapshot->thread_pos++];
136     reply->count     = ptr->count;
137     reply->pid       = get_process_id( ptr->thread->process );
138     reply->tid       = get_thread_id( ptr->thread );
139     reply->base_pri  = ptr->priority;
140     reply->delta_pri = 0;  /* FIXME */
141     return 1;
142 }
143
144 /* get the next module in the snapshot */
145 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
146 {
147     struct module_snapshot *ptr;
148
149     if (!snapshot->module_count)
150     {
151         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
152         return 0;
153     }
154     if (snapshot->module_pos >= snapshot->module_count)
155     {
156         set_error( STATUS_NO_MORE_FILES );
157         return 0;
158     }
159     ptr = &snapshot->modules[snapshot->module_pos++];
160     reply->pid  = get_process_id( snapshot->process );
161     reply->base = ptr->base;
162     return 1;
163 }
164
165 static void snapshot_dump( struct object *obj, int verbose )
166 {
167     struct snapshot *snapshot = (struct snapshot *)obj;
168     assert( obj->ops == &snapshot_ops );
169     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
170              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
171 }
172
173 static void snapshot_destroy( struct object *obj )
174 {
175     int i;
176     struct snapshot *snapshot = (struct snapshot *)obj;
177     assert( obj->ops == &snapshot_ops );
178     if (snapshot->process_count)
179     {
180         for (i = 0; i < snapshot->process_count; i++)
181             release_object( snapshot->processes[i].process );
182         free( snapshot->processes );
183     }
184     if (snapshot->thread_count)
185     {
186         for (i = 0; i < snapshot->thread_count; i++)
187             release_object( snapshot->threads[i].thread );
188         free( snapshot->threads );
189     }
190     if (snapshot->module_count) free( snapshot->modules );
191     if (snapshot->process) release_object( snapshot->process );
192 }
193
194 /* create a snapshot */
195 DECL_HANDLER(create_snapshot)
196 {
197     struct snapshot *snapshot;
198
199     reply->handle = 0;
200     if ((snapshot = create_snapshot( req->pid, req->flags )))
201     {
202         reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
203         release_object( snapshot );
204     }
205 }
206
207 /* get the next process from a snapshot */
208 DECL_HANDLER(next_process)
209 {
210     struct snapshot *snapshot;
211
212     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
213                                                        0, &snapshot_ops )))
214     {
215         if (req->reset) snapshot->process_pos = 0;
216         snapshot_next_process( snapshot, reply );
217         release_object( snapshot );
218     }
219 }
220
221 /* get the next thread from a snapshot */
222 DECL_HANDLER(next_thread)
223 {
224     struct snapshot *snapshot;
225
226     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
227                                                        0, &snapshot_ops )))
228     {
229         if (req->reset) snapshot->thread_pos = 0;
230         snapshot_next_thread( snapshot, reply );
231         release_object( snapshot );
232     }
233 }
234
235 /* get the next module from a snapshot */
236 DECL_HANDLER(next_module)
237 {
238     struct snapshot *snapshot;
239
240     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
241                                                        0, &snapshot_ops )))
242     {
243         if (req->reset) snapshot->module_pos = 0;
244         snapshot_next_module( snapshot, reply );
245         release_object( snapshot );
246     }
247 }