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)
10 /* INCLUDES *****************************************************************/
17 /* PRIVATE FUNCTIONS ********************************************************/
21 DebugPrint(IN PANSI_STRING DebugString
,
25 /* Call the INT2D Service */
26 return DebugService(BREAKPOINT_PRINT
,
29 UlongToPtr(ComponentId
),
35 DebugPrompt(IN PCSTRING Output
,
38 /* Call the INT2D Service */
39 return DebugService(BREAKPOINT_PROMPT
,
43 UlongToPtr(Input
->MaximumLength
));
46 /* FUNCTIONS ****************************************************************/
50 vDbgPrintExWithPrefixInternal(IN LPCSTR Prefix
,
55 IN BOOLEAN HandleBreakpoint
)
57 NTSTATUS Status
= STATUS_SUCCESS
;
58 ANSI_STRING DebugString
;
60 ULONG Length
, PrefixLength
;
61 EXCEPTION_RECORD ExceptionRecord
;
63 /* Check if we should print it or not */
64 if ((ComponentId
!= -1U) &&
65 !(NtQueryDebugFilterState(ComponentId
, Level
)))
67 /* This message is masked */
71 /* For user mode, don't recursively DbgPrint */
72 if (RtlpSetInDbgPrint(TRUE
)) return Status
;
74 /* Guard against incorrect pointers */
77 /* Get the length and normalize it */
78 PrefixLength
= strlen(Prefix
);
79 if (PrefixLength
> sizeof(Buffer
)) PrefixLength
= sizeof(Buffer
);
82 strncpy(Buffer
, Prefix
, PrefixLength
);
85 Length
= _vsnprintf(Buffer
+ PrefixLength
,
86 sizeof(Buffer
) - PrefixLength
,
90 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
93 Length
= PrefixLength
= 0;
94 Status
= _SEH2_GetExceptionCode();
97 if (!NT_SUCCESS(Status
)) return Status
;
99 /* Check if we went past the buffer */
102 /* Terminate it if we went over-board */
103 Buffer
[sizeof(Buffer
) - 1] = '\n';
106 Length
= sizeof(Buffer
);
111 Length
+= PrefixLength
;
114 /* Build the string */
115 DebugString
.Length
= Length
;
116 DebugString
.Buffer
= Buffer
;
118 /* First, let the debugger know as well */
119 if (RtlpCheckForActiveDebugger(FALSE
))
121 /* Fill out an exception record */
122 ExceptionRecord
.ExceptionCode
= DBG_PRINTEXCEPTION_C
;
123 ExceptionRecord
.ExceptionRecord
= NULL
;
124 ExceptionRecord
.NumberParameters
= 2;
125 ExceptionRecord
.ExceptionFlags
= 0;
126 ExceptionRecord
.ExceptionInformation
[0] = DebugString
.Length
+ 1;
127 ExceptionRecord
.ExceptionInformation
[1] = (ULONG_PTR
)DebugString
.Buffer
;
129 /* Raise the exception */
130 RtlRaiseException(&ExceptionRecord
);
132 /* This code only runs in user-mode, so setting the flag is safe */
133 NtCurrentTeb()->InDbgPrint
= FALSE
;
134 return STATUS_SUCCESS
;
137 /* Call the Debug Print routine */
138 Status
= DebugPrint(&DebugString
, ComponentId
, Level
);
140 /* Check if this was with Control-C */
141 if (HandleBreakpoint
)
143 /* Check if we got a breakpoint */
144 if (Status
== STATUS_BREAKPOINT
)
147 //DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
148 Status
= STATUS_SUCCESS
;
152 /* In user-mode, remove the InDbgPrint Flag */
153 RtlpSetInDbgPrint(FALSE
);
164 vDbgPrintExWithPrefix(IN LPCSTR Prefix
,
165 IN ULONG ComponentId
,
170 /* Call the internal routine that also handles ControlC */
171 return vDbgPrintExWithPrefixInternal(Prefix
,
184 vDbgPrintEx(IN ULONG ComponentId
,
189 /* Call the internal routine that also handles ControlC */
190 return vDbgPrintExWithPrefixInternal("",
203 DbgPrint(PCCH Format
,
209 /* Call the internal routine that also handles ControlC */
210 va_start(ap
, Format
);
211 n
= vDbgPrintExWithPrefixInternal("",
226 DbgPrintEx(IN ULONG ComponentId
,
234 /* Call the internal routine that also handles ControlC */
235 va_start(ap
, Format
);
236 n
= vDbgPrintExWithPrefixInternal("",
251 DbgPrintReturnControlC(PCH Format
,
257 /* Call the internal routine that also handles ControlC */
258 va_start(ap
, Format
);
259 n
= vDbgPrintExWithPrefixInternal("",
274 DbgPrompt(IN PCCH Prompt
,
276 IN ULONG MaximumResponseLength
)
281 /* Setup the input string */
282 Input
.MaximumLength
= (USHORT
)MaximumResponseLength
;
283 Input
.Buffer
= Response
;
285 /* Setup the output string */
286 Output
.Length
= strlen(Prompt
);
287 Output
.Buffer
= Prompt
;
289 /* Call the system service */
290 return DebugPrompt(&Output
, &Input
);
298 DbgQueryDebugFilterState(IN ULONG ComponentId
,
301 /* Call the Nt routine */
302 return NtQueryDebugFilterState(ComponentId
, Level
);
310 DbgSetDebugFilterState(IN ULONG ComponentId
,
314 /* Call the Nt routine */
315 return NtSetDebugFilterState(ComponentId
, Level
, State
);
323 DbgLoadImageSymbols(IN PANSI_STRING Name
,
325 IN ULONG_PTR ProcessId
)
327 PIMAGE_NT_HEADERS NtHeader
;
328 KD_SYMBOLS_INFO SymbolInfo
;
330 /* Setup the symbol data */
331 SymbolInfo
.BaseOfDll
= Base
;
332 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
335 NtHeader
= RtlImageNtHeader(Base
);
338 /* Get the rest of the data */
339 SymbolInfo
.CheckSum
= NtHeader
->OptionalHeader
.CheckSum
;
340 SymbolInfo
.SizeOfImage
= NtHeader
->OptionalHeader
.SizeOfImage
;
344 /* No data available */
345 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
348 /* Load the symbols */
349 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_LOAD_SYMBOLS
);
350 return STATUS_SUCCESS
;
358 DbgUnLoadImageSymbols(IN PANSI_STRING Name
,
360 IN ULONG_PTR ProcessId
)
362 KD_SYMBOLS_INFO SymbolInfo
;
364 /* Setup the symbol data */
365 SymbolInfo
.BaseOfDll
= Base
;
366 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
367 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
369 /* Load the symbols */
370 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_UNLOAD_SYMBOLS
);