Asserts
  Home
  Introduction
  Getting KGDB
  Documentation
  Using KGDB
  Credits
  Miscellaneous
  Troubleshooting
  FAQ
  Support
  About KGDB
  KGDB Pronew

 

kgdb asserts have been deprecated from version 2.0 onwards.

Kgdb asserts

An assertion is a check for a condition. If the condition fails, kgdb is called. Asserts are used in different code paths to check for validity of conditions assumed during coding. A kernel assertion which uses kgdb is based on the macro KGDB_ASSERT. The macro is defined as follows:

#define KGDB_ASSERT(message, condition) do {   \
    if (!(condition)) {     \
        printk("kgdb assertion failed: %s\n", message); \
        asm ("int $0x3");    \
    }       \
} while (0)

Typical usage of a kgdb assert is as follows:

code...
KGDB_ASSERT("message indicating the problem", some-test);
code...

Kernel asserts

A few asserts are added to kernel when a kgdb patch is applied. Currently there are very few of them. More kernel asserts will be added in future. These asserts use a macro KERNEL_ASSERT which is defined to KGDB_ASSERT if kernel asserts are enabled, otherwise is defined to nothing. An example of an assertion is shown below.

lock_kernel();
result = dir->i_op->lookup(dir, dentry);
KERNEL_ASSERT("lookup return value invalid", KA_VALID_PTRORERR(result));
unlock_kernel();

The assertion checks whether lookup inode operation returns a valid result. The macros KA_VALID_PTRORERR checks whether the argument is a valid kernel pointer or an error value.  If the result is invalid, a message, lookup return value invalid, is printed on the console and kgdb is called. User can then analyze the condition from gdb.

Kernel asserts are useful for adding new code to the kernel. They help locating problems quickly. Above problem would typically showup only after the filesystem is unmounted.

Assertions in modules

Modules which have source code outside the kernel can also use kgdb asserts. The translation filesystem source code has a few such assertions. To ensure that the module source code compiles with a kernel having kgdb as well one without, it is recommended that another macro specific to the module should be defined for assertions. If kgdb is present, it should compile into a KGDB_ASSERT whereas in absence of kgdb, it should compile into nothing.

The translation filesystem defines an assertion macro TRFS_ASSERT in file trfs.h, as follows:

#ifdef CONFIG_X86_REMOTE_DEBUG
#include <linux/gdb.h>

#define TRFS_ASSERT(message, condition) KGDB_ASSERT(message, condition)
#else
#define TRFS_ASSERT(message, condition)
#endif

An example of how the macro is used is given below:

/*
 * Releases hold on a view. Frees the view if required.
 */
void tr_putview (struct tr_view *view)
{
 struct tr_view *pview = view->pview;
 int  freeit = 0;

 TRFS_ASSERT("invalid view hold count", atomic_read(&view->count) >= 2);
 atomic_dec(&view->count)