Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a .beam file is compiled with debug_info?

Tags:

erlang

I see that I need to compile an .erl file withdebug_info parameter to make it possible to debug it in the debugger.

When I try to debug a .beam file in the debugger, I always see that the file has no debug information and can not be opened.

** Invalid beam file or no abstract code: "/erlang-debug/myapp.beam"

I suspect it can be that I compile the files in a wrong way. I tried all the possible way but still no luck and I feel like files are compiled without debug_info.

One of the simplest examples I used is mentioned on Erlang documentation page:

% erlc +debug_info module.erl

Is there a way to know if some specific .beam file is compiled with debug_info or not?

like image 399
Sergei Basharov Avatar asked Dec 24 '22 03:12

Sergei Basharov


2 Answers

You have access to all the compilation option using the module_info function. To make a test on the debug info flag, you can use the proplists function to extract the information:

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>
like image 77
Pascal Avatar answered Jan 12 '23 15:01

Pascal


One way is to use the beam_lib:chunks/2 function to check the beam file for an abstract code chunk of non-zero size. For example, given a beam file named x.beam, you can perform this check from a Linux/UNIX/OS X shell as shown below (note that the $ is my shell prompt, and I broke this across multiple lines to make it easier to read here but you could put it all on a single line as well — it works either way):

$ 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

This examines the beam file for a chunk with the id "Abst" and checks that its associated binary data is of non-zero size. If so, it prints yes, else it prints no.

Below is an example of using it, where we first compile with debug info, check the beam file, then compile without debug info, and check it again:

$ 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
like image 41
Steve Vinoski Avatar answered Jan 12 '23 13:01

Steve Vinoski