14 compilers, interpreters and debuggers

28
14 Compilers, Interpreters and Debuggers Mauro Jaskelioff (Originally by Gail Hopkins)

Upload: noelani-mcconnell

Post on 30-Dec-2015

46 views

Category:

Documents


7 download

DESCRIPTION

14 Compilers, Interpreters and Debuggers. Mauro Jaskelioff (Originally by Gail Hopkins). Introduction. The role of language translators Compilers and Interpreters The Java Virtual Machine Debugging (based on material by Kenneth M. Anderson and Robert Steinke) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 14 Compilers, Interpreters and Debuggers

14 Compilers, Interpreters and Debuggers

Mauro Jaskelioff(Originally by Gail Hopkins)

Page 2: 14 Compilers, Interpreters and Debuggers

Introduction

• The role of language translators• Compilers and Interpreters• The Java Virtual Machine• Debugging

– (based on material by Kenneth M. Anderson and Robert Steinke)

– Using print statements to debug a program– Basic functions performed by a debugger

Page 3: 14 Compilers, Interpreters and Debuggers

The role of language translators

• Programming languages have evolved to make programming easier for humans

• Computers still execute machine code• Language translators are required to

translate between the two• A new translator is required for each

combination of programming language and machine-code

Page 4: 14 Compilers, Interpreters and Debuggers

• Translation involves several tasks– translating high level program statements

into combinations of machine code instructions

– translating high level data structures into combinations of memory locations

– linking to existing code (libraries, modules, objects, system calls) to form a final program

• Two broad approaches to translation– compilation and interpretation

The role of language translators (2)

Page 5: 14 Compilers, Interpreters and Debuggers

Compilers

• Whole program is translated into a file containing machine code. This file can then be executed when required

• The programmer writes the source program which is translated into the object program

• Start at the beginning of the source programREPEAT

translate next source program statementUNTIL the end of the source programIF translation errors occurredTHEN report errorsELSE produce the object program

Page 6: 14 Compilers, Interpreters and Debuggers

Interpreters

• One statement is translated and then executed at a time

• Start at the beginning of the program

REPEATtranslate next program statementif no translation error occurred then execute the

statement

UNTIL the end of the program or an error occurs

Page 7: 14 Compilers, Interpreters and Debuggers

Compilers: Pros and Cons• Pros:

– Enable programmers to write human-readable code with a richer set of instructions

– Program execution is fast– Compiled programs can be run more than

once without re-compilation– Programs can be compiled for different

platforms• Cons:

– To run on a different platform, code has to be recompiled

Page 8: 14 Compilers, Interpreters and Debuggers

Interpreters: Pros and Cons

• Pros:– No need to compile before running the

program; program can just be run straight away

– Program can be run on different platforms without recompiling (provided an interpreter is available for that platform)

• Cons:– Program execution is slower than with

compiled code

Page 9: 14 Compilers, Interpreters and Debuggers

Java Virtual Machine

• Until the advent of Java, the trend for programming was moving away from interpreted languages and towards compiled languages– Software interpreters are complex

(compared to machine code) and so run slowly

• Java takes a new approach…

Page 10: 14 Compilers, Interpreters and Debuggers

Java Virtual Machine (2)

• Java is compiled into a standard machine language called Bytecode

• This standard machine language is then interpreted by a software interpreter, the Java Virtual Machine (JVM)– JVM takes Bytecode and executes it– Bytecode is not the standard machine

language for any specific hardware processor

– Any machine which has a JVM can run the compiled Java program

Page 11: 14 Compilers, Interpreters and Debuggers

Advantages of JVM

• Because Java is compiled before being interpreted it runs more quickly than standard interpreted languages

• BUT, Java is also portable because the platform it is compiled for is available for lots of different machine architectures– Java therefore does not need to be recompiled

to run on different types of machine• The JVM can be seen as an interpreter for

Java bytecodes, so it’s an intermediate solution between compilers and interpreters

Page 12: 14 Compilers, Interpreters and Debuggers

Debugging

Page 13: 14 Compilers, Interpreters and Debuggers

Debugging

• A program fails a test case (or a bug is reported).

• A programmer is given the test case and the associated source code and “debugs” the program until the fault has been corrected.

