28
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham Ningbo, China

P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Embed Size (px)

Citation preview

Page 1: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

P51UST: Unix and Software Tools

Unix and Software Tools (P51UST)

Compilers, Interpreters and Debuggers

Ruibin Bai (Room AB326)

Division of Computer Science

The University of Nottingham Ningbo, China

Page 2: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Introduction

• The role of language translators

• Compilers and Interpreters

• The Java Virtual Machine

• Debugging – Using print statements to debug a program

– Basic functions performed by a debugger

Page 3: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

• 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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Compilers

• A compiler translates text written in a computer language (the source language) into another computer language (the target language).

• Is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine language).

• A compiler is likely to perform many or all of the following operations: – lexical analysis,

– preprocessing,

– parsing/semantic analysis,

– code generation, and

– code optimization.

Page 6: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Interpreters

• One statement is translated and then executed at a time

Start at the beginning of the programREPEAT

translate next program statementif no translation error occurred then execute the statement

UNTIL the end of the program or an error occurs

Page 7: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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 and so run slowly

• Java takes a new approach…

Page 10: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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

Page 12: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Debugging

Page 13: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

A Buggy awk Program

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

Page 17: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

Output of Buggy Program

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

Page 19: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

More Output

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

Page 21: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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: P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science

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.