1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #include <sal/config.h>
11 
12 #include <limits>
13 #include <memory>
14 
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>
23 
24 #include <rtl/ustrbuf.hxx>
25 #include <sal/backtrace.hxx>
26 
27 #include <backtraceasstring.hxx>
28 
29 namespace {
30 
clampToULONG(T n)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;
34 }
35 
36 }
37 
backtraceAsString(sal_uInt32 maxDepth)38 OUString osl::detail::backtraceAsString(sal_uInt32 maxDepth)
39 {
40     std::unique_ptr<sal::BacktraceState> backtrace = sal::backtrace_get( maxDepth );
41     return sal::backtrace_to_string( backtrace.get());
42 }
43 
backtrace_get(sal_uInt32 maxDepth)44 std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth)
45 {
46     assert(maxDepth != 0);
47     maxDepth = clampToULONG(maxDepth);
48 
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 );
56 
57     return std::unique_ptr<BacktraceState>(new BacktraceState{ pStack, nFrames });
58 }
59 
backtrace_to_string(BacktraceState * backtraceState)60 OUString sal::backtrace_to_string(BacktraceState* backtraceState)
61 {
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. This still leaks for some
65     // reason if called often enough.
66     static bool needsInit = true;
67     if( needsInit )
68         SymInitialize( hProcess, nullptr, true );
69     else
70         SymRefreshModuleList( hProcess );
71     SYMBOL_INFO  * pSymbol;
72     pSymbol = static_cast<SYMBOL_INFO *>(calloc( sizeof( SYMBOL_INFO ) + 1024 * sizeof( char ), 1 ));
73     assert(pSymbol);
74     pSymbol->MaxNameLen = 1024 - 1;
75     pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
76 
77     auto nFrames = backtraceState->nDepth;
78     OUStringBuffer aBuf;
79     for( int i = 0; i < nFrames; i++ )
80     {
81         SymFromAddr( hProcess, reinterpret_cast<DWORD64>(backtraceState->buffer[ i ]), nullptr, pSymbol );
82         aBuf.append( static_cast<sal_Int32>(nFrames - i - 1) );
83         aBuf.append( ": " );
84         aBuf.appendAscii( pSymbol->Name );
85         aBuf.append( " - 0x" );
86         aBuf.append( static_cast<sal_Int64>(pSymbol->Address), 16 );
87         aBuf.append( "\n" );
88     }
89 
90     free( pSymbol );
91 
92     return aBuf.makeStringAndClear();
93 }
94 
95 
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
97