JuliaのDebugger


後で書き足すかもしれませんが、とりあえず報告です。(追記しました)

つい先日、Debugger.jlがリリースされました。
ステップ実行、ブレークポイント(条件付きブレークポイントも使えます)、ウォッチリスト、デバッグ中のコンテキストでの任意コマンド実行、等の通常のデバッグに必要と思われる機能を網羅しています。

現在は、REPL上でのコマンドラインデバッグしかできませんが、Juno等でのIDE側での対応を目指した作業も進んでいます。これとかこれとか。
近いうちに、IDEでのデバッグもできるようになるでしょう。

Juliaを人に勧めるうえでの最大の障害といってよかったDebuggerがないという問題が解消されます。


追記。

使い方の簡単な説明

インストール

REPL上で]を押して、PKGモードに入って、

(v1.1) pkg> add https://github.com/JuliaDebug/Debugger.jl
(v1.1) pkg> add JuliaInterpreter

でインストール

使い方

例として

julia> function foo(n)
           x = n+1
           ((BigInt[1 1; 1 0])^x)[2,1]
       end
foo (generic function with 1 method)

という関数をデバッグしたいとします。

まず、

julia> using Debugger, JuliaInterpreter

しておいて、

julia> @enter foo(20)
In foo(n) at REPL[1]
>2      x = n+1
 3      ((BigInt[1 1; 1 0])^x)[2,1]
 4  end

About to run: (+)(20, 1)
1|debug>

のように、@enterマクロでfooを呼び出せば、関数の最初の行でとまります。
ここで、例えば、nと押せばステップ実行できます。使えるコマンド一覧は、?と押せば表示されます。

1|debug> ?
  Basic Commands:
  - st: show the status
  - n: step to the next line
  - s: step into the next call
  - so: step out of the current call
  - c: continue execution until a breakpoint is hit
  - bt: show a simple backtrace
  - `stuff: run stuff in the current function's context
  - fr [v::Int]: show all variables in the current frame, v defaults to 1
  - f [n::Int]: go to the n-th frame
  - w
   - w add expr: add an expression to the watch list
   - w: show all watch expressions evaluated in the current function's context
   - w rm [i::Int]: remove all or the i:th watch expression
  - q: quit the debugger, returning nothing
  Advanced commands:
  - nc: step to the next call
  - se: step one expression step
  - si: same as se but step into a call if a call is the next expression
  - sg: step into a generated function

とくに、`と押すと、プロンプトが変わって、デバッグ中のコンテキストで任意のコマンドを実行できます。
変数の中身を表示したり、

1|julia> n
20

変数を書き換えたり。

1|julia> n=15
15

ブレークポイント

ブレークポイントは、JuliaInterpreter.jlを使って設定します。公式ドキュメント
例えば、

julia> breakpoint(foo, Tuple{Int}, 3)

とすれば、foo(::Int)メソッドの3行目にブレークポイントが設定されます。
@runマクロでfooを呼び出せば、ブレークポイントでとまります。

julia> @run foo(20)
Hit breakpoint: foo(n) at REPL[1], line 3
In foo(n) at REPL[1]
 2      x = n+1
>3      ((BigInt[1 1; 1 0])^x)[2,1]
 4  end

About to run: (tuple)(2, 2)
1|debug>

ファイル上の特定の場所にブレークポイントを設定することもできます。

julia> breakpoint(filename, line)
julia> breakpoint(filename, line, condition)

条件付きブレークポイントとかは、公式ドキュメントを見てください。