Call Stack & Symbol Resolver Usage
What is it
A collection of tools to inspect the call stack and resolve symbols to a human-readable format. The collection is comprised of:
- Containers
- lpt::stack::call_stack< Depth >
- lpt::stack::call_frame
- lpt::stack::address_type // A plain memory address. Platform specific.
- Symbol resolvers:
- lpt::stack::null_symbol_info // Minimal; without memory allocations and other side effects.
- lpt::stack::basic_symbol_info // Resolution offered by default libraries on the platform.
- lpt::stack::extended_symbol_info // Best resolution with additional libraries (libbfd on GNU-Linux).
- Formatters: how to print the symbol information
- lpt::stack::terse_formatter
- lpt::stack::fancy_formatter
- Aggregator classes:
- lpt::stack::call_stack_info<>
- lpt::stack::call_frame_info<>
- lpt::stack::symbol_info<>
Supported platforms
- Windows: Visual Studio 2012 CTP; Win32, x64, Itanium
- GNU-Linux: gcc 4.7.0 and later; all supported CPUs
Usage
Capturing and printing the call stack
#include <lpt/call_stack.hpp>
// Link with: -lbfd on GNU-Linux
typedef lpt::stack::call_stack<40> wstack_type; // We want max 40 call frames
typedef lpt::stack::call_stack_info< wstack_type
, lpt::stack::extended_symbol_info // All possible symbol information for the platform
> extended_call_stack_info_type;
wstack_type here(true);
std::cout << "Stack: " << here.depth() << " frames:\n"
<< extended_call_stack_info_type(here) << std::endl;
On a Windows platform this could print:
Stack: 13 frames:
[13f5ab596] lpt::stack::detail::dbghelp::library::backtrace+0xb6
At c:\users\amelinte\desktop\lpt\lpt\include\lpt\detail\win_call_stack.h
pp:312
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5a1dd9] lpt::stack::detail::backtrace+0x39
At c:\users\amelinte\desktop\lpt\lpt\include\lpt\detail\win_call_stack.h
pp:386
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5ac7cf] lpt::stack::call_stack<40>::get_backtrace+0x5f
At c:\users\amelinte\desktop\lpt\lpt\include\lpt\call_stack.hpp:195
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5a68c2] lpt::stack::call_stack<40>::call_stack<40>+0x52
At c:\users\amelinte\desktop\lpt\lpt\include\lpt\call_stack.hpp:129
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5a1e58] func2+0x68
At c:\users\amelinte\desktop\lpt\tests\winstack\winstack.cpp:28
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5ad038] aclass::meth2+0x58
At c:\users\amelinte\desktop\lpt\tests\winstack\winstack.cpp:49
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5acfba] aclass::meth1+0x5a
At c:\users\amelinte\desktop\lpt\tests\winstack\winstack.cpp:55
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5a20dc] func1+0x5c
At c:\users\amelinte\desktop\lpt\tests\winstack\winstack.cpp:67
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5a218a] wmain+0x7a
At c:\users\amelinte\desktop\lpt\tests\winstack\winstack.cpp:77
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5ae80d] __tmainCRTStartup+0x19d
At f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crtexe.c:533
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[13f5ae93e] wmainCRTStartup+0xe
At f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crtexe.c:377
In C:\Users\amelinte\Desktop\lpt\tests\winstack\x64\Debug\winstack.exe
[774a652d] +0x0
At ??:0
In C:\Windows\system32\kernel32.dll
[7772c521] +0x0
At ??:0
In C:\Windows\SYSTEM32\ntdll.dll
Resolving and printing an individual frame of a call stack
lpt::stack::call_frame aframe( here[1] );
std::cout << "Frame 2:\n"
<< lpt::stack::call_frame_info< lpt::stack::extended_symbol_info
, lpt::stack::fancy_formatter >(aframe);
Resolving and printing some memory address
lpt::stack::extended_symbol_info ei1(0x1111); //Windows
lpt::stack::extended_symbol_info ei1(reinterpret_cast<lpt::stack::address_type>(0x1111)); //Linux
std:: cout << ei1;
Code