PROPOSAL: Code.beam_instructions/2

36 views
Skip to first unread message

Devon Estes

unread,
Aug 6, 2018, 3:48:36 AM8/6/18
to elixir-lang-core
As part of debugging an issue in Benchee recently, we needed to look at BEAM instructions for some code to see what exactly was being generated on OTP 20 and OTP 21. I also know of several other people in the community that need to look at the BEAM instructions from time to time, either for debugging purposes or for performance analysis purposes. Each of these people have their own little script or function stored somewhere that generates this code for them. I think, in the spirit of adding things to Elixir Core that help in the development of the language itself, it would be nice to have a function to take care of generating BEAM instructions in Elixir Core.

That's why I am proposing a new function called `Code.beam_instructions/1`. It would accept a string of Elixir code and would return a human readable binary representation of the BEAM instructions for the given code. 

For reference, here's the implementation that I'm currently using to inspect BEAM instructions (which I got from Tobias Peiffer):

```
defmodule ASM do
  def for(code) do
    code = """
    defmodule Test do
      def test do
        #{code}
      end
    end
    """

    [{_, beam}] = Code.compile_string(code)
    {:ok,{_,[{:abstract_code,{_,abstract_code}}]}} = :beam_lib.chunks(beam,[:abstract_code])
    {:ok, _module, asm} = :compile.forms(abstract_code, [:to_asm])
    asm
  end
end
```

and here's a link to Saša Jurić's gist on which that's based: https://gist.github.com/sasa1977/73274c2be733b5321ace

The reason it's helpful to have in Elixir core instead of in a separate file somewhere is that you can drop this function in as a debugging statement in the code you're working on instead of having to pull out the code you're working on and paste it into an IEX session somewhere else. It's a convenience for sure, but it would help make the development experience nicer.

José Valim

unread,
Aug 6, 2018, 4:39:58 AM8/6/18
to elixir-l...@googlegroups.com
What do you think about Michal's decompile package: https://github.com/michalmuskala/decompile

You can install as an archive and use it any project from the command line and it supports multiple formats.





José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b3e26fb3-9034-42d7-bb25-fd04d5686b6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Devon Estes

unread,
Aug 6, 2018, 2:32:08 PM8/6/18
to elixir-lang-core
I didn't know that existed! I mean, I guess that could at least be a starting point for something. Needs some more documentation to make it a little more SEO friendly, though ;)
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages