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     assert(maxDepth != 0);
41     maxDepth = clampToULONG(maxDepth);
42 
43     OUStringBuffer aBuf;
44 
45     HANDLE hProcess = GetCurrentProcess();
46     SymInitialize( hProcess, nullptr, true );
47 
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 );
55 
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 );
61 
62     for( sal_uInt32 i = 0; i < nFrames; i++ )
63     {
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" );
71     }
72 
73     free( pSymbol );
74 
75     return aBuf.makeStringAndClear();
76 }
77 
backtrace_get(sal_uInt32 maxDepth)78 std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth)
79 {
80     assert(maxDepth != 0);
81     maxDepth = clampToULONG(maxDepth);
82 
83     HANDLE hProcess = GetCurrentProcess();
84     SymInitialize( hProcess, nullptr, true );
85 
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 );
93 
94     return std::unique_ptr<BacktraceState>(new BacktraceState{ pStack, nFrames });
95 }
96 
backtrace_to_string(BacktraceState * backtraceState)97 OUString sal::backtrace_to_string(BacktraceState* backtraceState)
98 {
99     OUStringBuffer aBuf;
100 
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();
107 
108     auto nFrames = backtraceState->nDepth;
109     for( int i = 0; i < nFrames; i++ )
110     {
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" );
118     }
119 
120     free( pSymbol );
121 
122     return aBuf.makeStringAndClear();
123 }
124 
125 
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
127