27 #include "../../arch/windows/debug.c"
31 # define min(_x, _y) ( ((_x) < (_y)) ? (_x) : (_y) )
51 #define REPORT_BUG(_no, _a, _b, _c, _d, _str) KeBugCheckEx(_no, _a, _b, _c, _d)
53 #define REPORT_BUG(_no, _a, _b, _c, _d, _str) \
54 DbgPrint("--- REPORT_BUG: %04x (%08x, %08x, %08x, %08x). '%s' " \
55 "in %s, %s(), line %u\n", \
56 _no, _a, _b, _c, _d, _str, \
57 __FILE__, __FUNCTION__, __LINE__);
75 #define DEFINE_STATUSCODE(_x) { #_x, _x }
78 #define _MARKLASTENTRY ((NTSTATUS) -1)
81 #define _LASTENTRY { "", _MARKLASTENTRY }
1033 if (text[i].Value == Value)
1035 return text[i].
Name;
1046 #define USE_SPINLOCK 1
1051 static volatile PCHAR DbgMemoryBuffer = NULL;
1052 static volatile ULONG DbgNextWriteMemoryBuffer = 0;
1056 static KSPIN_LOCK DbgMemoryBufferSpinLock;
1057 static KIRQL DbgMemoryBufferSpinLockIrql;
1059 #else // #ifdef USE_SPINLOCK
1061 static LONG DbgMemoryBufferSpinLock = 0;
1062 static LONG DbgMemoryBufferUsageCounter = 0;
1064 #ifdef USE_IRQL_ELEVATION
1065 static KIRQL DbgMemoryBufferIrql;
1068 #endif // #ifdef USE_SPINLOCK
1071 #define DBG_SIZE_MEMORY_BUFFER 0x20000u
1089 DbgBufferSynchronizeStart(VOID)
1098 KeAcquireSpinLock(&DbgMemoryBufferSpinLock, &irql);
1099 DbgMemoryBufferSpinLockIrql = irql;
1101 #else // #ifdef USE_SPINLOCK
1103 #ifdef USE_IRQL_ELEVATION
1105 if (DbgMemoryBufferIrql != PASSIVE_LEVEL)
1107 REPORT_BUG(0x133, DbgMemoryBufferUsageCounter,
1108 DbgMemoryBufferSpinLock, DbgMemoryBufferIrql, 0,
1109 "Not IRQL == PASSIVE_LEVEL");
1112 KeRaiseIrql(DISPATCH_LEVEL, &DbgMemoryBufferIrql);
1114 #endif // #ifdef USE_IRQL_ELEVATION
1116 while (InterlockedExchange(&DbgMemoryBufferSpinLock, 1) == 0)
1119 tmp = InterlockedIncrement(&DbgMemoryBufferUsageCounter);
1122 REPORT_BUG(0x130, tmp, DbgMemoryBufferSpinLock, 0, 0,
1123 "Usage counter bigger than 1 after incrementing!");
1126 #endif // #ifdef USE_SPINLOCK
1137 DbgBufferSynchronizeStop(VOID)
1143 KeReleaseSpinLock(&DbgMemoryBufferSpinLock, DbgMemoryBufferSpinLockIrql);
1145 #else // #ifdef USE_SPINLOCK
1147 tmp = InterlockedDecrement(&DbgMemoryBufferUsageCounter);
1150 REPORT_BUG(0x131, tmp, DbgMemoryBufferSpinLock, 0, 0,
1151 "Usage counter not null after decrementing!");
1154 tmp = InterlockedExchange(&DbgMemoryBufferSpinLock, 0);
1157 REPORT_BUG(0x132, DbgMemoryBufferUsageCounter, tmp, 0, 0,
1158 "(own) Spinlock not 1 before releasing.");
1161 #ifdef USE_IRQL_ELEVATION
1163 KeLowerIrql(DbgMemoryBufferIrql);
1164 DbgMemoryBufferIrql = PASSIVE_LEVEL;
1166 #endif // #ifdef USE_IRQL_ELEVATION
1168 #endif // #ifdef USE_SPINLOCK
1181 KeInitializeSpinLock(&DbgMemoryBufferSpinLock);
1183 #endif // #ifdef USE_SPINLOCK
1194 DbgBufferSynchronizeStart();
1196 DbgMemoryBuffer = ExAllocatePoolWithTag(NonPagedPool,
1199 if (DbgMemoryBuffer)
1204 DbgNextWriteMemoryBuffer = 0;
1206 DbgBufferSynchronizeStop();
1216 DbgBufferSynchronizeStart();
1218 if (DbgMemoryBuffer)
1220 ExFreePool(DbgMemoryBuffer);
1221 DbgMemoryBuffer = NULL;
1224 DbgBufferSynchronizeStop();
1243 DbgBufferSynchronizeStart();
1245 if (DbgMemoryBuffer)
1247 PCHAR localMemoryBuffer;
1248 ULONG localNextWriteMemoryBuffer;
1251 localMemoryBuffer = DbgMemoryBuffer;
1252 localNextWriteMemoryBuffer = DbgNextWriteMemoryBuffer;
1257 "DbgNextWriteMemoryBuffer already out of buffer!");
1260 strLength = strlen(String) + 1;
1262 if (strLength >= 200)
1265 strLength, 0,
"string too long");
1270 if ((localMemoryBuffer != DbgMemoryBuffer)
1271 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1273 REPORT_BUG(0x140, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1274 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1275 "local copies do not match the global ones (1)");
1290 if ((localMemoryBuffer != DbgMemoryBuffer)
1291 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1293 REPORT_BUG(0x141, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1294 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1295 "local copies do not match the global ones (2)");
1298 RtlZeroMemory(&DbgMemoryBuffer[DbgNextWriteMemoryBuffer],
1303 if ((localMemoryBuffer != DbgMemoryBuffer)
1304 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1306 REPORT_BUG(0x142, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1307 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1308 "local copies do not match the global ones (3)");
1311 DbgNextWriteMemoryBuffer = 0;
1312 localNextWriteMemoryBuffer = 0;
1317 if ((localMemoryBuffer != DbgMemoryBuffer)
1318 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1320 REPORT_BUG(0x143, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1321 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1322 "local copies do not match the global ones (4)");
1329 "String does not fit in the buffer (but should have fitted!)");
1334 if ((localMemoryBuffer != DbgMemoryBuffer)
1335 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1337 REPORT_BUG(0x144, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1338 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1339 "local copies do not match the global ones (5)");
1342 if (DbgNextWriteMemoryBuffer > 0)
1344 DbgMemoryBuffer[DbgNextWriteMemoryBuffer-1] = 13;
1349 if ((localMemoryBuffer != DbgMemoryBuffer)
1350 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1352 REPORT_BUG(0x145, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1353 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1354 "local copies do not match the global ones (6)");
1357 RtlCopyMemory(&DbgMemoryBuffer[DbgNextWriteMemoryBuffer], String, strLength);
1358 DbgNextWriteMemoryBuffer += strLength;
1359 localNextWriteMemoryBuffer += strLength;
1363 if ((localMemoryBuffer != DbgMemoryBuffer)
1364 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1366 REPORT_BUG(0x146, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1367 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1368 "local copies do not match the global ones (7)");
1375 "DbgNextWriteMemoryBuffer out of buffer after write!");
1380 if ((localMemoryBuffer != DbgMemoryBuffer)
1381 || (localNextWriteMemoryBuffer != DbgNextWriteMemoryBuffer))
1383 REPORT_BUG(0x147, (ULONG_PTR) localMemoryBuffer, (ULONG_PTR) DbgMemoryBuffer,
1384 localNextWriteMemoryBuffer, DbgNextWriteMemoryBuffer,
1385 "local copies do not match the global ones (8)");
1389 DbgBufferSynchronizeStop();
1417 IN OUT PULONG ReturnLength)
1421 DbgBufferSynchronizeStart();
1423 if (DbgMemoryBuffer)
1425 PCHAR writePosition;
1429 writePosition = ReturnBuffer;
1430 stillToWrite = *ReturnLength;
1434 if (stillToWrite > DbgNextWriteMemoryBuffer)
1436 ULONG lengthBeforeWrapAround;
1443 lengthBeforeWrapAround =
1444 strlen(&DbgMemoryBuffer[DbgNextWriteMemoryBuffer+1]) + 1;
1446 if (lengthBeforeWrapAround > 1)
1450 lengthToCopy = stillToWrite - DbgNextWriteMemoryBuffer + 1;
1452 lengthToCopy =
min(lengthToCopy, lengthBeforeWrapAround);
1454 RtlCopyMemory(writePosition,
1455 &DbgMemoryBuffer[DbgNextWriteMemoryBuffer
1456 + lengthBeforeWrapAround
1460 writePosition += lengthToCopy;
1461 writtenBytes += lengthToCopy;
1462 stillToWrite -= lengthToCopy;
1464 if (stillToWrite > 0)
1466 *writePosition++ = 13;
1473 stillToWrite =
min(stillToWrite, DbgNextWriteMemoryBuffer);
1475 RtlCopyMemory(writePosition,
1476 &DbgMemoryBuffer[DbgNextWriteMemoryBuffer - stillToWrite],
1479 writtenBytes += stillToWrite;
1482 *ReturnLength = writtenBytes;
1485 DbgBufferSynchronizeStop();
1487 FUNC_LEAVE_NTSTATUS_CONST(STATUS_SUCCESS);
#define DBG_SIZE_MEMORY_BUFFER
the size of the memory buffer used for the debugging buffer
const NTSTATUS_DEBUGCODE text[]
VOID DbgAllocateMemoryBuffer(VOID)
Get storage area for debugging output.
struct NTSTATUS_DEBUGCODE NTSTATUS_DEBUGCODE
NTSTATUS cbm_dbg_readbuffer(IN PDEVICE_EXTENSION Pdx, OUT PCHAR ReturnBuffer, IN OUT PULONG ReturnLength)
Give the debug buffer contents to the installer.
#define REPORT_BUG(_no, _a, _b, _c, _d, _str)
@@@
VOID DbgOutputMemoryBuffer(const char *String)
Output into the debugging buffer.
const UCHAR * DebugNtStatus(NTSTATUS Value)
Return the description of an NTSTATUS code.
Definitions for the opencbm driver.
#define DEFINE_STATUSCODE(_x)
VOID DbgInit(VOID)
Initialise debugging system.
VOID DbgFreeMemoryBuffer(VOID)
Free storage area for debugging output.