msvcrt: Implemented wctime(), wasctime().
[wine/wine-kai.git] / server / snapshot.c
blob0ef53708a9b33113cf224bb15e45dc9a1da46f71
1 /*
2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FIXME: heap snapshots not implemented
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
35 #include "handle.h"
36 #include "process.h"
37 #include "thread.h"
38 #include "request.h"
41 struct snapshot
43 struct object obj; /* object header */
44 struct process *process; /* process of this snapshot (for modules and heaps) */
45 struct process_snapshot *processes; /* processes snapshot */
46 int process_count; /* count of processes */
47 int process_pos; /* current position in proc snapshot */
48 struct thread_snapshot *threads; /* threads snapshot */
49 int thread_count; /* count of threads */
50 int thread_pos; /* current position in thread snapshot */
51 struct module_snapshot *modules; /* modules snapshot */
52 int module_count; /* count of modules */
53 int module_pos; /* current position in module snapshot */
56 static void snapshot_dump( struct object *obj, int verbose );
57 static void snapshot_destroy( struct object *obj );
59 static const struct object_ops snapshot_ops =
61 sizeof(struct snapshot), /* size */
62 snapshot_dump, /* dump */
63 no_add_queue, /* add_queue */
64 NULL, /* remove_queue */
65 NULL, /* signaled */
66 NULL, /* satisfied */
67 no_signal, /* signal */
68 no_get_fd, /* get_fd */
69 no_map_access, /* map_access */
70 no_lookup_name, /* lookup_name */
71 no_close_handle, /* close_handle */
72 snapshot_destroy /* destroy */
76 /* create a new snapshot */
77 static struct snapshot *create_snapshot( process_id_t pid, int flags )
79 struct process *process = NULL;
80 struct snapshot *snapshot;
82 /* need a process for modules and heaps */
83 if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
85 if (!pid) process = (struct process *)grab_object( current->process );
86 else if (!(process = get_process_from_id( pid ))) return NULL;
89 if (!(snapshot = alloc_object( &snapshot_ops )))
91 if (process) release_object( process );
92 return NULL;
95 snapshot->process = process;
97 snapshot->process_pos = 0;
98 snapshot->process_count = 0;
99 if (flags & SNAP_PROCESS)
100 snapshot->processes = process_snap( &snapshot->process_count );
102 snapshot->thread_pos = 0;
103 snapshot->thread_count = 0;
104 if (flags & SNAP_THREAD)
105 snapshot->threads = thread_snap( &snapshot->thread_count );
107 snapshot->module_pos = 0;
108 snapshot->module_count = 0;
109 if (flags & SNAP_MODULE)
110 snapshot->modules = module_snap( process, &snapshot->module_count );
112 return snapshot;
115 /* get the next process in the snapshot */
116 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
118 struct process_snapshot *ptr;
120 if (!snapshot->process_count)
122 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
123 return 0;
125 if (snapshot->process_pos >= snapshot->process_count)
127 set_error( STATUS_NO_MORE_FILES );
128 return 0;
130 ptr = &snapshot->processes[snapshot->process_pos++];
131 reply->count = ptr->count;
132 reply->pid = get_process_id( ptr->process );
133 reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
134 reply->heap = NULL; /* FIXME */
135 reply->module = NULL; /* FIXME */
136 reply->threads = ptr->threads;
137 reply->priority = ptr->priority;
138 reply->handles = ptr->handles;
139 if (ptr->process->exe.filename)
141 size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
142 set_reply_data( ptr->process->exe.filename, len );
144 return 1;
147 /* get the next thread in the snapshot */
148 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
150 struct thread_snapshot *ptr;
152 if (!snapshot->thread_count)
154 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
155 return 0;
157 if (snapshot->thread_pos >= snapshot->thread_count)
159 set_error( STATUS_NO_MORE_FILES );
160 return 0;
162 ptr = &snapshot->threads[snapshot->thread_pos++];
163 reply->count = ptr->count;
164 reply->pid = get_process_id( ptr->thread->process );
165 reply->tid = get_thread_id( ptr->thread );
166 reply->base_pri = ptr->priority;
167 reply->delta_pri = 0; /* FIXME */
168 return 1;
171 /* get the next module in the snapshot */
172 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
174 struct module_snapshot *ptr;
176 if (!snapshot->module_count)
178 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
179 return 0;
181 if (snapshot->module_pos >= snapshot->module_count)
183 set_error( STATUS_NO_MORE_FILES );
184 return 0;
186 ptr = &snapshot->modules[snapshot->module_pos++];
187 reply->pid = get_process_id( snapshot->process );
188 reply->base = ptr->base;
189 reply->size = ptr->size;
190 if (ptr->filename)
192 size_t len = min( ptr->namelen, get_reply_max_size() );
193 set_reply_data( ptr->filename, len );
195 return 1;
198 static void snapshot_dump( struct object *obj, int verbose )
200 struct snapshot *snapshot = (struct snapshot *)obj;
201 assert( obj->ops == &snapshot_ops );
202 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
203 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
206 static void snapshot_destroy( struct object *obj )
208 int i;
209 struct snapshot *snapshot = (struct snapshot *)obj;
210 assert( obj->ops == &snapshot_ops );
211 if (snapshot->process_count)
213 for (i = 0; i < snapshot->process_count; i++)
214 release_object( snapshot->processes[i].process );
215 free( snapshot->processes );
217 if (snapshot->thread_count)
219 for (i = 0; i < snapshot->thread_count; i++)
220 release_object( snapshot->threads[i].thread );
221 free( snapshot->threads );
223 if (snapshot->module_count)
225 for (i = 0; i < snapshot->module_count; i++)
226 free( snapshot->modules[i].filename );
227 free( snapshot->modules );
229 if (snapshot->process) release_object( snapshot->process );
232 /* create a snapshot */
233 DECL_HANDLER(create_snapshot)
235 struct snapshot *snapshot;
237 reply->handle = 0;
238 if ((snapshot = create_snapshot( req->pid, req->flags )))
240 reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
241 release_object( snapshot );
245 /* get the next process from a snapshot */
246 DECL_HANDLER(next_process)
248 struct snapshot *snapshot;
250 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
251 0, &snapshot_ops )))
253 if (req->reset) snapshot->process_pos = 0;
254 snapshot_next_process( snapshot, reply );
255 release_object( snapshot );
259 /* get the next thread from a snapshot */
260 DECL_HANDLER(next_thread)
262 struct snapshot *snapshot;
264 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
265 0, &snapshot_ops )))
267 if (req->reset) snapshot->thread_pos = 0;
268 snapshot_next_thread( snapshot, reply );
269 release_object( snapshot );
273 /* get the next module from a snapshot */
274 DECL_HANDLER(next_module)
276 struct snapshot *snapshot;
278 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
279 0, &snapshot_ops )))
281 if (req->reset) snapshot->module_pos = 0;
282 snapshot_next_module( snapshot, reply );
283 release_object( snapshot );