snapshot.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Server-side snapshots
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * FIXME: heap snapshots not implemented
  21. */
  22. #include "config.h"
  23. #include "wine/port.h"
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include "ntstatus.h"
  29. #define WIN32_NO_STATUS
  30. #include "windef.h"
  31. #include "winternl.h"
  32. #include "handle.h"
  33. #include "process.h"
  34. #include "thread.h"
  35. #include "request.h"
  36. struct snapshot
  37. {
  38. struct object obj; /* object header */
  39. struct process_snapshot *processes; /* processes snapshot */
  40. int process_count; /* count of processes */
  41. int process_pos; /* current position in proc snapshot */
  42. struct thread_snapshot *threads; /* threads snapshot */
  43. int thread_count; /* count of threads */
  44. int thread_pos; /* current position in thread snapshot */
  45. };
  46. static void snapshot_dump( struct object *obj, int verbose );
  47. static void snapshot_destroy( struct object *obj );
  48. static const struct object_ops snapshot_ops =
  49. {
  50. sizeof(struct snapshot), /* size */
  51. snapshot_dump, /* dump */
  52. no_get_type, /* get_type */
  53. no_add_queue, /* add_queue */
  54. NULL, /* remove_queue */
  55. NULL, /* signaled */
  56. NULL, /* get_esync_fd */
  57. NULL, /* satisfied */
  58. no_signal, /* signal */
  59. no_get_fd, /* get_fd */
  60. no_map_access, /* map_access */
  61. default_get_sd, /* get_sd */
  62. default_set_sd, /* set_sd */
  63. no_lookup_name, /* lookup_name */
  64. no_link_name, /* link_name */
  65. NULL, /* unlink_name */
  66. no_open_file, /* open_file */
  67. no_kernel_obj_list, /* get_kernel_obj_list */
  68. no_alloc_handle, /* alloc_handle */
  69. no_close_handle, /* close_handle */
  70. snapshot_destroy /* destroy */
  71. };
  72. /* create a new snapshot */
  73. static struct snapshot *create_snapshot( unsigned int flags )
  74. {
  75. struct snapshot *snapshot;
  76. if (!(snapshot = alloc_object( &snapshot_ops ))) return NULL;
  77. snapshot->process_pos = 0;
  78. snapshot->process_count = 0;
  79. if (flags & SNAP_PROCESS)
  80. snapshot->processes = process_snap( &snapshot->process_count );
  81. snapshot->thread_pos = 0;
  82. snapshot->thread_count = 0;
  83. if (flags & SNAP_THREAD)
  84. snapshot->threads = thread_snap( &snapshot->thread_count );
  85. return snapshot;
  86. }
  87. /* get the next process in the snapshot */
  88. static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
  89. {
  90. struct process_snapshot *ptr;
  91. struct process_dll *exe_module;
  92. if (!snapshot->process_count)
  93. {
  94. set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
  95. return 0;
  96. }
  97. if (snapshot->process_pos >= snapshot->process_count)
  98. {
  99. set_error( STATUS_NO_MORE_FILES );
  100. return 0;
  101. }
  102. ptr = &snapshot->processes[snapshot->process_pos++];
  103. reply->count = ptr->count;
  104. reply->pid = get_process_id( ptr->process );
  105. reply->ppid = ptr->process->parent_id;
  106. reply->threads = ptr->threads;
  107. reply->priority = ptr->priority;
  108. reply->handles = ptr->handles;
  109. reply->unix_pid = ptr->process->unix_pid;
  110. reply->start_time = ptr->process->start_time;
  111. if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
  112. {
  113. data_size_t len = min( exe_module->namelen, get_reply_max_size() );
  114. set_reply_data( exe_module->filename, len );
  115. }
  116. return 1;
  117. }
  118. /* get the next thread in the snapshot */
  119. static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
  120. {
  121. struct thread_snapshot *ptr;
  122. if (!snapshot->thread_count)
  123. {
  124. set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
  125. return 0;
  126. }
  127. if (snapshot->thread_pos >= snapshot->thread_count)
  128. {
  129. set_error( STATUS_NO_MORE_FILES );
  130. return 0;
  131. }
  132. ptr = &snapshot->threads[snapshot->thread_pos++];
  133. reply->count = ptr->count;
  134. reply->pid = get_process_id( ptr->thread->process );
  135. reply->tid = get_thread_id( ptr->thread );
  136. reply->creation_time = get_thread_creation_time( ptr->thread );
  137. reply->base_pri = ptr->priority;
  138. reply->delta_pri = 0; /* FIXME */
  139. reply->unix_tid = get_thread_unix_tid( ptr->thread );
  140. return 1;
  141. }
  142. static void snapshot_dump( struct object *obj, int verbose )
  143. {
  144. struct snapshot *snapshot = (struct snapshot *)obj;
  145. assert( obj->ops == &snapshot_ops );
  146. fprintf( stderr, "Snapshot: %d procs %d threads\n",
  147. snapshot->process_count, snapshot->thread_count );
  148. }
  149. static void snapshot_destroy( struct object *obj )
  150. {
  151. int i;
  152. struct snapshot *snapshot = (struct snapshot *)obj;
  153. assert( obj->ops == &snapshot_ops );
  154. if (snapshot->process_count)
  155. {
  156. for (i = 0; i < snapshot->process_count; i++)
  157. release_object( snapshot->processes[i].process );
  158. free( snapshot->processes );
  159. }
  160. if (snapshot->thread_count)
  161. {
  162. for (i = 0; i < snapshot->thread_count; i++)
  163. release_object( snapshot->threads[i].thread );
  164. free( snapshot->threads );
  165. }
  166. }
  167. /* create a snapshot */
  168. DECL_HANDLER(create_snapshot)
  169. {
  170. struct snapshot *snapshot;
  171. reply->handle = 0;
  172. if ((snapshot = create_snapshot( req->flags )))
  173. {
  174. reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
  175. release_object( snapshot );
  176. }
  177. }
  178. /* get the next process from a snapshot */
  179. DECL_HANDLER(next_process)
  180. {
  181. struct snapshot *snapshot;
  182. if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
  183. 0, &snapshot_ops )))
  184. {
  185. if (req->reset) snapshot->process_pos = 0;
  186. snapshot_next_process( snapshot, reply );
  187. release_object( snapshot );
  188. }
  189. }
  190. /* get the next thread from a snapshot */
  191. DECL_HANDLER(next_thread)
  192. {
  193. struct snapshot *snapshot;
  194. if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
  195. 0, &snapshot_ops )))
  196. {
  197. if (req->reset) snapshot->thread_pos = 0;
  198. snapshot_next_thread( snapshot, reply );
  199. release_object( snapshot );
  200. }
  201. }