Cambridge SMT System
backtrace.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef BACKTRACE_HPP
16 #define BACKTRACE_HPP
17 
27 #include <execinfo.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 
31 #ifdef __cplusplus
32 #include <cxxabi.h>
33 #endif
34 
36 inline int displayBacktrace ( void ) {
37  const int size = 20;
38  void *buffer[size];
39  int ret;
40  int idx;
41  char **ptr;
42  // get the void* pointers to functions; "ret" is the number of items returned
43  ret = backtrace ( buffer, size );
44  // now we want to convert those pointers to text
45  ptr = backtrace_symbols ( buffer, ret );
46  for ( idx = 0; idx < ret; idx ++ ) {
47  // if using C++, you can look up abi::__cxa_demangle( ... ) in #include <cxxabi.h>
48  printf ( "%d: %s\n", idx, ptr[ idx ] );
49  }
50  free ( ptr );
52  /*
53  #ifdef __cplusplus
54  // here is an example of C++ demangling
55  int status = -1;
56  char *demangledName = abi::__cxa_demangle( "_Z16displayBacktracev", NULL, NULL, &status );
57  if ( status == 0 )
58  {
59  printf( "_Z16displayBacktracev demangles to: %s\n", demangledName );
60  }
61  free( demangledName );
62  #endif
63 
64  */
65  return ret;
66 }
67 
68 #endif // BACKTRACE_HPP
int displayBacktrace(void)
Display backtrace of functions.
Definition: backtrace.hpp:36