:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / ke / bug.c
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  *
21  * PROJECT:         ReactOS kernel
22  * FILE:            ntoskrnl/ke/bug.c
23  * PURPOSE:         Graceful system shutdown if a bug is detected
24  * PROGRAMMER:      David Welch (welch@cwcom.net)
25  * PORTABILITY:     Unchecked
26  * UPDATE HISTORY:
27  *                  Created 22/05/98
28  *                  Phillip Susi: 12/8/99: Minor fix
29  */
30
31 /* INCLUDES *****************************************************************/
32
33 #include <ddk/ntddk.h>
34 #include <internal/ke.h>
35 #include <internal/ps.h>
36
37 #include <internal/debug.h>
38
39 /* GLOBALS ******************************************************************/
40
41 static LIST_ENTRY BugcheckCallbackListHead = {NULL,NULL};
42 static ULONG InBugCheck;
43
44 VOID PsDumpThreads(VOID);
45
46 /* FUNCTIONS *****************************************************************/
47
48 VOID
49 KeInitializeBugCheck(VOID)
50 {
51   InitializeListHead(&BugcheckCallbackListHead);
52   InBugCheck = 0;
53 }
54
55 BOOLEAN STDCALL
56 KeDeregisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord)
57 {
58   UNIMPLEMENTED;
59 }
60
61 BOOLEAN STDCALL
62 KeRegisterBugCheckCallback(PKBUGCHECK_CALLBACK_RECORD CallbackRecord,
63                            PKBUGCHECK_CALLBACK_ROUTINE  CallbackRoutine,
64                            PVOID Buffer,
65                            ULONG Length,
66                            PUCHAR Component)
67 {
68   InsertTailList(&BugcheckCallbackListHead, &CallbackRecord->Entry);
69   CallbackRecord->Length = Length;
70   CallbackRecord->Buffer = Buffer;
71   CallbackRecord->Component = Component;
72   CallbackRecord->CallbackRoutine = CallbackRoutine;
73   return(TRUE);
74 }
75
76 VOID STDCALL
77 KeBugCheckWithTf(ULONG BugCheckCode,         
78                  ULONG BugCheckParameter1,
79                  ULONG BugCheckParameter2,
80                  ULONG BugCheckParameter3,
81                  ULONG BugCheckParameter4,
82                  PKTRAP_FRAME Tf)
83 {
84   KIRQL oldIrql;
85   KeRaiseIrql(HIGH_LEVEL, &oldIrql);
86   DbgPrint("Bug detected code: 0x%X\n", BugCheckCode);
87   KiDumpTrapFrame(Tf, BugCheckParameter1, BugCheckParameter2);
88   MmDumpToPagingFile(BugCheckCode, BugCheckParameter1, 
89                      BugCheckParameter2, BugCheckParameter3,
90                      BugCheckParameter4, Tf);
91   for(;;);
92 }
93
94 VOID STDCALL
95 KeBugCheckEx(ULONG BugCheckCode,
96              ULONG BugCheckParameter1,
97              ULONG BugCheckParameter2,
98              ULONG BugCheckParameter3,
99              ULONG BugCheckParameter4)
100 /*
101  * FUNCTION: Brings the system down in a controlled manner when an 
102  * inconsistency that might otherwise cause corruption has been detected
103  * ARGUMENTS:
104  *           BugCheckCode = Specifies the reason for the bug check
105  *           BugCheckParameter[1-4] = Additional information about bug
106  * RETURNS: Doesn't
107  */
108 {
109   PRTL_MESSAGE_RESOURCE_ENTRY Message;
110   NTSTATUS Status;
111   
112   __asm__("cli\n\t");
113   DbgPrint("Bug detected (code %x param %x %x %x %x)\n",
114            BugCheckCode,
115            BugCheckParameter1,
116            BugCheckParameter2,
117            BugCheckParameter3,
118            BugCheckParameter4);
119
120   Status = RtlFindMessage((PVOID)KERNEL_BASE, //0xC0000000,
121                           11, //RT_MESSAGETABLE,
122                           0x09, //0x409,
123                           BugCheckCode,
124                           &Message);
125   if (NT_SUCCESS(Status))
126     {
127       if (Message->Flags == 0)
128         DbgPrint("  %s\n", Message->Text);
129       else
130         DbgPrint("  %S\n", (PWSTR)Message->Text);
131     }
132   else
133     {
134       DbgPrint("  No message text found!\n\n");
135     }
136
137   if (InBugCheck == 1)
138     {
139       DbgPrint("Recursive bug check halting now\n");
140       for (;;)
141         {
142           __asm__("hlt\n\t");
143         }
144     }
145   InBugCheck = 1;
146   if (PsGetCurrentProcess() != NULL)
147     {
148       DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId);
149       DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName);
150     }
151   if (PsGetCurrentThread() != NULL)
152     {
153       DbgPrint("Thrd: %x Tid: %x\n",
154                PsGetCurrentThread(),
155                PsGetCurrentThread()->Cid.UniqueThread);
156     }
157   KeDumpStackFrames((PULONG)__builtin_frame_address(0));
158   MmDumpToPagingFile(BugCheckCode, BugCheckParameter1, 
159                      BugCheckParameter2, BugCheckParameter3,
160                      BugCheckParameter4, NULL);
161   
162   if (KdDebuggerEnabled)
163     {
164       __asm__("sti\n\t");
165       DbgBreakPoint();
166     }
167
168   for(;;);
169 }
170
171 VOID STDCALL
172 KeBugCheck(ULONG BugCheckCode)
173 /*
174  * FUNCTION: Brings the system down in a controlled manner when an 
175  * inconsistency that might otherwise cause corruption has been detected
176  * ARGUMENTS:
177  *           BugCheckCode = Specifies the reason for the bug check
178  * RETURNS: Doesn't
179  */
180 {
181   KeBugCheckEx(BugCheckCode, 0, 0, 0, 0);
182 }
183
184 /* EOF */