Life of breakpoint or an introduction to LLDB internal 2010/07/25 MORITA Hajime
LLDB? <ul><li>http:// lldb . llvm .org/ </li></ul><ul><ul><li>An open-source debugger, developed by  Apple . </li></ul></u...
Features and Highlights <ul><ul><li>Written in  C++  (to be LLVM family) </li></ul></ul><ul><ul><li>Scripting aware  SWIG ...
Interesting facts <ul><ul><li>Code size  200k lines  (vs. 1000k lines for gdb) </li></ul></ul><ul><ul><li>Has LLDB .framew...
How far from 1.0 <ul><li>What isn't there yet: </li></ul><ul><ul><li>Regression test suite </li></ul></ul><ul><ul><li>Oper...
How far from 1.0 <ul><li>ProcessMacOSX::EnableBreakpoint (BreakpointSite *bp_site) </li></ul><ul><li>{ </li></ul><ul><li> ...
Questions arise <ul><ul><li>What is  Clang  used for? </li></ul></ul><ul><ul><li>What part is  scriptable ? </li></ul></ul...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul...
Play with LLDB <ul><li>$ svn co http://llvm.org/svn/llvm-project/lldb/trunk </li></ul><ul><li>$ cd trunk </li></ul><ul><li...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul...
Architecture
Architecture (contd.) <ul><ul><li>Pluggable parts: </li></ul></ul><ul><ul><ul><li>Target : {Process, Thread, ...} for Mac ...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul...
To set a breakpoint, we should ... <ul><ul><li>Before process launch: </li></ul></ul><ul><ul><ul><li>Read  Symbols  from o...
LLDB representation of breakpoints
System-calls around breakpoint <ul><ul><li>Launching/stopping a process:  posix_spawnp(), kill() </li></ul></ul><ul><ul><l...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul...
Questions arise <ul><ul><li>What is  Clang  used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><...
Evaluating Expression <ul><li>It's just a yet another interpreter, except: </li></ul><ul><ul><li>Data and code stay in the...
Evaluating expression: 2 Paths &quot;void ___clang_expr(void *___clang_arg) {&quot;  + text +  &quot;}&quot; @target @host
DWARF Expression <ul><ul><li>An virtual instruction set (stack machine style) </li></ul></ul><ul><ul><li>Defined in DWARF3...
Evaluating Expr: some questions <ul><ul><li>How to lookup variables in the exp? </li></ul></ul><ul><ul><ul><li>Clang provi...
Printing structured variables <ul><ul><li>Reconstruct  Clang's  type representations from DWARF entries </li></ul></ul><ul...
Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is  scriptable ? </li></ul></ul><...
Scriptability <ul><ul><li>via SWIG </li></ul></ul><ul><ul><li>  Process, Thread, Symbol, Type, Value, Debugger  ... </li><...
Other topics <ul><ul><li>Testing </li></ul></ul><ul><ul><li>External contribution </li></ul></ul>
Testing <ul><ul><li>~20  test cases (publicly available) </li></ul></ul><ul><ul><li>Written over  Python  binding </li></u...
class TestClassTypes( lldbtest.TestBase ):      ...      def test_function_types(self):          &quot;&quot;&quot;Test 'c...
External Contribution <ul><ul><li>Linux  porting  has been started. </li></ul></ul><ul><ul><li>Huge space to contribution:...
Questions?
Upcoming SlideShare
Loading in...5
×

A Life of breakpoint

1,461

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,461
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

A Life of breakpoint

  1. 1. Life of breakpoint or an introduction to LLDB internal 2010/07/25 MORITA Hajime
  2. 2. LLDB? <ul><li>http:// lldb . llvm .org/ </li></ul><ul><ul><li>An open-source debugger, developed by Apple . </li></ul></ul><ul><ul><li>Announced at WWDC2010. </li></ul></ul><ul><ul><li>Will be used by XCode 4.0. </li></ul></ul><ul><ul><li>An LLVM subproject, as the domain name implies. </li></ul></ul>
  3. 3. Features and Highlights <ul><ul><li>Written in C++ (to be LLVM family) </li></ul></ul><ul><ul><li>Scripting aware  SWIG API. </li></ul></ul><ul><ul><li>Designed as a library ,not as a CLI program. (It has one, though.) </li></ul></ul><ul><ul><li>Using Clang in some parts. </li></ul></ul><ul><ul><ul><li>Clang is C/C++/Obj-C Frontend for LLVM </li></ul></ul></ul><ul><ul><li>Has  pluggable parts </li></ul></ul>
  4. 4. Interesting facts <ul><ul><li>Code size  200k lines (vs. 1000k lines for gdb) </li></ul></ul><ul><ul><li>Has LLDB .framework (vs. *.a for LLVM) </li></ul></ul><ul><ul><li>Currently 13 different commiters found </li></ul></ul><ul><ul><ul><li>Some are gdb-apple folks </li></ul></ul></ul><ul><ul><ul><li>Others from llvm, clang, llvm-gcc </li></ul></ul></ul><ul><ul><ul><li>2 external contributors , 1 ex-intern   </li></ul></ul></ul><ul><ul><li>Focused on iOS ( SpringBoard ) </li></ul></ul><ul><ul><ul><li>Many #idef __arm__ codepaths. </li></ul></ul></ul><ul><ul><ul><li>API classes are named as &quot;SBXxx&quot; </li></ul></ul></ul><ul><ul><li>Does not have unit-tests .  </li></ul></ul><ul><ul><ul><li>some scripting-based tests. </li></ul></ul></ul><ul><ul><li>Looks  far  from 1.0 release. </li></ul></ul><ul><ul><ul><li>Missing features, frequent crashes.... </li></ul></ul></ul>
  5. 5. How far from 1.0 <ul><li>What isn't there yet: </li></ul><ul><ul><li>Regression test suite </li></ul></ul><ul><ul><li>Operating system support hasn't been fully modularized yet </li></ul></ul><ul><ul><li>Blocks support </li></ul></ul><ul><ul><li>Calling functions in expressions </li></ul></ul><ul><ul><li>Objective-C 2.0 Support: Printing properties, synthetic properties, Objective-C expressions, KVO, dynamic types, dot syntax, runtime data </li></ul></ul><ul><ul><li>C++ support: Method access, handling demangled names, dynamic types </li></ul></ul><ul><ul><li>Exception support: Breaking by name, thrown object, thrower </li></ul></ul>http://lldb.llvm.org/status.html
  6. 6. How far from 1.0 <ul><li>ProcessMacOSX::EnableBreakpoint (BreakpointSite *bp_site) </li></ul><ul><li>{ </li></ul><ul><li>     .... </li></ul><ul><li>     if (bp_site->HardwarePreferred()) </li></ul><ul><li>     { </li></ul><ul><li>         // FIXME: This code doesn't make sense .  ...        </li></ul><ul><li>//        ThreadMacOSX *thread = (ThreadMacOSX *)m_thread_list.FindThreadByID(bp_site->GetThreadID()).get(); </li></ul><ul><li>//        if (thread) </li></ul><ul><li>//        { </li></ul><ul><li>//            bp_site->SetHardwareIndex (thread->SetHardwareBreakpoint(bp_site)); </li></ul><ul><li>//            if (bp_site->IsHardware()) </li></ul><ul><li>//            { </li></ul><ul><li>//                bp_site->SetEnabled(true); </li></ul><ul><li>//                return error; </li></ul><ul><li>//            } </li></ul><ul><li>//        } </li></ul><ul><li>     } </li></ul><ul><li>     // Just let lldb::Process::EnableSoftwareBreakpoint() handle everything... </li></ul><ul><li>     return EnableSoftwareBreakpoint (bp_site); </li></ul><ul><li>} </li></ul>
  7. 7. Questions arise <ul><ul><li>What is  Clang  used for? </li></ul></ul><ul><ul><li>What part is scriptable ? </li></ul></ul><ul><ul><li>What part is pluggable ?  </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized ? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul>
  8. 8. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul>
  9. 9. Play with LLDB <ul><li>$ svn co http://llvm.org/svn/llvm-project/lldb/trunk </li></ul><ul><li>$ cd trunk </li></ul><ul><li>  ... setup code signing ... see docs/code-signing.txt </li></ul><ul><li>$ xcodebuild -project lldb.xcodeproj -configuration Debug </li></ul><ul><li>$ ./build/Debug/lldb # invoking CLI client </li></ul>CAUTION: Will checkout and build LLVM!
  10. 10. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable ? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized ? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul>
  11. 11. Architecture
  12. 12. Architecture (contd.) <ul><ul><li>Pluggable parts: </li></ul></ul><ul><ul><ul><li>Target : {Process, Thread, ...} for Mac OS, Linux, gdb </li></ul></ul></ul><ul><ul><ul><li>Symbol : for DWARF, SYMTAB </li></ul></ul></ul><ul><ul><ul><li>ObjectFile : for ELF, Mach-O </li></ul></ul></ul><ul><ul><li>API: </li></ul></ul><ul><ul><ul><li>SWIG compatible headers </li></ul></ul></ul><ul><ul><ul><li>Pimpl-style separation from internal  </li></ul></ul></ul><ul><ul><li>Don't have CPU simulators (gdb has it.) </li></ul></ul><ul><ul><li>Modules are heavily Iter-dependent. </li></ul></ul>
  13. 13. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul>
  14. 14. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul><ul><ul><ul><li>Breakpoint </li></ul></ul></ul><ul><ul><ul><li>Eval/Print </li></ul></ul></ul>
  15. 15. To set a breakpoint, we should ... <ul><ul><li>Before process launch: </li></ul></ul><ul><ul><ul><li>Read Symbols from object files to launch </li></ul></ul></ul><ul><ul><li>.... </li></ul></ul><ul><ul><li>Suspend a target process </li></ul></ul><ul><ul><ul><li>Using special system calls </li></ul></ul></ul><ul><ul><li>Find function locations from Symbols. </li></ul></ul><ul><ul><ul><li>Symbol informations are from object files </li></ul></ul></ul><ul><ul><li>Map that locations to addresses in target process </li></ul></ul><ul><ul><li>Set breakpoints there </li></ul></ul><ul><ul><ul><li>Rewrite the code to 0xcc (sw bp) </li></ul></ul></ul><ul><ul><ul><li>Set the address to the special register (hw bp) </li></ul></ul></ul><ul><ul><li>Resume suspended </li></ul></ul>
  16. 16. LLDB representation of breakpoints
  17. 17. System-calls around breakpoint <ul><ul><li>Launching/stopping a process:  posix_spawnp(), kill() </li></ul></ul><ul><ul><li>Suspending/Resuming: task_suspend(), task_resume()  </li></ul></ul><ul><ul><li>Writing breakpoint bytes: mach_vm_write() </li></ul></ul><ul><li>See: </li></ul><ul><ul><li>tools/debugserver/source/MacOSX/MachTask.cpp </li></ul></ul><ul><ul><li>tools/debugserver/source/MacOSX/MachVMMemory.cpp </li></ul></ul><ul><ul><li>The book. </li></ul></ul>
  18. 18. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul><ul><ul><ul><li>Breakpoint </li></ul></ul></ul><ul><ul><ul><li>Eval/Print </li></ul></ul></ul>
  19. 19. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul><ul><ul><ul><li>Breakpoint </li></ul></ul></ul><ul><ul><ul><li>Eval/Print </li></ul></ul></ul>
  20. 20. Evaluating Expression <ul><li>It's just a yet another interpreter, except: </li></ul><ul><ul><li>Data and code stay in the target process . </li></ul></ul><ul><ul><li>Type definitions are in the object files . </li></ul></ul>
  21. 21. Evaluating expression: 2 Paths &quot;void ___clang_expr(void *___clang_arg) {&quot; + text + &quot;}&quot; @target @host
  22. 22. DWARF Expression <ul><ul><li>An virtual instruction set (stack machine style) </li></ul></ul><ul><ul><li>Defined in DWARF3 standard or later </li></ul></ul><ul><ul><li>LLDB implementing </li></ul></ul><ul><ul><ul><li>An interpreter for DWARF expression. </li></ul></ul></ul><ul><ul><ul><li>Clang AST to DWARF expression conversion. (not LLVM backend.) </li></ul></ul></ul><ul><ul><li>Using LLVM to invoke target functions. </li></ul></ul>In DWARF Version 2, all DWARF expressions were called &quot;location expressions&quot;, whether they computed a location (address, register) or not.  (from Dwarf3.pdf)
  23. 23. Evaluating Expr: some questions <ul><ul><li>How to lookup variables in the exp? </li></ul></ul><ul><ul><ul><li>Clang provides hooks, LLDB takes them. </li></ul></ul></ul><ul><ul><li>How to run a compiled function? </li></ul></ul><ul><ul><ul><li>Write the code to the target memory. </li></ul></ul></ul><ul><ul><ul><li>Troubles around linking.  </li></ul></ul></ul><ul><ul><li>How to get the result of expression? </li></ul></ul><ul><ul><ul><li>Modify the AST to store the last stmt. </li></ul></ul></ul><ul><ul><li>Works well? </li></ul></ul><ul><ul><ul><li>No. It crashes early and often. </li></ul></ul></ul>
  24. 24. Printing structured variables <ul><ul><li>Reconstruct Clang's type representations from DWARF entries </li></ul></ul><ul><ul><ul><li>Recursively traverses the object with it. </li></ul></ul></ul><ul><ul><li>Doesn't looks to work yet. But code is there... </li></ul></ul>
  25. 25. Questions arise <ul><ul><li>What is Clang used for? </li></ul></ul><ul><ul><li>What part is scriptable ? </li></ul></ul><ul><ul><li>What part is pluggable? </li></ul></ul><ul><li>But before these.... </li></ul><ul><ul><li>How we can play with it? </li></ul></ul><ul><ul><li>How code is organized? </li></ul></ul><ul><ul><li>How debugger works so far? </li></ul></ul>
  26. 26. Scriptability <ul><ul><li>via SWIG </li></ul></ul><ul><ul><li>  Process, Thread, Symbol, Type, Value, Debugger  ... </li></ul></ul><ul><ul><li>2 entry points: </li></ul></ul><ul><ul><ul><li>From a standalone program. </li></ul></ul></ul><ul><ul><ul><li>From the CLI interpreter. </li></ul></ul></ul><ul><ul><ul><li>Integrations is not enough yet. (cannot print WTF::Vector from CLI side.) </li></ul></ul></ul>
  27. 27. Other topics <ul><ul><li>Testing </li></ul></ul><ul><ul><li>External contribution </li></ul></ul>
  28. 28. Testing <ul><ul><li>~20 test cases (publicly available) </li></ul></ul><ul><ul><li>Written over Python binding </li></ul></ul>
  29. 29. class TestClassTypes( lldbtest.TestBase ):      ...      def test_function_types(self):          &quot;&quot;&quot;Test 'callback' has function ptr type, then ...&quot;&quot;&quot;          res = self.res          exe = os.path.join(os.getcwd(), &quot;a.out&quot;)          self. ci.HandleCommand ( &quot;file &quot; + exe , res)          self.assertTrue( res.Succeeded() )          # Break inside the main.          self. ci.HandleCommand ( &quot;breakpoint set -f main.c -l 21&quot; , res)          self.assertTrue( res.Succeeded() )          self.assertTrue( res.GetOutput() .startswith(              &quot;Breakpoint created: 1: file ='main.c', line = 21, ...&quot; ))          self. ci.HandleCommand ( &quot;run&quot; , res)          time.sleep(0.1)          self.assertTrue( res.Succeeded() )      ...          # The stop reason of the thread should be breakpoint.          self. ci.HandleCommand ( &quot;thread list&quot; , res)          print &quot;thread list ->&quot;, res.GetOutput()          self.assertTrue(res. Succeeded() )          self.assertTrue(res. GetOutput() .find( 'state is Stopped' ) > 0 and                          ...)      ...
  30. 30. External Contribution <ul><ul><li>Linux porting has been started. </li></ul></ul><ul><ul><li>Huge space to contribution: </li></ul></ul><ul><ul><ul><li>Testing!!! </li></ul></ul></ul><ul><ul><ul><li>CLI improvement (What Apple folks has little interest) </li></ul></ul></ul><ul><ul><ul><li>Reporting crashes </li></ul></ul></ul><ul><ul><li>Looks better to avoid </li></ul></ul><ul><ul><ul><li>Digging in the internal structures (will change fast) </li></ul></ul></ul><ul><ul><li>What I'd like to have as an (imaginary) contributor </li></ul></ul><ul><ul><ul><li>Non-mail-based Review process </li></ul></ul></ul><ul><ul><ul><li>Buildbots </li></ul></ul></ul><ul><ul><ul><li>Coding convention (currently scattered.) </li></ul></ul></ul>
  31. 31. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×