Skip to content

Instantly share code, notes, and snippets.

@garthk
Created September 9, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/65abeacd11e917a9c70e721485fd7705 to your computer and use it in GitHub Desktop.
Save garthk/65abeacd11e917a9c70e721485fd7705 to your computer and use it in GitHub Desktop.
Watch Phoenix.PubSub.broadcast/2 calls from your context with :dbg
{:ok, _} =
:dbg.tracer(
:process,
{fn
{:trace, pid, :call, {mod, fun, args} = call}, stacks ->
mfa = Exception.format_mfa(mod, fun, Enum.count(args))
stack = Map.get(stacks, pid, [])
if match?({Phoenix.PubSub, :broadcast, _}, call) do
Pretty.inspect(stack, label: "broadcast stack")
end
Map.put(stacks, pid, [mfa | stack])
{:trace, pid, :exception_from, {mod, fun, arity}, {class, value}}, stacks ->
mfa = Exception.format_mfa(mod, fun, arity)
Pretty.inspect({class, value}, label: "exception_from")
case Map.get(stacks, pid) do
[^mfa | tail] -> Map.put(stacks, pid, tail)
stack -> IO.warn("bad exception_from " <> inspect(%{mfa: mfa, stack: stack}))
end
{:trace, pid, :return_from, {mod, fun, arity}, _retval}, stacks ->
mfa = Exception.format_mfa(mod, fun, arity)
case Map.get(stacks, pid) do
[^mfa | tail] -> Map.put(stacks, pid, tail)
stack -> IO.warn("bad return_from " <> inspect(%{mfa: mfa, stack: stack}))
end
end, %{}}
)
:dbg.p(:all, [:call])
:dbg.p(:all, [:call, :return_to])
:dbg.tp(MyApp.Context, [{:_, [], [{:return_trace}, {:exception_trace}]}])
:dbg.tp(Phoenix.PubSub, :broadcast, [{:_, [], [{:return_trace}, {:exception_trace}]}])
on_exit(&:dbg.stop_clear/0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment