1beam_lib(3)                Erlang Module Definition                beam_lib(3)
2
3
4

NAME

6       beam_lib - An interface to the BEAM file format.
7

DESCRIPTION

9       This module provides an interface to files created by the BEAM Compiler
10       ("BEAM files"). The format used, a variant of "EA  IFF  1985"  Standard
11       for Interchange Format Files, divides data into chunks.
12
13       Chunk  data  can be returned as binaries or as compound terms. Compound
14       terms are returned when chunks are referenced by names  (atoms)  rather
15       than  identifiers (strings). The recognized names and the corresponding
16       identifiers are as follows:
17
18         * atoms ("Atom")
19
20         * attributes ("Attr")
21
22         * compile_info ("CInf")
23
24         * debug_info ("Dbgi")
25
26         * exports ("ExpT")
27
28         * imports ("ImpT")
29
30         * indexed_imports ("ImpT")
31
32         * labeled_exports ("ExpT")
33
34         * labeled_locals ("LocT")
35
36         * locals ("LocT")
37

DEBUG INFORMATION/ABSTRACT CODE

39       Option debug_info can be specified to the Compiler (see compile(3))  to
40       have  debug  information, such as Erlang Abstract Format, stored in the
41       debug_info chunk. Tools such as Debugger and  Xref  require  the  debug
42       information to be included.
43
44   Warning:
45       Source code can be reconstructed from the debug information. To prevent
46       this, use encrypted debug information (see below).
47
48
49       The debug information  can  also  be  removed  from  BEAM  files  using
50       strip/1, strip_files/1, and/or strip_release/1.
51

RECONSTRUCT SOURCE CODE

53       The  following example shows how to reconstruct Erlang source code from
54       the debug information in a BEAM file Beam:
55
56       {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).
57       io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
58

ENCRYPTED DEBUG INFORMATION

60       The debug information can be encrypted to keep the source code  secret,
61       but still be able to use tools such as Debugger or Xref.
62
63       To  use encrypted debug information, a key must be provided to the com‐
64       piler and beam_lib. The key is specified as a string. It is recommended
65       that the string contains at least 32 characters and that both upper and
66       lower case letters as well as digits and special characters are used.
67
68       The default type (and currently the only type) of crypto  algorithm  is
69       des3_cbc,  three  rounds  of  DES.  The  key  string is scrambled using
70       erlang:md5/1 to generate the keys used for des3_cbc.
71
72   Note:
73       As far as we know by the time of writing, it  is  infeasible  to  break
74       des3_cbc  encryption  without  any  knowledge of the key. Therefore, as
75       long as the key is kept safe and is unguessable,  the  encrypted  debug
76       information should be safe from intruders.
77
78
79       The key can be provided in the following two ways:
80
81         * Use  Compiler option {debug_info_key,Key}, see compile(3) and func‐
82           tion crypto_key_fun/1 to register a fun that returns the key  when‐
83           ever beam_lib must decrypt the debug information.
84
85           If  no  such  fun  is  registered, beam_lib instead searches for an
86           .erlang.crypt file, see the next section.
87
88         * Store the key in a text file named .erlang.crypt.
89
90           In this case, Compiler option encrypt_debug_info can be  used,  see
91           compile(3).
92

.ERLANG.CRYPT

