NO-OP re-indent
[ardour.git] / libs / pbd / stacktrace.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "libpbd-config.h"
21
22 #include "pbd/stacktrace.h"
23 #include "pbd/debug.h"
24 #include "pbd/demangle.h"
25 #include "pbd/compose.h"
26 #include "pbd/pthread_utils.h"
27
28 #include <cstdio>
29 #include <iostream>
30 #include <string>
31
32 #ifdef PLATFORM_WINDOWS
33 #include <windows.h>
34 #include <dbghelp.h>
35 #endif
36
37 void
38 PBD::trace_twb ()
39 {
40 }
41
42 /* Obtain a backtrace and print it to stdout. */
43
44 #ifdef HAVE_EXECINFO
45
46 #include <execinfo.h>
47
48 void
49 PBD::stacktrace (std::ostream& out, int levels)
50 {
51         void *array[200];
52         size_t size;
53         char **strings;
54         size_t i;
55
56         size = backtrace (array, 200);
57
58         if (size) {
59                 strings = backtrace_symbols (array, size);
60
61                 if (strings) {
62
63                         for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
64                                 out << "  " << demangle (strings[i]) << std::endl;
65                         }
66
67                         free (strings);
68                 }
69         } else {
70                 out << "no stacktrace available!" << std::endl;
71         }
72 }
73
74 #elif defined (PLATFORM_WINDOWS)
75
76 #if defined DEBUG && !defined CaptureStackBackTrace
77 #define CaptureStackBackTrace RtlCaptureStackBackTrace
78
79 extern "C" {
80     __declspec(dllimport) USHORT WINAPI CaptureStackBackTrace (
81                                  ULONG  FramesToSkip,
82                                  ULONG  FramesToCapture,
83                                  PVOID  *BackTrace,
84                                  PULONG BackTraceHash
85                               );
86 }
87 #endif
88
89 void
90 PBD::stacktrace( std::ostream& out, int)
91 {
92 #ifdef DEBUG
93         const size_t levels = 62; // does not support more then 62 levels of stacktrace
94         unsigned int   i;
95         void         * stack[ levels ];
96         unsigned short frames;
97         SYMBOL_INFO  * symbol;
98         HANDLE         process;
99
100         process = GetCurrentProcess();
101         out << "+++++Backtrace process: " <<  DEBUG_THREAD_SELF << std::endl;
102
103         SymInitialize( process, NULL, TRUE );
104
105         frames               = CaptureStackBackTrace( 0, levels, stack, NULL );
106
107         out << "+++++Backtrace frames: " <<  frames << std::endl;
108
109         symbol               = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
110         symbol->MaxNameLen   = 255;
111         symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
112
113         for( i = 0; i < frames; i++ )
114         {
115                 SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
116                 out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address );
117         }
118
119         out.flush();
120
121         free( symbol );
122 #endif
123 }
124
125 #else
126
127 void
128 PBD::stacktrace (std::ostream& out, int /*levels*/)
129 {
130         out << "stack tracing is not enabled on this platform" << std::endl;
131 }
132
133 #endif
134
135 void
136 c_stacktrace ()
137 {
138         PBD::stacktrace (std::cout);
139 }