gpt4 book ai didi

elixir - 来自 Dbgi chunk 的 pretty-print Elixir 源代码

转载 作者:行者123 更新时间:2023-12-02 09:31:44 24 4
gpt4 key购买 nike

如果我有一个使用 debug_info 从 Erlang 代码编译的 Beam 文件,打印相应的源代码相当容易:

{ok, {_, [{debug_info, {debug_info_v1, erl_abstract_code, AbstractCode}}]}} =
beam_lib:chunks("my_module.beam", [debug_info]).
{ok, Forms} = erl_abstract_code:debug_info(erlang_v1, module_name, AbstractCode, []).
io:format("~s~n", [erl_prettypr:format(erl_syntax:form_list(Forms))]).

但是 Elixir 呢?我可以像这样执行前两个步骤:

{ok, {_, [{debug_info, {debug_info_v1, elixir_erl, AbstractCode}}]}} = 
beam_lib:chunks("Elixir.MyModule.beam", [debug_info]).
{ok, Forms} = elixir_erl:debug_info(elixir_v1, module_name, AbstractCode).

这给出了这种形式的 map :

#{attributes => ...,
compile_opts => [],
definitions => ...,
deprecated => [],
file => <<"my_module.ex">>,
line => 95,
module => 'Elixir.MyModule',
unreachable => []}

如何将其打印为人类可读的 Elixir 代码?

最佳答案

有一个 Visual Studio extension可以将 BEAM 文件解码为 Elixir 源代码:

To activate the extension select "Disassemble BEAM" from the context menu for a .beam file in the Explorer View:

enter image description here

以下引用文献:

1) https://elixirforum.com/t/visual-studio-code-extension-to-view-beam-files/13373/4

2) http://beam-wisdoms.clau.se/en/latest/indepth-beam-file.html

编辑 1:---

ElixirLS是另一个用于调试 Elixir 或 Erlang 代码的工具。

Your compiled .beam modules don’t have the necessary function calls to send these messages. In other languages, you might compile two versions of your binaries, one with the debug calls and one without, but in Elixir, it works a bit differently.

When you compile Erlang or Elixir modules with the :debug_info option set, the resulting .beam files include a chunk with the Erlang Abstract Format representation of your code. Before you can debug a module, you have to “interpret” it by calling :int.ni/1, which reads this chunk and then purges the module. After that, any future calls to the module are handled by evaluating the Erlang abstract forms and making the necessary calls to the meta process after each evaluation.

Calling :int.ni/1 on each module in your project manually is a pain, so when you run a Mix task in the ElixirLS debugger, it automatically interprets all the modules in your project and its dependencies. This is a good default for most projects, though it can cause a noticeable lag in starting the task. Future versions of ElixirLS will likely include more configuration options to specify which modules to interpret.

Note: As a consequence of having to interpret modules prior to debugging, you can’t debug any code that lives outside of a module definition.

重要链接:

1) https://medium.com/@JakeBeckerCode/debugging-elixir-in-vs-code-400e21814614

2) http://blog.plataformatec.com.br/2016/04/debugging-techniques-in-elixir-lang/

3) https://zorbash.com/post/debugging-elixir-applications/#otp-behaviour-tracing

关于elixir - 来自 Dbgi chunk 的 pretty-print Elixir 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com