1999-02-14 19:03:15 +01:00
|
|
|
/*
|
|
|
|
* Server-side snapshots
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Alexandre Julliard
|
|
|
|
*
|
2000-04-16 21:45:05 +02:00
|
|
|
* FIXME: heap snapshots not implemented
|
1999-02-14 19:03:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-12-15 22:30:35 +01:00
|
|
|
#include "windef.h"
|
1999-02-14 19:03:15 +01:00
|
|
|
#include "tlhelp32.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
|
|
|
#include "handle.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
1999-02-14 19:03:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct snapshot
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2000-04-16 21:45:05 +02:00
|
|
|
struct process *process; /* process of this snapshot (for modules and heaps) */
|
|
|
|
struct process_snapshot *processes; /* processes snapshot */
|
1999-02-14 19:03:15 +01:00
|
|
|
int process_count; /* count of processes */
|
|
|
|
int process_pos; /* current position in proc snapshot */
|
2000-04-16 21:45:05 +02:00
|
|
|
struct thread_snapshot *threads; /* threads snapshot */
|
|
|
|
int thread_count; /* count of threads */
|
|
|
|
int thread_pos; /* current position in thread snapshot */
|
|
|
|
struct module_snapshot *modules; /* modules snapshot */
|
|
|
|
int module_count; /* count of modules */
|
|
|
|
int module_pos; /* current position in module snapshot */
|
1999-02-14 19:03:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void snapshot_dump( struct object *obj, int verbose );
|
|
|
|
static void snapshot_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops snapshot_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct snapshot), /* size */
|
|
|
|
snapshot_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
|
|
|
NULL, /* get_poll_events */
|
|
|
|
NULL, /* poll_event */
|
2000-12-19 03:12:45 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2000-01-01 01:56:27 +01:00
|
|
|
no_flush, /* flush */
|
|
|
|
no_get_file_info, /* get_file_info */
|
|
|
|
snapshot_destroy /* destroy */
|
1999-02-14 19:03:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* create a new snapshot */
|
2000-04-16 21:45:05 +02:00
|
|
|
static struct snapshot *create_snapshot( void *pid, int flags )
|
1999-02-14 19:03:15 +01:00
|
|
|
{
|
2000-04-16 21:45:05 +02:00
|
|
|
struct process *process = NULL;
|
1999-02-14 19:03:15 +01:00
|
|
|
struct snapshot *snapshot;
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2000-04-16 21:45:05 +02:00
|
|
|
/* need a process for modules and heaps */
|
|
|
|
if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
|
1999-06-22 19:26:53 +02:00
|
|
|
{
|
2000-04-16 21:45:05 +02:00
|
|
|
if (!pid) process = (struct process *)grab_object( current->process );
|
|
|
|
else if (!(process = get_process_from_id( pid ))) return NULL;
|
1999-06-22 19:26:53 +02:00
|
|
|
}
|
2000-04-16 21:45:05 +02:00
|
|
|
|
|
|
|
if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
|
|
|
|
{
|
|
|
|
if (process) release_object( process );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot->process = process;
|
|
|
|
|
|
|
|
snapshot->process_pos = 0;
|
|
|
|
snapshot->process_count = 0;
|
|
|
|
if (flags & TH32CS_SNAPPROCESS)
|
|
|
|
snapshot->processes = process_snap( &snapshot->process_count );
|
|
|
|
|
|
|
|
snapshot->thread_pos = 0;
|
|
|
|
snapshot->thread_count = 0;
|
|
|
|
if (flags & TH32CS_SNAPTHREAD)
|
|
|
|
snapshot->threads = thread_snap( &snapshot->thread_count );
|
|
|
|
|
|
|
|
snapshot->module_pos = 0;
|
|
|
|
snapshot->module_count = 0;
|
|
|
|
if (flags & TH32CS_SNAPMODULE)
|
|
|
|
snapshot->modules = module_snap( process, &snapshot->module_count );
|
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
return snapshot;
|
1999-02-14 19:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next process in the snapshot */
|
1999-06-26 10:43:26 +02:00
|
|
|
static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
|
1999-02-14 19:03:15 +01:00
|
|
|
{
|
|
|
|
struct process_snapshot *ptr;
|
1999-06-26 10:43:26 +02:00
|
|
|
|
1999-02-14 19:03:15 +01:00
|
|
|
if (!snapshot->process_count)
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
|
1999-02-14 19:03:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
1999-06-26 10:43:26 +02:00
|
|
|
if (req->reset) snapshot->process_pos = 0;
|
1999-02-14 19:03:15 +01:00
|
|
|
else if (snapshot->process_pos >= snapshot->process_count)
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NO_MORE_FILES );
|
1999-02-14 19:03:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2000-04-16 21:45:05 +02:00
|
|
|
ptr = &snapshot->processes[snapshot->process_pos++];
|
|
|
|
req->count = ptr->count;
|
|
|
|
req->pid = get_process_id( ptr->process );
|
1999-06-26 10:43:26 +02:00
|
|
|
req->threads = ptr->threads;
|
|
|
|
req->priority = ptr->priority;
|
1999-02-14 19:03:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-04-16 21:45:05 +02:00
|
|
|
/* get the next thread in the snapshot */
|
|
|
|
static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
|
|
|
|
{
|
|
|
|
struct thread_snapshot *ptr;
|
|
|
|
|
|
|
|
if (!snapshot->thread_count)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (req->reset) snapshot->thread_pos = 0;
|
|
|
|
else if (snapshot->thread_pos >= snapshot->thread_count)
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MORE_FILES );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ptr = &snapshot->threads[snapshot->thread_pos++];
|
|
|
|
req->count = ptr->count;
|
|
|
|
req->pid = get_process_id( ptr->thread->process );
|
|
|
|
req->tid = get_thread_id( ptr->thread );
|
|
|
|
req->base_pri = ptr->priority;
|
|
|
|
req->delta_pri = 0; /* FIXME */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next module in the snapshot */
|
|
|
|
static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
|
|
|
|
{
|
|
|
|
struct module_snapshot *ptr;
|
|
|
|
|
|
|
|
if (!snapshot->module_count)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (req->reset) snapshot->module_pos = 0;
|
|
|
|
else if (snapshot->module_pos >= snapshot->module_count)
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MORE_FILES );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ptr = &snapshot->modules[snapshot->module_pos++];
|
|
|
|
req->pid = get_process_id( snapshot->process );
|
|
|
|
req->base = ptr->base;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-02-14 19:03:15 +01:00
|
|
|
static void snapshot_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct snapshot *snapshot = (struct snapshot *)obj;
|
|
|
|
assert( obj->ops == &snapshot_ops );
|
2000-04-16 21:45:05 +02:00
|
|
|
fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
|
|
|
|
snapshot->process_count, snapshot->thread_count, snapshot->module_count );
|
1999-02-14 19:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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++)
|
2000-04-16 21:45:05 +02:00
|
|
|
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 );
|
1999-02-14 19:03:15 +01:00
|
|
|
}
|
2000-04-16 21:45:05 +02:00
|
|
|
if (snapshot->module_count) free( snapshot->modules );
|
|
|
|
if (snapshot->process) release_object( snapshot->process );
|
1999-02-14 19:03:15 +01:00
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
|
|
|
|
/* create a snapshot */
|
|
|
|
DECL_HANDLER(create_snapshot)
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
struct snapshot *snapshot;
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2001-01-05 05:08:07 +01:00
|
|
|
req->handle = 0;
|
2000-04-16 21:45:05 +02:00
|
|
|
if ((snapshot = create_snapshot( req->pid, req->flags )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
|
1999-06-22 19:26:53 +02:00
|
|
|
release_object( snapshot );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next process from a snapshot */
|
|
|
|
DECL_HANDLER(next_process)
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
struct snapshot *snapshot;
|
|
|
|
|
|
|
|
if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
|
|
|
|
0, &snapshot_ops )))
|
|
|
|
{
|
|
|
|
snapshot_next_process( snapshot, req );
|
|
|
|
release_object( snapshot );
|
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
2000-04-16 21:45:05 +02:00
|
|
|
|
|
|
|
/* 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 )))
|
|
|
|
{
|
|
|
|
snapshot_next_thread( snapshot, req );
|
|
|
|
release_object( snapshot );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next module from a snapshot */
|
|
|
|
DECL_HANDLER(next_module)
|
|
|
|
{
|
|
|
|
struct snapshot *snapshot;
|
|
|
|
|
|
|
|
if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
|
|
|
|
0, &snapshot_ops )))
|
|
|
|
{
|
|
|
|
snapshot_next_module( snapshot, req );
|
|
|
|
release_object( snapshot );
|
|
|
|
}
|
|
|
|
}
|