github.com/searKing/golang/go@v1.2.117/runtime/cgosymbolizer/traceback.cpp (about)

     1  #include "traceback.h"
     2  
     3  #include <stdio.h>
     4  #include <string.h>
     5  
     6  #include <boost/stacktrace.hpp>
     7  #include <string>
     8  // Gather addresses from the call stack.
     9  void cgoTraceback(cgoTracebackArg* arg) {
    10    try {
    11      // We can only unwind the current stack.
    12      if (arg->context != 0) {
    13        arg->buf[0] = 0;
    14        return;
    15      }
    16  
    17      std::size_t skip = 3;
    18      std::size_t max_depth = arg->max;
    19  
    20      boost::stacktrace::stacktrace stacktrace(skip, max_depth);
    21  //    std::cout << boost::stacktrace::stacktrace();
    22  
    23      std::size_t i = 0;
    24      for (auto it = stacktrace.cbegin(); it != stacktrace.cend(); it++) {
    25        arg->buf[i++] = (uintptr_t)(it->address());
    26      }
    27      auto frames_count = stacktrace.size();
    28      // The list of addresses terminates at a 0, so make sure there is one.
    29      if (frames_count < 0) {
    30        arg->buf[0] = 0;
    31      } else if (frames_count < arg->max) {
    32        arg->buf[frames_count] = 0;
    33      }
    34    } catch (...) {
    35      // ignore exception
    36    }
    37  
    38  }