2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Run-Time Library
4 * FILE: ntoskrnl/rtl/dbgprint.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
!= -1) && !(NtQueryDebugFilterState(ComponentId
, Level
)))
66 /* This message is masked */
70 /* For user mode, don't recursively DbgPrint */
71 if (RtlpSetInDbgPrint(TRUE
)) return Status
;
73 /* Guard against incorrect pointers */
76 /* Get the length and normalize it */
77 PrefixLength
= strlen(Prefix
);
78 if (PrefixLength
> sizeof(Buffer
)) PrefixLength
= sizeof(Buffer
);
81 strncpy(Buffer
, Prefix
, PrefixLength
);
84 Length
= _vsnprintf(Buffer
+ PrefixLength
,
85 sizeof(Buffer
) - PrefixLength
,
92 Length
= PrefixLength
= 0;
93 Status
= _SEH_GetExceptionCode();
96 if (!NT_SUCCESS(Status
)) return Status
;
98 /* Check if we went past the buffer */
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
,
207 /* Call the internal routine that also handles ControlC */
208 va_start(ap
, Format
);
209 return vDbgPrintExWithPrefixInternal("",
223 DbgPrintEx(IN ULONG ComponentId
,
230 /* Call the internal routine that also handles ControlC */
231 va_start(ap
, Format
);
232 return vDbgPrintExWithPrefixInternal("",
246 DbgPrintReturnControlC(PCH Format
,
251 /* Call the internal routine that also handles ControlC */
252 va_start(ap
, Format
);
253 return vDbgPrintExWithPrefixInternal("",
266 DbgPrompt(IN PCCH Prompt
,
268 IN ULONG MaximumResponseLength
)
273 /* Setup the input string */
274 Input
.MaximumLength
= (USHORT
)MaximumResponseLength
;
275 Input
.Buffer
= Response
;
277 /* Setup the output string */
278 Output
.Length
= strlen(Prompt
);
279 Output
.Buffer
= Prompt
;
281 /* Call the system service */
282 return DebugPrompt(&Output
, &Input
);
290 DbgQueryDebugFilterState(IN ULONG ComponentId
,
293 /* Call the Nt routine */
294 return NtQueryDebugFilterState(ComponentId
, Level
);
302 DbgSetDebugFilterState(IN ULONG ComponentId
,
306 /* Call the Nt routine */
307 return NtSetDebugFilterState(ComponentId
, Level
, State
);
315 DbgLoadImageSymbols(IN PANSI_STRING Name
,
317 IN ULONG_PTR ProcessId
)
319 PIMAGE_NT_HEADERS NtHeader
;
320 KD_SYMBOLS_INFO SymbolInfo
;
322 /* Setup the symbol data */
323 SymbolInfo
.BaseOfDll
= Base
;
324 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
327 NtHeader
= RtlImageNtHeader(Base
);
330 /* Get the rest of the data */
331 SymbolInfo
.CheckSum
= NtHeader
->OptionalHeader
.CheckSum
;
332 SymbolInfo
.SizeOfImage
= NtHeader
->OptionalHeader
.SizeOfImage
;
336 /* No data available */
337 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
340 /* Load the symbols */
341 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_LOAD_SYMBOLS
);
342 return STATUS_SUCCESS
;
350 DbgUnLoadImageSymbols(IN PANSI_STRING Name
,
352 IN ULONG_PTR ProcessId
)
354 KD_SYMBOLS_INFO SymbolInfo
;
356 /* Setup the symbol data */
357 SymbolInfo
.BaseOfDll
= Base
;
358 SymbolInfo
.ProcessId
= (ULONG
)ProcessId
;
359 SymbolInfo
.CheckSum
= SymbolInfo
.SizeOfImage
= 0;
361 /* Load the symbols */
362 DebugService2(Name
, &SymbolInfo
, BREAKPOINT_UNLOAD_SYMBOLS
);