Debugging

A failing test names a line; the question after that is which value reached it and who called the function. Elixir answers with three tools: IO.inspect for a value, IEx.pry for a breakpoint in the shell, and the stack trace of a rescued error for the call path. This lesson uses each one on a program that is small enough to see the answer.

IO.inspect with a label

IO.inspect/2 returns its first argument, so it fits anywhere in an expression. The label: option writes a word in front of the value, which is what tells two inspections apart in a long output:

inspect.exs

value = %{name: "Ada", scores: [10, 20, 30]} IO.inspect(value, label: "value") IO.inspect(value.scores, label: "scores")

elixir inspect.exs

value: %{name: "Ada", scores: [10, 20, 30]}
scores: [10, 20, 30]

The value is printed once and returned, so the line value |> Map.get(:scores) |> IO.inspect(label: "scores") keeps the pipeline and the output. A label is cheap, and a run with ten unlabelled values is a puzzle.

The value that floods the output

A map or a list with a thousand entries prints all of them by default. The limit: option keeps the first entries and writes ... for the rest, which is the difference between a line a reader can use and a screen of data:

limit.exs

map = Map.new(1..20, fn n -> {n, n} end) IO.inspect(map, limit: 5)

elixir limit.exs

%{1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, ...}

The truncated map is still a map; only the printing is shorter. pretty: true spreads a nested value over several lines, and structs: false prints a struct as a plain map when the field names are the important part.

IEx.pry

IEx.pry() is a breakpoint: the shell stops at the line, lets the reader inspect every binding, and continues when the prompt is left. It only works inside iex -S mix or a test run with --trace, and a plain elixir file.exs has no shell to stop in. The line is therefore described here and not run: the iex lesson shows the shell it needs.

The trace that says who called

A rescued error carries __STACKTRACE__, the list of calls that led to the raise. Exception.format/3 turns it into the text a crash would print, with the nearest frame first:

who.exs

defmodule Who do def call, do: raise "boom" def run do call() rescue error -> IO.puts(Exception.format(:error, error, __STACKTRACE__)) end end Who.run()

elixir who.exs

** (RuntimeError) boom
    who.exs:2: Who.call/0
    who.exs:5: Who.run/0
    who.exs:11: (file)
    (elixir 1.19.7) src/elixir_compiler.erl:81: :elixir_compiler.dispatch/4
    (elixir 1.19.7) src/elixir_compiler.erl:56: :elixir_compiler.compile/4
    (elixir 1.19.7) src/elixir_compiler.erl:43: :elixir_compiler.maybe_fast_compile/2
    (elixir 1.19.7) src/elixir_lexical.erl:17: :elixir_lexical.run/3
    (elixir 1.19.7) src/elixir_compiler.erl:21: :elixir_compiler.quoted/3

The first three frames are the program: the raise, the function that called it, and the file. The frames below are the compiler that ran the file. A trace with :dbg or :erlang.trace goes further and reports a call that did not raise, but the stack of a rescued error is the trace a program already has.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does IO.inspect/2 return?

    Answer

    Its first argument, after printing it

    inspect returns the value it printed, so it fits in a pipeline.

  2. What does the label: option of IO.inspect/2 add?

    Answer

    A word in front of the printed value

    The label names the inspection, which is what tells two outputs apart.

  3. What does limit: 5 do in IO.inspect/2?

    Answer

    Prints the first five entries and ... for the rest

    The limit shortens a big term in the printed form.

  4. Where does IEx.pry/0 work?

    Answer

    In a shell or a test run that can stop, not in a plain script

    pry needs a shell to hand over; a plain elixir run has none.

  5. What does this program print? value = [1, 2, 3] IO.inspect(value, label: "list")

    Answer

    list: [1, 2, 3]

    The label is printed before the value.

  6. What does this program print? map = Map.new(1..10, fn n -> {n, n} end) IO.inspect(map, limit: 2)

    Answer

    %{1 => 1, 2 => 2, ...}

    The limit keeps two entries and marks the rest with ...

  7. What is __STACKTRACE__?

    Answer

    The list of calls that led to the error being rescued

    It is available in a rescue block and holds the call path of the error.

  8. What does Exception.format(:error, error, __STACKTRACE__) answer?

    Answer

    The text a crash would print, with the message and the frames

    It formats the exception and the stack as the reporter prints them.

  9. In the trace of a rescued error, which frame comes first?

    Answer

    The nearest call, where the error was raised

    The stack is printed with the innermost frame first.

  10. Why is IO.inspect safe in the middle of a pipeline?

    Answer

    It returns the value it printed

    The value passes through unchanged, so the next stage still receives it.

Article author: Arthur Isaev