How Debuggers Work

34
Main sponsor Picasso Monet + Rembrandt Matejko + Canaletto How Debuggers Work Karl Rehmer Failures Come in Flavors Michael Nygard REST in Java Stefan Tilkov EJB 3.1 vs Contexts and Dependency Injection (CDI) and Dependency Injection for Java in Java EE 6 Jacek Laskowski

description

How Debuggers Work. Karl Rehmer. Failures Come in Flavors Michael Nygard. REST in Java Stefan Tilkov. EJB 3.1 vs Contexts and Dependency Injection (CDI) and Dependency Injection for Java in Java EE 6 Jacek Laskowski. How Debuggers Work. Dr. Karl Rehmer [email protected]. - PowerPoint PPT Presentation

Transcript of How Debuggers Work

Page 1: How Debuggers Work

Main sponsor

Picasso Monet + Rembrandt Matejko + Canaletto

How Debuggers Work

Karl Rehmer

Failures Come in Flavors

Michael Nygard

REST in JavaStefan Tilkov

EJB 3.1 vs Contexts and Dependency

Injection (CDI) and Dependency

Injection for Java in Java EE 6

Jacek Laskowski

Page 2: How Debuggers Work

How Debuggers How Debuggers WorkWork

Dr. Karl RehmerDr. Karl Rehmer

[email protected]@acm.org

Page 3: How Debuggers Work

What is in this talkWhat is in this talk

►Overview of debugger for embedded Overview of debugger for embedded systemsystem

►Story of debugger developmentStory of debugger development

►Architectural structure and motivationArchitectural structure and motivation

►How is a Java debugger different?How is a Java debugger different?

Page 4: How Debuggers Work

What is a DebuggerWhat is a Debugger

►A tool to remove bugs from a programA tool to remove bugs from a program

►Used in program testing/inspectionUsed in program testing/inspection

►Used to test a module or algorithmUsed to test a module or algorithm

Page 5: How Debuggers Work

Current State of the ArtCurrent State of the Art

►GUI debugger that may be part of a GUI debugger that may be part of a larger development environmentlarger development environment

►Many windows each of which supports Many windows each of which supports different aspects of debuggingdifferent aspects of debugging

►Should have a non-GUI, script-driven Should have a non-GUI, script-driven mode for batch testing mode for batch testing

Page 6: How Debuggers Work

Basic PrinciplesBasic Principles

►Debugging a program affects the Debugging a program affects the programprogram Debugger is present an runningDebugger is present an running

►On cross systems this often means a debug On cross systems this often means a debug monitor is running on the targetmonitor is running on the target

►On cross systems, the debugger may be “in On cross systems, the debugger may be “in control” of the target. It controls interrupts, I/O, control” of the target. It controls interrupts, I/O, etc.etc.

►On native systems, the program is sharing On native systems, the program is sharing resources with the debuggerresources with the debugger

Stopping a program affects timing.Stopping a program affects timing.

Page 7: How Debuggers Work

Basic PrinciplesBasic Principles

►Debuggers should be truthfulDebuggers should be truthful Sounds simple, but can be very difficult, Sounds simple, but can be very difficult,

especially when compiler optimizations especially when compiler optimizations are involved.are involved.

Examples:Examples:►Code motionCode motion►Variable sometimes in register, sometimes in Variable sometimes in register, sometimes in

memory (sometimes in cache)memory (sometimes in cache)

Page 8: How Debuggers Work

Basic PrinciplesBasic Principles

►Context is importantContext is important When stopped, show associated source When stopped, show associated source

codecode Provide ability to see call stackProvide ability to see call stack Provide tracing mechanism (source and Provide tracing mechanism (source and

machine level)machine level) When multiple threads are involved, When multiple threads are involved,

provide information on the state of the provide information on the state of the threads.threads.

Page 9: How Debuggers Work

Basic Debugger DivisionBasic Debugger Division

Host debugger

Provides user interface

Provides lookup of debug information

Communicates with target

Target Debug Monitor

In control of the target

When program being debugged stops, control is given to the debug monitor

Page 10: How Debuggers Work

Basic Requirements for Debug Basic Requirements for Debug MonitorMonitor

►Set and delete breakpointsSet and delete breakpoints Software breakpointsSoftware breakpoints Hardware breakpointsHardware breakpoints

►Single step one machine instructionSingle step one machine instruction►Read and write memoryRead and write memory►Read and write registersRead and write registers

Page 11: How Debuggers Work

Debug Monitor FunctionalityDebug Monitor Functionality

►Software breakpointSoftware breakpoint Usually write some specific instruction in Usually write some specific instruction in

memory. Attempt to execute causes a memory. Attempt to execute causes a trap. Trap handler gives control to D.M.trap. Trap handler gives control to D.M.

►Hardware breakpointHardware breakpoint Write an address in one of a number of Write an address in one of a number of

special registers. When the instruction special registers. When the instruction about to be executed is at an address in about to be executed is at an address in one of these registers, a Trap occurs.one of these registers, a Trap occurs.

