Decompile beam to erlang

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Decompile beam to erlang

Chaitanya Chalasani
Dear List,

I have a very serious issue, We lost all the source code of our erlang based
project, we only have the beam files. Is there any way i can retrive the
erlang files beam files.

Please help me out.

--
Chaitanya Chalasani
Reply | Threaded
Open this post in threaded view
|

RE: Decompile beam to erlang

Lennart Öhman-2
Hi, sorry to hear that.
I would say your chance is that you have debug_info compiled
your code, making the abstract-format available inside the
beam.

Best Regards,
Lennart

-------------------------------------------------------------
Lennart Ohman                   office  : +46-8-587 623 27
Sjoland & Thyselius Telecom AB  cellular: +46-70-552 67 35
Sehlstedtsgatan 6               fax     : +46-8-667 82 30
SE-115 28, STOCKHOLM, SWEDEN    email   : [hidden email]
-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Chaitanya Chalasani
Sent: Friday, January 27, 2006 2:10 PM
To: [hidden email]
Subject: Decompile beam to erlang

Dear List,

I have a very serious issue, We lost all the source code of our erlang based

project, we only have the beam files. Is there any way i can retrive the
erlang files beam files.

Please help me out.

--
Chaitanya Chalasani


Reply | Threaded
Open this post in threaded view
|

RE: Decompile beam to erlang

Matthias Lang

 > Is there any way i can retrive the erlang files beam files.

This question seems to come up every so often. Figured I'd write an
answer to put into the FAQ next time I update it. Improvements welcome.

Matthias

----------------------------------------------------------------------

Q: Can I re-create .erl files using just the .beam files?

A: _If_ the code was compiled with the debug_info flag,
   then the .beam file contains a 'partially compiled'
   representation of the source---basically the parse tree.
   Here is a simple module:

     -module(hw).
     -export([go/0]).

     go() when true ->
       "this is my function".

   and the corresponding abstract code:

     3> {ok, {hw, [{abstract_code, Abs}]}} =  beam_lib:chunks("hw.beam", [abstract_code]), Abs.
         {raw_abstract_v1,[{attribute,1,file,{"./hw.erl",1}},
                  {attribute,1,module,hw},
                  {attribute,2,export,[{go,0}]},
                  {function,4,
                            go,
                            0,
                            [{clause,4,
                                     [],
                                     [],
                                     [{string,5,"this is my function"}]}]},
                  {eof,6}]}

   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.

   If the abstract code is _not_ present in the beam file, the problem
   gets much harder. It is possible to study the remaining information
   and draw some conclusions about what the original .erl file might
   have looked like, for instance which functions were exported. But
   a lot of other important information, such as variable names, is
   not present. In general, recreating the source code from a beam
   file without abstract code is not practical.

----------------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

RE: Decompile beam to erlang

Ulf Wiger (TN/EAB)
In reply to this post by Chaitanya Chalasani

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