Add name_lookup function in object_ops.
[wine.git] / server / snapshot.c
blob5401375a19606dd864b4307be3db4c33a6931687
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 "windef.h"
32 #include "handle.h"
33 #include "process.h"
34 #include "thread.h"
35 #include "request.h"
38 struct snapshot
40 struct object obj; /* object header */
41 struct process *process; /* process of this snapshot (for modules and heaps) */
42 struct process_snapshot *processes; /* processes snapshot */
43 int process_count; /* count of processes */
44 int process_pos; /* current position in proc snapshot */
45 struct thread_snapshot *threads; /* threads snapshot */
46 int thread_count; /* count of threads */
47 int thread_pos; /* current position in thread snapshot */
48 struct module_snapshot *modules; /* modules snapshot */
49 int module_count; /* count of modules */
50 int module_pos; /* current position in module snapshot */
53 static void snapshot_dump( struct object *obj, int verbose );
54 static void snapshot_destroy( struct object *obj );
56 static const struct object_ops snapshot_ops =
58 sizeof(struct snapshot), /* size */
59 snapshot_dump, /* dump */
60 no_add_queue, /* add_queue */
61 NULL, /* remove_queue */
62 NULL, /* signaled */
63 NULL, /* satisfied */
64 no_signal, /* signal */
65 no_get_fd, /* get_fd */
66 no_lookup_name, /* lookup_name */
67 no_close_handle, /* close_handle */
68 snapshot_destroy /* destroy */
72 /* create a new snapshot */
73 static struct snapshot *create_snapshot( process_id_t pid, int flags )
75 struct process *process = NULL;
76 struct snapshot *snapshot;
78 /* need a process for modules and heaps */
79 if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
81 if (!pid) process = (struct process *)grab_object( current->process );
82 else if (!(process = get_process_from_id( pid ))) return NULL;
85 if (!(snapshot = alloc_object( &snapshot_ops )))
87 if (process) release_object( process );
88 return NULL;
91 snapshot->process = process;
93 snapshot->process_pos = 0;
94 snapshot->process_count = 0;
95 if (flags & SNAP_PROCESS)
96 snapshot->processes = process_snap( &snapshot->process_count );
98 snapshot->thread_pos = 0;
99 snapshot->thread_count = 0;
100 if (flags & SNAP_THREAD)
101 snapshot->threads = thread_snap( &snapshot->thread_count );
103 snapshot->module_pos = 0;
104 snapshot->module_count = 0;
105 if (flags & SNAP_MODULE)
106 snapshot->modules = module_snap( process, &snapshot->module_count );
108 return snapshot;
111 /* get the next process in the snapshot */
112 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
114 struct process_snapshot *ptr;
116 if (!snapshot->process_count)
118 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
119 return 0;
121 if (snapshot->process_pos >= snapshot->process_count)
123 set_error( STATUS_NO_MORE_FILES );
124 return 0;
126 ptr = &snapshot->processes[snapshot->process_pos++];
127 reply->count = ptr->count;
128 reply->pid = get_process_id( ptr->process );
129 reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
130 reply->heap = NULL; /* FIXME */
131 reply->module = NULL; /* FIXME */
132 reply->threads = ptr->threads;
133 reply->priority = ptr->priority;
134 reply->handles = ptr->handles;
135 if (ptr->process->exe.filename)
137 size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
138 set_reply_data( ptr->process->exe.filename, len );
140 return 1;
143 /* get the next thread in the snapshot */
144 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
146 struct thread_snapshot *ptr;
148 if (!snapshot->thread_count)
150 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
151 return 0;
153 if (snapshot->thread_pos >= snapshot->thread_count)
155 set_error( STATUS_NO_MORE_FILES );
156 return 0;
158 ptr = &snapshot->threads[snapshot->thread_pos++];
159 reply->count = ptr->count;
160 reply->pid = get_process_id( ptr->thread->process );
161 reply->tid = get_thread_id( ptr->thread );
162 reply->base_pri = ptr->priority;
163 reply->delta_pri = 0; /* FIXME */
164 return 1;
167 /* get the next module in the snapshot */
168 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
170 struct module_snapshot *ptr;
172 if (!snapshot->module_count)
174 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
175 return 0;
177 if (snapshot->module_pos >= snapshot->module_count)
179 set_error( STATUS_NO_MORE_FILES );
180 return 0;
182 ptr = &snapshot->modules[snapshot->module_pos++];
183 reply->pid = get_process_id( snapshot->process );
184 reply->base = ptr->base;
185 reply->size = ptr->size;
186 if (ptr->filename)
188 size_t len = min( ptr->namelen, get_reply_max_size() );
189 set_reply_data( ptr->filename, len );
191 return 1;
194 static void snapshot_dump( struct object *obj, int verbose )
196 struct snapshot *snapshot = (struct snapshot *)obj;
197 assert( obj->ops == &snapshot_ops );
198 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
199 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
202 static void snapshot_destroy( struct object *obj )
204 int i;
205 struct snapshot *snapshot = (struct snapshot *)obj;
206 assert( obj->ops == &snapshot_ops );
207 if (snapshot->process_count)
209 for (i = 0; i < snapshot->process_count; i++)
210 release_object( snapshot->processes[i].process );
211 free( snapshot->processes );
213 if (snapshot->thread_count)
215 for (i = 0; i < snapshot->thread_count; i++)
216 release_object( snapshot->threads[i].thread );
217 free( snapshot->threads );
219 if (snapshot->module_count)
221 for (i = 0; i < snapshot->module_count; i++)
222 free( snapshot->modules[i].filename );
223 free( snapshot->modules );
225 if (snapshot->process) release_object( snapshot->process );
228 /* create a snapshot */
229 DECL_HANDLER(create_snapshot)
231 struct snapshot *snapshot;
233 reply->handle = 0;
234 if ((snapshot = create_snapshot( req->pid, req->flags )))
236 reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
237 release_object( snapshot );
241 /* get the next process from a snapshot */
242 DECL_HANDLER(next_process)
244 struct snapshot *snapshot;
246 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
247 0, &snapshot_ops )))
249 if (req->reset) snapshot->process_pos = 0;
250 snapshot_next_process( snapshot, reply );
251 release_object( snapshot );
255 /* get the next thread from a snapshot */
256 DECL_HANDLER(next_thread)
258 struct snapshot *snapshot;
260 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
261 0, &snapshot_ops )))
263 if (req->reset) snapshot->thread_pos = 0;
264 snapshot_next_thread( snapshot, reply );
265 release_object( snapshot );
269 /* get the next module from a snapshot */
270 DECL_HANDLER(next_module)
272 struct snapshot *snapshot;
274 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
275 0, &snapshot_ops )))
277 if (req->reset) snapshot->module_pos = 0;
278 snapshot_next_module( snapshot, reply );
279 release_object( snapshot );