2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* platform-independent debug support code.
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "debug.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "app_hooks.h"
|
2008-01-07 21:03:19 +01:00
|
|
|
#include "os_path.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "path_util.h"
|
2008-01-07 21:03:19 +01:00
|
|
|
#include "lib/allocators/allocators.h" // page_aligned_alloc
|
2007-05-09 23:01:11 +02:00
|
|
|
#include "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
|
2008-06-30 19:34:18 +02: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
|
|
|
|
2007-05-28 11:20:55 +02:00
|
|
|
ERROR_ASSOCIATE(ERR::SYM_NO_STACK_FRAMES_FOUND, "No stack frames found", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNRETRIEVABLE_STATIC, "Value unretrievable (stored in external module)", -1);
|
2008-09-18 20:06:48 +02:00
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNRETRIEVABLE, "Value unretrievable", -1);
|
2007-05-28 11:20:55 +02:00
|
|
|
ERROR_ASSOCIATE(ERR::SYM_TYPE_INFO_UNAVAILABLE, "Error getting type_info", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_INTERNAL_ERROR, "Exception raised while processing a symbol", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_UNSUPPORTED, "Symbol type not (fully) supported", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_CHILD_NOT_FOUND, "Symbol does not have the given child", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_NESTING_LIMIT, "Symbol nesting too deep or infinite recursion", -1);
|
|
|
|
ERROR_ASSOCIATE(ERR::SYM_SINGLE_SYMBOL_LIMIT, "Symbol has produced too much output", -1);
|
|
|
|
ERROR_ASSOCIATE(INFO::SYM_SUPPRESS_OUTPUT, "Symbol was suppressed", -1);
|
2006-09-22 15:19:40 +02:00
|
|
|
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// needed when writing crashlog
|
|
|
|
static const size_t LOG_CHARS = 16384;
|
|
|
|
wchar_t debug_log[LOG_CHARS];
|
|
|
|
wchar_t* debug_log_pos = debug_log;
|
|
|
|
|
|
|
|
// write to memory buffer (fast)
|
|
|
|
void debug_wprintf_mem(const wchar_t* fmt, ...)
|
|
|
|
{
|
2008-12-17 17:32:46 +01:00
|
|
|
const ssize_t charsLeft = (ssize_t)(LOG_CHARS - (debug_log_pos-debug_log));
|
2008-09-27 12:05:11 +02:00
|
|
|
debug_assert(charsLeft >= 0);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// potentially not enough room for the new string; throw away the
|
|
|
|
// older half of the log. we still protect against overflow below.
|
2008-09-27 12:05:11 +02:00
|
|
|
if(charsLeft < 512)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
const size_t copySize = sizeof(wchar_t) * LOG_CHARS/2;
|
2006-04-24 01:14:18 +02:00
|
|
|
wchar_t* const middle = &debug_log[LOG_CHARS/2];
|
2008-09-27 12:05:11 +02:00
|
|
|
cpu_memcpy(debug_log, middle, copySize);
|
|
|
|
memset(middle, 0, copySize);
|
2006-04-24 01:14:18 +02:00
|
|
|
debug_log_pos -= LOG_CHARS/2; // don't assign middle (may leave gap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write into buffer (in-place)
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2008-09-27 12:05:11 +02:00
|
|
|
int len = vswprintf(debug_log_pos, charsLeft-2, fmt, args);
|
2008-02-25 22:19:52 +01:00
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
va_end(args);
|
|
|
|
debug_log_pos += len+2;
|
2008-02-25 22:19:52 +01:00
|
|
|
wcscpy_s(debug_log_pos-2, 3, L"\r\n");
|
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
|
|
|
|
|
|
|
void debug_filter_add(const char* tag)
|
|
|
|
{
|
|
|
|
const u32 hash = fnv_hash(tag);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug_filter_remove(const char* tag)
|
|
|
|
{
|
|
|
|
const u32 hash = fnv_hash(tag);
|
|
|
|
|
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
|
|
|
// found it
|
|
|
|
if(tags[i] == hash)
|
|
|
|
{
|
|
|
|
// replace with last element (avoid holes)
|
|
|
|
tags[i] = tags[MAX_TAGS-1];
|
|
|
|
num_tags--;
|
|
|
|
|
|
|
|
// can only happen once, so we're done.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-01-07 18:35:26 +01:00
|
|
|
bool debug_filter_allows(const char* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 hash = fnv_hash(text, i);
|
|
|
|
|
|
|
|
// check if entry allowing this tag is found
|
|
|
|
for(i = 0; i < MAX_TAGS; i++)
|
|
|
|
if(tags[i] == hash)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-25 22:19:52 +01:00
|
|
|
// max # characters (including \0) output by debug_printf in one call.
|
|
|
|
const size_t DEBUG_PRINTF_MAX_CHARS = 1024; // refer to wdbg.cpp!debug_vsprintf before changing this
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-04-30 16:35:19 +02:00
|
|
|
#undef debug_printf // allowing #defining it out
|
2006-04-24 01:14:18 +02:00
|
|
|
void debug_printf(const char* fmt, ...)
|
|
|
|
{
|
2008-02-25 22:19:52 +01:00
|
|
|
char buf[DEBUG_PRINTF_MAX_CHARS]; buf[ARRAY_SIZE(buf)-1] = '\0';
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2008-02-25 22:19:52 +01:00
|
|
|
const int len = vsnprintf(buf, DEBUG_PRINTF_MAX_CHARS, fmt, ap);
|
|
|
|
debug_assert(len >= 0);
|
2006-04-24 01:14:18 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2007-01-07 18:35:26 +01:00
|
|
|
if(debug_filter_allows(buf))
|
2006-04-24 01:14:18 +02:00
|
|
|
debug_puts(buf);
|
|
|
|
}
|
|
|
|
|
2008-08-09 21:22:13 +02:00
|
|
|
void debug_printf(const wchar_t* fmt, ...)
|
|
|
|
{
|
|
|
|
wchar_t buf[DEBUG_PRINTF_MAX_CHARS]; buf[ARRAY_SIZE(buf)-1] = '\0';
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2008-09-18 20:06:48 +02:00
|
|
|
const int numChars = vswprintf(buf, DEBUG_PRINTF_MAX_CHARS, fmt, ap);
|
|
|
|
debug_assert(numChars >= 0);
|
2008-08-09 21:22:13 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
char buf2[DEBUG_PRINTF_MAX_CHARS];
|
2008-09-20 20:37:22 +02:00
|
|
|
|
|
|
|
size_t bytesWritten = wcstombs(buf2, buf, DEBUG_PRINTF_MAX_CHARS);
|
|
|
|
|
|
|
|
debug_assert(bytesWritten == (size_t)numChars);
|
2008-08-09 21:22:13 +02:00
|
|
|
|
|
|
|
if(debug_filter_allows(buf2))
|
|
|
|
debug_puts(buf2);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2006-07-07 03:22:23 +02:00
|
|
|
// avoid potential infinite loop if an error occurs here.
|
2008-09-27 12:05:11 +02:00
|
|
|
static uintptr_t isBusy;
|
|
|
|
if(!cpu_CAS(&isBusy, 0, 1))
|
2006-09-22 15:19:40 +02:00
|
|
|
return ERR::REENTERED; // NOWARN
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
OsPath path = OsPath(ah_get_log_dir())/"crashlog.txt";
|
|
|
|
FILE* f = fopen(path.string().c_str(), "w");
|
2006-04-24 01:14:18 +02:00
|
|
|
if(!f)
|
2006-07-07 03:22:23 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
isBusy = 0;
|
2007-06-13 01:29:27 +02:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
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);
|
|
|
|
|
|
|
|
fwprintf(f, L"Last known activity:\n\n %ls\n", debug_log);
|
|
|
|
|
|
|
|
fclose(f);
|
2008-09-27 12:05:11 +02:00
|
|
|
isBusy = 0;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// output
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// translates and displays the given strings in a dialog.
|
2008-09-27 12:05:11 +02:00
|
|
|
// this is typically only used when debug_DisplayError has failed or
|
2006-04-24 01:14:18 +02:00
|
|
|
// is unavailable because that function is much more capable.
|
2008-09-18 13:27:55 +02:00
|
|
|
// implemented via sys_display_msg; see documentation there.
|
2008-09-27 12:05:11 +02:00
|
|
|
void debug_DisplayMessage(const wchar_t* caption, const wchar_t* msg)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2008-09-18 13:27:55 +02:00
|
|
|
sys_display_msg(ah_translate(caption), ah_translate(msg));
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
// 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. it sucks to hide errors, but we assume that whoever clicked
|
|
|
|
// exit really doesn't want to see any more errors.
|
2008-09-27 12:05:11 +02:00
|
|
|
static bool isExiting;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
|
|
|
// this logic is applicable to any type of error. special cases such as
|
|
|
|
// suppressing certain expected WARN_ERRs are done there.
|
2008-09-27 12:05:11 +02:00
|
|
|
static bool ShouldSuppressError(u8* suppress)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-22 20:26:08 +02:00
|
|
|
if(!suppress)
|
|
|
|
return false;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
if(*suppress == DEBUG_SUPPRESS)
|
|
|
|
return true;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
if(isExiting)
|
2006-06-22 20:26:08 +02:00
|
|
|
return true;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2008-09-18 20:06:48 +02:00
|
|
|
// (NB: this may appear obscene, but deep stack traces have been
|
|
|
|
// observed to take up > 256 KiB)
|
2008-09-27 12:05:11 +02:00
|
|
|
static const size_t messageSize = 512*KiB;
|
2006-07-07 03:22:23 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
void debug_FreeErrorMessage(ErrorMessageMem* emm)
|
2006-07-07 03:22:23 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
page_aligned_free(emm->pa_mem, messageSize);
|
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,
|
2008-09-27 12:05:11 +02:00
|
|
|
const char* filename, int line, const char* func,
|
|
|
|
void* context, const char* 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
|
|
|
if(!emm->pa_mem)
|
2006-06-22 20:26:08 +02:00
|
|
|
return L"(insufficient memory to generate error message)";
|
2007-05-02 18:42:27 +02:00
|
|
|
wchar_t* const buf = (wchar_t*)emm->pa_mem;
|
2008-09-27 12:05:11 +02:00
|
|
|
const size_t maxChars = messageSize / sizeof(wchar_t);
|
|
|
|
wchar_t* pos = buf; size_t charsLeft = maxChars; int len;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2007-01-18 23:59:44 +01:00
|
|
|
// header
|
2008-09-27 12:05:11 +02:00
|
|
|
len = swprintf(pos, charsLeft,
|
2006-06-22 20:26:08 +02:00
|
|
|
L"%ls\r\n"
|
|
|
|
L"Location: %hs:%d (%hs)\r\n"
|
|
|
|
L"\r\n"
|
|
|
|
L"Call stack:\r\n"
|
2007-01-18 23:59:44 +01:00
|
|
|
L"\r\n",
|
2008-09-27 12:05:11 +02:00
|
|
|
description, filename, line, func);
|
2007-01-20 00:03:35 +01:00
|
|
|
if(len < 0)
|
|
|
|
{
|
|
|
|
fail:
|
|
|
|
return L"(error while formatting error message)";
|
|
|
|
}
|
2008-09-27 12:05:11 +02:00
|
|
|
pos += len; charsLeft -= len;
|
2006-06-22 20:26:08 +02:00
|
|
|
|
2007-01-18 23:59:44 +01:00
|
|
|
// append stack trace
|
2008-09-27 12:05:11 +02:00
|
|
|
LibError ret = debug_DumpStack(pos, charsLeft, context, lastFuncToSkip);
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret == ERR::REENTERED)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
len = swprintf(pos, charsLeft,
|
2006-06-22 20:26:08 +02:00
|
|
|
L"(cannot start a nested stack trace; what probably happened is that "
|
|
|
|
L"an debug_assert/debug_warn/CHECK_ERR fired during the current trace.)"
|
|
|
|
);
|
2008-09-27 12:05:11 +02:00
|
|
|
if(len < 0) goto fail; pos += len; charsLeft -= len;
|
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
|
|
|
{
|
2007-01-18 23:59:44 +01:00
|
|
|
char description_buf[100] = {'?'};
|
2008-09-27 12:05:11 +02:00
|
|
|
len = swprintf(pos, charsLeft,
|
2006-07-07 03:22:23 +02:00
|
|
|
L"(error while dumping stack: %hs)",
|
|
|
|
error_description_r(ret, description_buf, ARRAY_SIZE(description_buf))
|
|
|
|
);
|
2008-09-27 12:05:11 +02:00
|
|
|
if(len < 0) goto fail; pos += len; charsLeft -= len;
|
2007-01-18 23:59:44 +01:00
|
|
|
}
|
|
|
|
else // success
|
|
|
|
{
|
|
|
|
len = (int)wcslen(buf);
|
2008-09-27 12:05:11 +02:00
|
|
|
pos = buf+len; charsLeft = maxChars-len;
|
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)
|
2008-05-01 17:41:42 +02:00
|
|
|
char description_buf[100] = "?";
|
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));
|
2008-05-01 17:41:42 +02:00
|
|
|
char os_error[100] = "?";
|
|
|
|
sys_error_description_r(0, os_error, ARRAY_SIZE(os_error));
|
2008-09-27 12:05:11 +02:00
|
|
|
len = swprintf(pos, charsLeft,
|
2007-01-18 23:59:44 +01:00
|
|
|
L"\r\n"
|
|
|
|
L"errno = %d (%hs)\r\n"
|
|
|
|
L"OS error = %hs\r\n",
|
|
|
|
errno, description_buf, os_error
|
|
|
|
);
|
2008-09-27 12:05:11 +02:00
|
|
|
if(len < 0) goto fail; pos += len; charsLeft -= len;
|
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
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
static ErrorReaction CallDisplayError(const wchar_t* text, size_t flags)
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
|
|
|
// first try app hook implementation
|
|
|
|
ErrorReaction er = ah_display_error(text, flags);
|
|
|
|
// .. it's only a stub: default to normal implementation
|
|
|
|
if(er == ER_NOT_IMPLEMENTED)
|
|
|
|
er = sys_display_error(text, flags);
|
|
|
|
|
|
|
|
return er;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
static ErrorReaction PerformErrorReaction(ErrorReaction er, size_t flags, u8* 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
|
|
|
{
|
2006-06-22 20:26:08 +02:00
|
|
|
case ER_BREAK:
|
|
|
|
// 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();
|
|
|
|
er = ER_CONTINUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ER_SUPPRESS:
|
|
|
|
*suppress = DEBUG_SUPPRESS;
|
2006-04-24 01:14:18 +02:00
|
|
|
er = ER_CONTINUE;
|
2006-06-22 20:26:08 +02:00
|
|
|
break;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
case ER_EXIT:
|
2008-09-27 12:05:11 +02:00
|
|
|
isExiting = true; // see declaration
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return er;
|
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
ErrorReaction debug_DisplayError(const wchar_t* description,
|
|
|
|
size_t flags, void* context, const char* lastFuncToSkip,
|
|
|
|
const char* pathname, int line, const char* func,
|
2006-06-22 20:26:08 +02:00
|
|
|
u8* 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;
|
|
|
|
// .. deal with incomplete file/line info
|
2008-09-27 12:05:11 +02:00
|
|
|
if(!pathname || pathname[0] == '\0')
|
|
|
|
pathname = "unknown";
|
2006-06-22 20:26:08 +02:00
|
|
|
if(line <= 0)
|
|
|
|
line = 0;
|
|
|
|
if(!func || func[0] == '\0')
|
|
|
|
func = "?";
|
|
|
|
// .. _FILE__ evaluates to the full path (albeit without drive letter)
|
|
|
|
// which is rather long. we only display the base name for clarity.
|
2008-09-27 12:05:11 +02:00
|
|
|
const char* 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.
|
2008-09-27 12:05:11 +02:00
|
|
|
debug_printf("%s(%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
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
debug_WriteCrashlog(text);
|
|
|
|
ErrorReaction 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
// strobe indicating expectedError is valid and the next error should be
|
2006-06-25 17:35:28 +02:00
|
|
|
// compared against that / skipped if equal to it.
|
2007-09-23 17:36:29 +02:00
|
|
|
// set/reset via cpu_CAS for thread-safety (hence uintptr_t).
|
2008-09-27 12:05:11 +02:00
|
|
|
static uintptr_t isExpectedErrorValid;
|
|
|
|
static LibError expectedError;
|
2006-06-22 21:12:10 +02:00
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
void debug_SkipNextError(LibError err)
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
if(cpu_CAS(&isExpectedErrorValid, 0, 1))
|
|
|
|
expectedError = err;
|
2006-06-22 21:12:10 +02:00
|
|
|
else
|
2007-12-20 21:09:19 +01:00
|
|
|
debug_assert(0); // internal error: concurrent attempt to skip assert/error
|
2006-06-22 21:12:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
static bool ShouldSkipThisError(LibError err)
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
// (compare before resetting strobe - expectedError may change afterwards)
|
|
|
|
bool isExpected = (expectedError == err);
|
2007-09-23 17:36:29 +02:00
|
|
|
// (use cpu_CAS to ensure only one error is skipped)
|
2008-09-27 12:05:11 +02:00
|
|
|
if(cpu_CAS(&isExpectedErrorValid, 1, 0))
|
2006-06-25 17:35:28 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
debug_assert(isExpected);
|
|
|
|
return isExpected;
|
2006-06-25 17:35:28 +02:00
|
|
|
}
|
2006-06-22 21:12:10 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
ErrorReaction debug_OnError(LibError err, u8* suppress, const char* file, int line, const char* func)
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
if(ShouldSkipThisError(err))
|
|
|
|
return ER_CONTINUE;
|
|
|
|
|
|
|
|
void* context = 0; const char* lastFuncToSkip = __func__;
|
|
|
|
wchar_t buf[400];
|
|
|
|
char err_buf[200]; error_description_r(err, err_buf, ARRAY_SIZE(err_buf));
|
|
|
|
swprintf(buf, ARRAY_SIZE(buf), L"Function call failed: return value was %d (%hs)", err, err_buf);
|
|
|
|
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
|
|
|
|
|
|
|
void debug_SkipNextAssertion()
|
2006-06-22 21:12:10 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
// to share code between assert and error skip mechanism, we treat the
|
|
|
|
// former as an error.
|
|
|
|
debug_SkipNextError(ERR::ASSERTION_FAILED);
|
2006-06-22 21:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
static bool ShouldSkipThisAssertion()
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
return ShouldSkipThisError(ERR::ASSERTION_FAILED);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 12:05:11 +02:00
|
|
|
ErrorReaction debug_OnAssertionFailure(const char* expr, u8* suppress, const char* file, int line, const char* func)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
if(ShouldSkipThisAssertion())
|
2006-06-22 21:12:10 +02:00
|
|
|
return ER_CONTINUE;
|
2008-09-27 12:05:11 +02:00
|
|
|
void* context = 0; const char* lastFuncToSkip = __func__;
|
2006-04-24 01:14:18 +02:00
|
|
|
wchar_t buf[400];
|
2008-09-27 12:05:11 +02:00
|
|
|
swprintf(buf, ARRAY_SIZE(buf), L"Assertion failed: \"%hs\"", expr);
|
|
|
|
return debug_DisplayError(buf, DE_MANUAL_BREAK, context, lastFuncToSkip, file,line,func, suppress);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2008-09-27 12:05:11 +02:00
|
|
|
|
|
|
|
|
2008-12-17 17:32:46 +01:00
|
|
|
|