ReactOS 0.4.17-dev-1190-g4c812f4
dbgtrace.cpp
Go to the documentation of this file.
1/*++
2
3Copyright (c) Microsoft Corporation
4
5ModuleName:
6
7 DbgTrace.cpp
8
9Abstract:
10
11 Temporary file to be used until ETW can be used
12 for UM
13
14Author:
15
16
17
18Revision History:
19
20
21
22--*/
23
24#include "fxobjectpch.hpp"
25
26#if FX_CORE_MODE==FX_CORE_USER_MODE
27#include "strsafe.h"
28#endif
29
30#if !defined(EVENT_TRACING)
31
32#if DBG && defined(__REACTOS__)
33
37static
39FxTraceGetTypeConversion(
40 _In_reads_(TypeLength) PCSTR TypeName,
41 _In_ SIZE_T TypeLength)
42{
43 if (TypeLength == 2 && strncmp(TypeName, "wZ", 2) == 0)
44 return "%wZ";
45
46 if (TypeLength == 4 && strncmp(TypeName, "GUID", 4) == 0)
47 return "%p";
48
49 if (TypeLength == 4 && strncmp(TypeName, "FUNC", 4) == 0)
50 return "(function)";
51
52 if (TypeLength == 7 && strncmp(TypeName, "BOOLEAN", 7) == 0)
53 return "%u";
54
55 /* Status codes, error codes and enumerations all arrive as 32-bit values */
56 return "0x%08lx";
57}
58
62static
63VOID
64FxTraceTranslateFormat(
66 _Out_writes_z_(OutputSize) PCHAR Output,
67 _In_ SIZE_T OutputSize)
68{
69 SIZE_T outIndex;
70 SIZE_T convLength;
71 PCSTR typeEnd;
72 PCSTR conversion;
73
74 outIndex = 0;
75
76 while (*Format != ANSI_NULL && outIndex + 1 < OutputSize)
77 {
78 if (Format[0] == '%' && Format[1] == '%')
79 {
80 if (outIndex + 2 >= OutputSize)
81 break;
82
83 Output[outIndex++] = *Format++;
84 Output[outIndex++] = *Format++;
85 continue;
86 }
87
88 if (Format[0] == '%' && Format[1] == '!')
89 {
90 typeEnd = strchr(Format + 2, '!');
91 if (typeEnd != NULL)
92 {
93 conversion = FxTraceGetTypeConversion(Format + 2,
94 typeEnd - (Format + 2));
95 convLength = strlen(conversion);
96
97 /* Stop rather than emit a partial conversion */
98 if (outIndex + convLength >= OutputSize)
99 break;
100
101 RtlCopyMemory(&Output[outIndex], conversion, convLength);
102 outIndex += convLength;
103 Format = typeEnd + 1;
104 continue;
105 }
106 }
107
108 Output[outIndex++] = *Format++;
109 }
110
111 Output[outIndex] = ANSI_NULL;
112}
113
114#endif
115
116VOID
119 __in PVOID FxDriverGlobals,
120 __in ULONG DebugPrintLevel,
121 __in ULONG DebugPrintFlag,
123 __in PCSTR DebugMessage,
124 ...
125 )
126
127/*++
128
129Routine Description:
130
131 Print the trace message to debugger.
132
133Arguments:
134
135 TraceEventsLevel - print level between 0 and 3, with 3 the most verbose
136
137Return Value:
138
139 None.
140
141 --*/
142 {
143#if DBG
144 UNREFERENCED_PARAMETER(FxDriverGlobals);
145
146#define TEMP_BUFFER_SIZE 1024
148 CHAR debugMessageBuffer[TEMP_BUFFER_SIZE];
149#ifdef __REACTOS__
150#define FORMAT_BUFFER_SIZE 512
151 CHAR formatBuffer[FORMAT_BUFFER_SIZE];
152#endif
154
155 va_start(list, DebugMessage);
156
157 if (DebugMessage) {
158
159#ifdef __REACTOS__
160 FxTraceTranslateFormat(DebugMessage,
161 formatBuffer,
162 sizeof(formatBuffer));
163#endif
164
165 //
166 // Using new safe string functions instead of _vsnprintf.
167 // This function takes care of NULL terminating if the message
168 // is longer than the buffer.
169 //
170#if FX_CORE_MODE==FX_CORE_KERNEL_MODE
171 status = RtlStringCbVPrintfA( debugMessageBuffer,
172 sizeof(debugMessageBuffer),
173#ifdef __REACTOS__
174 formatBuffer,
175#else
176 DebugMessage,
177#endif
178 list );
179#else
180 HRESULT hr;
181 hr = StringCbVPrintfA( debugMessageBuffer,
182 sizeof(debugMessageBuffer),
183#ifdef __REACTOS__
184 formatBuffer,
185#else
186 DebugMessage,
187#endif
188 list );
189
190
192 {
194 }
195 else
196 {
198 }
199#endif
200#ifdef __REACTOS__
202#else
203 if(!NT_SUCCESS(status)) {
204#endif
205
206#if FX_CORE_MODE==FX_CORE_KERNEL_MODE
207 DbgPrint ("WDFTrace: RtlStringCbVPrintfA failed 0x%x\n", status);
208#else
209 OutputDebugString("WDFTrace: Unable to expand: ");
210#ifdef __REACTOS__
211 OutputDebugString(formatBuffer);
212#else
213 OutputDebugString(DebugMessage);
214#endif
215#endif
216#ifdef __REACTOS__
217 va_end(list);
218#endif
219 return;
220 }
221 if (DebugPrintLevel <= TRACE_LEVEL_ERROR ||
222 (DebugPrintLevel <= DebugLevel &&
223 ((DebugPrintFlag & DebugFlag) == DebugPrintFlag))) {
224#if FX_CORE_MODE==FX_CORE_KERNEL_MODE
225 DbgPrint("WDFTrace: %s\n", debugMessageBuffer);
226#else
227 OutputDebugString("WDFTrace: ");
228#ifdef __REACTOS__
229 OutputDebugString(debugMessageBuffer);
230#else
231 OutputDebugString(DebugMessage);
232#endif
233#endif
234 }
235 }
236 va_end(list);
237
238 return;
239#else
240 UNREFERENCED_PARAMETER(FxDriverGlobals);
241 UNREFERENCED_PARAMETER(DebugPrintLevel);
242 UNREFERENCED_PARAMETER(DebugPrintFlag);
243 UNREFERENCED_PARAMETER(DebugMessage);
244#endif
245}
246
247#endif
LONG NTSTATUS
Definition: precomp.h:26
Definition: list.h:39
#define __in
Definition: dbghelp.h:35
VOID __cdecl DoTraceLevelMessage(__in PVOID FxDriverGlobals, __in ULONG DebugPrintLevel, __in ULONG DebugPrintFlag, __drv_formatString(FormatMessage) __in PCSTR DebugMessage,...)
Definition: dbgtrace.cpp:118
ULONG DebugFlag
Definition: fxobject.cpp:44
HRESULT hr
Definition: delayimp.cpp:582
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define __cdecl
Definition: corecrt.h:121
#define va_end(v)
Definition: stdarg.h:28
#define va_start(v, l)
Definition: stdarg.h:26
_ACRTIMP char *__cdecl strchr(const char *, int)
Definition: string.c:3291
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1597
_ACRTIMP int __cdecl strncmp(const char *, const char *, size_t)
Definition: string.c:3335
char * va_list
Definition: vadefs.h:50
#define __drv_formatString(kind)
Definition: driverspecs.h:271
NTSTATUS WinErrorToNtStatus(__in ULONG WinError)
Definition: errtostatus.cpp:60
ULONG DebugLevel
Definition: fbtusb.c:26
#define DbgPrint
Definition: hal.h:12
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FORMAT_BUFFER_SIZE
Definition: logfile.c:36
#define TEMP_BUFFER_SIZE
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _In_z_
Definition: no_sal2.h:164
#define _In_
Definition: no_sal2.h:158
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
#define ANSI_NULL
NTSTRSAFEAPI RtlStringCbVPrintfA(_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest, _In_ size_t cbDest, _In_ _Printf_format_string_ NTSTRSAFE_PCSTR pszFormat, _In_ va_list argList)
Definition: ntstrsafe.h:1034
char CHAR
Definition: pedump.c:57
#define list
Definition: rosglue.h:35
@ Output
Definition: arc.h:94
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
STRSAFEAPI StringCbVPrintfA(STRSAFE_LPSTR pszDest, size_t cbDest, STRSAFE_LPCSTR pszFormat, va_list argList)
Definition: strsafe.h:502
Definition: ps.c:97
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define FormatMessage
Definition: winbase.h:3518
#define OutputDebugString
Definition: winbase.h:3613
#define HRESULT_FACILITY(hr)
Definition: winerror.h:191
#define FACILITY_WIN32
Definition: winerror.h:29
#define HRESULT_CODE(hr)
Definition: winerror.h:188