Skip to content

Commit 37c7019

Browse files
committed
return OTP 27 process labels in debug adapter threads response
1 parent 505a50f commit 37c7019

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

apps/debug_adapter/lib/debug_adapter/server.ex

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,13 +2278,27 @@ defmodule ElixirLS.DebugAdapter.Server do
22782278
end
22792279

22802280
defp process_name(process_info) do
2281-
registered_name = Keyword.get(process_info, :registered_name)
2281+
# if present registered_name is na atom
2282+
registered_name = Keyword.fetch(process_info, :registered_name)
2283+
# OTP 27+ process label may be any term
2284+
# it's not documented but :proc_lib.get_label reads `:$process_label` key from process dictionary
2285+
# https://github.com/erlang/otp/blob/601a012837ea0a5c8095bf24223132824177124d/lib/stdlib/src/proc_lib.erl#L876
2286+
# we have already read it so can get it directly
2287+
label = process_info |> Keyword.fetch!(:dictionary) |> Keyword.fetch(:"$process_label")
22822288

2283-
if registered_name do
2284-
inspect(registered_name)
2285-
else
2286-
{mod, func, arity} = Keyword.fetch!(process_info, :initial_call)
2287-
"#{inspect(mod)}.#{to_string(func)}/#{arity}"
2289+
case {registered_name, label} do
2290+
{{:ok, registered_name}, {:ok, label}} ->
2291+
"#{registered_name}: #{inspect(label)}"
2292+
2293+
{{:ok, registered_name}, _} ->
2294+
to_string(registered_name)
2295+
2296+
{_, {:ok, label}} ->
2297+
inspect(label)
2298+
2299+
_ ->
2300+
{mod, func, arity} = Keyword.fetch!(process_info, :initial_call)
2301+
"#{inspect(mod)}.#{to_string(func)}/#{arity}"
22882302
end
22892303
end
22902304

apps/debug_adapter/test/debugger_test.exs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,78 @@ defmodule ElixirLS.DebugAdapter.ServerTest do
33033303
end)
33043304
end
33053305

3306+
if System.otp_release() |> String.to_integer() >= 27 do
3307+
@tag :fixture
3308+
test "returns process label", %{server: server} do
3309+
in_fixture(__DIR__, "mix_project", fn ->
3310+
Server.receive_packet(server, initialize_req(1, %{}))
3311+
3312+
assert_receive(
3313+
response(_, 1, "initialize", %{"supportsConfigurationDoneRequest" => true})
3314+
)
3315+
3316+
Server.receive_packet(
3317+
server,
3318+
launch_req(2, %{
3319+
"request" => "launch",
3320+
"type" => "mix_task",
3321+
"task" => "run",
3322+
"taskArgs" => ["-e", "MixProject.Some.sleep()"],
3323+
"projectDir" => File.cwd!()
3324+
})
3325+
)
3326+
3327+
assert_receive(response(_, 2, "launch", %{}), 5000)
3328+
assert_receive(event(_, "initialized", %{}))
3329+
3330+
Server.receive_packet(server, request(5, "configurationDone", %{}))
3331+
assert_receive(response(_, 5, "configurationDone", %{}))
3332+
Process.sleep(1000)
3333+
3334+
{:ok, pid} =
3335+
Task.start(fn ->
3336+
:proc_lib.set_label("foo")
3337+
3338+
receive do
3339+
:done -> :ok
3340+
end
3341+
end)
3342+
3343+
Process.monitor(pid)
3344+
3345+
send(server, :update_threads)
3346+
state = :sys.get_state(server)
3347+
3348+
thread_id = state.pids_to_thread_ids[pid]
3349+
assert thread_id
3350+
assert state.thread_ids_to_pids[thread_id] == pid
3351+
3352+
Server.receive_packet(server, request(6, "threads", %{}))
3353+
assert_receive(response(_, 6, "threads", %{"threads" => threads}), 1_000)
3354+
3355+
assert Enum.find(threads, &(&1["id"] == thread_id))["name"] ==
3356+
"\"foo\" #{:erlang.pid_to_list(pid)}"
3357+
3358+
send(pid, :done)
3359+
3360+
receive do
3361+
{:DOWN, _, _, ^pid, _} -> :ok
3362+
end
3363+
3364+
send(server, :update_threads)
3365+
state = :sys.get_state(server)
3366+
3367+
refute Map.has_key?(state.pids_to_thread_ids, pid)
3368+
refute Map.has_key?(state.thread_ids_to_pids, thread_id)
3369+
3370+
Server.receive_packet(server, request(6, "threads", %{}))
3371+
assert_receive(response(_, 6, "threads", %{"threads" => threads}), 1_000)
3372+
3373+
refute Enum.find(threads, &(&1["id"] == thread_id))
3374+
end)
3375+
end
3376+
end
3377+
33063378
describe "evaluate" do
33073379
defp gen_watch_expression_packet(seq, expr) do
33083380
%{

0 commit comments

Comments
 (0)