IEx.Pryでデバッグ

ここ

デバッガみたいなのはある?

と書いた。調べてみたところIEx.pryが使えることがわかった。

使い方

大雑把にいうとこんな感じ

  1. IEx モジュールを require する。
  2. ブレイクポイントコードに仕込む。
  3. iex でサーバを起動する
  4. リクエストを出す。
  5. ブレイクポイントに到達すると、pry を実行するかを尋ねられるので Y を入力する。
  6. respwan で処理を続行する。

コードはこんな感じ(ベースはここ)。

defmodule HelloPhoenix.PageController do
  use HelloPhoenix.Web, :controller

  require IEx

  def index(conn, _params) do
    IEx.pry                    # ブレイクポイント
    render conn, "index.html"
  end
end

できること

  • 変数の内容を表示させる

などなど(詳しく調べていない)

実行サンプル

localhost:4000/?foo=bar にリクエストを出したところ。

% iex -S mix phoenix.server
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 17 Aug 21:02:01 - info: compiled 5 files into 2 files, copied 3 in 2280ms
Request to pry #PID<0.267.0> at web/controllers/page_controller.ex:7. Allow? [Yn] y

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
pry(1)> _params
%{"foo" => "bar", "format" => "html"}
pry(2)> conn
%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{},
 before_send: [#Function<1.90859823/1 in Plug.CSRFProtection.call/2>,
  #Function<4.77861751/1 in Phoenix.Controller.fetch_flash/2>,
  #Function<0.121794808/1 in Plug.Session.before_send/2>,
  #Function<1.60260050/1 in Plug.Logger.call/2>,
  #Function<0.94118563/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>],
 body_params: %{}, cookies: %{}, halted: false, host: "localhost",
 method: "GET", owner: #PID<0.267.0>,
 params: %{"foo" => "bar", "format" => "html"}, path_info: [],
 peer: {{127, 0, 0, 1}, 36626}, port: 4000,
 private: %{HelloPhoenix.Router => {[], %{}}, :phoenix_action => :index,
   :phoenix_controller => HelloPhoenix.PageController,
   :phoenix_endpoint => HelloPhoenix.Endpoint, :phoenix_flash => %{},
   :phoenix_layout => {HelloPhoenix.LayoutView, :app},
   :phoenix_pipelines => [:browser],
   :phoenix_route => #Function<1.114667012/1 in HelloPhoenix.Router.match/4>,
   :phoenix_router => HelloPhoenix.Router,
   :phoenix_view => HelloPhoenix.PageView, :plug_session => %{},
   :plug_session_fetch => :done}, query_params: %{"foo" => "bar"},
 query_string: "foo=bar", remote_ip: {127, 0, 0, 1}, req_cookies: %{},
 req_headers: [{"user-agent", "curl/7.35.0"}, {"host", "localhost:4000"},
  {"accept", "*/*"}], request_path: "/", resp_body: nil, resp_cookies: %{},
 resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"},
  {"x-request-id", "jcei0fi1smlj9c9npf323nfukor7p6eo"},
  {"x-frame-options", "SAMEORIGIN"}, {"x-xss-protection", "1; mode=block"},
  {"x-content-type-options", "nosniff"}], scheme: :http, script_name: [],
 secret_key_base: nil, state: :unset, status: nil}
pry(3)> respawn

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>