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 | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 6 | * FIXME: heap snapshots not implemented |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <assert.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 13 | #include "winnt.h" |
| 14 | #include "tlhelp32.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 15 | |
| 16 | #include "handle.h" |
| 17 | #include "process.h" |
| 18 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 19 | #include "request.h" |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | struct snapshot |
| 23 | { |
| 24 | struct object obj; /* object header */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 25 | struct process *process; /* process of this snapshot (for modules and heaps) */ |
| 26 | struct process_snapshot *processes; /* processes snapshot */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 27 | int process_count; /* count of processes */ |
| 28 | int process_pos; /* current position in proc snapshot */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 29 | struct thread_snapshot *threads; /* threads snapshot */ |
| 30 | int thread_count; /* count of threads */ |
| 31 | int thread_pos; /* current position in thread snapshot */ |
| 32 | struct module_snapshot *modules; /* modules snapshot */ |
| 33 | int module_count; /* count of modules */ |
| 34 | int module_pos; /* current position in module snapshot */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static void snapshot_dump( struct object *obj, int verbose ); |
| 38 | static void snapshot_destroy( struct object *obj ); |
| 39 | |
| 40 | static const struct object_ops snapshot_ops = |
| 41 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 42 | sizeof(struct snapshot), /* size */ |
| 43 | snapshot_dump, /* dump */ |
| 44 | no_add_queue, /* add_queue */ |
| 45 | NULL, /* remove_queue */ |
| 46 | NULL, /* signaled */ |
| 47 | NULL, /* satisfied */ |
| 48 | NULL, /* get_poll_events */ |
| 49 | NULL, /* poll_event */ |
| 50 | no_read_fd, /* get_read_fd */ |
| 51 | no_write_fd, /* get_write_fd */ |
| 52 | no_flush, /* flush */ |
| 53 | no_get_file_info, /* get_file_info */ |
| 54 | snapshot_destroy /* destroy */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
| 58 | /* create a new snapshot */ |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 59 | static struct snapshot *create_snapshot( void *pid, int flags ) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 60 | { |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 61 | struct process *process = NULL; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 62 | struct snapshot *snapshot; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 63 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 64 | /* need a process for modules and heaps */ |
| 65 | if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST)) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 66 | { |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 67 | if (!pid) process = (struct process *)grab_object( current->process ); |
| 68 | else if (!(process = get_process_from_id( pid ))) return NULL; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 69 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 70 | |
| 71 | if (!(snapshot = alloc_object( &snapshot_ops, -1 ))) |
| 72 | { |
| 73 | if (process) release_object( process ); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | snapshot->process = process; |
| 78 | |
| 79 | snapshot->process_pos = 0; |
| 80 | snapshot->process_count = 0; |
| 81 | if (flags & TH32CS_SNAPPROCESS) |
| 82 | snapshot->processes = process_snap( &snapshot->process_count ); |
| 83 | |
| 84 | snapshot->thread_pos = 0; |
| 85 | snapshot->thread_count = 0; |
| 86 | if (flags & TH32CS_SNAPTHREAD) |
| 87 | snapshot->threads = thread_snap( &snapshot->thread_count ); |
| 88 | |
| 89 | snapshot->module_pos = 0; |
| 90 | snapshot->module_count = 0; |
| 91 | if (flags & TH32CS_SNAPMODULE) |
| 92 | snapshot->modules = module_snap( process, &snapshot->module_count ); |
| 93 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 94 | return snapshot; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* get the next process in the snapshot */ |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 98 | static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req ) |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 99 | { |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 100 | struct process_snapshot *ptr; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 101 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 102 | if (!snapshot->process_count) |
| 103 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 104 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 107 | if (req->reset) snapshot->process_pos = 0; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 108 | else if (snapshot->process_pos >= snapshot->process_count) |
| 109 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 110 | set_error( STATUS_NO_MORE_FILES ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 113 | ptr = &snapshot->processes[snapshot->process_pos++]; |
| 114 | req->count = ptr->count; |
| 115 | req->pid = get_process_id( ptr->process ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 116 | req->threads = ptr->threads; |
| 117 | req->priority = ptr->priority; |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 118 | return 1; |
| 119 | } |
| 120 | |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 121 | /* get the next thread in the snapshot */ |
| 122 | static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req ) |
| 123 | { |
| 124 | struct thread_snapshot *ptr; |
| 125 | |
| 126 | if (!snapshot->thread_count) |
| 127 | { |
| 128 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 129 | return 0; |
| 130 | } |
| 131 | if (req->reset) snapshot->thread_pos = 0; |
| 132 | else if (snapshot->thread_pos >= snapshot->thread_count) |
| 133 | { |
| 134 | set_error( STATUS_NO_MORE_FILES ); |
| 135 | return 0; |
| 136 | } |
| 137 | ptr = &snapshot->threads[snapshot->thread_pos++]; |
| 138 | req->count = ptr->count; |
| 139 | req->pid = get_process_id( ptr->thread->process ); |
| 140 | req->tid = get_thread_id( ptr->thread ); |
| 141 | req->base_pri = ptr->priority; |
| 142 | req->delta_pri = 0; /* FIXME */ |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | /* get the next module in the snapshot */ |
| 147 | static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req ) |
| 148 | { |
| 149 | struct module_snapshot *ptr; |
| 150 | |
| 151 | if (!snapshot->module_count) |
| 152 | { |
| 153 | set_error( STATUS_INVALID_PARAMETER ); /* FIXME */ |
| 154 | return 0; |
| 155 | } |
| 156 | if (req->reset) snapshot->module_pos = 0; |
| 157 | else if (snapshot->module_pos >= snapshot->module_count) |
| 158 | { |
| 159 | set_error( STATUS_NO_MORE_FILES ); |
| 160 | return 0; |
| 161 | } |
| 162 | ptr = &snapshot->modules[snapshot->module_pos++]; |
| 163 | req->pid = get_process_id( snapshot->process ); |
| 164 | req->base = ptr->base; |
| 165 | return 1; |
| 166 | } |
| 167 | |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 168 | static void snapshot_dump( struct object *obj, int verbose ) |
| 169 | { |
| 170 | struct snapshot *snapshot = (struct snapshot *)obj; |
| 171 | assert( obj->ops == &snapshot_ops ); |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 172 | fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n", |
| 173 | snapshot->process_count, snapshot->thread_count, snapshot->module_count ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void snapshot_destroy( struct object *obj ) |
| 177 | { |
| 178 | int i; |
| 179 | struct snapshot *snapshot = (struct snapshot *)obj; |
| 180 | assert( obj->ops == &snapshot_ops ); |
| 181 | if (snapshot->process_count) |
| 182 | { |
| 183 | for (i = 0; i < snapshot->process_count; i++) |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 184 | release_object( snapshot->processes[i].process ); |
| 185 | free( snapshot->processes ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 186 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 187 | if (snapshot->thread_count) |
| 188 | { |
| 189 | for (i = 0; i < snapshot->thread_count; i++) |
| 190 | release_object( snapshot->threads[i].thread ); |
| 191 | free( snapshot->threads ); |
| 192 | } |
| 193 | if (snapshot->module_count) free( snapshot->modules ); |
| 194 | if (snapshot->process) release_object( snapshot->process ); |
Alexandre Julliard | fdc92ba | 1999-02-14 18:03:15 +0000 | [diff] [blame] | 195 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 196 | |
| 197 | /* create a snapshot */ |
| 198 | DECL_HANDLER(create_snapshot) |
| 199 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 200 | struct snapshot *snapshot; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 201 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 202 | req->handle = -1; |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 203 | if ((snapshot = create_snapshot( req->pid, req->flags ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 204 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 205 | req->handle = alloc_handle( current->process, snapshot, 0, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 206 | release_object( snapshot ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 207 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* get the next process from a snapshot */ |
| 211 | DECL_HANDLER(next_process) |
| 212 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 213 | struct snapshot *snapshot; |
| 214 | |
| 215 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 216 | 0, &snapshot_ops ))) |
| 217 | { |
| 218 | snapshot_next_process( snapshot, req ); |
| 219 | release_object( snapshot ); |
| 220 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 221 | } |
Alexandre Julliard | 07d8446 | 2000-04-16 19:45:05 +0000 | [diff] [blame] | 222 | |
| 223 | /* get the next thread from a snapshot */ |
| 224 | DECL_HANDLER(next_thread) |
| 225 | { |
| 226 | struct snapshot *snapshot; |
| 227 | |
| 228 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 229 | 0, &snapshot_ops ))) |
| 230 | { |
| 231 | snapshot_next_thread( snapshot, req ); |
| 232 | release_object( snapshot ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* get the next module from a snapshot */ |
| 237 | DECL_HANDLER(next_module) |
| 238 | { |
| 239 | struct snapshot *snapshot; |
| 240 | |
| 241 | if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle, |
| 242 | 0, &snapshot_ops ))) |
| 243 | { |
| 244 | snapshot_next_module( snapshot, req ); |
| 245 | release_object( snapshot ); |
| 246 | } |
| 247 | } |