ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
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.
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  *
20  * FIXME: heap snapshots not implemented
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31
32 #include "handle.h"
33 #include "process.h"
34 #include "thread.h"
35 #include "request.h"
36
37
38 struct snapshot
39 {
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 */
51 };
52
53 static void snapshot_dump( struct object *obj, int verbose );
54 static void snapshot_destroy( struct object *obj );
55
56 static const struct object_ops snapshot_ops =
57 {
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     NULL,                         /* get_poll_events */
65     NULL,                         /* poll_event */
66     no_get_fd,                    /* get_fd */
67     no_flush,                     /* flush */
68     no_get_file_info,             /* get_file_info */
69     NULL,                         /* queue_async */
70     snapshot_destroy              /* destroy */
71 };
72
73
74 /* create a new snapshot */
75 static struct snapshot *create_snapshot( process_id_t pid, int flags )
76 {
77     struct process *process = NULL;
78     struct snapshot *snapshot;
79
80     /* need a process for modules and heaps */
81     if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
82     {
83         if (!pid) process = (struct process *)grab_object( current->process );
84         else if (!(process = get_process_from_id( pid ))) return NULL;
85     }
86
87     if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
88     {
89         if (process) release_object( process );
90         return NULL;
91     }
92
93     snapshot->process = process;
94
95     snapshot->process_pos = 0;
96     snapshot->process_count = 0;
97     if (flags & SNAP_PROCESS)
98         snapshot->processes = process_snap( &snapshot->process_count );
99
100     snapshot->thread_pos = 0;
101     snapshot->thread_count = 0;
102     if (flags & SNAP_THREAD)
103         snapshot->threads = thread_snap( &snapshot->thread_count );
104
105     snapshot->module_pos = 0;
106     snapshot->module_count = 0;
107     if (flags & SNAP_MODULE)
108         snapshot->modules = module_snap( process, &snapshot->module_count );
109
110     return snapshot;
111 }
112
113 /* get the next process in the snapshot */
114 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
115 {
116     struct process_snapshot *ptr;
117
118     if (!snapshot->process_count)
119     {
120         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
121         return 0;
122     }
123     if (snapshot->process_pos >= snapshot->process_count)
124     {
125         set_error( STATUS_NO_MORE_FILES );
126         return 0;
127     }
128     ptr = &snapshot->processes[snapshot->process_pos++];
129     reply->count    = ptr->count;
130     reply->pid      = get_process_id( ptr->process );
131     reply->ppid     = get_process_id( ptr->process->parent );
132     reply->heap     = 0;  /* FIXME */
133     reply->module   = 0;  /* FIXME */
134     reply->threads  = ptr->threads;
135     reply->priority = ptr->priority;
136     if (ptr->process->exe.filename)
137     {
138         size_t len = min( ptr->process->exe.namelen, get_reply_max_size() );
139         set_reply_data( ptr->process->exe.filename, len );
140     }
141     return 1;
142 }
143
144 /* get the next thread in the snapshot */
145 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
146 {
147     struct thread_snapshot *ptr;
148
149     if (!snapshot->thread_count)
150     {
151         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
152         return 0;
153     }
154     if (snapshot->thread_pos >= snapshot->thread_count)
155     {
156         set_error( STATUS_NO_MORE_FILES );
157         return 0;
158     }
159     ptr = &snapshot->threads[snapshot->thread_pos++];
160     reply->count     = ptr->count;
161     reply->pid       = get_process_id( ptr->thread->process );
162     reply->tid       = get_thread_id( ptr->thread );
163     reply->base_pri  = ptr->priority;
164     reply->delta_pri = 0;  /* FIXME */
165     return 1;
166 }
167
168 /* get the next module in the snapshot */
169 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
170 {
171     struct module_snapshot *ptr;
172
173     if (!snapshot->module_count)
174     {
175         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
176         return 0;
177     }
178     if (snapshot->module_pos >= snapshot->module_count)
179     {
180         set_error( STATUS_NO_MORE_FILES );
181         return 0;
182     }
183     ptr = &snapshot->modules[snapshot->module_pos++];
184     reply->pid  = get_process_id( snapshot->process );
185     reply->base = ptr->base;
186     reply->size = ptr->size;
187     if (ptr->filename)
188     {
189         size_t len = min( ptr->namelen, get_reply_max_size() );
190         set_reply_data( ptr->filename, len );
191     }
192     return 1;
193 }
194
195 static void snapshot_dump( struct object *obj, int verbose )
196 {
197     struct snapshot *snapshot = (struct snapshot *)obj;
198     assert( obj->ops == &snapshot_ops );
199     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
200              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
201 }
202
203 static void snapshot_destroy( struct object *obj )
204 {
205     int i;
206     struct snapshot *snapshot = (struct snapshot *)obj;
207     assert( obj->ops == &snapshot_ops );
208     if (snapshot->process_count)
209     {
210         for (i = 0; i < snapshot->process_count; i++)
211             release_object( snapshot->processes[i].process );
212         free( snapshot->processes );
213     }
214     if (snapshot->thread_count)
215     {
216         for (i = 0; i < snapshot->thread_count; i++)
217             release_object( snapshot->threads[i].thread );
218         free( snapshot->threads );
219     }
220     if (snapshot->module_count)
221     {
222         for (i = 0; i < snapshot->module_count; i++)
223             free( snapshot->modules[i].filename );
224         free( snapshot->modules );
225     }
226     if (snapshot->process) release_object( snapshot->process );
227 }
228
229 /* create a snapshot */
230 DECL_HANDLER(create_snapshot)
231 {
232     struct snapshot *snapshot;
233
234     reply->handle = 0;
235     if ((snapshot = create_snapshot( req->pid, req->flags )))
236     {
237         reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
238         release_object( snapshot );
239     }
240 }
241
242 /* get the next process from a snapshot */
243 DECL_HANDLER(next_process)
244 {
245     struct snapshot *snapshot;
246
247     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
248                                                        0, &snapshot_ops )))
249     {
250         if (req->reset) snapshot->process_pos = 0;
251         snapshot_next_process( snapshot, reply );
252         release_object( snapshot );
253     }
254 }
255
256 /* get the next thread from a snapshot */
257 DECL_HANDLER(next_thread)
258 {
259     struct snapshot *snapshot;
260
261     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
262                                                        0, &snapshot_ops )))
263     {
264         if (req->reset) snapshot->thread_pos = 0;
265         snapshot_next_thread( snapshot, reply );
266         release_object( snapshot );
267     }
268 }
269
270 /* get the next module from a snapshot */
271 DECL_HANDLER(next_module)
272 {
273     struct snapshot *snapshot;
274
275     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
276                                                        0, &snapshot_ops )))
277     {
278         if (req->reset) snapshot->module_pos = 0;
279         snapshot_next_module( snapshot, reply );
280         release_object( snapshot );
281     }
282 }