Fix remaining signed/unsigned mismatches.
[wine.git] / server / snapshot.c
blobae4eb4926ac4e8a2b70e5bed54eb7260596f5f0f
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 "windef.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_get_fd, /* get_fd */
51 no_flush, /* flush */
52 no_get_file_info, /* get_file_info */
53 snapshot_destroy /* destroy */
57 /* create a new snapshot */
58 static struct snapshot *create_snapshot( void *pid, int flags )
60 struct process *process = NULL;
61 struct snapshot *snapshot;
63 /* need a process for modules and heaps */
64 if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
66 if (!pid) process = (struct process *)grab_object( current->process );
67 else if (!(process = get_process_from_id( pid ))) return NULL;
70 if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
72 if (process) release_object( process );
73 return NULL;
76 snapshot->process = process;
78 snapshot->process_pos = 0;
79 snapshot->process_count = 0;
80 if (flags & TH32CS_SNAPPROCESS)
81 snapshot->processes = process_snap( &snapshot->process_count );
83 snapshot->thread_pos = 0;
84 snapshot->thread_count = 0;
85 if (flags & TH32CS_SNAPTHREAD)
86 snapshot->threads = thread_snap( &snapshot->thread_count );
88 snapshot->module_pos = 0;
89 snapshot->module_count = 0;
90 if (flags & TH32CS_SNAPMODULE)
91 snapshot->modules = module_snap( process, &snapshot->module_count );
93 return snapshot;
96 /* get the next process in the snapshot */
97 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
99 struct process_snapshot *ptr;
101 if (!snapshot->process_count)
103 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
104 return 0;
106 if (req->reset) snapshot->process_pos = 0;
107 else if (snapshot->process_pos >= snapshot->process_count)
109 set_error( STATUS_NO_MORE_FILES );
110 return 0;
112 ptr = &snapshot->processes[snapshot->process_pos++];
113 req->count = ptr->count;
114 req->pid = get_process_id( ptr->process );
115 req->threads = ptr->threads;
116 req->priority = ptr->priority;
117 return 1;
120 /* get the next thread in the snapshot */
121 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
123 struct thread_snapshot *ptr;
125 if (!snapshot->thread_count)
127 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
128 return 0;
130 if (req->reset) snapshot->thread_pos = 0;
131 else if (snapshot->thread_pos >= snapshot->thread_count)
133 set_error( STATUS_NO_MORE_FILES );
134 return 0;
136 ptr = &snapshot->threads[snapshot->thread_pos++];
137 req->count = ptr->count;
138 req->pid = get_process_id( ptr->thread->process );
139 req->tid = get_thread_id( ptr->thread );
140 req->base_pri = ptr->priority;
141 req->delta_pri = 0; /* FIXME */
142 return 1;
145 /* get the next module in the snapshot */
146 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
148 struct module_snapshot *ptr;
150 if (!snapshot->module_count)
152 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
153 return 0;
155 if (req->reset) snapshot->module_pos = 0;
156 else if (snapshot->module_pos >= snapshot->module_count)
158 set_error( STATUS_NO_MORE_FILES );
159 return 0;
161 ptr = &snapshot->modules[snapshot->module_pos++];
162 req->pid = get_process_id( snapshot->process );
163 req->base = ptr->base;
164 return 1;
167 static void snapshot_dump( struct object *obj, int verbose )
169 struct snapshot *snapshot = (struct snapshot *)obj;
170 assert( obj->ops == &snapshot_ops );
171 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
172 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
175 static void snapshot_destroy( struct object *obj )
177 int i;
178 struct snapshot *snapshot = (struct snapshot *)obj;
179 assert( obj->ops == &snapshot_ops );
180 if (snapshot->process_count)
182 for (i = 0; i < snapshot->process_count; i++)
183 release_object( snapshot->processes[i].process );
184 free( snapshot->processes );
186 if (snapshot->thread_count)
188 for (i = 0; i < snapshot->thread_count; i++)
189 release_object( snapshot->threads[i].thread );
190 free( snapshot->threads );
192 if (snapshot->module_count) free( snapshot->modules );
193 if (snapshot->process) release_object( snapshot->process );
196 /* create a snapshot */
197 DECL_HANDLER(create_snapshot)
199 struct snapshot *snapshot;
201 req->handle = -1;
202 if ((snapshot = create_snapshot( req->pid, req->flags )))
204 req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
205 release_object( snapshot );
209 /* get the next process from a snapshot */
210 DECL_HANDLER(next_process)
212 struct snapshot *snapshot;
214 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
215 0, &snapshot_ops )))
217 snapshot_next_process( snapshot, req );
218 release_object( snapshot );
222 /* get the next thread from a snapshot */
223 DECL_HANDLER(next_thread)
225 struct snapshot *snapshot;
227 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
228 0, &snapshot_ops )))
230 snapshot_next_thread( snapshot, req );
231 release_object( snapshot );
235 /* get the next module from a snapshot */
236 DECL_HANDLER(next_module)
238 struct snapshot *snapshot;
240 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
241 0, &snapshot_ops )))
243 snapshot_next_module( snapshot, req );
244 release_object( snapshot );