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