Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beam_debug_info: Emit a proper entry debug_line for funs #9602

Merged
merged 1 commit into from
Mar 21, 2025
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
8 changes: 7 additions & 1 deletion lib/compiler/src/beam_asm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ build_bdi([{FrameSize0,Vars0}|Items], Dict0) ->
end,
Vars1 = case FrameSize0 of
entry ->
[[Name,Reg] || {Name,[Reg]} <:- Vars0];
[[bdi_name_to_term(Name),Reg] ||
{Name,[Reg]} <:- Vars0];
_ ->
[[{literal,atom_to_binary(Name)},last(Regs)] ||
{Name,[_|_]=Regs} <:- Vars0]
Expand All @@ -490,6 +491,11 @@ build_bdi([{FrameSize0,Vars0}|Items], Dict0) ->
build_bdi([], Dict) ->
{[],Dict}.

bdi_name_to_term(Int) when is_integer(Int) ->
{integer,Int};
bdi_name_to_term(Atom) when is_atom(Atom) ->
{literal,atom_to_binary(Atom)}.

%%%
%%% Functions for assembling BEAM instruction.
%%%
Expand Down
9 changes: 6 additions & 3 deletions lib/compiler/src/beam_core_to_ssa.erl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ expr(#c_binary{anno=A,segments=Cv}, Sub, St0) ->
Error = #c_call{anno=A,module=Erl,name=Name,args=Args},
expr(Error, Sub, St1)
end;
expr(#c_fun{anno=A,vars=Cvs,body=Cb}, Sub0, #kern{fargs=OldFargs}=St0) ->
expr(#c_fun{anno=A,vars=Cvs}=Fun, Sub0, #kern{fargs=OldFargs}=St0) ->
FilteredAnno = [Item || {debug_line,_}=Item <- A],
#c_fun{body=Cb} = handle_debug_line(FilteredAnno, Fun),
{Kvs,Sub1,St1} = pattern_list(Cvs, Sub0, St0),
{Kb,Pb,St2} = body(Cb, Sub1, St1#kern{fargs=Kvs}),
{#ifun{anno=A,vars=Kvs,body=pre_seq(Pb, Kb)},[],St2#kern{fargs=OldFargs}};
Expand Down Expand Up @@ -435,12 +437,13 @@ primop_succeeded(Op, Anno0, Args) ->
letrec_local_function(A, Cfs, Cb, Sub0, St0) ->
%% Make new function names and store substitution.
{Fs0,{Sub1,St1}} =
mapfoldl(fun ({#c_var{name={F,Ar}},#c_fun{}=B0}, {Sub,S0}) ->
mapfoldl(fun ({#c_var{name={F,Ar}},#c_fun{anno=Anno0}=B0}, {Sub,S0}) ->
{N,St1} = new_fun_name(atom_to_list(F)
++ "/" ++
integer_to_list(Ar),
S0),
B = B0#c_fun{anno=[{letrec_name,N}]},
Anno = [{letrec_name,N} | [Dbg || {debug_line,_}=Dbg <- Anno0]],
B = B0#c_fun{anno=Anno},
{{N,B},{set_fsub(F, Ar, N, Sub),St1}}
end, {Sub0,St0}, Cfs),
%% Run translation on functions and body.
Expand Down
11 changes: 9 additions & 2 deletions lib/compiler/src/beam_ssa_codegen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ fix_debug_line(Is0, Live, #cg{debug_info=true}) ->
{line,_}=Li,
{func_info,_,_,_}=Fi,
{label,_}=Entry,
{debug_line,Location,Index,Live,{none,[]}}|Is] ->
{debug_line,Location,Index,Live,{none,Args0}}|Is] ->
%% Mark this debug_line instruction as being the
%% very first instruction in the function.
Args = [{{integer,I}, [{x,I-1}]} || I <- lists:seq(1, Live)],
RegToVar = #{Reg => Var || {Var,[{x,_}=Reg|_]} <- Args0},
Args = [begin
X = {x,I-1},
case RegToVar of
#{X := Var} -> {Var,[X]};
#{} -> {I,[X]}
end
end || I <- lists:seq(1, Live)],
DbgLine = {debug_line,Location,Index,Live,{entry,Args}},
[FiLbl,Li,Fi,Entry,DbgLine|Is];
_ ->
Expand Down
19 changes: 17 additions & 2 deletions lib/compiler/src/sys_coverage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,10 @@ munge_expr({'maybe',MaybeAnno,Exprs,{'else',ElseAnno,Clauses}}, Vars0) ->
{MungedClauses, Vars2} = munge_clauses(Clauses, Vars1),
{{'maybe',MaybeAnno,MungedExprs,{'else',ElseAnno,MungedClauses}}, Vars2};
munge_expr({'fun',Anno,{clauses,Clauses}}, Vars0) ->
{MungedClauses,Vars1} = munge_clauses(Clauses, Vars0),
{MungedClauses, Vars1} = munge_fun(Anno, Clauses, Vars0),
{{'fun',Anno,{clauses,MungedClauses}}, Vars1};
munge_expr({named_fun,Anno,Name,Clauses}, Vars0) ->
{MungedClauses,Vars1} = munge_clauses(Clauses, Vars0),
{MungedClauses, Vars1} = munge_fun(Anno, Clauses, Vars0),
{{named_fun,Anno,Name,MungedClauses}, Vars1};
munge_expr({bin,Anno,BinElements}, Vars0) ->
{MungedBinElements,Vars1} = munge_exprs(BinElements, Vars0),
Expand All @@ -570,6 +570,21 @@ munge_expr({bin_element,Anno,Value,Size,TypeSpecifierList}, Vars0) ->
munge_expr(Form, Vars0) ->
{Form, Vars0}.

munge_fun(Anno, Clauses, #vars{bump_instr=debug_line}=Vars0) ->
[{clause,_,Args0,_,_}|_] = Clauses,
Arity = length(Args0),
Args = duplicate(Arity, {var,Anno,'_'}),
FakeBif = {remote,Anno,{atom,Anno,fake},{atom,Anno,is_beam_bif_info}},
Gs = [[{call,Anno,FakeBif,[]}]],
Bump = bump_call(Vars0, Anno),
Body = [Bump],
C = {clause,Anno,Args,Gs,Body},
{MungedClauses0,Vars1} = munge_clauses(Clauses, Vars0),
MungedClauses = [C|MungedClauses0],
{MungedClauses,Vars1};
munge_fun(_Anno, Clauses, Vars) ->
munge_clauses(Clauses, Vars).

munge_args(Args0, #vars{in_guard=false,bump_instr=debug_line}=Vars) ->
%% We want to have `debug_line` instructions inserted before each line in
%% this example:
Expand Down
9 changes: 5 additions & 4 deletions lib/compiler/src/v3_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1610,17 +1610,18 @@ make_combined(SegLine, Val, Size) ->
%% fun_tq(Id, [Clauses], Line, State, NameInfo) -> {Fun,[PreExp],State}.

fun_tq(Cs0, L, St0, NameInfo) ->
Arity = clause_arity(hd(Cs0)),
{Cs1,St1} = clauses(Cs0, St0),
{Cs1,Anno0} = handle_debug_line(Cs0, St0),
Arity = clause_arity(hd(Cs1)),
{Cs2,St1} = clauses(Cs1, St0),
{Args,St2} = new_vars(Arity, St1),
{Ps,St3} = new_vars(Arity, St2), %Need new variables here
Anno = full_anno(L, St3),
Anno = Anno0 ++ full_anno(L, St3),
{Name,St4} = new_fun_name(St3),
Fc = function_clause(Ps, Anno),
Id = {0,0,Name},
Fun = #ifun{anno=#a{anno=Anno},
id=[{id,Id}], %We KNOW!
vars=Args,clauses=Cs1,fc=Fc,name=NameInfo},
vars=Args,clauses=Cs2,fc=Fc,name=NameInfo},
{Fun,[],St4}.

%% lc_tq(Line, Exp, [Qualifier], Mc, State) -> {LetRec,[PreExp],State}.
Expand Down
8 changes: 3 additions & 5 deletions lib/compiler/test/beam_debug_info_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ extract_src_vars({named_fun,_,Name,Cs}, Lc, Acc0) ->
%% none of the variables defined in the fun should ever
%% show up in the debug info.
Acc0;
true when Name =/= '_' ->
true ->
Acc = case Name of
'_' -> Acc0;
_ -> extract_src_vars({var,anno,Name}, Lc, Acc0)
Expand Down Expand Up @@ -471,9 +471,7 @@ extract_guards([A|As], Acc) ->
extract_guards(As, extract_args(A, Acc));
extract_guards([], Acc) -> Acc.

extract_sv_qs([{block,BlkL,[{executable_line,_,_}|Bs]}|Qs1]) ->
%% Note: `debug_line` instructions are `executable_line`
%% instructions in the abstract code.
extract_sv_qs([{block,BlkL,[{debug_line,_,_}|Bs]}|Qs1]) ->
[{block,BlkL,Bs}|extract_sv_qs_1(Qs1)];
extract_sv_qs(Qs) -> Qs.

Expand Down Expand Up @@ -770,7 +768,7 @@ missing_vars(Config) ->
end || {debug_line,Anno,_,_,{FrameSz,Vars}} <- Is],
DebugLines = lists:sort(DebugLines0),
io:format("~p\n", [DebugLines]),
Expected = [{3,entry,[{integer,1},{integer,2},{integer,3}]},
Expected = [{3,entry,[1,2,3]},
{4,none,['X','Y','Z0']},
{6,none,['X','Y','Z0']},
{7,none,['X','Z0','Z1']},
Expand Down
Loading