ReactOS 0.4.17-dev-1321-g5c122a9
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) - 2] = '\n';
106 Buffer[sizeof(Buffer) - 1] = '\0';
107
108 /* Put maximum */
109 Length = sizeof(Buffer) - 1;
110 }
111 else
112 {
113 /* Add the prefix */
114 Length += PrefixLength;
115 }
116
117 /* Build the string */
118 DebugString.Length = (USHORT)Length;
119 DebugString.Buffer = Buffer;
120
121 /* First, let the debugger know as well */
123 {
124 /* Fill out an exception record */
125 ExceptionRecord.ExceptionCode = DBG_PRINTEXCEPTION_C;
126 ExceptionRecord.ExceptionRecord = NULL;
127 ExceptionRecord.NumberParameters = 2;
128 ExceptionRecord.ExceptionFlags = 0;
129 ExceptionRecord.ExceptionInformation[0] = DebugString.Length + 1;
130 ExceptionRecord.ExceptionInformation[1] = (ULONG_PTR)DebugString.Buffer;
131
132 /* Raise the exception */
133 RtlRaiseException(&ExceptionRecord);
134
135 /* In user-mode, clear the InDbgPrint Flag */
137 return STATUS_SUCCESS;
138 }
139
140 /* Call the Debug Print routine */
142
143 /* Check if this was with Control-C */
144 if (HandleBreakpoint)
145 {
146 /* Check if we got a breakpoint */
148 {
149 /* Breakpoint */
152 }
153 }
154
155 /* In user-mode, clear the InDbgPrint Flag */
157
158 /* Return */
159 return Status;
160}
161
162/*
163 * @implemented
164 */
165ULONG
166NTAPI
169 IN ULONG Level,
170 IN PCCH Format,
171 IN va_list ap)
172{
173 /* Call the internal routine that also handles ControlC */
176 Level,
177 Format,
178 ap,
179 TRUE);
180}
181
182/*
183 * @implemented
184 */
185ULONG
186NTAPI
188 IN ULONG Level,
189 IN PCCH Format,
190 IN va_list ap)
191{
192 /* Call the internal routine that also handles ControlC */
195 Level,
196 Format,
197 ap,
198 TRUE);
199}
200
201/*
202 * @implemented
203 */
204ULONG
207 ...)
208{
210 va_list ap;
211
212 /* Call the internal routine that also handles ControlC */
215 -1,
217 Format,
218 ap,
219 TRUE);
220 va_end(ap);
221 return Status;
222}
223
224/*
225 * @implemented
226 */
227ULONG
230 IN ULONG Level,
231 IN PCCH Format,
232 ...)
233{
235 va_list ap;
236
237 /* Call the internal routine that also handles ControlC */
241 Level,
242 Format,
243 ap,
244 TRUE);
245 va_end(ap);
246 return Status;
247}
248
249/*
250 * @implemented
251 */
252ULONG
255 ...)
256{
258 va_list ap;
259
260 /* Call the internal routine that also handles ControlC */
263 -1,
265 Format,
266 ap,
267 FALSE);
268 va_end(ap);
269 return Status;
270}
271
272/*
273 * @implemented
274 */
275ULONG
276NTAPI
280{
283
284 /* Setup the input string */
285 Input.MaximumLength = (USHORT)MaximumResponseLength;
286 Input.Buffer = Response;
287
288 /* Setup the output string */
289 Output.Length = (USHORT)strlen(Prompt);
290 Output.Buffer = (PCHAR)Prompt;
291
292 /* Call the system service */
293 return DebugPrompt(&Output, &Input);
294}
295
296/*
297 * @implemented
298 */
300NTAPI
302 IN ULONG Level)
303{
304 /* Call the Nt routine */
306}
307
308/*
309 * @implemented
310 */
312NTAPI
314 IN ULONG Level,
316{
317 /* Call the Nt routine */
319}
320
321/*
322 * @implemented
323 */
324VOID
325NTAPI
327 IN PVOID Base,
329{
330 PIMAGE_NT_HEADERS NtHeader;
331 KD_SYMBOLS_INFO SymbolInfo;
332
333 /* Setup the symbol data */
334 SymbolInfo.BaseOfDll = Base;
335 SymbolInfo.ProcessId = ProcessId;
336
337 /* Get NT Headers */
338 NtHeader = RtlImageNtHeader(Base);
339 if (NtHeader)
340 {
341 /* Get the rest of the data */
342 SymbolInfo.CheckSum = NtHeader->OptionalHeader.CheckSum;
343 SymbolInfo.SizeOfImage = NtHeader->OptionalHeader.SizeOfImage;
344 }
345 else
346 {
347 /* No data available */
348 SymbolInfo.CheckSum =
349 SymbolInfo.SizeOfImage = 0;
350 }
351
352 /* Load the symbols */
354}
355
356/*
357 * @implemented
358 */
359VOID
360NTAPI
362 IN PVOID Base,
364{
365 KD_SYMBOLS_INFO SymbolInfo;
366
367 /* Setup the symbol data */
368 SymbolInfo.BaseOfDll = Base;
369 SymbolInfo.ProcessId = ProcessId;
370 SymbolInfo.CheckSum = SymbolInfo.SizeOfImage = 0;
371
372 /* Load the symbols */
374}
375
376/*
377 * @implemented
378 */
379VOID
380NTAPI
383{
384 STRING NameString, CommandString;
385
386 /* Setup the strings */
387 NameString.Buffer = (PCHAR)Name;
388 NameString.Length = (USHORT)strlen(Name);
389 CommandString.Buffer = (PCHAR)Command;
390 CommandString.Length = (USHORT)strlen(Command);
391
392 /* Send them to the debugger */
393 DebugService2(&NameString, &CommandString, BREAKPOINT_COMMAND_STRING);
394}
395
396/*
397* @implemented
398*/
399VOID
400NTAPI
402{
403 /* Restore the previous frame as the active one */
404 NtCurrentTeb()->ActiveFrame = Frame->Previous;
405}
406
407/*
408* @implemented
409*/
410VOID
411NTAPI
413{
414 /* Save the current frame and set the new one as active */
415 Frame->Previous = NtCurrentTeb()->ActiveFrame;
416 NtCurrentTeb()->ActiveFrame = Frame;
417}
418
420NTAPI
422{
423 /* Return the frame that's currently active */
424 return NtCurrentTeb()->ActiveFrame;
425}
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
unsigned char BOOLEAN
Definition: actypes.h:127
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
static CCHAR DebugString[256]
Definition: settings.c:16
Definition: bufpool.h:45
#define DebugPrint(x)
Definition: classpnp.h:125
Walk Base
Definition: desccheck.cpp:2270
LPWSTR Name
Definition: desk.c:124
_In_ ULONG _In_ UCHAR Level
Definition: dispmprt.h:1329
#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:27
VOID NTAPI RtlpClearInDbgPrint(VOID)
Definition: libsupp.c:54
BOOLEAN NTAPI RtlpSetInDbgPrint(VOID)
Definition: libsupp.c:35
#define RtlImageNtHeader
Definition: compat.h:806
#define __cdecl
Definition: corecrt.h:121
#define va_end(v)
Definition: stdarg.h:28
#define va_start(v, l)
Definition: stdarg.h:26
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1597
char * va_list
Definition: vadefs.h:50
#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:2712
std::wstring STRING
Definition: fontsub.cpp:33
Status
Definition: gdiplustypes.h:24
#define DbgPrint
Definition: hal.h:12
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define NtCurrentTeb
NTSTATUS NTAPI NtQueryDebugFilterState(_In_ ULONG ComponentId, _In_ ULONG Level)
Definition: kdapi.c:2712
NTSTATUS NTAPI NtSetDebugFilterState(_In_ ULONG ComponentId, _In_ ULONG Level, _In_ BOOLEAN State)
Definition: kdapi.c:2766
#define DbgPrintEx(cmpid, lvl, fmt,...)
Definition: kdinit.c:24
#define PCHAR
Definition: match.c:90
#define _vsnprintf
Definition: _vsnprintf.c:28
#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 VOID NTAPI RtlRaiseException(_In_ PEXCEPTION_RECORD ExceptionRecord)
CHAR * PCH
Definition: ntbasedef.h:403
CONST CHAR * PCCH
Definition: ntbasedef.h:404
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_BREAKPOINT
Definition: ntstatus.h:264
#define DBG_PRINTEXCEPTION_C
Definition: ntstatus.h:104
unsigned short USHORT
Definition: pedump.c:61
char CHAR
Definition: pedump.c:57
_In_ ULONG _In_ WDFMEMORY Input
Definition: portacpi.cpp:109
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:204
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:207
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)
strncpy
Definition: string.h:335
NTSTATUS NTAPI DbgSetDebugFilterState(IN ULONG ComponentId, IN ULONG Level, IN BOOLEAN State)
Definition: debug.c:313
VOID NTAPI DbgCommandString(IN PCCH Name, IN PCCH Command)
Definition: debug.c:381
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:401
ULONG __cdecl DbgPrintReturnControlC(PCCH Format,...)
Definition: debug.c:254
ULONG NTAPI DbgPrompt(IN PCCH Prompt, OUT PCH Response, IN ULONG MaximumResponseLength)
Definition: debug.c:277
NTSTATUS NTAPI DbgQueryDebugFilterState(IN ULONG ComponentId, IN ULONG Level)
Definition: debug.c:301
ULONG NTAPI vDbgPrintExWithPrefix(IN PCCH Prefix, IN ULONG ComponentId, IN ULONG Level, IN PCCH Format, IN va_list ap)
Definition: debug.c:167
VOID NTAPI RtlPushFrame(IN PTEB_ACTIVE_FRAME Frame)
Definition: debug.c:412
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:326
VOID NTAPI DbgUnLoadImageSymbols(IN PSTRING Name, IN PVOID Base, IN ULONG_PTR ProcessId)
Definition: debug.c:361
#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:269
ULONG SizeOfImage
Definition: kdtypes.h:271
ULONG CheckSum
Definition: kdtypes.h:270
PVOID BaseOfDll
Definition: kdtypes.h:268
#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)
PUSB_ROOT_HUB_NAME Output
Definition: userreq.cpp:699
NTSYSAPI TEB_ACTIVE_FRAME *WINAPI RtlGetFrame(void)
Definition: debug.c:421
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
_In_ ULONG MaximumResponseLength
Definition: kdfuncs.h:11
_In_ __drv_aliasesMem PSTRING Prefix
Definition: rtlfuncs.h:1647