Allgemeines

Fuer uns wichtige Punkte im Source Baum:

sonstiges:

Debugging schwierig, also aufpassen! Kein floating Point SMP & kernel preemption muessen bedacht werden

Wichtigste Optimierung: Code sollte schnell zu verstehen sein.

Es gibt einige Kernel Debugging Optionen, die einem helfen ein paar haeufige Fehler zu entdecken.

Error checking

#include <linux/err.h> #include <linux/errno.h>

Check the return value! Most functions use negative return values to indicate an error. The actual value determines the kind of error (as defined in include/linux/errno.h). When an invalid parameter was encountered, -EINVAL is returned for example.

There are two different conventions for functions that return pointers:

  1. return NULL in case of error (e.g. kmalloc)

  2. return a special pointer indicating the actual error.

Be careful to check for the right condition! There are some macros to work with the second method:

IS_ERR(ptr)

return true if the given pointer is considered an error code

PTR_ERR(ptr)

return the (negative) error code represented by the pointer

ERR_PTR(error)

wrap the (negative) error code inside a pointer

If your function has to error-out, be careful to undo every step that already succeeded. The following structure is often used in the kernel to do that:

 	ret = register_foo(...);
 	if (ret < 0) goto out;
 	ret = register_bar(...);
 	if (ret < 0) goto out_foo;
 	ret = -ENOMEM;
 	obj = kmalloc(...);
 	if (obj == NULL) goto out_bar;
 	more_work();
 	err = 0;
 	goto out;
 out_bar:
 	unregister_bar(...);
 out_foo:
 	unregister_foo(...);
 out:
 	return ret;

When any subfunction returns with an error, we undo everything in the reverse order and pass the return code to the calling function, which will do similiar things on its own. This is a little bit like C++ exceptions.

Webseiten

Tipps

  • Alle Funktionen und Variablen static machen.

  • Alle Funktionen mit dem Treibernamen prefixen.

Dadurch gibts keine Konflikte mit vordefinierten Routinen und man sieht in einem Backtrace gleich wer schuld ist.

  • Addressraum annotierungen: __USER, __iomem

  • Anzeigen von aufgerufenen Systemcalls: strace