Skip to content

apps/system/init: Add C++ terminate handler support#7143

Open
pcs1265 wants to merge 2 commits intoSamsung:masterfrom
pcs1265:260203_cxx_terminate_handler
Open

apps/system/init: Add C++ terminate handler support#7143
pcs1265 wants to merge 2 commits intoSamsung:masterfrom
pcs1265:260203_cxx_terminate_handler

Conversation

@pcs1265
Copy link
Member

@pcs1265 pcs1265 commented Feb 3, 2026

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

This PR includes defconfig modification to fix CI build error.

static void __global_terminate_handler(void)
{
if (!IS_SECURE_STATE()) {
sched_lock();
Copy link
Member Author

@pcs1265 pcs1265 Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please share your opinion that sched_lock is needed here or not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, sched_lock isn’t necessary.
However, if we’re going to handle it with PANIC() anyway, it seems worthwhile to ensure that this thread can operate without being preempted by other threads.
On the other hand, depending on the config, the system might not reset after a PANIC(), so it should be paired with sched_unlock.

@pcs1265 pcs1265 force-pushed the 260203_cxx_terminate_handler branch 2 times, most recently from 21ca162 to f48776f Compare February 3, 2026 03:24
@sunghan-chang
Copy link
Contributor

@aashish-l @amandeep-samsung Could you review this?

@pcs1265 pcs1265 force-pushed the 260203_cxx_terminate_handler branch 2 times, most recently from 7bffa95 to 2c72f82 Compare February 3, 2026 08:48
if (!IS_SECURE_STATE()) {
sched_lock();

#ifdef CONFIG_LIBCXX_EXCEPTION
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using #if defined(CONFIG_LIBCXX_EXCEPTION) && defined(__EXCEPTIONS).
__EXCEPTIONS is defined when -fexceptions is enabled.

{
printf("C++ terminate handler called! PID: %d\n", getpid());
if (!IS_SECURE_STATE()) {
sched_lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an one simple question, All log in try-catch must be printed out totally. can we guarantee it by sched_lock ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In SMP configuration, We can't.
If assert or panic occur on other core while terminate_handler printing logs, the log will be lost.

@pcs1265 pcs1265 force-pushed the 260203_cxx_terminate_handler branch 2 times, most recently from 7c942a2 to d5f83fb Compare February 5, 2026 03:19
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`
@pcs1265 pcs1265 force-pushed the 260203_cxx_terminate_handler branch from d5f83fb to 9293a2c Compare February 5, 2026 03:20
artik055s/audio enables LIBCXX_EXCEPTION. but Make.defs does not accept that so fix the mismatching Make.defs
and Exception handling requires newer toolchain version. so update defconfig to use 2.0.0 docker image.
@pcs1265 pcs1265 force-pushed the 260203_cxx_terminate_handler branch from 9293a2c to 5f50d9d Compare February 5, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants