Developing code for a software project tends to follow a fairly standard procedure:1) You write the source code, typically in a high-level programming language such as C++ or Java, that uses language and instructions that you can understand. In a large project, the code is usually written as a number of modules for ease of understanding.2) You combine your modules into a single large piece of code using a software compiler, which translates your human-readable text into object code that the microcontroller can execute.3) You load your object code into the computer memory and start running your software.4) It doesn't work.
Don't worry, that’s entirely normal. In fact, commercial enterprise-scale software typically contains 20 – 30 errors, or bugs, per thousand lines of code even after it is released. “Fatal” errors actually cause the program to stop running; non-fatal errors allow the program to execute, but the results aren’t what you expect.
If your program doesn’t do what you want, what’s the next step? That's where another vital piece of software—the debugger—comes in.
What is Debugging?
Debugging is the process of locating and fixing bugs in computer program code. To debug a program is to start with a faulty piece of code, isolate the sources of any problems, and then fix them.
A debugger is a program that runs another program (i.e., your code), allowing you to exercise a level of control over the code as it’s running, and to examine particular items when things go wrong.
How a Debugger Works
When you are executing a program containing errors, there are several things you might want to do or know:
- What statement or expression was the program executing at the time of a fatal error?
- If a fatal error occurs while executing a function, what line of the program contains the call to that function?
- What are the values of program variables (including parameters) at a particular point during execution of the program?
- What is the result of evaluating a particular expression at some point in the program?
- What is the sequence of statements actually executed in a program?
These functions require that you be able to examine program data; to obtain a list of currently running functions; to set breakpoints where execution of the program is suspended to allow its data to be examined; and to step through the statements of a program to see what’s actually happening.
Basic Features of a Debugger
To give you more insight into the operation of the original code, almost all debuggers work at the symbolic or source-code level. In essence, they create the appearance that you are executing the C++ or Java statements in your source program rather than the machine code they have actually been translated into.
When debugging code, it’s usually necessary to examine several items simultaneously: the code that’s running, the hardware registers in the microcontroller, specific memory locations, and breakpoints. As such, you’ll want to display multiple windows in a GUI format. Figure 1 below shows a debugger screenshot, annotated to highlight some of the available features.
Figure 1: A debugger screen for the Eclipse IDE, showing multiple windows and their functions. (Source: Rice University)
Microcontroller Emulator
Sometimes there is another program—the emulator—that simulates the microcontroller itself so you don’t need any real hardware, but for embedded systems you normally run your code on the microcontroller development kit or production-intent board.
Most microcontrollers have features designed in specifically to make debugging easier, such as: hardware support for stepping through a program one instruction at a time; special test modes; and support for breakpoints.
Integrated Development Environment
In many cases, debuggers—together with other software development tools—are bundled together in a single package, called an Integrated Development Environment (IDE). Many microcontroller manufacturers supply IDEs for their products, and they’re also available from third-party vendors and even the Open Source community. The Eclipse IDE shown in Figure 1 is an open-source product.
IDEs can be either standalone products or (if from a manufacturer) bundled with development hardware for a particular microcontroller. To help you get started, Arrow offers a selection of debuggers, development tools and IDEs from numerous manufacturers.