1 /* $Id: dlog.c,v 1.1 2001/02/16 18:32:20 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 KSPIN_LOCK DebugLogLock
;
29 static ULONG DebugLogOverflow
;
30 static HANDLE DebugLogThreadHandle
;
31 static CLIENT_ID DebugLogThreadCid
;
32 static HANDLE DebugLogFile
;
33 static KSEMAPHORE DebugLogSem
;
35 #endif /* DBGPRINT_FILE_LOG */
37 /* FUNCTIONS *****************************************************************/
39 #ifdef DBGPRINT_FILE_LOG
44 KeInitializeSpinLock(&DebugLogLock
);
48 KeInitializeSemaphore(&DebugLogSem
, 0, 255);
52 DebugLogThreadMain(PVOID Context
)
61 KeWaitForSingleObject(&DebugLogSem
,
66 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
67 while (DebugLogStart
!= -1)
69 if (DebugLogStart
> DebugLogEnd
)
71 WLen
= min(256, DEBUGLOG_SIZE
- DebugLogStart
);
72 memcpy(Buffer
, &DebugLog
[DebugLogStart
], WLen
);
74 (DebugLogStart
+ WLen
) % DEBUGLOG_SIZE
;
75 if (DebugLogStart
== DebugLogEnd
)
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 if (DebugLogStart
== DebugLogEnd
)
100 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
101 NtWriteFile(DebugLogFile
,
111 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
113 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
121 OBJECT_ATTRIBUTES ObjectAttributes
;
122 UNICODE_STRING FileName
;
123 IO_STATUS_BLOCK Iosb
;
125 RtlInitUnicodeString(&FileName
, L
"\\SystemRoot\\debug.log");
126 InitializeObjectAttributes(&ObjectAttributes
,
132 Status
= NtCreateFile(&DebugLogFile
,
137 FILE_ATTRIBUTE_NORMAL
,
140 FILE_WRITE_THROUGH
| FILE_SYNCHRONOUS_IO_NONALERT
,
143 if (!NT_SUCCESS(Status
))
145 DbgPrint("Failed to create debug log file\n");
149 Status
= PsCreateSystemThread(&DebugLogThreadHandle
,
159 DebugLogWrite(PCH String
)
163 if (KeGetCurrentIrql() > DISPATCH_LEVEL
)
169 KeAcquireSpinLock(&DebugLogLock
, &oldIrql
);
171 if (DebugLogEnd
== DebugLogStart
)
174 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
175 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
178 if (DebugLogStart
== -1)
180 DebugLogStart
= DebugLogEnd
;
183 while ((*String
) != 0)
185 DebugLog
[DebugLogEnd
] = *String
;
187 if (((DebugLogEnd
+ 1) % DEBUGLOG_SIZE
) == DebugLogStart
)
190 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
191 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
194 DebugLogEnd
= (DebugLogEnd
+ 1) % DEBUGLOG_SIZE
;
197 KeReleaseSpinLock(&DebugLogLock
, oldIrql
);
198 KeReleaseSemaphore(&DebugLogSem
, IO_NO_INCREMENT
, 1, FALSE
);
201 #else /* not DBGPRINT_FILE_LOG */
214 DebugLogWrite(PCH String
)
218 #endif /* DBGPRINT_FILE_LOG */