Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TracePoint Tips Talk

TracePoint Tips Talk

Ruby の TracePoint についてサンプルコードを混ぜながら紹介します。

Shuichi Tamayose

March 10, 2018
Tweet

More Decks by Shuichi Tamayose

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ ໊લ : ۄد मҰ ॴଐ : YassLab גࣜձࣾ Twitter

    : @_simanman GitHub : siman-man Rubyྺ : 7೥͙Β͍
  2. Ruby ʹଘࡏ͢Δ༷ʑͳϑοΫϝιου Πϕϯτ ݺͼग़͞ΕΔλΠϛϯά method_missing ଘࡏ͠ͳ͍ϝιου͕ݺͼग़͞Εͨ࣌ const_missing ଘࡏ͠ͳ͍ఆ਺Λࢀরͨ࣌͠ respond_to_missing? respond_to?

    ͷ݁Ռ͕ false ͷ࣌ method_added ϝιου͕௥Ճ͞Εͨ࣌ method_removed ϝιου͕ remove_method ʹΑͬͯ࡟আ͞Εͨ࣌ method_undefined ϝιου͕ undef_method ͔ undef ʹΑͬͯ࡟আ͞Εͨ࣌ singleton_method_added ಛҟϝιου͕௥Ճ͞Εͨ࣌ singleton_method_removed ಛҟϝιου͕ remove_method ʹΑͬͯ࡟আ͞Εͨ࣌ singleton_method_undefined ಛҟϝιου͕ undef_method ͔ undef ʹΑͬͯ࡟আ͞Εͨ࣌ prepend_features Module#prepend ͷ࣮ମ prepended Module#prepend ͨ͠ޙ append_features Module#include ͷ࣮ମ included Module#include ͨ͠ޙ extend_object Object#extend ͷ࣮ମ extended Object#extend ͨ͠ޙ inherited ΫϥεͷαϒΫϥε͕ఆٛ͞Εͨ࣌ at_exit ϓϩάϥϜऴྃ࣌ "Qiita", "Ruby ͰϑοΫͰ͖ΔΠϕϯτҰཡ", https://qiita.com/siman/items/b6895d0826d5e83fd9cb
  3. TracePoint Ͱ͞Βʹ৭ʑͳΠϕϯτ͕ϑοΫՄೳʹͳΔ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html Πϕϯτ આ໌ :line

    ࣜͷධՁ :class ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛ΁ͷಥೖ :end ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷऴྃ :call Ruby Ͱهड़͞Εͨϝιουͷݺͼग़͠ :return Ruby Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :c_call C Ͱهड़͞Εͨϝιουͷݺͼग़͠ :c_return CͰهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :raise ྫ֎ͷൃੜ :b_call ϒϩοΫͷ։࢝ :b_return ϒϩοΫͷऴྃ :thread_begin εϨουͷ։࢝ :thread_end εϨουͷऴྃ :fiber_switch ϑΝΠόʔͷ੾Γସ͑
  4. TracePoint Ͱ͞Βʹ৭ʑͳΠϕϯτ͕ϑοΫՄೳʹͳΔ Πϕϯτ આ໌ :line ࣜͷධՁ :class ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛ΁ͷಥೖ :end ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷऴྃ

    :call Ruby Ͱهड़͞Εͨϝιουͷݺͼग़͠ :return Ruby Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :c_call C Ͱهड़͞Εͨϝιουͷݺͼग़͠ :c_return CͰهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :raise ྫ֎ͷൃੜ :b_call ϒϩοΫͷ։࢝ :b_return ϒϩοΫͷऴྃ :thread_begin εϨουͷ։࢝ :thread_end εϨουͷऴྃ :fiber_switch ϑΝΠόʔͷ੾Γସ͑ ࠓճ঺հग़དྷΔͷ͸͘͝Θ͔ͣ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html
  5. TracePoint Ͱ࢖͑Δϝιου ϝιου໊ આ໌ binding binding ΦϒδΣΫτΛฦ͢ defined_class ϝιουΛఆٛͨ͠Ϋϥε͔ϞδϡʔϧΛฦ͢ disable

    τϨʔεΛແޮʹ͢Δ enable τϨʔεΛ༗ޮʹ͢Δ enabled? τϨʔε͕༗ޮ͔Ͳ͏͔Λௐ΂Δ event ൃੜͨ͠ΠϕϯτͷछྨΛฦ͢ inspect self ͷঢ়ଶΛਓ͕ؒಡΈ΍͍͢΋ͷʹͯ͘͠ΕΔ lineno Πϕϯτ͕ൃੜͨ͠ߦ൪߸Λฦ͢ method_id Πϕϯτ͕ൃੜͨ͠ϝιου໊Λฦ͢ path Πϕϯτ͕ൃੜͨ͠ϑΝΠϧͷύεΛฦ͢ raised_exception ൃੜͨ͠ྫ֎Λฦ͢ return_value ϝιου΍ϒϩοΫͷ໭Γ஋Λฦ͢ self ΠϕϯτΛൃੜͤͨ͞ΦϒδΣΫτΛฦ͢ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html
  6. TracePoint Ͱ࢖͑Δϝιου ϝιου໊ આ໌ binding binding ΦϒδΣΫτΛฦ͢ defined_class ϝιουΛఆٛͨ͠Ϋϥε͔ϞδϡʔϧΛฦ͢ disable

    τϨʔεΛແޮʹ͢Δ enable τϨʔεΛ༗ޮʹ͢Δ enabled? τϨʔε͕༗ޮ͔Ͳ͏͔Λௐ΂Δ event ൃੜͨ͠ΠϕϯτͷछྨΛฦ͢ inspect self ͷঢ়ଶΛਓ͕ؒಡΈ΍͍͢΋ͷʹͯ͘͠ΕΔ lineno Πϕϯτ͕ൃੜͨ͠ߦ൪߸Λฦ͢ method_id Πϕϯτ͕ൃੜͨ͠ϝιου໊Λฦ͢ path Πϕϯτ͕ൃੜͨ͠ϑΝΠϧͷύεΛฦ͢ raised_exception ൃੜͨ͠ྫ֎Λฦ͢ return_value ϝιου΍ϒϩοΫͷ໭Γ஋Λฦ͢ self ΠϕϯτΛൃੜͤͨ͞ΦϒδΣΫτΛฦ͢ ͪ͜Β΋ࠓճ঺հग़དྷΔͷ͸͘͝Θ͔ͣ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html
  7. Binding Ͱ࢖͑Δϝιου ϝιου໊ આ໌ eval self ΛίϯςΩετͱͯ͠౉͞ΕͨจࣈྻΛධՁͦ͠ͷ݁ՌΛฦ͢ local_variable_defined? Ҿ਺Ͱ౉͞Ε໊ͨલͷม਺͕ఆٛ͞Ε͍ͯΔ͔Ͳ͏͔Λ൑ఆ͢Δ local_variable_get

    Ҿ਺Ͱ౉͞Ε໊ͨલͷม਺ͷ஋Λऔಘ͢Δ local_variable_set ୈҰҾ਺Ͱࢦఆ໊ͨ͠લͷม਺ʹୈೋҾ਺Ͱ౉͞Εͨ஋Λઃఆ͢Δ local_variables ϩʔΧϧม਺ͷҰཡΛऔಘ͠·͢ receiver อ࣋͢ΔίϯςΩετ಺Ͱͷ self Λฦ͠·͢ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html
  8. Binding Ͱ࢖͑Δϝιου ϝιου໊ આ໌ eval self ΛίϯςΩετͱͯ͠౉͞ΕͨจࣈྻΛධՁͦ͠ͷ݁ՌΛฦ͢ local_variable_defined? Ҿ਺Ͱ౉͞Ε໊ͨલͷม਺͕ఆٛ͞Ε͍ͯΔ͔Ͳ͏͔Λ൑ఆ͢Δ local_variable_get

    Ҿ਺Ͱ౉͞Ε໊ͨલͷม਺ͷ஋Λऔಘ͢Δ local_variable_set ୈҰҾ਺Ͱࢦఆ໊ͨ͠લͷม਺ʹୈೋҾ਺Ͱ౉͞Εͨ஋Λઃఆ͢Δ local_variables ϩʔΧϧม਺ͷҰཡΛऔಘ͠·͢ receiver อ࣋͢ΔίϯςΩετ಺Ͱͷ self Λฦ͠·͢ ޙ΄Ͳొ৔ "TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html
  9. 1ͭͷղ๏ ϝιου໊ આ໌ binding binding ΦϒδΣΫτΛฦ͢ lineno Πϕϯτ͕ൃੜͨ͠ߦ൪߸Λฦ͢ ར༻͍ͯ͠Δ TracePoint

    ͷϝιου ϝιου໊ આ໌ local_variable_set ୈҰҾ਺Ͱࢦఆ໊ͨ͠લͷม਺ʹୈೋҾ਺Ͱ౉͞Εͨ஋Λઃఆ͢Δ ར༻͍ͯ͠Δ Binding ͷϝιου Qiita, "RubyͰif xʹωετͨ͠unless xͷதʹ࣮ߦ౸ୡ͢Δํ๏", https://qiita.com/hanachin_/items/4415c178f2980aa23dd6
  10. 1ͭͷղ๏ x ͷ஋͸ true ʹͳ͍ͬͯΔͷͰ if ͷ৚݅Λύε͢Δ Qiita, "RubyͰif xʹωετͨ͠unless

    xͷதʹ࣮ߦ౸ୡ͢Δํ๏", https://qiita.com/hanachin_/items/4415c178f2980aa23dd6
  11. 1ͭͷղ๏ ࠶౓ local_variable_set Λ࢖༻ͯ͠ x Λ false ʹॻ͖ม͑ Qiita, "RubyͰif

    xʹωετͨ͠unless xͷதʹ࣮ߦ౸ୡ͢Δํ๏", https://qiita.com/hanachin_/items/4415c178f2980aa23dd6
  12. 1ͭͷղ๏ x ͷ஋͸ false ʹͳ͍ͬͯΔͷͰ unless ͷ৚݅Λύε͢Δ Qiita, "RubyͰif xʹωετͨ͠unless

    xͷதʹ࣮ߦ౸ୡ͢Δํ๏", https://qiita.com/hanachin_/items/4415c178f2980aa23dd6
  13. ࢓૊Έʹ͍ͭͯ tree = { "Foo::foo": { "Foo::bar": { }, "Foo::baz":

    { "Foo::hoge": { } } } } ࠷ऴతͳ tree ͷ஋͕͜͏ͳΔ
  14. TracePoint Λ஌͍ͬͯΔͱແࣄղܾ TracePoint Ͱ c_return ͷΠϕϯτΛϑοΫͯ͠ɺ method_id ͕ "upcase" ͷ৔߹ʹ

    return_value ͷ஋Λ downcase! Ͱഁյతʹมߋ͍ͯͨ͠ͷ͕ݪҼ͔ʙ Α͔ͬͨʂ
  15. ࢀߟαΠτ ɾhttp://www.atdot.net/~ko1/diary/201212.html#d12 [Ruby VM ΞυϕϯτΧϨϯμʔ #12 TracePoint ͷ঺հ (1)] ɾhttp://www.atdot.net/~ko1/diary/201212.html#d13

    [Ruby VM ΞυϕϯτΧϨϯμʔ #13 TracePoint ͷ঺հ (2)] ɾhttp://www.atdot.net/~ko1/diary/201212.html#d14 [Ruby VM ΞυϕϯτΧϨϯμʔ #14 TracePoint ͷ঺հ (3)] ɾ"ΫοΫύου։ൃऀϒϩά", "Ruby 2.5 ͷվળΛࣗຫ͍ͨ͠", http://techlife.cookpad.com/entry/2017/12/28/113839 ɾ"TracePointΫϥε", "ϓϩάϥϛϯάݴޠ Ruby ϦϑΝϨϯεϚχϡΞϧ", https://docs.ruby-lang.org/ja/latest/class/TracePoint.html