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
)
58 ANSI_STRING DebugString
;
60 ULONG Length
, PrefixLength
;
61 EXCEPTION_RECORD ExceptionRecord
;
63 /* Check if we should print it or not */
64 if ((ComponentId
!= MAXULONG
) &&
65 !(NtQueryDebugFilterState(ComponentId
, Level
)))
67 /* This message is masked */
68 return STATUS_SUCCESS
;
71 /* For user mode, don't recursively DbgPrint */
72 if (RtlpSetInDbgPrint(TRUE
)) return STATUS_SUCCESS
;
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 _SEH2_YIELD(return _SEH2_GetExceptionCode());
98 /* Check if we went past the buffer */
99 if (Length
== MAXULONG
)
101 /* Terminate it if we went over-board */
102 Buffer
[sizeof(Buffer
) - 1] = '\n';
105 Length
= sizeof(Buffer
);
110 Length
+= PrefixLength
;
113 /* Build the string */
114 DebugString
.Length
= Length
;
115 DebugString
.Buffer
= Buffer
;
117 /* First, let the debugger know as well */
118 if (RtlpCheckForActiveDebugger(FALSE
))
120 /* Fill out an exception record */
121 ExceptionRecord
.ExceptionCode
= DBG_PRINTEXCEPTION_C
;
122 ExceptionRecord
.ExceptionRecord
= NULL
;
123 ExceptionRecord
.NumberParameters
= 2;
124 ExceptionRecord
.ExceptionFlags
= 0;
125 ExceptionRecord
.ExceptionInformation
[0] = DebugString
.Length
+ 1;
126 ExceptionRecord
.ExceptionInformation
[1] = (ULONG_PTR
)DebugString
.Buffer
;
128 /* Raise the exception */
129 RtlRaiseException(&ExceptionRecord
);
131 /* This code only runs in user-mode, so setting the flag is safe */
132 NtCurrentTeb()->InDbgPrint
= FALSE
;
133 return STATUS_SUCCESS
;
136 /* Call the Debug Print routine */
137 Status
= DebugPrint(&DebugString
, ComponentId
, Level
);
139 /* Check if this was with Control-C */
140 if (HandleBreakpoint
)
142 /* Check if we got a breakpoint */
143 if (Status
== STATUS_BREAKPOINT
)
146 //DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
147 Status
= STATUS_SUCCESS
;
151 /* In user-mode, remove the InDbgPrint Flag */
152 RtlpSetInDbgPrint(FALSE
);
163 vDbgPrintExWithPrefix(IN LPCSTR Prefix
,
164 IN ULONG ComponentId
,
169 /* Call the internal routine that also handles ControlC */
170 return vDbgPrintExWithPrefixInternal(Prefix
,
183 vDbgPrintEx(IN ULONG ComponentId
,
188 /* Call the internal routine that also handles ControlC */
189 return vDbgPrintExWithPrefixInternal("",
202 DbgPrint(PCCH Format
,
208 /* Call the internal routine that also handles ControlC */
209 va_start(ap
, Format
);
210 n
= vDbgPrintExWithPrefixInternal("",
225 DbgPrintEx(IN ULONG ComponentId
,
233 /* Call the internal routine that also handles ControlC */
234 va_start(ap
, Format
);
235 n
= vDbgPrintExWithPrefixInternal("",
250 DbgPrintReturnControlC(PCH Format
,
256 /* Call the internal routine that also handles ControlC */
257 va_start(ap
, Format
);
258 n
= vDbgPrintExWithPrefixInternal("",
273 DbgPrompt(IN PCCH Prompt
,
275 IN ULONG MaximumResponseLength
)
280 /* Setup the input string */
281 Input
.MaximumLength
= (USHORT
)MaximumResponseLength
;
282 Input
.Buffer
= Response
;
284 /* Setup the output string */
285 Output
.Length
= strlen(Prompt
);
286 Output
.Buffer
= Prompt
;
288 /* Call the system service */
289 return DebugPrompt(&Output
, &Input
);
297 DbgQueryDebugFilterState(IN ULONG ComponentId
,
300 /* Call the Nt routine */
301 return NtQueryDebugFilterState(ComponentId
, Level
);
309 DbgSetDebugFilterState(IN ULONG ComponentId
,
313 /* Call the Nt routine */
314 return NtSetDebugFilterState(ComponentId
, Level
, State
);
322 DbgLoadImageSymbols(IN PANSI_STRING Name
,
324 IN ULONG_PTR ProcessId
)
326 PIMAGE_NT_HEADERS NtHeader
;
327 KD_SYMBOLS_INFO SymbolInfo
;
329 /* Setup the symbol data */
330 SymbolInfo
.BaseOfDll
= Base
;
331 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
334 NtHeader
= RtlImageNtHeader(Base
);
337 /* Get the rest of the data */
338 SymbolInfo
.CheckSum
= NtHeader
->OptionalHeader
.CheckSum
;
339 SymbolInfo
.SizeOfImage
= NtHeader
->OptionalHeader
.SizeOfImage
;
343 /* No data available */
344 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
347 /* Load the symbols */
348 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_LOAD_SYMBOLS
);
356 DbgUnLoadImageSymbols(IN PANSI_STRING Name
,
358 IN ULONG_PTR ProcessId
)
360 KD_SYMBOLS_INFO SymbolInfo
;
362 /* Setup the symbol data */
363 SymbolInfo
.BaseOfDll
= Base
;
364 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
365 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
367 /* Load the symbols */
368 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_UNLOAD_SYMBOLS
);