2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include "libpbd-config.h"
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"
32 #ifdef PLATFORM_WINDOWS
42 /* Obtain a backtrace and print it to stdout. */
49 PBD::stacktrace (std::ostream& out, int levels)
56 size = backtrace (array, 200);
59 strings = backtrace_symbols (array, size);
63 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
64 out << " " << demangle (strings[i]) << std::endl;
70 out << "no stacktrace available!" << std::endl;
74 #elif defined (PLATFORM_WINDOWS)
76 #if defined DEBUG && !defined CaptureStackBackTrace
77 #define CaptureStackBackTrace RtlCaptureStackBackTrace
80 __declspec(dllimport) USHORT WINAPI CaptureStackBackTrace (
82 ULONG FramesToCapture,
84 PULONG BackTraceHash);
89 PBD::stacktrace (std::ostream& out, int)
92 const size_t levels = 62; // does not support more then 62 levels of stacktrace
94 void * stack[ levels ];
95 unsigned short frames;
99 process = GetCurrentProcess();
100 out << "+++++Backtrace process: " << DEBUG_THREAD_SELF << std::endl;
102 SymInitialize (process, NULL, TRUE);
104 frames = CaptureStackBackTrace (0, levels, stack, NULL);
106 out << "+++++Backtrace frames: " << frames << std::endl;
108 symbol = (SYMBOL_INFO*)calloc (sizeof (SYMBOL_INFO) + 256 * sizeof (char), 1);
109 symbol->MaxNameLen = 255;
110 symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
112 for (i = 0; i < frames; ++i) {
113 SymFromAddr (process, (DWORD64)(stack[i]), 0, symbol);
114 out << string_compose ("%1: %2 - %3\n", samples - i - 1, symbol->Name, symbol->Address);
126 PBD::stacktrace (std::ostream& out, int /*levels*/)
128 out << "stack tracing is not enabled on this platform" << std::endl;
136 PBD::stacktrace (std::cout);