540 likes | 704 Vues
An implementation overview By Aldo Núñez. Mago Debugger Inner Workings. Mago Debugger. What is debugging? What is a debugger? What is Mago ? Execution Agent Expression Evaluator Symbol Reader Debug Engine D and the debugger. What is debugging?. Run/Attach Control Inspect Why?
E N D
An implementation overview By Aldo Núñez Mago DebuggerInner Workings
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
What is debugging? • Run/Attach • Control • Inspect • Why? • Find out the cause of a problem (bug)
Run/Attach • Kick off a process • Attach to an already running process
Control • Breakpoints • Stepping • Changing instruction pointer • Suspend and resume threads
Inspect • Callstack • Loaded modules • Threads • Memory • Registers • Variables • Expressions
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
What is a debugger? • A process that runs, controls, and inspects another process • Special relationship between debugger and debuggee • System notifies debugger of events taking place in debuggee
A Windows Debugger Loop event ← WaitForDebugEvent( timeout ) if got event ContinueDebugEvent( event.pid,event.tid, DISCARD_EXCEPTION ) Until event.code = EXIT_PROCESS
Debug Events • Start Process • Exit Process • Start Thread • Exit Thread • Load Module • Unload Module • Exception • Message
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
What is Mago? • A debugger for D programs • A set of independent libraries • A Visual Studio plug-in
History • Interest in debuggers since 2005 • Started September 2009 • Source code released August 2010 • Integrated into Visual D September 2010
Libraries v. Visual Studio plug-in • Benefits to making separate components • Targeted testing • Mix and match for different purposes • Use with any shell program • Benefits to making VS plug-in • Well tested shell program already written • High level debug programming model
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
Execution Agent • Abstracts run, control, and inspection services • Built first to make it as solid as possible • Many APIs are locked to thread that started debuggee • Because of underlying Windows API
Services • WaitForEvent,Continue from event • Launch, Terminate • Attach, Detach • Read, Write Memory • Set, Remove Breakpoint • Step, Cancel Step • Async Break
Breakpoint Management • Software breakpoint abstraction • Hardware breakpoint abstraction • Breakpoint sharing • Resuming from breakpoint
Multithreaded Single-Step • Stepping over a single instruction • Can easily step over most instructions with native single step (SS) • Others require setting a BP after the instruction • REP string instructions
Steppers • State machines for complex stepping • In, Over, Out, Go/Resume • Instruction, Statement • Control low-level SS and BP • Receive notification of SS and BP events • Can be canceled
Stepping Scenarios • Instruction steppers handle 18 scenarios • 3x Instruction type: (simple, call, REP) • 2x At a BP • 3x Movement: (Go, Step In, Step Over) • Range stepper uses instruction steppers over an address range • Step Out stepper runs to a BP at return address
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
Expression Evaluator • Evaluates D expressions • Input is textual expression • Output is a result value record • Declarations, symbols, and input values come from outside • IValueBinder, IDeclaration • Handles formatting values • Enumerates children of values • Based on DMD front end
EE Usage MakeTypeEnv( &typeEnv ); MakeNameTable( &nameTable ); ParseText( L”a[2] + 3”, typeEnv, nameTable, &expr ); expr->Bind( options, binder ); expr->Evaluate( options, binder, &result );
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
Symbol Reader • Reads debug info for a program • Maps of source files to lines • Maps of source code lines to addresses • Functions – address and scopes • Symbols – name, type, value, storage • Types • Reads specific formats • Currently, CodeView 4.10, output by DMD
Compare to DWARF CodeView DWARF Flexible Attributes: key-value Explicit base type definition Location expressions Compression Flatten tree Abbreviations Byte code for tables • Fixed record fields • Numeric constant compression • Common type encoding • Sorted symbols • Nested Lexical blocks
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
Debug Engine • A plug-in to the VS Debugger package (vsdebug.dll) • Standalone DLL doesn’t depend on any other package • Expected to implement AD7 interface • Knows how to debug one kind of program • DEs are multiplexed during a debug session
AD7 Interface • A programming model for debugging processes • Single-threaded calls from VS Debugger to DE • Simplifies design • COM interfaces • Debug Engine is a COM co-class
Programming Model • IDebugEngine2 • IDebugThread2 • IDebugBoundBreakpoint2 • IDebugExpression2 • IDebugStackFrame2 • IDebugDisassemblyStream2 • IDebugEvent2
Mago Debugger • What is debugging? • What is a debugger? • What is Mago? • Execution Agent • Expression Evaluator • Symbol Reader • Debug Engine • D and the debugger
D and the debugger • Rewrite in D eventually • Only EE and parts of DE know about D • EE Test input generated by D program • Expression and expected value • Uses compile-time reflection