Page 12: How Debuggers Work

Debug Monitor FunctionalityDebug Monitor Functionality

►When trap occurs, D.M. must save When trap occurs, D.M. must save context of program, so that its use of context of program, so that its use of resources does not corrupt program. resources does not corrupt program. Probably needs to flush any data Probably needs to flush any data cache, invalidate instruction cache.cache, invalidate instruction cache.

►Now can read/write any memory. Now can read/write any memory. Read/write registers by manipulating Read/write registers by manipulating the saved context.the saved context.

Page 13: How Debuggers Work

JTAG DebuggingJTAG Debugging

Host debugger

Provides user interface

Provides lookup of debug information

Communicates with target

Target Program in control – no debug monitor present

JTAGProbe

Page 14: How Debuggers Work

JTAG ProbeJTAG Probe

►Provides direct access to the registers Provides direct access to the registers and memory of the target through the and memory of the target through the hardware. May be “smart enough” to hardware. May be “smart enough” to know about cache.know about cache.

►Using JTAG means program being Using JTAG means program being debugged runs more normally – no debugged runs more normally – no debug monitor.debug monitor.

Page 15: How Debuggers Work

Host Debugger ComponentsHost Debugger Components

Debugger FunctionsUICC

ExpressionEvaluation

Debug Info Context

BreakpointsExecution

ControlSymbolTable

Target T.I.P.

Host-TargetProtocol

RunTime

Msg.

Utilities

DebuggerTypes

Seq.Interface

Page 16: How Debuggers Work

Setting BreakpointsSetting Breakpoints

►Find the address or addresses at which Find the address or addresses at which to set the breakpoint.to set the breakpoint.

►Breakpoint Handler keeps track of Breakpoint Handler keeps track of breakpoints (inc. conditions, breakpoints (inc. conditions, associated commands, etc.)associated commands, etc.)

►Read memory at breakpoint address Read memory at breakpoint address and save contents.and save contents.

►Write breakpoint instruction to address Write breakpoint instruction to address

Page 17: How Debuggers Work

Evaluating an ExpressionEvaluating an Expression

►Find appropriate debug information for Find appropriate debug information for current context.current context.

►For each variable, look up.For each variable, look up. Determine location, type info (for structs, this Determine location, type info (for structs, this

means finding offsets of each component)means finding offsets of each component)►Read appropriate location (reg or mem) Read appropriate location (reg or mem)

from target. Create appropriate “object”from target. Create appropriate “object”►Perform appropriate operations on objectsPerform appropriate operations on objects►Format and display resultFormat and display result

Page 18: How Debuggers Work

Modifying a VariableModifying a Variable

►As with expression, get context, find As with expression, get context, find variable, get its “shape” and location.variable, get its “shape” and location.

► If the location is not constant, create If the location is not constant, create an appropriate “memory image” for an appropriate “memory image” for the variablethe variable

►Write the memory (or register) on the Write the memory (or register) on the target.target.

Page 19: How Debuggers Work

Showing Machine CodeShowing Machine Code

►Read a “chunk” of memory containing Read a “chunk” of memory containing the address at which disassembly is to the address at which disassembly is to take place.take place.

► Invoke disassembler part of target Invoke disassembler part of target subsystem.subsystem.

►For variable length instruction For variable length instruction architectures, interesting problem to architectures, interesting problem to determine if address given is actual determine if address given is actual start of an instructionstart of an instruction

Page 20: How Debuggers Work

Showing a Call StackShowing a Call Stack

►Need to determine return address for Need to determine return address for a calla call Can use debug information that tells Can use debug information that tells

where to find the return addresswhere to find the return address Can use architecture knowledge of how Can use architecture knowledge of how

generated code is supposed to behave (if generated code is supposed to behave (if there is a standard).there is a standard).

Page 21: How Debuggers Work

Variable in a CallerVariable in a Caller

►Need to determine caller context to Need to determine caller context to find variable, location, and “shape”find variable, location, and “shape”

►Location may be modified by Location may be modified by subsequent callsubsequent call If in register, register may have been If in register, register may have been

saved somewheresaved somewhere If on stack, location may be relative to a If on stack, location may be relative to a

“frame pointer”. What has subsequent “frame pointer”. What has subsequent frame done to the frame pointer?frame done to the frame pointer?

Page 22: How Debuggers Work

Continuing from a BreakpointContinuing from a Breakpoint

► If you continue from address, you will If you continue from address, you will just trap againjust trap again

►Get saved instruction for that address.Get saved instruction for that address.►Write that instruction to memoryWrite that instruction to memory►Do a machine singlestep away from Do a machine singlestep away from

the break location.the break location.►Write the breakpoint instruction back.Write the breakpoint instruction back.

Page 23: How Debuggers Work

Machine Code SingleStepMachine Code SingleStep

►Most modern architectures have a Most modern architectures have a processor mode where continuing processor mode where continuing execution will cause a trap after one execution will cause a trap after one machine instruction is executed.machine instruction is executed.

