Release 20000821.
[wine.git] / server / snapshot.c
blob3f806bee9cc6bc37c5d5b53825405e466cf3e72e
1 /*
2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
6 * FIXME: heap snapshots not implemented
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include "winnt.h"
14 #include "tlhelp32.h"
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
22 struct snapshot
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 */
37 static void snapshot_dump( struct object *obj, int verbose );
38 static void snapshot_destroy( struct object *obj );
40 static const struct object_ops snapshot_ops =
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 */
58 /* create a new snapshot */
59 static struct snapshot *create_snapshot( void *pid, int flags )
61 struct process *process = NULL;
62 struct snapshot *snapshot;
64 /* need a process for modules and heaps */
65 if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
67 if (!pid) process = (struct process *)grab_object( current->process );
68 else if (!(process = get_process_from_id( pid ))) return NULL;
71 if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
73 if (process) release_object( process );
74 return NULL;
77 snapshot->process = process;
79 snapshot->process_pos = 0;
80 snapshot->process_count = 0;
81 if (flags & TH32CS_SNAPPROCESS)
82 snapshot->processes = process_snap( &snapshot->process_count );
84 snapshot->thread_pos = 0;
85 snapshot->thread_count = 0;
86 if (flags & TH32CS_SNAPTHREAD)
87 snapshot->threads = thread_snap( &snapshot->thread_count );
89 snapshot->module_pos = 0;
90 snapshot->module_count = 0;
91 if (flags & TH32CS_SNAPMODULE)
92 snapshot->modules = module_snap( process, &snapshot->module_count );
94 return snapshot;
97 /* get the next process in the snapshot */
98 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
100 struct process_snapshot *ptr;
102 if (!snapshot->process_count)
104 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
105 return 0;
107 if (req->reset) snapshot->process_pos = 0;
108 else if (snapshot->process_pos >= snapshot->process_count)
110 set_error( STATUS_NO_MORE_FILES );
111 return 0;
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;
121 /* get the next thread in the snapshot */
122 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
124 struct thread_snapshot *ptr;
126 if (!snapshot->thread_count)
128 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
129 return 0;
131 if (req->reset) snapshot->thread_pos = 0;
132 else if (snapshot->thread_pos >= snapshot->thread_count)
134 set_error( STATUS_NO_MORE_FILES );
135 return 0;
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;
146 /* get the next module in the snapshot */
147 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
149 struct module_snapshot *ptr;
151 if (!snapshot->module_count)
153 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
154 return 0;
156 if (req->reset) snapshot->module_pos = 0;
157 else if (snapshot->module_pos >= snapshot->module_count)
159 set_error( STATUS_NO_MORE_FILES );
160 return 0;
162 ptr = &snapshot->modules[snapshot->module_pos++];
163 req->pid = get_process_id( snapshot->process );
164 req->base = ptr->base;
165 return 1;
168 static void snapshot_dump( struct object *obj, int verbose )
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 );
176 static void snapshot_destroy( struct object *obj )
178 int i;
179 struct snapshot *snapshot = (struct snapshot *)obj;
180 assert( obj->ops == &snapshot_ops );
181 if (snapshot->process_count)
183 for (i = 0; i < snapshot->process_count; i++)
184 release_object( snapshot->processes[i].process );
185 free( snapshot->processes );
187 if (snapshot->thread_count)
189 for (i = 0; i < snapshot->thread_count; i++)
190 release_object( snapshot->threads[i].thread );
191 free( snapshot->threads );
193 if (snapshot->module_count) free( snapshot->modules );
194 if (snapshot->process) release_object( snapshot->process );
197 /* create a snapshot */
198 DECL_HANDLER(create_snapshot)
200 struct snapshot *snapshot;
202 req->handle = -1;
203 if ((snapshot = create_snapshot( req->pid, req->flags )))
205 req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
206 release_object( snapshot );
210 /* get the next process from a snapshot */
211 DECL_HANDLER(next_process)
213 struct snapshot *snapshot;
215 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
216 0, &snapshot_ops )))
218 snapshot_next_process( snapshot, req );
219 release_object( snapshot );
223 /* get the next thread from a snapshot */
224 DECL_HANDLER(next_thread)
226 struct snapshot *snapshot;
228 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
229 0, &snapshot_ops )))
231 snapshot_next_thread( snapshot, req );
232 release_object( snapshot );
236 /* get the next module from a snapshot */
237 DECL_HANDLER(next_module)
239 struct snapshot *snapshot;
241 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
242 0, &snapshot_ops )))
244 snapshot_next_module( snapshot, req );
245 release_object( snapshot );