Dispelling the Dark Magic: Inside a Ruby Debugger

Dispelling the Dark Magic: Inside a Ruby Debugger

Presentation by Daniel Azuma at RubyConf 2017. Covers how to implement a basic debugger for Ruby programs, and discusses some techniques behind the Stackdriver Debugger for Ruby. See also the live coding artifact at https://github.com/dazuma/google-snippets/blob/master/mini-debug


Daniel Azuma

November 16, 2017


  1. Dispelling the dark magic: inside a Ruby debugger Daniel Azuma

    November 16, 2017
  2. photo by Daniel Azuma

  3. https://eclipse2017.nasa.gov/eclipse-who-what-where-when-and-how

  4. https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html

  5. Live coding!? OMG!!

  6. Live coding!? OMG!!

  7. Daniel Azuma http:/ /daniel-azuma.com/

  8. None
  9. Lawyercat says: Code samples are: Copyright 2017 Google Inc. Licensed

    under the Apache License, Version 2.0 (the "License"); you may not use this code except in compliance with the License. You may obtain a copy of the License at: https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  10. Breakpoints by file/line Command shell Step in/out/over Debugger features

  11. ( Live coding session )

  12. TracePoint

  13. TracePoint.trace :call do |tp| puts "Called method #{tp.method_id}" end

  14. line class end call return c_call c_return raise b_call b_return

    thread_begin thread_end fiber_switch TracePoint event types
  15. line class end call return c_call c_return raise b_call b_return

    thread_begin thread_end fiber_switch TracePoint event types
  16. ( Live coding session )

  17. Inception (2010) © Warner Bros. https://commons.wikimedia.org/wiki/File:Solar_eclipse_1878Jul29-Corona_Pikes_peak_Langley.png

  18. Stackdriver Debugger

  19. https://commons.wikimedia.org/wiki/File:Lunar_eclipse_al-Biruni.jpg Threads

  20. TracePoint.trace :call do |tp| puts "Called method #{tp.method_id}" end

  21. void rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data); int

    rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func);
  22. Ruby interface to thread-scoped TracePoint call?

  23. https://commons.wikimedia.org/wiki/File:Astronomy;_a_view_of_London_in_1748,_with_diagrams_of_an_ecl_Wellcome_V0024734.jpg Speed

  24. file_events = [:call, :b_call, :return, :b_return, :fiber_switch] TracePoint.trace *file_events do

    |tp| if @breakpoint_files.include? tp.path @line_trace.enable else @line_trace.disable end end
  25. None
  26. Native breakpoint API?

  27. https://commons.wikimedia.org/wiki/File:Macrobius,_lunar_and_solar_eclipse.jpg Side effects

  28. Object state changes External side effects Side effects

  29. BYTE_CODE_BLACKLIST = %w( setinstancevariable setclassvariable setconstant setglobal defineclass opt_ltlt opt_aset

    opt_aset_with ).freeze
  30. C_METHOD_WHITELIST = %w( Array#+ Array#- Array#== Array#any? Array#assoc Array#at Array#bsearch

    # etc...
  31. None
  32. Immutability?

  33. Immutability? Code jails?

  34. Immutability? Code jails? Guilds??

  35. None
  36. Debuggers (the basics) are straightforward!

  37. Ruby has partial support for debuggers

  38. Try out a debugger! Byebug: interactive debugger shell https://github.com/deivid-rodriguez/byebug Stackdriver

    debugger: for live web applications https://cloud.google.com/debugger https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/ master/google-cloud-debugger
  39. It's just Ruby

  40. Thanks for viewing! Daniel Azuma dazuma@gmail.com @danielazuma