tdf#130857 qt weld: Delete unused image label object
[LibreOffice.git] / sal / osl / w32 / backtrace.cxx
blob0b38d1962bc1b0f90b4a7c21c9591ad8ad172745
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 std::unique_ptr<sal::BacktraceState> backtrace = sal::backtrace_get( maxDepth );
41 return sal::backtrace_to_string( backtrace.get());
44 std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth)
46 assert(maxDepth != 0);
47 maxDepth = clampToULONG(maxDepth);
49 auto pStack = new void *[maxDepth];
50 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb204633.aspx
51 // "CaptureStackBackTrace function" claims that you "can capture up to
52 // MAXUSHORT frames", and on Windows Server 2003 and Windows XP it even
53 // "must be less than 63", but assume that a too large input value is
54 // clamped internally, instead of resulting in an error:
55 int nFrames = CaptureStackBackTrace( 0, static_cast<ULONG>(maxDepth), pStack, nullptr );
57 return std::unique_ptr<BacktraceState>(new BacktraceState{ pStack, nFrames });
60 OUString sal::backtrace_to_string(BacktraceState* backtraceState)
62 HANDLE hProcess = GetCurrentProcess();
63 // https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-syminitialize
64 // says to not initialize more than once.
65 [[maybe_unused]] static bool bInitialized = SymInitialize(hProcess, nullptr, false);
66 SymRefreshModuleList(hProcess);
67 SYMBOL_INFO * pSymbol;
68 pSymbol = static_cast<SYMBOL_INFO *>(calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ));
69 assert(pSymbol);
70 pSymbol->MaxNameLen = 1024 - 1;
71 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
73 auto nFrames = backtraceState->nDepth;
74 OUStringBuffer aBuf;
75 for( int i = 0; i < nFrames; i++ )
77 auto pSymAddr = reinterpret_cast<DWORD64>(backtraceState->buffer[ i ]);
78 SymFromAddr( hProcess, pSymAddr, nullptr, pSymbol );
79 aBuf.append( OUString::number(nFrames - i - 1) + ": " );
80 aBuf.appendAscii( pSymbol->Name );
81 aBuf.append( " - 0x" + OUString::number(pSymAddr, 16) + "\n" );
84 free( pSymbol );
86 return aBuf.makeStringAndClear();
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */