2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "stacktrace.h"
23 #include "stream/textstream.h"
25 #include "environment.h"
30 void write_stack_trace( TextOutputStream& outputStream ){
31 const unsigned int MAX_SYMBOLS = 256;
32 void* symbols[MAX_SYMBOLS];
34 // get return addresses
35 int symbol_count = backtrace( symbols, MAX_SYMBOLS );
37 if ( !symbol_count ) {
41 // resolve and print names
42 char** symbol_names = backtrace_symbols( symbols, symbol_count );
44 for ( int i = 0; ( i < symbol_count ); ++i )
45 outputStream << symbol_names[i] << "\n";
47 // not a memleak, see www.gnu.org/software/libc/manual (Debugging Support, Backtraces)
51 #elif defined ( WIN32 ) && defined ( _MSC_VER )
61 Address( void* value ) : m_value( value ){
65 /// \brief Writes an address \p p to \p ostream in hexadecimal form.
66 template<typename TextOutputStreamType>
67 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Address& p ){
68 const std::size_t bufferSize = ( sizeof( void* ) * 2 ) + 1;
70 ostream.write( buf, snprintf( buf, bufferSize, "%0p", p.m_value ) );
78 Offset( void* value ) : m_value( value ){
82 /// \brief Writes an address \p p to \p ostream in hexadecimal form.
83 template<typename TextOutputStreamType>
84 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Offset& p ){
85 const std::size_t bufferSize = ( sizeof( void* ) * 2 ) + 1;
87 ostream.write( buf, snprintf( buf, bufferSize, "%X", p.m_value ) );
91 /// \brief Writes a WCHAR string \p s to \p ostream.
92 template<typename TextOutputStreamType>
93 inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const WCHAR* s ){
94 const std::size_t bufferSize = 1024;
96 ostream.write( buf, snprintf( buf, bufferSize, "%ls", s ) );
100 struct EnumerateSymbolsContext
103 TextOutputStream& outputStream;
105 EnumerateSymbolsContext( STACKFRAME64& sf, TextOutputStream& outputStream ) : sf( sf ), outputStream( outputStream ), count( 0 ){
109 void write_symbol( PSYMBOL_INFO pSym, STACKFRAME64& sf, TextOutputStream& outputStream, std::size_t& count ){
113 EnumerateSymbolsCallback(
114 PSYMBOL_INFO pSymInfo,
117 write_symbol( pSymInfo, ( (EnumerateSymbolsContext*)UserContext )->sf, ( (EnumerateSymbolsContext*)UserContext )->outputStream, ( (EnumerateSymbolsContext*)UserContext )->count );
123 void write_stack_trace( PCONTEXT pContext, TextOutputStream& outputStream ){
124 HANDLE m_hProcess = GetCurrentProcess();
125 DWORD dwMachineType = 0;
127 CONTEXT context = *pContext;
129 // Could use SymSetOptions here to add the SYMOPT_DEFERRED_LOADS flag
130 if ( !SymInitialize( m_hProcess, (PSTR)environment_get_app_path(), TRUE ) ) {
135 memset( &sf, 0, sizeof( sf ) );
136 sf.AddrPC.Mode = AddrModeFlat;
137 sf.AddrStack.Mode = AddrModeFlat;
138 sf.AddrFrame.Mode = AddrModeFlat;
141 // Initialize the STACKFRAME structure for the first call. This is only
142 // necessary for Intel CPUs, and isn't mentioned in the documentation.
143 sf.AddrPC.Offset = context.Eip;
144 sf.AddrStack.Offset = context.Esp;
145 sf.AddrFrame.Offset = context.Ebp;
147 dwMachineType = IMAGE_FILE_MACHINE_I386;
149 sf.AddrPC.Offset = context.Rip;
150 sf.AddrStack.Offset = context.Rsp;
152 // MSDN: x64: The frame pointer is RBP or RDI. This value is not always used.
153 // very funny, we'll try Rdi for now
154 sf.AddrFrame.Offset = context.Rdi;
156 dwMachineType = IMAGE_FILE_MACHINE_AMD64;
159 const unsigned int max_sym_name = 1024; // should be enough
163 // Get the next stack frame
164 if ( !StackWalk64( dwMachineType,
170 SymFunctionTableAccess64,
176 if ( 0 == sf.AddrFrame.Offset ) { // Basic sanity check to make sure
177 break; // the frame is OK. Bail if not.
180 // Get the name of the function for this stack frame entry
181 BYTE symbolBuffer[ sizeof( SYMBOL_INFO ) + max_sym_name ];
182 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbolBuffer;
183 pSymbol->SizeOfStruct = sizeof( SYMBOL_INFO );
184 pSymbol->MaxNameLen = max_sym_name;
186 DWORD64 symDisplacement = 0; // Displacement of the input address,
187 // relative to the start of the symbol
189 IMAGEHLP_MODULE64 module = { sizeof( IMAGEHLP_MODULE64 ) };
190 if ( SymGetModuleInfo64( m_hProcess, sf.AddrPC.Offset, &module ) ) {
191 outputStream << module.ModuleName << "!";
193 if ( SymFromAddr( m_hProcess, sf.AddrPC.Offset, &symDisplacement, pSymbol ) ) {
194 char undecoratedName[max_sym_name];
195 UnDecorateSymbolName( pSymbol->Name, undecoratedName, max_sym_name, UNDNAME_COMPLETE );
197 outputStream << undecoratedName;
200 // Use SymSetContext to get just the locals/params for this frame
201 IMAGEHLP_STACK_FRAME imagehlpStackFrame;
202 imagehlpStackFrame.InstructionOffset = sf.AddrPC.Offset;
203 SymSetContext( m_hProcess, &imagehlpStackFrame, 0 );
205 // Enumerate the locals/parameters
206 EnumerateSymbolsContext context( sf, outputStream );
207 SymEnumSymbols( m_hProcess, 0, 0, EnumerateSymbolsCallback, &context );
210 outputStream << " + " << Offset( reinterpret_cast<void*>( symDisplacement ) );
212 // Get the source line for this stack frame entry
213 IMAGEHLP_LINE64 lineInfo = { sizeof( IMAGEHLP_LINE64 ) };
214 DWORD dwLineDisplacement;
215 if ( SymGetLineFromAddr64( m_hProcess, sf.AddrPC.Offset,
216 &dwLineDisplacement, &lineInfo ) ) {
217 outputStream << " " << lineInfo.FileName << " line " << Unsigned( lineInfo.LineNumber );
222 outputStream << Address( reinterpret_cast<void*>( sf.AddrPC.Offset ) );
226 outputStream << "\n";
229 SymCleanup( m_hProcess );
234 void write_stack_trace( TextOutputStream& outputStream ){
235 __try { RaiseException( 0,0,0,0 ); } __except( write_stack_trace( ( GetExceptionInformation() )->ContextRecord, outputStream ), EXCEPTION_CONTINUE_EXECUTION ) {
239 #elif defined ( WIN32 )
240 void write_stack_trace( TextOutputStream& outputStream ){
241 outputStream << "\nStacktrace is disabled on this compiler\n";
244 void write_stack_trace( TextOutputStream& outputStream ){
245 outputStream << "\nStacktrace is disabled on this platform\n";