improve buffer debugging by logging the call stack of where it was allocated and freed from

This commit is contained in:
Arvid Norberg
2011-04-15 08:37:27 +00:00
parent 9382c77ea2
commit 554812f0ab
2 changed files with 31 additions and 15 deletions

View File

@@ -97,16 +97,17 @@ std::string demangle(char const* name) { return name; }
#if (defined __linux__ || (defined __APPLE__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050))
#include <execinfo.h>
void print_backtrace(FILE* out, char const* label)
void print_backtrace(char* out, int len)
{
void* stack[50];
int size = backtrace(stack, 50);
char** symbols = backtrace_symbols(stack, size);
fprintf(out, "%s\n", label);
for (int i = 1; i < size; ++i)
for (int i = 1; i < size && len > 0; ++i)
{
fprintf(out, "%d: %s\n", i, demangle(symbols[i]).c_str());
int ret = snprintf(out, len, "%d: %s\n", i, demangle(symbols[i]).c_str());
out += ret;
len -= ret;
}
free(symbols);
@@ -123,7 +124,6 @@ char const* libtorrent_assert_log = "asserts.log";
void assert_fail(char const* expr, int line, char const* file, char const* function, char const* value)
{
#if TORRENT_PRODUCTION_ASSERTS
FILE* out = fopen(libtorrent_assert_log, "a+");
if (out == 0) out = stderr;
@@ -131,6 +131,9 @@ void assert_fail(char const* expr, int line, char const* file, char const* funct
FILE* out = stderr;
#endif
char stack[8192];
print_backtrace(stack, sizeof(stack));
fprintf(out, "assertion failed. Please file a bugreport at "
"http://code.rasterbar.com/libtorrent/newticket\n"
"Please include the following information:\n\n"
@@ -140,11 +143,12 @@ void assert_fail(char const* expr, int line, char const* file, char const* funct
"line: %d\n"
"function: %s\n"
"expression: %s\n"
"%s%s"
"%s%s\n"
"stack:\n"
"%s\n"
, LIBTORRENT_REVISION, file, line, function, expr
, value ? value : "", value ? "\n" : "");
print_backtrace(out, "stack:");
, value ? value : "", value ? "\n" : ""
, stack);
// if production asserts are defined, don't abort, just print the error
#if TORRENT_PRODUCTION_ASSERTS