Table of Contents

ChibiOS Debug guide

One of the most important features in an operating system is the support to developers for debugging applicative code. ChibiOS RT and NIL offer several mechanisms that can help in the debug phase of the development cycle.

Problems with debugging Embedded Code

Problems with debugging Embedded Code

Debugging embedded code is, in general, difficult because the inherent nature of the task:

Kinds of Malfunctions

There are several ways for a system to fail. First lets define some categories for the system anomalies.

System Crashed

A crash is defined as the system going into an exception vector where it is usually stopped.

RT and NIL are static, this means that it is inherently robust, if you are experiencing a crash then the cause is probably external to the system. In a static system the most common causes of a crash are:

System Stuck

This condition occurs when the system (application and/or OS) always executes the same code over and over without exiting. For example you could see the system traversing the threads ready list without exiting the loop because a list corruption.

If you verify such a condition within the system code then one of the following causes should be considered.

System Halted

Halting is a voluntary action of the OS or the application. When an anomalous condition is detected the system calls a special handler that enters the halt state. This is done by calling chSysHalt() in RT or NIL.

Halts are a good thing because:

All ChibiOS debug mechanisms trigger halts in order to signal the developer that a problem has been detected.

System Misbehaving

This happens when the system does not die but behaves in an unexpected way. This could be either a good or a bad thing. Good because it is possible to use the debugger and try to understand the issue. Bad because the system itself has not found a problem.

Debug First Actions

During development you are supposed to configure the OS with debug options enabled and compile the code with the least optimizations, -O0 for GCC users.

Debug Configuration Settings

Now lets see all the available debug options and how those are supposed to help us. All options are located in the file chconf.h usually located in the ./cfg directory of your project source tree.

CH_DBG_SYSTEM_STATE_CHECK

This is probably the most important debug option, it makes sure that all the RTOS functions are called from the proper context. If a invalid function call is detected then the system is stopped and the global variable ch.dbg.panic_msg points to an error string. The possible error codes are:

You can see that this option is extremely useful because it allows to catch very common usage errors very early during the development process. Just a note, “SV#” stands for “State Violation”. System states are very strongly checked in ChibiOS RT and NIL.

Also see the article RT State Checker.

CH_DBG_ENABLE_CHECKS

This option enables the check of API function parameters, in case of error the system is halted and a message is pointed by ch.dbg.panic_msg.

CH_DBG_ENABLE_ASSERTS

This option enable system assertions. Assertions are checks placed in critical places able to detect anomalous conditions. In case of error the system halted and a message is pointed by ch.dbg.panic_msg.

CH_DBG_TRACE_MASK

This option is an “or” of various option flags, each option selects an event to be traced:

The trace buffer stores the last N context switch operations. It can be used to determine the sequence of operations that lead to an halt condition. The trace buffer is a memory structure but the ChibiOS/RT Eclipse Debug plugin is able to show the content in a table. Note. this option is only present in RT, not in NIL.

CH_DBG_ENABLE_STACK_CHECK

This option enables port-defined stacks checking. Note that the check is usually performed at context switch time and does not necessarily catch all the overflow conditions.

CH_DBG_FILL_THREADS

Stacks are filled with a fixed pattern (0x55555555) before running threads. The patter allows to determine how much of a stack area has been really used.

CH_CFG_ST_TIMEDELTA

This option disables the tickless mode. This could be useful in case your problem is caused by use of virtual timers exceeding the system ability to serve all the timers.

Generic Suggestions

For the less expert users, there are several things you may do in order to minimize the need for debugging: