1 /* $Id: dlog.c,v 1.7 2002/09/08 10:23:27 chorns 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>
17 #include <internal/kd.h>
18 #include <ntos/minmax.h>
20 /* GLOBALS *******************************************************************/
22 #define DEBUGLOG_SIZE (32*1024)
24 #ifdef DBGPRINT_FILE_LOG
26 static CHAR DebugLog
[DEBUGLOG_SIZE
];
27 static ULONG DebugLogStart
;
28 static ULONG DebugLogEnd
;
29 static ULONG DebugLogCount
;
30 static KSPIN_LOCK DebugLogLock
;
31 static ULONG DebugLogOverflow
;
32 static HANDLE DebugLogThreadHandle
;
33 static CLIENT_ID DebugLogThreadCid
;
34 static HANDLE DebugLogFile
;
35 static KSEMAPHORE DebugLogSem
;
37 #endif /* DBGPRINT_FILE_LOG */
39 /* FUNCTIONS *****************************************************************/
41 #ifdef DBGPRINT_FILE_LOG
46 KeInitializeSpinLock(&DebugLogLock
);
51 KeInitializeSemaphore(&DebugLogSem
, 0, 255);
55 DebugLogThreadMain(PVOID Context
)
59 static CHAR Buffer
[256];
64 KeWaitForSingleObject(&DebugLogSem
,
69 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
70 while (DebugLogCount
> 0)
72 if (DebugLogStart
> DebugLogEnd
)
74 WLen
= min(256, DEBUGLOG_SIZE
- DebugLogStart
);
75 memcpy(Buffer
, &DebugLog
[DebugLogStart
], WLen
);
77 (DebugLogStart
+ WLen
) % DEBUGLOG_SIZE
;
78 DebugLogCount
= DebugLogCount
- WLen
;
79 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
80 NtWriteFile(DebugLogFile
,
92 WLen
= min(256, DebugLogEnd
- DebugLogStart
);
93 memcpy(Buffer
, &DebugLog
[DebugLogStart
], WLen
);
95 (DebugLogStart
+ WLen
) % DEBUGLOG_SIZE
;
96 DebugLogCount
= DebugLogCount
- WLen
;
97 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
98 NtWriteFile(DebugLogFile
,
108 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
110 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
118 OBJECT_ATTRIBUTES ObjectAttributes
;
119 UNICODE_STRING FileName
;
120 IO_STATUS_BLOCK Iosb
;
122 RtlInitUnicodeStringFromLiteral(&FileName
, L
"\\SystemRoot\\debug.log");
123 InitializeObjectAttributes(&ObjectAttributes
,
129 Status
= NtCreateFile(&DebugLogFile
,
134 FILE_ATTRIBUTE_NORMAL
,
137 FILE_WRITE_THROUGH
| FILE_SYNCHRONOUS_IO_NONALERT
,
140 if (!NT_SUCCESS(Status
))
142 DbgPrint("Failed to create debug log file\n");
146 Status
= PsCreateSystemThread(&DebugLogThreadHandle
,
156 DebugLogWrite(PCH String
)
160 if (KeGetCurrentIrql() > DISPATCH_LEVEL
)
166 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
168 if (DebugLogCount
== DEBUGLOG_SIZE
)
171 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
172 if (oldIrql
< DISPATCH_LEVEL
)
174 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
179 while ((*String
) != 0)
181 DebugLog
[DebugLogEnd
] = *String
;
184 if (DebugLogCount
== DEBUGLOG_SIZE
)
187 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
188 if (oldIrql
< DISPATCH_LEVEL
)
190 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
194 DebugLogEnd
= (DebugLogEnd
+ 1) % DEBUGLOG_SIZE
;
197 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
198 if (oldIrql
< DISPATCH_LEVEL
)
200 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
204 #else /* not DBGPRINT_FILE_LOG */
217 DebugLogWrite(PCH String
)
221 #endif /* DBGPRINT_FILE_LOG */