From source to a running process

In Erlang, the file you write is not the file the runtime runs. Between them sit the compiler, a beam file, the code path and a load step, and knowing the four makes an undefined function or a stale result easy to explain. This lesson follows one function from a text file to the process that calls it.

From source to beam

erlc is a thin driver around the compiler in compile. It reads the .erl file, checks the syntax, the exports and the warnings, and writes one .beam file next to the source, or into the folder -o names. A project keeps its beams in ebin, which the rebar3 lesson explains.

Save a module as greet.erl, make the folder the beams belong in, and compile into it:

greet.erl

-module(greet). -export([greet/0]). greet() -> "Hello".

mkdir -p ebin

erlc -o ebin greet.erl

ls ebin

greet.beam

The beam holds the compiled code, the export table, the module attributes and, with +debug_info, the data the debugger and Dialyzer read. It is not a text file: the runtime reads it directly, and no compiler is needed on the machine that runs it.

What the beam holds

A beam is a container with named sections: the compiled code, the export table, the module attributes, the literals, and with +debug_info the data the debugger and Dialyzer read. The compiler keeps the .erl file out of it: the runtime needs the beam, not the text. This program reads the sections of its own beam:

inspect_beam.erl

-module(inspect_beam). -export([main/0]). main() -> File = code:which(inspect_beam), {ok, {inspect_beam, [{exports, Exports}]}} = beam_lib:chunks(File, [exports]), io:format("beam: ~s~n", [filename:basename(File)]), io:format("exports: ~p~n", [Exports]), io:format("running in a pid: ~p~n", [is_pid(self())]).

erlc inspect_beam.erl

erl -noshell -eval 'inspect_beam:main(), halt().'

beam: inspect_beam.beam exports: [{main,0},{module_info,0},{module_info,1}] running in a pid: true

code:which/1 finds the beam through the code path, beam_lib:chunks/2 reads one section of it, and the export table holds the one function the module exports plus module_info/0 and module_info/1, which the compiler adds to every module for the code server. The last line is the point the next section returns to: the function runs in a process, and that process has a pid.

The code path

A module is found through the code path, the list of folders the loader searches. It starts with the current directory and the ebin folders of the installed OTP libraries. -pa Folder puts a folder in front of the list, which is how a program runs against a build it just wrote, and code:which/1 answers with the file a module would be loaded from.

erl

1> code:is_loaded(mnesia). false 2> code:ensure_loaded(mnesia). {module,mnesia} 3> code:is_loaded(mnesia) =/= false. true 4> filename:basename(code:which(lists)). "lists.beam" 5> is_pid(self()). true

code:ensure_loaded/1 loads a module and its dependencies without starting anything; code:is_loaded/1 then names the beam. code:which/1 answers non_existing for a module the path does not hold, and preloaded for a module compiled into the runtime.

Loading a module

The first call to a module loads it. The loader reads the beam once, puts the code in memory, and every later call uses that copy until the module is loaded again. Two versions may exist at the same time, which is what a hot code upgrade uses: the old version answers the calls that are already running and the new one takes the next.

A function call itself runs in the process that made it. In the program above, main/0 runs in one process, the one the runtime started for -eval; spawn would start another, and the same function would then run there. A process is not a thread of the module: it is a separate stack and mailbox that runs whatever function it was given. That is the subject of Processes and message passing.

When the module is missing

The most common error of a first project is a call to a module that was never compiled into a place the path holds. The function is not found, and the shell reports it as though the module did not exist, because from its side the two cases look the same:

erl

1> hello:main(). ** exception error: undefined function hello:main/0

Three causes cover almost every case: the file was never compiled, the beam is in a folder that is not on the path, or the folder is on the path but the file has another name. erlc refuses a file whose name differs from its module name, so the third one usually means the beam was renamed by hand. code:which(hello) tells the three apart: it answers non_existing until the loader can see the beam, then the file name.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does erlc write from greet.erl?

    Answer

    greet.beam

    The compiler writes one beam file, the bytecode the runtime loads.

  2. What does the beam file contain?

    Answer

    The compiled code and the export table

    The beam is the compiled module: code, exports, attributes, and debug data when +debug_info was given.

  3. What does -pa ebin do?

    Answer

    Puts ebin in front of the code path

    -pa prepends a folder to the code path, so the loader finds the beams of a build folder first.

  4. What does this program print? -module(q3). -export([main/0]). main() -> io:format("~s~n", [filename:basename(code:which(lists))]).

    Answer

    lists.beam

    ~s prints the string without quotes, so the line is lists.beam.

  5. What loads a module that no program has called yet?

    Answer

    code:ensure_loaded/1

    code:ensure_loaded/1 loads the module and its dependencies, and answers {module, Name}.

  6. In which process does main/0 run when erl -noshell -eval calls it?

    Answer

    The process the runtime started for -eval

    A call runs in the process that made it; -eval is run by one process of the node.

  7. A call answers undefined function hello:main/0. Which cause is possible?

    Answer

    The module was compiled into a folder outside the code path

    The loader only sees beams on the code path; a beam elsewhere is invisible, so the function is undefined.

  8. What does this program print in a fresh node? -module(q4). -export([main/0]). main() -> io:format("~p~n", [code:is_loaded(mnesia)]).

    Answer

    false

    Nothing in a fresh node has loaded mnesia, so code:is_loaded/1 answers false.

  9. What does this program print? -module(q1). -export([main/0]). main() -> File = code:which(q1), {ok, {q1, [{exports, Exports}]}} = beam_lib:chunks(File, [exports]), io:format("~p~n", [lists:member({main, 0}, Exports)]).

    Answer

    true

    The export table of the running module holds {main,0}, so lists:member/2 answers true.

  10. What does this program print? -module(q2). -export([main/0]). main() -> io:format("~p~n", [is_pid(self())]).

    Answer

    true

    Every running function has a process, and self() is the pid of the one that called it.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev

Изображение баннера