Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side snapshots |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
| 5 | * |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 6 | * 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 |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 20 | * FIXME: heap snapshots not implemented |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | #include "wine/port.h" |
| 25 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | |
François Gouget | ecae926 | 2000-12-15 21:30:35 +0000 | [diff] [blame] | 30 | #include "windef.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 31 | |
| 32 | #include "handle.h" |
| 33 | #include "process.h" |
| 34 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 35 | #include "request.h" |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | struct snapshot |
| 39 | { |
| 40 | struct object obj; /* object header */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 41 | struct process *process; /* process of this snapshot (for modules and heaps) */ |
| 42 | struct process_snapshot *processes; /* processes snapshot */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 43 | int process_count; /* count of processes */ |
| 44 | int process_pos; /* current position in proc snapshot */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 45 | struct thread_snapshot *threads; /* threads snapshot */ |
| 46 | int thread_count; /* count of threads */ |
| 47 | int thread_pos; /* current position in thread snapshot */ |
| 48 | struct module_snapshot *modules; /* modules snapshot */ |
| 49 | int module_count; /* count of modules */ |
| 50 | int module_pos; /* current position in module snapshot */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static void snapshot_dump( struct object *obj, int verbose ); |
| 54 | static void snapshot_destroy( struct object *obj ); |
| 55 | |
| 56 | static const struct object_ops snapshot_ops = |
| 57 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 58 | sizeof(struct snapshot), /* size */ |
| 59 | snapshot_dump, /* dump */ |
| 60 | no_add_queue, /* add_queue */ |
| 61 | NULL, /* remove_queue */ |
| 62 | NULL, /* signaled */ |
| 63 | NULL, /* satisfied */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 64 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 65 | snapshot_destroy /* destroy */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | |
| 69 | /* create a new snapshot */ |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 70 | static struct snapshot *create_snapshot( process_id_t pid, int flags ) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 71 | { |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 72 | struct process *process = NULL; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 73 | struct snapshot *snapshot; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 74 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 75 | /* need a process for modules and heaps */ |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 76 | if (flags & (SNAP_MODULE|SNAP_HEAPLIST)) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 77 | { |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 78 | if (!pid) process = (struct process *)grab_object( current->process ); |
| 79 | else if (!(process = get_process_from_id( pid ))) return NULL; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 80 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 81 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 82 | if (!(snapshot = alloc_object( &snapshot_ops ))) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 83 | { |
| 84 | if (process) release_object( process ); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | snapshot->process = process; |
| 89 | |
| 90 | snapshot->process_pos = 0; |
| 91 | snapshot->process_count = 0; |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 92 | if (flags & SNAP_PROCESS) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 93 | snapshot->processes = process_snap( &snapshot->process_count ); |
| 94 | |
| 95 | snapshot->thread_pos = 0; |
| 96 | snapshot->thread_count = 0; |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 97 | if (flags & SNAP_THREAD) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 98 | snapshot->threads = thread_snap( &snapshot->thread_count ); |
| 99 | |
| 100 | snapshot->module_pos = 0; |
| 101 | snapshot->module_count = 0; |
Eric Pouech | 2359b57 | 2003-01-09 00:01:28 +0000 | [diff] [blame] | 102 | if (flags & SNAP_MODULE) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 103 | snapshot->modules = module_snap( process, &snapshot->module_count ); |
| 104 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 105 | return snapshot; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* get the next process in the snapshot */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 109 | static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply ) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 110 | { |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 111 | struct process_snapshot *ptr; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 112 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 113 | if (!snapshot->process_count) |
| 114 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 115 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 118 | if (snapshot->process_pos >= snapshot->process_count) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 119 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 120 | set_error( STATUS_NO_MORE_FILES ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 123 | ptr = &snapshot->processes[snapshot->process_pos++]; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 124 | reply->count = ptr->count; |
| 125 | reply->pid = get_process_id( ptr->process ); |
Eric Pouech | 4ecc32a | 2003-02-14 23:31:10 +0000 | [diff] [blame] | 126 | reply->ppid = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 127 | reply->heap = 0; /* FIXME */ |
| 128 | reply->module = 0; /* FIXME */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 129 | reply->threads = ptr->threads; |
| 130 | reply->priority = ptr->priority; |
Eric Pouech | 9fd54b2 | 2003-09-16 01:07:21 +0000 | [diff] [blame] | 131 | reply->handles = ptr->handles; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 132 | if (ptr->process->exe.filename) |
| 133 | { |
| 134 | size_t len = min( ptr->process->exe.namelen, get_reply_max_size() ); |
| 135 | set_reply_data( ptr->process->exe.filename, len ); |
| 136 | } |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 140 | /* get the next thread in the snapshot */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 141 | static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply ) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 142 | { |
| 143 | struct thread_snapshot *ptr; |
| 144 | |
| 145 | if (!snapshot->thread_count) |
| 146 | { |
| 147 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 148 | return 0; |
| 149 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 150 | if (snapshot->thread_pos >= snapshot->thread_count) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 151 | { |
| 152 | set_error( STATUS_NO_MORE_FILES ); |
| 153 | return 0; |
| 154 | } |
| 155 | ptr = &snapshot->threads[snapshot->thread_pos++]; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 156 | reply->count = ptr->count; |
| 157 | reply->pid = get_process_id( ptr->thread->process ); |
| 158 | reply->tid = get_thread_id( ptr->thread ); |
| 159 | reply->base_pri = ptr->priority; |
| 160 | reply->delta_pri = 0; /* FIXME */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | /* get the next module in the snapshot */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 165 | static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply ) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 166 | { |
| 167 | struct module_snapshot *ptr; |
| 168 | |
| 169 | if (!snapshot->module_count) |
| 170 | { |
| 171 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 172 | return 0; |
| 173 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 174 | if (snapshot->module_pos >= snapshot->module_count) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 175 | { |
| 176 | set_error( STATUS_NO_MORE_FILES ); |
| 177 | return 0; |
| 178 | } |
| 179 | ptr = &snapshot->modules[snapshot->module_pos++]; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 180 | reply->pid = get_process_id( snapshot->process ); |
| 181 | reply->base = ptr->base; |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 182 | reply->size = ptr->size; |
| 183 | if (ptr->filename) |
| 184 | { |
| 185 | size_t len = min( ptr->namelen, get_reply_max_size() ); |
| 186 | set_reply_data( ptr->filename, len ); |
| 187 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 191 | static void snapshot_dump( struct object *obj, int verbose ) |
| 192 | { |
| 193 | struct snapshot *snapshot = (struct snapshot *)obj; |
| 194 | assert( obj->ops == &snapshot_ops ); |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 195 | fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n", |
| 196 | snapshot->process_count, snapshot->thread_count, snapshot->module_count ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void snapshot_destroy( struct object *obj ) |
| 200 | { |
| 201 | int i; |
| 202 | struct snapshot *snapshot = (struct snapshot *)obj; |
| 203 | assert( obj->ops == &snapshot_ops ); |
| 204 | if (snapshot->process_count) |
| 205 | { |
| 206 | for (i = 0; i < snapshot->process_count; i++) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 207 | release_object( snapshot->processes[i].process ); |
| 208 | free( snapshot->processes ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 209 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 210 | if (snapshot->thread_count) |
| 211 | { |
| 212 | for (i = 0; i < snapshot->thread_count; i++) |
| 213 | release_object( snapshot->threads[i].thread ); |
| 214 | free( snapshot->threads ); |
| 215 | } |
Alexandre Julliard | aeb5660 | 2002-03-22 00:21:23 +0000 | [diff] [blame] | 216 | if (snapshot->module_count) |
| 217 | { |
| 218 | for (i = 0; i < snapshot->module_count; i++) |
| 219 | free( snapshot->modules[i].filename ); |
| 220 | free( snapshot->modules ); |
| 221 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 222 | if (snapshot->process) release_object( snapshot->process ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 223 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 224 | |
| 225 | /* create a snapshot */ |
| 226 | DECL_HANDLER(create_snapshot) |
| 227 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 228 | struct snapshot *snapshot; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 229 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 230 | reply->handle = 0; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 231 | if ((snapshot = create_snapshot( req->pid, req->flags ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 232 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 233 | reply->handle = alloc_handle( current->process, snapshot, 0, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 234 | release_object( snapshot ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 235 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* get the next process from a snapshot */ |
| 239 | DECL_HANDLER(next_process) |
| 240 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 241 | struct snapshot *snapshot; |
| 242 | |
| 243 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 244 | 0, &snapshot_ops ))) |
| 245 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 246 | if (req->reset) snapshot->process_pos = 0; |
| 247 | snapshot_next_process( snapshot, reply ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 248 | release_object( snapshot ); |
| 249 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 250 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 251 | |
| 252 | /* get the next thread from a snapshot */ |
| 253 | DECL_HANDLER(next_thread) |
| 254 | { |
| 255 | struct snapshot *snapshot; |
| 256 | |
| 257 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 258 | 0, &snapshot_ops ))) |
| 259 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 260 | if (req->reset) snapshot->thread_pos = 0; |
| 261 | snapshot_next_thread( snapshot, reply ); |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 262 | release_object( snapshot ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* get the next module from a snapshot */ |
| 267 | DECL_HANDLER(next_module) |
| 268 | { |
| 269 | struct snapshot *snapshot; |
| 270 | |
| 271 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 272 | 0, &snapshot_ops ))) |
| 273 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 274 | if (req->reset) snapshot->module_pos = 0; |
| 275 | snapshot_next_module( snapshot, reply ); |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 276 | release_object( snapshot ); |
| 277 | } |
| 278 | } |