Branch libreoffice-6-3
[LibreOffice.git] / sal / osl / w32 / backtrace.cxx
blob9e31616eaaff660affa1f08dffc8614cc6eada31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <limits>
13 #include <memory>
15 #if !defined WIN32_LEAN_AND_MEAN
16 # define WIN32_LEAN_AND_MEAN
17 #endif
18 #include <windows.h>
19 #include <process.h>
20 #include <iostream>
21 #define OPTIONAL
22 #include <DbgHelp.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <sal/backtrace.hxx>
27 #include <backtraceasstring.hxx>
29 namespace {
31 template<typename T> T clampToULONG(T n) {
32 auto const maxUlong = std::numeric_limits<ULONG>::max();
33 return n > maxUlong ? static_cast<T>(maxUlong) : n;
38 OUString osl::detail::backtraceAsString(sal_uInt32 maxDepth)
40 assert(maxDepth != 0);
41 maxDepth = clampToULONG(maxDepth);
43 OUStringBuffer aBuf;
45 HANDLE hProcess = GetCurrentProcess();
46 SymInitialize( hProcess, nullptr, true );
48 std::unique_ptr<void*[]> aStack(new void*[ maxDepth ]);
49 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb204633.aspx
50 // "CaptureStackBackTrace function" claims that you "can capture up to
51 // MAXUSHORT frames", and on Windows Server 2003 and Windows XP it even
52 // "must be less than 63", but assume that a too large input value is
53 // clamped internally, instead of resulting in an error:
54 sal_uInt32 nFrames = CaptureStackBackTrace( 0, static_cast<ULONG>(maxDepth), aStack.get(), nullptr );
56 SYMBOL_INFO * pSymbol;
57 pSymbol = static_cast<SYMBOL_INFO *>(calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ));
58 assert(pSymbol);
59 pSymbol->MaxNameLen = 1024 - 1;
60 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
62 for( sal_uInt32 i = 0; i < nFrames; i++ )
64 SymFromAddr( hProcess, reinterpret_cast<DWORD64>(aStack[ i ]), nullptr, pSymbol );
65 aBuf.append( static_cast<sal_Int32>(nFrames - i - 1) );
66 aBuf.append( ": " );
67 aBuf.appendAscii( pSymbol->Name );
68 aBuf.append( " - 0x" );
69 aBuf.append( static_cast<sal_Int64>(pSymbol->Address), 16 );
70 aBuf.append( "\n" );
73 free( pSymbol );
75 return aBuf.makeStringAndClear();
78 std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth)
80 assert(maxDepth != 0);
81 maxDepth = clampToULONG(maxDepth);
83 HANDLE hProcess = GetCurrentProcess();
84 SymInitialize( hProcess, nullptr, true );
86 auto pStack = new void *[maxDepth];
87 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb204633.aspx
88 // "CaptureStackBackTrace function" claims that you "can capture up to
89 // MAXUSHORT frames", and on Windows Server 2003 and Windows XP it even
90 // "must be less than 63", but assume that a too large input value is
91 // clamped internally, instead of resulting in an error:
92 int nFrames = CaptureStackBackTrace( 0, static_cast<ULONG>(maxDepth), pStack, nullptr );
94 return std::unique_ptr<BacktraceState>(new BacktraceState{ pStack, nFrames });
97 OUString sal::backtrace_to_string(BacktraceState* backtraceState)
99 OUStringBuffer aBuf;
101 SYMBOL_INFO * pSymbol;
102 pSymbol = static_cast<SYMBOL_INFO *>(calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ));
103 assert(pSymbol);
104 pSymbol->MaxNameLen = 1024 - 1;
105 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
106 HANDLE hProcess = GetCurrentProcess();
108 auto nFrames = backtraceState->nDepth;
109 for( int i = 0; i < nFrames; i++ )
111 SymFromAddr( hProcess, reinterpret_cast<DWORD64>(backtraceState->buffer[ i ]), nullptr, pSymbol );
112 aBuf.append( static_cast<sal_Int32>(nFrames - i - 1) );
113 aBuf.append( ": " );
114 aBuf.appendAscii( pSymbol->Name );
115 aBuf.append( " - 0x" );
116 aBuf.append( static_cast<sal_Int64>(pSymbol->Address), 16 );
117 aBuf.append( "\n" );
120 free( pSymbol );
122 return aBuf.makeStringAndClear();
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */