Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygendebug.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS Run-Time Library 00004 * FILE: lib/rtl/debug.c 00005 * PURPOSE: Debug Print and Prompt routines 00006 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net) 00007 * Royce Mitchel III 00008 */ 00009 00010 /* INCLUDES *****************************************************************/ 00011 00012 #include <rtl.h> 00013 00014 #define NDEBUG 00015 #include <debug.h> 00016 00017 /* PRIVATE FUNCTIONS ********************************************************/ 00018 00019 ULONG 00020 NTAPI 00021 DebugPrint(IN PSTRING DebugString, 00022 IN ULONG ComponentId, 00023 IN ULONG Level) 00024 { 00025 /* Call the Debug Service */ 00026 return DebugService(BREAKPOINT_PRINT, 00027 DebugString->Buffer, 00028 UlongToPtr(DebugString->Length), 00029 UlongToPtr(ComponentId), 00030 UlongToPtr(Level)); 00031 } 00032 00033 ULONG 00034 NTAPI 00035 DebugPrompt(IN PSTRING Output, 00036 IN PSTRING Input) 00037 { 00038 /* Call the Debug Service */ 00039 return DebugService(BREAKPOINT_PROMPT, 00040 Output->Buffer, 00041 UlongToPtr(Output->Length), 00042 Input->Buffer, 00043 UlongToPtr(Input->MaximumLength)); 00044 } 00045 00046 /* FUNCTIONS ****************************************************************/ 00047 00048 ULONG 00049 NTAPI 00050 vDbgPrintExWithPrefixInternal(IN PCCH Prefix, 00051 IN ULONG ComponentId, 00052 IN ULONG Level, 00053 IN PCCH Format, 00054 IN va_list ap, 00055 IN BOOLEAN HandleBreakpoint) 00056 { 00057 NTSTATUS Status; 00058 STRING DebugString; 00059 CHAR Buffer[512]; 00060 SIZE_T Length, PrefixLength; 00061 EXCEPTION_RECORD ExceptionRecord; 00062 00063 /* Check if we should print it or not */ 00064 if ((ComponentId != MAXULONG) && 00065 (NtQueryDebugFilterState(ComponentId, Level)) != TRUE) 00066 { 00067 /* This message is masked */ 00068 return STATUS_SUCCESS; 00069 } 00070 00071 /* For user mode, don't recursively DbgPrint */ 00072 if (RtlpSetInDbgPrint()) return STATUS_SUCCESS; 00073 00074 /* Guard against incorrect pointers */ 00075 _SEH2_TRY 00076 { 00077 /* Get the length and normalize it */ 00078 PrefixLength = strlen(Prefix); 00079 if (PrefixLength > sizeof(Buffer)) PrefixLength = sizeof(Buffer); 00080 00081 /* Copy it */ 00082 strncpy(Buffer, Prefix, PrefixLength); 00083 00084 /* Do the printf */ 00085 Length = _vsnprintf(Buffer + PrefixLength, 00086 sizeof(Buffer) - PrefixLength, 00087 Format, 00088 ap); 00089 } 00090 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) 00091 { 00092 /* Fail */ 00093 _SEH2_YIELD(return _SEH2_GetExceptionCode()); 00094 } 00095 _SEH2_END; 00096 00097 /* Check if we went past the buffer */ 00098 if (Length == MAXULONG) 00099 { 00100 /* Terminate it if we went over-board */ 00101 Buffer[sizeof(Buffer) - 1] = '\n'; 00102 00103 /* Put maximum */ 00104 Length = sizeof(Buffer); 00105 } 00106 else 00107 { 00108 /* Add the prefix */ 00109 Length += PrefixLength; 00110 } 00111 00112 /* Build the string */ 00113 DebugString.Length = (USHORT)Length; 00114 DebugString.Buffer = Buffer; 00115 00116 /* First, let the debugger know as well */ 00117 if (RtlpCheckForActiveDebugger()) 00118 { 00119 /* Fill out an exception record */ 00120 ExceptionRecord.ExceptionCode = DBG_PRINTEXCEPTION_C; 00121 ExceptionRecord.ExceptionRecord = NULL; 00122 ExceptionRecord.NumberParameters = 2; 00123 ExceptionRecord.ExceptionFlags = 0; 00124 ExceptionRecord.ExceptionInformation[0] = DebugString.Length + 1; 00125 ExceptionRecord.ExceptionInformation[1] = (ULONG_PTR)DebugString.Buffer; 00126 00127 /* Raise the exception */ 00128 RtlRaiseException(&ExceptionRecord); 00129 00130 /* This code only runs in user-mode, so setting the flag is safe */ 00131 NtCurrentTeb()->InDbgPrint = FALSE; 00132 return STATUS_SUCCESS; 00133 } 00134 00135 /* Call the Debug Print routine */ 00136 Status = DebugPrint(&DebugString, ComponentId, Level); 00137 00138 /* Check if this was with Control-C */ 00139 if (HandleBreakpoint) 00140 { 00141 /* Check if we got a breakpoint */ 00142 if (Status == STATUS_BREAKPOINT) 00143 { 00144 /* Breakpoint */ 00145 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C); 00146 Status = STATUS_SUCCESS; 00147 } 00148 } 00149 00150 /* In user-mode, clear the InDbgPrint Flag */ 00151 RtlpClearInDbgPrint(); 00152 00153 /* Return */ 00154 return Status; 00155 } 00156 00157 /* 00158 * @implemented 00159 */ 00160 ULONG 00161 NTAPI 00162 vDbgPrintExWithPrefix(IN PCCH Prefix, 00163 IN ULONG ComponentId, 00164 IN ULONG Level, 00165 IN PCCH Format, 00166 IN va_list ap) 00167 { 00168 /* Call the internal routine that also handles ControlC */ 00169 return vDbgPrintExWithPrefixInternal(Prefix, 00170 ComponentId, 00171 Level, 00172 Format, 00173 ap, 00174 TRUE); 00175 } 00176 00177 /* 00178 * @implemented 00179 */ 00180 ULONG 00181 NTAPI 00182 vDbgPrintEx(IN ULONG ComponentId, 00183 IN ULONG Level, 00184 IN PCCH Format, 00185 IN va_list ap) 00186 { 00187 /* Call the internal routine that also handles ControlC */ 00188 return vDbgPrintExWithPrefixInternal("", 00189 ComponentId, 00190 Level, 00191 Format, 00192 ap, 00193 TRUE); 00194 } 00195 00196 /* 00197 * @implemented 00198 */ 00199 ULONG 00200 __cdecl 00201 DbgPrint(PCCH Format, 00202 ...) 00203 { 00204 ULONG Status; 00205 va_list ap; 00206 00207 /* Call the internal routine that also handles ControlC */ 00208 va_start(ap, Format); 00209 Status = vDbgPrintExWithPrefixInternal("", 00210 -1, 00211 DPFLTR_ERROR_LEVEL, 00212 Format, 00213 ap, 00214 TRUE); 00215 va_end(ap); 00216 return Status; 00217 } 00218 00219 /* 00220 * @implemented 00221 */ 00222 ULONG 00223 __cdecl 00224 DbgPrintEx(IN ULONG ComponentId, 00225 IN ULONG Level, 00226 IN PCCH Format, 00227 ...) 00228 { 00229 ULONG Status; 00230 va_list ap; 00231 00232 /* Call the internal routine that also handles ControlC */ 00233 va_start(ap, Format); 00234 Status = vDbgPrintExWithPrefixInternal("", 00235 ComponentId, 00236 Level, 00237 Format, 00238 ap, 00239 TRUE); 00240 va_end(ap); 00241 return Status; 00242 } 00243 00244 /* 00245 * @implemented 00246 */ 00247 ULONG 00248 __cdecl 00249 DbgPrintReturnControlC(PCCH Format, 00250 ...) 00251 { 00252 ULONG Status; 00253 va_list ap; 00254 00255 /* Call the internal routine that also handles ControlC */ 00256 va_start(ap, Format); 00257 Status = vDbgPrintExWithPrefixInternal("", 00258 -1, 00259 DPFLTR_ERROR_LEVEL, 00260 Format, 00261 ap, 00262 FALSE); 00263 va_end(ap); 00264 return Status; 00265 } 00266 00267 /* 00268 * @implemented 00269 */ 00270 ULONG 00271 NTAPI 00272 DbgPrompt(IN PCCH Prompt, 00273 OUT PCH Response, 00274 IN ULONG MaximumResponseLength) 00275 { 00276 STRING Output; 00277 STRING Input; 00278 00279 /* Setup the input string */ 00280 Input.MaximumLength = (USHORT)MaximumResponseLength; 00281 Input.Buffer = Response; 00282 00283 /* Setup the output string */ 00284 Output.Length = (USHORT)strlen(Prompt); 00285 Output.Buffer = (PCHAR)Prompt; 00286 00287 /* Call the system service */ 00288 return DebugPrompt(&Output, &Input); 00289 } 00290 00291 /* 00292 * @implemented 00293 */ 00294 NTSTATUS 00295 NTAPI 00296 DbgQueryDebugFilterState(IN ULONG ComponentId, 00297 IN ULONG Level) 00298 { 00299 /* Call the Nt routine */ 00300 return NtQueryDebugFilterState(ComponentId, Level); 00301 } 00302 00303 /* 00304 * @implemented 00305 */ 00306 NTSTATUS 00307 NTAPI 00308 DbgSetDebugFilterState(IN ULONG ComponentId, 00309 IN ULONG Level, 00310 IN BOOLEAN State) 00311 { 00312 /* Call the Nt routine */ 00313 return NtSetDebugFilterState(ComponentId, Level, State); 00314 } 00315 00316 /* 00317 * @implemented 00318 */ 00319 VOID 00320 NTAPI 00321 DbgLoadImageSymbols(IN PSTRING Name, 00322 IN PVOID Base, 00323 IN ULONG_PTR ProcessId) 00324 { 00325 PIMAGE_NT_HEADERS NtHeader; 00326 KD_SYMBOLS_INFO SymbolInfo; 00327 00328 /* Setup the symbol data */ 00329 SymbolInfo.BaseOfDll = Base; 00330 SymbolInfo.ProcessId = ProcessId; 00331 00332 /* Get NT Headers */ 00333 NtHeader = RtlImageNtHeader(Base); 00334 if (NtHeader) 00335 { 00336 /* Get the rest of the data */ 00337 SymbolInfo.CheckSum = NtHeader->OptionalHeader.CheckSum; 00338 SymbolInfo.SizeOfImage = NtHeader->OptionalHeader.SizeOfImage; 00339 } 00340 else 00341 { 00342 /* No data available */ 00343 SymbolInfo.CheckSum = 00344 SymbolInfo.SizeOfImage = 0; 00345 } 00346 00347 /* Load the symbols */ 00348 DebugService2(Name, &SymbolInfo, BREAKPOINT_LOAD_SYMBOLS); 00349 } 00350 00351 /* 00352 * @implemented 00353 */ 00354 VOID 00355 NTAPI 00356 DbgUnLoadImageSymbols(IN PSTRING Name, 00357 IN PVOID Base, 00358 IN ULONG_PTR ProcessId) 00359 { 00360 KD_SYMBOLS_INFO SymbolInfo; 00361 00362 /* Setup the symbol data */ 00363 SymbolInfo.BaseOfDll = Base; 00364 SymbolInfo.ProcessId = ProcessId; 00365 SymbolInfo.CheckSum = SymbolInfo.SizeOfImage = 0; 00366 00367 /* Load the symbols */ 00368 DebugService2(Name, &SymbolInfo, BREAKPOINT_UNLOAD_SYMBOLS); 00369 } 00370 00371 /* 00372 * @implemented 00373 */ 00374 VOID 00375 NTAPI 00376 DbgCommandString(IN PCCH Name, 00377 IN PCCH Command) 00378 { 00379 STRING NameString, CommandString; 00380 00381 /* Setup the strings */ 00382 NameString.Buffer = (PCHAR)Name; 00383 NameString.Length = (USHORT)strlen(Name); 00384 CommandString.Buffer = (PCHAR)Command; 00385 CommandString.Length = (USHORT)strlen(Command); 00386 00387 /* Send them to the debugger */ 00388 DebugService2(&NameString, &CommandString, BREAKPOINT_COMMAND_STRING); 00389 } Generated on Sun May 27 2012 04:17:45 for ReactOS by
1.7.6.1
|