c isro debugging
DESCRIPTION
TRANSCRIPT
Ca series of presentations to help a bunch of
brilliant space scientists understand a brilliant programming language
Debugging
Debugging
It is the process of finding and removing “bugs” (logical errors) from a program
bluntly put …
… debugging is an art
depends on …
•Skill of programmer
•Complexity of the program
•Simplicity of the programming language
•Availability of debugging tools
Techniques
It involves adding a ‘printf’ statement at various locations to inspect the value stored in the variables
Analyzing the memory snapshot of the program after the crash. The operating system dumps the entire contents of the memory related to the program into a core dump file. Used to detect memory related bugs in your program.
Step by step execution of the program to analyze the execution path and change in control or data.
Print / Trace
Core dump Analysis
Execution monitoring
Debugger
Debuggers are tools that enable the programmer to monitor the memory and/or execution of the program.
DemonstrationData Display Debugger (DDD)
•DDD is a well known C debugger in the Linux platform.
•It’s a GUI based front end for GNU Debugger (gdb) program.
Demonstration//simple_loop.c
1. Compile the file using the –g flag in gcc
-g flag stores extra information regarding the program which is required for the debugger
#include <stdio.h>int main(void){
int i = 0;printf("Entering Loop\n");
for(i=0;i<10;i++){
printf("%d ",i);}printf("Exiting Loop\n");return 0;
}
$> gcc –g simple_loop.c
2. Load ‘a.out’ in DDD
$> ddd a.out &
Demonstration
Data Window
Source Code
Control Panel
Control Panel
… rest for homework :P
Start the program
Step program until it reaches a different source line
Step program, proceeding through subroutine calls
Execute until source line reaches greater than current
Breakpoint
A breakpoint informs the debugger to freeze execution at a chosen line. A programmer can then evaluate all the variables and stack of the program.
Demonstration: setting a breakpoint in DDD
Breakpoint
1. Right click on the line you wish to set the breakpoint
2. Select ‘Set Breakpoint’
Conditional Breakpoints
A breakpoint that informs the debugger to freeze execution at a chosen line when a desired condition is met .
Demonstration: setting a conditional breakpoint in DDD
Conditional Breakpoint
1. Right click on the new breakpoint
2. Select ‘Properties’
Conditional Breakpoint
1. Set your required condition in the ‘Condition’ text box
2. Click ‘Apply’
Conditional Breakpoints
#include <stdio.h>
int main(void){
int i = 0;int j = 0;int k = 0;
for(i = 0; i < 10; i++){
for(j = 0; j < 10000; j++){
for(k = 0; k < 10000; k++){
printf("%d ",i+j+k);}
}}return 0;
}
//complex_loop.c
How do I stop the execution when
i = 0
j = 10
k = 3000
Conditional Breakpoints
Conditional Breakpoints
id animal1 CAT2 SHEEP3 WOLF4 DOG5 MONKEY6 EAGLE7 MAN
#include <stdio.h>#include <string.h>
int main(void){
FILE *input_fp = NULL;char buff[255];int id = 0;char animal[16];input_fp = fopen("input.dat","r");
while(fgets(buff,sizeof(buff)-1,input_fp) != NULL){
sscanf(buff,"%d %s %*[^\n]",&id,animal);printf("Animal at pos %d is %s\n",id,animal);
}fclose(input_fp);return 0;
}
//file_read.cinput.dat
id = 4How do I stop the execution when
Conditional Breakpoints
Conditional Breakpoints
id animal1 CAT2 SHEEP3 WOLF4 DOG5 MONKEY6 EAGLE7 MAN
#include <stdio.h>#include <string.h>
int main(void){
FILE *input_fp = NULL;char buff[255];int id = 0;char animal[16];input_fp = fopen("input.dat","r");
while(fgets(buff,sizeof(buff)-1,input_fp) != NULL){
sscanf(buff,"%d %s %*[^\n]",&id,animal);printf("Animal at pos %d is %s\n",id,animal);
}fclose(input_fp);return 0;
}
//file_read.cinput.dat
animal = MANHow do I stop the execution when
Conditional Breakpoints
Final Words
Prevention is better than cure
Always strive for simplicity in your code. A good measure of simplicity lies in the number of instructions used to get the job done. Lesser the better.
Less conditional statements (if, else if , else)More repeatable statements (loops, function recursion )
Less nested statements (loop in a loop, nested ifs)Learn and be aware of standard library functions
Don’t re-invent the wheel, many open source library are available to solve various problems in the scientific domain
what next?File I/O
Pointers & Memory Allocation
Data types& structures
Debugging
Macro and Pre Processor
Version Management
Multi file projects
Standard C Library
FINSIHED
thank you
A man who wants to lead an orchestra must turn his back on the crowd
- Max Lucado