Skip to content

feat: add cpptrace for better stack traces #4042

Draft
kdrienCG wants to merge 5 commits intodevelopfrom
feature/kdrienCG/betterStackTrace
Draft

feat: add cpptrace for better stack traces #4042
kdrienCG wants to merge 5 commits intodevelopfrom
feature/kdrienCG/betterStackTrace

Conversation

@kdrienCG
Copy link
Copy Markdown

(Requires TPL PR #347)

This PR aims to improve the stack trace output when an error is thrown.

When GEOS crashes from a C++ exception, the stack trace we log is not really useful: it points at main.cpp instead of the code that actually threw, missing on valuable informations for devs. This is not a bug in the error handler, it is how the C++ runtime works.
cpptrace solves this by providing a mechanism to get stack traces from thrown exceptions, that will make us able to know exactly where the code went.

Here is an example defining a myThrowingFunction() incorrectly using a map directly (written in the main.cpp for convenience)

Without cpptrace:

***** Exception
***** Rank 0
***** Message :
map::at

***** StackTrace of 7 frames
  - Frame  0:  geos::ErrorLogger::initCurrentExceptionMessage(geos::MsgType, std::basic_string_view<char, std::char_traits<char> >, int) 
  - Frame  1:  main /somewhere/in/my/disk/geos/GEOS/GEOS/src/main/main.cpp:272 (discriminator 3)
  - Frame  2:  __libc_start_main 
  - Frame  3:  _start 
  - Frame  4:  main /somewhere/in/my/disk/geos/GEOS/src/main/main.cpp:275 (discriminator 2)
  - Frame  5:  __libc_start_main 
  - Frame  6:  _start 
*****

With cpptrace:
(truncated to fit the PR description)

***** Exception
***** Rank 0
***** Message :
map::at

***** StackTrace of 22 frames
  - Frame  0: 0x00007fb50abec834 at /lib64/libstdc++.so.6
  - Frame  1: 0x00007fb50abed136 in __gxx_personality_v0 at /lib64/libstdc++.so.6
  - Frame  2: 0x00007fb50a94df0a in _Unwind_RaiseException at /lib64/libgcc_s.so.1
  - Frame  3: 0x00007fb50abed7fa in __cxa_throw at /lib64/libstdc++.so.6
  - Frame  4: 0x00007fb50abe9044 at /lib64/libstdc++.so.6
  - Frame  5: 0x00000000004512b4 in std::map<std::__cxx11::basic_string...lset-12/root/usr/include/c++/12/bits/stl_map.h:551:24
  - Frame  6: 0x00000000004491d3 in myThrowingFunction() at /somewhere/in/my/disk/geos/GEOS/src/main/main.cpp:57:24
  - Frame  7: 0x0000000000449289 in operator() at /somewhere/in/my/disk/geos/GEOS/src/main/main.cpp:141:16
  - Frame  8: 0x000000000044a08e in do_try_catch<const geos::NotAnError&, main(int, char**)::<lambda()>, main(int, ...
  - Frame  9: 0x0000000000449ff5 in operator() at /somewhere/in/my/disk/geos/thirdPartyLibs/gcc-12.2.1/cpptrace...
  - Frame 10: 0x000000000044a1db in do_try_catch<geos::Exception&, cpptrace::v1::detail::try_catch_impl<main(int, ...
  - Frame 11: 0x000000000044a14f in operator() at /somewhere/in/my/disk/geos/thirdPartyLibs/gcc-12.2.1/cpptrace...
  - Frame 12: 0x000000000044a30e in do_try_catch<const std::exception&, cpptrace::v1::detail::try_catch_impl<try_...
  - Frame 13: 0x000000000044a29d in operator() at /somewhere/in/my/disk/geos/thirdPartyLibs/gcc-12.2.1/cpptrace...
  - Frame 14: 0x000000000044a3b5 in try_catch_impl<cpptrace::v1::detail::try_catch_impl<try_catch_impl<try_...
  - Frame 15: 0x000000000044a2d8 in try_catch_impl<cpptrace::v1::detail::try_catch_impl<try_catch_impl<main(int, ...
  - Frame 16: 0x000000000044a1a1 in try_catch_impl<cpptrace::v1::detail::try_catch_impl<main(int, char**)::<lambda()>,...
  - Frame 17: 0x000000000044a05f in try_catch_impl<main(int, char**)::<lambda()>, main(int, char**)::<lambda...
  - Frame 18: 0x0000000000449f77 in try_catch<main(int, char**)::<lambda()>, main(int, char**)::<lambda(const...
  - Frame 19: 0x0000000000449ecc in main at /somewhere/in/my/disk/geos/GEOS/src/main/main.cpp:138:3
  - Frame 20: 0x00007fb50a5a07e4 in __libc_start_main at /lib64/libc.so.6
  - Frame 21: 0x0000000000448c3d in _start at /somewhere/in/my/disk/geos/GEOS/build-p4log-linux-rhel8-zen3-gcc@12.2.1-debug/bin/geosx
*****

We have a detailled stack trace with the name of the function that throws and the correct line and character position.

@kdrienCG kdrienCG self-assigned this Apr 28, 2026
@kdrienCG kdrienCG added type: feature New feature or request flag: requires updated TPL(s) Needs a specific TPL PR labels Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flag: requires updated TPL(s) Needs a specific TPL PR type: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant