Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / context / example / execution_context_v2 / backtrace.cpp
1
2 //          Copyright Oliver Kowalke 2014.
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/execution_context.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::execution_context< void > f1( ctx::execution_context< void > && ctxm) {
49     foo();
50     return std::move( ctxm);
51 }
52
53 int main() {
54     ctx::execution_context< void > ctx1( f1);
55     ctx1 = ctx1();
56     std::cout << "main: done" << std::endl;
57     return EXIT_SUCCESS;
58 }