デバッグ
参考:
Rails 4 でのデバッグ方法 | blog.10rane.com
Ruby と Rails の独特な挙動と、それと戦うためのデバッグ手法 – Qiita
Debugging Ruby – Techniques to Help you Fix your Code | RubyGuides
目次
- 1 警告の発生箇所を突き止める
- 2 バックトレースを取得する
- 3 cause
- 4 Byebug
- 5 Pry
- 6 Pry-Byebug
- 7 Pry Doc
- 8 Pry プラグイン
- 9 source_location
- 10 Thread.current.backtrace_locations
- 11 Pry::Byebug::Breakpoints#add_method
- 12 Bindex
- 13 binding_of_caller
- 14 debug_inspector
- 15 セグメンテーションフォルト
- 16 gdb
- 17 gdb-ruby
- 18 DTrace
- 19 gdbdump-ruby
- 20 sigdump
- 21 gdbruby
- 22 segv-handler-gdb
- 23 blab
- 24 ruby-debug
- 25 gem のインストールログを取る
警告の発生箇所を突き止める
デバッグを有効にする。
$DEBUG = true
コマンドラインでデバッグを有効にしてスクリプトを実行する。
$ ruby --debug script.rb
参考:
Getting stack trace info for a ruby warning – Stack Overflow
Can you ask ruby to treat warnings as errors? – Stack Overflow
バックトレースを取得する
参考:
バックトレース (stacktrace) を取得する方法 | DQNEO 起業日記
Ruby でのバックトレース活用法 | Rubyist Magazine (るびま)
もっと簡単にデバッグ用にメソッドの呼び出し元 (スタックトレース) をログに出力する方法 | まちゅダイアリー
How to get a stack trace object in Ruby? – Stack Overflow
cause
参考:
Ruby で RuntimeError (例外) のバックトレースを最初の raise までさかのぼって出力する – Qiita
Byebug
参考:
deivid-rodriguez/byebug: Debugging in Ruby 2 – GitHub
byebug/GUIDE.md at master · deivid-rodriguez/byebug – GitHub
Documentation for byebug | RubyDoc.info
byebug で Ruby スクリプトをコマンドラインデバッグする – Qiita
print デバッグにさようなら!Ruby 初心者のための Byebug チュートリアル – Qiita
byebug コードリーディング | freedom-man
byebug や pry-byebug を使った後の挙動を10倍高速にしました | k0kubun’s blog
Debugging using ByeBug Gem in Rails 5 | Ruby Plus
Byebug Cheatsheet | Fleeblewidget
Pry
Pry を使って Rails のコンソールをパワーアップ & デバッグをする | Rails Webook
今更ながら Pry について色々と調べてみた 前編 – Qiita
今更ながら Pry について色々と調べてみた 後編 – Qiita
Rubyist よ、irb を捨てて Pry を使おう | TIM Labs
Ruby / Rails デバッガことはじめ – Qiita
pry を使ってデバックする | liberta-web blog
binding.pry でループ内にブレイクポイント仕掛けるとだるいことになる | もふもふ技術部
Create Break Points in Rails with Pry | Coderwall
Pry – an IRB alternative and runtime developer console [Official]
pry/pry: An IRB alternative and runtime developer console – GitHub
deivid-rodriguez/pry-byebug: Step-by-step debugging and stack navigation in Pry – GitHub
Documentation for pry | RubyDoc.info
Pry-Byebug
指定したコードの位置で Pry を起動する。
binding.pry
ステップ実行する。
> step
次の行まで実行する。
> next
現在のフレームを抜ける。
> finish
Pry セッションを終了して通常の実行を再開する。
> continue
参考:
deivid-rodriguez/pry-byebug: Step-by-step debugging and stack navigation in Pry – GitHub
Documentation for pry-byebug | RubyDoc.info
pry-byebug を使って Rails アプリをステップ実行する | Hack Your Design!
pry-byebug で ruby をデバッグする – Qiita
Rails デバッグの為に pry を使いこなす | DoRuby
Ruby デバッガー binding.pry | のんびりしているエンジニアの日記
Ruby (on Rails) 開発でブレークポイント作りたいなら「pry-byebug」がオススメ! | SUKEMATSU.NET
docker-compose 上の Rails のデバッグを行う | My External Storage
Using `break` in `pry` · deivid-rodriguez/pry-byebug Wiki – GitHub
Add binding.pry breakpoint dynamically with pry-byebug | kinopyo blog
Set conditional breakpoint with pry-byebug – Stack Overflow
Breakpoints with pry-byebug don’t trigger in the console – Stack Overflow
How to correctly set up Pry in Rails 4.2 – Stack Overflow
How to move to the next line in binding.pry ? – Stack Overflow
Pry Doc
Gemfile に pry-doc を追加する。
gem "pry-doc", group: [:development, :test]
ソースを表示する。
> show-source open
show-source
のエイリアス $
を使う。
> $ open
オブジェクトのメソッド一覧を表示する。
> "hello".methods
モジュールのメソッド一覧を表示する。
> File.methods
メソッドが定義されているモジュールを調べる。
> method(:open).owner
オブジェクトのメソッドが定義されているクラスを調べる。
> "hello".method(:to_s).owner
モジュール関数のソースを表示する。
> $ OpenURI.open_uri
オブジェクト内に移動 (cd
) 、メソッド一覧を表示 (ls
)、メソッドのソースを表示する ($
)、Pry セッションを抜ける (exit
/ quit
)
(main)> require "open-uri" => true (main)> cd OpenURI (OpenURI):1> ls constants: Buffer HTTPError HTTPRedirect Meta OpenRead Options OpenURI.methods: check_options open_http open_loop open_uri redirectable? scan_open_optional_arguments locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_ (OpenURI):1> $ open_uri From: /usr/local/rbenv/versions/2.5.0/lib/ruby/2.5.0/open-uri.rb @ line 140: Owner: #<Class:OpenURI> Visibility: public Number of lines: 41 def OpenURI.open_uri(name, *rest) # :nodoc: uri = URI::Generic === name ? name : URI.parse(name) ...(省略) (OpenURI):1> exit => OpenURI
参考:
pry、pry-doc を使って Ruby 学習ヘ | Murajun’s Style
Ruby の IRB や pry でメソッドの定義元をすっと調べる方法 | TechRacho
pry-doc でカジュアルに Ruby のソースコードを読む – Qiita
Pry と pry-doc gem を使って Ruby のソースコードを読む | ジョージプログラマーの日記
pry/pry-doc: Provide MRI Core documentation and source code for the Pry REPL – GitHub
Documentation for pry-doc | RubyDoc.info
Pry プラグイン
参考:
Ruby の pry をより高機能に使うためのパッケージ6選のメモ | メモ帳代わりのブログ
Pry をより快適にしてくれる Gems とその設定方法 – Qiita
Pry の関係 gem が最初からいろいろはいった jazz_fingers を試してみた | Scimpr Blog
source_location
参考:
Ruby でメソッドの定義場所を見つける方法 – Qiita
Ruby で class や const の定義位置を調べる方法 – Qiita
How to find the method definition in Ruby | Rails Guides
How do I find where a constant is defined in Ruby? – Stack Overflow
How to find where a method is defined at runtime? – Stack Overflow
Access Pry’s show-source method from Ruby file – Stack Overflow
Thread.current.backtrace_locations
参考:
How to correctly set up Pry in Rails 4.2 – Stack Overflow
Pry::Byebug::Breakpoints#add_method
参考:
Method: Pry::Byebug::Breakpoints#add_method — Documentation for pry-byebug | RubyDoc.info
Bindex
例外発生時に binding
を使えるようにする。
参考:
gsamokovarov/bindex: Bindings for your Ruby exceptions – GitHub
binding_of_caller
参考:
banister/binding_of_caller: Retrieve the binding of a method’s caller – GitHub
Documentation for binding_of_caller | RubyDoc.info
binding_of_caller gem でメソッド呼び出し元の binding を手に入れる | pockestrap
debug_inspector
参考:
banister/debug_inspector: A Ruby wrapper for the MRI 2.0 debug_inspector API – GitHub
Can you eval code in the context of a caller in Ruby? – Stack Overflow
セグメンテーションフォルト
Mac の場合、クラッシュログは ~/Library/Logs/DiagnosticReports
ディレクトリ、もしくは /Library/Logs/DiagnosticReports
ディレクトリに保存される。
参考:
Ruby 2.5 の SEGV と闘った話 | @watson1978 の日記
Ruby が SEGV したら | やっぱRubyでしょ。(LoveRuby.Net)
Ruby の C 拡張で SEGV おこした時の gdb での追い方、またはデバグ手法 | sonots:blog
Ruby の落とし方 | Rubyist Magazine (るびま)
core dump をとった後中身を見てみた gdbruby 編 | CubicLouve
プロセスのコアファイルからバックトレースする。| 技術的ブログ (www.ginriki.net)
Segfault on macOS with ffi > 1.9.21 · Issue #619 · ffi/ffi – GitHub
ruby で segmentation fault – teratail
Ruby C extension, how to recover from segmentation fault – Stack Overflow
gdb
コアダンプを読み込んで GDB のセッションを開始する。
$ gdb $(rbenv which ruby) core
バックトレースを表示する。
(gdb) bt
指定したフレームに移動する。
(gdb) frame 12
フレームを上に移動する。
(gdb) up
フレームを下に移動する。
(gdb) down
セッションを終了する。
(gdb) quit
参考:
gdb を使った ruby のデバッグ | クックパッド開発者ブログ
gdb を使って Ruby 2.4.0 のライブプロセスの情報を取得してみる | CubicLouve
gdb を使って Ruby (2.3.3) のライブプロセスの情報を取得する | CubicLouve
gdb で ruby のバックトレースを自動で採取 | hiboma の日記
gdb で ruby プロセスの C レベルと Ruby レベルのバックトレースを表示する方法 | sonots:blog
Getting a Ruby backtrace from GNU debugger | isotope eleven
Finding a Ruby bug with GDB | Zachary Anker
Debugging Stuck Ruby Processes – What to do Before You Kill -9 | New Relic Blog
How to use gdb to get ruby backtrace from different thread – Stack Overflow
gdb-ruby
参考:
david942j/gdb-ruby: It’s time for Ruby lovers to use Ruby in gdb, and gdb in Ruby – GitHub
DTrace
DTrace を有効にして rbenv で Ruby をインストールする。
$ CONFIGURE_OPTS="--enable-dtrace" rbenv install 2.6.1
メソッドをトレースする。
ruby$target:::method-entry { printf("%s#%s\n", copyinstr(arg0), copyinstr(arg1)); }
C 言語で書かれたメソッドをトレースする。
ruby$target:::cmethod-entry { printf("%s#%s\n", copyinstr(arg0), copyinstr(arg1)); }
DTrace による監視を開始する。(Ctrl
+ C
で停止する。)
$ sudo dtrace -q -s probes.d -p PID
-q
:メッセージの出力を抑制する-s
:スクリプトを指定する-p
:プロセス ID を指定する
参考:
Ruby 2.5 の SEGV と闘った話 | @watson1978 の日記
Ruby 2.0.0 の DTrace の紹介 | Rubyist Magazine (るびま)
バッチ処理の一部で 30 分以上かかっていた処理を 14 秒で終わるようにした話 | @watson1978 の日記
gdbdump-ruby
参考:
sigdump
参考:
frsyuki/sigdump: Use signal to show stacktrace of a Ruby process without restarting it – GitHub
gdbruby
参考:
gunyarakun/gdbruby: gdbperl for Ruby – GitHub
Core から Ruby のバックトレースを表示する gdbruby.rb を作った | グニャラくんの wktk 運営日記
core dump をとった後中身を見てみた gdbruby 編 | CubicLouve
segv-handler-gdb
参考:
kou/segv-handler-gdb: Dump C level backtrace by GDB on SEGV – GitHub
Rubyス クリプトがクラッシュしたときにより詳しくCレベルのバックトレースを出力する gem | ククログ
blab
参考:
tuwukee/blab: A debugging tool – GitHub
ruby-debug
参考:
ruby-debug/ruby-debug: ruby-debug for Ruby 1.8 – GitHub
ruby-debug を使った Ruby・Rails アプリケーションのデバッグ方法 – GitHub Gist
gem のインストールログを取る
$ MAKE="make V=1" gem install gem_name --verbose > install_log.txt 2>&1
参考:
Segfault on macOS with ffi > 1.9.21 · Issue #619 · ffi/ffi – GitHub