struct _stat layout is different between crtdll and msvcrt.
[wine/multimedia.git] / server / snapshot.c
blobc9a12253f561d1ba3fea1c00927de2989e8ad742
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"
31 #include "tlhelp32.h"
33 #include "handle.h"
34 #include "process.h"
35 #include "thread.h"
36 #include "request.h"
39 struct snapshot
41 struct object obj; /* object header */
42 struct process *process; /* process of this snapshot (for modules and heaps) */
43 struct process_snapshot *processes; /* processes snapshot */
44 int process_count; /* count of processes */
45 int process_pos; /* current position in proc snapshot */
46 struct thread_snapshot *threads; /* threads snapshot */
47 int thread_count; /* count of threads */
48 int thread_pos; /* current position in thread snapshot */
49 struct module_snapshot *modules; /* modules snapshot */
50 int module_count; /* count of modules */
51 int module_pos; /* current position in module snapshot */
54 static void snapshot_dump( struct object *obj, int verbose );
55 static void snapshot_destroy( struct object *obj );
57 static const struct object_ops snapshot_ops =
59 sizeof(struct snapshot), /* size */
60 snapshot_dump, /* dump */
61 no_add_queue, /* add_queue */
62 NULL, /* remove_queue */
63 NULL, /* signaled */
64 NULL, /* satisfied */
65 NULL, /* get_poll_events */
66 NULL, /* poll_event */
67 no_get_fd, /* get_fd */
68 no_flush, /* flush */
69 no_get_file_info, /* get_file_info */
70 NULL, /* queue_async */
71 snapshot_destroy /* destroy */
75 /* create a new snapshot */
76 static struct snapshot *create_snapshot( void *pid, int flags )
78 struct process *process = NULL;
79 struct snapshot *snapshot;
81 /* need a process for modules and heaps */
82 if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
84 if (!pid) process = (struct process *)grab_object( current->process );
85 else if (!(process = get_process_from_id( pid ))) return NULL;
88 if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
90 if (process) release_object( process );
91 return NULL;
94 snapshot->process = process;
96 snapshot->process_pos = 0;
97 snapshot->process_count = 0;
98 if (flags & TH32CS_SNAPPROCESS)
99 snapshot->processes = process_snap( &snapshot->process_count );
101 snapshot->thread_pos = 0;
102 snapshot->thread_count = 0;
103 if (flags & TH32CS_SNAPTHREAD)
104 snapshot->threads = thread_snap( &snapshot->thread_count );
106 snapshot->module_pos = 0;
107 snapshot->module_count = 0;
108 if (flags & TH32CS_SNAPMODULE)
109 snapshot->modules = module_snap( process, &snapshot->module_count );
111 return snapshot;
114 /* get the next process in the snapshot */
115 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
117 struct process_snapshot *ptr;
119 if (!snapshot->process_count)
121 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
122 return 0;
124 if (snapshot->process_pos >= snapshot->process_count)
126 set_error( STATUS_NO_MORE_FILES );
127 return 0;
129 ptr = &snapshot->processes[snapshot->process_pos++];
130 reply->count = ptr->count;
131 reply->pid = get_process_id( ptr->process );
132 reply->ppid = get_process_id( ptr->process->parent );
133 reply->heap = 0; /* FIXME */
134 reply->module = 0; /* FIXME */
135 reply->threads = ptr->threads;
136 reply->priority = ptr->priority;
137 if (ptr->process->exe.filename)
139 size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
140 set_reply_data( ptr->process->exe.filename, len );
142 return 1;
145 /* get the next thread in the snapshot */
146 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
148 struct thread_snapshot *ptr;
150 if (!snapshot->thread_count)
152 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
153 return 0;
155 if (snapshot->thread_pos >= snapshot->thread_count)
157 set_error( STATUS_NO_MORE_FILES );
158 return 0;
160 ptr = &snapshot->threads[snapshot->thread_pos++];
161 reply->count = ptr->count;
162 reply->pid = get_process_id( ptr->thread->process );
163 reply->tid = get_thread_id( ptr->thread );
164 reply->base_pri = ptr->priority;
165 reply->delta_pri = 0; /* FIXME */
166 return 1;
169 /* get the next module in the snapshot */
170 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
172 struct module_snapshot *ptr;
174 if (!snapshot->module_count)
176 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
177 return 0;
179 if (snapshot->module_pos >= snapshot->module_count)
181 set_error( STATUS_NO_MORE_FILES );
182 return 0;
184 ptr = &snapshot->modules[snapshot->module_pos++];
185 reply->pid = get_process_id( snapshot->process );
186 reply->base = ptr->base;
187 reply->size = ptr->size;
188 if (ptr->filename)
190 size_t len = min( ptr->namelen, get_reply_max_size() );
191 set_reply_data( ptr->filename, len );
193 return 1;
196 static void snapshot_dump( struct object *obj, int verbose )
198 struct snapshot *snapshot = (struct snapshot *)obj;
199 assert( obj->ops == &snapshot_ops );
200 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
201 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
204 static void snapshot_destroy( struct object *obj )
206 int i;
207 struct snapshot *snapshot = (struct snapshot *)obj;
208 assert( obj->ops == &snapshot_ops );
209 if (snapshot->process_count)
211 for (i = 0; i < snapshot->process_count; i++)
212 release_object( snapshot->processes[i].process );
213 free( snapshot->processes );
215 if (snapshot->thread_count)
217 for (i = 0; i < snapshot->thread_count; i++)
218 release_object( snapshot->threads[i].thread );
219 free( snapshot->threads );
221 if (snapshot->module_count)
223 for (i = 0; i < snapshot->module_count; i++)
224 free( snapshot->modules[i].filename );
225 free( snapshot->modules );
227 if (snapshot->process) release_object( snapshot->process );
230 /* create a snapshot */
231 DECL_HANDLER(create_snapshot)
233 struct snapshot *snapshot;
235 reply->handle = 0;
236 if ((snapshot = create_snapshot( req->pid, req->flags )))
238 reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
239 release_object( snapshot );
243 /* get the next process from a snapshot */
244 DECL_HANDLER(next_process)
246 struct snapshot *snapshot;
248 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
249 0, &snapshot_ops )))
251 if (req->reset) snapshot->process_pos = 0;
252 snapshot_next_process( snapshot, reply );
253 release_object( snapshot );
257 /* get the next thread from a snapshot */
258 DECL_HANDLER(next_thread)
260 struct snapshot *snapshot;
262 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
263 0, &snapshot_ops )))
265 if (req->reset) snapshot->thread_pos = 0;
266 snapshot_next_thread( snapshot, reply );
267 release_object( snapshot );
271 /* get the next module from a snapshot */
272 DECL_HANDLER(next_module)
274 struct snapshot *snapshot;
276 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
277 0, &snapshot_ops )))
279 if (req->reset) snapshot->module_pos = 0;
280 snapshot_next_module( snapshot, reply );
281 release_object( snapshot );