Link the tools and the server against libwine so we can use the
[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 "windef.h"
31 #include "tlhelp32.h"
32
33 #include "handle.h"
34 #include "process.h"
35 #include "thread.h"
36 #include "request.h"
37
38
39 struct snapshot
40 {
41     struct object             obj;           /* object header */
42     struct process           *process;       /* process of this snapshot (for modules and heaps) */
43     struct process_snapshot  *processes;     /* processes snapshot */
44     int                       process_count; /* count of processes */
45     int                       process_pos;   /* current position in proc snapshot */
46     struct thread_snapshot   *threads;       /* threads snapshot */
47     int                       thread_count;  /* count of threads */
48     int                       thread_pos;    /* current position in thread snapshot */
49     struct module_snapshot   *modules;       /* modules snapshot */
50     int                       module_count;  /* count of modules */
51     int                       module_pos;    /* current position in module snapshot */
52 };
53
54 static void snapshot_dump( struct object *obj, int verbose );
55 static void snapshot_destroy( struct object *obj );
56
57 static const struct object_ops snapshot_ops =
58 {
59     sizeof(struct snapshot),      /* size */
60     snapshot_dump,                /* dump */
61     no_add_queue,                 /* add_queue */
62     NULL,                         /* remove_queue */
63     NULL,                         /* signaled */
64     NULL,                         /* satisfied */
65     NULL,                         /* get_poll_events */
66     NULL,                         /* poll_event */
67     no_get_fd,                    /* get_fd */
68     no_flush,                     /* flush */
69     no_get_file_info,             /* get_file_info */
70     NULL,                         /* queue_async */
71     snapshot_destroy              /* destroy */
72 };
73
74
75 /* create a new snapshot */
76 static struct snapshot *create_snapshot( void *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 & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
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, -1 )))
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 & TH32CS_SNAPPROCESS)
99         snapshot->processes = process_snap( &snapshot->process_count );
100
101     snapshot->thread_pos = 0;
102     snapshot->thread_count = 0;
103     if (flags & TH32CS_SNAPTHREAD)
104         snapshot->threads = thread_snap( &snapshot->thread_count );
105
106     snapshot->module_pos = 0;
107     snapshot->module_count = 0;
108     if (flags & TH32CS_SNAPMODULE)
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     = get_process_id( ptr->process->parent );
133     reply->heap     = 0;  /* FIXME */
134     reply->module   = 0;  /* FIXME */
135     reply->threads  = ptr->threads;
136     reply->priority = ptr->priority;
137     if (ptr->process->exe.filename)
138     {
139         size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
140         set_reply_data( ptr->process->exe.filename, len );
141     }
142     return 1;
143 }
144
145 /* get the next thread in the snapshot */
146 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
147 {
148     struct thread_snapshot *ptr;
149
150     if (!snapshot->thread_count)
151     {
152         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
153         return 0;
154     }
155     if (snapshot->thread_pos >= snapshot->thread_count)
156     {
157         set_error( STATUS_NO_MORE_FILES );
158         return 0;
159     }
160     ptr = &snapshot->threads[snapshot->thread_pos++];
161     reply->count     = ptr->count;
162     reply->pid       = get_process_id( ptr->thread->process );
163     reply->tid       = get_thread_id( ptr->thread );
164     reply->base_pri  = ptr->priority;
165     reply->delta_pri = 0;  /* FIXME */
166     return 1;
167 }
168
169 /* get the next module in the snapshot */
170 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
171 {
172     struct module_snapshot *ptr;
173
174     if (!snapshot->module_count)
175     {
176         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
177         return 0;
178     }
179     if (snapshot->module_pos >= snapshot->module_count)
180     {
181         set_error( STATUS_NO_MORE_FILES );
182         return 0;
183     }
184     ptr = &snapshot->modules[snapshot->module_pos++];
185     reply->pid  = get_process_id( snapshot->process );
186     reply->base = ptr->base;
187     reply->size = ptr->size;
188     if (ptr->filename)
189     {
190         size_t len = min( ptr->namelen, get_reply_max_size() );
191         set_reply_data( ptr->filename, len );
192     }
193     return 1;
194 }
195
196 static void snapshot_dump( struct object *obj, int verbose )
197 {
198     struct snapshot *snapshot = (struct snapshot *)obj;
199     assert( obj->ops == &snapshot_ops );
200     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
201              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
202 }
203
204 static void snapshot_destroy( struct object *obj )
205 {
206     int i;
207     struct snapshot *snapshot = (struct snapshot *)obj;
208     assert( obj->ops == &snapshot_ops );
209     if (snapshot->process_count)
210     {
211         for (i = 0; i < snapshot->process_count; i++)
212             release_object( snapshot->processes[i].process );
213         free( snapshot->processes );
214     }
215     if (snapshot->thread_count)
216     {
217         for (i = 0; i < snapshot->thread_count; i++)
218             release_object( snapshot->threads[i].thread );
219         free( snapshot->threads );
220     }
221     if (snapshot->module_count)
222     {
223         for (i = 0; i < snapshot->module_count; i++)
224             free( snapshot->modules[i].filename );
225         free( snapshot->modules );
226     }
227     if (snapshot->process) release_object( snapshot->process );
228 }
229
230 /* create a snapshot */
231 DECL_HANDLER(create_snapshot)
232 {
233     struct snapshot *snapshot;
234
235     reply->handle = 0;
236     if ((snapshot = create_snapshot( req->pid, req->flags )))
237     {
238         reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
239         release_object( snapshot );
240     }
241 }
242
243 /* get the next process from a snapshot */
244 DECL_HANDLER(next_process)
245 {
246     struct snapshot *snapshot;
247
248     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
249                                                        0, &snapshot_ops )))
250     {
251         if (req->reset) snapshot->process_pos = 0;
252         snapshot_next_process( snapshot, reply );
253         release_object( snapshot );
254     }
255 }
256
257 /* get the next thread from a snapshot */
258 DECL_HANDLER(next_thread)
259 {
260     struct snapshot *snapshot;
261
262     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
263                                                        0, &snapshot_ops )))
264     {
265         if (req->reset) snapshot->thread_pos = 0;
266         snapshot_next_thread( snapshot, reply );
267         release_object( snapshot );
268     }
269 }
270
271 /* get the next module from a snapshot */
272 DECL_HANDLER(next_module)
273 {
274     struct snapshot *snapshot;
275
276     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
277                                                        0, &snapshot_ops )))
278     {
279         if (req->reset) snapshot->module_pos = 0;
280         snapshot_next_module( snapshot, reply );
281         release_object( snapshot );
282     }
283 }