debugging way in Elixir

August 17, 2017    Elixir Debug Test

Environment


version

  • Elixir: 1.5.1
  • IEx: 1.5.1
  • OTP: 20

IEx 1.5 ~ provides break! macro

According to CHANGELOG1, IEx supports developer to setup a breakpoint in the iex console

it is more useful, flexible and easy to debug your code without writing require IEx; IEx.pry in your soucecode every time

But as the doc2 say, you have to upgrade Erlang OTP to its version 20 and upgrade Elixir to its version 1.5 ~ as well.

Erlang provides the GUI :debugger too

Erlang also provides a debugger but I forget how to start GUI and setup breakpoints because a function looks weird. Whenever I use it, I search it again and again

> iex -S mix phoenix.server
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

[info] Running Concertrip.Endpoint with Cowboy using http://localhost:4000
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
configured file: ~/.iex.exs

iex(2)> :debugger.start()
{:ok, #PID<0.403.0>}
iex(4)> :int.ni(Ecto)
{:module, Ecto}
iex(5)> :int.break(Ecto, 10)
:ok

:debugger.start() shows GUI and :int.ni() set Module

Debug GUI

:debugger.start() is OKay but what do :int module and ni() function mean?

Erlang doc3 says that :int means Interpreter. And I have no clue about ni() hahaha

Is that not memorable you know?

Both are fine to set breakpoints to libraries

In my opnion either is fine, but IEx is getting better than before.

For instance before IEx provides break!, it’s not a good way to debug compiled Modules with IEx.

Thanks to break! macro, now you can debug them if you want to like :debugger.

Let’s check out Usage of break! with a breakpoint set in Ecto module.

##Usage of break! break! requires at least 3 informations such as Module, function and arity.

The syntax are like IEx.break!(Ecto.Changeset, :cast, 3) or like IEx.break!(Ecto.Changeset.cast/3)

iex -S mix phoenix.server
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

[info] Running Concertrip.Endpoint with Cowboy using http://localhost:4000
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
configured file: ~/.iex.exs
iex(1)> require IEx
iex(2)> IEx.break!(Ecto.Changeset, :cast, 3)
1
iex(3)> [info] POST /graphiql
Request to pry #PID<0.408.0> at Ecto.Changeset.cast/3 (deps/ecto/lib/ecto/changeset.ex:385)

  383:              %{binary => term} | %{atom => term} | :invalid,
  384:              [String.t | atom]) :: t | no_return
  385:   def cast(data, params, allowed) do
  386:     do_cast(data, params, [], allowed)
  387:   end

Allow? [Yn]

At first you have to require IEx once and then you can insert pry and stop the function that you set.

Take a look at IEx doc for more informations, if you wnat.



comments powered by Disqus