Skip to content

Commit 7bffa95

Browse files
committed
apps/system/init: Add C++ terminate handler support
This patch adds global terminate handler for unhandled C++ exceptions. The handler will print exception information and call PANIC() when an unhandled exception occurs. and Corrected a typo in `init.c`
1 parent e9c1e84 commit 7bffa95

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

apps/system/init/Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,21 @@ endif
6565

6666
ASRCS =
6767
CSRCS =
68+
69+
ifeq ($(CONFIG_HAVE_CXX)$(CONFIG_LIBCXX_EXCEPTION),yy)
70+
CXXSRCS = terminate_handler.cxx
71+
else
72+
CXXSRCS =
73+
endif
6874
MAINSRC = init.c
6975

7076
AOBJS = $(ASRCS:.S=$(OBJEXT))
7177
COBJS = $(CSRCS:.c=$(OBJEXT))
78+
CXXOBJS = $(CXXSRCS:.cxx=$(OBJEXT))
7279
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
7380

74-
SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
75-
OBJS = $(AOBJS) $(COBJS)
81+
SRCS = $(ASRCS) $(CSRCS) $(CXXSRCS) $(MAINSRC)
82+
OBJS = $(AOBJS) $(COBJS) $(CXXOBJS)
7683

7784
ifneq ($(CONFIG_BUILD_KERNEL),y)
7885
OBJS += $(MAINOBJ)
@@ -112,6 +119,9 @@ $(AOBJS): %$(OBJEXT): %.S
112119
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
113120
$(call COMPILE, $<, $@)
114121

122+
$(CXXOBJS): %$(OBJEXT): %.cxx
123+
$(call COMPILEXX, $<, $@)
124+
115125
.built: $(OBJS)
116126
$(call ARCHIVE, $(BIN), $(OBJS))
117127
$(Q) touch .built

apps/system/init/init.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
#include <semaphore.h>
5959
#include <errno.h>
6060
#endif
61+
#ifdef CONFIG_LIBCXX_EXCEPTION
62+
extern void register_cxx_terminate_handler(void);
63+
#endif
6164

6265
/****************************************************************************
6366
* Pre-processor Definitions
@@ -74,7 +77,7 @@ extern void iotjs_register_cmds(void);
7477
#endif
7578

7679
/****************************************************************************
77-
* Pravite Functions
80+
* Private Functions
7881
****************************************************************************/
7982
#ifdef CONFIG_TASH
8083
static void tash_register_cmds(void)
@@ -131,6 +134,9 @@ int preapp_start(int argc, char *argv[])
131134

132135
up_cxxinitialize();
133136
#endif
137+
#ifdef CONFIG_LIBCXX_EXCEPTION
138+
register_cxx_terminate_handler();
139+
#endif
134140

135141
#ifdef CONFIG_SYSTEM_INFORMATION
136142
sysinfo();
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/****************************************************************************
2+
*
3+
* Copyright 2026 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
/****************************************************************************
19+
* Included Files
20+
****************************************************************************/
21+
22+
#include <tinyara/config.h>
23+
#include <tinyara/security_level.h>
24+
25+
#include <cstdio>
26+
#include <debug.h>
27+
#include <exception>
28+
#include <stdlib.h>
29+
#include <typeinfo>
30+
#include <sched.h>
31+
32+
33+
/****************************************************************************
34+
* Private Functions
35+
****************************************************************************/
36+
37+
static void __global_terminate_handler(void)
38+
{
39+
if (!IS_SECURE_STATE()) {
40+
sched_lock();
41+
printf("C++ terminate handler called! PID: %d\n", getpid());
42+
43+
#ifdef CONFIG_LIBCXX_EXCEPTION
44+
// Check if there's an active exception
45+
std::exception_ptr exptr = std::current_exception();
46+
if (exptr) {
47+
try {
48+
std::rethrow_exception(exptr);
49+
} catch (const std::exception& e) {
50+
try {
51+
printf("Exception Type: %s\n", typeid(e).name());
52+
printf("Exception Message: %s\n", e.what());
53+
} catch (...) {
54+
printf("Exception info extraction failed\n");
55+
}
56+
} catch (...) {
57+
printf("Unknown exception type\n");
58+
}
59+
} else {
60+
printf("No active exception\n");
61+
}
62+
#endif
63+
}
64+
65+
PANIC();
66+
}
67+
68+
//***************************************************************************
69+
// Public Functions
70+
//***************************************************************************
71+
72+
extern "C" void register_cxx_terminate_handler(void)
73+
{
74+
std::set_terminate(__global_terminate_handler);
75+
}

0 commit comments

Comments
 (0)