Skip to content

Commit

Permalink
Store abstract code in the Dbgi chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Mar 8, 2017
1 parent b1b8a8d commit d373417
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ format_error_reason(Reason) ->
ofile="" :: file:filename(),
module=[] :: module() | [],
core_code=[] :: cerl:c_module() | [],
abstract_code=[] :: binary() | [], %Abstract code for debugger.
abstract_code=[] :: abstract_code(), %Abstract code for debugger.
options=[] :: [option()], %Options for compilation
mod_options=[] :: [option()], %Options for module_info
encoding=none :: none | epp:source_encoding(),
Expand Down Expand Up @@ -1321,44 +1321,42 @@ core_inline_module(Code0, #compile{options=Opts}=St) ->
Code = cerl_inline:core_transform(Code0, Opts),
{ok,Code,St}.

save_abstract_code(Code, #compile{ifile=File}=St) ->
case abstract_code(Code, St) of
{ok,Abstr} ->
{ok,Code,St#compile{abstract_code=Abstr}};
{error,Es} ->
{error,St#compile{errors=St#compile.errors ++ [{File,Es}]}}
end.
save_abstract_code(Code, St) ->
{ok,Code,St#compile{abstract_code=erl_parse:anno_to_term(Code)}}.

abstract_code(Code0, #compile{options=Opts,ofile=OFile}) ->
Code = erl_parse:anno_to_term(Code0),
Abstr = erlang:term_to_binary({raw_abstract_v1,Code}, [compressed]),
case member(encrypt_debug_info, Opts) of
debug_info(#compile{module=Module,mod_options=Opts0,ofile=OFile,abstract_code=Abst}) ->
Opts1 = proplists:delete(debug_info, Opts0),
{Backend,Metadata,Opts2} =
case proplists:get_value(debug_info, Opts0, false) of
{OptBackend,OptMetadata} when is_atom(OptBackend) -> {OptBackend,OptMetadata,Opts1};
false -> {erlang_abstract_format,none,Opts1};
true -> {erlang_abstract_format,Abst,[debug_info | Opts1]}
end,
DebugInfo = erlang:term_to_binary({debug_info_v1,Backend,Metadata}, [compressed]),

case member(encrypt_debug_info, Opts2) of
true ->
case keyfind(debug_info_key, 1, Opts) of
{_,Key} ->
encrypt_abs_code(Abstr, Key);
case lists:keytake(debug_info_key, 1, Opts2) of
{value,{_, Key},Opts3} ->
encrypt_debug_info(DebugInfo, Key, [{debug_info_key,'********'} | Opts3]);
false ->
%% Note: #compile.module has not been set yet.
%% Here is an approximation that should work for
%% all valid cases.
Module = list_to_atom(filename:rootname(filename:basename(OFile))),
Mode = proplists:get_value(crypto_mode, Opts, des3_cbc),
Mode = proplists:get_value(crypto_mode, Opts2, des3_cbc),
case beam_lib:get_crypto_key({debug_info, Mode, Module, OFile}) of
error ->
{error, [{none,?MODULE,no_crypto_key}]};
Key ->
encrypt_abs_code(Abstr, {Mode, Key})
encrypt_debug_info(DebugInfo, {Mode, Key}, Opts2)
end
end;
false ->
{ok,Abstr}
{ok,DebugInfo,Opts2}
end.

encrypt_abs_code(Abstr, Key0) ->
encrypt_debug_info(DebugInfo, Key, Opts) ->
try
RealKey = generate_key(Key0),
RealKey = generate_key(Key),
case start_crypto() of
ok -> {ok,encrypt(RealKey, Abstr)};
ok -> {ok,encrypt(RealKey, DebugInfo),Opts};
{error,_}=E -> E
end
catch
Expand Down Expand Up @@ -1392,16 +1390,16 @@ encrypt({des3_cbc=Type,Key,IVec,BlockSize}, Bin0) ->
save_core_code(Code, St) ->
{ok,Code,St#compile{core_code=cerl:from_records(Code)}}.

beam_asm(Code0, #compile{ifile=File,abstract_code=Abst,extra_chunks=ExtraChunks,
options=CompilerOpts,mod_options=Opts0}=St) ->
Source = paranoid_absname(File),
Opts1 = lists:map(fun({debug_info_key,_}) -> {debug_info_key,'********'};
(Other) -> Other
end, Opts0),
Opts2 = [O || O <- Opts1, effects_code_generation(O)],
Chunks = [{<<"Abst">>, Abst} | ExtraChunks],
case beam_asm:module(Code0, Chunks, Source, Opts2, CompilerOpts) of
{ok,Code} -> {ok,Code,St#compile{abstract_code=[]}}
beam_asm(Code0, #compile{ifile=File,extra_chunks=ExtraChunks,options=CompilerOpts}=St) ->
case debug_info(St) of
{ok,DebugInfo,Opts0} ->
Source = paranoid_absname(File),
Opts1 = [O || O <- Opts0, effects_code_generation(O)],
Chunks = [{<<"Dbgi">>, DebugInfo} | ExtraChunks],
{ok,Code} = beam_asm:module(Code0, Chunks, Source, Opts1, CompilerOpts),
{ok,Code,St#compile{abstract_code=[]}};
{error,Es} ->
{error,St#compile{errors=St#compile.errors ++ [{File,Es}]}}
end.

paranoid_absname(""=File) ->
Expand Down Expand Up @@ -1485,7 +1483,7 @@ embed_native_code(Code, {Architecture,NativeCode}) ->
%% errors will be reported).

effects_code_generation(Option) ->
case Option of
case Option of
beam -> false;
report_warnings -> false;
report_errors -> false;
Expand Down

0 comments on commit d373417

Please sign in to comment.