反编译函数
beam_lib:chunks(BeamPath, [abstract_code]).
test_decompile.erl
-module(test_decompile).
-export([decompile/1]).
decompile(Beam) ->
case beam_lib:chunks(code:which(Beam), [abstract_code]) of
{ok, {_, [{abstract_code, {_, AC}}]}} ->
io:format("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]);
Other ->
Other
end.
test.erl
-module(test).
-compile(export_all).
-record(kv, {key, val}).
list() ->
[1, 2, 3].
tuple() ->
{a, b, c}.
string() ->
"test".
atom() ->
hello.
record() ->
#kv{key = 1}.
binary() ->
<<"aabbcc">>.
示例:
1> c(test).
{ok,test}
2> c(test_decompile).
{ok,test_decompile}
3> test_decompile:decompile(test).
{ok,{test,[{abstract_code,no_abstract_code}]}}
注意:要想反编译就必须在编译的时候加上+debug_info标记
[root@localhost]# erlc +debug_info test.erl
[root@localhost]# erl
Eshell V8.3 (abort with ^G)
1> test_decompile:decompile(test).
-file("test.erl", 1).
-module(test).
-compile(export_all).
-record(kv, {key, val}).
list() -> [1, 2, 3].
tuple() -> {a, b, c}.
string() -> "test".
atom() -> hello.
record() -> #kv{key = 1}.
binary() -> <<"aabbcc">>.
ok
697

被折叠的 条评论
为什么被折叠?



