Lecture (14)Debugging

14
8/14/2019 Lecture (14)Debugging http://slidepdf.com/reader/full/lecture-14debugging 1/14

Transcript of Lecture (14)Debugging

Page 1: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 1/14

Page 2: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 2/14

FSB23103 2

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Introduction

In constructing a program, the code produced by the

programmer, usually referred to as source code willprogress through three stages:

1. CompilationEvery high level programming language comprises a set of reserved words, i.e. sequences of characters, that have a

particular interpretation, as well as rules governing theacceptable input of other characters. This is referred to asthe language’s syntax.Generally, once a program has been written, it issubmitted to a compiler. This is a program which parses

the code, i.e. examines the code character by character, toensure that it complies with the language’s syntax. If thisis not the case the compiler produces a report usuallyindicating the point at which the code started to deviateand with, sometimes, some explanation. Such instancesare called compilation or syntax errors.

Page 3: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 3/14

FSB23103 3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Introduction – cont’d

Working within an IDE, code is usually checked as it is typed

in. In such cases the code is being interpreted as opposed tocompiled – hence the term interpreter.Once the source code has compiled without error it isconverted into a more machine readable form called objector binary code. In the process it will be noted whether anylibrary methods or programmer-written classes are required.

2. LinkingHere any necessary library methods or programmer-writtenclasses are incorporated. If the code is being interpreted, if these are not available, they will be detected early onfollowing the syntax check.

3. RunningWhen the source code has been compiled and linkedsuccessfully, it is loaded to produce machine code and thenrun.

Page 4: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 4/14

Page 5: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 5/14

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Error detection

Understanding how your program works is essential in

the search for errors. The effort required is oftenreduced substantially with the aid of a debugger.

A debugger is a facility of the IDE and is a program thatlets programmers

• execute their software one step at a time;

• allows them to stop and start at different points, and• to examine the value of the variables.

Page 6: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 6/14

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Syntax Errors

 To ensure the greatest possible error checking is brought

into action, it is recommended that the options Explicit andStrict are switched on.

•  Explicit: ensures that the compiler checks that allvariables have been declared.

• Strict: requires the compiler to check that all data

conversions have been carried out explicitly.

Page 7: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 7/14

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Syntax Errors – cont’d

 The zig-zag underlining indicates a syntax error. Hoveringover the error provides some indication of its nature.

E.g.

Correcting the variable name ‘num’ and substituting ‘Text’for ‘value’ produces:

Requires: n = CInt(txtNumber.Text)

Page 8: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 8/14

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Run-time errors

As a program runs, variables usually take a number of different

values - the program is said to be in different states. Sometimesthe values of variables become inconsistent, i.e. they might notcomply with previous declarations or it is not possible to performprescribed operations.

E.g.

• A value assigned is not of the correct type

• An array subscript value is outside the declared range

• An attempt is made to divide by a variable whose value is zero

• An attempt is made to find the square root of a negative value

 These are run-time errors since they become apparent whenthe program is run in particular circumstances. A program mightappear to run satisfactorily for one set of input but ‘crash’ (break,fall-over) for another; hence the need for methodical testing.

Page 9: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 9/14

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Run-time errors – cont’d

E.g. Entering a value 9 in the textbox produces:

Clicking Break and hovering over the array subscript

produces: The size of the array is declared tobe 8. So is the declaration wrong 

or is there an error in the program

logic? 

Page 10: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 10/14

FSB23103 10

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Debugging facilities

 The VB IDE debugger allows a program to be executed one step ata time and to examine the changing values of the variables.

• Breakpoints – These allow a program to be halted at specifiedpoints. Breakpoints are introduced by clicking the margin, levelwith the line at which the program is to halt. Values of variablescan then be examined by hovering over the relevant variable.

Execution is restarted by clicking the Continue arrow.

Page 11: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 11/14FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Debugging facilities – cont’d

Breakpoints are removed by re-clicking the margin or,

alternatively, by selecting the Clear All Breakpoints inthe Debug menu

• Watch Window - Sometimes it is useful to examineseveral variables together. Once the program has halted,variables can be added to a watch window panel by

selecting the variable, right-clicking and selecting AddWatch. The variables name, value and type are thendisplayed in the panel to allow continuous monitoring.

Page 12: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 12/14FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Debugging facilities – cont’d

• Single stepping

Alternatively a programmay be executed one stepat a time by selecting StepInto from the Debug menuor using the function key:

It is usual to use

breakpoints and singlestepping together to focuson particular sections of code.

 The yellow arrow andhighlighting indicates to

next line to be executed.Changes of value in thewatch window are shown inred.

Page 13: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 13/14FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Debugging facilities – cont’d

• Single stepping – cont’d

 Two other stepping processes are available: Step over: When a method call is reached,

single-stepping is suspended for the called methodto be executed and then continued with the stepimmediately following the call.

Step Out: Finishes executing the methodand returns control to the step immediatelyfollowing the call.

Page 14: Lecture (14)Debugging

8/14/2019 Lecture (14)Debugging

http://slidepdf.com/reader/full/lecture-14debugging 14/14FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Debugging strategy

 The debugger is a useful facility but should be used in a

methodical fashion. E.g. 

1. From the symptoms observed, infer where the bug lies –possibly within 2 or 3 methods

2. Place breakpoints at the entry and exit of suspected

methods

3. Run the program. When the program stops examine thevalues of the methods’ arguments and subsequently anyreturn value. Identify the appropriate method.

4. Re-run the program stopping at the erroneous method

and then single step through it until the cause has beenidentified.