Add more Critical Section Debug Data and fix two bugs.

svn path=/trunk/; revision=12774
This commit is contained in:
Alex Ionescu 2005-01-04 01:51:16 +00:00
parent b06887c39a
commit 8c065412f4
2 changed files with 31 additions and 10 deletions

View file

@ -279,10 +279,7 @@ __true_LdrInitializeThunk (ULONG Unknown1,
RtlResetRtlTranslations (&NlsTable); RtlResetRtlTranslations (&NlsTable);
NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + PEDosHeader->e_lfanew); NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + PEDosHeader->e_lfanew);
/* Initialize Critical Section Data */
RtlpInitDeferedCriticalSection();
/* create process heap */ /* create process heap */
RtlInitializeHeapManager(); RtlInitializeHeapManager();
Peb->ProcessHeap = RtlCreateHeap(HEAP_GROWABLE, Peb->ProcessHeap = RtlCreateHeap(HEAP_GROWABLE,
@ -296,7 +293,10 @@ __true_LdrInitializeThunk (ULONG Unknown1,
DPRINT1("Failed to create process heap\n"); DPRINT1("Failed to create process heap\n");
ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL); ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
} }
/* Initialize Critical Section Data */
RtlpInitDeferedCriticalSection();
/* initalize peb lock support */ /* initalize peb lock support */
RtlInitializeCriticalSection (&PebLock); RtlInitializeCriticalSection (&PebLock);
Peb->FastPebLock = &PebLock; Peb->FastPebLock = &PebLock;

View file

