Match PSDK STATUS_* definitions.
[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 "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
34
35 #include "handle.h"
36 #include "process.h"
37 #include "thread.h"
38 #include "request.h"
39
40
41 struct snapshot
42 {
43     struct object             obj;           /* object header */
44     struct process           *process;       /* process of this snapshot (for modules and heaps) */
45     struct process_snapshot  *processes;     /* processes snapshot */
46     int                       process_count; /* count of processes */
47     int                       process_pos;   /* current position in proc snapshot */
48     struct thread_snapshot   *threads;       /* threads snapshot */
49     int                       thread_count;  /* count of threads */
50     int                       thread_pos;    /* current position in thread snapshot */
51     struct module_snapshot   *modules;       /* modules snapshot */
52     int                       module_count;  /* count of modules */
53     int                       module_pos;    /* current position in module snapshot */
54 };
55
56 static void snapshot_dump( struct object *obj, int verbose );
57 static void snapshot_destroy( struct object *obj );
58
59 static const struct object_ops snapshot_ops =
60 {
61     sizeof(struct snapshot),      /* size */
62     snapshot_dump,                /* dump */
63     no_add_queue,                 /* add_queue */
64     NULL,                         /* remove_queue */
65     NULL,                         /* signaled */
66     NULL,                         /* satisfied */
67     no_signal,                    /* signal */
68     no_get_fd,                    /* get_fd */
69     no_lookup_name,               /* lookup_name */
70     no_close_handle,              /* close_handle */
71     snapshot_destroy              /* destroy */
72 };
73
74
75 /* create a new snapshot */
76 static struct snapshot *create_snapshot( process_id_t pid, int flags )
77 {
78     struct process *process = NULL;
79     struct snapshot *snapshot;
80
81     /* need a process for modules and heaps */
82     if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
83     {
84         if (!pid) process = (struct process *)grab_object( current->process );
85         else if (!(process = get_process_from_id( pid ))) return NULL;
86     }
87
88     if (!(snapshot = alloc_object( &snapshot_ops )))
89     {
90         if (process) release_object( process );
91         return NULL;
92     }
93
94     snapshot->process = process;
95
96     snapshot->process_pos = 0;
97     snapshot->process_count = 0;
98     if (flags & SNAP_PROCESS)
99         snapshot->processes = process_snap( &snapshot->process_count );
100
101     snapshot->thread_pos = 0;
102     snapshot->thread_count = 0;
103     if (flags & SNAP_THREAD)
104         snapshot->threads = thread_snap( &snapshot->thread_count );
105
106     snapshot->module_pos = 0;
107     snapshot->module_count = 0;
108     if (flags & SNAP_MODULE)
109         snapshot->modules = module_snap( process, &snapshot->module_count );
110
111     return snapshot;
112 }
113
114 /* get the next process in the snapshot */
115 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
116 {
117     struct process_snapshot *ptr;
118
119     if (!snapshot->process_count)
120     {
121         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
122         return 0;
123     }
124     if (snapshot->process_pos >= snapshot->process_count)
125     {
126         set_error( STATUS_NO_MORE_FILES );
127         return 0;
128     }
129     ptr = &snapshot->processes[snapshot->process_pos++];
130     reply->count    = ptr->count;
131     reply->pid      = get_process_id( ptr->process );
132     reply->ppid     = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
133     reply->heap     = NULL;  /* FIXME */
134     reply->module   = NULL;  /* FIXME */
135     reply->threads  = ptr->threads;
136     reply->priority = ptr->priority;
137     reply->handles  = ptr->handles;
138     if (ptr->process->exe.filename)
139     {
140         size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
141         set_reply_data( ptr->process->exe.filename, len );
142     }
143     return 1;
144 }
145
146 /* get the next thread in the snapshot */
147 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
148 {
149     struct thread_snapshot *ptr;
150
151     if (!snapshot->thread_count)
152     {
153         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
154         return 0;
155     }
156     if (snapshot->thread_pos >= snapshot->thread_count)
157     {
158         set_error( STATUS_NO_MORE_FILES );
159         return 0;
160     }
161     ptr = &snapshot->threads[snapshot->thread_pos++];
162     reply->count     = ptr->count;
163     reply->pid       = get_process_id( ptr->thread->process );
164     reply->tid       = get_thread_id( ptr->thread );
165     reply->base_pri  = ptr->priority;
166     reply->delta_pri = 0;  /* FIXME */
167     return 1;
168 }
169
170 /* get the next module in the snapshot */
171 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
172 {
173     struct module_snapshot *ptr;
174
175     if (!snapshot->module_count)
176     {
177         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
178         return 0;
179     }
180     if (snapshot->module_pos >= snapshot->module_count)
181     {
182         set_error( STATUS_NO_MORE_FILES );
183         return 0;
184     }
185     ptr = &snapshot->modules[snapshot->module_pos++];
186     reply->pid  = get_process_id( snapshot->process );
187     reply->base = ptr->base;
188     reply->size = ptr->size;
189     if (ptr->filename)
190     {
191         size_t len = min( ptr->namelen, get_reply_max_size() );
192         set_reply_data( ptr->filename, len );
193     }
194     return 1;
195 }
196
197 static void snapshot_dump( struct object *obj, int verbose )
198 {
199     struct snapshot *snapshot = (struct snapshot *)obj;
200     assert( obj->ops == &snapshot_ops );
201     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
202              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
203 }
204
205 static void snapshot_destroy( struct object *obj )
206 {
207     int i;
208     struct snapshot *snapshot = (struct snapshot *)obj;
209     assert( obj->ops == &snapshot_ops );
210     if (snapshot->process_count)
211     {
212         for (i = 0; i < snapshot->process_count; i++)
213             release_object( snapshot->processes[i].process );
214         free( snapshot->processes );
215     }
216     if (snapshot->thread_count)
217     {
218         for (i = 0; i < snapshot->thread_count; i++)
219             release_object( snapshot->threads[i].thread );
220         free( snapshot->threads );
221     }
222     if (snapshot->module_count)
223     {
224         for (i = 0; i < snapshot->module_count; i++)
225             free( snapshot->modules[i].filename );
226         free( snapshot->modules );
227     }
228     if (snapshot->process) release_object( snapshot->process );
229 }
230
231 /* create a snapshot */
232 DECL_HANDLER(create_snapshot)
233 {
234     struct snapshot *snapshot;
235
236     reply->handle = 0;
237     if ((snapshot = create_snapshot( req->pid, req->flags )))
238     {
239         reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
240         release_object( snapshot );
241     }
242 }
243
244 /* get the next process from a snapshot */
245 DECL_HANDLER(next_process)
246 {
247     struct snapshot *snapshot;
248
249     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
250                                                        0, &snapshot_ops )))
251     {
252         if (req->reset) snapshot->process_pos = 0;
253         snapshot_next_process( snapshot, reply );
254         release_object( snapshot );
255     }
256 }
257
258 /* get the next thread from a snapshot */
259 DECL_HANDLER(next_thread)
260 {
261     struct snapshot *snapshot;
262
263     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
264                                                        0, &snapshot_ops )))
265     {
266         if (req->reset) snapshot->thread_pos = 0;
267         snapshot_next_thread( snapshot, reply );
268         release_object( snapshot );
269     }
270 }
271
272 /* get the next module from a snapshot */
273 DECL_HANDLER(next_module)
274 {
275     struct snapshot *snapshot;
276
277     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
278                                                        0, &snapshot_ops )))
279     {
280         if (req->reset) snapshot->module_pos = 0;
281         snapshot_next_module( snapshot, reply );
282         release_object( snapshot );
283     }
284 }