1. 首先必须要在compile里面加上debug_info信息:
2. 从beam中通过beam_lib模块取出recod信息或源代码:
-module(beamI).
%% API
-export([record_attrs/1,beam_to_source/1]).
%%得到record信息[{RecName1,[Filed11,Filed12]},{RecName2,[Filed21,Filed22]}]
record_attrs(Beam) ->
{ok,{_Mod,[{abstract_code,{_Version,Forms}},{"CInf",_CB}]}} =
beam_lib:chunks(code:which(Beam), [abstract_code,"CInf"]),
[join_name_field(D) || {attribute,_,record,D} <- Forms].
%%得到源代码Beam.erl内容:
beam_to_source(Beam) ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(code:which(Beam),[abstract_code]),
io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
join_name_field({Name, Fs}) -> {Name, [element(3, element(3,F)) || F <- Fs]}.
3. 测试:得到record和源文件:
3> beamI:record_attrs(mnesia).
[{tid,[counter,pid]},
{tidstore,[store,up_stores,level]},
{cstruct,[name,type,ram_copies,disc_copies,disc_only_copies,
load_order,access_mode,majority,index,snmp,local_content,
record_name,attributes,user_properties,frag_properties,
storage_properties,cookie,version]},
{log_header,[log_kind,log_version,mnesia_version,node,now]},
{commit,[node,decision,ram_copies,disc_copies,
disc_only_copies,snmp,schema_ops]},
{decision,[tid,outcome,disc_nodes,ram_nodes]},
{cyclic,[node,oid,op,lock,lucky]},
{mnesia_select,[tab,tid,node,storage,cont,written,spec,type,
orig]}]
4> beamI:beam_to_source(load).
-file("load.erl", 1).
-module(load).
-behaviour(gen_server).
-vsn("1.0").
-export([multi_start/0, multi_start/1, start/0, start/1,
start_link/0, start_link/1]).
-export([stop/0, multi_stop/0]).
.......
--------------------------------------------------------------------------------------------------------------------------------------