Você está na página 1de 16

Debugging: Importance and Techniques

By Abhijit Nathwani
PES Trainee
Jan 17
What is Debugging?

Debugging, in computer programming and


engineering, is a multistep process that
involves identifying a problem, isolating the
source of the problem, and then either
correcting the problem or determining a way to
work around it.
How debugging was termed?
Debugging Process

Localising a bug
Classifying a bug
Understanding a bug
Repairing a bug
Localising a Bug

Check the code till correct execution


Add printf statements to catch the place where program
terminates.
Check variable values at certain stages
Classifying a Bug
A bug can be classified as:
Syntactical Errors: Simple syntax mistakes, such as
leftout ;, incomplete braces, etc.
Build Errors: derived from linking object files which were
not rebuilt after a change in some source files. Can be
avoided by using makefiles.
Basic Semantic Errors:comprise using uninitialised
variables, dead code and problems with variable types
Semantic Errors: No tool can catch these problems as
they are syntactically correct but logically wrong. (For
e.g confused & and && )
Understanding a bug

It should be understood completely before fixing it.


It not, chances are that youll rather corrupt a working code
segment by modifying the code without understanding it.
The bug, rather than removing would be added causing
even more damage
Do not confuse observing symptoms with finding the real
source of the problem
Verify that its just a programming error, and not a more
fundamental problem.
Repairing a bug

The final step in the debugging process is bug fixing.


Repairing a bug is more than modifying code.
Any fixes must be documented in the code and tested
properly.
More important, learning from mistakes is an effective
attitude.
It is good practice filling a small file with detailed
explanations about the way the bug was discovered and
corrected.
A check-list can be a useful aid.
how the bug was noticed, to help in writing a test case;
how it was tracked down, to give you a better insight on
the approach to choose in similar circumstances;
what type of bug was encountered;
if this bug was encountered often, write a workaround to
save time next time.
How to debug?

Exploit the compiler features. The compiler is quite


intelligent and we often ignore the warnings.
Use all the switches wisely
Read the right documentation. It is most important while
trying to solve the problems caused be built-in functions.
The misused cout method. (experts avoid, novice
preferred)
Logging
Debug it now, not later
Examine the most recent change
Use debuggers.
GDB GNU Project Debugger
Allows you to see what is going on `inside' another program while it
executes -- or what another program was doing at the moment it
crashed.
GDB can do four main kinds of things (plus other things in support of
these) to help you catch bugs in the act:
Start your program, specifying anything that might affect its
behavior.
Make your program stop on specified conditions.
Examine what has happened, when your program has stopped.
Change things in your program, so you can experiment with
correcting the effects of one bug and go on to learn about another.
Basic GDB Commands
b main - Puts a breakpoint at the beginning of the program
b - Puts a breakpoint at the current line
b N - Puts a breakpoint at line N
b +N - Puts a breakpoint N lines down from the current line
b fn - Puts a breakpoint at the beginning of function "fn"
d N - Deletes breakpoint number N
info break - list breakpoints
r - Runs the program until a breakpoint or error
c - Continues running the program until the next breakpoint or error
f - Runs until the current function is finished
s - Runs the next line of the program
s N - Runs the next N lines of the program
n - Like s, but it does not step into functions
u N - Runs until you get N lines in front of the current line
p var - Prints the current value of the variable "var"
q - Quits gdb
Thank You