1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| -module(my_decompile). -compile(export_all).
decompile() -> {ok, Tmp} = file:list_dir("."), %% 自己这个文件my_decompile没有加DEBUG_INFO反编译不了,筛选掉 BeamFiles = [list_to_atom(filename:rootname(X)) || X <- Tmp, (filename:extension(X) == ".beam" andalso filename:rootname(X) =/= "my_decompile")], %% BeamFiles是beam文件原子列表 sources(BeamFiles, []).
sources([], Result) -> Result; sources([Module | T], Result) -> NResult = case check_source(Module) of ok -> Result; _ -> [Module | Result] end, sources(T, NResult).
check_source(Module) -> case catch source(Module) of ok -> ok; _E -> io:format("ERR:~p end~n",[_E]), null end.
source(Module) -> Path = code:which(Module), {ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks(Path, [abstract_code]), {ok, S} = file:open(atom_to_list(Module) ++ ".erl", write), io:format(S, "~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]), file:close(S), ok.
|