2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
6 * FIXME: heap snapshots not implemented
24 struct object obj; /* object header */
25 struct process *process; /* process of this snapshot (for modules and heaps) */
26 struct process_snapshot *processes; /* processes snapshot */
27 int process_count; /* count of processes */
28 int process_pos; /* current position in proc snapshot */
29 struct thread_snapshot *threads; /* threads snapshot */
30 int thread_count; /* count of threads */
31 int thread_pos; /* current position in thread snapshot */
32 struct module_snapshot *modules; /* modules snapshot */
33 int module_count; /* count of modules */
34 int module_pos; /* current position in module snapshot */
37 static void snapshot_dump( struct object *obj, int verbose );
38 static void snapshot_destroy( struct object *obj );
40 static const struct object_ops snapshot_ops =
42 sizeof(struct snapshot), /* size */
43 snapshot_dump, /* dump */
44 no_add_queue, /* add_queue */
45 NULL, /* remove_queue */
48 NULL, /* get_poll_events */
49 NULL, /* poll_event */
50 no_get_fd, /* get_fd */
52 no_get_file_info, /* get_file_info */
53 snapshot_destroy /* destroy */
57 /* create a new snapshot */
58 static struct snapshot *create_snapshot( void *pid, int flags )
60 struct process *process = NULL;
61 struct snapshot *snapshot;
63 /* need a process for modules and heaps */
64 if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
66 if (!pid) process = (struct process *)grab_object( current->process );
67 else if (!(process = get_process_from_id( pid ))) return NULL;
70 if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
72 if (process) release_object( process );
76 snapshot->process = process;
78 snapshot->process_pos = 0;
79 snapshot->process_count = 0;
80 if (flags & TH32CS_SNAPPROCESS)
81 snapshot->processes = process_snap( &snapshot->process_count );
83 snapshot->thread_pos = 0;
84 snapshot->thread_count = 0;
85 if (flags & TH32CS_SNAPTHREAD)
86 snapshot->threads = thread_snap( &snapshot->thread_count );
88 snapshot->module_pos = 0;
89 snapshot->module_count = 0;
90 if (flags & TH32CS_SNAPMODULE)
91 snapshot->modules = module_snap( process, &snapshot->module_count );
96 /* get the next process in the snapshot */
97 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
99 struct process_snapshot *ptr;
101 if (!snapshot->process_count)
103 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
106 if (req->reset) snapshot->process_pos = 0;
107 else if (snapshot->process_pos >= snapshot->process_count)
109 set_error( STATUS_NO_MORE_FILES );
112 ptr = &snapshot->processes[snapshot->process_pos++];
113 req->count = ptr->count;
114 req->pid = get_process_id( ptr->process );
115 req->threads = ptr->threads;
116 req->priority = ptr->priority;
120 /* get the next thread in the snapshot */
121 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
123 struct thread_snapshot *ptr;
125 if (!snapshot->thread_count)
127 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
130 if (req->reset) snapshot->thread_pos = 0;
131 else if (snapshot->thread_pos >= snapshot->thread_count)
133 set_error( STATUS_NO_MORE_FILES );
136 ptr = &snapshot->threads[snapshot->thread_pos++];
137 req->count = ptr->count;
138 req->pid = get_process_id( ptr->thread->process );
139 req->tid = get_thread_id( ptr->thread );
140 req->base_pri = ptr->priority;
141 req->delta_pri = 0; /* FIXME */
145 /* get the next module in the snapshot */
146 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
148 struct module_snapshot *ptr;
150 if (!snapshot->module_count)
152 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
155 if (req->reset) snapshot->module_pos = 0;
156 else if (snapshot->module_pos >= snapshot->module_count)
158 set_error( STATUS_NO_MORE_FILES );
161 ptr = &snapshot->modules[snapshot->module_pos++];
162 req->pid = get_process_id( snapshot->process );
163 req->base = ptr->base;
167 static void snapshot_dump( struct object *obj, int verbose )
169 struct snapshot *snapshot = (struct snapshot *)obj;
170 assert( obj->ops == &snapshot_ops );
171 fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
172 snapshot->process_count, snapshot->thread_count, snapshot->module_count );
175 static void snapshot_destroy( struct object *obj )
178 struct snapshot *snapshot = (struct snapshot *)obj;
179 assert( obj->ops == &snapshot_ops );
180 if (snapshot->process_count)
182 for (i = 0; i < snapshot->process_count; i++)
183 release_object( snapshot->processes[i].process );
184 free( snapshot->processes );
186 if (snapshot->thread_count)
188 for (i = 0; i < snapshot->thread_count; i++)
189 release_object( snapshot->threads[i].thread );
190 free( snapshot->threads );
192 if (snapshot->module_count) free( snapshot->modules );
193 if (snapshot->process) release_object( snapshot->process );
196 /* create a snapshot */
197 DECL_HANDLER(create_snapshot)
199 struct snapshot *snapshot;
202 if ((snapshot = create_snapshot( req->pid, req->flags )))
204 req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
205 release_object( snapshot );
209 /* get the next process from a snapshot */
210 DECL_HANDLER(next_process)
212 struct snapshot *snapshot;
214 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
217 snapshot_next_process( snapshot, req );
218 release_object( snapshot );
222 /* get the next thread from a snapshot */
223 DECL_HANDLER(next_thread)
225 struct snapshot *snapshot;
227 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
230 snapshot_next_thread( snapshot, req );
231 release_object( snapshot );
235 /* get the next module from a snapshot */
236 DECL_HANDLER(next_module)
238 struct snapshot *snapshot;
240 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
243 snapshot_next_module( snapshot, req );
244 release_object( snapshot );