13
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected] EECS 230 Lectures Series

1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected] EECS 230 Lectures

Embed Size (px)

Citation preview

Page 1: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

1

Debugging: Catching Bugs ( II )

Ying Wu Electrical Engineering & Computer

ScienceNorthwestern [email protected]

EECS 230 Lectures Series

Page 2: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

2

What shall we learn today?

Introduction– What is debugging– Why do we need debugging– What do you need

Trace your code– Debugging tools

Page 3: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

3

Why?

The complier will never catch all the bugs– The complier only check syntax errors– It will never check if your code would

fulfill your tasks. You are on you own to

– make your program correct– make your program robust– make sure what is really going on in

your code

Page 4: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

4

What is debugging?

Always make sure you know what you are expecting from the code

If your program did not output what you expected, tract it down to the exact point

You will find the bug! Go ahead and fix it.

your codeinput output

Debugging

Page 5: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

5

!!!

What is the BEST way to mastering C/C++?

Viewing examples?

Reading C/C++ syntax?

Taking exams?

NO. The BEST way is through playing with a debugger!

Page 6: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

6

Debug Tools Debuggers

– have full control of the execution of the programset break pointscheck values of variables and structuresmodify variablesdump memories…

– depend on platformsUNIX debuggers: e.g., adb, dbx, gdbWIN-PC debugger: e.g., VC++ debugger

Integrated development environment (IDE)– Put together editor, complier, linker,

debuggers, etc.– e.g., MS Visual Studio

Page 7: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

7

Observe, Guess and Trace You get bugs, but there is no need to panic! It is very normal. How to catch and fix them?

– Create testing cases (normal and abnormal)– Observe the run-time error information,

intermediate results, stranger behaviors, and printouts

– Guess the possible reasons (which needs experience)

– Trace down to the troublesome code lines which needs strategy, tool, experience and patient.

– Fix the bug, which will be a piece of cake! It is full of fun doing bug-catching.

Page 8: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

8

Setting breakpoints

Where do you want to stop and check?

How to set breakpoints– at a source-code line– at the beginning of a function– when an expression is true– when a variable changes value– when an expression changes value

Page 9: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

9

Stepping Through Your Code

What if you do not want to set so many breakpoints?

If you want a careful trace Stepping

– run to cursor– step over– step into– step out

Page 10: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

10

Check and Modify Variables

How can access those variable? VC++ debugger provides many

windows– Output – Variables– Watch– Quick watch – Call stack– Register– Memory– disassembly

Page 11: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

11

Simulating Conditions

You can test your functions by simulating some “artificial” conditions

Modify the value of a variable in debugger, instead of the source code!

Page 12: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

12

assert ( b != NULL && size > 0 );

assert ( low >= 0 && high <= size-1 );

assert ( low <= high );

#include <cassert>

#include <cstdlib>

// Binary search

int binarySearch( const int b[], int searchKey, int low, int high,

int size )

{

int middle;

while ( low < high ) { //?

middle = ( low + high ) / 2;

if ( searchKey == b[ middle ] ) // match

return middle;

else if ( searchKey < b[ middle ] )

high = middle - 1; // search low end of array

else

low = middle + 1; // search high end of array

}

return -1; // searchKey not found

}

Let’s Rock’n Roll!

Page 13: 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures

13

#include <iostream>

int binarySearch( const int b[], int searchKey, int low, int high, int size )

void main()

{

int a[10] = {1, 3, 5, 6, 7, 10, 19, 25, 38, 88};

int q = 5; // test case 1

// int q = 6; // test case 2

int res;

res = binarySearch(a, q, 0, 9, 10);

if (res == -1){

cout << “not found!\n”;

}

else{

cout << q << “is in the database at a[“ << res<< “]\n”;

}

}