No longer directly accessing debuggee memory.
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * FIXME: only process snapshots implemented for now
7  */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "winnt.h"
14 #include "tlhelp32.h"
15
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
20
21
22 struct snapshot
23 {
24     struct object             obj;           /* object header */
25     struct process_snapshot  *process;       /* processes snapshot */
26     int                       process_count; /* count of processes */
27     int                       process_pos;   /* current position in proc snapshot */
28 };
29
30 static void snapshot_dump( struct object *obj, int verbose );
31 static void snapshot_destroy( struct object *obj );
32
33 static const struct object_ops snapshot_ops =
34 {
35     sizeof(struct snapshot),      /* size */
36     snapshot_dump,                /* dump */
37     no_add_queue,                 /* add_queue */
38     NULL,                         /* remove_queue */
39     NULL,                         /* signaled */
40     NULL,                         /* satisfied */
41     NULL,                         /* get_poll_events */
42     NULL,                         /* poll_event */
43     no_read_fd,                   /* get_read_fd */
44     no_write_fd,                  /* get_write_fd */
45     no_flush,                     /* flush */
46     no_get_file_info,             /* get_file_info */
47     snapshot_destroy              /* destroy */
48 };
49
50
51 /* create a new snapshot */
52 static struct snapshot *create_snapshot( int flags )
53 {
54     struct snapshot *snapshot;
55
56     if ((snapshot = alloc_object( &snapshot_ops, -1 )))
57     {
58         if (flags & TH32CS_SNAPPROCESS)
59             snapshot->process = process_snap( &snapshot->process_count );
60         else
61             snapshot->process_count = 0;
62         snapshot->process_pos = 0;
63     }
64     return snapshot;
65 }
66
67 /* get the next process in the snapshot */
68 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
69 {
70     struct process_snapshot *ptr;
71
72     if (!snapshot->process_count)
73     {
74         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
75         return 0;
76     }
77     if (req->reset) snapshot->process_pos = 0;
78     else if (snapshot->process_pos >= snapshot->process_count)
79     {
80         set_error( STATUS_NO_MORE_FILES );
81         return 0;
82     }
83     ptr = &snapshot->process[snapshot->process_pos++];
84     req->pid      = ptr->process;
85     req->threads  = ptr->threads;
86     req->priority = ptr->priority;
87     return 1;
88 }
89
90 static void snapshot_dump( struct object *obj, int verbose )
91 {
92     struct snapshot *snapshot = (struct snapshot *)obj;
93     assert( obj->ops == &snapshot_ops );
94     fprintf( stderr, "Snapshot: %d processes\n",
95              snapshot->process_count );
96 }
97
98 static void snapshot_destroy( struct object *obj )
99 {
100     int i;
101     struct snapshot *snapshot = (struct snapshot *)obj;
102     assert( obj->ops == &snapshot_ops );
103     if (snapshot->process_count)
104     {
105         for (i = 0; i < snapshot->process_count; i++)
106             release_object( snapshot->process[i].process );
107         free( snapshot->process );
108     }
109 }
110
111 /* create a snapshot */
112 DECL_HANDLER(create_snapshot)
113 {
114     struct snapshot *snapshot;
115
116     req->handle = -1;
117     if ((snapshot = create_snapshot( req->flags )))
118     {
119         req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
120         release_object( snapshot );
121     }
122 }
123
124 /* get the next process from a snapshot */
125 DECL_HANDLER(next_process)
126 {
127     struct snapshot *snapshot;
128
129     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
130                                                        0, &snapshot_ops )))
131     {
132         snapshot_next_process( snapshot, req );
133         release_object( snapshot );
134     }
135 }