5. depends on âŠ
âąSkill of programmer
âąComplexity of the program
âąSimplicity of the programming language
âąAvailability of debugging tools
6. Techniques
Print / Trace
It involves adding a âprintfâ statement at various locations to
inspect the value stored in the variables
Core dump Analysis
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.
Execution monitoring
Step by step execution of the program to analyze the execution
path and change in control or data.
8. Demonstration
Data 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.
9. Demonstration
//simple_loop.c
1. Compile the file using
#include <stdio.h> the âg flag in gcc
int main(void)
{
int i = 0; $> gcc âg simple_loop.c
printf("Entering Loopn");
for(i=0;i<10;i++)
{
printf("%d ",i);
} 2. Load âa.outâ in DDD
printf("Exiting Loopn");
return 0; $> ddd a.out &
}
-g flag stores extra information regarding the program which is required for the debugger
11. Control Panel
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
⊠rest for homework :P
12. 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
13. Breakpoint
1. Right click on the line you
wish to set the breakpoint
2. Select âSet Breakpointâ
14. 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
17. Conditional Breakpoints
//complex_loop.c
#include <stdio.h> How do I stop the
int main(void) execution when
{
int i = 0;
int j = 0; i = 0
int k = 0;
for(i = 0; i < 10; i++) j = 10
{
for(j = 0; j < 10000; j++)
{ k = 3000
for(k = 0; k < 10000; k++)
{
printf("%d ",i+j+k);
}
}
}
return 0;
}
19. Conditional Breakpoints
//file_read.c
input.dat
#include <stdio.h>
#include <string.h> id animal
int main(void) 1 CAT
{ 2 SHEEP
FILE *input_fp = NULL;
char buff[255]; 3 WOLF
int id = 0; 4 DOG
char animal[16];
input_fp = fopen("input.dat","r"); 5 MONKEY
6 EAGLE
while(fgets(buff,sizeof(buff)-1,input_fp) != NULL)
{ 7 MAN
sscanf(buff,"%d %s %*[^n]",&id,animal);
printf("Animal at pos %d is %sn",id,animal);
}
fclose(input_fp);
return 0;
}
How do I stop the
id = 4
execution when
21. Conditional Breakpoints
//file_read.c
input.dat
#include <stdio.h>
#include <string.h> id animal
int main(void) 1 CAT
{ 2 SHEEP
FILE *input_fp = NULL;
char buff[255]; 3 WOLF
int id = 0; 4 DOG
char animal[16];
input_fp = fopen("input.dat","r"); 5 MONKEY
6 EAGLE
while(fgets(buff,sizeof(buff)-1,input_fp) != NULL)
{ 7 MAN
sscanf(buff,"%d %s %*[^n]",&id,animal);
printf("Animal at pos %d is %sn",id,animal);
}
fclose(input_fp);
return 0;
}
How do I stop the
animal = MAN
execution when
23. 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
24. what next?
File I/O Data types
Standard & structures
C Library
Pointers & Memory
Allocation Debugging
Macro and Version
Pre Processor Management
Multi file
projects
25. A man who wants to lead an orchestra
must turn his back on the crowd
- Max Lucado
thank you