minghua
minghua

Reputation: 6613

Any Idea how to make C++ throw an exception on a kernel fault

Or in another words: How do you throw a kernel fault through to a C++ exception?

Because C++ is able to throw most of the programming errors as an exception. But why not also the divide-by-zero fault? The segmentation fault? And the other kernel faults?

I understand how those faults are raised in kernel and handled by kernel. I also understand how an exception is created and handled in C++ with aid from the compiler. How do you see the dots between the two can be connected? Are there any solid work with this regard?

Thanks in advance for any ideas or hints.

Edit in response to possible-duplicate flag: As a similar question How to catch segmentation already asked about the capture part of a segv, the recovery is still missing. Moreover I'm looking for a systematic approach to recover from all kinds of kernel exceptions, not just the segv.

Edit again: I have explained why this question is different than the other question. Still 5 people voted to close.

Upvotes: 0

Views: 1032

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120079

"kernel fault" is not a standard term. There are "kernel panic" events. Those are catastrophic failures in the kernel itself caused by bugs in the kernel code. Your application has nothing to do with them.

If we are talking about user space programs written in C++, signals such as SIGSEGV (but not signals such as SIGUSR1) are events caused by undefined behaviour in C++ code. It may or may not be handled by the kernel, depending on what kind of OS and what kind of program you have. Either way it is not called a kernel fault. As far as C++ is concerned, this is the end of the world. You never try to handle such signals, except probably to attempt to print a farewell message and exit. There is no recovery.

In some very rare and implementation-dependent cases catching signals like SIGSEGV may make sense, but a systematic approach does not and cannot exist.

Edit in response to possible-duplicate flag: As a similar question How to catch segmentation already asked about the capture part of a segv, the recovery is still missing. Moreover I'm looking for a systematic approach to recover from all kinds of kernel exceptions, not just the segv.

Recovery is not possible in a generic way, therefore the question is similar to,

At first glance this seems possible. However, many of the exceptions can indicate a corrupt environment where there is no possible way to recover, yet alone resume normal execution.

Upvotes: 2

Related Questions