ReactOS  0.4.14-dev-52-g6116262
dbgbuffer.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS system libraries
4  * FILE: lib/rtl/dbgbuffer.c
5  * PROGRAMER: James Tabor
6  */
7 
8 /* INCLUDES *****************************************************************/
9 
10 #include <rtl.h>
11 
12 #define NDEBUG
13 #include <debug.h>
14 
15 /* FUNCTIONS *****************************************************************/
16 
17 /*
18  * @unimplemented
19  */
21 NTAPI
23  IN BOOLEAN EventPair)
24 {
27  SIZE_T ViewSize = 100 * PAGE_SIZE;
28 
30  (PVOID*)&Buf,
31  0,
32  &ViewSize,
35  if (!NT_SUCCESS(Status)) return NULL;
36 
37  Buf->ViewBaseClient = Buf;
38  Buf->ViewSize = (ULONG)ViewSize;
39 
40  DPRINT("RtlCQDB: BA: %p BS: 0x%lx\n", Buf->ViewBaseClient, Buf->ViewSize);
41 
42  return Buf;
43 }
44 
45 /*
46  * @unimplemented
47  */
49 NTAPI
51 {
53  SIZE_T ViewSize = 0;
54 
55  if (NULL != Buf)
56  {
58  (PVOID*)&Buf,
59  &ViewSize,
60  MEM_RELEASE);
61  }
62  if (!NT_SUCCESS(Status))
63  {
64  DPRINT1("RtlDQDB: Failed to free VM!\n");
65  }
66  return Status;
67 }
68 
69 /*
70  * Based on lib/epsapi/enum/modules.c by KJK::Hyperion.
71  */
73 NTAPI
77  OUT PULONG ReturnedSize)
78 {
80  PPEB_LDR_DATA ppldLdrData;
81  LDR_DATA_TABLE_ENTRY lmModule;
82  PLIST_ENTRY pleListHead;
83  PLIST_ENTRY pleCurEntry;
84 
87  ULONG UsedSize = sizeof(ULONG);
89  PCHAR p;
90 
91  DPRINT("RtlpQueryRemoteProcessModules Start\n");
92 
93  /* query the process basic information (includes the PEB address) */
96  &pbiInfo,
98  NULL);
99 
100  if (!NT_SUCCESS(Status))
101  {
102  /* failure */
103  DPRINT("NtQueryInformationProcess 1 0x%lx \n", Status);
104  return Status;
105  }
106 
107  if (Modules == NULL || Size == 0)
108  {
110  }
111  else
112  {
113  Modules->NumberOfModules = 0;
114  ModulePtr = &Modules->Modules[0];
116  }
117 
118  /* get the address of the PE Loader data */
120  &(pbiInfo.PebBaseAddress->Ldr),
121  &ppldLdrData,
122  sizeof(ppldLdrData),
123  NULL);
124 
125  if (!NT_SUCCESS(Status))
126  {
127  /* failure */
128  DPRINT("NtReadVirtualMemory 1 0x%lx \n", Status);
129  return Status;
130  }
131 
132 
133  /* head of the module list: the last element in the list will point to this */
134  pleListHead = &ppldLdrData->InLoadOrderModuleList;
135 
136  /* get the address of the first element in the list */
138  &(ppldLdrData->InLoadOrderModuleList.Flink),
139  &pleCurEntry,
140  sizeof(pleCurEntry),
141  NULL);
142 
143  if (!NT_SUCCESS(Status))
144  {
145  /* failure */
146  DPRINT("NtReadVirtualMemory 2 0x%lx \n", Status);
147  return Status;
148  }
149 
150  while(pleCurEntry != pleListHead)
151  {
152  UNICODE_STRING Unicode;
153  WCHAR Buffer[256 * sizeof(WCHAR)];
154 
155  /* read the current module */
157  CONTAINING_RECORD(pleCurEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
158  &lmModule,
159  sizeof(LDR_DATA_TABLE_ENTRY),
160  NULL);
161 
162  if (!NT_SUCCESS(Status))
163  {
164  /* failure */
165  DPRINT( "NtReadVirtualMemory 3 0x%lx \n", Status);
166  return Status;
167  }
168 
169  /* Import module name from remote Process user space. */
170  Unicode.Length = lmModule.FullDllName.Length;
171  Unicode.MaximumLength = lmModule.FullDllName.MaximumLength;
172  Unicode.Buffer = Buffer;
173 
175  lmModule.FullDllName.Buffer,
176  Unicode.Buffer,
177  Unicode.Length,
178  NULL);
179 
180  if (!NT_SUCCESS(Status))
181  {
182  /* failure */
183  DPRINT( "NtReadVirtualMemory 3 0x%lx \n", Status);
184  return Status;
185  }
186 
187  DPRINT(" Module %wZ\n", &Unicode);
188 
189  if (UsedSize > Size)
190  {
192  }
193  else if (Modules != NULL)
194  {
195  ModulePtr->Section = 0;
196  ModulePtr->MappedBase = NULL; // FIXME: ??
197  ModulePtr->ImageBase = lmModule.DllBase;
198  ModulePtr->ImageSize = lmModule.SizeOfImage;
199  ModulePtr->Flags = lmModule.Flags;
200  ModulePtr->LoadOrderIndex = 0; // FIXME: ??
201  ModulePtr->InitOrderIndex = 0; // FIXME: ??
202  ModulePtr->LoadCount = lmModule.LoadCount;
203 
204  AnsiString.Length = 0;
205  AnsiString.MaximumLength = 256;
206  AnsiString.Buffer = ModulePtr->FullPathName;
208  &Unicode,
209  FALSE);
210 
211  p = strrchr(ModulePtr->FullPathName, '\\');
212  if (p != NULL)
213  ModulePtr->OffsetToFileName = (USHORT)(p - ModulePtr->FullPathName + 1);
214  else
215  ModulePtr->OffsetToFileName = 0;
216 
217  ModulePtr++;
218  Modules->NumberOfModules++;
219  }
220  UsedSize += sizeof(RTL_PROCESS_MODULE_INFORMATION);
221 
222  /* address of the next module in the list */
223  pleCurEntry = lmModule.InLoadOrderLinks.Flink;
224  }
225 
226  if (ReturnedSize != 0)
227  *ReturnedSize = UsedSize;
228 
229  DPRINT("RtlpQueryRemoteProcessModules End\n");
230 
231  /* success */
232  return (STATUS_SUCCESS);
233 }
234 
235 /*
236  * @unimplemented
237  */
238 NTSTATUS
239 NTAPI
241  IN ULONG DebugInfoMask,
243 {
245  ULONG Pid = (ULONG)(ULONG_PTR) NtCurrentTeb()->ClientId.UniqueProcess;
246 
247  Buf->Flags = DebugInfoMask;
248  Buf->OffsetFree = sizeof(RTL_DEBUG_INFORMATION);
249 
250  DPRINT("QueryProcessDebugInformation Start\n");
251 
252  /*
253  Currently ROS can not read-only from kenrel space, and doesn't
254  check for boundaries inside kernel space that are page protected
255  from every one but the kernel. aka page 0 - 2
256  */
257  if (ProcessId <= 1)
258  {
260  }
261  else
262  if (Pid == ProcessId)
263  {
264  if (DebugInfoMask & RTL_DEBUG_QUERY_MODULES)
265  {
267  ULONG ReturnSize = 0;
268  ULONG MSize;
269 
270  Mp = (PRTL_PROCESS_MODULES)((PUCHAR)Buf + Buf->OffsetFree);
271 
272  /* I like this better than the do & while loop. */
274  0,
275  &ReturnSize);
277  ReturnSize ,
278  &ReturnSize);
279  if (!NT_SUCCESS(Status))
280  {
281  return Status;
282  }
283 
284  MSize = Mp->NumberOfModules * (sizeof(RTL_PROCESS_MODULES) + 8);
285  Buf->Modules = Mp;
286  Buf->OffsetFree = Buf->OffsetFree + MSize;
287  }
288 
289  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAPS)
290  {
292  ULONG HSize;
293 
294  Hp = (PRTL_PROCESS_HEAPS)((PUCHAR)Buf + Buf->OffsetFree);
295  HSize = sizeof(RTL_PROCESS_HEAPS);
296  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAP_TAGS)
297  {
298  // TODO
299  }
300  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAP_BLOCKS)
301  {
302  // TODO
303  }
304  Buf->Heaps = Hp;
305  Buf->OffsetFree = Buf->OffsetFree + HSize;
306 
307  }
308 
309  if (DebugInfoMask & RTL_DEBUG_QUERY_LOCKS)
310  {
312  ULONG LSize;
313 
314  Lp = (PRTL_PROCESS_LOCKS)((PUCHAR)Buf + Buf->OffsetFree);
315  LSize = sizeof(RTL_PROCESS_LOCKS);
316  Buf->Locks = Lp;
317  Buf->OffsetFree = Buf->OffsetFree + LSize;
318  }
319 
320  DPRINT("QueryProcessDebugInformation end \n");
321  DPRINT("QueryDebugInfo : 0x%lx\n", Buf->OffsetFree);
322  }
323  else
324  {
328 
329  Buf->TargetProcessHandle = NtCurrentProcess();
330 
334  NULL,
335  0,
336  NULL,
337  NULL);
338 
342  &ClientId );
343  if (!NT_SUCCESS(Status))
344  {
345  return Status;
346  }
347 
348  if (DebugInfoMask & RTL_DEBUG_QUERY_MODULES)
349  {
351  ULONG ReturnSize = 0;
352  ULONG MSize;
353 
354  Mp = (PRTL_PROCESS_MODULES)((PUCHAR)Buf + Buf->OffsetFree);
355 
357  NULL,
358  0,
359  &ReturnSize);
360 
362  Mp,
363  ReturnSize ,
364  &ReturnSize);
365  if (!NT_SUCCESS(Status))
366  {
367  return Status;
368  }
369 
370  MSize = Mp->NumberOfModules * (sizeof(RTL_PROCESS_MODULES) + 8);
371  Buf->Modules = Mp;
372  Buf->OffsetFree = Buf->OffsetFree + MSize;
373  }
374 
375  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAPS)
376  {
378  ULONG HSize;
379 
380  Hp = (PRTL_PROCESS_HEAPS)((PUCHAR)Buf + Buf->OffsetFree);
381  HSize = sizeof(RTL_PROCESS_HEAPS);
382  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAP_TAGS)
383  {
384  // TODO
385  }
386  if (DebugInfoMask & RTL_DEBUG_QUERY_HEAP_BLOCKS)
387  {
388  // TODO
389  }
390  Buf->Heaps = Hp;
391  Buf->OffsetFree = Buf->OffsetFree + HSize;
392 
393  }
394 
395  if (DebugInfoMask & RTL_DEBUG_QUERY_LOCKS)
396  {
398  ULONG LSize;
399 
400  Lp = (PRTL_PROCESS_LOCKS)((PUCHAR)Buf + Buf->OffsetFree);
401  LSize = sizeof(RTL_PROCESS_LOCKS);
402  Buf->Locks = Lp;
403  Buf->OffsetFree = Buf->OffsetFree + LSize;
404  }
405 
406  DPRINT("QueryProcessDebugInformation end \n");
407  DPRINT("QueryDebugInfo : 0x%lx\n", Buf->OffsetFree);
408  }
409 
410  return Status;
411 }
412 
413 /* EOL */
signed char * PCHAR
Definition: retypes.h:7
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IN
Definition: typedefs.h:38
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2697
#define RTL_DEBUG_QUERY_LOCKS
Definition: rtltypes.h:367
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define PROCESS_ALL_ACCESS
Definition: nt_native.h:1324
#define RTL_DEBUG_QUERY_HEAP_TAGS
Definition: rtltypes.h:365
USHORT MaximumLength
Definition: env_spec_w32.h:370
LIST_ENTRY InLoadOrderModuleList
Definition: ldrtypes.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
struct _RTL_PROCESS_MODULES * PRTL_PROCESS_MODULES
#define RTL_DEBUG_QUERY_HEAP_BLOCKS
Definition: rtltypes.h:366
LONG NTSTATUS
Definition: precomp.h:26
struct _RTL_DEBUG_INFORMATION RTL_DEBUG_INFORMATION
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
ULONG SizeOfImage
Definition: ldrtypes.h:143
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
HANDLE UniqueProcess
Definition: compat.h:474
struct _RTL_PROCESS_HEAPS RTL_PROCESS_HEAPS
#define MEM_COMMIT
Definition: nt_native.h:1313
NTSTATUS NTAPI NtOpenProcess(OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN PCLIENT_ID ClientId)
Definition: process.c:1440
struct _RTL_PROCESS_MODULES RTL_PROCESS_MODULES
uint32_t ULONG_PTR
Definition: typedefs.h:63
PVOID DllBase
Definition: btrfs_drv.h:1784
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
NTSTATUS NTAPI RtlDestroyQueryDebugBuffer(IN PRTL_DEBUG_INFORMATION Buf)
Definition: dbgbuffer.c:50
#define MEM_RESERVE
Definition: nt_native.h:1314
#define RTL_DEBUG_QUERY_HEAPS
Definition: rtltypes.h:364
PRTL_DEBUG_INFORMATION NTAPI RtlCreateQueryDebugBuffer(IN ULONG Size, IN BOOLEAN EventPair)
Definition: dbgbuffer.c:22
RTL_PROCESS_LOCK_INFORMATION Locks[1]
Definition: rtltypes.h:1151
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
#define RTL_DEBUG_QUERY_MODULES
Definition: rtltypes.h:362
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define NtCurrentProcess()
Definition: nt_native.h:1657
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1176
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI RtlQueryProcessDebugInformation(IN ULONG ProcessId, IN ULONG DebugInfoMask, IN OUT PRTL_DEBUG_INFORMATION Buf)
Definition: dbgbuffer.c:240
PVOID HANDLE
Definition: typedefs.h:71
NTSTATUS NTAPI NtReadVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, OUT PVOID Buffer, IN SIZE_T NumberOfBytesToRead, OUT PSIZE_T NumberOfBytesRead OPTIONAL)
Definition: virtual.c:2691
NTSTATUS NTAPI LdrQueryProcessModuleInformation(IN PRTL_PROCESS_MODULES ModuleInformation, IN ULONG Size, OUT PULONG ReturnedSize OPTIONAL)
Definition: ldrapi.c:1107
HANDLE UniqueThread
Definition: compat.h:475
struct _RTL_PROCESS_MODULE_INFORMATION RTL_PROCESS_MODULE_INFORMATION
Definition: btrfs_drv.h:1780
struct _RTL_PROCESS_LOCKS * PRTL_PROCESS_LOCKS
struct _RTL_PROCESS_LOCKS RTL_PROCESS_LOCKS
struct _RTL_PROCESS_HEAPS * PRTL_PROCESS_HEAPS
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Definition: typedefs.h:117
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
LIST_ENTRY InLoadOrderLinks
Definition: ldrtypes.h:138
ULONG_PTR SIZE_T
Definition: typedefs.h:78
NTSTATUS NTAPI RtlpQueryRemoteProcessModules(HANDLE ProcessHandle, IN PRTL_PROCESS_MODULES Modules OPTIONAL, IN ULONG Size OPTIONAL, OUT PULONG ReturnedSize)
Definition: dbgbuffer.c:74
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4367
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
unsigned short USHORT
Definition: pedump.c:61
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1816
UNICODE_STRING FullDllName
Definition: btrfs_drv.h:1786
unsigned int * PULONG
Definition: retypes.h:1
#define DPRINT1
Definition: precomp.h:8
#define Mp
Definition: i386-dis.c:351
#define MEM_RELEASE
Definition: nt_native.h:1316
#define OUT
Definition: typedefs.h:39
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
ULONG Flags
Definition: ntddk_ex.h:207
unsigned int ULONG
Definition: retypes.h:1
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
RTL_HEAP_INFORMATION Heaps[1]
Definition: rtltypes.h:1131
GLfloat GLfloat p
Definition: glext.h:8902
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
return STATUS_SUCCESS
Definition: btrfs.c:2966
USHORT LoadCount
Definition: ntddk_ex.h:208
#define PAGE_READWRITE
Definition: nt_native.h:1304
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5090
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68