unit5 ss notes

Upload: sathish-raja

Post on 07-Apr-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Unit5 Ss Notes

    1/16

    1

    7.3 In t erac t ive debugg ing

    sys tems

    2

    In t roduc t ion

    An interactive debugging systemprovides programmers with facilities thataid in the testing and debugging of

    programs.

  • 8/6/2019 Unit5 Ss Notes

    2/16

    2

    3

    Approaches t o debugging

    Print statements Printing to log files Sprinkling the code with assertions Using post-mortem dumps Having programs that provide function call stacks on

    termination Profiling Heap checking

    System call tracing tools Interactive source-level debugging Reverse execution ...

    4

    Debugger bas ic pr inc ip les

    The Heisenberg principle This principle says that the debugger must intrude on the

    debuggee in a minimal way.

    Truthful debugging

    The debugger must be truthful so the programmer alwaystrusts it. Providing context information

    The debugger should provide the program contextinformation during debugging.

    Program context information. source code, stack back-track variable values, thread information, ...

  • 8/6/2019 Unit5 Ss Notes

    3/16

    3

    5

    Debugging funct ions andcapab i l i t ies

    Breakpoints

    The programmer may define a breakpointwhich causesexecution to be suspended when a specified point in theprogram is reached.

    After execution is suspended, other debugging commands canbe used to analyze the progress of the program and todiagnose errors detected.

    6

    Debugging funct ions and

    capab i l i t ies

    Tracing

    Tracing can be used to track the flow the executionlogic and data modifications.

    Step into Step over Run to cursor Run until return ...

  • 8/6/2019 Unit5 Ss Notes

    4/16

    4

    7

    Debugging funct ions and

    capab i l i t ies

    Traceback

    Trackback can show the path by which the currentstatement was reached (a list of all the functions calledin order).

    This kind of information should be displayedsymbolically, and should be related to the sourceprogram.

    call stack

    registers

    watch

  • 8/6/2019 Unit5 Ss Notes

    5/16

    5

    9

    Debugging funct ions andcapab i l i t ies

    Program-display capabilities

    It must be possible to display the program being debugged. The user should be able to control the level at which this

    display occurs.

    source level

    instruction

    level

    10

    Debugging funct ions and

    capab i l i t ies

    Capabilities of dealing with optimized code

    If the compiler has so distorted the final instruction streamthrough optimizations, the programmer cannot easily mapthis back onto the original source code

    switching to a mode where the actual source code isinterpreted may remove a major source of confusion for theprogrammer.

    Fix-and-run

    The goal of this approach is to minimize the turnaroundtime between creating a fix to test and the ability to actuallytest the system with the modification in place.

  • 8/6/2019 Unit5 Ss Notes

    6/16

    6

    11

    Debugging funct ions andcapab i l i t ies

    Capabilities of handling multilingual situations

    Many application systems involve the use of severaldifferent programming language.

    A debugging system must be sensitive to the specificlanguage being debugged so that procedural,arithmetic, and conditional logic can be coded in thesyntax of that language.

    12

    Hardw are/OS debugging support

    The debuggers ability to control theexecution of the debuggee depends both

    on hardware support built into the processorand

    on sophisticated, specialized debuggingmechanisms built into the operating system.

  • 8/6/2019 Unit5 Ss Notes

    7/16

    7

    13

    Hardw are/OS debugging support

    A way to specify a breakpoint---a specific location inthe executing code such that when the processor reachesthis location, execution will stop.

    This can be provided for by simply writing some illegalinstruction into the code stream for the debuggee.

    A notification system, also called an interrupt or a trap,that will notify the operating system (and thereby thedebugger) that an important event has occurred with

    respect to the running process. The ability to read and write directly out of and into

    the hardware registers when the interrupt occurs.

    14

    Hardw are debugging suppor t

    Breakpoint support

    Single-step support

    Fault-detection support Watchpoint support

    ... (such as Multithread control,Multiprocessor control)

  • 8/6/2019 Unit5 Ss Notes

    8/16

    8

    15

    Breakpoin t suppor t

    Breakpoints are usually implemented as a specialinstruction that causes a trap to the operation system,which then can notify a special program that hasregistered itself as a debugger.

    On architectures with varying length instructions, it isnormal for the trap---or breakpoint---instruction to be thelength of the shortest possible instruction.

    This makes it much simpler for the debugger to guaranteebreakpoints are placed on instruction boundaries.

    Breakpoint instruction of Intel x86 is a one-byte instructionINT 3.

    16

    Single-st ep support

    Single-step means that the processor isinstructed to execute a single machineinstruction when it is next processing

    instructions for the debuggee.

    Most processors provide a mode bit thatcontrols single-step operation.

  • 8/6/2019 Unit5 Ss Notes

    9/16

    9

    17

    Faul t -detec t ion suppor t

    When some faults of the debuggee aredetected by the processor, such as divide by zero and memory

    access violations,

    by OS, such as I/O failure, or by some higher level software systems, such as stack

    corruption or array bounds check violations,

    the OS notifies the debugger that its debuggeehas received a fault before the debuggeeactually is allowed control again.

    18

    Watchpoint suppo rt

    Watchpoints are notifications presentedto the debugger when specified portionsof the debuggees address space are

    modified.

    Set the portions of the address space asread-only.

    The processor traps on any modification ofdata in these addresses and stop itself before

    the write occurs.

  • 8/6/2019 Unit5 Ss Notes

    10/16

    10

    19

    OS debugging supp ort

    To control a debuggee process, a debuggerneeds a mechanism to notify the OS about theexecutable program it wishes to control. Once this is done, the debugger needs to modify that

    debuggees code in order to modify the instructionstream with the breakpoint instructions;

    And then, the debugger needs to tell the OS to run thedebuggee but notify the debugger if any sort of

    exceptions occur before the debuggee itself getnotified or terminated.

    Once the debuggee stops, the debugger needs to beable to gather information from the debuggees registerset and data memory.

    20

    Breakpo in t da ta s t ruc t u res

    At least two levels of breakpointrepresentation are needed: the logical breakpoints,

    set by users, associated with a point in the source code.

    the physical breakpoints, it relates directly to executable machine instructions, the points in the address space where actual hardware

    breakpoint instructions get written.

    it stores the original instruction. the original instruction is restored if the breakpoint is to

    be removed.

  • 8/6/2019 Unit5 Ss Notes

    11/16

    11

    logical physical

    File foo.c: line 9

    File foo.c: line 9

    File foo.c: line 9

    File foo.c: line 5

    0x458F0

    0x400F0

    Logical-to-physical: set, modify, or delete breakpoints

    Physical-to-logical: whenever a hardware breakpoint event occurs

    physical breakpoint

    structure

    Originalinstruction

    Reference count

    Physical address

    22

    Breakpo in t se t t ing and

    ac t i va t ion

    Breakpoint setting

    request symbol table agent to map given file nameand line number information into physical address.

    create logical breakpoint object containing thisinformation. create physical breakpoint object, or increment

    reference count if this object already exists

    physical breakpoint agent must put breakpointinstruction and save original instruction at that location

  • 8/6/2019 Unit5 Ss Notes

    12/16

    12

    23

    Condi t iona l breakpoin t

    A conditional breakpoint may or may notstop the debuggee when the breakpointfires depending on the value of anassociated condition.

    Conditions are Boolean expressions that areevaluated by the debugger when thebreakpoint is activated.

    24

    St ep over and s tep in t o

    Step overis a statement step in the context ofthe current function scope running anydescendent function full speed to completion.

    For statement step over, an internal breakpoint is seton the functions return address to allow the functionand all its descendents at full speed.

    Step intois a statement step that goes into anydescendent functions found during the currentoperations.

  • 8/6/2019 Unit5 Ss Notes

    13/16

    13

    float compute(int a,int b)

    {

    float c = b;

    for(int i = 0; i < a; i++)

    c = c * b;

    return c;

    }

    main()

    {

    int a,b;

    float c;

    printf(Enter a, b:\n);

    scanf(%d %d,&a,&b);

    c = compute(a,b);

    printf(result: %f\n,c);

    }

    Step over ends here

    after running

    compute

    Last step ended

    here. From here do

    a step into or a step

    over.

    Step into stops here

    when entering

    compute().

    26

    Sourc e-level s t ep into

    How to implement step into ? Main concern.

    A source statement may be translated into several machine instructions. Approaches

    Use machine step with repeatedly checking at each instruction step to see ifthe current address matches a source statements starting address. Drawback: very slow. (1000 times slower than full-speed run to a breakpoint)

    Set a temporary breakpoint at the address of the beginning of the nextsource statement.

    How to handle branching instruction?if (expression) {

    ...

    } else {

    ...

    }

    either machine single-step instruction, or decode the branch target and set a temporary breakpoint at that address

  • 8/6/2019 Unit5 Ss Notes

    14/16

    14

    27

    St ep over

    The algorithm for step over is like that of stepinto but it notes a call instruction duringinstruction decode as special, and once thecalled function has been entered, a breakpointis inserted at the return address of this functionby looking at the current stack frame.

    28

    Stac k t race

    When a fault or breakpoint occurs, theuser needs a stack track to know howdid the program get here?

    A stack traceis a list of the procedure

    activation records currently on the callstack.

    call stackSTACK

    frame 0

    frame 1

    frame 2

    call P1

    call P2

  • 8/6/2019 Unit5 Ss Notes

    15/16

    15

    29

    Stac k t race

    Once the debugger knowsthe boundaries of a activation record and

    the procedure-calling conventions,

    a stack trace can showthe procedure names, and

    formal arguments.

    The debugger can also retrieve the values ofthe local variables.

    P0 return address

    arguments from P0 to P1

    activation record 0 locals

    registers saved by P0

    P1 return address

    arguments from P1 to P2activation record 1 locals

    registers saved by P1

    activation record 2 locals

    registers saved by P2

    P0 calls P1

    P1 calls P2

  • 8/6/2019 Unit5 Ss Notes

    16/16

    31

    Rela t ionsh ip w i th o t her par ts o f t he sys tem

    The debugger must also exist in a waythat is consistent with the security andintegrity components of the system.