Skip to content

Instantly share code, notes, and snippets.

@itsgreggreg
Last active June 28, 2017 11:56
Show Gist options
  • Save itsgreggreg/bd8e562ad4277aad966ab18346f3752e to your computer and use it in GitHub Desktop.
Save itsgreggreg/bd8e562ad4277aad966ab18346f3752e to your computer and use it in GitHub Desktop.
Elixir Debugging

Elixir Debugging Techniques

IO

Old standby for a quick look into your code

IO.puts

Works with anything that implements the String.Chars protocol, AKA the to_string method. Elixir ships with: https://github.com/elixir-lang/elixir/blob/e769afbed96b6b9eee6a4f4ac4a6ea00bc0630f7/lib/elixir/lib/string/chars.ex

IO.inspect

Returns the term handed to it untouched so you can plop it straight in your code
Can be configured with options: http://elixir-lang.org/docs/master/elixir/Inspect.Opts.html
Not to be confused with Kernel.inspect

**< 1.4 **

[1, 2, 3]
|> (fn(a) -> IO.puts("Before"); IO.inspect(a) end).()
|> Enum.map(&(&1 * 2))
|> (fn(a) -> IO.puts("After"); IO.inspect(a) end).()
|> Enum.sum

**> 1.4 **

[1, 2, 3]
|> IO.inspect(label: "before")
|> Enum.map(&(&1 * 2))
|> IO.inspect(label: "after")
|> Enum.sum

IO.warn

Returns the warning along with a stack trace.

Pry

Quickly look into the active scope

require IEx
IEx.pry()

To resume

respawn

Debugger

A tool for inspecting running code.

Breakpoints

  • :debugger.start()
  • :int.ni(ModuleName)
  • :int.break(ModuleName, line_num) or graphically

Observer

An insight tool for all kinds of metrics for a running BEAM node.

Locally

  • :observer.start()

Remotely

https://sgeos.github.io/elixir/erlang/observer/2016/09/16/elixir_erlang_running_otp_observer_remotely.html

  • iex --name sally -S mix s
  • epmd -names
  • iex --name bob
  • Node.connect(:"[email protected]")
  • :observer.start()
  • Nodes -> sally

Further Reading

https://s3.amazonaws.com/erlang-in-anger/text.v1.1.0.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment