blob: 6c71b759431cf5d18faf51cf82f338e929dcbd3c [file] [log] [blame]
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +00001/*
2 * Server-side snapshots
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * 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.
10 *
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.
15 *
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
19 *
Alexandre Julliard07d84462000-04-16 19:45:05 +000020 * FIXME: heap snapshots not implemented
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000021 */
22
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000023#include "config.h"
24#include "wine/port.h"
25
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000026#include <assert.h>
27#include <stdio.h>
28#include <stdlib.h>
29
François Gougetecae9262000-12-15 21:30:35 +000030#include "windef.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000031
32#include "handle.h"
33#include "process.h"
34#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000035#include "request.h"
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000036
37
38struct snapshot
39{
40 struct object obj; /* object header */
Alexandre Julliard07d84462000-04-16 19:45:05 +000041 struct process *process; /* process of this snapshot (for modules and heaps) */
42 struct process_snapshot *processes; /* processes snapshot */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000043 int process_count; /* count of processes */
44 int process_pos; /* current position in proc snapshot */
Alexandre Julliard07d84462000-04-16 19:45:05 +000045 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 */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000051};
52
53static void snapshot_dump( struct object *obj, int verbose );
54static void snapshot_destroy( struct object *obj );
55
56static const struct object_ops snapshot_ops =
57{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000058 sizeof(struct snapshot), /* size */
59 snapshot_dump, /* dump */
60 no_add_queue, /* add_queue */
61 NULL, /* remove_queue */
62 NULL, /* signaled */
63 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000064 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000065 no_get_fd, /* get_fd */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000066 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000067 snapshot_destroy /* destroy */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000068};
69
70
71/* create a new snapshot */
Alexandre Julliard54f22872002-10-03 19:54:57 +000072static struct snapshot *create_snapshot( process_id_t pid, int flags )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000073{
Alexandre Julliard07d84462000-04-16 19:45:05 +000074 struct process *process = NULL;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000075 struct snapshot *snapshot;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000076
Alexandre Julliard07d84462000-04-16 19:45:05 +000077 /* need a process for modules and heaps */
Eric Pouech2359b572003-01-09 00:01:28 +000078 if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
Alexandre Julliard5bc78081999-06-22 17:26:53 +000079 {
Alexandre Julliard07d84462000-04-16 19:45:05 +000080 if (!pid) process = (struct process *)grab_object( current->process );
81 else if (!(process = get_process_from_id( pid ))) return NULL;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000082 }
Alexandre Julliard07d84462000-04-16 19:45:05 +000083
Alexandre Julliarde66207e2003-02-19 00:33:32 +000084 if (!(snapshot = alloc_object( &snapshot_ops )))
Alexandre Julliard07d84462000-04-16 19:45:05 +000085 {
86 if (process) release_object( process );
87 return NULL;
88 }
89
90 snapshot->process = process;
91
92 snapshot->process_pos = 0;
93 snapshot->process_count = 0;
Eric Pouech2359b572003-01-09 00:01:28 +000094 if (flags & SNAP_PROCESS)
Alexandre Julliard07d84462000-04-16 19:45:05 +000095 snapshot->processes = process_snap( &snapshot->process_count );
96
97 snapshot->thread_pos = 0;
98 snapshot->thread_count = 0;
Eric Pouech2359b572003-01-09 00:01:28 +000099 if (flags & SNAP_THREAD)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000100 snapshot->threads = thread_snap( &snapshot->thread_count );
101
102 snapshot->module_pos = 0;
103 snapshot->module_count = 0;
Eric Pouech2359b572003-01-09 00:01:28 +0000104 if (flags & SNAP_MODULE)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000105 snapshot->modules = module_snap( process, &snapshot->module_count );
106
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000107 return snapshot;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000108}
109
110/* get the next process in the snapshot */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000111static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000112{
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000113 struct process_snapshot *ptr;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000114
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000115 if (!snapshot->process_count)
116 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000117 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000118 return 0;
119 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000120 if (snapshot->process_pos >= snapshot->process_count)
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000121 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000122 set_error( STATUS_NO_MORE_FILES );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000123 return 0;
124 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000125 ptr = &snapshot->processes[snapshot->process_pos++];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000126 reply->count = ptr->count;
127 reply->pid = get_process_id( ptr->process );
Eric Pouech4ecc32a2003-02-14 23:31:10 +0000128 reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
Robert Shearmanc5165712005-05-25 18:41:09 +0000129 reply->heap = NULL; /* FIXME */
130 reply->module = NULL; /* FIXME */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000131 reply->threads = ptr->threads;
132 reply->priority = ptr->priority;
Eric Pouech9fd54b22003-09-16 01:07:21 +0000133 reply->handles = ptr->handles;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000134 if (ptr->process->exe.filename)
135 {
136 size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
137 set_reply_data( ptr->process->exe.filename, len );
138 }
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000139 return 1;
140}
141
Alexandre Julliard07d84462000-04-16 19:45:05 +0000142/* get the next thread in the snapshot */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000143static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
Alexandre Julliard07d84462000-04-16 19:45:05 +0000144{
145 struct thread_snapshot *ptr;
146
147 if (!snapshot->thread_count)
148 {
149 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
150 return 0;
151 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000152 if (snapshot->thread_pos >= snapshot->thread_count)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000153 {
154 set_error( STATUS_NO_MORE_FILES );
155 return 0;
156 }
157 ptr = &snapshot->threads[snapshot->thread_pos++];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000158 reply->count = ptr->count;
159 reply->pid = get_process_id( ptr->thread->process );
160 reply->tid = get_thread_id( ptr->thread );
161 reply->base_pri = ptr->priority;
162 reply->delta_pri = 0; /* FIXME */
Alexandre Julliard07d84462000-04-16 19:45:05 +0000163 return 1;
164}
165
166/* get the next module in the snapshot */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000167static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
Alexandre Julliard07d84462000-04-16 19:45:05 +0000168{
169 struct module_snapshot *ptr;
170
171 if (!snapshot->module_count)
172 {
173 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
174 return 0;
175 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000176 if (snapshot->module_pos >= snapshot->module_count)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000177 {
178 set_error( STATUS_NO_MORE_FILES );
179 return 0;
180 }
181 ptr = &snapshot->modules[snapshot->module_pos++];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000182 reply->pid = get_process_id( snapshot->process );
183 reply->base = ptr->base;
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000184 reply->size = ptr->size;
185 if (ptr->filename)
186 {
187 size_t len = min( ptr->namelen, get_reply_max_size() );
188 set_reply_data( ptr->filename, len );
189 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000190 return 1;
191}
192
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000193static void snapshot_dump( struct object *obj, int verbose )
194{
195 struct snapshot *snapshot = (struct snapshot *)obj;
196 assert( obj->ops == &snapshot_ops );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000197 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
198 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000199}
200
201static void snapshot_destroy( struct object *obj )
202{
203 int i;
204 struct snapshot *snapshot = (struct snapshot *)obj;
205 assert( obj->ops == &snapshot_ops );
206 if (snapshot->process_count)
207 {
208 for (i = 0; i < snapshot->process_count; i++)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000209 release_object( snapshot->processes[i].process );
210 free( snapshot->processes );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000211 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000212 if (snapshot->thread_count)
213 {
214 for (i = 0; i < snapshot->thread_count; i++)
215 release_object( snapshot->threads[i].thread );
216 free( snapshot->threads );
217 }
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000218 if (snapshot->module_count)
219 {
220 for (i = 0; i < snapshot->module_count; i++)
221 free( snapshot->modules[i].filename );
222 free( snapshot->modules );
223 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000224 if (snapshot->process) release_object( snapshot->process );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000225}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000226
227/* create a snapshot */
228DECL_HANDLER(create_snapshot)
229{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000230 struct snapshot *snapshot;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000231
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000232 reply->handle = 0;
Alexandre Julliard07d84462000-04-16 19:45:05 +0000233 if ((snapshot = create_snapshot( req->pid, req->flags )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000234 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000235 reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000236 release_object( snapshot );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000237 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000238}
239
240/* get the next process from a snapshot */
241DECL_HANDLER(next_process)
242{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000243 struct snapshot *snapshot;
244
245 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
246 0, &snapshot_ops )))
247 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000248 if (req->reset) snapshot->process_pos = 0;
249 snapshot_next_process( snapshot, reply );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000250 release_object( snapshot );
251 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000252}
Alexandre Julliard07d84462000-04-16 19:45:05 +0000253
254/* get the next thread from a snapshot */
255DECL_HANDLER(next_thread)
256{
257 struct snapshot *snapshot;
258
259 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
260 0, &snapshot_ops )))
261 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000262 if (req->reset) snapshot->thread_pos = 0;
263 snapshot_next_thread( snapshot, reply );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000264 release_object( snapshot );
265 }
266}
267
268/* get the next module from a snapshot */
269DECL_HANDLER(next_module)
270{
271 struct snapshot *snapshot;
272
273 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
274 0, &snapshot_ops )))
275 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000276 if (req->reset) snapshot->module_pos = 0;
277 snapshot_next_module( snapshot, reply );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000278 release_object( snapshot );
279 }
280}