|
| 1 | +defmodule ElixirLS.Debugger.BreakpointCondition do |
| 2 | + @moduledoc """ |
| 3 | + Server that tracks breakpoint conditions |
| 4 | + """ |
| 5 | + |
| 6 | + use GenServer |
| 7 | + @range 0..99 |
| 8 | + |
| 9 | + def start_link(args) do |
| 10 | + GenServer.start_link(__MODULE__, Keyword.delete(args, :name), |
| 11 | + name: Keyword.get(args, :name, __MODULE__) |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + def register_condition(name \\ __MODULE__, module, lines, condition) do |
| 16 | + GenServer.call(name, {:register_condition, {module, lines}, condition}) |
| 17 | + end |
| 18 | + |
| 19 | + def unregister_condition(name \\ __MODULE__, module, lines) do |
| 20 | + GenServer.cast(name, {:unregister_condition, {module, lines}}) |
| 21 | + end |
| 22 | + |
| 23 | + def has_condition?(name \\ __MODULE__, module, lines) do |
| 24 | + GenServer.call(name, {:has_condition?, {module, lines}}) |
| 25 | + end |
| 26 | + |
| 27 | + def get_condition(name \\ __MODULE__, number) do |
| 28 | + GenServer.call(name, {:get_condition, number}) |
| 29 | + end |
| 30 | + |
| 31 | + @impl GenServer |
| 32 | + def init(_args) do |
| 33 | + {:ok, |
| 34 | + %{ |
| 35 | + free: @range |> Enum.map(& &1), |
| 36 | + conditions: %{} |
| 37 | + }} |
| 38 | + end |
| 39 | + |
| 40 | + @impl GenServer |
| 41 | + def handle_call( |
| 42 | + {:register_condition, key, condition}, |
| 43 | + _from, |
| 44 | + %{free: free, conditions: conditions} = state |
| 45 | + ) do |
| 46 | + case conditions[key] do |
| 47 | + nil -> |
| 48 | + case free do |
| 49 | + [] -> |
| 50 | + {:reply, {:error, :limit_reached}, state} |
| 51 | + |
| 52 | + [number | rest] -> |
| 53 | + state = %{ |
| 54 | + state |
| 55 | + | free: rest, |
| 56 | + conditions: conditions |> Map.put(key, {number, condition}) |
| 57 | + } |
| 58 | + |
| 59 | + {:reply, {:ok, {__MODULE__, :"check_#{number}"}}, state} |
| 60 | + end |
| 61 | + |
| 62 | + {number, _old_condition} -> |
| 63 | + state = %{state | conditions: conditions |> Map.put(key, {number, condition})} |
| 64 | + {:reply, {:ok, {__MODULE__, :"check_#{number}"}}, state} |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def handle_call({:has_condition?, key}, _from, %{conditions: conditions} = state) do |
| 69 | + {:reply, Map.has_key?(conditions, key), state} |
| 70 | + end |
| 71 | + |
| 72 | + def handle_call({:get_condition, number}, _from, %{conditions: conditions} = state) do |
| 73 | + condition = conditions |> Map.values() |> Enum.find(fn {n, _c} -> n == number end) |> elem(1) |
| 74 | + {:reply, condition, state} |
| 75 | + end |
| 76 | + |
| 77 | + @impl GenServer |
| 78 | + def handle_cast({:unregister_condition, key}, %{free: free, conditions: conditions} = state) do |
| 79 | + state = |
| 80 | + case Map.pop(conditions, key) do |
| 81 | + {{number, _}, conditions} -> |
| 82 | + %{state | free: [number | free], conditions: conditions} |
| 83 | + |
| 84 | + {nil, _} -> |
| 85 | + state |
| 86 | + end |
| 87 | + |
| 88 | + {:noreply, state} |
| 89 | + end |
| 90 | + |
| 91 | + # `:int` module supports setting breakpoint conditions in the form `{module, function}` |
| 92 | + # we need a way of dynamically generating such pairs and assigning conditions that they will evaluate |
| 93 | + # an arbitrary limit of 100 conditions was chosen |
| 94 | + for i <- @range do |
| 95 | + @spec unquote(:"check_#{i}")(term) :: boolean |
| 96 | + def unquote(:"check_#{i}")(binding) do |
| 97 | + condition = get_condition(unquote(i)) |
| 98 | + eval_condition(condition, binding) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def eval_condition(condition, binding) do |
| 103 | + elixir_binding = binding |> ElixirLS.Debugger.Binding.to_elixir_variable_names() |
| 104 | + |
| 105 | + try do |
| 106 | + {term, _bindings} = Code.eval_string(condition, elixir_binding) |
| 107 | + if term, do: true, else: false |
| 108 | + catch |
| 109 | + kind, error -> |
| 110 | + IO.warn("Error in conditional breakpoint: " <> Exception.format_banner(kind, error)) |
| 111 | + false |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments