Skip to content

Commit f69b305

Browse files
edolstracole-h
authored andcommitted
__cxa_throw(): Print exception info
Example: Aborting on unexpected exception of type 'St16invalid_argument', error: bitset::_M_copy_from_ptr
1 parent 4cfdfb2 commit f69b305

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

nix-meson-build-support/common/cxa-throw/interpose-cxa-throw.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
#include "is-logic-error.hh"
66

7-
#ifndef CXA_THROW_ON_LOGIC_ERROR
8-
# define CXA_THROW_ON_LOGIC_ERROR() abort()
9-
#endif
10-
117
typedef void (*cxa_throw_type)(void *, std::type_info *, void (*)(void *));
128

139
extern "C" void __cxa_throw(void * exc, std::type_info * tinfo, void (*dest)(void *))
1410
{
1511
if (is_logic_error(tinfo))
16-
CXA_THROW_ON_LOGIC_ERROR();
12+
abort_on_exception(exc, tinfo);
1713

1814
static auto * orig = (cxa_throw_type) dlsym(RTLD_NEXT, "__cxa_throw");
1915
if (!orig)

nix-meson-build-support/common/cxa-throw/is-logic-error.hh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#pragma once
22

3+
#include <unistd.h>
34
#include <cxxabi.h>
45
#include <stdexcept>
56
#include <typeinfo>
7+
#include <cstring>
8+
9+
#ifndef CXA_THROW_ON_LOGIC_ERROR
10+
# define CXA_THROW_ON_LOGIC_ERROR() abort()
11+
#endif
612

713
static bool is_logic_error(const std::type_info * tinfo)
814
{
@@ -15,3 +21,20 @@ static bool is_logic_error(const std::type_info * tinfo)
1521

1622
return false;
1723
}
24+
25+
static void abort_on_exception(void * exc, const std::type_info * tinfo)
26+
{
27+
if (!is_logic_error(tinfo))
28+
return;
29+
30+
char buf[512];
31+
snprintf(
32+
buf,
33+
sizeof(buf),
34+
"Aborting on unexpected exception of type '%s', error: %s\n",
35+
tinfo->name(),
36+
((std::exception *) exc)->what());
37+
[[maybe_unused]] auto r = write(STDERR_FILENO, buf, strlen(buf));
38+
39+
CXA_THROW_ON_LOGIC_ERROR();
40+
}

nix-meson-build-support/common/cxa-throw/wrap-cxa-throw.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33

44
#include "is-logic-error.hh"
55

6-
#ifndef CXA_THROW_ON_LOGIC_ERROR
7-
# define CXA_THROW_ON_LOGIC_ERROR() abort()
8-
#endif
9-
106
extern "C" void __real___cxa_throw(void *, std::type_info *, void (*)(void *));
117

128
extern "C" void __wrap___cxa_throw(void * exc, std::type_info * tinfo, void (*dest)(void *))
139
{
1410
if (is_logic_error(tinfo))
15-
CXA_THROW_ON_LOGIC_ERROR();
11+
abort_on_exception(exc, tinfo);
1612

1713
__real___cxa_throw(exc, tinfo, dest);
1814

0 commit comments

Comments
 (0)