boostorg / stacktrace

C++ library for storing and printing backtraces.
https://boost.org/libs/stacktrace
422 stars 69 forks source link

from_dump unable to load safe_dump_to file #145

Open syamajala opened 10 months ago

syamajala commented 10 months ago

I don't seem to be able to read a stacktrace file written by one process using another process. Here is a simple example:

write.cc:

#include <boost/stacktrace/safe_dump_to.hpp>

int nested_func(int c)
{
  boost::stacktrace::safe_dump_to("stack.bin");
  return c + 1;
}

int func(int b)
{
  return nested_func(b + 1);
}

int main()
{
  func(777);
}

read.cc:

#include <boost/stacktrace/stacktrace.hpp>
#include <iostream>
#include <fstream>

int main()
{
  std::ifstream fs("stack.bin", std::ios::binary);

  const auto st = boost::stacktrace::basic_stacktrace<>::from_dump(fs);
  std::cout << st << std::endl;
}

This results in the following:

 0# 0x0000564109D1F1B5
 1# 0x0000564109D1F1DA
 2# 0x0000564109D1F1EA
 3# 0x00007FC002620CD0
 4# 0x00007FC002620D8A
 5# 0x0000564109D1F0B5

If I read stack.bin from write.cc though it seems to work correctly.

Both read.cc and write.cc where built the following way:

g++ -DBOOST_STACKTRACE_USE_BACKTRACE -lbacktrace -ldl -g -o read read.cc
g++ -DBOOST_STACKTRACE_USE_BACKTRACE -lbacktrace -ldl -g -o write write.cc
apolukhin commented 6 months ago

Yes, this is due to PIC/PIE. I should provide a patch like https://github.com/boostorg/stacktrace/commit/fbcd543b5180b8ee307fef6fbfc34aa372d330cf to make it work for position independent code. This would take quite some time...