Elixir — quick reference for debugging techniques
Much has been said about Elixir debugging techniques, but in this post, I’d like to give a quick overview of all possible options to serve as a go-to reference when you need to debug Elixir code. Enough talking, let’s check each of them:
IO.inspect
The simplest technique:
IO.inspect/2
can also be used inside pipelines because it returns the item passed to be inspected. And the tip here is to use the option label:
to output a string identifying each inspect:
IO.inspect with binding
binding/1
is very useful when you want to see all variable names and values of the current function:
apex
Similar to IO.inspect/2
, apex is a lib worth mentioning especially because of its adef
macro:
IEx.pry
But it can be very tedious and be limiting to debug with just data inspection likeIO.inspect
or apex
, that's when IEx.pry/0
comes at hand because it allows you to pry into the current code. Put the following code at the line you want to pry:
And now execute your code inside an IEx session: iex -S mix
or iex -S mix phx.server
if you are using Phoenix Framework. Tip: if you are running tasks like database seeds, you can pry into that code by running iex -S mix run priv/repo/seeds.exs
or any other script.
Once the code execution gets to the point of IEx.pry
, an interactive shell opens and allow you to interact with the current code.
Go to Debugging Phoenix with IEx.pry if you want more tips about debugging Phoenix with IEx.pry.
:debugger
Pretty much the same as IEx.pry
which stops the execution at the break point and allows you to inspect the current code, but :debugger
gives you a nice visual interface, like the ones in IDEs:
To open this debugger, you need to start it and set a break point:
:sys.get_state and :sys.get_status
This one works only for processes. As the name suggests, :sys.get_state/1
gets the current state of a process, and not only from a GenServer
but also from any kind of process:
Snippet extracted from Looking at the state of processes in Elixir.
If you need more data than just the current state, just call :sys.get_status/1
, which will return whole process information:
Snnipet extracted from Programming Elixir book.
Bonus: you can replace a process state at runtime using :sys.replace_state/2
, which can be very handy to test some specific situation.
Process.info
You can also use Process.info/1
to get information about a specific process:
:sys.trace
Still talking about process debug, another resource we have is :sys.trace/2
to trace process calls, which will display each call and state change of the calling process:
Distillery commands
After you deploy your application as a package release, it may be very helpful to get information about it, specially if something is not working as expected. If you have deployed using Distillery, you'll soon discover that mix
does not work the same way as in your local machine, and that's expected. But you can execute some commands on that release by calling bin/<app_name> <command>
, the two more useful commands are: remote_console
to start an IEx session on the running release and help
to give you a list of all commands.
:observer
Although it's not exactly a debug tool, it's worth mentioning that :observer
helps you get an overview of the running system. I'll not dig into details because there are a lot of articles that already do that, so I'll just leave here the function call to start the observer:
Notes and sources
If you have more tips and techniques, please leave a comment so I can update this post and help spread more Elixir knowledge. :)
Some code snippets were extracted from the great official documentation or from the official getting started tutorial.
The book Programming Elixir was also used as source.
The Today I Learned from Hashrocket is also a great source of tips.
More where this came from
This story is published in Noteworthy, where thousands come every day to learn about the people & ideas shaping the products we love.
Follow our publication to see more product & design stories featured by the Journal team.
Looking for a creative company to implement your next idea ? Check out LNA Systems and let’s talk.