ReactOS  0.4.15-dev-499-g1f31905
dbgrpt.cpp File Reference
#include <crtdbg.h>
#include <stdio.h>
#include <signal.h>
#include <windows.h>
Include dependency graph for dbgrpt.cpp:

Go to the source code of this file.

Classes

struct  dbgrpt_char_traits< char_t >
 
struct  dbgrpt_char_traits< char >
 
struct  dbgrpt_char_traits< wchar_t >
 

Macros

#define DBGRPT_MAX_BUFFER_SIZE   4096
 
#define DBGRPT_ASSERT_PREFIX_MESSAGE   "Assertion failed: "
 
#define DBGRPT_ASSERT_PREFIX_NOMESSAGE   "Assertion failed!"
 
#define DBGRPT_STRING_TOO_LONG   "_CrtDbgReport: String too long"
 

Typedefs

typedef int(WINAPItMessageBoxW) (_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
 
typedef dbgrpt_char_traits< charachar_traits
 
typedef dbgrpt_char_traits< wchar_twchar_traits
 

Functions

static HMODULE _CrtGetUser32 ()
 
static tMessageBoxW _CrtGetMessageBox ()
 
template<typename char_t >
static int _CrtDbgReportWindow (int reportType, const char_t *filename, int linenumber, const char_t *moduleName, const char_t *message)
 
template<typename char_t >
static int _CrtEnterDbgReport (int reportType, const char_t *filename, int linenumber)
 
static void _CrtLeaveDbgReport (int reportType)
 
template<typename char_t >
static int _CrtHandleDbgReport (int reportType, const char_t *szCompleteMessage, const char_t *szFormatted, const char_t *filename, int linenumber, const char_t *moduleName)
 
EXTERN_C int __cdecl _CrtDbgReport (int reportType, const char *filename, int linenumber, const char *moduleName, const char *format,...)
 
EXTERN_C int __cdecl _CrtDbgReportW (int reportType, const wchar_t *filename, int linenumber, const wchar_t *moduleName, const wchar_t *format,...)
 

Variables

static long _CrtInAssert = -1
 
static int _CrtModeOutputFormat [_CRT_ERRCNT]
 
static const wchar_t_CrtModeMessages [_CRT_ERRCNT]
 
static HMODULE _CrtUser32Handle = NULL
 
static tMessageBoxW _CrtMessageBoxW = NULL
 

Macro Definition Documentation

◆ DBGRPT_ASSERT_PREFIX_MESSAGE

#define DBGRPT_ASSERT_PREFIX_MESSAGE   "Assertion failed: "

Definition at line 21 of file dbgrpt.cpp.

◆ DBGRPT_ASSERT_PREFIX_NOMESSAGE

#define DBGRPT_ASSERT_PREFIX_NOMESSAGE   "Assertion failed!"

Definition at line 22 of file dbgrpt.cpp.

◆ DBGRPT_MAX_BUFFER_SIZE

#define DBGRPT_MAX_BUFFER_SIZE   4096

Definition at line 20 of file dbgrpt.cpp.

◆ DBGRPT_STRING_TOO_LONG

#define DBGRPT_STRING_TOO_LONG   "_CrtDbgReport: String too long"

Definition at line 23 of file dbgrpt.cpp.

Typedef Documentation

◆ achar_traits

Definition at line 75 of file dbgrpt.cpp.

◆ tMessageBoxW

typedef int(WINAPI * tMessageBoxW) (_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)

Definition at line 43 of file dbgrpt.cpp.

◆ wchar_traits

Definition at line 76 of file dbgrpt.cpp.

Function Documentation

◆ _CrtDbgReport()

EXTERN_C int __cdecl _CrtDbgReport ( int  reportType,
const char filename,
int  linenumber,
const char moduleName,
const char format,
  ... 
)

Definition at line 264 of file dbgrpt.cpp.

265 {
266  char szFormatted[DBGRPT_MAX_BUFFER_SIZE+1] = {0}; // The user provided message
267  char szCompleteMessage[(DBGRPT_MAX_BUFFER_SIZE+1)*2] = {0}; // The output for debug / file
268 
269  // Check for recursive _CrtDbgReport calls, and validate reportType
270  if (!_CrtEnterDbgReport(reportType, filename, linenumber))
271  return -1;
272 
273  if (filename)
274  {
275  _snprintf(szCompleteMessage, DBGRPT_MAX_BUFFER_SIZE, "%s(%d) : ", filename, linenumber);
276  }
277 
278  if (format)
279  {
283  va_end(arglist);
284 
285  if (len < 0)
286  {
287  strcpy(szFormatted, DBGRPT_STRING_TOO_LONG);
288  }
289 
290  if (reportType == _CRT_ASSERT)
291  strcat(szCompleteMessage, DBGRPT_ASSERT_PREFIX_MESSAGE);
292  strcat(szCompleteMessage, szFormatted);
293  }
294  else if (reportType == _CRT_ASSERT)
295  {
296  strcat(szCompleteMessage, DBGRPT_ASSERT_PREFIX_NOMESSAGE);
297  }
298 
299  if (reportType == _CRT_ASSERT)
300  {
301  if (_CrtModeOutputFormat[reportType] & _CRTDBG_MODE_FILE)
302  strcat(szCompleteMessage, "\r");
303  strcat(szCompleteMessage, "\n");
304  }
305 
306  // FIXME: Handle user report hooks here
307 
308  int nResult = _CrtHandleDbgReport(reportType, szCompleteMessage, szFormatted, filename, linenumber, moduleName);
309 
310  _CrtLeaveDbgReport(reportType);
311 
312  return nResult;
313 }
#define DBGRPT_ASSERT_PREFIX_NOMESSAGE
Definition: dbgrpt.cpp:22
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
const char * filename
Definition: ioapi.h:135
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
#define DBGRPT_STRING_TOO_LONG
Definition: dbgrpt.cpp:23
static int _CrtHandleDbgReport(int reportType, const char_t *szCompleteMessage, const char_t *szFormatted, const char_t *filename, int linenumber, const char_t *moduleName)
Definition: dbgrpt.cpp:226
#define _snprintf
Definition: xmlstorage.h:200
#define va_end(ap)
Definition: acmsvcex.h:90
char * va_list
Definition: acmsvcex.h:78
static void _CrtLeaveDbgReport(int reportType)
Definition: dbgrpt.cpp:218
#define _CRTDBG_MODE_FILE
Definition: crtdbg.h:32
GLenum GLsizei len
Definition: glext.h:6722
#define DBGRPT_ASSERT_PREFIX_MESSAGE
Definition: dbgrpt.cpp:21
#define DBGRPT_MAX_BUFFER_SIZE
Definition: dbgrpt.cpp:20
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define _CRT_ASSERT
Definition: crtdbg.h:29
#define _vsnprintf
Definition: xmlstorage.h:202
static int _CrtModeOutputFormat[_CRT_ERRCNT]
Definition: dbgrpt.cpp:28
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static int _CrtEnterDbgReport(int reportType, const char_t *filename, int linenumber)
Definition: dbgrpt.cpp:187
va_lists_t arglist[FMT_ARGMAX+1]
Definition: format.c:284

◆ _CrtDbgReportW()

EXTERN_C int __cdecl _CrtDbgReportW ( int  reportType,
const wchar_t filename,
int  linenumber,
const wchar_t moduleName,
const wchar_t format,
  ... 
)

Definition at line 316 of file dbgrpt.cpp.

317 {
318  wchar_t szFormatted[DBGRPT_MAX_BUFFER_SIZE+1] = {0}; // The user provided message
319  wchar_t szCompleteMessage[(DBGRPT_MAX_BUFFER_SIZE+1)*2] = {0}; // The output for debug / file
320 
321  // Check for recursive _CrtDbgReportW calls, and validate reportType
322  if (!_CrtEnterDbgReport(reportType, filename, linenumber))
323  return -1;
324 
325  if (filename)
326  {
327  _snwprintf(szCompleteMessage, DBGRPT_MAX_BUFFER_SIZE, L"%s(%d) : ", filename, linenumber);
328  }
329 
330  if (format)
331  {
335  va_end(arglist);
336 
337  if (len < 0)
338  {
339  wcscpy(szFormatted, _CRT_WIDE(DBGRPT_STRING_TOO_LONG));
340  }
341 
342  if (reportType == _CRT_ASSERT)
343  wcscat(szCompleteMessage, _CRT_WIDE(DBGRPT_ASSERT_PREFIX_MESSAGE));
344  wcscat(szCompleteMessage, szFormatted);
345  }
346  else if (reportType == _CRT_ASSERT)
347  {
348  wcscat(szCompleteMessage, _CRT_WIDE(DBGRPT_ASSERT_PREFIX_NOMESSAGE));
349  }
350 
351  if (reportType == _CRT_ASSERT)
352  {
353  if (_CrtModeOutputFormat[reportType] & _CRTDBG_MODE_FILE)
354  wcscat(szCompleteMessage, L"\r");
355  wcscat(szCompleteMessage, L"\n");
356  }
357 
358  // FIXME: Handle user report hooks here
359 
360  int nResult = _CrtHandleDbgReport(reportType, szCompleteMessage, szFormatted, filename, linenumber, moduleName);
361 
362  _CrtLeaveDbgReport(reportType);
363 
364  return nResult;
365 }
#define DBGRPT_ASSERT_PREFIX_NOMESSAGE
Definition: dbgrpt.cpp:22
const char * filename
Definition: ioapi.h:135
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
#define DBGRPT_STRING_TOO_LONG
Definition: dbgrpt.cpp:23
static int _CrtHandleDbgReport(int reportType, const char_t *szCompleteMessage, const char_t *szFormatted, const char_t *filename, int linenumber, const char_t *moduleName)
Definition: dbgrpt.cpp:226
#define va_end(ap)
Definition: acmsvcex.h:90
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
char * va_list
Definition: acmsvcex.h:78
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static void _CrtLeaveDbgReport(int reportType)
Definition: dbgrpt.cpp:218
static const WCHAR L[]
Definition: oid.c:1250
#define _CRTDBG_MODE_FILE
Definition: crtdbg.h:32
GLenum GLsizei len
Definition: glext.h:6722
_CRTIMP int __cdecl _vsnwprintf(wchar_t *_Dest, size_t _Count, const wchar_t *_Format, va_list _Args)
#define DBGRPT_ASSERT_PREFIX_MESSAGE
Definition: dbgrpt.cpp:21
#define DBGRPT_MAX_BUFFER_SIZE
Definition: dbgrpt.cpp:20
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define _CRT_ASSERT
Definition: crtdbg.h:29
static int _CrtModeOutputFormat[_CRT_ERRCNT]
Definition: dbgrpt.cpp:28
static int _CrtEnterDbgReport(int reportType, const char_t *filename, int linenumber)
Definition: dbgrpt.cpp:187
#define _CRT_WIDE(_String)
Definition: crtdefs.h:43
va_lists_t arglist[FMT_ARGMAX+1]
Definition: format.c:284

◆ _CrtDbgReportWindow()

template<typename char_t >
static int _CrtDbgReportWindow ( int  reportType,
const char_t *  filename,
int  linenumber,
const char_t *  moduleName,
const char_t *  message 
)
static

Definition at line 148 of file dbgrpt.cpp.

149 {
150  typedef dbgrpt_char_traits<char_t> traits;
151 
152  wchar_t szCompleteMessage[(DBGRPT_MAX_BUFFER_SIZE+1)*2] = {0};
153  wchar_t LineBuffer[20] = {0};
154 
155  if (filename && !filename[0])
156  filename = NULL;
157  if (moduleName && !moduleName[0])
158  moduleName = NULL;
159  if (message && !message[0])
160  message = NULL;
161  if (linenumber)
162  _itow(linenumber, LineBuffer, 10);
163 
164  _snwprintf(szCompleteMessage, DBGRPT_MAX_BUFFER_SIZE * 2,
165  traits::szAssertionMessage,
166  _CrtModeMessages[reportType],
167  moduleName ? L"\nModule: " : L"", moduleName ? moduleName : traits::szEmptyString,
168  filename ? L"\nFile: " : L"", filename ? filename : traits::szEmptyString,
169  LineBuffer[0] ? L"\nLine: " : L"", LineBuffer[0] ? LineBuffer : L"",
170  message ? L"\n\n" : L"", message ? message : traits::szEmptyString);
171 
172  if (IsDebuggerPresent())
173  {
174  OutputDebugStringW(szCompleteMessage);
175  }
176 
177  tMessageBoxW messageBox = _CrtGetMessageBox();
178  if (!messageBox)
179  return IsDebuggerPresent() ? IDRETRY : IDABORT;
180 
181  // TODO: If we are not interacive, add MB_SERVICE_NOTIFICATION
182  return messageBox(NULL, szCompleteMessage, L"ReactOS C++ Runtime Library",
184 }
BOOL WINAPI IsDebuggerPresent(VOID)
Definition: debugger.c:615
#define MB_ICONHAND
Definition: winuser.h:782
Definition: tftpd.h:59
#define IDABORT
Definition: winuser.h:826
_CRTIMP wchar_t *__cdecl _itow(_In_ int _Value, _Pre_notnull_ _Post_z_ wchar_t *_Dest, _In_ int _Radix)
static tMessageBoxW _CrtGetMessageBox()
Definition: dbgrpt.cpp:130
const char * filename
Definition: ioapi.h:135
void WINAPI SHIM_OBJ_NAME() OutputDebugStringW(LPCWSTR lpOutputString)
Definition: ignoredbgout.c:23
#define MB_TASKMODAL
Definition: winuser.h:810
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
smooth NULL
Definition: ftsmooth.c:416
static const wchar_t * _CrtModeMessages[_CRT_ERRCNT]
Definition: dbgrpt.cpp:35
#define IDRETRY
Definition: winuser.h:827
static const WCHAR L[]
Definition: oid.c:1250
#define MB_ABORTRETRYIGNORE
Definition: winuser.h:785
#define DBGRPT_MAX_BUFFER_SIZE
Definition: dbgrpt.cpp:20
int(WINAPI * tMessageBoxW)(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
Definition: dbgrpt.cpp:43
#define MB_SETFOREGROUND
Definition: winuser.h:808

Referenced by _CrtHandleDbgReport().

◆ _CrtEnterDbgReport()

template<typename char_t >
static int _CrtEnterDbgReport ( int  reportType,
const char_t *  filename,
int  linenumber 
)
static

Definition at line 187 of file dbgrpt.cpp.

188 {
189  typedef dbgrpt_char_traits<char_t> traits;
190 
191  if (reportType < 0 || reportType >= _CRT_ERRCNT)
192  return FALSE;
193 
194  if (reportType == _CRT_ASSERT)
195  {
197  {
198  char LineBuffer[20] = {0};
199 
200  _itoa(linenumber, LineBuffer, 10);
201 
202  OutputDebugStringA("Nested Assert from File: ");
203  traits::OutputDebugString(filename ? filename : traits::szUnknownFile);
204  OutputDebugStringA(", Line: ");
205  OutputDebugStringA(LineBuffer);
206  OutputDebugStringA("\n");
207 
208  _CrtDbgBreak();
209 
211  return FALSE;
212  }
213  }
214  return TRUE;
215 }
#define TRUE
Definition: types.h:120
#define OutputDebugString
Definition: winbase.h:3730
const char * filename
Definition: ioapi.h:135
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define _CRT_ERRCNT
Definition: crtdbg.h:30
long __cdecl _InterlockedDecrement(_Interlocked_operand_ long volatile *_Addend)
#define _CrtDbgBreak()
Definition: crtdbg.h:113
_CRTIMP char *__cdecl _itoa(_In_ int _Value, _Pre_notnull_ _Post_z_ char *_Dest, _In_ int _Radix)
#define _CRT_ASSERT
Definition: crtdbg.h:29
static long _CrtInAssert
Definition: dbgrpt.cpp:26
void WINAPI SHIM_OBJ_NAME() OutputDebugStringA(LPCSTR lpOutputString)
Definition: ignoredbgout.c:18

Referenced by _CrtDbgReport(), and _CrtDbgReportW().

◆ _CrtGetMessageBox()

static tMessageBoxW _CrtGetMessageBox ( )
static

Definition at line 130 of file dbgrpt.cpp.

131 {
133 
135  {
136  tMessageBoxW proc = (tMessageBoxW)GetProcAddress(mod, "MessageBoxW");
137  if (proc == NULL)
139 
141  }
142 
144 }
static HMODULE _CrtGetUser32()
Definition: dbgrpt.cpp:112
#define INVALID_HANDLE_VALUE
Definition: compat.h:400
static HANDLE proc()
Definition: pdb.c:32
smooth NULL
Definition: ftsmooth.c:416
void * _InterlockedCompareExchangePointer(_Interlocked_operand_ void *volatile *_Destination, void *_Exchange, void *_Comparand)
int(WINAPI * tMessageBoxW)(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
Definition: dbgrpt.cpp:43
#define GetProcAddress(x, y)
Definition: compat.h:419
static tMessageBoxW _CrtMessageBoxW
Definition: dbgrpt.cpp:45
static int mod
Definition: i386-dis.c:1273

Referenced by _CrtDbgReportWindow().

◆ _CrtGetUser32()

static HMODULE _CrtGetUser32 ( )
static

Definition at line 112 of file dbgrpt.cpp.

113 {
114  if (_CrtUser32Handle == NULL)
115  {
117  if (mod == NULL)
119 
121  {
122  if (mod != INVALID_HANDLE_VALUE)
123  FreeLibrary(mod);
124  }
125  }
126 
128 }
static HMODULE _CrtUser32Handle
Definition: dbgrpt.cpp:44
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
Definition: loader.c:288
#define INVALID_HANDLE_VALUE
Definition: compat.h:400
#define LOAD_LIBRARY_SEARCH_SYSTEM32
Definition: winbase.h:346
smooth NULL
Definition: ftsmooth.c:416
#define FreeLibrary(x)
Definition: compat.h:414
static const WCHAR L[]
Definition: oid.c:1250
HANDLE HMODULE
Definition: typedefs.h:76
void * _InterlockedCompareExchangePointer(_Interlocked_operand_ void *volatile *_Destination, void *_Exchange, void *_Comparand)
static int mod
Definition: i386-dis.c:1273

Referenced by _CrtGetMessageBox().

◆ _CrtHandleDbgReport()

template<typename char_t >
static int _CrtHandleDbgReport ( int  reportType,
const char_t *  szCompleteMessage,
const char_t *  szFormatted,
const char_t *  filename,
int  linenumber,
const char_t *  moduleName 
)
static

Definition at line 226 of file dbgrpt.cpp.

228 {
229  typedef dbgrpt_char_traits<char_t> traits;
230 
231  if (_CrtModeOutputFormat[reportType] & _CRTDBG_MODE_FILE)
232  {
233  OutputDebugStringA("ERROR: Please implement _CrtSetReportFile first\n");
234  _CrtDbgBreak();
235  }
236 
237  if (_CrtModeOutputFormat[reportType] & _CRTDBG_MODE_DEBUG)
238  {
239  traits::OutputDebugString(szCompleteMessage);
240  }
241 
242  if (_CrtModeOutputFormat[reportType] & _CRTDBG_MODE_WNDW)
243  {
244  int nResult = _CrtDbgReportWindow(reportType, filename, linenumber, moduleName, szFormatted);
245  switch (nResult)
246  {
247  case IDRETRY:
248  return TRUE;
249  case IDIGNORE:
250  default:
251  return FALSE;
252  case IDABORT:
253  raise(SIGABRT);
254  _exit(3);
255  return FALSE; // Unreachable
256  }
257  }
258 
259  return FALSE;
260 }
#define TRUE
Definition: types.h:120
#define SIGABRT
Definition: signal.h:28
#define IDABORT
Definition: winuser.h:826
#define OutputDebugString
Definition: winbase.h:3730
const char * filename
Definition: ioapi.h:135
void _exit(int exitcode)
Definition: _exit.c:25
#define _CrtDbgBreak()
Definition: crtdbg.h:113
#define IDRETRY
Definition: winuser.h:827
#define _CRTDBG_MODE_FILE
Definition: crtdbg.h:32
static int _CrtDbgReportWindow(int reportType, const char_t *filename, int linenumber, const char_t *moduleName, const char_t *message)
Definition: dbgrpt.cpp:148
#define _CRTDBG_MODE_WNDW
Definition: crtdbg.h:34
static int _CrtModeOutputFormat[_CRT_ERRCNT]
Definition: dbgrpt.cpp:28
#define _CRTDBG_MODE_DEBUG
Definition: crtdbg.h:33
void WINAPI SHIM_OBJ_NAME() OutputDebugStringA(LPCSTR lpOutputString)
Definition: ignoredbgout.c:18
#define IDIGNORE
Definition: winuser.h:828

Referenced by _CrtDbgReport(), and _CrtDbgReportW().

◆ _CrtLeaveDbgReport()

static void _CrtLeaveDbgReport ( int  reportType)
static

Definition at line 218 of file dbgrpt.cpp.

219 {
220  if (reportType == _CRT_ASSERT)
222 }
long __cdecl _InterlockedDecrement(_Interlocked_operand_ long volatile *_Addend)
#define _CRT_ASSERT
Definition: crtdbg.h:29
static long _CrtInAssert
Definition: dbgrpt.cpp:26

Referenced by _CrtDbgReport(), and _CrtDbgReportW().

Variable Documentation

◆ _CrtInAssert

long _CrtInAssert = -1
static

Definition at line 26 of file dbgrpt.cpp.

Referenced by _CrtEnterDbgReport(), and _CrtLeaveDbgReport().

◆ _CrtMessageBoxW

tMessageBoxW _CrtMessageBoxW = NULL
static

Definition at line 45 of file dbgrpt.cpp.

Referenced by _CrtGetMessageBox().

◆ _CrtModeMessages

const wchar_t* _CrtModeMessages[_CRT_ERRCNT]
static
Initial value:
=
{
L"Warning",
L"Error",
L"Assertion Failed"
}
static const WCHAR L[]
Definition: oid.c:1250

Definition at line 35 of file dbgrpt.cpp.

Referenced by _CrtDbgReportWindow().

◆ _CrtModeOutputFormat

int _CrtModeOutputFormat[_CRT_ERRCNT]
static
Initial value:
=
{
}
#define _CRTDBG_MODE_WNDW
Definition: crtdbg.h:34
#define _CRTDBG_MODE_DEBUG
Definition: crtdbg.h:33

Definition at line 28 of file dbgrpt.cpp.

Referenced by _CrtDbgReport(), _CrtDbgReportW(), and _CrtHandleDbgReport().

◆ _CrtUser32Handle

HMODULE _CrtUser32Handle = NULL
static

Definition at line 44 of file dbgrpt.cpp.

Referenced by _CrtGetUser32().