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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * FIXME: heap snapshots not implemented
24 #include "wine/port.h"
32 #define WIN32_NO_STATUS
44 struct object obj; /* object header */
45 struct process *process; /* process of this snapshot (for modules and heaps) */
46 struct process_snapshot *processes; /* processes snapshot */
47 int process_count; /* count of processes */
48 int process_pos; /* current position in proc snapshot */
49 struct thread_snapshot *threads; /* threads snapshot */
50 int thread_count; /* count of threads */
51 int thread_pos; /* current position in thread snapshot */
52 struct module_snapshot *modules; /* modules snapshot */
53 int module_count; /* count of modules */
54 int module_pos; /* current position in module snapshot */
57 static void snapshot_dump( struct object *obj, int verbose );
58 static void snapshot_destroy( struct object *obj );
60 static const struct object_ops snapshot_ops =
62 sizeof(struct snapshot), /* size */
63 snapshot_dump, /* dump */
64 no_add_queue, /* add_queue */
65 NULL, /* remove_queue */
68 no_signal, /* signal */
69 no_get_fd, /* get_fd */
70 no_map_access, /* map_access */
71 default_get_sd, /* get_sd */
72 default_set_sd, /* set_sd */
73 no_lookup_name, /* lookup_name */
74 no_open_file, /* open_file */
75 no_close_handle, /* close_handle */
76 snapshot_destroy /* destroy */
80 /* create a new snapshot */
81 static struct snapshot *create_snapshot( process_id_t pid, int flags )
83 struct process *process = NULL;
84 struct snapshot *snapshot;
86 /* need a process for modules and heaps */
87 if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
89 if (!pid) process = (struct process *)grab_object( current->process );
90 else if (!(process = get_process_from_id( pid ))) return NULL;
93 if (!(snapshot = alloc_object( &snapshot_ops )))
95 if (process) release_object( process );
99 snapshot->process = process;
101 snapshot->process_pos = 0;
102 snapshot->process_count = 0;
103 if (flags & SNAP_PROCESS)
104 snapshot->processes = process_snap( &snapshot->process_count );
106 snapshot->thread_pos = 0;
107 snapshot->thread_count = 0;
108 if (flags & SNAP_THREAD)
109 snapshot->threads = thread_snap( &snapshot->thread_count );
111 snapshot->module_pos = 0;
112 snapshot->module_count = 0;
113 if (flags & SNAP_MODULE)
114 snapshot->modules = module_snap( process, &snapshot->module_count );
119 /* get the next process in the snapshot */
120 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
122 struct process_snapshot *ptr;
123 struct process_dll *exe_module;
125 if (!snapshot->process_count)
127 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
130 if (snapshot->process_pos >= snapshot->process_count)
132 set_error( STATUS_NO_MORE_FILES );
135 ptr = &snapshot->processes[snapshot->process_pos++];
136 reply->count = ptr->count;
137 reply->pid = get_process_id( ptr->process );
138 reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
139 reply->heap = NULL; /* FIXME */
140 reply->module = NULL; /* FIXME */
141 reply->threads = ptr->threads;
142 reply->priority = ptr->priority;
143 reply->handles = ptr->handles;
144 if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
146 data_size_t len = min( exe_module->namelen, get_reply_max_size() );
147 set_reply_data( exe_module->filename, len );
152 /* get the next thread in the snapshot */
153 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
155 struct thread_snapshot *ptr;
157 if (!snapshot->thread_count)
159 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
162 if (snapshot->thread_pos >= snapshot->thread_count)
164 set_error( STATUS_NO_MORE_FILES );
167 ptr = &snapshot->threads[snapshot->thread_pos++];
168 reply->count = ptr->count;
169 reply->pid = get_process_id( ptr->thread->process );
170 reply->tid = get_thread_id( ptr->thread );
171 reply->base_pri = ptr->priority;
172 reply->delta_pri = 0; /* FIXME */
176 /* get the next module in the snapshot */
177 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
179 struct module_snapshot *ptr;
181 if (!snapshot->module_count)
183 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
186 if (snapshot->module_pos >= snapshot->module_count)
188 set_error( STATUS_NO_MORE_FILES );
191 ptr = &snapshot->modules[snapshot->module_pos++];
192 reply->pid = get_process_id( snapshot->process );
193 reply->base = ptr->base;
194 reply->size = ptr->size;
197 data_size_t len = min( ptr->namelen, get_reply_max_size() );
198 set_reply_data( ptr->filename, len );
203 static void snapshot_dump( struct object *obj, int verbose )
205 struct snapshot *snapshot = (struct snapshot *)obj;
206 assert( obj->ops == &snapshot_ops );
207 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
208 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
211 static void snapshot_destroy( struct object *obj )
214 struct snapshot *snapshot = (struct snapshot *)obj;
215 assert( obj->ops == &snapshot_ops );
216 if (snapshot->process_count)
218 for (i = 0; i < snapshot->process_count; i++)
219 release_object( snapshot->processes[i].process );
220 free( snapshot->processes );
222 if (snapshot->thread_count)
224 for (i = 0; i < snapshot->thread_count; i++)
225 release_object( snapshot->threads[i].thread );
226 free( snapshot->threads );
228 if (snapshot->module_count)
230 for (i = 0; i < snapshot->module_count; i++)
231 free( snapshot->modules[i].filename );
232 free( snapshot->modules );
234 if (snapshot->process) release_object( snapshot->process );
237 /* create a snapshot */
238 DECL_HANDLER(create_snapshot)
240 struct snapshot *snapshot;
243 if ((snapshot = create_snapshot( req->pid, req->flags )))
245 reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
246 release_object( snapshot );
250 /* get the next process from a snapshot */
251 DECL_HANDLER(next_process)
253 struct snapshot *snapshot;
255 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
258 if (req->reset) snapshot->process_pos = 0;
259 snapshot_next_process( snapshot, reply );
260 release_object( snapshot );
264 /* get the next thread from a snapshot */
265 DECL_HANDLER(next_thread)
267 struct snapshot *snapshot;
269 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
272 if (req->reset) snapshot->thread_pos = 0;
273 snapshot_next_thread( snapshot, reply );
274 release_object( snapshot );
278 /* get the next module from a snapshot */
279 DECL_HANDLER(next_module)
281 struct snapshot *snapshot;
283 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
286 if (req->reset) snapshot->module_pos = 0;
287 snapshot_next_module( snapshot, reply );
288 release_object( snapshot );