DEV Community

Sushant Bajracharya
Sushant Bajracharya

Posted on • Edited on

4

Debugging with IEx

There are already too many articles about Debugging with IEx on the web, so I will just get to the point straight.

FYI: As per Jose, Pry is not a debugger. It just allows you to interact with a particular context. https://stackoverflow.com/questions/33203676/iex-pry-going-step-by-step

If you want to

add a breakpoint in your code: require IEx; IEx.pry()

continue to the next breakpoint: continue()

continue the execution of code without hitting any other breakpoints: respawn()

add a breakpoint to a function: IEx.break!(Module, :function, arity) eg IEx.break!(String, :length, 1)

know your breakpoints current location: whereami

open the code in the editor: open()

list out all the methods of a module: exports(ModuleName)

Keep In Mind, that

  • helper functions like continue, respawn, whereami are only available inside pry session.

  • whereami, open won't work with precompiled source code

  • You need to configure either ELIXIR_EDITOR or EDITOR env variable for open to work

Productivity

It would make your life easier if you would configure a couple of things.

  • Enabling History

If you want to have access to the commands you typed in your previous iex session, then add this export ERL_AFLAGS="-kernel shell_history enabled" to your .bashrc file and source .bashrc. You won't get your shell history if you had terminated the previous IEx session by pressing Ctrl + c two times. The right way to exit IEx session is to ctrl g + q

  • Customizing your IEx

You can customize your IEx by creating .iex.exs file. This file gets executed every time you run IEx.

My .iex.exs file:

IO.puts """
        `
       ;:
      `+;
      ++',
     .+++,
     :'++'.
    .+'++';       Welcome to elixir-land
    ,+'+''':
   `:+'+';';;     http://elixir-lang.org
   ,:++';;;;'.    http://www.phoenixframework.org/
   ::'+';;;;''    https://hex.pm/
   :;;++;;';''    http://www.elixirschool.com/
   .;;'''';;;:    https://github.com/christopheradams/elixir_style_guide
    ,:::'';,,.
    ,,,,,:::,
     ,,,,,,,
      ``.`
"""
IEx.configure(
  alive_prompt: "%prefix(%node):%counter>",
  default_prompt: "%prefix:%counter>",
  inspect: [pretty: true, char_lists: :as_lists, limit: :infinity],
  history_size: -1,
  colors: [
    ls_directory: :cyan,
    ls_device: :yellow,
    doc_code: :green,
    doc_inline_code: :magenta,
    doc_headings: [:cyan, :underline],
    doc_title: [:cyan, :bright, :underline],
    eval_result: [ :cyan, :bright ]
  ]
)

import_file_if_available(".secret.exs")

rs = fn ->
  Process.exit(self(), :normal)
end

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay