| 1 | |
|---|---|
| 2 | // Copyright Oliver Kowalke 2016. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #define UNW_LOCAL_ONLY |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | #include <iostream> |
| 11 | |
| 12 | #include <libunwind.h> |
| 13 | |
| 14 | #include <boost/context/continuation.hpp> |
| 15 | |
| 16 | namespace ctx = boost::context; |
| 17 | |
| 18 | void backtrace() { |
| 19 | unw_cursor_t cursor; |
| 20 | unw_context_t context; |
| 21 | unw_getcontext( & context); |
| 22 | unw_init_local( & cursor, & context); |
| 23 | while ( 0 < unw_step( & cursor) ) { |
| 24 | unw_word_t offset, pc; |
| 25 | unw_get_reg( & cursor, UNW_REG_IP, & pc); |
| 26 | if ( 0 == pc) { |
| 27 | break; |
| 28 | } |
| 29 | std::cout << "0x"<< pc << ":"; |
| 30 | |
| 31 | char sym[256]; |
| 32 | if ( 0 == unw_get_proc_name( & cursor, sym, sizeof( sym), & offset) ) { |
| 33 | std::cout << " ("<< sym << "+0x"<< offset << ")"<< std::endl; |
| 34 | } else { |
| 35 | std::cout << " -- error: unable to obtain symbol name for this frame"<< std::endl; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void bar() { |
| 41 | backtrace(); |
| 42 | } |
| 43 | |
| 44 | void foo() { |
| 45 | bar(); |
| 46 | } |
| 47 | |
| 48 | ctx::continuation f1( ctx::continuation && c) { |
| 49 | foo(); |
| 50 | return std::move( c); |
| 51 | } |
| 52 | |
| 53 | int main() { |
| 54 | ctx::callcc( fn&: f1); |
| 55 | std::cout << "main: done"<< std::endl; |
| 56 | return EXIT_SUCCESS; |
| 57 | } |
| 58 |
