trace_addresses.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config.hpp>
  7. #ifdef BOOST_NO_CXX11_RANGE_BASED_FOR
  8. #include <boost/stacktrace.hpp>
  9. #include <iostream> // std::cout
  10. namespace bs = boost::stacktrace;
  11. void dump_compact(const bs::stacktrace& st) {
  12. for (unsigned i = 0; i < st.size(); ++i) {
  13. bs::frame frame = st[i];
  14. std::cout << frame.address() << ',';
  15. }
  16. std::cout << std::endl;
  17. }
  18. #else
  19. //[getting_started_trace_addresses
  20. #include <boost/stacktrace.hpp>
  21. #include <iostream> // std::cout
  22. namespace bs = boost::stacktrace;
  23. void dump_compact(const bs::stacktrace& st) {
  24. for (bs::frame frame: st) {
  25. std::cout << frame.address() << ',';
  26. }
  27. std::cout << std::endl;
  28. }
  29. //]
  30. #endif
  31. BOOST_NOINLINE boost::stacktrace::stacktrace rec1(int i);
  32. BOOST_NOINLINE boost::stacktrace::stacktrace rec2(int i);
  33. BOOST_NOINLINE boost::stacktrace::stacktrace rec1(int i) {
  34. if (i < 5) {
  35. if (!i) return boost::stacktrace::stacktrace();
  36. return rec2(--i);
  37. }
  38. return rec2(i - 2);
  39. }
  40. BOOST_NOINLINE boost::stacktrace::stacktrace rec2(int i) {
  41. if (i < 5) {
  42. if (!i) return boost::stacktrace::stacktrace();
  43. return rec2(--i);
  44. }
  45. return rec2(i - 2);
  46. }
  47. int main() {
  48. dump_compact(rec1(8));
  49. }