Share
Embed
Transcript
78 slides
-
᮫ԙզ • John Lin • Twitter: @johnlinvc • Senior Solution
Architect, DevOps @ West Pharmaceutical Service (ᖯҖ๏) -
ৗݟత debug • ෆ֬ఆஈఔࣜ။ෆ။ඃࣥߦ౸ɻ • ෆ֬ఆᏓᏐతᆴੋଟগɻ • ෆ֬ఆᏓᏐੋࡏ䬟ཫඃमվతɻ •
ෆ֬ఆ౸ఈݺڣ౸ྃ䬟Ұஈఔࣜɻ • ෆ֬ఆ䬟ཫݺڣṜҰஈఔࣜɻ -
ৗݟత debug ํ๏ • /፺ਘ code => ༻ਓᡵฤᩄث/ᩄثɻ • ༻
puts େ๏ɻ • ༻আࡨثɻ -
ྖંၢ݊ coupon.rb def get_coupon(user_id, history) if history[user_id] = true return
0 end history[user_id] = true 3000 end puts get_coupon(1, {}) Ṝछศᑍࢴཫੋఔࣜᛰ -
ྖંၢ݊ coupon.rb $ ruby coupon.rb 0 def get_coupon(user_id, history) if
history[user_id] = true return 0 end history[user_id] = true 3000 end puts get_coupon(1, {}) Ṝछศᑍࢴཫੋఔࣜᛰ →৭ᐽཫੋbash/byebug ༬ظෆಉɼጯ֘ཁ༌ग़ 3000 -
༻ puts ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ def get_coupon(user_id, history) if history[user_id] = true
return 0 end puts "here" history[user_id] = true 3000 end puts get_coupon(1, {}) Ռ༗ࣥߦ౸Ṝཫब။༌ग़ here -
༻ puts ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ $ ruby coupon_here.rb 0 def get_coupon(user_id, history)
if history[user_id] = true return 0 end puts "here" history[user_id] = true 3000 end puts get_coupon(1, {}) Ռ༗ࣥߦ౸Ṝཫब။༌ग़ here ᔒ༗ hereɼ ॴҎᔒ༗ࣥߦ౸ਖ਼֬తҐஔ -
byebug • ໋ྩྻత Ruby আࡨثɻ • ༻ TracePoint API ိሣ
Ruby ఔࣜআࡨɻ • ࢧԉ MRI Ruby 2.4.0 Ҏ্ɻ ෆࢧԉ jruby, rubinius ଖଞ Ruby ᩄثɻ -
҆ • ༻ bundler ိ҆ • ࡏ Gemfile ཫՃೖ
gem 'byebug' • ࣥߦ bundle install -
༻ byebug root@coscup:/app# byebug coupon.rb [1, 9] in /app/coupon.rb =>
1: def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) -
ᙘ໘ղ㘸 root@coscup:/app# byebug coupon.rb [1, 9] in /app/coupon.rb => 1:
def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) 䈕໊᪑ߦᏐൣᅴ -
ᙘ໘ղ㘸 root@coscup:/app# byebug coupon.rb [1, 9] in /app/coupon.rb => 1:
def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) ఔࣜᛰߦᥒ -
ᙘ໘ղ㘸 root@coscup:/app# byebug coupon.rb [1, 9] in /app/coupon.rb => 1:
def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) ඪࣔલ ఀࢭҐஔ -
ᙘ໘ղ㘸 root@coscup:/app# byebug coupon.rb [1, 9] in /app/coupon.rb => 1:
def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) prompt ɼඪࣔ䬟زߦੋզ၇༌ೖతࢦྩ -
։ byebug [1, 9] in /app/coupon.rb => 1: def get_coupon(user_id,
history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) quit Really quit? (y/n) y root@coscup:/app# -
help • byebug ༗㚎ݐ㘸໌ࢦྩɻ • ࡏ prompt ༌ೖ help बՄҎ౸ࢦྩྻදɻ
(byebug) help -
help (byebug) help break -- Sets breakpoints in the source
code catch -- Handles exception catchpoints condition -- Sets conditions on breakpoints continue -- Runs until program ends, hits a breakpoint or reaches a line debug -- Spawns a subdebugger delete -- Deletes breakpoints disable -- Disables breakpoints or displays display -- Evaluates expressions every time the debugger stops down -- Moves to a lower frame in the stack trace edit -- Edits source files enable -- Enables breakpoints or displays finish -- Runs the program until frame returns frame -- Moves to a frame in the call stack help -- Helps you using byebug history -- Shows byebug's history of commands info -- Shows several informations about the program being debugged interrupt -- Interrupts the program irb -- Starts an IRB session kill -- Sends a signal to the current process list -- Lists lines of source code method -- Shows methods of an object, class or module next -- Runs one or more lines of code pry -- Starts a Pry session quit -- Exits byebug restart -- Restarts the debugged program save -- Saves current byebug session to a file set -- Modifies byebug settings show -- Shows byebug settings skip -- Runs until the next breakpoint as long as it is different from the current one source -- Restores a previously saved byebug session step -- Steps into blocks or methods one or more times thread -- Commands to manipulate threads -
help • byebug ༗㚎ݐ㘸໌ࢦྩɻ • ࡏ prompt ༌ೖ help बՄҎ౸ࢦྩྻදɻ
• ࡏ prompt ༌ೖ help <ࢦྩ> बՄҎ౸ࢦྩత㘸໌ɻ (byebug) help (byebug) help help -
h[elp] (byebug) help help h[elp][ <cmd>[ <subcmd>]] Helps you using
byebug help -- prints a summary of all commands help <cmd> -- prints help on command <cmd> help <cmd> <subcmd> -- prints help on <cmd>'s subcommand <subcmd> [ ] 㚎త෦ੋબᎩੑతɼॴҎՄҎଧ h -
l[ist] • ݱࡏࣥߦ౸䬟Ұߦࢦྩɻ။༌ग़ෟۙతزߦɻ (byebug) list [1, 9] in /app/coupon.rb =>
1: def get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) -
b[reak] • ཁٻ byebug ࡏࣥߦ౸ࢦఆߦᏐલఀԼိɻఀԼိతҐஔɼ ላ໊ڣ၏ ”தᏗᴍ“ • ՄҎ༻؆ሜ b
(byebug) break 2 Created breakpoint 1 at /app/coupon.rb:2 (byebug) b 3 Created breakpoint 3 at /app/coupon.rb:3 -
༻ byebug ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ [1, 9] in /app/coupon.rb => 1: def
get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) break 5 Created breakpoint 1 at /app/coupon.rb:5 (byebug) ֬ఆ။ࣥߦୈ 5 ߦ -
༻ byebug ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ [1, 9] in /app/coupon.rb => 1: def
get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) break 5 Created breakpoint 1 at /app/coupon.rb:5 (byebug) ཁٻࡏࣥߦୈ 5 ߦ࣌ఀԼိ -
༻ byebug ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ [1, 9] in /app/coupon.rb => 1: def
get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) break 5 Created breakpoint 1 at /app/coupon.rb:5 (byebug) continue ཁٻ᠃ࣥߦ -
༻ byebug ֬ೝఔࣜ။ෆ။ඃࣥߦ౸ [1, 9] in /app/coupon.rb => 1: def
get_coupon(user_id, history) 2: if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) break 5 Created breakpoint 1 at /app/coupon.rb:5 (byebug) continue 0 root@coscup:/app# ᔒ༗ఀԼိब݁ଋྃɼॴҎୈ 5 ߦੋᔒ༗ࣥߦ౸తɻ -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) => 2:
if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) next [1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if history[user_id] = true => 3: return 0 4: end ݱࡏࣥߦ౸ୈ 2 ߦ -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) => 2:
if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) next [1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if history[user_id] = true => 3: return 0 4: end ࣥߦԼҰߦ -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) => 2:
if history[user_id] = true 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) next [1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if history[user_id] = true => 3: return 0 4: end ݱࡏࣥߦ౸ྃୈࡾߦ -
ෆੋ㑌ݸํೳதᏗ • ༗Ұࠣᔒ༗ࣥߦҙٛతํੋෆೳதᏗతɻ૾ੋۭߦɼ end, {, }ɻ (byebug) break /app/coupon.rb:4 ***
Line 4 is not a valid breakpoint in file /app/coupon.rb. Valid break points are: [B] 1: def get_coupon(user_id, history) [B] 2: if history[user_id] = true [B] 3: return 0 4: end ᔒ༗ඪ [B] తߦᏐແ๏தᏗ -
ࡏಛఆگԼ break • ՄҎ༗ࡏಛఆతگԼ break • break [file:]line [if <expr>]
(byebug) break 3 if history[user_id] Created breakpoint 5 at /app/coupon.rb:3 -
ࡏ rails server ༻ byebug • rails 4.2 ೭ޙब༬ઃࡏ։ᚙࣜ҆ byebug
• ࡏ code ཫ༻ byebug method ိࡏࣥߦఔࣜత࣌ީఀࡏ byebug ႔ɻ • ඇ rails తఔࣜ require 'byebug' ޙՄ༻ byebug methodɻ • ෆա byebug method ෆࢉੋதᏗᴍɻ -
ࡏ rails ཫ༻ byebug Started GET "/posts" for 127.0.0.1 at
2020-08-01 12:18:37 +0000 Processing by PostsController#index as */* [3, 12] in /app/rails_example/app/controllers/ posts_controller.rb 3: 4: # GET /posts 5: # GET /posts.json 6: def index 7: byebug => 8: @posts = Post.all 9: end 10: 11: # GET /posts/1 12: # GET /posts/1.json (byebug) ။ఀࡏ byebug తԼҰߦ -
ࡏඇ rails ཫ༻ byebug $ ruby coupon_byebug.rb [1, 10] in
/app/coupon_byebug.rb 1: require 'byebug' 2: def get_coupon(user_id, history) 3: byebug => 4: if history[user_id] = true 5: return 0 6: end 7: history[user_id] = true 8: 3000 9: end 10: (byebug) ઌ require ࠽ೳ༻ byebug -
ᑍ݅ࣜ byebug • code ཫత byebug बੋҰൠత ruby methodɻՄҎ์ࡏ ԿํɼՄҎ༻
if ိઃఆᑍ݅ ɻ $ ruby coupon_byebug_cond.rb [1, 10] in /app/coupon_byebug_cond.rb 1: require 'byebug' 2: def get_coupon(user_id, history) 3: byebug if user_id == 1 => 4: if history[user_id] = true 5: return 0 ༗ᑍ݅ූ߹࣌࠽தᏗ -
༻ puts ֬ೝᏓᏐతᆴ def get_coupon(user_id, history) if history[user_id] = true
puts user_id puts history return 0 end history[user_id] = true 3000 end puts get_coupon(1, {}) $ ruby coupon_puts_val.rb 1 {1=>true} 0 -
༻ byebug ֬ೝᏓᏐతᆴ [1, 9] in /app/coupon.rb 1: def get_coupon(user_id,
history) 2: if history[user_id] = true => 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) user_id 1 -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if
history[user_id] = true => 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) history 1 help 2 help help 3 list -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if
history[user_id] = true => 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) history 1 help 2 help help 3 list -
ᙛ㚎ݐࢦྩিಥ࣌ɼ ༻ eval [1, 9] in /app/coupon.rb 1: def get_coupon(user_id,
history) 2: if history[user_id] = true => 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) eval('history') {1=>true} -
v[ar] • v[ar] args ɿྻग़ method తჩᏐɻ • v[ar] c[onst]
ɿྻग़ݱࡏ scope 㚎త constantɻ • v[ar] i[nstance] [object] ɿྻग़ object ҃ੋ self త ivarɻ • v[ar] l[ocal]ɿྻग़ॴ༗ local ᏓᏐɻ • v[ar] a[ll]ɿྻग़ local, global, instance ᏓᏐɻ -
[1, 9] in /app/coupon.rb 1: def get_coupon(user_id, history) 2: if
history[user_id] = true => 3: return 0 4: end 5: history[user_id] = true 6: 3000 7: end 8: 9: puts get_coupon(1, {}) (byebug) var local history = {1=>true} user_id = 1 -
ෆ᧺ɼؐՄҎհೖ [1, 2] in /app/modify_var.rb => 1: a = 1
2: puts "a is: #{a}" (byebug) n [1, 2] in /app/modify_var.rb 1: a = 1 => 2: puts "a is: #{a}" (byebug) a = 56 56 (byebug) c a is: 56 a = 1 puts "a is: #{a}" -
༻ puts ፙᏓᏐඃमվతҐஔ def get_coupon(user_id, history) puts history if history[user_id]
= true puts history return 0 end history[user_id] = true 3000 end puts get_coupon(1, {}) $ ruby coupon_puts_find.rb {} {1=>true} 0 -
༻ byebug ፙᏓᏐඃमվతҐஔ (byebug) display history 1: history = (undefined)
(byebug) set linetrace linetrace is on (byebug) continue Tracing: /app/coupon.rb:9 puts get_coupon(1, {}) 1: history = (undefined) Tracing: /app/coupon.rb:2 if history[user_id] = true 1: history = {} Tracing: /app/coupon.rb:3 return 0 1: history = {1=>true} -
༻ byebug ፙᏓᏐඃमվతҐஔ (byebug) display history 1: history = (undefined)
(byebug) set linetrace linetrace is on (byebug) continue Tracing: /app/coupon.rb:9 puts get_coupon(1, {}) 1: history = (undefined) Tracing: /app/coupon.rb:2 if history[user_id] = true 1: history = {} Tracing: /app/coupon.rb:3 return 0 1: history = {1=>true} -
set linetrace • ࣗಈࡏ㑌Ұߦఔࣜఀ೭ޙཱࠁࣥߦɻ display expr बՄҎࡏ㑌Ұߦఔࣜࣥߦ expr ိ߇ఔࣜࣥߦաఔɻ •
set nolinetrace ՄҎ᮫ᎃɻ -
༻ byebug ፙᏓᏐඃमվతҐஔ (byebug) display history 1: history = (undefined)
(byebug) set linetrace linetrace is on (byebug) continue Tracing: /app/coupon.rb:9 puts get_coupon(1, {}) 1: history = (undefined) Tracing: /app/coupon.rb:2 if history[user_id] = true 1: history = {} Tracing: /app/coupon.rb:3 return 0 1: history = {1=>true} -
༻ byebug ፙᏓᏐඃमվతҐஔ (byebug) display history 1: history = (undefined)
(byebug) set linetrace linetrace is on (byebug) continue Tracing: /app/coupon.rb:9 puts get_coupon(1, {}) 1: history = (undefined) Tracing: /app/coupon.rb:2 if history[user_id] = true 1: history = {} Tracing: /app/coupon.rb:3 return 0 1: history = {1=>true} -
༻ byebug ፙᏓᏐඃमվతҐஔ (byebug) display history 1: history = (undefined)
(byebug) set linetrace linetrace is on (byebug) continue Tracing: /app/coupon.rb:9 puts get_coupon(1, {}) 1: history = (undefined) Tracing: /app/coupon.rb:2 if history[user_id] = true 1: history = {} Tracing: /app/coupon.rb:3 return 0 1: history = {1=>true} -
౸ఈݺڣྃ䬟Ұஈఔࣜ? require_relative 'hidden_lib.rb' puts HiddenLib.new.hello("world") $ grep 'hello' hidden_lib.rb $
ፙෆ౸ hello Ṝݸࣈ۲ʁʁ $ ruby method_caller.rb called hello with args: ["world"] -
౸ఈݺڣྃ䬟Ұஈఔࣜ? # method_caller.rb require_relative 'hidden_lib.rb' puts HiddenLib.new.hello("world") $ grep 'hello'
hidden_lib.rb $ ፙෆ౸ hello Ṝݸࣈ۲ʁʁ $ ruby method_caller.rb called hello with args: ["world"] -
class HiddenLib # hidden_lib.rb def method_missing(name, *args) "called #{name} with
args: #{args.map(&:to_s)}" end end ౸ఈݺڣྃ䬟Ұஈఔࣜ? # method_caller.rb require_relative 'hidden_lib.rb' puts HiddenLib.new.hello("world") શᔒ༗ hello Ṝݸࣈ۲ʂʂ $ ruby method_caller.rb called hello with args: ["world"] -
"ஶ໊"త method_missing • method_missing ੋᙛ object ඃݺڣෆଘࡏత method ɼब ။վҝݺڣṜݸ
methodɻඃݺڣత method ໊ሏతჩ Ꮠ။ੋ method_missing తჩᏐɻ Ruby on Rails ༻ྃ ෆগత method_missingɻ • obj.foo(a,b) => obj.method_missing(:foo, [a,b]) -
class HiddenLib # hidden_lib.rb def method_missing(name, *args) "called #{name} with
args: #{args.map(&:to_s)}" end end ༻ puts ፙඃݺڣతఔࣜ # method_caller.rb require_relative 'hidden_lib.rb' puts HiddenLib.new.hello("world") -
༻ byebug ፙඃݺڣతఔࣜ [1, 3] in /app/method_caller.rb 1: require_relative 'hidden_lib.rb'
2: => 3: puts HiddenLib.new.hello("world") (byebug) step [1, 5] in /app/hidden_lib.rb 1: class HiddenLib 2: def method_missing(name, *args) => 3: "called #{name} with args: #{args.map(&:to_s)}" 4: end 5: end (byebug) -
s[tep] [1, 3] in /app/method_caller.rb 1: require_relative 'hidden_lib.rb' 2: =>
3: puts HiddenLib.new.hello("world") (byebug) step [1, 5] in /app/hidden_lib.rb 1: class HiddenLib 2: def method_missing(name, *args) => 3: "called #{name} with args: #{args.map(&:to_s)}" 4: end 5: end (byebug) • ᄸ㑊ࣥߦਐೖඃݺڣత methodɻ -
framework vs library Framework త code զ၇త code զ၇త code
Library త code -
༻ puts ፙݺڣ႔ $ ./framework (eval):1:in `<main>' ./framework:4:in `eval' ./framework:4:in
`<main>' hello # my_code.rb def my_method puts caller_locations "hello" end -
༻ byebug ፙݺڣ႔ ./framework [1, 5] in /app/my_code.rb 1: require
'byebug' 2: def my_method 3: byebug => 4: "hello" 5: end (byebug) where -
༻ byebug ፙݺڣ႔ ./framework [1, 5] in /app/my_code.rb 1: require
'byebug' 2: def my_method 3: byebug => 4: "hello" 5: end (byebug) where -
༻ byebug ፙݺڣ႔ ./framework [1, 5] in /app/my_code.rb 1: require
'byebug' 2: def my_method 3: byebug => 4: "hello" 5: end (byebug) where -
w[here] • ᰖࣔલత call stack ɼ0 ੋલඃݺڣత methodɻᏐࣈ େతݺڣᏐࣈখతɻ ex:
1 ݺڣ 0ɻ (byebug) where --> #0 Object.my_method at /app/my_code.rb:4 #1 <main> at /app/(eval):1 ͱ-- #2 Kernel.eval at /app/framework:4 #3 <main> at /app/framework:4 -
bye ࢦྩখঞ • h[elp] [cmd] • q[uit] • l[ist] •
b[reak] [file:]line [if expr] • c[ontinue] • n[ext] • v[ar] l[ocal] • s[tep] • disp[lay] expr • set linetrace • w[here]