Debugging with IEx

You already know that IEx is the go-to utility to play with Elixir code. It also has a secret and dark second life as a debugger. It isn’t fancy, but it lets you get into a running program and examine the environment.

You enter the debugger when running Elixir code hits a breakpoint. There are two ways of creating a breakpoint. One works by adding calls into the code you want to debug. The other is initiated from inside IEx. We’ll look at both using the following (buggy) code:

 defmodule​ Buggy ​do
 def​ parse_header(
  <<
  format::integer-16,
  tracks::integer-16,
» division::integer-16
  >>
  ) ​do
 
  IO.puts ​"​​format: ​​#{​format​}​​"
  IO.puts ​"​​tracks: ​​#{​tracks​}​​"
  IO.puts ​ ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.