2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* platform-independent debug support code.
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/debug.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-08-07 19:22:05 +02:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/app_hooks.h"
|
2008-01-07 21:03:19 +01:00
|
|
|
#include "lib/allocators/allocators.h" // page_aligned_alloc
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/fnv_hash.h"
|
2007-09-23 17:36:29 +02:00
|
|
|
#include "lib/sysdep/cpu.h" // cpu_CAS
|
2007-04-30 16:35:19 +02:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-01-20 17:53:09 +01:00
|
|
|
#if OS_WIN
|
2011-03-11 17:29:27 +01:00
|
|
|
# include "lib/sysdep/os/win/wdbg_heap.h"
|
2008-01-20 17:53:09 +01:00
|
|
|
#endif
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
ERROR_ASSOCIATE(ERR::SYM_NO_STACK_FRAMES_FOUND, L"No stack frames found", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNRETRIEVABLE_STATIC, L"Value unretrievable (stored in external module)", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNRETRIEVABLE, L"Value unretrievable", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_TYPE_INFO_UNAVAILABLE, L"Error getting type_info", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_INTERNAL_ERROR, L"Exception raised while processing a symbol", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNSUPPORTED, L"Symbol type not (fully) supported", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_CHILD_NOT_FOUND, L"Symbol does not have the given child", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_NESTING_LIMIT, L"Symbol nesting too deep or infinite recursion", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_SINGLE_SYMBOL_LIMIT, L"Symbol has produced too much output", -1);
|
|
|
|
ERROR_ASSOCIATE(INFO::SYM_SUPPRESS_OUTPUT, L"Symbol was suppressed", -1);
|
2006-09-22 15:19:40 +02:00
|
|
|
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// need to shoehorn printf-style variable params into
|
|
|
|
// the OutputDebugString call.
|
|
|
|
// - don't want to split into multiple calls - would add newlines to output.
|
|
|
|
// - fixing Win32 _vsnprintf to return # characters that would be written,
|
|
|
|
// as required by C99, looks difficult and unnecessary. if any other code
|
|
|
|
// needs that, implement GNU vasprintf.
|
|
|
|
// - fixed size buffers aren't nice, but much simpler than vasprintf-style
|
|
|
|
// allocate+expand_until_it_fits. these calls are for quick debug output,
|
|
|
|
// not loads of data, anyway.
|
|
|
|
|
|
|
|
// rationale: static data instead of std::set to allow setting at any time.
|
|
|
|
// we store FNV hash of tag strings for fast comparison; collisions are
|
|
|
|
// extremely unlikely and can only result in displaying more/less text.
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static const size_t MAX_TAGS = 20;
|
2006-04-24 01:14:18 +02:00
|
|
|
static u32 tags[MAX_TAGS];
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static size_t num_tags;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
void debug_filter_add(const wchar_t* tag)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
const u32 hash = fnv_hash(tag, wcslen(tag)*sizeof(tag[0]));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// make sure it isn't already in the list
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for(size_t i = 0; i < MAX_TAGS; i++)
|
2006-04-24 01:14:18 +02:00
|
|
|
if(tags[i] == hash)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// too many already?
|
|
|
|
if(num_tags == MAX_TAGS)
|
|
|
|
{
|
2007-12-20 21:09:19 +01:00
|
|
|
debug_assert(0); // increase MAX_TAGS
|
2006-04-24 01:14:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tags[num_tags++] = hash;
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
void debug_filter_remove(const wchar_t* tag)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
const u32 hash = fnv_hash(tag, wcslen(tag)*sizeof(tag[0]));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for(size_t i = 0; i < MAX_TAGS; i++)
|
2009-11-03 22:46:35 +01:00
|
|
|
{
|
|
|
|
if(tags[i] == hash) // found it
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// replace with last element (avoid holes)
|
|
|
|
tags[i] = tags[MAX_TAGS-1];
|
|
|
|
num_tags--;
|
|
|
|
|
|
|
|
// can only happen once, so we're done.
|
|
|
|
return;
|
|
|
|
}
|
2009-11-03 22:46:35 +01:00
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void debug_filter_clear()
|
|
|
|
{
|
2008-01-19 12:33:11 +01:00
|
|
|
std::fill(tags, tags+MAX_TAGS, 0);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
bool debug_filter_allows(const wchar_t* text)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t i;
|
2006-04-24 01:14:18 +02:00
|
|
|
for(i = 0; ; i++)
|
|
|
|
{
|
|
|
|
// no | found => no tag => should always be displayed
|
|
|
|
if(text[i] == ' ' || text[i] == '\0')
|
|
|
|
return true;
|
|
|
|
if(text[i] == '|' && i != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
const u32 hash = fnv_hash(text, i*sizeof(text[0]));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// check if entry allowing this tag is found
|
|
|
|
for(i = 0; i < MAX_TAGS; i++)
|
|
|
|
if(tags[i] == hash)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-30 16:35:19 +02:00
|
|
|
#undef debug_printf // allowing #defining it out
|
2008-08-09 21:22:13 +02:00
|
|
|
void debug_printf(const wchar_t* fmt, ...)
|
|
|
|
{
|
2009-11-14 19:32:27 +01:00
|
|
|
wchar_t buf[4096];
|
2008-08-09 21:22:13 +02:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2009-11-14 19:32:27 +01:00
|
|
|
const int numChars = vswprintf_s(buf, ARRAY_SIZE(buf), fmt, ap);
|
2008-09-18 20:06:48 +02:00
|
|
|
debug_assert(numChars >= 0);
|
2008-08-09 21:22:13 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
if(debug_filter_allows(buf))
|
|
|
|
debug_puts(buf);
|
2008-08-09 21:22:13 +02:00
|
|
|
}
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
LibError debug_WriteCrashlog(const wchar_t* text)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
// (avoid infinite recursion and/or reentering this function if it
|
|
|
|
// fails/reports an error)
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
IDLE,
|
|
|
|
BUSY,
|
|
|
|
FAILED
|
|
|
|
};
|
2009-07-31 19:48:45 +02:00
|
|
|
// note: the initial state is IDLE. we rely on zero-init because
|
|
|
|
// initializing local static objects from constants may happen when
|
|
|
|
// this is first called, which isn't thread-safe. (see C++ 6.7.4)
|
|
|
|
cassert(IDLE == 0);
|
2010-07-12 14:57:58 +02:00
|
|
|
static volatile intptr_t state;
|
2009-07-31 19:48:45 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!cpu_CAS(&state, IDLE, BUSY))
|
2006-09-22 15:19:40 +02:00
|
|
|
return ERR::REENTERED; // NOWARN
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
OsPath pathname = ah_get_log_dir()/"crashlog.txt";
|
|
|
|
FILE* f = sys_OpenFile(pathname, "w");
|
|
|
|
if(!f)
|
2006-07-07 03:22:23 +02:00
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
state = FAILED; // must come before DEBUG_DISPLAY_ERROR
|
|
|
|
DEBUG_DISPLAY_ERROR(L"Unable to open crashlog.txt for writing (please ensure the log directory is writable)");
|
|
|
|
return ERR::FAIL; // NOWARN (the above text is more helpful than a generic error code)
|
2006-07-07 03:22:23 +02:00
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
fputwc(0xFEFF, f); // BOM
|
2006-04-24 01:14:18 +02:00
|
|
|
fwprintf(f, L"%ls\n", text);
|
|
|
|
fwprintf(f, L"\n\n====================================\n\n");
|
|
|
|
|
|
|
|
// allow user to bundle whatever information they want
|
|
|
|
ah_bundle_logs(f);
|
|
|
|
|
|
|
|
fclose(f);
|
2009-07-31 18:39:54 +02:00
|
|
|
state = IDLE;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2009-07-31 18:39:54 +02:00
|
|
|
// error message
|
2006-04-24 01:14:18 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
// (NB: this may appear obscene, but deep stack traces have been
|
|
|
|
// observed to take up > 256 KiB)
|
|
|
|
static const size_t messageSize = 512*KiB;
|
|
|
|
|
|
|
|
void debug_FreeErrorMessage(ErrorMessageMem* emm)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
page_aligned_free(emm->pa_mem, messageSize);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
// a stream with printf-style varargs and the possibility of
|
|
|
|
// writing directly to the output buffer.
|
|
|
|
class PrintfWriter
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
public:
|
|
|
|
PrintfWriter(wchar_t* buf, size_t maxChars)
|
|
|
|
: m_pos(buf), m_charsLeft(maxChars)
|
|
|
|
{
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-11-07 00:06:50 +01:00
|
|
|
bool operator()(const wchar_t* fmt, ...) WPRINTF_ARGS(2)
|
2009-07-31 18:39:54 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
const int len = vswprintf_s(m_pos, m_charsLeft, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if(len < 0)
|
|
|
|
return false;
|
|
|
|
m_pos += len;
|
|
|
|
m_charsLeft -= len;
|
2006-06-22 20:26:08 +02:00
|
|
|
return true;
|
2009-07-31 18:39:54 +02:00
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
wchar_t* Position() const
|
|
|
|
{
|
|
|
|
return m_pos;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
size_t CharsLeft() const
|
|
|
|
{
|
|
|
|
return m_charsLeft;
|
|
|
|
}
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
void CountAddedChars()
|
|
|
|
{
|
|
|
|
const size_t len = wcslen(m_pos);
|
|
|
|
m_pos += len;
|
|
|
|
m_charsLeft -= len;
|
|
|
|
}
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
private:
|
|
|
|
wchar_t* m_pos;
|
|
|
|
size_t m_charsLeft;
|
|
|
|
};
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
|
|
|
|
// split out of debug_DisplayError because it's used by the self-test.
|
|
|
|
const wchar_t* debug_BuildErrorMessage(
|
2006-06-22 20:26:08 +02:00
|
|
|
const wchar_t* description,
|
2009-11-04 16:29:28 +01:00
|
|
|
const wchar_t* filename, int line, const char* func,
|
2009-11-03 22:46:35 +01:00
|
|
|
void* context, const wchar_t* lastFuncToSkip,
|
2006-07-07 03:22:23 +02:00
|
|
|
ErrorMessageMem* emm)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
2007-05-02 18:42:27 +02:00
|
|
|
// rationale: see ErrorMessageMem
|
2008-09-27 12:05:11 +02:00
|
|
|
emm->pa_mem = page_aligned_alloc(messageSize);
|
2007-05-02 18:42:27 +02:00
|
|
|
wchar_t* const buf = (wchar_t*)emm->pa_mem;
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!buf)
|
|
|
|
return L"(insufficient memory to generate error message)";
|
|
|
|
PrintfWriter writer(buf, messageSize / sizeof(wchar_t));
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2007-01-18 23:59:44 +01:00
|
|
|
// header
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!writer(
|
2006-06-22 20:26:08 +02:00
|
|
|
L"%ls\r\n"
|
2009-11-04 16:29:28 +01:00
|
|
|
L"Location: %ls:%d (%hs)\r\n"
|
2006-06-22 20:26:08 +02:00
|
|
|
L"\r\n"
|
|
|
|
L"Call stack:\r\n"
|
2007-01-18 23:59:44 +01:00
|
|
|
L"\r\n",
|
2009-07-31 18:39:54 +02:00
|
|
|
description, filename, line, func
|
|
|
|
))
|
2007-01-20 00:03:35 +01:00
|
|
|
{
|
|
|
|
fail:
|
|
|
|
return L"(error while formatting error message)";
|
|
|
|
}
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2007-01-18 23:59:44 +01:00
|
|
|
// append stack trace
|
2009-07-31 18:39:54 +02:00
|
|
|
LibError ret = debug_DumpStack(writer.Position(), writer.CharsLeft(), context, lastFuncToSkip);
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret == ERR::REENTERED)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!writer(
|
|
|
|
L"While generating an error report, we encountered a second "
|
|
|
|
L"problem. Please be sure to report both this and the subsequent "
|
|
|
|
L"error messages."
|
|
|
|
))
|
|
|
|
goto fail;
|
2006-06-22 20:26:08 +02:00
|
|
|
}
|
2006-09-22 15:19:40 +02:00
|
|
|
else if(ret != INFO::OK)
|
2006-07-07 03:22:23 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t description_buf[100] = {'?'};
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!writer(
|
2009-11-03 22:46:35 +01:00
|
|
|
L"(error while dumping stack: %ls)",
|
2006-07-07 03:22:23 +02:00
|
|
|
error_description_r(ret, description_buf, ARRAY_SIZE(description_buf))
|
2009-07-31 18:39:54 +02:00
|
|
|
))
|
|
|
|
goto fail;
|
2007-01-18 23:59:44 +01:00
|
|
|
}
|
|
|
|
else // success
|
|
|
|
{
|
2009-07-31 18:39:54 +02:00
|
|
|
writer.CountAddedChars();
|
2006-07-07 03:22:23 +02:00
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-01-18 23:59:44 +01:00
|
|
|
// append OS error (just in case it happens to be relevant -
|
|
|
|
// it's usually still set from unrelated operations)
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t description_buf[100] = L"?";
|
2007-01-20 00:03:35 +01:00
|
|
|
LibError errno_equiv = LibError_from_errno(false);
|
2007-01-18 23:59:44 +01:00
|
|
|
if(errno_equiv != ERR::FAIL) // meaningful translation
|
|
|
|
error_description_r(errno_equiv, description_buf, ARRAY_SIZE(description_buf));
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t os_error[100] = L"?";
|
2008-05-01 17:41:42 +02:00
|
|
|
sys_error_description_r(0, os_error, ARRAY_SIZE(os_error));
|
2009-07-31 18:39:54 +02:00
|
|
|
if(!writer(
|
2007-01-18 23:59:44 +01:00
|
|
|
L"\r\n"
|
2009-11-03 22:46:35 +01:00
|
|
|
L"errno = %d (%ls)\r\n"
|
|
|
|
L"OS error = %ls\r\n",
|
2007-01-18 23:59:44 +01:00
|
|
|
errno, description_buf, os_error
|
2009-07-31 18:39:54 +02:00
|
|
|
))
|
|
|
|
goto fail;
|
2007-01-18 23:59:44 +01:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
return buf;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// display error messages
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// translates and displays the given strings in a dialog.
|
|
|
|
// this is typically only used when debug_DisplayError has failed or
|
|
|
|
// is unavailable because that function is much more capable.
|
|
|
|
// implemented via sys_display_msg; see documentation there.
|
|
|
|
void debug_DisplayMessage(const wchar_t* caption, const wchar_t* msg)
|
|
|
|
{
|
|
|
|
sys_display_msg(ah_translate(caption), ah_translate(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// when an error has come up and user clicks Exit, we don't want any further
|
|
|
|
// errors (e.g. caused by atexit handlers) to come up, possibly causing an
|
|
|
|
// infinite loop. hiding errors isn't good, but we assume that whoever clicked
|
|
|
|
// exit really doesn't want to see any more messages.
|
2010-11-06 20:14:16 +01:00
|
|
|
static atomic_bool isExiting;
|
2009-07-31 18:39:54 +02:00
|
|
|
|
|
|
|
// this logic is applicable to any type of error. special cases such as
|
|
|
|
// suppressing certain expected WARN_ERRs are done there.
|
2010-11-06 20:14:16 +01:00
|
|
|
static bool ShouldSuppressError(atomic_bool* suppress)
|
2009-07-31 18:39:54 +02:00
|
|
|
{
|
|
|
|
if(isExiting)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!suppress)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(*suppress == DEBUG_SUPPRESS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:16:21 +01:00
|
|
|
static ErrorReactionInternal CallDisplayError(const wchar_t* text, size_t flags)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
|
|
|
// first try app hook implementation
|
2010-12-09 12:16:21 +01:00
|
|
|
ErrorReactionInternal er = ah_display_error(text, flags);
|
2006-06-22 20:26:08 +02:00
|
|
|
// .. it's only a stub: default to normal implementation
|
2010-12-09 12:16:21 +01:00
|
|
|
if(er == ERI_NOT_IMPLEMENTED)
|
2006-06-22 20:26:08 +02:00
|
|
|
er = sys_display_error(text, flags);
|
|
|
|
|
|
|
|
return er;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-12-09 12:16:21 +01:00
|
|
|
static ErrorReaction PerformErrorReaction(ErrorReactionInternal er, size_t flags, atomic_bool* suppress)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
const bool shouldHandleBreak = (flags & DE_MANUAL_BREAK) == 0;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
|
|
|
switch(er)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-12-09 12:16:21 +01:00
|
|
|
case ERI_CONTINUE:
|
|
|
|
return ER_CONTINUE;
|
|
|
|
|
|
|
|
case ERI_BREAK:
|
2006-06-22 20:26:08 +02:00
|
|
|
// handle "break" request unless the caller wants to (doing so here
|
|
|
|
// instead of within the dlgproc yields a correct call stack)
|
2008-09-27 12:05:11 +02:00
|
|
|
if(shouldHandleBreak)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
|
|
|
debug_break();
|
2010-12-09 12:16:21 +01:00
|
|
|
return ER_CONTINUE;
|
2006-06-22 20:26:08 +02:00
|
|
|
}
|
2010-12-09 12:16:21 +01:00
|
|
|
else
|
|
|
|
return ER_BREAK;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2010-12-09 12:16:21 +01:00
|
|
|
case ERI_SUPPRESS:
|
2010-11-06 20:14:16 +01:00
|
|
|
(void)cpu_CAS(suppress, 0, DEBUG_SUPPRESS);
|
2010-12-09 12:16:21 +01:00
|
|
|
return ER_CONTINUE;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-12-09 12:16:21 +01:00
|
|
|
case ERI_EXIT:
|
2010-11-06 20:14:16 +01:00
|
|
|
isExiting = 1; // see declaration
|
|
|
|
COMPILER_FENCE;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-01-20 17:53:09 +01:00
|
|
|
#if OS_WIN
|
|
|
|
// prevent (slow) heap reporting since we're exiting abnormally and
|
|
|
|
// thus probably leaking like a sieve.
|
|
|
|
wdbg_heap_Enable(false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-12-09 12:16:21 +01:00
|
|
|
case ERI_NOT_IMPLEMENTED:
|
|
|
|
default:
|
|
|
|
debug_break(); // not expected to be reached
|
|
|
|
return ER_CONTINUE;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
ErrorReaction debug_DisplayError(const wchar_t* description,
|
2009-11-03 22:46:35 +01:00
|
|
|
size_t flags, void* context, const wchar_t* lastFuncToSkip,
|
2009-11-04 16:29:28 +01:00
|
|
|
const wchar_t* pathname, int line, const char* func,
|
2010-11-06 20:14:16 +01:00
|
|
|
atomic_bool* suppress)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-22 20:26:08 +02:00
|
|
|
// "suppressing" this error means doing nothing and returning ER_CONTINUE.
|
2008-09-27 12:05:11 +02:00
|
|
|
if(ShouldSuppressError(suppress))
|
2006-04-24 01:14:18 +02:00
|
|
|
return ER_CONTINUE;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
|
|
|
// fix up params
|
|
|
|
// .. translate
|
|
|
|
description = ah_translate(description);
|
|
|
|
// .. caller supports a suppress flag; set the corresponding flag so that
|
|
|
|
// the error display implementation enables the Suppress option.
|
|
|
|
if(suppress)
|
|
|
|
flags |= DE_ALLOW_SUPPRESS;
|
2010-11-20 20:46:49 +01:00
|
|
|
|
|
|
|
if(flags & DE_NO_DEBUG_INFO)
|
|
|
|
{
|
|
|
|
// in non-debug-info mode, simply display the given description
|
|
|
|
// and then return immediately
|
2010-12-09 12:16:21 +01:00
|
|
|
ErrorReactionInternal er = CallDisplayError(description, flags);
|
2010-11-20 20:46:49 +01:00
|
|
|
return PerformErrorReaction(er, flags, suppress);
|
|
|
|
}
|
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
// .. deal with incomplete file/line info
|
2008-09-27 12:05:11 +02:00
|
|
|
if(!pathname || pathname[0] == '\0')
|
2009-11-03 22:46:35 +01:00
|
|
|
pathname = L"unknown";
|
2006-06-22 20:26:08 +02:00
|
|
|
if(line <= 0)
|
|
|
|
line = 0;
|
|
|
|
if(!func || func[0] == '\0')
|
2009-11-04 16:29:28 +01:00
|
|
|
func = "?";
|
2006-06-22 20:26:08 +02:00
|
|
|
// .. _FILE__ evaluates to the full path (albeit without drive letter)
|
|
|
|
// which is rather long. we only display the base name for clarity.
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* filename = path_name_only(pathname);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
// display in output window; double-click will navigate to error location.
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"%ls(%d): %ls\n", filename, line, description);
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2006-07-07 03:22:23 +02:00
|
|
|
ErrorMessageMem emm;
|
2008-09-27 12:05:11 +02:00
|
|
|
const wchar_t* text = debug_BuildErrorMessage(description, filename, line, func, context, lastFuncToSkip, &emm);
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2009-07-31 18:39:54 +02:00
|
|
|
(void)debug_WriteCrashlog(text);
|
2010-12-09 12:16:21 +01:00
|
|
|
ErrorReactionInternal er = CallDisplayError(text, flags);
|
2006-06-22 20:26:08 +02:00
|
|
|
|
|
|
|
// note: debug_break-ing here to make sure the app doesn't continue
|
2008-09-27 12:05:11 +02:00
|
|
|
// running is no longer necessary. debug_DisplayError now determines our
|
2006-06-22 20:26:08 +02:00
|
|
|
// window handle and is modal.
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
// must happen before PerformErrorReaction because that may exit.
|
|
|
|
debug_FreeErrorMessage(&emm);
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
return PerformErrorReaction(er, flags, suppress);
|
2006-06-22 20:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
// is errorToSkip valid? (also guarantees mutual exclusion)
|
|
|
|
enum SkipStatus
|
|
|
|
{
|
|
|
|
INVALID, VALID, BUSY
|
|
|
|
};
|
2010-11-06 20:14:16 +01:00
|
|
|
static intptr_t skipStatus = INVALID;
|
2009-11-10 16:48:41 +01:00
|
|
|
static LibError errorToSkip;
|
|
|
|
static size_t numSkipped;
|
2006-06-22 21:12:10 +02:00
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
void debug_SkipErrors(LibError err)
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2009-11-10 16:48:41 +01:00
|
|
|
if(cpu_CAS(&skipStatus, INVALID, BUSY))
|
|
|
|
{
|
|
|
|
errorToSkip = err;
|
|
|
|
numSkipped = 0;
|
2010-10-29 14:54:51 +02:00
|
|
|
COMPILER_FENCE;
|
2009-11-10 16:48:41 +01:00
|
|
|
skipStatus = VALID; // linearization point
|
|
|
|
}
|
2006-06-22 21:12:10 +02:00
|
|
|
else
|
2009-11-10 16:48:41 +01:00
|
|
|
DEBUG_WARN_ERR(ERR::REENTERED);
|
2006-06-22 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
size_t debug_StopSkippingErrors()
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2009-11-10 16:48:41 +01:00
|
|
|
if(cpu_CAS(&skipStatus, VALID, BUSY))
|
2006-06-25 17:35:28 +02:00
|
|
|
{
|
2009-11-10 16:48:41 +01:00
|
|
|
const size_t ret = numSkipped;
|
2010-10-29 14:54:51 +02:00
|
|
|
COMPILER_FENCE;
|
2009-11-10 16:48:41 +01:00
|
|
|
skipStatus = INVALID; // linearization point
|
|
|
|
return ret;
|
2006-06-25 17:35:28 +02:00
|
|
|
}
|
2009-11-10 16:48:41 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_WARN_ERR(ERR::REENTERED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2006-06-22 21:12:10 +02:00
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
static bool ShouldSkipError(LibError err)
|
|
|
|
{
|
|
|
|
if(cpu_CAS(&skipStatus, VALID, BUSY))
|
|
|
|
{
|
|
|
|
numSkipped++;
|
|
|
|
const bool ret = (err == errorToSkip);
|
2010-10-29 14:54:51 +02:00
|
|
|
COMPILER_FENCE;
|
2009-11-10 16:48:41 +01:00
|
|
|
skipStatus = VALID;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-06-22 21:12:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
|
2010-11-06 20:14:16 +01:00
|
|
|
ErrorReaction debug_OnError(LibError err, atomic_bool* suppress, const wchar_t* file, int line, const char* func)
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2009-11-10 16:48:41 +01:00
|
|
|
if(ShouldSkipError(err))
|
2008-09-27 12:05:11 +02:00
|
|
|
return ER_CONTINUE;
|
|
|
|
|
2009-11-04 16:29:28 +01:00
|
|
|
void* context = 0;
|
|
|
|
const wchar_t* lastFuncToSkip = L"debug_OnError";
|
2008-09-27 12:05:11 +02:00
|
|
|
wchar_t buf[400];
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t err_buf[200]; error_description_r(err, err_buf, ARRAY_SIZE(err_buf));
|
2009-11-06 11:59:10 +01:00
|
|
|
swprintf_s(buf, ARRAY_SIZE(buf), L"Function call failed: return value was %ld (%ls)", err, err_buf);
|
2008-09-27 12:05:11 +02:00
|
|
|
return debug_DisplayError(buf, DE_MANUAL_BREAK, context, lastFuncToSkip, file,line,func, suppress);
|
2006-06-22 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
|
2010-11-06 20:14:16 +01:00
|
|
|
ErrorReaction debug_OnAssertionFailure(const wchar_t* expr, atomic_bool* suppress, const wchar_t* file, int line, const char* func)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2009-11-04 16:29:28 +01:00
|
|
|
void* context = 0;
|
|
|
|
const std::wstring lastFuncToSkip = L"debug_OnAssertionFailure";
|
2006-04-24 01:14:18 +02:00
|
|
|
wchar_t buf[400];
|
2009-11-03 22:46:35 +01:00
|
|
|
swprintf_s(buf, ARRAY_SIZE(buf), L"Assertion failed: \"%ls\"", expr);
|
2009-11-04 16:29:28 +01:00
|
|
|
return debug_DisplayError(buf, DE_MANUAL_BREAK, context, lastFuncToSkip.c_str(), file,line,func, suppress);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|