Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 46 additions & 34 deletions apps/debug_adapter/lib/debug_adapter/breakpoint_condition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do
end

@spec get_condition(module, non_neg_integer) ::
{Macro.Env.t(), String.t(), String.t() | nil, String.t(), non_neg_integer}
{Macro.Env.t(), String.t(), String.t() | nil, String.t(), non_neg_integer} | nil
def get_condition(name \\ __MODULE__, number) do
GenServer.call(name, {:get_condition, number})
end
Expand Down Expand Up @@ -142,11 +142,16 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do
end

def handle_call({:get_condition, number}, _from, %{conditions: conditions, hits: hits} = state) do
{env, condition, log_message, hit_count} =
conditions |> Map.values() |> Enum.find(fn {n, _c} -> n == number end) |> elem(1)
case conditions |> Map.values() |> Enum.find(fn {n, _c} -> n == number end) do
nil ->
# the number may have been freed by unregister_condition/clear while
# the interpreted code still calls the check function
{:reply, nil, state}

hits = hits |> Map.get(number, 0)
{:reply, {env, condition, log_message, hit_count, hits}, state}
{_n, {env, condition, log_message, hit_count}} ->
hits = hits |> Map.get(number, 0)
{:reply, {env, condition, log_message, hit_count, hits}, state}
end
end

def handle_call(:clear, _from, _state) do
Expand Down Expand Up @@ -187,35 +192,42 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do
for i <- @range do
@spec unquote(:"check_#{i}")(term) :: boolean
def unquote(:"check_#{i}")(binding) do
{env, condition, log_message, hit_count_condition, hits} = get_condition(unquote(i))
elixir_binding = binding |> ElixirLS.DebugAdapter.Binding.to_elixir_variable_names()
result = eval_condition(condition, elixir_binding, env)

result =
if result do
register_hit(unquote(i))
# do not break if hit count not reached
# the spec requires:
# If both this property and `condition` are specified, `hitCondition` should
# be evaluated only if the `condition` is met, and the debug adapter should
# stop only if both conditions are met.

hit_count = eval_hit_condition(hit_count_condition, elixir_binding, env)
hits + 1 >= hit_count
else
result
end

if result and log_message != nil do
# Debug Adapter Protocol:
# If this attribute exists and is non-empty, the backend must not 'break' (stop)
# but log the message instead. Expressions within {} are interpolated.
# If either `hitCondition` or `condition` is specified, then the message
# should only be logged if those conditions are met.
Output.debugger_console(interpolate(log_message, elixir_binding, env))
false
else
result
case get_condition(unquote(i)) do
# breakpoint was removed or replaced after the code was instrumented
nil ->
false

{env, condition, log_message, hit_count_condition, hits} ->
elixir_binding = binding |> ElixirLS.DebugAdapter.Binding.to_elixir_variable_names()
result = eval_condition(condition, elixir_binding, env)

result =
if result do
register_hit(unquote(i))

# do not break if hit count not reached
# the spec requires:
# If both this property and `condition` are specified, `hitCondition` should
# be evaluated only if the `condition` is met, and the debug adapter should
# stop only if both conditions are met.

hit_count = eval_hit_condition(hit_count_condition, elixir_binding, env)
hits + 1 >= hit_count
else
result
end

if result and log_message != nil do
# Debug Adapter Protocol:
# If this attribute exists and is non-empty, the backend must not 'break' (stop)
# but log the message instead. Expressions within {} are interpolated.
# If either `hitCondition` or `condition` is specified, then the message
# should only be logged if those conditions are met.
Output.debugger_console(interpolate(log_message, elixir_binding, env))
false
else
result
end
end
end
end
Expand Down
66 changes: 66 additions & 0 deletions apps/debug_adapter/test/breakpoint_condition_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,72 @@ defmodule ElixirLS.DebugAdapter.BreakpointConditionTest do
assert {_, "c == d", "xxx", "2", 1} = BreakpointCondition.get_condition(@name, 1)
end

test "get_condition for an unregistered number returns nil and keeps the server alive" do
pid = Process.whereis(@name)

assert BreakpointCondition.get_condition(@name, 5) == nil

assert {:ok, {BreakpointCondition, :check_0}} ==
BreakpointCondition.register_condition(@name, Some, 123, __ENV__, "a == b", nil, "1")

assert :ok == BreakpointCondition.unregister_condition(@name, Some, 123)
assert BreakpointCondition.get_condition(@name, 0) == nil

assert :ok == BreakpointCondition.clear(@name)
assert BreakpointCondition.get_condition(@name, 0) == nil

assert Process.alive?(pid)
assert Process.whereis(@name) == pid
end

test "check functions return false for an unregistered number and keep the server alive" do
start_supervised!(
Supervisor.child_spec({BreakpointCondition, name: BreakpointCondition},
id: :default_breakpoint_condition
)
)

pid = Process.whereis(BreakpointCondition)

# never registered
refute apply(BreakpointCondition, :check_3, [[]])

# registered, then removed while the instrumented code still calls it
{:ok, {BreakpointCondition, check_fun}} =
BreakpointCondition.register_condition(
BreakpointCondition,
Some,
201,
__ENV__,
"true",
nil,
"0"
)

assert apply(BreakpointCondition, check_fun, [[]])

:ok = BreakpointCondition.unregister_condition(BreakpointCondition, Some, 201)
refute apply(BreakpointCondition, check_fun, [[]])

# cleared after registration
{:ok, {BreakpointCondition, check_fun}} =
BreakpointCondition.register_condition(
BreakpointCondition,
Some,
202,
__ENV__,
"true",
nil,
"0"
)

:ok = BreakpointCondition.clear(BreakpointCondition)
refute apply(BreakpointCondition, check_fun, [[]])

assert Process.alive?(pid)
assert Process.whereis(BreakpointCondition) == pid
end

test "register_hit" do
assert {:ok, {BreakpointCondition, :check_0}} ==
BreakpointCondition.register_condition(@name, Some, 123, __ENV__, "a == b", nil, "1")
Expand Down
Loading