From e7ee48fd38788e3b6edbfd948999d1d67d3a54f9 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 7 Dec 2022 16:06:07 +0800 Subject: [PATCH] enh: add tassert funcs --- include/os/os.h | 1 + include/os/osSystem.h | 20 ++++++++++++++++++++ include/util/tlog.h | 4 ++-- source/dnode/mgmt/node_mgmt/src/dmMgmt.c | 2 ++ source/util/src/tlog.c | 23 ++++++++++++++--------- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/include/os/os.h b/include/os/os.h index 0688eeb9ad..b27fa84406 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -27,6 +27,7 @@ extern "C" { #if !defined(WINDOWS) #include +#include #include #include #include diff --git a/include/os/osSystem.h b/include/os/osSystem.h index eca984c41a..dd1c5cd204 100644 --- a/include/os/osSystem.h +++ b/include/os/osSystem.h @@ -46,6 +46,26 @@ void taosSetTerminalMode(); int32_t taosGetOldTerminalMode(); void taosResetTerminalMode(); +#if defined(LINUX) +#define taosPrintTrace(flags, level, dflag) \ + { \ + void* array[100]; \ + int32_t size = backtrace(array, 100); \ + char** strings = backtrace_symbols(array, size); \ + if (strings != NULL) { \ + taosPrintLog(flags, level, dflag, "obtained %d stack frames", size); \ + for (int32_t i = 0; i < size; i++) { \ + taosPrintLog(flags, level, dflag, "frame:%d, %s", i, strings[i]); \ + } \ + } \ + \ + taosMemoryFree(strings); \ + } +#else +#define taosPrintTrace(flags, level, dflag) \ + {} +#endif + #ifdef __cplusplus } #endif diff --git a/include/util/tlog.h b/include/util/tlog.h index 12103d97f1..2caeaf71a9 100644 --- a/include/util/tlog.h +++ b/include/util/tlog.h @@ -84,8 +84,8 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons ; bool taosAssertLog(bool condition, const char *file, int32_t line, const char *format, ...); -#define tAssert(...) (void)taosAssertLog(condition, __FILE__, __LINE__, __VA_ARGS__); -#define tAssertR(...) taosAssertLog(condition, __FILE__, __LINE__, __VA_ARGS__) +#define tAssert(condition, ...) (void)taosAssertLog(condition, __FILE__, __LINE__, __VA_ARGS__); +#define tAssertR(condition, ...) taosAssertLog(condition, __FILE__, __LINE__, __VA_ARGS__) // clang-format off #define uFatal(...) { if (uDebugFlag & DEBUG_FATAL) { taosPrintLog("UTL FATAL", DEBUG_FATAL, tsLogEmbedded ? 255 : uDebugFlag, __VA_ARGS__); }} diff --git a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c index 02a268afda..c3f5313f7b 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c +++ b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c @@ -103,6 +103,8 @@ int32_t dmInitDnode(SDnode *pDnode) { goto _OVER; } + tAssert(0, ""); + pDnode->wrappers[DNODE].func = dmGetMgmtFunc(); pDnode->wrappers[MNODE].func = mmGetMgmtFunc(); pDnode->wrappers[VNODE].func = vmGetMgmtFunc(); diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 5d887bb1ac..7beddd5d4f 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -781,28 +781,33 @@ cmp_end: } bool taosAssertLog(bool condition, const char *file, int32_t line, const char *format, ...) { - if (!condition) return false; + if (condition) return false; - char buffer[LOG_MAX_LINE_BUFFER_SIZE]; - int32_t len = taosBuildLogHead(buffer, "UTL FATAL"); + const char *flags = "UTL FATAL "; + ELogLevel level = DEBUG_FATAL; + int32_t dflag = 255; // tsLogEmbedded ? 255 : uDebugFlag + char buffer[LOG_MAX_LINE_BUFFER_SIZE]; + int32_t len = taosBuildLogHead(buffer, flags); va_list argpointer; va_start(argpointer, format); - int32_t writeLen = len + vsnprintf(buffer + len, LOG_MAX_LINE_BUFFER_SIZE - len, format, argpointer); + len = len + vsnprintf(buffer + len, LOG_MAX_LINE_BUFFER_SIZE - len, format, argpointer); va_end(argpointer); + buffer[len++] = '\n'; + buffer[len] = 0; + taosPrintLogImp(1, 255, buffer, len); - char fullBuf[LOG_MAX_LINE_BUFFER_SIZE]; - int32_t fullLen = snprintf(fullBuf, sizeof(fullBuf), "ASSERT at file:%s:%d, %s", file, line, buffer); - taosPrintLogImp(1, 255, fullBuf, fullLen); + taosPrintLog(flags, level, dflag, "ASSERT at file %s:%d exit:%d", file, line, tsAssert); + taosPrintTrace(flags, level, dflag); if (tsAssert) { taosCloseLog(); taosMsleep(300); -#if NDEBUG +#ifdef NDEBUG abort(); #else - ASSERT(1); + ASSERT(0); #endif }