Patch from agorka to add some includes required for building with the GCC shipped...
[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 "pbd/stacktrace.h"
21 #include <cstdio>
22 #include <iostream>
23
24 void
25 PBD::trace_twb ()
26 {
27 }
28
29 /* Obtain a backtrace and print it to stdout. */
30
31 #ifdef HAVE_EXECINFO
32
33 #include <execinfo.h>
34
35 void
36 PBD::stacktrace (std::ostream& out, int levels)
37 {
38         void *array[200];
39         size_t size;
40         char **strings;
41         size_t i;
42      
43         size = backtrace (array, 200);
44         strings = backtrace_symbols (array, size);
45      
46         if (strings) {
47
48                 printf ("Obtained %zd stack frames.\n", size);
49                 
50                 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
51                         out << strings[i] << std::endl;
52                 }
53                 
54                 free (strings);
55         }
56 }
57
58 #else
59
60 void
61 PBD::stacktrace (std::ostream& out, int levels)
62 {
63         out << "stack tracing is not enabled on this platform" << std::endl;
64 }
65
66 void
67 c_stacktrace ()
68 {
69         PBD::stacktrace (std::cout);
70 }
71
72 #endif /* HAVE_EXECINFO */