Radium Engine  1.5.20
Loading...
Searching...
No Matches
StackTrace.cpp
1#include <Core/Utils/StackTrace.hpp>
2#include <radium_backtrace.h> // for backtrace, generated by cmake
3
4namespace Ra {
5namespace Core {
6namespace Utils {
7
8std::string StackTrace( int skip ) {
9#ifdef HAS_BACKTRACE
10 void* callstack[128];
11 const int nMaxFrames = sizeof( callstack ) / sizeof( callstack[0] );
12 char buf[1024];
13 int nFrames = backtrace( callstack, nMaxFrames );
14
15 std::ostringstream trace_buf;
16 for ( int i = skip; i < nFrames; i++ ) {
17 Dl_info info;
18 if ( dladdr( callstack[i], &info ) ) {
19 char* demangled = nullptr;
20 int status;
21 demangled = abi::__cxa_demangle( info.dli_sname, NULL, 0, &status );
22 snprintf( buf,
23 sizeof( buf ),
24 "%-3d %*0p %s + %td\n",
25 i,
26 int( 2 + sizeof( void* ) * 2 ),
27 callstack[i],
28 status == 0 ? demangled : info.dli_sname,
29 (char*)callstack[i] - (char*)info.dli_saddr );
30 free( demangled );
31 }
32 else {
33 snprintf( buf,
34 sizeof( buf ),
35 "%-3d %*0p\n",
36 i,
37 int( 2 + sizeof( void* ) * 2 ),
38 callstack[i] );
39 }
40 trace_buf << buf;
41 }
42 if ( nFrames == nMaxFrames ) trace_buf << " [truncated]\n";
43 return trace_buf.str();
44#else
45 return "Execution stack trace not available.";
46#endif
47}
48
49} // namespace Utils
50} // namespace Core
51} // namespace Ra
T snprintf(T... args)
T free(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T str(T... args)