94       beam_lib  searches  for .erlang.crypt in the current directory and then
95       the home directory for the current user. If the file is found and  con‐
96       tains a key, beam_lib implicitly creates a crypto key fun and registers
97       it.
98
99       File .erlang.crypt is to contain a single list of tuples:
100
101       {debug_info, Mode, Module, Key}
102
103       Mode is the type of crypto algorithm; currently, the only allowed value
104       is  des3_cbc.  Module is either an atom, in which case Key is only used
105       for the module Module, or [], in which case Key is used  for  all  mod‐
106       ules. Key is the non-empty key string.
107
108       Key in the first tuple where both Mode and Module match is used.
109
110       The  following  is an example of an .erlang.crypt file that returns the
111       same key for all modules:
112
113       [{debug_info, des3_cbc, [], "%>7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].
114
115       The  following  is  a  slightly  more   complicated   example   of   an
116       .erlang.crypt  providing  one  key for module t and another key for all
117       other modules:
118
119       [{debug_info, des3_cbc, t, "My KEY"},
120        {debug_info, des3_cbc, [], "%>7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].
121
122   Note:
123       Do not use any of the keys in these examples. Use your own keys.
124
125

DATA TYPES

127       beam() = module() | file:filename() | binary()
128
129              Each of the functions described below accept either  the  module
130              name, the filename, or a binary containing the BEAM module.
131
132       chunkdata() =
133           {chunkid(), dataB()} |
134           {abstract_code, abst_code()} |
135           {debug_info, debug_info()} |
136           {attributes, [attrib_entry()]} |
137           {compile_info, [compinfo_entry()]} |
138           {exports, [{atom(), arity()}]} |
139           {labeled_exports, [labeled_entry()]} |
140           {imports, [mfa()]} |
141           {indexed_imports,
142            [{index(), module(), Function :: atom(), arity()}]} |
143           {locals, [{atom(), arity()}]} |
144           {labeled_locals, [labeled_entry()]} |
145           {atoms, [{integer(), atom()}]}
146
147              The   list   of   attributes   is   sorted   on   Attribute  (in
148              attrib_entry()) and each attribute name occurs once in the list.
149              The attribute values occur in the same order as in the file. The
150              lists of functions are also sorted.
151
152       chunkid() = nonempty_string()
153
154              "Attr" | "CInf" | "Dbgi" | "ExpT" | "ImpT" | "LocT" | "AtU8"
155
156       dataB() = binary()
157
158       debug_info() =
159           {DbgiVersion :: atom(), Backend :: module(), Data :: term()} |
160           no_debug_info
161
162              The format stored in the debug_info chunk. To retrieve  particu‐
163              lar    code    representation    from    the    backend,   Back‐
164              end:debug_info(Format, Module, Data, Opts) must be invoked. For‐
165              mat is an atom, such as erlang_v1 for the Erlang Abstract Format
166              or core_v1 for Core Erlang. Module is the module represented  by
167              the  beam  file  and  Data is the value stored in the debug info
168              chunk. Opts is any list of  values  supported  by  the  Backend.
169              Backend:debug_info/4 must return {ok, Code} or {error, Term}.
170
171              Developers  must  always  invoke  the  debug_info/4 function and
172              never rely on the Data stored in the debug_info chunk, as it  is
173              opaque  and  may  change at any moment. no_debug_info means that
174              chunk "Dbgi" is present, but empty.
175
176       abst_code() =
177           {AbstVersion :: atom(), forms()} | no_abstract_code
178
179              It is not checked that the forms conform to the abstract  format
180              indicated  by  AbstVersion.  no_abstract_code  means  that chunk
181              "Abst" is present, but empty.
182
183              For modules compiled with OTP 20 onwards, the abst_code chunk is
184              automatically computed from the debug_info chunk.
185
186       forms() = [erl_parse:abstract_form() | erl_parse:form_info()]
187
188       compinfo_entry() = {InfoKey :: atom(), term()}
189
190       attrib_entry() =
191           {Attribute :: atom(), [AttributeValue :: term()]}
192
193       labeled_entry() = {Function :: atom(), arity(), label()}
194
195       index() = integer() >= 0
196
197       label() = integer()
198
199       chunkref() = chunkname() | chunkid()
200
201       chunkname() =
202           abstract_code |
203           debug_info |
204           attributes |
205           compile_info |
206           exports |
207           labeled_exports |
208           imports |
209           indexed_imports |
210           locals |
211           labeled_locals |
212           atoms
213
214       chnk_rsn() =
215           {unknown_chunk, file:filename(), atom()} |
216           {key_missing_or_invalid,
217            file:filename(),
218            abstract_code | debug_info} |
219           info_rsn()
220
221       info_rsn() =
222           {chunk_too_big,
223            file:filename(),
224            chunkid(),
225            ChunkSize :: integer() >= 0,
226            FileSize :: integer() >= 0} |
227           {invalid_beam_file,
228            file:filename(),
229            Position :: integer() >= 0} |
230           {invalid_chunk, file:filename(), chunkid()} |
231           {missing_chunk, file:filename(), chunkid()} |
232           {not_a_beam_file, file:filename()} |
233           {file_error, file:filename(), file:posix()}
234

EXPORTS

236       all_chunks(File :: beam()) ->
237                     {ok, beam_lib, [{chunkid(), dataB()}]}
238
239              Reads chunk data for all chunks.
240
241       build_module(Chunks) -> {ok, Binary}
242
243              Types:
244
245                 Chunks = [{chunkid(), dataB()}]
246                 Binary = binary()
247
248              Builds a BEAM module (as a binary) from a list of chunks.
249
250       chunks(Beam, ChunkRefs) ->
251                 {ok, {module(), [chunkdata()]}} |
252                 {error, beam_lib, chnk_rsn()}
253
254              Types:
255
256                 Beam = beam()
257                 ChunkRefs = [chunkref()]
258
259              Reads  chunk  data  for selected chunks references. The order of
260              the returned list of chunk data is determined by  the  order  of
261              the list of chunks references.
262
263       chunks(Beam, ChunkRefs, Options) ->
264                 {ok, {module(), [ChunkResult]}} |
265                 {error, beam_lib, chnk_rsn()}
266
267              Types:
268
269                 Beam = beam()
270                 ChunkRefs = [chunkref()]
271                 Options = [allow_missing_chunks]
272                 ChunkResult =
273                     chunkdata() | {ChunkRef :: chunkref(), missing_chunk}
274
275              Reads  chunk  data  for selected chunks references. The order of
276              the returned list of chunk data is determined by  the  order  of
277              the list of chunks references.
278
279              By  default, if any requested chunk is missing in Beam, an error
280              tuple is returned. However, if  option  allow_missing_chunks  is
281              specified,  a  result is returned even if chunks are missing. In
282              the  result  list,  any  missing  chunks  are   represented   as
283              {ChunkRef,missing_chunk}. Notice however that if chunk "Atom" is
284              missing, that is considered a fatal error and the  return  value
285              is an error tuple.
286
287       clear_crypto_key_fun() -> undefined | {ok, Result}
288
289              Types:
290
291                 Result = undefined | term()
292
293              Unregisters  the crypto key fun and terminates the process hold‐
294              ing it, started by crypto_key_fun/1.
295
296              Returns either {ok, undefined} if no crypto key  fun  is  regis‐
297              tered,  or {ok, Term}, where Term is the return value from Cryp‐
298              toKeyFun(clear), see crypto_key_fun/1.
299
300       cmp(Beam1, Beam2) -> ok | {error, beam_lib, cmp_rsn()}
301
302              Types:
303
304                 Beam1 = Beam2 = beam()
305                 cmp_rsn() =
306                     {modules_different, module(), module()} |
307                     {chunks_different, chunkid()} |
308                     different_chunks |
309                     info_rsn()
310
311              Compares the contents of two BEAM files. If the module names are
312              the same, and all chunks except for chunk "CInf" (the chunk con‐
313              taining the compilation information that  is  returned  by  Mod‐
314              ule:module_info(compile))  have the same contents in both files,
315              ok is returned. Otherwise an error message is returned.
316
317       cmp_dirs(Dir1, Dir2) ->
318                   {Only1, Only2, Different} | {error, beam_lib, Reason}
319
320              Types:
321
322                 Dir1 = Dir2 = atom() | file:filename()
323                 Only1 = Only2 = [file:filename()]
324                 Different =
325                     [{Filename1 :: file:filename(), Filename2  ::  file:file‐
326                 name()}]
327                 Reason = {not_a_directory, term()} | info_rsn()
328
329              Compares  the  BEAM  files  in  two directories. Only files with
330              extension ".beam" are compared. BEAM files that  exist  only  in
331              directory  Dir1 (Dir2) are returned in Only1 (Only2). BEAM files
332              that exist in both directories but are considered  different  by
333              cmp/2  are returned as pairs {Filename1, Filename2}, where File‐
334              name1 (Filename2) exists in directory Dir1 (Dir2).
335
336       crypto_key_fun(CryptoKeyFun) -> ok | {error, Reason}
337
338              Types:
339
340                 CryptoKeyFun = crypto_fun()
341                 Reason = badfun | exists | term()
342                 crypto_fun() = fun((crypto_fun_arg()) -> term())
343                 crypto_fun_arg() =
344                     init | clear | {debug_info, mode(), module(), file:filename()}
345                 mode() = des3_cbc
346
347              Registers an unary fun that is called if beam_lib must  read  an
348              debug_info  chunk  that has been encrypted. The fun is held in a
349              process that is started by the function.
350
351              If a fun is already registered when  attempting  to  register  a
352              fun, {error, exists} is returned.
353
354              The fun must handle the following arguments:
355
356              CryptoKeyFun(init) -> ok | {ok, NewCryptoKeyFun} | {error, Term}
357
358              Called when the fun is registered, in the process that holds the
359              fun. Here the crypto key fun can do  any  necessary  initializa‐
360              tions.  If {ok, NewCryptoKeyFun} is returned, NewCryptoKeyFun is
361              registered  instead  of  CryptoKeyFun.  If  {error,   Term}   is
362              returned,  the registration is aborted and crypto_key_fun/1 also
363              returns {error, Term}.
364
365              CryptoKeyFun({debug_info, Mode, Module, Filename}) -> Key
366
367              Called when the key is needed for  module  Module  in  the  file
368              named Filename. Mode is the type of crypto algorithm; currently,
369              the only possible value is des3_cbc. The call is to fail  (raise
370              an exception) if no key is available.
371
372              CryptoKeyFun(clear) -> term()
373
374              Called  before the fun is unregistered. Here any cleaning up can
375              be done. The return value is not important, but is  passed  back
376              to  the  caller  of clear_crypto_key_fun/0 as part of its return
377              value.
378
379       diff_dirs(Dir1, Dir2) -> ok | {error, beam_lib, Reason}
380
381              Types:
382
383                 Dir1 = Dir2 = atom() | file:filename()
384                 Reason = {not_a_directory, term()} | info_rsn()
385
386              Compares the BEAM files in two directories  as  cmp_dirs/2,  but
387              the  names of files that exist in only one directory or are dif‐
388              ferent are presented on standard output.
389
390       format_error(Reason) -> io_lib:chars()
391
392              Types:
393
394                 Reason = term()
395
396              For a specified error returned by any function in  this  module,
397              this  function returns a descriptive string of the error in Eng‐
398              lish. For file errors, function file:format_error(Posix)  is  to
399              be called.
400
401       info(Beam) -> [InfoPair] | {error, beam_lib, info_rsn()}
402
403              Types:
404
405                 Beam = beam()
406                 InfoPair =
407                     {file, Filename :: file:filename()} |
408                     {binary, Binary :: binary()} |
409                     {module, Module :: module()} |
410                     {chunks,
411                      [{ChunkId :: chunkid(),
412                        Pos :: integer() >= 0,
413                        Size :: integer() >= 0}]}
414
415              Returns  a list containing some information about a BEAM file as
416              tuples {Item, Info}:
417
418                {file, Filename} | {binary, Binary}:
419                  The name (string) of the BEAM file, or the binary from which
420                  the information was extracted.
421
422                {module, Module}:
423                  The name (atom) of the module.
424
425                {chunks, [{ChunkId, Pos, Size}]}:
426                  For each chunk, the identifier (string) and the position and
427                  size of the chunk data, in bytes.
428
429       md5(Beam) -> {ok, {module(), MD5}} | {error, beam_lib, chnk_rsn()}
430
431              Types:
432
433                 Beam = beam()
434                 MD5 = binary()
435
436              Calculates an MD5 redundancy check for the code  of  the  module
437              (compilation date and other attributes are not included).
438
439       strip(Beam1) ->
440                {ok, {module(), Beam2}} | {error, beam_lib, info_rsn()}
441
442              Types:
443
444                 Beam1 = Beam2 = beam()
445
446              Removes  all  chunks from a BEAM file except those needed by the
447              loader. In particular, the debug information  (chunk  debug_info
448              and abstract_code) is removed.
449
450       strip_files(Files) ->
451                      {ok, [{module(), Beam}]} |
452                      {error, beam_lib, info_rsn()}
453
454              Types:
455
456                 Files = [beam()]
457                 Beam = beam()
458
459              Removes  all  chunks except those needed by the loader from BEAM
460              files. In particular, the debug  information  (chunk  debug_info
461              and  abstract_code)  is  removed. The returned list contains one
462              element for each specified filename, in the  same  order  as  in
463              Files.
464
465       strip_release(Dir) ->
466                        {ok, [{module(), file:filename()}]} |
467                        {error, beam_lib, Reason}
468
469              Types:
470
471                 Dir = atom() | file:filename()
472                 Reason = {not_a_directory, term()} | info_rsn()
473
474              Removes  all  chunks  except those needed by the loader from the
475              BEAM files of a release. Dir is  to  be  the  installation  root
476              directory.  For example, the current OTP release can be stripped
477              with the call beam_lib:strip_release(code:root_dir()).
478
479       version(Beam) ->
480                  {ok, {module(), [Version :: term()]}} |
481                  {error, beam_lib, chnk_rsn()}
482
483              Types:
484
485                 Beam = beam()
486
487              Returns the module version or versions. A version is defined  by
488              module  attribute -vsn(Vsn). If this attribute is not specified,
489              the version defaults to the checksum of the module. Notice  that
490              if  version  Vsn  is  not  a  list, it is made into one, that is
491              {ok,{Module,[Vsn]}} is returned. If there are many  -vsn  module
492              attributes, the result is the concatenated list of versions.
493
494              Examples:
495
496              1> beam_lib:version(a). % -vsn(1).
497              {ok,{a,[1]}}
498              2> beam_lib:version(b). % -vsn([1]).
499              {ok,{b,[1]}}
500              3> beam_lib:version(c). % -vsn([1]). -vsn(2).
501              {ok,{c,[1,2]}}
502              4> beam_lib:version(d). % no -vsn attribute
503              {ok,{d,[275613208176997377698094100858909383631]}}
504
505
506
507Ericsson AB                     stdlib 3.4.5.1                     beam_lib(3)
Impressum