Debugging and debugging tools in Elixir?

Elixir

Elixir Problem Overview


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

Elixir Solutions


Solution 1 - Elixir

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.

Solution 2 - Elixir

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]

Solution 3 - Elixir

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.

Solution 4 - Elixir

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.

Solution 5 - Elixir

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

Solution 6 - Elixir

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartinElvarView Question on Stackoverflow
Solution 1 - ElixirPaweł ObrokView Answer on Stackoverflow
Solution 2 - ElixirMartinElvarView Answer on Stackoverflow
Solution 3 - ElixirJulien LirochonView Answer on Stackoverflow
Solution 4 - ElixirN. HoogervorstView Answer on Stackoverflow
Solution 5 - ElixirCyzanfarView Answer on Stackoverflow
Solution 6 - ElixirMateus LuizView Answer on Stackoverflow