Utils: Add some backtrace dumping facility to QTC_ASSERT

Unix only for now, opt-in, by setting QTC_BACKTRACE_MAXDEPTH=<number>
in the environment.

Change-Id: Ice110dbf5d25003706a947b27c279fec464df7a1
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2021-09-23 18:29:05 +02:00
parent 214968a80b
commit 0745dc1634
2 changed files with 29 additions and 1 deletions

View File

@@ -26,9 +26,30 @@
#include "qtcassert.h"
#include <QByteArray>
#include <QDebug>
#if defined(Q_OS_UNIX)
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
#endif
namespace Utils {
void dumpBacktrace(int maxdepth)
{
if (maxdepth == -1)
maxdepth = 1000;
#if defined(Q_OS_UNIX)
void *bt[1000] = {nullptr};
int size = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
char **lines = backtrace_symbols(bt, size);
for (int i = 0; i < size; ++i)
qDebug() << "0x" + QByteArray::number(quintptr(bt[i]), 16) << lines[i];
free(lines);
#endif
}
void writeAssertLocation(const char *msg)
{
static bool goBoom = qEnvironmentVariableIsSet("QTC_FATAL_ASSERTS");
@@ -36,6 +57,10 @@ void writeAssertLocation(const char *msg)
qFatal("SOFT ASSERT made fatal: %s", msg);
else
qDebug("SOFT ASSERT: %s", msg);
static int maxdepth = qEnvironmentVariableIntValue("QTC_BACKTRACE_MAXDEPTH");
if (maxdepth != 0)
dumpBacktrace(maxdepth);
}
} // namespace Utils

View File

@@ -27,7 +27,10 @@
#include "utils_global.h"
namespace Utils { QTCREATOR_UTILS_EXPORT void writeAssertLocation(const char *msg); }
namespace Utils {
QTCREATOR_UTILS_EXPORT void writeAssertLocation(const char *msg);
QTCREATOR_UTILS_EXPORT void dumpBacktrace(int maxdepth);
} // Utils
#define QTC_ASSERT_STRINGIFY_HELPER(x) #x
#define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x)