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
24 #include "wine/port.h"
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 */
64 no_get_fd
, /* get_fd */
65 snapshot_destroy
/* destroy */
69 /* create a new snapshot */
70 static struct snapshot
*create_snapshot( process_id_t pid
, int flags
)
72 struct process
*process
= NULL
;
73 struct snapshot
*snapshot
;
75 /* need a process for modules and heaps */
76 if (flags
& (SNAP_MODULE
|SNAP_HEAPLIST
))
78 if (!pid
) process
= (struct process
*)grab_object( current
->process
);
79 else if (!(process
= get_process_from_id( pid
))) return NULL
;
82 if (!(snapshot
= alloc_object( &snapshot_ops
, -1 )))
84 if (process
) release_object( process
);
88 snapshot
->process
= process
;
90 snapshot
->process_pos
= 0;
91 snapshot
->process_count
= 0;
92 if (flags
& SNAP_PROCESS
)
93 snapshot
->processes
= process_snap( &snapshot
->process_count
);
95 snapshot
->thread_pos
= 0;
96 snapshot
->thread_count
= 0;
97 if (flags
& SNAP_THREAD
)
98 snapshot
->threads
= thread_snap( &snapshot
->thread_count
);
100 snapshot
->module_pos
= 0;
101 snapshot
->module_count
= 0;
102 if (flags
& SNAP_MODULE
)
103 snapshot
->modules
= module_snap( process
, &snapshot
->module_count
);
108 /* get the next process in the snapshot */
109 static int snapshot_next_process( struct snapshot
*snapshot
, struct next_process_reply
*reply
)
111 struct process_snapshot
*ptr
;
113 if (!snapshot
->process_count
)
115 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
118 if (snapshot
->process_pos
>= snapshot
->process_count
)
120 set_error( STATUS_NO_MORE_FILES
);
123 ptr
= &snapshot
->processes
[snapshot
->process_pos
++];
124 reply
->count
= ptr
->count
;
125 reply
->pid
= get_process_id( ptr
->process
);
126 reply
->ppid
= ptr
->process
->parent
? get_process_id( ptr
->process
->parent
) : 0;
127 reply
->heap
= 0; /* FIXME */
128 reply
->module
= 0; /* FIXME */
129 reply
->threads
= ptr
->threads
;
130 reply
->priority
= ptr
->priority
;
131 if (ptr
->process
->exe
.filename
)
133 size_t len
= min( ptr
->process
->exe
.namelen
, get_reply_max_size() );
134 set_reply_data( ptr
->process
->exe
.filename
, len
);
139 /* get the next thread in the snapshot */
140 static int snapshot_next_thread( struct snapshot
*snapshot
, struct next_thread_reply
*reply
)
142 struct thread_snapshot
*ptr
;
144 if (!snapshot
->thread_count
)
146 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
149 if (snapshot
->thread_pos
>= snapshot
->thread_count
)
151 set_error( STATUS_NO_MORE_FILES
);
154 ptr
= &snapshot
->threads
[snapshot
->thread_pos
++];
155 reply
->count
= ptr
->count
;
156 reply
->pid
= get_process_id( ptr
->thread
->process
);
157 reply
->tid
= get_thread_id( ptr
->thread
);
158 reply
->base_pri
= ptr
->priority
;
159 reply
->delta_pri
= 0; /* FIXME */
163 /* get the next module in the snapshot */
164 static int snapshot_next_module( struct snapshot
*snapshot
, struct next_module_reply
*reply
)
166 struct module_snapshot
*ptr
;
168 if (!snapshot
->module_count
)
170 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
173 if (snapshot
->module_pos
>= snapshot
->module_count
)
175 set_error( STATUS_NO_MORE_FILES
);
178 ptr
= &snapshot
->modules
[snapshot
->module_pos
++];
179 reply
->pid
= get_process_id( snapshot
->process
);
180 reply
->base
= ptr
->base
;
181 reply
->size
= ptr
->size
;
184 size_t len
= min( ptr
->namelen
, get_reply_max_size() );
185 set_reply_data( ptr
->filename
, len
);
190 static void snapshot_dump( struct object
*obj
, int verbose
)
192 struct snapshot
*snapshot
= (struct snapshot
*)obj
;
193 assert( obj
->ops
== &snapshot_ops
);
194 fprintf( stderr
, "Snapshot: %d procs %d threads %d modules\n",
195 snapshot
->process_count
, snapshot
->thread_count
, snapshot
->module_count
);
198 static void snapshot_destroy( struct object
*obj
)
201 struct snapshot
*snapshot
= (struct snapshot
*)obj
;
202 assert( obj
->ops
== &snapshot_ops
);
203 if (snapshot
->process_count
)
205 for (i
= 0; i
< snapshot
->process_count
; i
++)
206 release_object( snapshot
->processes
[i
].process
);
207 free( snapshot
->processes
);
209 if (snapshot
->thread_count
)
211 for (i
= 0; i
< snapshot
->thread_count
; i
++)
212 release_object( snapshot
->threads
[i
].thread
);
213 free( snapshot
->threads
);
215 if (snapshot
->module_count
)
217 for (i
= 0; i
< snapshot
->module_count
; i
++)
218 free( snapshot
->modules
[i
].filename
);
219 free( snapshot
->modules
);
221 if (snapshot
->process
) release_object( snapshot
->process
);
224 /* create a snapshot */
225 DECL_HANDLER(create_snapshot
)
227 struct snapshot
*snapshot
;
230 if ((snapshot
= create_snapshot( req
->pid
, req
->flags
)))
232 reply
->handle
= alloc_handle( current
->process
, snapshot
, 0, req
->inherit
);
233 release_object( snapshot
);
237 /* get the next process from a snapshot */
238 DECL_HANDLER(next_process
)
240 struct snapshot
*snapshot
;
242 if ((snapshot
= (struct snapshot
*)get_handle_obj( current
->process
, req
->handle
,
245 if (req
->reset
) snapshot
->process_pos
= 0;
246 snapshot_next_process( snapshot
, reply
);
247 release_object( snapshot
);
251 /* get the next thread from a snapshot */
252 DECL_HANDLER(next_thread
)
254 struct snapshot
*snapshot
;
256 if ((snapshot
= (struct snapshot
*)get_handle_obj( current
->process
, req
->handle
,
259 if (req
->reset
) snapshot
->thread_pos
= 0;
260 snapshot_next_thread( snapshot
, reply
);
261 release_object( snapshot
);
265 /* get the next module from a snapshot */
266 DECL_HANDLER(next_module
)
268 struct snapshot
*snapshot
;
270 if ((snapshot
= (struct snapshot
*)get_handle_obj( current
->process
, req
->handle
,
273 if (req
->reset
) snapshot
->module_pos
= 0;
274 snapshot_next_module( snapshot
, reply
);
275 release_object( snapshot
);