Debugging in Elixir

deepak sharma
4 min readJan 19, 2023

Welcome to our guide on debugging in Elixir! Elixir is a functional programming language that is known for its concurrency and scalability features. However, like any other language, debugging in Elixir can be challenging. In this guide, we’ll explore the different tools and techniques available for debugging Elixir applications. From understanding error messages to using the built-in IEx shell, we’ll cover everything you need to know to troubleshoot and fix bugs in your Elixir code. Whether you’re a beginner or an experienced Elixir developer, we hope you find this guide informative and useful. Happy debugging!

IO.inspect

One of the most valuable tools for debugging Elixir code is the IO.inspect function. This function allows you to print the value of a variable or expression to the console, making it easy to see what's happening inside your code.

Here’s an example of how to use IO.inspect in your code:

def my_function(arg) do
IO.inspect arg
# ... more code here
end

In this example, the IO.inspect function is used to print the value of the arg variable to the console. This can be especially useful when you're trying to understand the behavior of a function, or when you're trying to debug a specific part of your code.

You can also use the IO.inspect function to print the value of an expression, like so:

IO.inspect 2 + 2

In addition to the basic usage, IO.inspect also accepts options to customize the output. For instance, you can pass in label: "my label" to add a label to the printed output or pretty: true to enable pretty printing of the output.

IO.inspect 2 + 2, label: "result"
IO.inspect [1, 2, 3], pretty: true

Overall, IO.inspect is a powerful and versatile tool that can help you debug your Elixir code quickly and easily. By using it throughout your development process, you can identify and fix bugs with confidence.

IEX.pry

Another useful tool for debugging in Elixir is IEx.pry. It allows you to pause the execution of your code and open an interactive shell, where you can inspect the current state of your program, execute expressions and navigate the call stack.

To use IEx.pry you need to first install the pry package by adding it to your dependencies in mix.exs file.

defp deps do
[
{:pry, "~> 0.13"}
]
end

Once you have it installed, you can use IEx.pry in your code by calling it directly.

def my_function(arg) do
IEx.pry
# ... more code here
end

When the execution reaches the line where IEx.pry is called, it will pause the execution and open an interactive shell where you can inspect the current state of the program, for example, you can check the value of variables, call functions, etc.

You can also use binding() function to get the current environment and inspect it in the shell.

def my_function(arg) do
IEx.pry(binding())
# ... more code here
end

When you are done with the inspection, you can type continue to resume the execution.

IEx.pry is a powerful tool that allows you to gain insight into the state of your program while it's running. It can help you identify and fix bugs quickly, and it's especially useful when you're trying to understand the behavior of a function or a specific part of your code.

dbg

Another useful tool for debugging in Elixir is :dbg. It allows you to insert breakpoints in your code, similar to the way traditional debuggers work.

To use :dbg, you need to first start your application in debug mode by passing the --debug option to iex or elixir command.

iex --debug -S mix

Once your application is running in debug mode, you can use :dbg to insert breakpoints in your code.

def my_function(arg) do
:dbg.tpl(arg)
# ... more code here
end

When the execution reaches the line where :dbg.tpl(arg) is called, it will pause the execution and open an interactive shell where you can inspect the current state of the program, for example, you can check the value of variables, call functions, etc.

You can also use :dbg.p to print the value of an expression, like so:

:dbg.p 2 + 2

You can also use :dbg.c to continue the execution and :dbg.n to step to the next line.

:dbg is a powerful tool that allows you to gain insight into the state of your program while it's running. It can help you identify and fix bugs quickly, and it's especially useful when you're trying to understand the behavior of a function or a specific part of your code. However, it's important to note that the application will be running slower when it's running in debug mode and it is not recommended to run production code in debug mode.

In conclusion, debugging in Elixir can be a challenging task but with the right tools and techniques, it can be made much more manageable. The IO.inspect, IEx.pry, and :dbg are all great tools that can be used to gain insight into the state of your program, understand error messages, and identify and fix bugs. By using these tools throughout your development process, you can develop more efficiently and with more confidence. Remember to use them in the correct context and use them in development environments, not in production. Happy debugging!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

deepak sharma
deepak sharma

Written by deepak sharma

Engineer with 10 years of experience. I have worked on varied tech stacks like Python flask, Elixir etc. Support me https://buymeacoffee.com/deepaksharma89

Responses (3)

Write a response