In CBPaintText use the text size as returned by LB_GETTEXT. The size
[wine.git] / server / snapshot.c
blob9bd05dfa120c0c12d7b850230d5605dca1e1e63d
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 NULL, /* queue_async */
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_reply *reply )
100 struct process_snapshot *ptr;
102 if (!snapshot->process_count)
104 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
105 return 0;
107 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 reply->count = ptr->count;
114 reply->pid = get_process_id( ptr->process );
115 reply->threads = ptr->threads;
116 reply->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_reply *reply )
123 struct thread_snapshot *ptr;
125 if (!snapshot->thread_count)
127 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
128 return 0;
130 if (snapshot->thread_pos >= snapshot->thread_count)
132 set_error( STATUS_NO_MORE_FILES );
133 return 0;
135 ptr = &snapshot->threads[snapshot->thread_pos++];
136 reply->count = ptr->count;
137 reply->pid = get_process_id( ptr->thread->process );
138 reply->tid = get_thread_id( ptr->thread );
139 reply->base_pri = ptr->priority;
140 reply->delta_pri = 0; /* FIXME */
141 return 1;
144 /* get the next module in the snapshot */
145 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
147 struct module_snapshot *ptr;
149 if (!snapshot->module_count)
151 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
152 return 0;
154 if (snapshot->module_pos >= snapshot->module_count)
156 set_error( STATUS_NO_MORE_FILES );
157 return 0;
159 ptr = &snapshot->modules[snapshot->module_pos++];
160 reply->pid = get_process_id( snapshot->process );
161 reply->base = ptr->base;
162 return 1;
165 static void snapshot_dump( struct object *obj, int verbose )
167 struct snapshot *snapshot = (struct snapshot *)obj;
168 assert( obj->ops == &snapshot_ops );
169 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
170 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
173 static void snapshot_destroy( struct object *obj )
175 int i;
176 struct snapshot *snapshot = (struct snapshot *)obj;
177 assert( obj->ops == &snapshot_ops );
178 if (snapshot->process_count)
180 for (i = 0; i < snapshot->process_count; i++)
181 release_object( snapshot->processes[i].process );
182 free( snapshot->processes );
184 if (snapshot->thread_count)
186 for (i = 0; i < snapshot->thread_count; i++)
187 release_object( snapshot->threads[i].thread );
188 free( snapshot->threads );
190 if (snapshot->module_count) free( snapshot->modules );
191 if (snapshot->process) release_object( snapshot->process );
194 /* create a snapshot */
195 DECL_HANDLER(create_snapshot)
197 struct snapshot *snapshot;
199 reply->handle = 0;
200 if ((snapshot = create_snapshot( req->pid, req->flags )))
202 reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
203 release_object( snapshot );
207 /* get the next process from a snapshot */
208 DECL_HANDLER(next_process)
210 struct snapshot *snapshot;
212 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
213 0, &snapshot_ops )))
215 if (req->reset) snapshot->process_pos = 0;
216 snapshot_next_process( snapshot, reply );
217 release_object( snapshot );
221 /* get the next thread from a snapshot */
222 DECL_HANDLER(next_thread)
224 struct snapshot *snapshot;
226 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
227 0, &snapshot_ops )))
229 if (req->reset) snapshot->thread_pos = 0;
230 snapshot_next_thread( snapshot, reply );
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 if (req->reset) snapshot->module_pos = 0;
244 snapshot_next_module( snapshot, reply );
245 release_object( snapshot );