Decompile beam to erlang

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Sat Jan 28 15:03:41 CET 2006


Matthias Lang wrote:
> Writing a decompiler which can turn the above example
> back to source is a fifteen minute job. Writing a
> decompiler which handles more complex Erlang code
> is more time consuming, but not much harder.

Here's a pretty generic way to convert abstract
forms to .erl:


-module(abst_to_src).
-export([transform/2]).

transform(BeamFName, ErlFName) ->
  case beam_lib:chunks(BeamFName, [abstract_code]) of
    {ok, {_, [{abstract_code, {raw_abstract_v1,Forms}}]}} ->
      Src = 
        erl_prettypr:format(erl_syntax:form_list(tl(Forms))),
        {ok, Fd} = file:open(ErlFName, [write]),
        io:fwrite(Fd, "~s~n", [Src]),
        file:close(Fd); 
    Error ->
       Error
  end.

(It _did_ take exactly 15 minutes, 
including:
- just trying to hack it from memory
- reading the docs, waiting two minutes for 
  the erlang.org webserver to respond
- fetching the docs from local disk
- reading some more, giving up and then 
  searching my mail archive for an old 
  mail to Luke Gorrie, where I had included
  the above code
- Copy-paste, compiling, running and 
  verifying the results... :)

/Uffe



More information about the erlang-questions mailing list