[Libreoffice-commits] core.git: include/sal sal/inc sal/Library_sal.mk sal/osl

Michael Meeks michael.meeks at collabora.com
Wed Dec 2 08:47:37 PST 2015


 include/sal/detail/log.h     |    1 
 include/sal/log.hxx          |   12 +++++++++
 sal/Library_sal.mk           |    3 ++
 sal/inc/internal/misc.hxx    |   18 ++++++++++++++
 sal/osl/all/log.cxx          |   11 ++++++++-
 sal/osl/unx/backtraceapi.cxx |   24 +++++++++++++++++++
 sal/osl/w32/backtrace.cxx    |   52 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 120 insertions(+), 1 deletion(-)

New commits:
commit 94427a1b000344b24948374953a099571901a2ed
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Mon Oct 26 14:13:10 2015 +0000

    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 at libreoffice.org>
    Reviewed-by: Michael Meeks <michael.meeks at collabora.com>
    Tested-by: Michael Meeks <michael.meeks at collabora.com>

diff --git a/include/sal/detail/log.h b/include/sal/detail/log.h
index 11ece36..ab97185 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 aefcdd1..e01d708 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 ee25cd2..ed2e914 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 0000000..b93868e
--- /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 994169a..a343403 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 0000000..a2a2cac
--- /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 0000000..80f0856
--- /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: */


More information about the Libreoffice-commits mailing list