xref: /reactos/ntoskrnl/kd/kdmain.c (revision 7353af1e)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Kernel
4  * FILE:            ntoskrnl/kd/kdmain.c
5  * PURPOSE:         Kernel Debugger Initialization
6  *
7  * PROGRAMMERS:     Alex Ionescu (alex@relsoft.net)
8  */
9 
10 #include <ntoskrnl.h>
11 #define NDEBUG
12 #include <debug.h>
13 
14 /* VARIABLES ***************************************************************/
15 
16 VOID NTAPI PspDumpThreads(BOOLEAN SystemThreads);
17 
18 extern ANSI_STRING KdpLogFileName;
19 
20 /* PUBLIC FUNCTIONS *********************************************************/
21 
22 static PCHAR
23 NTAPI
24 KdpGetDebugMode(PCHAR Currentp2)
25 {
26     PCHAR p1, p2 = Currentp2;
27     ULONG Value;
28 
29     /* Check for Screen Debugging */
30     if (!_strnicmp(p2, "SCREEN", 6))
31     {
32         /* Enable It */
33         p2 += 6;
34         KdpDebugMode.Screen = TRUE;
35     }
36     /* Check for Serial Debugging */
37     else if (!_strnicmp(p2, "COM", 3))
38     {
39         /* Gheck for a valid Serial Port */
40         p2 += 3;
41         if (*p2 != ':')
42         {
43             Value = (ULONG)atol(p2);
44             if (Value > 0 && Value < 5)
45             {
46                 /* Valid port found, enable Serial Debugging */
47                 KdpDebugMode.Serial = TRUE;
48 
49                 /* Set the port to use */
50                 SerialPortNumber = Value;
51             }
52         }
53         else
54         {
55             Value = strtoul(p2 + 1, NULL, 0);
56             if (Value)
57             {
58                 KdpDebugMode.Serial = TRUE;
59                 SerialPortInfo.Address = UlongToPtr(Value);
60                 SerialPortNumber = 0;
61             }
62         }
63     }
64     /* Check for Debug Log Debugging */
65     else if (!_strnicmp(p2, "FILE", 4))
66     {
67         /* Enable It */
68         p2 += 4;
69         KdpDebugMode.File = TRUE;
70         if (*p2 == ':')
71         {
72             p2++;
73             p1 = p2;
74             while (*p2 != '\0' && *p2 != ' ') p2++;
75             KdpLogFileName.MaximumLength = KdpLogFileName.Length = p2 - p1;
76             KdpLogFileName.Buffer = p1;
77         }
78     }
79 
80     return p2;
81 }
82 
83 NTSTATUS
84 NTAPI
85 KdDebuggerInitialize0(
86     IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
87 {
88     ULONG i;
89     PCHAR CommandLine, Port = NULL;
90 
91     if (LoaderBlock)
92     {
93         /* Check if we have a command line */
94         CommandLine = LoaderBlock->LoadOptions;
95         if (CommandLine)
96         {
97             /* Upcase it */
98             _strupr(CommandLine);
99 
100 #ifdef KDBG
101             /* Get the KDBG Settings */
102             KdbpGetCommandLineSettings(CommandLine);
103 #endif
104 
105             /* Get the port */
106             Port = strstr(CommandLine, "DEBUGPORT");
107         }
108     }
109 
110     /* Check if we got the /DEBUGPORT parameter(s) */
111     while (Port)
112     {
113         /* Move past the actual string, to reach the port*/
114         Port += sizeof("DEBUGPORT") - 1;
115 
116         /* Now get past any spaces and skip the equal sign */
117         while (*Port == ' ') Port++;
118         Port++;
119 
120         /* Get the debug mode and wrapper */
121         Port = KdpGetDebugMode(Port);
122         Port = strstr(Port, "DEBUGPORT");
123     }
124 
125     /* Use serial port then */
126     if (KdpDebugMode.Value == 0)
127         KdpDebugMode.Serial = TRUE;
128 
129     /* Call Providers at Phase 0 */
130     for (i = 0; i < KdMax; i++)
131     {
132         InitRoutines[i](&DispatchTable[i], 0);
133     }
134 
135     return STATUS_SUCCESS;
136 }
137 
138 NTSTATUS
139 NTAPI
140 KdDebuggerInitialize1(
141     IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
142 {
143     PLIST_ENTRY CurrentEntry;
144     PKD_DISPATCH_TABLE CurrentTable;
145 
146     /* Call the registered handlers */
147     CurrentEntry = KdProviders.Flink;
148     while (CurrentEntry != &KdProviders)
149     {
150         /* Get the current table */
151         CurrentTable = CONTAINING_RECORD(CurrentEntry,
152                                          KD_DISPATCH_TABLE,
153                                          KdProvidersList);
154 
155         /* Call it */
156         CurrentTable->KdpInitRoutine(CurrentTable, 1);
157 
158         /* Next Table */
159         CurrentEntry = CurrentEntry->Flink;
160     }
161 
162     NtGlobalFlag |= FLG_STOP_ON_EXCEPTION;
163 
164     return STATUS_SUCCESS;
165 }
166 
167  /* EOF */
168