@ -16,7 +16,7 @@
#include <ntdll/rtl.h> #include <ntdll/rtl.h>
#include <ntos/synch.h> #include <ntos/synch.h>
//#define NDEBUG #define NDEBUG
#include <ntdll/ntdll.h> #include <ntdll/ntdll.h>
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
@ -24,6 +24,8 @@
static RTL_CRITICAL_SECTION RtlCriticalSectionLock; static RTL_CRITICAL_SECTION RtlCriticalSectionLock;
static LIST_ENTRY RtlCriticalSectionList; static LIST_ENTRY RtlCriticalSectionList;
static BOOLEAN RtlpCritSectInitialized = FALSE; static BOOLEAN RtlpCritSectInitialized = FALSE;
//static RTL_CRITICAL_SECTION_DEBUG RtlpStaticDebugInfo[64];
//static PRTL_CRITICAL_SECTION_DEBUG RtlpDebugInfoFreeList;
/*++ /*++
* RtlDeleteCriticalSection * RtlDeleteCriticalSection
@ -209,6 +211,7 @@ RtlInitializeCriticalSectionAndSpinCount (
PVOID Heap; PVOID Heap;
/* First things first, set up the Object */ /* First things first, set up the Object */
DPRINT("Initializing Critical Section: %x\n", CriticalSection);
CriticalSection->LockCount = -1; CriticalSection->LockCount = -1;
CriticalSection->RecursionCount = 0; CriticalSection->RecursionCount = 0;
CriticalSection->OwningThread = 0; CriticalSection->OwningThread = 0;
@ -222,6 +225,7 @@ RtlInitializeCriticalSectionAndSpinCount (
if ((Heap = RtlGetProcessHeap())) { if ((Heap = RtlGetProcessHeap())) {
CritcalSectionDebugData = RtlAllocateHeap(Heap, 0, sizeof(RTL_CRITICAL_SECTION_DEBUG)); CritcalSectionDebugData = RtlAllocateHeap(Heap, 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
DPRINT("Allocated Critical Section Debug Data: %x\n", CritcalSectionDebugData);
CritcalSectionDebugData->Type = RTL_CRITSECT_TYPE; CritcalSectionDebugData->Type = RTL_CRITSECT_TYPE;
CritcalSectionDebugData->ContentionCount = 0; CritcalSectionDebugData->ContentionCount = 0;
CritcalSectionDebugData->EntryCount = 0; CritcalSectionDebugData->EntryCount = 0;
@ -235,6 +239,10 @@ RtlInitializeCriticalSectionAndSpinCount (
*/ */
if ((CriticalSection != &RtlCriticalSectionLock) && (RtlpCritSectInitialized)) { if ((CriticalSection != &RtlCriticalSectionLock) && (RtlpCritSectInitialized)) {
DPRINT("Securely Inserting into ProcessLocks: %x, %x\n",
&CritcalSectionDebugData->ProcessLocksList,
CriticalSection);
/* Protect List */ /* Protect List */
RtlEnterCriticalSection(&RtlCriticalSectionLock); RtlEnterCriticalSection(&RtlCriticalSectionLock);
@ -246,12 +254,17 @@ RtlInitializeCriticalSectionAndSpinCount (
} else { } else {
DPRINT("Inserting into ProcessLocks: %x, %x\n",
&CritcalSectionDebugData->ProcessLocksList,
CriticalSection);
/* Add it directly */ /* Add it directly */
InsertTailList(&RtlCriticalSectionList, &CritcalSectionDebugData->ProcessLocksList); InsertTailList(&RtlCriticalSectionList, &CritcalSectionDebugData->ProcessLocksList);
} }
} else { } else {
DPRINT1("No Debug Data was allocated for: %x\n", CriticalSection);
/* This shouldn't happen... */ /* This shouldn't happen... */
CritcalSectionDebugData = NULL; CritcalSectionDebugData = NULL;
} }
@ -372,7 +385,9 @@ RtlpWaitForCriticalSection(
BOOLEAN LastChance = FALSE; BOOLEAN LastChance = FALSE;
LARGE_INTEGER Timeout; LARGE_INTEGER Timeout;
Timeout = RtlConvertLongToLargeInteger(150000); /* Wait 2.5 minutes */
Timeout.QuadPart = 150000L * (ULONGLONG)10000;
Timeout.QuadPart = -Timeout.QuadPart;
/* ^^ HACK HACK HACK. Good way: /* ^^ HACK HACK HACK. Good way:
Timeout = &NtCurrentPeb()->CriticalSectionTimeout */ Timeout = &NtCurrentPeb()->CriticalSectionTimeout */
@ -382,14 +397,16 @@ RtlpWaitForCriticalSection(
} }
/* Increase the Debug Entry count */ /* Increase the Debug Entry count */
CriticalSection->DebugInfo->EntryCount++; DPRINT("Waiting on Critical Section: %x\n", CriticalSection);
if (CriticalSection->DebugInfo) CriticalSection->DebugInfo->EntryCount++;
for (;;) { for (;;) {
/* Increase the number of times we've had contention */ /* Increase the number of times we've had contention */
CriticalSection->DebugInfo->ContentionCount++; if (CriticalSection->DebugInfo) CriticalSection->DebugInfo->ContentionCount++;
/* Wait on the Event */ /* Wait on the Event */
DPRINT("Waiting on Event: %x\n", CriticalSection->LockSemaphore);
Status = NtWaitForSingleObject(CriticalSection->LockSemaphore, Status = NtWaitForSingleObject(CriticalSection->LockSemaphore,
FALSE, FALSE,
&Timeout); &Timeout);
@ -400,6 +417,8 @@ RtlpWaitForCriticalSection(
/* Is this the 2nd time we've timed out? */ /* Is this the 2nd time we've timed out? */
if (LastChance) { if (LastChance) {
DPRINT1("Deadlock: %x\n", CriticalSection);
/* Yes it is, we are raising an exception */ /* Yes it is, we are raising an exception */
ExceptionRecord.ExceptionCode = STATUS_POSSIBLE_DEADLOCK; ExceptionRecord.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
ExceptionRecord.ExceptionFlags = 0; ExceptionRecord.ExceptionFlags = 0;
@ -451,11 +470,13 @@ RtlpUnWaitCriticalSection(
} }
/* Signal the Event */ /* Signal the Event */
DPRINT("Signaling CS Event\n");
Status = NtSetEvent(CriticalSection->LockSemaphore, NULL); Status = NtSetEvent(CriticalSection->LockSemaphore, NULL);
if (!NT_SUCCESS(Status)) { if (!NT_SUCCESS(Status)) {
/* We've failed */ /* We've failed */
DPRINT1("Signaling Failed\n");
RtlRaiseStatus(Status); RtlRaiseStatus(Status);
} }
} }
@ -487,7 +508,7 @@ RtlpCreateCriticalSectionSem(
/* Chevk if we have an event */ /* Chevk if we have an event */
if (!hEvent) { if (!hEvent) {
DPRINT ("Creating Event\n"); DPRINT("Creating Event\n");
/* No, so create it */ /* No, so create it */
if (!NT_SUCCESS(Status = NtCreateEvent(&hNewEvent, if (!NT_SUCCESS(Status = NtCreateEvent(&hNewEvent,
EVENT_ALL_ACCESS, EVENT_ALL_ACCESS,