• The program, having been fixed, no longer exhibits the failure.

• The hard part is locating the problem!!

Page 14: 14 Compilers, Interpreters and Debuggers

Bridging the Gap

• One problem with faults is that they are not necessarily located “near” their associated failure.

• We need to “bring the failure close to the fault”.

• The simplest way to do this is with print statements.

Page 15: 14 Compilers, Interpreters and Debuggers

Using print Statements

• Your program has compiled but crashes when it runs. What should you do? – Sprinkle print statements throughout your

program and see how many are executed.

• Your program runs but is printing the wrong answer. What should you do? – Use print statements to print out the values of

your internal variables. See which give you the right answer and which give you the wrong answer.

Page 16: 14 Compilers, Interpreters and Debuggers

A Buggy awk Program

{x = 10while (x > 5) { x++}print $x

}

Page 17: 14 Compilers, Interpreters and Debuggers

Program with print Statements

{x = 10print "Got past initialisation\n"while (x > 5) { print "Entered while loop\n" x++ print "incremented x\n"}print "Exited while loop\n"print $x

}

Page 18: 14 Compilers, Interpreters and Debuggers

Output of Buggy Program

$ awkInfinite.shGot past initialisationEntered while Loopincremented xEntered while Loopincremented xEntered while Loopincremented xEntered while Loopincremented x

Page 19: 14 Compilers, Interpreters and Debuggers

More Print Statements

{x = 10print "Got past initialisation\n"while (x > 5) { print "Entered while Loop\n" print $x ”\n" x++ print "incremented x\n“ print $x ”\n”}print "Exited while loop\n"print $x

}

Page 20: 14 Compilers, Interpreters and Debuggers

More Output

$ awkInfinite.shGot past initialisationEntered while Loop10incremented x11Entered while Loop11incremented x12

Page 21: 14 Compilers, Interpreters and Debuggers

Debugging Tools

• A debugger allows a programmer to monitor the internal state of a program while it is executing (saves putting in and taking out print statements).

• Two types of debugger– Interpretive– Direct Execution

Page 22: 14 Compilers, Interpreters and Debuggers

Types of Debuggers

• Interpretive Debuggers– work by reading a program and

simulating its execution one line at a time.

• Direct Execution Debuggers– work by running the actual program in a

special mode where the debugger can read and write the program’s memory.

Page 23: 14 Compilers, Interpreters and Debuggers

Interpretive Vs. Direct Execution Debuggers

• Interpretive Debuggers – Easier to program– Safer, a program cannot crash the

machine (just the debugger).

• Direct Execution– Faster– More accurate, the actual program

instructions are being executed

Page 24: 14 Compilers, Interpreters and Debuggers

Two Styles of Use

• Line-at-a-time– A programmer loads the program into

the debugger and “steps” through the program one line at a time.

– The debugger stops the program after each line and gives the programmer a chance to check the program’s variables to see if it is operating correctly.

Page 25: 14 Compilers, Interpreters and Debuggers

Two Styles of Use (2)

• Breakpoints – A Programmer loads a program into the

debugger and specifies “breakpoints” at various locations in the program.

– The program runs until it hits a breakpoint.

Page 26: 14 Compilers, Interpreters and Debuggers

How are Breakpoints Useful?

• If you think you have function that might have a fault.– Set a breakpoint at the beginning of the

function.– Set another breakpoint at the end of the

function.– If a program’s data is correct at the

beginning of the function but incorrect at the end, then there is a fault in the function.

– They also allow you to skip over “init” code and debugged code quickly; letting the programmer focus on finding the fault.

Page 27: 14 Compilers, Interpreters and Debuggers

Editing Variables

• Debuggers let the programmer explore “what-if” scenarios:– You can execute a program to a certain

point, and then alter the values of the program’s variables.

– So you can explore unusual cases.– Plus, if a bug occurs, you can correct an

incorrect value and see how far the program goes before it encounters another fault.

Page 28: 14 Compilers, Interpreters and Debuggers

Summary

• Using a debugger is more flexible than print statements.

• Debuggers can be used to step through code or to run until some breakpoint.

• They also let you view the values of variables.

• They take a bit of getting used to but are usually well worth the time invested.