blob: bc48922236dc16b4b10b077e0e5960a0596c8b11 [file] [log] [blame]
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +00001/*
2 * Server-side snapshots
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * 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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000019 *
Alexandre Julliard07d84462000-04-16 19:45:05 +000020 * FIXME: heap snapshots not implemented
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000021 */
22
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000023#include "config.h"
24#include "wine/port.h"
25
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000026#include <assert.h>
27#include <stdio.h>
28#include <stdlib.h>
Vitaliy Margolen4fabe112006-01-03 12:06:03 +010029#include <stdarg.h>
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000030
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010031#include "ntstatus.h"
32#define WIN32_NO_STATUS
François Gougetecae9262000-12-15 21:30:35 +000033#include "windef.h"
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010034#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000035
36#include "handle.h"
37#include "process.h"
38#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000039#include "request.h"
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000040
41
42struct snapshot
43{
44 struct object obj; /* object header */
Alexandre Julliard07d84462000-04-16 19:45:05 +000045 struct process_snapshot *processes; /* processes snapshot */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000046 int process_count; /* count of processes */
47 int process_pos; /* current position in proc snapshot */
Alexandre Julliard07d84462000-04-16 19:45:05 +000048 struct thread_snapshot *threads; /* threads snapshot */
49 int thread_count; /* count of threads */
50 int thread_pos; /* current position in thread snapshot */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000051};
52
53static void snapshot_dump( struct object *obj, int verbose );
54static void snapshot_destroy( struct object *obj );
55
56static const struct object_ops snapshot_ops =
57{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000058 sizeof(struct snapshot), /* size */
59 snapshot_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010060 no_get_type, /* get_type */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000061 no_add_queue, /* add_queue */
62 NULL, /* remove_queue */
63 NULL, /* signaled */
64 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000065 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000066 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +010067 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +010068 default_get_sd, /* get_sd */
69 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000070 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010071 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000072 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000073 snapshot_destroy /* destroy */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000074};
75
76
77/* create a new snapshot */
Alexandre Julliarddb6e4542008-12-09 11:49:37 +010078static struct snapshot *create_snapshot( unsigned int flags )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000079{
80 struct snapshot *snapshot;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000081
Alexandre Julliarddb6e4542008-12-09 11:49:37 +010082 if (!(snapshot = alloc_object( &snapshot_ops ))) return NULL;
Alexandre Julliard07d84462000-04-16 19:45:05 +000083
84 snapshot->process_pos = 0;
85 snapshot->process_count = 0;
Eric Pouech2359b572003-01-09 00:01:28 +000086 if (flags & SNAP_PROCESS)
Alexandre Julliard07d84462000-04-16 19:45:05 +000087 snapshot->processes = process_snap( &snapshot->process_count );
88
89 snapshot->thread_pos = 0;
90 snapshot->thread_count = 0;
Eric Pouech2359b572003-01-09 00:01:28 +000091 if (flags & SNAP_THREAD)
Alexandre Julliard07d84462000-04-16 19:45:05 +000092 snapshot->threads = thread_snap( &snapshot->thread_count );
93
Alexandre Julliard5bc78081999-06-22 17:26:53 +000094 return snapshot;
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000095}
96
97/* get the next process in the snapshot */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000098static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +000099{
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000100 struct process_snapshot *ptr;
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100101 struct process_dll *exe_module;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000102
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000103 if (!snapshot->process_count)
104 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000105 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000106 return 0;
107 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000108 if (snapshot->process_pos >= snapshot->process_count)
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000109 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000110 set_error( STATUS_NO_MORE_FILES );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000111 return 0;
112 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000113 ptr = &snapshot->processes[snapshot->process_pos++];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000114 reply->count = ptr->count;
115 reply->pid = get_process_id( ptr->process );
Eric Pouech4ecc32a2003-02-14 23:31:10 +0000116 reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000117 reply->threads = ptr->threads;
118 reply->priority = ptr->priority;
Eric Pouech9fd54b22003-09-16 01:07:21 +0000119 reply->handles = ptr->handles;
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100120 if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000121 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200122 data_size_t len = min( exe_module->namelen, get_reply_max_size() );
Alexandre Julliarde6374cb2006-02-16 12:13:01 +0100123 set_reply_data( exe_module->filename, len );
Alexandre Julliardaeb56602002-03-22 00:21:23 +0000124 }
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000125 return 1;
126}
127
Alexandre Julliard07d84462000-04-16 19:45:05 +0000128/* get the next thread in the snapshot */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000129static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
Alexandre Julliard07d84462000-04-16 19:45:05 +0000130{
131 struct thread_snapshot *ptr;
132
133 if (!snapshot->thread_count)
134 {
135 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
136 return 0;
137 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000138 if (snapshot->thread_pos >= snapshot->thread_count)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000139 {
140 set_error( STATUS_NO_MORE_FILES );
141 return 0;
142 }
143 ptr = &snapshot->threads[snapshot->thread_pos++];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000144 reply->count = ptr->count;
145 reply->pid = get_process_id( ptr->thread->process );
146 reply->tid = get_thread_id( ptr->thread );
147 reply->base_pri = ptr->priority;
148 reply->delta_pri = 0; /* FIXME */
Alexandre Julliard07d84462000-04-16 19:45:05 +0000149 return 1;
150}
151
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000152static void snapshot_dump( struct object *obj, int verbose )
153{
154 struct snapshot *snapshot = (struct snapshot *)obj;
155 assert( obj->ops == &snapshot_ops );
Alexandre Julliarddb6e4542008-12-09 11:49:37 +0100156 fprintf( stderr, "Snapshot: %d procs %d threads\n",
157 snapshot->process_count, snapshot->thread_count );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000158}
159
160static void snapshot_destroy( struct object *obj )
161{
162 int i;
163 struct snapshot *snapshot = (struct snapshot *)obj;
164 assert( obj->ops == &snapshot_ops );
165 if (snapshot->process_count)
166 {
167 for (i = 0; i < snapshot->process_count; i++)
Alexandre Julliard07d84462000-04-16 19:45:05 +0000168 release_object( snapshot->processes[i].process );
169 free( snapshot->processes );
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000170 }
Alexandre Julliard07d84462000-04-16 19:45:05 +0000171 if (snapshot->thread_count)
172 {
173 for (i = 0; i < snapshot->thread_count; i++)
174 release_object( snapshot->threads[i].thread );
175 free( snapshot->threads );
176 }
Alexandre Julliardfdc92ba1999-02-14 18:03:15 +0000177}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000178
179/* create a snapshot */
180DECL_HANDLER(create_snapshot)
181{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000182 struct snapshot *snapshot;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000183
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000184 reply->handle = 0;
Alexandre Julliarddb6e4542008-12-09 11:49:37 +0100185 if ((snapshot = create_snapshot( req->flags )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000186 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100187 reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000188 release_object( snapshot );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000189 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000190}
191
192/* get the next process from a snapshot */
193DECL_HANDLER(next_process)
194{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000195 struct snapshot *snapshot;
196
197 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
198 0, &snapshot_ops )))
199 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000200 if (req->reset) snapshot->process_pos = 0;
201 snapshot_next_process( snapshot, reply );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000202 release_object( snapshot );
203 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000204}
Alexandre Julliard07d84462000-04-16 19:45:05 +0000205
206/* get the next thread from a snapshot */
207DECL_HANDLER(next_thread)
208{
209 struct snapshot *snapshot;
210
211 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
212 0, &snapshot_ops )))
213 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000214 if (req->reset) snapshot->thread_pos = 0;
215 snapshot_next_thread( snapshot, reply );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000216 release_object( snapshot );
217 }
218}