1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
/*++
Copyright (c) 1994 Microsoft Corporation
Module Name:
savedump.c
Abstract:
This module contains the code to recover a dump from the system paging
file.
Author:
Darryl E. Havens (darrylh) 5-jan-1994
Environment:
Kernel mode
Revision History:
--*/
#ifndef UNICODE
#define UNICODE
#endif
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <assert.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <memory.h>
#include <lmcons.h>
#include <lmalert.h>
#include <ntiodump.h>
#include <sdevents.h>
#include "..\..\..\inc\alertmsg.h"
//
// Define physical memory blocks.
//
typedef struct _PHYSICAL_MEMORY_RUN {
ULONG BasePage;
ULONG PageCount;
} PHYSICAL_MEMORY_RUN, *PPHYSICAL_MEMORY_RUN;
typedef struct _PHYSICAL_MEMORY_DESCRIPTOR {
ULONG NumberOfRuns;
ULONG NumberOfPages;
PHYSICAL_MEMORY_RUN Run[1];
} PHYSICAL_MEMORY_DESCRIPTOR, *PPHYSICAL_MEMORY_DESCRIPTOR;
//
// Administrative alert information buffer.
//
WCHAR VariableInfo[256];
//
// Read/write copy of dump header.
//
CHAR HeaderBuffer[64 * 1024];
#if DBG
//
// Debug values:
//
// 1 - Basic debug information
// 2 - Registry & informational control information
// 4 - Flow control debug information
//
ULONG SdDebug = 4;
#endif // DBG
VOID __cdecl
main(
int argc,
char *argv[]
)
/*++
Routine Description:
This is the main driving routine for the dump recovery process.
Arguments:
None.
Return Value:
None.
--*/
{
NTSTATUS status;
SYSTEM_CRASH_DUMP_INFORMATION crashDumpInfo;
HANDLE sectionHandle;
HANDLE fileHandle;
SYSTEM_BASIC_INFORMATION basicInfo;
ULONG pageSize;
PDUMP_HEADER viewBase;
ULONG viewSize;
PULONG block;
PPHYSICAL_MEMORY_DESCRIPTOR memory;
BOOLEAN summary;
ULONG winStatus;
HKEY controlKey;
ULONG type;
ULONG crashDumpEnabled;
ULONG logEvent;
ULONG sendAlert;
ULONG returnedLength;
CHAR buffer1[256];
CHAR buffer2[256];
ANSI_STRING ansiString1;
ANSI_STRING ansiString2;
UNICODE_STRING unicodeString1;
UNICODE_STRING unicodeString2;
WCHAR fileName[MAX_PATH];
WCHAR expandedName[MAX_PATH];
ULONG i;
//
// Begin by determining whether or not there is a valid dump in the
// system's paging file. If not, then exit immediately.
//
status = NtQuerySystemInformation( SystemCrashDumpInformation,
&crashDumpInfo,
sizeof( SYSTEM_CRASH_DUMP_INFORMATION ),
(PULONG) NULL );
if (!NT_SUCCESS( status ) || !crashDumpInfo.CrashDumpSection) {
#if DBG
if (!NT_SUCCESS( status ) && SdDebug) {
DbgPrint( "SAVEDUMP: Error getting dump handle; error = %x\n", status );
}
#endif // DBG
return;
}
sectionHandle = crashDumpInfo.CrashDumpSection;
//
// Get the page size of the machine to determine the size of the header
// stored in the paging file.
//
status = NtQuerySystemInformation( SystemBasicInformation,
&basicInfo,
sizeof( SYSTEM_BASIC_INFORMATION ),
(PULONG) NULL );
pageSize = basicInfo.PageSize;
//
// Map the header of the section to determine whether what was written
// was a full dump or a summary dump. The dump is a summary dump if the
// size of the dump is a single page.
//
viewBase = NULL;
viewSize = pageSize;
status = NtMapViewOfSection( sectionHandle,
NtCurrentProcess(),
(PVOID *) &viewBase,
0,
0,
(PLARGE_INTEGER) NULL,
&viewSize,
ViewShare,
0,
PAGE_READONLY );
if (!NT_SUCCESS( status )) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to map view of paging file section; error = %x\n", status );
}
#endif // DBG
return;
}
RtlCopyMemory( HeaderBuffer, viewBase, pageSize );
block = (PULONG) viewBase;
memory = (PPHYSICAL_MEMORY_DESCRIPTOR) &block[DH_PHYSICAL_MEMORY_BLOCK];
summary = memory->NumberOfPages == 1;
//
// Open the base registry node for crash control information and get the
// actions for what needs to occur next.
//
winStatus = RegOpenKey( HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\CrashControl",
&controlKey );
if (winStatus != ERROR_SUCCESS) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to open CrashControl key; error = %d\n", winStatus );
}
#endif // DBG
return;
}
//
// Get the values of the following from the registry. Each are longwords
// so the returned length should always be 4 bytes.
//
// CrashDumpEnabled
// LogEvent
// SendAlert
//
// Note that it is possible for the values to not be there, if they were
// never initialized. In this case, they are taken to be zeroes.
//
crashDumpEnabled = 0;
logEvent = 0;
sendAlert = 0;
returnedLength = 4;
winStatus = RegQueryValueEx( controlKey,
L"CrashDumpEnabled",
(LPDWORD) NULL,
&type,
(LPBYTE) &crashDumpEnabled,
&returnedLength );
winStatus = RegQueryValueEx( controlKey,
L"LogEvent",
(LPDWORD) NULL,
&type,
(LPBYTE) &logEvent,
&returnedLength );
winStatus = RegQueryValueEx( controlKey,
L"SendAlert",
(LPDWORD) NULL,
&type,
(LPBYTE) &sendAlert,
&returnedLength );
#if DBG
if (SdDebug & 2) {
DbgPrint( "SAVEDUMP: CDE = %d, LE = %d, SA = %d\n",
crashDumpEnabled, logEvent, sendAlert );
DbgPrint( "SAVEDUMP: Number of pages = %d\n", memory->NumberOfPages );
}
#endif // DBG
//
// If crash dumping is enabled, and a dump was actually written to the file,
// copy the file to the specified location.
//
fileHandle = INVALID_HANDLE_VALUE;
if (crashDumpEnabled && !summary) {
LARGE_INTEGER sectionOffset;
ULONG overwrite;
ULONG numberOfPages;
IO_STATUS_BLOCK ioStatus;
FILE_ALLOCATION_INFORMATION allocInfo;
PCHAR bytes;
PCHAR sectionBase;
BOOLEAN partialMap;
//
// A crash dump is available and is to be copied to some other location.
// Determine whether or not the output file is to be overwritten, if
// it already exists.
//
overwrite = 0;
winStatus = RegQueryValueEx( controlKey,
L"Overwrite",
(LPDWORD) NULL,
&type,
(LPBYTE) &overwrite,
&returnedLength );
//
// Get the name of the target file. Note that because dumps are
// enabled, the filename must be present. If it is not, it is an
// error that cannot be properly dealt with, so skip attempting to
// save a dump.
//
returnedLength = sizeof( fileName );
winStatus = RegQueryValueEx( controlKey,
L"DumpFile",
(LPDWORD) NULL,
&type,
(LPBYTE) &fileName,
&returnedLength );
if (winStatus != ERROR_SUCCESS) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to query DumpFile file name; error = %d\n", winStatus );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
//
// Expand the pathname in case there are buried %'s.
//
ExpandEnvironmentStrings( &fileName[0], &expandedName[0], MAX_PATH );
#if DBG
if (SdDebug & 2) {
DbgPrint( "SAVEDUMP: Expanded pathname is: %ws\n", expandedName );
}
#endif // DBG
//
// Set the priority class of this application down to the Lowest
// priority class to ensure that copying the file does not overload
// everything else that is going on during system initialization.
//
SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_LOWEST );
//
// Create the output file, overwriting it if necessary.
//
i = 0;
do {
fileHandle = CreateFile( expandedName,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES) NULL,
overwrite ? CREATE_ALWAYS : CREATE_NEW,
0,
(HANDLE) NULL );
//
// If the create failed, check to see whether or not it was because
// the path was not found. If it is, then it is possible that the
// reason is because the network has not yet started, and the output
// file is being written to a network server. Try this every 15
// seconds for 5 minutes. If it still doesn't work, forget it.
//
if (fileHandle == INVALID_HANDLE_VALUE) {
winStatus = GetLastError();
if (winStatus == ERROR_PATH_NOT_FOUND) {
if (i++ > 20) {
break;
}
Sleep( 15000 );
}
}
} while (winStatus == ERROR_PATH_NOT_FOUND);
if (fileHandle == INVALID_HANDLE_VALUE) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to %s output file; error = %d\n",
overwrite ? "overwrite" : "create",
GetLastError() );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
//
// The output file has been created. Attempt to pre-allocate the file
// based on the file size of the dump. If this fails, then the output
// file cannot be fully created, so bail out now.
//
numberOfPages = memory->NumberOfPages + 1;
allocInfo.AllocationSize.QuadPart = numberOfPages * pageSize;
status = NtSetInformationFile( fileHandle,
&ioStatus,
&allocInfo,
sizeof( allocInfo ),
FileAllocationInformation );
if (!NT_SUCCESS( status )) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to pre-allocate copy of dump file; error = %x\n", status );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
//
// The output file was successfully pre-allocated, so it is possible to
// actually copy the paging file to it. Simply loop, writing chunks of
// the paging file to the output file, maximizing w/64kb at a time,
// until the entire file has been copied.
//
//
// Attempt to map the entire file. If this fails, back off and map
// a small chunk of the file that can be written to the output file.
// Note that if the allocation size of the file is greater than 4GB,
// then this will not work anyway, so don't even try to map the file.
//
partialMap = FALSE;
sectionOffset.QuadPart = 0;
if (!allocInfo.AllocationSize.HighPart) {
viewSize = pageSize * numberOfPages;
bytes = NULL;
status = NtMapViewOfSection( sectionHandle,
NtCurrentProcess(),
(PVOID *) &bytes,
0,
0,
§ionOffset,
&viewSize,
ViewShare,
0,
PAGE_READONLY );
if (viewSize < pageSize * numberOfPages) {
partialMap = TRUE;
}
} else {
status = STATUS_INSUFFICIENT_RESOURCES;
}
if (!NT_SUCCESS( status )) {
partialMap = TRUE;
//
// Map only 4MB of the file. If this fails, then something is
// seriously wrong, so bail out.
//
viewSize = 65536 * 64;
bytes = NULL;
status = NtMapViewOfSection( sectionHandle,
NtCurrentProcess(),
(PVOID *) &bytes,
0,
0,
§ionOffset,
&viewSize,
ViewShare,
0,
PAGE_READONLY );
if (!NT_SUCCESS( status )) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to map 4MB of paging file; error = %x\n", status );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
}
//
// Begin writing to the output file wherever the paging file was mapped
// in the address space. Continue until there are no more bytes left
// to write.
//
sectionBase = bytes;
while (allocInfo.AllocationSize.QuadPart) {
//
// Write as much of the view as possible, up to 64kb.
//
if (!WriteFile( fileHandle,
bytes,
viewSize > 65536 ? 65536 : viewSize,
&returnedLength,
(LPOVERLAPPED) NULL )) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Error writing file; error = %d\n", GetLastError() );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
//
// Account for the number of bytes just written.
//
if (viewSize > 65536) {
viewSize -= 65536;
bytes += 65536;
allocInfo.AllocationSize.QuadPart -= 65536;
} else {
allocInfo.AllocationSize.QuadPart -= viewSize;
bytes += viewSize;
viewSize = 0;
}
//
// If a partial view was mapped to the file and all of the bytes in
// that view have now been written, map the next portion of the file
// if there is any more to write.
//
if (!viewSize && partialMap && allocInfo.AllocationSize.QuadPart) {
//
// Unmap the current view to the section and map the next piece.
//
NtUnmapViewOfSection( NtCurrentProcess(),
sectionBase );
viewSize = 65536 * 64;
sectionOffset.QuadPart += viewSize;
bytes = NULL;
NtMapViewOfSection( sectionHandle,
NtCurrentProcess(),
(PVOID *) &bytes,
0,
0,
§ionOffset,
&viewSize,
ViewShare,
0,
PAGE_READONLY );
sectionBase = bytes;
}
}
//
// The entire file has now been copied. Overwrite the header of the
// file using the in-memory copy, after making it a valid dump.
//
viewBase = (PDUMP_HEADER) &HeaderBuffer[0];
viewBase->ValidDump = 'PMUD';
sectionOffset.LowPart = 0;
SetFilePointer( fileHandle,
0,
§ionOffset.LowPart,
FILE_BEGIN );
if (!WriteFile( fileHandle,
viewBase,
pageSize,
&returnedLength,
(LPOVERLAPPED) NULL )) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Error writing file; error = %d\n", GetLastError() );
}
#endif // DBG
crashDumpEnabled = FALSE;
goto fileDone;
}
}
fileDone:
if (fileHandle != INVALID_HANDLE_VALUE) {
CloseHandle( fileHandle );
}
//
// If either an alert is to be sent or an event is to be logged, format
// the bugcheck parameter information.
//
if (sendAlert || logEvent) {
sprintf( buffer1,
"0x%08x (0x%08x, 0x%08x, 0x%08x, 0x%08x)",
viewBase->BugCheckCode,
viewBase->BugCheckParameter1,
viewBase->BugCheckParameter2,
viewBase->BugCheckParameter3,
viewBase->BugCheckParameter4 );
RtlInitAnsiString( &ansiString1, buffer1 );
RtlAnsiStringToUnicodeString( &unicodeString1, &ansiString1, TRUE );
sprintf( buffer2,
"Microsoft Windows NT [v%ld.%ld]",
viewBase->MajorVersion,
viewBase->MinorVersion );
RtlInitAnsiString( &ansiString2, buffer2 );
RtlAnsiStringToUnicodeString( &unicodeString2, &ansiString2, TRUE );
}
//
// If an event is to be logged, log it now.
//
if (logEvent) {
HANDLE logHandle;
LPWSTR stringArray[3];
BOOLEAN savedDump;
//
// Register w/the event log service so that events can be logged.
//
winStatus = 0;
i = 0;
do {
logHandle = RegisterEventSource( (LPWSTR) NULL,
L"Save Dump" );
if (!logHandle) {
winStatus = GetLastError();
if (winStatus == RPC_S_SERVER_UNAVAILABLE) {
if (i++ > 20) {
break;
}
#if DBG
if (SdDebug & 4 && ((i & 3) == 0)) {
DbgPrint( "SAVEDUMP: Waiting for event logger...\n" );
}
#endif // DBG
Sleep( 15000 );
}
} else {
winStatus = ERROR_SUCCESS;
}
} while (winStatus == RPC_S_SERVER_UNAVAILABLE);
if (!logHandle) {
#if DBG
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to register event source; error = %d\n", GetLastError() );
}
#endif // DBG
} else {
//
// Set up the parameters based on whether a full crash or summary
// was taken.
//
stringArray[0] = unicodeString1.Buffer;
stringArray[1] = unicodeString2.Buffer;
savedDump = (crashDumpEnabled && !summary);
if (savedDump) {
stringArray[2] = expandedName;
}
//
// Report the appropriate event.
//
winStatus = 0;
winStatus = ReportEvent( logHandle,
EVENTLOG_INFORMATION_TYPE,
0,
savedDump ? EVENT_BUGCHECK_SAVED : EVENT_BUGCHECK,
(PSID) NULL,
(WORD) (savedDump ? 3 : 2),
0,
stringArray,
(LPVOID) NULL );
#if DBG
if (!winStatus) {
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to report event; error = %d\n", GetLastError() );
}
}
#endif // DBG
}
}
//
// If an alert is to be raised, raise it now.
//
if (sendAlert) {
PADMIN_OTHER_INFO adminInfo;
DWORD adminInfoSize;
//
// Set up the administrator information variables for processing the
// buffer.
//
adminInfo = (PADMIN_OTHER_INFO) VariableInfo;
adminInfoSize = sizeof( ADMIN_OTHER_INFO );
//
// Format the bugcheck information into the appropriate message format.
//
RtlCopyMemory( (LPWSTR) ((PCHAR) adminInfo + adminInfoSize),
unicodeString1.Buffer,
unicodeString1.Length );
adminInfoSize += unicodeString1.Length + sizeof( WCHAR );
RtlCopyMemory( (LPWSTR) ((PCHAR) adminInfo + adminInfoSize),
unicodeString2.Buffer,
unicodeString2.Length );
adminInfoSize += unicodeString2.Length + sizeof( WCHAR );
//
// Set up the administrator alert information according to the type of
// dump that was taken.
//
if (crashDumpEnabled && !summary) {
adminInfo->alrtad_errcode = ALERT_BugCheckSaved;
adminInfo->alrtad_numstrings = 3;
wcscpy( (LPWSTR) ((PCHAR) adminInfo + adminInfoSize), expandedName );
adminInfoSize += ((wcslen( expandedName ) + 1) * sizeof( WCHAR ));
} else {
adminInfo->alrtad_errcode = ALERT_BugCheck;
adminInfo->alrtad_numstrings = 2;
}
//
// Get the name of the computer and insert it into the buffer.
//
returnedLength = sizeof( VariableInfo ) - adminInfoSize;
winStatus = GetComputerName( (LPWSTR) ((PCHAR) adminInfo + adminInfoSize),
&returnedLength );
returnedLength = ((returnedLength + 1) * sizeof( WCHAR ));
adminInfoSize += returnedLength;
//
// Raise the alert.
//
i = 0;
do {
winStatus = NetAlertRaiseEx( ALERT_ADMIN_EVENT,
adminInfo,
adminInfoSize,
L"SAVEDUMP" );
if (winStatus) {
if (winStatus == ERROR_FILE_NOT_FOUND) {
if (i++ > 20) {
break;
}
#if DBG
if (SdDebug & 4 && ((i & 3) == 0)) {
DbgPrint( "SAVEDUMP: Waiting for alerter...\n" );
}
#endif // DBG
Sleep( 15000 );
}
}
} while (winStatus == ERROR_FILE_NOT_FOUND);
#if DBG
if (winStatus) {
if (SdDebug) {
DbgPrint( "SAVEDUMP: Unable to raise alert; error = %d\n", winStatus );
}
}
#endif // DBG
}
}
|