Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
For each universe 
there are a dozen others; 
one must lack my bug?
A Partial-Multiverse Model 
of Time Travel 
for Debugging 
RubyConf 2014 
Brock Wilcox 
awwaiid@thelackthereof.org 
bwilco...
awwaiid@rbconf:~$ whoami
Debuggers
pry-byebug
How does THAT work‽
Sweet
OK. What could be better?
MAD SCIENCE
pry-timetravel
And we're done!
The Idea 
(nothing new under the sun)
fork() the universe during 'next'
Suspend the child fork
Keep track of all created child forks
Upon 'back', grab the most recent child fork
Resume (time-travel!) the previous child fork
Wha?
Two essential ingredients.
Unix fork()
New child process: 
new (child) PID 
copy of memory 
share file handles (sockets) 
... other stuff 
runs separately
Copy-On-Write!
Signals
Registered OS callbacks
SIGINT 
SIGTERM 
SIGKILL 
SIGHUP 
SIGUSR1 
SIGALRM 
SIGTSTP
SIGINT (^C) 
SIGTERM (kill) 
SIGKILL (kill -9) 
SIGHUP (daemon reload) 
SIGUSR1 (... things ...) 
SIGALRM (just another 5 ...
Signal handling / sending in Ruby
SIGTSTP (^Z) 
SIGSTOP 
SIGCONT
def snap 
if child_pid = fork 
# I am the parent! 
Process.kill 'SIGSTOP', $$ 
else 
# I am the child! 
@snap_pid.push chi...
def restore 
Process.kill 'SIGCONT', @snap_pid[-1] 
Process.kill 'SIGSTOP', $$ 
end
Limitation: 
Shared file handles (inc. sockets!)
Limitation: 
External state.
Limitation: 
Threads. Not so much.
Limitation: 
Can only travel back to checkpoints 
(see also: Primer)
Limitation: 
Might crash the multiverse through 
memory exhaustion. I guess.
Wrap in a pry plugin 
and we're done, right?
Complication: 
Shell detects STOP
awwaiid@floyd:~$ pry 
[1] pry(main)> 
zsh: suspended (signal) pry
Fix: 
Make a dummy parent process 
that just sleeps (or sig-pause)
Complication: 
Killing parent snapshots
Fix: 
Let the One True (parent) snapshot 
kill all of its children (via SIGUSR1). 
(also: nope. totally wrong.)
Complication: 
Every snapshot wants to clean up
Fix: 
If you are not the current snapshot, 
terminate yourself without cleanup!
Complication: 
ZOMBIES
Zombie: 
Child process that has exited nicely 
but then the parent exited not-nicely. 
The process is dead, yet remains.
Great. Now we can go back!
I WANT MOAR
Tree of snapshots
TODO: auto-snapshot
References: 
https://github.com/TomOnTime/timetravelpdb 
http://programmers.stackexchange.com 
/questions/181527 
/why-is-...
If I had the chance 
I'd write a better haiku 
oh wait, I HAVE A TIME MACHINE
THE END
A Partial Multiverse Model of Time Travel for Debugging
Upcoming SlideShare
Loading in …5
×

A Partial Multiverse Model of Time Travel for Debugging

513 views

Published on

As presented at RubyConf 2014 (San Diego). Lots of live coding, I'll link the video eventually.

ABSTRACT:

Ever type 'next' into your debugger and then realize you should have used 'step'? Or perhaps invoked a method that you wish you could take back? Regret no more! Just turn the clock back a few ticks and begin again! With only a few restrictions and side-effects we will learn how to construct and use a time machine.

WARNING: Time travel may cause zombies.

Github: https://github.com/awwaiid/pry-timetravel
RubyGems: https://rubygems.org/gems/pry-timetravel

Published in: Technology
  • Be the first to comment

  • Be the first to like this

A Partial Multiverse Model of Time Travel for Debugging

  1. 1. For each universe there are a dozen others; one must lack my bug?
  2. 2. A Partial-Multiverse Model of Time Travel for Debugging RubyConf 2014 Brock Wilcox awwaiid@thelackthereof.org bwilcox@optoro.com
  3. 3. awwaiid@rbconf:~$ whoami
  4. 4. Debuggers
  5. 5. pry-byebug
  6. 6. How does THAT work‽
  7. 7. Sweet
  8. 8. OK. What could be better?
  9. 9. MAD SCIENCE
  10. 10. pry-timetravel
  11. 11. And we're done!
  12. 12. The Idea (nothing new under the sun)
  13. 13. fork() the universe during 'next'
  14. 14. Suspend the child fork
  15. 15. Keep track of all created child forks
  16. 16. Upon 'back', grab the most recent child fork
  17. 17. Resume (time-travel!) the previous child fork
  18. 18. Wha?
  19. 19. Two essential ingredients.
  20. 20. Unix fork()
  21. 21. New child process: new (child) PID copy of memory share file handles (sockets) ... other stuff runs separately
  22. 22. Copy-On-Write!
  23. 23. Signals
  24. 24. Registered OS callbacks
  25. 25. SIGINT SIGTERM SIGKILL SIGHUP SIGUSR1 SIGALRM SIGTSTP
  26. 26. SIGINT (^C) SIGTERM (kill) SIGKILL (kill -9) SIGHUP (daemon reload) SIGUSR1 (... things ...) SIGALRM (just another 5 minutes!)
  27. 27. Signal handling / sending in Ruby
  28. 28. SIGTSTP (^Z) SIGSTOP SIGCONT
  29. 29. def snap if child_pid = fork # I am the parent! Process.kill 'SIGSTOP', $$ else # I am the child! @snap_pid.push child_pid end end
  30. 30. def restore Process.kill 'SIGCONT', @snap_pid[-1] Process.kill 'SIGSTOP', $$ end
  31. 31. Limitation: Shared file handles (inc. sockets!)
  32. 32. Limitation: External state.
  33. 33. Limitation: Threads. Not so much.
  34. 34. Limitation: Can only travel back to checkpoints (see also: Primer)
  35. 35. Limitation: Might crash the multiverse through memory exhaustion. I guess.
  36. 36. Wrap in a pry plugin and we're done, right?
  37. 37. Complication: Shell detects STOP
  38. 38. awwaiid@floyd:~$ pry [1] pry(main)> zsh: suspended (signal) pry
  39. 39. Fix: Make a dummy parent process that just sleeps (or sig-pause)
  40. 40. Complication: Killing parent snapshots
  41. 41. Fix: Let the One True (parent) snapshot kill all of its children (via SIGUSR1). (also: nope. totally wrong.)
  42. 42. Complication: Every snapshot wants to clean up
  43. 43. Fix: If you are not the current snapshot, terminate yourself without cleanup!
  44. 44. Complication: ZOMBIES
  45. 45. Zombie: Child process that has exited nicely but then the parent exited not-nicely. The process is dead, yet remains.
  46. 46. Great. Now we can go back!
  47. 47. I WANT MOAR
  48. 48. Tree of snapshots
  49. 49. TODO: auto-snapshot
  50. 50. References: https://github.com/TomOnTime/timetravelpdb http://programmers.stackexchange.com /questions/181527 /why-is-reverse-debugging-rarely-used http://debug.elm-lang.org/
  51. 51. If I had the chance I'd write a better haiku oh wait, I HAVE A TIME MACHINE
  52. 52. THE END

×