Question

J'ai une question sur assert() sous Linux: puis-je utiliser dans le noyau

?

Si non, quelles techniques utilisez-vous habituellement si, par exemple, je ne veux pas entrer pointeur NULL?

Était-ce utile?

La solution

Les macros du noyau correspondant sont BUG_ON et WARN_ON. Le premier est pour quand vous voulez faire la panique du noyau et arrêter le système (par exemple, erreur irrécupérable). Ce dernier est pour quand vous voulez vous connecter quelque chose dans le journal du noyau (visible via dmesg).

Comme le dit @ Michael, dans le noyau, vous devez valider tout ce qui vient de l'espace utilisateur et juste manipuler , quoi que ce soit. BUG_ON et WARN_ON sont des bugs de capture dans votre propre code ou des problèmes avec le matériel.

Autres conseils

One option would be to use the macro BUG_ON(). It will printk a message, and then panic() (i.e. crash) the kernel.

http://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel

Of course, this should only be used as an error handling strategy of last resort (just like assert)...

No. Unless you're working on the kernel core and rather on a module, you should do your best to never crash (technically, abort()) the kernel. If you don't want to use a NULL pointer, just don't do it. Check it before using it, and produce an error log if it is.

The closest thing you might want to do if you're actually handling a fatal case is the panic() function or the BUG_ON and WARN_ON macros, which will abort execution and produce diagnostic messages, a stack trace and a list of modules.

Well, dereferencing null pointer will produce an oops, which you can use to find the offending code. Now, if you want to assert() a given condition, you can use

BUG_ON(condition)

A less lethal mechanism is WARN_ON, which will produce a backtrace without crashing the kernel.

BUG_ON() is the appropriate approach to do it. It checks for the condition to be true and calls the macro BUG().

How BUG() handles the rest is explained very well in the following article:

http://kernelnewbies.org/FAQ/BUG

I use this macro, it uses BUG() but adds some more info I normally use for debugging, and of course you can edit it to include more info if you wish:

#define ASSERT(x)                                                       \
do {    if (x) break;                                                   \
        printk(KERN_EMERG "### ASSERTION FAILED %s: %s: %d: %s\n",      \
               __FILE__, __func__, __LINE__, #x); dump_stack(); BUG();  \
} while (0)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top