Skip to content

How to add stack traces to assertion failures? #302

@Seb-C

Description

@Seb-C

Hello,

I've been using this library for a while, and it's been super useful to me so far, thanks for making it!

Apologizes if there is an easy way that I overlooked (I'm quite new to C++), but I have been trying for a while to intercept and get a proper stack trace for runtime assertion failures, without success.

Is there any way to do this currently in cpptrace (or maybe a hint as to how I could achieve this)?

Exception handling does not catch it (as expected). It does not seem possible to intercept it with a terminate_handler either, and intercepting SIGABRT didn't help.

Example:

#include <cpptrace/cpptrace.hpp>
#include <iostream>
#include <memory>

struct Foo {
	int x;
};

int triggerAssertFailure() {
	auto foo = std::make_unique<Foo>(42);
	auto bar = std::move(foo);
	Foo& fooPtr = *foo; // <- Making it fail here
	return fooPtr.x;
}

int main() {
	cpptrace::absorb_trace_exceptions(false);
	cpptrace::use_default_stderr_logger();
	cpptrace::register_terminate_handler();

	std::cout << "RESULT: " << triggerAssertFailure() << "\n";

	std::cout << "DONE\n";
	return 0;
}
/usr/lib/gcc/x86_64-linux-gnu/15/../../../../include/c++/15/bits/unique_ptr.h:455: typename add_lvalue_reference<element_type>::type std::unique_ptr<Foo>::operator*() const [_Tp = Foo, _Dp = std::default_delete<Foo>]: Assertion 'get() != pointer()' failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions