diff options
author | Michael Meeks <michael.meeks@collabora.com> | 2015-10-26 14:13:10 +0000 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2015-12-02 16:47:16 +0000 |
commit | 94427a1b000344b24948374953a099571901a2ed (patch) | |
tree | e5a7551267810f07c884f276b47991109bd118c8 | |
parent | 08e5fc257fa8c106be34bb034ebe5e00b6fcf711 (diff) |
sal: add SAL_DEBUG_TRACE debugging API for Windows, with UNX stub.
Change-Id: I2fdbc2ac10f483eee154bdf69479ba217a91ef7f
Reviewed-on: https://gerrit.libreoffice.org/19605
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Michael Meeks <michael.meeks@collabora.com>
-rw-r--r-- | include/sal/detail/log.h | 1 | ||||
-rw-r--r-- | include/sal/log.hxx | 12 | ||||
-rw-r--r-- | sal/Library_sal.mk | 3 | ||||
-rw-r--r-- | sal/inc/internal/misc.hxx | 18 | ||||
-rw-r--r-- | sal/osl/all/log.cxx | 11 | ||||
-rw-r--r-- | sal/osl/unx/backtraceapi.cxx | 24 | ||||
-rw-r--r-- | sal/osl/w32/backtrace.cxx | 52 |
7 files changed, 120 insertions, 1 deletions
diff --git a/include/sal/detail/log.h b/include/sal/detail/log.h index 11ece36c3279..ab9718518646 100644 --- a/include/sal/detail/log.h +++ b/include/sal/detail/log.h @@ -52,6 +52,7 @@ extern "C" { enum sal_detail_LogLevel { SAL_DETAIL_LOG_LEVEL_INFO, SAL_DETAIL_LOG_LEVEL_WARN, + SAL_DETAIL_LOG_LEVEL_DEBUG_TRACE = SAL_MAX_ENUM - 1, SAL_DETAIL_LOG_LEVEL_DEBUG = SAL_MAX_ENUM }; diff --git a/include/sal/log.hxx b/include/sal/log.hxx index aefcdd1f7f5f..e01d70870042 100644 --- a/include/sal/log.hxx +++ b/include/sal/log.hxx @@ -316,6 +316,18 @@ inline char const * unwrapStream(SAL_UNUSED_PARAMETER StreamIgnore const &) { SAL_DETAIL_LOG_STREAM( \ SAL_LOG_TRUE, ::SAL_DETAIL_LOG_LEVEL_DEBUG, NULL, NULL, stream) +/** + Produce temporary debugging output from stream along with a + stack trace of the calling location. This macro is meant to + be used only while working on code and should never exist + in production code. + + See @ref sal_log "basic logging functionality" for details. +*/ +#define SAL_DEBUG_TRACE(stream) \ + SAL_DETAIL_LOG_STREAM( \ + SAL_LOG_TRUE, ::SAL_DETAIL_LOG_LEVEL_DEBUG_TRACE, NULL, NULL, stream) + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/Library_sal.mk b/sal/Library_sal.mk index ee25cd20d080..ed2e91459874 100644 --- a/sal/Library_sal.mk +++ b/sal/Library_sal.mk @@ -48,6 +48,7 @@ $(eval $(call gb_Library_use_externals,sal,\ $(eval $(call gb_Library_use_system_win32_libs,sal,\ advapi32 \ comdlg32 \ + dbghelp \ mpr \ ole32 \ shell32 \ @@ -146,6 +147,7 @@ endif ifneq ($(OS),WNT) $(eval $(call gb_Library_add_exception_objects,sal,\ + sal/osl/unx/backtraceapi \ sal/osl/unx/conditn \ sal/osl/unx/file \ sal/osl/unx/file_error_transl \ @@ -215,6 +217,7 @@ else # $(OS) == WNT # .ENDIF $(eval $(call gb_Library_add_exception_objects,sal,\ + sal/osl/w32/backtrace \ sal/osl/w32/file \ sal/osl/w32/file_dirvol \ sal/osl/w32/file_url \ diff --git a/sal/inc/internal/misc.hxx b/sal/inc/internal/misc.hxx new file mode 100644 index 000000000000..b93868e1baab --- /dev/null +++ b/sal/inc/internal/misc.hxx @@ -0,0 +1,18 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#ifndef INCLUDED_SAL_INC_INTERNAL_MISC_H +#define INCLUDED_SAL_INC_INTERNAL_MISC_H + +#include <rtl/ustring.hxx> + +/// Build a debugging backtrace from current PC location. +rtl_uString *osl_backtraceAsString(void); + +#endif // INCLUDED_SAL_INC_INTERNAL_MISC_H + diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index 994169aa58b0..a343403490bd 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -25,6 +25,7 @@ #include "sal/detail/log.h" #include "sal/log.hxx" #include "sal/types.h" +#include "internal/misc.hxx" #include "logformat.hxx" @@ -70,6 +71,7 @@ char const * toString(sal_detail_LogLevel level) { case SAL_DETAIL_LOG_LEVEL_WARN: return "warn"; case SAL_DETAIL_LOG_LEVEL_DEBUG: + case SAL_DETAIL_LOG_LEVEL_DEBUG_TRACE: return "debug"; } } @@ -199,7 +201,14 @@ void log( + (std::strncmp(where, SRCDIR "/", nStrLen) == 0 ? nStrLen : 0)); } - s << message << '\n'; + + s << message; + if (level == SAL_DETAIL_LOG_LEVEL_DEBUG_TRACE) { + s << " at:\n"; + s << OUString(osl_backtraceAsString(), SAL_NO_ACQUIRE); + } + s << '\n'; + #if defined ANDROID int android_log_level; switch (level) { diff --git a/sal/osl/unx/backtraceapi.cxx b/sal/osl/unx/backtraceapi.cxx new file mode 100644 index 000000000000..a2a2cac3aa4c --- /dev/null +++ b/sal/osl/unx/backtraceapi.cxx @@ -0,0 +1,24 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <rtl/ustrbuf.hxx> +#include "internal/misc.hxx" + +// FIXME: no-op for now; it needs implementing, cf. above. +rtl_uString *osl_backtraceAsString() +{ + OUStringBuffer aBuf; + OUString aStr = aBuf.makeStringAndClear(); + rtl_uString_acquire( aStr.pData ); + return aStr.pData; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/osl/w32/backtrace.cxx b/sal/osl/w32/backtrace.cxx new file mode 100644 index 000000000000..80f0856e9e12 --- /dev/null +++ b/sal/osl/w32/backtrace.cxx @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "internal/misc.hxx" + +#include <windows.h> +#include <process.h> +#include <iostream> +#define OPTIONAL +#include <DbgHelp.h> + +// No-op for now; it needs implementing. +rtl_uString *osl_backtraceAsString() +{ + OUStringBuffer aBuf; + + HANDLE hProcess = GetCurrentProcess(); + SymInitialize( hProcess, NULL, true ); + + void * aStack[ 512 ]; + sal_uInt32 nFrames = CaptureStackBackTrace( 0, 512, aStack, NULL ); + + SYMBOL_INFO * pSymbol; + pSymbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ); + pSymbol->MaxNameLen = 1024 - 1; + pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO ); + + for( sal_uInt32 i = 0; i < nFrames; i++ ) + { + SymFromAddr( hProcess, ( DWORD64 )aStack[ i ], 0, pSymbol ); + aBuf.append( (sal_Int32)(nFrames - i - 1) ); + aBuf.append( ": " ); + aBuf.appendAscii( pSymbol->Name ); + aBuf.append( " - 0x" ); + aBuf.append( (sal_Int64)pSymbol->Address, 16 ); + aBuf.append( "\n" ); + } + + free( pSymbol ); + + OUString aStr = aBuf.makeStringAndClear(); + rtl_uString_acquire( aStr.pData ); + return aStr.pData; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |