简体   繁体   English

来自Dbgi chunk的Pretty-print Elixir源代码

[英]Pretty-print Elixir source code from Dbgi chunk

If I have a beam file compiled from Erlang code with debug_info , printing the corresponding source code is fairly easy: 如果我有一个使用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))]).

But what about Elixir? 但是Elixir怎么样? I can do the first two steps like this: 我可以像这样做前两个步骤:

{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).

This gives a map of this form: 这给出了这种形式的地图:

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

How do I print that as human-readable Elixir code? 如何将其打印为人类可读的Elixir代码?

There is a Visual Studio extension that can de-code the BEAM file into Elixir Source Code: 有一个Visual Studio 扩展可以将BEAM文件解码为Elixir源代码:

To activate the extension select "Disassemble BEAM" from the context menu for a .beam file in the Explorer View: 要激活扩展,请在资源管理器视图中从.beam文件的上下文菜单中选择“Disassemble BEAM”:

在此输入图像描述

Following Refs: 以下参考:

1) https://elixirforum.com/t/visual-studio-code-extension-to-view-beam-files/13373/4 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 2) http://beam-wisdoms.clau.se/en/latest/indepth-beam-file.html

Edit 1:--- 编辑1:---

ElixirLS is another tool for debugging Elixir or Erlang code. ElixirLS是另一种调试Elixir或Erlang代码的工具。

Your compiled .beam modules don't have the necessary function calls to send these messages. 您编译的.beam模块没有必要的函数调用来发送这些消息。 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. 在其他语言中,您可以编译两个版本的二进制文件,一个使用调试调用,另一个没有,但在Elixir中,它的工作方式略有不同。

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. 当您使用:debug_info选项集编译Erlang或Elixir模块时,生成的.beam文件包含一个带有代码的Erlang抽象格式表示的块。 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. 在调试模块之前,必须通过调用:int.ni/1来“解释”它,它会读取此块然后清除模块。 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. 之后,通过评估Erlang抽象表单并在每次评估后对元进程进行必要的调用来处理对模块的任何未来调用。

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. 手动调用:int.ni/1中每个模块的:int.ni/1 ,因此当您在ElixirLS调试器中运行Mix任务时,它会自动解释项目中的所有模块及其依赖项。 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. ElixirLS的未来版本可能包含更多配置选项,以指定要解释的模块。

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. 注意:由于必须在调试之前解释模块,因此无法调试任何位于模块定义之外的代码。

Important Links: 重要链接:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM