Implemented thread and (partial) module snapshots, based on the work
[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 "winnt.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_read_fd,                   /* get_read_fd */
51     no_write_fd,                  /* get_write_fd */
52     no_flush,                     /* flush */
53     no_get_file_info,             /* get_file_info */
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_request *req )
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 (req->reset) snapshot->process_pos = 0;
108     else if (snapshot->process_pos >= snapshot->process_count)
109     {
110         set_error( STATUS_NO_MORE_FILES );
111         return 0;
112     }
113     ptr = &snapshot->processes[snapshot->process_pos++];
114     req->count    = ptr->count;
115     req->pid      = get_process_id( ptr->process );
116     req->threads  = ptr->threads;
117     req->priority = ptr->priority;
118     return 1;
119 }
120
121 /* get the next thread in the snapshot */
122 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
123 {
124     struct thread_snapshot *ptr;
125
126     if (!snapshot->thread_count)
127     {
128         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
129         return 0;
130     }
131     if (req->reset) snapshot->thread_pos = 0;
132     else if (snapshot->thread_pos >= snapshot->thread_count)
133     {
134         set_error( STATUS_NO_MORE_FILES );
135         return 0;
136     }
137     ptr = &snapshot->threads[snapshot->thread_pos++];
138     req->count     = ptr->count;
139     req->pid       = get_process_id( ptr->thread->process );
140     req->tid       = get_thread_id( ptr->thread );
141     req->base_pri  = ptr->priority;
142     req->delta_pri = 0;  /* FIXME */
143     return 1;
144 }
145
146 /* get the next module in the snapshot */
147 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
148 {
149     struct module_snapshot *ptr;
150
151     if (!snapshot->module_count)
152     {
153         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
154         return 0;
155     }
156     if (req->reset) snapshot->module_pos = 0;
157     else if (snapshot->module_pos >= snapshot->module_count)
158     {
159         set_error( STATUS_NO_MORE_FILES );
160         return 0;
161     }
162     ptr = &snapshot->modules[snapshot->module_pos++];
163     req->pid  = get_process_id( snapshot->process );
164     req->base = ptr->base;
165     return 1;
166 }
167
168 static void snapshot_dump( struct object *obj, int verbose )
169 {
170     struct snapshot *snapshot = (struct snapshot *)obj;
171     assert( obj->ops == &snapshot_ops );
172     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
173              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
174 }
175
176 static void snapshot_destroy( struct object *obj )
177 {
178     int i;
179     struct snapshot *snapshot = (struct snapshot *)obj;
180     assert( obj->ops == &snapshot_ops );
181     if (snapshot->process_count)
182     {
183         for (i = 0; i < snapshot->process_count; i++)
184             release_object( snapshot->processes[i].process );
185         free( snapshot->processes );
186     }
187     if (snapshot->thread_count)
188     {
189         for (i = 0; i < snapshot->thread_count; i++)
190             release_object( snapshot->threads[i].thread );
191         free( snapshot->threads );
192     }
193     if (snapshot->module_count) free( snapshot->modules );
194     if (snapshot->process) release_object( snapshot->process );
195 }
196
197 /* create a snapshot */
198 DECL_HANDLER(create_snapshot)
199 {
200     struct snapshot *snapshot;
201
202     req->handle = -1;
203     if ((snapshot = create_snapshot( req->pid, req->flags )))
204     {
205         req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
206         release_object( snapshot );
207     }
208 }
209
210 /* get the next process from a snapshot */
211 DECL_HANDLER(next_process)
212 {
213     struct snapshot *snapshot;
214
215     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
216                                                        0, &snapshot_ops )))
217     {
218         snapshot_next_process( snapshot, req );
219         release_object( snapshot );
220     }
221 }
222
223 /* get the next thread from a snapshot */
224 DECL_HANDLER(next_thread)
225 {
226     struct snapshot *snapshot;
227
228     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
229                                                        0, &snapshot_ops )))
230     {
231         snapshot_next_thread( snapshot, req );
232         release_object( snapshot );
233     }
234 }
235
236 /* get the next module from a snapshot */
237 DECL_HANDLER(next_module)
238 {
239     struct snapshot *snapshot;
240
241     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
242                                                        0, &snapshot_ops )))
243     {
244         snapshot_next_module( snapshot, req );
245         release_object( snapshot );
246     }
247 }