How do you handle your Errors?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

How do you handle your Errors?

Post by VolTeK »

This thread is mean't to give out ideas to other developers of their own projects of how to handle certain error events.

In this thread, post a system error your OS would encounter, and how would your OS would handle it. Why do you think your means of handling the problem is the best solution? Sure there are simple ways of handling average errors, try and come up with complex situations, or complex ideas of how to handle a situation that seems impossible to get by (A system stop being required at times) that you think you can find a solution to that would keep it going (Staying stable, and operational). I do realize there are some errors that have no solution but a crash screen and reboot, so try and stay away from those.


This is mean't to be educational to others who probably haven't encountered the same error, and to help them for when they do.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: How do you handle your Errors?

Post by Jezze »

Dealing with errors and handling them is a very difficult and often overlooked problem.

I don't think their exist some sort of standardized way to handling them because an error can occur in so many different ways. It is very much an iterative process meaning that depending on what type of error you receive you need to handle each and every one of them differently and if the solutions is unsatisfactory you need to improve the handling of them. The only common ground I can think of is to log each error in a unified way. Other than that I think I'm afraid there is no good or general rule of thumb you can follow here.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: How do you handle your Errors?

Post by OSwhatever »

For OS development, a JTAG debugger is king, even better with MIPI or ETM recording ability. Unfortunately hobbyists might not have this luxury and therefore they have to find other ways. Emulators or HW with remote debugging availability is also very useful which can be easier to obtain.

In general, in order to get the errors, make sure you trap unhandled expeditions early in the project. Just having a register dump can very useful.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How do you handle your Errors?

Post by bluemoon »

My general rule is:
For service provider: If an error occurs, the function should have no side effect, and return negative error code.
For error handler: check the return value.

And this apply on nested layers of framework.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: How do you handle your Errors?

Post by gravaera »

Yo:

Nope, I don't handle my errors. I leave them as is and move on. Progress waits for no-one.

--Peace out,
gravaera
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
rdos
Member
Member
Posts: 3286
Joined: Wed Oct 01, 2008 1:55 pm

Re: How do you handle your Errors?

Post by rdos »

The hardest errors to handle is sudden device-removal, like floppy discs or USB-discs. These are hard because the OS needs to build-up a complex file system structure, and might be in the middle of updating something when the device becomes unavailable.

For standard errors, all APIs use CY flag for error signalling, and doesn't give any error-codes. Thus handling errors from the RDOS API is simple: Either something succeeded or it didn't, no code required to figure out why. :mrgreen:
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by JamesM »

I don't like propagating errors through negative return values.

Errors often appear in helper functions, and it can be useful for these helper functions to have a proper (non-success/fail) return value. Until C gets multiple return values, I have a function "set_errno()" which sets a thread-local variable (in an efficient way).

Functions therefore only have to propagate success or failure, and the failure mode can be read out of a thread local variable with "get_errno()".
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by Owen »

I'm a C++ nut.

Why do I say this? Because I have a standardised method of handling errors:

throw AnException();

I have a very "don't repeat yourself" attitude. Thats why I make pervasive use of RAII: For every resource managed by RAII, that's one less thing to go wrong. I love RAII: It really does result in a simplification of program code. It's not perfect, but it's close. Outside C++ (and Objective-C*), Go is probably the language which comes closest with Goroutines, but it still takes code on top of the normal program flow to handle cleanup.

I reserve catch blocks for *handling* exceptions: in general, I find their use to handle *cleanup* is in general a mistake, as it indicates that the lifetime of a resource is encoded in the program's behaviour rather than in its structure.

That said, RAII on its own isn't a panacea: it fails to handle cases involving mutable state on its own. However, it gets you at least 75% of the way, because most state change is resource acquisition or release, and most of the time you can get the next 15% of the way by restructuring things so that you acquire resources up front and then "commit them" with state changes at the end.

Still, I think that most of the time exception handling beats error code returns for cleanliness and code clarity. Some people say they dislike it, because it "hides behaviour from you", but I think that its mostly a matter of being used to error code handling. You have to get used to the change in structure from "Do something, then if that fails, undo it" to "Do something, then commit the results"

