Cognitip

TIL: Explore the Elixir Function Call Stack

Today I Learned, that you can explore the functional call stack in elixir by adding Process.info(self(), :current_stacktrace) |> elem(1) |> IO.inspect into the function that you want to explore.

An example usecase of this, is wanting to make the Liquid port of Elixir execute Liquid Filters with Liquid Register/Context to be able to have some kind of state based Filter.

[
  {Process, :info, 2, [file: 'lib/process.ex', line: 646]},
  {Filters.Money, :money, 1, [file: 'lib/__/filters/money.ex', line: 3]},
  {Liquid.Filters, :apply_function, 3,
   [file: 'lib/liquid/filters.ex', line: 532]},
  {Liquid.Filters, :filter, 2, [file: 'lib/liquid/filters.ex', line: 499]},
  {Liquid.Variable, :lookup, 2, [file: 'lib/liquid/variable.ex', line: 26]},
  {Liquid.Render, :render, 3, [file: 'lib/liquid/render.ex', line: 31]},
  {Liquid.Render, :render, 3, [file: 'lib/liquid/render.ex', line: 19]},
  {Liquid.Render, :render, 2, [file: 'lib/liquid/render.ex', line: 10]},
  {GenieWeb.Proxy.ProductController, :get, 2,
   [file: 'lib/__/controllers/product_controller.ex', line: 22]},
  {GenieWeb.Proxy.ProductController, :action, 2,
   [file: 'lib/__/controllers/product_controller.ex', line: 1]},
  {GenieWeb.Proxy.ProductController, :phoenix_controller_pipeline, 2,
   [file: 'lib/__/controllers/product_controller.ex', line: 1]},
  {GenieWeb.Endpoint, :instrument, 4,
   [file: 'lib/_/endpoint.ex', line: 1]},
  {Phoenix.Router, :__call__, 1, [file: 'lib/phoenix/router.ex', line: 278]},
  {GenieWeb.Router, :call, 2, [file: 'lib/plug/error_handler.ex', line: 64]},
  {GenieWeb.Endpoint, :plug_builder_call, 2,
   [file: 'lib/_/endpoint.ex', line: 1]},
  {GenieWeb.Endpoint, :"call (overridable 3)", 2,
   [file: 'lib/plug/debugger.ex', line: 102]},
  {GenieWeb.Endpoint, :call, 2, [file: 'lib/__/endpoint.ex', line: 1]},
  {Plug.Adapters.Cowboy.Handler, :upgrade, 4,
   [file: 'lib/plug/adapters/cowboy/handler.ex', line: 16]},
  {:cowboy_protocol, :execute, 4,
   [file: '/Users/jordan/_/deps/cowboy/src/cowboy_protocol.erl', line: 442]}
]