Slide 1

Slide 1 text

Effective Debugging

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Overview • Case Study • Recap and advanced commands • Debugging Libraries • Pry

Slide 4

Slide 4 text

Case Study - use byebug - here’s the situation. - you’ve been handed an existing project by your boss..

Slide 5

Slide 5 text

Sacculina Carcini - parasitic barnacle - takes over the host; host no longer molts; male crabs act like female crabs - http://www.flickr.com/photos/81858878@N00/9025250716/in/photolist-eKwLAY

Slide 6

Slide 6 text

- this project has a test suite - previous developer completed feature and hands off a “green” test suite (or so he says)

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

- turnip / rspec, explain that steps are executed in order. - execution stops rspec expectation is not met.

Slide 12

Slide 12 text

- not enough information on line 12 to tell us why it failed - let’s look at the stack trace in the rspec output

Slide 13

Slide 13 text

let’s examine the step definition on line 32 to see if it can tell us more

Slide 14

Slide 14 text

- here we are in the step definition file

Slide 15

Slide 15 text

- the internal state of the crab should have a key that points to the parasite - let’s take a step back and review the feature again..

Slide 16

Slide 16 text

- here’s the feature - and we know the failure is on line 12...

Slide 17

Slide 17 text

- but we don’t know when the crab’s payload should have its infection - it could happen on lines 3 through 11..

Slide 18

Slide 18 text

- at this point, it may be tempting to investigate the internal details of the crab class and its payload and how that works but that would be premature at this point. instead of anticipating where the problems lies, we’re going to allow our tool, the debugger, to direct our investigations. we’re not making assumptions as to the root cause of the error. let’s stay “assumption free.” - so, let’s use the ruby gem ‘debugger’..

Slide 19

Slide 19 text

- we have to do two things to use the ‘byebug’ gem

Slide 20

Slide 20 text

- update our gem file to include a reference to the ‘byebug’ gem

Slide 21

Slide 21 text

- call the debugger method which pauses our application. - but where should we put it?...

Slide 22

Slide 22 text

- there’s no relationship between host, the crab, and parasite, the sacculina carcini

Slide 23

Slide 23 text

- instead, let’s place our debugger statement at the first step where the parasite and the host interact. - note that execution is paused on the ruby expression immediately following the debugger method call. - let’s run the debugger

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

- let’s take a moment to discuss what we see in the debugger

Slide 26

Slide 26 text

- we see ten lines of context

Slide 27

Slide 27 text

- the line numbers appear in the first column

Slide 28

Slide 28 text

- here’s the current line where the application is paused

Slide 29

Slide 29 text

- and that is indicated by the presence of the hash rocket. - great. now we know where we are in the debugger session, let’s examine the code in this step...

Slide 30

Slide 30 text

- we have two lines. - ah, line 12 looks interesting. do you remember what the original failure? it was related to the crab’s payload. - the crab’s payload was nil when the test expected there to be a value - let’s add the crab’s payload as a display (or watched) variable

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

- first thing to note is that our displayed ruby expression is still nil - secondly, whoa, where are we..

Slide 35

Slide 35 text

- we’re actually in a new file. we’re inside the attach method of the parasite. - the debugger command “step” goes into a method definition. we “step” into

Slide 36

Slide 36 text

- a little different than last time. we provide a number after step. this indicates how many times to run the step command. this is useful when you want to issue the same command multiple times. this will run “step” 3 times for us.

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

- yeah, we have a value for the payload

Slide 42

Slide 42 text

- uh oh, where are we?

Slide 43

Slide 43 text

- we’re interested in what happens at the turnip step levels of lines 8, 9, 10 and 11. - how can we quickly stop execution at those lines? ...

Slide 44

Slide 44 text

- well, i can think of one way, we could add ‘debugger’ statements at lines 16, 20, 24, and 28...

Slide 45

Slide 45 text

- so instead of just one debugger statement, we would have five...

Slide 46

Slide 46 text

- however, there’s a better way. let’s make use of the capabilities provided by the debugger tool.

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

- here we are at breakpoint 4

Slide 65

Slide 65 text

- the crab payload in our display list hasn’t changed from 0x007

Slide 66

Slide 66 text

- the last time we stepped into a method, we took a detour that wasn’t necessary or informative. - let’s explore another debugger command, ‘next’...

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

- hasn’t changed from 0x007. - but wait, what’s that?!?!

Slide 71

Slide 71 text

- string vs. symbol!

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Recap and Advanced Commands - here’s the situation. - you’ve been handed an existing project by your boss..

Slide 81

Slide 81 text

Case Study Recap • debugger • display • step • break • continue • next - debugger is a method that you may place in your application that will pause its execution allowing you to examine state, modify state, set further break points.

Slide 82

Slide 82 text

Case Study Recap • debugger • disp • step • break • next • debugger • disp • step • break • next - debugger is a method that you may place in your application that will pause its execution allowing you to examine state, modify state, set further break points.

Slide 83

Slide 83 text

Case Study Recap • debugger byebug • disp • step • break • next • debugger • disp • step • break • next If you’re using the latest version of byebug, which is 2.3.1, then you can’t use the ‘debugger’ alias. You must use ‘byebug’

Slide 84

Slide 84 text

Case Study Recap •!!!! s/debugger/byebug/g !!!! • disp • step • break • next • debugger • disp • step • break • next So everywhere we saw ‘debugger’ used (only on line 10 in spec/steps.rb), you would use ‘byebug.’

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

My PR was accepted yesterday. A new version of the gem hasn’t been pushed to rubygems.org yet but it is coming soon.

Slide 87

Slide 87 text

Case Study Recap • debugger • display • step • break • next • debugger • disp • step • break • next • debugger • disp • step • break - also called watch

Slide 88

Slide 88 text

Case Study Recap • debugger • display • step • break • next • debugger • disp • step • break • next • debugger • disp • step • break

Slide 89

Slide 89 text

Case Study Recap • debugger • disp • step • break • next • debugger • disp • step • break • next • debugger • disp • step • break

Slide 90

Slide 90 text

Case Study Recap • debugger • disp[lay] • step • break • next • debugger • disp • step • break • next • debugger • disp • step • break

Slide 91

Slide 91 text

Case Study Recap • debugger • disp[lay] • step [n] • break • next • debugger • disp • step • break • next • debugger • disp • step • break

Slide 92

Slide 92 text

Case Study Recap • debugger • disp[lay] • step • break • next • debugger • disp • step • break • debugger • disp • step • break • next

Slide 93

Slide 93 text

Case Study Recap • debugger • disp[lay] • step • break <file name>: • next • debugger • disp • step • break • debugger • disp • step • break • next

Slide 94

Slide 94 text

Case Study Recap • debugger • disp[lay] • step • b <file name>: • next • debugger • disp • step • break • debugger • disp • step • break • next

Slide 95

Slide 95 text

Case Study Recap • debugger • disp[lay] • step • b Class.class_method • next • debugger • disp • step • break • debugger • disp • step • break • next

Slide 96

Slide 96 text

Case Study Recap • debugger • disp[lay] • step • b Class#instance_method • next • debugger • disp • step • break • debugger • disp • step • break • next

Slide 97

Slide 97 text

Case Study Recap • debugger • display • step • break • continue • next • debugger • disp • step • break • next • debugger • disp • step - continue execution until the application completes or we hit another breakpoint

Slide 98

Slide 98 text

Case Study Recap • debugger • disp[lay] • step • break • next • debugger • disp • step • debugger • disp • step • break • next

Slide 99

Slide 99 text

Case Study Recap • debugger • disp[lay] • step • break • next [n] • debugger • disp • step • debugger • disp • step • break • next

Slide 100

Slide 100 text

Case Study Recap • debugger • disp[lay] • step • break • n [n] • debugger • disp • step • debugger • disp • step • break • next

Slide 101

Slide 101 text

Advanced Commands • finish • save • source - what did we miss?

Slide 102

Slide 102 text

What did we miss? • finish • source • debugger • disp • step • break • next

Slide 103

Slide 103 text

- remember in our case study when we were on line eleven and we “stepped” into the attach method on @sacculini_carcini?...

Slide 104

Slide 104 text

- and at that time we stepped 3? - what if we had a loop, or many many lines of code?...

Slide 105

Slide 105 text

- so instead of step, we use “finish” which execute code until the current stack has completed

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

What did we miss? • finish • save • source • debugger • disp • step • break • next • debugger • disp • step • break • next

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

- automatically evaluate ruby expressions - show full file paths or just the filename - - display context - auto start irb when prompt is shown

Slide 116

Slide 116 text

What did we miss? • finish • save • source • debugger • disp • step • break • next

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

