-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcallback_tracing_server.ex
More file actions
233 lines (192 loc) · 6.42 KB
/
Copy pathcallback_tracing_server.ex
File metadata and controls
233 lines (192 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
defmodule LiveDebugger.GenServers.CallbackTracingServer do
@moduledoc """
This gen_server is responsible for tracing callbacks.
"""
use GenServer
require Logger
alias LiveDebugger.Services.System.DbgService, as: Dbg
alias LiveDebugger.Services.ModuleDiscoveryService
alias LiveDebugger.Services.ChannelService
alias LiveDebugger.Services.TraceService
alias LiveDebugger.Structs.Trace
alias LiveDebugger.Utils.Callbacks, as: CallbackUtils
alias LiveDebugger.Utils.PubSub, as: PubSubUtils
@callback_functions CallbackUtils.callbacks_functions()
## API
@doc """
Checks if GenServer has been loaded
"""
@spec ping!() :: :ok
def ping!() do
GenServer.call(__MODULE__, :ping)
end
## GenServer
@doc false
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
@impl true
def init(_args) do
tracing_setup_delay = Application.get_env(:live_debugger, :tracing_setup_delay, 0)
Process.send_after(self(), :setup_tracing, tracing_setup_delay)
{:ok, %{}}
end
@impl true
def handle_info(:setup_tracing, state) do
Dbg.tracer(:process, {&handle_trace/2, 0})
Dbg.p(:all, [:c, :timestamp])
add_live_modules_to_tracer()
# This is not a callback created by user
# We trace it to refresh the components tree
Dbg.tp({Phoenix.LiveView.Diff, :delete_component, 2}, [])
if Application.get_env(:live_debugger, :tracing_update_on_code_reload?, false) do
# We need to get information when code reloads to properly trace modules
Dbg.tp({Mix.Tasks.Compile.Elixir, :run, 1}, [{:_, [], [{:return_trace}]}])
end
{:noreply, state}
end
@impl true
def handle_call(:ping, _from, state) do
{:reply, :ok, state}
end
@spec handle_trace(term(), n :: integer()) :: integer()
defp handle_trace({_, _, :return_from, {Mix.Tasks.Compile.Elixir, _, _}, {:ok, _}, _}, n) do
Process.sleep(100)
add_live_modules_to_tracer()
n
end
defp handle_trace({_, _, _, {Mix.Tasks.Compile.Elixir, _, _}, _}, n) do
n
end
defp handle_trace({_, _, _, {Mix.Tasks.Compile.Elixir, _, _}, _, _}, n) do
n
end
# This handler is heavy because of fetching state and we do not care for order because it is not displayed to user
# Because of that we do it asynchronously to speed up tracer a bit
# We do not persist this trace because it is not displayed to user
defp handle_trace(
{_, pid, _, {Phoenix.LiveView.Diff, :delete_component, [cid | _] = args}, timestamp},
n
) do
Task.start(fn ->
with cid <- %Phoenix.LiveComponent.CID{cid: cid},
{:ok, %{socket: socket}} <- ChannelService.state(pid),
%{id: socket_id, transport_pid: transport_pid} <- socket,
true <- is_pid(transport_pid),
trace <-
Trace.new(
n,
Phoenix.LiveView.Diff,
:delete_component,
args,
pid,
timestamp,
socket_id: socket_id,
transport_pid: transport_pid,
cid: cid
) do
publish_trace(trace)
end
end)
n
end
# This handles callbacks created by user that will be displayed to user
# It cannot be async because we care about order
defp handle_trace({_, pid, :call, {module, fun, args}, timestamp}, n)
when fun in @callback_functions do
with trace <- Trace.new(n, module, fun, args, pid, timestamp),
true <- is_pid(trace.transport_pid),
:ok <- persist_trace(trace) do
:erlang.put({pid, module, fun}, {timestamp, trace})
publish_trace(trace)
end
n - 1
end
defp handle_trace({_, pid, :return_from, {module, fun, _arity}, _, return_ts}, n)
when fun in @callback_functions do
with {call_ts, trace} <- :erlang.get({pid, module, fun}),
execution_time <- :timer.now_diff(return_ts, call_ts),
trace <- %{trace | execution_time: execution_time},
:ok <- persist_trace(trace) do
:erlang.erase({pid, module, fun})
publish_update_trace(trace)
end
n
end
defp handle_trace({_, _pid, :exception_from, {_module, fun, _}, _, _timestamp}, n)
when fun in @callback_functions do
n
end
defp handle_trace(trace, n) do
Logger.info("Ignoring unexpected trace: #{inspect(trace)}")
n
end
defp add_live_modules_to_tracer() do
all_modules = ModuleDiscoveryService.all_modules()
callbacks =
all_modules
|> ModuleDiscoveryService.live_view_modules()
|> CallbackUtils.live_view_callbacks()
all_modules
|> ModuleDiscoveryService.live_component_modules()
|> CallbackUtils.live_component_callbacks()
|> Enum.concat(callbacks)
|> Enum.each(fn mfa ->
Dbg.tp(mfa, [{:_, [], [{:return_trace}]}])
Dbg.tp(mfa, [{:_, [], [{:exception_trace}]}])
end)
end
@spec persist_trace(Trace.t()) :: :ok | {:error, term()}
defp persist_trace(%Trace{} = trace) do
TraceService.insert(trace)
:ok
rescue
err ->
Logger.error("Error while persisting trace: #{inspect(err)}")
{:error, err}
end
@spec publish_trace(Trace.t()) :: :ok | {:error, term()}
defp publish_trace(%Trace{} = trace) do
do_publish(trace)
:ok
rescue
err ->
Logger.error("Error while publishing trace: #{inspect(err)}")
{:error, err}
end
@spec publish_update_trace(Trace.t()) :: :ok | {:error, term()}
defp publish_update_trace(%Trace{} = trace) do
do_publish_update(trace)
:ok
rescue
err ->
Logger.error("Error while publishing trace: #{inspect(err)}")
{:error, err}
end
@spec do_publish(Trace.t()) :: :ok
defp do_publish(%{module: Phoenix.LiveView.Diff} = trace) do
PubSubUtils.component_deleted_topic()
|> PubSubUtils.broadcast({:component_deleted, trace})
end
defp do_publish(%Trace{} = trace) do
pid = trace.pid
node_id = Trace.node_id(trace)
fun = trace.function
pid
|> PubSubUtils.trace_topic_per_node(node_id, fun, :call)
|> PubSubUtils.broadcast({:new_trace, trace})
end
@spec do_publish_update(Trace.t()) :: :ok
defp do_publish_update(trace) do
pid = trace.pid
node_id = Trace.node_id(trace)
fun = trace.function
if fun == :render do
PubSubUtils.node_rendered_topic()
|> PubSubUtils.broadcast({:render_trace, trace})
end
pid
|> PubSubUtils.trace_topic_per_node(node_id, fun, :return)
|> PubSubUtils.broadcast({:updated_trace, trace})
end
end