C ISRO Debugging

25
C a series of presentations to help a bunch of brilliant space scientists understand a brilliant programming language

description

 

Transcript of C ISRO Debugging

Page 1: C ISRO Debugging

Ca series of presentations to help a bunch of

brilliant space scientists understand a brilliant programming language

Page 2: C ISRO Debugging

Debugging

Page 3: C ISRO Debugging

Debugging

It is the process of finding and removing “bugs” (logical errors) from a program

Page 4: C ISRO Debugging

bluntly put …

… debugging is an art

Page 5: C ISRO Debugging

depends on …

•Skill of programmer

•Complexity of the program

•Simplicity of the programming language

•Availability of debugging tools

Page 6: C ISRO Debugging

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

Page 7: C ISRO Debugging

Debugger

Debuggers are tools that enable the programmer to monitor the memory and/or execution of the program.

Page 8: C ISRO Debugging

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.

Page 9: C ISRO Debugging

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 &

Page 10: C ISRO Debugging

Demonstration

Data Window

Source Code

Control Panel

Page 11: C ISRO Debugging

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

Page 12: C ISRO Debugging

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

Page 13: C ISRO Debugging

Breakpoint

1. Right click on the line you wish to set the breakpoint

2. Select ‘Set Breakpoint’

Page 14: C ISRO Debugging

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

Page 15: C ISRO Debugging

Conditional Breakpoint

1. Right click on the new breakpoint

2. Select ‘Properties’

Page 16: C ISRO Debugging

Conditional Breakpoint

1. Set your required condition in the ‘Condition’ text box

2. Click ‘Apply’

Page 17: C ISRO Debugging

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

Page 18: C ISRO Debugging

Conditional Breakpoints

Page 19: C ISRO Debugging

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

Page 20: C ISRO Debugging

Conditional Breakpoints

Page 21: C ISRO Debugging

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

Page 22: C ISRO Debugging

Conditional Breakpoints

Page 23: C ISRO Debugging

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

Page 24: C ISRO Debugging

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

Page 25: C ISRO Debugging

thank you

A man who wants to lead an orchestra must turn his back on the crowd

- Max Lucado