What else did we miss? ! • which versions of ruby?

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Debugger Libraries • 1.8 -- ruby-debug • 1.9 -- debugger, ruby-debug19, debugger2 • 2.0 -- debugger, byebug, debugger2 - lag time between new version of ruby and a fully supported debugger - debugger2 / byebug - use external C APIs

Slide 125

Slide 125 text

DEBUGGERS STINK! provocative slide!

Slide 126

Slide 126 text

Why? - why? coupling! - let’s take a look at the change log for the debugger gem...

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

Ruby’s C API - in the past, most debugging tools are tightly coupled to the C internals. - because the debuggers were hooking into internals, every time ruby changed, you needed a new debugger

Slide 130

Slide 130 text

Debugger Versions • 1.8 -- ruby-debug • 1.9 -- debugger, debugger2, ruby-debug19 • 2.0 -- debugger, debugger2, byebug • debugger • disp • step • break • next - lag time between new version of ruby and a fully supported debugger - debugger2 / byebug - use external C APIs

Slide 131

Slide 131 text

~/.rdebugrc • set autoreload • set autoeval • set autolist
 - for ruby 1.8.7 - if you’re using debugger, byebug or debugger2, these are the defaults

Slide 132

Slide 132 text

Debugger Versions • 1.8 -- ruby-debug • 1.9 -- debugger, debugger2, ruby-debug19 • 2.0 -- debugger, debugger2, byebug • debugger • disp • step • break • next • debugger • disp • step - lag time between new version of ruby and a fully supported debugger - debugger2 / byebug - use external C APIs

Slide 133

Slide 133 text

Debugger Versions • 1.8 -- ruby-debug • 1.9 -- debugger, debugger2, ruby-debug19 • 2.0 -- debugger, debugger2, byebug • debugger • disp • step • break • debugger • disp • step • break • next - lag time between new version of ruby and a fully supported debugger - debugger2 / byebug - use external C APIs

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

Pry • “powerful alternative to [...] IRB [...]” • syntax highlighting • “flexible plugin architecture” - here’s the important thing, PRY is a REPL (read eval print loop) - let’s take a look; two things to do

Slide 136

Slide 136 text

- add pry and pry-byebug to your Gemfile

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

- similar display, column numbers on the left, hash rocket indicates the current line - pretty colors

Slide 140

Slide 140 text

- binding.pry stops us before the binding.pry statement on line 10 so we don’t need “; true”

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

- notice we have to use “break”, no shortcuts - and we must use the relative path from the project directory to when specifying the file

Slide 143

Slide 143 text

- after hitting enter, we see info about the break point...

Slide 144

Slide 144 text

- in addition we see the context too for the break point.

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

- pry tells us how often the break point is hit - we have a few more things to note about pry...

Slide 147

Slide 147 text

- no shortcuts for commands unless you define them

Slide 148

Slide 148 text

Debugger (+ Pry) Versions • 1.9 -- pry + pry-debugger • 2.0 -- pry + pry-byebug

Slide 149

Slide 149 text

byebug - mashup of debase, another debugger for ruby 2.0 (C ext part) and debugger (lib and test dirs) - i didn’t investigate debase much because there’s not a lot of documentation

Slide 150

Slide 150 text

why byebug?

Slide 151

Slide 151 text

- only for ruby 2.0. otherwise works as debugger in terms of commands, etc.

Slide 152

Slide 152 text

Others? • Rubinius? • JRuby?

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

- for JRuby, feel free to use any of the Java tools for debugging

Slide 155

Slide 155 text

Overview • Case Study • Recap and advanced commands • Debugging Libraries • Pry - next, step, break, continue, display - finish, source, save - ruby-debug 1.8.7, debugger for 1.9, byebug for 2.0 - can use pry w/debugger and byebug)

Slide 156

Slide 156 text

Slides and Code and Image • https://speakerdeck.com/jwallace/effective- debugging-rubyconf • https://github.com/wallace/ sacculina_carcini.git • http://www.flickr.com/photos/ 81858878@N00/9025250716/in/photolist- eKwLAY

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

Get to know me! KNOW ME http://blog.jonathanrwallace.com/about FOLLOW ME @jonathanwallace WORK WITH ME http://www.bignerdranch.com CODE WITH ME github/wallace - thanks to Big Nerd Ranch, organizers and people who helped me with this talk