2021 Program Design (I)
Debugger
What is a Bug ?
A software bug is an error, flaw or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
What causes bugs ?
-
Logical error
-
Semantic error
-
'=' and '=='
-
Dangling else
-
Operator precedence
-
-
Type error (casting)
-
Edge cases / Special cases
-
...etc
Debug
The process of finding and fixing bugs is termed "debugging" and often uses formal techniques or tools to pinpoint bugs.
How to debug
-
Mediumship (通靈) -
Bug ? No, it's a feature ! -
printf() is everything
-
Rewrite the code
-
Rubber Duck Debugging
-
Explain your code
-
-
Debugger !
For this course...
-
See error/warning message
-
Read the problem again
-
Print out some variables
-
Try other test data (special cases...)
-
Compare with sample output carefully
TA is not a debugger
We can help you, but you'd better try to solve it by yourself
Common Errors
-
Uninitialized variables / pointers
-
Type casting
-
Misuse of semicolon (if (x == 1); printf("x is 1");)
-
Missing braces / parentheses
-
Precedence of operators
-
Missing & operator in scanf() parameters
-
Crossing the bounds of an array
-
Conditions
-
if (1 <= x <= 100)
-
if (x = 1)
-
Debugger
See what happened when the program executed
Can pause the execution if you want
Modify the variable's value to find the problem
No need to re-compile if you only modify printf() function
CLion Debugging
for non-CSIE students
Add breakpoint && Run in debug mode
breakpoint
debug mode
Debug panel
re-run
resume
pause
stop
step over
step into
step out
check output
call stack
list of variables
Conditional breakpoint
right click
GDB Debugging
for CSIE students
GDB
The GNU Project Debugger
Note: GCC, the GNU Compiler Collection
Installation
sudo apt install gdb
Compile with command option `-g`
gcc -g -o to_debug debug.c
Run with GDB
gdb to_debug
GDB
The GNU Project Debugger
Note: GCC, the GNU Compiler Collection
GDB Commands
commands | abbr. | usage |
---|---|---|
list | l | print lines from a source file |
run | r | run the program |
break <location> | b | set a breakpoint at the given location |
info break | i b | print a table of all breakpoints |
enable / disable <breakpoint> | en/dis | enable / disable the breakpoint |
step | s | execute next step (step into) |
next | n | execute next step (step over) |
finish | fin | continue running until exiting current function |
continue | c | resume program execution |
print <variable> | p | print the variable's value |
... | ... | ... |
Conditional breakpoint
2021 Program Design (I) Debugger
By Sheng-Zhong Wang
2021 Program Design (I) Debugger
- 1,048