* Through it's almost-identical-to-C++ exception model and NSAutoreleasePool
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: How do you handle your Errors?

Post by eryjus »

JamesM wrote:[...], I have a function "set_errno()" which sets a thread-local variable (in an efficient way).
I like it. Gonna "borrow" the idea!
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by JamesM »

Owen wrote:I'm a C++ nut.

Why do I say this? Because I have a standardised method of handling errors:

throw AnException();

I have a very "don't repeat yourself" attitude. Thats why I make pervasive use of RAII: For every resource managed by RAII, that's one less thing to go wrong. I love RAII: It really does result in a simplification of program code. It's not perfect, but it's close. Outside C++ (and Objective-C*), Go is probably the language which comes closest with Goroutines, but it still takes code on top of the normal program flow to handle cleanup.

I reserve catch blocks for *handling* exceptions: in general, I find their use to handle *cleanup* is in general a mistake, as it indicates that the lifetime of a resource is encoded in the program's behaviour rather than in its structure.

That said, RAII on its own isn't a panacea: it fails to handle cases involving mutable state on its own. However, it gets you at least 75% of the way, because most state change is resource acquisition or release, and most of the time you can get the next 15% of the way by restructuring things so that you acquire resources up front and then "commit them" with state changes at the end.

Still, I think that most of the time exception handling beats error code returns for cleanliness and code clarity. Some people say they dislike it, because it "hides behaviour from you", but I think that its mostly a matter of being used to error code handling. You have to get used to the change in structure from "Do something, then if that fails, undo it" to "Do something, then commit the results"

* Through it's almost-identical-to-C++ exception model and NSAutoreleasePool
Out of interest, how have you solved the "exceptions aren't kernel thread reentrant" problem?
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by Owen »

JamesM wrote:Out of interest, how have you solved the "exceptions aren't kernel thread reentrant" problem?
"kernel thread reentrant"? I'm not quite sure what you're getting at with that question.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by JamesM »

Owen wrote:
JamesM wrote:Out of interest, how have you solved the "exceptions aren't kernel thread reentrant" problem?
"kernel thread reentrant"? I'm not quite sure what you're getting at with that question.
As far as I recall, the stack unwinding mechanism from libstdc++ is only reentrant across pthreads (uses pthread_setspecific?). I can't fully remember but knew there was some barrier to using exceptions in a homebrew kernel.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: How do you handle your Errors?

Post by Owen »

JamesM wrote:
Owen wrote:
JamesM wrote:Out of interest, how have you solved the "exceptions aren't kernel thread reentrant" problem?
"kernel thread reentrant"? I'm not quite sure what you're getting at with that question.
As far as I recall, the stack unwinding mechanism from libstdc++ is only reentrant across pthreads (uses pthread_setspecific?). I can't fully remember but knew there was some barrier to using exceptions in a homebrew kernel.
I use a modified version of Pathscale's libcxxrt. Presently, it's modified to use C11 threading primitives instead of pthreads (so that when used in userspace it won't drag in any part of the POSIX emulation layer); I can see it being customised slightly further in order to better abstract it from the TLS system (thereby I can modify it to make direct access to a member of the kernel Thread class)

My intention is to upstream these changes
palk
Posts: 16
Joined: Mon Nov 15, 2010 8:30 pm

Re: How do you handle your Errors?

Post by palk »

JamesM wrote:Until C gets multiple return values...
Like this?

Code: Select all

typedef struct {
	uint32_t value;
	_Bool valid : 1;
} uint32_v;

uint32_v foo(void *bar) {
	uint32_v invalid = { .valid = 0, };
	if (bar == NULL)
		return invalid;
	// do something
	uint32_v result = {
		.valid = 1,
		.value = whatever,
	};
	return result;
}
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How do you handle your Errors?

Post by gerryg400 »

In my kernel I use exceptions, coded in C and implemented with setjmp/longjmp. Generally the details are hidden inside my kernel library so that for example calling

Code: Select all

kcopy_fromuser(currt, d, s, n);
will return a simple EFAULT if the user passes a bad address for 's' and somehow causes a page fault or the like.

I haven't needed nested exceptions yet. My kernel is a very simple microkernel.
If a trainstation is where trains stop, what is a workstation ?
Post Reply