68
votes

I've just started using Elixir, and have started a Phoenix project, which I enjoy a lot. Now by having rails background, I'm used to being spoiled with debugging tools like "debugger", "byebug" and so on; I was wondering if there are any similar tools for Elixir? How are you guys debugging your Elixir applications?

Even an equivalent to Rubys raise my_object.inspect, would do wonders!

Thank you

6
Have a look at this awesome list, mentioning some debugging tools: github.com/h4cc/awesome-elixir#debugging - h4cc
For what it's worth, I've heard that this book: erlang-in-anger.com has some awesome advice on debugging Erlang. Most of the tools one would use with Erlang are equally applicable to Elixir. - Onorio Catenacci
Not 100% related but it is also worth mentioning :observer.start (run it on IEx). It is a great tool for debugging and exploring running applications. - José Valim
Any thoughts about debugging Phoenix/cowboy apps, say if you want to debug the behavior of a controller when a request rolls in. - MartinElvar
Old thread but you should check this out if you haven't yet youtube.com/watch?v=pj6zAgvVt5w - Shashank Kulkarni

6 Answers

75
votes

You can use IEx

require IEx

value = {:some, :erlang, :value}
IEx.pry

If you start this program with for example iex -s program.exs (or iex -S mix for a project) you'll be asked if you want to allow prying into this code when it is reached and value will be available for you for inspection.

You can also just do print debugging using IO.inspect allowing you to output basically any erlang data structure.

32
votes

Debugging Cowboy apps, and Phoenix apps.

I saw this post in the Elixir rader http://www.jessetrimble.net/iex-pry-elixir, and thought i would just summarise it up here, as it's extremely convenient :-).

In Rails applications (and other), you can simply put in the debugger tag in your controller, and when the path is triggered, it will break at the debugger tag.

When using pry in Phoenix the above will result in

Cannot pry #PID<0.259.0> at web/controllers/posts_controller.ex:8. Is an IEx shell running?

It turns out that the Phoenix process must run within an IEx session, this is done as such

iex -S mix phoenix.server

Now instead you will see

Request to pry #PID<0.266.0> at web/controllers/posts_controller.ex:9. Allow? [Yn]
11
votes

You can use Quaff.Debug module from https://github.com/qhool/quaff

The Debug module provides a simple helper interface for running Elixir code in the erlang graphical debugger

I tested it today with Elixir 1.0.4, it works.

4
votes

Use the Erlang debugger. Example with Phoenix 1.3 and Elixir 1.5.1, source file: ./lib/todo/api/api.ex and the module name is: Todo.API

~/elixir/todo_app/ iex -S mix phx.server
Erlang/OTP 20 [erts-9.0] [source] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]

[info] Running TodoWeb.Endpoint with Cowboy using http://0.0.0.0:4000
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :debugger.start()
{:ok, #PID<0.373.0>}
iex(2)> :int.ni(Todo.API)
{:module, Todo.API}

In the Erlang debugger:

  • The left panel in the Monitor window shows the loaded module.
  • The Module menu, the bottom item shows the loaded module with a 'View' and 'Delete' submenu. Use the View menu to see the source with line numbers.
  • To place a breakpoint, use the Break menu, Line breaks...
  • Run your program until it stops at the specified line. The Monitor windows shows a process with status 'break'. Double click on this line to open the attached process in the debugger. Here you can step, step over (next), continue, go up, inspect values, etc. To step into another module it must be loaded like above as well.
  • A breakpoint will be ignored if not correctly placed. If you have a multiline pipeline, place the breakpoint on the last line.
1
votes

In Elixir 1.5 and OTP 20 there's a new function Exception.blame/3 which can add debug information to certain exceptions. It only supports FunctionClauseErrors right now and you should only use it in development because it's an expensive task: the function will retrieve the available clauses from bytecode and evaluate them against the given arguments. See Release

0
votes

There's a way to debug tests similar to byebug does: Using the command iex -S mix test, this will run your tests and if an IEx.pry was encountered, it will ask if you want to "stop" there and analyze its context.

Code example:

defmodule AppTest do
  def hello do
    test_variable = "john doe"
    require IEx; IEx.pry
    :world
  end
end
defmodule AppTestTest do
  use ExUnit.Case
  doctest AppTest

  test "greets the world" do
    assert AppTest.hello() == :world
  end
end

Running iex -S mix test it will stop in the require IEx; IEx.pry.

Source: https://elixirforum.com/t/how-to-debug-exunit-tests-with-debugger/14170/4