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() = file:filename() | binary()
128
129              Each of the functions described below accept either the filename
130              (as a string) 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 | debug_info | attributes | compile_info |
203           exports | labeled_exports | imports | indexed_imports |
204           locals | labeled_locals | atoms
205
206       chnk_rsn() =
207           {unknown_chunk, file:filename(), atom()} |
208           {key_missing_or_invalid,
209            file:filename(),
210            abstract_code | debug_info} |
211           info_rsn()
212
213       info_rsn() =
214           {chunk_too_big,
215            file:filename(),
216            chunkid(),
217            ChunkSize :: integer() >= 0,
218            FileSize :: integer() >= 0} |
219           {invalid_beam_file,
220            file:filename(),
221            Position :: integer() >= 0} |
222           {invalid_chunk, file:filename(), chunkid()} |
223           {missing_chunk, file:filename(), chunkid()} |
224           {not_a_beam_file, file:filename()} |
225           {file_error, file:filename(), file:posix()}
226

EXPORTS

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