►Machine step from a breakpoint Machine step from a breakpoint involves writing back instruction, involves writing back instruction, stepping, writing back break stepping, writing back break instruction.instruction.

Page 24: How Debuggers Work

Source Code SingleStepSource Code SingleStep

►Find all locations where the execution Find all locations where the execution can go next. (involves consulting can go next. (involves consulting debug information)debug information)

►Set a temporary breakpoint at address Set a temporary breakpoint at address for each location.for each location.

►GoGo►Delete the temporary breakpointsDelete the temporary breakpoints►Report step completedReport step completed

Page 25: How Debuggers Work

Other interesting actionsOther interesting actions

►Machine code tracingMachine code tracing►Source code tracingSource code tracing►Breakpoints with conditionsBreakpoints with conditions►Parameters for a procedure or functionParameters for a procedure or function►Watching a set of registersWatching a set of registers►Watching a number of expressionsWatching a number of expressions►Watching memoryWatching memory

Page 26: How Debuggers Work

Other interesting actionsOther interesting actions

►Data access breakpointsData access breakpoints►Debugging code in ROMDebugging code in ROM►Supporting multiple languagesSupporting multiple languages

Page 27: How Debuggers Work

Java DebuggingJava Debugging

►Much differentMuch different No addressesNo addresses No machine codeNo machine code Use Java Debug Wire Protocol (JDWP)Use Java Debug Wire Protocol (JDWP)

►Or Java Debug Interface (JDI)Or Java Debug Interface (JDI)

Page 28: How Debuggers Work

JDWPJDWP

► Is a protocol to communicate between Is a protocol to communicate between a debugger and the Java Virtual a debugger and the Java Virtual Machine (JVM)Machine (JVM)

►Optional support in a JVMOptional support in a JVM►Details format and layout of messagesDetails format and layout of messages►Does not detail transport mechanismDoes not detail transport mechanism

The JVM may support various transport The JVM may support various transport mechanisms (sockets, serial, …)mechanisms (sockets, serial, …)

Page 29: How Debuggers Work

JDWPJDWP

►Three kinds of packetThree kinds of packet HandshakeHandshake Command (from either debugger or JVM)Command (from either debugger or JVM)

►Examples: Ask for notification of an event Examples: Ask for notification of an event (debugger to JVM) or notification of an event (JVM (debugger to JVM) or notification of an event (JVM to debugger)to debugger)

Response (response to a command)Response (response to a command)►Sometimes just success or fail statusSometimes just success or fail status►Often contains dataOften contains data►Commands from JVM to debugger do not require Commands from JVM to debugger do not require

responseresponse

Page 30: How Debuggers Work

JDWPJDWP

►Headers of messages are standardHeaders of messages are standard

►Format is flexibleFormat is flexible Different JVM may use different sizes for Different JVM may use different sizes for

parts of messages.parts of messages. Debugger must ask what these sizes areDebugger must ask what these sizes are

►Object ID, Reference ID Type, Field ID, …Object ID, Reference ID Type, Field ID, …►There is a command message to ask thisThere is a command message to ask this

Page 31: How Debuggers Work

JDWP Some Kinds of JDWP Some Kinds of CommandCommand

►Reference Type CommandsReference Type Commands Fields, Methods, Values, Source File of a Fields, Methods, Values, Source File of a

typetype

►Class Type CommandsClass Type Commands Superclass, Set Values, Invoke MethodSuperclass, Set Values, Invoke Method

►Method CommandsMethod Commands Line table, Variable TableLine table, Variable Table

Page 32: How Debuggers Work

JDWP Some Kinds of JDWP Some Kinds of CommandCommand

►Object Reference CommandsObject Reference Commands Reference type, Get Values, Set Values (note Reference type, Get Values, Set Values (note

a value is often an object ID), Invoke Methoda value is often an object ID), Invoke Method

►Thread Reference CommandsThread Reference Commands Suspend, Resume, FramesSuspend, Resume, Frames

►Event Request CommandsEvent Request Commands Set, Clear, Clear All BreakpointsSet, Clear, Clear All Breakpoints

►Many kinds of event (i.e. breakpoint, single step, Many kinds of event (i.e. breakpoint, single step, class loaded)class loaded)

Page 33: How Debuggers Work

JDWP Some Kinds of JDWP Some Kinds of CommandCommand

►Stack Frame CommandsStack Frame Commands Get Values, Set Values, This ObjectGet Values, Set Values, This Object

►Event CommandsEvent Commands Report that one or more events have Report that one or more events have

happenedhappened

Page 34: How Debuggers Work

Main sponsor

Picasso Monet + Rembrandt Matejko + Canaletto

How to solve unsolvable problems in projects

Marcin Kokott, Martin Chmelar

The Future of the Java Platform: Java SE 7 and Java SE 8

Simon Ritter

Using Spring with non relational

databasesCostin Leau

Command-query Responsibility

Segregation - nowe, bardziej racjonalne

podejście do warstw. (Polish)

Sławomir Sobótka