1 /* $Id: dlog.c,v 1.2 2001/03/25 18:56:12 dwelch Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/kd/kdebug.c
6 * PURPOSE: Kernel debugger
7 * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de)
12 /* INCLUDES ******************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/ntoskrnl.h>
16 #include <internal/kd.h>
17 #include <ntos/minmax.h>
19 /* GLOBALS *******************************************************************/
21 #define DEBUGLOG_SIZE (32*1024)
23 #ifdef DBGPRINT_FILE_LOG
25 static CHAR DebugLog
[DEBUGLOG_SIZE
];
26 static ULONG DebugLogStart
;
27 static ULONG DebugLogEnd
;
28 static ULONG DebugLogCount
;
29 static KSPIN_LOCK DebugLogLock
;
30 static ULONG DebugLogOverflow
;
31 static HANDLE DebugLogThreadHandle
;
32 static CLIENT_ID DebugLogThreadCid
;
33 static HANDLE DebugLogFile
;
34 static KSEMAPHORE DebugLogSem
;
36 #endif /* DBGPRINT_FILE_LOG */
38 /* FUNCTIONS *****************************************************************/
40 #ifdef DBGPRINT_FILE_LOG
45 KeInitializeSpinLock(&DebugLogLock
);
50 KeInitializeSemaphore(&DebugLogSem
, 0, 255);
54 DebugLogThreadMain(PVOID Context
)
58 static CHAR Buffer
[256];
63 KeWaitForSingleObject(&DebugLogSem
,
68 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
69 while (DebugLogCount
> 0)
71 if (DebugLogStart
> DebugLogEnd
)
73 WLen
= min(256, DEBUGLOG_SIZE
- DebugLogStart
);
74 memcpy(Buffer
, &DebugLog
[DebugLogStart
], WLen
);
76 (DebugLogStart
+ WLen
) % DEBUGLOG_SIZE
;
77 DebugLogCount
= DebugLogCount
- WLen
;
78 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
79 NtWriteFile(DebugLogFile
,
91 WLen
= min(256, DebugLogEnd
- DebugLogStart
);
92 memcpy(Buffer
, &DebugLog
[DebugLogStart
], WLen
);
94 (DebugLogStart
+ WLen
) % DEBUGLOG_SIZE
;
95 DebugLogCount
= DebugLogCount
- WLen
;
96 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
97 NtWriteFile(DebugLogFile
,
107 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
109 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
117 OBJECT_ATTRIBUTES ObjectAttributes
;
118 UNICODE_STRING FileName
;
119 IO_STATUS_BLOCK Iosb
;
121 RtlInitUnicodeString(&FileName
, L
"\\SystemRoot\\debug.log");
122 InitializeObjectAttributes(&ObjectAttributes
,
128 Status
= NtCreateFile(&DebugLogFile
,
133 FILE_ATTRIBUTE_NORMAL
,
136 FILE_WRITE_THROUGH
| FILE_SYNCHRONOUS_IO_NONALERT
,
139 if (!NT_SUCCESS(Status
))
141 DbgPrint("Failed to create debug log file\n");
145 Status
= PsCreateSystemThread(&DebugLogThreadHandle
,
155 DebugLogWrite(PCH String
)
159 if (KeGetCurrentIrql() > DISPATCH_LEVEL
)
165 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
167 if (DebugLogCount
== DEBUGLOG_SIZE
)
170 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
171 if (oldIrql
< DISPATCH_LEVEL
)
173 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
178 while ((*String
) != 0)
180 DebugLog
[DebugLogEnd
] = *String
;
183 if (DebugLogCount
== DEBUGLOG_SIZE
)
186 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
187 if (oldIrql
< DISPATCH_LEVEL
)
189 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
193 DebugLogEnd
= (DebugLogEnd
+ 1) % DEBUGLOG_SIZE
;
196 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
197 if (oldIrql
< DISPATCH_LEVEL
)
199 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
203 #else /* not DBGPRINT_FILE_LOG */
216 DebugLogWrite(PCH String
)
220 #endif /* DBGPRINT_FILE_LOG */