lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sal / osl / w32 / backtrace.cxx
blob160d4e7d02071e636aa3f0719ba5e26b5a885043
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 pSymbol->MaxNameLen = 1024 - 1;
59 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
61 for( sal_uInt32 i = 0; i < nFrames; i++ )
63 SymFromAddr( hProcess, reinterpret_cast<DWORD64>(aStack[ i ]), nullptr, pSymbol );
64 aBuf.append( static_cast<sal_Int32>(nFrames - i - 1) );
65 aBuf.append( ": " );
66 aBuf.appendAscii( pSymbol->Name );
67 aBuf.append( " - 0x" );
68 aBuf.append( static_cast<sal_Int64>(pSymbol->Address), 16 );
69 aBuf.append( "\n" );
72 free( pSymbol );
74 return aBuf.makeStringAndClear();
77 std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth)
79 assert(maxDepth != 0);
80 maxDepth = clampToULONG(maxDepth);
82 HANDLE hProcess = GetCurrentProcess();
83 SymInitialize( hProcess, nullptr, true );
85 auto pStack = new void *[maxDepth];
86 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb204633.aspx
87 // "CaptureStackBackTrace function" claims that you "can capture up to
88 // MAXUSHORT frames", and on Windows Server 2003 and Windows XP it even
89 // "must be less than 63", but assume that a too large input value is
90 // clamped internally, instead of resulting in an error:
91 int nFrames = CaptureStackBackTrace( 0, static_cast<ULONG>(maxDepth), pStack, nullptr );
93 return std::unique_ptr<BacktraceState>(new BacktraceState{ pStack, nFrames });
96 OUString sal::backtrace_to_string(BacktraceState* backtraceState)
98 OUStringBuffer aBuf;
100 SYMBOL_INFO * pSymbol;
101 pSymbol = static_cast<SYMBOL_INFO *>(calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ));
102 pSymbol->MaxNameLen = 1024 - 1;
103 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
104 HANDLE hProcess = GetCurrentProcess();
106 auto nFrames = backtraceState->nDepth;
107 for( int i = 0; i < nFrames; i++ )
109 SymFromAddr( hProcess, reinterpret_cast<DWORD64>(backtraceState->buffer[ i ]), nullptr, pSymbol );
110 aBuf.append( static_cast<sal_Int32>(nFrames - i - 1) );
111 aBuf.append( ": " );
112 aBuf.appendAscii( pSymbol->Name );
113 aBuf.append( " - 0x" );
114 aBuf.append( static_cast<sal_Int64>(pSymbol->Address), 16 );
115 aBuf.append( "\n" );
118 free( pSymbol );
120 return aBuf.makeStringAndClear();
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */