ReactOS  r75619
kdinit.c File Reference
#include <ntoskrnl.h>
#include <reactos/buildno.h>
#include <debug.h>
Include dependency graph for kdinit.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static SIZE_T INIT_FUNCTION KdpGetMemorySizeInMBs (IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
VOID NTAPI KdUpdateDataBlock (VOID)
 
BOOLEAN NTAPI KdRegisterDebuggerDataBlock (IN ULONG Tag, IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader, IN ULONG Size)
 
BOOLEAN NTAPI KdInitSystem (IN ULONG BootPhase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 

Macro Definition Documentation

#define NDEBUG

Definition at line 14 of file kdinit.c.

Function Documentation

BOOLEAN NTAPI KdInitSystem ( IN ULONG  BootPhase,
IN PLOADER_PARAMETER_BLOCK  LoaderBlock 
)

Definition at line 125 of file kdinit.c.

127 {
128  BOOLEAN EnableKd, DisableKdAfterInit = FALSE, BlockEnable;
129  LPSTR CommandLine, DebugLine, DebugOptionStart, DebugOptionEnd;
131  PLDR_DATA_TABLE_ENTRY LdrEntry;
132  PLIST_ENTRY NextEntry;
133  ULONG i, j, Length;
134  SIZE_T DebugOptionLength;
135  SIZE_T MemSizeMBs;
136  CHAR NameBuffer[256];
137  PWCHAR Name;
138 
139 #if defined(__GNUC__)
140  /* Make gcc happy */
141  BlockEnable = FALSE;
142 #endif
143 
144  /* Check if this is Phase 1 */
145  if (BootPhase)
146  {
147  /* Just query the performance counter */
149  return TRUE;
150  }
151 
152  /* Check if we already initialized once */
153  if (KdDebuggerEnabled) return TRUE;
154 
155  /* Set the Debug Routine as the Stub for now */
157 
158  /* Disable break after symbol load for now */
160 
161  /* Check if the Debugger Data Block was already initialized */
163  {
164  /* It wasn't...Initialize the KD Data Listhead */
166 
167  /* Register the Debugger Data Block */
170  sizeof(KdDebuggerDataBlock));
171 
172  /* Fill out the KD Version Block */
175 
176 #ifdef CONFIG_SMP
177  /* This is an MP Build */
179 #endif
180 
181  /* Save Pointers to Loaded Module List and Debugger Data */
184 
185  /* Set protocol limits */
190  KdVersionBlock.Unused[0] = 0;
191 
192  /* Link us in the KPCR */
193  KeGetPcr()->KdVersionBlock = &KdVersionBlock;
194  }
195 
196  /* Check if we have a loader block */
197  if (LoaderBlock)
198  {
199  /* Get the image entry */
200  LdrEntry = CONTAINING_RECORD(LoaderBlock->LoadOrderListHead.Flink,
202  InLoadOrderLinks);
203 
204  /* Save the Kernel Base */
205  PsNtosImageBase = (ULONG_PTR)LdrEntry->DllBase;
207 
208  /* Check if we have a command line */
209  CommandLine = LoaderBlock->LoadOptions;
210  if (CommandLine)
211  {
212  /* Upcase it */
213  _strupr(CommandLine);
214 
215  /* Assume we'll disable KD */
216  EnableKd = FALSE;
217 
218  /* Check for CRASHDEBUG, NODEBUG and just DEBUG */
219  if (strstr(CommandLine, "CRASHDEBUG"))
220  {
221  /* Don't enable KD now, but allow it to be enabled later */
223  }
224  else if (strstr(CommandLine, "NODEBUG"))
225  {
226  /* Don't enable KD and don't let it be enabled later */
228  }
229  else if ((DebugLine = strstr(CommandLine, "DEBUG")) != NULL)
230  {
231  /* Enable KD */
232  EnableKd = TRUE;
233 
234  /* Check if there are any options */
235  if (DebugLine[5] == '=')
236  {
237  /* Save pointers */
238  DebugOptionStart = DebugOptionEnd = &DebugLine[6];
239 
240  /* Scan the string for debug options */
241  for (;;)
242  {
243  /* Loop until we reach the end of the string */
244  while (*DebugOptionEnd != ANSI_NULL)
245  {
246  /* Check if this is a comma, a space or a tab */
247  if ((*DebugOptionEnd == ',') ||
248  (*DebugOptionEnd == ' ') ||
249  (*DebugOptionEnd == '\t'))
250  {
251  /*
252  * We reached the end of the option or
253  * the end of the string, break out
254  */
255  break;
256  }
257  else
258  {
259  /* Move on to the next character */
260  DebugOptionEnd++;
261  }
262  }
263 
264  /* Calculate the length of the current option */
265  DebugOptionLength = (DebugOptionEnd - DebugOptionStart);
266 
267  /*
268  * Break out if we reached the last option
269  * or if there were no options at all
270  */
271  if (!DebugOptionLength) break;
272 
273  /* Now check which option this is */
274  if ((DebugOptionLength == 10) &&
275  !(strncmp(DebugOptionStart, "AUTOENABLE", 10)))
276  {
277  /*
278  * Disable the debugger, but
279  * allow it to be reenabled
280  */
281  DisableKdAfterInit = TRUE;
282  BlockEnable = FALSE;
284  }
285  else if ((DebugOptionLength == 7) &&
286  !(strncmp(DebugOptionStart, "DISABLE", 7)))
287  {
288  /* Disable the debugger */
289  DisableKdAfterInit = TRUE;
290  BlockEnable = TRUE;
292  }
293  else if ((DebugOptionLength == 6) &&
294  !(strncmp(DebugOptionStart, "NOUMEX", 6)))
295  {
296  /* Ignore user mode exceptions */
298  }
299 
300  /*
301  * If there are more options then
302  * the next character should be a comma
303  */
304  if (*DebugOptionEnd != ',')
305  {
306  /* It isn't, break out */
307  break;
308  }
309 
310  /* Move on to the next option */
311  DebugOptionEnd++;
312  DebugOptionStart = DebugOptionEnd;
313  }
314  }
315  }
316  }
317  else
318  {
319  /* No command line options? Disable debugger by default */
321  EnableKd = FALSE;
322  }
323  }
324  else
325  {
326  /* Called from a bugcheck or a re-enable. Save the Kernel Base */
328 
329  /* Unconditionally enable KD */
330  EnableKd = TRUE;
331  }
332 
333  /* Set the Kernel Base in the Data Block */
335 
336  /* Initialize the debugger if requested */
337  if (EnableKd && (NT_SUCCESS(KdDebuggerInitialize0(LoaderBlock))))
338  {
339  /* Now set our real KD routine */
341 
342  /* Check if we've already initialized our structures */
344  {
345  /* Set the Debug Switch Routine and Retries */
348 
349  /* Initialize breakpoints owed flag and table */
351  for (i = 0; i < KD_BREAKPOINT_MAX; i++)
352  {
356  }
357 
358  /* Initialize the Time Slip DPC */
362 
363  /* First-time initialization done! */
365  }
366 
367  /* Initialize the timer */
369 
370  /* Officially enable KD */
373 
374  /* Let user-mode know that it's enabled as well */
375  SharedUserData->KdDebuggerEnabled = TRUE;
376 
377  /* Display separator + ReactOS version at start of the debug log */
378  DPRINT1("-----------------------------------------------------\n");
379  DPRINT1("ReactOS "KERNEL_VERSION_STR" (Build "KERNEL_VERSION_BUILD_STR")\n");
380  MemSizeMBs = KdpGetMemorySizeInMBs(KeLoaderBlock);
381  DPRINT1("%u System Processor [%u MB Memory]\n", KeNumberProcessors, MemSizeMBs);
382  if (KeLoaderBlock)
383  {
384  DPRINT1("Command Line: %s\n", KeLoaderBlock->LoadOptions);
385  DPRINT1("ARC Paths: %s %s %s %s\n", KeLoaderBlock->ArcBootDeviceName,
389  }
390 
391  /* Check if the debugger should be disabled initially */
392  if (DisableKdAfterInit)
393  {
394  /* Disable it */
396 
397  /*
398  * Save the enable block state and return initialized
399  * (the debugger is active but disabled).
400  */
401  KdBlockEnable = BlockEnable;
402  return TRUE;
403  }
404 
405  /* Check if we have a loader block */
406  if (LoaderBlock)
407  {
408  /* Loop boot images */
409  NextEntry = LoaderBlock->LoadOrderListHead.Flink;
410  i = 0;
411  while ((NextEntry != &LoaderBlock->LoadOrderListHead) && (i < 2))
412  {
413  /* Get the image entry */
414  LdrEntry = CONTAINING_RECORD(NextEntry,
416  InLoadOrderLinks);
417 
418  /* Generate the image name */
419  Name = LdrEntry->FullDllName.Buffer;
420  Length = LdrEntry->FullDllName.Length / sizeof(WCHAR);
421  j = 0;
422  do
423  {
424  /* Do cheap Unicode to ANSI conversion */
425  NameBuffer[j++] = (CHAR)*Name++;
426  } while (j < Length);
427 
428  /* Null-terminate */
429  NameBuffer[j] = ANSI_NULL;
430 
431  /* Load symbols for image */
432  RtlInitString(&ImageName, NameBuffer);
433  DbgLoadImageSymbols(&ImageName,
434  LdrEntry->DllBase,
436 
437  /* Go to the next entry */
438  NextEntry = NextEntry->Flink;
439  i++;
440  }
441  }
442 
443  /* Check for incoming breakin and break on symbol load if we have it */
445  }
446  else
447  {
448  /* Disable debugger */
450  }
451 
452  /* Return initialized */
453  return TRUE;
454 }
NTSTATUS NTAPI KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
Definition: kdcom.c:147
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
GLenum GLclampf GLint GLenum GLuint GLenum GLenum GLsizei GLenum const GLvoid GLfloat GLfloat GLfloat GLfloat GLclampd GLint GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean GLboolean GLboolean GLboolean GLint GLenum GLsizei const GLvoid GLenum GLint GLenum GLint GLint GLsizei GLint GLenum GLint GLint GLint GLint GLsizei GLenum GLsizei const GLuint GLboolean GLenum GLenum GLint GLsizei GLenum GLsizei GLenum const GLvoid GLboolean const GLboolean GLenum const GLdouble const GLfloat const GLdouble const GLfloat GLenum GLint GLint GLint GLint GLint GLint j
Definition: glfuncs.h:98
#define TRUE
Definition: types.h:120
DBGKD_GET_VERSION64 KdVersionBlock
Definition: kddata.c:371
PSTR ArcBootDeviceName
Definition: arc.h:503
BOOLEAN KdpDebuggerStructuresInitialized
Definition: kddata.c:88
#define DbgKdMaximumStateChange
Definition: windbgkd.h:62
PKDEBUG_ROUTINE KiDebugRoutine
Definition: kdmain.c:471
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
char CHAR
Definition: xmlstorage.h:175
BOOLEAN KdAutoEnableOnEvent
Definition: kddata.c:84
BOOLEAN NTAPI KdpStub(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
Definition: kdtrap.c:271
BOOLEAN KdBlockEnable
Definition: kddata.c:85
uint16_t * PWCHAR
Definition: typedefs.h:55
ULONG64 KernBase
Definition: wdbgexts.h:180
char * LPSTR
Definition: xmlstorage.h:182
#define WCHAR
Definition: msvc.h:43
PKDEBUG_SWITCH_ROUTINE KiDebugSwitchRoutine
Definition: kddata.c:75
#define KeGetPcr()
Definition: ke.h:25
uint32_t ULONG_PTR
Definition: typedefs.h:64
PVOID DllBase
Definition: ldrtypes.h:139
LARGE_INTEGER KdTimerStart
Definition: kd64.h:546
#define DbgKdMaximumManipulate
Definition: windbgkd.h:117
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define FALSE
Definition: types.h:117
#define ANSI_NULL
ULONG_PTR DirectoryTableBase
Definition: kd64.h:49
LARGE_INTEGER KdPerformanceCounterRate
Definition: kddata.c:91
uint64_t ULONG64
Definition: typedefs.h:66
BOOLEAN KdpOweBreakpoint
Definition: kddata.c:98
ULONG_PTR PsNtosImageBase
Definition: sysldr.c:38
BOOLEAN NTAPI KdPollBreakIn(VOID)
Definition: kdmain.c:337
static SIZE_T INIT_FUNCTION KdpGetMemorySizeInMBs(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: kdinit.c:31
smooth NULL
Definition: ftsmooth.c:513
WORK_QUEUE_ITEM KdpTimeSlipWorkItem
Definition: kddata.c:118
BOOLEAN KdDebuggerEnabled
Definition: kdmain.c:16
VOID NTAPI KeInitializeTimer(OUT PKTIMER Timer)
Definition: timerobj.c:233
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
KD_CONTEXT KdpContext
Definition: kdmain.c:23
ULONG Flags
Definition: kd64.h:48
#define KDBG_TAG
Definition: wdbgexts.h:28
struct _LIST_ENTRY * Flink
Definition: typedefs.h:120
unsigned char BOOLEAN
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
if(!(yy_init))
Definition: macro.lex.yy.c:704
NTSTATUS NTAPI KdDisableDebuggerWithLock(IN BOOLEAN NeedLock)
Definition: kdapi.c:2035
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
ULONG64 PsLoadedModuleList
Definition: wdbgexts.h:160
#define DbgKdMinimumManipulate
Definition: windbgkd.h:73
KDDEBUGGER_DATA64 * KdDebuggerDataBlock
Definition: kdpacket.c:21
USHORT Unused[1]
Definition: wdbgexts.h:158
ULONG NtBuildNumber
Definition: init.c:47
std::wstring STRING
Definition: fontsub.cpp:33
_In_ PUNICODE_STRING Name
Definition: mrx.h:218
#define SharedUserData
static const char * ImageName
Definition: image.c:34
VOID UINTN Length
Definition: acefiex.h:744
Definition: ntddk_ex.h:202
#define KD_BREAKPOINT_MAX
Definition: kd64.h:21
Definition: typedefs.h:118
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
#define DbgKdMinimumStateChange
Definition: windbgkd.h:58
NTSYSAPI VOID NTAPI RtlInitString(PSTRING DestinationString, PCSZ SourceString)
BOOLEAN KdPitchDebugger
Definition: kdmain.c:21
KDPC KdpTimeSlipDpc
Definition: kddata.c:116
BOOLEAN NTAPI KdpTrap(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
Definition: kdtrap.c:135
ULONG_PTR SIZE_T
Definition: typedefs.h:79
#define NT_SUCCESS(StatCode)
Definition: cmd.c:149
_CRTIMP char *__cdecl _strupr(_Inout_z_ char *_String)
unsigned short USHORT
Definition: pedump.c:61
LIST_ENTRY KdpDebuggerDataListHead
Definition: kddata.c:365
#define DBGKD_VERS_FLAG_MP
Definition: wdbgexts.h:21
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
BOOLEAN KdIgnoreUmExceptions
Definition: kdmain.c:22
UNICODE_STRING FullDllName
Definition: ldrtypes.h:142
BREAKPOINT_ENTRY KdpBreakpointTable[KD_BREAKPOINT_MAX]
Definition: kddata.c:96
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
VOID NTAPI KdpTimeSlipDpcRoutine(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: kdapi.c:1785
VOID NTAPI KdpTimeSlipWork(IN PVOID Context)
Definition: kdapi.c:1811
VOID NTAPI DbgLoadImageSymbols(_In_ PSTRING Name, _In_ PVOID Base, _In_ ULONG_PTR ProcessId)
ULONG KdpDefaultRetries
Definition: windbgkd.h:226
#define DPRINT1
Definition: precomp.h:8
PVOID Address
Definition: kd64.h:50
unsigned int ULONG
Definition: retypes.h:1
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
#define ULONG_PTR
Definition: config.h:101
BOOLEAN NTAPI KdpSwitchProcessor(IN PEXCEPTION_RECORD ExceptionRecord, IN OUT PCONTEXT ContextRecord, IN BOOLEAN SecondChanceException)
Definition: kdapi.c:1833
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:711
DBGKD_DEBUG_DATA_HEADER64 Header
Definition: wdbgexts.h:179
#define CHAR(Char)
KTIMER KdpTimeSlipTimer
Definition: kddata.c:117
BOOLEAN KdBreakAfterSymbolLoad
Definition: kdmain.c:19
BOOLEAN NTAPI KdRegisterDebuggerDataBlock(IN ULONG Tag, IN PDBGKD_DEBUG_DATA_HEADER64 DataHeader, IN ULONG Size)
Definition: kdinit.c:81
BOOLEAN KdDebuggerNotPresent
Definition: kdmain.c:18
ULONG64 DebuggerDataList
Definition: wdbgexts.h:161
#define LONG_PTR
Definition: generated.c:23
LONGLONG QuadPart
Definition: typedefs.h:113
static SIZE_T INIT_FUNCTION KdpGetMemorySizeInMBs ( IN PLOADER_PARAMETER_BLOCK  LoaderBlock)
static

Definition at line 31 of file kdinit.c.

Referenced by KdInitSystem().

32 {
33  PLIST_ENTRY ListEntry;
35  SIZE_T NumberOfPhysicalPages = 0;
36 
37  /* Loop the memory descriptors */
38  for (ListEntry = LoaderBlock->MemoryDescriptorListHead.Flink;
39  ListEntry != &LoaderBlock->MemoryDescriptorListHead;
40  ListEntry = ListEntry->Flink)
41  {
42  /* Get the descriptor */
43  Descriptor = CONTAINING_RECORD(ListEntry,
45  ListEntry);
46 
47  /* Check if this is invisible memory */
48  if ((Descriptor->MemoryType == LoaderFirmwarePermanent) ||
49  (Descriptor->MemoryType == LoaderSpecialMemory) ||
50  (Descriptor->MemoryType == LoaderHALCachedMemory) ||
51  (Descriptor->MemoryType == LoaderBBTMemory))
52  {
53  /* Skip this descriptor */
54  continue;
55  }
56 
57  /* Check if this is bad memory */
58  if (Descriptor->MemoryType != LoaderBad)
59  {
60  /* Count this in the total of pages */
61  NumberOfPhysicalPages += Descriptor->PageCount;
62  }
63  }
64 
65  return NumberOfPhysicalPages * PAGE_SIZE / 1024 / 1024;
66 }
TYPE_OF_MEMORY MemoryType
Definition: arc.h:193
Definition: arc.h:130
struct _LIST_ENTRY * Flink
Definition: typedefs.h:120
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Definition: typedefs.h:118
ULONG_PTR SIZE_T
Definition: typedefs.h:79
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
_In_ PSTORAGE_PROPERTY_ID _Outptr_ PSTORAGE_DESCRIPTOR_HEADER * Descriptor
Definition: classpnp.h:966
BOOLEAN NTAPI KdRegisterDebuggerDataBlock ( IN ULONG  Tag,
IN PDBGKD_DEBUG_DATA_HEADER64  DataHeader,
IN ULONG  Size 
)

Definition at line 81 of file kdinit.c.

Referenced by KdInitSystem().

84 {
85  KIRQL OldIrql;
86  PLIST_ENTRY NextEntry;
87  PDBGKD_DEBUG_DATA_HEADER64 CurrentHeader;
88 
89  /* Acquire the Data Lock */
91 
92  /* Loop the debugger data list */
93  NextEntry = KdpDebuggerDataListHead.Flink;
94  while (NextEntry != &KdpDebuggerDataListHead)
95  {
96  /* Get the header for this entry */
97  CurrentHeader = CONTAINING_RECORD(NextEntry,
99  List);
100 
101  /* Move to the next one */
102  NextEntry = NextEntry->Flink;
103 
104  /* Check if we already have this data block */
105  if ((CurrentHeader == DataHeader) || (CurrentHeader->OwnerTag == Tag))
106  {
107  /* Release the lock and fail */
109  return FALSE;
110  }
111  }
112 
113  /* Setup the header */
114  DataHeader->OwnerTag = Tag;
115  DataHeader->Size = Size;
116 
117  /* Insert it into the list and release the lock */
118  InsertTailList(&KdpDebuggerDataListHead, (PLIST_ENTRY)&DataHeader->List);
120  return TRUE;
121 }
#define TRUE
Definition: types.h:120
#define InsertTailList(ListHead, Entry)
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
KSPIN_LOCK KdpDataSpinLock
Definition: kddata.c:366
UINTN Size
Definition: acefiex.h:555
struct _LIST_ENTRY * Flink
Definition: typedefs.h:120
LIST_ENTRY List
Definition: psmgr.c:57
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
Definition: typedefs.h:118
LIST_ENTRY KdpDebuggerDataListHead
Definition: kddata.c:365
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
IN ULONG IN ULONG Tag
Definition: evtlib.h:155
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
VOID NTAPI KdUpdateDataBlock ( VOID  )

Definition at line 72 of file kdinit.c.

Referenced by PspInitializeSystemDll().

73 {
74  /* Update the KeUserCallbackDispatcher pointer */
77 }
PVOID KeUserCallbackDispatcher
Definition: ke.h:131
ULONG64 KeUserCallbackDispatcher
Definition: wdbgexts.h:188
KDDEBUGGER_DATA64 * KdDebuggerDataBlock
Definition: kdpacket.c:21
#define ULONG_PTR
Definition: config.h:101