Let the console renderer be defined as a thread.
[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 "windef.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_get_fd,                    /* get_fd */
51     no_flush,                     /* flush */
52     no_get_file_info,             /* get_file_info */
53     snapshot_destroy              /* destroy */
54 };
55
56
57 /* create a new snapshot */
58 static struct snapshot *create_snapshot( void *pid, int flags )
59 {
60     struct process *process = NULL;
61     struct snapshot *snapshot;
62
63     /* need a process for modules and heaps */
64     if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
65     {
66         if (!pid) process = (struct process *)grab_object( current->process );
67         else if (!(process = get_process_from_id( pid ))) return NULL;
68     }
69
70     if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
71     {
72         if (process) release_object( process );
73         return NULL;
74     }
75
76     snapshot->process = process;
77
78     snapshot->process_pos = 0;
79     snapshot->process_count = 0;
80     if (flags & TH32CS_SNAPPROCESS)
81         snapshot->processes = process_snap( &snapshot->process_count );
82
83     snapshot->thread_pos = 0;
84     snapshot->thread_count = 0;
85     if (flags & TH32CS_SNAPTHREAD)
86         snapshot->threads = thread_snap( &snapshot->thread_count );
87
88     snapshot->module_pos = 0;
89     snapshot->module_count = 0;
90     if (flags & TH32CS_SNAPMODULE)
91         snapshot->modules = module_snap( process, &snapshot->module_count );
92
93     return snapshot;
94 }
95
96 /* get the next process in the snapshot */
97 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
98 {
99     struct process_snapshot *ptr;
100
101     if (!snapshot->process_count)
102     {
103         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
104         return 0;
105     }
106     if (snapshot->process_pos >= snapshot->process_count)
107     {
108         set_error( STATUS_NO_MORE_FILES );
109         return 0;
110     }
111     ptr = &snapshot->processes[snapshot->process_pos++];
112     reply->count    = ptr->count;
113     reply->pid      = get_process_id( ptr->process );
114     reply->threads  = ptr->threads;
115     reply->priority = ptr->priority;
116     return 1;
117 }
118
119 /* get the next thread in the snapshot */
120 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
121 {
122     struct thread_snapshot *ptr;
123
124     if (!snapshot->thread_count)
125     {
126         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
127         return 0;
128     }
129     if (snapshot->thread_pos >= snapshot->thread_count)
130     {
131         set_error( STATUS_NO_MORE_FILES );
132         return 0;
133     }
134     ptr = &snapshot->threads[snapshot->thread_pos++];
135     reply->count     = ptr->count;
136     reply->pid       = get_process_id( ptr->thread->process );
137     reply->tid       = get_thread_id( ptr->thread );
138     reply->base_pri  = ptr->priority;
139     reply->delta_pri = 0;  /* FIXME */
140     return 1;
141 }
142
143 /* get the next module in the snapshot */
144 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
145 {
146     struct module_snapshot *ptr;
147
148     if (!snapshot->module_count)
149     {
150         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
151         return 0;
152     }
153     if (snapshot->module_pos >= snapshot->module_count)
154     {
155         set_error( STATUS_NO_MORE_FILES );
156         return 0;
157     }
158     ptr = &snapshot->modules[snapshot->module_pos++];
159     reply->pid  = get_process_id( snapshot->process );
160     reply->base = ptr->base;
161     return 1;
162 }
163
164 static void snapshot_dump( struct object *obj, int verbose )
165 {
166     struct snapshot *snapshot = (struct snapshot *)obj;
167     assert( obj->ops == &snapshot_ops );
168     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
169              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
170 }
171
172 static void snapshot_destroy( struct object *obj )
173 {
174     int i;
175     struct snapshot *snapshot = (struct snapshot *)obj;
176     assert( obj->ops == &snapshot_ops );
177     if (snapshot->process_count)
178     {
179         for (i = 0; i < snapshot->process_count; i++)
180             release_object( snapshot->processes[i].process );
181         free( snapshot->processes );
182     }
183     if (snapshot->thread_count)
184     {
185         for (i = 0; i < snapshot->thread_count; i++)
186             release_object( snapshot->threads[i].thread );
187         free( snapshot->threads );
188     }
189     if (snapshot->module_count) free( snapshot->modules );
190     if (snapshot->process) release_object( snapshot->process );
191 }
192
193 /* create a snapshot */
194 DECL_HANDLER(create_snapshot)
195 {
196     struct snapshot *snapshot;
197
198     reply->handle = 0;
199     if ((snapshot = create_snapshot( req->pid, req->flags )))
200     {
201         reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
202         release_object( snapshot );
203     }
204 }
205
206 /* get the next process from a snapshot */
207 DECL_HANDLER(next_process)
208 {
209     struct snapshot *snapshot;
210
211     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
212                                                        0, &snapshot_ops )))
213     {
214         if (req->reset) snapshot->process_pos = 0;
215         snapshot_next_process( snapshot, reply );
216         release_object( snapshot );
217     }
218 }
219
220 /* get the next thread from a snapshot */
221 DECL_HANDLER(next_thread)
222 {
223     struct snapshot *snapshot;
224
225     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
226                                                        0, &snapshot_ops )))
227     {
228         if (req->reset) snapshot->thread_pos = 0;
229         snapshot_next_thread( snapshot, reply );
230         release_object( snapshot );
231     }
232 }
233
234 /* get the next module from a snapshot */
235 DECL_HANDLER(next_module)
236 {
237     struct snapshot *snapshot;
238
239     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
240                                                        0, &snapshot_ops )))
241     {
242         if (req->reset) snapshot->module_pos = 0;
243         snapshot_next_module( snapshot, reply );
244         release_object( snapshot );
245     }
246 }