00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "kdp.h"
00022
00023
00024
00025
00026
00027 #define BAUD_OPTION "BAUDRATE"
00028 #define PORT_OPTION "DEBUGPORT"
00029
00030
00031
#ifdef ALLOC_PRAGMA
00032
#pragma alloc_text(PAGEKD, KdInitSystem)
00033
#endif
00034
00035 BOOLEAN
00036 KdInitSystem(
00037 IN
PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL,
00038 BOOLEAN StopInDebugger
00039 )
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 {
00062
00063 ULONG
Index;
00064 BOOLEAN Initialize;
00065 PCHAR Options;
00066 PCHAR BaudOption;
00067 PCHAR PortOption;
00068
00069
00070
00071
00072
00073
if (
KdDebuggerEnabled !=
FALSE) {
00074
return TRUE;
00075 }
00076
00077
KiDebugRoutine =
KdpStub;
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
if (LoaderBlock !=
NULL) {
00088
00089
KdpNtosImageBase = CONTAINING_RECORD(
00090 (LoaderBlock->LoadOrderListHead.Flink),
00091 LDR_DATA_TABLE_ENTRY,
00092 InLoadOrderLinks)->DllBase;
00093
00094
00095
00096
00097
00098 InitializeListHead(&
KdpDebuggerDataListHead);
00099
00100
00101
00102
00103
00104
00105
00106
KdDebuggerDataBlock.KernBase = (ULONG_PTR)
KdpNtosImageBase;
00107
KdDebuggerDataBlock.KeUserCallbackDispatcher = (ULONG_PTR)
KeUserCallbackDispatcher;
00108
00109
KdRegisterDebuggerDataBlock(KDBG_TAG,
00110 &
KdDebuggerDataBlock.Header,
00111
sizeof(
KdDebuggerDataBlock));
00112
00113
if (LoaderBlock->LoadOptions !=
NULL) {
00114 Options = LoaderBlock->LoadOptions;
00115 _strupr(Options);
00116
00117
00118
00119
00120
00121
00122 Initialize =
TRUE;
00123 PortOption = strstr(Options,
PORT_OPTION);
00124 BaudOption = strstr(Options,
BAUD_OPTION);
00125
if ((PortOption ==
NULL) && (BaudOption ==
NULL)) {
00126
if (strstr(Options,
"DEBUG") ==
NULL) {
00127 Initialize =
FALSE;
00128 }
00129
00130 }
else {
00131
if (PortOption) {
00132 PortOption = strstr(PortOption,
"COM");
00133
if (PortOption) {
00134
KdDebugParameters.
CommunicationPort =
00135 atol(PortOption + 3);
00136 }
00137 }
00138
00139
if (BaudOption) {
00140 BaudOption +=
strlen(
BAUD_OPTION);
00141
while (*BaudOption ==
' ') {
00142 BaudOption++;
00143 }
00144
00145
if (*BaudOption !=
'\0') {
00146
KdDebugParameters.
BaudRate = atol(BaudOption + 1);
00147 }
00148 }
00149 }
00150
00151
00152
00153
00154
00155
if (strstr(Options,
"NODEBUG")) {
00156 Initialize =
FALSE;
00157
KdPitchDebugger =
TRUE;
00158 }
00159
00160
if (strstr(Options,
"CRASHDEBUG")) {
00161 Initialize =
FALSE;
00162
KdPitchDebugger =
FALSE;
00163 }
00164
00165 }
else {
00166
00167
00168
00169
00170
00171
KdPitchDebugger =
TRUE;
00172 Initialize =
FALSE;
00173 }
00174
00175 }
else {
00176 Initialize =
TRUE;
00177 }
00178
00179
if ((
KdPortInitialize(&
KdDebugParameters, LoaderBlock, Initialize) ==
FALSE) ||
00180 (Initialize ==
FALSE)) {
00181
return(
TRUE);
00182 }
00183
00184
00185
00186
00187
00188
KiDebugRoutine =
KdpTrap;
00189
00190
if (!
KdpDebuggerStructuresInitialized) {
00191
00192
KiDebugSwitchRoutine =
KdpSwitchProcessor;
00193
KdpBreakpointInstruction =
KDP_BREAKPOINT_VALUE;
00194
KdpOweBreakpoint =
FALSE;
00195
00196
00197
00198
00199
00200
for (
Index = 0;
Index < BREAKPOINT_TABLE_SIZE;
Index += 1) {
00201
KdpBreakpointTable[
Index].
Flags = 0;
00202
KdpBreakpointTable[
Index].
Address =
NULL;
00203
KdpBreakpointTable[
Index].
DirectoryTableBase = 0
L;
00204 }
00205
00206
00207
00208
00209
KeInitializeDpc(&
KdpTimeSlipDpc,
KdpTimeSlipDpcRoutine,
NULL);
00210
KeInitializeTimer(&
KdpTimeSlipTimer);
00211
ExInitializeWorkItem(&
KdpTimeSlipWorkItem,
KdpTimeSlipWork,
NULL);
00212
00213
KdpDebuggerStructuresInitialized =
TRUE ;
00214 }
00215
00216
00217
00218
00219
00220
KeQueryPerformanceCounter(&
KdPerformanceCounterRate);
00221
KdTimerStart.HighPart = 0
L;
00222
KdTimerStart.LowPart = 0
L;
00223
00224
00225
00226
00227
00228
00229
KdpNextPacketIdToSend = INITIAL_PACKET_ID | SYNC_PACKET_ID;
00230
KdpPacketIdExpected = INITIAL_PACKET_ID;
00231
00232
00233
00234
00235
KdPitchDebugger =
FALSE;
00236
KdDebuggerEnabled =
TRUE;
00237 SharedUserData->KdDebuggerEnabled =
TRUE;
00238
00239
00240
00241
00242
00243
if (StopInDebugger) {
00244 DbgBreakPoint();
00245 }
00246
00247
return TRUE;
00248 }
00249
00250
00251 BOOLEAN
00252 KdRegisterDebuggerDataBlock(
00253 IN ULONG Tag,
00254 IN PDBGKD_DEBUG_DATA_HEADER DataHeader,
00255 IN ULONG Size
00256 )
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 {
00288 KIRQL OldIrql;
00289 PLIST_ENTRY
List;
00290 PDBGKD_DEBUG_DATA_HEADER
Header;
00291
00292
KeAcquireSpinLock(&
KdpDataSpinLock, &OldIrql);
00293
00294
00295
00296
00297
00298
List =
KdpDebuggerDataListHead.Flink;
00299
00300
while (
List != &
KdpDebuggerDataListHead) {
00301
00302
Header = CONTAINING_RECORD(
List, DBGKD_DEBUG_DATA_HEADER,
List);
00303
00304
List =
List->Flink;
00305
00306
if ((
Header == DataHeader) || (
Header->OwnerTag == Tag)) {
00307
KeReleaseSpinLock(&
KdpDataSpinLock, OldIrql);
00308
return FALSE;
00309 }
00310 }
00311
00312
00313
00314
00315
00316 DataHeader->OwnerTag = Tag;
00317 DataHeader->Size =
Size;
00318
00319 InsertTailList(&
KdpDebuggerDataListHead, &DataHeader->List);
00320
00321
KeReleaseSpinLock(&
KdpDataSpinLock, OldIrql);
00322
00323
return TRUE;
00324 }
00325
00326
00327
VOID
00328 KdDeregisterDebuggerDataBlock(
00329 IN PDBGKD_DEBUG_DATA_HEADER DataHeader
00330 )
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 {
00351 KIRQL OldIrql;
00352 PLIST_ENTRY
List;
00353 PDBGKD_DEBUG_DATA_HEADER
Header;
00354
00355
KeAcquireSpinLock(&
KdpDataSpinLock, &OldIrql);
00356
00357
00358
00359
00360
00361
List =
KdpDebuggerDataListHead.Flink;
00362
00363
while (
List != &
KdpDebuggerDataListHead) {
00364
00365
Header = CONTAINING_RECORD(
List, DBGKD_DEBUG_DATA_HEADER,
List);
00366
List =
List->Flink;
00367
00368
if (DataHeader ==
Header) {
00369 RemoveEntryList(&DataHeader->List);
00370
break;
00371 }
00372 }
00373
00374
KeReleaseSpinLock(&
KdpDataSpinLock, OldIrql);
00375 }
00376
00377
00378
VOID
00379 KdLogDbgPrint(
00380 IN PSTRING String
00381 )
00382 {
00383 KIRQL OldIrql;
00384 ULONG Length;
00385 ULONG LengthCopied;
00386
00387
for (; ;) {
00388
if (KeTestSpinLock (&
KdpPrintSpinLock)) {
00389
KeRaiseIrql (
HIGH_LEVEL, &OldIrql);
00390
if (
KiTryToAcquireSpinLock(&
KdpPrintSpinLock)) {
00391
break;
00392 }
00393
KeLowerIrql(OldIrql);
00394 }
00395 }
00396
00397
if (
KdPrintCircularBuffer) {
00398 Length =
String->Length;
00399
00400
00401
00402
if (Length >
KDPRINTBUFFERSIZE) {
00403 Length =
KDPRINTBUFFERSIZE;
00404 }
00405
00406
if (
KdPrintWritePointer + Length <=
KdPrintCircularBuffer+
KDPRINTBUFFERSIZE) {
00407 LengthCopied =
KdpMoveMemory(
KdPrintWritePointer,
String->Buffer, Length);
00408
KdPrintWritePointer += LengthCopied;
00409
if (
KdPrintWritePointer >=
KdPrintCircularBuffer+
KDPRINTBUFFERSIZE) {
00410
KdPrintWritePointer =
KdPrintCircularBuffer;
00411
KdPrintRolloverCount++;
00412 }
00413 }
else {
00414 ULONG First = (ULONG)(
KdPrintCircularBuffer +
KDPRINTBUFFERSIZE -
KdPrintWritePointer);
00415 LengthCopied =
KdpMoveMemory(
KdPrintWritePointer,
00416
String->Buffer,
00417 First);
00418
if (LengthCopied == First) {
00419 LengthCopied +=
KdpMoveMemory(
KdPrintCircularBuffer,
00420
String->Buffer + First,
00421 Length - First);
00422 }
00423
if (LengthCopied > First) {
00424
KdPrintWritePointer =
KdPrintCircularBuffer + LengthCopied - First;
00425
KdPrintRolloverCount++;
00426 }
else {
00427
KdPrintWritePointer += LengthCopied;
00428
if (
KdPrintWritePointer >=
KdPrintCircularBuffer+
KDPRINTBUFFERSIZE) {
00429
KdPrintWritePointer =
KdPrintCircularBuffer;
00430
KdPrintRolloverCount++;
00431 }
00432 }
00433 }
00434 }
00435
00436 KiReleaseSpinLock(&
KdpPrintSpinLock);
00437
KeLowerIrql(OldIrql);
00438 }