ReactOS 0.4.15-dev-7887-g64a59a1
debug.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Run-Time Library
4 * FILE: lib/rtl/debug.c
5 * PURPOSE: Debug Print and Prompt routines
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 * Royce Mitchel III
8 */
9
10/* INCLUDES *****************************************************************/
11
12#include <rtl.h>
13
14#include <ndk/kdfuncs.h>
15
16#define NDEBUG
17#include <debug.h>
18
19/* PRIVATE FUNCTIONS ********************************************************/
20
26{
27 /* Call the Debug Service */
29 DebugString->Buffer,
30 UlongToPtr(DebugString->Length),
33}
34
39{
40 /* Call the Debug Service */
42 Output->Buffer,
43 UlongToPtr(Output->Length),
44 Input->Buffer,
45 UlongToPtr(Input->MaximumLength));
46}
47
48/* FUNCTIONS ****************************************************************/
49
57 IN BOOLEAN HandleBreakpoint)
58{
61 CHAR Buffer[512];
62 SIZE_T Length, PrefixLength;
63 EXCEPTION_RECORD ExceptionRecord;
64
65 /* Check if we should print it or not */
66 if ((ComponentId != MAXULONG) &&
68 {
69 /* This message is masked */
70 return STATUS_SUCCESS;
71 }
72
73 /* For user mode, don't recursively DbgPrint */
75
76 /* Guard against incorrect pointers */
78 {
79 /* Get the length and normalize it */
80 PrefixLength = strlen(Prefix);
81 if (PrefixLength > sizeof(Buffer)) PrefixLength = sizeof(Buffer);
82
83 /* Copy it */
84 strncpy(Buffer, Prefix, PrefixLength);
85
86 /* Do the printf */
87 Length = _vsnprintf(Buffer + PrefixLength,
88 sizeof(Buffer) - PrefixLength,
89 Format,
90 ap);
91 }
93 {
94 /* In user-mode, clear the InDbgPrint Flag */
96 /* Fail */
98 }
100
101 /* Check if we went past the buffer */
102 if (Length == MAXULONG)
103 {
104 /* Terminate it if we went over-board */
105 Buffer[sizeof(Buffer) - 1] = '\n';
106
107 /* Put maximum */
108 Length = sizeof(Buffer);
109 }
110 else
111 {
112 /* Add the prefix */
113 Length += PrefixLength;
114 }
115
116 /* Build the string */
117 DebugString.Length = (USHORT)Length;
118 DebugString.Buffer = Buffer;
119
120 /* First, let the debugger know as well */
122 {
123 /* Fill out an exception record */
124 ExceptionRecord.ExceptionCode = DBG_PRINTEXCEPTION_C;
125 ExceptionRecord.ExceptionRecord = NULL;
126 ExceptionRecord.NumberParameters = 2;
127 ExceptionRecord.ExceptionFlags = 0;
128 ExceptionRecord.ExceptionInformation[0] = DebugString.Length + 1;
129 ExceptionRecord.ExceptionInformation[1] = (ULONG_PTR)DebugString.Buffer;
130
131 /* Raise the exception */
132 RtlRaiseException(&ExceptionRecord);
133
134 /* In user-mode, clear the InDbgPrint Flag */
136 return STATUS_SUCCESS;
137 }
138
139 /* Call the Debug Print routine */
141
142 /* Check if this was with Control-C */
143 if (HandleBreakpoint)
144 {
145 /* Check if we got a breakpoint */
147 {
148 /* Breakpoint */
151 }
152 }
153
154 /* In user-mode, clear the InDbgPrint Flag */
156
157 /* Return */
158 return Status;
159}
160
161/*
162 * @implemented
163 */
164ULONG
165NTAPI
168 IN ULONG Level,
169 IN PCCH Format,
170 IN va_list ap)
171{
172 /* Call the internal routine that also handles ControlC */
175 Level,
176 Format,
177 ap,
178 TRUE);
179}
180
181/*
182 * @implemented
183 */
184ULONG
185NTAPI
187 IN ULONG Level,
188 IN PCCH Format,
189 IN va_list ap)
190{
191 /* Call the internal routine that also handles ControlC */
194 Level,
195 Format,
196 ap,
197 TRUE);
198}
199
200/*
201 * @implemented
202 */
203ULONG
206 ...)
207{
209 va_list ap;
210
211 /* Call the internal routine that also handles ControlC */
214 -1,
216 Format,
217 ap,
218 TRUE);
219 va_end(ap);
220 return Status;
221}
222
223/*
224 * @implemented
225 */
226ULONG
229 IN ULONG Level,
230 IN PCCH Format,
231 ...)
232{
234 va_list ap;
235
236 /* Call the internal routine that also handles ControlC */
240 Level,
241 Format,
242 ap,
243 TRUE);
244 va_end(ap);
245 return Status;
246}
247
248/*
249 * @implemented
250 */
251ULONG
254 ...)
255{
257 va_list ap;
258
259 /* Call the internal routine that also handles ControlC */
262 -1,
264 Format,
265 ap,
266 FALSE);
267 va_end(ap);
268 return Status;
269}
270
271/*
272 * @implemented
273 */
274ULONG
275NTAPI
279{
282
283 /* Setup the input string */
284 Input.MaximumLength = (USHORT)MaximumResponseLength;
285 Input.Buffer = Response;
286
287 /* Setup the output string */
288 Output.Length = (USHORT)strlen(Prompt);
289 Output.Buffer = (PCHAR)Prompt;
290
291 /* Call the system service */
292 return DebugPrompt(&Output, &Input);
293}
294
295/*
296 * @implemented
297 */
299NTAPI
301 IN ULONG Level)
302{
303 /* Call the Nt routine */
305}
306
307/*
308 * @implemented
309 */
311NTAPI
313 IN ULONG Level,
315{
316 /* Call the Nt routine */
318}
319
320/*
321 * @implemented
322 */
323VOID
324NTAPI
326 IN PVOID Base,
328{
329 PIMAGE_NT_HEADERS NtHeader;
330 KD_SYMBOLS_INFO SymbolInfo;
331
332 /* Setup the symbol data */
333 SymbolInfo.BaseOfDll = Base;
334 SymbolInfo.ProcessId = ProcessId;
335
336 /* Get NT Headers */
337 NtHeader = RtlImageNtHeader(Base);
338 if (NtHeader)
339 {
340 /* Get the rest of the data */
341 SymbolInfo.CheckSum = NtHeader->OptionalHeader.CheckSum;
342 SymbolInfo.SizeOfImage = NtHeader->OptionalHeader.SizeOfImage;
343 }
344 else
345 {
346 /* No data available */
347 SymbolInfo.CheckSum =
348 SymbolInfo.SizeOfImage = 0;
349 }
350
351 /* Load the symbols */
353}
354
355/*
356 * @implemented
357 */
358VOID
359NTAPI
361 IN PVOID Base,
363{
364 KD_SYMBOLS_INFO SymbolInfo;
365
366 /* Setup the symbol data */
367 SymbolInfo.BaseOfDll = Base;
368 SymbolInfo.ProcessId = ProcessId;
369 SymbolInfo.CheckSum = SymbolInfo.SizeOfImage = 0;
370
371 /* Load the symbols */
373}
374
375/*
376 * @implemented
377 */
378VOID
379NTAPI
382{
383 STRING NameString, CommandString;
384
385 /* Setup the strings */
386 NameString.Buffer = (PCHAR)Name;
387 NameString.Length = (USHORT)strlen(Name);
388 CommandString.Buffer = (PCHAR)Command;
389 CommandString.Length = (USHORT)strlen(Command);
390
391 /* Send them to the debugger */
392 DebugService2(&NameString, &CommandString, BREAKPOINT_COMMAND_STRING);
393}
394
395/*
396* @implemented
397*/
398VOID
399NTAPI
401{
402 /* Restore the previous frame as the active one */
403 NtCurrentTeb()->ActiveFrame = Frame->Previous;
404}
405
406/*
407* @implemented
408*/
409VOID
410NTAPI
412{
413 /* Save the current frame and set the new one as active */
414 Frame->Previous = NtCurrentTeb()->ActiveFrame;
415 NtCurrentTeb()->ActiveFrame = Frame;
416}
417
419NTAPI
421{
422 /* Return the frame that's currently active */
423 return NtCurrentTeb()->ActiveFrame;
424}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
#define __cdecl
Definition: accygwin.h:79
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char UINT32 ComponentId
Definition: acpixf.h:1281
NTSYSAPI ULONG NTAPI vDbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ PCCH Format, _In_ va_list ap)
#define DPFLTR_ERROR_LEVEL
Definition: main.cpp:32
LONG NTSTATUS
Definition: precomp.h:26
Definition: bufpool.h:45
#define DebugPrint(x)
Definition: classpnp.h:125
CCHAR DebugString[256]
Definition: cmdline.c:22
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOLEAN NTAPI RtlpCheckForActiveDebugger(VOID)
Definition: libsupp.c:25
VOID NTAPI RtlpClearInDbgPrint(VOID)
Definition: libsupp.c:45
BOOLEAN NTAPI RtlpSetInDbgPrint(VOID)
Definition: libsupp.c:33
#define RtlImageNtHeader
Definition: compat.h:806
#define UlongToPtr(u)
Definition: config.h:106
#define ULONG_PTR
Definition: config.h:101
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2711
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
std::wstring STRING
Definition: fontsub.cpp:33
Status
Definition: gdiplustypes.h:25
#define DbgPrint
Definition: hal.h:12
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
NTSYSAPI TEB_ACTIVE_FRAME *WINAPI RtlGetFrame(void)
Definition: debug.c:420
#define NtCurrentTeb
NTSTATUS NTAPI NtQueryDebugFilterState(_In_ ULONG ComponentId, _In_ ULONG Level)
Definition: kdapi.c:2357
NTSTATUS NTAPI NtSetDebugFilterState(_In_ ULONG ComponentId, _In_ ULONG Level, _In_ BOOLEAN State)
Definition: kdapi.c:2411
#define PCHAR
Definition: match.c:90
#define BREAKPOINT_PRINT
Definition: kdtypes.h:51
#define BREAKPOINT_COMMAND_STRING
Definition: kdtypes.h:55
#define BREAKPOINT_LOAD_SYMBOLS
Definition: kdtypes.h:53
#define BREAKPOINT_UNLOAD_SYMBOLS
Definition: kdtypes.h:54
#define BREAKPOINT_PROMPT
Definition: kdtypes.h:52
#define DBG_STATUS_CONTROL_C
Definition: kdtypes.h:39
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2439
NTSYSAPI VOID NTAPI RtlRaiseException(_In_ PEXCEPTION_RECORD ExceptionRecord)
CHAR * PCH
Definition: ntbasedef.h:391
CONST CHAR * PCCH
Definition: ntbasedef.h:392
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
#define DBG_PRINTEXCEPTION_C
Definition: ntstatus.h:53
unsigned short USHORT
Definition: pedump.c:61
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:159
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:162
ULONG NTAPI DebugService(IN ULONG Service, IN PVOID Argument1, IN PVOID Argument2, IN PVOID Argument3, IN PVOID Argument4)
VOID NTAPI DebugService2(IN PVOID Argument1, IN PVOID Argument2, IN ULONG Service)
@ Input
Definition: arc.h:84
@ Output
Definition: arc.h:85
ULONG NTAPI vDbgPrintExWithPrefix(IN LPCSTR Prefix, IN ULONG ComponentId, IN ULONG Level, IN LPCSTR Format, IN va_list ap)
NTSTATUS NTAPI DbgSetDebugFilterState(IN ULONG ComponentId, IN ULONG Level, IN BOOLEAN State)
Definition: debug.c:312
VOID NTAPI DbgCommandString(IN PCCH Name, IN PCCH Command)
Definition: debug.c:380
ULONG NTAPI vDbgPrintExWithPrefixInternal(IN PCCH Prefix, IN ULONG ComponentId, IN ULONG Level, IN PCCH Format, IN va_list ap, IN BOOLEAN HandleBreakpoint)
Definition: debug.c:52
VOID NTAPI RtlPopFrame(IN PTEB_ACTIVE_FRAME Frame)
Definition: debug.c:400
ULONG __cdecl DbgPrintReturnControlC(PCCH Format,...)
Definition: debug.c:253
ULONG NTAPI DbgPrompt(IN PCCH Prompt, OUT PCH Response, IN ULONG MaximumResponseLength)
Definition: debug.c:276
NTSTATUS NTAPI DbgQueryDebugFilterState(IN ULONG ComponentId, IN ULONG Level)
Definition: debug.c:300
VOID NTAPI RtlPushFrame(IN PTEB_ACTIVE_FRAME Frame)
Definition: debug.c:411
ULONG NTAPI DebugPrompt(IN PSTRING Output, IN PSTRING Input)
Definition: debug.c:37
VOID NTAPI DbgLoadImageSymbols(IN PSTRING Name, IN PVOID Base, IN ULONG_PTR ProcessId)
Definition: debug.c:325
VOID NTAPI DbgUnLoadImageSymbols(IN PSTRING Name, IN PVOID Base, IN ULONG_PTR ProcessId)
Definition: debug.c:360
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: shell.h:41
Definition: ncftp.h:89
struct _EXCEPTION_RECORD * ExceptionRecord
Definition: compat.h:210
DWORD ExceptionCode
Definition: compat.h:208
DWORD NumberParameters
Definition: compat.h:212
DWORD ExceptionFlags
Definition: compat.h:209
ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]
Definition: compat.h:213
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
ULONG_PTR ProcessId
Definition: kdtypes.h:171
ULONG SizeOfImage
Definition: kdtypes.h:173
ULONG CheckSum
Definition: kdtypes.h:172
PVOID BaseOfDll
Definition: kdtypes.h:170
#define MAXULONG
Definition: typedefs.h:251
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
__analysis_noreturn NTSYSAPI VOID NTAPI DbgBreakPointWithStatus(_In_ ULONG Status)
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
_IRQL_requires_same_ typedef _In_ ULONG _In_ UCHAR Level
Definition: wmitypes.h:56
_In_ ULONG MaximumResponseLength
Definition: kdfuncs.h:11
_In_ __drv_aliasesMem PSTRING Prefix
Definition: rtlfuncs.h:1630
#define _vsnprintf
Definition: xmlstorage.h:202
char CHAR
Definition: xmlstorage.h:175