New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flawed safe_dump_to(..) API #62

Closed
cmannett85 opened this Issue Sep 2, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@cmannett85

cmannett85 commented Sep 2, 2018

I'm trying to save a stacktrace to raw memory using the safe_dump_to(void *, std::size_t) function, however the function returns the number of stack frames (plus the termination frame) written - not the number of bytes written.

This means that when I come to read this data, I have no idea what to put in the size field of stacktrace::from_dump(const void *, std::size_t, const allocator_type &). At first I assumed that I could use sizeof(boost::stacktrace::frame) * frames, but the frame type contains heap allocated memory (std::strings) so that won't work.

This SSCE demonstrates the problem:

#include <iostream>

#include <boost/stacktrace.hpp>

int main()
{
    auto buf = std::array<char, 8192>{};
    const auto size = boost::stacktrace::safe_dump_to(buf.data(), buf.size());
    const auto st = boost::stacktrace::stacktrace::from_dump(buf.data(), size);

    std::cout << "ST from dump (" << size << " bytes): " << st << std::endl
              << "ST: " << boost::stacktrace::stacktrace{} << std::endl;


    return EXIT_SUCCESS;
}

It outputs:

ST from dump (4 bytes): 
ST:  0# 0x00000000004016A7 in ./a.out
 1# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 2# 0x0000000000401759 in ./a.out

The second 'ST' line is just to show what is expected. The first line fails because I'm trying to treat the frame count as a byte size, which it obviously isn't.

So how is the API supposed to be used?

@apolukhin

This comment has been minimized.

Member

apolukhin commented Sep 13, 2018

You're right the API may be confusing.

from_dump takes the size argument that is the buffer size. In other words it usually should be the same as the buffer size that you've passed to the safe_dump_to.

Here' a non trivial example on that

= boost::stacktrace::stacktrace::from_dump(f + 1, g_region.get_size() - sizeof(bool));

If you do wish to get the actually consumed bytes, you should sizeof(boost::stacktrace::frame::native_frame_ptr_t) * frames. I'll update the reference to reflect that.

apolukhin added a commit that referenced this issue Sep 13, 2018

apolukhin added a commit that referenced this issue Sep 13, 2018

@apolukhin

This comment has been minimized.

Member

apolukhin commented Sep 17, 2018

Updated and regenerated the docs

@apolukhin apolukhin closed this Sep 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment