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