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