snapshot.c 6.93 KB
Newer Older
1 2 3 4 5
/*
 * Server-side snapshots
 *
 * Copyright (C) 1999 Alexandre Julliard
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 *
20
 * FIXME: heap snapshots not implemented
21 22
 */

23 24 25
#include "config.h"
#include "wine/port.h"

26 27 28
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
29
#include <stdarg.h>
30

31 32
#include "ntstatus.h"
#define WIN32_NO_STATUS
33
#include "windef.h"
34
#include "winternl.h"
35 36 37 38

#include "handle.h"
#include "process.h"
#include "thread.h"
39
#include "request.h"
40 41 42 43 44


struct snapshot
{
    struct object             obj;           /* object header */
45
    struct process_snapshot  *processes;     /* processes snapshot */
46 47
    int                       process_count; /* count of processes */
    int                       process_pos;   /* current position in proc snapshot */
48 49 50
    struct thread_snapshot   *threads;       /* threads snapshot */
    int                       thread_count;  /* count of threads */
    int                       thread_pos;    /* current position in thread snapshot */
51 52 53 54 55 56 57
};

static void snapshot_dump( struct object *obj, int verbose );
static void snapshot_destroy( struct object *obj );

static const struct object_ops snapshot_ops =
{
58 59
    sizeof(struct snapshot),      /* size */
    snapshot_dump,                /* dump */
60
    no_get_type,                  /* get_type */
61 62 63 64
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
65
    no_signal,                    /* signal */
66
    no_get_fd,                    /* get_fd */
67
    no_map_access,                /* map_access */
68 69
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
70
    no_lookup_name,               /* lookup_name */
71 72
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
73
    no_open_file,                 /* open_file */
74
    no_kernel_obj_list,           /* get_kernel_obj_list */
75
    no_close_handle,              /* close_handle */
76
    snapshot_destroy              /* destroy */
77 78 79 80
};


/* create a new snapshot */
81
static struct snapshot *create_snapshot( unsigned int flags )
82 83
{
    struct snapshot *snapshot;
84

85
    if (!(snapshot = alloc_object( &snapshot_ops ))) return NULL;
86 87 88

    snapshot->process_pos = 0;
    snapshot->process_count = 0;
89
    if (flags & SNAP_PROCESS)
90 91 92 93
        snapshot->processes = process_snap( &snapshot->process_count );

    snapshot->thread_pos = 0;
    snapshot->thread_count = 0;
94
    if (flags & SNAP_THREAD)
95 96
        snapshot->threads = thread_snap( &snapshot->thread_count );

97
    return snapshot;
98 99 100
}

/* get the next process in the snapshot */
101
static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
102 103
{
    struct process_snapshot *ptr;
104
    struct process_dll *exe_module;
105

106 107
    if (!snapshot->process_count)
    {
108
        set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
109 110
        return 0;
    }
111
    if (snapshot->process_pos >= snapshot->process_count)
112
    {
113
        set_error( STATUS_NO_MORE_FILES );
114 115
        return 0;
    }
116
    ptr = &snapshot->processes[snapshot->process_pos++];
117 118
    reply->count    = ptr->count;
    reply->pid      = get_process_id( ptr->process );
119
    reply->ppid     = ptr->process->parent_id;
120 121
    reply->threads  = ptr->threads;
    reply->priority = ptr->priority;
122
    reply->handles  = ptr->handles;
123
    reply->unix_pid = ptr->process->unix_pid;
124
    if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
125
    {
126
        data_size_t len = min( exe_module->namelen, get_reply_max_size() );
127
        set_reply_data( exe_module->filename, len );
128
    }
129 130 131
    return 1;
}

132
/* get the next thread in the snapshot */
133
static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
134 135 136 137 138 139 140 141
{
    struct thread_snapshot *ptr;

    if (!snapshot->thread_count)
    {
        set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
        return 0;
    }
142
    if (snapshot->thread_pos >= snapshot->thread_count)
143 144 145 146 147
    {
        set_error( STATUS_NO_MORE_FILES );
        return 0;
    }
    ptr = &snapshot->threads[snapshot->thread_pos++];
148 149 150 151 152
    reply->count     = ptr->count;
    reply->pid       = get_process_id( ptr->thread->process );
    reply->tid       = get_thread_id( ptr->thread );
    reply->base_pri  = ptr->priority;
    reply->delta_pri = 0;  /* FIXME */
153 154 155
    return 1;
}

156 157 158 159
static void snapshot_dump( struct object *obj, int verbose )
{
    struct snapshot *snapshot = (struct snapshot *)obj;
    assert( obj->ops == &snapshot_ops );
160 161
    fprintf( stderr, "Snapshot: %d procs %d threads\n",
             snapshot->process_count, snapshot->thread_count );
162 163 164 165 166 167 168 169 170 171
}

static void snapshot_destroy( struct object *obj )
{
    int i;
    struct snapshot *snapshot = (struct snapshot *)obj;
    assert( obj->ops == &snapshot_ops );
    if (snapshot->process_count)
    {
        for (i = 0; i < snapshot->process_count; i++)
172 173 174 175 176 177 178 179
            release_object( snapshot->processes[i].process );
        free( snapshot->processes );
    }
    if (snapshot->thread_count)
    {
        for (i = 0; i < snapshot->thread_count; i++)
            release_object( snapshot->threads[i].thread );
        free( snapshot->threads );
180 181
    }
}
182 183 184 185

/* create a snapshot */
DECL_HANDLER(create_snapshot)
{
186
    struct snapshot *snapshot;
187

188
    reply->handle = 0;
189
    if ((snapshot = create_snapshot( req->flags )))
190
    {
191
        reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
192
        release_object( snapshot );
193 194 195 196 197 198
    }
}

/* get the next process from a snapshot */
DECL_HANDLER(next_process)
{
199 200 201 202 203
    struct snapshot *snapshot;

    if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
                                                       0, &snapshot_ops )))
    {
204 205
        if (req->reset) snapshot->process_pos = 0;
        snapshot_next_process( snapshot, reply );
206 207
        release_object( snapshot );
    }
208
}
209 210 211 212 213 214 215 216 217

/* get the next thread from a snapshot */
DECL_HANDLER(next_thread)
{
    struct snapshot *snapshot;

    if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
                                                       0, &snapshot_ops )))
    {
218 219
        if (req->reset) snapshot->thread_pos = 0;
        snapshot_next_thread( snapshot, reply );
220 221 222
        release_object( snapshot );
    }
}