【问题标题】:How to know if a .beam file is compiled with debug_info?如何知道 .beam 文件是否使用 debug_info 编译?
【发布时间】:2016-07-18 12:27:40
【问题描述】:

我看到我需要编译一个带有debug_info 参数的.erl 文件,以便在调试器中对其进行调试。

当我尝试在调试器中调试.beam文件时,总是看到该文件没有调试信息,无法打开。

** 无效的梁文件或没有抽象代码:“/erlang-debug/myapp.beam”

我怀疑可能是我以错误的方式编译文件。 我尝试了所有可能的方法,但仍然没有运气,我觉得文件是在没有 debug_info 的情况下编译的。

Erlang documentation page 提到了我使用的最简单的例子之一:

% erlc +debug_info module.erl

有没有办法知道某些特定的.beam 文件是否使用 debug_info 编译?

【问题讨论】:

    标签: erlang


    【解决方案1】:

    您可以使用 module_info 函数访问所有编译选项。要对调试信息标志进行测试,您可以使用 proplists 函数提取信息:

    1> O = fun(M) ->               
    1> Comp = M:module_info(compile),      
    1> Options = proplists:get_value(options,Comp),
    1> proplists:get_value(debug_info,Options)     
    1> end.                                        
    #Fun<erl_eval.6.50752066>
    2> c(p564).
    {ok,p564}
    3> O(p564).
    undefined
    4> c(p564,[debug_info]). 
    {ok,p564}
    5> O(p564).             
    true
    6>
    

    【讨论】:

      【解决方案2】:

      一种方法是使用beam_lib:chunks/2 function 检查Beam 文件中是否存在非零大小的抽象代码块。例如,给定一个名为 x.beam 的梁文件,您可以从 Linux/UNIX/OS X shell 执行此检查,如下所示(请注意,$ 是我的 shell 提示符,我将其拆分为多行以使在这里更容易阅读,但您也可以将它们全部放在一行上——无论哪种方式都可以):

      $ erl -noinput -eval 'io:format("~s\n",
      [case beam_lib:chunks(hd(init:get_plain_arguments()), ["Abst"]) of
          {ok,{_,[{"Abst",A}]}} when byte_size(A) /= 0 -> "yes";
          _ -> "no" end])' -s init stop -- x.beam
      

      这将检查 id 为 "Abst" 的块的光束文件,并检查其关联的二进制数据的大小是否非零。如果是,则打印yes,否则打印no

      下面是一个使用例子,我们先用调试信息编译,检查beam文件,然后不使用调试信息编译,再检查一次:

      $ erlc +debug_info x.erl
      $ erl -noinput -eval 'io:format("~s\n",
      [case beam_lib:chunks(hd(init:get_plain_arguments()), ["Abst"]) of
          {ok,{_,[{"Abst",A}]}} when byte_size(A) /= 0 -> "yes";
          _ -> "no" end])' -s init stop -- x.beam
      yes
      $ erlc +no_debug_info x.erl
      $ erl -noinput -eval 'io:format("~s\n",
      [case beam_lib:chunks(hd(init:get_plain_arguments()), ["Abst"]) of
          {ok,{_,[{"Abst",A}]}} when byte_size(A) /= 0 -> "yes";
          _ -> "no" end])' -s init stop -- x.beam
      no
      

      【讨论】:

        猜你喜欢
        • 2022-01-05
        • 1970-01-01
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        相关资源
        最近更新 更多