2 - Debugging C-C++ programs
2 - Debugging C-C++ programs
Bug detection
Deployment
Deployment
Testing
Testing
Types of debugging
●
Printing/logging Debugging
●
Debugging tool
●
Interactive debugging
●
Postmortem debugging
Printing/logging
●
Watching (live or recorded) trace statements, or print
statements, that indicate the flow of execution of a
process and the data progression :
– Adding “print” statement to the code.
●
Need to compile the code each time a print statement is added.
– Using logging facilities provided by certain libraries :
●
log4cplus in C++
●
log4j in java
log4cplus
●
Provides facilities to log message in stdout,
program log file or system log file.
●
Provides 6 logging level: FATAL, ERROR,
WARN, INFO, DEBUG, TRACE.
log4cplus
●
Fatal => one program component is inoperable which is causing a fatal error.
●
Error => one program component is inoperable and is interfering with the operability of other
functionalities.
●
Warn: an unexpected event has occurred in an application that may disrupt or delay the
program.
●
Info => an event that has occurred, though it does not appear to affect operations. These
alerts usually can be ignored, assuming the rest of the system continues to operate normally.
●
Debug => captures relevant detail of events that may be useful during software debugging or
troubleshooting.
●
Trace => captures the execution of code. It is considered an info message and does not
require action.
log4cplus
●
The developer can add different levels of logging
statements to the code.
●
Using a configuration file, the developer can
configure logging behavior :
– Logging level
– Logging output (Ex : Console or file)
– Logging format
Log4cplus header files
Logger declaration
Log4cplus configuration
Set of debug level Set of appender
logging.ini
Set of logging format Set of log file location
log4cplus
●
Before using log4cplus → install
– log4cplus-dev package containing header files.
– log4cplus-dev package containing lib files.
●
While linking, use of -llog4cplus
gdb debugger
●
gdb allows to see :
– what is going on "inside" a program while it executes
– what a program was doing at the moment it crashed
●
To debug a C or C++ program, it sould be
compiled with the option “-g” to include the
debug symbols within the binary file.
Interactive debugging
●
Debugging starts, the debugged program should in a
running state.
– The debugged program can be launched under the control
of gdb
●
$ gdb <program name>
– > (gdb) run <progrmam arguments list>
– Or, gdb can be attached to a running process
●
$ gdb -p < process pid>
Interactive debugging
●
gdb commands
– break filename:line → set a break point at the
corresponding file and line number
– watch filename:line → set a watch point on a variable
– print → print a variable value.
– backtrace → print the program call stack where it is.
Interactive debugging
●
gdb commands
– step : Continue running your program until control reaches
a different source line. It steps inside any called function.
– next : Continue to the next source line in the current stack
frame. This is similar to step, but function calls are
executed without stopping
– continue : Resume program execution
Post-mortem debugging
●
In some situations, there is no means to attach
a program to a debugger before it crashes. For
example :
– No debugger on running platform
– No access on running platform.
Post-mortem debugging
●
Core dump : consists of the recorded state of
the working memory of a computer program at
a specific time, generally when the program has
crashed or otherwise terminated abnormally.
Post-mortem debugging
●
The core dump file is created after a program crash
●
The location of the core dump depend on your operation
system (see man core)
●
Need to set ulimit -c unlimited to avoid any restriction on
core dumping.
●
To analyse the core dump file
– $ gdb -c <core dump file>